59bd9ded4d780 (Thomas Gleixner 2019-05-28 10:10:12 -0700 1) // SPDX-License-Identifier: GPL-2.0-only
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 2) /*
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 3) * OMFS (as used by RIO Karma) file operations.
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 4) * Copyright (C) 2005 Bob Copeland <me@bobcopeland.com>
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 5) */
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 6)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 7) #include <linux/module.h>
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 8) #include <linux/fs.h>
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 9) #include <linux/buffer_head.h>
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 10) #include <linux/mpage.h>
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 11) #include "omfs.h"
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 12)
9419fc1c957d6 (Bob Copeland 2008-08-15 00:40:47 -0700 13) static u32 omfs_max_extents(struct omfs_sb_info *sbi, int offset)
9419fc1c957d6 (Bob Copeland 2008-08-15 00:40:47 -0700 14) {
9419fc1c957d6 (Bob Copeland 2008-08-15 00:40:47 -0700 15) return (sbi->s_sys_blocksize - offset -
9419fc1c957d6 (Bob Copeland 2008-08-15 00:40:47 -0700 16) sizeof(struct omfs_extent)) /
9419fc1c957d6 (Bob Copeland 2008-08-15 00:40:47 -0700 17) sizeof(struct omfs_extent_entry) + 1;
9419fc1c957d6 (Bob Copeland 2008-08-15 00:40:47 -0700 18) }
9419fc1c957d6 (Bob Copeland 2008-08-15 00:40:47 -0700 19)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 20) void omfs_make_empty_table(struct buffer_head *bh, int offset)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 21) {
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 22) struct omfs_extent *oe = (struct omfs_extent *) &bh->b_data[offset];
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 23)
d406f66ddb0d7 (Harvey Harrison 2008-07-29 22:33:46 -0700 24) oe->e_next = ~cpu_to_be64(0ULL);
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 25) oe->e_extent_count = cpu_to_be32(1),
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 26) oe->e_fill = cpu_to_be32(0x22),
d406f66ddb0d7 (Harvey Harrison 2008-07-29 22:33:46 -0700 27) oe->e_entry.e_cluster = ~cpu_to_be64(0ULL);
d406f66ddb0d7 (Harvey Harrison 2008-07-29 22:33:46 -0700 28) oe->e_entry.e_blocks = ~cpu_to_be64(0ULL);
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 29) }
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 30)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 31) int omfs_shrink_inode(struct inode *inode)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 32) {
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 33) struct omfs_sb_info *sbi = OMFS_SB(inode->i_sb);
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 34) struct omfs_extent *oe;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 35) struct omfs_extent_entry *entry;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 36) struct buffer_head *bh;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 37) u64 next, last;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 38) u32 extent_count;
9419fc1c957d6 (Bob Copeland 2008-08-15 00:40:47 -0700 39) u32 max_extents;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 40) int ret;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 41)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 42) /* traverse extent table, freeing each entry that is greater
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 43) * than inode->i_size;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 44) */
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 45) next = inode->i_ino;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 46)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 47) /* only support truncate -> 0 for now */
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 48) ret = -EIO;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 49) if (inode->i_size != 0)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 50) goto out;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 51)
f068272cb2f13 (Bob Copeland 2008-09-06 17:51:53 -0400 52) bh = omfs_bread(inode->i_sb, next);
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 53) if (!bh)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 54) goto out;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 55)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 56) oe = (struct omfs_extent *)(&bh->b_data[OMFS_EXTENT_START]);
9419fc1c957d6 (Bob Copeland 2008-08-15 00:40:47 -0700 57) max_extents = omfs_max_extents(sbi, OMFS_EXTENT_START);
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 58)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 59) for (;;) {
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 60)
9419fc1c957d6 (Bob Copeland 2008-08-15 00:40:47 -0700 61) if (omfs_is_bad(sbi, (struct omfs_header *) bh->b_data, next))
9419fc1c957d6 (Bob Copeland 2008-08-15 00:40:47 -0700 62) goto out_brelse;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 63)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 64) extent_count = be32_to_cpu(oe->e_extent_count);
9419fc1c957d6 (Bob Copeland 2008-08-15 00:40:47 -0700 65)
9419fc1c957d6 (Bob Copeland 2008-08-15 00:40:47 -0700 66) if (extent_count > max_extents)
9419fc1c957d6 (Bob Copeland 2008-08-15 00:40:47 -0700 67) goto out_brelse;
9419fc1c957d6 (Bob Copeland 2008-08-15 00:40:47 -0700 68)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 69) last = next;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 70) next = be64_to_cpu(oe->e_next);
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 71) entry = &oe->e_entry;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 72)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 73) /* ignore last entry as it is the terminator */
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 74) for (; extent_count > 1; extent_count--) {
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 75) u64 start, count;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 76) start = be64_to_cpu(entry->e_cluster);
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 77) count = be64_to_cpu(entry->e_blocks);
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 78)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 79) omfs_clear_range(inode->i_sb, start, (int) count);
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 80) entry++;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 81) }
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 82) omfs_make_empty_table(bh, (char *) oe - bh->b_data);
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 83) mark_buffer_dirty(bh);
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 84) brelse(bh);
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 85)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 86) if (last != inode->i_ino)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 87) omfs_clear_range(inode->i_sb, last, sbi->s_mirrors);
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 88)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 89) if (next == ~0)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 90) break;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 91)
f068272cb2f13 (Bob Copeland 2008-09-06 17:51:53 -0400 92) bh = omfs_bread(inode->i_sb, next);
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 93) if (!bh)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 94) goto out;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 95) oe = (struct omfs_extent *) (&bh->b_data[OMFS_EXTENT_CONT]);
9419fc1c957d6 (Bob Copeland 2008-08-15 00:40:47 -0700 96) max_extents = omfs_max_extents(sbi, OMFS_EXTENT_CONT);
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 97) }
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 98) ret = 0;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 99) out:
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 100) return ret;
9419fc1c957d6 (Bob Copeland 2008-08-15 00:40:47 -0700 101) out_brelse:
9419fc1c957d6 (Bob Copeland 2008-08-15 00:40:47 -0700 102) brelse(bh);
9419fc1c957d6 (Bob Copeland 2008-08-15 00:40:47 -0700 103) return ret;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 104) }
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 105)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 106) static void omfs_truncate(struct inode *inode)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 107) {
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 108) omfs_shrink_inode(inode);
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 109) mark_inode_dirty(inode);
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 110) }
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 111)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 112) /*
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 113) * Add new blocks to the current extent, or create new entries/continuations
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 114) * as necessary.
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 115) */
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 116) static int omfs_grow_extent(struct inode *inode, struct omfs_extent *oe,
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 117) u64 *ret_block)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 118) {
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 119) struct omfs_extent_entry *terminator;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 120) struct omfs_extent_entry *entry = &oe->e_entry;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 121) struct omfs_sb_info *sbi = OMFS_SB(inode->i_sb);
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 122) u32 extent_count = be32_to_cpu(oe->e_extent_count);
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 123) u64 new_block = 0;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 124) u32 max_count;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 125) int new_count;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 126) int ret = 0;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 127)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 128) /* reached the end of the extent table with no blocks mapped.
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 129) * there are three possibilities for adding: grow last extent,
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 130) * add a new extent to the current extent table, and add a
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 131) * continuation inode. in last two cases need an allocator for
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 132) * sbi->s_cluster_size
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 133) */
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 134)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 135) /* TODO: handle holes */
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 136)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 137) /* should always have a terminator */
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 138) if (extent_count < 1)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 139) return -EIO;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 140)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 141) /* trivially grow current extent, if next block is not taken */
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 142) terminator = entry + extent_count - 1;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 143) if (extent_count > 1) {
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 144) entry = terminator-1;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 145) new_block = be64_to_cpu(entry->e_cluster) +
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 146) be64_to_cpu(entry->e_blocks);
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 147)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 148) if (omfs_allocate_block(inode->i_sb, new_block)) {
c99b6841d74a5 (Wei Yongjun 2012-10-04 17:16:53 -0700 149) be64_add_cpu(&entry->e_blocks, 1);
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 150) terminator->e_blocks = ~(cpu_to_be64(
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 151) be64_to_cpu(~terminator->e_blocks) + 1));
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 152) goto out;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 153) }
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 154) }
9419fc1c957d6 (Bob Copeland 2008-08-15 00:40:47 -0700 155) max_count = omfs_max_extents(sbi, OMFS_EXTENT_START);
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 156)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 157) /* TODO: add a continuation block here */
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 158) if (be32_to_cpu(oe->e_extent_count) > max_count-1)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 159) return -EIO;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 160)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 161) /* try to allocate a new cluster */
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 162) ret = omfs_allocate_range(inode->i_sb, 1, sbi->s_clustersize,
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 163) &new_block, &new_count);
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 164) if (ret)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 165) goto out_fail;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 166)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 167) /* copy terminator down an entry */
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 168) entry = terminator;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 169) terminator++;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 170) memcpy(terminator, entry, sizeof(struct omfs_extent_entry));
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 171)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 172) entry->e_cluster = cpu_to_be64(new_block);
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 173) entry->e_blocks = cpu_to_be64((u64) new_count);
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 174)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 175) terminator->e_blocks = ~(cpu_to_be64(
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 176) be64_to_cpu(~terminator->e_blocks) + (u64) new_count));
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 177)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 178) /* write in new entry */
c99b6841d74a5 (Wei Yongjun 2012-10-04 17:16:53 -0700 179) be32_add_cpu(&oe->e_extent_count, 1);
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 180)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 181) out:
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 182) *ret_block = new_block;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 183) out_fail:
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 184) return ret;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 185) }
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 186)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 187) /*
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 188) * Scans across the directory table for a given file block number.
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 189) * If block not found, return 0.
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 190) */
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 191) static sector_t find_block(struct inode *inode, struct omfs_extent_entry *ent,
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 192) sector_t block, int count, int *left)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 193) {
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 194) /* count > 1 because of terminator */
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 195) sector_t searched = 0;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 196) for (; count > 1; count--) {
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 197) int numblocks = clus_to_blk(OMFS_SB(inode->i_sb),
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 198) be64_to_cpu(ent->e_blocks));
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 199)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 200) if (block >= searched &&
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 201) block < searched + numblocks) {
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 202) /*
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 203) * found it at cluster + (block - searched)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 204) * numblocks - (block - searched) is remainder
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 205) */
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 206) *left = numblocks - (block - searched);
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 207) return clus_to_blk(OMFS_SB(inode->i_sb),
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 208) be64_to_cpu(ent->e_cluster)) +
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 209) block - searched;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 210) }
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 211) searched += numblocks;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 212) ent++;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 213) }
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 214) return 0;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 215) }
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 216)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 217) static int omfs_get_block(struct inode *inode, sector_t block,
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 218) struct buffer_head *bh_result, int create)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 219) {
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 220) struct buffer_head *bh;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 221) sector_t next, offset;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 222) int ret;
3f649ab728cda (Kees Cook 2020-06-03 13:09:38 -0700 223) u64 new_block;
9419fc1c957d6 (Bob Copeland 2008-08-15 00:40:47 -0700 224) u32 max_extents;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 225) int extent_count;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 226) struct omfs_extent *oe;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 227) struct omfs_extent_entry *entry;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 228) struct omfs_sb_info *sbi = OMFS_SB(inode->i_sb);
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 229) int max_blocks = bh_result->b_size >> inode->i_blkbits;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 230) int remain;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 231)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 232) ret = -EIO;
f068272cb2f13 (Bob Copeland 2008-09-06 17:51:53 -0400 233) bh = omfs_bread(inode->i_sb, inode->i_ino);
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 234) if (!bh)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 235) goto out;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 236)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 237) oe = (struct omfs_extent *)(&bh->b_data[OMFS_EXTENT_START]);
9419fc1c957d6 (Bob Copeland 2008-08-15 00:40:47 -0700 238) max_extents = omfs_max_extents(sbi, OMFS_EXTENT_START);
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 239) next = inode->i_ino;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 240)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 241) for (;;) {
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 242)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 243) if (omfs_is_bad(sbi, (struct omfs_header *) bh->b_data, next))
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 244) goto out_brelse;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 245)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 246) extent_count = be32_to_cpu(oe->e_extent_count);
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 247) next = be64_to_cpu(oe->e_next);
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 248) entry = &oe->e_entry;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 249)
9419fc1c957d6 (Bob Copeland 2008-08-15 00:40:47 -0700 250) if (extent_count > max_extents)
9419fc1c957d6 (Bob Copeland 2008-08-15 00:40:47 -0700 251) goto out_brelse;
9419fc1c957d6 (Bob Copeland 2008-08-15 00:40:47 -0700 252)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 253) offset = find_block(inode, entry, block, extent_count, &remain);
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 254) if (offset > 0) {
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 255) ret = 0;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 256) map_bh(bh_result, inode->i_sb, offset);
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 257) if (remain > max_blocks)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 258) remain = max_blocks;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 259) bh_result->b_size = (remain << inode->i_blkbits);
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 260) goto out_brelse;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 261) }
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 262) if (next == ~0)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 263) break;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 264)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 265) brelse(bh);
f068272cb2f13 (Bob Copeland 2008-09-06 17:51:53 -0400 266) bh = omfs_bread(inode->i_sb, next);
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 267) if (!bh)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 268) goto out;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 269) oe = (struct omfs_extent *) (&bh->b_data[OMFS_EXTENT_CONT]);
9419fc1c957d6 (Bob Copeland 2008-08-15 00:40:47 -0700 270) max_extents = omfs_max_extents(sbi, OMFS_EXTENT_CONT);
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 271) }
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 272) if (create) {
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 273) ret = omfs_grow_extent(inode, oe, &new_block);
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 274) if (ret == 0) {
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 275) mark_buffer_dirty(bh);
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 276) mark_inode_dirty(inode);
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 277) map_bh(bh_result, inode->i_sb,
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 278) clus_to_blk(sbi, new_block));
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 279) }
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 280) }
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 281) out_brelse:
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 282) brelse(bh);
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 283) out:
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 284) return ret;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 285) }
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 286)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 287) static int omfs_readpage(struct file *file, struct page *page)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 288) {
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 289) return block_read_full_page(page, omfs_get_block);
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 290) }
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 291)
d4388340ae0bc (Matthew Wilcox (Oracle) 2020-06-01 21:47:02 -0700 292) static void omfs_readahead(struct readahead_control *rac)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 293) {
d4388340ae0bc (Matthew Wilcox (Oracle) 2020-06-01 21:47:02 -0700 294) mpage_readahead(rac, omfs_get_block);
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 295) }
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 296)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 297) static int omfs_writepage(struct page *page, struct writeback_control *wbc)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 298) {
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 299) return block_write_full_page(page, omfs_get_block, wbc);
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 300) }
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 301)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 302) static int
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 303) omfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 304) {
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 305) return mpage_writepages(mapping, wbc, omfs_get_block);
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 306) }
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 307)
a8f5293aac161 (Marco Stornelli 2012-12-15 11:49:42 +0100 308) static void omfs_write_failed(struct address_space *mapping, loff_t to)
a8f5293aac161 (Marco Stornelli 2012-12-15 11:49:42 +0100 309) {
a8f5293aac161 (Marco Stornelli 2012-12-15 11:49:42 +0100 310) struct inode *inode = mapping->host;
a8f5293aac161 (Marco Stornelli 2012-12-15 11:49:42 +0100 311)
a8f5293aac161 (Marco Stornelli 2012-12-15 11:49:42 +0100 312) if (to > inode->i_size) {
7caef26767c17 (Kirill A. Shutemov 2013-09-12 15:13:56 -0700 313) truncate_pagecache(inode, inode->i_size);
a8f5293aac161 (Marco Stornelli 2012-12-15 11:49:42 +0100 314) omfs_truncate(inode);
a8f5293aac161 (Marco Stornelli 2012-12-15 11:49:42 +0100 315) }
a8f5293aac161 (Marco Stornelli 2012-12-15 11:49:42 +0100 316) }
a8f5293aac161 (Marco Stornelli 2012-12-15 11:49:42 +0100 317)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 318) static int omfs_write_begin(struct file *file, struct address_space *mapping,
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 319) loff_t pos, unsigned len, unsigned flags,
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 320) struct page **pagep, void **fsdata)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 321) {
155130a4f7848 (Christoph Hellwig 2010-06-04 11:29:58 +0200 322) int ret;
155130a4f7848 (Christoph Hellwig 2010-06-04 11:29:58 +0200 323)
155130a4f7848 (Christoph Hellwig 2010-06-04 11:29:58 +0200 324) ret = block_write_begin(mapping, pos, len, flags, pagep,
155130a4f7848 (Christoph Hellwig 2010-06-04 11:29:58 +0200 325) omfs_get_block);
a8f5293aac161 (Marco Stornelli 2012-12-15 11:49:42 +0100 326) if (unlikely(ret))
a8f5293aac161 (Marco Stornelli 2012-12-15 11:49:42 +0100 327) omfs_write_failed(mapping, pos + len);
155130a4f7848 (Christoph Hellwig 2010-06-04 11:29:58 +0200 328)
155130a4f7848 (Christoph Hellwig 2010-06-04 11:29:58 +0200 329) return ret;
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 330) }
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 331)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 332) static sector_t omfs_bmap(struct address_space *mapping, sector_t block)
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 333) {
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 334) return generic_block_bmap(mapping, block, omfs_get_block);
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 335) }
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 336)
828c09509b969 (Alexey Dobriyan 2009-10-01 15:43:56 -0700 337) const struct file_operations omfs_file_operations = {
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 338) .llseek = generic_file_llseek,
aad4f8bb42af0 (Al Viro 2014-04-02 14:33:16 -0400 339) .read_iter = generic_file_read_iter,
8174202b34c30 (Al Viro 2014-04-03 03:17:43 -0400 340) .write_iter = generic_file_write_iter,
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 341) .mmap = generic_file_mmap,
1b061d9247f71 (Christoph Hellwig 2010-05-26 17:53:41 +0200 342) .fsync = generic_file_fsync,
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 343) .splice_read = generic_file_splice_read,
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 344) };
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 345)
549c7297717c3 (Christian Brauner 2021-01-21 14:19:43 +0100 346) static int omfs_setattr(struct user_namespace *mnt_userns,
549c7297717c3 (Christian Brauner 2021-01-21 14:19:43 +0100 347) struct dentry *dentry, struct iattr *attr)
d39aae9ec447d (Christoph Hellwig 2010-06-04 11:29:59 +0200 348) {
2b0143b5c986b (David Howells 2015-03-17 22:25:59 +0000 349) struct inode *inode = d_inode(dentry);
d39aae9ec447d (Christoph Hellwig 2010-06-04 11:29:59 +0200 350) int error;
d39aae9ec447d (Christoph Hellwig 2010-06-04 11:29:59 +0200 351)
2f221d6f7b881 (Christian Brauner 2021-01-21 14:19:26 +0100 352) error = setattr_prepare(&init_user_ns, dentry, attr);
d39aae9ec447d (Christoph Hellwig 2010-06-04 11:29:59 +0200 353) if (error)
d39aae9ec447d (Christoph Hellwig 2010-06-04 11:29:59 +0200 354) return error;
1025774ce411f (Christoph Hellwig 2010-06-04 11:30:02 +0200 355)
1025774ce411f (Christoph Hellwig 2010-06-04 11:30:02 +0200 356) if ((attr->ia_valid & ATTR_SIZE) &&
1025774ce411f (Christoph Hellwig 2010-06-04 11:30:02 +0200 357) attr->ia_size != i_size_read(inode)) {
a8f5293aac161 (Marco Stornelli 2012-12-15 11:49:42 +0100 358) error = inode_newsize_ok(inode, attr->ia_size);
1025774ce411f (Christoph Hellwig 2010-06-04 11:30:02 +0200 359) if (error)
1025774ce411f (Christoph Hellwig 2010-06-04 11:30:02 +0200 360) return error;
a8f5293aac161 (Marco Stornelli 2012-12-15 11:49:42 +0100 361) truncate_setsize(inode, attr->ia_size);
a8f5293aac161 (Marco Stornelli 2012-12-15 11:49:42 +0100 362) omfs_truncate(inode);
1025774ce411f (Christoph Hellwig 2010-06-04 11:30:02 +0200 363) }
1025774ce411f (Christoph Hellwig 2010-06-04 11:30:02 +0200 364)
2f221d6f7b881 (Christian Brauner 2021-01-21 14:19:26 +0100 365) setattr_copy(&init_user_ns, inode, attr);
1025774ce411f (Christoph Hellwig 2010-06-04 11:30:02 +0200 366) mark_inode_dirty(inode);
1025774ce411f (Christoph Hellwig 2010-06-04 11:30:02 +0200 367) return 0;
d39aae9ec447d (Christoph Hellwig 2010-06-04 11:29:59 +0200 368) }
d39aae9ec447d (Christoph Hellwig 2010-06-04 11:29:59 +0200 369)
6e1d5dcc2bbbe (Alexey Dobriyan 2009-09-21 17:01:11 -0700 370) const struct inode_operations omfs_file_inops = {
d39aae9ec447d (Christoph Hellwig 2010-06-04 11:29:59 +0200 371) .setattr = omfs_setattr,
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 372) };
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 373)
7f09410bbc430 (Alexey Dobriyan 2009-09-21 17:01:10 -0700 374) const struct address_space_operations omfs_aops = {
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 375) .readpage = omfs_readpage,
d4388340ae0bc (Matthew Wilcox (Oracle) 2020-06-01 21:47:02 -0700 376) .readahead = omfs_readahead,
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 377) .writepage = omfs_writepage,
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 378) .writepages = omfs_writepages,
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 379) .write_begin = omfs_write_begin,
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 380) .write_end = generic_write_end,
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 381) .bmap = omfs_bmap,
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 382) };
8f09e98768c17 (Bob Copeland 2008-07-25 19:45:16 -0700 383)