itree_common.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /* Generic part */
  2. typedef struct {
  3. block_t *p;
  4. block_t key;
  5. struct buffer_head *bh;
  6. } Indirect;
  7. static DEFINE_RWLOCK(pointers_lock);
  8. static inline void add_chain(Indirect *p, struct buffer_head *bh, block_t *v)
  9. {
  10. p->key = *(p->p = v);
  11. p->bh = bh;
  12. }
  13. static inline int verify_chain(Indirect *from, Indirect *to)
  14. {
  15. while (from <= to && from->key == *from->p)
  16. from++;
  17. return (from > to);
  18. }
  19. static inline block_t *block_end(struct buffer_head *bh)
  20. {
  21. return (block_t *)((char*)bh->b_data + bh->b_size);
  22. }
  23. static inline Indirect *get_branch(struct inode *inode,
  24. int depth,
  25. int *offsets,
  26. Indirect chain[DEPTH],
  27. int *err)
  28. {
  29. struct super_block *sb = inode->i_sb;
  30. Indirect *p = chain;
  31. struct buffer_head *bh;
  32. *err = 0;
  33. /* i_data is not going away, no lock needed */
  34. add_chain (chain, NULL, i_data(inode) + *offsets);
  35. if (!p->key)
  36. goto no_block;
  37. while (--depth) {
  38. bh = sb_bread(sb, block_to_cpu(p->key));
  39. if (!bh)
  40. goto failure;
  41. read_lock(&pointers_lock);
  42. if (!verify_chain(chain, p))
  43. goto changed;
  44. add_chain(++p, bh, (block_t *)bh->b_data + *++offsets);
  45. read_unlock(&pointers_lock);
  46. if (!p->key)
  47. goto no_block;
  48. }
  49. return NULL;
  50. changed:
  51. read_unlock(&pointers_lock);
  52. brelse(bh);
  53. *err = -EAGAIN;
  54. goto no_block;
  55. failure:
  56. *err = -EIO;
  57. no_block:
  58. return p;
  59. }
  60. static int alloc_branch(struct inode *inode,
  61. int num,
  62. int *offsets,
  63. Indirect *branch)
  64. {
  65. int n = 0;
  66. int i;
  67. int parent = minix_new_block(inode);
  68. branch[0].key = cpu_to_block(parent);
  69. if (parent) for (n = 1; n < num; n++) {
  70. struct buffer_head *bh;
  71. /* Allocate the next block */
  72. int nr = minix_new_block(inode);
  73. if (!nr)
  74. break;
  75. branch[n].key = cpu_to_block(nr);
  76. bh = sb_getblk(inode->i_sb, parent);
  77. lock_buffer(bh);
  78. memset(bh->b_data, 0, bh->b_size);
  79. branch[n].bh = bh;
  80. branch[n].p = (block_t*) bh->b_data + offsets[n];
  81. *branch[n].p = branch[n].key;
  82. set_buffer_uptodate(bh);
  83. unlock_buffer(bh);
  84. mark_buffer_dirty_inode(bh, inode);
  85. parent = nr;
  86. }
  87. if (n == num)
  88. return 0;
  89. /* Allocation failed, free what we already allocated */
  90. for (i = 1; i < n; i++)
  91. bforget(branch[i].bh);
  92. for (i = 0; i < n; i++)
  93. minix_free_block(inode, block_to_cpu(branch[i].key));
  94. return -ENOSPC;
  95. }
  96. static inline int splice_branch(struct inode *inode,
  97. Indirect chain[DEPTH],
  98. Indirect *where,
  99. int num)
  100. {
  101. int i;
  102. write_lock(&pointers_lock);
  103. /* Verify that place we are splicing to is still there and vacant */
  104. if (!verify_chain(chain, where-1) || *where->p)
  105. goto changed;
  106. *where->p = where->key;
  107. write_unlock(&pointers_lock);
  108. /* We are done with atomic stuff, now do the rest of housekeeping */
  109. inode->i_ctime = current_time(inode);
  110. /* had we spliced it onto indirect block? */
  111. if (where->bh)
  112. mark_buffer_dirty_inode(where->bh, inode);
  113. mark_inode_dirty(inode);
  114. return 0;
  115. changed:
  116. write_unlock(&pointers_lock);
  117. for (i = 1; i < num; i++)
  118. bforget(where[i].bh);
  119. for (i = 0; i < num; i++)
  120. minix_free_block(inode, block_to_cpu(where[i].key));
  121. return -EAGAIN;
  122. }
  123. static inline int get_block(struct inode * inode, sector_t block,
  124. struct buffer_head *bh, int create)
  125. {
  126. int err = -EIO;
  127. int offsets[DEPTH];
  128. Indirect chain[DEPTH];
  129. Indirect *partial;
  130. int left;
  131. int depth = block_to_path(inode, block, offsets);
  132. if (depth == 0)
  133. goto out;
  134. reread:
  135. partial = get_branch(inode, depth, offsets, chain, &err);
  136. /* Simplest case - block found, no allocation needed */
  137. if (!partial) {
  138. got_it:
  139. map_bh(bh, inode->i_sb, block_to_cpu(chain[depth-1].key));
  140. /* Clean up and exit */
  141. partial = chain+depth-1; /* the whole chain */
  142. goto cleanup;
  143. }
  144. /* Next simple case - plain lookup or failed read of indirect block */
  145. if (!create || err == -EIO) {
  146. cleanup:
  147. while (partial > chain) {
  148. brelse(partial->bh);
  149. partial--;
  150. }
  151. out:
  152. return err;
  153. }
  154. /*
  155. * Indirect block might be removed by truncate while we were
  156. * reading it. Handling of that case (forget what we've got and
  157. * reread) is taken out of the main path.
  158. */
  159. if (err == -EAGAIN)
  160. goto changed;
  161. left = (chain + depth) - partial;
  162. err = alloc_branch(inode, left, offsets+(partial-chain), partial);
  163. if (err)
  164. goto cleanup;
  165. if (splice_branch(inode, chain, partial, left) < 0)
  166. goto changed;
  167. set_buffer_new(bh);
  168. goto got_it;
  169. changed:
  170. while (partial > chain) {
  171. brelse(partial->bh);
  172. partial--;
  173. }
  174. goto reread;
  175. }
  176. static inline int all_zeroes(block_t *p, block_t *q)
  177. {
  178. while (p < q)
  179. if (*p++)
  180. return 0;
  181. return 1;
  182. }
  183. static Indirect *find_shared(struct inode *inode,
  184. int depth,
  185. int offsets[DEPTH],
  186. Indirect chain[DEPTH],
  187. block_t *top)
  188. {
  189. Indirect *partial, *p;
  190. int k, err;
  191. *top = 0;
  192. for (k = depth; k > 1 && !offsets[k-1]; k--)
  193. ;
  194. partial = get_branch(inode, k, offsets, chain, &err);
  195. write_lock(&pointers_lock);
  196. if (!partial)
  197. partial = chain + k-1;
  198. if (!partial->key && *partial->p) {
  199. write_unlock(&pointers_lock);
  200. goto no_top;
  201. }
  202. for (p=partial;p>chain && all_zeroes((block_t*)p->bh->b_data,p->p);p--)
  203. ;
  204. if (p == chain + k - 1 && p > chain) {
  205. p->p--;
  206. } else {
  207. *top = *p->p;
  208. *p->p = 0;
  209. }
  210. write_unlock(&pointers_lock);
  211. while(partial > p)
  212. {
  213. brelse(partial->bh);
  214. partial--;
  215. }
  216. no_top:
  217. return partial;
  218. }
  219. static inline void free_data(struct inode *inode, block_t *p, block_t *q)
  220. {
  221. unsigned long nr;
  222. for ( ; p < q ; p++) {
  223. nr = block_to_cpu(*p);
  224. if (nr) {
  225. *p = 0;
  226. minix_free_block(inode, nr);
  227. }
  228. }
  229. }
  230. static void free_branches(struct inode *inode, block_t *p, block_t *q, int depth)
  231. {
  232. struct buffer_head * bh;
  233. unsigned long nr;
  234. if (depth--) {
  235. for ( ; p < q ; p++) {
  236. nr = block_to_cpu(*p);
  237. if (!nr)
  238. continue;
  239. *p = 0;
  240. bh = sb_bread(inode->i_sb, nr);
  241. if (!bh)
  242. continue;
  243. free_branches(inode, (block_t*)bh->b_data,
  244. block_end(bh), depth);
  245. bforget(bh);
  246. minix_free_block(inode, nr);
  247. mark_inode_dirty(inode);
  248. }
  249. } else
  250. free_data(inode, p, q);
  251. }
  252. static inline void truncate (struct inode * inode)
  253. {
  254. struct super_block *sb = inode->i_sb;
  255. block_t *idata = i_data(inode);
  256. int offsets[DEPTH];
  257. Indirect chain[DEPTH];
  258. Indirect *partial;
  259. block_t nr = 0;
  260. int n;
  261. int first_whole;
  262. long iblock;
  263. iblock = (inode->i_size + sb->s_blocksize -1) >> sb->s_blocksize_bits;
  264. block_truncate_page(inode->i_mapping, inode->i_size, get_block);
  265. n = block_to_path(inode, iblock, offsets);
  266. if (!n)
  267. return;
  268. if (n == 1) {
  269. free_data(inode, idata+offsets[0], idata + DIRECT);
  270. first_whole = 0;
  271. goto do_indirects;
  272. }
  273. first_whole = offsets[0] + 1 - DIRECT;
  274. partial = find_shared(inode, n, offsets, chain, &nr);
  275. if (nr) {
  276. if (partial == chain)
  277. mark_inode_dirty(inode);
  278. else
  279. mark_buffer_dirty_inode(partial->bh, inode);
  280. free_branches(inode, &nr, &nr+1, (chain+n-1) - partial);
  281. }
  282. /* Clear the ends of indirect blocks on the shared branch */
  283. while (partial > chain) {
  284. free_branches(inode, partial->p + 1, block_end(partial->bh),
  285. (chain+n-1) - partial);
  286. mark_buffer_dirty_inode(partial->bh, inode);
  287. brelse (partial->bh);
  288. partial--;
  289. }
  290. do_indirects:
  291. /* Kill the remaining (whole) subtrees */
  292. while (first_whole < DEPTH-1) {
  293. nr = idata[DIRECT+first_whole];
  294. if (nr) {
  295. idata[DIRECT+first_whole] = 0;
  296. mark_inode_dirty(inode);
  297. free_branches(inode, &nr, &nr+1, first_whole+1);
  298. }
  299. first_whole++;
  300. }
  301. inode->i_mtime = inode->i_ctime = current_time(inode);
  302. mark_inode_dirty(inode);
  303. }
  304. static inline unsigned nblocks(loff_t size, struct super_block *sb)
  305. {
  306. int k = sb->s_blocksize_bits - 10;
  307. unsigned blocks, res, direct = DIRECT, i = DEPTH;
  308. blocks = (size + sb->s_blocksize - 1) >> (BLOCK_SIZE_BITS + k);
  309. res = blocks;
  310. while (--i && blocks > direct) {
  311. blocks -= direct;
  312. blocks += sb->s_blocksize/sizeof(block_t) - 1;
  313. blocks /= sb->s_blocksize/sizeof(block_t);
  314. res += blocks;
  315. direct = 1;
  316. }
  317. return res;
  318. }