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/string.h>
14 enum {DIRECT = 10, DEPTH = 4}; /* Have triple indirect */
16 static inline void dirty_indirect(struct buffer_head *bh, struct inode *inode)
18 mark_buffer_dirty_inode(bh, inode);
20 sync_dirty_buffer(bh);
23 static int block_to_path(struct inode *inode, long block, int offsets[DEPTH])
25 struct super_block *sb = inode->i_sb;
26 struct sysv_sb_info *sbi = SYSV_SB(sb);
27 int ptrs_bits = sbi->s_ind_per_block_bits;
28 unsigned long indirect_blocks = sbi->s_ind_per_block,
29 double_blocks = sbi->s_ind_per_block_2;
33 printk("sysv_block_map: block < 0\n");
34 } else if (block < DIRECT) {
36 } else if ( (block -= DIRECT) < indirect_blocks) {
37 offsets[n++] = DIRECT;
39 } else if ((block -= indirect_blocks) < double_blocks) {
40 offsets[n++] = DIRECT+1;
41 offsets[n++] = block >> ptrs_bits;
42 offsets[n++] = block & (indirect_blocks - 1);
43 } else if (((block -= double_blocks) >> (ptrs_bits * 2)) < indirect_blocks) {
44 offsets[n++] = DIRECT+2;
45 offsets[n++] = block >> (ptrs_bits * 2);
46 offsets[n++] = (block >> ptrs_bits) & (indirect_blocks - 1);
47 offsets[n++] = block & (indirect_blocks - 1);
54 static inline int block_to_cpu(struct sysv_sb_info *sbi, sysv_zone_t nr)
56 return sbi->s_block_base + fs32_to_cpu(sbi, nr);
62 struct buffer_head *bh;
65 static DEFINE_RWLOCK(pointers_lock);
67 static inline void add_chain(Indirect *p, struct buffer_head *bh, sysv_zone_t *v)
73 static inline int verify_chain(Indirect *from, Indirect *to)
75 while (from <= to && from->key == *from->p)
80 static inline sysv_zone_t *block_end(struct buffer_head *bh)
82 return (sysv_zone_t*)((char*)bh->b_data + bh->b_size);
86 * Requires read_lock(&pointers_lock) or write_lock(&pointers_lock)
88 static Indirect *get_branch(struct inode *inode,
94 struct super_block *sb = inode->i_sb;
96 struct buffer_head *bh;
99 add_chain(chain, NULL, SYSV_I(inode)->i_data + *offsets);
103 int block = block_to_cpu(SYSV_SB(sb), p->key);
104 bh = sb_bread(sb, block);
107 if (!verify_chain(chain, p))
109 add_chain(++p, bh, (sysv_zone_t*)bh->b_data + *++offsets);
125 static int alloc_branch(struct inode *inode,
130 int blocksize = inode->i_sb->s_blocksize;
134 branch[0].key = sysv_new_block(inode->i_sb);
135 if (branch[0].key) for (n = 1; n < num; n++) {
136 struct buffer_head *bh;
138 /* Allocate the next block */
139 branch[n].key = sysv_new_block(inode->i_sb);
143 * Get buffer_head for parent block, zero it out and set
144 * the pointer to new one, then send parent to disk.
146 parent = block_to_cpu(SYSV_SB(inode->i_sb), branch[n-1].key);
147 bh = sb_getblk(inode->i_sb, parent);
149 sysv_free_block(inode->i_sb, branch[n].key);
153 memset(bh->b_data, 0, blocksize);
155 branch[n].p = (sysv_zone_t*) bh->b_data + offsets[n];
156 *branch[n].p = branch[n].key;
157 set_buffer_uptodate(bh);
159 dirty_indirect(bh, inode);
164 /* Allocation failed, free what we already allocated */
165 for (i = 1; i < n; i++)
166 bforget(branch[i].bh);
167 for (i = 0; i < n; i++)
168 sysv_free_block(inode->i_sb, branch[i].key);
172 static inline int splice_branch(struct inode *inode,
179 /* Verify that place we are splicing to is still there and vacant */
180 write_lock(&pointers_lock);
181 if (!verify_chain(chain, where-1) || *where->p)
183 *where->p = where->key;
184 write_unlock(&pointers_lock);
186 inode->i_ctime = current_time(inode);
188 /* had we spliced it onto indirect block? */
190 dirty_indirect(where->bh, inode);
193 sysv_sync_inode(inode);
195 mark_inode_dirty(inode);
199 write_unlock(&pointers_lock);
200 for (i = 1; i < num; i++)
201 bforget(where[i].bh);
202 for (i = 0; i < num; i++)
203 sysv_free_block(inode->i_sb, where[i].key);
207 static int get_block(struct inode *inode, sector_t iblock, struct buffer_head *bh_result, int create)
211 Indirect chain[DEPTH];
212 struct super_block *sb = inode->i_sb;
215 int depth = block_to_path(inode, iblock, offsets);
221 read_lock(&pointers_lock);
222 partial = get_branch(inode, depth, offsets, chain, &err);
223 read_unlock(&pointers_lock);
225 /* Simplest case - block found, no allocation needed */
228 map_bh(bh_result, sb, block_to_cpu(SYSV_SB(sb),
229 chain[depth-1].key));
230 /* Clean up and exit */
231 partial = chain+depth-1; /* the whole chain */
235 /* Next simple case - plain lookup or failed read of indirect block */
236 if (!create || err == -EIO) {
238 while (partial > chain) {
247 * Indirect block might be removed by truncate while we were
248 * reading it. Handling of that case (forget what we've got and
249 * reread) is taken out of the main path.
254 left = (chain + depth) - partial;
255 err = alloc_branch(inode, left, offsets+(partial-chain), partial);
259 if (splice_branch(inode, chain, partial, left) < 0)
262 set_buffer_new(bh_result);
266 while (partial > chain) {
273 static inline int all_zeroes(sysv_zone_t *p, sysv_zone_t *q)
281 static Indirect *find_shared(struct inode *inode,
287 Indirect *partial, *p;
291 for (k = depth; k > 1 && !offsets[k-1]; k--)
294 write_lock(&pointers_lock);
295 partial = get_branch(inode, k, offsets, chain, &err);
297 partial = chain + k-1;
299 * If the branch acquired continuation since we've looked at it -
300 * fine, it should all survive and (new) top doesn't belong to us.
302 if (!partial->key && *partial->p) {
303 write_unlock(&pointers_lock);
306 for (p=partial; p>chain && all_zeroes((sysv_zone_t*)p->bh->b_data,p->p); p--)
309 * OK, we've found the last block that must survive. The rest of our
310 * branch should be detached before unlocking. However, if that rest
311 * of branch is all ours and does not grow immediately from the inode
312 * it's easier to cheat and just decrement partial->p.
314 if (p == chain + k - 1 && p > chain) {
320 write_unlock(&pointers_lock);
322 while (partial > p) {
330 static inline void free_data(struct inode *inode, sysv_zone_t *p, sysv_zone_t *q)
332 for ( ; p < q ; p++) {
336 sysv_free_block(inode->i_sb, nr);
337 mark_inode_dirty(inode);
342 static void free_branches(struct inode *inode, sysv_zone_t *p, sysv_zone_t *q, int depth)
344 struct buffer_head * bh;
345 struct super_block *sb = inode->i_sb;
348 for ( ; p < q ; p++) {
354 block = block_to_cpu(SYSV_SB(sb), nr);
355 bh = sb_bread(sb, block);
358 free_branches(inode, (sysv_zone_t*)bh->b_data,
359 block_end(bh), depth);
361 sysv_free_block(sb, nr);
362 mark_inode_dirty(inode);
365 free_data(inode, p, q);
368 void sysv_truncate (struct inode * inode)
370 sysv_zone_t *i_data = SYSV_I(inode)->i_data;
372 Indirect chain[DEPTH];
379 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
380 S_ISLNK(inode->i_mode)))
383 blocksize = inode->i_sb->s_blocksize;
384 iblock = (inode->i_size + blocksize-1)
385 >> inode->i_sb->s_blocksize_bits;
387 block_truncate_page(inode->i_mapping, inode->i_size, get_block);
389 n = block_to_path(inode, iblock, offsets);
394 free_data(inode, i_data+offsets[0], i_data + DIRECT);
398 partial = find_shared(inode, n, offsets, chain, &nr);
399 /* Kill the top of shared branch (already detached) */
401 if (partial == chain)
402 mark_inode_dirty(inode);
404 dirty_indirect(partial->bh, inode);
405 free_branches(inode, &nr, &nr+1, (chain+n-1) - partial);
407 /* Clear the ends of indirect blocks on the shared branch */
408 while (partial > chain) {
409 free_branches(inode, partial->p + 1, block_end(partial->bh),
410 (chain+n-1) - partial);
411 dirty_indirect(partial->bh, inode);
412 brelse (partial->bh);
416 /* Kill the remaining (whole) subtrees (== subtrees deeper than...) */
418 nr = i_data[DIRECT + n - 1];
420 i_data[DIRECT + n - 1] = 0;
421 mark_inode_dirty(inode);
422 free_branches(inode, &nr, &nr+1, n);
426 inode->i_mtime = inode->i_ctime = current_time(inode);
428 sysv_sync_inode (inode);
430 mark_inode_dirty(inode);
433 static unsigned sysv_nblocks(struct super_block *s, loff_t size)
435 struct sysv_sb_info *sbi = SYSV_SB(s);
436 int ptrs_bits = sbi->s_ind_per_block_bits;
437 unsigned blocks, res, direct = DIRECT, i = DEPTH;
438 blocks = (size + s->s_blocksize - 1) >> s->s_blocksize_bits;
440 while (--i && blocks > direct) {
441 blocks = ((blocks - direct - 1) >> ptrs_bits) + 1;
448 int sysv_getattr(struct mnt_idmap *idmap, const struct path *path,
449 struct kstat *stat, u32 request_mask, unsigned int flags)
451 struct super_block *s = path->dentry->d_sb;
452 generic_fillattr(&nop_mnt_idmap, d_inode(path->dentry), stat);
453 stat->blocks = (s->s_blocksize / 512) * sysv_nblocks(s, stat->size);
454 stat->blksize = s->s_blocksize;
458 static int sysv_writepage(struct page *page, struct writeback_control *wbc)
460 return block_write_full_page(page,get_block,wbc);
463 static int sysv_read_folio(struct file *file, struct folio *folio)
465 return block_read_full_folio(folio, get_block);
468 int sysv_prepare_chunk(struct page *page, loff_t pos, unsigned len)
470 return __block_write_begin(page, pos, len, get_block);
473 static void sysv_write_failed(struct address_space *mapping, loff_t to)
475 struct inode *inode = mapping->host;
477 if (to > inode->i_size) {
478 truncate_pagecache(inode, inode->i_size);
479 sysv_truncate(inode);
483 static int sysv_write_begin(struct file *file, struct address_space *mapping,
484 loff_t pos, unsigned len,
485 struct page **pagep, void **fsdata)
489 ret = block_write_begin(mapping, pos, len, pagep, get_block);
491 sysv_write_failed(mapping, pos + len);
496 static sector_t sysv_bmap(struct address_space *mapping, sector_t block)
498 return generic_block_bmap(mapping,block,get_block);
501 const struct address_space_operations sysv_aops = {
502 .dirty_folio = block_dirty_folio,
503 .invalidate_folio = block_invalidate_folio,
504 .read_folio = sysv_read_folio,
505 .writepage = sysv_writepage,
506 .write_begin = sysv_write_begin,
507 .write_end = generic_write_end,