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