Btrfs: fix heavy delalloc related deadlock
[deliverable/linux.git] / fs / btrfs / inode.c
1 /*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
19 #include <linux/kernel.h>
20 #include <linux/bio.h>
21 #include <linux/buffer_head.h>
22 #include <linux/file.h>
23 #include <linux/fs.h>
24 #include <linux/pagemap.h>
25 #include <linux/highmem.h>
26 #include <linux/time.h>
27 #include <linux/init.h>
28 #include <linux/string.h>
29 #include <linux/backing-dev.h>
30 #include <linux/mpage.h>
31 #include <linux/swap.h>
32 #include <linux/writeback.h>
33 #include <linux/statfs.h>
34 #include <linux/compat.h>
35 #include <linux/aio.h>
36 #include <linux/bit_spinlock.h>
37 #include <linux/xattr.h>
38 #include <linux/posix_acl.h>
39 #include <linux/falloc.h>
40 #include <linux/slab.h>
41 #include <linux/ratelimit.h>
42 #include <linux/mount.h>
43 #include <linux/btrfs.h>
44 #include <linux/blkdev.h>
45 #include <linux/posix_acl_xattr.h>
46 #include "compat.h"
47 #include "ctree.h"
48 #include "disk-io.h"
49 #include "transaction.h"
50 #include "btrfs_inode.h"
51 #include "print-tree.h"
52 #include "ordered-data.h"
53 #include "xattr.h"
54 #include "tree-log.h"
55 #include "volumes.h"
56 #include "compression.h"
57 #include "locking.h"
58 #include "free-space-cache.h"
59 #include "inode-map.h"
60 #include "backref.h"
61 #include "hash.h"
62
63 struct btrfs_iget_args {
64 u64 ino;
65 struct btrfs_root *root;
66 };
67
68 static const struct inode_operations btrfs_dir_inode_operations;
69 static const struct inode_operations btrfs_symlink_inode_operations;
70 static const struct inode_operations btrfs_dir_ro_inode_operations;
71 static const struct inode_operations btrfs_special_inode_operations;
72 static const struct inode_operations btrfs_file_inode_operations;
73 static const struct address_space_operations btrfs_aops;
74 static const struct address_space_operations btrfs_symlink_aops;
75 static const struct file_operations btrfs_dir_file_operations;
76 static struct extent_io_ops btrfs_extent_io_ops;
77
78 static struct kmem_cache *btrfs_inode_cachep;
79 static struct kmem_cache *btrfs_delalloc_work_cachep;
80 struct kmem_cache *btrfs_trans_handle_cachep;
81 struct kmem_cache *btrfs_transaction_cachep;
82 struct kmem_cache *btrfs_path_cachep;
83 struct kmem_cache *btrfs_free_space_cachep;
84
85 #define S_SHIFT 12
86 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
87 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
88 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
89 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
90 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
91 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
92 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
93 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
94 };
95
96 static int btrfs_setsize(struct inode *inode, struct iattr *attr);
97 static int btrfs_truncate(struct inode *inode);
98 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
99 static noinline int cow_file_range(struct inode *inode,
100 struct page *locked_page,
101 u64 start, u64 end, int *page_started,
102 unsigned long *nr_written, int unlock);
103 static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
104 u64 len, u64 orig_start,
105 u64 block_start, u64 block_len,
106 u64 orig_block_len, u64 ram_bytes,
107 int type);
108
109 static int btrfs_dirty_inode(struct inode *inode);
110
111 static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
112 struct inode *inode, struct inode *dir,
113 const struct qstr *qstr)
114 {
115 int err;
116
117 err = btrfs_init_acl(trans, inode, dir);
118 if (!err)
119 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
120 return err;
121 }
122
123 /*
124 * this does all the hard work for inserting an inline extent into
125 * the btree. The caller should have done a btrfs_drop_extents so that
126 * no overlapping inline items exist in the btree
127 */
128 static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
129 struct btrfs_root *root, struct inode *inode,
130 u64 start, size_t size, size_t compressed_size,
131 int compress_type,
132 struct page **compressed_pages)
133 {
134 struct btrfs_key key;
135 struct btrfs_path *path;
136 struct extent_buffer *leaf;
137 struct page *page = NULL;
138 char *kaddr;
139 unsigned long ptr;
140 struct btrfs_file_extent_item *ei;
141 int err = 0;
142 int ret;
143 size_t cur_size = size;
144 size_t datasize;
145 unsigned long offset;
146
147 if (compressed_size && compressed_pages)
148 cur_size = compressed_size;
149
150 path = btrfs_alloc_path();
151 if (!path)
152 return -ENOMEM;
153
154 path->leave_spinning = 1;
155
156 key.objectid = btrfs_ino(inode);
157 key.offset = start;
158 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
159 datasize = btrfs_file_extent_calc_inline_size(cur_size);
160
161 inode_add_bytes(inode, size);
162 ret = btrfs_insert_empty_item(trans, root, path, &key,
163 datasize);
164 if (ret) {
165 err = ret;
166 goto fail;
167 }
168 leaf = path->nodes[0];
169 ei = btrfs_item_ptr(leaf, path->slots[0],
170 struct btrfs_file_extent_item);
171 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
172 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
173 btrfs_set_file_extent_encryption(leaf, ei, 0);
174 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
175 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
176 ptr = btrfs_file_extent_inline_start(ei);
177
178 if (compress_type != BTRFS_COMPRESS_NONE) {
179 struct page *cpage;
180 int i = 0;
181 while (compressed_size > 0) {
182 cpage = compressed_pages[i];
183 cur_size = min_t(unsigned long, compressed_size,
184 PAGE_CACHE_SIZE);
185
186 kaddr = kmap_atomic(cpage);
187 write_extent_buffer(leaf, kaddr, ptr, cur_size);
188 kunmap_atomic(kaddr);
189
190 i++;
191 ptr += cur_size;
192 compressed_size -= cur_size;
193 }
194 btrfs_set_file_extent_compression(leaf, ei,
195 compress_type);
196 } else {
197 page = find_get_page(inode->i_mapping,
198 start >> PAGE_CACHE_SHIFT);
199 btrfs_set_file_extent_compression(leaf, ei, 0);
200 kaddr = kmap_atomic(page);
201 offset = start & (PAGE_CACHE_SIZE - 1);
202 write_extent_buffer(leaf, kaddr + offset, ptr, size);
203 kunmap_atomic(kaddr);
204 page_cache_release(page);
205 }
206 btrfs_mark_buffer_dirty(leaf);
207 btrfs_free_path(path);
208
209 /*
210 * we're an inline extent, so nobody can
211 * extend the file past i_size without locking
212 * a page we already have locked.
213 *
214 * We must do any isize and inode updates
215 * before we unlock the pages. Otherwise we
216 * could end up racing with unlink.
217 */
218 BTRFS_I(inode)->disk_i_size = inode->i_size;
219 ret = btrfs_update_inode(trans, root, inode);
220
221 return ret;
222 fail:
223 btrfs_free_path(path);
224 return err;
225 }
226
227
228 /*
229 * conditionally insert an inline extent into the file. This
230 * does the checks required to make sure the data is small enough
231 * to fit as an inline extent.
232 */
233 static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
234 struct btrfs_root *root,
235 struct inode *inode, u64 start, u64 end,
236 size_t compressed_size, int compress_type,
237 struct page **compressed_pages)
238 {
239 u64 isize = i_size_read(inode);
240 u64 actual_end = min(end + 1, isize);
241 u64 inline_len = actual_end - start;
242 u64 aligned_end = ALIGN(end, root->sectorsize);
243 u64 data_len = inline_len;
244 int ret;
245
246 if (compressed_size)
247 data_len = compressed_size;
248
249 if (start > 0 ||
250 actual_end >= PAGE_CACHE_SIZE ||
251 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
252 (!compressed_size &&
253 (actual_end & (root->sectorsize - 1)) == 0) ||
254 end + 1 < isize ||
255 data_len > root->fs_info->max_inline) {
256 return 1;
257 }
258
259 ret = btrfs_drop_extents(trans, root, inode, start, aligned_end, 1);
260 if (ret)
261 return ret;
262
263 if (isize > actual_end)
264 inline_len = min_t(u64, isize, actual_end);
265 ret = insert_inline_extent(trans, root, inode, start,
266 inline_len, compressed_size,
267 compress_type, compressed_pages);
268 if (ret && ret != -ENOSPC) {
269 btrfs_abort_transaction(trans, root, ret);
270 return ret;
271 } else if (ret == -ENOSPC) {
272 return 1;
273 }
274
275 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
276 btrfs_delalloc_release_metadata(inode, end + 1 - start);
277 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
278 return 0;
279 }
280
281 struct async_extent {
282 u64 start;
283 u64 ram_size;
284 u64 compressed_size;
285 struct page **pages;
286 unsigned long nr_pages;
287 int compress_type;
288 struct list_head list;
289 };
290
291 struct async_cow {
292 struct inode *inode;
293 struct btrfs_root *root;
294 struct page *locked_page;
295 u64 start;
296 u64 end;
297 struct list_head extents;
298 struct btrfs_work work;
299 };
300
301 static noinline int add_async_extent(struct async_cow *cow,
302 u64 start, u64 ram_size,
303 u64 compressed_size,
304 struct page **pages,
305 unsigned long nr_pages,
306 int compress_type)
307 {
308 struct async_extent *async_extent;
309
310 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
311 BUG_ON(!async_extent); /* -ENOMEM */
312 async_extent->start = start;
313 async_extent->ram_size = ram_size;
314 async_extent->compressed_size = compressed_size;
315 async_extent->pages = pages;
316 async_extent->nr_pages = nr_pages;
317 async_extent->compress_type = compress_type;
318 list_add_tail(&async_extent->list, &cow->extents);
319 return 0;
320 }
321
322 /*
323 * we create compressed extents in two phases. The first
324 * phase compresses a range of pages that have already been
325 * locked (both pages and state bits are locked).
326 *
327 * This is done inside an ordered work queue, and the compression
328 * is spread across many cpus. The actual IO submission is step
329 * two, and the ordered work queue takes care of making sure that
330 * happens in the same order things were put onto the queue by
331 * writepages and friends.
332 *
333 * If this code finds it can't get good compression, it puts an
334 * entry onto the work queue to write the uncompressed bytes. This
335 * makes sure that both compressed inodes and uncompressed inodes
336 * are written in the same order that the flusher thread sent them
337 * down.
338 */
339 static noinline int compress_file_range(struct inode *inode,
340 struct page *locked_page,
341 u64 start, u64 end,
342 struct async_cow *async_cow,
343 int *num_added)
344 {
345 struct btrfs_root *root = BTRFS_I(inode)->root;
346 struct btrfs_trans_handle *trans;
347 u64 num_bytes;
348 u64 blocksize = root->sectorsize;
349 u64 actual_end;
350 u64 isize = i_size_read(inode);
351 int ret = 0;
352 struct page **pages = NULL;
353 unsigned long nr_pages;
354 unsigned long nr_pages_ret = 0;
355 unsigned long total_compressed = 0;
356 unsigned long total_in = 0;
357 unsigned long max_compressed = 128 * 1024;
358 unsigned long max_uncompressed = 128 * 1024;
359 int i;
360 int will_compress;
361 int compress_type = root->fs_info->compress_type;
362 int redirty = 0;
363
364 /* if this is a small write inside eof, kick off a defrag */
365 if ((end - start + 1) < 16 * 1024 &&
366 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
367 btrfs_add_inode_defrag(NULL, inode);
368
369 actual_end = min_t(u64, isize, end + 1);
370 again:
371 will_compress = 0;
372 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
373 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
374
375 /*
376 * we don't want to send crud past the end of i_size through
377 * compression, that's just a waste of CPU time. So, if the
378 * end of the file is before the start of our current
379 * requested range of bytes, we bail out to the uncompressed
380 * cleanup code that can deal with all of this.
381 *
382 * It isn't really the fastest way to fix things, but this is a
383 * very uncommon corner.
384 */
385 if (actual_end <= start)
386 goto cleanup_and_bail_uncompressed;
387
388 total_compressed = actual_end - start;
389
390 /* we want to make sure that amount of ram required to uncompress
391 * an extent is reasonable, so we limit the total size in ram
392 * of a compressed extent to 128k. This is a crucial number
393 * because it also controls how easily we can spread reads across
394 * cpus for decompression.
395 *
396 * We also want to make sure the amount of IO required to do
397 * a random read is reasonably small, so we limit the size of
398 * a compressed extent to 128k.
399 */
400 total_compressed = min(total_compressed, max_uncompressed);
401 num_bytes = ALIGN(end - start + 1, blocksize);
402 num_bytes = max(blocksize, num_bytes);
403 total_in = 0;
404 ret = 0;
405
406 /*
407 * we do compression for mount -o compress and when the
408 * inode has not been flagged as nocompress. This flag can
409 * change at any time if we discover bad compression ratios.
410 */
411 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
412 (btrfs_test_opt(root, COMPRESS) ||
413 (BTRFS_I(inode)->force_compress) ||
414 (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) {
415 WARN_ON(pages);
416 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
417 if (!pages) {
418 /* just bail out to the uncompressed code */
419 goto cont;
420 }
421
422 if (BTRFS_I(inode)->force_compress)
423 compress_type = BTRFS_I(inode)->force_compress;
424
425 /*
426 * we need to call clear_page_dirty_for_io on each
427 * page in the range. Otherwise applications with the file
428 * mmap'd can wander in and change the page contents while
429 * we are compressing them.
430 *
431 * If the compression fails for any reason, we set the pages
432 * dirty again later on.
433 */
434 extent_range_clear_dirty_for_io(inode, start, end);
435 redirty = 1;
436 ret = btrfs_compress_pages(compress_type,
437 inode->i_mapping, start,
438 total_compressed, pages,
439 nr_pages, &nr_pages_ret,
440 &total_in,
441 &total_compressed,
442 max_compressed);
443
444 if (!ret) {
445 unsigned long offset = total_compressed &
446 (PAGE_CACHE_SIZE - 1);
447 struct page *page = pages[nr_pages_ret - 1];
448 char *kaddr;
449
450 /* zero the tail end of the last page, we might be
451 * sending it down to disk
452 */
453 if (offset) {
454 kaddr = kmap_atomic(page);
455 memset(kaddr + offset, 0,
456 PAGE_CACHE_SIZE - offset);
457 kunmap_atomic(kaddr);
458 }
459 will_compress = 1;
460 }
461 }
462 cont:
463 if (start == 0) {
464 trans = btrfs_join_transaction(root);
465 if (IS_ERR(trans)) {
466 ret = PTR_ERR(trans);
467 trans = NULL;
468 goto cleanup_and_out;
469 }
470 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
471
472 /* lets try to make an inline extent */
473 if (ret || total_in < (actual_end - start)) {
474 /* we didn't compress the entire range, try
475 * to make an uncompressed inline extent.
476 */
477 ret = cow_file_range_inline(trans, root, inode,
478 start, end, 0, 0, NULL);
479 } else {
480 /* try making a compressed inline extent */
481 ret = cow_file_range_inline(trans, root, inode,
482 start, end,
483 total_compressed,
484 compress_type, pages);
485 }
486 if (ret <= 0) {
487 unsigned long clear_flags = EXTENT_DELALLOC |
488 EXTENT_DEFRAG;
489 clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
490
491 /*
492 * inline extent creation worked or returned error,
493 * we don't need to create any more async work items.
494 * Unlock and free up our temp pages.
495 */
496 extent_clear_unlock_delalloc(inode, start, end, NULL,
497 clear_flags, PAGE_UNLOCK |
498 PAGE_CLEAR_DIRTY |
499 PAGE_SET_WRITEBACK |
500 PAGE_END_WRITEBACK);
501 btrfs_end_transaction(trans, root);
502 goto free_pages_out;
503 }
504 btrfs_end_transaction(trans, root);
505 }
506
507 if (will_compress) {
508 /*
509 * we aren't doing an inline extent round the compressed size
510 * up to a block size boundary so the allocator does sane
511 * things
512 */
513 total_compressed = ALIGN(total_compressed, blocksize);
514
515 /*
516 * one last check to make sure the compression is really a
517 * win, compare the page count read with the blocks on disk
518 */
519 total_in = ALIGN(total_in, PAGE_CACHE_SIZE);
520 if (total_compressed >= total_in) {
521 will_compress = 0;
522 } else {
523 num_bytes = total_in;
524 }
525 }
526 if (!will_compress && pages) {
527 /*
528 * the compression code ran but failed to make things smaller,
529 * free any pages it allocated and our page pointer array
530 */
531 for (i = 0; i < nr_pages_ret; i++) {
532 WARN_ON(pages[i]->mapping);
533 page_cache_release(pages[i]);
534 }
535 kfree(pages);
536 pages = NULL;
537 total_compressed = 0;
538 nr_pages_ret = 0;
539
540 /* flag the file so we don't compress in the future */
541 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
542 !(BTRFS_I(inode)->force_compress)) {
543 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
544 }
545 }
546 if (will_compress) {
547 *num_added += 1;
548
549 /* the async work queues will take care of doing actual
550 * allocation on disk for these compressed pages,
551 * and will submit them to the elevator.
552 */
553 add_async_extent(async_cow, start, num_bytes,
554 total_compressed, pages, nr_pages_ret,
555 compress_type);
556
557 if (start + num_bytes < end) {
558 start += num_bytes;
559 pages = NULL;
560 cond_resched();
561 goto again;
562 }
563 } else {
564 cleanup_and_bail_uncompressed:
565 /*
566 * No compression, but we still need to write the pages in
567 * the file we've been given so far. redirty the locked
568 * page if it corresponds to our extent and set things up
569 * for the async work queue to run cow_file_range to do
570 * the normal delalloc dance
571 */
572 if (page_offset(locked_page) >= start &&
573 page_offset(locked_page) <= end) {
574 __set_page_dirty_nobuffers(locked_page);
575 /* unlocked later on in the async handlers */
576 }
577 if (redirty)
578 extent_range_redirty_for_io(inode, start, end);
579 add_async_extent(async_cow, start, end - start + 1,
580 0, NULL, 0, BTRFS_COMPRESS_NONE);
581 *num_added += 1;
582 }
583
584 out:
585 return ret;
586
587 free_pages_out:
588 for (i = 0; i < nr_pages_ret; i++) {
589 WARN_ON(pages[i]->mapping);
590 page_cache_release(pages[i]);
591 }
592 kfree(pages);
593
594 goto out;
595
596 cleanup_and_out:
597 extent_clear_unlock_delalloc(inode, start, end, NULL,
598 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
599 EXTENT_DEFRAG, PAGE_UNLOCK |
600 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
601 PAGE_END_WRITEBACK);
602 if (!trans || IS_ERR(trans))
603 btrfs_error(root->fs_info, ret, "Failed to join transaction");
604 else
605 btrfs_abort_transaction(trans, root, ret);
606 goto free_pages_out;
607 }
608
609 /*
610 * phase two of compressed writeback. This is the ordered portion
611 * of the code, which only gets called in the order the work was
612 * queued. We walk all the async extents created by compress_file_range
613 * and send them down to the disk.
614 */
615 static noinline int submit_compressed_extents(struct inode *inode,
616 struct async_cow *async_cow)
617 {
618 struct async_extent *async_extent;
619 u64 alloc_hint = 0;
620 struct btrfs_trans_handle *trans;
621 struct btrfs_key ins;
622 struct extent_map *em;
623 struct btrfs_root *root = BTRFS_I(inode)->root;
624 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
625 struct extent_io_tree *io_tree;
626 int ret = 0;
627
628 if (list_empty(&async_cow->extents))
629 return 0;
630
631 again:
632 while (!list_empty(&async_cow->extents)) {
633 async_extent = list_entry(async_cow->extents.next,
634 struct async_extent, list);
635 list_del(&async_extent->list);
636
637 io_tree = &BTRFS_I(inode)->io_tree;
638
639 retry:
640 /* did the compression code fall back to uncompressed IO? */
641 if (!async_extent->pages) {
642 int page_started = 0;
643 unsigned long nr_written = 0;
644
645 lock_extent(io_tree, async_extent->start,
646 async_extent->start +
647 async_extent->ram_size - 1);
648
649 /* allocate blocks */
650 ret = cow_file_range(inode, async_cow->locked_page,
651 async_extent->start,
652 async_extent->start +
653 async_extent->ram_size - 1,
654 &page_started, &nr_written, 0);
655
656 /* JDM XXX */
657
658 /*
659 * if page_started, cow_file_range inserted an
660 * inline extent and took care of all the unlocking
661 * and IO for us. Otherwise, we need to submit
662 * all those pages down to the drive.
663 */
664 if (!page_started && !ret)
665 extent_write_locked_range(io_tree,
666 inode, async_extent->start,
667 async_extent->start +
668 async_extent->ram_size - 1,
669 btrfs_get_extent,
670 WB_SYNC_ALL);
671 else if (ret)
672 unlock_page(async_cow->locked_page);
673 kfree(async_extent);
674 cond_resched();
675 continue;
676 }
677
678 lock_extent(io_tree, async_extent->start,
679 async_extent->start + async_extent->ram_size - 1);
680
681 trans = btrfs_join_transaction(root);
682 if (IS_ERR(trans)) {
683 ret = PTR_ERR(trans);
684 } else {
685 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
686 ret = btrfs_reserve_extent(trans, root,
687 async_extent->compressed_size,
688 async_extent->compressed_size,
689 0, alloc_hint, &ins, 1);
690 if (ret && ret != -ENOSPC)
691 btrfs_abort_transaction(trans, root, ret);
692 btrfs_end_transaction(trans, root);
693 }
694
695 if (ret) {
696 int i;
697
698 for (i = 0; i < async_extent->nr_pages; i++) {
699 WARN_ON(async_extent->pages[i]->mapping);
700 page_cache_release(async_extent->pages[i]);
701 }
702 kfree(async_extent->pages);
703 async_extent->nr_pages = 0;
704 async_extent->pages = NULL;
705
706 if (ret == -ENOSPC) {
707 unlock_extent(io_tree, async_extent->start,
708 async_extent->start +
709 async_extent->ram_size - 1);
710 goto retry;
711 }
712 goto out_free;
713 }
714
715 /*
716 * here we're doing allocation and writeback of the
717 * compressed pages
718 */
719 btrfs_drop_extent_cache(inode, async_extent->start,
720 async_extent->start +
721 async_extent->ram_size - 1, 0);
722
723 em = alloc_extent_map();
724 if (!em) {
725 ret = -ENOMEM;
726 goto out_free_reserve;
727 }
728 em->start = async_extent->start;
729 em->len = async_extent->ram_size;
730 em->orig_start = em->start;
731 em->mod_start = em->start;
732 em->mod_len = em->len;
733
734 em->block_start = ins.objectid;
735 em->block_len = ins.offset;
736 em->orig_block_len = ins.offset;
737 em->ram_bytes = async_extent->ram_size;
738 em->bdev = root->fs_info->fs_devices->latest_bdev;
739 em->compress_type = async_extent->compress_type;
740 set_bit(EXTENT_FLAG_PINNED, &em->flags);
741 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
742 em->generation = -1;
743
744 while (1) {
745 write_lock(&em_tree->lock);
746 ret = add_extent_mapping(em_tree, em, 1);
747 write_unlock(&em_tree->lock);
748 if (ret != -EEXIST) {
749 free_extent_map(em);
750 break;
751 }
752 btrfs_drop_extent_cache(inode, async_extent->start,
753 async_extent->start +
754 async_extent->ram_size - 1, 0);
755 }
756
757 if (ret)
758 goto out_free_reserve;
759
760 ret = btrfs_add_ordered_extent_compress(inode,
761 async_extent->start,
762 ins.objectid,
763 async_extent->ram_size,
764 ins.offset,
765 BTRFS_ORDERED_COMPRESSED,
766 async_extent->compress_type);
767 if (ret)
768 goto out_free_reserve;
769
770 /*
771 * clear dirty, set writeback and unlock the pages.
772 */
773 extent_clear_unlock_delalloc(inode, async_extent->start,
774 async_extent->start +
775 async_extent->ram_size - 1,
776 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
777 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
778 PAGE_SET_WRITEBACK);
779 ret = btrfs_submit_compressed_write(inode,
780 async_extent->start,
781 async_extent->ram_size,
782 ins.objectid,
783 ins.offset, async_extent->pages,
784 async_extent->nr_pages);
785 alloc_hint = ins.objectid + ins.offset;
786 kfree(async_extent);
787 if (ret)
788 goto out;
789 cond_resched();
790 }
791 ret = 0;
792 out:
793 return ret;
794 out_free_reserve:
795 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
796 out_free:
797 extent_clear_unlock_delalloc(inode, async_extent->start,
798 async_extent->start +
799 async_extent->ram_size - 1,
800 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
801 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
802 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
803 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
804 kfree(async_extent);
805 goto again;
806 }
807
808 static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
809 u64 num_bytes)
810 {
811 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
812 struct extent_map *em;
813 u64 alloc_hint = 0;
814
815 read_lock(&em_tree->lock);
816 em = search_extent_mapping(em_tree, start, num_bytes);
817 if (em) {
818 /*
819 * if block start isn't an actual block number then find the
820 * first block in this inode and use that as a hint. If that
821 * block is also bogus then just don't worry about it.
822 */
823 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
824 free_extent_map(em);
825 em = search_extent_mapping(em_tree, 0, 0);
826 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
827 alloc_hint = em->block_start;
828 if (em)
829 free_extent_map(em);
830 } else {
831 alloc_hint = em->block_start;
832 free_extent_map(em);
833 }
834 }
835 read_unlock(&em_tree->lock);
836
837 return alloc_hint;
838 }
839
840 /*
841 * when extent_io.c finds a delayed allocation range in the file,
842 * the call backs end up in this code. The basic idea is to
843 * allocate extents on disk for the range, and create ordered data structs
844 * in ram to track those extents.
845 *
846 * locked_page is the page that writepage had locked already. We use
847 * it to make sure we don't do extra locks or unlocks.
848 *
849 * *page_started is set to one if we unlock locked_page and do everything
850 * required to start IO on it. It may be clean and already done with
851 * IO when we return.
852 */
853 static noinline int __cow_file_range(struct btrfs_trans_handle *trans,
854 struct inode *inode,
855 struct btrfs_root *root,
856 struct page *locked_page,
857 u64 start, u64 end, int *page_started,
858 unsigned long *nr_written,
859 int unlock)
860 {
861 u64 alloc_hint = 0;
862 u64 num_bytes;
863 unsigned long ram_size;
864 u64 disk_num_bytes;
865 u64 cur_alloc_size;
866 u64 blocksize = root->sectorsize;
867 struct btrfs_key ins;
868 struct extent_map *em;
869 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
870 int ret = 0;
871
872 BUG_ON(btrfs_is_free_space_inode(inode));
873
874 num_bytes = ALIGN(end - start + 1, blocksize);
875 num_bytes = max(blocksize, num_bytes);
876 disk_num_bytes = num_bytes;
877
878 /* if this is a small write inside eof, kick off defrag */
879 if (num_bytes < 64 * 1024 &&
880 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
881 btrfs_add_inode_defrag(trans, inode);
882
883 if (start == 0) {
884 /* lets try to make an inline extent */
885 ret = cow_file_range_inline(trans, root, inode,
886 start, end, 0, 0, NULL);
887 if (ret == 0) {
888 extent_clear_unlock_delalloc(inode, start, end, NULL,
889 EXTENT_LOCKED | EXTENT_DELALLOC |
890 EXTENT_DEFRAG, PAGE_UNLOCK |
891 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
892 PAGE_END_WRITEBACK);
893
894 *nr_written = *nr_written +
895 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
896 *page_started = 1;
897 goto out;
898 } else if (ret < 0) {
899 btrfs_abort_transaction(trans, root, ret);
900 goto out_unlock;
901 }
902 }
903
904 BUG_ON(disk_num_bytes >
905 btrfs_super_total_bytes(root->fs_info->super_copy));
906
907 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
908 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
909
910 while (disk_num_bytes > 0) {
911 unsigned long op;
912
913 cur_alloc_size = disk_num_bytes;
914 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
915 root->sectorsize, 0, alloc_hint,
916 &ins, 1);
917 if (ret < 0) {
918 btrfs_abort_transaction(trans, root, ret);
919 goto out_unlock;
920 }
921
922 em = alloc_extent_map();
923 if (!em) {
924 ret = -ENOMEM;
925 goto out_reserve;
926 }
927 em->start = start;
928 em->orig_start = em->start;
929 ram_size = ins.offset;
930 em->len = ins.offset;
931 em->mod_start = em->start;
932 em->mod_len = em->len;
933
934 em->block_start = ins.objectid;
935 em->block_len = ins.offset;
936 em->orig_block_len = ins.offset;
937 em->ram_bytes = ram_size;
938 em->bdev = root->fs_info->fs_devices->latest_bdev;
939 set_bit(EXTENT_FLAG_PINNED, &em->flags);
940 em->generation = -1;
941
942 while (1) {
943 write_lock(&em_tree->lock);
944 ret = add_extent_mapping(em_tree, em, 1);
945 write_unlock(&em_tree->lock);
946 if (ret != -EEXIST) {
947 free_extent_map(em);
948 break;
949 }
950 btrfs_drop_extent_cache(inode, start,
951 start + ram_size - 1, 0);
952 }
953 if (ret)
954 goto out_reserve;
955
956 cur_alloc_size = ins.offset;
957 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
958 ram_size, cur_alloc_size, 0);
959 if (ret)
960 goto out_reserve;
961
962 if (root->root_key.objectid ==
963 BTRFS_DATA_RELOC_TREE_OBJECTID) {
964 ret = btrfs_reloc_clone_csums(inode, start,
965 cur_alloc_size);
966 if (ret) {
967 btrfs_abort_transaction(trans, root, ret);
968 goto out_reserve;
969 }
970 }
971
972 if (disk_num_bytes < cur_alloc_size)
973 break;
974
975 /* we're not doing compressed IO, don't unlock the first
976 * page (which the caller expects to stay locked), don't
977 * clear any dirty bits and don't set any writeback bits
978 *
979 * Do set the Private2 bit so we know this page was properly
980 * setup for writepage
981 */
982 op = unlock ? PAGE_UNLOCK : 0;
983 op |= PAGE_SET_PRIVATE2;
984
985 extent_clear_unlock_delalloc(inode, start,
986 start + ram_size - 1, locked_page,
987 EXTENT_LOCKED | EXTENT_DELALLOC,
988 op);
989 disk_num_bytes -= cur_alloc_size;
990 num_bytes -= cur_alloc_size;
991 alloc_hint = ins.objectid + ins.offset;
992 start += cur_alloc_size;
993 }
994 out:
995 return ret;
996
997 out_reserve:
998 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
999 out_unlock:
1000 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1001 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
1002 EXTENT_DELALLOC | EXTENT_DEFRAG,
1003 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1004 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
1005 goto out;
1006 }
1007
1008 static noinline int cow_file_range(struct inode *inode,
1009 struct page *locked_page,
1010 u64 start, u64 end, int *page_started,
1011 unsigned long *nr_written,
1012 int unlock)
1013 {
1014 struct btrfs_trans_handle *trans;
1015 struct btrfs_root *root = BTRFS_I(inode)->root;
1016 int ret;
1017
1018 trans = btrfs_join_transaction(root);
1019 if (IS_ERR(trans)) {
1020 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1021 EXTENT_LOCKED | EXTENT_DELALLOC |
1022 EXTENT_DO_ACCOUNTING |
1023 EXTENT_DEFRAG, PAGE_UNLOCK |
1024 PAGE_CLEAR_DIRTY |
1025 PAGE_SET_WRITEBACK |
1026 PAGE_END_WRITEBACK);
1027 return PTR_ERR(trans);
1028 }
1029 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1030
1031 ret = __cow_file_range(trans, inode, root, locked_page, start, end,
1032 page_started, nr_written, unlock);
1033
1034 btrfs_end_transaction(trans, root);
1035
1036 return ret;
1037 }
1038
1039 /*
1040 * work queue call back to started compression on a file and pages
1041 */
1042 static noinline void async_cow_start(struct btrfs_work *work)
1043 {
1044 struct async_cow *async_cow;
1045 int num_added = 0;
1046 async_cow = container_of(work, struct async_cow, work);
1047
1048 compress_file_range(async_cow->inode, async_cow->locked_page,
1049 async_cow->start, async_cow->end, async_cow,
1050 &num_added);
1051 if (num_added == 0) {
1052 btrfs_add_delayed_iput(async_cow->inode);
1053 async_cow->inode = NULL;
1054 }
1055 }
1056
1057 /*
1058 * work queue call back to submit previously compressed pages
1059 */
1060 static noinline void async_cow_submit(struct btrfs_work *work)
1061 {
1062 struct async_cow *async_cow;
1063 struct btrfs_root *root;
1064 unsigned long nr_pages;
1065
1066 async_cow = container_of(work, struct async_cow, work);
1067
1068 root = async_cow->root;
1069 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1070 PAGE_CACHE_SHIFT;
1071
1072 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
1073 5 * 1024 * 1024 &&
1074 waitqueue_active(&root->fs_info->async_submit_wait))
1075 wake_up(&root->fs_info->async_submit_wait);
1076
1077 if (async_cow->inode)
1078 submit_compressed_extents(async_cow->inode, async_cow);
1079 }
1080
1081 static noinline void async_cow_free(struct btrfs_work *work)
1082 {
1083 struct async_cow *async_cow;
1084 async_cow = container_of(work, struct async_cow, work);
1085 if (async_cow->inode)
1086 btrfs_add_delayed_iput(async_cow->inode);
1087 kfree(async_cow);
1088 }
1089
1090 static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1091 u64 start, u64 end, int *page_started,
1092 unsigned long *nr_written)
1093 {
1094 struct async_cow *async_cow;
1095 struct btrfs_root *root = BTRFS_I(inode)->root;
1096 unsigned long nr_pages;
1097 u64 cur_end;
1098 int limit = 10 * 1024 * 1024;
1099
1100 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1101 1, 0, NULL, GFP_NOFS);
1102 while (start < end) {
1103 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
1104 BUG_ON(!async_cow); /* -ENOMEM */
1105 async_cow->inode = igrab(inode);
1106 async_cow->root = root;
1107 async_cow->locked_page = locked_page;
1108 async_cow->start = start;
1109
1110 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
1111 cur_end = end;
1112 else
1113 cur_end = min(end, start + 512 * 1024 - 1);
1114
1115 async_cow->end = cur_end;
1116 INIT_LIST_HEAD(&async_cow->extents);
1117
1118 async_cow->work.func = async_cow_start;
1119 async_cow->work.ordered_func = async_cow_submit;
1120 async_cow->work.ordered_free = async_cow_free;
1121 async_cow->work.flags = 0;
1122
1123 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1124 PAGE_CACHE_SHIFT;
1125 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1126
1127 btrfs_queue_worker(&root->fs_info->delalloc_workers,
1128 &async_cow->work);
1129
1130 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1131 wait_event(root->fs_info->async_submit_wait,
1132 (atomic_read(&root->fs_info->async_delalloc_pages) <
1133 limit));
1134 }
1135
1136 while (atomic_read(&root->fs_info->async_submit_draining) &&
1137 atomic_read(&root->fs_info->async_delalloc_pages)) {
1138 wait_event(root->fs_info->async_submit_wait,
1139 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1140 0));
1141 }
1142
1143 *nr_written += nr_pages;
1144 start = cur_end + 1;
1145 }
1146 *page_started = 1;
1147 return 0;
1148 }
1149
1150 static noinline int csum_exist_in_range(struct btrfs_root *root,
1151 u64 bytenr, u64 num_bytes)
1152 {
1153 int ret;
1154 struct btrfs_ordered_sum *sums;
1155 LIST_HEAD(list);
1156
1157 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
1158 bytenr + num_bytes - 1, &list, 0);
1159 if (ret == 0 && list_empty(&list))
1160 return 0;
1161
1162 while (!list_empty(&list)) {
1163 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1164 list_del(&sums->list);
1165 kfree(sums);
1166 }
1167 return 1;
1168 }
1169
1170 /*
1171 * when nowcow writeback call back. This checks for snapshots or COW copies
1172 * of the extents that exist in the file, and COWs the file as required.
1173 *
1174 * If no cow copies or snapshots exist, we write directly to the existing
1175 * blocks on disk
1176 */
1177 static noinline int run_delalloc_nocow(struct inode *inode,
1178 struct page *locked_page,
1179 u64 start, u64 end, int *page_started, int force,
1180 unsigned long *nr_written)
1181 {
1182 struct btrfs_root *root = BTRFS_I(inode)->root;
1183 struct btrfs_trans_handle *trans;
1184 struct extent_buffer *leaf;
1185 struct btrfs_path *path;
1186 struct btrfs_file_extent_item *fi;
1187 struct btrfs_key found_key;
1188 u64 cow_start;
1189 u64 cur_offset;
1190 u64 extent_end;
1191 u64 extent_offset;
1192 u64 disk_bytenr;
1193 u64 num_bytes;
1194 u64 disk_num_bytes;
1195 u64 ram_bytes;
1196 int extent_type;
1197 int ret, err;
1198 int type;
1199 int nocow;
1200 int check_prev = 1;
1201 bool nolock;
1202 u64 ino = btrfs_ino(inode);
1203
1204 path = btrfs_alloc_path();
1205 if (!path) {
1206 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1207 EXTENT_LOCKED | EXTENT_DELALLOC |
1208 EXTENT_DO_ACCOUNTING |
1209 EXTENT_DEFRAG, PAGE_UNLOCK |
1210 PAGE_CLEAR_DIRTY |
1211 PAGE_SET_WRITEBACK |
1212 PAGE_END_WRITEBACK);
1213 return -ENOMEM;
1214 }
1215
1216 nolock = btrfs_is_free_space_inode(inode);
1217
1218 if (nolock)
1219 trans = btrfs_join_transaction_nolock(root);
1220 else
1221 trans = btrfs_join_transaction(root);
1222
1223 if (IS_ERR(trans)) {
1224 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1225 EXTENT_LOCKED | EXTENT_DELALLOC |
1226 EXTENT_DO_ACCOUNTING |
1227 EXTENT_DEFRAG, PAGE_UNLOCK |
1228 PAGE_CLEAR_DIRTY |
1229 PAGE_SET_WRITEBACK |
1230 PAGE_END_WRITEBACK);
1231 btrfs_free_path(path);
1232 return PTR_ERR(trans);
1233 }
1234
1235 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1236
1237 cow_start = (u64)-1;
1238 cur_offset = start;
1239 while (1) {
1240 ret = btrfs_lookup_file_extent(trans, root, path, ino,
1241 cur_offset, 0);
1242 if (ret < 0) {
1243 btrfs_abort_transaction(trans, root, ret);
1244 goto error;
1245 }
1246 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1247 leaf = path->nodes[0];
1248 btrfs_item_key_to_cpu(leaf, &found_key,
1249 path->slots[0] - 1);
1250 if (found_key.objectid == ino &&
1251 found_key.type == BTRFS_EXTENT_DATA_KEY)
1252 path->slots[0]--;
1253 }
1254 check_prev = 0;
1255 next_slot:
1256 leaf = path->nodes[0];
1257 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1258 ret = btrfs_next_leaf(root, path);
1259 if (ret < 0) {
1260 btrfs_abort_transaction(trans, root, ret);
1261 goto error;
1262 }
1263 if (ret > 0)
1264 break;
1265 leaf = path->nodes[0];
1266 }
1267
1268 nocow = 0;
1269 disk_bytenr = 0;
1270 num_bytes = 0;
1271 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1272
1273 if (found_key.objectid > ino ||
1274 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1275 found_key.offset > end)
1276 break;
1277
1278 if (found_key.offset > cur_offset) {
1279 extent_end = found_key.offset;
1280 extent_type = 0;
1281 goto out_check;
1282 }
1283
1284 fi = btrfs_item_ptr(leaf, path->slots[0],
1285 struct btrfs_file_extent_item);
1286 extent_type = btrfs_file_extent_type(leaf, fi);
1287
1288 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
1289 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1290 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1291 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1292 extent_offset = btrfs_file_extent_offset(leaf, fi);
1293 extent_end = found_key.offset +
1294 btrfs_file_extent_num_bytes(leaf, fi);
1295 disk_num_bytes =
1296 btrfs_file_extent_disk_num_bytes(leaf, fi);
1297 if (extent_end <= start) {
1298 path->slots[0]++;
1299 goto next_slot;
1300 }
1301 if (disk_bytenr == 0)
1302 goto out_check;
1303 if (btrfs_file_extent_compression(leaf, fi) ||
1304 btrfs_file_extent_encryption(leaf, fi) ||
1305 btrfs_file_extent_other_encoding(leaf, fi))
1306 goto out_check;
1307 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1308 goto out_check;
1309 if (btrfs_extent_readonly(root, disk_bytenr))
1310 goto out_check;
1311 if (btrfs_cross_ref_exist(trans, root, ino,
1312 found_key.offset -
1313 extent_offset, disk_bytenr))
1314 goto out_check;
1315 disk_bytenr += extent_offset;
1316 disk_bytenr += cur_offset - found_key.offset;
1317 num_bytes = min(end + 1, extent_end) - cur_offset;
1318 /*
1319 * force cow if csum exists in the range.
1320 * this ensure that csum for a given extent are
1321 * either valid or do not exist.
1322 */
1323 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1324 goto out_check;
1325 nocow = 1;
1326 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1327 extent_end = found_key.offset +
1328 btrfs_file_extent_inline_len(leaf, fi);
1329 extent_end = ALIGN(extent_end, root->sectorsize);
1330 } else {
1331 BUG_ON(1);
1332 }
1333 out_check:
1334 if (extent_end <= start) {
1335 path->slots[0]++;
1336 goto next_slot;
1337 }
1338 if (!nocow) {
1339 if (cow_start == (u64)-1)
1340 cow_start = cur_offset;
1341 cur_offset = extent_end;
1342 if (cur_offset > end)
1343 break;
1344 path->slots[0]++;
1345 goto next_slot;
1346 }
1347
1348 btrfs_release_path(path);
1349 if (cow_start != (u64)-1) {
1350 ret = __cow_file_range(trans, inode, root, locked_page,
1351 cow_start, found_key.offset - 1,
1352 page_started, nr_written, 1);
1353 if (ret) {
1354 btrfs_abort_transaction(trans, root, ret);
1355 goto error;
1356 }
1357 cow_start = (u64)-1;
1358 }
1359
1360 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1361 struct extent_map *em;
1362 struct extent_map_tree *em_tree;
1363 em_tree = &BTRFS_I(inode)->extent_tree;
1364 em = alloc_extent_map();
1365 BUG_ON(!em); /* -ENOMEM */
1366 em->start = cur_offset;
1367 em->orig_start = found_key.offset - extent_offset;
1368 em->len = num_bytes;
1369 em->block_len = num_bytes;
1370 em->block_start = disk_bytenr;
1371 em->orig_block_len = disk_num_bytes;
1372 em->ram_bytes = ram_bytes;
1373 em->bdev = root->fs_info->fs_devices->latest_bdev;
1374 em->mod_start = em->start;
1375 em->mod_len = em->len;
1376 set_bit(EXTENT_FLAG_PINNED, &em->flags);
1377 set_bit(EXTENT_FLAG_FILLING, &em->flags);
1378 em->generation = -1;
1379 while (1) {
1380 write_lock(&em_tree->lock);
1381 ret = add_extent_mapping(em_tree, em, 1);
1382 write_unlock(&em_tree->lock);
1383 if (ret != -EEXIST) {
1384 free_extent_map(em);
1385 break;
1386 }
1387 btrfs_drop_extent_cache(inode, em->start,
1388 em->start + em->len - 1, 0);
1389 }
1390 type = BTRFS_ORDERED_PREALLOC;
1391 } else {
1392 type = BTRFS_ORDERED_NOCOW;
1393 }
1394
1395 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
1396 num_bytes, num_bytes, type);
1397 BUG_ON(ret); /* -ENOMEM */
1398
1399 if (root->root_key.objectid ==
1400 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1401 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1402 num_bytes);
1403 if (ret) {
1404 btrfs_abort_transaction(trans, root, ret);
1405 goto error;
1406 }
1407 }
1408
1409 extent_clear_unlock_delalloc(inode, cur_offset,
1410 cur_offset + num_bytes - 1,
1411 locked_page, EXTENT_LOCKED |
1412 EXTENT_DELALLOC, PAGE_UNLOCK |
1413 PAGE_SET_PRIVATE2);
1414 cur_offset = extent_end;
1415 if (cur_offset > end)
1416 break;
1417 }
1418 btrfs_release_path(path);
1419
1420 if (cur_offset <= end && cow_start == (u64)-1) {
1421 cow_start = cur_offset;
1422 cur_offset = end;
1423 }
1424
1425 if (cow_start != (u64)-1) {
1426 ret = __cow_file_range(trans, inode, root, locked_page,
1427 cow_start, end,
1428 page_started, nr_written, 1);
1429 if (ret) {
1430 btrfs_abort_transaction(trans, root, ret);
1431 goto error;
1432 }
1433 }
1434
1435 error:
1436 err = btrfs_end_transaction(trans, root);
1437 if (!ret)
1438 ret = err;
1439
1440 if (ret && cur_offset < end)
1441 extent_clear_unlock_delalloc(inode, cur_offset, end,
1442 locked_page, EXTENT_LOCKED |
1443 EXTENT_DELALLOC | EXTENT_DEFRAG |
1444 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1445 PAGE_CLEAR_DIRTY |
1446 PAGE_SET_WRITEBACK |
1447 PAGE_END_WRITEBACK);
1448 btrfs_free_path(path);
1449 return ret;
1450 }
1451
1452 /*
1453 * extent_io.c call back to do delayed allocation processing
1454 */
1455 static int run_delalloc_range(struct inode *inode, struct page *locked_page,
1456 u64 start, u64 end, int *page_started,
1457 unsigned long *nr_written)
1458 {
1459 int ret;
1460 struct btrfs_root *root = BTRFS_I(inode)->root;
1461
1462 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) {
1463 ret = run_delalloc_nocow(inode, locked_page, start, end,
1464 page_started, 1, nr_written);
1465 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) {
1466 ret = run_delalloc_nocow(inode, locked_page, start, end,
1467 page_started, 0, nr_written);
1468 } else if (!btrfs_test_opt(root, COMPRESS) &&
1469 !(BTRFS_I(inode)->force_compress) &&
1470 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) {
1471 ret = cow_file_range(inode, locked_page, start, end,
1472 page_started, nr_written, 1);
1473 } else {
1474 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1475 &BTRFS_I(inode)->runtime_flags);
1476 ret = cow_file_range_async(inode, locked_page, start, end,
1477 page_started, nr_written);
1478 }
1479 return ret;
1480 }
1481
1482 static void btrfs_split_extent_hook(struct inode *inode,
1483 struct extent_state *orig, u64 split)
1484 {
1485 /* not delalloc, ignore it */
1486 if (!(orig->state & EXTENT_DELALLOC))
1487 return;
1488
1489 spin_lock(&BTRFS_I(inode)->lock);
1490 BTRFS_I(inode)->outstanding_extents++;
1491 spin_unlock(&BTRFS_I(inode)->lock);
1492 }
1493
1494 /*
1495 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1496 * extents so we can keep track of new extents that are just merged onto old
1497 * extents, such as when we are doing sequential writes, so we can properly
1498 * account for the metadata space we'll need.
1499 */
1500 static void btrfs_merge_extent_hook(struct inode *inode,
1501 struct extent_state *new,
1502 struct extent_state *other)
1503 {
1504 /* not delalloc, ignore it */
1505 if (!(other->state & EXTENT_DELALLOC))
1506 return;
1507
1508 spin_lock(&BTRFS_I(inode)->lock);
1509 BTRFS_I(inode)->outstanding_extents--;
1510 spin_unlock(&BTRFS_I(inode)->lock);
1511 }
1512
1513 static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1514 struct inode *inode)
1515 {
1516 spin_lock(&root->delalloc_lock);
1517 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1518 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1519 &root->delalloc_inodes);
1520 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1521 &BTRFS_I(inode)->runtime_flags);
1522 root->nr_delalloc_inodes++;
1523 if (root->nr_delalloc_inodes == 1) {
1524 spin_lock(&root->fs_info->delalloc_root_lock);
1525 BUG_ON(!list_empty(&root->delalloc_root));
1526 list_add_tail(&root->delalloc_root,
1527 &root->fs_info->delalloc_roots);
1528 spin_unlock(&root->fs_info->delalloc_root_lock);
1529 }
1530 }
1531 spin_unlock(&root->delalloc_lock);
1532 }
1533
1534 static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1535 struct inode *inode)
1536 {
1537 spin_lock(&root->delalloc_lock);
1538 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1539 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1540 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1541 &BTRFS_I(inode)->runtime_flags);
1542 root->nr_delalloc_inodes--;
1543 if (!root->nr_delalloc_inodes) {
1544 spin_lock(&root->fs_info->delalloc_root_lock);
1545 BUG_ON(list_empty(&root->delalloc_root));
1546 list_del_init(&root->delalloc_root);
1547 spin_unlock(&root->fs_info->delalloc_root_lock);
1548 }
1549 }
1550 spin_unlock(&root->delalloc_lock);
1551 }
1552
1553 /*
1554 * extent_io.c set_bit_hook, used to track delayed allocation
1555 * bytes in this file, and to maintain the list of inodes that
1556 * have pending delalloc work to be done.
1557 */
1558 static void btrfs_set_bit_hook(struct inode *inode,
1559 struct extent_state *state, unsigned long *bits)
1560 {
1561
1562 /*
1563 * set_bit and clear bit hooks normally require _irqsave/restore
1564 * but in this case, we are only testing for the DELALLOC
1565 * bit, which is only set or cleared with irqs on
1566 */
1567 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
1568 struct btrfs_root *root = BTRFS_I(inode)->root;
1569 u64 len = state->end + 1 - state->start;
1570 bool do_list = !btrfs_is_free_space_inode(inode);
1571
1572 if (*bits & EXTENT_FIRST_DELALLOC) {
1573 *bits &= ~EXTENT_FIRST_DELALLOC;
1574 } else {
1575 spin_lock(&BTRFS_I(inode)->lock);
1576 BTRFS_I(inode)->outstanding_extents++;
1577 spin_unlock(&BTRFS_I(inode)->lock);
1578 }
1579
1580 __percpu_counter_add(&root->fs_info->delalloc_bytes, len,
1581 root->fs_info->delalloc_batch);
1582 spin_lock(&BTRFS_I(inode)->lock);
1583 BTRFS_I(inode)->delalloc_bytes += len;
1584 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1585 &BTRFS_I(inode)->runtime_flags))
1586 btrfs_add_delalloc_inodes(root, inode);
1587 spin_unlock(&BTRFS_I(inode)->lock);
1588 }
1589 }
1590
1591 /*
1592 * extent_io.c clear_bit_hook, see set_bit_hook for why
1593 */
1594 static void btrfs_clear_bit_hook(struct inode *inode,
1595 struct extent_state *state,
1596 unsigned long *bits)
1597 {
1598 /*
1599 * set_bit and clear bit hooks normally require _irqsave/restore
1600 * but in this case, we are only testing for the DELALLOC
1601 * bit, which is only set or cleared with irqs on
1602 */
1603 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
1604 struct btrfs_root *root = BTRFS_I(inode)->root;
1605 u64 len = state->end + 1 - state->start;
1606 bool do_list = !btrfs_is_free_space_inode(inode);
1607
1608 if (*bits & EXTENT_FIRST_DELALLOC) {
1609 *bits &= ~EXTENT_FIRST_DELALLOC;
1610 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1611 spin_lock(&BTRFS_I(inode)->lock);
1612 BTRFS_I(inode)->outstanding_extents--;
1613 spin_unlock(&BTRFS_I(inode)->lock);
1614 }
1615
1616 if (*bits & EXTENT_DO_ACCOUNTING)
1617 btrfs_delalloc_release_metadata(inode, len);
1618
1619 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1620 && do_list && !(state->state & EXTENT_NORESERVE))
1621 btrfs_free_reserved_data_space(inode, len);
1622
1623 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len,
1624 root->fs_info->delalloc_batch);
1625 spin_lock(&BTRFS_I(inode)->lock);
1626 BTRFS_I(inode)->delalloc_bytes -= len;
1627 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
1628 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1629 &BTRFS_I(inode)->runtime_flags))
1630 btrfs_del_delalloc_inode(root, inode);
1631 spin_unlock(&BTRFS_I(inode)->lock);
1632 }
1633 }
1634
1635 /*
1636 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1637 * we don't create bios that span stripes or chunks
1638 */
1639 int btrfs_merge_bio_hook(int rw, struct page *page, unsigned long offset,
1640 size_t size, struct bio *bio,
1641 unsigned long bio_flags)
1642 {
1643 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1644 u64 logical = (u64)bio->bi_sector << 9;
1645 u64 length = 0;
1646 u64 map_length;
1647 int ret;
1648
1649 if (bio_flags & EXTENT_BIO_COMPRESSED)
1650 return 0;
1651
1652 length = bio->bi_size;
1653 map_length = length;
1654 ret = btrfs_map_block(root->fs_info, rw, logical,
1655 &map_length, NULL, 0);
1656 /* Will always return 0 with map_multi == NULL */
1657 BUG_ON(ret < 0);
1658 if (map_length < length + size)
1659 return 1;
1660 return 0;
1661 }
1662
1663 /*
1664 * in order to insert checksums into the metadata in large chunks,
1665 * we wait until bio submission time. All the pages in the bio are
1666 * checksummed and sums are attached onto the ordered extent record.
1667 *
1668 * At IO completion time the cums attached on the ordered extent record
1669 * are inserted into the btree
1670 */
1671 static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1672 struct bio *bio, int mirror_num,
1673 unsigned long bio_flags,
1674 u64 bio_offset)
1675 {
1676 struct btrfs_root *root = BTRFS_I(inode)->root;
1677 int ret = 0;
1678
1679 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1680 BUG_ON(ret); /* -ENOMEM */
1681 return 0;
1682 }
1683
1684 /*
1685 * in order to insert checksums into the metadata in large chunks,
1686 * we wait until bio submission time. All the pages in the bio are
1687 * checksummed and sums are attached onto the ordered extent record.
1688 *
1689 * At IO completion time the cums attached on the ordered extent record
1690 * are inserted into the btree
1691 */
1692 static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
1693 int mirror_num, unsigned long bio_flags,
1694 u64 bio_offset)
1695 {
1696 struct btrfs_root *root = BTRFS_I(inode)->root;
1697 int ret;
1698
1699 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1700 if (ret)
1701 bio_endio(bio, ret);
1702 return ret;
1703 }
1704
1705 /*
1706 * extent_io.c submission hook. This does the right thing for csum calculation
1707 * on write, or reading the csums from the tree before a read
1708 */
1709 static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
1710 int mirror_num, unsigned long bio_flags,
1711 u64 bio_offset)
1712 {
1713 struct btrfs_root *root = BTRFS_I(inode)->root;
1714 int ret = 0;
1715 int skip_sum;
1716 int metadata = 0;
1717 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
1718
1719 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
1720
1721 if (btrfs_is_free_space_inode(inode))
1722 metadata = 2;
1723
1724 if (!(rw & REQ_WRITE)) {
1725 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1726 if (ret)
1727 goto out;
1728
1729 if (bio_flags & EXTENT_BIO_COMPRESSED) {
1730 ret = btrfs_submit_compressed_read(inode, bio,
1731 mirror_num,
1732 bio_flags);
1733 goto out;
1734 } else if (!skip_sum) {
1735 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1736 if (ret)
1737 goto out;
1738 }
1739 goto mapit;
1740 } else if (async && !skip_sum) {
1741 /* csum items have already been cloned */
1742 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1743 goto mapit;
1744 /* we're doing a write, do the async checksumming */
1745 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
1746 inode, rw, bio, mirror_num,
1747 bio_flags, bio_offset,
1748 __btrfs_submit_bio_start,
1749 __btrfs_submit_bio_done);
1750 goto out;
1751 } else if (!skip_sum) {
1752 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1753 if (ret)
1754 goto out;
1755 }
1756
1757 mapit:
1758 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1759
1760 out:
1761 if (ret < 0)
1762 bio_endio(bio, ret);
1763 return ret;
1764 }
1765
1766 /*
1767 * given a list of ordered sums record them in the inode. This happens
1768 * at IO completion time based on sums calculated at bio submission time.
1769 */
1770 static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
1771 struct inode *inode, u64 file_offset,
1772 struct list_head *list)
1773 {
1774 struct btrfs_ordered_sum *sum;
1775
1776 list_for_each_entry(sum, list, list) {
1777 trans->adding_csums = 1;
1778 btrfs_csum_file_blocks(trans,
1779 BTRFS_I(inode)->root->fs_info->csum_root, sum);
1780 trans->adding_csums = 0;
1781 }
1782 return 0;
1783 }
1784
1785 int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1786 struct extent_state **cached_state)
1787 {
1788 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
1789 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
1790 cached_state, GFP_NOFS);
1791 }
1792
1793 /* see btrfs_writepage_start_hook for details on why this is required */
1794 struct btrfs_writepage_fixup {
1795 struct page *page;
1796 struct btrfs_work work;
1797 };
1798
1799 static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
1800 {
1801 struct btrfs_writepage_fixup *fixup;
1802 struct btrfs_ordered_extent *ordered;
1803 struct extent_state *cached_state = NULL;
1804 struct page *page;
1805 struct inode *inode;
1806 u64 page_start;
1807 u64 page_end;
1808 int ret;
1809
1810 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1811 page = fixup->page;
1812 again:
1813 lock_page(page);
1814 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1815 ClearPageChecked(page);
1816 goto out_page;
1817 }
1818
1819 inode = page->mapping->host;
1820 page_start = page_offset(page);
1821 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1822
1823 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
1824 &cached_state);
1825
1826 /* already ordered? We're done */
1827 if (PagePrivate2(page))
1828 goto out;
1829
1830 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1831 if (ordered) {
1832 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1833 page_end, &cached_state, GFP_NOFS);
1834 unlock_page(page);
1835 btrfs_start_ordered_extent(inode, ordered, 1);
1836 btrfs_put_ordered_extent(ordered);
1837 goto again;
1838 }
1839
1840 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1841 if (ret) {
1842 mapping_set_error(page->mapping, ret);
1843 end_extent_writepage(page, ret, page_start, page_end);
1844 ClearPageChecked(page);
1845 goto out;
1846 }
1847
1848 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
1849 ClearPageChecked(page);
1850 set_page_dirty(page);
1851 out:
1852 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1853 &cached_state, GFP_NOFS);
1854 out_page:
1855 unlock_page(page);
1856 page_cache_release(page);
1857 kfree(fixup);
1858 }
1859
1860 /*
1861 * There are a few paths in the higher layers of the kernel that directly
1862 * set the page dirty bit without asking the filesystem if it is a
1863 * good idea. This causes problems because we want to make sure COW
1864 * properly happens and the data=ordered rules are followed.
1865 *
1866 * In our case any range that doesn't have the ORDERED bit set
1867 * hasn't been properly setup for IO. We kick off an async process
1868 * to fix it up. The async helper will wait for ordered extents, set
1869 * the delalloc bit and make it safe to write the page.
1870 */
1871 static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
1872 {
1873 struct inode *inode = page->mapping->host;
1874 struct btrfs_writepage_fixup *fixup;
1875 struct btrfs_root *root = BTRFS_I(inode)->root;
1876
1877 /* this page is properly in the ordered list */
1878 if (TestClearPagePrivate2(page))
1879 return 0;
1880
1881 if (PageChecked(page))
1882 return -EAGAIN;
1883
1884 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1885 if (!fixup)
1886 return -EAGAIN;
1887
1888 SetPageChecked(page);
1889 page_cache_get(page);
1890 fixup->work.func = btrfs_writepage_fixup_worker;
1891 fixup->page = page;
1892 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
1893 return -EBUSY;
1894 }
1895
1896 static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1897 struct inode *inode, u64 file_pos,
1898 u64 disk_bytenr, u64 disk_num_bytes,
1899 u64 num_bytes, u64 ram_bytes,
1900 u8 compression, u8 encryption,
1901 u16 other_encoding, int extent_type)
1902 {
1903 struct btrfs_root *root = BTRFS_I(inode)->root;
1904 struct btrfs_file_extent_item *fi;
1905 struct btrfs_path *path;
1906 struct extent_buffer *leaf;
1907 struct btrfs_key ins;
1908 int ret;
1909
1910 path = btrfs_alloc_path();
1911 if (!path)
1912 return -ENOMEM;
1913
1914 path->leave_spinning = 1;
1915
1916 /*
1917 * we may be replacing one extent in the tree with another.
1918 * The new extent is pinned in the extent map, and we don't want
1919 * to drop it from the cache until it is completely in the btree.
1920 *
1921 * So, tell btrfs_drop_extents to leave this extent in the cache.
1922 * the caller is expected to unpin it and allow it to be merged
1923 * with the others.
1924 */
1925 ret = btrfs_drop_extents(trans, root, inode, file_pos,
1926 file_pos + num_bytes, 0);
1927 if (ret)
1928 goto out;
1929
1930 ins.objectid = btrfs_ino(inode);
1931 ins.offset = file_pos;
1932 ins.type = BTRFS_EXTENT_DATA_KEY;
1933 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
1934 if (ret)
1935 goto out;
1936 leaf = path->nodes[0];
1937 fi = btrfs_item_ptr(leaf, path->slots[0],
1938 struct btrfs_file_extent_item);
1939 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1940 btrfs_set_file_extent_type(leaf, fi, extent_type);
1941 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1942 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1943 btrfs_set_file_extent_offset(leaf, fi, 0);
1944 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1945 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1946 btrfs_set_file_extent_compression(leaf, fi, compression);
1947 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1948 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
1949
1950 btrfs_mark_buffer_dirty(leaf);
1951 btrfs_release_path(path);
1952
1953 inode_add_bytes(inode, num_bytes);
1954
1955 ins.objectid = disk_bytenr;
1956 ins.offset = disk_num_bytes;
1957 ins.type = BTRFS_EXTENT_ITEM_KEY;
1958 ret = btrfs_alloc_reserved_file_extent(trans, root,
1959 root->root_key.objectid,
1960 btrfs_ino(inode), file_pos, &ins);
1961 out:
1962 btrfs_free_path(path);
1963
1964 return ret;
1965 }
1966
1967 /* snapshot-aware defrag */
1968 struct sa_defrag_extent_backref {
1969 struct rb_node node;
1970 struct old_sa_defrag_extent *old;
1971 u64 root_id;
1972 u64 inum;
1973 u64 file_pos;
1974 u64 extent_offset;
1975 u64 num_bytes;
1976 u64 generation;
1977 };
1978
1979 struct old_sa_defrag_extent {
1980 struct list_head list;
1981 struct new_sa_defrag_extent *new;
1982
1983 u64 extent_offset;
1984 u64 bytenr;
1985 u64 offset;
1986 u64 len;
1987 int count;
1988 };
1989
1990 struct new_sa_defrag_extent {
1991 struct rb_root root;
1992 struct list_head head;
1993 struct btrfs_path *path;
1994 struct inode *inode;
1995 u64 file_pos;
1996 u64 len;
1997 u64 bytenr;
1998 u64 disk_len;
1999 u8 compress_type;
2000 };
2001
2002 static int backref_comp(struct sa_defrag_extent_backref *b1,
2003 struct sa_defrag_extent_backref *b2)
2004 {
2005 if (b1->root_id < b2->root_id)
2006 return -1;
2007 else if (b1->root_id > b2->root_id)
2008 return 1;
2009
2010 if (b1->inum < b2->inum)
2011 return -1;
2012 else if (b1->inum > b2->inum)
2013 return 1;
2014
2015 if (b1->file_pos < b2->file_pos)
2016 return -1;
2017 else if (b1->file_pos > b2->file_pos)
2018 return 1;
2019
2020 /*
2021 * [------------------------------] ===> (a range of space)
2022 * |<--->| |<---->| =============> (fs/file tree A)
2023 * |<---------------------------->| ===> (fs/file tree B)
2024 *
2025 * A range of space can refer to two file extents in one tree while
2026 * refer to only one file extent in another tree.
2027 *
2028 * So we may process a disk offset more than one time(two extents in A)
2029 * and locate at the same extent(one extent in B), then insert two same
2030 * backrefs(both refer to the extent in B).
2031 */
2032 return 0;
2033 }
2034
2035 static void backref_insert(struct rb_root *root,
2036 struct sa_defrag_extent_backref *backref)
2037 {
2038 struct rb_node **p = &root->rb_node;
2039 struct rb_node *parent = NULL;
2040 struct sa_defrag_extent_backref *entry;
2041 int ret;
2042
2043 while (*p) {
2044 parent = *p;
2045 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2046
2047 ret = backref_comp(backref, entry);
2048 if (ret < 0)
2049 p = &(*p)->rb_left;
2050 else
2051 p = &(*p)->rb_right;
2052 }
2053
2054 rb_link_node(&backref->node, parent, p);
2055 rb_insert_color(&backref->node, root);
2056 }
2057
2058 /*
2059 * Note the backref might has changed, and in this case we just return 0.
2060 */
2061 static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2062 void *ctx)
2063 {
2064 struct btrfs_file_extent_item *extent;
2065 struct btrfs_fs_info *fs_info;
2066 struct old_sa_defrag_extent *old = ctx;
2067 struct new_sa_defrag_extent *new = old->new;
2068 struct btrfs_path *path = new->path;
2069 struct btrfs_key key;
2070 struct btrfs_root *root;
2071 struct sa_defrag_extent_backref *backref;
2072 struct extent_buffer *leaf;
2073 struct inode *inode = new->inode;
2074 int slot;
2075 int ret;
2076 u64 extent_offset;
2077 u64 num_bytes;
2078
2079 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2080 inum == btrfs_ino(inode))
2081 return 0;
2082
2083 key.objectid = root_id;
2084 key.type = BTRFS_ROOT_ITEM_KEY;
2085 key.offset = (u64)-1;
2086
2087 fs_info = BTRFS_I(inode)->root->fs_info;
2088 root = btrfs_read_fs_root_no_name(fs_info, &key);
2089 if (IS_ERR(root)) {
2090 if (PTR_ERR(root) == -ENOENT)
2091 return 0;
2092 WARN_ON(1);
2093 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2094 inum, offset, root_id);
2095 return PTR_ERR(root);
2096 }
2097
2098 key.objectid = inum;
2099 key.type = BTRFS_EXTENT_DATA_KEY;
2100 if (offset > (u64)-1 << 32)
2101 key.offset = 0;
2102 else
2103 key.offset = offset;
2104
2105 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2106 if (ret < 0) {
2107 WARN_ON(1);
2108 return ret;
2109 }
2110 ret = 0;
2111
2112 while (1) {
2113 cond_resched();
2114
2115 leaf = path->nodes[0];
2116 slot = path->slots[0];
2117
2118 if (slot >= btrfs_header_nritems(leaf)) {
2119 ret = btrfs_next_leaf(root, path);
2120 if (ret < 0) {
2121 goto out;
2122 } else if (ret > 0) {
2123 ret = 0;
2124 goto out;
2125 }
2126 continue;
2127 }
2128
2129 path->slots[0]++;
2130
2131 btrfs_item_key_to_cpu(leaf, &key, slot);
2132
2133 if (key.objectid > inum)
2134 goto out;
2135
2136 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2137 continue;
2138
2139 extent = btrfs_item_ptr(leaf, slot,
2140 struct btrfs_file_extent_item);
2141
2142 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2143 continue;
2144
2145 /*
2146 * 'offset' refers to the exact key.offset,
2147 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2148 * (key.offset - extent_offset).
2149 */
2150 if (key.offset != offset)
2151 continue;
2152
2153 extent_offset = btrfs_file_extent_offset(leaf, extent);
2154 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
2155
2156 if (extent_offset >= old->extent_offset + old->offset +
2157 old->len || extent_offset + num_bytes <=
2158 old->extent_offset + old->offset)
2159 continue;
2160 break;
2161 }
2162
2163 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2164 if (!backref) {
2165 ret = -ENOENT;
2166 goto out;
2167 }
2168
2169 backref->root_id = root_id;
2170 backref->inum = inum;
2171 backref->file_pos = offset;
2172 backref->num_bytes = num_bytes;
2173 backref->extent_offset = extent_offset;
2174 backref->generation = btrfs_file_extent_generation(leaf, extent);
2175 backref->old = old;
2176 backref_insert(&new->root, backref);
2177 old->count++;
2178 out:
2179 btrfs_release_path(path);
2180 WARN_ON(ret);
2181 return ret;
2182 }
2183
2184 static noinline bool record_extent_backrefs(struct btrfs_path *path,
2185 struct new_sa_defrag_extent *new)
2186 {
2187 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info;
2188 struct old_sa_defrag_extent *old, *tmp;
2189 int ret;
2190
2191 new->path = path;
2192
2193 list_for_each_entry_safe(old, tmp, &new->head, list) {
2194 ret = iterate_inodes_from_logical(old->bytenr +
2195 old->extent_offset, fs_info,
2196 path, record_one_backref,
2197 old);
2198 BUG_ON(ret < 0 && ret != -ENOENT);
2199
2200 /* no backref to be processed for this extent */
2201 if (!old->count) {
2202 list_del(&old->list);
2203 kfree(old);
2204 }
2205 }
2206
2207 if (list_empty(&new->head))
2208 return false;
2209
2210 return true;
2211 }
2212
2213 static int relink_is_mergable(struct extent_buffer *leaf,
2214 struct btrfs_file_extent_item *fi,
2215 struct new_sa_defrag_extent *new)
2216 {
2217 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
2218 return 0;
2219
2220 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2221 return 0;
2222
2223 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2224 return 0;
2225
2226 if (btrfs_file_extent_encryption(leaf, fi) ||
2227 btrfs_file_extent_other_encoding(leaf, fi))
2228 return 0;
2229
2230 return 1;
2231 }
2232
2233 /*
2234 * Note the backref might has changed, and in this case we just return 0.
2235 */
2236 static noinline int relink_extent_backref(struct btrfs_path *path,
2237 struct sa_defrag_extent_backref *prev,
2238 struct sa_defrag_extent_backref *backref)
2239 {
2240 struct btrfs_file_extent_item *extent;
2241 struct btrfs_file_extent_item *item;
2242 struct btrfs_ordered_extent *ordered;
2243 struct btrfs_trans_handle *trans;
2244 struct btrfs_fs_info *fs_info;
2245 struct btrfs_root *root;
2246 struct btrfs_key key;
2247 struct extent_buffer *leaf;
2248 struct old_sa_defrag_extent *old = backref->old;
2249 struct new_sa_defrag_extent *new = old->new;
2250 struct inode *src_inode = new->inode;
2251 struct inode *inode;
2252 struct extent_state *cached = NULL;
2253 int ret = 0;
2254 u64 start;
2255 u64 len;
2256 u64 lock_start;
2257 u64 lock_end;
2258 bool merge = false;
2259 int index;
2260
2261 if (prev && prev->root_id == backref->root_id &&
2262 prev->inum == backref->inum &&
2263 prev->file_pos + prev->num_bytes == backref->file_pos)
2264 merge = true;
2265
2266 /* step 1: get root */
2267 key.objectid = backref->root_id;
2268 key.type = BTRFS_ROOT_ITEM_KEY;
2269 key.offset = (u64)-1;
2270
2271 fs_info = BTRFS_I(src_inode)->root->fs_info;
2272 index = srcu_read_lock(&fs_info->subvol_srcu);
2273
2274 root = btrfs_read_fs_root_no_name(fs_info, &key);
2275 if (IS_ERR(root)) {
2276 srcu_read_unlock(&fs_info->subvol_srcu, index);
2277 if (PTR_ERR(root) == -ENOENT)
2278 return 0;
2279 return PTR_ERR(root);
2280 }
2281
2282 /* step 2: get inode */
2283 key.objectid = backref->inum;
2284 key.type = BTRFS_INODE_ITEM_KEY;
2285 key.offset = 0;
2286
2287 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2288 if (IS_ERR(inode)) {
2289 srcu_read_unlock(&fs_info->subvol_srcu, index);
2290 return 0;
2291 }
2292
2293 srcu_read_unlock(&fs_info->subvol_srcu, index);
2294
2295 /* step 3: relink backref */
2296 lock_start = backref->file_pos;
2297 lock_end = backref->file_pos + backref->num_bytes - 1;
2298 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2299 0, &cached);
2300
2301 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2302 if (ordered) {
2303 btrfs_put_ordered_extent(ordered);
2304 goto out_unlock;
2305 }
2306
2307 trans = btrfs_join_transaction(root);
2308 if (IS_ERR(trans)) {
2309 ret = PTR_ERR(trans);
2310 goto out_unlock;
2311 }
2312
2313 key.objectid = backref->inum;
2314 key.type = BTRFS_EXTENT_DATA_KEY;
2315 key.offset = backref->file_pos;
2316
2317 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2318 if (ret < 0) {
2319 goto out_free_path;
2320 } else if (ret > 0) {
2321 ret = 0;
2322 goto out_free_path;
2323 }
2324
2325 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2326 struct btrfs_file_extent_item);
2327
2328 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2329 backref->generation)
2330 goto out_free_path;
2331
2332 btrfs_release_path(path);
2333
2334 start = backref->file_pos;
2335 if (backref->extent_offset < old->extent_offset + old->offset)
2336 start += old->extent_offset + old->offset -
2337 backref->extent_offset;
2338
2339 len = min(backref->extent_offset + backref->num_bytes,
2340 old->extent_offset + old->offset + old->len);
2341 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2342
2343 ret = btrfs_drop_extents(trans, root, inode, start,
2344 start + len, 1);
2345 if (ret)
2346 goto out_free_path;
2347 again:
2348 key.objectid = btrfs_ino(inode);
2349 key.type = BTRFS_EXTENT_DATA_KEY;
2350 key.offset = start;
2351
2352 path->leave_spinning = 1;
2353 if (merge) {
2354 struct btrfs_file_extent_item *fi;
2355 u64 extent_len;
2356 struct btrfs_key found_key;
2357
2358 ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
2359 if (ret < 0)
2360 goto out_free_path;
2361
2362 path->slots[0]--;
2363 leaf = path->nodes[0];
2364 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2365
2366 fi = btrfs_item_ptr(leaf, path->slots[0],
2367 struct btrfs_file_extent_item);
2368 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2369
2370 if (extent_len + found_key.offset == start &&
2371 relink_is_mergable(leaf, fi, new)) {
2372 btrfs_set_file_extent_num_bytes(leaf, fi,
2373 extent_len + len);
2374 btrfs_mark_buffer_dirty(leaf);
2375 inode_add_bytes(inode, len);
2376
2377 ret = 1;
2378 goto out_free_path;
2379 } else {
2380 merge = false;
2381 btrfs_release_path(path);
2382 goto again;
2383 }
2384 }
2385
2386 ret = btrfs_insert_empty_item(trans, root, path, &key,
2387 sizeof(*extent));
2388 if (ret) {
2389 btrfs_abort_transaction(trans, root, ret);
2390 goto out_free_path;
2391 }
2392
2393 leaf = path->nodes[0];
2394 item = btrfs_item_ptr(leaf, path->slots[0],
2395 struct btrfs_file_extent_item);
2396 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2397 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2398 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2399 btrfs_set_file_extent_num_bytes(leaf, item, len);
2400 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2401 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2402 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2403 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2404 btrfs_set_file_extent_encryption(leaf, item, 0);
2405 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2406
2407 btrfs_mark_buffer_dirty(leaf);
2408 inode_add_bytes(inode, len);
2409 btrfs_release_path(path);
2410
2411 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2412 new->disk_len, 0,
2413 backref->root_id, backref->inum,
2414 new->file_pos, 0); /* start - extent_offset */
2415 if (ret) {
2416 btrfs_abort_transaction(trans, root, ret);
2417 goto out_free_path;
2418 }
2419
2420 ret = 1;
2421 out_free_path:
2422 btrfs_release_path(path);
2423 path->leave_spinning = 0;
2424 btrfs_end_transaction(trans, root);
2425 out_unlock:
2426 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2427 &cached, GFP_NOFS);
2428 iput(inode);
2429 return ret;
2430 }
2431
2432 static void relink_file_extents(struct new_sa_defrag_extent *new)
2433 {
2434 struct btrfs_path *path;
2435 struct old_sa_defrag_extent *old, *tmp;
2436 struct sa_defrag_extent_backref *backref;
2437 struct sa_defrag_extent_backref *prev = NULL;
2438 struct inode *inode;
2439 struct btrfs_root *root;
2440 struct rb_node *node;
2441 int ret;
2442
2443 inode = new->inode;
2444 root = BTRFS_I(inode)->root;
2445
2446 path = btrfs_alloc_path();
2447 if (!path)
2448 return;
2449
2450 if (!record_extent_backrefs(path, new)) {
2451 btrfs_free_path(path);
2452 goto out;
2453 }
2454 btrfs_release_path(path);
2455
2456 while (1) {
2457 node = rb_first(&new->root);
2458 if (!node)
2459 break;
2460 rb_erase(node, &new->root);
2461
2462 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2463
2464 ret = relink_extent_backref(path, prev, backref);
2465 WARN_ON(ret < 0);
2466
2467 kfree(prev);
2468
2469 if (ret == 1)
2470 prev = backref;
2471 else
2472 prev = NULL;
2473 cond_resched();
2474 }
2475 kfree(prev);
2476
2477 btrfs_free_path(path);
2478
2479 list_for_each_entry_safe(old, tmp, &new->head, list) {
2480 list_del(&old->list);
2481 kfree(old);
2482 }
2483 out:
2484 atomic_dec(&root->fs_info->defrag_running);
2485 wake_up(&root->fs_info->transaction_wait);
2486
2487 kfree(new);
2488 }
2489
2490 static struct new_sa_defrag_extent *
2491 record_old_file_extents(struct inode *inode,
2492 struct btrfs_ordered_extent *ordered)
2493 {
2494 struct btrfs_root *root = BTRFS_I(inode)->root;
2495 struct btrfs_path *path;
2496 struct btrfs_key key;
2497 struct old_sa_defrag_extent *old, *tmp;
2498 struct new_sa_defrag_extent *new;
2499 int ret;
2500
2501 new = kmalloc(sizeof(*new), GFP_NOFS);
2502 if (!new)
2503 return NULL;
2504
2505 new->inode = inode;
2506 new->file_pos = ordered->file_offset;
2507 new->len = ordered->len;
2508 new->bytenr = ordered->start;
2509 new->disk_len = ordered->disk_len;
2510 new->compress_type = ordered->compress_type;
2511 new->root = RB_ROOT;
2512 INIT_LIST_HEAD(&new->head);
2513
2514 path = btrfs_alloc_path();
2515 if (!path)
2516 goto out_kfree;
2517
2518 key.objectid = btrfs_ino(inode);
2519 key.type = BTRFS_EXTENT_DATA_KEY;
2520 key.offset = new->file_pos;
2521
2522 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2523 if (ret < 0)
2524 goto out_free_path;
2525 if (ret > 0 && path->slots[0] > 0)
2526 path->slots[0]--;
2527
2528 /* find out all the old extents for the file range */
2529 while (1) {
2530 struct btrfs_file_extent_item *extent;
2531 struct extent_buffer *l;
2532 int slot;
2533 u64 num_bytes;
2534 u64 offset;
2535 u64 end;
2536 u64 disk_bytenr;
2537 u64 extent_offset;
2538
2539 l = path->nodes[0];
2540 slot = path->slots[0];
2541
2542 if (slot >= btrfs_header_nritems(l)) {
2543 ret = btrfs_next_leaf(root, path);
2544 if (ret < 0)
2545 goto out_free_list;
2546 else if (ret > 0)
2547 break;
2548 continue;
2549 }
2550
2551 btrfs_item_key_to_cpu(l, &key, slot);
2552
2553 if (key.objectid != btrfs_ino(inode))
2554 break;
2555 if (key.type != BTRFS_EXTENT_DATA_KEY)
2556 break;
2557 if (key.offset >= new->file_pos + new->len)
2558 break;
2559
2560 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2561
2562 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2563 if (key.offset + num_bytes < new->file_pos)
2564 goto next;
2565
2566 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2567 if (!disk_bytenr)
2568 goto next;
2569
2570 extent_offset = btrfs_file_extent_offset(l, extent);
2571
2572 old = kmalloc(sizeof(*old), GFP_NOFS);
2573 if (!old)
2574 goto out_free_list;
2575
2576 offset = max(new->file_pos, key.offset);
2577 end = min(new->file_pos + new->len, key.offset + num_bytes);
2578
2579 old->bytenr = disk_bytenr;
2580 old->extent_offset = extent_offset;
2581 old->offset = offset - key.offset;
2582 old->len = end - offset;
2583 old->new = new;
2584 old->count = 0;
2585 list_add_tail(&old->list, &new->head);
2586 next:
2587 path->slots[0]++;
2588 cond_resched();
2589 }
2590
2591 btrfs_free_path(path);
2592 atomic_inc(&root->fs_info->defrag_running);
2593
2594 return new;
2595
2596 out_free_list:
2597 list_for_each_entry_safe(old, tmp, &new->head, list) {
2598 list_del(&old->list);
2599 kfree(old);
2600 }
2601 out_free_path:
2602 btrfs_free_path(path);
2603 out_kfree:
2604 kfree(new);
2605 return NULL;
2606 }
2607
2608 /*
2609 * helper function for btrfs_finish_ordered_io, this
2610 * just reads in some of the csum leaves to prime them into ram
2611 * before we start the transaction. It limits the amount of btree
2612 * reads required while inside the transaction.
2613 */
2614 /* as ordered data IO finishes, this gets called so we can finish
2615 * an ordered extent if the range of bytes in the file it covers are
2616 * fully written.
2617 */
2618 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
2619 {
2620 struct inode *inode = ordered_extent->inode;
2621 struct btrfs_root *root = BTRFS_I(inode)->root;
2622 struct btrfs_trans_handle *trans = NULL;
2623 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2624 struct extent_state *cached_state = NULL;
2625 struct new_sa_defrag_extent *new = NULL;
2626 int compress_type = 0;
2627 int ret;
2628 bool nolock;
2629
2630 nolock = btrfs_is_free_space_inode(inode);
2631
2632 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2633 ret = -EIO;
2634 goto out;
2635 }
2636
2637 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
2638 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
2639 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2640 if (nolock)
2641 trans = btrfs_join_transaction_nolock(root);
2642 else
2643 trans = btrfs_join_transaction(root);
2644 if (IS_ERR(trans)) {
2645 ret = PTR_ERR(trans);
2646 trans = NULL;
2647 goto out;
2648 }
2649 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2650 ret = btrfs_update_inode_fallback(trans, root, inode);
2651 if (ret) /* -ENOMEM or corruption */
2652 btrfs_abort_transaction(trans, root, ret);
2653 goto out;
2654 }
2655
2656 lock_extent_bits(io_tree, ordered_extent->file_offset,
2657 ordered_extent->file_offset + ordered_extent->len - 1,
2658 0, &cached_state);
2659
2660 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2661 ordered_extent->file_offset + ordered_extent->len - 1,
2662 EXTENT_DEFRAG, 1, cached_state);
2663 if (ret) {
2664 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
2665 if (last_snapshot >= BTRFS_I(inode)->generation)
2666 /* the inode is shared */
2667 new = record_old_file_extents(inode, ordered_extent);
2668
2669 clear_extent_bit(io_tree, ordered_extent->file_offset,
2670 ordered_extent->file_offset + ordered_extent->len - 1,
2671 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2672 }
2673
2674 if (nolock)
2675 trans = btrfs_join_transaction_nolock(root);
2676 else
2677 trans = btrfs_join_transaction(root);
2678 if (IS_ERR(trans)) {
2679 ret = PTR_ERR(trans);
2680 trans = NULL;
2681 goto out_unlock;
2682 }
2683 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
2684
2685 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
2686 compress_type = ordered_extent->compress_type;
2687 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
2688 BUG_ON(compress_type);
2689 ret = btrfs_mark_extent_written(trans, inode,
2690 ordered_extent->file_offset,
2691 ordered_extent->file_offset +
2692 ordered_extent->len);
2693 } else {
2694 BUG_ON(root == root->fs_info->tree_root);
2695 ret = insert_reserved_file_extent(trans, inode,
2696 ordered_extent->file_offset,
2697 ordered_extent->start,
2698 ordered_extent->disk_len,
2699 ordered_extent->len,
2700 ordered_extent->len,
2701 compress_type, 0, 0,
2702 BTRFS_FILE_EXTENT_REG);
2703 }
2704 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2705 ordered_extent->file_offset, ordered_extent->len,
2706 trans->transid);
2707 if (ret < 0) {
2708 btrfs_abort_transaction(trans, root, ret);
2709 goto out_unlock;
2710 }
2711
2712 add_pending_csums(trans, inode, ordered_extent->file_offset,
2713 &ordered_extent->list);
2714
2715 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2716 ret = btrfs_update_inode_fallback(trans, root, inode);
2717 if (ret) { /* -ENOMEM or corruption */
2718 btrfs_abort_transaction(trans, root, ret);
2719 goto out_unlock;
2720 }
2721 ret = 0;
2722 out_unlock:
2723 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2724 ordered_extent->file_offset +
2725 ordered_extent->len - 1, &cached_state, GFP_NOFS);
2726 out:
2727 if (root != root->fs_info->tree_root)
2728 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
2729 if (trans)
2730 btrfs_end_transaction(trans, root);
2731
2732 if (ret) {
2733 clear_extent_uptodate(io_tree, ordered_extent->file_offset,
2734 ordered_extent->file_offset +
2735 ordered_extent->len - 1, NULL, GFP_NOFS);
2736
2737 /*
2738 * If the ordered extent had an IOERR or something else went
2739 * wrong we need to return the space for this ordered extent
2740 * back to the allocator.
2741 */
2742 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2743 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2744 btrfs_free_reserved_extent(root, ordered_extent->start,
2745 ordered_extent->disk_len);
2746 }
2747
2748
2749 /*
2750 * This needs to be done to make sure anybody waiting knows we are done
2751 * updating everything for this ordered extent.
2752 */
2753 btrfs_remove_ordered_extent(inode, ordered_extent);
2754
2755 /* for snapshot-aware defrag */
2756 if (new)
2757 relink_file_extents(new);
2758
2759 /* once for us */
2760 btrfs_put_ordered_extent(ordered_extent);
2761 /* once for the tree */
2762 btrfs_put_ordered_extent(ordered_extent);
2763
2764 return ret;
2765 }
2766
2767 static void finish_ordered_fn(struct btrfs_work *work)
2768 {
2769 struct btrfs_ordered_extent *ordered_extent;
2770 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2771 btrfs_finish_ordered_io(ordered_extent);
2772 }
2773
2774 static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
2775 struct extent_state *state, int uptodate)
2776 {
2777 struct inode *inode = page->mapping->host;
2778 struct btrfs_root *root = BTRFS_I(inode)->root;
2779 struct btrfs_ordered_extent *ordered_extent = NULL;
2780 struct btrfs_workers *workers;
2781
2782 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2783
2784 ClearPagePrivate2(page);
2785 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2786 end - start + 1, uptodate))
2787 return 0;
2788
2789 ordered_extent->work.func = finish_ordered_fn;
2790 ordered_extent->work.flags = 0;
2791
2792 if (btrfs_is_free_space_inode(inode))
2793 workers = &root->fs_info->endio_freespace_worker;
2794 else
2795 workers = &root->fs_info->endio_write_workers;
2796 btrfs_queue_worker(workers, &ordered_extent->work);
2797
2798 return 0;
2799 }
2800
2801 /*
2802 * when reads are done, we need to check csums to verify the data is correct
2803 * if there's a match, we allow the bio to finish. If not, the code in
2804 * extent_io.c will try to find good copies for us.
2805 */
2806 static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
2807 u64 phy_offset, struct page *page,
2808 u64 start, u64 end, int mirror)
2809 {
2810 size_t offset = start - page_offset(page);
2811 struct inode *inode = page->mapping->host;
2812 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2813 char *kaddr;
2814 struct btrfs_root *root = BTRFS_I(inode)->root;
2815 u32 csum_expected;
2816 u32 csum = ~(u32)0;
2817 static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
2818 DEFAULT_RATELIMIT_BURST);
2819
2820 if (PageChecked(page)) {
2821 ClearPageChecked(page);
2822 goto good;
2823 }
2824
2825 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
2826 goto good;
2827
2828 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
2829 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
2830 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2831 GFP_NOFS);
2832 return 0;
2833 }
2834
2835 phy_offset >>= inode->i_sb->s_blocksize_bits;
2836 csum_expected = *(((u32 *)io_bio->csum) + phy_offset);
2837
2838 kaddr = kmap_atomic(page);
2839 csum = btrfs_csum_data(kaddr + offset, csum, end - start + 1);
2840 btrfs_csum_final(csum, (char *)&csum);
2841 if (csum != csum_expected)
2842 goto zeroit;
2843
2844 kunmap_atomic(kaddr);
2845 good:
2846 return 0;
2847
2848 zeroit:
2849 if (__ratelimit(&_rs))
2850 btrfs_info(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u",
2851 (unsigned long long)btrfs_ino(page->mapping->host),
2852 (unsigned long long)start, csum, csum_expected);
2853 memset(kaddr + offset, 1, end - start + 1);
2854 flush_dcache_page(page);
2855 kunmap_atomic(kaddr);
2856 if (csum_expected == 0)
2857 return 0;
2858 return -EIO;
2859 }
2860
2861 struct delayed_iput {
2862 struct list_head list;
2863 struct inode *inode;
2864 };
2865
2866 /* JDM: If this is fs-wide, why can't we add a pointer to
2867 * btrfs_inode instead and avoid the allocation? */
2868 void btrfs_add_delayed_iput(struct inode *inode)
2869 {
2870 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2871 struct delayed_iput *delayed;
2872
2873 if (atomic_add_unless(&inode->i_count, -1, 1))
2874 return;
2875
2876 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2877 delayed->inode = inode;
2878
2879 spin_lock(&fs_info->delayed_iput_lock);
2880 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2881 spin_unlock(&fs_info->delayed_iput_lock);
2882 }
2883
2884 void btrfs_run_delayed_iputs(struct btrfs_root *root)
2885 {
2886 LIST_HEAD(list);
2887 struct btrfs_fs_info *fs_info = root->fs_info;
2888 struct delayed_iput *delayed;
2889 int empty;
2890
2891 spin_lock(&fs_info->delayed_iput_lock);
2892 empty = list_empty(&fs_info->delayed_iputs);
2893 spin_unlock(&fs_info->delayed_iput_lock);
2894 if (empty)
2895 return;
2896
2897 spin_lock(&fs_info->delayed_iput_lock);
2898 list_splice_init(&fs_info->delayed_iputs, &list);
2899 spin_unlock(&fs_info->delayed_iput_lock);
2900
2901 while (!list_empty(&list)) {
2902 delayed = list_entry(list.next, struct delayed_iput, list);
2903 list_del(&delayed->list);
2904 iput(delayed->inode);
2905 kfree(delayed);
2906 }
2907 }
2908
2909 /*
2910 * This is called in transaction commit time. If there are no orphan
2911 * files in the subvolume, it removes orphan item and frees block_rsv
2912 * structure.
2913 */
2914 void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2915 struct btrfs_root *root)
2916 {
2917 struct btrfs_block_rsv *block_rsv;
2918 int ret;
2919
2920 if (atomic_read(&root->orphan_inodes) ||
2921 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2922 return;
2923
2924 spin_lock(&root->orphan_lock);
2925 if (atomic_read(&root->orphan_inodes)) {
2926 spin_unlock(&root->orphan_lock);
2927 return;
2928 }
2929
2930 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2931 spin_unlock(&root->orphan_lock);
2932 return;
2933 }
2934
2935 block_rsv = root->orphan_block_rsv;
2936 root->orphan_block_rsv = NULL;
2937 spin_unlock(&root->orphan_lock);
2938
2939 if (root->orphan_item_inserted &&
2940 btrfs_root_refs(&root->root_item) > 0) {
2941 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2942 root->root_key.objectid);
2943 if (ret)
2944 btrfs_abort_transaction(trans, root, ret);
2945 else
2946 root->orphan_item_inserted = 0;
2947 }
2948
2949 if (block_rsv) {
2950 WARN_ON(block_rsv->size > 0);
2951 btrfs_free_block_rsv(root, block_rsv);
2952 }
2953 }
2954
2955 /*
2956 * This creates an orphan entry for the given inode in case something goes
2957 * wrong in the middle of an unlink/truncate.
2958 *
2959 * NOTE: caller of this function should reserve 5 units of metadata for
2960 * this function.
2961 */
2962 int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2963 {
2964 struct btrfs_root *root = BTRFS_I(inode)->root;
2965 struct btrfs_block_rsv *block_rsv = NULL;
2966 int reserve = 0;
2967 int insert = 0;
2968 int ret;
2969
2970 if (!root->orphan_block_rsv) {
2971 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
2972 if (!block_rsv)
2973 return -ENOMEM;
2974 }
2975
2976 spin_lock(&root->orphan_lock);
2977 if (!root->orphan_block_rsv) {
2978 root->orphan_block_rsv = block_rsv;
2979 } else if (block_rsv) {
2980 btrfs_free_block_rsv(root, block_rsv);
2981 block_rsv = NULL;
2982 }
2983
2984 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2985 &BTRFS_I(inode)->runtime_flags)) {
2986 #if 0
2987 /*
2988 * For proper ENOSPC handling, we should do orphan
2989 * cleanup when mounting. But this introduces backward
2990 * compatibility issue.
2991 */
2992 if (!xchg(&root->orphan_item_inserted, 1))
2993 insert = 2;
2994 else
2995 insert = 1;
2996 #endif
2997 insert = 1;
2998 atomic_inc(&root->orphan_inodes);
2999 }
3000
3001 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3002 &BTRFS_I(inode)->runtime_flags))
3003 reserve = 1;
3004 spin_unlock(&root->orphan_lock);
3005
3006 /* grab metadata reservation from transaction handle */
3007 if (reserve) {
3008 ret = btrfs_orphan_reserve_metadata(trans, inode);
3009 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
3010 }
3011
3012 /* insert an orphan item to track this unlinked/truncated file */
3013 if (insert >= 1) {
3014 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
3015 if (ret) {
3016 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3017 &BTRFS_I(inode)->runtime_flags);
3018 if (reserve) {
3019 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3020 &BTRFS_I(inode)->runtime_flags);
3021 btrfs_orphan_release_metadata(inode);
3022 }
3023 if (ret != -EEXIST) {
3024 btrfs_abort_transaction(trans, root, ret);
3025 return ret;
3026 }
3027 }
3028 ret = 0;
3029 }
3030
3031 /* insert an orphan item to track subvolume contains orphan files */
3032 if (insert >= 2) {
3033 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
3034 root->root_key.objectid);
3035 if (ret && ret != -EEXIST) {
3036 btrfs_abort_transaction(trans, root, ret);
3037 return ret;
3038 }
3039 }
3040 return 0;
3041 }
3042
3043 /*
3044 * We have done the truncate/delete so we can go ahead and remove the orphan
3045 * item for this particular inode.
3046 */
3047 static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3048 struct inode *inode)
3049 {
3050 struct btrfs_root *root = BTRFS_I(inode)->root;
3051 int delete_item = 0;
3052 int release_rsv = 0;
3053 int ret = 0;
3054
3055 spin_lock(&root->orphan_lock);
3056 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3057 &BTRFS_I(inode)->runtime_flags))
3058 delete_item = 1;
3059
3060 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3061 &BTRFS_I(inode)->runtime_flags))
3062 release_rsv = 1;
3063 spin_unlock(&root->orphan_lock);
3064
3065 if (trans && delete_item)
3066 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
3067
3068 if (release_rsv) {
3069 btrfs_orphan_release_metadata(inode);
3070 atomic_dec(&root->orphan_inodes);
3071 }
3072
3073 return ret;
3074 }
3075
3076 /*
3077 * this cleans up any orphans that may be left on the list from the last use
3078 * of this root.
3079 */
3080 int btrfs_orphan_cleanup(struct btrfs_root *root)
3081 {
3082 struct btrfs_path *path;
3083 struct extent_buffer *leaf;
3084 struct btrfs_key key, found_key;
3085 struct btrfs_trans_handle *trans;
3086 struct inode *inode;
3087 u64 last_objectid = 0;
3088 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3089
3090 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
3091 return 0;
3092
3093 path = btrfs_alloc_path();
3094 if (!path) {
3095 ret = -ENOMEM;
3096 goto out;
3097 }
3098 path->reada = -1;
3099
3100 key.objectid = BTRFS_ORPHAN_OBJECTID;
3101 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
3102 key.offset = (u64)-1;
3103
3104 while (1) {
3105 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3106 if (ret < 0)
3107 goto out;
3108
3109 /*
3110 * if ret == 0 means we found what we were searching for, which
3111 * is weird, but possible, so only screw with path if we didn't
3112 * find the key and see if we have stuff that matches
3113 */
3114 if (ret > 0) {
3115 ret = 0;
3116 if (path->slots[0] == 0)
3117 break;
3118 path->slots[0]--;
3119 }
3120
3121 /* pull out the item */
3122 leaf = path->nodes[0];
3123 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3124
3125 /* make sure the item matches what we want */
3126 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3127 break;
3128 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
3129 break;
3130
3131 /* release the path since we're done with it */
3132 btrfs_release_path(path);
3133
3134 /*
3135 * this is where we are basically btrfs_lookup, without the
3136 * crossing root thing. we store the inode number in the
3137 * offset of the orphan item.
3138 */
3139
3140 if (found_key.offset == last_objectid) {
3141 btrfs_err(root->fs_info,
3142 "Error removing orphan entry, stopping orphan cleanup");
3143 ret = -EINVAL;
3144 goto out;
3145 }
3146
3147 last_objectid = found_key.offset;
3148
3149 found_key.objectid = found_key.offset;
3150 found_key.type = BTRFS_INODE_ITEM_KEY;
3151 found_key.offset = 0;
3152 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
3153 ret = PTR_RET(inode);
3154 if (ret && ret != -ESTALE)
3155 goto out;
3156
3157 if (ret == -ESTALE && root == root->fs_info->tree_root) {
3158 struct btrfs_root *dead_root;
3159 struct btrfs_fs_info *fs_info = root->fs_info;
3160 int is_dead_root = 0;
3161
3162 /*
3163 * this is an orphan in the tree root. Currently these
3164 * could come from 2 sources:
3165 * a) a snapshot deletion in progress
3166 * b) a free space cache inode
3167 * We need to distinguish those two, as the snapshot
3168 * orphan must not get deleted.
3169 * find_dead_roots already ran before us, so if this
3170 * is a snapshot deletion, we should find the root
3171 * in the dead_roots list
3172 */
3173 spin_lock(&fs_info->trans_lock);
3174 list_for_each_entry(dead_root, &fs_info->dead_roots,
3175 root_list) {
3176 if (dead_root->root_key.objectid ==
3177 found_key.objectid) {
3178 is_dead_root = 1;
3179 break;
3180 }
3181 }
3182 spin_unlock(&fs_info->trans_lock);
3183 if (is_dead_root) {
3184 /* prevent this orphan from being found again */
3185 key.offset = found_key.objectid - 1;
3186 continue;
3187 }
3188 }
3189 /*
3190 * Inode is already gone but the orphan item is still there,
3191 * kill the orphan item.
3192 */
3193 if (ret == -ESTALE) {
3194 trans = btrfs_start_transaction(root, 1);
3195 if (IS_ERR(trans)) {
3196 ret = PTR_ERR(trans);
3197 goto out;
3198 }
3199 btrfs_debug(root->fs_info, "auto deleting %Lu",
3200 found_key.objectid);
3201 ret = btrfs_del_orphan_item(trans, root,
3202 found_key.objectid);
3203 btrfs_end_transaction(trans, root);
3204 if (ret)
3205 goto out;
3206 continue;
3207 }
3208
3209 /*
3210 * add this inode to the orphan list so btrfs_orphan_del does
3211 * the proper thing when we hit it
3212 */
3213 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3214 &BTRFS_I(inode)->runtime_flags);
3215 atomic_inc(&root->orphan_inodes);
3216
3217 /* if we have links, this was a truncate, lets do that */
3218 if (inode->i_nlink) {
3219 if (!S_ISREG(inode->i_mode)) {
3220 WARN_ON(1);
3221 iput(inode);
3222 continue;
3223 }
3224 nr_truncate++;
3225
3226 /* 1 for the orphan item deletion. */
3227 trans = btrfs_start_transaction(root, 1);
3228 if (IS_ERR(trans)) {
3229 iput(inode);
3230 ret = PTR_ERR(trans);
3231 goto out;
3232 }
3233 ret = btrfs_orphan_add(trans, inode);
3234 btrfs_end_transaction(trans, root);
3235 if (ret) {
3236 iput(inode);
3237 goto out;
3238 }
3239
3240 ret = btrfs_truncate(inode);
3241 if (ret)
3242 btrfs_orphan_del(NULL, inode);
3243 } else {
3244 nr_unlink++;
3245 }
3246
3247 /* this will do delete_inode and everything for us */
3248 iput(inode);
3249 if (ret)
3250 goto out;
3251 }
3252 /* release the path since we're done with it */
3253 btrfs_release_path(path);
3254
3255 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3256
3257 if (root->orphan_block_rsv)
3258 btrfs_block_rsv_release(root, root->orphan_block_rsv,
3259 (u64)-1);
3260
3261 if (root->orphan_block_rsv || root->orphan_item_inserted) {
3262 trans = btrfs_join_transaction(root);
3263 if (!IS_ERR(trans))
3264 btrfs_end_transaction(trans, root);
3265 }
3266
3267 if (nr_unlink)
3268 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink);
3269 if (nr_truncate)
3270 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate);
3271
3272 out:
3273 if (ret)
3274 btrfs_crit(root->fs_info,
3275 "could not do orphan cleanup %d", ret);
3276 btrfs_free_path(path);
3277 return ret;
3278 }
3279
3280 /*
3281 * very simple check to peek ahead in the leaf looking for xattrs. If we
3282 * don't find any xattrs, we know there can't be any acls.
3283 *
3284 * slot is the slot the inode is in, objectid is the objectid of the inode
3285 */
3286 static noinline int acls_after_inode_item(struct extent_buffer *leaf,
3287 int slot, u64 objectid)
3288 {
3289 u32 nritems = btrfs_header_nritems(leaf);
3290 struct btrfs_key found_key;
3291 static u64 xattr_access = 0;
3292 static u64 xattr_default = 0;
3293 int scanned = 0;
3294
3295 if (!xattr_access) {
3296 xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS,
3297 strlen(POSIX_ACL_XATTR_ACCESS));
3298 xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT,
3299 strlen(POSIX_ACL_XATTR_DEFAULT));
3300 }
3301
3302 slot++;
3303 while (slot < nritems) {
3304 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3305
3306 /* we found a different objectid, there must not be acls */
3307 if (found_key.objectid != objectid)
3308 return 0;
3309
3310 /* we found an xattr, assume we've got an acl */
3311 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
3312 if (found_key.offset == xattr_access ||
3313 found_key.offset == xattr_default)
3314 return 1;
3315 }
3316
3317 /*
3318 * we found a key greater than an xattr key, there can't
3319 * be any acls later on
3320 */
3321 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3322 return 0;
3323
3324 slot++;
3325 scanned++;
3326
3327 /*
3328 * it goes inode, inode backrefs, xattrs, extents,
3329 * so if there are a ton of hard links to an inode there can
3330 * be a lot of backrefs. Don't waste time searching too hard,
3331 * this is just an optimization
3332 */
3333 if (scanned >= 8)
3334 break;
3335 }
3336 /* we hit the end of the leaf before we found an xattr or
3337 * something larger than an xattr. We have to assume the inode
3338 * has acls
3339 */
3340 return 1;
3341 }
3342
3343 /*
3344 * read an inode from the btree into the in-memory inode
3345 */
3346 static void btrfs_read_locked_inode(struct inode *inode)
3347 {
3348 struct btrfs_path *path;
3349 struct extent_buffer *leaf;
3350 struct btrfs_inode_item *inode_item;
3351 struct btrfs_timespec *tspec;
3352 struct btrfs_root *root = BTRFS_I(inode)->root;
3353 struct btrfs_key location;
3354 int maybe_acls;
3355 u32 rdev;
3356 int ret;
3357 bool filled = false;
3358
3359 ret = btrfs_fill_inode(inode, &rdev);
3360 if (!ret)
3361 filled = true;
3362
3363 path = btrfs_alloc_path();
3364 if (!path)
3365 goto make_bad;
3366
3367 path->leave_spinning = 1;
3368 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
3369
3370 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
3371 if (ret)
3372 goto make_bad;
3373
3374 leaf = path->nodes[0];
3375
3376 if (filled)
3377 goto cache_acl;
3378
3379 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3380 struct btrfs_inode_item);
3381 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
3382 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
3383 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3384 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
3385 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
3386
3387 tspec = btrfs_inode_atime(inode_item);
3388 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3389 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3390
3391 tspec = btrfs_inode_mtime(inode_item);
3392 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3393 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3394
3395 tspec = btrfs_inode_ctime(inode_item);
3396 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
3397 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
3398
3399 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
3400 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
3401 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3402
3403 /*
3404 * If we were modified in the current generation and evicted from memory
3405 * and then re-read we need to do a full sync since we don't have any
3406 * idea about which extents were modified before we were evicted from
3407 * cache.
3408 */
3409 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
3410 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3411 &BTRFS_I(inode)->runtime_flags);
3412
3413 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
3414 inode->i_generation = BTRFS_I(inode)->generation;
3415 inode->i_rdev = 0;
3416 rdev = btrfs_inode_rdev(leaf, inode_item);
3417
3418 BTRFS_I(inode)->index_cnt = (u64)-1;
3419 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
3420 cache_acl:
3421 /*
3422 * try to precache a NULL acl entry for files that don't have
3423 * any xattrs or acls
3424 */
3425 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
3426 btrfs_ino(inode));
3427 if (!maybe_acls)
3428 cache_no_acl(inode);
3429
3430 btrfs_free_path(path);
3431
3432 switch (inode->i_mode & S_IFMT) {
3433 case S_IFREG:
3434 inode->i_mapping->a_ops = &btrfs_aops;
3435 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
3436 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
3437 inode->i_fop = &btrfs_file_operations;
3438 inode->i_op = &btrfs_file_inode_operations;
3439 break;
3440 case S_IFDIR:
3441 inode->i_fop = &btrfs_dir_file_operations;
3442 if (root == root->fs_info->tree_root)
3443 inode->i_op = &btrfs_dir_ro_inode_operations;
3444 else
3445 inode->i_op = &btrfs_dir_inode_operations;
3446 break;
3447 case S_IFLNK:
3448 inode->i_op = &btrfs_symlink_inode_operations;
3449 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3450 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
3451 break;
3452 default:
3453 inode->i_op = &btrfs_special_inode_operations;
3454 init_special_inode(inode, inode->i_mode, rdev);
3455 break;
3456 }
3457
3458 btrfs_update_iflags(inode);
3459 return;
3460
3461 make_bad:
3462 btrfs_free_path(path);
3463 make_bad_inode(inode);
3464 }
3465
3466 /*
3467 * given a leaf and an inode, copy the inode fields into the leaf
3468 */
3469 static void fill_inode_item(struct btrfs_trans_handle *trans,
3470 struct extent_buffer *leaf,
3471 struct btrfs_inode_item *item,
3472 struct inode *inode)
3473 {
3474 struct btrfs_map_token token;
3475
3476 btrfs_init_map_token(&token);
3477
3478 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3479 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3480 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3481 &token);
3482 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3483 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
3484
3485 btrfs_set_token_timespec_sec(leaf, btrfs_inode_atime(item),
3486 inode->i_atime.tv_sec, &token);
3487 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_atime(item),
3488 inode->i_atime.tv_nsec, &token);
3489
3490 btrfs_set_token_timespec_sec(leaf, btrfs_inode_mtime(item),
3491 inode->i_mtime.tv_sec, &token);
3492 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_mtime(item),
3493 inode->i_mtime.tv_nsec, &token);
3494
3495 btrfs_set_token_timespec_sec(leaf, btrfs_inode_ctime(item),
3496 inode->i_ctime.tv_sec, &token);
3497 btrfs_set_token_timespec_nsec(leaf, btrfs_inode_ctime(item),
3498 inode->i_ctime.tv_nsec, &token);
3499
3500 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3501 &token);
3502 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3503 &token);
3504 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3505 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3506 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3507 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3508 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
3509 }
3510
3511 /*
3512 * copy everything in the in-memory inode into the btree.
3513 */
3514 static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
3515 struct btrfs_root *root, struct inode *inode)
3516 {
3517 struct btrfs_inode_item *inode_item;
3518 struct btrfs_path *path;
3519 struct extent_buffer *leaf;
3520 int ret;
3521
3522 path = btrfs_alloc_path();
3523 if (!path)
3524 return -ENOMEM;
3525
3526 path->leave_spinning = 1;
3527 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3528 1);
3529 if (ret) {
3530 if (ret > 0)
3531 ret = -ENOENT;
3532 goto failed;
3533 }
3534
3535 btrfs_unlock_up_safe(path, 1);
3536 leaf = path->nodes[0];
3537 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3538 struct btrfs_inode_item);
3539
3540 fill_inode_item(trans, leaf, inode_item, inode);
3541 btrfs_mark_buffer_dirty(leaf);
3542 btrfs_set_inode_last_trans(trans, inode);
3543 ret = 0;
3544 failed:
3545 btrfs_free_path(path);
3546 return ret;
3547 }
3548
3549 /*
3550 * copy everything in the in-memory inode into the btree.
3551 */
3552 noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3553 struct btrfs_root *root, struct inode *inode)
3554 {
3555 int ret;
3556
3557 /*
3558 * If the inode is a free space inode, we can deadlock during commit
3559 * if we put it into the delayed code.
3560 *
3561 * The data relocation inode should also be directly updated
3562 * without delay
3563 */
3564 if (!btrfs_is_free_space_inode(inode)
3565 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
3566 btrfs_update_root_times(trans, root);
3567
3568 ret = btrfs_delayed_update_inode(trans, root, inode);
3569 if (!ret)
3570 btrfs_set_inode_last_trans(trans, inode);
3571 return ret;
3572 }
3573
3574 return btrfs_update_inode_item(trans, root, inode);
3575 }
3576
3577 noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3578 struct btrfs_root *root,
3579 struct inode *inode)
3580 {
3581 int ret;
3582
3583 ret = btrfs_update_inode(trans, root, inode);
3584 if (ret == -ENOSPC)
3585 return btrfs_update_inode_item(trans, root, inode);
3586 return ret;
3587 }
3588
3589 /*
3590 * unlink helper that gets used here in inode.c and in the tree logging
3591 * recovery code. It remove a link in a directory with a given name, and
3592 * also drops the back refs in the inode to the directory
3593 */
3594 static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3595 struct btrfs_root *root,
3596 struct inode *dir, struct inode *inode,
3597 const char *name, int name_len)
3598 {
3599 struct btrfs_path *path;
3600 int ret = 0;
3601 struct extent_buffer *leaf;
3602 struct btrfs_dir_item *di;
3603 struct btrfs_key key;
3604 u64 index;
3605 u64 ino = btrfs_ino(inode);
3606 u64 dir_ino = btrfs_ino(dir);
3607
3608 path = btrfs_alloc_path();
3609 if (!path) {
3610 ret = -ENOMEM;
3611 goto out;
3612 }
3613
3614 path->leave_spinning = 1;
3615 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
3616 name, name_len, -1);
3617 if (IS_ERR(di)) {
3618 ret = PTR_ERR(di);
3619 goto err;
3620 }
3621 if (!di) {
3622 ret = -ENOENT;
3623 goto err;
3624 }
3625 leaf = path->nodes[0];
3626 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3627 ret = btrfs_delete_one_dir_name(trans, root, path, di);
3628 if (ret)
3629 goto err;
3630 btrfs_release_path(path);
3631
3632 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3633 dir_ino, &index);
3634 if (ret) {
3635 btrfs_info(root->fs_info,
3636 "failed to delete reference to %.*s, inode %llu parent %llu",
3637 name_len, name,
3638 (unsigned long long)ino, (unsigned long long)dir_ino);
3639 btrfs_abort_transaction(trans, root, ret);
3640 goto err;
3641 }
3642
3643 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
3644 if (ret) {
3645 btrfs_abort_transaction(trans, root, ret);
3646 goto err;
3647 }
3648
3649 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
3650 inode, dir_ino);
3651 if (ret != 0 && ret != -ENOENT) {
3652 btrfs_abort_transaction(trans, root, ret);
3653 goto err;
3654 }
3655
3656 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
3657 dir, index);
3658 if (ret == -ENOENT)
3659 ret = 0;
3660 else if (ret)
3661 btrfs_abort_transaction(trans, root, ret);
3662 err:
3663 btrfs_free_path(path);
3664 if (ret)
3665 goto out;
3666
3667 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
3668 inode_inc_iversion(inode);
3669 inode_inc_iversion(dir);
3670 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
3671 ret = btrfs_update_inode(trans, root, dir);
3672 out:
3673 return ret;
3674 }
3675
3676 int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3677 struct btrfs_root *root,
3678 struct inode *dir, struct inode *inode,
3679 const char *name, int name_len)
3680 {
3681 int ret;
3682 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3683 if (!ret) {
3684 btrfs_drop_nlink(inode);
3685 ret = btrfs_update_inode(trans, root, inode);
3686 }
3687 return ret;
3688 }
3689
3690 /*
3691 * helper to start transaction for unlink and rmdir.
3692 *
3693 * unlink and rmdir are special in btrfs, they do not always free space, so
3694 * if we cannot make our reservations the normal way try and see if there is
3695 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3696 * allow the unlink to occur.
3697 */
3698 static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
3699 {
3700 struct btrfs_trans_handle *trans;
3701 struct btrfs_root *root = BTRFS_I(dir)->root;
3702 int ret;
3703
3704 /*
3705 * 1 for the possible orphan item
3706 * 1 for the dir item
3707 * 1 for the dir index
3708 * 1 for the inode ref
3709 * 1 for the inode
3710 */
3711 trans = btrfs_start_transaction(root, 5);
3712 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
3713 return trans;
3714
3715 if (PTR_ERR(trans) == -ENOSPC) {
3716 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
3717
3718 trans = btrfs_start_transaction(root, 0);
3719 if (IS_ERR(trans))
3720 return trans;
3721 ret = btrfs_cond_migrate_bytes(root->fs_info,
3722 &root->fs_info->trans_block_rsv,
3723 num_bytes, 5);
3724 if (ret) {
3725 btrfs_end_transaction(trans, root);
3726 return ERR_PTR(ret);
3727 }
3728 trans->block_rsv = &root->fs_info->trans_block_rsv;
3729 trans->bytes_reserved = num_bytes;
3730 }
3731 return trans;
3732 }
3733
3734 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3735 {
3736 struct btrfs_root *root = BTRFS_I(dir)->root;
3737 struct btrfs_trans_handle *trans;
3738 struct inode *inode = dentry->d_inode;
3739 int ret;
3740
3741 trans = __unlink_start_trans(dir);
3742 if (IS_ERR(trans))
3743 return PTR_ERR(trans);
3744
3745 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3746
3747 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3748 dentry->d_name.name, dentry->d_name.len);
3749 if (ret)
3750 goto out;
3751
3752 if (inode->i_nlink == 0) {
3753 ret = btrfs_orphan_add(trans, inode);
3754 if (ret)
3755 goto out;
3756 }
3757
3758 out:
3759 btrfs_end_transaction(trans, root);
3760 btrfs_btree_balance_dirty(root);
3761 return ret;
3762 }
3763
3764 int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3765 struct btrfs_root *root,
3766 struct inode *dir, u64 objectid,
3767 const char *name, int name_len)
3768 {
3769 struct btrfs_path *path;
3770 struct extent_buffer *leaf;
3771 struct btrfs_dir_item *di;
3772 struct btrfs_key key;
3773 u64 index;
3774 int ret;
3775 u64 dir_ino = btrfs_ino(dir);
3776
3777 path = btrfs_alloc_path();
3778 if (!path)
3779 return -ENOMEM;
3780
3781 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
3782 name, name_len, -1);
3783 if (IS_ERR_OR_NULL(di)) {
3784 if (!di)
3785 ret = -ENOENT;
3786 else
3787 ret = PTR_ERR(di);
3788 goto out;
3789 }
3790
3791 leaf = path->nodes[0];
3792 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3793 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3794 ret = btrfs_delete_one_dir_name(trans, root, path, di);
3795 if (ret) {
3796 btrfs_abort_transaction(trans, root, ret);
3797 goto out;
3798 }
3799 btrfs_release_path(path);
3800
3801 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3802 objectid, root->root_key.objectid,
3803 dir_ino, &index, name, name_len);
3804 if (ret < 0) {
3805 if (ret != -ENOENT) {
3806 btrfs_abort_transaction(trans, root, ret);
3807 goto out;
3808 }
3809 di = btrfs_search_dir_index_item(root, path, dir_ino,
3810 name, name_len);
3811 if (IS_ERR_OR_NULL(di)) {
3812 if (!di)
3813 ret = -ENOENT;
3814 else
3815 ret = PTR_ERR(di);
3816 btrfs_abort_transaction(trans, root, ret);
3817 goto out;
3818 }
3819
3820 leaf = path->nodes[0];
3821 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3822 btrfs_release_path(path);
3823 index = key.offset;
3824 }
3825 btrfs_release_path(path);
3826
3827 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
3828 if (ret) {
3829 btrfs_abort_transaction(trans, root, ret);
3830 goto out;
3831 }
3832
3833 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
3834 inode_inc_iversion(dir);
3835 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
3836 ret = btrfs_update_inode_fallback(trans, root, dir);
3837 if (ret)
3838 btrfs_abort_transaction(trans, root, ret);
3839 out:
3840 btrfs_free_path(path);
3841 return ret;
3842 }
3843
3844 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3845 {
3846 struct inode *inode = dentry->d_inode;
3847 int err = 0;
3848 struct btrfs_root *root = BTRFS_I(dir)->root;
3849 struct btrfs_trans_handle *trans;
3850
3851 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
3852 return -ENOTEMPTY;
3853 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3854 return -EPERM;
3855
3856 trans = __unlink_start_trans(dir);
3857 if (IS_ERR(trans))
3858 return PTR_ERR(trans);
3859
3860 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
3861 err = btrfs_unlink_subvol(trans, root, dir,
3862 BTRFS_I(inode)->location.objectid,
3863 dentry->d_name.name,
3864 dentry->d_name.len);
3865 goto out;
3866 }
3867
3868 err = btrfs_orphan_add(trans, inode);
3869 if (err)
3870 goto out;
3871
3872 /* now the directory is empty */
3873 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3874 dentry->d_name.name, dentry->d_name.len);
3875 if (!err)
3876 btrfs_i_size_write(inode, 0);
3877 out:
3878 btrfs_end_transaction(trans, root);
3879 btrfs_btree_balance_dirty(root);
3880
3881 return err;
3882 }
3883
3884 /*
3885 * this can truncate away extent items, csum items and directory items.
3886 * It starts at a high offset and removes keys until it can't find
3887 * any higher than new_size
3888 *
3889 * csum items that cross the new i_size are truncated to the new size
3890 * as well.
3891 *
3892 * min_type is the minimum key type to truncate down to. If set to 0, this
3893 * will kill all the items on this inode, including the INODE_ITEM_KEY.
3894 */
3895 int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3896 struct btrfs_root *root,
3897 struct inode *inode,
3898 u64 new_size, u32 min_type)
3899 {
3900 struct btrfs_path *path;
3901 struct extent_buffer *leaf;
3902 struct btrfs_file_extent_item *fi;
3903 struct btrfs_key key;
3904 struct btrfs_key found_key;
3905 u64 extent_start = 0;
3906 u64 extent_num_bytes = 0;
3907 u64 extent_offset = 0;
3908 u64 item_end = 0;
3909 u32 found_type = (u8)-1;
3910 int found_extent;
3911 int del_item;
3912 int pending_del_nr = 0;
3913 int pending_del_slot = 0;
3914 int extent_type = -1;
3915 int ret;
3916 int err = 0;
3917 u64 ino = btrfs_ino(inode);
3918
3919 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
3920
3921 path = btrfs_alloc_path();
3922 if (!path)
3923 return -ENOMEM;
3924 path->reada = -1;
3925
3926 /*
3927 * We want to drop from the next block forward in case this new size is
3928 * not block aligned since we will be keeping the last block of the
3929 * extent just the way it is.
3930 */
3931 if (root->ref_cows || root == root->fs_info->tree_root)
3932 btrfs_drop_extent_cache(inode, ALIGN(new_size,
3933 root->sectorsize), (u64)-1, 0);
3934
3935 /*
3936 * This function is also used to drop the items in the log tree before
3937 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3938 * it is used to drop the loged items. So we shouldn't kill the delayed
3939 * items.
3940 */
3941 if (min_type == 0 && root == BTRFS_I(inode)->root)
3942 btrfs_kill_delayed_inode_items(inode);
3943
3944 key.objectid = ino;
3945 key.offset = (u64)-1;
3946 key.type = (u8)-1;
3947
3948 search_again:
3949 path->leave_spinning = 1;
3950 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3951 if (ret < 0) {
3952 err = ret;
3953 goto out;
3954 }
3955
3956 if (ret > 0) {
3957 /* there are no items in the tree for us to truncate, we're
3958 * done
3959 */
3960 if (path->slots[0] == 0)
3961 goto out;
3962 path->slots[0]--;
3963 }
3964
3965 while (1) {
3966 fi = NULL;
3967 leaf = path->nodes[0];
3968 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3969 found_type = btrfs_key_type(&found_key);
3970
3971 if (found_key.objectid != ino)
3972 break;
3973
3974 if (found_type < min_type)
3975 break;
3976
3977 item_end = found_key.offset;
3978 if (found_type == BTRFS_EXTENT_DATA_KEY) {
3979 fi = btrfs_item_ptr(leaf, path->slots[0],
3980 struct btrfs_file_extent_item);
3981 extent_type = btrfs_file_extent_type(leaf, fi);
3982 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
3983 item_end +=
3984 btrfs_file_extent_num_bytes(leaf, fi);
3985 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
3986 item_end += btrfs_file_extent_inline_len(leaf,
3987 fi);
3988 }
3989 item_end--;
3990 }
3991 if (found_type > min_type) {
3992 del_item = 1;
3993 } else {
3994 if (item_end < new_size)
3995 break;
3996 if (found_key.offset >= new_size)
3997 del_item = 1;
3998 else
3999 del_item = 0;
4000 }
4001 found_extent = 0;
4002 /* FIXME, shrink the extent if the ref count is only 1 */
4003 if (found_type != BTRFS_EXTENT_DATA_KEY)
4004 goto delete;
4005
4006 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
4007 u64 num_dec;
4008 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
4009 if (!del_item) {
4010 u64 orig_num_bytes =
4011 btrfs_file_extent_num_bytes(leaf, fi);
4012 extent_num_bytes = ALIGN(new_size -
4013 found_key.offset,
4014 root->sectorsize);
4015 btrfs_set_file_extent_num_bytes(leaf, fi,
4016 extent_num_bytes);
4017 num_dec = (orig_num_bytes -
4018 extent_num_bytes);
4019 if (root->ref_cows && extent_start != 0)
4020 inode_sub_bytes(inode, num_dec);
4021 btrfs_mark_buffer_dirty(leaf);
4022 } else {
4023 extent_num_bytes =
4024 btrfs_file_extent_disk_num_bytes(leaf,
4025 fi);
4026 extent_offset = found_key.offset -
4027 btrfs_file_extent_offset(leaf, fi);
4028
4029 /* FIXME blocksize != 4096 */
4030 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
4031 if (extent_start != 0) {
4032 found_extent = 1;
4033 if (root->ref_cows)
4034 inode_sub_bytes(inode, num_dec);
4035 }
4036 }
4037 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
4038 /*
4039 * we can't truncate inline items that have had
4040 * special encodings
4041 */
4042 if (!del_item &&
4043 btrfs_file_extent_compression(leaf, fi) == 0 &&
4044 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4045 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
4046 u32 size = new_size - found_key.offset;
4047
4048 if (root->ref_cows) {
4049 inode_sub_bytes(inode, item_end + 1 -
4050 new_size);
4051 }
4052 size =
4053 btrfs_file_extent_calc_inline_size(size);
4054 btrfs_truncate_item(root, path, size, 1);
4055 } else if (root->ref_cows) {
4056 inode_sub_bytes(inode, item_end + 1 -
4057 found_key.offset);
4058 }
4059 }
4060 delete:
4061 if (del_item) {
4062 if (!pending_del_nr) {
4063 /* no pending yet, add ourselves */
4064 pending_del_slot = path->slots[0];
4065 pending_del_nr = 1;
4066 } else if (pending_del_nr &&
4067 path->slots[0] + 1 == pending_del_slot) {
4068 /* hop on the pending chunk */
4069 pending_del_nr++;
4070 pending_del_slot = path->slots[0];
4071 } else {
4072 BUG();
4073 }
4074 } else {
4075 break;
4076 }
4077 if (found_extent && (root->ref_cows ||
4078 root == root->fs_info->tree_root)) {
4079 btrfs_set_path_blocking(path);
4080 ret = btrfs_free_extent(trans, root, extent_start,
4081 extent_num_bytes, 0,
4082 btrfs_header_owner(leaf),
4083 ino, extent_offset, 0);
4084 BUG_ON(ret);
4085 }
4086
4087 if (found_type == BTRFS_INODE_ITEM_KEY)
4088 break;
4089
4090 if (path->slots[0] == 0 ||
4091 path->slots[0] != pending_del_slot) {
4092 if (pending_del_nr) {
4093 ret = btrfs_del_items(trans, root, path,
4094 pending_del_slot,
4095 pending_del_nr);
4096 if (ret) {
4097 btrfs_abort_transaction(trans,
4098 root, ret);
4099 goto error;
4100 }
4101 pending_del_nr = 0;
4102 }
4103 btrfs_release_path(path);
4104 goto search_again;
4105 } else {
4106 path->slots[0]--;
4107 }
4108 }
4109 out:
4110 if (pending_del_nr) {
4111 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4112 pending_del_nr);
4113 if (ret)
4114 btrfs_abort_transaction(trans, root, ret);
4115 }
4116 error:
4117 btrfs_free_path(path);
4118 return err;
4119 }
4120
4121 /*
4122 * btrfs_truncate_page - read, zero a chunk and write a page
4123 * @inode - inode that we're zeroing
4124 * @from - the offset to start zeroing
4125 * @len - the length to zero, 0 to zero the entire range respective to the
4126 * offset
4127 * @front - zero up to the offset instead of from the offset on
4128 *
4129 * This will find the page for the "from" offset and cow the page and zero the
4130 * part we want to zero. This is used with truncate and hole punching.
4131 */
4132 int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
4133 int front)
4134 {
4135 struct address_space *mapping = inode->i_mapping;
4136 struct btrfs_root *root = BTRFS_I(inode)->root;
4137 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4138 struct btrfs_ordered_extent *ordered;
4139 struct extent_state *cached_state = NULL;
4140 char *kaddr;
4141 u32 blocksize = root->sectorsize;
4142 pgoff_t index = from >> PAGE_CACHE_SHIFT;
4143 unsigned offset = from & (PAGE_CACHE_SIZE-1);
4144 struct page *page;
4145 gfp_t mask = btrfs_alloc_write_mask(mapping);
4146 int ret = 0;
4147 u64 page_start;
4148 u64 page_end;
4149
4150 if ((offset & (blocksize - 1)) == 0 &&
4151 (!len || ((len & (blocksize - 1)) == 0)))
4152 goto out;
4153 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
4154 if (ret)
4155 goto out;
4156
4157 again:
4158 page = find_or_create_page(mapping, index, mask);
4159 if (!page) {
4160 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
4161 ret = -ENOMEM;
4162 goto out;
4163 }
4164
4165 page_start = page_offset(page);
4166 page_end = page_start + PAGE_CACHE_SIZE - 1;
4167
4168 if (!PageUptodate(page)) {
4169 ret = btrfs_readpage(NULL, page);
4170 lock_page(page);
4171 if (page->mapping != mapping) {
4172 unlock_page(page);
4173 page_cache_release(page);
4174 goto again;
4175 }
4176 if (!PageUptodate(page)) {
4177 ret = -EIO;
4178 goto out_unlock;
4179 }
4180 }
4181 wait_on_page_writeback(page);
4182
4183 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
4184 set_page_extent_mapped(page);
4185
4186 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4187 if (ordered) {
4188 unlock_extent_cached(io_tree, page_start, page_end,
4189 &cached_state, GFP_NOFS);
4190 unlock_page(page);
4191 page_cache_release(page);
4192 btrfs_start_ordered_extent(inode, ordered, 1);
4193 btrfs_put_ordered_extent(ordered);
4194 goto again;
4195 }
4196
4197 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
4198 EXTENT_DIRTY | EXTENT_DELALLOC |
4199 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
4200 0, 0, &cached_state, GFP_NOFS);
4201
4202 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
4203 &cached_state);
4204 if (ret) {
4205 unlock_extent_cached(io_tree, page_start, page_end,
4206 &cached_state, GFP_NOFS);
4207 goto out_unlock;
4208 }
4209
4210 if (offset != PAGE_CACHE_SIZE) {
4211 if (!len)
4212 len = PAGE_CACHE_SIZE - offset;
4213 kaddr = kmap(page);
4214 if (front)
4215 memset(kaddr, 0, offset);
4216 else
4217 memset(kaddr + offset, 0, len);
4218 flush_dcache_page(page);
4219 kunmap(page);
4220 }
4221 ClearPageChecked(page);
4222 set_page_dirty(page);
4223 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
4224 GFP_NOFS);
4225
4226 out_unlock:
4227 if (ret)
4228 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
4229 unlock_page(page);
4230 page_cache_release(page);
4231 out:
4232 return ret;
4233 }
4234
4235 /*
4236 * This function puts in dummy file extents for the area we're creating a hole
4237 * for. So if we are truncating this file to a larger size we need to insert
4238 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4239 * the range between oldsize and size
4240 */
4241 int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
4242 {
4243 struct btrfs_trans_handle *trans;
4244 struct btrfs_root *root = BTRFS_I(inode)->root;
4245 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4246 struct extent_map *em = NULL;
4247 struct extent_state *cached_state = NULL;
4248 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
4249 u64 hole_start = ALIGN(oldsize, root->sectorsize);
4250 u64 block_end = ALIGN(size, root->sectorsize);
4251 u64 last_byte;
4252 u64 cur_offset;
4253 u64 hole_size;
4254 int err = 0;
4255
4256 /*
4257 * If our size started in the middle of a page we need to zero out the
4258 * rest of the page before we expand the i_size, otherwise we could
4259 * expose stale data.
4260 */
4261 err = btrfs_truncate_page(inode, oldsize, 0, 0);
4262 if (err)
4263 return err;
4264
4265 if (size <= hole_start)
4266 return 0;
4267
4268 while (1) {
4269 struct btrfs_ordered_extent *ordered;
4270 btrfs_wait_ordered_range(inode, hole_start,
4271 block_end - hole_start);
4272 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
4273 &cached_state);
4274 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
4275 if (!ordered)
4276 break;
4277 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4278 &cached_state, GFP_NOFS);
4279 btrfs_put_ordered_extent(ordered);
4280 }
4281
4282 cur_offset = hole_start;
4283 while (1) {
4284 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4285 block_end - cur_offset, 0);
4286 if (IS_ERR(em)) {
4287 err = PTR_ERR(em);
4288 em = NULL;
4289 break;
4290 }
4291 last_byte = min(extent_map_end(em), block_end);
4292 last_byte = ALIGN(last_byte , root->sectorsize);
4293 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
4294 struct extent_map *hole_em;
4295 hole_size = last_byte - cur_offset;
4296
4297 trans = btrfs_start_transaction(root, 3);
4298 if (IS_ERR(trans)) {
4299 err = PTR_ERR(trans);
4300 break;
4301 }
4302
4303 err = btrfs_drop_extents(trans, root, inode,
4304 cur_offset,
4305 cur_offset + hole_size, 1);
4306 if (err) {
4307 btrfs_abort_transaction(trans, root, err);
4308 btrfs_end_transaction(trans, root);
4309 break;
4310 }
4311
4312 err = btrfs_insert_file_extent(trans, root,
4313 btrfs_ino(inode), cur_offset, 0,
4314 0, hole_size, 0, hole_size,
4315 0, 0, 0);
4316 if (err) {
4317 btrfs_abort_transaction(trans, root, err);
4318 btrfs_end_transaction(trans, root);
4319 break;
4320 }
4321
4322 btrfs_drop_extent_cache(inode, cur_offset,
4323 cur_offset + hole_size - 1, 0);
4324 hole_em = alloc_extent_map();
4325 if (!hole_em) {
4326 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4327 &BTRFS_I(inode)->runtime_flags);
4328 goto next;
4329 }
4330 hole_em->start = cur_offset;
4331 hole_em->len = hole_size;
4332 hole_em->orig_start = cur_offset;
4333
4334 hole_em->block_start = EXTENT_MAP_HOLE;
4335 hole_em->block_len = 0;
4336 hole_em->orig_block_len = 0;
4337 hole_em->ram_bytes = hole_size;
4338 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
4339 hole_em->compress_type = BTRFS_COMPRESS_NONE;
4340 hole_em->generation = trans->transid;
4341
4342 while (1) {
4343 write_lock(&em_tree->lock);
4344 err = add_extent_mapping(em_tree, hole_em, 1);
4345 write_unlock(&em_tree->lock);
4346 if (err != -EEXIST)
4347 break;
4348 btrfs_drop_extent_cache(inode, cur_offset,
4349 cur_offset +
4350 hole_size - 1, 0);
4351 }
4352 free_extent_map(hole_em);
4353 next:
4354 btrfs_update_inode(trans, root, inode);
4355 btrfs_end_transaction(trans, root);
4356 }
4357 free_extent_map(em);
4358 em = NULL;
4359 cur_offset = last_byte;
4360 if (cur_offset >= block_end)
4361 break;
4362 }
4363
4364 free_extent_map(em);
4365 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4366 GFP_NOFS);
4367 return err;
4368 }
4369
4370 static int btrfs_setsize(struct inode *inode, struct iattr *attr)
4371 {
4372 struct btrfs_root *root = BTRFS_I(inode)->root;
4373 struct btrfs_trans_handle *trans;
4374 loff_t oldsize = i_size_read(inode);
4375 loff_t newsize = attr->ia_size;
4376 int mask = attr->ia_valid;
4377 int ret;
4378
4379 /*
4380 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4381 * special case where we need to update the times despite not having
4382 * these flags set. For all other operations the VFS set these flags
4383 * explicitly if it wants a timestamp update.
4384 */
4385 if (newsize != oldsize && (!(mask & (ATTR_CTIME | ATTR_MTIME))))
4386 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
4387
4388 if (newsize > oldsize) {
4389 truncate_pagecache(inode, oldsize, newsize);
4390 ret = btrfs_cont_expand(inode, oldsize, newsize);
4391 if (ret)
4392 return ret;
4393
4394 trans = btrfs_start_transaction(root, 1);
4395 if (IS_ERR(trans))
4396 return PTR_ERR(trans);
4397
4398 i_size_write(inode, newsize);
4399 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4400 ret = btrfs_update_inode(trans, root, inode);
4401 btrfs_end_transaction(trans, root);
4402 } else {
4403
4404 /*
4405 * We're truncating a file that used to have good data down to
4406 * zero. Make sure it gets into the ordered flush list so that
4407 * any new writes get down to disk quickly.
4408 */
4409 if (newsize == 0)
4410 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4411 &BTRFS_I(inode)->runtime_flags);
4412
4413 /*
4414 * 1 for the orphan item we're going to add
4415 * 1 for the orphan item deletion.
4416 */
4417 trans = btrfs_start_transaction(root, 2);
4418 if (IS_ERR(trans))
4419 return PTR_ERR(trans);
4420
4421 /*
4422 * We need to do this in case we fail at _any_ point during the
4423 * actual truncate. Once we do the truncate_setsize we could
4424 * invalidate pages which forces any outstanding ordered io to
4425 * be instantly completed which will give us extents that need
4426 * to be truncated. If we fail to get an orphan inode down we
4427 * could have left over extents that were never meant to live,
4428 * so we need to garuntee from this point on that everything
4429 * will be consistent.
4430 */
4431 ret = btrfs_orphan_add(trans, inode);
4432 btrfs_end_transaction(trans, root);
4433 if (ret)
4434 return ret;
4435
4436 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4437 truncate_setsize(inode, newsize);
4438
4439 /* Disable nonlocked read DIO to avoid the end less truncate */
4440 btrfs_inode_block_unlocked_dio(inode);
4441 inode_dio_wait(inode);
4442 btrfs_inode_resume_unlocked_dio(inode);
4443
4444 ret = btrfs_truncate(inode);
4445 if (ret && inode->i_nlink)
4446 btrfs_orphan_del(NULL, inode);
4447 }
4448
4449 return ret;
4450 }
4451
4452 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4453 {
4454 struct inode *inode = dentry->d_inode;
4455 struct btrfs_root *root = BTRFS_I(inode)->root;
4456 int err;
4457
4458 if (btrfs_root_readonly(root))
4459 return -EROFS;
4460
4461 err = inode_change_ok(inode, attr);
4462 if (err)
4463 return err;
4464
4465 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
4466 err = btrfs_setsize(inode, attr);
4467 if (err)
4468 return err;
4469 }
4470
4471 if (attr->ia_valid) {
4472 setattr_copy(inode, attr);
4473 inode_inc_iversion(inode);
4474 err = btrfs_dirty_inode(inode);
4475
4476 if (!err && attr->ia_valid & ATTR_MODE)
4477 err = btrfs_acl_chmod(inode);
4478 }
4479
4480 return err;
4481 }
4482
4483 void btrfs_evict_inode(struct inode *inode)
4484 {
4485 struct btrfs_trans_handle *trans;
4486 struct btrfs_root *root = BTRFS_I(inode)->root;
4487 struct btrfs_block_rsv *rsv, *global_rsv;
4488 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
4489 int ret;
4490
4491 trace_btrfs_inode_evict(inode);
4492
4493 truncate_inode_pages(&inode->i_data, 0);
4494 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
4495 btrfs_is_free_space_inode(inode)))
4496 goto no_delete;
4497
4498 if (is_bad_inode(inode)) {
4499 btrfs_orphan_del(NULL, inode);
4500 goto no_delete;
4501 }
4502 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
4503 btrfs_wait_ordered_range(inode, 0, (u64)-1);
4504
4505 if (root->fs_info->log_root_recovering) {
4506 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
4507 &BTRFS_I(inode)->runtime_flags));
4508 goto no_delete;
4509 }
4510
4511 if (inode->i_nlink > 0) {
4512 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
4513 goto no_delete;
4514 }
4515
4516 ret = btrfs_commit_inode_delayed_inode(inode);
4517 if (ret) {
4518 btrfs_orphan_del(NULL, inode);
4519 goto no_delete;
4520 }
4521
4522 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
4523 if (!rsv) {
4524 btrfs_orphan_del(NULL, inode);
4525 goto no_delete;
4526 }
4527 rsv->size = min_size;
4528 rsv->failfast = 1;
4529 global_rsv = &root->fs_info->global_block_rsv;
4530
4531 btrfs_i_size_write(inode, 0);
4532
4533 /*
4534 * This is a bit simpler than btrfs_truncate since we've already
4535 * reserved our space for our orphan item in the unlink, so we just
4536 * need to reserve some slack space in case we add bytes and update
4537 * inode item when doing the truncate.
4538 */
4539 while (1) {
4540 ret = btrfs_block_rsv_refill(root, rsv, min_size,
4541 BTRFS_RESERVE_FLUSH_LIMIT);
4542
4543 /*
4544 * Try and steal from the global reserve since we will
4545 * likely not use this space anyway, we want to try as
4546 * hard as possible to get this to work.
4547 */
4548 if (ret)
4549 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
4550
4551 if (ret) {
4552 btrfs_warn(root->fs_info,
4553 "Could not get space for a delete, will truncate on mount %d",
4554 ret);
4555 btrfs_orphan_del(NULL, inode);
4556 btrfs_free_block_rsv(root, rsv);
4557 goto no_delete;
4558 }
4559
4560 trans = btrfs_join_transaction(root);
4561 if (IS_ERR(trans)) {
4562 btrfs_orphan_del(NULL, inode);
4563 btrfs_free_block_rsv(root, rsv);
4564 goto no_delete;
4565 }
4566
4567 trans->block_rsv = rsv;
4568
4569 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
4570 if (ret != -ENOSPC)
4571 break;
4572
4573 trans->block_rsv = &root->fs_info->trans_block_rsv;
4574 btrfs_end_transaction(trans, root);
4575 trans = NULL;
4576 btrfs_btree_balance_dirty(root);
4577 }
4578
4579 btrfs_free_block_rsv(root, rsv);
4580
4581 /*
4582 * Errors here aren't a big deal, it just means we leave orphan items
4583 * in the tree. They will be cleaned up on the next mount.
4584 */
4585 if (ret == 0) {
4586 trans->block_rsv = root->orphan_block_rsv;
4587 btrfs_orphan_del(trans, inode);
4588 } else {
4589 btrfs_orphan_del(NULL, inode);
4590 }
4591
4592 trans->block_rsv = &root->fs_info->trans_block_rsv;
4593 if (!(root == root->fs_info->tree_root ||
4594 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
4595 btrfs_return_ino(root, btrfs_ino(inode));
4596
4597 btrfs_end_transaction(trans, root);
4598 btrfs_btree_balance_dirty(root);
4599 no_delete:
4600 btrfs_remove_delayed_node(inode);
4601 clear_inode(inode);
4602 return;
4603 }
4604
4605 /*
4606 * this returns the key found in the dir entry in the location pointer.
4607 * If no dir entries were found, location->objectid is 0.
4608 */
4609 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
4610 struct btrfs_key *location)
4611 {
4612 const char *name = dentry->d_name.name;
4613 int namelen = dentry->d_name.len;
4614 struct btrfs_dir_item *di;
4615 struct btrfs_path *path;
4616 struct btrfs_root *root = BTRFS_I(dir)->root;
4617 int ret = 0;
4618
4619 path = btrfs_alloc_path();
4620 if (!path)
4621 return -ENOMEM;
4622
4623 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
4624 namelen, 0);
4625 if (IS_ERR(di))
4626 ret = PTR_ERR(di);
4627
4628 if (IS_ERR_OR_NULL(di))
4629 goto out_err;
4630
4631 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
4632 out:
4633 btrfs_free_path(path);
4634 return ret;
4635 out_err:
4636 location->objectid = 0;
4637 goto out;
4638 }
4639
4640 /*
4641 * when we hit a tree root in a directory, the btrfs part of the inode
4642 * needs to be changed to reflect the root directory of the tree root. This
4643 * is kind of like crossing a mount point.
4644 */
4645 static int fixup_tree_root_location(struct btrfs_root *root,
4646 struct inode *dir,
4647 struct dentry *dentry,
4648 struct btrfs_key *location,
4649 struct btrfs_root **sub_root)
4650 {
4651 struct btrfs_path *path;
4652 struct btrfs_root *new_root;
4653 struct btrfs_root_ref *ref;
4654 struct extent_buffer *leaf;
4655 int ret;
4656 int err = 0;
4657
4658 path = btrfs_alloc_path();
4659 if (!path) {
4660 err = -ENOMEM;
4661 goto out;
4662 }
4663
4664 err = -ENOENT;
4665 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
4666 BTRFS_I(dir)->root->root_key.objectid,
4667 location->objectid);
4668 if (ret) {
4669 if (ret < 0)
4670 err = ret;
4671 goto out;
4672 }
4673
4674 leaf = path->nodes[0];
4675 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
4676 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
4677 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4678 goto out;
4679
4680 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4681 (unsigned long)(ref + 1),
4682 dentry->d_name.len);
4683 if (ret)
4684 goto out;
4685
4686 btrfs_release_path(path);
4687
4688 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4689 if (IS_ERR(new_root)) {
4690 err = PTR_ERR(new_root);
4691 goto out;
4692 }
4693
4694 *sub_root = new_root;
4695 location->objectid = btrfs_root_dirid(&new_root->root_item);
4696 location->type = BTRFS_INODE_ITEM_KEY;
4697 location->offset = 0;
4698 err = 0;
4699 out:
4700 btrfs_free_path(path);
4701 return err;
4702 }
4703
4704 static void inode_tree_add(struct inode *inode)
4705 {
4706 struct btrfs_root *root = BTRFS_I(inode)->root;
4707 struct btrfs_inode *entry;
4708 struct rb_node **p;
4709 struct rb_node *parent;
4710 u64 ino = btrfs_ino(inode);
4711
4712 if (inode_unhashed(inode))
4713 return;
4714 again:
4715 parent = NULL;
4716 spin_lock(&root->inode_lock);
4717 p = &root->inode_tree.rb_node;
4718 while (*p) {
4719 parent = *p;
4720 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4721
4722 if (ino < btrfs_ino(&entry->vfs_inode))
4723 p = &parent->rb_left;
4724 else if (ino > btrfs_ino(&entry->vfs_inode))
4725 p = &parent->rb_right;
4726 else {
4727 WARN_ON(!(entry->vfs_inode.i_state &
4728 (I_WILL_FREE | I_FREEING)));
4729 rb_erase(parent, &root->inode_tree);
4730 RB_CLEAR_NODE(parent);
4731 spin_unlock(&root->inode_lock);
4732 goto again;
4733 }
4734 }
4735 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
4736 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
4737 spin_unlock(&root->inode_lock);
4738 }
4739
4740 static void inode_tree_del(struct inode *inode)
4741 {
4742 struct btrfs_root *root = BTRFS_I(inode)->root;
4743 int empty = 0;
4744
4745 spin_lock(&root->inode_lock);
4746 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
4747 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
4748 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
4749 empty = RB_EMPTY_ROOT(&root->inode_tree);
4750 }
4751 spin_unlock(&root->inode_lock);
4752
4753 /*
4754 * Free space cache has inodes in the tree root, but the tree root has a
4755 * root_refs of 0, so this could end up dropping the tree root as a
4756 * snapshot, so we need the extra !root->fs_info->tree_root check to
4757 * make sure we don't drop it.
4758 */
4759 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
4760 root != root->fs_info->tree_root) {
4761 synchronize_srcu(&root->fs_info->subvol_srcu);
4762 spin_lock(&root->inode_lock);
4763 empty = RB_EMPTY_ROOT(&root->inode_tree);
4764 spin_unlock(&root->inode_lock);
4765 if (empty)
4766 btrfs_add_dead_root(root);
4767 }
4768 }
4769
4770 void btrfs_invalidate_inodes(struct btrfs_root *root)
4771 {
4772 struct rb_node *node;
4773 struct rb_node *prev;
4774 struct btrfs_inode *entry;
4775 struct inode *inode;
4776 u64 objectid = 0;
4777
4778 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4779
4780 spin_lock(&root->inode_lock);
4781 again:
4782 node = root->inode_tree.rb_node;
4783 prev = NULL;
4784 while (node) {
4785 prev = node;
4786 entry = rb_entry(node, struct btrfs_inode, rb_node);
4787
4788 if (objectid < btrfs_ino(&entry->vfs_inode))
4789 node = node->rb_left;
4790 else if (objectid > btrfs_ino(&entry->vfs_inode))
4791 node = node->rb_right;
4792 else
4793 break;
4794 }
4795 if (!node) {
4796 while (prev) {
4797 entry = rb_entry(prev, struct btrfs_inode, rb_node);
4798 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
4799 node = prev;
4800 break;
4801 }
4802 prev = rb_next(prev);
4803 }
4804 }
4805 while (node) {
4806 entry = rb_entry(node, struct btrfs_inode, rb_node);
4807 objectid = btrfs_ino(&entry->vfs_inode) + 1;
4808 inode = igrab(&entry->vfs_inode);
4809 if (inode) {
4810 spin_unlock(&root->inode_lock);
4811 if (atomic_read(&inode->i_count) > 1)
4812 d_prune_aliases(inode);
4813 /*
4814 * btrfs_drop_inode will have it removed from
4815 * the inode cache when its usage count
4816 * hits zero.
4817 */
4818 iput(inode);
4819 cond_resched();
4820 spin_lock(&root->inode_lock);
4821 goto again;
4822 }
4823
4824 if (cond_resched_lock(&root->inode_lock))
4825 goto again;
4826
4827 node = rb_next(node);
4828 }
4829 spin_unlock(&root->inode_lock);
4830 }
4831
4832 static int btrfs_init_locked_inode(struct inode *inode, void *p)
4833 {
4834 struct btrfs_iget_args *args = p;
4835 inode->i_ino = args->ino;
4836 BTRFS_I(inode)->root = args->root;
4837 return 0;
4838 }
4839
4840 static int btrfs_find_actor(struct inode *inode, void *opaque)
4841 {
4842 struct btrfs_iget_args *args = opaque;
4843 return args->ino == btrfs_ino(inode) &&
4844 args->root == BTRFS_I(inode)->root;
4845 }
4846
4847 static struct inode *btrfs_iget_locked(struct super_block *s,
4848 u64 objectid,
4849 struct btrfs_root *root)
4850 {
4851 struct inode *inode;
4852 struct btrfs_iget_args args;
4853 args.ino = objectid;
4854 args.root = root;
4855
4856 inode = iget5_locked(s, objectid, btrfs_find_actor,
4857 btrfs_init_locked_inode,
4858 (void *)&args);
4859 return inode;
4860 }
4861
4862 /* Get an inode object given its location and corresponding root.
4863 * Returns in *is_new if the inode was read from disk
4864 */
4865 struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
4866 struct btrfs_root *root, int *new)
4867 {
4868 struct inode *inode;
4869
4870 inode = btrfs_iget_locked(s, location->objectid, root);
4871 if (!inode)
4872 return ERR_PTR(-ENOMEM);
4873
4874 if (inode->i_state & I_NEW) {
4875 BTRFS_I(inode)->root = root;
4876 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4877 btrfs_read_locked_inode(inode);
4878 if (!is_bad_inode(inode)) {
4879 inode_tree_add(inode);
4880 unlock_new_inode(inode);
4881 if (new)
4882 *new = 1;
4883 } else {
4884 unlock_new_inode(inode);
4885 iput(inode);
4886 inode = ERR_PTR(-ESTALE);
4887 }
4888 }
4889
4890 return inode;
4891 }
4892
4893 static struct inode *new_simple_dir(struct super_block *s,
4894 struct btrfs_key *key,
4895 struct btrfs_root *root)
4896 {
4897 struct inode *inode = new_inode(s);
4898
4899 if (!inode)
4900 return ERR_PTR(-ENOMEM);
4901
4902 BTRFS_I(inode)->root = root;
4903 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
4904 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
4905
4906 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
4907 inode->i_op = &btrfs_dir_ro_inode_operations;
4908 inode->i_fop = &simple_dir_operations;
4909 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
4910 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4911
4912 return inode;
4913 }
4914
4915 struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
4916 {
4917 struct inode *inode;
4918 struct btrfs_root *root = BTRFS_I(dir)->root;
4919 struct btrfs_root *sub_root = root;
4920 struct btrfs_key location;
4921 int index;
4922 int ret = 0;
4923
4924 if (dentry->d_name.len > BTRFS_NAME_LEN)
4925 return ERR_PTR(-ENAMETOOLONG);
4926
4927 ret = btrfs_inode_by_name(dir, dentry, &location);
4928 if (ret < 0)
4929 return ERR_PTR(ret);
4930
4931 if (location.objectid == 0)
4932 return NULL;
4933
4934 if (location.type == BTRFS_INODE_ITEM_KEY) {
4935 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
4936 return inode;
4937 }
4938
4939 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4940
4941 index = srcu_read_lock(&root->fs_info->subvol_srcu);
4942 ret = fixup_tree_root_location(root, dir, dentry,
4943 &location, &sub_root);
4944 if (ret < 0) {
4945 if (ret != -ENOENT)
4946 inode = ERR_PTR(ret);
4947 else
4948 inode = new_simple_dir(dir->i_sb, &location, sub_root);
4949 } else {
4950 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
4951 }
4952 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4953
4954 if (!IS_ERR(inode) && root != sub_root) {
4955 down_read(&root->fs_info->cleanup_work_sem);
4956 if (!(inode->i_sb->s_flags & MS_RDONLY))
4957 ret = btrfs_orphan_cleanup(sub_root);
4958 up_read(&root->fs_info->cleanup_work_sem);
4959 if (ret) {
4960 iput(inode);
4961 inode = ERR_PTR(ret);
4962 }
4963 }
4964
4965 return inode;
4966 }
4967
4968 static int btrfs_dentry_delete(const struct dentry *dentry)
4969 {
4970 struct btrfs_root *root;
4971 struct inode *inode = dentry->d_inode;
4972
4973 if (!inode && !IS_ROOT(dentry))
4974 inode = dentry->d_parent->d_inode;
4975
4976 if (inode) {
4977 root = BTRFS_I(inode)->root;
4978 if (btrfs_root_refs(&root->root_item) == 0)
4979 return 1;
4980
4981 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
4982 return 1;
4983 }
4984 return 0;
4985 }
4986
4987 static void btrfs_dentry_release(struct dentry *dentry)
4988 {
4989 if (dentry->d_fsdata)
4990 kfree(dentry->d_fsdata);
4991 }
4992
4993 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
4994 unsigned int flags)
4995 {
4996 struct dentry *ret;
4997
4998 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
4999 return ret;
5000 }
5001
5002 unsigned char btrfs_filetype_table[] = {
5003 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5004 };
5005
5006 static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
5007 {
5008 struct inode *inode = file_inode(file);
5009 struct btrfs_root *root = BTRFS_I(inode)->root;
5010 struct btrfs_item *item;
5011 struct btrfs_dir_item *di;
5012 struct btrfs_key key;
5013 struct btrfs_key found_key;
5014 struct btrfs_path *path;
5015 struct list_head ins_list;
5016 struct list_head del_list;
5017 int ret;
5018 struct extent_buffer *leaf;
5019 int slot;
5020 unsigned char d_type;
5021 int over = 0;
5022 u32 di_cur;
5023 u32 di_total;
5024 u32 di_len;
5025 int key_type = BTRFS_DIR_INDEX_KEY;
5026 char tmp_name[32];
5027 char *name_ptr;
5028 int name_len;
5029 int is_curr = 0; /* ctx->pos points to the current index? */
5030
5031 /* FIXME, use a real flag for deciding about the key type */
5032 if (root->fs_info->tree_root == root)
5033 key_type = BTRFS_DIR_ITEM_KEY;
5034
5035 if (!dir_emit_dots(file, ctx))
5036 return 0;
5037
5038 path = btrfs_alloc_path();
5039 if (!path)
5040 return -ENOMEM;
5041
5042 path->reada = 1;
5043
5044 if (key_type == BTRFS_DIR_INDEX_KEY) {
5045 INIT_LIST_HEAD(&ins_list);
5046 INIT_LIST_HEAD(&del_list);
5047 btrfs_get_delayed_items(inode, &ins_list, &del_list);
5048 }
5049
5050 btrfs_set_key_type(&key, key_type);
5051 key.offset = ctx->pos;
5052 key.objectid = btrfs_ino(inode);
5053
5054 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5055 if (ret < 0)
5056 goto err;
5057
5058 while (1) {
5059 leaf = path->nodes[0];
5060 slot = path->slots[0];
5061 if (slot >= btrfs_header_nritems(leaf)) {
5062 ret = btrfs_next_leaf(root, path);
5063 if (ret < 0)
5064 goto err;
5065 else if (ret > 0)
5066 break;
5067 continue;
5068 }
5069
5070 item = btrfs_item_nr(leaf, slot);
5071 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5072
5073 if (found_key.objectid != key.objectid)
5074 break;
5075 if (btrfs_key_type(&found_key) != key_type)
5076 break;
5077 if (found_key.offset < ctx->pos)
5078 goto next;
5079 if (key_type == BTRFS_DIR_INDEX_KEY &&
5080 btrfs_should_delete_dir_index(&del_list,
5081 found_key.offset))
5082 goto next;
5083
5084 ctx->pos = found_key.offset;
5085 is_curr = 1;
5086
5087 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5088 di_cur = 0;
5089 di_total = btrfs_item_size(leaf, item);
5090
5091 while (di_cur < di_total) {
5092 struct btrfs_key location;
5093
5094 if (verify_dir_item(root, leaf, di))
5095 break;
5096
5097 name_len = btrfs_dir_name_len(leaf, di);
5098 if (name_len <= sizeof(tmp_name)) {
5099 name_ptr = tmp_name;
5100 } else {
5101 name_ptr = kmalloc(name_len, GFP_NOFS);
5102 if (!name_ptr) {
5103 ret = -ENOMEM;
5104 goto err;
5105 }
5106 }
5107 read_extent_buffer(leaf, name_ptr,
5108 (unsigned long)(di + 1), name_len);
5109
5110 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5111 btrfs_dir_item_key_to_cpu(leaf, di, &location);
5112
5113
5114 /* is this a reference to our own snapshot? If so
5115 * skip it.
5116 *
5117 * In contrast to old kernels, we insert the snapshot's
5118 * dir item and dir index after it has been created, so
5119 * we won't find a reference to our own snapshot. We
5120 * still keep the following code for backward
5121 * compatibility.
5122 */
5123 if (location.type == BTRFS_ROOT_ITEM_KEY &&
5124 location.objectid == root->root_key.objectid) {
5125 over = 0;
5126 goto skip;
5127 }
5128 over = !dir_emit(ctx, name_ptr, name_len,
5129 location.objectid, d_type);
5130
5131 skip:
5132 if (name_ptr != tmp_name)
5133 kfree(name_ptr);
5134
5135 if (over)
5136 goto nopos;
5137 di_len = btrfs_dir_name_len(leaf, di) +
5138 btrfs_dir_data_len(leaf, di) + sizeof(*di);
5139 di_cur += di_len;
5140 di = (struct btrfs_dir_item *)((char *)di + di_len);
5141 }
5142 next:
5143 path->slots[0]++;
5144 }
5145
5146 if (key_type == BTRFS_DIR_INDEX_KEY) {
5147 if (is_curr)
5148 ctx->pos++;
5149 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
5150 if (ret)
5151 goto nopos;
5152 }
5153
5154 /* Reached end of directory/root. Bump pos past the last item. */
5155 ctx->pos++;
5156
5157 /*
5158 * Stop new entries from being returned after we return the last
5159 * entry.
5160 *
5161 * New directory entries are assigned a strictly increasing
5162 * offset. This means that new entries created during readdir
5163 * are *guaranteed* to be seen in the future by that readdir.
5164 * This has broken buggy programs which operate on names as
5165 * they're returned by readdir. Until we re-use freed offsets
5166 * we have this hack to stop new entries from being returned
5167 * under the assumption that they'll never reach this huge
5168 * offset.
5169 *
5170 * This is being careful not to overflow 32bit loff_t unless the
5171 * last entry requires it because doing so has broken 32bit apps
5172 * in the past.
5173 */
5174 if (key_type == BTRFS_DIR_INDEX_KEY) {
5175 if (ctx->pos >= INT_MAX)
5176 ctx->pos = LLONG_MAX;
5177 else
5178 ctx->pos = INT_MAX;
5179 }
5180 nopos:
5181 ret = 0;
5182 err:
5183 if (key_type == BTRFS_DIR_INDEX_KEY)
5184 btrfs_put_delayed_items(&ins_list, &del_list);
5185 btrfs_free_path(path);
5186 return ret;
5187 }
5188
5189 int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
5190 {
5191 struct btrfs_root *root = BTRFS_I(inode)->root;
5192 struct btrfs_trans_handle *trans;
5193 int ret = 0;
5194 bool nolock = false;
5195
5196 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
5197 return 0;
5198
5199 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
5200 nolock = true;
5201
5202 if (wbc->sync_mode == WB_SYNC_ALL) {
5203 if (nolock)
5204 trans = btrfs_join_transaction_nolock(root);
5205 else
5206 trans = btrfs_join_transaction(root);
5207 if (IS_ERR(trans))
5208 return PTR_ERR(trans);
5209 ret = btrfs_commit_transaction(trans, root);
5210 }
5211 return ret;
5212 }
5213
5214 /*
5215 * This is somewhat expensive, updating the tree every time the
5216 * inode changes. But, it is most likely to find the inode in cache.
5217 * FIXME, needs more benchmarking...there are no reasons other than performance
5218 * to keep or drop this code.
5219 */
5220 static int btrfs_dirty_inode(struct inode *inode)
5221 {
5222 struct btrfs_root *root = BTRFS_I(inode)->root;
5223 struct btrfs_trans_handle *trans;
5224 int ret;
5225
5226 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
5227 return 0;
5228
5229 trans = btrfs_join_transaction(root);
5230 if (IS_ERR(trans))
5231 return PTR_ERR(trans);
5232
5233 ret = btrfs_update_inode(trans, root, inode);
5234 if (ret && ret == -ENOSPC) {
5235 /* whoops, lets try again with the full transaction */
5236 btrfs_end_transaction(trans, root);
5237 trans = btrfs_start_transaction(root, 1);
5238 if (IS_ERR(trans))
5239 return PTR_ERR(trans);
5240
5241 ret = btrfs_update_inode(trans, root, inode);
5242 }
5243 btrfs_end_transaction(trans, root);
5244 if (BTRFS_I(inode)->delayed_node)
5245 btrfs_balance_delayed_items(root);
5246
5247 return ret;
5248 }
5249
5250 /*
5251 * This is a copy of file_update_time. We need this so we can return error on
5252 * ENOSPC for updating the inode in the case of file write and mmap writes.
5253 */
5254 static int btrfs_update_time(struct inode *inode, struct timespec *now,
5255 int flags)
5256 {
5257 struct btrfs_root *root = BTRFS_I(inode)->root;
5258
5259 if (btrfs_root_readonly(root))
5260 return -EROFS;
5261
5262 if (flags & S_VERSION)
5263 inode_inc_iversion(inode);
5264 if (flags & S_CTIME)
5265 inode->i_ctime = *now;
5266 if (flags & S_MTIME)
5267 inode->i_mtime = *now;
5268 if (flags & S_ATIME)
5269 inode->i_atime = *now;
5270 return btrfs_dirty_inode(inode);
5271 }
5272
5273 /*
5274 * find the highest existing sequence number in a directory
5275 * and then set the in-memory index_cnt variable to reflect
5276 * free sequence numbers
5277 */
5278 static int btrfs_set_inode_index_count(struct inode *inode)
5279 {
5280 struct btrfs_root *root = BTRFS_I(inode)->root;
5281 struct btrfs_key key, found_key;
5282 struct btrfs_path *path;
5283 struct extent_buffer *leaf;
5284 int ret;
5285
5286 key.objectid = btrfs_ino(inode);
5287 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
5288 key.offset = (u64)-1;
5289
5290 path = btrfs_alloc_path();
5291 if (!path)
5292 return -ENOMEM;
5293
5294 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5295 if (ret < 0)
5296 goto out;
5297 /* FIXME: we should be able to handle this */
5298 if (ret == 0)
5299 goto out;
5300 ret = 0;
5301
5302 /*
5303 * MAGIC NUMBER EXPLANATION:
5304 * since we search a directory based on f_pos we have to start at 2
5305 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5306 * else has to start at 2
5307 */
5308 if (path->slots[0] == 0) {
5309 BTRFS_I(inode)->index_cnt = 2;
5310 goto out;
5311 }
5312
5313 path->slots[0]--;
5314
5315 leaf = path->nodes[0];
5316 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5317
5318 if (found_key.objectid != btrfs_ino(inode) ||
5319 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
5320 BTRFS_I(inode)->index_cnt = 2;
5321 goto out;
5322 }
5323
5324 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
5325 out:
5326 btrfs_free_path(path);
5327 return ret;
5328 }
5329
5330 /*
5331 * helper to find a free sequence number in a given directory. This current
5332 * code is very simple, later versions will do smarter things in the btree
5333 */
5334 int btrfs_set_inode_index(struct inode *dir, u64 *index)
5335 {
5336 int ret = 0;
5337
5338 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
5339 ret = btrfs_inode_delayed_dir_index_count(dir);
5340 if (ret) {
5341 ret = btrfs_set_inode_index_count(dir);
5342 if (ret)
5343 return ret;
5344 }
5345 }
5346
5347 *index = BTRFS_I(dir)->index_cnt;
5348 BTRFS_I(dir)->index_cnt++;
5349
5350 return ret;
5351 }
5352
5353 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5354 struct btrfs_root *root,
5355 struct inode *dir,
5356 const char *name, int name_len,
5357 u64 ref_objectid, u64 objectid,
5358 umode_t mode, u64 *index)
5359 {
5360 struct inode *inode;
5361 struct btrfs_inode_item *inode_item;
5362 struct btrfs_key *location;
5363 struct btrfs_path *path;
5364 struct btrfs_inode_ref *ref;
5365 struct btrfs_key key[2];
5366 u32 sizes[2];
5367 unsigned long ptr;
5368 int ret;
5369 int owner;
5370
5371 path = btrfs_alloc_path();
5372 if (!path)
5373 return ERR_PTR(-ENOMEM);
5374
5375 inode = new_inode(root->fs_info->sb);
5376 if (!inode) {
5377 btrfs_free_path(path);
5378 return ERR_PTR(-ENOMEM);
5379 }
5380
5381 /*
5382 * we have to initialize this early, so we can reclaim the inode
5383 * number if we fail afterwards in this function.
5384 */
5385 inode->i_ino = objectid;
5386
5387 if (dir) {
5388 trace_btrfs_inode_request(dir);
5389
5390 ret = btrfs_set_inode_index(dir, index);
5391 if (ret) {
5392 btrfs_free_path(path);
5393 iput(inode);
5394 return ERR_PTR(ret);
5395 }
5396 }
5397 /*
5398 * index_cnt is ignored for everything but a dir,
5399 * btrfs_get_inode_index_count has an explanation for the magic
5400 * number
5401 */
5402 BTRFS_I(inode)->index_cnt = 2;
5403 BTRFS_I(inode)->root = root;
5404 BTRFS_I(inode)->generation = trans->transid;
5405 inode->i_generation = BTRFS_I(inode)->generation;
5406
5407 /*
5408 * We could have gotten an inode number from somebody who was fsynced
5409 * and then removed in this same transaction, so let's just set full
5410 * sync since it will be a full sync anyway and this will blow away the
5411 * old info in the log.
5412 */
5413 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5414
5415 if (S_ISDIR(mode))
5416 owner = 0;
5417 else
5418 owner = 1;
5419
5420 key[0].objectid = objectid;
5421 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
5422 key[0].offset = 0;
5423
5424 /*
5425 * Start new inodes with an inode_ref. This is slightly more
5426 * efficient for small numbers of hard links since they will
5427 * be packed into one item. Extended refs will kick in if we
5428 * add more hard links than can fit in the ref item.
5429 */
5430 key[1].objectid = objectid;
5431 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
5432 key[1].offset = ref_objectid;
5433
5434 sizes[0] = sizeof(struct btrfs_inode_item);
5435 sizes[1] = name_len + sizeof(*ref);
5436
5437 path->leave_spinning = 1;
5438 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
5439 if (ret != 0)
5440 goto fail;
5441
5442 inode_init_owner(inode, dir, mode);
5443 inode_set_bytes(inode, 0);
5444 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
5445 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5446 struct btrfs_inode_item);
5447 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
5448 sizeof(*inode_item));
5449 fill_inode_item(trans, path->nodes[0], inode_item, inode);
5450
5451 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5452 struct btrfs_inode_ref);
5453 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
5454 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
5455 ptr = (unsigned long)(ref + 1);
5456 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5457
5458 btrfs_mark_buffer_dirty(path->nodes[0]);
5459 btrfs_free_path(path);
5460
5461 location = &BTRFS_I(inode)->location;
5462 location->objectid = objectid;
5463 location->offset = 0;
5464 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
5465
5466 btrfs_inherit_iflags(inode, dir);
5467
5468 if (S_ISREG(mode)) {
5469 if (btrfs_test_opt(root, NODATASUM))
5470 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
5471 if (btrfs_test_opt(root, NODATACOW))
5472 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5473 BTRFS_INODE_NODATASUM;
5474 }
5475
5476 insert_inode_hash(inode);
5477 inode_tree_add(inode);
5478
5479 trace_btrfs_inode_new(inode);
5480 btrfs_set_inode_last_trans(trans, inode);
5481
5482 btrfs_update_root_times(trans, root);
5483
5484 return inode;
5485 fail:
5486 if (dir)
5487 BTRFS_I(dir)->index_cnt--;
5488 btrfs_free_path(path);
5489 iput(inode);
5490 return ERR_PTR(ret);
5491 }
5492
5493 static inline u8 btrfs_inode_type(struct inode *inode)
5494 {
5495 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
5496 }
5497
5498 /*
5499 * utility function to add 'inode' into 'parent_inode' with
5500 * a give name and a given sequence number.
5501 * if 'add_backref' is true, also insert a backref from the
5502 * inode to the parent directory.
5503 */
5504 int btrfs_add_link(struct btrfs_trans_handle *trans,
5505 struct inode *parent_inode, struct inode *inode,
5506 const char *name, int name_len, int add_backref, u64 index)
5507 {
5508 int ret = 0;
5509 struct btrfs_key key;
5510 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
5511 u64 ino = btrfs_ino(inode);
5512 u64 parent_ino = btrfs_ino(parent_inode);
5513
5514 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5515 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
5516 } else {
5517 key.objectid = ino;
5518 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
5519 key.offset = 0;
5520 }
5521
5522 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5523 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
5524 key.objectid, root->root_key.objectid,
5525 parent_ino, index, name, name_len);
5526 } else if (add_backref) {
5527 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
5528 parent_ino, index);
5529 }
5530
5531 /* Nothing to clean up yet */
5532 if (ret)
5533 return ret;
5534
5535 ret = btrfs_insert_dir_item(trans, root, name, name_len,
5536 parent_inode, &key,
5537 btrfs_inode_type(inode), index);
5538 if (ret == -EEXIST || ret == -EOVERFLOW)
5539 goto fail_dir_item;
5540 else if (ret) {
5541 btrfs_abort_transaction(trans, root, ret);
5542 return ret;
5543 }
5544
5545 btrfs_i_size_write(parent_inode, parent_inode->i_size +
5546 name_len * 2);
5547 inode_inc_iversion(parent_inode);
5548 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
5549 ret = btrfs_update_inode(trans, root, parent_inode);
5550 if (ret)
5551 btrfs_abort_transaction(trans, root, ret);
5552 return ret;
5553
5554 fail_dir_item:
5555 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
5556 u64 local_index;
5557 int err;
5558 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
5559 key.objectid, root->root_key.objectid,
5560 parent_ino, &local_index, name, name_len);
5561
5562 } else if (add_backref) {
5563 u64 local_index;
5564 int err;
5565
5566 err = btrfs_del_inode_ref(trans, root, name, name_len,
5567 ino, parent_ino, &local_index);
5568 }
5569 return ret;
5570 }
5571
5572 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
5573 struct inode *dir, struct dentry *dentry,
5574 struct inode *inode, int backref, u64 index)
5575 {
5576 int err = btrfs_add_link(trans, dir, inode,
5577 dentry->d_name.name, dentry->d_name.len,
5578 backref, index);
5579 if (err > 0)
5580 err = -EEXIST;
5581 return err;
5582 }
5583
5584 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
5585 umode_t mode, dev_t rdev)
5586 {
5587 struct btrfs_trans_handle *trans;
5588 struct btrfs_root *root = BTRFS_I(dir)->root;
5589 struct inode *inode = NULL;
5590 int err;
5591 int drop_inode = 0;
5592 u64 objectid;
5593 u64 index = 0;
5594
5595 if (!new_valid_dev(rdev))
5596 return -EINVAL;
5597
5598 /*
5599 * 2 for inode item and ref
5600 * 2 for dir items
5601 * 1 for xattr if selinux is on
5602 */
5603 trans = btrfs_start_transaction(root, 5);
5604 if (IS_ERR(trans))
5605 return PTR_ERR(trans);
5606
5607 err = btrfs_find_free_ino(root, &objectid);
5608 if (err)
5609 goto out_unlock;
5610
5611 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
5612 dentry->d_name.len, btrfs_ino(dir), objectid,
5613 mode, &index);
5614 if (IS_ERR(inode)) {
5615 err = PTR_ERR(inode);
5616 goto out_unlock;
5617 }
5618
5619 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
5620 if (err) {
5621 drop_inode = 1;
5622 goto out_unlock;
5623 }
5624
5625 /*
5626 * If the active LSM wants to access the inode during
5627 * d_instantiate it needs these. Smack checks to see
5628 * if the filesystem supports xattrs by looking at the
5629 * ops vector.
5630 */
5631
5632 inode->i_op = &btrfs_special_inode_operations;
5633 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
5634 if (err)
5635 drop_inode = 1;
5636 else {
5637 init_special_inode(inode, inode->i_mode, rdev);
5638 btrfs_update_inode(trans, root, inode);
5639 d_instantiate(dentry, inode);
5640 }
5641 out_unlock:
5642 btrfs_end_transaction(trans, root);
5643 btrfs_btree_balance_dirty(root);
5644 if (drop_inode) {
5645 inode_dec_link_count(inode);
5646 iput(inode);
5647 }
5648 return err;
5649 }
5650
5651 static int btrfs_create(struct inode *dir, struct dentry *dentry,
5652 umode_t mode, bool excl)
5653 {
5654 struct btrfs_trans_handle *trans;
5655 struct btrfs_root *root = BTRFS_I(dir)->root;
5656 struct inode *inode = NULL;
5657 int drop_inode_on_err = 0;
5658 int err;
5659 u64 objectid;
5660 u64 index = 0;
5661
5662 /*
5663 * 2 for inode item and ref
5664 * 2 for dir items
5665 * 1 for xattr if selinux is on
5666 */
5667 trans = btrfs_start_transaction(root, 5);
5668 if (IS_ERR(trans))
5669 return PTR_ERR(trans);
5670
5671 err = btrfs_find_free_ino(root, &objectid);
5672 if (err)
5673 goto out_unlock;
5674
5675 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
5676 dentry->d_name.len, btrfs_ino(dir), objectid,
5677 mode, &index);
5678 if (IS_ERR(inode)) {
5679 err = PTR_ERR(inode);
5680 goto out_unlock;
5681 }
5682 drop_inode_on_err = 1;
5683
5684 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
5685 if (err)
5686 goto out_unlock;
5687
5688 err = btrfs_update_inode(trans, root, inode);
5689 if (err)
5690 goto out_unlock;
5691
5692 /*
5693 * If the active LSM wants to access the inode during
5694 * d_instantiate it needs these. Smack checks to see
5695 * if the filesystem supports xattrs by looking at the
5696 * ops vector.
5697 */
5698 inode->i_fop = &btrfs_file_operations;
5699 inode->i_op = &btrfs_file_inode_operations;
5700
5701 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
5702 if (err)
5703 goto out_unlock;
5704
5705 inode->i_mapping->a_ops = &btrfs_aops;
5706 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5707 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
5708 d_instantiate(dentry, inode);
5709
5710 out_unlock:
5711 btrfs_end_transaction(trans, root);
5712 if (err && drop_inode_on_err) {
5713 inode_dec_link_count(inode);
5714 iput(inode);
5715 }
5716 btrfs_btree_balance_dirty(root);
5717 return err;
5718 }
5719
5720 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5721 struct dentry *dentry)
5722 {
5723 struct btrfs_trans_handle *trans;
5724 struct btrfs_root *root = BTRFS_I(dir)->root;
5725 struct inode *inode = old_dentry->d_inode;
5726 u64 index;
5727 int err;
5728 int drop_inode = 0;
5729
5730 /* do not allow sys_link's with other subvols of the same device */
5731 if (root->objectid != BTRFS_I(inode)->root->objectid)
5732 return -EXDEV;
5733
5734 if (inode->i_nlink >= BTRFS_LINK_MAX)
5735 return -EMLINK;
5736
5737 err = btrfs_set_inode_index(dir, &index);
5738 if (err)
5739 goto fail;
5740
5741 /*
5742 * 2 items for inode and inode ref
5743 * 2 items for dir items
5744 * 1 item for parent inode
5745 */
5746 trans = btrfs_start_transaction(root, 5);
5747 if (IS_ERR(trans)) {
5748 err = PTR_ERR(trans);
5749 goto fail;
5750 }
5751
5752 btrfs_inc_nlink(inode);
5753 inode_inc_iversion(inode);
5754 inode->i_ctime = CURRENT_TIME;
5755 ihold(inode);
5756 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
5757
5758 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
5759
5760 if (err) {
5761 drop_inode = 1;
5762 } else {
5763 struct dentry *parent = dentry->d_parent;
5764 err = btrfs_update_inode(trans, root, inode);
5765 if (err)
5766 goto fail;
5767 d_instantiate(dentry, inode);
5768 btrfs_log_new_name(trans, inode, NULL, parent);
5769 }
5770
5771 btrfs_end_transaction(trans, root);
5772 fail:
5773 if (drop_inode) {
5774 inode_dec_link_count(inode);
5775 iput(inode);
5776 }
5777 btrfs_btree_balance_dirty(root);
5778 return err;
5779 }
5780
5781 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
5782 {
5783 struct inode *inode = NULL;
5784 struct btrfs_trans_handle *trans;
5785 struct btrfs_root *root = BTRFS_I(dir)->root;
5786 int err = 0;
5787 int drop_on_err = 0;
5788 u64 objectid = 0;
5789 u64 index = 0;
5790
5791 /*
5792 * 2 items for inode and ref
5793 * 2 items for dir items
5794 * 1 for xattr if selinux is on
5795 */
5796 trans = btrfs_start_transaction(root, 5);
5797 if (IS_ERR(trans))
5798 return PTR_ERR(trans);
5799
5800 err = btrfs_find_free_ino(root, &objectid);
5801 if (err)
5802 goto out_fail;
5803
5804 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
5805 dentry->d_name.len, btrfs_ino(dir), objectid,
5806 S_IFDIR | mode, &index);
5807 if (IS_ERR(inode)) {
5808 err = PTR_ERR(inode);
5809 goto out_fail;
5810 }
5811
5812 drop_on_err = 1;
5813
5814 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
5815 if (err)
5816 goto out_fail;
5817
5818 inode->i_op = &btrfs_dir_inode_operations;
5819 inode->i_fop = &btrfs_dir_file_operations;
5820
5821 btrfs_i_size_write(inode, 0);
5822 err = btrfs_update_inode(trans, root, inode);
5823 if (err)
5824 goto out_fail;
5825
5826 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5827 dentry->d_name.len, 0, index);
5828 if (err)
5829 goto out_fail;
5830
5831 d_instantiate(dentry, inode);
5832 drop_on_err = 0;
5833
5834 out_fail:
5835 btrfs_end_transaction(trans, root);
5836 if (drop_on_err)
5837 iput(inode);
5838 btrfs_btree_balance_dirty(root);
5839 return err;
5840 }
5841
5842 /* helper for btfs_get_extent. Given an existing extent in the tree,
5843 * and an extent that you want to insert, deal with overlap and insert
5844 * the new extent into the tree.
5845 */
5846 static int merge_extent_mapping(struct extent_map_tree *em_tree,
5847 struct extent_map *existing,
5848 struct extent_map *em,
5849 u64 map_start, u64 map_len)
5850 {
5851 u64 start_diff;
5852
5853 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5854 start_diff = map_start - em->start;
5855 em->start = map_start;
5856 em->len = map_len;
5857 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5858 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
5859 em->block_start += start_diff;
5860 em->block_len -= start_diff;
5861 }
5862 return add_extent_mapping(em_tree, em, 0);
5863 }
5864
5865 static noinline int uncompress_inline(struct btrfs_path *path,
5866 struct inode *inode, struct page *page,
5867 size_t pg_offset, u64 extent_offset,
5868 struct btrfs_file_extent_item *item)
5869 {
5870 int ret;
5871 struct extent_buffer *leaf = path->nodes[0];
5872 char *tmp;
5873 size_t max_size;
5874 unsigned long inline_size;
5875 unsigned long ptr;
5876 int compress_type;
5877
5878 WARN_ON(pg_offset != 0);
5879 compress_type = btrfs_file_extent_compression(leaf, item);
5880 max_size = btrfs_file_extent_ram_bytes(leaf, item);
5881 inline_size = btrfs_file_extent_inline_item_len(leaf,
5882 btrfs_item_nr(leaf, path->slots[0]));
5883 tmp = kmalloc(inline_size, GFP_NOFS);
5884 if (!tmp)
5885 return -ENOMEM;
5886 ptr = btrfs_file_extent_inline_start(item);
5887
5888 read_extent_buffer(leaf, tmp, ptr, inline_size);
5889
5890 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
5891 ret = btrfs_decompress(compress_type, tmp, page,
5892 extent_offset, inline_size, max_size);
5893 if (ret) {
5894 char *kaddr = kmap_atomic(page);
5895 unsigned long copy_size = min_t(u64,
5896 PAGE_CACHE_SIZE - pg_offset,
5897 max_size - extent_offset);
5898 memset(kaddr + pg_offset, 0, copy_size);
5899 kunmap_atomic(kaddr);
5900 }
5901 kfree(tmp);
5902 return 0;
5903 }
5904
5905 /*
5906 * a bit scary, this does extent mapping from logical file offset to the disk.
5907 * the ugly parts come from merging extents from the disk with the in-ram
5908 * representation. This gets more complex because of the data=ordered code,
5909 * where the in-ram extents might be locked pending data=ordered completion.
5910 *
5911 * This also copies inline extents directly into the page.
5912 */
5913
5914 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
5915 size_t pg_offset, u64 start, u64 len,
5916 int create)
5917 {
5918 int ret;
5919 int err = 0;
5920 u64 bytenr;
5921 u64 extent_start = 0;
5922 u64 extent_end = 0;
5923 u64 objectid = btrfs_ino(inode);
5924 u32 found_type;
5925 struct btrfs_path *path = NULL;
5926 struct btrfs_root *root = BTRFS_I(inode)->root;
5927 struct btrfs_file_extent_item *item;
5928 struct extent_buffer *leaf;
5929 struct btrfs_key found_key;
5930 struct extent_map *em = NULL;
5931 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
5932 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5933 struct btrfs_trans_handle *trans = NULL;
5934 int compress_type;
5935
5936 again:
5937 read_lock(&em_tree->lock);
5938 em = lookup_extent_mapping(em_tree, start, len);
5939 if (em)
5940 em->bdev = root->fs_info->fs_devices->latest_bdev;
5941 read_unlock(&em_tree->lock);
5942
5943 if (em) {
5944 if (em->start > start || em->start + em->len <= start)
5945 free_extent_map(em);
5946 else if (em->block_start == EXTENT_MAP_INLINE && page)
5947 free_extent_map(em);
5948 else
5949 goto out;
5950 }
5951 em = alloc_extent_map();
5952 if (!em) {
5953 err = -ENOMEM;
5954 goto out;
5955 }
5956 em->bdev = root->fs_info->fs_devices->latest_bdev;
5957 em->start = EXTENT_MAP_HOLE;
5958 em->orig_start = EXTENT_MAP_HOLE;
5959 em->len = (u64)-1;
5960 em->block_len = (u64)-1;
5961
5962 if (!path) {
5963 path = btrfs_alloc_path();
5964 if (!path) {
5965 err = -ENOMEM;
5966 goto out;
5967 }
5968 /*
5969 * Chances are we'll be called again, so go ahead and do
5970 * readahead
5971 */
5972 path->reada = 1;
5973 }
5974
5975 ret = btrfs_lookup_file_extent(trans, root, path,
5976 objectid, start, trans != NULL);
5977 if (ret < 0) {
5978 err = ret;
5979 goto out;
5980 }
5981
5982 if (ret != 0) {
5983 if (path->slots[0] == 0)
5984 goto not_found;
5985 path->slots[0]--;
5986 }
5987
5988 leaf = path->nodes[0];
5989 item = btrfs_item_ptr(leaf, path->slots[0],
5990 struct btrfs_file_extent_item);
5991 /* are we inside the extent that was found? */
5992 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5993 found_type = btrfs_key_type(&found_key);
5994 if (found_key.objectid != objectid ||
5995 found_type != BTRFS_EXTENT_DATA_KEY) {
5996 goto not_found;
5997 }
5998
5999 found_type = btrfs_file_extent_type(leaf, item);
6000 extent_start = found_key.offset;
6001 compress_type = btrfs_file_extent_compression(leaf, item);
6002 if (found_type == BTRFS_FILE_EXTENT_REG ||
6003 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
6004 extent_end = extent_start +
6005 btrfs_file_extent_num_bytes(leaf, item);
6006 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6007 size_t size;
6008 size = btrfs_file_extent_inline_len(leaf, item);
6009 extent_end = ALIGN(extent_start + size, root->sectorsize);
6010 }
6011
6012 if (start >= extent_end) {
6013 path->slots[0]++;
6014 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6015 ret = btrfs_next_leaf(root, path);
6016 if (ret < 0) {
6017 err = ret;
6018 goto out;
6019 }
6020 if (ret > 0)
6021 goto not_found;
6022 leaf = path->nodes[0];
6023 }
6024 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6025 if (found_key.objectid != objectid ||
6026 found_key.type != BTRFS_EXTENT_DATA_KEY)
6027 goto not_found;
6028 if (start + len <= found_key.offset)
6029 goto not_found;
6030 em->start = start;
6031 em->orig_start = start;
6032 em->len = found_key.offset - start;
6033 goto not_found_em;
6034 }
6035
6036 em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, item);
6037 if (found_type == BTRFS_FILE_EXTENT_REG ||
6038 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
6039 em->start = extent_start;
6040 em->len = extent_end - extent_start;
6041 em->orig_start = extent_start -
6042 btrfs_file_extent_offset(leaf, item);
6043 em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf,
6044 item);
6045 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
6046 if (bytenr == 0) {
6047 em->block_start = EXTENT_MAP_HOLE;
6048 goto insert;
6049 }
6050 if (compress_type != BTRFS_COMPRESS_NONE) {
6051 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
6052 em->compress_type = compress_type;
6053 em->block_start = bytenr;
6054 em->block_len = em->orig_block_len;
6055 } else {
6056 bytenr += btrfs_file_extent_offset(leaf, item);
6057 em->block_start = bytenr;
6058 em->block_len = em->len;
6059 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
6060 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
6061 }
6062 goto insert;
6063 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6064 unsigned long ptr;
6065 char *map;
6066 size_t size;
6067 size_t extent_offset;
6068 size_t copy_size;
6069
6070 em->block_start = EXTENT_MAP_INLINE;
6071 if (!page || create) {
6072 em->start = extent_start;
6073 em->len = extent_end - extent_start;
6074 goto out;
6075 }
6076
6077 size = btrfs_file_extent_inline_len(leaf, item);
6078 extent_offset = page_offset(page) + pg_offset - extent_start;
6079 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
6080 size - extent_offset);
6081 em->start = extent_start + extent_offset;
6082 em->len = ALIGN(copy_size, root->sectorsize);
6083 em->orig_block_len = em->len;
6084 em->orig_start = em->start;
6085 if (compress_type) {
6086 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
6087 em->compress_type = compress_type;
6088 }
6089 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
6090 if (create == 0 && !PageUptodate(page)) {
6091 if (btrfs_file_extent_compression(leaf, item) !=
6092 BTRFS_COMPRESS_NONE) {
6093 ret = uncompress_inline(path, inode, page,
6094 pg_offset,
6095 extent_offset, item);
6096 BUG_ON(ret); /* -ENOMEM */
6097 } else {
6098 map = kmap(page);
6099 read_extent_buffer(leaf, map + pg_offset, ptr,
6100 copy_size);
6101 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
6102 memset(map + pg_offset + copy_size, 0,
6103 PAGE_CACHE_SIZE - pg_offset -
6104 copy_size);
6105 }
6106 kunmap(page);
6107 }
6108 flush_dcache_page(page);
6109 } else if (create && PageUptodate(page)) {
6110 BUG();
6111 if (!trans) {
6112 kunmap(page);
6113 free_extent_map(em);
6114 em = NULL;
6115
6116 btrfs_release_path(path);
6117 trans = btrfs_join_transaction(root);
6118
6119 if (IS_ERR(trans))
6120 return ERR_CAST(trans);
6121 goto again;
6122 }
6123 map = kmap(page);
6124 write_extent_buffer(leaf, map + pg_offset, ptr,
6125 copy_size);
6126 kunmap(page);
6127 btrfs_mark_buffer_dirty(leaf);
6128 }
6129 set_extent_uptodate(io_tree, em->start,
6130 extent_map_end(em) - 1, NULL, GFP_NOFS);
6131 goto insert;
6132 } else {
6133 WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type);
6134 }
6135 not_found:
6136 em->start = start;
6137 em->orig_start = start;
6138 em->len = len;
6139 not_found_em:
6140 em->block_start = EXTENT_MAP_HOLE;
6141 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
6142 insert:
6143 btrfs_release_path(path);
6144 if (em->start > start || extent_map_end(em) <= start) {
6145 btrfs_err(root->fs_info, "bad extent! em: [%llu %llu] passed [%llu %llu]",
6146 (unsigned long long)em->start,
6147 (unsigned long long)em->len,
6148 (unsigned long long)start,
6149 (unsigned long long)len);
6150 err = -EIO;
6151 goto out;
6152 }
6153
6154 err = 0;
6155 write_lock(&em_tree->lock);
6156 ret = add_extent_mapping(em_tree, em, 0);
6157 /* it is possible that someone inserted the extent into the tree
6158 * while we had the lock dropped. It is also possible that
6159 * an overlapping map exists in the tree
6160 */
6161 if (ret == -EEXIST) {
6162 struct extent_map *existing;
6163
6164 ret = 0;
6165
6166 existing = lookup_extent_mapping(em_tree, start, len);
6167 if (existing && (existing->start > start ||
6168 existing->start + existing->len <= start)) {
6169 free_extent_map(existing);
6170 existing = NULL;
6171 }
6172 if (!existing) {
6173 existing = lookup_extent_mapping(em_tree, em->start,
6174 em->len);
6175 if (existing) {
6176 err = merge_extent_mapping(em_tree, existing,
6177 em, start,
6178 root->sectorsize);
6179 free_extent_map(existing);
6180 if (err) {
6181 free_extent_map(em);
6182 em = NULL;
6183 }
6184 } else {
6185 err = -EIO;
6186 free_extent_map(em);
6187 em = NULL;
6188 }
6189 } else {
6190 free_extent_map(em);
6191 em = existing;
6192 err = 0;
6193 }
6194 }
6195 write_unlock(&em_tree->lock);
6196 out:
6197
6198 if (em)
6199 trace_btrfs_get_extent(root, em);
6200
6201 if (path)
6202 btrfs_free_path(path);
6203 if (trans) {
6204 ret = btrfs_end_transaction(trans, root);
6205 if (!err)
6206 err = ret;
6207 }
6208 if (err) {
6209 free_extent_map(em);
6210 return ERR_PTR(err);
6211 }
6212 BUG_ON(!em); /* Error is always set */
6213 return em;
6214 }
6215
6216 struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
6217 size_t pg_offset, u64 start, u64 len,
6218 int create)
6219 {
6220 struct extent_map *em;
6221 struct extent_map *hole_em = NULL;
6222 u64 range_start = start;
6223 u64 end;
6224 u64 found;
6225 u64 found_end;
6226 int err = 0;
6227
6228 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
6229 if (IS_ERR(em))
6230 return em;
6231 if (em) {
6232 /*
6233 * if our em maps to
6234 * - a hole or
6235 * - a pre-alloc extent,
6236 * there might actually be delalloc bytes behind it.
6237 */
6238 if (em->block_start != EXTENT_MAP_HOLE &&
6239 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6240 return em;
6241 else
6242 hole_em = em;
6243 }
6244
6245 /* check to see if we've wrapped (len == -1 or similar) */
6246 end = start + len;
6247 if (end < start)
6248 end = (u64)-1;
6249 else
6250 end -= 1;
6251
6252 em = NULL;
6253
6254 /* ok, we didn't find anything, lets look for delalloc */
6255 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
6256 end, len, EXTENT_DELALLOC, 1);
6257 found_end = range_start + found;
6258 if (found_end < range_start)
6259 found_end = (u64)-1;
6260
6261 /*
6262 * we didn't find anything useful, return
6263 * the original results from get_extent()
6264 */
6265 if (range_start > end || found_end <= start) {
6266 em = hole_em;
6267 hole_em = NULL;
6268 goto out;
6269 }
6270
6271 /* adjust the range_start to make sure it doesn't
6272 * go backwards from the start they passed in
6273 */
6274 range_start = max(start,range_start);
6275 found = found_end - range_start;
6276
6277 if (found > 0) {
6278 u64 hole_start = start;
6279 u64 hole_len = len;
6280
6281 em = alloc_extent_map();
6282 if (!em) {
6283 err = -ENOMEM;
6284 goto out;
6285 }
6286 /*
6287 * when btrfs_get_extent can't find anything it
6288 * returns one huge hole
6289 *
6290 * make sure what it found really fits our range, and
6291 * adjust to make sure it is based on the start from
6292 * the caller
6293 */
6294 if (hole_em) {
6295 u64 calc_end = extent_map_end(hole_em);
6296
6297 if (calc_end <= start || (hole_em->start > end)) {
6298 free_extent_map(hole_em);
6299 hole_em = NULL;
6300 } else {
6301 hole_start = max(hole_em->start, start);
6302 hole_len = calc_end - hole_start;
6303 }
6304 }
6305 em->bdev = NULL;
6306 if (hole_em && range_start > hole_start) {
6307 /* our hole starts before our delalloc, so we
6308 * have to return just the parts of the hole
6309 * that go until the delalloc starts
6310 */
6311 em->len = min(hole_len,
6312 range_start - hole_start);
6313 em->start = hole_start;
6314 em->orig_start = hole_start;
6315 /*
6316 * don't adjust block start at all,
6317 * it is fixed at EXTENT_MAP_HOLE
6318 */
6319 em->block_start = hole_em->block_start;
6320 em->block_len = hole_len;
6321 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6322 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
6323 } else {
6324 em->start = range_start;
6325 em->len = found;
6326 em->orig_start = range_start;
6327 em->block_start = EXTENT_MAP_DELALLOC;
6328 em->block_len = found;
6329 }
6330 } else if (hole_em) {
6331 return hole_em;
6332 }
6333 out:
6334
6335 free_extent_map(hole_em);
6336 if (err) {
6337 free_extent_map(em);
6338 return ERR_PTR(err);
6339 }
6340 return em;
6341 }
6342
6343 static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6344 u64 start, u64 len)
6345 {
6346 struct btrfs_root *root = BTRFS_I(inode)->root;
6347 struct btrfs_trans_handle *trans;
6348 struct extent_map *em;
6349 struct btrfs_key ins;
6350 u64 alloc_hint;
6351 int ret;
6352
6353 trans = btrfs_join_transaction(root);
6354 if (IS_ERR(trans))
6355 return ERR_CAST(trans);
6356
6357 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
6358
6359 alloc_hint = get_extent_allocation_hint(inode, start, len);
6360 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
6361 alloc_hint, &ins, 1);
6362 if (ret) {
6363 em = ERR_PTR(ret);
6364 goto out;
6365 }
6366
6367 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
6368 ins.offset, ins.offset, ins.offset, 0);
6369 if (IS_ERR(em))
6370 goto out;
6371
6372 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
6373 ins.offset, ins.offset, 0);
6374 if (ret) {
6375 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
6376 em = ERR_PTR(ret);
6377 }
6378 out:
6379 btrfs_end_transaction(trans, root);
6380 return em;
6381 }
6382
6383 /*
6384 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6385 * block must be cow'd
6386 */
6387 noinline int can_nocow_extent(struct btrfs_trans_handle *trans,
6388 struct inode *inode, u64 offset, u64 *len,
6389 u64 *orig_start, u64 *orig_block_len,
6390 u64 *ram_bytes)
6391 {
6392 struct btrfs_path *path;
6393 int ret;
6394 struct extent_buffer *leaf;
6395 struct btrfs_root *root = BTRFS_I(inode)->root;
6396 struct btrfs_file_extent_item *fi;
6397 struct btrfs_key key;
6398 u64 disk_bytenr;
6399 u64 backref_offset;
6400 u64 extent_end;
6401 u64 num_bytes;
6402 int slot;
6403 int found_type;
6404 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
6405 path = btrfs_alloc_path();
6406 if (!path)
6407 return -ENOMEM;
6408
6409 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
6410 offset, 0);
6411 if (ret < 0)
6412 goto out;
6413
6414 slot = path->slots[0];
6415 if (ret == 1) {
6416 if (slot == 0) {
6417 /* can't find the item, must cow */
6418 ret = 0;
6419 goto out;
6420 }
6421 slot--;
6422 }
6423 ret = 0;
6424 leaf = path->nodes[0];
6425 btrfs_item_key_to_cpu(leaf, &key, slot);
6426 if (key.objectid != btrfs_ino(inode) ||
6427 key.type != BTRFS_EXTENT_DATA_KEY) {
6428 /* not our file or wrong item type, must cow */
6429 goto out;
6430 }
6431
6432 if (key.offset > offset) {
6433 /* Wrong offset, must cow */
6434 goto out;
6435 }
6436
6437 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6438 found_type = btrfs_file_extent_type(leaf, fi);
6439 if (found_type != BTRFS_FILE_EXTENT_REG &&
6440 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6441 /* not a regular extent, must cow */
6442 goto out;
6443 }
6444
6445 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
6446 goto out;
6447
6448 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
6449 if (disk_bytenr == 0)
6450 goto out;
6451
6452 if (btrfs_file_extent_compression(leaf, fi) ||
6453 btrfs_file_extent_encryption(leaf, fi) ||
6454 btrfs_file_extent_other_encoding(leaf, fi))
6455 goto out;
6456
6457 backref_offset = btrfs_file_extent_offset(leaf, fi);
6458
6459 if (orig_start) {
6460 *orig_start = key.offset - backref_offset;
6461 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
6462 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6463 }
6464
6465 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
6466
6467 if (btrfs_extent_readonly(root, disk_bytenr))
6468 goto out;
6469
6470 /*
6471 * look for other files referencing this extent, if we
6472 * find any we must cow
6473 */
6474 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
6475 key.offset - backref_offset, disk_bytenr))
6476 goto out;
6477
6478 /*
6479 * adjust disk_bytenr and num_bytes to cover just the bytes
6480 * in this extent we are about to write. If there
6481 * are any csums in that range we have to cow in order
6482 * to keep the csums correct
6483 */
6484 disk_bytenr += backref_offset;
6485 disk_bytenr += offset - key.offset;
6486 num_bytes = min(offset + *len, extent_end) - offset;
6487 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
6488 goto out;
6489 /*
6490 * all of the above have passed, it is safe to overwrite this extent
6491 * without cow
6492 */
6493 *len = num_bytes;
6494 ret = 1;
6495 out:
6496 btrfs_free_path(path);
6497 return ret;
6498 }
6499
6500 static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
6501 struct extent_state **cached_state, int writing)
6502 {
6503 struct btrfs_ordered_extent *ordered;
6504 int ret = 0;
6505
6506 while (1) {
6507 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6508 0, cached_state);
6509 /*
6510 * We're concerned with the entire range that we're going to be
6511 * doing DIO to, so we need to make sure theres no ordered
6512 * extents in this range.
6513 */
6514 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6515 lockend - lockstart + 1);
6516
6517 /*
6518 * We need to make sure there are no buffered pages in this
6519 * range either, we could have raced between the invalidate in
6520 * generic_file_direct_write and locking the extent. The
6521 * invalidate needs to happen so that reads after a write do not
6522 * get stale data.
6523 */
6524 if (!ordered && (!writing ||
6525 !test_range_bit(&BTRFS_I(inode)->io_tree,
6526 lockstart, lockend, EXTENT_UPTODATE, 0,
6527 *cached_state)))
6528 break;
6529
6530 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6531 cached_state, GFP_NOFS);
6532
6533 if (ordered) {
6534 btrfs_start_ordered_extent(inode, ordered, 1);
6535 btrfs_put_ordered_extent(ordered);
6536 } else {
6537 /* Screw you mmap */
6538 ret = filemap_write_and_wait_range(inode->i_mapping,
6539 lockstart,
6540 lockend);
6541 if (ret)
6542 break;
6543
6544 /*
6545 * If we found a page that couldn't be invalidated just
6546 * fall back to buffered.
6547 */
6548 ret = invalidate_inode_pages2_range(inode->i_mapping,
6549 lockstart >> PAGE_CACHE_SHIFT,
6550 lockend >> PAGE_CACHE_SHIFT);
6551 if (ret)
6552 break;
6553 }
6554
6555 cond_resched();
6556 }
6557
6558 return ret;
6559 }
6560
6561 static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
6562 u64 len, u64 orig_start,
6563 u64 block_start, u64 block_len,
6564 u64 orig_block_len, u64 ram_bytes,
6565 int type)
6566 {
6567 struct extent_map_tree *em_tree;
6568 struct extent_map *em;
6569 struct btrfs_root *root = BTRFS_I(inode)->root;
6570 int ret;
6571
6572 em_tree = &BTRFS_I(inode)->extent_tree;
6573 em = alloc_extent_map();
6574 if (!em)
6575 return ERR_PTR(-ENOMEM);
6576
6577 em->start = start;
6578 em->orig_start = orig_start;
6579 em->mod_start = start;
6580 em->mod_len = len;
6581 em->len = len;
6582 em->block_len = block_len;
6583 em->block_start = block_start;
6584 em->bdev = root->fs_info->fs_devices->latest_bdev;
6585 em->orig_block_len = orig_block_len;
6586 em->ram_bytes = ram_bytes;
6587 em->generation = -1;
6588 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6589 if (type == BTRFS_ORDERED_PREALLOC)
6590 set_bit(EXTENT_FLAG_FILLING, &em->flags);
6591
6592 do {
6593 btrfs_drop_extent_cache(inode, em->start,
6594 em->start + em->len - 1, 0);
6595 write_lock(&em_tree->lock);
6596 ret = add_extent_mapping(em_tree, em, 1);
6597 write_unlock(&em_tree->lock);
6598 } while (ret == -EEXIST);
6599
6600 if (ret) {
6601 free_extent_map(em);
6602 return ERR_PTR(ret);
6603 }
6604
6605 return em;
6606 }
6607
6608
6609 static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
6610 struct buffer_head *bh_result, int create)
6611 {
6612 struct extent_map *em;
6613 struct btrfs_root *root = BTRFS_I(inode)->root;
6614 struct extent_state *cached_state = NULL;
6615 u64 start = iblock << inode->i_blkbits;
6616 u64 lockstart, lockend;
6617 u64 len = bh_result->b_size;
6618 struct btrfs_trans_handle *trans;
6619 int unlock_bits = EXTENT_LOCKED;
6620 int ret = 0;
6621
6622 if (create)
6623 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
6624 else
6625 len = min_t(u64, len, root->sectorsize);
6626
6627 lockstart = start;
6628 lockend = start + len - 1;
6629
6630 /*
6631 * If this errors out it's because we couldn't invalidate pagecache for
6632 * this range and we need to fallback to buffered.
6633 */
6634 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
6635 return -ENOTBLK;
6636
6637 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
6638 if (IS_ERR(em)) {
6639 ret = PTR_ERR(em);
6640 goto unlock_err;
6641 }
6642
6643 /*
6644 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6645 * io. INLINE is special, and we could probably kludge it in here, but
6646 * it's still buffered so for safety lets just fall back to the generic
6647 * buffered path.
6648 *
6649 * For COMPRESSED we _have_ to read the entire extent in so we can
6650 * decompress it, so there will be buffering required no matter what we
6651 * do, so go ahead and fallback to buffered.
6652 *
6653 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6654 * to buffered IO. Don't blame me, this is the price we pay for using
6655 * the generic code.
6656 */
6657 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
6658 em->block_start == EXTENT_MAP_INLINE) {
6659 free_extent_map(em);
6660 ret = -ENOTBLK;
6661 goto unlock_err;
6662 }
6663
6664 /* Just a good old fashioned hole, return */
6665 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
6666 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
6667 free_extent_map(em);
6668 goto unlock_err;
6669 }
6670
6671 /*
6672 * We don't allocate a new extent in the following cases
6673 *
6674 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6675 * existing extent.
6676 * 2) The extent is marked as PREALLOC. We're good to go here and can
6677 * just use the extent.
6678 *
6679 */
6680 if (!create) {
6681 len = min(len, em->len - (start - em->start));
6682 lockstart = start + len;
6683 goto unlock;
6684 }
6685
6686 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
6687 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
6688 em->block_start != EXTENT_MAP_HOLE)) {
6689 int type;
6690 int ret;
6691 u64 block_start, orig_start, orig_block_len, ram_bytes;
6692
6693 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6694 type = BTRFS_ORDERED_PREALLOC;
6695 else
6696 type = BTRFS_ORDERED_NOCOW;
6697 len = min(len, em->len - (start - em->start));
6698 block_start = em->block_start + (start - em->start);
6699
6700 /*
6701 * we're not going to log anything, but we do need
6702 * to make sure the current transaction stays open
6703 * while we look for nocow cross refs
6704 */
6705 trans = btrfs_join_transaction(root);
6706 if (IS_ERR(trans))
6707 goto must_cow;
6708
6709 if (can_nocow_extent(trans, inode, start, &len, &orig_start,
6710 &orig_block_len, &ram_bytes) == 1) {
6711 if (type == BTRFS_ORDERED_PREALLOC) {
6712 free_extent_map(em);
6713 em = create_pinned_em(inode, start, len,
6714 orig_start,
6715 block_start, len,
6716 orig_block_len,
6717 ram_bytes, type);
6718 if (IS_ERR(em)) {
6719 btrfs_end_transaction(trans, root);
6720 goto unlock_err;
6721 }
6722 }
6723
6724 ret = btrfs_add_ordered_extent_dio(inode, start,
6725 block_start, len, len, type);
6726 btrfs_end_transaction(trans, root);
6727 if (ret) {
6728 free_extent_map(em);
6729 goto unlock_err;
6730 }
6731 goto unlock;
6732 }
6733 btrfs_end_transaction(trans, root);
6734 }
6735 must_cow:
6736 /*
6737 * this will cow the extent, reset the len in case we changed
6738 * it above
6739 */
6740 len = bh_result->b_size;
6741 free_extent_map(em);
6742 em = btrfs_new_extent_direct(inode, start, len);
6743 if (IS_ERR(em)) {
6744 ret = PTR_ERR(em);
6745 goto unlock_err;
6746 }
6747 len = min(len, em->len - (start - em->start));
6748 unlock:
6749 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
6750 inode->i_blkbits;
6751 bh_result->b_size = len;
6752 bh_result->b_bdev = em->bdev;
6753 set_buffer_mapped(bh_result);
6754 if (create) {
6755 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6756 set_buffer_new(bh_result);
6757
6758 /*
6759 * Need to update the i_size under the extent lock so buffered
6760 * readers will get the updated i_size when we unlock.
6761 */
6762 if (start + len > i_size_read(inode))
6763 i_size_write(inode, start + len);
6764
6765 spin_lock(&BTRFS_I(inode)->lock);
6766 BTRFS_I(inode)->outstanding_extents++;
6767 spin_unlock(&BTRFS_I(inode)->lock);
6768
6769 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6770 lockstart + len - 1, EXTENT_DELALLOC, NULL,
6771 &cached_state, GFP_NOFS);
6772 BUG_ON(ret);
6773 }
6774
6775 /*
6776 * In the case of write we need to clear and unlock the entire range,
6777 * in the case of read we need to unlock only the end area that we
6778 * aren't using if there is any left over space.
6779 */
6780 if (lockstart < lockend) {
6781 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6782 lockend, unlock_bits, 1, 0,
6783 &cached_state, GFP_NOFS);
6784 } else {
6785 free_extent_state(cached_state);
6786 }
6787
6788 free_extent_map(em);
6789
6790 return 0;
6791
6792 unlock_err:
6793 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6794 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
6795 return ret;
6796 }
6797
6798 static void btrfs_endio_direct_read(struct bio *bio, int err)
6799 {
6800 struct btrfs_dio_private *dip = bio->bi_private;
6801 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
6802 struct bio_vec *bvec = bio->bi_io_vec;
6803 struct inode *inode = dip->inode;
6804 struct btrfs_root *root = BTRFS_I(inode)->root;
6805 struct bio *dio_bio;
6806 u32 *csums = (u32 *)dip->csum;
6807 int index = 0;
6808 u64 start;
6809
6810 start = dip->logical_offset;
6811 do {
6812 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
6813 struct page *page = bvec->bv_page;
6814 char *kaddr;
6815 u32 csum = ~(u32)0;
6816 unsigned long flags;
6817
6818 local_irq_save(flags);
6819 kaddr = kmap_atomic(page);
6820 csum = btrfs_csum_data(kaddr + bvec->bv_offset,
6821 csum, bvec->bv_len);
6822 btrfs_csum_final(csum, (char *)&csum);
6823 kunmap_atomic(kaddr);
6824 local_irq_restore(flags);
6825
6826 flush_dcache_page(bvec->bv_page);
6827 if (csum != csums[index]) {
6828 btrfs_err(root->fs_info, "csum failed ino %llu off %llu csum %u expected csum %u",
6829 (unsigned long long)btrfs_ino(inode),
6830 (unsigned long long)start,
6831 csum, csums[index]);
6832 err = -EIO;
6833 }
6834 }
6835
6836 start += bvec->bv_len;
6837 bvec++;
6838 index++;
6839 } while (bvec <= bvec_end);
6840
6841 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
6842 dip->logical_offset + dip->bytes - 1);
6843 dio_bio = dip->dio_bio;
6844
6845 kfree(dip);
6846
6847 /* If we had a csum failure make sure to clear the uptodate flag */
6848 if (err)
6849 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
6850 dio_end_io(dio_bio, err);
6851 bio_put(bio);
6852 }
6853
6854 static void btrfs_endio_direct_write(struct bio *bio, int err)
6855 {
6856 struct btrfs_dio_private *dip = bio->bi_private;
6857 struct inode *inode = dip->inode;
6858 struct btrfs_root *root = BTRFS_I(inode)->root;
6859 struct btrfs_ordered_extent *ordered = NULL;
6860 u64 ordered_offset = dip->logical_offset;
6861 u64 ordered_bytes = dip->bytes;
6862 struct bio *dio_bio;
6863 int ret;
6864
6865 if (err)
6866 goto out_done;
6867 again:
6868 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
6869 &ordered_offset,
6870 ordered_bytes, !err);
6871 if (!ret)
6872 goto out_test;
6873
6874 ordered->work.func = finish_ordered_fn;
6875 ordered->work.flags = 0;
6876 btrfs_queue_worker(&root->fs_info->endio_write_workers,
6877 &ordered->work);
6878 out_test:
6879 /*
6880 * our bio might span multiple ordered extents. If we haven't
6881 * completed the accounting for the whole dio, go back and try again
6882 */
6883 if (ordered_offset < dip->logical_offset + dip->bytes) {
6884 ordered_bytes = dip->logical_offset + dip->bytes -
6885 ordered_offset;
6886 ordered = NULL;
6887 goto again;
6888 }
6889 out_done:
6890 dio_bio = dip->dio_bio;
6891
6892 kfree(dip);
6893
6894 /* If we had an error make sure to clear the uptodate flag */
6895 if (err)
6896 clear_bit(BIO_UPTODATE, &dio_bio->bi_flags);
6897 dio_end_io(dio_bio, err);
6898 bio_put(bio);
6899 }
6900
6901 static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
6902 struct bio *bio, int mirror_num,
6903 unsigned long bio_flags, u64 offset)
6904 {
6905 int ret;
6906 struct btrfs_root *root = BTRFS_I(inode)->root;
6907 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
6908 BUG_ON(ret); /* -ENOMEM */
6909 return 0;
6910 }
6911
6912 static void btrfs_end_dio_bio(struct bio *bio, int err)
6913 {
6914 struct btrfs_dio_private *dip = bio->bi_private;
6915
6916 if (err) {
6917 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
6918 "sector %#Lx len %u err no %d\n",
6919 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
6920 (unsigned long long)bio->bi_sector, bio->bi_size, err);
6921 dip->errors = 1;
6922
6923 /*
6924 * before atomic variable goto zero, we must make sure
6925 * dip->errors is perceived to be set.
6926 */
6927 smp_mb__before_atomic_dec();
6928 }
6929
6930 /* if there are more bios still pending for this dio, just exit */
6931 if (!atomic_dec_and_test(&dip->pending_bios))
6932 goto out;
6933
6934 if (dip->errors) {
6935 bio_io_error(dip->orig_bio);
6936 } else {
6937 set_bit(BIO_UPTODATE, &dip->dio_bio->bi_flags);
6938 bio_endio(dip->orig_bio, 0);
6939 }
6940 out:
6941 bio_put(bio);
6942 }
6943
6944 static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
6945 u64 first_sector, gfp_t gfp_flags)
6946 {
6947 int nr_vecs = bio_get_nr_vecs(bdev);
6948 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
6949 }
6950
6951 static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
6952 int rw, u64 file_offset, int skip_sum,
6953 int async_submit)
6954 {
6955 struct btrfs_dio_private *dip = bio->bi_private;
6956 int write = rw & REQ_WRITE;
6957 struct btrfs_root *root = BTRFS_I(inode)->root;
6958 int ret;
6959
6960 if (async_submit)
6961 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
6962
6963 bio_get(bio);
6964
6965 if (!write) {
6966 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
6967 if (ret)
6968 goto err;
6969 }
6970
6971 if (skip_sum)
6972 goto map;
6973
6974 if (write && async_submit) {
6975 ret = btrfs_wq_submit_bio(root->fs_info,
6976 inode, rw, bio, 0, 0,
6977 file_offset,
6978 __btrfs_submit_bio_start_direct_io,
6979 __btrfs_submit_bio_done);
6980 goto err;
6981 } else if (write) {
6982 /*
6983 * If we aren't doing async submit, calculate the csum of the
6984 * bio now.
6985 */
6986 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
6987 if (ret)
6988 goto err;
6989 } else if (!skip_sum) {
6990 ret = btrfs_lookup_bio_sums_dio(root, inode, dip, bio,
6991 file_offset);
6992 if (ret)
6993 goto err;
6994 }
6995
6996 map:
6997 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
6998 err:
6999 bio_put(bio);
7000 return ret;
7001 }
7002
7003 static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
7004 int skip_sum)
7005 {
7006 struct inode *inode = dip->inode;
7007 struct btrfs_root *root = BTRFS_I(inode)->root;
7008 struct bio *bio;
7009 struct bio *orig_bio = dip->orig_bio;
7010 struct bio_vec *bvec = orig_bio->bi_io_vec;
7011 u64 start_sector = orig_bio->bi_sector;
7012 u64 file_offset = dip->logical_offset;
7013 u64 submit_len = 0;
7014 u64 map_length;
7015 int nr_pages = 0;
7016 int ret = 0;
7017 int async_submit = 0;
7018
7019 map_length = orig_bio->bi_size;
7020 ret = btrfs_map_block(root->fs_info, rw, start_sector << 9,
7021 &map_length, NULL, 0);
7022 if (ret) {
7023 bio_put(orig_bio);
7024 return -EIO;
7025 }
7026
7027 if (map_length >= orig_bio->bi_size) {
7028 bio = orig_bio;
7029 goto submit;
7030 }
7031
7032 /* async crcs make it difficult to collect full stripe writes. */
7033 if (btrfs_get_alloc_profile(root, 1) &
7034 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6))
7035 async_submit = 0;
7036 else
7037 async_submit = 1;
7038
7039 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
7040 if (!bio)
7041 return -ENOMEM;
7042 bio->bi_private = dip;
7043 bio->bi_end_io = btrfs_end_dio_bio;
7044 atomic_inc(&dip->pending_bios);
7045
7046 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
7047 if (unlikely(map_length < submit_len + bvec->bv_len ||
7048 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
7049 bvec->bv_offset) < bvec->bv_len)) {
7050 /*
7051 * inc the count before we submit the bio so
7052 * we know the end IO handler won't happen before
7053 * we inc the count. Otherwise, the dip might get freed
7054 * before we're done setting it up
7055 */
7056 atomic_inc(&dip->pending_bios);
7057 ret = __btrfs_submit_dio_bio(bio, inode, rw,
7058 file_offset, skip_sum,
7059 async_submit);
7060 if (ret) {
7061 bio_put(bio);
7062 atomic_dec(&dip->pending_bios);
7063 goto out_err;
7064 }
7065
7066 start_sector += submit_len >> 9;
7067 file_offset += submit_len;
7068
7069 submit_len = 0;
7070 nr_pages = 0;
7071
7072 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
7073 start_sector, GFP_NOFS);
7074 if (!bio)
7075 goto out_err;
7076 bio->bi_private = dip;
7077 bio->bi_end_io = btrfs_end_dio_bio;
7078
7079 map_length = orig_bio->bi_size;
7080 ret = btrfs_map_block(root->fs_info, rw,
7081 start_sector << 9,
7082 &map_length, NULL, 0);
7083 if (ret) {
7084 bio_put(bio);
7085 goto out_err;
7086 }
7087 } else {
7088 submit_len += bvec->bv_len;
7089 nr_pages ++;
7090 bvec++;
7091 }
7092 }
7093
7094 submit:
7095 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
7096 async_submit);
7097 if (!ret)
7098 return 0;
7099
7100 bio_put(bio);
7101 out_err:
7102 dip->errors = 1;
7103 /*
7104 * before atomic variable goto zero, we must
7105 * make sure dip->errors is perceived to be set.
7106 */
7107 smp_mb__before_atomic_dec();
7108 if (atomic_dec_and_test(&dip->pending_bios))
7109 bio_io_error(dip->orig_bio);
7110
7111 /* bio_end_io() will handle error, so we needn't return it */
7112 return 0;
7113 }
7114
7115 static void btrfs_submit_direct(int rw, struct bio *dio_bio,
7116 struct inode *inode, loff_t file_offset)
7117 {
7118 struct btrfs_root *root = BTRFS_I(inode)->root;
7119 struct btrfs_dio_private *dip;
7120 struct bio *io_bio;
7121 int skip_sum;
7122 int sum_len;
7123 int write = rw & REQ_WRITE;
7124 int ret = 0;
7125 u16 csum_size;
7126
7127 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7128
7129 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
7130 if (!io_bio) {
7131 ret = -ENOMEM;
7132 goto free_ordered;
7133 }
7134
7135 if (!skip_sum && !write) {
7136 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
7137 sum_len = dio_bio->bi_size >> inode->i_sb->s_blocksize_bits;
7138 sum_len *= csum_size;
7139 } else {
7140 sum_len = 0;
7141 }
7142
7143 dip = kmalloc(sizeof(*dip) + sum_len, GFP_NOFS);
7144 if (!dip) {
7145 ret = -ENOMEM;
7146 goto free_io_bio;
7147 }
7148
7149 dip->private = dio_bio->bi_private;
7150 dip->inode = inode;
7151 dip->logical_offset = file_offset;
7152 dip->bytes = dio_bio->bi_size;
7153 dip->disk_bytenr = (u64)dio_bio->bi_sector << 9;
7154 io_bio->bi_private = dip;
7155 dip->errors = 0;
7156 dip->orig_bio = io_bio;
7157 dip->dio_bio = dio_bio;
7158 atomic_set(&dip->pending_bios, 0);
7159
7160 if (write)
7161 io_bio->bi_end_io = btrfs_endio_direct_write;
7162 else
7163 io_bio->bi_end_io = btrfs_endio_direct_read;
7164
7165 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
7166 if (!ret)
7167 return;
7168
7169 free_io_bio:
7170 bio_put(io_bio);
7171
7172 free_ordered:
7173 /*
7174 * If this is a write, we need to clean up the reserved space and kill
7175 * the ordered extent.
7176 */
7177 if (write) {
7178 struct btrfs_ordered_extent *ordered;
7179 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
7180 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
7181 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
7182 btrfs_free_reserved_extent(root, ordered->start,
7183 ordered->disk_len);
7184 btrfs_put_ordered_extent(ordered);
7185 btrfs_put_ordered_extent(ordered);
7186 }
7187 bio_endio(dio_bio, ret);
7188 }
7189
7190 static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
7191 const struct iovec *iov, loff_t offset,
7192 unsigned long nr_segs)
7193 {
7194 int seg;
7195 int i;
7196 size_t size;
7197 unsigned long addr;
7198 unsigned blocksize_mask = root->sectorsize - 1;
7199 ssize_t retval = -EINVAL;
7200 loff_t end = offset;
7201
7202 if (offset & blocksize_mask)
7203 goto out;
7204
7205 /* Check the memory alignment. Blocks cannot straddle pages */
7206 for (seg = 0; seg < nr_segs; seg++) {
7207 addr = (unsigned long)iov[seg].iov_base;
7208 size = iov[seg].iov_len;
7209 end += size;
7210 if ((addr & blocksize_mask) || (size & blocksize_mask))
7211 goto out;
7212
7213 /* If this is a write we don't need to check anymore */
7214 if (rw & WRITE)
7215 continue;
7216
7217 /*
7218 * Check to make sure we don't have duplicate iov_base's in this
7219 * iovec, if so return EINVAL, otherwise we'll get csum errors
7220 * when reading back.
7221 */
7222 for (i = seg + 1; i < nr_segs; i++) {
7223 if (iov[seg].iov_base == iov[i].iov_base)
7224 goto out;
7225 }
7226 }
7227 retval = 0;
7228 out:
7229 return retval;
7230 }
7231
7232 static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
7233 const struct iovec *iov, loff_t offset,
7234 unsigned long nr_segs)
7235 {
7236 struct file *file = iocb->ki_filp;
7237 struct inode *inode = file->f_mapping->host;
7238 size_t count = 0;
7239 int flags = 0;
7240 bool wakeup = true;
7241 bool relock = false;
7242 ssize_t ret;
7243
7244 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
7245 offset, nr_segs))
7246 return 0;
7247
7248 atomic_inc(&inode->i_dio_count);
7249 smp_mb__after_atomic_inc();
7250
7251 /*
7252 * The generic stuff only does filemap_write_and_wait_range, which isn't
7253 * enough if we've written compressed pages to this area, so we need to
7254 * call btrfs_wait_ordered_range to make absolutely sure that any
7255 * outstanding dirty pages are on disk.
7256 */
7257 count = iov_length(iov, nr_segs);
7258 btrfs_wait_ordered_range(inode, offset, count);
7259
7260 if (rw & WRITE) {
7261 /*
7262 * If the write DIO is beyond the EOF, we need update
7263 * the isize, but it is protected by i_mutex. So we can
7264 * not unlock the i_mutex at this case.
7265 */
7266 if (offset + count <= inode->i_size) {
7267 mutex_unlock(&inode->i_mutex);
7268 relock = true;
7269 }
7270 ret = btrfs_delalloc_reserve_space(inode, count);
7271 if (ret)
7272 goto out;
7273 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7274 &BTRFS_I(inode)->runtime_flags))) {
7275 inode_dio_done(inode);
7276 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7277 wakeup = false;
7278 }
7279
7280 ret = __blockdev_direct_IO(rw, iocb, inode,
7281 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
7282 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
7283 btrfs_submit_direct, flags);
7284 if (rw & WRITE) {
7285 if (ret < 0 && ret != -EIOCBQUEUED)
7286 btrfs_delalloc_release_space(inode, count);
7287 else if (ret >= 0 && (size_t)ret < count)
7288 btrfs_delalloc_release_space(inode,
7289 count - (size_t)ret);
7290 else
7291 btrfs_delalloc_release_metadata(inode, 0);
7292 }
7293 out:
7294 if (wakeup)
7295 inode_dio_done(inode);
7296 if (relock)
7297 mutex_lock(&inode->i_mutex);
7298
7299 return ret;
7300 }
7301
7302 #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7303
7304 static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
7305 __u64 start, __u64 len)
7306 {
7307 int ret;
7308
7309 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
7310 if (ret)
7311 return ret;
7312
7313 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
7314 }
7315
7316 int btrfs_readpage(struct file *file, struct page *page)
7317 {
7318 struct extent_io_tree *tree;
7319 tree = &BTRFS_I(page->mapping->host)->io_tree;
7320 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
7321 }
7322
7323 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
7324 {
7325 struct extent_io_tree *tree;
7326
7327
7328 if (current->flags & PF_MEMALLOC) {
7329 redirty_page_for_writepage(wbc, page);
7330 unlock_page(page);
7331 return 0;
7332 }
7333 tree = &BTRFS_I(page->mapping->host)->io_tree;
7334 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
7335 }
7336
7337 static int btrfs_writepages(struct address_space *mapping,
7338 struct writeback_control *wbc)
7339 {
7340 struct extent_io_tree *tree;
7341
7342 tree = &BTRFS_I(mapping->host)->io_tree;
7343 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
7344 }
7345
7346 static int
7347 btrfs_readpages(struct file *file, struct address_space *mapping,
7348 struct list_head *pages, unsigned nr_pages)
7349 {
7350 struct extent_io_tree *tree;
7351 tree = &BTRFS_I(mapping->host)->io_tree;
7352 return extent_readpages(tree, mapping, pages, nr_pages,
7353 btrfs_get_extent);
7354 }
7355 static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
7356 {
7357 struct extent_io_tree *tree;
7358 struct extent_map_tree *map;
7359 int ret;
7360
7361 tree = &BTRFS_I(page->mapping->host)->io_tree;
7362 map = &BTRFS_I(page->mapping->host)->extent_tree;
7363 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
7364 if (ret == 1) {
7365 ClearPagePrivate(page);
7366 set_page_private(page, 0);
7367 page_cache_release(page);
7368 }
7369 return ret;
7370 }
7371
7372 static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
7373 {
7374 if (PageWriteback(page) || PageDirty(page))
7375 return 0;
7376 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
7377 }
7378
7379 static void btrfs_invalidatepage(struct page *page, unsigned int offset,
7380 unsigned int length)
7381 {
7382 struct inode *inode = page->mapping->host;
7383 struct extent_io_tree *tree;
7384 struct btrfs_ordered_extent *ordered;
7385 struct extent_state *cached_state = NULL;
7386 u64 page_start = page_offset(page);
7387 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
7388
7389 /*
7390 * we have the page locked, so new writeback can't start,
7391 * and the dirty bit won't be cleared while we are here.
7392 *
7393 * Wait for IO on this page so that we can safely clear
7394 * the PagePrivate2 bit and do ordered accounting
7395 */
7396 wait_on_page_writeback(page);
7397
7398 tree = &BTRFS_I(inode)->io_tree;
7399 if (offset) {
7400 btrfs_releasepage(page, GFP_NOFS);
7401 return;
7402 }
7403 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
7404 ordered = btrfs_lookup_ordered_extent(inode, page_offset(page));
7405 if (ordered) {
7406 /*
7407 * IO on this page will never be started, so we need
7408 * to account for any ordered extents now
7409 */
7410 clear_extent_bit(tree, page_start, page_end,
7411 EXTENT_DIRTY | EXTENT_DELALLOC |
7412 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
7413 EXTENT_DEFRAG, 1, 0, &cached_state, GFP_NOFS);
7414 /*
7415 * whoever cleared the private bit is responsible
7416 * for the finish_ordered_io
7417 */
7418 if (TestClearPagePrivate2(page) &&
7419 btrfs_dec_test_ordered_pending(inode, &ordered, page_start,
7420 PAGE_CACHE_SIZE, 1)) {
7421 btrfs_finish_ordered_io(ordered);
7422 }
7423 btrfs_put_ordered_extent(ordered);
7424 cached_state = NULL;
7425 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
7426 }
7427 clear_extent_bit(tree, page_start, page_end,
7428 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
7429 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
7430 &cached_state, GFP_NOFS);
7431 __btrfs_releasepage(page, GFP_NOFS);
7432
7433 ClearPageChecked(page);
7434 if (PagePrivate(page)) {
7435 ClearPagePrivate(page);
7436 set_page_private(page, 0);
7437 page_cache_release(page);
7438 }
7439 }
7440
7441 /*
7442 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7443 * called from a page fault handler when a page is first dirtied. Hence we must
7444 * be careful to check for EOF conditions here. We set the page up correctly
7445 * for a written page which means we get ENOSPC checking when writing into
7446 * holes and correct delalloc and unwritten extent mapping on filesystems that
7447 * support these features.
7448 *
7449 * We are not allowed to take the i_mutex here so we have to play games to
7450 * protect against truncate races as the page could now be beyond EOF. Because
7451 * vmtruncate() writes the inode size before removing pages, once we have the
7452 * page lock we can determine safely if the page is beyond EOF. If it is not
7453 * beyond EOF, then the page is guaranteed safe against truncation until we
7454 * unlock the page.
7455 */
7456 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
7457 {
7458 struct page *page = vmf->page;
7459 struct inode *inode = file_inode(vma->vm_file);
7460 struct btrfs_root *root = BTRFS_I(inode)->root;
7461 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7462 struct btrfs_ordered_extent *ordered;
7463 struct extent_state *cached_state = NULL;
7464 char *kaddr;
7465 unsigned long zero_start;
7466 loff_t size;
7467 int ret;
7468 int reserved = 0;
7469 u64 page_start;
7470 u64 page_end;
7471
7472 sb_start_pagefault(inode->i_sb);
7473 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
7474 if (!ret) {
7475 ret = file_update_time(vma->vm_file);
7476 reserved = 1;
7477 }
7478 if (ret) {
7479 if (ret == -ENOMEM)
7480 ret = VM_FAULT_OOM;
7481 else /* -ENOSPC, -EIO, etc */
7482 ret = VM_FAULT_SIGBUS;
7483 if (reserved)
7484 goto out;
7485 goto out_noreserve;
7486 }
7487
7488 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
7489 again:
7490 lock_page(page);
7491 size = i_size_read(inode);
7492 page_start = page_offset(page);
7493 page_end = page_start + PAGE_CACHE_SIZE - 1;
7494
7495 if ((page->mapping != inode->i_mapping) ||
7496 (page_start >= size)) {
7497 /* page got truncated out from underneath us */
7498 goto out_unlock;
7499 }
7500 wait_on_page_writeback(page);
7501
7502 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
7503 set_page_extent_mapped(page);
7504
7505 /*
7506 * we can't set the delalloc bits if there are pending ordered
7507 * extents. Drop our locks and wait for them to finish
7508 */
7509 ordered = btrfs_lookup_ordered_extent(inode, page_start);
7510 if (ordered) {
7511 unlock_extent_cached(io_tree, page_start, page_end,
7512 &cached_state, GFP_NOFS);
7513 unlock_page(page);
7514 btrfs_start_ordered_extent(inode, ordered, 1);
7515 btrfs_put_ordered_extent(ordered);
7516 goto again;
7517 }
7518
7519 /*
7520 * XXX - page_mkwrite gets called every time the page is dirtied, even
7521 * if it was already dirty, so for space accounting reasons we need to
7522 * clear any delalloc bits for the range we are fixing to save. There
7523 * is probably a better way to do this, but for now keep consistent with
7524 * prepare_pages in the normal write path.
7525 */
7526 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
7527 EXTENT_DIRTY | EXTENT_DELALLOC |
7528 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
7529 0, 0, &cached_state, GFP_NOFS);
7530
7531 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
7532 &cached_state);
7533 if (ret) {
7534 unlock_extent_cached(io_tree, page_start, page_end,
7535 &cached_state, GFP_NOFS);
7536 ret = VM_FAULT_SIGBUS;
7537 goto out_unlock;
7538 }
7539 ret = 0;
7540
7541 /* page is wholly or partially inside EOF */
7542 if (page_start + PAGE_CACHE_SIZE > size)
7543 zero_start = size & ~PAGE_CACHE_MASK;
7544 else
7545 zero_start = PAGE_CACHE_SIZE;
7546
7547 if (zero_start != PAGE_CACHE_SIZE) {
7548 kaddr = kmap(page);
7549 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
7550 flush_dcache_page(page);
7551 kunmap(page);
7552 }
7553 ClearPageChecked(page);
7554 set_page_dirty(page);
7555 SetPageUptodate(page);
7556
7557 BTRFS_I(inode)->last_trans = root->fs_info->generation;
7558 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
7559 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
7560
7561 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
7562
7563 out_unlock:
7564 if (!ret) {
7565 sb_end_pagefault(inode->i_sb);
7566 return VM_FAULT_LOCKED;
7567 }
7568 unlock_page(page);
7569 out:
7570 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
7571 out_noreserve:
7572 sb_end_pagefault(inode->i_sb);
7573 return ret;
7574 }
7575
7576 static int btrfs_truncate(struct inode *inode)
7577 {
7578 struct btrfs_root *root = BTRFS_I(inode)->root;
7579 struct btrfs_block_rsv *rsv;
7580 int ret = 0;
7581 int err = 0;
7582 struct btrfs_trans_handle *trans;
7583 u64 mask = root->sectorsize - 1;
7584 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
7585
7586 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
7587 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
7588
7589 /*
7590 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
7591 * 3 things going on here
7592 *
7593 * 1) We need to reserve space for our orphan item and the space to
7594 * delete our orphan item. Lord knows we don't want to have a dangling
7595 * orphan item because we didn't reserve space to remove it.
7596 *
7597 * 2) We need to reserve space to update our inode.
7598 *
7599 * 3) We need to have something to cache all the space that is going to
7600 * be free'd up by the truncate operation, but also have some slack
7601 * space reserved in case it uses space during the truncate (thank you
7602 * very much snapshotting).
7603 *
7604 * And we need these to all be seperate. The fact is we can use alot of
7605 * space doing the truncate, and we have no earthly idea how much space
7606 * we will use, so we need the truncate reservation to be seperate so it
7607 * doesn't end up using space reserved for updating the inode or
7608 * removing the orphan item. We also need to be able to stop the
7609 * transaction and start a new one, which means we need to be able to
7610 * update the inode several times, and we have no idea of knowing how
7611 * many times that will be, so we can't just reserve 1 item for the
7612 * entirety of the opration, so that has to be done seperately as well.
7613 * Then there is the orphan item, which does indeed need to be held on
7614 * to for the whole operation, and we need nobody to touch this reserved
7615 * space except the orphan code.
7616 *
7617 * So that leaves us with
7618 *
7619 * 1) root->orphan_block_rsv - for the orphan deletion.
7620 * 2) rsv - for the truncate reservation, which we will steal from the
7621 * transaction reservation.
7622 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7623 * updating the inode.
7624 */
7625 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
7626 if (!rsv)
7627 return -ENOMEM;
7628 rsv->size = min_size;
7629 rsv->failfast = 1;
7630
7631 /*
7632 * 1 for the truncate slack space
7633 * 1 for updating the inode.
7634 */
7635 trans = btrfs_start_transaction(root, 2);
7636 if (IS_ERR(trans)) {
7637 err = PTR_ERR(trans);
7638 goto out;
7639 }
7640
7641 /* Migrate the slack space for the truncate to our reserve */
7642 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
7643 min_size);
7644 BUG_ON(ret);
7645
7646 /*
7647 * setattr is responsible for setting the ordered_data_close flag,
7648 * but that is only tested during the last file release. That
7649 * could happen well after the next commit, leaving a great big
7650 * window where new writes may get lost if someone chooses to write
7651 * to this file after truncating to zero
7652 *
7653 * The inode doesn't have any dirty data here, and so if we commit
7654 * this is a noop. If someone immediately starts writing to the inode
7655 * it is very likely we'll catch some of their writes in this
7656 * transaction, and the commit will find this file on the ordered
7657 * data list with good things to send down.
7658 *
7659 * This is a best effort solution, there is still a window where
7660 * using truncate to replace the contents of the file will
7661 * end up with a zero length file after a crash.
7662 */
7663 if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
7664 &BTRFS_I(inode)->runtime_flags))
7665 btrfs_add_ordered_operation(trans, root, inode);
7666
7667 /*
7668 * So if we truncate and then write and fsync we normally would just
7669 * write the extents that changed, which is a problem if we need to
7670 * first truncate that entire inode. So set this flag so we write out
7671 * all of the extents in the inode to the sync log so we're completely
7672 * safe.
7673 */
7674 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
7675 trans->block_rsv = rsv;
7676
7677 while (1) {
7678 ret = btrfs_truncate_inode_items(trans, root, inode,
7679 inode->i_size,
7680 BTRFS_EXTENT_DATA_KEY);
7681 if (ret != -ENOSPC) {
7682 err = ret;
7683 break;
7684 }
7685
7686 trans->block_rsv = &root->fs_info->trans_block_rsv;
7687 ret = btrfs_update_inode(trans, root, inode);
7688 if (ret) {
7689 err = ret;
7690 break;
7691 }
7692
7693 btrfs_end_transaction(trans, root);
7694 btrfs_btree_balance_dirty(root);
7695
7696 trans = btrfs_start_transaction(root, 2);
7697 if (IS_ERR(trans)) {
7698 ret = err = PTR_ERR(trans);
7699 trans = NULL;
7700 break;
7701 }
7702
7703 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
7704 rsv, min_size);
7705 BUG_ON(ret); /* shouldn't happen */
7706 trans->block_rsv = rsv;
7707 }
7708
7709 if (ret == 0 && inode->i_nlink > 0) {
7710 trans->block_rsv = root->orphan_block_rsv;
7711 ret = btrfs_orphan_del(trans, inode);
7712 if (ret)
7713 err = ret;
7714 }
7715
7716 if (trans) {
7717 trans->block_rsv = &root->fs_info->trans_block_rsv;
7718 ret = btrfs_update_inode(trans, root, inode);
7719 if (ret && !err)
7720 err = ret;
7721
7722 ret = btrfs_end_transaction(trans, root);
7723 btrfs_btree_balance_dirty(root);
7724 }
7725
7726 out:
7727 btrfs_free_block_rsv(root, rsv);
7728
7729 if (ret && !err)
7730 err = ret;
7731
7732 return err;
7733 }
7734
7735 /*
7736 * create a new subvolume directory/inode (helper for the ioctl).
7737 */
7738 int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
7739 struct btrfs_root *new_root, u64 new_dirid)
7740 {
7741 struct inode *inode;
7742 int err;
7743 u64 index = 0;
7744
7745 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
7746 new_dirid, new_dirid,
7747 S_IFDIR | (~current_umask() & S_IRWXUGO),
7748 &index);
7749 if (IS_ERR(inode))
7750 return PTR_ERR(inode);
7751 inode->i_op = &btrfs_dir_inode_operations;
7752 inode->i_fop = &btrfs_dir_file_operations;
7753
7754 set_nlink(inode, 1);
7755 btrfs_i_size_write(inode, 0);
7756
7757 err = btrfs_update_inode(trans, new_root, inode);
7758
7759 iput(inode);
7760 return err;
7761 }
7762
7763 struct inode *btrfs_alloc_inode(struct super_block *sb)
7764 {
7765 struct btrfs_inode *ei;
7766 struct inode *inode;
7767
7768 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
7769 if (!ei)
7770 return NULL;
7771
7772 ei->root = NULL;
7773 ei->generation = 0;
7774 ei->last_trans = 0;
7775 ei->last_sub_trans = 0;
7776 ei->logged_trans = 0;
7777 ei->delalloc_bytes = 0;
7778 ei->disk_i_size = 0;
7779 ei->flags = 0;
7780 ei->csum_bytes = 0;
7781 ei->index_cnt = (u64)-1;
7782 ei->last_unlink_trans = 0;
7783 ei->last_log_commit = 0;
7784
7785 spin_lock_init(&ei->lock);
7786 ei->outstanding_extents = 0;
7787 ei->reserved_extents = 0;
7788
7789 ei->runtime_flags = 0;
7790 ei->force_compress = BTRFS_COMPRESS_NONE;
7791
7792 ei->delayed_node = NULL;
7793
7794 inode = &ei->vfs_inode;
7795 extent_map_tree_init(&ei->extent_tree);
7796 extent_io_tree_init(&ei->io_tree, &inode->i_data);
7797 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
7798 ei->io_tree.track_uptodate = 1;
7799 ei->io_failure_tree.track_uptodate = 1;
7800 atomic_set(&ei->sync_writers, 0);
7801 mutex_init(&ei->log_mutex);
7802 mutex_init(&ei->delalloc_mutex);
7803 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
7804 INIT_LIST_HEAD(&ei->delalloc_inodes);
7805 INIT_LIST_HEAD(&ei->ordered_operations);
7806 RB_CLEAR_NODE(&ei->rb_node);
7807
7808 return inode;
7809 }
7810
7811 static void btrfs_i_callback(struct rcu_head *head)
7812 {
7813 struct inode *inode = container_of(head, struct inode, i_rcu);
7814 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
7815 }
7816
7817 void btrfs_destroy_inode(struct inode *inode)
7818 {
7819 struct btrfs_ordered_extent *ordered;
7820 struct btrfs_root *root = BTRFS_I(inode)->root;
7821
7822 WARN_ON(!hlist_empty(&inode->i_dentry));
7823 WARN_ON(inode->i_data.nrpages);
7824 WARN_ON(BTRFS_I(inode)->outstanding_extents);
7825 WARN_ON(BTRFS_I(inode)->reserved_extents);
7826 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
7827 WARN_ON(BTRFS_I(inode)->csum_bytes);
7828
7829 /*
7830 * This can happen where we create an inode, but somebody else also
7831 * created the same inode and we need to destroy the one we already
7832 * created.
7833 */
7834 if (!root)
7835 goto free;
7836
7837 /*
7838 * Make sure we're properly removed from the ordered operation
7839 * lists.
7840 */
7841 smp_mb();
7842 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
7843 spin_lock(&root->fs_info->ordered_root_lock);
7844 list_del_init(&BTRFS_I(inode)->ordered_operations);
7845 spin_unlock(&root->fs_info->ordered_root_lock);
7846 }
7847
7848 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
7849 &BTRFS_I(inode)->runtime_flags)) {
7850 btrfs_info(root->fs_info, "inode %llu still on the orphan list",
7851 (unsigned long long)btrfs_ino(inode));
7852 atomic_dec(&root->orphan_inodes);
7853 }
7854
7855 while (1) {
7856 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
7857 if (!ordered)
7858 break;
7859 else {
7860 btrfs_err(root->fs_info, "found ordered extent %llu %llu on inode cleanup",
7861 (unsigned long long)ordered->file_offset,
7862 (unsigned long long)ordered->len);
7863 btrfs_remove_ordered_extent(inode, ordered);
7864 btrfs_put_ordered_extent(ordered);
7865 btrfs_put_ordered_extent(ordered);
7866 }
7867 }
7868 inode_tree_del(inode);
7869 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
7870 free:
7871 call_rcu(&inode->i_rcu, btrfs_i_callback);
7872 }
7873
7874 int btrfs_drop_inode(struct inode *inode)
7875 {
7876 struct btrfs_root *root = BTRFS_I(inode)->root;
7877
7878 if (root == NULL)
7879 return 1;
7880
7881 /* the snap/subvol tree is on deleting */
7882 if (btrfs_root_refs(&root->root_item) == 0 &&
7883 root != root->fs_info->tree_root)
7884 return 1;
7885 else
7886 return generic_drop_inode(inode);
7887 }
7888
7889 static void init_once(void *foo)
7890 {
7891 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
7892
7893 inode_init_once(&ei->vfs_inode);
7894 }
7895
7896 void btrfs_destroy_cachep(void)
7897 {
7898 /*
7899 * Make sure all delayed rcu free inodes are flushed before we
7900 * destroy cache.
7901 */
7902 rcu_barrier();
7903 if (btrfs_inode_cachep)
7904 kmem_cache_destroy(btrfs_inode_cachep);
7905 if (btrfs_trans_handle_cachep)
7906 kmem_cache_destroy(btrfs_trans_handle_cachep);
7907 if (btrfs_transaction_cachep)
7908 kmem_cache_destroy(btrfs_transaction_cachep);
7909 if (btrfs_path_cachep)
7910 kmem_cache_destroy(btrfs_path_cachep);
7911 if (btrfs_free_space_cachep)
7912 kmem_cache_destroy(btrfs_free_space_cachep);
7913 if (btrfs_delalloc_work_cachep)
7914 kmem_cache_destroy(btrfs_delalloc_work_cachep);
7915 }
7916
7917 int btrfs_init_cachep(void)
7918 {
7919 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
7920 sizeof(struct btrfs_inode), 0,
7921 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
7922 if (!btrfs_inode_cachep)
7923 goto fail;
7924
7925 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
7926 sizeof(struct btrfs_trans_handle), 0,
7927 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7928 if (!btrfs_trans_handle_cachep)
7929 goto fail;
7930
7931 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
7932 sizeof(struct btrfs_transaction), 0,
7933 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7934 if (!btrfs_transaction_cachep)
7935 goto fail;
7936
7937 btrfs_path_cachep = kmem_cache_create("btrfs_path",
7938 sizeof(struct btrfs_path), 0,
7939 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7940 if (!btrfs_path_cachep)
7941 goto fail;
7942
7943 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
7944 sizeof(struct btrfs_free_space), 0,
7945 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7946 if (!btrfs_free_space_cachep)
7947 goto fail;
7948
7949 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
7950 sizeof(struct btrfs_delalloc_work), 0,
7951 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
7952 NULL);
7953 if (!btrfs_delalloc_work_cachep)
7954 goto fail;
7955
7956 return 0;
7957 fail:
7958 btrfs_destroy_cachep();
7959 return -ENOMEM;
7960 }
7961
7962 static int btrfs_getattr(struct vfsmount *mnt,
7963 struct dentry *dentry, struct kstat *stat)
7964 {
7965 u64 delalloc_bytes;
7966 struct inode *inode = dentry->d_inode;
7967 u32 blocksize = inode->i_sb->s_blocksize;
7968
7969 generic_fillattr(inode, stat);
7970 stat->dev = BTRFS_I(inode)->root->anon_dev;
7971 stat->blksize = PAGE_CACHE_SIZE;
7972
7973 spin_lock(&BTRFS_I(inode)->lock);
7974 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
7975 spin_unlock(&BTRFS_I(inode)->lock);
7976 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
7977 ALIGN(delalloc_bytes, blocksize)) >> 9;
7978 return 0;
7979 }
7980
7981 static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7982 struct inode *new_dir, struct dentry *new_dentry)
7983 {
7984 struct btrfs_trans_handle *trans;
7985 struct btrfs_root *root = BTRFS_I(old_dir)->root;
7986 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
7987 struct inode *new_inode = new_dentry->d_inode;
7988 struct inode *old_inode = old_dentry->d_inode;
7989 struct timespec ctime = CURRENT_TIME;
7990 u64 index = 0;
7991 u64 root_objectid;
7992 int ret;
7993 u64 old_ino = btrfs_ino(old_inode);
7994
7995 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
7996 return -EPERM;
7997
7998 /* we only allow rename subvolume link between subvolumes */
7999 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
8000 return -EXDEV;
8001
8002 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
8003 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
8004 return -ENOTEMPTY;
8005
8006 if (S_ISDIR(old_inode->i_mode) && new_inode &&
8007 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
8008 return -ENOTEMPTY;
8009
8010
8011 /* check for collisions, even if the name isn't there */
8012 ret = btrfs_check_dir_item_collision(root, new_dir->i_ino,
8013 new_dentry->d_name.name,
8014 new_dentry->d_name.len);
8015
8016 if (ret) {
8017 if (ret == -EEXIST) {
8018 /* we shouldn't get
8019 * eexist without a new_inode */
8020 if (!new_inode) {
8021 WARN_ON(1);
8022 return ret;
8023 }
8024 } else {
8025 /* maybe -EOVERFLOW */
8026 return ret;
8027 }
8028 }
8029 ret = 0;
8030
8031 /*
8032 * we're using rename to replace one file with another.
8033 * and the replacement file is large. Start IO on it now so
8034 * we don't add too much work to the end of the transaction
8035 */
8036 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
8037 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
8038 filemap_flush(old_inode->i_mapping);
8039
8040 /* close the racy window with snapshot create/destroy ioctl */
8041 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
8042 down_read(&root->fs_info->subvol_sem);
8043 /*
8044 * We want to reserve the absolute worst case amount of items. So if
8045 * both inodes are subvols and we need to unlink them then that would
8046 * require 4 item modifications, but if they are both normal inodes it
8047 * would require 5 item modifications, so we'll assume their normal
8048 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8049 * should cover the worst case number of items we'll modify.
8050 */
8051 trans = btrfs_start_transaction(root, 11);
8052 if (IS_ERR(trans)) {
8053 ret = PTR_ERR(trans);
8054 goto out_notrans;
8055 }
8056
8057 if (dest != root)
8058 btrfs_record_root_in_trans(trans, dest);
8059
8060 ret = btrfs_set_inode_index(new_dir, &index);
8061 if (ret)
8062 goto out_fail;
8063
8064 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
8065 /* force full log commit if subvolume involved. */
8066 root->fs_info->last_trans_log_full_commit = trans->transid;
8067 } else {
8068 ret = btrfs_insert_inode_ref(trans, dest,
8069 new_dentry->d_name.name,
8070 new_dentry->d_name.len,
8071 old_ino,
8072 btrfs_ino(new_dir), index);
8073 if (ret)
8074 goto out_fail;
8075 /*
8076 * this is an ugly little race, but the rename is required
8077 * to make sure that if we crash, the inode is either at the
8078 * old name or the new one. pinning the log transaction lets
8079 * us make sure we don't allow a log commit to come in after
8080 * we unlink the name but before we add the new name back in.
8081 */
8082 btrfs_pin_log_trans(root);
8083 }
8084 /*
8085 * make sure the inode gets flushed if it is replacing
8086 * something.
8087 */
8088 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
8089 btrfs_add_ordered_operation(trans, root, old_inode);
8090
8091 inode_inc_iversion(old_dir);
8092 inode_inc_iversion(new_dir);
8093 inode_inc_iversion(old_inode);
8094 old_dir->i_ctime = old_dir->i_mtime = ctime;
8095 new_dir->i_ctime = new_dir->i_mtime = ctime;
8096 old_inode->i_ctime = ctime;
8097
8098 if (old_dentry->d_parent != new_dentry->d_parent)
8099 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
8100
8101 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
8102 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
8103 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
8104 old_dentry->d_name.name,
8105 old_dentry->d_name.len);
8106 } else {
8107 ret = __btrfs_unlink_inode(trans, root, old_dir,
8108 old_dentry->d_inode,
8109 old_dentry->d_name.name,
8110 old_dentry->d_name.len);
8111 if (!ret)
8112 ret = btrfs_update_inode(trans, root, old_inode);
8113 }
8114 if (ret) {
8115 btrfs_abort_transaction(trans, root, ret);
8116 goto out_fail;
8117 }
8118
8119 if (new_inode) {
8120 inode_inc_iversion(new_inode);
8121 new_inode->i_ctime = CURRENT_TIME;
8122 if (unlikely(btrfs_ino(new_inode) ==
8123 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
8124 root_objectid = BTRFS_I(new_inode)->location.objectid;
8125 ret = btrfs_unlink_subvol(trans, dest, new_dir,
8126 root_objectid,
8127 new_dentry->d_name.name,
8128 new_dentry->d_name.len);
8129 BUG_ON(new_inode->i_nlink == 0);
8130 } else {
8131 ret = btrfs_unlink_inode(trans, dest, new_dir,
8132 new_dentry->d_inode,
8133 new_dentry->d_name.name,
8134 new_dentry->d_name.len);
8135 }
8136 if (!ret && new_inode->i_nlink == 0)
8137 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
8138 if (ret) {
8139 btrfs_abort_transaction(trans, root, ret);
8140 goto out_fail;
8141 }
8142 }
8143
8144 ret = btrfs_add_link(trans, new_dir, old_inode,
8145 new_dentry->d_name.name,
8146 new_dentry->d_name.len, 0, index);
8147 if (ret) {
8148 btrfs_abort_transaction(trans, root, ret);
8149 goto out_fail;
8150 }
8151
8152 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
8153 struct dentry *parent = new_dentry->d_parent;
8154 btrfs_log_new_name(trans, old_inode, old_dir, parent);
8155 btrfs_end_log_trans(root);
8156 }
8157 out_fail:
8158 btrfs_end_transaction(trans, root);
8159 out_notrans:
8160 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
8161 up_read(&root->fs_info->subvol_sem);
8162
8163 return ret;
8164 }
8165
8166 static void btrfs_run_delalloc_work(struct btrfs_work *work)
8167 {
8168 struct btrfs_delalloc_work *delalloc_work;
8169
8170 delalloc_work = container_of(work, struct btrfs_delalloc_work,
8171 work);
8172 if (delalloc_work->wait)
8173 btrfs_wait_ordered_range(delalloc_work->inode, 0, (u64)-1);
8174 else
8175 filemap_flush(delalloc_work->inode->i_mapping);
8176
8177 if (delalloc_work->delay_iput)
8178 btrfs_add_delayed_iput(delalloc_work->inode);
8179 else
8180 iput(delalloc_work->inode);
8181 complete(&delalloc_work->completion);
8182 }
8183
8184 struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
8185 int wait, int delay_iput)
8186 {
8187 struct btrfs_delalloc_work *work;
8188
8189 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
8190 if (!work)
8191 return NULL;
8192
8193 init_completion(&work->completion);
8194 INIT_LIST_HEAD(&work->list);
8195 work->inode = inode;
8196 work->wait = wait;
8197 work->delay_iput = delay_iput;
8198 work->work.func = btrfs_run_delalloc_work;
8199
8200 return work;
8201 }
8202
8203 void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
8204 {
8205 wait_for_completion(&work->completion);
8206 kmem_cache_free(btrfs_delalloc_work_cachep, work);
8207 }
8208
8209 /*
8210 * some fairly slow code that needs optimization. This walks the list
8211 * of all the inodes with pending delalloc and forces them to disk.
8212 */
8213 static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
8214 {
8215 struct btrfs_inode *binode;
8216 struct inode *inode;
8217 struct btrfs_delalloc_work *work, *next;
8218 struct list_head works;
8219 struct list_head splice;
8220 int ret = 0;
8221
8222 INIT_LIST_HEAD(&works);
8223 INIT_LIST_HEAD(&splice);
8224
8225 spin_lock(&root->delalloc_lock);
8226 list_splice_init(&root->delalloc_inodes, &splice);
8227 while (!list_empty(&splice)) {
8228 binode = list_entry(splice.next, struct btrfs_inode,
8229 delalloc_inodes);
8230
8231 list_move_tail(&binode->delalloc_inodes,
8232 &root->delalloc_inodes);
8233 inode = igrab(&binode->vfs_inode);
8234 if (!inode) {
8235 cond_resched_lock(&root->delalloc_lock);
8236 continue;
8237 }
8238 spin_unlock(&root->delalloc_lock);
8239
8240 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
8241 if (unlikely(!work)) {
8242 ret = -ENOMEM;
8243 goto out;
8244 }
8245 list_add_tail(&work->list, &works);
8246 btrfs_queue_worker(&root->fs_info->flush_workers,
8247 &work->work);
8248
8249 cond_resched();
8250 spin_lock(&root->delalloc_lock);
8251 }
8252 spin_unlock(&root->delalloc_lock);
8253
8254 list_for_each_entry_safe(work, next, &works, list) {
8255 list_del_init(&work->list);
8256 btrfs_wait_and_free_delalloc_work(work);
8257 }
8258 return 0;
8259 out:
8260 list_for_each_entry_safe(work, next, &works, list) {
8261 list_del_init(&work->list);
8262 btrfs_wait_and_free_delalloc_work(work);
8263 }
8264
8265 if (!list_empty_careful(&splice)) {
8266 spin_lock(&root->delalloc_lock);
8267 list_splice_tail(&splice, &root->delalloc_inodes);
8268 spin_unlock(&root->delalloc_lock);
8269 }
8270 return ret;
8271 }
8272
8273 int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
8274 {
8275 int ret;
8276
8277 if (root->fs_info->sb->s_flags & MS_RDONLY)
8278 return -EROFS;
8279
8280 ret = __start_delalloc_inodes(root, delay_iput);
8281 /*
8282 * the filemap_flush will queue IO into the worker threads, but
8283 * we have to make sure the IO is actually started and that
8284 * ordered extents get created before we return
8285 */
8286 atomic_inc(&root->fs_info->async_submit_draining);
8287 while (atomic_read(&root->fs_info->nr_async_submits) ||
8288 atomic_read(&root->fs_info->async_delalloc_pages)) {
8289 wait_event(root->fs_info->async_submit_wait,
8290 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
8291 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
8292 }
8293 atomic_dec(&root->fs_info->async_submit_draining);
8294 return ret;
8295 }
8296
8297 int btrfs_start_all_delalloc_inodes(struct btrfs_fs_info *fs_info,
8298 int delay_iput)
8299 {
8300 struct btrfs_root *root;
8301 struct list_head splice;
8302 int ret;
8303
8304 if (fs_info->sb->s_flags & MS_RDONLY)
8305 return -EROFS;
8306
8307 INIT_LIST_HEAD(&splice);
8308
8309 spin_lock(&fs_info->delalloc_root_lock);
8310 list_splice_init(&fs_info->delalloc_roots, &splice);
8311 while (!list_empty(&splice)) {
8312 root = list_first_entry(&splice, struct btrfs_root,
8313 delalloc_root);
8314 root = btrfs_grab_fs_root(root);
8315 BUG_ON(!root);
8316 list_move_tail(&root->delalloc_root,
8317 &fs_info->delalloc_roots);
8318 spin_unlock(&fs_info->delalloc_root_lock);
8319
8320 ret = __start_delalloc_inodes(root, delay_iput);
8321 btrfs_put_fs_root(root);
8322 if (ret)
8323 goto out;
8324
8325 spin_lock(&fs_info->delalloc_root_lock);
8326 }
8327 spin_unlock(&fs_info->delalloc_root_lock);
8328
8329 atomic_inc(&fs_info->async_submit_draining);
8330 while (atomic_read(&fs_info->nr_async_submits) ||
8331 atomic_read(&fs_info->async_delalloc_pages)) {
8332 wait_event(fs_info->async_submit_wait,
8333 (atomic_read(&fs_info->nr_async_submits) == 0 &&
8334 atomic_read(&fs_info->async_delalloc_pages) == 0));
8335 }
8336 atomic_dec(&fs_info->async_submit_draining);
8337 return 0;
8338 out:
8339 if (!list_empty_careful(&splice)) {
8340 spin_lock(&fs_info->delalloc_root_lock);
8341 list_splice_tail(&splice, &fs_info->delalloc_roots);
8342 spin_unlock(&fs_info->delalloc_root_lock);
8343 }
8344 return ret;
8345 }
8346
8347 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
8348 const char *symname)
8349 {
8350 struct btrfs_trans_handle *trans;
8351 struct btrfs_root *root = BTRFS_I(dir)->root;
8352 struct btrfs_path *path;
8353 struct btrfs_key key;
8354 struct inode *inode = NULL;
8355 int err;
8356 int drop_inode = 0;
8357 u64 objectid;
8358 u64 index = 0 ;
8359 int name_len;
8360 int datasize;
8361 unsigned long ptr;
8362 struct btrfs_file_extent_item *ei;
8363 struct extent_buffer *leaf;
8364
8365 name_len = strlen(symname) + 1;
8366 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
8367 return -ENAMETOOLONG;
8368
8369 /*
8370 * 2 items for inode item and ref
8371 * 2 items for dir items
8372 * 1 item for xattr if selinux is on
8373 */
8374 trans = btrfs_start_transaction(root, 5);
8375 if (IS_ERR(trans))
8376 return PTR_ERR(trans);
8377
8378 err = btrfs_find_free_ino(root, &objectid);
8379 if (err)
8380 goto out_unlock;
8381
8382 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
8383 dentry->d_name.len, btrfs_ino(dir), objectid,
8384 S_IFLNK|S_IRWXUGO, &index);
8385 if (IS_ERR(inode)) {
8386 err = PTR_ERR(inode);
8387 goto out_unlock;
8388 }
8389
8390 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
8391 if (err) {
8392 drop_inode = 1;
8393 goto out_unlock;
8394 }
8395
8396 /*
8397 * If the active LSM wants to access the inode during
8398 * d_instantiate it needs these. Smack checks to see
8399 * if the filesystem supports xattrs by looking at the
8400 * ops vector.
8401 */
8402 inode->i_fop = &btrfs_file_operations;
8403 inode->i_op = &btrfs_file_inode_operations;
8404
8405 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
8406 if (err)
8407 drop_inode = 1;
8408 else {
8409 inode->i_mapping->a_ops = &btrfs_aops;
8410 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
8411 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
8412 }
8413 if (drop_inode)
8414 goto out_unlock;
8415
8416 path = btrfs_alloc_path();
8417 if (!path) {
8418 err = -ENOMEM;
8419 drop_inode = 1;
8420 goto out_unlock;
8421 }
8422 key.objectid = btrfs_ino(inode);
8423 key.offset = 0;
8424 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
8425 datasize = btrfs_file_extent_calc_inline_size(name_len);
8426 err = btrfs_insert_empty_item(trans, root, path, &key,
8427 datasize);
8428 if (err) {
8429 drop_inode = 1;
8430 btrfs_free_path(path);
8431 goto out_unlock;
8432 }
8433 leaf = path->nodes[0];
8434 ei = btrfs_item_ptr(leaf, path->slots[0],
8435 struct btrfs_file_extent_item);
8436 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
8437 btrfs_set_file_extent_type(leaf, ei,
8438 BTRFS_FILE_EXTENT_INLINE);
8439 btrfs_set_file_extent_encryption(leaf, ei, 0);
8440 btrfs_set_file_extent_compression(leaf, ei, 0);
8441 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
8442 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
8443
8444 ptr = btrfs_file_extent_inline_start(ei);
8445 write_extent_buffer(leaf, symname, ptr, name_len);
8446 btrfs_mark_buffer_dirty(leaf);
8447 btrfs_free_path(path);
8448
8449 inode->i_op = &btrfs_symlink_inode_operations;
8450 inode->i_mapping->a_ops = &btrfs_symlink_aops;
8451 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
8452 inode_set_bytes(inode, name_len);
8453 btrfs_i_size_write(inode, name_len - 1);
8454 err = btrfs_update_inode(trans, root, inode);
8455 if (err)
8456 drop_inode = 1;
8457
8458 out_unlock:
8459 if (!err)
8460 d_instantiate(dentry, inode);
8461 btrfs_end_transaction(trans, root);
8462 if (drop_inode) {
8463 inode_dec_link_count(inode);
8464 iput(inode);
8465 }
8466 btrfs_btree_balance_dirty(root);
8467 return err;
8468 }
8469
8470 static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
8471 u64 start, u64 num_bytes, u64 min_size,
8472 loff_t actual_len, u64 *alloc_hint,
8473 struct btrfs_trans_handle *trans)
8474 {
8475 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
8476 struct extent_map *em;
8477 struct btrfs_root *root = BTRFS_I(inode)->root;
8478 struct btrfs_key ins;
8479 u64 cur_offset = start;
8480 u64 i_size;
8481 u64 cur_bytes;
8482 int ret = 0;
8483 bool own_trans = true;
8484
8485 if (trans)
8486 own_trans = false;
8487 while (num_bytes > 0) {
8488 if (own_trans) {
8489 trans = btrfs_start_transaction(root, 3);
8490 if (IS_ERR(trans)) {
8491 ret = PTR_ERR(trans);
8492 break;
8493 }
8494 }
8495
8496 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
8497 cur_bytes = max(cur_bytes, min_size);
8498 ret = btrfs_reserve_extent(trans, root, cur_bytes,
8499 min_size, 0, *alloc_hint, &ins, 1);
8500 if (ret) {
8501 if (own_trans)
8502 btrfs_end_transaction(trans, root);
8503 break;
8504 }
8505
8506 ret = insert_reserved_file_extent(trans, inode,
8507 cur_offset, ins.objectid,
8508 ins.offset, ins.offset,
8509 ins.offset, 0, 0, 0,
8510 BTRFS_FILE_EXTENT_PREALLOC);
8511 if (ret) {
8512 btrfs_abort_transaction(trans, root, ret);
8513 if (own_trans)
8514 btrfs_end_transaction(trans, root);
8515 break;
8516 }
8517 btrfs_drop_extent_cache(inode, cur_offset,
8518 cur_offset + ins.offset -1, 0);
8519
8520 em = alloc_extent_map();
8521 if (!em) {
8522 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
8523 &BTRFS_I(inode)->runtime_flags);
8524 goto next;
8525 }
8526
8527 em->start = cur_offset;
8528 em->orig_start = cur_offset;
8529 em->len = ins.offset;
8530 em->block_start = ins.objectid;
8531 em->block_len = ins.offset;
8532 em->orig_block_len = ins.offset;
8533 em->ram_bytes = ins.offset;
8534 em->bdev = root->fs_info->fs_devices->latest_bdev;
8535 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
8536 em->generation = trans->transid;
8537
8538 while (1) {
8539 write_lock(&em_tree->lock);
8540 ret = add_extent_mapping(em_tree, em, 1);
8541 write_unlock(&em_tree->lock);
8542 if (ret != -EEXIST)
8543 break;
8544 btrfs_drop_extent_cache(inode, cur_offset,
8545 cur_offset + ins.offset - 1,
8546 0);
8547 }
8548 free_extent_map(em);
8549 next:
8550 num_bytes -= ins.offset;
8551 cur_offset += ins.offset;
8552 *alloc_hint = ins.objectid + ins.offset;
8553
8554 inode_inc_iversion(inode);
8555 inode->i_ctime = CURRENT_TIME;
8556 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
8557 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
8558 (actual_len > inode->i_size) &&
8559 (cur_offset > inode->i_size)) {
8560 if (cur_offset > actual_len)
8561 i_size = actual_len;
8562 else
8563 i_size = cur_offset;
8564 i_size_write(inode, i_size);
8565 btrfs_ordered_update_i_size(inode, i_size, NULL);
8566 }
8567
8568 ret = btrfs_update_inode(trans, root, inode);
8569
8570 if (ret) {
8571 btrfs_abort_transaction(trans, root, ret);
8572 if (own_trans)
8573 btrfs_end_transaction(trans, root);
8574 break;
8575 }
8576
8577 if (own_trans)
8578 btrfs_end_transaction(trans, root);
8579 }
8580 return ret;
8581 }
8582
8583 int btrfs_prealloc_file_range(struct inode *inode, int mode,
8584 u64 start, u64 num_bytes, u64 min_size,
8585 loff_t actual_len, u64 *alloc_hint)
8586 {
8587 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8588 min_size, actual_len, alloc_hint,
8589 NULL);
8590 }
8591
8592 int btrfs_prealloc_file_range_trans(struct inode *inode,
8593 struct btrfs_trans_handle *trans, int mode,
8594 u64 start, u64 num_bytes, u64 min_size,
8595 loff_t actual_len, u64 *alloc_hint)
8596 {
8597 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
8598 min_size, actual_len, alloc_hint, trans);
8599 }
8600
8601 static int btrfs_set_page_dirty(struct page *page)
8602 {
8603 return __set_page_dirty_nobuffers(page);
8604 }
8605
8606 static int btrfs_permission(struct inode *inode, int mask)
8607 {
8608 struct btrfs_root *root = BTRFS_I(inode)->root;
8609 umode_t mode = inode->i_mode;
8610
8611 if (mask & MAY_WRITE &&
8612 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
8613 if (btrfs_root_readonly(root))
8614 return -EROFS;
8615 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
8616 return -EACCES;
8617 }
8618 return generic_permission(inode, mask);
8619 }
8620
8621 static const struct inode_operations btrfs_dir_inode_operations = {
8622 .getattr = btrfs_getattr,
8623 .lookup = btrfs_lookup,
8624 .create = btrfs_create,
8625 .unlink = btrfs_unlink,
8626 .link = btrfs_link,
8627 .mkdir = btrfs_mkdir,
8628 .rmdir = btrfs_rmdir,
8629 .rename = btrfs_rename,
8630 .symlink = btrfs_symlink,
8631 .setattr = btrfs_setattr,
8632 .mknod = btrfs_mknod,
8633 .setxattr = btrfs_setxattr,
8634 .getxattr = btrfs_getxattr,
8635 .listxattr = btrfs_listxattr,
8636 .removexattr = btrfs_removexattr,
8637 .permission = btrfs_permission,
8638 .get_acl = btrfs_get_acl,
8639 };
8640 static const struct inode_operations btrfs_dir_ro_inode_operations = {
8641 .lookup = btrfs_lookup,
8642 .permission = btrfs_permission,
8643 .get_acl = btrfs_get_acl,
8644 };
8645
8646 static const struct file_operations btrfs_dir_file_operations = {
8647 .llseek = generic_file_llseek,
8648 .read = generic_read_dir,
8649 .iterate = btrfs_real_readdir,
8650 .unlocked_ioctl = btrfs_ioctl,
8651 #ifdef CONFIG_COMPAT
8652 .compat_ioctl = btrfs_ioctl,
8653 #endif
8654 .release = btrfs_release_file,
8655 .fsync = btrfs_sync_file,
8656 };
8657
8658 static struct extent_io_ops btrfs_extent_io_ops = {
8659 .fill_delalloc = run_delalloc_range,
8660 .submit_bio_hook = btrfs_submit_bio_hook,
8661 .merge_bio_hook = btrfs_merge_bio_hook,
8662 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
8663 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
8664 .writepage_start_hook = btrfs_writepage_start_hook,
8665 .set_bit_hook = btrfs_set_bit_hook,
8666 .clear_bit_hook = btrfs_clear_bit_hook,
8667 .merge_extent_hook = btrfs_merge_extent_hook,
8668 .split_extent_hook = btrfs_split_extent_hook,
8669 };
8670
8671 /*
8672 * btrfs doesn't support the bmap operation because swapfiles
8673 * use bmap to make a mapping of extents in the file. They assume
8674 * these extents won't change over the life of the file and they
8675 * use the bmap result to do IO directly to the drive.
8676 *
8677 * the btrfs bmap call would return logical addresses that aren't
8678 * suitable for IO and they also will change frequently as COW
8679 * operations happen. So, swapfile + btrfs == corruption.
8680 *
8681 * For now we're avoiding this by dropping bmap.
8682 */
8683 static const struct address_space_operations btrfs_aops = {
8684 .readpage = btrfs_readpage,
8685 .writepage = btrfs_writepage,
8686 .writepages = btrfs_writepages,
8687 .readpages = btrfs_readpages,
8688 .direct_IO = btrfs_direct_IO,
8689 .invalidatepage = btrfs_invalidatepage,
8690 .releasepage = btrfs_releasepage,
8691 .set_page_dirty = btrfs_set_page_dirty,
8692 .error_remove_page = generic_error_remove_page,
8693 };
8694
8695 static const struct address_space_operations btrfs_symlink_aops = {
8696 .readpage = btrfs_readpage,
8697 .writepage = btrfs_writepage,
8698 .invalidatepage = btrfs_invalidatepage,
8699 .releasepage = btrfs_releasepage,
8700 };
8701
8702 static const struct inode_operations btrfs_file_inode_operations = {
8703 .getattr = btrfs_getattr,
8704 .setattr = btrfs_setattr,
8705 .setxattr = btrfs_setxattr,
8706 .getxattr = btrfs_getxattr,
8707 .listxattr = btrfs_listxattr,
8708 .removexattr = btrfs_removexattr,
8709 .permission = btrfs_permission,
8710 .fiemap = btrfs_fiemap,
8711 .get_acl = btrfs_get_acl,
8712 .update_time = btrfs_update_time,
8713 };
8714 static const struct inode_operations btrfs_special_inode_operations = {
8715 .getattr = btrfs_getattr,
8716 .setattr = btrfs_setattr,
8717 .permission = btrfs_permission,
8718 .setxattr = btrfs_setxattr,
8719 .getxattr = btrfs_getxattr,
8720 .listxattr = btrfs_listxattr,
8721 .removexattr = btrfs_removexattr,
8722 .get_acl = btrfs_get_acl,
8723 .update_time = btrfs_update_time,
8724 };
8725 static const struct inode_operations btrfs_symlink_inode_operations = {
8726 .readlink = generic_readlink,
8727 .follow_link = page_follow_link_light,
8728 .put_link = page_put_link,
8729 .getattr = btrfs_getattr,
8730 .setattr = btrfs_setattr,
8731 .permission = btrfs_permission,
8732 .setxattr = btrfs_setxattr,
8733 .getxattr = btrfs_getxattr,
8734 .listxattr = btrfs_listxattr,
8735 .removexattr = btrfs_removexattr,
8736 .get_acl = btrfs_get_acl,
8737 .update_time = btrfs_update_time,
8738 };
8739
8740 const struct dentry_operations btrfs_dentry_operations = {
8741 .d_delete = btrfs_dentry_delete,
8742 .d_release = btrfs_dentry_release,
8743 };
This page took 0.54385 seconds and 5 git commands to generate.