Merge commit 'v2.6.34-rc2' into perf/core
[linux-2.6-block.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>
39279cc3
CM
25#include <linux/backing-dev.h>
26#include <linux/mpage.h>
27#include <linux/swap.h>
28#include <linux/writeback.h>
29#include <linux/statfs.h>
30#include <linux/compat.h>
31#include "ctree.h"
32#include "disk-io.h"
33#include "transaction.h"
34#include "btrfs_inode.h"
35#include "ioctl.h"
36#include "print-tree.h"
e02119d5
CM
37#include "tree-log.h"
38#include "locking.h"
12fa8ec6 39#include "compat.h"
39279cc3
CM
40
41
d352ac68
CM
42/* simple helper to fault in pages and copy. This should go away
43 * and be replaced with calls into generic code.
44 */
d397712b 45static noinline int btrfs_copy_from_user(loff_t pos, int num_pages,
a1b32a59
CM
46 int write_bytes,
47 struct page **prepared_pages,
d397712b 48 const char __user *buf)
39279cc3
CM
49{
50 long page_fault = 0;
51 int i;
52 int offset = pos & (PAGE_CACHE_SIZE - 1);
53
54 for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
55 size_t count = min_t(size_t,
56 PAGE_CACHE_SIZE - offset, write_bytes);
57 struct page *page = prepared_pages[i];
58 fault_in_pages_readable(buf, count);
59
60 /* Copy data from userspace to the current page */
61 kmap(page);
62 page_fault = __copy_from_user(page_address(page) + offset,
63 buf, count);
64 /* Flush processor's dcache for this page */
65 flush_dcache_page(page);
66 kunmap(page);
67 buf += count;
68 write_bytes -= count;
69
70 if (page_fault)
71 break;
72 }
73 return page_fault ? -EFAULT : 0;
74}
75
d352ac68
CM
76/*
77 * unlocks pages after btrfs_file_write is done with them
78 */
d397712b 79static noinline void btrfs_drop_pages(struct page **pages, size_t num_pages)
39279cc3
CM
80{
81 size_t i;
82 for (i = 0; i < num_pages; i++) {
83 if (!pages[i])
84 break;
d352ac68
CM
85 /* page checked is some magic around finding pages that
86 * have been modified without going through btrfs_set_page_dirty
87 * clear it here
88 */
4a096752 89 ClearPageChecked(pages[i]);
39279cc3
CM
90 unlock_page(pages[i]);
91 mark_page_accessed(pages[i]);
92 page_cache_release(pages[i]);
93 }
94}
95
d352ac68
CM
96/*
97 * after copy_from_user, pages need to be dirtied and we need to make
98 * sure holes are created between the current EOF and the start of
99 * any next extents (if required).
100 *
101 * this also makes the decision about creating an inline extent vs
102 * doing real data extents, marking pages dirty and delalloc as required.
103 */
d397712b 104static noinline int dirty_and_release_pages(struct btrfs_trans_handle *trans,
39279cc3
CM
105 struct btrfs_root *root,
106 struct file *file,
107 struct page **pages,
108 size_t num_pages,
109 loff_t pos,
110 size_t write_bytes)
111{
39279cc3 112 int err = 0;
a52d9a80 113 int i;
6da6abae 114 struct inode *inode = fdentry(file)->d_inode;
db94535d 115 u64 num_bytes;
a52d9a80
CM
116 u64 start_pos;
117 u64 end_of_last_block;
118 u64 end_pos = pos + write_bytes;
119 loff_t isize = i_size_read(inode);
39279cc3 120
5f39d397 121 start_pos = pos & ~((u64)root->sectorsize - 1);
db94535d
CM
122 num_bytes = (write_bytes + pos - start_pos +
123 root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
39279cc3 124
db94535d 125 end_of_last_block = start_pos + num_bytes - 1;
2ac55d41
JB
126 err = btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block,
127 NULL);
9ed74f2d
JB
128 if (err)
129 return err;
130
c8b97818
CM
131 for (i = 0; i < num_pages; i++) {
132 struct page *p = pages[i];
133 SetPageUptodate(p);
134 ClearPageChecked(p);
135 set_page_dirty(p);
a52d9a80
CM
136 }
137 if (end_pos > isize) {
138 i_size_write(inode, end_pos);
f597bb19
CM
139 /* we've only changed i_size in ram, and we haven't updated
140 * the disk i_size. There is no need to log the inode
141 * at this time.
142 */
39279cc3 143 }
39279cc3
CM
144 return err;
145}
146
d352ac68
CM
147/*
148 * this drops all the extents in the cache that intersect the range
149 * [start, end]. Existing extents are split as required.
150 */
5b21f2ed
ZY
151int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end,
152 int skip_pinned)
a52d9a80
CM
153{
154 struct extent_map *em;
3b951516
CM
155 struct extent_map *split = NULL;
156 struct extent_map *split2 = NULL;
a52d9a80 157 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
39b5637f 158 u64 len = end - start + 1;
3b951516
CM
159 int ret;
160 int testend = 1;
5b21f2ed 161 unsigned long flags;
c8b97818 162 int compressed = 0;
a52d9a80 163
e6dcd2dc 164 WARN_ON(end < start);
3b951516 165 if (end == (u64)-1) {
39b5637f 166 len = (u64)-1;
3b951516
CM
167 testend = 0;
168 }
d397712b 169 while (1) {
3b951516
CM
170 if (!split)
171 split = alloc_extent_map(GFP_NOFS);
172 if (!split2)
173 split2 = alloc_extent_map(GFP_NOFS);
174
890871be 175 write_lock(&em_tree->lock);
39b5637f 176 em = lookup_extent_mapping(em_tree, start, len);
d1310b2e 177 if (!em) {
890871be 178 write_unlock(&em_tree->lock);
a52d9a80 179 break;
d1310b2e 180 }
5b21f2ed
ZY
181 flags = em->flags;
182 if (skip_pinned && test_bit(EXTENT_FLAG_PINNED, &em->flags)) {
55ef6899 183 if (testend && em->start + em->len >= start + len) {
5b21f2ed 184 free_extent_map(em);
a1ed835e 185 write_unlock(&em_tree->lock);
5b21f2ed
ZY
186 break;
187 }
55ef6899
YZ
188 start = em->start + em->len;
189 if (testend)
5b21f2ed 190 len = start + len - (em->start + em->len);
5b21f2ed 191 free_extent_map(em);
a1ed835e 192 write_unlock(&em_tree->lock);
5b21f2ed
ZY
193 continue;
194 }
c8b97818 195 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
3ce7e67a 196 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
a52d9a80 197 remove_extent_mapping(em_tree, em);
3b951516
CM
198
199 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
200 em->start < start) {
201 split->start = em->start;
202 split->len = start - em->start;
ff5b7ee3 203 split->orig_start = em->orig_start;
3b951516 204 split->block_start = em->block_start;
c8b97818
CM
205
206 if (compressed)
207 split->block_len = em->block_len;
208 else
209 split->block_len = split->len;
210
3b951516 211 split->bdev = em->bdev;
5b21f2ed 212 split->flags = flags;
3b951516
CM
213 ret = add_extent_mapping(em_tree, split);
214 BUG_ON(ret);
215 free_extent_map(split);
216 split = split2;
217 split2 = NULL;
218 }
219 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
220 testend && em->start + em->len > start + len) {
221 u64 diff = start + len - em->start;
222
223 split->start = start + len;
224 split->len = em->start + em->len - (start + len);
225 split->bdev = em->bdev;
5b21f2ed 226 split->flags = flags;
3b951516 227
c8b97818
CM
228 if (compressed) {
229 split->block_len = em->block_len;
230 split->block_start = em->block_start;
445a6944 231 split->orig_start = em->orig_start;
c8b97818
CM
232 } else {
233 split->block_len = split->len;
234 split->block_start = em->block_start + diff;
445a6944 235 split->orig_start = split->start;
c8b97818 236 }
3b951516
CM
237
238 ret = add_extent_mapping(em_tree, split);
239 BUG_ON(ret);
240 free_extent_map(split);
241 split = NULL;
242 }
890871be 243 write_unlock(&em_tree->lock);
d1310b2e 244
a52d9a80
CM
245 /* once for us */
246 free_extent_map(em);
247 /* once for the tree*/
248 free_extent_map(em);
249 }
3b951516
CM
250 if (split)
251 free_extent_map(split);
252 if (split2)
253 free_extent_map(split2);
a52d9a80
CM
254 return 0;
255}
256
39279cc3
CM
257/*
258 * this is very complex, but the basic idea is to drop all extents
259 * in the range start - end. hint_block is filled in with a block number
260 * that would be a good hint to the block allocator for this file.
261 *
262 * If an extent intersects the range but is not entirely inside the range
263 * it is either truncated or split. Anything entirely inside the range
264 * is deleted from the tree.
265 */
920bbbfb
YZ
266int btrfs_drop_extents(struct btrfs_trans_handle *trans, struct inode *inode,
267 u64 start, u64 end, u64 *hint_byte, int drop_cache)
39279cc3 268{
920bbbfb 269 struct btrfs_root *root = BTRFS_I(inode)->root;
5f39d397 270 struct extent_buffer *leaf;
920bbbfb 271 struct btrfs_file_extent_item *fi;
39279cc3 272 struct btrfs_path *path;
00f5c795 273 struct btrfs_key key;
920bbbfb
YZ
274 struct btrfs_key new_key;
275 u64 search_start = start;
276 u64 disk_bytenr = 0;
277 u64 num_bytes = 0;
278 u64 extent_offset = 0;
279 u64 extent_end = 0;
280 int del_nr = 0;
281 int del_slot = 0;
282 int extent_type;
ccd467d6 283 int recow;
00f5c795 284 int ret;
39279cc3 285
a1ed835e
CM
286 if (drop_cache)
287 btrfs_drop_extent_cache(inode, start, end - 1, 0);
a52d9a80 288
39279cc3
CM
289 path = btrfs_alloc_path();
290 if (!path)
291 return -ENOMEM;
920bbbfb 292
d397712b 293 while (1) {
ccd467d6 294 recow = 0;
39279cc3
CM
295 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
296 search_start, -1);
297 if (ret < 0)
920bbbfb
YZ
298 break;
299 if (ret > 0 && path->slots[0] > 0 && search_start == start) {
300 leaf = path->nodes[0];
301 btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1);
302 if (key.objectid == inode->i_ino &&
303 key.type == BTRFS_EXTENT_DATA_KEY)
304 path->slots[0]--;
39279cc3 305 }
920bbbfb 306 ret = 0;
8c2383c3 307next_slot:
5f39d397 308 leaf = path->nodes[0];
920bbbfb
YZ
309 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
310 BUG_ON(del_nr > 0);
311 ret = btrfs_next_leaf(root, path);
312 if (ret < 0)
313 break;
314 if (ret > 0) {
315 ret = 0;
316 break;
8c2383c3 317 }
920bbbfb
YZ
318 leaf = path->nodes[0];
319 recow = 1;
320 }
321
322 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
323 if (key.objectid > inode->i_ino ||
324 key.type > BTRFS_EXTENT_DATA_KEY || key.offset >= end)
325 break;
326
327 fi = btrfs_item_ptr(leaf, path->slots[0],
328 struct btrfs_file_extent_item);
329 extent_type = btrfs_file_extent_type(leaf, fi);
330
331 if (extent_type == BTRFS_FILE_EXTENT_REG ||
332 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
333 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
334 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
335 extent_offset = btrfs_file_extent_offset(leaf, fi);
336 extent_end = key.offset +
337 btrfs_file_extent_num_bytes(leaf, fi);
338 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
339 extent_end = key.offset +
340 btrfs_file_extent_inline_len(leaf, fi);
8c2383c3 341 } else {
920bbbfb 342 WARN_ON(1);
8c2383c3 343 extent_end = search_start;
39279cc3
CM
344 }
345
920bbbfb
YZ
346 if (extent_end <= search_start) {
347 path->slots[0]++;
8c2383c3 348 goto next_slot;
39279cc3
CM
349 }
350
920bbbfb
YZ
351 search_start = max(key.offset, start);
352 if (recow) {
353 btrfs_release_path(root, path);
354 continue;
39279cc3 355 }
6643558d 356
920bbbfb
YZ
357 /*
358 * | - range to drop - |
359 * | -------- extent -------- |
360 */
361 if (start > key.offset && end < extent_end) {
362 BUG_ON(del_nr > 0);
363 BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
364
365 memcpy(&new_key, &key, sizeof(new_key));
366 new_key.offset = start;
367 ret = btrfs_duplicate_item(trans, root, path,
368 &new_key);
369 if (ret == -EAGAIN) {
370 btrfs_release_path(root, path);
371 continue;
6643558d 372 }
920bbbfb
YZ
373 if (ret < 0)
374 break;
375
376 leaf = path->nodes[0];
377 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
378 struct btrfs_file_extent_item);
379 btrfs_set_file_extent_num_bytes(leaf, fi,
380 start - key.offset);
381
382 fi = btrfs_item_ptr(leaf, path->slots[0],
383 struct btrfs_file_extent_item);
384
385 extent_offset += start - key.offset;
386 btrfs_set_file_extent_offset(leaf, fi, extent_offset);
387 btrfs_set_file_extent_num_bytes(leaf, fi,
388 extent_end - start);
389 btrfs_mark_buffer_dirty(leaf);
390
391 if (disk_bytenr > 0) {
771ed689 392 ret = btrfs_inc_extent_ref(trans, root,
920bbbfb
YZ
393 disk_bytenr, num_bytes, 0,
394 root->root_key.objectid,
395 new_key.objectid,
396 start - extent_offset);
771ed689 397 BUG_ON(ret);
920bbbfb 398 *hint_byte = disk_bytenr;
771ed689 399 }
920bbbfb 400 key.offset = start;
6643558d 401 }
920bbbfb
YZ
402 /*
403 * | ---- range to drop ----- |
404 * | -------- extent -------- |
405 */
406 if (start <= key.offset && end < extent_end) {
407 BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
6643558d 408
920bbbfb
YZ
409 memcpy(&new_key, &key, sizeof(new_key));
410 new_key.offset = end;
411 btrfs_set_item_key_safe(trans, root, path, &new_key);
6643558d 412
920bbbfb
YZ
413 extent_offset += end - key.offset;
414 btrfs_set_file_extent_offset(leaf, fi, extent_offset);
415 btrfs_set_file_extent_num_bytes(leaf, fi,
416 extent_end - end);
417 btrfs_mark_buffer_dirty(leaf);
418 if (disk_bytenr > 0) {
419 inode_sub_bytes(inode, end - key.offset);
420 *hint_byte = disk_bytenr;
39279cc3 421 }
920bbbfb 422 break;
39279cc3 423 }
771ed689 424
920bbbfb
YZ
425 search_start = extent_end;
426 /*
427 * | ---- range to drop ----- |
428 * | -------- extent -------- |
429 */
430 if (start > key.offset && end >= extent_end) {
431 BUG_ON(del_nr > 0);
432 BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
8c2383c3 433
920bbbfb
YZ
434 btrfs_set_file_extent_num_bytes(leaf, fi,
435 start - key.offset);
436 btrfs_mark_buffer_dirty(leaf);
437 if (disk_bytenr > 0) {
438 inode_sub_bytes(inode, extent_end - start);
439 *hint_byte = disk_bytenr;
440 }
441 if (end == extent_end)
442 break;
c8b97818 443
920bbbfb
YZ
444 path->slots[0]++;
445 goto next_slot;
31840ae1
ZY
446 }
447
920bbbfb
YZ
448 /*
449 * | ---- range to drop ----- |
450 * | ------ extent ------ |
451 */
452 if (start <= key.offset && end >= extent_end) {
453 if (del_nr == 0) {
454 del_slot = path->slots[0];
455 del_nr = 1;
456 } else {
457 BUG_ON(del_slot + del_nr != path->slots[0]);
458 del_nr++;
459 }
31840ae1 460
920bbbfb 461 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
a76a3cd4 462 inode_sub_bytes(inode,
920bbbfb
YZ
463 extent_end - key.offset);
464 extent_end = ALIGN(extent_end,
465 root->sectorsize);
466 } else if (disk_bytenr > 0) {
31840ae1 467 ret = btrfs_free_extent(trans, root,
920bbbfb
YZ
468 disk_bytenr, num_bytes, 0,
469 root->root_key.objectid,
5d4f98a2 470 key.objectid, key.offset -
920bbbfb 471 extent_offset);
31840ae1 472 BUG_ON(ret);
920bbbfb
YZ
473 inode_sub_bytes(inode,
474 extent_end - key.offset);
475 *hint_byte = disk_bytenr;
31840ae1 476 }
31840ae1 477
920bbbfb
YZ
478 if (end == extent_end)
479 break;
480
481 if (path->slots[0] + 1 < btrfs_header_nritems(leaf)) {
482 path->slots[0]++;
483 goto next_slot;
484 }
485
486 ret = btrfs_del_items(trans, root, path, del_slot,
487 del_nr);
488 BUG_ON(ret);
489
490 del_nr = 0;
491 del_slot = 0;
492
493 btrfs_release_path(root, path);
494 continue;
39279cc3 495 }
920bbbfb
YZ
496
497 BUG_ON(1);
39279cc3 498 }
920bbbfb
YZ
499
500 if (del_nr > 0) {
501 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
502 BUG_ON(ret);
6643558d 503 }
920bbbfb
YZ
504
505 btrfs_free_path(path);
39279cc3
CM
506 return ret;
507}
508
d899e052 509static int extent_mergeable(struct extent_buffer *leaf, int slot,
6c7d54ac
YZ
510 u64 objectid, u64 bytenr, u64 orig_offset,
511 u64 *start, u64 *end)
d899e052
YZ
512{
513 struct btrfs_file_extent_item *fi;
514 struct btrfs_key key;
515 u64 extent_end;
516
517 if (slot < 0 || slot >= btrfs_header_nritems(leaf))
518 return 0;
519
520 btrfs_item_key_to_cpu(leaf, &key, slot);
521 if (key.objectid != objectid || key.type != BTRFS_EXTENT_DATA_KEY)
522 return 0;
523
524 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
525 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG ||
526 btrfs_file_extent_disk_bytenr(leaf, fi) != bytenr ||
6c7d54ac 527 btrfs_file_extent_offset(leaf, fi) != key.offset - orig_offset ||
d899e052
YZ
528 btrfs_file_extent_compression(leaf, fi) ||
529 btrfs_file_extent_encryption(leaf, fi) ||
530 btrfs_file_extent_other_encoding(leaf, fi))
531 return 0;
532
533 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
534 if ((*start && *start != key.offset) || (*end && *end != extent_end))
535 return 0;
536
537 *start = key.offset;
538 *end = extent_end;
539 return 1;
540}
541
542/*
543 * Mark extent in the range start - end as written.
544 *
545 * This changes extent type from 'pre-allocated' to 'regular'. If only
546 * part of extent is marked as written, the extent will be split into
547 * two or three.
548 */
549int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
d899e052
YZ
550 struct inode *inode, u64 start, u64 end)
551{
920bbbfb 552 struct btrfs_root *root = BTRFS_I(inode)->root;
d899e052
YZ
553 struct extent_buffer *leaf;
554 struct btrfs_path *path;
555 struct btrfs_file_extent_item *fi;
556 struct btrfs_key key;
920bbbfb 557 struct btrfs_key new_key;
d899e052
YZ
558 u64 bytenr;
559 u64 num_bytes;
560 u64 extent_end;
5d4f98a2 561 u64 orig_offset;
d899e052
YZ
562 u64 other_start;
563 u64 other_end;
920bbbfb
YZ
564 u64 split;
565 int del_nr = 0;
566 int del_slot = 0;
6c7d54ac 567 int recow;
d899e052
YZ
568 int ret;
569
570 btrfs_drop_extent_cache(inode, start, end - 1, 0);
571
572 path = btrfs_alloc_path();
573 BUG_ON(!path);
574again:
6c7d54ac 575 recow = 0;
920bbbfb 576 split = start;
d899e052
YZ
577 key.objectid = inode->i_ino;
578 key.type = BTRFS_EXTENT_DATA_KEY;
920bbbfb 579 key.offset = split;
d899e052
YZ
580
581 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
582 if (ret > 0 && path->slots[0] > 0)
583 path->slots[0]--;
584
585 leaf = path->nodes[0];
586 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
587 BUG_ON(key.objectid != inode->i_ino ||
588 key.type != BTRFS_EXTENT_DATA_KEY);
589 fi = btrfs_item_ptr(leaf, path->slots[0],
590 struct btrfs_file_extent_item);
920bbbfb
YZ
591 BUG_ON(btrfs_file_extent_type(leaf, fi) !=
592 BTRFS_FILE_EXTENT_PREALLOC);
d899e052
YZ
593 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
594 BUG_ON(key.offset > start || extent_end < end);
595
596 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
597 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
5d4f98a2 598 orig_offset = key.offset - btrfs_file_extent_offset(leaf, fi);
6c7d54ac
YZ
599 memcpy(&new_key, &key, sizeof(new_key));
600
601 if (start == key.offset && end < extent_end) {
602 other_start = 0;
603 other_end = start;
604 if (extent_mergeable(leaf, path->slots[0] - 1,
605 inode->i_ino, bytenr, orig_offset,
606 &other_start, &other_end)) {
607 new_key.offset = end;
608 btrfs_set_item_key_safe(trans, root, path, &new_key);
609 fi = btrfs_item_ptr(leaf, path->slots[0],
610 struct btrfs_file_extent_item);
611 btrfs_set_file_extent_num_bytes(leaf, fi,
612 extent_end - end);
613 btrfs_set_file_extent_offset(leaf, fi,
614 end - orig_offset);
615 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
616 struct btrfs_file_extent_item);
617 btrfs_set_file_extent_num_bytes(leaf, fi,
618 end - other_start);
619 btrfs_mark_buffer_dirty(leaf);
620 goto out;
621 }
622 }
623
624 if (start > key.offset && end == extent_end) {
625 other_start = end;
626 other_end = 0;
627 if (extent_mergeable(leaf, path->slots[0] + 1,
628 inode->i_ino, bytenr, orig_offset,
629 &other_start, &other_end)) {
630 fi = btrfs_item_ptr(leaf, path->slots[0],
631 struct btrfs_file_extent_item);
632 btrfs_set_file_extent_num_bytes(leaf, fi,
633 start - key.offset);
634 path->slots[0]++;
635 new_key.offset = start;
636 btrfs_set_item_key_safe(trans, root, path, &new_key);
637
638 fi = btrfs_item_ptr(leaf, path->slots[0],
639 struct btrfs_file_extent_item);
640 btrfs_set_file_extent_num_bytes(leaf, fi,
641 other_end - start);
642 btrfs_set_file_extent_offset(leaf, fi,
643 start - orig_offset);
644 btrfs_mark_buffer_dirty(leaf);
645 goto out;
646 }
647 }
d899e052 648
920bbbfb
YZ
649 while (start > key.offset || end < extent_end) {
650 if (key.offset == start)
651 split = end;
652
920bbbfb
YZ
653 new_key.offset = split;
654 ret = btrfs_duplicate_item(trans, root, path, &new_key);
655 if (ret == -EAGAIN) {
656 btrfs_release_path(root, path);
657 goto again;
d899e052 658 }
920bbbfb 659 BUG_ON(ret < 0);
d899e052 660
920bbbfb
YZ
661 leaf = path->nodes[0];
662 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
d899e052 663 struct btrfs_file_extent_item);
d899e052 664 btrfs_set_file_extent_num_bytes(leaf, fi,
920bbbfb
YZ
665 split - key.offset);
666
667 fi = btrfs_item_ptr(leaf, path->slots[0],
668 struct btrfs_file_extent_item);
669
670 btrfs_set_file_extent_offset(leaf, fi, split - orig_offset);
671 btrfs_set_file_extent_num_bytes(leaf, fi,
672 extent_end - split);
d899e052
YZ
673 btrfs_mark_buffer_dirty(leaf);
674
920bbbfb
YZ
675 ret = btrfs_inc_extent_ref(trans, root, bytenr, num_bytes, 0,
676 root->root_key.objectid,
677 inode->i_ino, orig_offset);
d899e052 678 BUG_ON(ret);
d899e052 679
920bbbfb
YZ
680 if (split == start) {
681 key.offset = start;
682 } else {
683 BUG_ON(start != key.offset);
d899e052 684 path->slots[0]--;
920bbbfb 685 extent_end = end;
d899e052 686 }
6c7d54ac 687 recow = 1;
d899e052
YZ
688 }
689
920bbbfb
YZ
690 other_start = end;
691 other_end = 0;
6c7d54ac
YZ
692 if (extent_mergeable(leaf, path->slots[0] + 1,
693 inode->i_ino, bytenr, orig_offset,
694 &other_start, &other_end)) {
695 if (recow) {
696 btrfs_release_path(root, path);
697 goto again;
698 }
920bbbfb
YZ
699 extent_end = other_end;
700 del_slot = path->slots[0] + 1;
701 del_nr++;
702 ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
703 0, root->root_key.objectid,
704 inode->i_ino, orig_offset);
705 BUG_ON(ret);
d899e052 706 }
920bbbfb
YZ
707 other_start = 0;
708 other_end = start;
6c7d54ac
YZ
709 if (extent_mergeable(leaf, path->slots[0] - 1,
710 inode->i_ino, bytenr, orig_offset,
711 &other_start, &other_end)) {
712 if (recow) {
713 btrfs_release_path(root, path);
714 goto again;
715 }
920bbbfb
YZ
716 key.offset = other_start;
717 del_slot = path->slots[0];
718 del_nr++;
719 ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
720 0, root->root_key.objectid,
721 inode->i_ino, orig_offset);
722 BUG_ON(ret);
723 }
724 if (del_nr == 0) {
3f6fae95
SL
725 fi = btrfs_item_ptr(leaf, path->slots[0],
726 struct btrfs_file_extent_item);
920bbbfb
YZ
727 btrfs_set_file_extent_type(leaf, fi,
728 BTRFS_FILE_EXTENT_REG);
729 btrfs_mark_buffer_dirty(leaf);
6c7d54ac 730 } else {
3f6fae95
SL
731 fi = btrfs_item_ptr(leaf, del_slot - 1,
732 struct btrfs_file_extent_item);
6c7d54ac
YZ
733 btrfs_set_file_extent_type(leaf, fi,
734 BTRFS_FILE_EXTENT_REG);
735 btrfs_set_file_extent_num_bytes(leaf, fi,
736 extent_end - key.offset);
737 btrfs_mark_buffer_dirty(leaf);
920bbbfb 738
6c7d54ac
YZ
739 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
740 BUG_ON(ret);
741 }
920bbbfb 742out:
d899e052
YZ
743 btrfs_free_path(path);
744 return 0;
745}
746
39279cc3 747/*
d352ac68
CM
748 * this gets pages into the page cache and locks them down, it also properly
749 * waits for data=ordered extents to finish before allowing the pages to be
750 * modified.
39279cc3 751 */
d397712b 752static noinline int prepare_pages(struct btrfs_root *root, struct file *file,
98ed5174
CM
753 struct page **pages, size_t num_pages,
754 loff_t pos, unsigned long first_index,
755 unsigned long last_index, size_t write_bytes)
39279cc3 756{
2ac55d41 757 struct extent_state *cached_state = NULL;
39279cc3
CM
758 int i;
759 unsigned long index = pos >> PAGE_CACHE_SHIFT;
6da6abae 760 struct inode *inode = fdentry(file)->d_inode;
39279cc3 761 int err = 0;
8c2383c3 762 u64 start_pos;
e6dcd2dc 763 u64 last_pos;
8c2383c3 764
5f39d397 765 start_pos = pos & ~((u64)root->sectorsize - 1);
e6dcd2dc 766 last_pos = ((u64)index + num_pages) << PAGE_CACHE_SHIFT;
39279cc3 767
9036c102
YZ
768 if (start_pos > inode->i_size) {
769 err = btrfs_cont_expand(inode, start_pos);
770 if (err)
771 return err;
772 }
773
39279cc3 774 memset(pages, 0, num_pages * sizeof(struct page *));
e6dcd2dc 775again:
39279cc3
CM
776 for (i = 0; i < num_pages; i++) {
777 pages[i] = grab_cache_page(inode->i_mapping, index + i);
778 if (!pages[i]) {
779 err = -ENOMEM;
a52d9a80 780 BUG_ON(1);
39279cc3 781 }
ccd467d6 782 wait_on_page_writeback(pages[i]);
39279cc3 783 }
0762704b 784 if (start_pos < inode->i_size) {
e6dcd2dc 785 struct btrfs_ordered_extent *ordered;
2ac55d41
JB
786 lock_extent_bits(&BTRFS_I(inode)->io_tree,
787 start_pos, last_pos - 1, 0, &cached_state,
788 GFP_NOFS);
d397712b
CM
789 ordered = btrfs_lookup_first_ordered_extent(inode,
790 last_pos - 1);
e6dcd2dc
CM
791 if (ordered &&
792 ordered->file_offset + ordered->len > start_pos &&
793 ordered->file_offset < last_pos) {
794 btrfs_put_ordered_extent(ordered);
2ac55d41
JB
795 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
796 start_pos, last_pos - 1,
797 &cached_state, GFP_NOFS);
e6dcd2dc
CM
798 for (i = 0; i < num_pages; i++) {
799 unlock_page(pages[i]);
800 page_cache_release(pages[i]);
801 }
802 btrfs_wait_ordered_range(inode, start_pos,
803 last_pos - start_pos);
804 goto again;
805 }
806 if (ordered)
807 btrfs_put_ordered_extent(ordered);
808
2ac55d41 809 clear_extent_bit(&BTRFS_I(inode)->io_tree, start_pos,
32c00aff 810 last_pos - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
2ac55d41 811 EXTENT_DO_ACCOUNTING, 0, 0, &cached_state,
0762704b 812 GFP_NOFS);
2ac55d41
JB
813 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
814 start_pos, last_pos - 1, &cached_state,
815 GFP_NOFS);
0762704b 816 }
e6dcd2dc 817 for (i = 0; i < num_pages; i++) {
f87f057b 818 clear_page_dirty_for_io(pages[i]);
e6dcd2dc
CM
819 set_page_extent_mapped(pages[i]);
820 WARN_ON(!PageLocked(pages[i]));
821 }
39279cc3 822 return 0;
39279cc3
CM
823}
824
825static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
826 size_t count, loff_t *ppos)
827{
828 loff_t pos;
2ff3e9b6
CM
829 loff_t start_pos;
830 ssize_t num_written = 0;
831 ssize_t err = 0;
39279cc3 832 int ret = 0;
6da6abae 833 struct inode *inode = fdentry(file)->d_inode;
39279cc3 834 struct btrfs_root *root = BTRFS_I(inode)->root;
8c2383c3
CM
835 struct page **pages = NULL;
836 int nrptrs;
39279cc3
CM
837 struct page *pinned[2];
838 unsigned long first_index;
839 unsigned long last_index;
cb843a6f
CM
840 int will_write;
841
6b2f3d1f 842 will_write = ((file->f_flags & O_DSYNC) || IS_SYNC(inode) ||
cb843a6f 843 (file->f_flags & O_DIRECT));
8c2383c3
CM
844
845 nrptrs = min((count + PAGE_CACHE_SIZE - 1) / PAGE_CACHE_SIZE,
846 PAGE_CACHE_SIZE / (sizeof(struct page *)));
39279cc3
CM
847 pinned[0] = NULL;
848 pinned[1] = NULL;
2ff3e9b6 849
39279cc3 850 pos = *ppos;
2ff3e9b6
CM
851 start_pos = pos;
852
39279cc3 853 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
ab93dbec
CM
854
855 /* do the reserve before the mutex lock in case we have to do some
856 * flushing. We wouldn't deadlock, but this is more polite.
857 */
858 err = btrfs_reserve_metadata_for_delalloc(root, inode, 1);
859 if (err)
860 goto out_nolock;
861
862 mutex_lock(&inode->i_mutex);
863
39279cc3
CM
864 current->backing_dev_info = inode->i_mapping->backing_dev_info;
865 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
866 if (err)
ab93dbec
CM
867 goto out;
868
39279cc3 869 if (count == 0)
ab93dbec 870 goto out;
2b1f55b0 871
0ee0fda0 872 err = file_remove_suid(file);
39279cc3 873 if (err)
ab93dbec 874 goto out;
9ed74f2d 875
39279cc3
CM
876 file_update_time(file);
877
8c2383c3 878 pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
39279cc3 879
ab93dbec
CM
880 /* generic_write_checks can change our pos */
881 start_pos = pos;
882
c3027eb5 883 BTRFS_I(inode)->sequence++;
39279cc3
CM
884 first_index = pos >> PAGE_CACHE_SHIFT;
885 last_index = (pos + count) >> PAGE_CACHE_SHIFT;
886
887 /*
888 * there are lots of better ways to do this, but this code
889 * makes sure the first and last page in the file range are
890 * up to date and ready for cow
891 */
892 if ((pos & (PAGE_CACHE_SIZE - 1))) {
893 pinned[0] = grab_cache_page(inode->i_mapping, first_index);
894 if (!PageUptodate(pinned[0])) {
9ebefb18 895 ret = btrfs_readpage(NULL, pinned[0]);
39279cc3
CM
896 BUG_ON(ret);
897 wait_on_page_locked(pinned[0]);
898 } else {
899 unlock_page(pinned[0]);
900 }
901 }
902 if ((pos + count) & (PAGE_CACHE_SIZE - 1)) {
903 pinned[1] = grab_cache_page(inode->i_mapping, last_index);
904 if (!PageUptodate(pinned[1])) {
9ebefb18 905 ret = btrfs_readpage(NULL, pinned[1]);
39279cc3
CM
906 BUG_ON(ret);
907 wait_on_page_locked(pinned[1]);
908 } else {
909 unlock_page(pinned[1]);
910 }
911 }
912
d397712b 913 while (count > 0) {
39279cc3 914 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
11bd143f
CM
915 size_t write_bytes = min(count, nrptrs *
916 (size_t)PAGE_CACHE_SIZE -
8c2383c3 917 offset);
39279cc3
CM
918 size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
919 PAGE_CACHE_SHIFT;
920
8c2383c3 921 WARN_ON(num_pages > nrptrs);
9aead435 922 memset(pages, 0, sizeof(struct page *) * nrptrs);
1832a6d5 923
6a63209f 924 ret = btrfs_check_data_free_space(root, inode, write_bytes);
1832a6d5
CM
925 if (ret)
926 goto out;
927
39279cc3
CM
928 ret = prepare_pages(root, file, pages, num_pages,
929 pos, first_index, last_index,
8c2383c3 930 write_bytes);
6a63209f
JB
931 if (ret) {
932 btrfs_free_reserved_data_space(root, inode,
933 write_bytes);
54aa1f4d 934 goto out;
6a63209f 935 }
39279cc3 936
39279cc3
CM
937 ret = btrfs_copy_from_user(pos, num_pages,
938 write_bytes, pages, buf);
54aa1f4d 939 if (ret) {
6a63209f
JB
940 btrfs_free_reserved_data_space(root, inode,
941 write_bytes);
54aa1f4d
CM
942 btrfs_drop_pages(pages, num_pages);
943 goto out;
944 }
39279cc3
CM
945
946 ret = dirty_and_release_pages(NULL, root, file, pages,
947 num_pages, pos, write_bytes);
39279cc3 948 btrfs_drop_pages(pages, num_pages);
6a63209f
JB
949 if (ret) {
950 btrfs_free_reserved_data_space(root, inode,
951 write_bytes);
54aa1f4d 952 goto out;
6a63209f 953 }
39279cc3 954
cb843a6f 955 if (will_write) {
8aa38c31
CH
956 filemap_fdatawrite_range(inode->i_mapping, pos,
957 pos + write_bytes - 1);
cb843a6f
CM
958 } else {
959 balance_dirty_pages_ratelimited_nr(inode->i_mapping,
960 num_pages);
961 if (num_pages <
962 (root->leafsize >> PAGE_CACHE_SHIFT) + 1)
963 btrfs_btree_balance_dirty(root, 1);
964 btrfs_throttle(root);
965 }
966
39279cc3
CM
967 buf += write_bytes;
968 count -= write_bytes;
969 pos += write_bytes;
970 num_written += write_bytes;
971
39279cc3
CM
972 cond_resched();
973 }
39279cc3 974out:
1832a6d5 975 mutex_unlock(&inode->i_mutex);
6a63209f
JB
976 if (ret)
977 err = ret;
9ed74f2d 978 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
5b92ee72 979
1832a6d5 980out_nolock:
8c2383c3 981 kfree(pages);
39279cc3
CM
982 if (pinned[0])
983 page_cache_release(pinned[0]);
984 if (pinned[1])
985 page_cache_release(pinned[1]);
986 *ppos = pos;
2ff3e9b6 987
5a3f23d5
CM
988 /*
989 * we want to make sure fsync finds this change
990 * but we haven't joined a transaction running right now.
991 *
992 * Later on, someone is sure to update the inode and get the
993 * real transid recorded.
994 *
995 * We set last_trans now to the fs_info generation + 1,
996 * this will either be one more than the running transaction
997 * or the generation used for the next transaction if there isn't
998 * one running right now.
999 */
1000 BTRFS_I(inode)->last_trans = root->fs_info->generation + 1;
1001
cb843a6f 1002 if (num_written > 0 && will_write) {
e02119d5
CM
1003 struct btrfs_trans_handle *trans;
1004
cb843a6f
CM
1005 err = btrfs_wait_ordered_range(inode, start_pos, num_written);
1006 if (err)
2ff3e9b6 1007 num_written = err;
e02119d5 1008
6b2f3d1f 1009 if ((file->f_flags & O_DSYNC) || IS_SYNC(inode)) {
cb843a6f
CM
1010 trans = btrfs_start_transaction(root, 1);
1011 ret = btrfs_log_dentry_safe(trans, root,
1012 file->f_dentry);
1013 if (ret == 0) {
12fcfd22
CM
1014 ret = btrfs_sync_log(trans, root);
1015 if (ret == 0)
1016 btrfs_end_transaction(trans, root);
1017 else
1018 btrfs_commit_transaction(trans, root);
257c62e1 1019 } else if (ret != BTRFS_NO_LOG_SYNC) {
cb843a6f 1020 btrfs_commit_transaction(trans, root);
257c62e1
CM
1021 } else {
1022 btrfs_end_transaction(trans, root);
cb843a6f
CM
1023 }
1024 }
1025 if (file->f_flags & O_DIRECT) {
1026 invalidate_mapping_pages(inode->i_mapping,
1027 start_pos >> PAGE_CACHE_SHIFT,
1028 (start_pos + num_written - 1) >> PAGE_CACHE_SHIFT);
e02119d5 1029 }
2ff3e9b6 1030 }
39279cc3 1031 current->backing_dev_info = NULL;
39279cc3
CM
1032 return num_written ? num_written : err;
1033}
1034
d397712b 1035int btrfs_release_file(struct inode *inode, struct file *filp)
e1b81e67 1036{
5a3f23d5
CM
1037 /*
1038 * ordered_data_close is set by settattr when we are about to truncate
1039 * a file from a non-zero size to a zero size. This tries to
1040 * flush down new bytes that may have been written if the
1041 * application were using truncate to replace a file in place.
1042 */
1043 if (BTRFS_I(inode)->ordered_data_close) {
1044 BTRFS_I(inode)->ordered_data_close = 0;
1045 btrfs_add_ordered_operation(NULL, BTRFS_I(inode)->root, inode);
1046 if (inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
1047 filemap_flush(inode->i_mapping);
1048 }
6bf13c0c
SW
1049 if (filp->private_data)
1050 btrfs_ioctl_trans_end(filp);
e1b81e67
M
1051 return 0;
1052}
1053
d352ac68
CM
1054/*
1055 * fsync call for both files and directories. This logs the inode into
1056 * the tree log instead of forcing full commits whenever possible.
1057 *
1058 * It needs to call filemap_fdatawait so that all ordered extent updates are
1059 * in the metadata btree are up to date for copying to the log.
1060 *
1061 * It drops the inode mutex before doing the tree log commit. This is an
1062 * important optimization for directories because holding the mutex prevents
1063 * new operations on the dir while we write to disk.
1064 */
e02119d5 1065int btrfs_sync_file(struct file *file, struct dentry *dentry, int datasync)
39279cc3
CM
1066{
1067 struct inode *inode = dentry->d_inode;
1068 struct btrfs_root *root = BTRFS_I(inode)->root;
15ee9bc7 1069 int ret = 0;
39279cc3
CM
1070 struct btrfs_trans_handle *trans;
1071
257c62e1
CM
1072
1073 /* we wait first, since the writeback may change the inode */
1074 root->log_batch++;
1075 /* the VFS called filemap_fdatawrite for us */
1076 btrfs_wait_ordered_range(inode, 0, (u64)-1);
1077 root->log_batch++;
1078
39279cc3 1079 /*
15ee9bc7
JB
1080 * check the transaction that last modified this inode
1081 * and see if its already been committed
39279cc3 1082 */
15ee9bc7
JB
1083 if (!BTRFS_I(inode)->last_trans)
1084 goto out;
a2135011 1085
257c62e1
CM
1086 /*
1087 * if the last transaction that changed this file was before
1088 * the current transaction, we can bail out now without any
1089 * syncing
1090 */
15ee9bc7
JB
1091 mutex_lock(&root->fs_info->trans_mutex);
1092 if (BTRFS_I(inode)->last_trans <=
1093 root->fs_info->last_trans_committed) {
1094 BTRFS_I(inode)->last_trans = 0;
1095 mutex_unlock(&root->fs_info->trans_mutex);
1096 goto out;
1097 }
1098 mutex_unlock(&root->fs_info->trans_mutex);
1099
1100 /*
a52d9a80
CM
1101 * ok we haven't committed the transaction yet, lets do a commit
1102 */
2cfbd50b 1103 if (file && file->private_data)
6bf13c0c
SW
1104 btrfs_ioctl_trans_end(file);
1105
39279cc3
CM
1106 trans = btrfs_start_transaction(root, 1);
1107 if (!trans) {
1108 ret = -ENOMEM;
1109 goto out;
1110 }
e02119d5 1111
2cfbd50b 1112 ret = btrfs_log_dentry_safe(trans, root, dentry);
d397712b 1113 if (ret < 0)
e02119d5 1114 goto out;
49eb7e46
CM
1115
1116 /* we've logged all the items and now have a consistent
1117 * version of the file in the log. It is possible that
1118 * someone will come in and modify the file, but that's
1119 * fine because the log is consistent on disk, and we
1120 * have references to all of the file's extents
1121 *
1122 * It is possible that someone will come in and log the
1123 * file again, but that will end up using the synchronization
1124 * inside btrfs_sync_log to keep things safe.
1125 */
2cfbd50b 1126 mutex_unlock(&dentry->d_inode->i_mutex);
49eb7e46 1127
257c62e1
CM
1128 if (ret != BTRFS_NO_LOG_SYNC) {
1129 if (ret > 0) {
12fcfd22 1130 ret = btrfs_commit_transaction(trans, root);
257c62e1
CM
1131 } else {
1132 ret = btrfs_sync_log(trans, root);
1133 if (ret == 0)
1134 ret = btrfs_end_transaction(trans, root);
1135 else
1136 ret = btrfs_commit_transaction(trans, root);
1137 }
1138 } else {
1139 ret = btrfs_end_transaction(trans, root);
e02119d5 1140 }
2cfbd50b 1141 mutex_lock(&dentry->d_inode->i_mutex);
39279cc3 1142out:
014e4ac4 1143 return ret > 0 ? -EIO : ret;
39279cc3
CM
1144}
1145
f0f37e2f 1146static const struct vm_operations_struct btrfs_file_vm_ops = {
92fee66d 1147 .fault = filemap_fault,
9ebefb18
CM
1148 .page_mkwrite = btrfs_page_mkwrite,
1149};
1150
1151static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
1152{
1153 vma->vm_ops = &btrfs_file_vm_ops;
1154 file_accessed(filp);
1155 return 0;
1156}
1157
828c0950 1158const struct file_operations btrfs_file_operations = {
39279cc3
CM
1159 .llseek = generic_file_llseek,
1160 .read = do_sync_read,
9ebefb18 1161 .aio_read = generic_file_aio_read,
e9906a98 1162 .splice_read = generic_file_splice_read,
39279cc3 1163 .write = btrfs_file_write,
9ebefb18 1164 .mmap = btrfs_file_mmap,
39279cc3 1165 .open = generic_file_open,
e1b81e67 1166 .release = btrfs_release_file,
39279cc3 1167 .fsync = btrfs_sync_file,
34287aa3 1168 .unlocked_ioctl = btrfs_ioctl,
39279cc3 1169#ifdef CONFIG_COMPAT
34287aa3 1170 .compat_ioctl = btrfs_ioctl,
39279cc3
CM
1171#endif
1172};