itree.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:10k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  *  linux/fs/sysv/itree.c
  3.  *
  4.  *  Handling of indirect blocks' trees.
  5.  *  AV, Sep--Dec 2000
  6.  */
  7. #include <linux/fs.h>
  8. #include <linux/sysv_fs.h>
  9. #include <linux/locks.h>
  10. #include <linux/smp_lock.h>
  11. enum {DIRECT = 10, DEPTH = 4}; /* Have triple indirect */
  12. static inline void dirty_indirect(struct buffer_head *bh, struct inode *inode)
  13. {
  14. mark_buffer_dirty_inode(bh, inode);
  15. if (IS_SYNC(inode)) {
  16. ll_rw_block (WRITE, 1, &bh);
  17. wait_on_buffer (bh);
  18. }
  19. }
  20. static int block_to_path(struct inode *inode, long block, int offsets[DEPTH])
  21. {
  22. struct super_block *sb = inode->i_sb;
  23. int ptrs_bits = sb->sv_ind_per_block_bits;
  24. unsigned long indirect_blocks = sb->sv_ind_per_block,
  25. double_blocks = sb->sv_ind_per_block_2;
  26. int n = 0;
  27. if (block < 0) {
  28. printk("sysv_block_map: block < 0n");
  29. } else if (block < DIRECT) {
  30. offsets[n++] = block;
  31. } else if ( (block -= DIRECT) < indirect_blocks) {
  32. offsets[n++] = DIRECT;
  33. offsets[n++] = block;
  34. } else if ((block -= indirect_blocks) < double_blocks) {
  35. offsets[n++] = DIRECT+1;
  36. offsets[n++] = block >> ptrs_bits;
  37. offsets[n++] = block & (indirect_blocks - 1);
  38. } else if (((block -= double_blocks) >> (ptrs_bits * 2)) < indirect_blocks) {
  39. offsets[n++] = DIRECT+2;
  40. offsets[n++] = block >> (ptrs_bits * 2);
  41. offsets[n++] = (block >> ptrs_bits) & (indirect_blocks - 1);
  42. offsets[n++] = block & (indirect_blocks - 1);
  43. } else {
  44. /* nothing */;
  45. }
  46. return n;
  47. }
  48. static inline int block_to_cpu(struct super_block *sb, u32 nr)
  49. {
  50. return sb->sv_block_base + fs32_to_cpu(sb, nr);
  51. }
  52. typedef struct {
  53. u32     *p;
  54. u32     key;
  55. struct buffer_head *bh;
  56. } Indirect;
  57. static inline void add_chain(Indirect *p, struct buffer_head *bh, u32 *v)
  58. {
  59. p->key = *(p->p = v);
  60. p->bh = bh;
  61. }
  62. static inline int verify_chain(Indirect *from, Indirect *to)
  63. {
  64. while (from <= to && from->key == *from->p)
  65. from++;
  66. return (from > to);
  67. }
  68. static inline u32 *block_end(struct buffer_head *bh)
  69. {
  70. return (u32*)((char*)bh->b_data + bh->b_size);
  71. }
  72. static Indirect *get_branch(struct inode *inode,
  73.     int depth,
  74.     int offsets[],
  75.     Indirect chain[],
  76.     int *err)
  77. {
  78. struct super_block *sb = inode->i_sb;
  79. Indirect *p = chain;
  80. struct buffer_head *bh;
  81. *err = 0;
  82. add_chain (chain, NULL, inode->u.sysv_i.i_data + *offsets);
  83. if (!p->key)
  84. goto no_block;
  85. while (--depth) {
  86. int block = block_to_cpu(sb, p->key);
  87. bh = sb_bread(sb, block);
  88. if (!bh)
  89. goto failure;
  90. if (!verify_chain(chain, p))
  91. goto changed;
  92. add_chain(++p, bh, (u32*)bh->b_data + *++offsets);
  93. if (!p->key)
  94. goto no_block;
  95. }
  96. return NULL;
  97. changed:
  98. *err = -EAGAIN;
  99. goto no_block;
  100. failure:
  101. *err = -EIO;
  102. no_block:
  103. return p;
  104. }
  105. static int alloc_branch(struct inode *inode,
  106. int num,
  107. int *offsets,
  108. Indirect *branch)
  109. {
  110. int blocksize = inode->i_sb->s_blocksize;
  111. int n = 0;
  112. int i;
  113. branch[0].key = sysv_new_block(inode->i_sb);
  114. if (branch[0].key) for (n = 1; n < num; n++) {
  115. struct buffer_head *bh;
  116. int parent;
  117. /* Allocate the next block */
  118. branch[n].key = sysv_new_block(inode->i_sb);
  119. if (!branch[n].key)
  120. break;
  121. /*
  122.  * Get buffer_head for parent block, zero it out and set 
  123.  * the pointer to new one, then send parent to disk.
  124.  */
  125. parent = block_to_cpu(inode->i_sb, branch[n-1].key);
  126. bh = sb_getblk(inode->i_sb, parent);
  127. lock_buffer(bh);
  128. memset(bh->b_data, 0, blocksize);
  129. branch[n].bh = bh;
  130. branch[n].p = (u32*) bh->b_data + offsets[n];
  131. *branch[n].p = branch[n].key;
  132. mark_buffer_uptodate(bh, 1);
  133. unlock_buffer(bh);
  134. dirty_indirect(bh, inode);
  135. }
  136. if (n == num)
  137. return 0;
  138. /* Allocation failed, free what we already allocated */
  139. for (i = 1; i < n; i++)
  140. bforget(branch[i].bh);
  141. for (i = 0; i < n; i++)
  142. sysv_free_block(inode->i_sb, branch[i].key);
  143. return -ENOSPC;
  144. }
  145. static inline int splice_branch(struct inode *inode,
  146. Indirect chain[],
  147. Indirect *where,
  148. int num)
  149. {
  150. int i;
  151. /* Verify that place we are splicing to is still there and vacant */
  152. if (!verify_chain(chain, where-1) || *where->p)
  153. goto changed;
  154. *where->p = where->key;
  155. inode->i_ctime = CURRENT_TIME;
  156. /* had we spliced it onto indirect block? */
  157. if (where->bh)
  158. dirty_indirect(where->bh, inode);
  159. if (IS_SYNC(inode))
  160. sysv_sync_inode(inode);
  161. else
  162. mark_inode_dirty(inode);
  163. return 0;
  164. changed:
  165. for (i = 1; i < num; i++)
  166. bforget(where[i].bh);
  167. for (i = 0; i < num; i++)
  168. sysv_free_block(inode->i_sb, where[i].key);
  169. return -EAGAIN;
  170. }
  171. static int get_block(struct inode *inode, long iblock, struct buffer_head *bh_result, int create)
  172. {
  173. int err = -EIO;
  174. int offsets[DEPTH];
  175. Indirect chain[DEPTH];
  176. struct super_block *sb = inode->i_sb;
  177. Indirect *partial;
  178. int left;
  179. int depth = block_to_path(inode, iblock, offsets);
  180. if (depth == 0)
  181. goto out;
  182. lock_kernel();
  183. reread:
  184. partial = get_branch(inode, depth, offsets, chain, &err);
  185. /* Simplest case - block found, no allocation needed */
  186. if (!partial) {
  187. got_it:
  188. bh_result->b_dev = sb->s_dev;
  189. bh_result->b_blocknr = block_to_cpu(sb, chain[depth-1].key);
  190. bh_result->b_state |= (1UL << BH_Mapped);
  191. /* Clean up and exit */
  192. partial = chain+depth-1; /* the whole chain */
  193. goto cleanup;
  194. }
  195. /* Next simple case - plain lookup or failed read of indirect block */
  196. if (!create || err == -EIO) {
  197. cleanup:
  198. while (partial > chain) {
  199. brelse(partial->bh);
  200. partial--;
  201. }
  202. unlock_kernel();
  203. out:
  204. return err;
  205. }
  206. /*
  207.  * Indirect block might be removed by truncate while we were
  208.  * reading it. Handling of that case (forget what we've got and
  209.  * reread) is taken out of the main path.
  210.  */
  211. if (err == -EAGAIN)
  212. goto changed;
  213. left = (chain + depth) - partial;
  214. err = alloc_branch(inode, left, offsets+(partial-chain), partial);
  215. if (err)
  216. goto cleanup;
  217. if (splice_branch(inode, chain, partial, left) < 0)
  218. goto changed;
  219. bh_result->b_state |= (1UL << BH_New);
  220. goto got_it;
  221. changed:
  222. while (partial > chain) {
  223. brelse(partial->bh);
  224. partial--;
  225. }
  226. goto reread;
  227. }
  228. static inline int all_zeroes(u32 *p, u32 *q)
  229. {
  230. while (p < q)
  231. if (*p++)
  232. return 0;
  233. return 1;
  234. }
  235. static Indirect *find_shared(struct inode *inode,
  236. int depth,
  237. int offsets[],
  238. Indirect chain[],
  239. u32 *top)
  240. {
  241. Indirect *partial, *p;
  242. int k, err;
  243. *top = 0;
  244. for (k = depth; k > 1 && !offsets[k-1]; k--)
  245. ;
  246. partial = get_branch(inode, k, offsets, chain, &err);
  247. if (!partial)
  248. partial = chain + k-1;
  249. /*
  250.  * If the branch acquired continuation since we've looked at it -
  251.  * fine, it should all survive and (new) top doesn't belong to us.
  252.  */
  253. if (!partial->key && *partial->p)
  254. goto no_top;
  255. for (p=partial; p>chain && all_zeroes((u32*)p->bh->b_data,p->p); p--)
  256. ;
  257. /*
  258.  * OK, we've found the last block that must survive. The rest of our
  259.  * branch should be detached before unlocking. However, if that rest
  260.  * of branch is all ours and does not grow immediately from the inode
  261.  * it's easier to cheat and just decrement partial->p.
  262.  */
  263. if (p == chain + k - 1 && p > chain) {
  264. p->p--;
  265. } else {
  266. *top = *p->p;
  267. *p->p = 0;
  268. }
  269. while(partial > p) {
  270. brelse(partial->bh);
  271. partial--;
  272. }
  273. no_top:
  274. return partial;
  275. }
  276. static inline void free_data(struct inode *inode, u32 *p, u32 *q)
  277. {
  278. for ( ; p < q ; p++) {
  279. u32 nr = *p;
  280. if (nr) {
  281. *p = 0;
  282. sysv_free_block(inode->i_sb, nr);
  283. mark_inode_dirty(inode);
  284. }
  285. }
  286. }
  287. static void free_branches(struct inode *inode, u32 *p, u32 *q, int depth)
  288. {
  289. struct buffer_head * bh;
  290. struct super_block *sb = inode->i_sb;
  291. if (depth--) {
  292. for ( ; p < q ; p++) {
  293. int block;
  294. u32 nr = *p;
  295. if (!nr)
  296. continue;
  297. *p = 0;
  298. block = block_to_cpu(sb, nr);
  299. bh = sb_bread(sb, block);
  300. if (!bh)
  301. continue;
  302. free_branches(inode, (u32*)bh->b_data,
  303. block_end(bh), depth);
  304. bforget(bh);
  305. sysv_free_block(sb, nr);
  306. mark_inode_dirty(inode);
  307. }
  308. } else
  309. free_data(inode, p, q);
  310. }
  311. void sysv_truncate (struct inode * inode)
  312. {
  313. u32 *i_data = inode->u.sysv_i.i_data;
  314. int offsets[DEPTH];
  315. Indirect chain[DEPTH];
  316. Indirect *partial;
  317. int nr = 0;
  318. int n;
  319. long iblock;
  320. unsigned blocksize;
  321. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  322.     S_ISLNK(inode->i_mode)))
  323. return;
  324. blocksize = inode->i_sb->s_blocksize;
  325. iblock = (inode->i_size + blocksize-1)
  326. >> inode->i_sb->s_blocksize_bits;
  327. block_truncate_page(inode->i_mapping, inode->i_size, get_block);
  328. n = block_to_path(inode, iblock, offsets);
  329. if (n == 0)
  330. return;
  331. if (n == 1) {
  332. free_data(inode, i_data+offsets[0], i_data + DIRECT);
  333. goto do_indirects;
  334. }
  335. partial = find_shared(inode, n, offsets, chain, &nr);
  336. /* Kill the top of shared branch (already detached) */
  337. if (nr) {
  338. if (partial == chain)
  339. mark_inode_dirty(inode);
  340. else
  341. dirty_indirect(partial->bh, inode);
  342. free_branches(inode, &nr, &nr+1, (chain+n-1) - partial);
  343. }
  344. /* Clear the ends of indirect blocks on the shared branch */
  345. while (partial > chain) {
  346. free_branches(inode, partial->p + 1, block_end(partial->bh),
  347. (chain+n-1) - partial);
  348. dirty_indirect(partial->bh, inode);
  349. brelse (partial->bh);
  350. partial--;
  351. }
  352. do_indirects:
  353. /* Kill the remaining (whole) subtrees (== subtrees deeper than...) */
  354. while (n < DEPTH) {
  355. nr = i_data[DIRECT + n - 1];
  356. if (nr) {
  357. i_data[DIRECT + n - 1] = 0;
  358. mark_inode_dirty(inode);
  359. free_branches(inode, &nr, &nr+1, n);
  360. }
  361. n++;
  362. }
  363. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  364. if (IS_SYNC(inode))
  365. sysv_sync_inode (inode);
  366. else
  367. mark_inode_dirty(inode);
  368. }
  369. static int sysv_writepage(struct page *page)
  370. {
  371. return block_write_full_page(page,get_block);
  372. }
  373. static int sysv_readpage(struct file *file, struct page *page)
  374. {
  375. return block_read_full_page(page,get_block);
  376. }
  377. static int sysv_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to)
  378. {
  379. return block_prepare_write(page,from,to,get_block);
  380. }
  381. static int sysv_bmap(struct address_space *mapping, long block)
  382. {
  383. return generic_block_bmap(mapping,block,get_block);
  384. }
  385. struct address_space_operations sysv_aops = {
  386. readpage: sysv_readpage,
  387. writepage: sysv_writepage,
  388. sync_page: block_sync_page,
  389. prepare_write: sysv_prepare_write,
  390. commit_write: generic_commit_write,
  391. bmap: sysv_bmap
  392. };