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