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