Chris Mason | 6cbd557 | 2007-06-12 09:07:21 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 Oracle. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public |
| 6 | * License v2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public |
| 14 | * License along with this program; if not, write to the |
| 15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 16 | * Boston, MA 021110-1307, USA. |
| 17 | */ |
| 18 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 19 | #include <linux/fs.h> |
| 20 | #include <linux/pagemap.h> |
| 21 | #include <linux/highmem.h> |
| 22 | #include <linux/time.h> |
| 23 | #include <linux/init.h> |
| 24 | #include <linux/string.h> |
| 25 | #include <linux/smp_lock.h> |
| 26 | #include <linux/backing-dev.h> |
| 27 | #include <linux/mpage.h> |
| 28 | #include <linux/swap.h> |
| 29 | #include <linux/writeback.h> |
| 30 | #include <linux/statfs.h> |
| 31 | #include <linux/compat.h> |
Chris Mason | 92fee66 | 2007-07-25 12:31:35 -0400 | [diff] [blame] | 32 | #include <linux/version.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 33 | #include "ctree.h" |
| 34 | #include "disk-io.h" |
| 35 | #include "transaction.h" |
| 36 | #include "btrfs_inode.h" |
| 37 | #include "ioctl.h" |
| 38 | #include "print-tree.h" |
| 39 | |
| 40 | |
| 41 | static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes, |
| 42 | struct page **prepared_pages, |
| 43 | const char __user * buf) |
| 44 | { |
| 45 | long page_fault = 0; |
| 46 | int i; |
| 47 | int offset = pos & (PAGE_CACHE_SIZE - 1); |
| 48 | |
| 49 | for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) { |
| 50 | size_t count = min_t(size_t, |
| 51 | PAGE_CACHE_SIZE - offset, write_bytes); |
| 52 | struct page *page = prepared_pages[i]; |
| 53 | fault_in_pages_readable(buf, count); |
| 54 | |
| 55 | /* Copy data from userspace to the current page */ |
| 56 | kmap(page); |
| 57 | page_fault = __copy_from_user(page_address(page) + offset, |
| 58 | buf, count); |
| 59 | /* Flush processor's dcache for this page */ |
| 60 | flush_dcache_page(page); |
| 61 | kunmap(page); |
| 62 | buf += count; |
| 63 | write_bytes -= count; |
| 64 | |
| 65 | if (page_fault) |
| 66 | break; |
| 67 | } |
| 68 | return page_fault ? -EFAULT : 0; |
| 69 | } |
| 70 | |
| 71 | static void btrfs_drop_pages(struct page **pages, size_t num_pages) |
| 72 | { |
| 73 | size_t i; |
| 74 | for (i = 0; i < num_pages; i++) { |
| 75 | if (!pages[i]) |
| 76 | break; |
| 77 | unlock_page(pages[i]); |
| 78 | mark_page_accessed(pages[i]); |
| 79 | page_cache_release(pages[i]); |
| 80 | } |
| 81 | } |
| 82 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 83 | static int insert_inline_extent(struct btrfs_trans_handle *trans, |
| 84 | struct btrfs_root *root, struct inode *inode, |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 85 | u64 offset, ssize_t size, |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 86 | struct page *page, size_t page_offset) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 87 | { |
| 88 | struct btrfs_key key; |
| 89 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 90 | struct extent_buffer *leaf; |
| 91 | char *kaddr; |
| 92 | unsigned long ptr; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 93 | struct btrfs_file_extent_item *ei; |
| 94 | u32 datasize; |
| 95 | int err = 0; |
| 96 | int ret; |
| 97 | |
| 98 | path = btrfs_alloc_path(); |
| 99 | if (!path) |
| 100 | return -ENOMEM; |
| 101 | |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 102 | btrfs_set_trans_block_group(trans, inode); |
| 103 | |
| 104 | key.objectid = inode->i_ino; |
| 105 | key.offset = offset; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 106 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
| 107 | BUG_ON(size >= PAGE_CACHE_SIZE); |
| 108 | datasize = btrfs_file_extent_calc_inline_size(size); |
| 109 | |
| 110 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 111 | datasize); |
| 112 | if (ret) { |
| 113 | err = ret; |
| 114 | goto fail; |
| 115 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 116 | leaf = path->nodes[0]; |
| 117 | ei = btrfs_item_ptr(leaf, path->slots[0], |
| 118 | struct btrfs_file_extent_item); |
| 119 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); |
| 120 | btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 121 | ptr = btrfs_file_extent_inline_start(ei); |
| 122 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame^] | 123 | kaddr = kmap_atomic(page, KM_USER1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 124 | write_extent_buffer(leaf, kaddr + page_offset, ptr, size); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame^] | 125 | kunmap_atomic(kaddr, KM_USER1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 126 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 127 | fail: |
| 128 | btrfs_free_path(path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 129 | return err; |
| 130 | } |
| 131 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 132 | static int dirty_and_release_pages(struct btrfs_trans_handle *trans, |
| 133 | struct btrfs_root *root, |
| 134 | struct file *file, |
| 135 | struct page **pages, |
| 136 | size_t num_pages, |
| 137 | loff_t pos, |
| 138 | size_t write_bytes) |
| 139 | { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 140 | int err = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 141 | int i; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 142 | struct inode *inode = file->f_path.dentry->d_inode; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 143 | struct extent_map *em; |
| 144 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame^] | 145 | u64 hint_byte; |
| 146 | u64 num_bytes; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 147 | u64 start_pos; |
| 148 | u64 end_of_last_block; |
| 149 | u64 end_pos = pos + write_bytes; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame^] | 150 | u32 inline_size; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 151 | loff_t isize = i_size_read(inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 152 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 153 | em = alloc_extent_map(GFP_NOFS); |
| 154 | if (!em) |
| 155 | return -ENOMEM; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 156 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 157 | em->bdev = inode->i_sb->s_bdev; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 158 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 159 | start_pos = pos & ~((u64)root->sectorsize - 1); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame^] | 160 | num_bytes = (write_bytes + pos - start_pos + |
| 161 | root->sectorsize - 1) & ~((u64)root->sectorsize - 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 162 | |
Chris Mason | 011410b | 2007-09-10 19:58:36 -0400 | [diff] [blame] | 163 | down_read(&BTRFS_I(inode)->root->snap_sem); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame^] | 164 | end_of_last_block = start_pos + num_bytes - 1; |
| 165 | |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 166 | lock_extent(em_tree, start_pos, end_of_last_block, GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 167 | mutex_lock(&root->fs_info->fs_mutex); |
| 168 | trans = btrfs_start_transaction(root, 1); |
| 169 | if (!trans) { |
| 170 | err = -ENOMEM; |
| 171 | goto out_unlock; |
| 172 | } |
| 173 | btrfs_set_trans_block_group(trans, inode); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame^] | 174 | inode->i_blocks += num_bytes >> 9; |
| 175 | hint_byte = 0; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 176 | |
| 177 | if ((end_of_last_block & 4095) == 0) { |
Christoph Hellwig | 9433063 | 2007-09-10 20:02:22 -0400 | [diff] [blame] | 178 | printk("strange end of last %Lu %zu %Lu\n", start_pos, write_bytes, end_of_last_block); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 179 | } |
| 180 | set_extent_uptodate(em_tree, start_pos, end_of_last_block, GFP_NOFS); |
| 181 | |
| 182 | /* FIXME...EIEIO, ENOSPC and more */ |
| 183 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 184 | /* insert any holes we need to create */ |
| 185 | if (inode->i_size < start_pos) { |
| 186 | u64 last_pos_in_file; |
| 187 | u64 hole_size; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 188 | u64 mask = root->sectorsize - 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 189 | last_pos_in_file = (isize + mask) & ~mask; |
| 190 | hole_size = (start_pos - last_pos_in_file + mask) & ~mask; |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 191 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 192 | if (last_pos_in_file < start_pos) { |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 193 | err = btrfs_drop_extents(trans, root, inode, |
| 194 | last_pos_in_file, |
| 195 | last_pos_in_file + hole_size, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame^] | 196 | &hint_byte); |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 197 | if (err) |
| 198 | goto failed; |
| 199 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 200 | err = btrfs_insert_file_extent(trans, root, |
| 201 | inode->i_ino, |
| 202 | last_pos_in_file, |
| 203 | 0, 0, hole_size); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 204 | } |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 205 | if (err) |
| 206 | goto failed; |
| 207 | } |
| 208 | |
| 209 | /* |
| 210 | * either allocate an extent for the new bytes or setup the key |
| 211 | * to show we are doing inline data in the extent |
| 212 | */ |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame^] | 213 | inline_size = end_pos - start_pos; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 214 | if (isize >= PAGE_CACHE_SIZE || pos + write_bytes < inode->i_size || |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame^] | 215 | inline_size >= BTRFS_MAX_INLINE_DATA_SIZE(root) || |
| 216 | inline_size >= PAGE_CACHE_SIZE) { |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 217 | u64 last_end; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 218 | for (i = 0; i < num_pages; i++) { |
| 219 | struct page *p = pages[i]; |
| 220 | SetPageUptodate(p); |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 221 | set_page_dirty(p); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 222 | } |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 223 | last_end = pages[num_pages -1]->index << PAGE_CACHE_SHIFT; |
| 224 | last_end += PAGE_CACHE_SIZE - 1; |
| 225 | set_extent_delalloc(em_tree, start_pos, end_of_last_block, |
| 226 | GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 227 | } else { |
| 228 | struct page *p = pages[0]; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 229 | /* step one, delete the existing extents in this range */ |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 230 | err = btrfs_drop_extents(trans, root, inode, start_pos, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 231 | (pos + write_bytes + root->sectorsize -1) & |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame^] | 232 | ~((u64)root->sectorsize - 1), &hint_byte); |
Chris Mason | 2bf5a72 | 2007-08-30 11:54:02 -0400 | [diff] [blame] | 233 | if (err) |
| 234 | goto failed; |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 235 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 236 | err = insert_inline_extent(trans, root, inode, start_pos, |
| 237 | end_pos - start_pos, p, 0); |
| 238 | BUG_ON(err); |
| 239 | em->start = start_pos; |
Yan | 6af858b | 2007-09-14 10:23:29 -0400 | [diff] [blame] | 240 | em->end = end_pos - 1; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 241 | em->block_start = EXTENT_MAP_INLINE; |
| 242 | em->block_end = EXTENT_MAP_INLINE; |
| 243 | add_extent_mapping(em_tree, em); |
| 244 | } |
| 245 | if (end_pos > isize) { |
| 246 | i_size_write(inode, end_pos); |
| 247 | btrfs_update_inode(trans, root, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 248 | } |
| 249 | failed: |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 250 | err = btrfs_end_transaction(trans, root); |
| 251 | out_unlock: |
| 252 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 253 | unlock_extent(em_tree, start_pos, end_of_last_block, GFP_NOFS); |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 254 | free_extent_map(em); |
Chris Mason | 011410b | 2007-09-10 19:58:36 -0400 | [diff] [blame] | 255 | up_read(&BTRFS_I(inode)->root->snap_sem); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 256 | return err; |
| 257 | } |
| 258 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 259 | int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end) |
| 260 | { |
| 261 | struct extent_map *em; |
| 262 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
| 263 | |
| 264 | while(1) { |
| 265 | em = lookup_extent_mapping(em_tree, start, end); |
| 266 | if (!em) |
| 267 | break; |
| 268 | remove_extent_mapping(em_tree, em); |
| 269 | /* once for us */ |
| 270 | free_extent_map(em); |
| 271 | /* once for the tree*/ |
| 272 | free_extent_map(em); |
| 273 | } |
| 274 | return 0; |
| 275 | } |
| 276 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 277 | /* |
| 278 | * this is very complex, but the basic idea is to drop all extents |
| 279 | * in the range start - end. hint_block is filled in with a block number |
| 280 | * that would be a good hint to the block allocator for this file. |
| 281 | * |
| 282 | * If an extent intersects the range but is not entirely inside the range |
| 283 | * it is either truncated or split. Anything entirely inside the range |
| 284 | * is deleted from the tree. |
| 285 | */ |
| 286 | int btrfs_drop_extents(struct btrfs_trans_handle *trans, |
| 287 | struct btrfs_root *root, struct inode *inode, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame^] | 288 | u64 start, u64 end, u64 *hint_byte) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 289 | { |
| 290 | int ret; |
| 291 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 292 | struct extent_buffer *leaf; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 293 | int slot; |
| 294 | struct btrfs_file_extent_item *extent; |
| 295 | u64 extent_end = 0; |
| 296 | int keep; |
| 297 | struct btrfs_file_extent_item old; |
| 298 | struct btrfs_path *path; |
| 299 | u64 search_start = start; |
| 300 | int bookend; |
| 301 | int found_type; |
| 302 | int found_extent; |
| 303 | int found_inline; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 304 | int recow; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 305 | |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 306 | btrfs_drop_extent_cache(inode, start, end - 1); |
| 307 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 308 | path = btrfs_alloc_path(); |
| 309 | if (!path) |
| 310 | return -ENOMEM; |
| 311 | while(1) { |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 312 | recow = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 313 | btrfs_release_path(root, path); |
| 314 | ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino, |
| 315 | search_start, -1); |
| 316 | if (ret < 0) |
| 317 | goto out; |
| 318 | if (ret > 0) { |
| 319 | if (path->slots[0] == 0) { |
| 320 | ret = 0; |
| 321 | goto out; |
| 322 | } |
| 323 | path->slots[0]--; |
| 324 | } |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 325 | next_slot: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 326 | keep = 0; |
| 327 | bookend = 0; |
| 328 | found_extent = 0; |
| 329 | found_inline = 0; |
| 330 | extent = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 331 | leaf = path->nodes[0]; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 332 | slot = path->slots[0]; |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 333 | ret = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 334 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 335 | if (key.offset >= end || key.objectid != inode->i_ino) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 336 | goto out; |
| 337 | } |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 338 | if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 339 | goto out; |
| 340 | } |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 341 | if (recow) { |
| 342 | search_start = key.offset; |
| 343 | continue; |
| 344 | } |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 345 | if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) { |
| 346 | extent = btrfs_item_ptr(leaf, slot, |
| 347 | struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 348 | found_type = btrfs_file_extent_type(leaf, extent); |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 349 | if (found_type == BTRFS_FILE_EXTENT_REG) { |
| 350 | extent_end = key.offset + |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame^] | 351 | btrfs_file_extent_num_bytes(leaf, extent); |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 352 | found_extent = 1; |
| 353 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 354 | struct btrfs_item *item; |
| 355 | item = btrfs_item_nr(leaf, slot); |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 356 | found_inline = 1; |
| 357 | extent_end = key.offset + |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 358 | btrfs_file_extent_inline_len(leaf, item); |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 359 | } |
| 360 | } else { |
| 361 | extent_end = search_start; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | /* we found nothing we can drop */ |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 365 | if ((!found_extent && !found_inline) || |
| 366 | search_start >= extent_end) { |
| 367 | int nextret; |
| 368 | u32 nritems; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 369 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 370 | if (slot >= nritems - 1) { |
| 371 | nextret = btrfs_next_leaf(root, path); |
| 372 | if (nextret) |
| 373 | goto out; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 374 | recow = 1; |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 375 | } else { |
| 376 | path->slots[0]++; |
| 377 | } |
| 378 | goto next_slot; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | /* FIXME, there's only one inline extent allowed right now */ |
| 382 | if (found_inline) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 383 | u64 mask = root->sectorsize - 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 384 | search_start = (extent_end + mask) & ~mask; |
| 385 | } else |
| 386 | search_start = extent_end; |
| 387 | |
| 388 | if (end < extent_end && end >= key.offset) { |
| 389 | if (found_extent) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame^] | 390 | u64 disk_bytenr = |
| 391 | btrfs_file_extent_disk_bytenr(leaf, extent); |
| 392 | u64 disk_num_bytes = |
| 393 | btrfs_file_extent_disk_num_bytes(leaf, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 394 | extent); |
| 395 | read_extent_buffer(leaf, &old, |
| 396 | (unsigned long)extent, |
| 397 | sizeof(old)); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame^] | 398 | if (disk_bytenr != 0) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 399 | ret = btrfs_inc_extent_ref(trans, root, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame^] | 400 | disk_bytenr, disk_num_bytes); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 401 | BUG_ON(ret); |
| 402 | } |
| 403 | } |
| 404 | WARN_ON(found_inline); |
| 405 | bookend = 1; |
| 406 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 407 | /* truncate existing extent */ |
| 408 | if (start > key.offset) { |
| 409 | u64 new_num; |
| 410 | u64 old_num; |
| 411 | keep = 1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 412 | WARN_ON(start & (root->sectorsize - 1)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 413 | if (found_extent) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame^] | 414 | new_num = start - key.offset; |
| 415 | old_num = btrfs_file_extent_num_bytes(leaf, |
| 416 | extent); |
| 417 | *hint_byte = |
| 418 | btrfs_file_extent_disk_bytenr(leaf, |
| 419 | extent); |
| 420 | if (btrfs_file_extent_disk_bytenr(leaf, |
| 421 | extent)) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 422 | inode->i_blocks -= |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame^] | 423 | (old_num - new_num) >> 9; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 424 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame^] | 425 | btrfs_set_file_extent_num_bytes(leaf, extent, |
| 426 | new_num); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 427 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 428 | } else { |
| 429 | WARN_ON(1); |
| 430 | } |
| 431 | } |
| 432 | /* delete the entire extent */ |
| 433 | if (!keep) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame^] | 434 | u64 disk_bytenr = 0; |
| 435 | u64 disk_num_bytes = 0; |
| 436 | u64 extent_num_bytes = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 437 | if (found_extent) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame^] | 438 | disk_bytenr = |
| 439 | btrfs_file_extent_disk_bytenr(leaf, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 440 | extent); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame^] | 441 | disk_num_bytes = |
| 442 | btrfs_file_extent_disk_num_bytes(leaf, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 443 | extent); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame^] | 444 | extent_num_bytes = |
| 445 | btrfs_file_extent_num_bytes(leaf, extent); |
| 446 | *hint_byte = |
| 447 | btrfs_file_extent_disk_bytenr(leaf, |
| 448 | extent); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 449 | } |
| 450 | ret = btrfs_del_item(trans, root, path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 451 | /* TODO update progress marker and return */ |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 452 | BUG_ON(ret); |
| 453 | btrfs_release_path(root, path); |
| 454 | extent = NULL; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame^] | 455 | if (found_extent && disk_bytenr != 0) { |
| 456 | inode->i_blocks -= extent_num_bytes >> 9; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 457 | ret = btrfs_free_extent(trans, root, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame^] | 458 | disk_bytenr, |
| 459 | disk_num_bytes, 0); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | BUG_ON(ret); |
| 463 | if (!bookend && search_start >= end) { |
| 464 | ret = 0; |
| 465 | goto out; |
| 466 | } |
| 467 | if (!bookend) |
| 468 | continue; |
| 469 | } |
| 470 | /* create bookend, splitting the extent in two */ |
| 471 | if (bookend && found_extent) { |
| 472 | struct btrfs_key ins; |
| 473 | ins.objectid = inode->i_ino; |
| 474 | ins.offset = end; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 475 | btrfs_set_key_type(&ins, BTRFS_EXTENT_DATA_KEY); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 476 | btrfs_release_path(root, path); |
| 477 | ret = btrfs_insert_empty_item(trans, root, path, &ins, |
| 478 | sizeof(*extent)); |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 479 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 480 | leaf = path->nodes[0]; |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 481 | if (ret) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 482 | btrfs_print_leaf(root, leaf); |
| 483 | printk("got %d on inserting %Lu %u %Lu start %Lu end %Lu found %Lu %Lu keep was %d\n", ret , ins.objectid, ins.type, ins.offset, start, end, key.offset, extent_end, keep); |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 484 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 485 | BUG_ON(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 486 | extent = btrfs_item_ptr(leaf, path->slots[0], |
| 487 | struct btrfs_file_extent_item); |
| 488 | write_extent_buffer(leaf, &old, |
| 489 | (unsigned long)extent, sizeof(old)); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 490 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 491 | btrfs_set_file_extent_offset(leaf, extent, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame^] | 492 | le64_to_cpu(old.offset) + end - key.offset); |
| 493 | WARN_ON(le64_to_cpu(old.num_bytes) < |
| 494 | (extent_end - end)); |
| 495 | btrfs_set_file_extent_num_bytes(leaf, extent, |
| 496 | extent_end - end); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 497 | btrfs_set_file_extent_type(leaf, extent, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 498 | BTRFS_FILE_EXTENT_REG); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame^] | 499 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 500 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame^] | 501 | if (le64_to_cpu(old.disk_bytenr) != 0) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 502 | inode->i_blocks += |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame^] | 503 | btrfs_file_extent_num_bytes(leaf, |
| 504 | extent) >> 9; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 505 | } |
| 506 | ret = 0; |
| 507 | goto out; |
| 508 | } |
| 509 | } |
| 510 | out: |
| 511 | btrfs_free_path(path); |
| 512 | return ret; |
| 513 | } |
| 514 | |
| 515 | /* |
| 516 | * this gets pages into the page cache and locks them down |
| 517 | */ |
| 518 | static int prepare_pages(struct btrfs_root *root, |
| 519 | struct file *file, |
| 520 | struct page **pages, |
| 521 | size_t num_pages, |
| 522 | loff_t pos, |
| 523 | unsigned long first_index, |
| 524 | unsigned long last_index, |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 525 | size_t write_bytes) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 526 | { |
| 527 | int i; |
| 528 | unsigned long index = pos >> PAGE_CACHE_SHIFT; |
| 529 | struct inode *inode = file->f_path.dentry->d_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 530 | int err = 0; |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 531 | u64 start_pos; |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 532 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 533 | start_pos = pos & ~((u64)root->sectorsize - 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 534 | |
| 535 | memset(pages, 0, num_pages * sizeof(struct page *)); |
| 536 | |
| 537 | for (i = 0; i < num_pages; i++) { |
| 538 | pages[i] = grab_cache_page(inode->i_mapping, index + i); |
| 539 | if (!pages[i]) { |
| 540 | err = -ENOMEM; |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 541 | BUG_ON(1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 542 | } |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 543 | cancel_dirty_page(pages[i], PAGE_CACHE_SIZE); |
| 544 | wait_on_page_writeback(pages[i]); |
Christoph Hellwig | b3cfa35 | 2007-09-17 11:25:58 -0400 | [diff] [blame] | 545 | set_page_extent_mapped(pages[i]); |
Chris Mason | b888db2 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 546 | WARN_ON(!PageLocked(pages[i])); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 547 | } |
| 548 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | static ssize_t btrfs_file_write(struct file *file, const char __user *buf, |
| 552 | size_t count, loff_t *ppos) |
| 553 | { |
| 554 | loff_t pos; |
| 555 | size_t num_written = 0; |
| 556 | int err = 0; |
| 557 | int ret = 0; |
| 558 | struct inode *inode = file->f_path.dentry->d_inode; |
| 559 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 560 | struct page **pages = NULL; |
| 561 | int nrptrs; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 562 | struct page *pinned[2]; |
| 563 | unsigned long first_index; |
| 564 | unsigned long last_index; |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 565 | |
| 566 | nrptrs = min((count + PAGE_CACHE_SIZE - 1) / PAGE_CACHE_SIZE, |
| 567 | PAGE_CACHE_SIZE / (sizeof(struct page *))); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 568 | pinned[0] = NULL; |
| 569 | pinned[1] = NULL; |
| 570 | if (file->f_flags & O_DIRECT) |
| 571 | return -EINVAL; |
| 572 | pos = *ppos; |
| 573 | vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE); |
| 574 | current->backing_dev_info = inode->i_mapping->backing_dev_info; |
| 575 | err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode)); |
| 576 | if (err) |
| 577 | goto out; |
| 578 | if (count == 0) |
| 579 | goto out; |
| 580 | err = remove_suid(file->f_path.dentry); |
| 581 | if (err) |
| 582 | goto out; |
| 583 | file_update_time(file); |
| 584 | |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 585 | pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 586 | |
| 587 | mutex_lock(&inode->i_mutex); |
| 588 | first_index = pos >> PAGE_CACHE_SHIFT; |
| 589 | last_index = (pos + count) >> PAGE_CACHE_SHIFT; |
| 590 | |
| 591 | /* |
| 592 | * there are lots of better ways to do this, but this code |
| 593 | * makes sure the first and last page in the file range are |
| 594 | * up to date and ready for cow |
| 595 | */ |
| 596 | if ((pos & (PAGE_CACHE_SIZE - 1))) { |
| 597 | pinned[0] = grab_cache_page(inode->i_mapping, first_index); |
| 598 | if (!PageUptodate(pinned[0])) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 599 | ret = btrfs_readpage(NULL, pinned[0]); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 600 | BUG_ON(ret); |
| 601 | wait_on_page_locked(pinned[0]); |
| 602 | } else { |
| 603 | unlock_page(pinned[0]); |
| 604 | } |
| 605 | } |
| 606 | if ((pos + count) & (PAGE_CACHE_SIZE - 1)) { |
| 607 | pinned[1] = grab_cache_page(inode->i_mapping, last_index); |
| 608 | if (!PageUptodate(pinned[1])) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 609 | ret = btrfs_readpage(NULL, pinned[1]); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 610 | BUG_ON(ret); |
| 611 | wait_on_page_locked(pinned[1]); |
| 612 | } else { |
| 613 | unlock_page(pinned[1]); |
| 614 | } |
| 615 | } |
| 616 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 617 | while(count > 0) { |
| 618 | size_t offset = pos & (PAGE_CACHE_SIZE - 1); |
Chris Mason | 11bd143 | 2007-06-22 14:16:24 -0400 | [diff] [blame] | 619 | size_t write_bytes = min(count, nrptrs * |
| 620 | (size_t)PAGE_CACHE_SIZE - |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 621 | offset); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 622 | size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >> |
| 623 | PAGE_CACHE_SHIFT; |
| 624 | |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 625 | WARN_ON(num_pages > nrptrs); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 626 | memset(pages, 0, sizeof(pages)); |
| 627 | ret = prepare_pages(root, file, pages, num_pages, |
| 628 | pos, first_index, last_index, |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 629 | write_bytes); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 630 | if (ret) |
| 631 | goto out; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 632 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 633 | ret = btrfs_copy_from_user(pos, num_pages, |
| 634 | write_bytes, pages, buf); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 635 | if (ret) { |
| 636 | btrfs_drop_pages(pages, num_pages); |
| 637 | goto out; |
| 638 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 639 | |
| 640 | ret = dirty_and_release_pages(NULL, root, file, pages, |
| 641 | num_pages, pos, write_bytes); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 642 | btrfs_drop_pages(pages, num_pages); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 643 | if (ret) |
| 644 | goto out; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 645 | |
| 646 | buf += write_bytes; |
| 647 | count -= write_bytes; |
| 648 | pos += write_bytes; |
| 649 | num_written += write_bytes; |
| 650 | |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 651 | balance_dirty_pages_ratelimited_nr(inode->i_mapping, num_pages); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 652 | btrfs_btree_balance_dirty(root, 1); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 653 | cond_resched(); |
| 654 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 655 | mutex_unlock(&inode->i_mutex); |
| 656 | out: |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 657 | kfree(pages); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 658 | if (pinned[0]) |
| 659 | page_cache_release(pinned[0]); |
| 660 | if (pinned[1]) |
| 661 | page_cache_release(pinned[1]); |
| 662 | *ppos = pos; |
| 663 | current->backing_dev_info = NULL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 664 | return num_written ? num_written : err; |
| 665 | } |
| 666 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 667 | static int btrfs_sync_file(struct file *file, |
| 668 | struct dentry *dentry, int datasync) |
| 669 | { |
| 670 | struct inode *inode = dentry->d_inode; |
| 671 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 672 | int ret = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 673 | struct btrfs_trans_handle *trans; |
| 674 | |
| 675 | /* |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 676 | * check the transaction that last modified this inode |
| 677 | * and see if its already been committed |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 678 | */ |
| 679 | mutex_lock(&root->fs_info->fs_mutex); |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 680 | if (!BTRFS_I(inode)->last_trans) |
| 681 | goto out; |
| 682 | mutex_lock(&root->fs_info->trans_mutex); |
| 683 | if (BTRFS_I(inode)->last_trans <= |
| 684 | root->fs_info->last_trans_committed) { |
| 685 | BTRFS_I(inode)->last_trans = 0; |
| 686 | mutex_unlock(&root->fs_info->trans_mutex); |
| 687 | goto out; |
| 688 | } |
| 689 | mutex_unlock(&root->fs_info->trans_mutex); |
| 690 | |
| 691 | /* |
Chris Mason | a52d9a8 | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 692 | * ok we haven't committed the transaction yet, lets do a commit |
| 693 | */ |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 694 | trans = btrfs_start_transaction(root, 1); |
| 695 | if (!trans) { |
| 696 | ret = -ENOMEM; |
| 697 | goto out; |
| 698 | } |
| 699 | ret = btrfs_commit_transaction(trans, root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 700 | out: |
Josef Bacik | 15ee9bc | 2007-08-10 16:22:09 -0400 | [diff] [blame] | 701 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 702 | return ret > 0 ? EIO : ret; |
| 703 | } |
| 704 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 705 | static struct vm_operations_struct btrfs_file_vm_ops = { |
Chris Mason | 92fee66 | 2007-07-25 12:31:35 -0400 | [diff] [blame] | 706 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23) |
| 707 | .nopage = filemap_nopage, |
| 708 | .populate = filemap_populate, |
| 709 | #else |
| 710 | .fault = filemap_fault, |
| 711 | #endif |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 712 | .page_mkwrite = btrfs_page_mkwrite, |
| 713 | }; |
| 714 | |
| 715 | static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma) |
| 716 | { |
| 717 | vma->vm_ops = &btrfs_file_vm_ops; |
| 718 | file_accessed(filp); |
| 719 | return 0; |
| 720 | } |
| 721 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 722 | struct file_operations btrfs_file_operations = { |
| 723 | .llseek = generic_file_llseek, |
| 724 | .read = do_sync_read, |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 725 | .aio_read = generic_file_aio_read, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 726 | .write = btrfs_file_write, |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 727 | .mmap = btrfs_file_mmap, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 728 | .open = generic_file_open, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 729 | .fsync = btrfs_sync_file, |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 730 | .unlocked_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 731 | #ifdef CONFIG_COMPAT |
Christoph Hellwig | 34287aa | 2007-09-14 10:22:47 -0400 | [diff] [blame] | 732 | .compat_ioctl = btrfs_ioctl, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 733 | #endif |
| 734 | }; |
| 735 | |