Bob Copeland | 8f09e98 | 2008-07-25 19:45:16 -0700 | [diff] [blame] | 1 | /* |
| 2 | * OMFS (as used by RIO Karma) file operations. |
| 3 | * Copyright (C) 2005 Bob Copeland <me@bobcopeland.com> |
| 4 | * Released under GPL v2. |
| 5 | */ |
| 6 | |
| 7 | #include <linux/version.h> |
| 8 | #include <linux/module.h> |
| 9 | #include <linux/fs.h> |
| 10 | #include <linux/buffer_head.h> |
| 11 | #include <linux/mpage.h> |
| 12 | #include "omfs.h" |
| 13 | |
| 14 | static int omfs_sync_file(struct file *file, struct dentry *dentry, |
| 15 | int datasync) |
| 16 | { |
| 17 | struct inode *inode = dentry->d_inode; |
| 18 | int err; |
| 19 | |
| 20 | err = sync_mapping_buffers(inode->i_mapping); |
| 21 | if (!(inode->i_state & I_DIRTY)) |
| 22 | return err; |
| 23 | if (datasync && !(inode->i_state & I_DIRTY_DATASYNC)) |
| 24 | return err; |
| 25 | err |= omfs_sync_inode(inode); |
| 26 | return err ? -EIO : 0; |
| 27 | } |
| 28 | |
| 29 | void omfs_make_empty_table(struct buffer_head *bh, int offset) |
| 30 | { |
| 31 | struct omfs_extent *oe = (struct omfs_extent *) &bh->b_data[offset]; |
| 32 | |
Harvey Harrison | d406f66 | 2008-07-29 22:33:46 -0700 | [diff] [blame^] | 33 | oe->e_next = ~cpu_to_be64(0ULL); |
Bob Copeland | 8f09e98 | 2008-07-25 19:45:16 -0700 | [diff] [blame] | 34 | oe->e_extent_count = cpu_to_be32(1), |
| 35 | oe->e_fill = cpu_to_be32(0x22), |
Harvey Harrison | d406f66 | 2008-07-29 22:33:46 -0700 | [diff] [blame^] | 36 | oe->e_entry.e_cluster = ~cpu_to_be64(0ULL); |
| 37 | oe->e_entry.e_blocks = ~cpu_to_be64(0ULL); |
Bob Copeland | 8f09e98 | 2008-07-25 19:45:16 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | int omfs_shrink_inode(struct inode *inode) |
| 41 | { |
| 42 | struct omfs_sb_info *sbi = OMFS_SB(inode->i_sb); |
| 43 | struct omfs_extent *oe; |
| 44 | struct omfs_extent_entry *entry; |
| 45 | struct buffer_head *bh; |
| 46 | u64 next, last; |
| 47 | u32 extent_count; |
| 48 | int ret; |
| 49 | |
| 50 | /* traverse extent table, freeing each entry that is greater |
| 51 | * than inode->i_size; |
| 52 | */ |
| 53 | next = inode->i_ino; |
| 54 | |
| 55 | /* only support truncate -> 0 for now */ |
| 56 | ret = -EIO; |
| 57 | if (inode->i_size != 0) |
| 58 | goto out; |
| 59 | |
| 60 | bh = sb_bread(inode->i_sb, clus_to_blk(sbi, next)); |
| 61 | if (!bh) |
| 62 | goto out; |
| 63 | |
| 64 | oe = (struct omfs_extent *)(&bh->b_data[OMFS_EXTENT_START]); |
| 65 | |
| 66 | for (;;) { |
| 67 | |
| 68 | if (omfs_is_bad(sbi, (struct omfs_header *) bh->b_data, next)) { |
| 69 | brelse(bh); |
| 70 | goto out; |
| 71 | } |
| 72 | |
| 73 | extent_count = be32_to_cpu(oe->e_extent_count); |
| 74 | last = next; |
| 75 | next = be64_to_cpu(oe->e_next); |
| 76 | entry = &oe->e_entry; |
| 77 | |
| 78 | /* ignore last entry as it is the terminator */ |
| 79 | for (; extent_count > 1; extent_count--) { |
| 80 | u64 start, count; |
| 81 | start = be64_to_cpu(entry->e_cluster); |
| 82 | count = be64_to_cpu(entry->e_blocks); |
| 83 | |
| 84 | omfs_clear_range(inode->i_sb, start, (int) count); |
| 85 | entry++; |
| 86 | } |
| 87 | omfs_make_empty_table(bh, (char *) oe - bh->b_data); |
| 88 | mark_buffer_dirty(bh); |
| 89 | brelse(bh); |
| 90 | |
| 91 | if (last != inode->i_ino) |
| 92 | omfs_clear_range(inode->i_sb, last, sbi->s_mirrors); |
| 93 | |
| 94 | if (next == ~0) |
| 95 | break; |
| 96 | |
| 97 | bh = sb_bread(inode->i_sb, clus_to_blk(sbi, next)); |
| 98 | if (!bh) |
| 99 | goto out; |
| 100 | oe = (struct omfs_extent *) (&bh->b_data[OMFS_EXTENT_CONT]); |
| 101 | } |
| 102 | ret = 0; |
| 103 | out: |
| 104 | return ret; |
| 105 | } |
| 106 | |
| 107 | static void omfs_truncate(struct inode *inode) |
| 108 | { |
| 109 | omfs_shrink_inode(inode); |
| 110 | mark_inode_dirty(inode); |
| 111 | } |
| 112 | |
| 113 | /* |
| 114 | * Add new blocks to the current extent, or create new entries/continuations |
| 115 | * as necessary. |
| 116 | */ |
| 117 | static int omfs_grow_extent(struct inode *inode, struct omfs_extent *oe, |
| 118 | u64 *ret_block) |
| 119 | { |
| 120 | struct omfs_extent_entry *terminator; |
| 121 | struct omfs_extent_entry *entry = &oe->e_entry; |
| 122 | struct omfs_sb_info *sbi = OMFS_SB(inode->i_sb); |
| 123 | u32 extent_count = be32_to_cpu(oe->e_extent_count); |
| 124 | u64 new_block = 0; |
| 125 | u32 max_count; |
| 126 | int new_count; |
| 127 | int ret = 0; |
| 128 | |
| 129 | /* reached the end of the extent table with no blocks mapped. |
| 130 | * there are three possibilities for adding: grow last extent, |
| 131 | * add a new extent to the current extent table, and add a |
| 132 | * continuation inode. in last two cases need an allocator for |
| 133 | * sbi->s_cluster_size |
| 134 | */ |
| 135 | |
| 136 | /* TODO: handle holes */ |
| 137 | |
| 138 | /* should always have a terminator */ |
| 139 | if (extent_count < 1) |
| 140 | return -EIO; |
| 141 | |
| 142 | /* trivially grow current extent, if next block is not taken */ |
| 143 | terminator = entry + extent_count - 1; |
| 144 | if (extent_count > 1) { |
| 145 | entry = terminator-1; |
| 146 | new_block = be64_to_cpu(entry->e_cluster) + |
| 147 | be64_to_cpu(entry->e_blocks); |
| 148 | |
| 149 | if (omfs_allocate_block(inode->i_sb, new_block)) { |
| 150 | entry->e_blocks = |
| 151 | cpu_to_be64(be64_to_cpu(entry->e_blocks) + 1); |
| 152 | terminator->e_blocks = ~(cpu_to_be64( |
| 153 | be64_to_cpu(~terminator->e_blocks) + 1)); |
| 154 | goto out; |
| 155 | } |
| 156 | } |
| 157 | max_count = (sbi->s_sys_blocksize - OMFS_EXTENT_START - |
| 158 | sizeof(struct omfs_extent)) / |
| 159 | sizeof(struct omfs_extent_entry) + 1; |
| 160 | |
| 161 | /* TODO: add a continuation block here */ |
| 162 | if (be32_to_cpu(oe->e_extent_count) > max_count-1) |
| 163 | return -EIO; |
| 164 | |
| 165 | /* try to allocate a new cluster */ |
| 166 | ret = omfs_allocate_range(inode->i_sb, 1, sbi->s_clustersize, |
| 167 | &new_block, &new_count); |
| 168 | if (ret) |
| 169 | goto out_fail; |
| 170 | |
| 171 | /* copy terminator down an entry */ |
| 172 | entry = terminator; |
| 173 | terminator++; |
| 174 | memcpy(terminator, entry, sizeof(struct omfs_extent_entry)); |
| 175 | |
| 176 | entry->e_cluster = cpu_to_be64(new_block); |
| 177 | entry->e_blocks = cpu_to_be64((u64) new_count); |
| 178 | |
| 179 | terminator->e_blocks = ~(cpu_to_be64( |
| 180 | be64_to_cpu(~terminator->e_blocks) + (u64) new_count)); |
| 181 | |
| 182 | /* write in new entry */ |
| 183 | oe->e_extent_count = cpu_to_be32(1 + be32_to_cpu(oe->e_extent_count)); |
| 184 | |
| 185 | out: |
| 186 | *ret_block = new_block; |
| 187 | out_fail: |
| 188 | return ret; |
| 189 | } |
| 190 | |
| 191 | /* |
| 192 | * Scans across the directory table for a given file block number. |
| 193 | * If block not found, return 0. |
| 194 | */ |
| 195 | static sector_t find_block(struct inode *inode, struct omfs_extent_entry *ent, |
| 196 | sector_t block, int count, int *left) |
| 197 | { |
| 198 | /* count > 1 because of terminator */ |
| 199 | sector_t searched = 0; |
| 200 | for (; count > 1; count--) { |
| 201 | int numblocks = clus_to_blk(OMFS_SB(inode->i_sb), |
| 202 | be64_to_cpu(ent->e_blocks)); |
| 203 | |
| 204 | if (block >= searched && |
| 205 | block < searched + numblocks) { |
| 206 | /* |
| 207 | * found it at cluster + (block - searched) |
| 208 | * numblocks - (block - searched) is remainder |
| 209 | */ |
| 210 | *left = numblocks - (block - searched); |
| 211 | return clus_to_blk(OMFS_SB(inode->i_sb), |
| 212 | be64_to_cpu(ent->e_cluster)) + |
| 213 | block - searched; |
| 214 | } |
| 215 | searched += numblocks; |
| 216 | ent++; |
| 217 | } |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | static int omfs_get_block(struct inode *inode, sector_t block, |
| 222 | struct buffer_head *bh_result, int create) |
| 223 | { |
| 224 | struct buffer_head *bh; |
| 225 | sector_t next, offset; |
| 226 | int ret; |
| 227 | u64 new_block; |
| 228 | int extent_count; |
| 229 | struct omfs_extent *oe; |
| 230 | struct omfs_extent_entry *entry; |
| 231 | struct omfs_sb_info *sbi = OMFS_SB(inode->i_sb); |
| 232 | int max_blocks = bh_result->b_size >> inode->i_blkbits; |
| 233 | int remain; |
| 234 | |
| 235 | ret = -EIO; |
| 236 | bh = sb_bread(inode->i_sb, clus_to_blk(sbi, inode->i_ino)); |
| 237 | if (!bh) |
| 238 | goto out; |
| 239 | |
| 240 | oe = (struct omfs_extent *)(&bh->b_data[OMFS_EXTENT_START]); |
| 241 | next = inode->i_ino; |
| 242 | |
| 243 | for (;;) { |
| 244 | |
| 245 | if (omfs_is_bad(sbi, (struct omfs_header *) bh->b_data, next)) |
| 246 | goto out_brelse; |
| 247 | |
| 248 | extent_count = be32_to_cpu(oe->e_extent_count); |
| 249 | next = be64_to_cpu(oe->e_next); |
| 250 | entry = &oe->e_entry; |
| 251 | |
| 252 | offset = find_block(inode, entry, block, extent_count, &remain); |
| 253 | if (offset > 0) { |
| 254 | ret = 0; |
| 255 | map_bh(bh_result, inode->i_sb, offset); |
| 256 | if (remain > max_blocks) |
| 257 | remain = max_blocks; |
| 258 | bh_result->b_size = (remain << inode->i_blkbits); |
| 259 | goto out_brelse; |
| 260 | } |
| 261 | if (next == ~0) |
| 262 | break; |
| 263 | |
| 264 | brelse(bh); |
| 265 | bh = sb_bread(inode->i_sb, clus_to_blk(sbi, next)); |
| 266 | if (!bh) |
| 267 | goto out; |
| 268 | oe = (struct omfs_extent *) (&bh->b_data[OMFS_EXTENT_CONT]); |
| 269 | } |
| 270 | if (create) { |
| 271 | ret = omfs_grow_extent(inode, oe, &new_block); |
| 272 | if (ret == 0) { |
| 273 | mark_buffer_dirty(bh); |
| 274 | mark_inode_dirty(inode); |
| 275 | map_bh(bh_result, inode->i_sb, |
| 276 | clus_to_blk(sbi, new_block)); |
| 277 | } |
| 278 | } |
| 279 | out_brelse: |
| 280 | brelse(bh); |
| 281 | out: |
| 282 | return ret; |
| 283 | } |
| 284 | |
| 285 | static int omfs_readpage(struct file *file, struct page *page) |
| 286 | { |
| 287 | return block_read_full_page(page, omfs_get_block); |
| 288 | } |
| 289 | |
| 290 | static int omfs_readpages(struct file *file, struct address_space *mapping, |
| 291 | struct list_head *pages, unsigned nr_pages) |
| 292 | { |
| 293 | return mpage_readpages(mapping, pages, nr_pages, omfs_get_block); |
| 294 | } |
| 295 | |
| 296 | static int omfs_writepage(struct page *page, struct writeback_control *wbc) |
| 297 | { |
| 298 | return block_write_full_page(page, omfs_get_block, wbc); |
| 299 | } |
| 300 | |
| 301 | static int |
| 302 | omfs_writepages(struct address_space *mapping, struct writeback_control *wbc) |
| 303 | { |
| 304 | return mpage_writepages(mapping, wbc, omfs_get_block); |
| 305 | } |
| 306 | |
| 307 | static int omfs_write_begin(struct file *file, struct address_space *mapping, |
| 308 | loff_t pos, unsigned len, unsigned flags, |
| 309 | struct page **pagep, void **fsdata) |
| 310 | { |
| 311 | *pagep = NULL; |
| 312 | return block_write_begin(file, mapping, pos, len, flags, |
| 313 | pagep, fsdata, omfs_get_block); |
| 314 | } |
| 315 | |
| 316 | static sector_t omfs_bmap(struct address_space *mapping, sector_t block) |
| 317 | { |
| 318 | return generic_block_bmap(mapping, block, omfs_get_block); |
| 319 | } |
| 320 | |
| 321 | struct file_operations omfs_file_operations = { |
| 322 | .llseek = generic_file_llseek, |
| 323 | .read = do_sync_read, |
| 324 | .write = do_sync_write, |
| 325 | .aio_read = generic_file_aio_read, |
| 326 | .aio_write = generic_file_aio_write, |
| 327 | .mmap = generic_file_mmap, |
| 328 | .fsync = omfs_sync_file, |
| 329 | .splice_read = generic_file_splice_read, |
| 330 | }; |
| 331 | |
| 332 | struct inode_operations omfs_file_inops = { |
| 333 | .truncate = omfs_truncate |
| 334 | }; |
| 335 | |
| 336 | struct address_space_operations omfs_aops = { |
| 337 | .readpage = omfs_readpage, |
| 338 | .readpages = omfs_readpages, |
| 339 | .writepage = omfs_writepage, |
| 340 | .writepages = omfs_writepages, |
| 341 | .sync_page = block_sync_page, |
| 342 | .write_begin = omfs_write_begin, |
| 343 | .write_end = generic_write_end, |
| 344 | .bmap = omfs_bmap, |
| 345 | }; |
| 346 | |