Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * fs/bfs/file.c |
| 3 | * BFS file operations. |
| 4 | * Copyright (C) 1999,2000 Tigran Aivazian <tigran@veritas.com> |
| 5 | */ |
| 6 | |
| 7 | #include <linux/fs.h> |
| 8 | #include <linux/buffer_head.h> |
| 9 | #include <linux/smp_lock.h> |
| 10 | #include "bfs.h" |
| 11 | |
| 12 | #undef DEBUG |
| 13 | |
| 14 | #ifdef DEBUG |
| 15 | #define dprintf(x...) printf(x) |
| 16 | #else |
| 17 | #define dprintf(x...) |
| 18 | #endif |
| 19 | |
| 20 | struct file_operations bfs_file_operations = { |
| 21 | .llseek = generic_file_llseek, |
| 22 | .read = generic_file_read, |
| 23 | .write = generic_file_write, |
| 24 | .mmap = generic_file_mmap, |
| 25 | .sendfile = generic_file_sendfile, |
| 26 | }; |
| 27 | |
| 28 | static int bfs_move_block(unsigned long from, unsigned long to, struct super_block *sb) |
| 29 | { |
| 30 | struct buffer_head *bh, *new; |
| 31 | |
| 32 | bh = sb_bread(sb, from); |
| 33 | if (!bh) |
| 34 | return -EIO; |
| 35 | new = sb_getblk(sb, to); |
| 36 | memcpy(new->b_data, bh->b_data, bh->b_size); |
| 37 | mark_buffer_dirty(new); |
| 38 | bforget(bh); |
| 39 | brelse(new); |
| 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | static int bfs_move_blocks(struct super_block *sb, unsigned long start, unsigned long end, |
| 44 | unsigned long where) |
| 45 | { |
| 46 | unsigned long i; |
| 47 | |
| 48 | dprintf("%08lx-%08lx->%08lx\n", start, end, where); |
| 49 | for (i = start; i <= end; i++) |
| 50 | if(bfs_move_block(i, where + i, sb)) { |
| 51 | dprintf("failed to move block %08lx -> %08lx\n", i, where + i); |
| 52 | return -EIO; |
| 53 | } |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | static int bfs_get_block(struct inode * inode, sector_t block, |
| 58 | struct buffer_head * bh_result, int create) |
| 59 | { |
| 60 | long phys; |
| 61 | int err; |
| 62 | struct super_block *sb = inode->i_sb; |
| 63 | struct bfs_sb_info *info = BFS_SB(sb); |
| 64 | struct bfs_inode_info *bi = BFS_I(inode); |
| 65 | struct buffer_head *sbh = info->si_sbh; |
| 66 | |
| 67 | if (block < 0 || block > info->si_blocks) |
| 68 | return -EIO; |
| 69 | |
| 70 | phys = bi->i_sblock + block; |
| 71 | if (!create) { |
| 72 | if (phys <= bi->i_eblock) { |
| 73 | dprintf("c=%d, b=%08lx, phys=%08lx (granted)\n", create, block, phys); |
| 74 | map_bh(bh_result, sb, phys); |
| 75 | } |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | /* if the file is not empty and the requested block is within the range |
| 80 | of blocks allocated for this file, we can grant it */ |
| 81 | if (inode->i_size && phys <= bi->i_eblock) { |
| 82 | dprintf("c=%d, b=%08lx, phys=%08lx (interim block granted)\n", |
| 83 | create, block, phys); |
| 84 | map_bh(bh_result, sb, phys); |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | /* the rest has to be protected against itself */ |
| 89 | lock_kernel(); |
| 90 | |
| 91 | /* if the last data block for this file is the last allocated block, we can |
| 92 | extend the file trivially, without moving it anywhere */ |
| 93 | if (bi->i_eblock == info->si_lf_eblk) { |
| 94 | dprintf("c=%d, b=%08lx, phys=%08lx (simple extension)\n", |
| 95 | create, block, phys); |
| 96 | map_bh(bh_result, sb, phys); |
| 97 | info->si_freeb -= phys - bi->i_eblock; |
| 98 | info->si_lf_eblk = bi->i_eblock = phys; |
| 99 | mark_inode_dirty(inode); |
| 100 | mark_buffer_dirty(sbh); |
| 101 | err = 0; |
| 102 | goto out; |
| 103 | } |
| 104 | |
| 105 | /* Ok, we have to move this entire file to the next free block */ |
| 106 | phys = info->si_lf_eblk + 1; |
| 107 | if (bi->i_sblock) { /* if data starts on block 0 then there is no data */ |
| 108 | err = bfs_move_blocks(inode->i_sb, bi->i_sblock, |
| 109 | bi->i_eblock, phys); |
| 110 | if (err) { |
| 111 | dprintf("failed to move ino=%08lx -> fs corruption\n", inode->i_ino); |
| 112 | goto out; |
| 113 | } |
| 114 | } else |
| 115 | err = 0; |
| 116 | |
| 117 | dprintf("c=%d, b=%08lx, phys=%08lx (moved)\n", create, block, phys); |
| 118 | bi->i_sblock = phys; |
| 119 | phys += block; |
| 120 | info->si_lf_eblk = bi->i_eblock = phys; |
| 121 | |
| 122 | /* this assumes nothing can write the inode back while we are here |
| 123 | * and thus update inode->i_blocks! (XXX)*/ |
| 124 | info->si_freeb -= bi->i_eblock - bi->i_sblock + 1 - inode->i_blocks; |
| 125 | mark_inode_dirty(inode); |
| 126 | mark_buffer_dirty(sbh); |
| 127 | map_bh(bh_result, sb, phys); |
| 128 | out: |
| 129 | unlock_kernel(); |
| 130 | return err; |
| 131 | } |
| 132 | |
| 133 | static int bfs_writepage(struct page *page, struct writeback_control *wbc) |
| 134 | { |
| 135 | return block_write_full_page(page, bfs_get_block, wbc); |
| 136 | } |
| 137 | |
| 138 | static int bfs_readpage(struct file *file, struct page *page) |
| 139 | { |
| 140 | return block_read_full_page(page, bfs_get_block); |
| 141 | } |
| 142 | |
| 143 | static int bfs_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to) |
| 144 | { |
| 145 | return block_prepare_write(page, from, to, bfs_get_block); |
| 146 | } |
| 147 | |
| 148 | static sector_t bfs_bmap(struct address_space *mapping, sector_t block) |
| 149 | { |
| 150 | return generic_block_bmap(mapping, block, bfs_get_block); |
| 151 | } |
| 152 | |
| 153 | struct address_space_operations bfs_aops = { |
| 154 | .readpage = bfs_readpage, |
| 155 | .writepage = bfs_writepage, |
| 156 | .sync_page = block_sync_page, |
| 157 | .prepare_write = bfs_prepare_write, |
| 158 | .commit_write = generic_commit_write, |
| 159 | .bmap = bfs_bmap, |
| 160 | }; |
| 161 | |
| 162 | struct inode_operations bfs_file_inops; |