2 * Copyright (C) 2007 Oracle. All rights reserved.
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.
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.
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.
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>
35 #include "transaction.h"
36 #include "btrfs_inode.h"
38 #include "print-tree.h"
42 static int noinline
btrfs_copy_from_user(loff_t pos
, int num_pages
,
44 struct page
**prepared_pages
,
45 const char __user
* buf
)
49 int offset
= pos
& (PAGE_CACHE_SIZE
- 1);
51 for (i
= 0; i
< num_pages
&& write_bytes
> 0; i
++, offset
= 0) {
52 size_t count
= min_t(size_t,
53 PAGE_CACHE_SIZE
- offset
, write_bytes
);
54 struct page
*page
= prepared_pages
[i
];
55 fault_in_pages_readable(buf
, count
);
57 /* Copy data from userspace to the current page */
59 page_fault
= __copy_from_user(page_address(page
) + offset
,
61 /* Flush processor's dcache for this page */
62 flush_dcache_page(page
);
70 return page_fault
? -EFAULT
: 0;
73 static void noinline
btrfs_drop_pages(struct page
**pages
, size_t num_pages
)
76 for (i
= 0; i
< num_pages
; i
++) {
79 ClearPageChecked(pages
[i
]);
80 unlock_page(pages
[i
]);
81 mark_page_accessed(pages
[i
]);
82 page_cache_release(pages
[i
]);
86 static int noinline
insert_inline_extent(struct btrfs_trans_handle
*trans
,
87 struct btrfs_root
*root
, struct inode
*inode
,
88 u64 offset
, size_t size
,
89 struct page
**pages
, size_t page_offset
,
93 struct btrfs_path
*path
;
94 struct extent_buffer
*leaf
;
97 struct btrfs_file_extent_item
*ei
;
105 path
= btrfs_alloc_path();
109 btrfs_set_trans_block_group(trans
, inode
);
111 key
.objectid
= inode
->i_ino
;
113 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
115 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
121 struct btrfs_key found_key
;
123 if (path
->slots
[0] == 0)
127 leaf
= path
->nodes
[0];
128 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
130 if (found_key
.objectid
!= inode
->i_ino
)
133 if (found_key
.type
!= BTRFS_EXTENT_DATA_KEY
)
135 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
136 struct btrfs_file_extent_item
);
138 if (btrfs_file_extent_type(leaf
, ei
) !=
139 BTRFS_FILE_EXTENT_INLINE
) {
142 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
149 leaf
= path
->nodes
[0];
150 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
151 struct btrfs_file_extent_item
);
153 if (btrfs_file_extent_type(leaf
, ei
) !=
154 BTRFS_FILE_EXTENT_INLINE
) {
156 btrfs_print_leaf(root
, leaf
);
157 printk("found wasn't inline offset %Lu inode %lu\n",
158 offset
, inode
->i_ino
);
161 found_size
= btrfs_file_extent_inline_len(leaf
,
162 btrfs_item_nr(leaf
, path
->slots
[0]));
163 found_end
= key
.offset
+ found_size
;
165 if (found_end
< offset
+ size
) {
166 btrfs_release_path(root
, path
);
167 ret
= btrfs_search_slot(trans
, root
, &key
, path
,
168 offset
+ size
- found_end
, 1);
171 ret
= btrfs_extend_item(trans
, root
, path
,
172 offset
+ size
- found_end
);
177 leaf
= path
->nodes
[0];
178 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
179 struct btrfs_file_extent_item
);
180 inode
->i_blocks
+= (offset
+ size
- found_end
) >> 9;
182 if (found_end
< offset
) {
183 ptr
= btrfs_file_extent_inline_start(ei
) + found_size
;
184 memset_extent_buffer(leaf
, 0, ptr
, offset
- found_end
);
188 btrfs_release_path(root
, path
);
189 datasize
= offset
+ size
- key
.offset
;
190 inode
->i_blocks
+= datasize
>> 9;
191 datasize
= btrfs_file_extent_calc_inline_size(datasize
);
192 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
196 printk("got bad ret %d\n", ret
);
199 leaf
= path
->nodes
[0];
200 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
201 struct btrfs_file_extent_item
);
202 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
203 btrfs_set_file_extent_type(leaf
, ei
, BTRFS_FILE_EXTENT_INLINE
);
205 ptr
= btrfs_file_extent_inline_start(ei
) + offset
- key
.offset
;
211 kaddr
= kmap_atomic(page
, KM_USER0
);
212 cur_size
= min_t(size_t, PAGE_CACHE_SIZE
- page_offset
, size
);
213 write_extent_buffer(leaf
, kaddr
+ page_offset
, ptr
, cur_size
);
214 kunmap_atomic(kaddr
, KM_USER0
);
218 if (i
>= num_pages
) {
219 printk("i %d num_pages %d\n", i
, num_pages
);
223 btrfs_mark_buffer_dirty(leaf
);
225 btrfs_free_path(path
);
229 static int noinline
dirty_and_release_pages(struct btrfs_trans_handle
*trans
,
230 struct btrfs_root
*root
,
239 struct inode
*inode
= fdentry(file
)->d_inode
;
240 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
244 u64 end_of_last_block
;
245 u64 end_pos
= pos
+ write_bytes
;
248 loff_t isize
= i_size_read(inode
);
250 start_pos
= pos
& ~((u64
)root
->sectorsize
- 1);
251 num_bytes
= (write_bytes
+ pos
- start_pos
+
252 root
->sectorsize
- 1) & ~((u64
)root
->sectorsize
- 1);
254 end_of_last_block
= start_pos
+ num_bytes
- 1;
256 lock_extent(io_tree
, start_pos
, end_of_last_block
, GFP_NOFS
);
257 trans
= btrfs_join_transaction(root
, 1);
262 btrfs_set_trans_block_group(trans
, inode
);
265 if ((end_of_last_block
& 4095) == 0) {
266 printk("strange end of last %Lu %zu %Lu\n", start_pos
, write_bytes
, end_of_last_block
);
268 set_extent_uptodate(io_tree
, start_pos
, end_of_last_block
, GFP_NOFS
);
270 /* FIXME...EIEIO, ENOSPC and more */
271 /* insert any holes we need to create */
272 if (isize
< start_pos
) {
273 u64 last_pos_in_file
;
275 u64 mask
= root
->sectorsize
- 1;
276 last_pos_in_file
= (isize
+ mask
) & ~mask
;
277 hole_size
= (start_pos
- last_pos_in_file
+ mask
) & ~mask
;
279 btrfs_wait_ordered_range(inode
, last_pos_in_file
,
280 last_pos_in_file
+ hole_size
);
281 mutex_lock(&BTRFS_I(inode
)->extent_mutex
);
282 err
= btrfs_drop_extents(trans
, root
, inode
,
284 last_pos_in_file
+ hole_size
,
290 err
= btrfs_insert_file_extent(trans
, root
,
294 btrfs_drop_extent_cache(inode
, last_pos_in_file
,
295 last_pos_in_file
+ hole_size
-1);
296 mutex_unlock(&BTRFS_I(inode
)->extent_mutex
);
297 btrfs_check_file(root
, inode
);
304 * either allocate an extent for the new bytes or setup the key
305 * to show we are doing inline data in the extent
307 inline_size
= end_pos
;
308 if (isize
>= BTRFS_MAX_INLINE_DATA_SIZE(root
) ||
309 inline_size
> root
->fs_info
->max_inline
||
310 (inline_size
& (root
->sectorsize
-1)) == 0 ||
311 inline_size
>= BTRFS_MAX_INLINE_DATA_SIZE(root
)) {
312 /* check for reserved extents on each page, we don't want
313 * to reset the delalloc bit on things that already have
316 btrfs_set_extent_delalloc(inode
, start_pos
, end_of_last_block
);
317 for (i
= 0; i
< num_pages
; i
++) {
318 struct page
*p
= pages
[i
];
325 /* step one, delete the existing extents in this range */
326 aligned_end
= (pos
+ write_bytes
+ root
->sectorsize
- 1) &
327 ~((u64
)root
->sectorsize
- 1);
328 mutex_lock(&BTRFS_I(inode
)->extent_mutex
);
329 err
= btrfs_drop_extents(trans
, root
, inode
, start_pos
,
330 aligned_end
, aligned_end
, &hint_byte
);
333 if (isize
> inline_size
)
334 inline_size
= min_t(u64
, isize
, aligned_end
);
335 inline_size
-= start_pos
;
336 err
= insert_inline_extent(trans
, root
, inode
, start_pos
,
337 inline_size
, pages
, 0, num_pages
);
338 btrfs_drop_extent_cache(inode
, start_pos
, aligned_end
- 1);
340 mutex_unlock(&BTRFS_I(inode
)->extent_mutex
);
343 * an ugly way to do all the prop accounting around
344 * the page bits and mapping tags
346 set_page_writeback(pages
[0]);
347 end_page_writeback(pages
[0]);
350 if (end_pos
> isize
) {
351 i_size_write(inode
, end_pos
);
353 BTRFS_I(inode
)->disk_i_size
= end_pos
;
354 btrfs_update_inode(trans
, root
, inode
);
357 err
= btrfs_end_transaction(trans
, root
);
359 unlock_extent(io_tree
, start_pos
, end_of_last_block
, GFP_NOFS
);
363 int noinline
btrfs_drop_extent_cache(struct inode
*inode
, u64 start
, u64 end
)
365 struct extent_map
*em
;
366 struct extent_map
*split
= NULL
;
367 struct extent_map
*split2
= NULL
;
368 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
369 u64 len
= end
- start
+ 1;
373 WARN_ON(end
< start
);
374 if (end
== (u64
)-1) {
380 split
= alloc_extent_map(GFP_NOFS
);
382 split2
= alloc_extent_map(GFP_NOFS
);
384 spin_lock(&em_tree
->lock
);
385 em
= lookup_extent_mapping(em_tree
, start
, len
);
387 spin_unlock(&em_tree
->lock
);
390 clear_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
391 remove_extent_mapping(em_tree
, em
);
393 if (em
->block_start
< EXTENT_MAP_LAST_BYTE
&&
395 split
->start
= em
->start
;
396 split
->len
= start
- em
->start
;
397 split
->block_start
= em
->block_start
;
398 split
->bdev
= em
->bdev
;
399 split
->flags
= em
->flags
;
400 ret
= add_extent_mapping(em_tree
, split
);
402 free_extent_map(split
);
406 if (em
->block_start
< EXTENT_MAP_LAST_BYTE
&&
407 testend
&& em
->start
+ em
->len
> start
+ len
) {
408 u64 diff
= start
+ len
- em
->start
;
410 split
->start
= start
+ len
;
411 split
->len
= em
->start
+ em
->len
- (start
+ len
);
412 split
->bdev
= em
->bdev
;
413 split
->flags
= em
->flags
;
415 split
->block_start
= em
->block_start
+ diff
;
417 ret
= add_extent_mapping(em_tree
, split
);
419 free_extent_map(split
);
422 spin_unlock(&em_tree
->lock
);
426 /* once for the tree*/
430 free_extent_map(split
);
432 free_extent_map(split2
);
436 int btrfs_check_file(struct btrfs_root
*root
, struct inode
*inode
)
440 struct btrfs_path
*path
;
441 struct btrfs_key found_key
;
442 struct extent_buffer
*leaf
;
443 struct btrfs_file_extent_item
*extent
;
452 path
= btrfs_alloc_path();
453 ret
= btrfs_lookup_file_extent(NULL
, root
, path
, inode
->i_ino
,
456 nritems
= btrfs_header_nritems(path
->nodes
[0]);
457 if (path
->slots
[0] >= nritems
) {
458 ret
= btrfs_next_leaf(root
, path
);
461 nritems
= btrfs_header_nritems(path
->nodes
[0]);
463 slot
= path
->slots
[0];
464 leaf
= path
->nodes
[0];
465 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
466 if (found_key
.objectid
!= inode
->i_ino
)
468 if (found_key
.type
!= BTRFS_EXTENT_DATA_KEY
)
471 if (found_key
.offset
< last_offset
) {
473 btrfs_print_leaf(root
, leaf
);
474 printk("inode %lu found offset %Lu expected %Lu\n",
475 inode
->i_ino
, found_key
.offset
, last_offset
);
479 extent
= btrfs_item_ptr(leaf
, slot
,
480 struct btrfs_file_extent_item
);
481 found_type
= btrfs_file_extent_type(leaf
, extent
);
482 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
483 extent_end
= found_key
.offset
+
484 btrfs_file_extent_num_bytes(leaf
, extent
);
485 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
486 struct btrfs_item
*item
;
487 item
= btrfs_item_nr(leaf
, slot
);
488 extent_end
= found_key
.offset
+
489 btrfs_file_extent_inline_len(leaf
, item
);
490 extent_end
= (extent_end
+ root
->sectorsize
- 1) &
491 ~((u64
)root
->sectorsize
-1 );
493 last_offset
= extent_end
;
496 if (0 && last_offset
< inode
->i_size
) {
498 btrfs_print_leaf(root
, leaf
);
499 printk("inode %lu found offset %Lu size %Lu\n", inode
->i_ino
,
500 last_offset
, inode
->i_size
);
505 btrfs_free_path(path
);
511 * this is very complex, but the basic idea is to drop all extents
512 * in the range start - end. hint_block is filled in with a block number
513 * that would be a good hint to the block allocator for this file.
515 * If an extent intersects the range but is not entirely inside the range
516 * it is either truncated or split. Anything entirely inside the range
517 * is deleted from the tree.
519 int noinline
btrfs_drop_extents(struct btrfs_trans_handle
*trans
,
520 struct btrfs_root
*root
, struct inode
*inode
,
521 u64 start
, u64 end
, u64 inline_limit
, u64
*hint_byte
)
524 u64 search_start
= start
;
525 struct extent_buffer
*leaf
;
526 struct btrfs_file_extent_item
*extent
;
527 struct btrfs_path
*path
;
528 struct btrfs_key key
;
529 struct btrfs_file_extent_item old
;
539 btrfs_drop_extent_cache(inode
, start
, end
- 1);
541 path
= btrfs_alloc_path();
546 btrfs_release_path(root
, path
);
547 ret
= btrfs_lookup_file_extent(trans
, root
, path
, inode
->i_ino
,
552 if (path
->slots
[0] == 0) {
564 leaf
= path
->nodes
[0];
565 slot
= path
->slots
[0];
567 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
568 if (btrfs_key_type(&key
) == BTRFS_EXTENT_DATA_KEY
&&
572 if (btrfs_key_type(&key
) > BTRFS_EXTENT_DATA_KEY
||
573 key
.objectid
!= inode
->i_ino
) {
577 search_start
= key
.offset
;
580 if (btrfs_key_type(&key
) == BTRFS_EXTENT_DATA_KEY
) {
581 extent
= btrfs_item_ptr(leaf
, slot
,
582 struct btrfs_file_extent_item
);
583 found_type
= btrfs_file_extent_type(leaf
, extent
);
584 if (found_type
== BTRFS_FILE_EXTENT_REG
) {
586 btrfs_file_extent_disk_bytenr(leaf
,
589 *hint_byte
= extent_end
;
591 extent_end
= key
.offset
+
592 btrfs_file_extent_num_bytes(leaf
, extent
);
594 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
595 struct btrfs_item
*item
;
596 item
= btrfs_item_nr(leaf
, slot
);
598 extent_end
= key
.offset
+
599 btrfs_file_extent_inline_len(leaf
, item
);
602 extent_end
= search_start
;
605 /* we found nothing we can drop */
606 if ((!found_extent
&& !found_inline
) ||
607 search_start
>= extent_end
) {
610 nritems
= btrfs_header_nritems(leaf
);
611 if (slot
>= nritems
- 1) {
612 nextret
= btrfs_next_leaf(root
, path
);
623 u64 mask
= root
->sectorsize
- 1;
624 search_start
= (extent_end
+ mask
) & ~mask
;
626 search_start
= extent_end
;
627 if (end
<= extent_end
&& start
>= key
.offset
&& found_inline
) {
628 *hint_byte
= EXTENT_MAP_INLINE
;
631 if (end
< extent_end
&& end
>= key
.offset
) {
634 btrfs_file_extent_disk_bytenr(leaf
, extent
);
636 btrfs_file_extent_disk_num_bytes(leaf
,
638 read_extent_buffer(leaf
, &old
,
639 (unsigned long)extent
,
641 if (disk_bytenr
!= 0) {
642 ret
= btrfs_inc_extent_ref(trans
, root
,
643 disk_bytenr
, disk_num_bytes
,
644 root
->root_key
.objectid
,
651 if (found_inline
&& start
<= key
.offset
)
654 /* truncate existing extent */
655 if (start
> key
.offset
) {
659 WARN_ON(start
& (root
->sectorsize
- 1));
661 new_num
= start
- key
.offset
;
662 old_num
= btrfs_file_extent_num_bytes(leaf
,
665 btrfs_file_extent_disk_bytenr(leaf
,
667 if (btrfs_file_extent_disk_bytenr(leaf
,
669 dec_i_blocks(inode
, old_num
- new_num
);
671 btrfs_set_file_extent_num_bytes(leaf
, extent
,
673 btrfs_mark_buffer_dirty(leaf
);
674 } else if (key
.offset
< inline_limit
&&
675 (end
> extent_end
) &&
676 (inline_limit
< extent_end
)) {
678 new_size
= btrfs_file_extent_calc_inline_size(
679 inline_limit
- key
.offset
);
680 dec_i_blocks(inode
, (extent_end
- key
.offset
) -
681 (inline_limit
- key
.offset
));
682 btrfs_truncate_item(trans
, root
, path
,
686 /* delete the entire extent */
689 u64 disk_num_bytes
= 0;
690 u64 extent_num_bytes
= 0;
694 root_gen
= btrfs_header_generation(leaf
);
695 root_owner
= btrfs_header_owner(leaf
);
698 btrfs_file_extent_disk_bytenr(leaf
,
701 btrfs_file_extent_disk_num_bytes(leaf
,
704 btrfs_file_extent_num_bytes(leaf
, extent
);
706 btrfs_file_extent_disk_bytenr(leaf
,
709 ret
= btrfs_del_item(trans
, root
, path
);
710 /* TODO update progress marker and return */
712 btrfs_release_path(root
, path
);
714 if (found_extent
&& disk_bytenr
!= 0) {
715 dec_i_blocks(inode
, extent_num_bytes
);
716 ret
= btrfs_free_extent(trans
, root
,
720 root_gen
, inode
->i_ino
,
725 if (!bookend
&& search_start
>= end
) {
732 if (bookend
&& found_inline
&& start
<= key
.offset
) {
734 new_size
= btrfs_file_extent_calc_inline_size(
736 dec_i_blocks(inode
, (extent_end
- key
.offset
) -
738 btrfs_truncate_item(trans
, root
, path
, new_size
, 0);
740 /* create bookend, splitting the extent in two */
741 if (bookend
&& found_extent
) {
742 struct btrfs_key ins
;
743 ins
.objectid
= inode
->i_ino
;
745 btrfs_set_key_type(&ins
, BTRFS_EXTENT_DATA_KEY
);
746 btrfs_release_path(root
, path
);
747 ret
= btrfs_insert_empty_item(trans
, root
, path
, &ins
,
750 leaf
= path
->nodes
[0];
752 btrfs_print_leaf(root
, leaf
);
753 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
);
756 extent
= btrfs_item_ptr(leaf
, path
->slots
[0],
757 struct btrfs_file_extent_item
);
758 write_extent_buffer(leaf
, &old
,
759 (unsigned long)extent
, sizeof(old
));
761 btrfs_set_file_extent_offset(leaf
, extent
,
762 le64_to_cpu(old
.offset
) + end
- key
.offset
);
763 WARN_ON(le64_to_cpu(old
.num_bytes
) <
765 btrfs_set_file_extent_num_bytes(leaf
, extent
,
767 btrfs_set_file_extent_type(leaf
, extent
,
768 BTRFS_FILE_EXTENT_REG
);
770 btrfs_mark_buffer_dirty(path
->nodes
[0]);
771 if (le64_to_cpu(old
.disk_bytenr
) != 0) {
773 btrfs_file_extent_num_bytes(leaf
,
781 btrfs_free_path(path
);
782 btrfs_check_file(root
, inode
);
787 * this gets pages into the page cache and locks them down
789 static int noinline
prepare_pages(struct btrfs_root
*root
, struct file
*file
,
790 struct page
**pages
, size_t num_pages
,
791 loff_t pos
, unsigned long first_index
,
792 unsigned long last_index
, size_t write_bytes
)
795 unsigned long index
= pos
>> PAGE_CACHE_SHIFT
;
796 struct inode
*inode
= fdentry(file
)->d_inode
;
801 start_pos
= pos
& ~((u64
)root
->sectorsize
- 1);
802 last_pos
= ((u64
)index
+ num_pages
) << PAGE_CACHE_SHIFT
;
804 memset(pages
, 0, num_pages
* sizeof(struct page
*));
806 for (i
= 0; i
< num_pages
; i
++) {
807 pages
[i
] = grab_cache_page(inode
->i_mapping
, index
+ i
);
812 wait_on_page_writeback(pages
[i
]);
814 if (start_pos
< inode
->i_size
) {
815 struct btrfs_ordered_extent
*ordered
;
816 lock_extent(&BTRFS_I(inode
)->io_tree
,
817 start_pos
, last_pos
- 1, GFP_NOFS
);
818 ordered
= btrfs_lookup_first_ordered_extent(inode
, last_pos
-1);
820 ordered
->file_offset
+ ordered
->len
> start_pos
&&
821 ordered
->file_offset
< last_pos
) {
822 btrfs_put_ordered_extent(ordered
);
823 unlock_extent(&BTRFS_I(inode
)->io_tree
,
824 start_pos
, last_pos
- 1, GFP_NOFS
);
825 for (i
= 0; i
< num_pages
; i
++) {
826 unlock_page(pages
[i
]);
827 page_cache_release(pages
[i
]);
829 btrfs_wait_ordered_range(inode
, start_pos
,
830 last_pos
- start_pos
);
834 btrfs_put_ordered_extent(ordered
);
836 clear_extent_bits(&BTRFS_I(inode
)->io_tree
, start_pos
,
837 last_pos
- 1, EXTENT_DIRTY
| EXTENT_DELALLOC
,
839 unlock_extent(&BTRFS_I(inode
)->io_tree
,
840 start_pos
, last_pos
- 1, GFP_NOFS
);
842 for (i
= 0; i
< num_pages
; i
++) {
843 clear_page_dirty_for_io(pages
[i
]);
844 set_page_extent_mapped(pages
[i
]);
845 WARN_ON(!PageLocked(pages
[i
]));
850 static ssize_t
btrfs_file_write(struct file
*file
, const char __user
*buf
,
851 size_t count
, loff_t
*ppos
)
855 ssize_t num_written
= 0;
858 struct inode
*inode
= fdentry(file
)->d_inode
;
859 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
860 struct page
**pages
= NULL
;
862 struct page
*pinned
[2];
863 unsigned long first_index
;
864 unsigned long last_index
;
866 nrptrs
= min((count
+ PAGE_CACHE_SIZE
- 1) / PAGE_CACHE_SIZE
,
867 PAGE_CACHE_SIZE
/ (sizeof(struct page
*)));
874 vfs_check_frozen(inode
->i_sb
, SB_FREEZE_WRITE
);
875 current
->backing_dev_info
= inode
->i_mapping
->backing_dev_info
;
876 err
= generic_write_checks(file
, &pos
, &count
, S_ISBLK(inode
->i_mode
));
881 #ifdef REMOVE_SUID_PATH
882 err
= remove_suid(&file
->f_path
);
884 # if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
885 err
= file_remove_suid(file
);
887 err
= remove_suid(fdentry(file
));
892 file_update_time(file
);
894 pages
= kmalloc(nrptrs
* sizeof(struct page
*), GFP_KERNEL
);
896 mutex_lock(&inode
->i_mutex
);
897 first_index
= pos
>> PAGE_CACHE_SHIFT
;
898 last_index
= (pos
+ count
) >> PAGE_CACHE_SHIFT
;
901 * if this is a nodatasum mount, force summing off for the inode
902 * all the time. That way a later mount with summing on won't
905 if (btrfs_test_opt(root
, NODATASUM
))
906 btrfs_set_flag(inode
, NODATASUM
);
909 * there are lots of better ways to do this, but this code
910 * makes sure the first and last page in the file range are
911 * up to date and ready for cow
913 if ((pos
& (PAGE_CACHE_SIZE
- 1))) {
914 pinned
[0] = grab_cache_page(inode
->i_mapping
, first_index
);
915 if (!PageUptodate(pinned
[0])) {
916 ret
= btrfs_readpage(NULL
, pinned
[0]);
918 wait_on_page_locked(pinned
[0]);
920 unlock_page(pinned
[0]);
923 if ((pos
+ count
) & (PAGE_CACHE_SIZE
- 1)) {
924 pinned
[1] = grab_cache_page(inode
->i_mapping
, last_index
);
925 if (!PageUptodate(pinned
[1])) {
926 ret
= btrfs_readpage(NULL
, pinned
[1]);
928 wait_on_page_locked(pinned
[1]);
930 unlock_page(pinned
[1]);
935 size_t offset
= pos
& (PAGE_CACHE_SIZE
- 1);
936 size_t write_bytes
= min(count
, nrptrs
*
937 (size_t)PAGE_CACHE_SIZE
-
939 size_t num_pages
= (write_bytes
+ PAGE_CACHE_SIZE
- 1) >>
942 WARN_ON(num_pages
> nrptrs
);
943 memset(pages
, 0, sizeof(pages
));
945 ret
= btrfs_check_free_space(root
, write_bytes
, 0);
949 ret
= prepare_pages(root
, file
, pages
, num_pages
,
950 pos
, first_index
, last_index
,
955 ret
= btrfs_copy_from_user(pos
, num_pages
,
956 write_bytes
, pages
, buf
);
958 btrfs_drop_pages(pages
, num_pages
);
962 ret
= dirty_and_release_pages(NULL
, root
, file
, pages
,
963 num_pages
, pos
, write_bytes
);
964 btrfs_drop_pages(pages
, num_pages
);
969 count
-= write_bytes
;
971 num_written
+= write_bytes
;
973 balance_dirty_pages_ratelimited_nr(inode
->i_mapping
, num_pages
);
974 if (num_pages
< (root
->leafsize
>> PAGE_CACHE_SHIFT
) + 1)
975 btrfs_btree_balance_dirty(root
, 1);
976 btrfs_throttle(root
);
980 mutex_unlock(&inode
->i_mutex
);
985 page_cache_release(pinned
[0]);
987 page_cache_release(pinned
[1]);
990 if (num_written
> 0 && ((file
->f_flags
& O_SYNC
) || IS_SYNC(inode
))) {
991 err
= sync_page_range(inode
, inode
->i_mapping
,
992 start_pos
, num_written
);
995 } else if (num_written
> 0 && (file
->f_flags
& O_DIRECT
)) {
996 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22)
997 do_sync_file_range(file
, start_pos
,
998 start_pos
+ num_written
- 1,
999 SYNC_FILE_RANGE_WRITE
|
1000 SYNC_FILE_RANGE_WAIT_AFTER
);
1002 do_sync_mapping_range(inode
->i_mapping
, start_pos
,
1003 start_pos
+ num_written
- 1,
1004 SYNC_FILE_RANGE_WRITE
|
1005 SYNC_FILE_RANGE_WAIT_AFTER
);
1007 invalidate_mapping_pages(inode
->i_mapping
,
1008 start_pos
>> PAGE_CACHE_SHIFT
,
1009 (start_pos
+ num_written
- 1) >> PAGE_CACHE_SHIFT
);
1011 current
->backing_dev_info
= NULL
;
1012 return num_written
? num_written
: err
;
1015 int btrfs_release_file(struct inode
* inode
, struct file
* filp
)
1017 if (filp
->private_data
)
1018 btrfs_ioctl_trans_end(filp
);
1022 static int btrfs_sync_file(struct file
*file
,
1023 struct dentry
*dentry
, int datasync
)
1025 struct inode
*inode
= dentry
->d_inode
;
1026 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1028 struct btrfs_trans_handle
*trans
;
1031 * check the transaction that last modified this inode
1032 * and see if its already been committed
1034 if (!BTRFS_I(inode
)->last_trans
)
1037 mutex_lock(&root
->fs_info
->trans_mutex
);
1038 if (BTRFS_I(inode
)->last_trans
<=
1039 root
->fs_info
->last_trans_committed
) {
1040 BTRFS_I(inode
)->last_trans
= 0;
1041 mutex_unlock(&root
->fs_info
->trans_mutex
);
1044 mutex_unlock(&root
->fs_info
->trans_mutex
);
1047 * ok we haven't committed the transaction yet, lets do a commit
1049 if (file
->private_data
)
1050 btrfs_ioctl_trans_end(file
);
1052 trans
= btrfs_start_transaction(root
, 1);
1057 ret
= btrfs_commit_transaction(trans
, root
);
1059 return ret
> 0 ? EIO
: ret
;
1062 static struct vm_operations_struct btrfs_file_vm_ops
= {
1063 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
1064 .nopage
= filemap_nopage
,
1065 .populate
= filemap_populate
,
1067 .fault
= filemap_fault
,
1069 .page_mkwrite
= btrfs_page_mkwrite
,
1072 static int btrfs_file_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
1074 vma
->vm_ops
= &btrfs_file_vm_ops
;
1075 file_accessed(filp
);
1079 struct file_operations btrfs_file_operations
= {
1080 .llseek
= generic_file_llseek
,
1081 .read
= do_sync_read
,
1082 .aio_read
= generic_file_aio_read
,
1083 .splice_read
= generic_file_splice_read
,
1084 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1085 .sendfile
= generic_file_sendfile
,
1087 .write
= btrfs_file_write
,
1088 .mmap
= btrfs_file_mmap
,
1089 .open
= generic_file_open
,
1090 .release
= btrfs_release_file
,
1091 .fsync
= btrfs_sync_file
,
1092 .unlocked_ioctl
= btrfs_ioctl
,
1093 #ifdef CONFIG_COMPAT
1094 .compat_ioctl
= btrfs_ioctl
,