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> |
Dmitri Vorobiev | f433dc5 | 2007-11-14 16:59:47 -0800 | [diff] [blame] | 5 | * |
| 6 | * Make the file block allocation algorithm understand the size |
| 7 | * of the underlying block device. |
| 8 | * Copyright (C) 2007 Dmitri Vorobiev <dmitri.vorobiev@gmail.com> |
| 9 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <linux/fs.h> |
| 13 | #include <linux/buffer_head.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include "bfs.h" |
| 15 | |
| 16 | #undef DEBUG |
| 17 | |
| 18 | #ifdef DEBUG |
| 19 | #define dprintf(x...) printf(x) |
| 20 | #else |
| 21 | #define dprintf(x...) |
| 22 | #endif |
| 23 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 24 | const struct file_operations bfs_file_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | .llseek = generic_file_llseek, |
Badari Pulavarty | 543ade1 | 2006-09-30 23:28:48 -0700 | [diff] [blame] | 26 | .read = do_sync_read, |
| 27 | .aio_read = generic_file_aio_read, |
| 28 | .write = do_sync_write, |
| 29 | .aio_write = generic_file_aio_write, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | .mmap = generic_file_mmap, |
Jens Axboe | 5ffc4ef | 2007-06-01 11:49:19 +0200 | [diff] [blame] | 31 | .splice_read = generic_file_splice_read, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | }; |
| 33 | |
Dmitri Vorobiev | f433dc5 | 2007-11-14 16:59:47 -0800 | [diff] [blame] | 34 | static int bfs_move_block(unsigned long from, unsigned long to, |
| 35 | struct super_block *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | { |
| 37 | struct buffer_head *bh, *new; |
| 38 | |
| 39 | bh = sb_bread(sb, from); |
| 40 | if (!bh) |
| 41 | return -EIO; |
| 42 | new = sb_getblk(sb, to); |
| 43 | memcpy(new->b_data, bh->b_data, bh->b_size); |
| 44 | mark_buffer_dirty(new); |
| 45 | bforget(bh); |
| 46 | brelse(new); |
| 47 | return 0; |
| 48 | } |
| 49 | |
Andrew Stribblehill | fac92be | 2005-09-09 13:02:04 -0700 | [diff] [blame] | 50 | static int bfs_move_blocks(struct super_block *sb, unsigned long start, |
Dmitri Vorobiev | f433dc5 | 2007-11-14 16:59:47 -0800 | [diff] [blame] | 51 | unsigned long end, unsigned long where) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | { |
| 53 | unsigned long i; |
| 54 | |
| 55 | dprintf("%08lx-%08lx->%08lx\n", start, end, where); |
| 56 | for (i = start; i <= end; i++) |
| 57 | if(bfs_move_block(i, where + i, sb)) { |
Dmitri Vorobiev | f433dc5 | 2007-11-14 16:59:47 -0800 | [diff] [blame] | 58 | dprintf("failed to move block %08lx -> %08lx\n", i, |
| 59 | where + i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | return -EIO; |
| 61 | } |
| 62 | return 0; |
| 63 | } |
| 64 | |
Dmitri Vorobiev | f433dc5 | 2007-11-14 16:59:47 -0800 | [diff] [blame] | 65 | static int bfs_get_block(struct inode *inode, sector_t block, |
| 66 | struct buffer_head *bh_result, int create) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | { |
Andrew Stribblehill | fac92be | 2005-09-09 13:02:04 -0700 | [diff] [blame] | 68 | unsigned long phys; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | int err; |
| 70 | struct super_block *sb = inode->i_sb; |
| 71 | struct bfs_sb_info *info = BFS_SB(sb); |
| 72 | struct bfs_inode_info *bi = BFS_I(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | phys = bi->i_sblock + block; |
| 75 | if (!create) { |
| 76 | if (phys <= bi->i_eblock) { |
Andrew Stribblehill | fac92be | 2005-09-09 13:02:04 -0700 | [diff] [blame] | 77 | dprintf("c=%d, b=%08lx, phys=%09lx (granted)\n", |
| 78 | create, (unsigned long)block, phys); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | map_bh(bh_result, sb, phys); |
| 80 | } |
| 81 | return 0; |
| 82 | } |
| 83 | |
Dmitri Vorobiev | f433dc5 | 2007-11-14 16:59:47 -0800 | [diff] [blame] | 84 | /* |
| 85 | * If the file is not empty and the requested block is within the |
| 86 | * range of blocks allocated for this file, we can grant it. |
| 87 | */ |
| 88 | if (bi->i_sblock && (phys <= bi->i_eblock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | dprintf("c=%d, b=%08lx, phys=%08lx (interim block granted)\n", |
Andrew Stribblehill | fac92be | 2005-09-09 13:02:04 -0700 | [diff] [blame] | 90 | create, (unsigned long)block, phys); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | map_bh(bh_result, sb, phys); |
| 92 | return 0; |
| 93 | } |
| 94 | |
Dmitri Vorobiev | f433dc5 | 2007-11-14 16:59:47 -0800 | [diff] [blame] | 95 | /* The file will be extended, so let's see if there is enough space. */ |
| 96 | if (phys >= info->si_blocks) |
| 97 | return -ENOSPC; |
| 98 | |
| 99 | /* The rest has to be protected against itself. */ |
Dmitri Vorobiev | 3f165e4 | 2008-07-25 19:44:54 -0700 | [diff] [blame] | 100 | mutex_lock(&info->bfs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
Dmitri Vorobiev | f433dc5 | 2007-11-14 16:59:47 -0800 | [diff] [blame] | 102 | /* |
| 103 | * If the last data block for this file is the last allocated |
| 104 | * block, we can extend the file trivially, without moving it |
| 105 | * anywhere. |
| 106 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | if (bi->i_eblock == info->si_lf_eblk) { |
| 108 | dprintf("c=%d, b=%08lx, phys=%08lx (simple extension)\n", |
Andrew Stribblehill | fac92be | 2005-09-09 13:02:04 -0700 | [diff] [blame] | 109 | create, (unsigned long)block, phys); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | map_bh(bh_result, sb, phys); |
| 111 | info->si_freeb -= phys - bi->i_eblock; |
| 112 | info->si_lf_eblk = bi->i_eblock = phys; |
| 113 | mark_inode_dirty(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | err = 0; |
| 115 | goto out; |
| 116 | } |
| 117 | |
Dmitri Vorobiev | f433dc5 | 2007-11-14 16:59:47 -0800 | [diff] [blame] | 118 | /* Ok, we have to move this entire file to the next free block. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | phys = info->si_lf_eblk + 1; |
Dmitri Vorobiev | f433dc5 | 2007-11-14 16:59:47 -0800 | [diff] [blame] | 120 | if (phys + block >= info->si_blocks) { |
| 121 | err = -ENOSPC; |
| 122 | goto out; |
| 123 | } |
| 124 | |
| 125 | if (bi->i_sblock) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | err = bfs_move_blocks(inode->i_sb, bi->i_sblock, |
Dmitri Vorobiev | f433dc5 | 2007-11-14 16:59:47 -0800 | [diff] [blame] | 127 | bi->i_eblock, phys); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | if (err) { |
Dmitri Vorobiev | f433dc5 | 2007-11-14 16:59:47 -0800 | [diff] [blame] | 129 | dprintf("failed to move ino=%08lx -> fs corruption\n", |
| 130 | inode->i_ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | goto out; |
| 132 | } |
| 133 | } else |
| 134 | err = 0; |
| 135 | |
Andrew Stribblehill | fac92be | 2005-09-09 13:02:04 -0700 | [diff] [blame] | 136 | dprintf("c=%d, b=%08lx, phys=%08lx (moved)\n", |
| 137 | create, (unsigned long)block, phys); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | bi->i_sblock = phys; |
| 139 | phys += block; |
| 140 | info->si_lf_eblk = bi->i_eblock = phys; |
| 141 | |
Dmitri Vorobiev | f433dc5 | 2007-11-14 16:59:47 -0800 | [diff] [blame] | 142 | /* |
| 143 | * This assumes nothing can write the inode back while we are here |
| 144 | * and thus update inode->i_blocks! (XXX) |
| 145 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | info->si_freeb -= bi->i_eblock - bi->i_sblock + 1 - inode->i_blocks; |
| 147 | mark_inode_dirty(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | map_bh(bh_result, sb, phys); |
| 149 | out: |
Dmitri Vorobiev | 3f165e4 | 2008-07-25 19:44:54 -0700 | [diff] [blame] | 150 | mutex_unlock(&info->bfs_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | return err; |
| 152 | } |
| 153 | |
| 154 | static int bfs_writepage(struct page *page, struct writeback_control *wbc) |
| 155 | { |
| 156 | return block_write_full_page(page, bfs_get_block, wbc); |
| 157 | } |
| 158 | |
| 159 | static int bfs_readpage(struct file *file, struct page *page) |
| 160 | { |
| 161 | return block_read_full_page(page, bfs_get_block); |
| 162 | } |
| 163 | |
Nick Piggin | eedcbba | 2007-10-16 01:25:11 -0700 | [diff] [blame] | 164 | static int bfs_write_begin(struct file *file, struct address_space *mapping, |
| 165 | loff_t pos, unsigned len, unsigned flags, |
| 166 | struct page **pagep, void **fsdata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | { |
Christoph Hellwig | 155130a | 2010-06-04 11:29:58 +0200 | [diff] [blame] | 168 | int ret; |
| 169 | |
| 170 | ret = block_write_begin(mapping, pos, len, flags, pagep, |
| 171 | bfs_get_block); |
| 172 | if (unlikely(ret)) { |
| 173 | loff_t isize = mapping->host->i_size; |
| 174 | if (pos + len > isize) |
| 175 | vmtruncate(mapping->host, isize); |
| 176 | } |
| 177 | |
| 178 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | static sector_t bfs_bmap(struct address_space *mapping, sector_t block) |
| 182 | { |
| 183 | return generic_block_bmap(mapping, block, bfs_get_block); |
| 184 | } |
| 185 | |
Christoph Hellwig | f5e54d6 | 2006-06-28 04:26:44 -0700 | [diff] [blame] | 186 | const struct address_space_operations bfs_aops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | .readpage = bfs_readpage, |
| 188 | .writepage = bfs_writepage, |
Nick Piggin | eedcbba | 2007-10-16 01:25:11 -0700 | [diff] [blame] | 189 | .write_begin = bfs_write_begin, |
| 190 | .write_end = generic_write_end, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | .bmap = bfs_bmap, |
| 192 | }; |
| 193 | |
Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 194 | const struct inode_operations bfs_file_inops; |