mworm.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "all.h"
  10. /*
  11. * multiple cat devices
  12. */
  13. void
  14. mcatinit(Device *d)
  15. {
  16. Device *x, **list;
  17. d->cat.ndev = 0;
  18. for(x=d->cat.first; x; x=x->link) {
  19. devinit(x);
  20. d->cat.ndev++;
  21. }
  22. list = malloc(d->cat.ndev*sizeof(Device*));
  23. d->private = list;
  24. for(x=d->cat.first; x; x=x->link) {
  25. *list++ = x;
  26. x->size = devsize(x);
  27. }
  28. }
  29. Devsize
  30. mcatsize(Device *d)
  31. {
  32. Device *x;
  33. Devsize l, m;
  34. l = 0;
  35. for(x=d->cat.first; x; x=x->link) {
  36. m = x->size;
  37. if(m == 0) {
  38. m = devsize(x);
  39. x->size = m;
  40. }
  41. l += m;
  42. }
  43. return l;
  44. }
  45. int
  46. mcatread(Device *d, Off b, void *c)
  47. {
  48. Device *x;
  49. Devsize l, m;
  50. l = 0;
  51. for(x=d->cat.first; x; x=x->link) {
  52. m = x->size;
  53. if(m == 0) {
  54. m = devsize(x);
  55. x->size = m;
  56. }
  57. if(b < l+m)
  58. return devread(x, b-l, c);
  59. l += m;
  60. }
  61. print("mcatread past end: %Z block %lld, %lld beyond end\n",
  62. d, (Wideoff)b, (Wideoff)l);
  63. return 1;
  64. }
  65. int
  66. mcatwrite(Device *d, Off b, void *c)
  67. {
  68. Device *x;
  69. Devsize l, m;
  70. l = 0;
  71. for(x=d->cat.first; x; x=x->link) {
  72. m = x->size;
  73. if(m == 0) {
  74. m = devsize(x);
  75. x->size = m;
  76. }
  77. if(b < l+m)
  78. return devwrite(x, b-l, c);
  79. l += m;
  80. }
  81. print("mcatwrite past end: %Z block %lld, %lld beyond end\n",
  82. d, (Wideoff)b, (Wideoff)l);
  83. return 1;
  84. }
  85. /*
  86. * multiple interleave devices
  87. */
  88. void
  89. mlevinit(Device *d)
  90. {
  91. Device *x;
  92. mcatinit(d);
  93. for(x=d->cat.first; x; x=x->link)
  94. x->size = devsize(x);
  95. }
  96. Devsize
  97. mlevsize(Device *d)
  98. {
  99. Device *x;
  100. int n;
  101. Devsize m, min;
  102. min = 0;
  103. n = 0;
  104. for(x=d->cat.first; x; x=x->link) {
  105. m = x->size;
  106. if(m == 0) {
  107. m = devsize(x);
  108. x->size = m;
  109. }
  110. if(min == 0 || m < min)
  111. min = m;
  112. n++;
  113. }
  114. return n * min;
  115. }
  116. int
  117. mlevread(Device *d, Off b, void *c)
  118. {
  119. int n;
  120. Device **list;
  121. n = d->cat.ndev;
  122. list = d->private;
  123. return devread(list[b%n], b/n, c);
  124. }
  125. int
  126. mlevwrite(Device *d, Off b, void *c)
  127. {
  128. int n;
  129. Device **list;
  130. n = d->cat.ndev;
  131. list = d->private;
  132. return devwrite(list[b%n], b/n, c);
  133. }
  134. /*
  135. * partition device
  136. */
  137. void
  138. partinit(Device *d)
  139. {
  140. devinit(d->part.d);
  141. d->part.d->size = devsize(d->part.d);
  142. }
  143. Devsize
  144. partsize(Device *d)
  145. {
  146. Devsize size, l;
  147. l = d->part.d->size / 100;
  148. size = d->part.size * l;
  149. if(size == 0)
  150. size = l*100;
  151. return size;
  152. }
  153. int
  154. partread(Device *d, Off b, void *c)
  155. {
  156. Devsize base, size, l;
  157. l = d->part.d->size / 100;
  158. base = d->part.base * l;
  159. size = d->part.size * l;
  160. if(size == 0)
  161. size = l*100;
  162. if(b < size)
  163. return devread(d->part.d, base+b, c);
  164. print("partread past end: %Z blk %lld size %lld\n",
  165. d, (Wideoff)b, (Wideoff)size);
  166. return 1;
  167. }
  168. int
  169. partwrite(Device *d, Off b, void *c)
  170. {
  171. Devsize base, size, l;
  172. l = d->part.d->size / 100;
  173. base = d->part.base * l;
  174. size = d->part.size * l;
  175. if(size == 0)
  176. size = l*100;
  177. if(b < size)
  178. return devwrite(d->part.d, base+b, c);
  179. print("partwrite past end: %Z blk %lld size %lld\n",
  180. d, (Wideoff)b, (Wideoff)size);
  181. return 1;
  182. }
  183. /*
  184. * mirror device
  185. */
  186. void
  187. mirrinit(Device *d)
  188. {
  189. Device *x;
  190. mcatinit(d);
  191. for(x=d->cat.first; x; x=x->link)
  192. x->size = devsize(x);
  193. }
  194. Devsize
  195. mirrsize(Device *d)
  196. {
  197. Device *x;
  198. int n;
  199. Devsize m, min;
  200. min = 0;
  201. n = 0;
  202. for(x=d->cat.first; x; x=x->link) {
  203. m = x->size;
  204. if(m == 0) {
  205. m = devsize(x);
  206. x->size = m;
  207. }
  208. if(min == 0 || m < min)
  209. min = m;
  210. n++;
  211. }
  212. return min;
  213. }
  214. int
  215. mirrread(Device *d, Off b, void *c)
  216. {
  217. Device *x;
  218. if (d->cat.first == nil) {
  219. print("mirrread: empty mirror %Z\n", d);
  220. return 1;
  221. }
  222. for(x=d->cat.first; x; x=x->link) {
  223. if(x->size == 0)
  224. x->size = devsize(x);
  225. if (devread(x, b, c) == 0) /* okay? */
  226. return 0;
  227. }
  228. // DANGER WILL ROBINSON
  229. print("mirrread: all mirrors of %Z block %lld are bad\n",
  230. d, (Wideoff)b);
  231. return 1;
  232. }
  233. /*
  234. * write the mirror(s) first so that a power outage, for example, will
  235. * find the main device written only if the mirrors are too, thus
  236. * checking the main device will also correctly check the mirror(s).
  237. *
  238. * devread and devwrite are synchronous; all buffering must be
  239. * implemented at higher levels.
  240. */
  241. static int
  242. ewrite(Device *x, Off b, void *c)
  243. {
  244. if(x->size == 0)
  245. x->size = devsize(x);
  246. if (devwrite(x, b, c) != 0) {
  247. print("mirrwrite: error at %Z block %lld\n", x, (Wideoff)b);
  248. return 1;
  249. }
  250. return 0;
  251. }
  252. static int
  253. wrmirrs1st(Device *x, Off b, void *c) // write any mirrors of x, then x
  254. {
  255. int e;
  256. if (x == nil)
  257. return 0;
  258. e = wrmirrs1st(x->link, b, c);
  259. return e | ewrite(x, b, c);
  260. }
  261. int
  262. mirrwrite(Device *d, Off b, void *c)
  263. {
  264. if (d->cat.first == nil) {
  265. print("mirrwrite: empty mirror %Z\n", d);
  266. return 1;
  267. }
  268. return wrmirrs1st(d->cat.first, b, c);
  269. }