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