1 // SPDX-License-Identifier: GPL-2.0
3 * linux/fs/sysv/itree.c
5 * Handling of indirect blocks' trees.
9 #include <linux/buffer_head.h>
10 #include <linux/mount.h>
11 #include <linux/mpage.h>
12 #include <linux/string.h>
15 enum {DIRECT = 10, DEPTH = 4}; /* Have triple indirect */
17 static inline void dirty_indirect(struct buffer_head *bh, struct inode *inode)
19 mark_buffer_dirty_inode(bh, inode);
21 sync_dirty_buffer(bh);
24 static int block_to_path(struct inode *inode, long block, int offsets[DEPTH])
26 struct super_block *sb = inode->i_sb;
27 struct sysv_sb_info *sbi = SYSV_SB(sb);
28 int ptrs_bits = sbi->s_ind_per_block_bits;
29 unsigned long indirect_blocks = sbi->s_ind_per_block,
30 double_blocks = sbi->s_ind_per_block_2;
34 printk("sysv_block_map: block < 0\n");
35 } else if (block < DIRECT) {
37 } else if ( (block -= DIRECT) < indirect_blocks) {
38 offsets[n++] = DIRECT;
40 } else if ((block -= indirect_blocks) < double_blocks) {
41 offsets[n++] = DIRECT+1;
42 offsets[n++] = block >> ptrs_bits;
43 offsets[n++] = block & (indirect_blocks - 1);
44 } else if (((block -= double_blocks) >> (ptrs_bits * 2)) < indirect_blocks) {
45 offsets[n++] = DIRECT+2;
46 offsets[n++] = block >> (ptrs_bits * 2);
47 offsets[n++] = (block >> ptrs_bits) & (indirect_blocks - 1);
48 offsets[n++] = block & (indirect_blocks - 1);
55 static inline int block_to_cpu(struct sysv_sb_info *sbi, sysv_zone_t nr)
57 return sbi->s_block_base + fs32_to_cpu(sbi, nr);
63 struct buffer_head *bh;
66 static DEFINE_RWLOCK(pointers_lock);
68 static inline void add_chain(Indirect *p, struct buffer_head *bh, sysv_zone_t *v)
74 static inline int verify_chain(Indirect *from, Indirect *to)
76 while (from <= to && from->key == *from->p)
81 static inline sysv_zone_t *block_end(struct buffer_head *bh)
83 return (sysv_zone_t*)((char*)bh->b_data + bh->b_size);
87 * Requires read_lock(&pointers_lock) or write_lock(&pointers_lock)
89 static Indirect *get_branch(struct inode *inode,
95 struct super_block *sb = inode->i_sb;
97 struct buffer_head *bh;
100 add_chain(chain, NULL, SYSV_I(inode)->i_data + *offsets);
104 int block = block_to_cpu(SYSV_SB(sb), p->key);
105 bh = sb_bread(sb, block);
108 if (!verify_chain(chain, p))
110 add_chain(++p, bh, (sysv_zone_t*)bh->b_data + *++offsets);
126 static int alloc_branch(struct inode *inode,
131 int blocksize = inode->i_sb->s_blocksize;
135 branch[0].key = sysv_new_block(inode->i_sb);
136 if (branch[0].key) for (n = 1; n < num; n++) {
137 struct buffer_head *bh;
139 /* Allocate the next block */
140 branch[n].key = sysv_new_block(inode->i_sb);
144 * Get buffer_head for parent block, zero it out and set
145 * the pointer to new one, then send parent to disk.
147 parent = block_to_cpu(SYSV_SB(inode->i_sb), branch[n-1].key);
148 bh = sb_getblk(inode->i_sb, parent);
150 sysv_free_block(inode->i_sb, branch[n].key);
154 memset(bh->b_data, 0, blocksize);
156 branch[n].p = (sysv_zone_t*) bh->b_data + offsets[n];
157 *branch[n].p = branch[n].key;
158 set_buffer_uptodate(bh);
160 dirty_indirect(bh, inode);
165 /* Allocation failed, free what we already allocated */
166 for (i = 1; i < n; i++)
167 bforget(branch[i].bh);
168 for (i = 0; i < n; i++)
169 sysv_free_block(inode->i_sb, branch[i].key);
173 static inline int splice_branch(struct inode *inode,
180 /* Verify that place we are splicing to is still there and vacant */
181 write_lock(&pointers_lock);
182 if (!verify_chain(chain, where-1) || *where->p)
184 *where->p = where->key;
185 write_unlock(&pointers_lock);
187 inode_set_ctime_current(inode);
189 /* had we spliced it onto indirect block? */
191 dirty_indirect(where->bh, inode);
194 sysv_sync_inode(inode);
196 mark_inode_dirty(inode);
200 write_unlock(&pointers_lock);
201 for (i = 1; i < num; i++)
202 bforget(where[i].bh);
203 for (i = 0; i < num; i++)
204 sysv_free_block(inode->i_sb, where[i].key);
208 static int get_block(struct inode *inode, sector_t iblock, struct buffer_head *bh_result, int create)
212 Indirect chain[DEPTH];
213 struct super_block *sb = inode->i_sb;
216 int depth = block_to_path(inode, iblock, offsets);
222 read_lock(&pointers_lock);
223 partial = get_branch(inode, depth, offsets, chain, &err);
224 read_unlock(&pointers_lock);
226 /* Simplest case - block found, no allocation needed */
229 map_bh(bh_result, sb, block_to_cpu(SYSV_SB(sb),
230 chain[depth-1].key));
231 /* Clean up and exit */
232 partial = chain+depth-1; /* the whole chain */
236 /* Next simple case - plain lookup or failed read of indirect block */
237 if (!create || err == -EIO) {
239 while (partial > chain) {
248 * Indirect block might be removed by truncate while we were
249 * reading it. Handling of that case (forget what we've got and
250 * reread) is taken out of the main path.
255 left = (chain + depth) - partial;
256 err = alloc_branch(inode, left, offsets+(partial-chain), partial);
260 if (splice_branch(inode, chain, partial, left) < 0)
263 set_buffer_new(bh_result);
267 while (partial > chain) {
274 static inline int all_zeroes(sysv_zone_t *p, sysv_zone_t *q)
282 static Indirect *find_shared(struct inode *inode,
288 Indirect *partial, *p;
292 for (k = depth; k > 1 && !offsets[k-1]; k--)
295 write_lock(&pointers_lock);
296 partial = get_branch(inode, k, offsets, chain, &err);
298 partial = chain + k-1;
300 * If the branch acquired continuation since we've looked at it -
301 * fine, it should all survive and (new) top doesn't belong to us.
303 if (!partial->key && *partial->p) {
304 write_unlock(&pointers_lock);
307 for (p=partial; p>chain && all_zeroes((sysv_zone_t*)p->bh->b_data,p->p); p--)
310 * OK, we've found the last block that must survive. The rest of our
311 * branch should be detached before unlocking. However, if that rest
312 * of branch is all ours and does not grow immediately from the inode
313 * it's easier to cheat and just decrement partial->p.
315 if (p == chain + k - 1 && p > chain) {
321 write_unlock(&pointers_lock);
323 while (partial > p) {
331 static inline void free_data(struct inode *inode, sysv_zone_t *p, sysv_zone_t *q)
333 for ( ; p < q ; p++) {
337 sysv_free_block(inode->i_sb, nr);
338 mark_inode_dirty(inode);
343 static void free_branches(struct inode *inode, sysv_zone_t *p, sysv_zone_t *q, int depth)
345 struct buffer_head * bh;
346 struct super_block *sb = inode->i_sb;
349 for ( ; p < q ; p++) {
355 block = block_to_cpu(SYSV_SB(sb), nr);
356 bh = sb_bread(sb, block);
359 free_branches(inode, (sysv_zone_t*)bh->b_data,
360 block_end(bh), depth);
362 sysv_free_block(sb, nr);
363 mark_inode_dirty(inode);
366 free_data(inode, p, q);
369 void sysv_truncate (struct inode * inode)
371 sysv_zone_t *i_data = SYSV_I(inode)->i_data;
373 Indirect chain[DEPTH];
380 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
381 S_ISLNK(inode->i_mode)))
384 blocksize = inode->i_sb->s_blocksize;
385 iblock = (inode->i_size + blocksize-1)
386 >> inode->i_sb->s_blocksize_bits;
388 block_truncate_page(inode->i_mapping, inode->i_size, get_block);
390 n = block_to_path(inode, iblock, offsets);
395 free_data(inode, i_data+offsets[0], i_data + DIRECT);
399 partial = find_shared(inode, n, offsets, chain, &nr);
400 /* Kill the top of shared branch (already detached) */
402 if (partial == chain)
403 mark_inode_dirty(inode);
405 dirty_indirect(partial->bh, inode);
406 free_branches(inode, &nr, &nr+1, (chain+n-1) - partial);
408 /* Clear the ends of indirect blocks on the shared branch */
409 while (partial > chain) {
410 free_branches(inode, partial->p + 1, block_end(partial->bh),
411 (chain+n-1) - partial);
412 dirty_indirect(partial->bh, inode);
413 brelse (partial->bh);
417 /* Kill the remaining (whole) subtrees (== subtrees deeper than...) */
419 nr = i_data[DIRECT + n - 1];
421 i_data[DIRECT + n - 1] = 0;
422 mark_inode_dirty(inode);
423 free_branches(inode, &nr, &nr+1, n);
427 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
429 sysv_sync_inode (inode);
431 mark_inode_dirty(inode);
434 static unsigned sysv_nblocks(struct super_block *s, loff_t size)
436 struct sysv_sb_info *sbi = SYSV_SB(s);
437 int ptrs_bits = sbi->s_ind_per_block_bits;
438 unsigned blocks, res, direct = DIRECT, i = DEPTH;
439 blocks = (size + s->s_blocksize - 1) >> s->s_blocksize_bits;
441 while (--i && blocks > direct) {
442 blocks = ((blocks - direct - 1) >> ptrs_bits) + 1;
449 int sysv_getattr(struct mnt_idmap *idmap, const struct path *path,
450 struct kstat *stat, u32 request_mask, unsigned int flags)
452 struct super_block *s = path->dentry->d_sb;
453 generic_fillattr(&nop_mnt_idmap, request_mask, d_inode(path->dentry),
455 stat->blocks = (s->s_blocksize / 512) * sysv_nblocks(s, stat->size);
456 stat->blksize = s->s_blocksize;
460 static int sysv_writepages(struct address_space *mapping,
461 struct writeback_control *wbc)
463 return mpage_writepages(mapping, wbc, get_block);
466 static int sysv_read_folio(struct file *file, struct folio *folio)
468 return block_read_full_folio(folio, get_block);
471 int sysv_prepare_chunk(struct page *page, loff_t pos, unsigned len)
473 return __block_write_begin(page, pos, len, get_block);
476 static void sysv_write_failed(struct address_space *mapping, loff_t to)
478 struct inode *inode = mapping->host;
480 if (to > inode->i_size) {
481 truncate_pagecache(inode, inode->i_size);
482 sysv_truncate(inode);
486 static int sysv_write_begin(struct file *file, struct address_space *mapping,
487 loff_t pos, unsigned len,
488 struct page **pagep, void **fsdata)
492 ret = block_write_begin(mapping, pos, len, pagep, get_block);
494 sysv_write_failed(mapping, pos + len);
499 static sector_t sysv_bmap(struct address_space *mapping, sector_t block)
501 return generic_block_bmap(mapping,block,get_block);
504 const struct address_space_operations sysv_aops = {
505 .dirty_folio = block_dirty_folio,
506 .invalidate_folio = block_invalidate_folio,
507 .read_folio = sysv_read_folio,
508 .writepages = sysv_writepages,
509 .write_begin = sysv_write_begin,
510 .write_end = generic_write_end,
511 .migrate_folio = buffer_migrate_folio,