Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  *  linux/fs/sysv/itree.c
0004  *
0005  *  Handling of indirect blocks' trees.
0006  *  AV, Sep--Dec 2000
0007  */
0008 
0009 #include <linux/buffer_head.h>
0010 #include <linux/mount.h>
0011 #include <linux/string.h>
0012 #include "sysv.h"
0013 
0014 enum {DIRECT = 10, DEPTH = 4};  /* Have triple indirect */
0015 
0016 static inline void dirty_indirect(struct buffer_head *bh, struct inode *inode)
0017 {
0018     mark_buffer_dirty_inode(bh, inode);
0019     if (IS_SYNC(inode))
0020         sync_dirty_buffer(bh);
0021 }
0022 
0023 static int block_to_path(struct inode *inode, long block, int offsets[DEPTH])
0024 {
0025     struct super_block *sb = inode->i_sb;
0026     struct sysv_sb_info *sbi = SYSV_SB(sb);
0027     int ptrs_bits = sbi->s_ind_per_block_bits;
0028     unsigned long   indirect_blocks = sbi->s_ind_per_block,
0029             double_blocks = sbi->s_ind_per_block_2;
0030     int n = 0;
0031 
0032     if (block < 0) {
0033         printk("sysv_block_map: block < 0\n");
0034     } else if (block < DIRECT) {
0035         offsets[n++] = block;
0036     } else if ( (block -= DIRECT) < indirect_blocks) {
0037         offsets[n++] = DIRECT;
0038         offsets[n++] = block;
0039     } else if ((block -= indirect_blocks) < double_blocks) {
0040         offsets[n++] = DIRECT+1;
0041         offsets[n++] = block >> ptrs_bits;
0042         offsets[n++] = block & (indirect_blocks - 1);
0043     } else if (((block -= double_blocks) >> (ptrs_bits * 2)) < indirect_blocks) {
0044         offsets[n++] = DIRECT+2;
0045         offsets[n++] = block >> (ptrs_bits * 2);
0046         offsets[n++] = (block >> ptrs_bits) & (indirect_blocks - 1);
0047         offsets[n++] = block & (indirect_blocks - 1);
0048     } else {
0049         /* nothing */;
0050     }
0051     return n;
0052 }
0053 
0054 static inline int block_to_cpu(struct sysv_sb_info *sbi, sysv_zone_t nr)
0055 {
0056     return sbi->s_block_base + fs32_to_cpu(sbi, nr);
0057 }
0058 
0059 typedef struct {
0060     sysv_zone_t     *p;
0061     sysv_zone_t     key;
0062     struct buffer_head *bh;
0063 } Indirect;
0064 
0065 static DEFINE_RWLOCK(pointers_lock);
0066 
0067 static inline void add_chain(Indirect *p, struct buffer_head *bh, sysv_zone_t *v)
0068 {
0069     p->key = *(p->p = v);
0070     p->bh = bh;
0071 }
0072 
0073 static inline int verify_chain(Indirect *from, Indirect *to)
0074 {
0075     while (from <= to && from->key == *from->p)
0076         from++;
0077     return (from > to);
0078 }
0079 
0080 static inline sysv_zone_t *block_end(struct buffer_head *bh)
0081 {
0082     return (sysv_zone_t*)((char*)bh->b_data + bh->b_size);
0083 }
0084 
0085 /*
0086  * Requires read_lock(&pointers_lock) or write_lock(&pointers_lock)
0087  */
0088 static Indirect *get_branch(struct inode *inode,
0089                 int depth,
0090                 int offsets[],
0091                 Indirect chain[],
0092                 int *err)
0093 {
0094     struct super_block *sb = inode->i_sb;
0095     Indirect *p = chain;
0096     struct buffer_head *bh;
0097 
0098     *err = 0;
0099     add_chain(chain, NULL, SYSV_I(inode)->i_data + *offsets);
0100     if (!p->key)
0101         goto no_block;
0102     while (--depth) {
0103         int block = block_to_cpu(SYSV_SB(sb), p->key);
0104         bh = sb_bread(sb, block);
0105         if (!bh)
0106             goto failure;
0107         if (!verify_chain(chain, p))
0108             goto changed;
0109         add_chain(++p, bh, (sysv_zone_t*)bh->b_data + *++offsets);
0110         if (!p->key)
0111             goto no_block;
0112     }
0113     return NULL;
0114 
0115 changed:
0116     brelse(bh);
0117     *err = -EAGAIN;
0118     goto no_block;
0119 failure:
0120     *err = -EIO;
0121 no_block:
0122     return p;
0123 }
0124 
0125 static int alloc_branch(struct inode *inode,
0126             int num,
0127             int *offsets,
0128             Indirect *branch)
0129 {
0130     int blocksize = inode->i_sb->s_blocksize;
0131     int n = 0;
0132     int i;
0133 
0134     branch[0].key = sysv_new_block(inode->i_sb);
0135     if (branch[0].key) for (n = 1; n < num; n++) {
0136         struct buffer_head *bh;
0137         int parent;
0138         /* Allocate the next block */
0139         branch[n].key = sysv_new_block(inode->i_sb);
0140         if (!branch[n].key)
0141             break;
0142         /*
0143          * Get buffer_head for parent block, zero it out and set 
0144          * the pointer to new one, then send parent to disk.
0145          */
0146         parent = block_to_cpu(SYSV_SB(inode->i_sb), branch[n-1].key);
0147         bh = sb_getblk(inode->i_sb, parent);
0148         lock_buffer(bh);
0149         memset(bh->b_data, 0, blocksize);
0150         branch[n].bh = bh;
0151         branch[n].p = (sysv_zone_t*) bh->b_data + offsets[n];
0152         *branch[n].p = branch[n].key;
0153         set_buffer_uptodate(bh);
0154         unlock_buffer(bh);
0155         dirty_indirect(bh, inode);
0156     }
0157     if (n == num)
0158         return 0;
0159 
0160     /* Allocation failed, free what we already allocated */
0161     for (i = 1; i < n; i++)
0162         bforget(branch[i].bh);
0163     for (i = 0; i < n; i++)
0164         sysv_free_block(inode->i_sb, branch[i].key);
0165     return -ENOSPC;
0166 }
0167 
0168 static inline int splice_branch(struct inode *inode,
0169                 Indirect chain[],
0170                 Indirect *where,
0171                 int num)
0172 {
0173     int i;
0174 
0175     /* Verify that place we are splicing to is still there and vacant */
0176     write_lock(&pointers_lock);
0177     if (!verify_chain(chain, where-1) || *where->p)
0178         goto changed;
0179     *where->p = where->key;
0180     write_unlock(&pointers_lock);
0181 
0182     inode->i_ctime = current_time(inode);
0183 
0184     /* had we spliced it onto indirect block? */
0185     if (where->bh)
0186         dirty_indirect(where->bh, inode);
0187 
0188     if (IS_SYNC(inode))
0189         sysv_sync_inode(inode);
0190     else
0191         mark_inode_dirty(inode);
0192     return 0;
0193 
0194 changed:
0195     write_unlock(&pointers_lock);
0196     for (i = 1; i < num; i++)
0197         bforget(where[i].bh);
0198     for (i = 0; i < num; i++)
0199         sysv_free_block(inode->i_sb, where[i].key);
0200     return -EAGAIN;
0201 }
0202 
0203 static int get_block(struct inode *inode, sector_t iblock, struct buffer_head *bh_result, int create)
0204 {
0205     int err = -EIO;
0206     int offsets[DEPTH];
0207     Indirect chain[DEPTH];
0208     struct super_block *sb = inode->i_sb;
0209     Indirect *partial;
0210     int left;
0211     int depth = block_to_path(inode, iblock, offsets);
0212 
0213     if (depth == 0)
0214         goto out;
0215 
0216 reread:
0217     read_lock(&pointers_lock);
0218     partial = get_branch(inode, depth, offsets, chain, &err);
0219     read_unlock(&pointers_lock);
0220 
0221     /* Simplest case - block found, no allocation needed */
0222     if (!partial) {
0223 got_it:
0224         map_bh(bh_result, sb, block_to_cpu(SYSV_SB(sb),
0225                     chain[depth-1].key));
0226         /* Clean up and exit */
0227         partial = chain+depth-1; /* the whole chain */
0228         goto cleanup;
0229     }
0230 
0231     /* Next simple case - plain lookup or failed read of indirect block */
0232     if (!create || err == -EIO) {
0233 cleanup:
0234         while (partial > chain) {
0235             brelse(partial->bh);
0236             partial--;
0237         }
0238 out:
0239         return err;
0240     }
0241 
0242     /*
0243      * Indirect block might be removed by truncate while we were
0244      * reading it. Handling of that case (forget what we've got and
0245      * reread) is taken out of the main path.
0246      */
0247     if (err == -EAGAIN)
0248         goto changed;
0249 
0250     left = (chain + depth) - partial;
0251     err = alloc_branch(inode, left, offsets+(partial-chain), partial);
0252     if (err)
0253         goto cleanup;
0254 
0255     if (splice_branch(inode, chain, partial, left) < 0)
0256         goto changed;
0257 
0258     set_buffer_new(bh_result);
0259     goto got_it;
0260 
0261 changed:
0262     while (partial > chain) {
0263         brelse(partial->bh);
0264         partial--;
0265     }
0266     goto reread;
0267 }
0268 
0269 static inline int all_zeroes(sysv_zone_t *p, sysv_zone_t *q)
0270 {
0271     while (p < q)
0272         if (*p++)
0273             return 0;
0274     return 1;
0275 }
0276 
0277 static Indirect *find_shared(struct inode *inode,
0278                 int depth,
0279                 int offsets[],
0280                 Indirect chain[],
0281                 sysv_zone_t *top)
0282 {
0283     Indirect *partial, *p;
0284     int k, err;
0285 
0286     *top = 0;
0287     for (k = depth; k > 1 && !offsets[k-1]; k--)
0288         ;
0289 
0290     write_lock(&pointers_lock);
0291     partial = get_branch(inode, k, offsets, chain, &err);
0292     if (!partial)
0293         partial = chain + k-1;
0294     /*
0295      * If the branch acquired continuation since we've looked at it -
0296      * fine, it should all survive and (new) top doesn't belong to us.
0297      */
0298     if (!partial->key && *partial->p) {
0299         write_unlock(&pointers_lock);
0300         goto no_top;
0301     }
0302     for (p=partial; p>chain && all_zeroes((sysv_zone_t*)p->bh->b_data,p->p); p--)
0303         ;
0304     /*
0305      * OK, we've found the last block that must survive. The rest of our
0306      * branch should be detached before unlocking. However, if that rest
0307      * of branch is all ours and does not grow immediately from the inode
0308      * it's easier to cheat and just decrement partial->p.
0309      */
0310     if (p == chain + k - 1 && p > chain) {
0311         p->p--;
0312     } else {
0313         *top = *p->p;
0314         *p->p = 0;
0315     }
0316     write_unlock(&pointers_lock);
0317 
0318     while (partial > p) {
0319         brelse(partial->bh);
0320         partial--;
0321     }
0322 no_top:
0323     return partial;
0324 }
0325 
0326 static inline void free_data(struct inode *inode, sysv_zone_t *p, sysv_zone_t *q)
0327 {
0328     for ( ; p < q ; p++) {
0329         sysv_zone_t nr = *p;
0330         if (nr) {
0331             *p = 0;
0332             sysv_free_block(inode->i_sb, nr);
0333             mark_inode_dirty(inode);
0334         }
0335     }
0336 }
0337 
0338 static void free_branches(struct inode *inode, sysv_zone_t *p, sysv_zone_t *q, int depth)
0339 {
0340     struct buffer_head * bh;
0341     struct super_block *sb = inode->i_sb;
0342 
0343     if (depth--) {
0344         for ( ; p < q ; p++) {
0345             int block;
0346             sysv_zone_t nr = *p;
0347             if (!nr)
0348                 continue;
0349             *p = 0;
0350             block = block_to_cpu(SYSV_SB(sb), nr);
0351             bh = sb_bread(sb, block);
0352             if (!bh)
0353                 continue;
0354             free_branches(inode, (sysv_zone_t*)bh->b_data,
0355                     block_end(bh), depth);
0356             bforget(bh);
0357             sysv_free_block(sb, nr);
0358             mark_inode_dirty(inode);
0359         }
0360     } else
0361         free_data(inode, p, q);
0362 }
0363 
0364 void sysv_truncate (struct inode * inode)
0365 {
0366     sysv_zone_t *i_data = SYSV_I(inode)->i_data;
0367     int offsets[DEPTH];
0368     Indirect chain[DEPTH];
0369     Indirect *partial;
0370     sysv_zone_t nr = 0;
0371     int n;
0372     long iblock;
0373     unsigned blocksize;
0374 
0375     if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
0376         S_ISLNK(inode->i_mode)))
0377         return;
0378 
0379     blocksize = inode->i_sb->s_blocksize;
0380     iblock = (inode->i_size + blocksize-1)
0381                     >> inode->i_sb->s_blocksize_bits;
0382 
0383     block_truncate_page(inode->i_mapping, inode->i_size, get_block);
0384 
0385     n = block_to_path(inode, iblock, offsets);
0386     if (n == 0)
0387         return;
0388 
0389     if (n == 1) {
0390         free_data(inode, i_data+offsets[0], i_data + DIRECT);
0391         goto do_indirects;
0392     }
0393 
0394     partial = find_shared(inode, n, offsets, chain, &nr);
0395     /* Kill the top of shared branch (already detached) */
0396     if (nr) {
0397         if (partial == chain)
0398             mark_inode_dirty(inode);
0399         else
0400             dirty_indirect(partial->bh, inode);
0401         free_branches(inode, &nr, &nr+1, (chain+n-1) - partial);
0402     }
0403     /* Clear the ends of indirect blocks on the shared branch */
0404     while (partial > chain) {
0405         free_branches(inode, partial->p + 1, block_end(partial->bh),
0406                 (chain+n-1) - partial);
0407         dirty_indirect(partial->bh, inode);
0408         brelse (partial->bh);
0409         partial--;
0410     }
0411 do_indirects:
0412     /* Kill the remaining (whole) subtrees (== subtrees deeper than...) */
0413     while (n < DEPTH) {
0414         nr = i_data[DIRECT + n - 1];
0415         if (nr) {
0416             i_data[DIRECT + n - 1] = 0;
0417             mark_inode_dirty(inode);
0418             free_branches(inode, &nr, &nr+1, n);
0419         }
0420         n++;
0421     }
0422     inode->i_mtime = inode->i_ctime = current_time(inode);
0423     if (IS_SYNC(inode))
0424         sysv_sync_inode (inode);
0425     else
0426         mark_inode_dirty(inode);
0427 }
0428 
0429 static unsigned sysv_nblocks(struct super_block *s, loff_t size)
0430 {
0431     struct sysv_sb_info *sbi = SYSV_SB(s);
0432     int ptrs_bits = sbi->s_ind_per_block_bits;
0433     unsigned blocks, res, direct = DIRECT, i = DEPTH;
0434     blocks = (size + s->s_blocksize - 1) >> s->s_blocksize_bits;
0435     res = blocks;
0436     while (--i && blocks > direct) {
0437         blocks = ((blocks - direct - 1) >> ptrs_bits) + 1;
0438         res += blocks;
0439         direct = 1;
0440     }
0441     return blocks;
0442 }
0443 
0444 int sysv_getattr(struct user_namespace *mnt_userns, const struct path *path,
0445          struct kstat *stat, u32 request_mask, unsigned int flags)
0446 {
0447     struct super_block *s = path->dentry->d_sb;
0448     generic_fillattr(&init_user_ns, d_inode(path->dentry), stat);
0449     stat->blocks = (s->s_blocksize / 512) * sysv_nblocks(s, stat->size);
0450     stat->blksize = s->s_blocksize;
0451     return 0;
0452 }
0453 
0454 static int sysv_writepage(struct page *page, struct writeback_control *wbc)
0455 {
0456     return block_write_full_page(page,get_block,wbc);
0457 }
0458 
0459 static int sysv_read_folio(struct file *file, struct folio *folio)
0460 {
0461     return block_read_full_folio(folio, get_block);
0462 }
0463 
0464 int sysv_prepare_chunk(struct page *page, loff_t pos, unsigned len)
0465 {
0466     return __block_write_begin(page, pos, len, get_block);
0467 }
0468 
0469 static void sysv_write_failed(struct address_space *mapping, loff_t to)
0470 {
0471     struct inode *inode = mapping->host;
0472 
0473     if (to > inode->i_size) {
0474         truncate_pagecache(inode, inode->i_size);
0475         sysv_truncate(inode);
0476     }
0477 }
0478 
0479 static int sysv_write_begin(struct file *file, struct address_space *mapping,
0480             loff_t pos, unsigned len,
0481             struct page **pagep, void **fsdata)
0482 {
0483     int ret;
0484 
0485     ret = block_write_begin(mapping, pos, len, pagep, get_block);
0486     if (unlikely(ret))
0487         sysv_write_failed(mapping, pos + len);
0488 
0489     return ret;
0490 }
0491 
0492 static sector_t sysv_bmap(struct address_space *mapping, sector_t block)
0493 {
0494     return generic_block_bmap(mapping,block,get_block);
0495 }
0496 
0497 const struct address_space_operations sysv_aops = {
0498     .dirty_folio = block_dirty_folio,
0499     .invalidate_folio = block_invalidate_folio,
0500     .read_folio = sysv_read_folio,
0501     .writepage = sysv_writepage,
0502     .write_begin = sysv_write_begin,
0503     .write_end = generic_write_end,
0504     .bmap = sysv_bmap
0505 };