]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - fs/btrfs/file.c
Btrfs: Use the extent map cache to find the logical disk block during data retries
[mirror_ubuntu-bionic-kernel.git] / fs / btrfs / file.c
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
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>
32 #include <linux/version.h>
33 #include "ctree.h"
34 #include "disk-io.h"
35 #include "transaction.h"
36 #include "btrfs_inode.h"
37 #include "ordered-data.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
84 static int noinline insert_inline_extent(struct btrfs_trans_handle *trans,
85 struct btrfs_root *root, struct inode *inode,
86 u64 offset, size_t size,
87 struct page **pages, size_t page_offset,
88 int num_pages)
89 {
90 struct btrfs_key key;
91 struct btrfs_path *path;
92 struct extent_buffer *leaf;
93 char *kaddr;
94 unsigned long ptr;
95 struct btrfs_file_extent_item *ei;
96 struct page *page;
97 u32 datasize;
98 int err = 0;
99 int ret;
100 int i;
101 ssize_t cur_size;
102
103 path = btrfs_alloc_path();
104 if (!path)
105 return -ENOMEM;
106
107 btrfs_set_trans_block_group(trans, inode);
108
109 key.objectid = inode->i_ino;
110 key.offset = offset;
111 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
112
113 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
114 if (ret < 0) {
115 err = ret;
116 goto fail;
117 }
118 if (ret == 1) {
119 struct btrfs_key found_key;
120
121 if (path->slots[0] == 0)
122 goto insert;
123
124 path->slots[0]--;
125 leaf = path->nodes[0];
126 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
127
128 if (found_key.objectid != inode->i_ino)
129 goto insert;
130
131 if (found_key.type != BTRFS_EXTENT_DATA_KEY)
132 goto insert;
133 ei = btrfs_item_ptr(leaf, path->slots[0],
134 struct btrfs_file_extent_item);
135
136 if (btrfs_file_extent_type(leaf, ei) !=
137 BTRFS_FILE_EXTENT_INLINE) {
138 goto insert;
139 }
140 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
141 ret = 0;
142 }
143 if (ret == 0) {
144 u32 found_size;
145 u64 found_end;
146
147 leaf = path->nodes[0];
148 ei = btrfs_item_ptr(leaf, path->slots[0],
149 struct btrfs_file_extent_item);
150
151 if (btrfs_file_extent_type(leaf, ei) !=
152 BTRFS_FILE_EXTENT_INLINE) {
153 err = ret;
154 btrfs_print_leaf(root, leaf);
155 printk("found wasn't inline offset %Lu inode %lu\n",
156 offset, inode->i_ino);
157 goto fail;
158 }
159 found_size = btrfs_file_extent_inline_len(leaf,
160 btrfs_item_nr(leaf, path->slots[0]));
161 found_end = key.offset + found_size;
162
163 if (found_end < offset + size) {
164 btrfs_release_path(root, path);
165 ret = btrfs_search_slot(trans, root, &key, path,
166 offset + size - found_end, 1);
167 BUG_ON(ret != 0);
168
169 ret = btrfs_extend_item(trans, root, path,
170 offset + size - found_end);
171 if (ret) {
172 err = ret;
173 goto fail;
174 }
175 leaf = path->nodes[0];
176 ei = btrfs_item_ptr(leaf, path->slots[0],
177 struct btrfs_file_extent_item);
178 inode->i_blocks += (offset + size - found_end) >> 9;
179 }
180 if (found_end < offset) {
181 ptr = btrfs_file_extent_inline_start(ei) + found_size;
182 memset_extent_buffer(leaf, 0, ptr, offset - found_end);
183 }
184 } else {
185 insert:
186 btrfs_release_path(root, path);
187 datasize = offset + size - key.offset;
188 inode->i_blocks += datasize >> 9;
189 datasize = btrfs_file_extent_calc_inline_size(datasize);
190 ret = btrfs_insert_empty_item(trans, root, path, &key,
191 datasize);
192 if (ret) {
193 err = ret;
194 printk("got bad ret %d\n", ret);
195 goto fail;
196 }
197 leaf = path->nodes[0];
198 ei = btrfs_item_ptr(leaf, path->slots[0],
199 struct btrfs_file_extent_item);
200 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
201 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
202 }
203 ptr = btrfs_file_extent_inline_start(ei) + offset - key.offset;
204
205 cur_size = size;
206 i = 0;
207 while (size > 0) {
208 page = pages[i];
209 kaddr = kmap_atomic(page, KM_USER0);
210 cur_size = min_t(size_t, PAGE_CACHE_SIZE - page_offset, size);
211 write_extent_buffer(leaf, kaddr + page_offset, ptr, cur_size);
212 kunmap_atomic(kaddr, KM_USER0);
213 page_offset = 0;
214 ptr += cur_size;
215 size -= cur_size;
216 if (i >= num_pages) {
217 printk("i %d num_pages %d\n", i, num_pages);
218 }
219 i++;
220 }
221 btrfs_mark_buffer_dirty(leaf);
222 fail:
223 btrfs_free_path(path);
224 return err;
225 }
226
227 static int noinline dirty_and_release_pages(struct btrfs_trans_handle *trans,
228 struct btrfs_root *root,
229 struct file *file,
230 struct page **pages,
231 size_t num_pages,
232 loff_t pos,
233 size_t write_bytes)
234 {
235 int err = 0;
236 int i;
237 struct inode *inode = fdentry(file)->d_inode;
238 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
239 u64 hint_byte;
240 u64 num_bytes;
241 u64 start_pos;
242 u64 end_of_last_block;
243 u64 end_pos = pos + write_bytes;
244 u64 inline_size;
245 loff_t isize = i_size_read(inode);
246
247 start_pos = pos & ~((u64)root->sectorsize - 1);
248 num_bytes = (write_bytes + pos - start_pos +
249 root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
250
251 end_of_last_block = start_pos + num_bytes - 1;
252
253 lock_extent(io_tree, start_pos, end_of_last_block, GFP_NOFS);
254 mutex_lock(&root->fs_info->fs_mutex);
255 trans = btrfs_start_transaction(root, 1);
256 if (!trans) {
257 err = -ENOMEM;
258 goto out_unlock;
259 }
260 btrfs_set_trans_block_group(trans, inode);
261 hint_byte = 0;
262
263 if ((end_of_last_block & 4095) == 0) {
264 printk("strange end of last %Lu %zu %Lu\n", start_pos, write_bytes, end_of_last_block);
265 }
266 set_extent_uptodate(io_tree, start_pos, end_of_last_block, GFP_NOFS);
267
268 /* FIXME...EIEIO, ENOSPC and more */
269 /* insert any holes we need to create */
270 if (isize < end_pos) {
271 u64 last_pos_in_file;
272 u64 hole_size;
273 u64 mask = root->sectorsize - 1;
274 last_pos_in_file = (isize + mask) & ~mask;
275 hole_size = (end_pos - last_pos_in_file + mask) & ~mask;
276 if (last_pos_in_file < end_pos) {
277 err = btrfs_drop_extents(trans, root, inode,
278 last_pos_in_file,
279 last_pos_in_file + hole_size,
280 last_pos_in_file,
281 &hint_byte);
282 if (err)
283 goto failed;
284
285 err = btrfs_insert_file_extent(trans, root,
286 inode->i_ino,
287 last_pos_in_file,
288 0, 0, hole_size);
289 btrfs_drop_extent_cache(inode, last_pos_in_file,
290 last_pos_in_file + hole_size -1);
291 btrfs_check_file(root, inode);
292 }
293 if (err)
294 goto failed;
295 }
296
297 /*
298 * either allocate an extent for the new bytes or setup the key
299 * to show we are doing inline data in the extent
300 */
301 inline_size = end_pos;
302 if (isize >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
303 inline_size > root->fs_info->max_inline ||
304 (inline_size & (root->sectorsize -1)) == 0 ||
305 inline_size >= BTRFS_MAX_INLINE_DATA_SIZE(root)) {
306 u64 last_end;
307 u64 existing_delalloc = 0;
308
309 for (i = 0; i < num_pages; i++) {
310 struct page *p = pages[i];
311 SetPageUptodate(p);
312 set_page_dirty(p);
313 }
314 last_end = (u64)(pages[num_pages -1]->index) <<
315 PAGE_CACHE_SHIFT;
316 last_end += PAGE_CACHE_SIZE - 1;
317 if (start_pos < isize) {
318 u64 delalloc_start = start_pos;
319 existing_delalloc = count_range_bits(io_tree,
320 &delalloc_start,
321 end_of_last_block, (u64)-1,
322 EXTENT_DELALLOC);
323 }
324 set_extent_delalloc(io_tree, start_pos, end_of_last_block,
325 GFP_NOFS);
326 btrfs_add_ordered_inode(inode);
327 } else {
328 u64 aligned_end;
329 /* step one, delete the existing extents in this range */
330 aligned_end = (pos + write_bytes + root->sectorsize - 1) &
331 ~((u64)root->sectorsize - 1);
332 err = btrfs_drop_extents(trans, root, inode, start_pos,
333 aligned_end, aligned_end, &hint_byte);
334 if (err)
335 goto failed;
336 if (isize > inline_size)
337 inline_size = min_t(u64, isize, aligned_end);
338 inline_size -= start_pos;
339 err = insert_inline_extent(trans, root, inode, start_pos,
340 inline_size, pages, 0, num_pages);
341 btrfs_drop_extent_cache(inode, start_pos, aligned_end - 1);
342 BUG_ON(err);
343 }
344 if (end_pos > isize) {
345 i_size_write(inode, end_pos);
346 btrfs_update_inode(trans, root, inode);
347 }
348 failed:
349 err = btrfs_end_transaction(trans, root);
350 out_unlock:
351 mutex_unlock(&root->fs_info->fs_mutex);
352 unlock_extent(io_tree, start_pos, end_of_last_block, GFP_NOFS);
353 return err;
354 }
355
356 int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end)
357 {
358 struct extent_map *em;
359 struct extent_map *split = NULL;
360 struct extent_map *split2 = NULL;
361 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
362 u64 len = end - start + 1;
363 int ret;
364 int testend = 1;
365
366 if (end == (u64)-1) {
367 len = (u64)-1;
368 testend = 0;
369 }
370 while(1) {
371 if (!split)
372 split = alloc_extent_map(GFP_NOFS);
373 if (!split2)
374 split2 = alloc_extent_map(GFP_NOFS);
375
376 spin_lock(&em_tree->lock);
377 em = lookup_extent_mapping(em_tree, start, len);
378 if (!em) {
379 spin_unlock(&em_tree->lock);
380 break;
381 }
382 remove_extent_mapping(em_tree, em);
383
384 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
385 em->start < start) {
386 split->start = em->start;
387 split->len = start - em->start;
388 split->block_start = em->block_start;
389 split->bdev = em->bdev;
390 split->flags = em->flags;
391 ret = add_extent_mapping(em_tree, split);
392 BUG_ON(ret);
393 free_extent_map(split);
394 split = split2;
395 split2 = NULL;
396 }
397 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
398 testend && em->start + em->len > start + len) {
399 u64 diff = start + len - em->start;
400
401 split->start = start + len;
402 split->len = em->start + em->len - (start + len);
403 split->bdev = em->bdev;
404 split->flags = em->flags;
405
406 split->block_start = em->block_start + diff;
407
408 ret = add_extent_mapping(em_tree, split);
409 BUG_ON(ret);
410 free_extent_map(split);
411 split = NULL;
412 }
413 spin_unlock(&em_tree->lock);
414
415 /* once for us */
416 free_extent_map(em);
417 /* once for the tree*/
418 free_extent_map(em);
419 }
420 if (split)
421 free_extent_map(split);
422 if (split2)
423 free_extent_map(split2);
424 return 0;
425 }
426
427 int btrfs_check_file(struct btrfs_root *root, struct inode *inode)
428 {
429 return 0;
430 #if 0
431 struct btrfs_path *path;
432 struct btrfs_key found_key;
433 struct extent_buffer *leaf;
434 struct btrfs_file_extent_item *extent;
435 u64 last_offset = 0;
436 int nritems;
437 int slot;
438 int found_type;
439 int ret;
440 int err = 0;
441 u64 extent_end = 0;
442
443 path = btrfs_alloc_path();
444 ret = btrfs_lookup_file_extent(NULL, root, path, inode->i_ino,
445 last_offset, 0);
446 while(1) {
447 nritems = btrfs_header_nritems(path->nodes[0]);
448 if (path->slots[0] >= nritems) {
449 ret = btrfs_next_leaf(root, path);
450 if (ret)
451 goto out;
452 nritems = btrfs_header_nritems(path->nodes[0]);
453 }
454 slot = path->slots[0];
455 leaf = path->nodes[0];
456 btrfs_item_key_to_cpu(leaf, &found_key, slot);
457 if (found_key.objectid != inode->i_ino)
458 break;
459 if (found_key.type != BTRFS_EXTENT_DATA_KEY)
460 goto out;
461
462 if (found_key.offset < last_offset) {
463 WARN_ON(1);
464 btrfs_print_leaf(root, leaf);
465 printk("inode %lu found offset %Lu expected %Lu\n",
466 inode->i_ino, found_key.offset, last_offset);
467 err = 1;
468 goto out;
469 }
470 extent = btrfs_item_ptr(leaf, slot,
471 struct btrfs_file_extent_item);
472 found_type = btrfs_file_extent_type(leaf, extent);
473 if (found_type == BTRFS_FILE_EXTENT_REG) {
474 extent_end = found_key.offset +
475 btrfs_file_extent_num_bytes(leaf, extent);
476 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
477 struct btrfs_item *item;
478 item = btrfs_item_nr(leaf, slot);
479 extent_end = found_key.offset +
480 btrfs_file_extent_inline_len(leaf, item);
481 extent_end = (extent_end + root->sectorsize - 1) &
482 ~((u64)root->sectorsize -1 );
483 }
484 last_offset = extent_end;
485 path->slots[0]++;
486 }
487 if (0 && last_offset < inode->i_size) {
488 WARN_ON(1);
489 btrfs_print_leaf(root, leaf);
490 printk("inode %lu found offset %Lu size %Lu\n", inode->i_ino,
491 last_offset, inode->i_size);
492 err = 1;
493
494 }
495 out:
496 btrfs_free_path(path);
497 return err;
498 #endif
499 }
500
501 /*
502 * this is very complex, but the basic idea is to drop all extents
503 * in the range start - end. hint_block is filled in with a block number
504 * that would be a good hint to the block allocator for this file.
505 *
506 * If an extent intersects the range but is not entirely inside the range
507 * it is either truncated or split. Anything entirely inside the range
508 * is deleted from the tree.
509 */
510 int btrfs_drop_extents(struct btrfs_trans_handle *trans,
511 struct btrfs_root *root, struct inode *inode,
512 u64 start, u64 end, u64 inline_limit, u64 *hint_byte)
513 {
514 u64 extent_end = 0;
515 u64 search_start = start;
516 struct extent_buffer *leaf;
517 struct btrfs_file_extent_item *extent;
518 struct btrfs_path *path;
519 struct btrfs_key key;
520 struct btrfs_file_extent_item old;
521 int keep;
522 int slot;
523 int bookend;
524 int found_type;
525 int found_extent;
526 int found_inline;
527 int recow;
528 int ret;
529
530 btrfs_drop_extent_cache(inode, start, end - 1);
531
532 path = btrfs_alloc_path();
533 if (!path)
534 return -ENOMEM;
535 while(1) {
536 recow = 0;
537 btrfs_release_path(root, path);
538 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
539 search_start, -1);
540 if (ret < 0)
541 goto out;
542 if (ret > 0) {
543 if (path->slots[0] == 0) {
544 ret = 0;
545 goto out;
546 }
547 path->slots[0]--;
548 }
549 next_slot:
550 keep = 0;
551 bookend = 0;
552 found_extent = 0;
553 found_inline = 0;
554 extent = NULL;
555 leaf = path->nodes[0];
556 slot = path->slots[0];
557 ret = 0;
558 btrfs_item_key_to_cpu(leaf, &key, slot);
559 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY &&
560 key.offset >= end) {
561 goto out;
562 }
563 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
564 key.objectid != inode->i_ino) {
565 goto out;
566 }
567 if (recow) {
568 search_start = key.offset;
569 continue;
570 }
571 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
572 extent = btrfs_item_ptr(leaf, slot,
573 struct btrfs_file_extent_item);
574 found_type = btrfs_file_extent_type(leaf, extent);
575 if (found_type == BTRFS_FILE_EXTENT_REG) {
576 extent_end =
577 btrfs_file_extent_disk_bytenr(leaf,
578 extent);
579 if (extent_end)
580 *hint_byte = extent_end;
581
582 extent_end = key.offset +
583 btrfs_file_extent_num_bytes(leaf, extent);
584 found_extent = 1;
585 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
586 struct btrfs_item *item;
587 item = btrfs_item_nr(leaf, slot);
588 found_inline = 1;
589 extent_end = key.offset +
590 btrfs_file_extent_inline_len(leaf, item);
591 }
592 } else {
593 extent_end = search_start;
594 }
595
596 /* we found nothing we can drop */
597 if ((!found_extent && !found_inline) ||
598 search_start >= extent_end) {
599 int nextret;
600 u32 nritems;
601 nritems = btrfs_header_nritems(leaf);
602 if (slot >= nritems - 1) {
603 nextret = btrfs_next_leaf(root, path);
604 if (nextret)
605 goto out;
606 recow = 1;
607 } else {
608 path->slots[0]++;
609 }
610 goto next_slot;
611 }
612
613 if (found_inline) {
614 u64 mask = root->sectorsize - 1;
615 search_start = (extent_end + mask) & ~mask;
616 } else
617 search_start = extent_end;
618 if (end <= extent_end && start >= key.offset && found_inline) {
619 *hint_byte = EXTENT_MAP_INLINE;
620 continue;
621 }
622 if (end < extent_end && end >= key.offset) {
623 if (found_extent) {
624 u64 disk_bytenr =
625 btrfs_file_extent_disk_bytenr(leaf, extent);
626 u64 disk_num_bytes =
627 btrfs_file_extent_disk_num_bytes(leaf,
628 extent);
629 read_extent_buffer(leaf, &old,
630 (unsigned long)extent,
631 sizeof(old));
632 if (disk_bytenr != 0) {
633 ret = btrfs_inc_extent_ref(trans, root,
634 disk_bytenr, disk_num_bytes,
635 root->root_key.objectid,
636 trans->transid,
637 key.objectid, end);
638 BUG_ON(ret);
639 }
640 }
641 bookend = 1;
642 if (found_inline && start <= key.offset)
643 keep = 1;
644 }
645 /* truncate existing extent */
646 if (start > key.offset) {
647 u64 new_num;
648 u64 old_num;
649 keep = 1;
650 WARN_ON(start & (root->sectorsize - 1));
651 if (found_extent) {
652 new_num = start - key.offset;
653 old_num = btrfs_file_extent_num_bytes(leaf,
654 extent);
655 *hint_byte =
656 btrfs_file_extent_disk_bytenr(leaf,
657 extent);
658 if (btrfs_file_extent_disk_bytenr(leaf,
659 extent)) {
660 dec_i_blocks(inode, old_num - new_num);
661 }
662 btrfs_set_file_extent_num_bytes(leaf, extent,
663 new_num);
664 btrfs_mark_buffer_dirty(leaf);
665 } else if (key.offset < inline_limit &&
666 (end > extent_end) &&
667 (inline_limit < extent_end)) {
668 u32 new_size;
669 new_size = btrfs_file_extent_calc_inline_size(
670 inline_limit - key.offset);
671 dec_i_blocks(inode, (extent_end - key.offset) -
672 (inline_limit - key.offset));
673 btrfs_truncate_item(trans, root, path,
674 new_size, 1);
675 }
676 }
677 /* delete the entire extent */
678 if (!keep) {
679 u64 disk_bytenr = 0;
680 u64 disk_num_bytes = 0;
681 u64 extent_num_bytes = 0;
682 u64 root_gen;
683 u64 root_owner;
684
685 root_gen = btrfs_header_generation(leaf);
686 root_owner = btrfs_header_owner(leaf);
687 if (found_extent) {
688 disk_bytenr =
689 btrfs_file_extent_disk_bytenr(leaf,
690 extent);
691 disk_num_bytes =
692 btrfs_file_extent_disk_num_bytes(leaf,
693 extent);
694 extent_num_bytes =
695 btrfs_file_extent_num_bytes(leaf, extent);
696 *hint_byte =
697 btrfs_file_extent_disk_bytenr(leaf,
698 extent);
699 }
700 ret = btrfs_del_item(trans, root, path);
701 /* TODO update progress marker and return */
702 BUG_ON(ret);
703 btrfs_release_path(root, path);
704 extent = NULL;
705 if (found_extent && disk_bytenr != 0) {
706 dec_i_blocks(inode, extent_num_bytes);
707 ret = btrfs_free_extent(trans, root,
708 disk_bytenr,
709 disk_num_bytes,
710 root_owner,
711 root_gen, inode->i_ino,
712 key.offset, 0);
713 }
714
715 BUG_ON(ret);
716 if (!bookend && search_start >= end) {
717 ret = 0;
718 goto out;
719 }
720 if (!bookend)
721 continue;
722 }
723 if (bookend && found_inline && start <= key.offset) {
724 u32 new_size;
725 new_size = btrfs_file_extent_calc_inline_size(
726 extent_end - end);
727 dec_i_blocks(inode, (extent_end - key.offset) -
728 (extent_end - end));
729 btrfs_truncate_item(trans, root, path, new_size, 0);
730 }
731 /* create bookend, splitting the extent in two */
732 if (bookend && found_extent) {
733 struct btrfs_key ins;
734 ins.objectid = inode->i_ino;
735 ins.offset = end;
736 btrfs_set_key_type(&ins, BTRFS_EXTENT_DATA_KEY);
737 btrfs_release_path(root, path);
738 ret = btrfs_insert_empty_item(trans, root, path, &ins,
739 sizeof(*extent));
740
741 leaf = path->nodes[0];
742 if (ret) {
743 btrfs_print_leaf(root, leaf);
744 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);
745 }
746 BUG_ON(ret);
747 extent = btrfs_item_ptr(leaf, path->slots[0],
748 struct btrfs_file_extent_item);
749 write_extent_buffer(leaf, &old,
750 (unsigned long)extent, sizeof(old));
751
752 btrfs_set_file_extent_offset(leaf, extent,
753 le64_to_cpu(old.offset) + end - key.offset);
754 WARN_ON(le64_to_cpu(old.num_bytes) <
755 (extent_end - end));
756 btrfs_set_file_extent_num_bytes(leaf, extent,
757 extent_end - end);
758 btrfs_set_file_extent_type(leaf, extent,
759 BTRFS_FILE_EXTENT_REG);
760
761 btrfs_mark_buffer_dirty(path->nodes[0]);
762 if (le64_to_cpu(old.disk_bytenr) != 0) {
763 inode->i_blocks +=
764 btrfs_file_extent_num_bytes(leaf,
765 extent) >> 9;
766 }
767 ret = 0;
768 goto out;
769 }
770 }
771 out:
772 btrfs_free_path(path);
773 btrfs_check_file(root, inode);
774 return ret;
775 }
776
777 /*
778 * this gets pages into the page cache and locks them down
779 */
780 static int prepare_pages(struct btrfs_root *root, struct file *file,
781 struct page **pages, size_t num_pages,
782 loff_t pos, unsigned long first_index,
783 unsigned long last_index, size_t write_bytes)
784 {
785 int i;
786 unsigned long index = pos >> PAGE_CACHE_SHIFT;
787 struct inode *inode = fdentry(file)->d_inode;
788 int err = 0;
789 u64 start_pos;
790
791 start_pos = pos & ~((u64)root->sectorsize - 1);
792
793 memset(pages, 0, num_pages * sizeof(struct page *));
794
795 for (i = 0; i < num_pages; i++) {
796 pages[i] = grab_cache_page(inode->i_mapping, index + i);
797 if (!pages[i]) {
798 err = -ENOMEM;
799 BUG_ON(1);
800 }
801 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
802 ClearPageDirty(pages[i]);
803 #else
804 cancel_dirty_page(pages[i], PAGE_CACHE_SIZE);
805 #endif
806 wait_on_page_writeback(pages[i]);
807 set_page_extent_mapped(pages[i]);
808 WARN_ON(!PageLocked(pages[i]));
809 }
810 if (start_pos < inode->i_size) {
811 u64 last_pos;
812 last_pos = ((u64)index + num_pages) << PAGE_CACHE_SHIFT;
813 lock_extent(&BTRFS_I(inode)->io_tree,
814 start_pos, last_pos - 1, GFP_NOFS);
815 clear_extent_bits(&BTRFS_I(inode)->io_tree, start_pos,
816 last_pos - 1, EXTENT_DIRTY | EXTENT_DELALLOC,
817 GFP_NOFS);
818 unlock_extent(&BTRFS_I(inode)->io_tree,
819 start_pos, last_pos - 1, GFP_NOFS);
820 }
821 return 0;
822 }
823
824 static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
825 size_t count, loff_t *ppos)
826 {
827 loff_t pos;
828 loff_t start_pos;
829 ssize_t num_written = 0;
830 ssize_t err = 0;
831 int ret = 0;
832 struct inode *inode = fdentry(file)->d_inode;
833 struct btrfs_root *root = BTRFS_I(inode)->root;
834 struct page **pages = NULL;
835 int nrptrs;
836 struct page *pinned[2];
837 unsigned long first_index;
838 unsigned long last_index;
839
840 nrptrs = min((count + PAGE_CACHE_SIZE - 1) / PAGE_CACHE_SIZE,
841 PAGE_CACHE_SIZE / (sizeof(struct page *)));
842 pinned[0] = NULL;
843 pinned[1] = NULL;
844
845 pos = *ppos;
846 start_pos = pos;
847
848 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
849 current->backing_dev_info = inode->i_mapping->backing_dev_info;
850 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
851 if (err)
852 goto out_nolock;
853 if (count == 0)
854 goto out_nolock;
855 err = remove_suid(fdentry(file));
856 if (err)
857 goto out_nolock;
858 file_update_time(file);
859
860 pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
861
862 mutex_lock(&inode->i_mutex);
863 first_index = pos >> PAGE_CACHE_SHIFT;
864 last_index = (pos + count) >> PAGE_CACHE_SHIFT;
865
866 /*
867 * there are lots of better ways to do this, but this code
868 * makes sure the first and last page in the file range are
869 * up to date and ready for cow
870 */
871 if ((pos & (PAGE_CACHE_SIZE - 1))) {
872 pinned[0] = grab_cache_page(inode->i_mapping, first_index);
873 if (!PageUptodate(pinned[0])) {
874 ret = btrfs_readpage(NULL, pinned[0]);
875 BUG_ON(ret);
876 wait_on_page_locked(pinned[0]);
877 } else {
878 unlock_page(pinned[0]);
879 }
880 }
881 if ((pos + count) & (PAGE_CACHE_SIZE - 1)) {
882 pinned[1] = grab_cache_page(inode->i_mapping, last_index);
883 if (!PageUptodate(pinned[1])) {
884 ret = btrfs_readpage(NULL, pinned[1]);
885 BUG_ON(ret);
886 wait_on_page_locked(pinned[1]);
887 } else {
888 unlock_page(pinned[1]);
889 }
890 }
891
892 while(count > 0) {
893 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
894 size_t write_bytes = min(count, nrptrs *
895 (size_t)PAGE_CACHE_SIZE -
896 offset);
897 size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
898 PAGE_CACHE_SHIFT;
899
900 WARN_ON(num_pages > nrptrs);
901 memset(pages, 0, sizeof(pages));
902
903 mutex_lock(&root->fs_info->fs_mutex);
904 ret = btrfs_check_free_space(root, write_bytes, 0);
905 mutex_unlock(&root->fs_info->fs_mutex);
906 if (ret)
907 goto out;
908
909 ret = prepare_pages(root, file, pages, num_pages,
910 pos, first_index, last_index,
911 write_bytes);
912 if (ret)
913 goto out;
914
915 ret = btrfs_copy_from_user(pos, num_pages,
916 write_bytes, pages, buf);
917 if (ret) {
918 btrfs_drop_pages(pages, num_pages);
919 goto out;
920 }
921
922 ret = dirty_and_release_pages(NULL, root, file, pages,
923 num_pages, pos, write_bytes);
924 btrfs_drop_pages(pages, num_pages);
925 if (ret)
926 goto out;
927
928 buf += write_bytes;
929 count -= write_bytes;
930 pos += write_bytes;
931 num_written += write_bytes;
932
933 balance_dirty_pages_ratelimited_nr(inode->i_mapping, num_pages);
934 if (num_pages < (root->leafsize >> PAGE_CACHE_SHIFT) + 1)
935 btrfs_btree_balance_dirty(root, 1);
936 btrfs_throttle(root);
937 cond_resched();
938 }
939 out:
940 mutex_unlock(&inode->i_mutex);
941
942 out_nolock:
943 kfree(pages);
944 if (pinned[0])
945 page_cache_release(pinned[0]);
946 if (pinned[1])
947 page_cache_release(pinned[1]);
948 *ppos = pos;
949
950 if (num_written > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
951 err = sync_page_range(inode, inode->i_mapping,
952 start_pos, num_written);
953 if (err < 0)
954 num_written = err;
955 } else if (num_written > 0 && (file->f_flags & O_DIRECT)) {
956 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
957 do_sync_file_range(file, start_pos,
958 start_pos + num_written - 1,
959 SYNC_FILE_RANGE_WRITE |
960 SYNC_FILE_RANGE_WAIT_AFTER);
961 #else
962 do_sync_mapping_range(inode->i_mapping, start_pos,
963 start_pos + num_written - 1,
964 SYNC_FILE_RANGE_WRITE |
965 SYNC_FILE_RANGE_WAIT_AFTER);
966 #endif
967 invalidate_mapping_pages(inode->i_mapping,
968 start_pos >> PAGE_CACHE_SHIFT,
969 (start_pos + num_written - 1) >> PAGE_CACHE_SHIFT);
970 }
971 current->backing_dev_info = NULL;
972 return num_written ? num_written : err;
973 }
974
975 static int btrfs_sync_file(struct file *file,
976 struct dentry *dentry, int datasync)
977 {
978 struct inode *inode = dentry->d_inode;
979 struct btrfs_root *root = BTRFS_I(inode)->root;
980 int ret = 0;
981 struct btrfs_trans_handle *trans;
982
983 /*
984 * check the transaction that last modified this inode
985 * and see if its already been committed
986 */
987 mutex_lock(&root->fs_info->fs_mutex);
988 if (!BTRFS_I(inode)->last_trans)
989 goto out;
990 mutex_lock(&root->fs_info->trans_mutex);
991 if (BTRFS_I(inode)->last_trans <=
992 root->fs_info->last_trans_committed) {
993 BTRFS_I(inode)->last_trans = 0;
994 mutex_unlock(&root->fs_info->trans_mutex);
995 goto out;
996 }
997 mutex_unlock(&root->fs_info->trans_mutex);
998
999 /*
1000 * ok we haven't committed the transaction yet, lets do a commit
1001 */
1002 trans = btrfs_start_transaction(root, 1);
1003 if (!trans) {
1004 ret = -ENOMEM;
1005 goto out;
1006 }
1007 ret = btrfs_commit_transaction(trans, root);
1008 out:
1009 mutex_unlock(&root->fs_info->fs_mutex);
1010 return ret > 0 ? EIO : ret;
1011 }
1012
1013 static struct vm_operations_struct btrfs_file_vm_ops = {
1014 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
1015 .nopage = filemap_nopage,
1016 .populate = filemap_populate,
1017 #else
1018 .fault = filemap_fault,
1019 #endif
1020 .page_mkwrite = btrfs_page_mkwrite,
1021 };
1022
1023 static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
1024 {
1025 vma->vm_ops = &btrfs_file_vm_ops;
1026 file_accessed(filp);
1027 return 0;
1028 }
1029
1030 struct file_operations btrfs_file_operations = {
1031 .llseek = generic_file_llseek,
1032 .read = do_sync_read,
1033 .aio_read = generic_file_aio_read,
1034 .splice_read = generic_file_splice_read,
1035 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1036 .sendfile = generic_file_sendfile,
1037 #endif
1038 .write = btrfs_file_write,
1039 .mmap = btrfs_file_mmap,
1040 .open = generic_file_open,
1041 .fsync = btrfs_sync_file,
1042 .unlocked_ioctl = btrfs_ioctl,
1043 #ifdef CONFIG_COMPAT
1044 .compat_ioctl = btrfs_ioctl,
1045 #endif
1046 };
1047