^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* Generic part */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) typedef struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) block_t *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) block_t key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) } Indirect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) static DEFINE_RWLOCK(pointers_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) static inline void add_chain(Indirect *p, struct buffer_head *bh, block_t *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) p->key = *(p->p = v);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) p->bh = bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) static inline int verify_chain(Indirect *from, Indirect *to)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) while (from <= to && from->key == *from->p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) from++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) return (from > to);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) static inline block_t *block_end(struct buffer_head *bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) return (block_t *)((char*)bh->b_data + bh->b_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) static inline Indirect *get_branch(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) int depth,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) int *offsets,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) Indirect chain[DEPTH],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) int *err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) Indirect *p = chain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) *err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) /* i_data is not going away, no lock needed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) add_chain (chain, NULL, i_data(inode) + *offsets);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) if (!p->key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) goto no_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) while (--depth) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) bh = sb_bread(sb, block_to_cpu(p->key));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) if (!bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) goto failure;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) read_lock(&pointers_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) if (!verify_chain(chain, p))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) goto changed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) add_chain(++p, bh, (block_t *)bh->b_data + *++offsets);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) read_unlock(&pointers_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) if (!p->key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) goto no_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) changed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) read_unlock(&pointers_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) *err = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) goto no_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) failure:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) *err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) no_block:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) return p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) static int alloc_branch(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) int num,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) int *offsets,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) Indirect *branch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) int n = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) int parent = minix_new_block(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) int err = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) branch[0].key = cpu_to_block(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) if (parent) for (n = 1; n < num; n++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) /* Allocate the next block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) int nr = minix_new_block(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (!nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) branch[n].key = cpu_to_block(nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) bh = sb_getblk(inode->i_sb, parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) if (!bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) minix_free_block(inode, nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) lock_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) memset(bh->b_data, 0, bh->b_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) branch[n].bh = bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) branch[n].p = (block_t*) bh->b_data + offsets[n];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) *branch[n].p = branch[n].key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) set_buffer_uptodate(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) unlock_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) mark_buffer_dirty_inode(bh, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) parent = nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) if (n == num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) /* Allocation failed, free what we already allocated */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) for (i = 1; i < n; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) bforget(branch[i].bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) for (i = 0; i < n; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) minix_free_block(inode, block_to_cpu(branch[i].key));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) static inline int splice_branch(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) Indirect chain[DEPTH],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) Indirect *where,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) int num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) write_lock(&pointers_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) /* Verify that place we are splicing to is still there and vacant */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) if (!verify_chain(chain, where-1) || *where->p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) goto changed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) *where->p = where->key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) write_unlock(&pointers_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) /* We are done with atomic stuff, now do the rest of housekeeping */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) inode->i_ctime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) /* had we spliced it onto indirect block? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) if (where->bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) mark_buffer_dirty_inode(where->bh, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) mark_inode_dirty(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) changed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) write_unlock(&pointers_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) for (i = 1; i < num; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) bforget(where[i].bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) for (i = 0; i < num; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) minix_free_block(inode, block_to_cpu(where[i].key));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) static int get_block(struct inode * inode, sector_t block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) struct buffer_head *bh, int create)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) int err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) int offsets[DEPTH];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) Indirect chain[DEPTH];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) Indirect *partial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) int left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) int depth = block_to_path(inode, block, offsets);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) if (depth == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) reread:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) partial = get_branch(inode, depth, offsets, chain, &err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) /* Simplest case - block found, no allocation needed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) if (!partial) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) got_it:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) map_bh(bh, inode->i_sb, block_to_cpu(chain[depth-1].key));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) /* Clean up and exit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) partial = chain+depth-1; /* the whole chain */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) /* Next simple case - plain lookup or failed read of indirect block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) if (!create || err == -EIO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) cleanup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) while (partial > chain) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) brelse(partial->bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) partial--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) * Indirect block might be removed by truncate while we were
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) * reading it. Handling of that case (forget what we've got and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) * reread) is taken out of the main path.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) if (err == -EAGAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) goto changed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) left = (chain + depth) - partial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) err = alloc_branch(inode, left, offsets+(partial-chain), partial);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) goto cleanup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) if (splice_branch(inode, chain, partial, left) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) goto changed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) set_buffer_new(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) goto got_it;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) changed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) while (partial > chain) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) brelse(partial->bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) partial--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) goto reread;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) static inline int all_zeroes(block_t *p, block_t *q)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) while (p < q)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) if (*p++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) static Indirect *find_shared(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) int depth,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) int offsets[DEPTH],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) Indirect chain[DEPTH],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) block_t *top)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) Indirect *partial, *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) int k, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) *top = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) for (k = depth; k > 1 && !offsets[k-1]; k--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) partial = get_branch(inode, k, offsets, chain, &err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) write_lock(&pointers_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) if (!partial)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) partial = chain + k-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) if (!partial->key && *partial->p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) write_unlock(&pointers_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) goto no_top;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) for (p=partial;p>chain && all_zeroes((block_t*)p->bh->b_data,p->p);p--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) if (p == chain + k - 1 && p > chain) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) p->p--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) *top = *p->p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) *p->p = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) write_unlock(&pointers_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) while(partial > p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) brelse(partial->bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) partial--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) no_top:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) return partial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) static inline void free_data(struct inode *inode, block_t *p, block_t *q)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) unsigned long nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) for ( ; p < q ; p++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) nr = block_to_cpu(*p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) if (nr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) *p = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) minix_free_block(inode, nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) static void free_branches(struct inode *inode, block_t *p, block_t *q, int depth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) struct buffer_head * bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) unsigned long nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) if (depth--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) for ( ; p < q ; p++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) nr = block_to_cpu(*p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) if (!nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) *p = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) bh = sb_bread(inode->i_sb, nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) if (!bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) free_branches(inode, (block_t*)bh->b_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) block_end(bh), depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) bforget(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) minix_free_block(inode, nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) mark_inode_dirty(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) free_data(inode, p, q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) static inline void truncate (struct inode * inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) block_t *idata = i_data(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) int offsets[DEPTH];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) Indirect chain[DEPTH];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) Indirect *partial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) block_t nr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) int first_whole;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) long iblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) iblock = (inode->i_size + sb->s_blocksize -1) >> sb->s_blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) block_truncate_page(inode->i_mapping, inode->i_size, get_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) n = block_to_path(inode, iblock, offsets);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) if (!n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) if (n == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) free_data(inode, idata+offsets[0], idata + DIRECT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) first_whole = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) goto do_indirects;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) first_whole = offsets[0] + 1 - DIRECT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) partial = find_shared(inode, n, offsets, chain, &nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) if (nr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) if (partial == chain)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) mark_inode_dirty(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) mark_buffer_dirty_inode(partial->bh, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) free_branches(inode, &nr, &nr+1, (chain+n-1) - partial);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) /* Clear the ends of indirect blocks on the shared branch */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) while (partial > chain) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) free_branches(inode, partial->p + 1, block_end(partial->bh),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) (chain+n-1) - partial);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) mark_buffer_dirty_inode(partial->bh, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) brelse (partial->bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) partial--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) do_indirects:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) /* Kill the remaining (whole) subtrees */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) while (first_whole < DEPTH-1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) nr = idata[DIRECT+first_whole];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) if (nr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) idata[DIRECT+first_whole] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) mark_inode_dirty(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) free_branches(inode, &nr, &nr+1, first_whole+1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) first_whole++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) inode->i_mtime = inode->i_ctime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) mark_inode_dirty(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) static inline unsigned nblocks(loff_t size, struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) int k = sb->s_blocksize_bits - 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) unsigned blocks, res, direct = DIRECT, i = DEPTH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) blocks = (size + sb->s_blocksize - 1) >> (BLOCK_SIZE_BITS + k);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) res = blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) while (--i && blocks > direct) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) blocks -= direct;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) blocks += sb->s_blocksize/sizeof(block_t) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) blocks /= sb->s_blocksize/sizeof(block_t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) res += blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) direct = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) }