itree.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/sysv/itree.c
  4. *
  5. * Handling of indirect blocks' trees.
  6. * AV, Sep--Dec 2000
  7. */
  8. #include <linux/buffer_head.h>
  9. #include <linux/mount.h>
  10. #include <linux/mpage.h>
  11. #include <linux/string.h>
  12. #include "sysv.h"
  13. enum {DIRECT = 10, DEPTH = 4}; /* Have triple indirect */
  14. static inline void dirty_indirect(struct buffer_head *bh, struct inode *inode)
  15. {
  16. mark_buffer_dirty_inode(bh, inode);
  17. if (IS_SYNC(inode))
  18. sync_dirty_buffer(bh);
  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. struct sysv_sb_info *sbi = SYSV_SB(sb);
  24. int ptrs_bits = sbi->s_ind_per_block_bits;
  25. unsigned long indirect_blocks = sbi->s_ind_per_block,
  26. double_blocks = sbi->s_ind_per_block_2;
  27. int n = 0;
  28. if (block < 0) {
  29. printk("sysv_block_map: block < 0\n");
  30. } else if (block < DIRECT) {
  31. offsets[n++] = block;
  32. } else if ( (block -= DIRECT) < indirect_blocks) {
  33. offsets[n++] = DIRECT;
  34. offsets[n++] = block;
  35. } else if ((block -= indirect_blocks) < double_blocks) {
  36. offsets[n++] = DIRECT+1;
  37. offsets[n++] = block >> ptrs_bits;
  38. offsets[n++] = block & (indirect_blocks - 1);
  39. } else if (((block -= double_blocks) >> (ptrs_bits * 2)) < indirect_blocks) {
  40. offsets[n++] = DIRECT+2;
  41. offsets[n++] = block >> (ptrs_bits * 2);
  42. offsets[n++] = (block >> ptrs_bits) & (indirect_blocks - 1);
  43. offsets[n++] = block & (indirect_blocks - 1);
  44. } else {
  45. /* nothing */;
  46. }
  47. return n;
  48. }
  49. static inline int block_to_cpu(struct sysv_sb_info *sbi, sysv_zone_t nr)
  50. {
  51. return sbi->s_block_base + fs32_to_cpu(sbi, nr);
  52. }
  53. typedef struct {
  54. sysv_zone_t *p;
  55. sysv_zone_t key;
  56. struct buffer_head *bh;
  57. } Indirect;
  58. static DEFINE_RWLOCK(pointers_lock);
  59. static inline void add_chain(Indirect *p, struct buffer_head *bh, sysv_zone_t *v)
  60. {
  61. p->key = *(p->p = v);
  62. p->bh = bh;
  63. }
  64. static inline int verify_chain(Indirect *from, Indirect *to)
  65. {
  66. while (from <= to && from->key == *from->p)
  67. from++;
  68. return (from > to);
  69. }
  70. static inline sysv_zone_t *block_end(struct buffer_head *bh)
  71. {
  72. return (sysv_zone_t*)((char*)bh->b_data + bh->b_size);
  73. }
  74. static Indirect *get_branch(struct inode *inode,
  75. int depth,
  76. int offsets[],
  77. Indirect chain[],
  78. int *err)
  79. {
  80. struct super_block *sb = inode->i_sb;
  81. Indirect *p = chain;
  82. struct buffer_head *bh;
  83. *err = 0;
  84. add_chain(chain, NULL, SYSV_I(inode)->i_data + *offsets);
  85. if (!p->key)
  86. goto no_block;
  87. while (--depth) {
  88. int block = block_to_cpu(SYSV_SB(sb), p->key);
  89. bh = sb_bread(sb, block);
  90. if (!bh)
  91. goto failure;
  92. read_lock(&pointers_lock);
  93. if (!verify_chain(chain, p))
  94. goto changed;
  95. add_chain(++p, bh, (sysv_zone_t*)bh->b_data + *++offsets);
  96. read_unlock(&pointers_lock);
  97. if (!p->key)
  98. goto no_block;
  99. }
  100. return NULL;
  101. changed:
  102. read_unlock(&pointers_lock);
  103. brelse(bh);
  104. *err = -EAGAIN;
  105. goto no_block;
  106. failure:
  107. *err = -EIO;
  108. no_block:
  109. return p;
  110. }
  111. static int alloc_branch(struct inode *inode,
  112. int num,
  113. int *offsets,
  114. Indirect *branch)
  115. {
  116. int blocksize = inode->i_sb->s_blocksize;
  117. int n = 0;
  118. int i;
  119. branch[0].key = sysv_new_block(inode->i_sb);
  120. if (branch[0].key) for (n = 1; n < num; n++) {
  121. struct buffer_head *bh;
  122. int parent;
  123. /* Allocate the next block */
  124. branch[n].key = sysv_new_block(inode->i_sb);
  125. if (!branch[n].key)
  126. break;
  127. /*
  128. * Get buffer_head for parent block, zero it out and set
  129. * the pointer to new one, then send parent to disk.
  130. */
  131. parent = block_to_cpu(SYSV_SB(inode->i_sb), branch[n-1].key);
  132. bh = sb_getblk(inode->i_sb, parent);
  133. if (!bh) {
  134. sysv_free_block(inode->i_sb, branch[n].key);
  135. break;
  136. }
  137. lock_buffer(bh);
  138. memset(bh->b_data, 0, blocksize);
  139. branch[n].bh = bh;
  140. branch[n].p = (sysv_zone_t*) bh->b_data + offsets[n];
  141. *branch[n].p = branch[n].key;
  142. set_buffer_uptodate(bh);
  143. unlock_buffer(bh);
  144. dirty_indirect(bh, inode);
  145. }
  146. if (n == num)
  147. return 0;
  148. /* Allocation failed, free what we already allocated */
  149. for (i = 1; i < n; i++)
  150. bforget(branch[i].bh);
  151. for (i = 0; i < n; i++)
  152. sysv_free_block(inode->i_sb, branch[i].key);
  153. return -ENOSPC;
  154. }
  155. static inline int splice_branch(struct inode *inode,
  156. Indirect chain[],
  157. Indirect *where,
  158. int num)
  159. {
  160. int i;
  161. /* Verify that place we are splicing to is still there and vacant */
  162. write_lock(&pointers_lock);
  163. if (!verify_chain(chain, where-1) || *where->p)
  164. goto changed;
  165. *where->p = where->key;
  166. write_unlock(&pointers_lock);
  167. inode_set_ctime_current(inode);
  168. /* had we spliced it onto indirect block? */
  169. if (where->bh)
  170. dirty_indirect(where->bh, inode);
  171. if (IS_SYNC(inode))
  172. sysv_sync_inode(inode);
  173. else
  174. mark_inode_dirty(inode);
  175. return 0;
  176. changed:
  177. write_unlock(&pointers_lock);
  178. for (i = 1; i < num; i++)
  179. bforget(where[i].bh);
  180. for (i = 0; i < num; i++)
  181. sysv_free_block(inode->i_sb, where[i].key);
  182. return -EAGAIN;
  183. }
  184. static int get_block(struct inode *inode, sector_t iblock, struct buffer_head *bh_result, int create)
  185. {
  186. int err = -EIO;
  187. int offsets[DEPTH];
  188. Indirect chain[DEPTH];
  189. struct super_block *sb = inode->i_sb;
  190. Indirect *partial;
  191. int left;
  192. int depth = block_to_path(inode, iblock, offsets);
  193. if (depth == 0)
  194. goto out;
  195. reread:
  196. partial = get_branch(inode, depth, offsets, chain, &err);
  197. /* Simplest case - block found, no allocation needed */
  198. if (!partial) {
  199. got_it:
  200. map_bh(bh_result, sb, block_to_cpu(SYSV_SB(sb),
  201. chain[depth-1].key));
  202. /* Clean up and exit */
  203. partial = chain+depth-1; /* the whole chain */
  204. goto cleanup;
  205. }
  206. /* Next simple case - plain lookup or failed read of indirect block */
  207. if (!create || err == -EIO) {
  208. cleanup:
  209. while (partial > chain) {
  210. brelse(partial->bh);
  211. partial--;
  212. }
  213. out:
  214. return err;
  215. }
  216. /*
  217. * Indirect block might be removed by truncate while we were
  218. * reading it. Handling of that case (forget what we've got and
  219. * reread) is taken out of the main path.
  220. */
  221. if (err == -EAGAIN)
  222. goto changed;
  223. left = (chain + depth) - partial;
  224. err = alloc_branch(inode, left, offsets+(partial-chain), partial);
  225. if (err)
  226. goto cleanup;
  227. if (splice_branch(inode, chain, partial, left) < 0)
  228. goto changed;
  229. set_buffer_new(bh_result);
  230. goto got_it;
  231. changed:
  232. while (partial > chain) {
  233. brelse(partial->bh);
  234. partial--;
  235. }
  236. goto reread;
  237. }
  238. static inline int all_zeroes(sysv_zone_t *p, sysv_zone_t *q)
  239. {
  240. while (p < q)
  241. if (*p++)
  242. return 0;
  243. return 1;
  244. }
  245. static Indirect *find_shared(struct inode *inode,
  246. int depth,
  247. int offsets[],
  248. Indirect chain[],
  249. sysv_zone_t *top)
  250. {
  251. Indirect *partial, *p;
  252. int k, err;
  253. *top = 0;
  254. for (k = depth; k > 1 && !offsets[k-1]; k--)
  255. ;
  256. partial = get_branch(inode, k, offsets, chain, &err);
  257. write_lock(&pointers_lock);
  258. if (!partial)
  259. partial = chain + k-1;
  260. /*
  261. * If the branch acquired continuation since we've looked at it -
  262. * fine, it should all survive and (new) top doesn't belong to us.
  263. */
  264. if (!partial->key && *partial->p) {
  265. write_unlock(&pointers_lock);
  266. goto no_top;
  267. }
  268. for (p=partial; p>chain && all_zeroes((sysv_zone_t*)p->bh->b_data,p->p); p--)
  269. ;
  270. /*
  271. * OK, we've found the last block that must survive. The rest of our
  272. * branch should be detached before unlocking. However, if that rest
  273. * of branch is all ours and does not grow immediately from the inode
  274. * it's easier to cheat and just decrement partial->p.
  275. */
  276. if (p == chain + k - 1 && p > chain) {
  277. p->p--;
  278. } else {
  279. *top = *p->p;
  280. *p->p = 0;
  281. }
  282. write_unlock(&pointers_lock);
  283. while (partial > p) {
  284. brelse(partial->bh);
  285. partial--;
  286. }
  287. no_top:
  288. return partial;
  289. }
  290. static inline void free_data(struct inode *inode, sysv_zone_t *p, sysv_zone_t *q)
  291. {
  292. for ( ; p < q ; p++) {
  293. sysv_zone_t nr = *p;
  294. if (nr) {
  295. *p = 0;
  296. sysv_free_block(inode->i_sb, nr);
  297. mark_inode_dirty(inode);
  298. }
  299. }
  300. }
  301. static void free_branches(struct inode *inode, sysv_zone_t *p, sysv_zone_t *q, int depth)
  302. {
  303. struct buffer_head * bh;
  304. struct super_block *sb = inode->i_sb;
  305. if (depth--) {
  306. for ( ; p < q ; p++) {
  307. int block;
  308. sysv_zone_t nr = *p;
  309. if (!nr)
  310. continue;
  311. *p = 0;
  312. block = block_to_cpu(SYSV_SB(sb), nr);
  313. bh = sb_bread(sb, block);
  314. if (!bh)
  315. continue;
  316. free_branches(inode, (sysv_zone_t*)bh->b_data,
  317. block_end(bh), depth);
  318. bforget(bh);
  319. sysv_free_block(sb, nr);
  320. mark_inode_dirty(inode);
  321. }
  322. } else
  323. free_data(inode, p, q);
  324. }
  325. void sysv_truncate (struct inode * inode)
  326. {
  327. sysv_zone_t *i_data = SYSV_I(inode)->i_data;
  328. int offsets[DEPTH];
  329. Indirect chain[DEPTH];
  330. Indirect *partial;
  331. sysv_zone_t nr = 0;
  332. int n;
  333. long iblock;
  334. unsigned blocksize;
  335. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  336. S_ISLNK(inode->i_mode)))
  337. return;
  338. blocksize = inode->i_sb->s_blocksize;
  339. iblock = (inode->i_size + blocksize-1)
  340. >> inode->i_sb->s_blocksize_bits;
  341. block_truncate_page(inode->i_mapping, inode->i_size, get_block);
  342. n = block_to_path(inode, iblock, offsets);
  343. if (n == 0)
  344. return;
  345. if (n == 1) {
  346. free_data(inode, i_data+offsets[0], i_data + DIRECT);
  347. goto do_indirects;
  348. }
  349. partial = find_shared(inode, n, offsets, chain, &nr);
  350. /* Kill the top of shared branch (already detached) */
  351. if (nr) {
  352. if (partial == chain)
  353. mark_inode_dirty(inode);
  354. else
  355. dirty_indirect(partial->bh, inode);
  356. free_branches(inode, &nr, &nr+1, (chain+n-1) - partial);
  357. }
  358. /* Clear the ends of indirect blocks on the shared branch */
  359. while (partial > chain) {
  360. free_branches(inode, partial->p + 1, block_end(partial->bh),
  361. (chain+n-1) - partial);
  362. dirty_indirect(partial->bh, inode);
  363. brelse (partial->bh);
  364. partial--;
  365. }
  366. do_indirects:
  367. /* Kill the remaining (whole) subtrees (== subtrees deeper than...) */
  368. while (n < DEPTH) {
  369. nr = i_data[DIRECT + n - 1];
  370. if (nr) {
  371. i_data[DIRECT + n - 1] = 0;
  372. mark_inode_dirty(inode);
  373. free_branches(inode, &nr, &nr+1, n);
  374. }
  375. n++;
  376. }
  377. inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
  378. if (IS_SYNC(inode))
  379. sysv_sync_inode (inode);
  380. else
  381. mark_inode_dirty(inode);
  382. }
  383. static unsigned sysv_nblocks(struct super_block *s, loff_t size)
  384. {
  385. struct sysv_sb_info *sbi = SYSV_SB(s);
  386. int ptrs_bits = sbi->s_ind_per_block_bits;
  387. unsigned blocks, res, direct = DIRECT, i = DEPTH;
  388. blocks = (size + s->s_blocksize - 1) >> s->s_blocksize_bits;
  389. res = blocks;
  390. while (--i && blocks > direct) {
  391. blocks = ((blocks - direct - 1) >> ptrs_bits) + 1;
  392. res += blocks;
  393. direct = 1;
  394. }
  395. return res;
  396. }
  397. int sysv_getattr(struct mnt_idmap *idmap, const struct path *path,
  398. struct kstat *stat, u32 request_mask, unsigned int flags)
  399. {
  400. struct super_block *s = path->dentry->d_sb;
  401. generic_fillattr(&nop_mnt_idmap, request_mask, d_inode(path->dentry),
  402. stat);
  403. stat->blocks = (s->s_blocksize / 512) * sysv_nblocks(s, stat->size);
  404. stat->blksize = s->s_blocksize;
  405. return 0;
  406. }
  407. static int sysv_writepages(struct address_space *mapping,
  408. struct writeback_control *wbc)
  409. {
  410. return mpage_writepages(mapping, wbc, get_block);
  411. }
  412. static int sysv_read_folio(struct file *file, struct folio *folio)
  413. {
  414. return block_read_full_folio(folio, get_block);
  415. }
  416. int sysv_prepare_chunk(struct folio *folio, loff_t pos, unsigned len)
  417. {
  418. return __block_write_begin(folio, pos, len, get_block);
  419. }
  420. static void sysv_write_failed(struct address_space *mapping, loff_t to)
  421. {
  422. struct inode *inode = mapping->host;
  423. if (to > inode->i_size) {
  424. truncate_pagecache(inode, inode->i_size);
  425. sysv_truncate(inode);
  426. }
  427. }
  428. static int sysv_write_begin(struct file *file, struct address_space *mapping,
  429. loff_t pos, unsigned len,
  430. struct folio **foliop, void **fsdata)
  431. {
  432. int ret;
  433. ret = block_write_begin(mapping, pos, len, foliop, get_block);
  434. if (unlikely(ret))
  435. sysv_write_failed(mapping, pos + len);
  436. return ret;
  437. }
  438. static sector_t sysv_bmap(struct address_space *mapping, sector_t block)
  439. {
  440. return generic_block_bmap(mapping,block,get_block);
  441. }
  442. const struct address_space_operations sysv_aops = {
  443. .dirty_folio = block_dirty_folio,
  444. .invalidate_folio = block_invalidate_folio,
  445. .read_folio = sysv_read_folio,
  446. .writepages = sysv_writepages,
  447. .write_begin = sysv_write_begin,
  448. .write_end = generic_write_end,
  449. .migrate_folio = buffer_migrate_folio,
  450. .bmap = sysv_bmap
  451. };