]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - fs/btrfs/inode.c
drm/vc4: Add support for feeding DSI encoders from the pixel valve.
[mirror_ubuntu-zesty-kernel.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/compat.h>
34 #include <linux/bit_spinlock.h>
35 #include <linux/xattr.h>
36 #include <linux/posix_acl.h>
37 #include <linux/falloc.h>
38 #include <linux/slab.h>
39 #include <linux/ratelimit.h>
40 #include <linux/mount.h>
41 #include <linux/btrfs.h>
42 #include <linux/blkdev.h>
43 #include <linux/posix_acl_xattr.h>
44 #include <linux/uio.h>
45 #include "ctree.h"
46 #include "disk-io.h"
47 #include "transaction.h"
48 #include "btrfs_inode.h"
49 #include "print-tree.h"
50 #include "ordered-data.h"
51 #include "xattr.h"
52 #include "tree-log.h"
53 #include "volumes.h"
54 #include "compression.h"
55 #include "locking.h"
56 #include "free-space-cache.h"
57 #include "inode-map.h"
58 #include "backref.h"
59 #include "hash.h"
60 #include "props.h"
61 #include "qgroup.h"
62 #include "dedupe.h"
63
64 struct btrfs_iget_args {
65 struct btrfs_key *location;
66 struct btrfs_root *root;
67 };
68
69 struct btrfs_dio_data {
70 u64 outstanding_extents;
71 u64 reserve;
72 u64 unsubmitted_oe_range_start;
73 u64 unsubmitted_oe_range_end;
74 };
75
76 static const struct inode_operations btrfs_dir_inode_operations;
77 static const struct inode_operations btrfs_symlink_inode_operations;
78 static const struct inode_operations btrfs_dir_ro_inode_operations;
79 static const struct inode_operations btrfs_special_inode_operations;
80 static const struct inode_operations btrfs_file_inode_operations;
81 static const struct address_space_operations btrfs_aops;
82 static const struct address_space_operations btrfs_symlink_aops;
83 static const struct file_operations btrfs_dir_file_operations;
84 static const struct extent_io_ops btrfs_extent_io_ops;
85
86 static struct kmem_cache *btrfs_inode_cachep;
87 struct kmem_cache *btrfs_trans_handle_cachep;
88 struct kmem_cache *btrfs_transaction_cachep;
89 struct kmem_cache *btrfs_path_cachep;
90 struct kmem_cache *btrfs_free_space_cachep;
91
92 #define S_SHIFT 12
93 static const unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
94 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
95 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
96 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
97 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
98 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
99 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
100 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
101 };
102
103 static int btrfs_setsize(struct inode *inode, struct iattr *attr);
104 static int btrfs_truncate(struct inode *inode);
105 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
106 static noinline int cow_file_range(struct inode *inode,
107 struct page *locked_page,
108 u64 start, u64 end, u64 delalloc_end,
109 int *page_started, unsigned long *nr_written,
110 int unlock, struct btrfs_dedupe_hash *hash);
111 static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
112 u64 len, u64 orig_start,
113 u64 block_start, u64 block_len,
114 u64 orig_block_len, u64 ram_bytes,
115 int type);
116
117 static int btrfs_dirty_inode(struct inode *inode);
118
119 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
120 void btrfs_test_inode_set_ops(struct inode *inode)
121 {
122 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
123 }
124 #endif
125
126 static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
127 struct inode *inode, struct inode *dir,
128 const struct qstr *qstr)
129 {
130 int err;
131
132 err = btrfs_init_acl(trans, inode, dir);
133 if (!err)
134 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
135 return err;
136 }
137
138 /*
139 * this does all the hard work for inserting an inline extent into
140 * the btree. The caller should have done a btrfs_drop_extents so that
141 * no overlapping inline items exist in the btree
142 */
143 static int insert_inline_extent(struct btrfs_trans_handle *trans,
144 struct btrfs_path *path, int extent_inserted,
145 struct btrfs_root *root, struct inode *inode,
146 u64 start, size_t size, size_t compressed_size,
147 int compress_type,
148 struct page **compressed_pages)
149 {
150 struct extent_buffer *leaf;
151 struct page *page = NULL;
152 char *kaddr;
153 unsigned long ptr;
154 struct btrfs_file_extent_item *ei;
155 int err = 0;
156 int ret;
157 size_t cur_size = size;
158 unsigned long offset;
159
160 if (compressed_size && compressed_pages)
161 cur_size = compressed_size;
162
163 inode_add_bytes(inode, size);
164
165 if (!extent_inserted) {
166 struct btrfs_key key;
167 size_t datasize;
168
169 key.objectid = btrfs_ino(inode);
170 key.offset = start;
171 key.type = BTRFS_EXTENT_DATA_KEY;
172
173 datasize = btrfs_file_extent_calc_inline_size(cur_size);
174 path->leave_spinning = 1;
175 ret = btrfs_insert_empty_item(trans, root, path, &key,
176 datasize);
177 if (ret) {
178 err = ret;
179 goto fail;
180 }
181 }
182 leaf = path->nodes[0];
183 ei = btrfs_item_ptr(leaf, path->slots[0],
184 struct btrfs_file_extent_item);
185 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
186 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
187 btrfs_set_file_extent_encryption(leaf, ei, 0);
188 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
189 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
190 ptr = btrfs_file_extent_inline_start(ei);
191
192 if (compress_type != BTRFS_COMPRESS_NONE) {
193 struct page *cpage;
194 int i = 0;
195 while (compressed_size > 0) {
196 cpage = compressed_pages[i];
197 cur_size = min_t(unsigned long, compressed_size,
198 PAGE_SIZE);
199
200 kaddr = kmap_atomic(cpage);
201 write_extent_buffer(leaf, kaddr, ptr, cur_size);
202 kunmap_atomic(kaddr);
203
204 i++;
205 ptr += cur_size;
206 compressed_size -= cur_size;
207 }
208 btrfs_set_file_extent_compression(leaf, ei,
209 compress_type);
210 } else {
211 page = find_get_page(inode->i_mapping,
212 start >> PAGE_SHIFT);
213 btrfs_set_file_extent_compression(leaf, ei, 0);
214 kaddr = kmap_atomic(page);
215 offset = start & (PAGE_SIZE - 1);
216 write_extent_buffer(leaf, kaddr + offset, ptr, size);
217 kunmap_atomic(kaddr);
218 put_page(page);
219 }
220 btrfs_mark_buffer_dirty(leaf);
221 btrfs_release_path(path);
222
223 /*
224 * we're an inline extent, so nobody can
225 * extend the file past i_size without locking
226 * a page we already have locked.
227 *
228 * We must do any isize and inode updates
229 * before we unlock the pages. Otherwise we
230 * could end up racing with unlink.
231 */
232 BTRFS_I(inode)->disk_i_size = inode->i_size;
233 ret = btrfs_update_inode(trans, root, inode);
234
235 return ret;
236 fail:
237 return err;
238 }
239
240
241 /*
242 * conditionally insert an inline extent into the file. This
243 * does the checks required to make sure the data is small enough
244 * to fit as an inline extent.
245 */
246 static noinline int cow_file_range_inline(struct btrfs_root *root,
247 struct inode *inode, u64 start,
248 u64 end, size_t compressed_size,
249 int compress_type,
250 struct page **compressed_pages)
251 {
252 struct btrfs_fs_info *fs_info = root->fs_info;
253 struct btrfs_trans_handle *trans;
254 u64 isize = i_size_read(inode);
255 u64 actual_end = min(end + 1, isize);
256 u64 inline_len = actual_end - start;
257 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
258 u64 data_len = inline_len;
259 int ret;
260 struct btrfs_path *path;
261 int extent_inserted = 0;
262 u32 extent_item_size;
263
264 if (compressed_size)
265 data_len = compressed_size;
266
267 if (start > 0 ||
268 actual_end > fs_info->sectorsize ||
269 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
270 (!compressed_size &&
271 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
272 end + 1 < isize ||
273 data_len > fs_info->max_inline) {
274 return 1;
275 }
276
277 path = btrfs_alloc_path();
278 if (!path)
279 return -ENOMEM;
280
281 trans = btrfs_join_transaction(root);
282 if (IS_ERR(trans)) {
283 btrfs_free_path(path);
284 return PTR_ERR(trans);
285 }
286 trans->block_rsv = &fs_info->delalloc_block_rsv;
287
288 if (compressed_size && compressed_pages)
289 extent_item_size = btrfs_file_extent_calc_inline_size(
290 compressed_size);
291 else
292 extent_item_size = btrfs_file_extent_calc_inline_size(
293 inline_len);
294
295 ret = __btrfs_drop_extents(trans, root, inode, path,
296 start, aligned_end, NULL,
297 1, 1, extent_item_size, &extent_inserted);
298 if (ret) {
299 btrfs_abort_transaction(trans, ret);
300 goto out;
301 }
302
303 if (isize > actual_end)
304 inline_len = min_t(u64, isize, actual_end);
305 ret = insert_inline_extent(trans, path, extent_inserted,
306 root, inode, start,
307 inline_len, compressed_size,
308 compress_type, compressed_pages);
309 if (ret && ret != -ENOSPC) {
310 btrfs_abort_transaction(trans, ret);
311 goto out;
312 } else if (ret == -ENOSPC) {
313 ret = 1;
314 goto out;
315 }
316
317 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
318 btrfs_delalloc_release_metadata(inode, end + 1 - start);
319 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
320 out:
321 /*
322 * Don't forget to free the reserved space, as for inlined extent
323 * it won't count as data extent, free them directly here.
324 * And at reserve time, it's always aligned to page size, so
325 * just free one page here.
326 */
327 btrfs_qgroup_free_data(inode, 0, PAGE_SIZE);
328 btrfs_free_path(path);
329 btrfs_end_transaction(trans);
330 return ret;
331 }
332
333 struct async_extent {
334 u64 start;
335 u64 ram_size;
336 u64 compressed_size;
337 struct page **pages;
338 unsigned long nr_pages;
339 int compress_type;
340 struct list_head list;
341 };
342
343 struct async_cow {
344 struct inode *inode;
345 struct btrfs_root *root;
346 struct page *locked_page;
347 u64 start;
348 u64 end;
349 struct list_head extents;
350 struct btrfs_work work;
351 };
352
353 static noinline int add_async_extent(struct async_cow *cow,
354 u64 start, u64 ram_size,
355 u64 compressed_size,
356 struct page **pages,
357 unsigned long nr_pages,
358 int compress_type)
359 {
360 struct async_extent *async_extent;
361
362 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
363 BUG_ON(!async_extent); /* -ENOMEM */
364 async_extent->start = start;
365 async_extent->ram_size = ram_size;
366 async_extent->compressed_size = compressed_size;
367 async_extent->pages = pages;
368 async_extent->nr_pages = nr_pages;
369 async_extent->compress_type = compress_type;
370 list_add_tail(&async_extent->list, &cow->extents);
371 return 0;
372 }
373
374 static inline int inode_need_compress(struct inode *inode)
375 {
376 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
377
378 /* force compress */
379 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
380 return 1;
381 /* bad compression ratios */
382 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
383 return 0;
384 if (btrfs_test_opt(fs_info, COMPRESS) ||
385 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
386 BTRFS_I(inode)->force_compress)
387 return 1;
388 return 0;
389 }
390
391 /*
392 * we create compressed extents in two phases. The first
393 * phase compresses a range of pages that have already been
394 * locked (both pages and state bits are locked).
395 *
396 * This is done inside an ordered work queue, and the compression
397 * is spread across many cpus. The actual IO submission is step
398 * two, and the ordered work queue takes care of making sure that
399 * happens in the same order things were put onto the queue by
400 * writepages and friends.
401 *
402 * If this code finds it can't get good compression, it puts an
403 * entry onto the work queue to write the uncompressed bytes. This
404 * makes sure that both compressed inodes and uncompressed inodes
405 * are written in the same order that the flusher thread sent them
406 * down.
407 */
408 static noinline void compress_file_range(struct inode *inode,
409 struct page *locked_page,
410 u64 start, u64 end,
411 struct async_cow *async_cow,
412 int *num_added)
413 {
414 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
415 struct btrfs_root *root = BTRFS_I(inode)->root;
416 u64 num_bytes;
417 u64 blocksize = fs_info->sectorsize;
418 u64 actual_end;
419 u64 isize = i_size_read(inode);
420 int ret = 0;
421 struct page **pages = NULL;
422 unsigned long nr_pages;
423 unsigned long nr_pages_ret = 0;
424 unsigned long total_compressed = 0;
425 unsigned long total_in = 0;
426 unsigned long max_compressed = SZ_128K;
427 unsigned long max_uncompressed = SZ_128K;
428 int i;
429 int will_compress;
430 int compress_type = fs_info->compress_type;
431 int redirty = 0;
432
433 /* if this is a small write inside eof, kick off a defrag */
434 if ((end - start + 1) < SZ_16K &&
435 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
436 btrfs_add_inode_defrag(NULL, inode);
437
438 actual_end = min_t(u64, isize, end + 1);
439 again:
440 will_compress = 0;
441 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
442 nr_pages = min_t(unsigned long, nr_pages, SZ_128K / PAGE_SIZE);
443
444 /*
445 * we don't want to send crud past the end of i_size through
446 * compression, that's just a waste of CPU time. So, if the
447 * end of the file is before the start of our current
448 * requested range of bytes, we bail out to the uncompressed
449 * cleanup code that can deal with all of this.
450 *
451 * It isn't really the fastest way to fix things, but this is a
452 * very uncommon corner.
453 */
454 if (actual_end <= start)
455 goto cleanup_and_bail_uncompressed;
456
457 total_compressed = actual_end - start;
458
459 /*
460 * skip compression for a small file range(<=blocksize) that
461 * isn't an inline extent, since it doesn't save disk space at all.
462 */
463 if (total_compressed <= blocksize &&
464 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
465 goto cleanup_and_bail_uncompressed;
466
467 /* we want to make sure that amount of ram required to uncompress
468 * an extent is reasonable, so we limit the total size in ram
469 * of a compressed extent to 128k. This is a crucial number
470 * because it also controls how easily we can spread reads across
471 * cpus for decompression.
472 *
473 * We also want to make sure the amount of IO required to do
474 * a random read is reasonably small, so we limit the size of
475 * a compressed extent to 128k.
476 */
477 total_compressed = min(total_compressed, max_uncompressed);
478 num_bytes = ALIGN(end - start + 1, blocksize);
479 num_bytes = max(blocksize, num_bytes);
480 total_in = 0;
481 ret = 0;
482
483 /*
484 * we do compression for mount -o compress and when the
485 * inode has not been flagged as nocompress. This flag can
486 * change at any time if we discover bad compression ratios.
487 */
488 if (inode_need_compress(inode)) {
489 WARN_ON(pages);
490 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
491 if (!pages) {
492 /* just bail out to the uncompressed code */
493 goto cont;
494 }
495
496 if (BTRFS_I(inode)->force_compress)
497 compress_type = BTRFS_I(inode)->force_compress;
498
499 /*
500 * we need to call clear_page_dirty_for_io on each
501 * page in the range. Otherwise applications with the file
502 * mmap'd can wander in and change the page contents while
503 * we are compressing them.
504 *
505 * If the compression fails for any reason, we set the pages
506 * dirty again later on.
507 */
508 extent_range_clear_dirty_for_io(inode, start, end);
509 redirty = 1;
510 ret = btrfs_compress_pages(compress_type,
511 inode->i_mapping, start,
512 total_compressed, pages,
513 nr_pages, &nr_pages_ret,
514 &total_in,
515 &total_compressed,
516 max_compressed);
517
518 if (!ret) {
519 unsigned long offset = total_compressed &
520 (PAGE_SIZE - 1);
521 struct page *page = pages[nr_pages_ret - 1];
522 char *kaddr;
523
524 /* zero the tail end of the last page, we might be
525 * sending it down to disk
526 */
527 if (offset) {
528 kaddr = kmap_atomic(page);
529 memset(kaddr + offset, 0,
530 PAGE_SIZE - offset);
531 kunmap_atomic(kaddr);
532 }
533 will_compress = 1;
534 }
535 }
536 cont:
537 if (start == 0) {
538 /* lets try to make an inline extent */
539 if (ret || total_in < (actual_end - start)) {
540 /* we didn't compress the entire range, try
541 * to make an uncompressed inline extent.
542 */
543 ret = cow_file_range_inline(root, inode, start, end,
544 0, 0, NULL);
545 } else {
546 /* try making a compressed inline extent */
547 ret = cow_file_range_inline(root, inode, start, end,
548 total_compressed,
549 compress_type, pages);
550 }
551 if (ret <= 0) {
552 unsigned long clear_flags = EXTENT_DELALLOC |
553 EXTENT_DEFRAG;
554 unsigned long page_error_op;
555
556 clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
557 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
558
559 /*
560 * inline extent creation worked or returned error,
561 * we don't need to create any more async work items.
562 * Unlock and free up our temp pages.
563 */
564 extent_clear_unlock_delalloc(inode, start, end, end,
565 NULL, clear_flags,
566 PAGE_UNLOCK |
567 PAGE_CLEAR_DIRTY |
568 PAGE_SET_WRITEBACK |
569 page_error_op |
570 PAGE_END_WRITEBACK);
571 btrfs_free_reserved_data_space_noquota(inode, start,
572 end - start + 1);
573 goto free_pages_out;
574 }
575 }
576
577 if (will_compress) {
578 /*
579 * we aren't doing an inline extent round the compressed size
580 * up to a block size boundary so the allocator does sane
581 * things
582 */
583 total_compressed = ALIGN(total_compressed, blocksize);
584
585 /*
586 * one last check to make sure the compression is really a
587 * win, compare the page count read with the blocks on disk
588 */
589 total_in = ALIGN(total_in, PAGE_SIZE);
590 if (total_compressed >= total_in) {
591 will_compress = 0;
592 } else {
593 num_bytes = total_in;
594 *num_added += 1;
595
596 /*
597 * The async work queues will take care of doing actual
598 * allocation on disk for these compressed pages, and
599 * will submit them to the elevator.
600 */
601 add_async_extent(async_cow, start, num_bytes,
602 total_compressed, pages, nr_pages_ret,
603 compress_type);
604
605 if (start + num_bytes < end) {
606 start += num_bytes;
607 pages = NULL;
608 cond_resched();
609 goto again;
610 }
611 return;
612 }
613 }
614 if (pages) {
615 /*
616 * the compression code ran but failed to make things smaller,
617 * free any pages it allocated and our page pointer array
618 */
619 for (i = 0; i < nr_pages_ret; i++) {
620 WARN_ON(pages[i]->mapping);
621 put_page(pages[i]);
622 }
623 kfree(pages);
624 pages = NULL;
625 total_compressed = 0;
626 nr_pages_ret = 0;
627
628 /* flag the file so we don't compress in the future */
629 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
630 !(BTRFS_I(inode)->force_compress)) {
631 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
632 }
633 }
634 cleanup_and_bail_uncompressed:
635 /*
636 * No compression, but we still need to write the pages in the file
637 * we've been given so far. redirty the locked page if it corresponds
638 * to our extent and set things up for the async work queue to run
639 * cow_file_range to do the normal delalloc dance.
640 */
641 if (page_offset(locked_page) >= start &&
642 page_offset(locked_page) <= end)
643 __set_page_dirty_nobuffers(locked_page);
644 /* unlocked later on in the async handlers */
645
646 if (redirty)
647 extent_range_redirty_for_io(inode, start, end);
648 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0,
649 BTRFS_COMPRESS_NONE);
650 *num_added += 1;
651
652 return;
653
654 free_pages_out:
655 for (i = 0; i < nr_pages_ret; i++) {
656 WARN_ON(pages[i]->mapping);
657 put_page(pages[i]);
658 }
659 kfree(pages);
660 }
661
662 static void free_async_extent_pages(struct async_extent *async_extent)
663 {
664 int i;
665
666 if (!async_extent->pages)
667 return;
668
669 for (i = 0; i < async_extent->nr_pages; i++) {
670 WARN_ON(async_extent->pages[i]->mapping);
671 put_page(async_extent->pages[i]);
672 }
673 kfree(async_extent->pages);
674 async_extent->nr_pages = 0;
675 async_extent->pages = NULL;
676 }
677
678 /*
679 * phase two of compressed writeback. This is the ordered portion
680 * of the code, which only gets called in the order the work was
681 * queued. We walk all the async extents created by compress_file_range
682 * and send them down to the disk.
683 */
684 static noinline void submit_compressed_extents(struct inode *inode,
685 struct async_cow *async_cow)
686 {
687 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
688 struct async_extent *async_extent;
689 u64 alloc_hint = 0;
690 struct btrfs_key ins;
691 struct extent_map *em;
692 struct btrfs_root *root = BTRFS_I(inode)->root;
693 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
694 struct extent_io_tree *io_tree;
695 int ret = 0;
696
697 again:
698 while (!list_empty(&async_cow->extents)) {
699 async_extent = list_entry(async_cow->extents.next,
700 struct async_extent, list);
701 list_del(&async_extent->list);
702
703 io_tree = &BTRFS_I(inode)->io_tree;
704
705 retry:
706 /* did the compression code fall back to uncompressed IO? */
707 if (!async_extent->pages) {
708 int page_started = 0;
709 unsigned long nr_written = 0;
710
711 lock_extent(io_tree, async_extent->start,
712 async_extent->start +
713 async_extent->ram_size - 1);
714
715 /* allocate blocks */
716 ret = cow_file_range(inode, async_cow->locked_page,
717 async_extent->start,
718 async_extent->start +
719 async_extent->ram_size - 1,
720 async_extent->start +
721 async_extent->ram_size - 1,
722 &page_started, &nr_written, 0,
723 NULL);
724
725 /* JDM XXX */
726
727 /*
728 * if page_started, cow_file_range inserted an
729 * inline extent and took care of all the unlocking
730 * and IO for us. Otherwise, we need to submit
731 * all those pages down to the drive.
732 */
733 if (!page_started && !ret)
734 extent_write_locked_range(io_tree,
735 inode, async_extent->start,
736 async_extent->start +
737 async_extent->ram_size - 1,
738 btrfs_get_extent,
739 WB_SYNC_ALL);
740 else if (ret)
741 unlock_page(async_cow->locked_page);
742 kfree(async_extent);
743 cond_resched();
744 continue;
745 }
746
747 lock_extent(io_tree, async_extent->start,
748 async_extent->start + async_extent->ram_size - 1);
749
750 ret = btrfs_reserve_extent(root, async_extent->ram_size,
751 async_extent->compressed_size,
752 async_extent->compressed_size,
753 0, alloc_hint, &ins, 1, 1);
754 if (ret) {
755 free_async_extent_pages(async_extent);
756
757 if (ret == -ENOSPC) {
758 unlock_extent(io_tree, async_extent->start,
759 async_extent->start +
760 async_extent->ram_size - 1);
761
762 /*
763 * we need to redirty the pages if we decide to
764 * fallback to uncompressed IO, otherwise we
765 * will not submit these pages down to lower
766 * layers.
767 */
768 extent_range_redirty_for_io(inode,
769 async_extent->start,
770 async_extent->start +
771 async_extent->ram_size - 1);
772
773 goto retry;
774 }
775 goto out_free;
776 }
777 /*
778 * here we're doing allocation and writeback of the
779 * compressed pages
780 */
781 btrfs_drop_extent_cache(inode, async_extent->start,
782 async_extent->start +
783 async_extent->ram_size - 1, 0);
784
785 em = alloc_extent_map();
786 if (!em) {
787 ret = -ENOMEM;
788 goto out_free_reserve;
789 }
790 em->start = async_extent->start;
791 em->len = async_extent->ram_size;
792 em->orig_start = em->start;
793 em->mod_start = em->start;
794 em->mod_len = em->len;
795
796 em->block_start = ins.objectid;
797 em->block_len = ins.offset;
798 em->orig_block_len = ins.offset;
799 em->ram_bytes = async_extent->ram_size;
800 em->bdev = fs_info->fs_devices->latest_bdev;
801 em->compress_type = async_extent->compress_type;
802 set_bit(EXTENT_FLAG_PINNED, &em->flags);
803 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
804 em->generation = -1;
805
806 while (1) {
807 write_lock(&em_tree->lock);
808 ret = add_extent_mapping(em_tree, em, 1);
809 write_unlock(&em_tree->lock);
810 if (ret != -EEXIST) {
811 free_extent_map(em);
812 break;
813 }
814 btrfs_drop_extent_cache(inode, async_extent->start,
815 async_extent->start +
816 async_extent->ram_size - 1, 0);
817 }
818
819 if (ret)
820 goto out_free_reserve;
821
822 ret = btrfs_add_ordered_extent_compress(inode,
823 async_extent->start,
824 ins.objectid,
825 async_extent->ram_size,
826 ins.offset,
827 BTRFS_ORDERED_COMPRESSED,
828 async_extent->compress_type);
829 if (ret) {
830 btrfs_drop_extent_cache(inode, async_extent->start,
831 async_extent->start +
832 async_extent->ram_size - 1, 0);
833 goto out_free_reserve;
834 }
835 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
836
837 /*
838 * clear dirty, set writeback and unlock the pages.
839 */
840 extent_clear_unlock_delalloc(inode, async_extent->start,
841 async_extent->start +
842 async_extent->ram_size - 1,
843 async_extent->start +
844 async_extent->ram_size - 1,
845 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
846 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
847 PAGE_SET_WRITEBACK);
848 ret = btrfs_submit_compressed_write(inode,
849 async_extent->start,
850 async_extent->ram_size,
851 ins.objectid,
852 ins.offset, async_extent->pages,
853 async_extent->nr_pages);
854 if (ret) {
855 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
856 struct page *p = async_extent->pages[0];
857 const u64 start = async_extent->start;
858 const u64 end = start + async_extent->ram_size - 1;
859
860 p->mapping = inode->i_mapping;
861 tree->ops->writepage_end_io_hook(p, start, end,
862 NULL, 0);
863 p->mapping = NULL;
864 extent_clear_unlock_delalloc(inode, start, end, end,
865 NULL, 0,
866 PAGE_END_WRITEBACK |
867 PAGE_SET_ERROR);
868 free_async_extent_pages(async_extent);
869 }
870 alloc_hint = ins.objectid + ins.offset;
871 kfree(async_extent);
872 cond_resched();
873 }
874 return;
875 out_free_reserve:
876 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
877 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
878 out_free:
879 extent_clear_unlock_delalloc(inode, async_extent->start,
880 async_extent->start +
881 async_extent->ram_size - 1,
882 async_extent->start +
883 async_extent->ram_size - 1,
884 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
885 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
886 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
887 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
888 PAGE_SET_ERROR);
889 free_async_extent_pages(async_extent);
890 kfree(async_extent);
891 goto again;
892 }
893
894 static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
895 u64 num_bytes)
896 {
897 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
898 struct extent_map *em;
899 u64 alloc_hint = 0;
900
901 read_lock(&em_tree->lock);
902 em = search_extent_mapping(em_tree, start, num_bytes);
903 if (em) {
904 /*
905 * if block start isn't an actual block number then find the
906 * first block in this inode and use that as a hint. If that
907 * block is also bogus then just don't worry about it.
908 */
909 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
910 free_extent_map(em);
911 em = search_extent_mapping(em_tree, 0, 0);
912 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
913 alloc_hint = em->block_start;
914 if (em)
915 free_extent_map(em);
916 } else {
917 alloc_hint = em->block_start;
918 free_extent_map(em);
919 }
920 }
921 read_unlock(&em_tree->lock);
922
923 return alloc_hint;
924 }
925
926 /*
927 * when extent_io.c finds a delayed allocation range in the file,
928 * the call backs end up in this code. The basic idea is to
929 * allocate extents on disk for the range, and create ordered data structs
930 * in ram to track those extents.
931 *
932 * locked_page is the page that writepage had locked already. We use
933 * it to make sure we don't do extra locks or unlocks.
934 *
935 * *page_started is set to one if we unlock locked_page and do everything
936 * required to start IO on it. It may be clean and already done with
937 * IO when we return.
938 */
939 static noinline int cow_file_range(struct inode *inode,
940 struct page *locked_page,
941 u64 start, u64 end, u64 delalloc_end,
942 int *page_started, unsigned long *nr_written,
943 int unlock, struct btrfs_dedupe_hash *hash)
944 {
945 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
946 struct btrfs_root *root = BTRFS_I(inode)->root;
947 u64 alloc_hint = 0;
948 u64 num_bytes;
949 unsigned long ram_size;
950 u64 disk_num_bytes;
951 u64 cur_alloc_size;
952 u64 blocksize = fs_info->sectorsize;
953 struct btrfs_key ins;
954 struct extent_map *em;
955 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
956 int ret = 0;
957
958 if (btrfs_is_free_space_inode(inode)) {
959 WARN_ON_ONCE(1);
960 ret = -EINVAL;
961 goto out_unlock;
962 }
963
964 num_bytes = ALIGN(end - start + 1, blocksize);
965 num_bytes = max(blocksize, num_bytes);
966 disk_num_bytes = num_bytes;
967
968 /* if this is a small write inside eof, kick off defrag */
969 if (num_bytes < SZ_64K &&
970 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
971 btrfs_add_inode_defrag(NULL, inode);
972
973 if (start == 0) {
974 /* lets try to make an inline extent */
975 ret = cow_file_range_inline(root, inode, start, end, 0, 0,
976 NULL);
977 if (ret == 0) {
978 extent_clear_unlock_delalloc(inode, start, end,
979 delalloc_end, NULL,
980 EXTENT_LOCKED | EXTENT_DELALLOC |
981 EXTENT_DEFRAG, PAGE_UNLOCK |
982 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
983 PAGE_END_WRITEBACK);
984 btrfs_free_reserved_data_space_noquota(inode, start,
985 end - start + 1);
986 *nr_written = *nr_written +
987 (end - start + PAGE_SIZE) / PAGE_SIZE;
988 *page_started = 1;
989 goto out;
990 } else if (ret < 0) {
991 goto out_unlock;
992 }
993 }
994
995 BUG_ON(disk_num_bytes >
996 btrfs_super_total_bytes(fs_info->super_copy));
997
998 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
999 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
1000
1001 while (disk_num_bytes > 0) {
1002 unsigned long op;
1003
1004 cur_alloc_size = disk_num_bytes;
1005 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
1006 fs_info->sectorsize, 0, alloc_hint,
1007 &ins, 1, 1);
1008 if (ret < 0)
1009 goto out_unlock;
1010
1011 em = alloc_extent_map();
1012 if (!em) {
1013 ret = -ENOMEM;
1014 goto out_reserve;
1015 }
1016 em->start = start;
1017 em->orig_start = em->start;
1018 ram_size = ins.offset;
1019 em->len = ins.offset;
1020 em->mod_start = em->start;
1021 em->mod_len = em->len;
1022
1023 em->block_start = ins.objectid;
1024 em->block_len = ins.offset;
1025 em->orig_block_len = ins.offset;
1026 em->ram_bytes = ram_size;
1027 em->bdev = fs_info->fs_devices->latest_bdev;
1028 set_bit(EXTENT_FLAG_PINNED, &em->flags);
1029 em->generation = -1;
1030
1031 while (1) {
1032 write_lock(&em_tree->lock);
1033 ret = add_extent_mapping(em_tree, em, 1);
1034 write_unlock(&em_tree->lock);
1035 if (ret != -EEXIST) {
1036 free_extent_map(em);
1037 break;
1038 }
1039 btrfs_drop_extent_cache(inode, start,
1040 start + ram_size - 1, 0);
1041 }
1042 if (ret)
1043 goto out_reserve;
1044
1045 cur_alloc_size = ins.offset;
1046 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
1047 ram_size, cur_alloc_size, 0);
1048 if (ret)
1049 goto out_drop_extent_cache;
1050
1051 if (root->root_key.objectid ==
1052 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1053 ret = btrfs_reloc_clone_csums(inode, start,
1054 cur_alloc_size);
1055 if (ret)
1056 goto out_drop_extent_cache;
1057 }
1058
1059 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
1060
1061 if (disk_num_bytes < cur_alloc_size)
1062 break;
1063
1064 /* we're not doing compressed IO, don't unlock the first
1065 * page (which the caller expects to stay locked), don't
1066 * clear any dirty bits and don't set any writeback bits
1067 *
1068 * Do set the Private2 bit so we know this page was properly
1069 * setup for writepage
1070 */
1071 op = unlock ? PAGE_UNLOCK : 0;
1072 op |= PAGE_SET_PRIVATE2;
1073
1074 extent_clear_unlock_delalloc(inode, start,
1075 start + ram_size - 1,
1076 delalloc_end, locked_page,
1077 EXTENT_LOCKED | EXTENT_DELALLOC,
1078 op);
1079 disk_num_bytes -= cur_alloc_size;
1080 num_bytes -= cur_alloc_size;
1081 alloc_hint = ins.objectid + ins.offset;
1082 start += cur_alloc_size;
1083 }
1084 out:
1085 return ret;
1086
1087 out_drop_extent_cache:
1088 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
1089 out_reserve:
1090 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
1091 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
1092 out_unlock:
1093 extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
1094 locked_page,
1095 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
1096 EXTENT_DELALLOC | EXTENT_DEFRAG,
1097 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1098 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
1099 goto out;
1100 }
1101
1102 /*
1103 * work queue call back to started compression on a file and pages
1104 */
1105 static noinline void async_cow_start(struct btrfs_work *work)
1106 {
1107 struct async_cow *async_cow;
1108 int num_added = 0;
1109 async_cow = container_of(work, struct async_cow, work);
1110
1111 compress_file_range(async_cow->inode, async_cow->locked_page,
1112 async_cow->start, async_cow->end, async_cow,
1113 &num_added);
1114 if (num_added == 0) {
1115 btrfs_add_delayed_iput(async_cow->inode);
1116 async_cow->inode = NULL;
1117 }
1118 }
1119
1120 /*
1121 * work queue call back to submit previously compressed pages
1122 */
1123 static noinline void async_cow_submit(struct btrfs_work *work)
1124 {
1125 struct btrfs_fs_info *fs_info;
1126 struct async_cow *async_cow;
1127 struct btrfs_root *root;
1128 unsigned long nr_pages;
1129
1130 async_cow = container_of(work, struct async_cow, work);
1131
1132 root = async_cow->root;
1133 fs_info = root->fs_info;
1134 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1135 PAGE_SHIFT;
1136
1137 /*
1138 * atomic_sub_return implies a barrier for waitqueue_active
1139 */
1140 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
1141 5 * SZ_1M &&
1142 waitqueue_active(&fs_info->async_submit_wait))
1143 wake_up(&fs_info->async_submit_wait);
1144
1145 if (async_cow->inode)
1146 submit_compressed_extents(async_cow->inode, async_cow);
1147 }
1148
1149 static noinline void async_cow_free(struct btrfs_work *work)
1150 {
1151 struct async_cow *async_cow;
1152 async_cow = container_of(work, struct async_cow, work);
1153 if (async_cow->inode)
1154 btrfs_add_delayed_iput(async_cow->inode);
1155 kfree(async_cow);
1156 }
1157
1158 static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1159 u64 start, u64 end, int *page_started,
1160 unsigned long *nr_written)
1161 {
1162 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1163 struct async_cow *async_cow;
1164 struct btrfs_root *root = BTRFS_I(inode)->root;
1165 unsigned long nr_pages;
1166 u64 cur_end;
1167 int limit = 10 * SZ_1M;
1168
1169 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1170 1, 0, NULL, GFP_NOFS);
1171 while (start < end) {
1172 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
1173 BUG_ON(!async_cow); /* -ENOMEM */
1174 async_cow->inode = igrab(inode);
1175 async_cow->root = root;
1176 async_cow->locked_page = locked_page;
1177 async_cow->start = start;
1178
1179 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
1180 !btrfs_test_opt(fs_info, FORCE_COMPRESS))
1181 cur_end = end;
1182 else
1183 cur_end = min(end, start + SZ_512K - 1);
1184
1185 async_cow->end = cur_end;
1186 INIT_LIST_HEAD(&async_cow->extents);
1187
1188 btrfs_init_work(&async_cow->work,
1189 btrfs_delalloc_helper,
1190 async_cow_start, async_cow_submit,
1191 async_cow_free);
1192
1193 nr_pages = (cur_end - start + PAGE_SIZE) >>
1194 PAGE_SHIFT;
1195 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
1196
1197 btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work);
1198
1199 if (atomic_read(&fs_info->async_delalloc_pages) > limit) {
1200 wait_event(fs_info->async_submit_wait,
1201 (atomic_read(&fs_info->async_delalloc_pages) <
1202 limit));
1203 }
1204
1205 while (atomic_read(&fs_info->async_submit_draining) &&
1206 atomic_read(&fs_info->async_delalloc_pages)) {
1207 wait_event(fs_info->async_submit_wait,
1208 (atomic_read(&fs_info->async_delalloc_pages) ==
1209 0));
1210 }
1211
1212 *nr_written += nr_pages;
1213 start = cur_end + 1;
1214 }
1215 *page_started = 1;
1216 return 0;
1217 }
1218
1219 static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
1220 u64 bytenr, u64 num_bytes)
1221 {
1222 int ret;
1223 struct btrfs_ordered_sum *sums;
1224 LIST_HEAD(list);
1225
1226 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
1227 bytenr + num_bytes - 1, &list, 0);
1228 if (ret == 0 && list_empty(&list))
1229 return 0;
1230
1231 while (!list_empty(&list)) {
1232 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1233 list_del(&sums->list);
1234 kfree(sums);
1235 }
1236 return 1;
1237 }
1238
1239 /*
1240 * when nowcow writeback call back. This checks for snapshots or COW copies
1241 * of the extents that exist in the file, and COWs the file as required.
1242 *
1243 * If no cow copies or snapshots exist, we write directly to the existing
1244 * blocks on disk
1245 */
1246 static noinline int run_delalloc_nocow(struct inode *inode,
1247 struct page *locked_page,
1248 u64 start, u64 end, int *page_started, int force,
1249 unsigned long *nr_written)
1250 {
1251 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1252 struct btrfs_root *root = BTRFS_I(inode)->root;
1253 struct btrfs_trans_handle *trans;
1254 struct extent_buffer *leaf;
1255 struct btrfs_path *path;
1256 struct btrfs_file_extent_item *fi;
1257 struct btrfs_key found_key;
1258 u64 cow_start;
1259 u64 cur_offset;
1260 u64 extent_end;
1261 u64 extent_offset;
1262 u64 disk_bytenr;
1263 u64 num_bytes;
1264 u64 disk_num_bytes;
1265 u64 ram_bytes;
1266 int extent_type;
1267 int ret, err;
1268 int type;
1269 int nocow;
1270 int check_prev = 1;
1271 bool nolock;
1272 u64 ino = btrfs_ino(inode);
1273
1274 path = btrfs_alloc_path();
1275 if (!path) {
1276 extent_clear_unlock_delalloc(inode, start, end, end,
1277 locked_page,
1278 EXTENT_LOCKED | EXTENT_DELALLOC |
1279 EXTENT_DO_ACCOUNTING |
1280 EXTENT_DEFRAG, PAGE_UNLOCK |
1281 PAGE_CLEAR_DIRTY |
1282 PAGE_SET_WRITEBACK |
1283 PAGE_END_WRITEBACK);
1284 return -ENOMEM;
1285 }
1286
1287 nolock = btrfs_is_free_space_inode(inode);
1288
1289 if (nolock)
1290 trans = btrfs_join_transaction_nolock(root);
1291 else
1292 trans = btrfs_join_transaction(root);
1293
1294 if (IS_ERR(trans)) {
1295 extent_clear_unlock_delalloc(inode, start, end, end,
1296 locked_page,
1297 EXTENT_LOCKED | EXTENT_DELALLOC |
1298 EXTENT_DO_ACCOUNTING |
1299 EXTENT_DEFRAG, PAGE_UNLOCK |
1300 PAGE_CLEAR_DIRTY |
1301 PAGE_SET_WRITEBACK |
1302 PAGE_END_WRITEBACK);
1303 btrfs_free_path(path);
1304 return PTR_ERR(trans);
1305 }
1306
1307 trans->block_rsv = &fs_info->delalloc_block_rsv;
1308
1309 cow_start = (u64)-1;
1310 cur_offset = start;
1311 while (1) {
1312 ret = btrfs_lookup_file_extent(trans, root, path, ino,
1313 cur_offset, 0);
1314 if (ret < 0)
1315 goto error;
1316 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1317 leaf = path->nodes[0];
1318 btrfs_item_key_to_cpu(leaf, &found_key,
1319 path->slots[0] - 1);
1320 if (found_key.objectid == ino &&
1321 found_key.type == BTRFS_EXTENT_DATA_KEY)
1322 path->slots[0]--;
1323 }
1324 check_prev = 0;
1325 next_slot:
1326 leaf = path->nodes[0];
1327 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1328 ret = btrfs_next_leaf(root, path);
1329 if (ret < 0)
1330 goto error;
1331 if (ret > 0)
1332 break;
1333 leaf = path->nodes[0];
1334 }
1335
1336 nocow = 0;
1337 disk_bytenr = 0;
1338 num_bytes = 0;
1339 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1340
1341 if (found_key.objectid > ino)
1342 break;
1343 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1344 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1345 path->slots[0]++;
1346 goto next_slot;
1347 }
1348 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
1349 found_key.offset > end)
1350 break;
1351
1352 if (found_key.offset > cur_offset) {
1353 extent_end = found_key.offset;
1354 extent_type = 0;
1355 goto out_check;
1356 }
1357
1358 fi = btrfs_item_ptr(leaf, path->slots[0],
1359 struct btrfs_file_extent_item);
1360 extent_type = btrfs_file_extent_type(leaf, fi);
1361
1362 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
1363 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1364 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1365 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1366 extent_offset = btrfs_file_extent_offset(leaf, fi);
1367 extent_end = found_key.offset +
1368 btrfs_file_extent_num_bytes(leaf, fi);
1369 disk_num_bytes =
1370 btrfs_file_extent_disk_num_bytes(leaf, fi);
1371 if (extent_end <= start) {
1372 path->slots[0]++;
1373 goto next_slot;
1374 }
1375 if (disk_bytenr == 0)
1376 goto out_check;
1377 if (btrfs_file_extent_compression(leaf, fi) ||
1378 btrfs_file_extent_encryption(leaf, fi) ||
1379 btrfs_file_extent_other_encoding(leaf, fi))
1380 goto out_check;
1381 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1382 goto out_check;
1383 if (btrfs_extent_readonly(fs_info, disk_bytenr))
1384 goto out_check;
1385 if (btrfs_cross_ref_exist(trans, root, ino,
1386 found_key.offset -
1387 extent_offset, disk_bytenr))
1388 goto out_check;
1389 disk_bytenr += extent_offset;
1390 disk_bytenr += cur_offset - found_key.offset;
1391 num_bytes = min(end + 1, extent_end) - cur_offset;
1392 /*
1393 * if there are pending snapshots for this root,
1394 * we fall into common COW way.
1395 */
1396 if (!nolock) {
1397 err = btrfs_start_write_no_snapshoting(root);
1398 if (!err)
1399 goto out_check;
1400 }
1401 /*
1402 * force cow if csum exists in the range.
1403 * this ensure that csum for a given extent are
1404 * either valid or do not exist.
1405 */
1406 if (csum_exist_in_range(fs_info, disk_bytenr,
1407 num_bytes))
1408 goto out_check;
1409 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
1410 goto out_check;
1411 nocow = 1;
1412 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1413 extent_end = found_key.offset +
1414 btrfs_file_extent_inline_len(leaf,
1415 path->slots[0], fi);
1416 extent_end = ALIGN(extent_end,
1417 fs_info->sectorsize);
1418 } else {
1419 BUG_ON(1);
1420 }
1421 out_check:
1422 if (extent_end <= start) {
1423 path->slots[0]++;
1424 if (!nolock && nocow)
1425 btrfs_end_write_no_snapshoting(root);
1426 if (nocow)
1427 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
1428 goto next_slot;
1429 }
1430 if (!nocow) {
1431 if (cow_start == (u64)-1)
1432 cow_start = cur_offset;
1433 cur_offset = extent_end;
1434 if (cur_offset > end)
1435 break;
1436 path->slots[0]++;
1437 goto next_slot;
1438 }
1439
1440 btrfs_release_path(path);
1441 if (cow_start != (u64)-1) {
1442 ret = cow_file_range(inode, locked_page,
1443 cow_start, found_key.offset - 1,
1444 end, page_started, nr_written, 1,
1445 NULL);
1446 if (ret) {
1447 if (!nolock && nocow)
1448 btrfs_end_write_no_snapshoting(root);
1449 if (nocow)
1450 btrfs_dec_nocow_writers(fs_info,
1451 disk_bytenr);
1452 goto error;
1453 }
1454 cow_start = (u64)-1;
1455 }
1456
1457 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1458 struct extent_map *em;
1459 struct extent_map_tree *em_tree;
1460 em_tree = &BTRFS_I(inode)->extent_tree;
1461 em = alloc_extent_map();
1462 BUG_ON(!em); /* -ENOMEM */
1463 em->start = cur_offset;
1464 em->orig_start = found_key.offset - extent_offset;
1465 em->len = num_bytes;
1466 em->block_len = num_bytes;
1467 em->block_start = disk_bytenr;
1468 em->orig_block_len = disk_num_bytes;
1469 em->ram_bytes = ram_bytes;
1470 em->bdev = fs_info->fs_devices->latest_bdev;
1471 em->mod_start = em->start;
1472 em->mod_len = em->len;
1473 set_bit(EXTENT_FLAG_PINNED, &em->flags);
1474 set_bit(EXTENT_FLAG_FILLING, &em->flags);
1475 em->generation = -1;
1476 while (1) {
1477 write_lock(&em_tree->lock);
1478 ret = add_extent_mapping(em_tree, em, 1);
1479 write_unlock(&em_tree->lock);
1480 if (ret != -EEXIST) {
1481 free_extent_map(em);
1482 break;
1483 }
1484 btrfs_drop_extent_cache(inode, em->start,
1485 em->start + em->len - 1, 0);
1486 }
1487 type = BTRFS_ORDERED_PREALLOC;
1488 } else {
1489 type = BTRFS_ORDERED_NOCOW;
1490 }
1491
1492 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
1493 num_bytes, num_bytes, type);
1494 if (nocow)
1495 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
1496 BUG_ON(ret); /* -ENOMEM */
1497
1498 if (root->root_key.objectid ==
1499 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1500 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1501 num_bytes);
1502 if (ret) {
1503 if (!nolock && nocow)
1504 btrfs_end_write_no_snapshoting(root);
1505 goto error;
1506 }
1507 }
1508
1509 extent_clear_unlock_delalloc(inode, cur_offset,
1510 cur_offset + num_bytes - 1, end,
1511 locked_page, EXTENT_LOCKED |
1512 EXTENT_DELALLOC |
1513 EXTENT_CLEAR_DATA_RESV,
1514 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1515
1516 if (!nolock && nocow)
1517 btrfs_end_write_no_snapshoting(root);
1518 cur_offset = extent_end;
1519 if (cur_offset > end)
1520 break;
1521 }
1522 btrfs_release_path(path);
1523
1524 if (cur_offset <= end && cow_start == (u64)-1) {
1525 cow_start = cur_offset;
1526 cur_offset = end;
1527 }
1528
1529 if (cow_start != (u64)-1) {
1530 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1531 page_started, nr_written, 1, NULL);
1532 if (ret)
1533 goto error;
1534 }
1535
1536 error:
1537 err = btrfs_end_transaction(trans);
1538 if (!ret)
1539 ret = err;
1540
1541 if (ret && cur_offset < end)
1542 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
1543 locked_page, EXTENT_LOCKED |
1544 EXTENT_DELALLOC | EXTENT_DEFRAG |
1545 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1546 PAGE_CLEAR_DIRTY |
1547 PAGE_SET_WRITEBACK |
1548 PAGE_END_WRITEBACK);
1549 btrfs_free_path(path);
1550 return ret;
1551 }
1552
1553 static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1554 {
1555
1556 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1557 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1558 return 0;
1559
1560 /*
1561 * @defrag_bytes is a hint value, no spinlock held here,
1562 * if is not zero, it means the file is defragging.
1563 * Force cow if given extent needs to be defragged.
1564 */
1565 if (BTRFS_I(inode)->defrag_bytes &&
1566 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1567 EXTENT_DEFRAG, 0, NULL))
1568 return 1;
1569
1570 return 0;
1571 }
1572
1573 /*
1574 * extent_io.c call back to do delayed allocation processing
1575 */
1576 static int run_delalloc_range(struct inode *inode, struct page *locked_page,
1577 u64 start, u64 end, int *page_started,
1578 unsigned long *nr_written)
1579 {
1580 int ret;
1581 int force_cow = need_force_cow(inode, start, end);
1582
1583 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
1584 ret = run_delalloc_nocow(inode, locked_page, start, end,
1585 page_started, 1, nr_written);
1586 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
1587 ret = run_delalloc_nocow(inode, locked_page, start, end,
1588 page_started, 0, nr_written);
1589 } else if (!inode_need_compress(inode)) {
1590 ret = cow_file_range(inode, locked_page, start, end, end,
1591 page_started, nr_written, 1, NULL);
1592 } else {
1593 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1594 &BTRFS_I(inode)->runtime_flags);
1595 ret = cow_file_range_async(inode, locked_page, start, end,
1596 page_started, nr_written);
1597 }
1598 return ret;
1599 }
1600
1601 static void btrfs_split_extent_hook(struct inode *inode,
1602 struct extent_state *orig, u64 split)
1603 {
1604 u64 size;
1605
1606 /* not delalloc, ignore it */
1607 if (!(orig->state & EXTENT_DELALLOC))
1608 return;
1609
1610 size = orig->end - orig->start + 1;
1611 if (size > BTRFS_MAX_EXTENT_SIZE) {
1612 u64 num_extents;
1613 u64 new_size;
1614
1615 /*
1616 * See the explanation in btrfs_merge_extent_hook, the same
1617 * applies here, just in reverse.
1618 */
1619 new_size = orig->end - split + 1;
1620 num_extents = div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
1621 BTRFS_MAX_EXTENT_SIZE);
1622 new_size = split - orig->start;
1623 num_extents += div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
1624 BTRFS_MAX_EXTENT_SIZE);
1625 if (div64_u64(size + BTRFS_MAX_EXTENT_SIZE - 1,
1626 BTRFS_MAX_EXTENT_SIZE) >= num_extents)
1627 return;
1628 }
1629
1630 spin_lock(&BTRFS_I(inode)->lock);
1631 BTRFS_I(inode)->outstanding_extents++;
1632 spin_unlock(&BTRFS_I(inode)->lock);
1633 }
1634
1635 /*
1636 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1637 * extents so we can keep track of new extents that are just merged onto old
1638 * extents, such as when we are doing sequential writes, so we can properly
1639 * account for the metadata space we'll need.
1640 */
1641 static void btrfs_merge_extent_hook(struct inode *inode,
1642 struct extent_state *new,
1643 struct extent_state *other)
1644 {
1645 u64 new_size, old_size;
1646 u64 num_extents;
1647
1648 /* not delalloc, ignore it */
1649 if (!(other->state & EXTENT_DELALLOC))
1650 return;
1651
1652 if (new->start > other->start)
1653 new_size = new->end - other->start + 1;
1654 else
1655 new_size = other->end - new->start + 1;
1656
1657 /* we're not bigger than the max, unreserve the space and go */
1658 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1659 spin_lock(&BTRFS_I(inode)->lock);
1660 BTRFS_I(inode)->outstanding_extents--;
1661 spin_unlock(&BTRFS_I(inode)->lock);
1662 return;
1663 }
1664
1665 /*
1666 * We have to add up either side to figure out how many extents were
1667 * accounted for before we merged into one big extent. If the number of
1668 * extents we accounted for is <= the amount we need for the new range
1669 * then we can return, otherwise drop. Think of it like this
1670 *
1671 * [ 4k][MAX_SIZE]
1672 *
1673 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1674 * need 2 outstanding extents, on one side we have 1 and the other side
1675 * we have 1 so they are == and we can return. But in this case
1676 *
1677 * [MAX_SIZE+4k][MAX_SIZE+4k]
1678 *
1679 * Each range on their own accounts for 2 extents, but merged together
1680 * they are only 3 extents worth of accounting, so we need to drop in
1681 * this case.
1682 */
1683 old_size = other->end - other->start + 1;
1684 num_extents = div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
1685 BTRFS_MAX_EXTENT_SIZE);
1686 old_size = new->end - new->start + 1;
1687 num_extents += div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
1688 BTRFS_MAX_EXTENT_SIZE);
1689
1690 if (div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
1691 BTRFS_MAX_EXTENT_SIZE) >= num_extents)
1692 return;
1693
1694 spin_lock(&BTRFS_I(inode)->lock);
1695 BTRFS_I(inode)->outstanding_extents--;
1696 spin_unlock(&BTRFS_I(inode)->lock);
1697 }
1698
1699 static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1700 struct inode *inode)
1701 {
1702 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1703
1704 spin_lock(&root->delalloc_lock);
1705 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1706 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1707 &root->delalloc_inodes);
1708 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1709 &BTRFS_I(inode)->runtime_flags);
1710 root->nr_delalloc_inodes++;
1711 if (root->nr_delalloc_inodes == 1) {
1712 spin_lock(&fs_info->delalloc_root_lock);
1713 BUG_ON(!list_empty(&root->delalloc_root));
1714 list_add_tail(&root->delalloc_root,
1715 &fs_info->delalloc_roots);
1716 spin_unlock(&fs_info->delalloc_root_lock);
1717 }
1718 }
1719 spin_unlock(&root->delalloc_lock);
1720 }
1721
1722 static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1723 struct inode *inode)
1724 {
1725 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1726
1727 spin_lock(&root->delalloc_lock);
1728 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1729 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1730 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1731 &BTRFS_I(inode)->runtime_flags);
1732 root->nr_delalloc_inodes--;
1733 if (!root->nr_delalloc_inodes) {
1734 spin_lock(&fs_info->delalloc_root_lock);
1735 BUG_ON(list_empty(&root->delalloc_root));
1736 list_del_init(&root->delalloc_root);
1737 spin_unlock(&fs_info->delalloc_root_lock);
1738 }
1739 }
1740 spin_unlock(&root->delalloc_lock);
1741 }
1742
1743 /*
1744 * extent_io.c set_bit_hook, used to track delayed allocation
1745 * bytes in this file, and to maintain the list of inodes that
1746 * have pending delalloc work to be done.
1747 */
1748 static void btrfs_set_bit_hook(struct inode *inode,
1749 struct extent_state *state, unsigned *bits)
1750 {
1751
1752 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1753
1754 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1755 WARN_ON(1);
1756 /*
1757 * set_bit and clear bit hooks normally require _irqsave/restore
1758 * but in this case, we are only testing for the DELALLOC
1759 * bit, which is only set or cleared with irqs on
1760 */
1761 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
1762 struct btrfs_root *root = BTRFS_I(inode)->root;
1763 u64 len = state->end + 1 - state->start;
1764 bool do_list = !btrfs_is_free_space_inode(inode);
1765
1766 if (*bits & EXTENT_FIRST_DELALLOC) {
1767 *bits &= ~EXTENT_FIRST_DELALLOC;
1768 } else {
1769 spin_lock(&BTRFS_I(inode)->lock);
1770 BTRFS_I(inode)->outstanding_extents++;
1771 spin_unlock(&BTRFS_I(inode)->lock);
1772 }
1773
1774 /* For sanity tests */
1775 if (btrfs_is_testing(fs_info))
1776 return;
1777
1778 __percpu_counter_add(&fs_info->delalloc_bytes, len,
1779 fs_info->delalloc_batch);
1780 spin_lock(&BTRFS_I(inode)->lock);
1781 BTRFS_I(inode)->delalloc_bytes += len;
1782 if (*bits & EXTENT_DEFRAG)
1783 BTRFS_I(inode)->defrag_bytes += len;
1784 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1785 &BTRFS_I(inode)->runtime_flags))
1786 btrfs_add_delalloc_inodes(root, inode);
1787 spin_unlock(&BTRFS_I(inode)->lock);
1788 }
1789 }
1790
1791 /*
1792 * extent_io.c clear_bit_hook, see set_bit_hook for why
1793 */
1794 static void btrfs_clear_bit_hook(struct inode *inode,
1795 struct extent_state *state,
1796 unsigned *bits)
1797 {
1798 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1799 u64 len = state->end + 1 - state->start;
1800 u64 num_extents = div64_u64(len + BTRFS_MAX_EXTENT_SIZE -1,
1801 BTRFS_MAX_EXTENT_SIZE);
1802
1803 spin_lock(&BTRFS_I(inode)->lock);
1804 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
1805 BTRFS_I(inode)->defrag_bytes -= len;
1806 spin_unlock(&BTRFS_I(inode)->lock);
1807
1808 /*
1809 * set_bit and clear bit hooks normally require _irqsave/restore
1810 * but in this case, we are only testing for the DELALLOC
1811 * bit, which is only set or cleared with irqs on
1812 */
1813 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
1814 struct btrfs_root *root = BTRFS_I(inode)->root;
1815 bool do_list = !btrfs_is_free_space_inode(inode);
1816
1817 if (*bits & EXTENT_FIRST_DELALLOC) {
1818 *bits &= ~EXTENT_FIRST_DELALLOC;
1819 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1820 spin_lock(&BTRFS_I(inode)->lock);
1821 BTRFS_I(inode)->outstanding_extents -= num_extents;
1822 spin_unlock(&BTRFS_I(inode)->lock);
1823 }
1824
1825 /*
1826 * We don't reserve metadata space for space cache inodes so we
1827 * don't need to call dellalloc_release_metadata if there is an
1828 * error.
1829 */
1830 if (*bits & EXTENT_DO_ACCOUNTING &&
1831 root != fs_info->tree_root)
1832 btrfs_delalloc_release_metadata(inode, len);
1833
1834 /* For sanity tests. */
1835 if (btrfs_is_testing(fs_info))
1836 return;
1837
1838 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1839 && do_list && !(state->state & EXTENT_NORESERVE)
1840 && (*bits & (EXTENT_DO_ACCOUNTING |
1841 EXTENT_CLEAR_DATA_RESV)))
1842 btrfs_free_reserved_data_space_noquota(inode,
1843 state->start, len);
1844
1845 __percpu_counter_add(&fs_info->delalloc_bytes, -len,
1846 fs_info->delalloc_batch);
1847 spin_lock(&BTRFS_I(inode)->lock);
1848 BTRFS_I(inode)->delalloc_bytes -= len;
1849 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
1850 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1851 &BTRFS_I(inode)->runtime_flags))
1852 btrfs_del_delalloc_inode(root, inode);
1853 spin_unlock(&BTRFS_I(inode)->lock);
1854 }
1855 }
1856
1857 /*
1858 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1859 * we don't create bios that span stripes or chunks
1860 *
1861 * return 1 if page cannot be merged to bio
1862 * return 0 if page can be merged to bio
1863 * return error otherwise
1864 */
1865 int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
1866 size_t size, struct bio *bio,
1867 unsigned long bio_flags)
1868 {
1869 struct inode *inode = page->mapping->host;
1870 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1871 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
1872 u64 length = 0;
1873 u64 map_length;
1874 int ret;
1875
1876 if (bio_flags & EXTENT_BIO_COMPRESSED)
1877 return 0;
1878
1879 length = bio->bi_iter.bi_size;
1880 map_length = length;
1881 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1882 NULL, 0);
1883 if (ret < 0)
1884 return ret;
1885 if (map_length < length + size)
1886 return 1;
1887 return 0;
1888 }
1889
1890 /*
1891 * in order to insert checksums into the metadata in large chunks,
1892 * we wait until bio submission time. All the pages in the bio are
1893 * checksummed and sums are attached onto the ordered extent record.
1894 *
1895 * At IO completion time the cums attached on the ordered extent record
1896 * are inserted into the btree
1897 */
1898 static int __btrfs_submit_bio_start(struct inode *inode, struct bio *bio,
1899 int mirror_num, unsigned long bio_flags,
1900 u64 bio_offset)
1901 {
1902 int ret = 0;
1903
1904 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
1905 BUG_ON(ret); /* -ENOMEM */
1906 return 0;
1907 }
1908
1909 /*
1910 * in order to insert checksums into the metadata in large chunks,
1911 * we wait until bio submission time. All the pages in the bio are
1912 * checksummed and sums are attached onto the ordered extent record.
1913 *
1914 * At IO completion time the cums attached on the ordered extent record
1915 * are inserted into the btree
1916 */
1917 static int __btrfs_submit_bio_done(struct inode *inode, struct bio *bio,
1918 int mirror_num, unsigned long bio_flags,
1919 u64 bio_offset)
1920 {
1921 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1922 int ret;
1923
1924 ret = btrfs_map_bio(fs_info, bio, mirror_num, 1);
1925 if (ret) {
1926 bio->bi_error = ret;
1927 bio_endio(bio);
1928 }
1929 return ret;
1930 }
1931
1932 /*
1933 * extent_io.c submission hook. This does the right thing for csum calculation
1934 * on write, or reading the csums from the tree before a read
1935 */
1936 static int btrfs_submit_bio_hook(struct inode *inode, struct bio *bio,
1937 int mirror_num, unsigned long bio_flags,
1938 u64 bio_offset)
1939 {
1940 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1941 struct btrfs_root *root = BTRFS_I(inode)->root;
1942 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
1943 int ret = 0;
1944 int skip_sum;
1945 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
1946
1947 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
1948
1949 if (btrfs_is_free_space_inode(inode))
1950 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
1951
1952 if (bio_op(bio) != REQ_OP_WRITE) {
1953 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
1954 if (ret)
1955 goto out;
1956
1957 if (bio_flags & EXTENT_BIO_COMPRESSED) {
1958 ret = btrfs_submit_compressed_read(inode, bio,
1959 mirror_num,
1960 bio_flags);
1961 goto out;
1962 } else if (!skip_sum) {
1963 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
1964 if (ret)
1965 goto out;
1966 }
1967 goto mapit;
1968 } else if (async && !skip_sum) {
1969 /* csum items have already been cloned */
1970 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1971 goto mapit;
1972 /* we're doing a write, do the async checksumming */
1973 ret = btrfs_wq_submit_bio(fs_info, inode, bio, mirror_num,
1974 bio_flags, bio_offset,
1975 __btrfs_submit_bio_start,
1976 __btrfs_submit_bio_done);
1977 goto out;
1978 } else if (!skip_sum) {
1979 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
1980 if (ret)
1981 goto out;
1982 }
1983
1984 mapit:
1985 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
1986
1987 out:
1988 if (ret < 0) {
1989 bio->bi_error = ret;
1990 bio_endio(bio);
1991 }
1992 return ret;
1993 }
1994
1995 /*
1996 * given a list of ordered sums record them in the inode. This happens
1997 * at IO completion time based on sums calculated at bio submission time.
1998 */
1999 static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
2000 struct inode *inode, u64 file_offset,
2001 struct list_head *list)
2002 {
2003 struct btrfs_ordered_sum *sum;
2004
2005 list_for_each_entry(sum, list, list) {
2006 trans->adding_csums = 1;
2007 btrfs_csum_file_blocks(trans,
2008 BTRFS_I(inode)->root->fs_info->csum_root, sum);
2009 trans->adding_csums = 0;
2010 }
2011 return 0;
2012 }
2013
2014 int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
2015 struct extent_state **cached_state, int dedupe)
2016 {
2017 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
2018 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
2019 cached_state);
2020 }
2021
2022 /* see btrfs_writepage_start_hook for details on why this is required */
2023 struct btrfs_writepage_fixup {
2024 struct page *page;
2025 struct btrfs_work work;
2026 };
2027
2028 static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
2029 {
2030 struct btrfs_writepage_fixup *fixup;
2031 struct btrfs_ordered_extent *ordered;
2032 struct extent_state *cached_state = NULL;
2033 struct page *page;
2034 struct inode *inode;
2035 u64 page_start;
2036 u64 page_end;
2037 int ret;
2038
2039 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2040 page = fixup->page;
2041 again:
2042 lock_page(page);
2043 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2044 ClearPageChecked(page);
2045 goto out_page;
2046 }
2047
2048 inode = page->mapping->host;
2049 page_start = page_offset(page);
2050 page_end = page_offset(page) + PAGE_SIZE - 1;
2051
2052 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
2053 &cached_state);
2054
2055 /* already ordered? We're done */
2056 if (PagePrivate2(page))
2057 goto out;
2058
2059 ordered = btrfs_lookup_ordered_range(inode, page_start,
2060 PAGE_SIZE);
2061 if (ordered) {
2062 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
2063 page_end, &cached_state, GFP_NOFS);
2064 unlock_page(page);
2065 btrfs_start_ordered_extent(inode, ordered, 1);
2066 btrfs_put_ordered_extent(ordered);
2067 goto again;
2068 }
2069
2070 ret = btrfs_delalloc_reserve_space(inode, page_start,
2071 PAGE_SIZE);
2072 if (ret) {
2073 mapping_set_error(page->mapping, ret);
2074 end_extent_writepage(page, ret, page_start, page_end);
2075 ClearPageChecked(page);
2076 goto out;
2077 }
2078
2079 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state,
2080 0);
2081 ClearPageChecked(page);
2082 set_page_dirty(page);
2083 out:
2084 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
2085 &cached_state, GFP_NOFS);
2086 out_page:
2087 unlock_page(page);
2088 put_page(page);
2089 kfree(fixup);
2090 }
2091
2092 /*
2093 * There are a few paths in the higher layers of the kernel that directly
2094 * set the page dirty bit without asking the filesystem if it is a
2095 * good idea. This causes problems because we want to make sure COW
2096 * properly happens and the data=ordered rules are followed.
2097 *
2098 * In our case any range that doesn't have the ORDERED bit set
2099 * hasn't been properly setup for IO. We kick off an async process
2100 * to fix it up. The async helper will wait for ordered extents, set
2101 * the delalloc bit and make it safe to write the page.
2102 */
2103 static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
2104 {
2105 struct inode *inode = page->mapping->host;
2106 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2107 struct btrfs_writepage_fixup *fixup;
2108
2109 /* this page is properly in the ordered list */
2110 if (TestClearPagePrivate2(page))
2111 return 0;
2112
2113 if (PageChecked(page))
2114 return -EAGAIN;
2115
2116 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2117 if (!fixup)
2118 return -EAGAIN;
2119
2120 SetPageChecked(page);
2121 get_page(page);
2122 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2123 btrfs_writepage_fixup_worker, NULL, NULL);
2124 fixup->page = page;
2125 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
2126 return -EBUSY;
2127 }
2128
2129 static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2130 struct inode *inode, u64 file_pos,
2131 u64 disk_bytenr, u64 disk_num_bytes,
2132 u64 num_bytes, u64 ram_bytes,
2133 u8 compression, u8 encryption,
2134 u16 other_encoding, int extent_type)
2135 {
2136 struct btrfs_root *root = BTRFS_I(inode)->root;
2137 struct btrfs_file_extent_item *fi;
2138 struct btrfs_path *path;
2139 struct extent_buffer *leaf;
2140 struct btrfs_key ins;
2141 int extent_inserted = 0;
2142 int ret;
2143
2144 path = btrfs_alloc_path();
2145 if (!path)
2146 return -ENOMEM;
2147
2148 /*
2149 * we may be replacing one extent in the tree with another.
2150 * The new extent is pinned in the extent map, and we don't want
2151 * to drop it from the cache until it is completely in the btree.
2152 *
2153 * So, tell btrfs_drop_extents to leave this extent in the cache.
2154 * the caller is expected to unpin it and allow it to be merged
2155 * with the others.
2156 */
2157 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2158 file_pos + num_bytes, NULL, 0,
2159 1, sizeof(*fi), &extent_inserted);
2160 if (ret)
2161 goto out;
2162
2163 if (!extent_inserted) {
2164 ins.objectid = btrfs_ino(inode);
2165 ins.offset = file_pos;
2166 ins.type = BTRFS_EXTENT_DATA_KEY;
2167
2168 path->leave_spinning = 1;
2169 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2170 sizeof(*fi));
2171 if (ret)
2172 goto out;
2173 }
2174 leaf = path->nodes[0];
2175 fi = btrfs_item_ptr(leaf, path->slots[0],
2176 struct btrfs_file_extent_item);
2177 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2178 btrfs_set_file_extent_type(leaf, fi, extent_type);
2179 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2180 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2181 btrfs_set_file_extent_offset(leaf, fi, 0);
2182 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2183 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2184 btrfs_set_file_extent_compression(leaf, fi, compression);
2185 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2186 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
2187
2188 btrfs_mark_buffer_dirty(leaf);
2189 btrfs_release_path(path);
2190
2191 inode_add_bytes(inode, num_bytes);
2192
2193 ins.objectid = disk_bytenr;
2194 ins.offset = disk_num_bytes;
2195 ins.type = BTRFS_EXTENT_ITEM_KEY;
2196 ret = btrfs_alloc_reserved_file_extent(trans, root->root_key.objectid,
2197 btrfs_ino(inode), file_pos,
2198 ram_bytes, &ins);
2199 /*
2200 * Release the reserved range from inode dirty range map, as it is
2201 * already moved into delayed_ref_head
2202 */
2203 btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
2204 out:
2205 btrfs_free_path(path);
2206
2207 return ret;
2208 }
2209
2210 /* snapshot-aware defrag */
2211 struct sa_defrag_extent_backref {
2212 struct rb_node node;
2213 struct old_sa_defrag_extent *old;
2214 u64 root_id;
2215 u64 inum;
2216 u64 file_pos;
2217 u64 extent_offset;
2218 u64 num_bytes;
2219 u64 generation;
2220 };
2221
2222 struct old_sa_defrag_extent {
2223 struct list_head list;
2224 struct new_sa_defrag_extent *new;
2225
2226 u64 extent_offset;
2227 u64 bytenr;
2228 u64 offset;
2229 u64 len;
2230 int count;
2231 };
2232
2233 struct new_sa_defrag_extent {
2234 struct rb_root root;
2235 struct list_head head;
2236 struct btrfs_path *path;
2237 struct inode *inode;
2238 u64 file_pos;
2239 u64 len;
2240 u64 bytenr;
2241 u64 disk_len;
2242 u8 compress_type;
2243 };
2244
2245 static int backref_comp(struct sa_defrag_extent_backref *b1,
2246 struct sa_defrag_extent_backref *b2)
2247 {
2248 if (b1->root_id < b2->root_id)
2249 return -1;
2250 else if (b1->root_id > b2->root_id)
2251 return 1;
2252
2253 if (b1->inum < b2->inum)
2254 return -1;
2255 else if (b1->inum > b2->inum)
2256 return 1;
2257
2258 if (b1->file_pos < b2->file_pos)
2259 return -1;
2260 else if (b1->file_pos > b2->file_pos)
2261 return 1;
2262
2263 /*
2264 * [------------------------------] ===> (a range of space)
2265 * |<--->| |<---->| =============> (fs/file tree A)
2266 * |<---------------------------->| ===> (fs/file tree B)
2267 *
2268 * A range of space can refer to two file extents in one tree while
2269 * refer to only one file extent in another tree.
2270 *
2271 * So we may process a disk offset more than one time(two extents in A)
2272 * and locate at the same extent(one extent in B), then insert two same
2273 * backrefs(both refer to the extent in B).
2274 */
2275 return 0;
2276 }
2277
2278 static void backref_insert(struct rb_root *root,
2279 struct sa_defrag_extent_backref *backref)
2280 {
2281 struct rb_node **p = &root->rb_node;
2282 struct rb_node *parent = NULL;
2283 struct sa_defrag_extent_backref *entry;
2284 int ret;
2285
2286 while (*p) {
2287 parent = *p;
2288 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2289
2290 ret = backref_comp(backref, entry);
2291 if (ret < 0)
2292 p = &(*p)->rb_left;
2293 else
2294 p = &(*p)->rb_right;
2295 }
2296
2297 rb_link_node(&backref->node, parent, p);
2298 rb_insert_color(&backref->node, root);
2299 }
2300
2301 /*
2302 * Note the backref might has changed, and in this case we just return 0.
2303 */
2304 static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2305 void *ctx)
2306 {
2307 struct btrfs_file_extent_item *extent;
2308 struct old_sa_defrag_extent *old = ctx;
2309 struct new_sa_defrag_extent *new = old->new;
2310 struct btrfs_path *path = new->path;
2311 struct btrfs_key key;
2312 struct btrfs_root *root;
2313 struct sa_defrag_extent_backref *backref;
2314 struct extent_buffer *leaf;
2315 struct inode *inode = new->inode;
2316 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2317 int slot;
2318 int ret;
2319 u64 extent_offset;
2320 u64 num_bytes;
2321
2322 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2323 inum == btrfs_ino(inode))
2324 return 0;
2325
2326 key.objectid = root_id;
2327 key.type = BTRFS_ROOT_ITEM_KEY;
2328 key.offset = (u64)-1;
2329
2330 root = btrfs_read_fs_root_no_name(fs_info, &key);
2331 if (IS_ERR(root)) {
2332 if (PTR_ERR(root) == -ENOENT)
2333 return 0;
2334 WARN_ON(1);
2335 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
2336 inum, offset, root_id);
2337 return PTR_ERR(root);
2338 }
2339
2340 key.objectid = inum;
2341 key.type = BTRFS_EXTENT_DATA_KEY;
2342 if (offset > (u64)-1 << 32)
2343 key.offset = 0;
2344 else
2345 key.offset = offset;
2346
2347 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2348 if (WARN_ON(ret < 0))
2349 return ret;
2350 ret = 0;
2351
2352 while (1) {
2353 cond_resched();
2354
2355 leaf = path->nodes[0];
2356 slot = path->slots[0];
2357
2358 if (slot >= btrfs_header_nritems(leaf)) {
2359 ret = btrfs_next_leaf(root, path);
2360 if (ret < 0) {
2361 goto out;
2362 } else if (ret > 0) {
2363 ret = 0;
2364 goto out;
2365 }
2366 continue;
2367 }
2368
2369 path->slots[0]++;
2370
2371 btrfs_item_key_to_cpu(leaf, &key, slot);
2372
2373 if (key.objectid > inum)
2374 goto out;
2375
2376 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2377 continue;
2378
2379 extent = btrfs_item_ptr(leaf, slot,
2380 struct btrfs_file_extent_item);
2381
2382 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2383 continue;
2384
2385 /*
2386 * 'offset' refers to the exact key.offset,
2387 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2388 * (key.offset - extent_offset).
2389 */
2390 if (key.offset != offset)
2391 continue;
2392
2393 extent_offset = btrfs_file_extent_offset(leaf, extent);
2394 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
2395
2396 if (extent_offset >= old->extent_offset + old->offset +
2397 old->len || extent_offset + num_bytes <=
2398 old->extent_offset + old->offset)
2399 continue;
2400 break;
2401 }
2402
2403 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2404 if (!backref) {
2405 ret = -ENOENT;
2406 goto out;
2407 }
2408
2409 backref->root_id = root_id;
2410 backref->inum = inum;
2411 backref->file_pos = offset;
2412 backref->num_bytes = num_bytes;
2413 backref->extent_offset = extent_offset;
2414 backref->generation = btrfs_file_extent_generation(leaf, extent);
2415 backref->old = old;
2416 backref_insert(&new->root, backref);
2417 old->count++;
2418 out:
2419 btrfs_release_path(path);
2420 WARN_ON(ret);
2421 return ret;
2422 }
2423
2424 static noinline bool record_extent_backrefs(struct btrfs_path *path,
2425 struct new_sa_defrag_extent *new)
2426 {
2427 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
2428 struct old_sa_defrag_extent *old, *tmp;
2429 int ret;
2430
2431 new->path = path;
2432
2433 list_for_each_entry_safe(old, tmp, &new->head, list) {
2434 ret = iterate_inodes_from_logical(old->bytenr +
2435 old->extent_offset, fs_info,
2436 path, record_one_backref,
2437 old);
2438 if (ret < 0 && ret != -ENOENT)
2439 return false;
2440
2441 /* no backref to be processed for this extent */
2442 if (!old->count) {
2443 list_del(&old->list);
2444 kfree(old);
2445 }
2446 }
2447
2448 if (list_empty(&new->head))
2449 return false;
2450
2451 return true;
2452 }
2453
2454 static int relink_is_mergable(struct extent_buffer *leaf,
2455 struct btrfs_file_extent_item *fi,
2456 struct new_sa_defrag_extent *new)
2457 {
2458 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
2459 return 0;
2460
2461 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2462 return 0;
2463
2464 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2465 return 0;
2466
2467 if (btrfs_file_extent_encryption(leaf, fi) ||
2468 btrfs_file_extent_other_encoding(leaf, fi))
2469 return 0;
2470
2471 return 1;
2472 }
2473
2474 /*
2475 * Note the backref might has changed, and in this case we just return 0.
2476 */
2477 static noinline int relink_extent_backref(struct btrfs_path *path,
2478 struct sa_defrag_extent_backref *prev,
2479 struct sa_defrag_extent_backref *backref)
2480 {
2481 struct btrfs_file_extent_item *extent;
2482 struct btrfs_file_extent_item *item;
2483 struct btrfs_ordered_extent *ordered;
2484 struct btrfs_trans_handle *trans;
2485 struct btrfs_root *root;
2486 struct btrfs_key key;
2487 struct extent_buffer *leaf;
2488 struct old_sa_defrag_extent *old = backref->old;
2489 struct new_sa_defrag_extent *new = old->new;
2490 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
2491 struct inode *inode;
2492 struct extent_state *cached = NULL;
2493 int ret = 0;
2494 u64 start;
2495 u64 len;
2496 u64 lock_start;
2497 u64 lock_end;
2498 bool merge = false;
2499 int index;
2500
2501 if (prev && prev->root_id == backref->root_id &&
2502 prev->inum == backref->inum &&
2503 prev->file_pos + prev->num_bytes == backref->file_pos)
2504 merge = true;
2505
2506 /* step 1: get root */
2507 key.objectid = backref->root_id;
2508 key.type = BTRFS_ROOT_ITEM_KEY;
2509 key.offset = (u64)-1;
2510
2511 index = srcu_read_lock(&fs_info->subvol_srcu);
2512
2513 root = btrfs_read_fs_root_no_name(fs_info, &key);
2514 if (IS_ERR(root)) {
2515 srcu_read_unlock(&fs_info->subvol_srcu, index);
2516 if (PTR_ERR(root) == -ENOENT)
2517 return 0;
2518 return PTR_ERR(root);
2519 }
2520
2521 if (btrfs_root_readonly(root)) {
2522 srcu_read_unlock(&fs_info->subvol_srcu, index);
2523 return 0;
2524 }
2525
2526 /* step 2: get inode */
2527 key.objectid = backref->inum;
2528 key.type = BTRFS_INODE_ITEM_KEY;
2529 key.offset = 0;
2530
2531 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2532 if (IS_ERR(inode)) {
2533 srcu_read_unlock(&fs_info->subvol_srcu, index);
2534 return 0;
2535 }
2536
2537 srcu_read_unlock(&fs_info->subvol_srcu, index);
2538
2539 /* step 3: relink backref */
2540 lock_start = backref->file_pos;
2541 lock_end = backref->file_pos + backref->num_bytes - 1;
2542 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2543 &cached);
2544
2545 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2546 if (ordered) {
2547 btrfs_put_ordered_extent(ordered);
2548 goto out_unlock;
2549 }
2550
2551 trans = btrfs_join_transaction(root);
2552 if (IS_ERR(trans)) {
2553 ret = PTR_ERR(trans);
2554 goto out_unlock;
2555 }
2556
2557 key.objectid = backref->inum;
2558 key.type = BTRFS_EXTENT_DATA_KEY;
2559 key.offset = backref->file_pos;
2560
2561 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2562 if (ret < 0) {
2563 goto out_free_path;
2564 } else if (ret > 0) {
2565 ret = 0;
2566 goto out_free_path;
2567 }
2568
2569 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2570 struct btrfs_file_extent_item);
2571
2572 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2573 backref->generation)
2574 goto out_free_path;
2575
2576 btrfs_release_path(path);
2577
2578 start = backref->file_pos;
2579 if (backref->extent_offset < old->extent_offset + old->offset)
2580 start += old->extent_offset + old->offset -
2581 backref->extent_offset;
2582
2583 len = min(backref->extent_offset + backref->num_bytes,
2584 old->extent_offset + old->offset + old->len);
2585 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2586
2587 ret = btrfs_drop_extents(trans, root, inode, start,
2588 start + len, 1);
2589 if (ret)
2590 goto out_free_path;
2591 again:
2592 key.objectid = btrfs_ino(inode);
2593 key.type = BTRFS_EXTENT_DATA_KEY;
2594 key.offset = start;
2595
2596 path->leave_spinning = 1;
2597 if (merge) {
2598 struct btrfs_file_extent_item *fi;
2599 u64 extent_len;
2600 struct btrfs_key found_key;
2601
2602 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2603 if (ret < 0)
2604 goto out_free_path;
2605
2606 path->slots[0]--;
2607 leaf = path->nodes[0];
2608 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2609
2610 fi = btrfs_item_ptr(leaf, path->slots[0],
2611 struct btrfs_file_extent_item);
2612 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2613
2614 if (extent_len + found_key.offset == start &&
2615 relink_is_mergable(leaf, fi, new)) {
2616 btrfs_set_file_extent_num_bytes(leaf, fi,
2617 extent_len + len);
2618 btrfs_mark_buffer_dirty(leaf);
2619 inode_add_bytes(inode, len);
2620
2621 ret = 1;
2622 goto out_free_path;
2623 } else {
2624 merge = false;
2625 btrfs_release_path(path);
2626 goto again;
2627 }
2628 }
2629
2630 ret = btrfs_insert_empty_item(trans, root, path, &key,
2631 sizeof(*extent));
2632 if (ret) {
2633 btrfs_abort_transaction(trans, ret);
2634 goto out_free_path;
2635 }
2636
2637 leaf = path->nodes[0];
2638 item = btrfs_item_ptr(leaf, path->slots[0],
2639 struct btrfs_file_extent_item);
2640 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2641 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2642 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2643 btrfs_set_file_extent_num_bytes(leaf, item, len);
2644 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2645 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2646 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2647 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2648 btrfs_set_file_extent_encryption(leaf, item, 0);
2649 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2650
2651 btrfs_mark_buffer_dirty(leaf);
2652 inode_add_bytes(inode, len);
2653 btrfs_release_path(path);
2654
2655 ret = btrfs_inc_extent_ref(trans, fs_info, new->bytenr,
2656 new->disk_len, 0,
2657 backref->root_id, backref->inum,
2658 new->file_pos); /* start - extent_offset */
2659 if (ret) {
2660 btrfs_abort_transaction(trans, ret);
2661 goto out_free_path;
2662 }
2663
2664 ret = 1;
2665 out_free_path:
2666 btrfs_release_path(path);
2667 path->leave_spinning = 0;
2668 btrfs_end_transaction(trans);
2669 out_unlock:
2670 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2671 &cached, GFP_NOFS);
2672 iput(inode);
2673 return ret;
2674 }
2675
2676 static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2677 {
2678 struct old_sa_defrag_extent *old, *tmp;
2679
2680 if (!new)
2681 return;
2682
2683 list_for_each_entry_safe(old, tmp, &new->head, list) {
2684 kfree(old);
2685 }
2686 kfree(new);
2687 }
2688
2689 static void relink_file_extents(struct new_sa_defrag_extent *new)
2690 {
2691 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
2692 struct btrfs_path *path;
2693 struct sa_defrag_extent_backref *backref;
2694 struct sa_defrag_extent_backref *prev = NULL;
2695 struct inode *inode;
2696 struct btrfs_root *root;
2697 struct rb_node *node;
2698 int ret;
2699
2700 inode = new->inode;
2701 root = BTRFS_I(inode)->root;
2702
2703 path = btrfs_alloc_path();
2704 if (!path)
2705 return;
2706
2707 if (!record_extent_backrefs(path, new)) {
2708 btrfs_free_path(path);
2709 goto out;
2710 }
2711 btrfs_release_path(path);
2712
2713 while (1) {
2714 node = rb_first(&new->root);
2715 if (!node)
2716 break;
2717 rb_erase(node, &new->root);
2718
2719 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2720
2721 ret = relink_extent_backref(path, prev, backref);
2722 WARN_ON(ret < 0);
2723
2724 kfree(prev);
2725
2726 if (ret == 1)
2727 prev = backref;
2728 else
2729 prev = NULL;
2730 cond_resched();
2731 }
2732 kfree(prev);
2733
2734 btrfs_free_path(path);
2735 out:
2736 free_sa_defrag_extent(new);
2737
2738 atomic_dec(&fs_info->defrag_running);
2739 wake_up(&fs_info->transaction_wait);
2740 }
2741
2742 static struct new_sa_defrag_extent *
2743 record_old_file_extents(struct inode *inode,
2744 struct btrfs_ordered_extent *ordered)
2745 {
2746 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2747 struct btrfs_root *root = BTRFS_I(inode)->root;
2748 struct btrfs_path *path;
2749 struct btrfs_key key;
2750 struct old_sa_defrag_extent *old;
2751 struct new_sa_defrag_extent *new;
2752 int ret;
2753
2754 new = kmalloc(sizeof(*new), GFP_NOFS);
2755 if (!new)
2756 return NULL;
2757
2758 new->inode = inode;
2759 new->file_pos = ordered->file_offset;
2760 new->len = ordered->len;
2761 new->bytenr = ordered->start;
2762 new->disk_len = ordered->disk_len;
2763 new->compress_type = ordered->compress_type;
2764 new->root = RB_ROOT;
2765 INIT_LIST_HEAD(&new->head);
2766
2767 path = btrfs_alloc_path();
2768 if (!path)
2769 goto out_kfree;
2770
2771 key.objectid = btrfs_ino(inode);
2772 key.type = BTRFS_EXTENT_DATA_KEY;
2773 key.offset = new->file_pos;
2774
2775 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2776 if (ret < 0)
2777 goto out_free_path;
2778 if (ret > 0 && path->slots[0] > 0)
2779 path->slots[0]--;
2780
2781 /* find out all the old extents for the file range */
2782 while (1) {
2783 struct btrfs_file_extent_item *extent;
2784 struct extent_buffer *l;
2785 int slot;
2786 u64 num_bytes;
2787 u64 offset;
2788 u64 end;
2789 u64 disk_bytenr;
2790 u64 extent_offset;
2791
2792 l = path->nodes[0];
2793 slot = path->slots[0];
2794
2795 if (slot >= btrfs_header_nritems(l)) {
2796 ret = btrfs_next_leaf(root, path);
2797 if (ret < 0)
2798 goto out_free_path;
2799 else if (ret > 0)
2800 break;
2801 continue;
2802 }
2803
2804 btrfs_item_key_to_cpu(l, &key, slot);
2805
2806 if (key.objectid != btrfs_ino(inode))
2807 break;
2808 if (key.type != BTRFS_EXTENT_DATA_KEY)
2809 break;
2810 if (key.offset >= new->file_pos + new->len)
2811 break;
2812
2813 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2814
2815 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2816 if (key.offset + num_bytes < new->file_pos)
2817 goto next;
2818
2819 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2820 if (!disk_bytenr)
2821 goto next;
2822
2823 extent_offset = btrfs_file_extent_offset(l, extent);
2824
2825 old = kmalloc(sizeof(*old), GFP_NOFS);
2826 if (!old)
2827 goto out_free_path;
2828
2829 offset = max(new->file_pos, key.offset);
2830 end = min(new->file_pos + new->len, key.offset + num_bytes);
2831
2832 old->bytenr = disk_bytenr;
2833 old->extent_offset = extent_offset;
2834 old->offset = offset - key.offset;
2835 old->len = end - offset;
2836 old->new = new;
2837 old->count = 0;
2838 list_add_tail(&old->list, &new->head);
2839 next:
2840 path->slots[0]++;
2841 cond_resched();
2842 }
2843
2844 btrfs_free_path(path);
2845 atomic_inc(&fs_info->defrag_running);
2846
2847 return new;
2848
2849 out_free_path:
2850 btrfs_free_path(path);
2851 out_kfree:
2852 free_sa_defrag_extent(new);
2853 return NULL;
2854 }
2855
2856 static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
2857 u64 start, u64 len)
2858 {
2859 struct btrfs_block_group_cache *cache;
2860
2861 cache = btrfs_lookup_block_group(fs_info, start);
2862 ASSERT(cache);
2863
2864 spin_lock(&cache->lock);
2865 cache->delalloc_bytes -= len;
2866 spin_unlock(&cache->lock);
2867
2868 btrfs_put_block_group(cache);
2869 }
2870
2871 /* as ordered data IO finishes, this gets called so we can finish
2872 * an ordered extent if the range of bytes in the file it covers are
2873 * fully written.
2874 */
2875 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
2876 {
2877 struct inode *inode = ordered_extent->inode;
2878 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2879 struct btrfs_root *root = BTRFS_I(inode)->root;
2880 struct btrfs_trans_handle *trans = NULL;
2881 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2882 struct extent_state *cached_state = NULL;
2883 struct new_sa_defrag_extent *new = NULL;
2884 int compress_type = 0;
2885 int ret = 0;
2886 u64 logical_len = ordered_extent->len;
2887 bool nolock;
2888 bool truncated = false;
2889
2890 nolock = btrfs_is_free_space_inode(inode);
2891
2892 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2893 ret = -EIO;
2894 goto out;
2895 }
2896
2897 btrfs_free_io_failure_record(inode, ordered_extent->file_offset,
2898 ordered_extent->file_offset +
2899 ordered_extent->len - 1);
2900
2901 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2902 truncated = true;
2903 logical_len = ordered_extent->truncated_len;
2904 /* Truncated the entire extent, don't bother adding */
2905 if (!logical_len)
2906 goto out;
2907 }
2908
2909 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
2910 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
2911
2912 /*
2913 * For mwrite(mmap + memset to write) case, we still reserve
2914 * space for NOCOW range.
2915 * As NOCOW won't cause a new delayed ref, just free the space
2916 */
2917 btrfs_qgroup_free_data(inode, ordered_extent->file_offset,
2918 ordered_extent->len);
2919 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2920 if (nolock)
2921 trans = btrfs_join_transaction_nolock(root);
2922 else
2923 trans = btrfs_join_transaction(root);
2924 if (IS_ERR(trans)) {
2925 ret = PTR_ERR(trans);
2926 trans = NULL;
2927 goto out;
2928 }
2929 trans->block_rsv = &fs_info->delalloc_block_rsv;
2930 ret = btrfs_update_inode_fallback(trans, root, inode);
2931 if (ret) /* -ENOMEM or corruption */
2932 btrfs_abort_transaction(trans, ret);
2933 goto out;
2934 }
2935
2936 lock_extent_bits(io_tree, ordered_extent->file_offset,
2937 ordered_extent->file_offset + ordered_extent->len - 1,
2938 &cached_state);
2939
2940 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2941 ordered_extent->file_offset + ordered_extent->len - 1,
2942 EXTENT_DEFRAG, 1, cached_state);
2943 if (ret) {
2944 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
2945 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
2946 /* the inode is shared */
2947 new = record_old_file_extents(inode, ordered_extent);
2948
2949 clear_extent_bit(io_tree, ordered_extent->file_offset,
2950 ordered_extent->file_offset + ordered_extent->len - 1,
2951 EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2952 }
2953
2954 if (nolock)
2955 trans = btrfs_join_transaction_nolock(root);
2956 else
2957 trans = btrfs_join_transaction(root);
2958 if (IS_ERR(trans)) {
2959 ret = PTR_ERR(trans);
2960 trans = NULL;
2961 goto out_unlock;
2962 }
2963
2964 trans->block_rsv = &fs_info->delalloc_block_rsv;
2965
2966 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
2967 compress_type = ordered_extent->compress_type;
2968 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
2969 BUG_ON(compress_type);
2970 ret = btrfs_mark_extent_written(trans, inode,
2971 ordered_extent->file_offset,
2972 ordered_extent->file_offset +
2973 logical_len);
2974 } else {
2975 BUG_ON(root == fs_info->tree_root);
2976 ret = insert_reserved_file_extent(trans, inode,
2977 ordered_extent->file_offset,
2978 ordered_extent->start,
2979 ordered_extent->disk_len,
2980 logical_len, logical_len,
2981 compress_type, 0, 0,
2982 BTRFS_FILE_EXTENT_REG);
2983 if (!ret)
2984 btrfs_release_delalloc_bytes(fs_info,
2985 ordered_extent->start,
2986 ordered_extent->disk_len);
2987 }
2988 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2989 ordered_extent->file_offset, ordered_extent->len,
2990 trans->transid);
2991 if (ret < 0) {
2992 btrfs_abort_transaction(trans, ret);
2993 goto out_unlock;
2994 }
2995
2996 add_pending_csums(trans, inode, ordered_extent->file_offset,
2997 &ordered_extent->list);
2998
2999 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3000 ret = btrfs_update_inode_fallback(trans, root, inode);
3001 if (ret) { /* -ENOMEM or corruption */
3002 btrfs_abort_transaction(trans, ret);
3003 goto out_unlock;
3004 }
3005 ret = 0;
3006 out_unlock:
3007 unlock_extent_cached(io_tree, ordered_extent->file_offset,
3008 ordered_extent->file_offset +
3009 ordered_extent->len - 1, &cached_state, GFP_NOFS);
3010 out:
3011 if (root != fs_info->tree_root)
3012 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
3013 if (trans)
3014 btrfs_end_transaction(trans);
3015
3016 if (ret || truncated) {
3017 u64 start, end;
3018
3019 if (truncated)
3020 start = ordered_extent->file_offset + logical_len;
3021 else
3022 start = ordered_extent->file_offset;
3023 end = ordered_extent->file_offset + ordered_extent->len - 1;
3024 clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
3025
3026 /* Drop the cache for the part of the extent we didn't write. */
3027 btrfs_drop_extent_cache(inode, start, end, 0);
3028
3029 /*
3030 * If the ordered extent had an IOERR or something else went
3031 * wrong we need to return the space for this ordered extent
3032 * back to the allocator. We only free the extent in the
3033 * truncated case if we didn't write out the extent at all.
3034 */
3035 if ((ret || !logical_len) &&
3036 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
3037 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
3038 btrfs_free_reserved_extent(fs_info,
3039 ordered_extent->start,
3040 ordered_extent->disk_len, 1);
3041 }
3042
3043
3044 /*
3045 * This needs to be done to make sure anybody waiting knows we are done
3046 * updating everything for this ordered extent.
3047 */
3048 btrfs_remove_ordered_extent(inode, ordered_extent);
3049
3050 /* for snapshot-aware defrag */
3051 if (new) {
3052 if (ret) {
3053 free_sa_defrag_extent(new);
3054 atomic_dec(&fs_info->defrag_running);
3055 } else {
3056 relink_file_extents(new);
3057 }
3058 }
3059
3060 /* once for us */
3061 btrfs_put_ordered_extent(ordered_extent);
3062 /* once for the tree */
3063 btrfs_put_ordered_extent(ordered_extent);
3064
3065 return ret;
3066 }
3067
3068 static void finish_ordered_fn(struct btrfs_work *work)
3069 {
3070 struct btrfs_ordered_extent *ordered_extent;
3071 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3072 btrfs_finish_ordered_io(ordered_extent);
3073 }
3074
3075 static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
3076 struct extent_state *state, int uptodate)
3077 {
3078 struct inode *inode = page->mapping->host;
3079 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3080 struct btrfs_ordered_extent *ordered_extent = NULL;
3081 struct btrfs_workqueue *wq;
3082 btrfs_work_func_t func;
3083
3084 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3085
3086 ClearPagePrivate2(page);
3087 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3088 end - start + 1, uptodate))
3089 return 0;
3090
3091 if (btrfs_is_free_space_inode(inode)) {
3092 wq = fs_info->endio_freespace_worker;
3093 func = btrfs_freespace_write_helper;
3094 } else {
3095 wq = fs_info->endio_write_workers;
3096 func = btrfs_endio_write_helper;
3097 }
3098
3099 btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3100 NULL);
3101 btrfs_queue_work(wq, &ordered_extent->work);
3102
3103 return 0;
3104 }
3105
3106 static int __readpage_endio_check(struct inode *inode,
3107 struct btrfs_io_bio *io_bio,
3108 int icsum, struct page *page,
3109 int pgoff, u64 start, size_t len)
3110 {
3111 char *kaddr;
3112 u32 csum_expected;
3113 u32 csum = ~(u32)0;
3114
3115 csum_expected = *(((u32 *)io_bio->csum) + icsum);
3116
3117 kaddr = kmap_atomic(page);
3118 csum = btrfs_csum_data(kaddr + pgoff, csum, len);
3119 btrfs_csum_final(csum, (u8 *)&csum);
3120 if (csum != csum_expected)
3121 goto zeroit;
3122
3123 kunmap_atomic(kaddr);
3124 return 0;
3125 zeroit:
3126 btrfs_warn_rl(BTRFS_I(inode)->root->fs_info,
3127 "csum failed ino %llu off %llu csum %u expected csum %u",
3128 btrfs_ino(inode), start, csum, csum_expected);
3129 memset(kaddr + pgoff, 1, len);
3130 flush_dcache_page(page);
3131 kunmap_atomic(kaddr);
3132 if (csum_expected == 0)
3133 return 0;
3134 return -EIO;
3135 }
3136
3137 /*
3138 * when reads are done, we need to check csums to verify the data is correct
3139 * if there's a match, we allow the bio to finish. If not, the code in
3140 * extent_io.c will try to find good copies for us.
3141 */
3142 static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3143 u64 phy_offset, struct page *page,
3144 u64 start, u64 end, int mirror)
3145 {
3146 size_t offset = start - page_offset(page);
3147 struct inode *inode = page->mapping->host;
3148 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3149 struct btrfs_root *root = BTRFS_I(inode)->root;
3150
3151 if (PageChecked(page)) {
3152 ClearPageChecked(page);
3153 return 0;
3154 }
3155
3156 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
3157 return 0;
3158
3159 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
3160 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
3161 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
3162 return 0;
3163 }
3164
3165 phy_offset >>= inode->i_sb->s_blocksize_bits;
3166 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3167 start, (size_t)(end - start + 1));
3168 }
3169
3170 void btrfs_add_delayed_iput(struct inode *inode)
3171 {
3172 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3173 struct btrfs_inode *binode = BTRFS_I(inode);
3174
3175 if (atomic_add_unless(&inode->i_count, -1, 1))
3176 return;
3177
3178 spin_lock(&fs_info->delayed_iput_lock);
3179 if (binode->delayed_iput_count == 0) {
3180 ASSERT(list_empty(&binode->delayed_iput));
3181 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
3182 } else {
3183 binode->delayed_iput_count++;
3184 }
3185 spin_unlock(&fs_info->delayed_iput_lock);
3186 }
3187
3188 void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
3189 {
3190
3191 spin_lock(&fs_info->delayed_iput_lock);
3192 while (!list_empty(&fs_info->delayed_iputs)) {
3193 struct btrfs_inode *inode;
3194
3195 inode = list_first_entry(&fs_info->delayed_iputs,
3196 struct btrfs_inode, delayed_iput);
3197 if (inode->delayed_iput_count) {
3198 inode->delayed_iput_count--;
3199 list_move_tail(&inode->delayed_iput,
3200 &fs_info->delayed_iputs);
3201 } else {
3202 list_del_init(&inode->delayed_iput);
3203 }
3204 spin_unlock(&fs_info->delayed_iput_lock);
3205 iput(&inode->vfs_inode);
3206 spin_lock(&fs_info->delayed_iput_lock);
3207 }
3208 spin_unlock(&fs_info->delayed_iput_lock);
3209 }
3210
3211 /*
3212 * This is called in transaction commit time. If there are no orphan
3213 * files in the subvolume, it removes orphan item and frees block_rsv
3214 * structure.
3215 */
3216 void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3217 struct btrfs_root *root)
3218 {
3219 struct btrfs_fs_info *fs_info = root->fs_info;
3220 struct btrfs_block_rsv *block_rsv;
3221 int ret;
3222
3223 if (atomic_read(&root->orphan_inodes) ||
3224 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3225 return;
3226
3227 spin_lock(&root->orphan_lock);
3228 if (atomic_read(&root->orphan_inodes)) {
3229 spin_unlock(&root->orphan_lock);
3230 return;
3231 }
3232
3233 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3234 spin_unlock(&root->orphan_lock);
3235 return;
3236 }
3237
3238 block_rsv = root->orphan_block_rsv;
3239 root->orphan_block_rsv = NULL;
3240 spin_unlock(&root->orphan_lock);
3241
3242 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
3243 btrfs_root_refs(&root->root_item) > 0) {
3244 ret = btrfs_del_orphan_item(trans, fs_info->tree_root,
3245 root->root_key.objectid);
3246 if (ret)
3247 btrfs_abort_transaction(trans, ret);
3248 else
3249 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3250 &root->state);
3251 }
3252
3253 if (block_rsv) {
3254 WARN_ON(block_rsv->size > 0);
3255 btrfs_free_block_rsv(fs_info, block_rsv);
3256 }
3257 }
3258
3259 /*
3260 * This creates an orphan entry for the given inode in case something goes
3261 * wrong in the middle of an unlink/truncate.
3262 *
3263 * NOTE: caller of this function should reserve 5 units of metadata for
3264 * this function.
3265 */
3266 int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
3267 {
3268 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3269 struct btrfs_root *root = BTRFS_I(inode)->root;
3270 struct btrfs_block_rsv *block_rsv = NULL;
3271 int reserve = 0;
3272 int insert = 0;
3273 int ret;
3274
3275 if (!root->orphan_block_rsv) {
3276 block_rsv = btrfs_alloc_block_rsv(fs_info,
3277 BTRFS_BLOCK_RSV_TEMP);
3278 if (!block_rsv)
3279 return -ENOMEM;
3280 }
3281
3282 spin_lock(&root->orphan_lock);
3283 if (!root->orphan_block_rsv) {
3284 root->orphan_block_rsv = block_rsv;
3285 } else if (block_rsv) {
3286 btrfs_free_block_rsv(fs_info, block_rsv);
3287 block_rsv = NULL;
3288 }
3289
3290 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3291 &BTRFS_I(inode)->runtime_flags)) {
3292 #if 0
3293 /*
3294 * For proper ENOSPC handling, we should do orphan
3295 * cleanup when mounting. But this introduces backward
3296 * compatibility issue.
3297 */
3298 if (!xchg(&root->orphan_item_inserted, 1))
3299 insert = 2;
3300 else
3301 insert = 1;
3302 #endif
3303 insert = 1;
3304 atomic_inc(&root->orphan_inodes);
3305 }
3306
3307 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3308 &BTRFS_I(inode)->runtime_flags))
3309 reserve = 1;
3310 spin_unlock(&root->orphan_lock);
3311
3312 /* grab metadata reservation from transaction handle */
3313 if (reserve) {
3314 ret = btrfs_orphan_reserve_metadata(trans, inode);
3315 ASSERT(!ret);
3316 if (ret) {
3317 atomic_dec(&root->orphan_inodes);
3318 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3319 &BTRFS_I(inode)->runtime_flags);
3320 if (insert)
3321 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3322 &BTRFS_I(inode)->runtime_flags);
3323 return ret;
3324 }
3325 }
3326
3327 /* insert an orphan item to track this unlinked/truncated file */
3328 if (insert >= 1) {
3329 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
3330 if (ret) {
3331 atomic_dec(&root->orphan_inodes);
3332 if (reserve) {
3333 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3334 &BTRFS_I(inode)->runtime_flags);
3335 btrfs_orphan_release_metadata(inode);
3336 }
3337 if (ret != -EEXIST) {
3338 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3339 &BTRFS_I(inode)->runtime_flags);
3340 btrfs_abort_transaction(trans, ret);
3341 return ret;
3342 }
3343 }
3344 ret = 0;
3345 }
3346
3347 /* insert an orphan item to track subvolume contains orphan files */
3348 if (insert >= 2) {
3349 ret = btrfs_insert_orphan_item(trans, fs_info->tree_root,
3350 root->root_key.objectid);
3351 if (ret && ret != -EEXIST) {
3352 btrfs_abort_transaction(trans, ret);
3353 return ret;
3354 }
3355 }
3356 return 0;
3357 }
3358
3359 /*
3360 * We have done the truncate/delete so we can go ahead and remove the orphan
3361 * item for this particular inode.
3362 */
3363 static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3364 struct inode *inode)
3365 {
3366 struct btrfs_root *root = BTRFS_I(inode)->root;
3367 int delete_item = 0;
3368 int release_rsv = 0;
3369 int ret = 0;
3370
3371 spin_lock(&root->orphan_lock);
3372 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3373 &BTRFS_I(inode)->runtime_flags))
3374 delete_item = 1;
3375
3376 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3377 &BTRFS_I(inode)->runtime_flags))
3378 release_rsv = 1;
3379 spin_unlock(&root->orphan_lock);
3380
3381 if (delete_item) {
3382 atomic_dec(&root->orphan_inodes);
3383 if (trans)
3384 ret = btrfs_del_orphan_item(trans, root,
3385 btrfs_ino(inode));
3386 }
3387
3388 if (release_rsv)
3389 btrfs_orphan_release_metadata(inode);
3390
3391 return ret;
3392 }
3393
3394 /*
3395 * this cleans up any orphans that may be left on the list from the last use
3396 * of this root.
3397 */
3398 int btrfs_orphan_cleanup(struct btrfs_root *root)
3399 {
3400 struct btrfs_fs_info *fs_info = root->fs_info;
3401 struct btrfs_path *path;
3402 struct extent_buffer *leaf;
3403 struct btrfs_key key, found_key;
3404 struct btrfs_trans_handle *trans;
3405 struct inode *inode;
3406 u64 last_objectid = 0;
3407 int ret = 0, nr_unlink = 0, nr_truncate = 0;
3408
3409 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
3410 return 0;
3411
3412 path = btrfs_alloc_path();
3413 if (!path) {
3414 ret = -ENOMEM;
3415 goto out;
3416 }
3417 path->reada = READA_BACK;
3418
3419 key.objectid = BTRFS_ORPHAN_OBJECTID;
3420 key.type = BTRFS_ORPHAN_ITEM_KEY;
3421 key.offset = (u64)-1;
3422
3423 while (1) {
3424 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3425 if (ret < 0)
3426 goto out;
3427
3428 /*
3429 * if ret == 0 means we found what we were searching for, which
3430 * is weird, but possible, so only screw with path if we didn't
3431 * find the key and see if we have stuff that matches
3432 */
3433 if (ret > 0) {
3434 ret = 0;
3435 if (path->slots[0] == 0)
3436 break;
3437 path->slots[0]--;
3438 }
3439
3440 /* pull out the item */
3441 leaf = path->nodes[0];
3442 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3443
3444 /* make sure the item matches what we want */
3445 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3446 break;
3447 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
3448 break;
3449
3450 /* release the path since we're done with it */
3451 btrfs_release_path(path);
3452
3453 /*
3454 * this is where we are basically btrfs_lookup, without the
3455 * crossing root thing. we store the inode number in the
3456 * offset of the orphan item.
3457 */
3458
3459 if (found_key.offset == last_objectid) {
3460 btrfs_err(fs_info,
3461 "Error removing orphan entry, stopping orphan cleanup");
3462 ret = -EINVAL;
3463 goto out;
3464 }
3465
3466 last_objectid = found_key.offset;
3467
3468 found_key.objectid = found_key.offset;
3469 found_key.type = BTRFS_INODE_ITEM_KEY;
3470 found_key.offset = 0;
3471 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
3472 ret = PTR_ERR_OR_ZERO(inode);
3473 if (ret && ret != -ENOENT)
3474 goto out;
3475
3476 if (ret == -ENOENT && root == fs_info->tree_root) {
3477 struct btrfs_root *dead_root;
3478 struct btrfs_fs_info *fs_info = root->fs_info;
3479 int is_dead_root = 0;
3480
3481 /*
3482 * this is an orphan in the tree root. Currently these
3483 * could come from 2 sources:
3484 * a) a snapshot deletion in progress
3485 * b) a free space cache inode
3486 * We need to distinguish those two, as the snapshot
3487 * orphan must not get deleted.
3488 * find_dead_roots already ran before us, so if this
3489 * is a snapshot deletion, we should find the root
3490 * in the dead_roots list
3491 */
3492 spin_lock(&fs_info->trans_lock);
3493 list_for_each_entry(dead_root, &fs_info->dead_roots,
3494 root_list) {
3495 if (dead_root->root_key.objectid ==
3496 found_key.objectid) {
3497 is_dead_root = 1;
3498 break;
3499 }
3500 }
3501 spin_unlock(&fs_info->trans_lock);
3502 if (is_dead_root) {
3503 /* prevent this orphan from being found again */
3504 key.offset = found_key.objectid - 1;
3505 continue;
3506 }
3507 }
3508 /*
3509 * Inode is already gone but the orphan item is still there,
3510 * kill the orphan item.
3511 */
3512 if (ret == -ENOENT) {
3513 trans = btrfs_start_transaction(root, 1);
3514 if (IS_ERR(trans)) {
3515 ret = PTR_ERR(trans);
3516 goto out;
3517 }
3518 btrfs_debug(fs_info, "auto deleting %Lu",
3519 found_key.objectid);
3520 ret = btrfs_del_orphan_item(trans, root,
3521 found_key.objectid);
3522 btrfs_end_transaction(trans);
3523 if (ret)
3524 goto out;
3525 continue;
3526 }
3527
3528 /*
3529 * add this inode to the orphan list so btrfs_orphan_del does
3530 * the proper thing when we hit it
3531 */
3532 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3533 &BTRFS_I(inode)->runtime_flags);
3534 atomic_inc(&root->orphan_inodes);
3535
3536 /* if we have links, this was a truncate, lets do that */
3537 if (inode->i_nlink) {
3538 if (WARN_ON(!S_ISREG(inode->i_mode))) {
3539 iput(inode);
3540 continue;
3541 }
3542 nr_truncate++;
3543
3544 /* 1 for the orphan item deletion. */
3545 trans = btrfs_start_transaction(root, 1);
3546 if (IS_ERR(trans)) {
3547 iput(inode);
3548 ret = PTR_ERR(trans);
3549 goto out;
3550 }
3551 ret = btrfs_orphan_add(trans, inode);
3552 btrfs_end_transaction(trans);
3553 if (ret) {
3554 iput(inode);
3555 goto out;
3556 }
3557
3558 ret = btrfs_truncate(inode);
3559 if (ret)
3560 btrfs_orphan_del(NULL, inode);
3561 } else {
3562 nr_unlink++;
3563 }
3564
3565 /* this will do delete_inode and everything for us */
3566 iput(inode);
3567 if (ret)
3568 goto out;
3569 }
3570 /* release the path since we're done with it */
3571 btrfs_release_path(path);
3572
3573 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3574
3575 if (root->orphan_block_rsv)
3576 btrfs_block_rsv_release(fs_info, root->orphan_block_rsv,
3577 (u64)-1);
3578
3579 if (root->orphan_block_rsv ||
3580 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
3581 trans = btrfs_join_transaction(root);
3582 if (!IS_ERR(trans))
3583 btrfs_end_transaction(trans);
3584 }
3585
3586 if (nr_unlink)
3587 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
3588 if (nr_truncate)
3589 btrfs_debug(fs_info, "truncated %d orphans", nr_truncate);
3590
3591 out:
3592 if (ret)
3593 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
3594 btrfs_free_path(path);
3595 return ret;
3596 }
3597
3598 /*
3599 * very simple check to peek ahead in the leaf looking for xattrs. If we
3600 * don't find any xattrs, we know there can't be any acls.
3601 *
3602 * slot is the slot the inode is in, objectid is the objectid of the inode
3603 */
3604 static noinline int acls_after_inode_item(struct extent_buffer *leaf,
3605 int slot, u64 objectid,
3606 int *first_xattr_slot)
3607 {
3608 u32 nritems = btrfs_header_nritems(leaf);
3609 struct btrfs_key found_key;
3610 static u64 xattr_access = 0;
3611 static u64 xattr_default = 0;
3612 int scanned = 0;
3613
3614 if (!xattr_access) {
3615 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3616 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3617 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3618 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
3619 }
3620
3621 slot++;
3622 *first_xattr_slot = -1;
3623 while (slot < nritems) {
3624 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3625
3626 /* we found a different objectid, there must not be acls */
3627 if (found_key.objectid != objectid)
3628 return 0;
3629
3630 /* we found an xattr, assume we've got an acl */
3631 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
3632 if (*first_xattr_slot == -1)
3633 *first_xattr_slot = slot;
3634 if (found_key.offset == xattr_access ||
3635 found_key.offset == xattr_default)
3636 return 1;
3637 }
3638
3639 /*
3640 * we found a key greater than an xattr key, there can't
3641 * be any acls later on
3642 */
3643 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3644 return 0;
3645
3646 slot++;
3647 scanned++;
3648
3649 /*
3650 * it goes inode, inode backrefs, xattrs, extents,
3651 * so if there are a ton of hard links to an inode there can
3652 * be a lot of backrefs. Don't waste time searching too hard,
3653 * this is just an optimization
3654 */
3655 if (scanned >= 8)
3656 break;
3657 }
3658 /* we hit the end of the leaf before we found an xattr or
3659 * something larger than an xattr. We have to assume the inode
3660 * has acls
3661 */
3662 if (*first_xattr_slot == -1)
3663 *first_xattr_slot = slot;
3664 return 1;
3665 }
3666
3667 /*
3668 * read an inode from the btree into the in-memory inode
3669 */
3670 static int btrfs_read_locked_inode(struct inode *inode)
3671 {
3672 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3673 struct btrfs_path *path;
3674 struct extent_buffer *leaf;
3675 struct btrfs_inode_item *inode_item;
3676 struct btrfs_root *root = BTRFS_I(inode)->root;
3677 struct btrfs_key location;
3678 unsigned long ptr;
3679 int maybe_acls;
3680 u32 rdev;
3681 int ret;
3682 bool filled = false;
3683 int first_xattr_slot;
3684
3685 ret = btrfs_fill_inode(inode, &rdev);
3686 if (!ret)
3687 filled = true;
3688
3689 path = btrfs_alloc_path();
3690 if (!path) {
3691 ret = -ENOMEM;
3692 goto make_bad;
3693 }
3694
3695 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
3696
3697 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
3698 if (ret) {
3699 if (ret > 0)
3700 ret = -ENOENT;
3701 goto make_bad;
3702 }
3703
3704 leaf = path->nodes[0];
3705
3706 if (filled)
3707 goto cache_index;
3708
3709 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3710 struct btrfs_inode_item);
3711 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
3712 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
3713 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3714 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
3715 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
3716
3717 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3718 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
3719
3720 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3721 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
3722
3723 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3724 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
3725
3726 BTRFS_I(inode)->i_otime.tv_sec =
3727 btrfs_timespec_sec(leaf, &inode_item->otime);
3728 BTRFS_I(inode)->i_otime.tv_nsec =
3729 btrfs_timespec_nsec(leaf, &inode_item->otime);
3730
3731 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
3732 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
3733 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3734
3735 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
3736 inode->i_generation = BTRFS_I(inode)->generation;
3737 inode->i_rdev = 0;
3738 rdev = btrfs_inode_rdev(leaf, inode_item);
3739
3740 BTRFS_I(inode)->index_cnt = (u64)-1;
3741 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
3742
3743 cache_index:
3744 /*
3745 * If we were modified in the current generation and evicted from memory
3746 * and then re-read we need to do a full sync since we don't have any
3747 * idea about which extents were modified before we were evicted from
3748 * cache.
3749 *
3750 * This is required for both inode re-read from disk and delayed inode
3751 * in delayed_nodes_tree.
3752 */
3753 if (BTRFS_I(inode)->last_trans == fs_info->generation)
3754 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3755 &BTRFS_I(inode)->runtime_flags);
3756
3757 /*
3758 * We don't persist the id of the transaction where an unlink operation
3759 * against the inode was last made. So here we assume the inode might
3760 * have been evicted, and therefore the exact value of last_unlink_trans
3761 * lost, and set it to last_trans to avoid metadata inconsistencies
3762 * between the inode and its parent if the inode is fsync'ed and the log
3763 * replayed. For example, in the scenario:
3764 *
3765 * touch mydir/foo
3766 * ln mydir/foo mydir/bar
3767 * sync
3768 * unlink mydir/bar
3769 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3770 * xfs_io -c fsync mydir/foo
3771 * <power failure>
3772 * mount fs, triggers fsync log replay
3773 *
3774 * We must make sure that when we fsync our inode foo we also log its
3775 * parent inode, otherwise after log replay the parent still has the
3776 * dentry with the "bar" name but our inode foo has a link count of 1
3777 * and doesn't have an inode ref with the name "bar" anymore.
3778 *
3779 * Setting last_unlink_trans to last_trans is a pessimistic approach,
3780 * but it guarantees correctness at the expense of occasional full
3781 * transaction commits on fsync if our inode is a directory, or if our
3782 * inode is not a directory, logging its parent unnecessarily.
3783 */
3784 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3785
3786 path->slots[0]++;
3787 if (inode->i_nlink != 1 ||
3788 path->slots[0] >= btrfs_header_nritems(leaf))
3789 goto cache_acl;
3790
3791 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
3792 if (location.objectid != btrfs_ino(inode))
3793 goto cache_acl;
3794
3795 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3796 if (location.type == BTRFS_INODE_REF_KEY) {
3797 struct btrfs_inode_ref *ref;
3798
3799 ref = (struct btrfs_inode_ref *)ptr;
3800 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3801 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3802 struct btrfs_inode_extref *extref;
3803
3804 extref = (struct btrfs_inode_extref *)ptr;
3805 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3806 extref);
3807 }
3808 cache_acl:
3809 /*
3810 * try to precache a NULL acl entry for files that don't have
3811 * any xattrs or acls
3812 */
3813 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
3814 btrfs_ino(inode), &first_xattr_slot);
3815 if (first_xattr_slot != -1) {
3816 path->slots[0] = first_xattr_slot;
3817 ret = btrfs_load_inode_props(inode, path);
3818 if (ret)
3819 btrfs_err(fs_info,
3820 "error loading props for ino %llu (root %llu): %d",
3821 btrfs_ino(inode),
3822 root->root_key.objectid, ret);
3823 }
3824 btrfs_free_path(path);
3825
3826 if (!maybe_acls)
3827 cache_no_acl(inode);
3828
3829 switch (inode->i_mode & S_IFMT) {
3830 case S_IFREG:
3831 inode->i_mapping->a_ops = &btrfs_aops;
3832 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
3833 inode->i_fop = &btrfs_file_operations;
3834 inode->i_op = &btrfs_file_inode_operations;
3835 break;
3836 case S_IFDIR:
3837 inode->i_fop = &btrfs_dir_file_operations;
3838 inode->i_op = &btrfs_dir_inode_operations;
3839 break;
3840 case S_IFLNK:
3841 inode->i_op = &btrfs_symlink_inode_operations;
3842 inode_nohighmem(inode);
3843 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3844 break;
3845 default:
3846 inode->i_op = &btrfs_special_inode_operations;
3847 init_special_inode(inode, inode->i_mode, rdev);
3848 break;
3849 }
3850
3851 btrfs_update_iflags(inode);
3852 return 0;
3853
3854 make_bad:
3855 btrfs_free_path(path);
3856 make_bad_inode(inode);
3857 return ret;
3858 }
3859
3860 /*
3861 * given a leaf and an inode, copy the inode fields into the leaf
3862 */
3863 static void fill_inode_item(struct btrfs_trans_handle *trans,
3864 struct extent_buffer *leaf,
3865 struct btrfs_inode_item *item,
3866 struct inode *inode)
3867 {
3868 struct btrfs_map_token token;
3869
3870 btrfs_init_map_token(&token);
3871
3872 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3873 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3874 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3875 &token);
3876 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3877 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
3878
3879 btrfs_set_token_timespec_sec(leaf, &item->atime,
3880 inode->i_atime.tv_sec, &token);
3881 btrfs_set_token_timespec_nsec(leaf, &item->atime,
3882 inode->i_atime.tv_nsec, &token);
3883
3884 btrfs_set_token_timespec_sec(leaf, &item->mtime,
3885 inode->i_mtime.tv_sec, &token);
3886 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
3887 inode->i_mtime.tv_nsec, &token);
3888
3889 btrfs_set_token_timespec_sec(leaf, &item->ctime,
3890 inode->i_ctime.tv_sec, &token);
3891 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
3892 inode->i_ctime.tv_nsec, &token);
3893
3894 btrfs_set_token_timespec_sec(leaf, &item->otime,
3895 BTRFS_I(inode)->i_otime.tv_sec, &token);
3896 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3897 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3898
3899 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3900 &token);
3901 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3902 &token);
3903 btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3904 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3905 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3906 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3907 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
3908 }
3909
3910 /*
3911 * copy everything in the in-memory inode into the btree.
3912 */
3913 static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
3914 struct btrfs_root *root, struct inode *inode)
3915 {
3916 struct btrfs_inode_item *inode_item;
3917 struct btrfs_path *path;
3918 struct extent_buffer *leaf;
3919 int ret;
3920
3921 path = btrfs_alloc_path();
3922 if (!path)
3923 return -ENOMEM;
3924
3925 path->leave_spinning = 1;
3926 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3927 1);
3928 if (ret) {
3929 if (ret > 0)
3930 ret = -ENOENT;
3931 goto failed;
3932 }
3933
3934 leaf = path->nodes[0];
3935 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3936 struct btrfs_inode_item);
3937
3938 fill_inode_item(trans, leaf, inode_item, inode);
3939 btrfs_mark_buffer_dirty(leaf);
3940 btrfs_set_inode_last_trans(trans, inode);
3941 ret = 0;
3942 failed:
3943 btrfs_free_path(path);
3944 return ret;
3945 }
3946
3947 /*
3948 * copy everything in the in-memory inode into the btree.
3949 */
3950 noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3951 struct btrfs_root *root, struct inode *inode)
3952 {
3953 struct btrfs_fs_info *fs_info = root->fs_info;
3954 int ret;
3955
3956 /*
3957 * If the inode is a free space inode, we can deadlock during commit
3958 * if we put it into the delayed code.
3959 *
3960 * The data relocation inode should also be directly updated
3961 * without delay
3962 */
3963 if (!btrfs_is_free_space_inode(inode)
3964 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
3965 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
3966 btrfs_update_root_times(trans, root);
3967
3968 ret = btrfs_delayed_update_inode(trans, root, inode);
3969 if (!ret)
3970 btrfs_set_inode_last_trans(trans, inode);
3971 return ret;
3972 }
3973
3974 return btrfs_update_inode_item(trans, root, inode);
3975 }
3976
3977 noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3978 struct btrfs_root *root,
3979 struct inode *inode)
3980 {
3981 int ret;
3982
3983 ret = btrfs_update_inode(trans, root, inode);
3984 if (ret == -ENOSPC)
3985 return btrfs_update_inode_item(trans, root, inode);
3986 return ret;
3987 }
3988
3989 /*
3990 * unlink helper that gets used here in inode.c and in the tree logging
3991 * recovery code. It remove a link in a directory with a given name, and
3992 * also drops the back refs in the inode to the directory
3993 */
3994 static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3995 struct btrfs_root *root,
3996 struct inode *dir, struct inode *inode,
3997 const char *name, int name_len)
3998 {
3999 struct btrfs_fs_info *fs_info = root->fs_info;
4000 struct btrfs_path *path;
4001 int ret = 0;
4002 struct extent_buffer *leaf;
4003 struct btrfs_dir_item *di;
4004 struct btrfs_key key;
4005 u64 index;
4006 u64 ino = btrfs_ino(inode);
4007 u64 dir_ino = btrfs_ino(dir);
4008
4009 path = btrfs_alloc_path();
4010 if (!path) {
4011 ret = -ENOMEM;
4012 goto out;
4013 }
4014
4015 path->leave_spinning = 1;
4016 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
4017 name, name_len, -1);
4018 if (IS_ERR(di)) {
4019 ret = PTR_ERR(di);
4020 goto err;
4021 }
4022 if (!di) {
4023 ret = -ENOENT;
4024 goto err;
4025 }
4026 leaf = path->nodes[0];
4027 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4028 ret = btrfs_delete_one_dir_name(trans, root, path, di);
4029 if (ret)
4030 goto err;
4031 btrfs_release_path(path);
4032
4033 /*
4034 * If we don't have dir index, we have to get it by looking up
4035 * the inode ref, since we get the inode ref, remove it directly,
4036 * it is unnecessary to do delayed deletion.
4037 *
4038 * But if we have dir index, needn't search inode ref to get it.
4039 * Since the inode ref is close to the inode item, it is better
4040 * that we delay to delete it, and just do this deletion when
4041 * we update the inode item.
4042 */
4043 if (BTRFS_I(inode)->dir_index) {
4044 ret = btrfs_delayed_delete_inode_ref(inode);
4045 if (!ret) {
4046 index = BTRFS_I(inode)->dir_index;
4047 goto skip_backref;
4048 }
4049 }
4050
4051 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4052 dir_ino, &index);
4053 if (ret) {
4054 btrfs_info(fs_info,
4055 "failed to delete reference to %.*s, inode %llu parent %llu",
4056 name_len, name, ino, dir_ino);
4057 btrfs_abort_transaction(trans, ret);
4058 goto err;
4059 }
4060 skip_backref:
4061 ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
4062 if (ret) {
4063 btrfs_abort_transaction(trans, ret);
4064 goto err;
4065 }
4066
4067 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
4068 inode, dir_ino);
4069 if (ret != 0 && ret != -ENOENT) {
4070 btrfs_abort_transaction(trans, ret);
4071 goto err;
4072 }
4073
4074 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
4075 dir, index);
4076 if (ret == -ENOENT)
4077 ret = 0;
4078 else if (ret)
4079 btrfs_abort_transaction(trans, ret);
4080 err:
4081 btrfs_free_path(path);
4082 if (ret)
4083 goto out;
4084
4085 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
4086 inode_inc_iversion(inode);
4087 inode_inc_iversion(dir);
4088 inode->i_ctime = dir->i_mtime =
4089 dir->i_ctime = current_time(inode);
4090 ret = btrfs_update_inode(trans, root, dir);
4091 out:
4092 return ret;
4093 }
4094
4095 int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4096 struct btrfs_root *root,
4097 struct inode *dir, struct inode *inode,
4098 const char *name, int name_len)
4099 {
4100 int ret;
4101 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4102 if (!ret) {
4103 drop_nlink(inode);
4104 ret = btrfs_update_inode(trans, root, inode);
4105 }
4106 return ret;
4107 }
4108
4109 /*
4110 * helper to start transaction for unlink and rmdir.
4111 *
4112 * unlink and rmdir are special in btrfs, they do not always free space, so
4113 * if we cannot make our reservations the normal way try and see if there is
4114 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4115 * allow the unlink to occur.
4116 */
4117 static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
4118 {
4119 struct btrfs_root *root = BTRFS_I(dir)->root;
4120
4121 /*
4122 * 1 for the possible orphan item
4123 * 1 for the dir item
4124 * 1 for the dir index
4125 * 1 for the inode ref
4126 * 1 for the inode
4127 */
4128 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
4129 }
4130
4131 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4132 {
4133 struct btrfs_root *root = BTRFS_I(dir)->root;
4134 struct btrfs_trans_handle *trans;
4135 struct inode *inode = d_inode(dentry);
4136 int ret;
4137
4138 trans = __unlink_start_trans(dir);
4139 if (IS_ERR(trans))
4140 return PTR_ERR(trans);
4141
4142 btrfs_record_unlink_dir(trans, dir, d_inode(dentry), 0);
4143
4144 ret = btrfs_unlink_inode(trans, root, dir, d_inode(dentry),
4145 dentry->d_name.name, dentry->d_name.len);
4146 if (ret)
4147 goto out;
4148
4149 if (inode->i_nlink == 0) {
4150 ret = btrfs_orphan_add(trans, inode);
4151 if (ret)
4152 goto out;
4153 }
4154
4155 out:
4156 btrfs_end_transaction(trans);
4157 btrfs_btree_balance_dirty(root->fs_info);
4158 return ret;
4159 }
4160
4161 int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4162 struct btrfs_root *root,
4163 struct inode *dir, u64 objectid,
4164 const char *name, int name_len)
4165 {
4166 struct btrfs_fs_info *fs_info = root->fs_info;
4167 struct btrfs_path *path;
4168 struct extent_buffer *leaf;
4169 struct btrfs_dir_item *di;
4170 struct btrfs_key key;
4171 u64 index;
4172 int ret;
4173 u64 dir_ino = btrfs_ino(dir);
4174
4175 path = btrfs_alloc_path();
4176 if (!path)
4177 return -ENOMEM;
4178
4179 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
4180 name, name_len, -1);
4181 if (IS_ERR_OR_NULL(di)) {
4182 if (!di)
4183 ret = -ENOENT;
4184 else
4185 ret = PTR_ERR(di);
4186 goto out;
4187 }
4188
4189 leaf = path->nodes[0];
4190 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4191 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4192 ret = btrfs_delete_one_dir_name(trans, root, path, di);
4193 if (ret) {
4194 btrfs_abort_transaction(trans, ret);
4195 goto out;
4196 }
4197 btrfs_release_path(path);
4198
4199 ret = btrfs_del_root_ref(trans, fs_info, objectid,
4200 root->root_key.objectid, dir_ino,
4201 &index, name, name_len);
4202 if (ret < 0) {
4203 if (ret != -ENOENT) {
4204 btrfs_abort_transaction(trans, ret);
4205 goto out;
4206 }
4207 di = btrfs_search_dir_index_item(root, path, dir_ino,
4208 name, name_len);
4209 if (IS_ERR_OR_NULL(di)) {
4210 if (!di)
4211 ret = -ENOENT;
4212 else
4213 ret = PTR_ERR(di);
4214 btrfs_abort_transaction(trans, ret);
4215 goto out;
4216 }
4217
4218 leaf = path->nodes[0];
4219 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4220 btrfs_release_path(path);
4221 index = key.offset;
4222 }
4223 btrfs_release_path(path);
4224
4225 ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
4226 if (ret) {
4227 btrfs_abort_transaction(trans, ret);
4228 goto out;
4229 }
4230
4231 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
4232 inode_inc_iversion(dir);
4233 dir->i_mtime = dir->i_ctime = current_time(dir);
4234 ret = btrfs_update_inode_fallback(trans, root, dir);
4235 if (ret)
4236 btrfs_abort_transaction(trans, ret);
4237 out:
4238 btrfs_free_path(path);
4239 return ret;
4240 }
4241
4242 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4243 {
4244 struct inode *inode = d_inode(dentry);
4245 int err = 0;
4246 struct btrfs_root *root = BTRFS_I(dir)->root;
4247 struct btrfs_trans_handle *trans;
4248 u64 last_unlink_trans;
4249
4250 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
4251 return -ENOTEMPTY;
4252 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
4253 return -EPERM;
4254
4255 trans = __unlink_start_trans(dir);
4256 if (IS_ERR(trans))
4257 return PTR_ERR(trans);
4258
4259 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
4260 err = btrfs_unlink_subvol(trans, root, dir,
4261 BTRFS_I(inode)->location.objectid,
4262 dentry->d_name.name,
4263 dentry->d_name.len);
4264 goto out;
4265 }
4266
4267 err = btrfs_orphan_add(trans, inode);
4268 if (err)
4269 goto out;
4270
4271 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4272
4273 /* now the directory is empty */
4274 err = btrfs_unlink_inode(trans, root, dir, d_inode(dentry),
4275 dentry->d_name.name, dentry->d_name.len);
4276 if (!err) {
4277 btrfs_i_size_write(inode, 0);
4278 /*
4279 * Propagate the last_unlink_trans value of the deleted dir to
4280 * its parent directory. This is to prevent an unrecoverable
4281 * log tree in the case we do something like this:
4282 * 1) create dir foo
4283 * 2) create snapshot under dir foo
4284 * 3) delete the snapshot
4285 * 4) rmdir foo
4286 * 5) mkdir foo
4287 * 6) fsync foo or some file inside foo
4288 */
4289 if (last_unlink_trans >= trans->transid)
4290 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4291 }
4292 out:
4293 btrfs_end_transaction(trans);
4294 btrfs_btree_balance_dirty(root->fs_info);
4295
4296 return err;
4297 }
4298
4299 static int truncate_space_check(struct btrfs_trans_handle *trans,
4300 struct btrfs_root *root,
4301 u64 bytes_deleted)
4302 {
4303 struct btrfs_fs_info *fs_info = root->fs_info;
4304 int ret;
4305
4306 /*
4307 * This is only used to apply pressure to the enospc system, we don't
4308 * intend to use this reservation at all.
4309 */
4310 bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
4311 bytes_deleted *= fs_info->nodesize;
4312 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
4313 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
4314 if (!ret) {
4315 trace_btrfs_space_reservation(fs_info, "transaction",
4316 trans->transid,
4317 bytes_deleted, 1);
4318 trans->bytes_reserved += bytes_deleted;
4319 }
4320 return ret;
4321
4322 }
4323
4324 static int truncate_inline_extent(struct inode *inode,
4325 struct btrfs_path *path,
4326 struct btrfs_key *found_key,
4327 const u64 item_end,
4328 const u64 new_size)
4329 {
4330 struct extent_buffer *leaf = path->nodes[0];
4331 int slot = path->slots[0];
4332 struct btrfs_file_extent_item *fi;
4333 u32 size = (u32)(new_size - found_key->offset);
4334 struct btrfs_root *root = BTRFS_I(inode)->root;
4335
4336 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
4337
4338 if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) {
4339 loff_t offset = new_size;
4340 loff_t page_end = ALIGN(offset, PAGE_SIZE);
4341
4342 /*
4343 * Zero out the remaining of the last page of our inline extent,
4344 * instead of directly truncating our inline extent here - that
4345 * would be much more complex (decompressing all the data, then
4346 * compressing the truncated data, which might be bigger than
4347 * the size of the inline extent, resize the extent, etc).
4348 * We release the path because to get the page we might need to
4349 * read the extent item from disk (data not in the page cache).
4350 */
4351 btrfs_release_path(path);
4352 return btrfs_truncate_block(inode, offset, page_end - offset,
4353 0);
4354 }
4355
4356 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4357 size = btrfs_file_extent_calc_inline_size(size);
4358 btrfs_truncate_item(root->fs_info, path, size, 1);
4359
4360 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4361 inode_sub_bytes(inode, item_end + 1 - new_size);
4362
4363 return 0;
4364 }
4365
4366 /*
4367 * this can truncate away extent items, csum items and directory items.
4368 * It starts at a high offset and removes keys until it can't find
4369 * any higher than new_size
4370 *
4371 * csum items that cross the new i_size are truncated to the new size
4372 * as well.
4373 *
4374 * min_type is the minimum key type to truncate down to. If set to 0, this
4375 * will kill all the items on this inode, including the INODE_ITEM_KEY.
4376 */
4377 int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4378 struct btrfs_root *root,
4379 struct inode *inode,
4380 u64 new_size, u32 min_type)
4381 {
4382 struct btrfs_fs_info *fs_info = root->fs_info;
4383 struct btrfs_path *path;
4384 struct extent_buffer *leaf;
4385 struct btrfs_file_extent_item *fi;
4386 struct btrfs_key key;
4387 struct btrfs_key found_key;
4388 u64 extent_start = 0;
4389 u64 extent_num_bytes = 0;
4390 u64 extent_offset = 0;
4391 u64 item_end = 0;
4392 u64 last_size = new_size;
4393 u32 found_type = (u8)-1;
4394 int found_extent;
4395 int del_item;
4396 int pending_del_nr = 0;
4397 int pending_del_slot = 0;
4398 int extent_type = -1;
4399 int ret;
4400 int err = 0;
4401 u64 ino = btrfs_ino(inode);
4402 u64 bytes_deleted = 0;
4403 bool be_nice = 0;
4404 bool should_throttle = 0;
4405 bool should_end = 0;
4406
4407 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
4408
4409 /*
4410 * for non-free space inodes and ref cows, we want to back off from
4411 * time to time
4412 */
4413 if (!btrfs_is_free_space_inode(inode) &&
4414 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4415 be_nice = 1;
4416
4417 path = btrfs_alloc_path();
4418 if (!path)
4419 return -ENOMEM;
4420 path->reada = READA_BACK;
4421
4422 /*
4423 * We want to drop from the next block forward in case this new size is
4424 * not block aligned since we will be keeping the last block of the
4425 * extent just the way it is.
4426 */
4427 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4428 root == fs_info->tree_root)
4429 btrfs_drop_extent_cache(inode, ALIGN(new_size,
4430 fs_info->sectorsize),
4431 (u64)-1, 0);
4432
4433 /*
4434 * This function is also used to drop the items in the log tree before
4435 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4436 * it is used to drop the loged items. So we shouldn't kill the delayed
4437 * items.
4438 */
4439 if (min_type == 0 && root == BTRFS_I(inode)->root)
4440 btrfs_kill_delayed_inode_items(inode);
4441
4442 key.objectid = ino;
4443 key.offset = (u64)-1;
4444 key.type = (u8)-1;
4445
4446 search_again:
4447 /*
4448 * with a 16K leaf size and 128MB extents, you can actually queue
4449 * up a huge file in a single leaf. Most of the time that
4450 * bytes_deleted is > 0, it will be huge by the time we get here
4451 */
4452 if (be_nice && bytes_deleted > SZ_32M) {
4453 if (btrfs_should_end_transaction(trans)) {
4454 err = -EAGAIN;
4455 goto error;
4456 }
4457 }
4458
4459
4460 path->leave_spinning = 1;
4461 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4462 if (ret < 0) {
4463 err = ret;
4464 goto out;
4465 }
4466
4467 if (ret > 0) {
4468 /* there are no items in the tree for us to truncate, we're
4469 * done
4470 */
4471 if (path->slots[0] == 0)
4472 goto out;
4473 path->slots[0]--;
4474 }
4475
4476 while (1) {
4477 fi = NULL;
4478 leaf = path->nodes[0];
4479 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4480 found_type = found_key.type;
4481
4482 if (found_key.objectid != ino)
4483 break;
4484
4485 if (found_type < min_type)
4486 break;
4487
4488 item_end = found_key.offset;
4489 if (found_type == BTRFS_EXTENT_DATA_KEY) {
4490 fi = btrfs_item_ptr(leaf, path->slots[0],
4491 struct btrfs_file_extent_item);
4492 extent_type = btrfs_file_extent_type(leaf, fi);
4493 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
4494 item_end +=
4495 btrfs_file_extent_num_bytes(leaf, fi);
4496 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
4497 item_end += btrfs_file_extent_inline_len(leaf,
4498 path->slots[0], fi);
4499 }
4500 item_end--;
4501 }
4502 if (found_type > min_type) {
4503 del_item = 1;
4504 } else {
4505 if (item_end < new_size)
4506 break;
4507 if (found_key.offset >= new_size)
4508 del_item = 1;
4509 else
4510 del_item = 0;
4511 }
4512 found_extent = 0;
4513 /* FIXME, shrink the extent if the ref count is only 1 */
4514 if (found_type != BTRFS_EXTENT_DATA_KEY)
4515 goto delete;
4516
4517 if (del_item)
4518 last_size = found_key.offset;
4519 else
4520 last_size = new_size;
4521
4522 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
4523 u64 num_dec;
4524 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
4525 if (!del_item) {
4526 u64 orig_num_bytes =
4527 btrfs_file_extent_num_bytes(leaf, fi);
4528 extent_num_bytes = ALIGN(new_size -
4529 found_key.offset,
4530 fs_info->sectorsize);
4531 btrfs_set_file_extent_num_bytes(leaf, fi,
4532 extent_num_bytes);
4533 num_dec = (orig_num_bytes -
4534 extent_num_bytes);
4535 if (test_bit(BTRFS_ROOT_REF_COWS,
4536 &root->state) &&
4537 extent_start != 0)
4538 inode_sub_bytes(inode, num_dec);
4539 btrfs_mark_buffer_dirty(leaf);
4540 } else {
4541 extent_num_bytes =
4542 btrfs_file_extent_disk_num_bytes(leaf,
4543 fi);
4544 extent_offset = found_key.offset -
4545 btrfs_file_extent_offset(leaf, fi);
4546
4547 /* FIXME blocksize != 4096 */
4548 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
4549 if (extent_start != 0) {
4550 found_extent = 1;
4551 if (test_bit(BTRFS_ROOT_REF_COWS,
4552 &root->state))
4553 inode_sub_bytes(inode, num_dec);
4554 }
4555 }
4556 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
4557 /*
4558 * we can't truncate inline items that have had
4559 * special encodings
4560 */
4561 if (!del_item &&
4562 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4563 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
4564
4565 /*
4566 * Need to release path in order to truncate a
4567 * compressed extent. So delete any accumulated
4568 * extent items so far.
4569 */
4570 if (btrfs_file_extent_compression(leaf, fi) !=
4571 BTRFS_COMPRESS_NONE && pending_del_nr) {
4572 err = btrfs_del_items(trans, root, path,
4573 pending_del_slot,
4574 pending_del_nr);
4575 if (err) {
4576 btrfs_abort_transaction(trans,
4577 err);
4578 goto error;
4579 }
4580 pending_del_nr = 0;
4581 }
4582
4583 err = truncate_inline_extent(inode, path,
4584 &found_key,
4585 item_end,
4586 new_size);
4587 if (err) {
4588 btrfs_abort_transaction(trans, err);
4589 goto error;
4590 }
4591 } else if (test_bit(BTRFS_ROOT_REF_COWS,
4592 &root->state)) {
4593 inode_sub_bytes(inode, item_end + 1 - new_size);
4594 }
4595 }
4596 delete:
4597 if (del_item) {
4598 if (!pending_del_nr) {
4599 /* no pending yet, add ourselves */
4600 pending_del_slot = path->slots[0];
4601 pending_del_nr = 1;
4602 } else if (pending_del_nr &&
4603 path->slots[0] + 1 == pending_del_slot) {
4604 /* hop on the pending chunk */
4605 pending_del_nr++;
4606 pending_del_slot = path->slots[0];
4607 } else {
4608 BUG();
4609 }
4610 } else {
4611 break;
4612 }
4613 should_throttle = 0;
4614
4615 if (found_extent &&
4616 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4617 root == fs_info->tree_root)) {
4618 btrfs_set_path_blocking(path);
4619 bytes_deleted += extent_num_bytes;
4620 ret = btrfs_free_extent(trans, fs_info, extent_start,
4621 extent_num_bytes, 0,
4622 btrfs_header_owner(leaf),
4623 ino, extent_offset);
4624 BUG_ON(ret);
4625 if (btrfs_should_throttle_delayed_refs(trans, fs_info))
4626 btrfs_async_run_delayed_refs(fs_info,
4627 trans->delayed_ref_updates * 2,
4628 trans->transid, 0);
4629 if (be_nice) {
4630 if (truncate_space_check(trans, root,
4631 extent_num_bytes)) {
4632 should_end = 1;
4633 }
4634 if (btrfs_should_throttle_delayed_refs(trans,
4635 fs_info))
4636 should_throttle = 1;
4637 }
4638 }
4639
4640 if (found_type == BTRFS_INODE_ITEM_KEY)
4641 break;
4642
4643 if (path->slots[0] == 0 ||
4644 path->slots[0] != pending_del_slot ||
4645 should_throttle || should_end) {
4646 if (pending_del_nr) {
4647 ret = btrfs_del_items(trans, root, path,
4648 pending_del_slot,
4649 pending_del_nr);
4650 if (ret) {
4651 btrfs_abort_transaction(trans, ret);
4652 goto error;
4653 }
4654 pending_del_nr = 0;
4655 }
4656 btrfs_release_path(path);
4657 if (should_throttle) {
4658 unsigned long updates = trans->delayed_ref_updates;
4659 if (updates) {
4660 trans->delayed_ref_updates = 0;
4661 ret = btrfs_run_delayed_refs(trans,
4662 fs_info,
4663 updates * 2);
4664 if (ret && !err)
4665 err = ret;
4666 }
4667 }
4668 /*
4669 * if we failed to refill our space rsv, bail out
4670 * and let the transaction restart
4671 */
4672 if (should_end) {
4673 err = -EAGAIN;
4674 goto error;
4675 }
4676 goto search_again;
4677 } else {
4678 path->slots[0]--;
4679 }
4680 }
4681 out:
4682 if (pending_del_nr) {
4683 ret = btrfs_del_items(trans, root, path, pending_del_slot,
4684 pending_del_nr);
4685 if (ret)
4686 btrfs_abort_transaction(trans, ret);
4687 }
4688 error:
4689 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4690 ASSERT(last_size >= new_size);
4691 if (!err && last_size > new_size)
4692 last_size = new_size;
4693 btrfs_ordered_update_i_size(inode, last_size, NULL);
4694 }
4695
4696 btrfs_free_path(path);
4697
4698 if (be_nice && bytes_deleted > SZ_32M) {
4699 unsigned long updates = trans->delayed_ref_updates;
4700 if (updates) {
4701 trans->delayed_ref_updates = 0;
4702 ret = btrfs_run_delayed_refs(trans, fs_info,
4703 updates * 2);
4704 if (ret && !err)
4705 err = ret;
4706 }
4707 }
4708 return err;
4709 }
4710
4711 /*
4712 * btrfs_truncate_block - read, zero a chunk and write a block
4713 * @inode - inode that we're zeroing
4714 * @from - the offset to start zeroing
4715 * @len - the length to zero, 0 to zero the entire range respective to the
4716 * offset
4717 * @front - zero up to the offset instead of from the offset on
4718 *
4719 * This will find the block for the "from" offset and cow the block and zero the
4720 * part we want to zero. This is used with truncate and hole punching.
4721 */
4722 int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
4723 int front)
4724 {
4725 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4726 struct address_space *mapping = inode->i_mapping;
4727 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4728 struct btrfs_ordered_extent *ordered;
4729 struct extent_state *cached_state = NULL;
4730 char *kaddr;
4731 u32 blocksize = fs_info->sectorsize;
4732 pgoff_t index = from >> PAGE_SHIFT;
4733 unsigned offset = from & (blocksize - 1);
4734 struct page *page;
4735 gfp_t mask = btrfs_alloc_write_mask(mapping);
4736 int ret = 0;
4737 u64 block_start;
4738 u64 block_end;
4739
4740 if ((offset & (blocksize - 1)) == 0 &&
4741 (!len || ((len & (blocksize - 1)) == 0)))
4742 goto out;
4743
4744 ret = btrfs_delalloc_reserve_space(inode,
4745 round_down(from, blocksize), blocksize);
4746 if (ret)
4747 goto out;
4748
4749 again:
4750 page = find_or_create_page(mapping, index, mask);
4751 if (!page) {
4752 btrfs_delalloc_release_space(inode,
4753 round_down(from, blocksize),
4754 blocksize);
4755 ret = -ENOMEM;
4756 goto out;
4757 }
4758
4759 block_start = round_down(from, blocksize);
4760 block_end = block_start + blocksize - 1;
4761
4762 if (!PageUptodate(page)) {
4763 ret = btrfs_readpage(NULL, page);
4764 lock_page(page);
4765 if (page->mapping != mapping) {
4766 unlock_page(page);
4767 put_page(page);
4768 goto again;
4769 }
4770 if (!PageUptodate(page)) {
4771 ret = -EIO;
4772 goto out_unlock;
4773 }
4774 }
4775 wait_on_page_writeback(page);
4776
4777 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
4778 set_page_extent_mapped(page);
4779
4780 ordered = btrfs_lookup_ordered_extent(inode, block_start);
4781 if (ordered) {
4782 unlock_extent_cached(io_tree, block_start, block_end,
4783 &cached_state, GFP_NOFS);
4784 unlock_page(page);
4785 put_page(page);
4786 btrfs_start_ordered_extent(inode, ordered, 1);
4787 btrfs_put_ordered_extent(ordered);
4788 goto again;
4789 }
4790
4791 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
4792 EXTENT_DIRTY | EXTENT_DELALLOC |
4793 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
4794 0, 0, &cached_state, GFP_NOFS);
4795
4796 ret = btrfs_set_extent_delalloc(inode, block_start, block_end,
4797 &cached_state, 0);
4798 if (ret) {
4799 unlock_extent_cached(io_tree, block_start, block_end,
4800 &cached_state, GFP_NOFS);
4801 goto out_unlock;
4802 }
4803
4804 if (offset != blocksize) {
4805 if (!len)
4806 len = blocksize - offset;
4807 kaddr = kmap(page);
4808 if (front)
4809 memset(kaddr + (block_start - page_offset(page)),
4810 0, offset);
4811 else
4812 memset(kaddr + (block_start - page_offset(page)) + offset,
4813 0, len);
4814 flush_dcache_page(page);
4815 kunmap(page);
4816 }
4817 ClearPageChecked(page);
4818 set_page_dirty(page);
4819 unlock_extent_cached(io_tree, block_start, block_end, &cached_state,
4820 GFP_NOFS);
4821
4822 out_unlock:
4823 if (ret)
4824 btrfs_delalloc_release_space(inode, block_start,
4825 blocksize);
4826 unlock_page(page);
4827 put_page(page);
4828 out:
4829 return ret;
4830 }
4831
4832 static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4833 u64 offset, u64 len)
4834 {
4835 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4836 struct btrfs_trans_handle *trans;
4837 int ret;
4838
4839 /*
4840 * Still need to make sure the inode looks like it's been updated so
4841 * that any holes get logged if we fsync.
4842 */
4843 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4844 BTRFS_I(inode)->last_trans = fs_info->generation;
4845 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4846 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4847 return 0;
4848 }
4849
4850 /*
4851 * 1 - for the one we're dropping
4852 * 1 - for the one we're adding
4853 * 1 - for updating the inode.
4854 */
4855 trans = btrfs_start_transaction(root, 3);
4856 if (IS_ERR(trans))
4857 return PTR_ERR(trans);
4858
4859 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4860 if (ret) {
4861 btrfs_abort_transaction(trans, ret);
4862 btrfs_end_transaction(trans);
4863 return ret;
4864 }
4865
4866 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
4867 0, 0, len, 0, len, 0, 0, 0);
4868 if (ret)
4869 btrfs_abort_transaction(trans, ret);
4870 else
4871 btrfs_update_inode(trans, root, inode);
4872 btrfs_end_transaction(trans);
4873 return ret;
4874 }
4875
4876 /*
4877 * This function puts in dummy file extents for the area we're creating a hole
4878 * for. So if we are truncating this file to a larger size we need to insert
4879 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4880 * the range between oldsize and size
4881 */
4882 int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
4883 {
4884 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4885 struct btrfs_root *root = BTRFS_I(inode)->root;
4886 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4887 struct extent_map *em = NULL;
4888 struct extent_state *cached_state = NULL;
4889 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
4890 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4891 u64 block_end = ALIGN(size, fs_info->sectorsize);
4892 u64 last_byte;
4893 u64 cur_offset;
4894 u64 hole_size;
4895 int err = 0;
4896
4897 /*
4898 * If our size started in the middle of a block we need to zero out the
4899 * rest of the block before we expand the i_size, otherwise we could
4900 * expose stale data.
4901 */
4902 err = btrfs_truncate_block(inode, oldsize, 0, 0);
4903 if (err)
4904 return err;
4905
4906 if (size <= hole_start)
4907 return 0;
4908
4909 while (1) {
4910 struct btrfs_ordered_extent *ordered;
4911
4912 lock_extent_bits(io_tree, hole_start, block_end - 1,
4913 &cached_state);
4914 ordered = btrfs_lookup_ordered_range(inode, hole_start,
4915 block_end - hole_start);
4916 if (!ordered)
4917 break;
4918 unlock_extent_cached(io_tree, hole_start, block_end - 1,
4919 &cached_state, GFP_NOFS);
4920 btrfs_start_ordered_extent(inode, ordered, 1);
4921 btrfs_put_ordered_extent(ordered);
4922 }
4923
4924 cur_offset = hole_start;
4925 while (1) {
4926 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4927 block_end - cur_offset, 0);
4928 if (IS_ERR(em)) {
4929 err = PTR_ERR(em);
4930 em = NULL;
4931 break;
4932 }
4933 last_byte = min(extent_map_end(em), block_end);
4934 last_byte = ALIGN(last_byte, fs_info->sectorsize);
4935 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
4936 struct extent_map *hole_em;
4937 hole_size = last_byte - cur_offset;
4938
4939 err = maybe_insert_hole(root, inode, cur_offset,
4940 hole_size);
4941 if (err)
4942 break;
4943 btrfs_drop_extent_cache(inode, cur_offset,
4944 cur_offset + hole_size - 1, 0);
4945 hole_em = alloc_extent_map();
4946 if (!hole_em) {
4947 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4948 &BTRFS_I(inode)->runtime_flags);
4949 goto next;
4950 }
4951 hole_em->start = cur_offset;
4952 hole_em->len = hole_size;
4953 hole_em->orig_start = cur_offset;
4954
4955 hole_em->block_start = EXTENT_MAP_HOLE;
4956 hole_em->block_len = 0;
4957 hole_em->orig_block_len = 0;
4958 hole_em->ram_bytes = hole_size;
4959 hole_em->bdev = fs_info->fs_devices->latest_bdev;
4960 hole_em->compress_type = BTRFS_COMPRESS_NONE;
4961 hole_em->generation = fs_info->generation;
4962
4963 while (1) {
4964 write_lock(&em_tree->lock);
4965 err = add_extent_mapping(em_tree, hole_em, 1);
4966 write_unlock(&em_tree->lock);
4967 if (err != -EEXIST)
4968 break;
4969 btrfs_drop_extent_cache(inode, cur_offset,
4970 cur_offset +
4971 hole_size - 1, 0);
4972 }
4973 free_extent_map(hole_em);
4974 }
4975 next:
4976 free_extent_map(em);
4977 em = NULL;
4978 cur_offset = last_byte;
4979 if (cur_offset >= block_end)
4980 break;
4981 }
4982 free_extent_map(em);
4983 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4984 GFP_NOFS);
4985 return err;
4986 }
4987
4988 static int btrfs_setsize(struct inode *inode, struct iattr *attr)
4989 {
4990 struct btrfs_root *root = BTRFS_I(inode)->root;
4991 struct btrfs_trans_handle *trans;
4992 loff_t oldsize = i_size_read(inode);
4993 loff_t newsize = attr->ia_size;
4994 int mask = attr->ia_valid;
4995 int ret;
4996
4997 /*
4998 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4999 * special case where we need to update the times despite not having
5000 * these flags set. For all other operations the VFS set these flags
5001 * explicitly if it wants a timestamp update.
5002 */
5003 if (newsize != oldsize) {
5004 inode_inc_iversion(inode);
5005 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5006 inode->i_ctime = inode->i_mtime =
5007 current_time(inode);
5008 }
5009
5010 if (newsize > oldsize) {
5011 /*
5012 * Don't do an expanding truncate while snapshoting is ongoing.
5013 * This is to ensure the snapshot captures a fully consistent
5014 * state of this file - if the snapshot captures this expanding
5015 * truncation, it must capture all writes that happened before
5016 * this truncation.
5017 */
5018 btrfs_wait_for_snapshot_creation(root);
5019 ret = btrfs_cont_expand(inode, oldsize, newsize);
5020 if (ret) {
5021 btrfs_end_write_no_snapshoting(root);
5022 return ret;
5023 }
5024
5025 trans = btrfs_start_transaction(root, 1);
5026 if (IS_ERR(trans)) {
5027 btrfs_end_write_no_snapshoting(root);
5028 return PTR_ERR(trans);
5029 }
5030
5031 i_size_write(inode, newsize);
5032 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
5033 pagecache_isize_extended(inode, oldsize, newsize);
5034 ret = btrfs_update_inode(trans, root, inode);
5035 btrfs_end_write_no_snapshoting(root);
5036 btrfs_end_transaction(trans);
5037 } else {
5038
5039 /*
5040 * We're truncating a file that used to have good data down to
5041 * zero. Make sure it gets into the ordered flush list so that
5042 * any new writes get down to disk quickly.
5043 */
5044 if (newsize == 0)
5045 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5046 &BTRFS_I(inode)->runtime_flags);
5047
5048 /*
5049 * 1 for the orphan item we're going to add
5050 * 1 for the orphan item deletion.
5051 */
5052 trans = btrfs_start_transaction(root, 2);
5053 if (IS_ERR(trans))
5054 return PTR_ERR(trans);
5055
5056 /*
5057 * We need to do this in case we fail at _any_ point during the
5058 * actual truncate. Once we do the truncate_setsize we could
5059 * invalidate pages which forces any outstanding ordered io to
5060 * be instantly completed which will give us extents that need
5061 * to be truncated. If we fail to get an orphan inode down we
5062 * could have left over extents that were never meant to live,
5063 * so we need to guarantee from this point on that everything
5064 * will be consistent.
5065 */
5066 ret = btrfs_orphan_add(trans, inode);
5067 btrfs_end_transaction(trans);
5068 if (ret)
5069 return ret;
5070
5071 /* we don't support swapfiles, so vmtruncate shouldn't fail */
5072 truncate_setsize(inode, newsize);
5073
5074 /* Disable nonlocked read DIO to avoid the end less truncate */
5075 btrfs_inode_block_unlocked_dio(inode);
5076 inode_dio_wait(inode);
5077 btrfs_inode_resume_unlocked_dio(inode);
5078
5079 ret = btrfs_truncate(inode);
5080 if (ret && inode->i_nlink) {
5081 int err;
5082
5083 /*
5084 * failed to truncate, disk_i_size is only adjusted down
5085 * as we remove extents, so it should represent the true
5086 * size of the inode, so reset the in memory size and
5087 * delete our orphan entry.
5088 */
5089 trans = btrfs_join_transaction(root);
5090 if (IS_ERR(trans)) {
5091 btrfs_orphan_del(NULL, inode);
5092 return ret;
5093 }
5094 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
5095 err = btrfs_orphan_del(trans, inode);
5096 if (err)
5097 btrfs_abort_transaction(trans, err);
5098 btrfs_end_transaction(trans);
5099 }
5100 }
5101
5102 return ret;
5103 }
5104
5105 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5106 {
5107 struct inode *inode = d_inode(dentry);
5108 struct btrfs_root *root = BTRFS_I(inode)->root;
5109 int err;
5110
5111 if (btrfs_root_readonly(root))
5112 return -EROFS;
5113
5114 err = setattr_prepare(dentry, attr);
5115 if (err)
5116 return err;
5117
5118 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
5119 err = btrfs_setsize(inode, attr);
5120 if (err)
5121 return err;
5122 }
5123
5124 if (attr->ia_valid) {
5125 setattr_copy(inode, attr);
5126 inode_inc_iversion(inode);
5127 err = btrfs_dirty_inode(inode);
5128
5129 if (!err && attr->ia_valid & ATTR_MODE)
5130 err = posix_acl_chmod(inode, inode->i_mode);
5131 }
5132
5133 return err;
5134 }
5135
5136 /*
5137 * While truncating the inode pages during eviction, we get the VFS calling
5138 * btrfs_invalidatepage() against each page of the inode. This is slow because
5139 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5140 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5141 * extent_state structures over and over, wasting lots of time.
5142 *
5143 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5144 * those expensive operations on a per page basis and do only the ordered io
5145 * finishing, while we release here the extent_map and extent_state structures,
5146 * without the excessive merging and splitting.
5147 */
5148 static void evict_inode_truncate_pages(struct inode *inode)
5149 {
5150 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5151 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5152 struct rb_node *node;
5153
5154 ASSERT(inode->i_state & I_FREEING);
5155 truncate_inode_pages_final(&inode->i_data);
5156
5157 write_lock(&map_tree->lock);
5158 while (!RB_EMPTY_ROOT(&map_tree->map)) {
5159 struct extent_map *em;
5160
5161 node = rb_first(&map_tree->map);
5162 em = rb_entry(node, struct extent_map, rb_node);
5163 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5164 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
5165 remove_extent_mapping(map_tree, em);
5166 free_extent_map(em);
5167 if (need_resched()) {
5168 write_unlock(&map_tree->lock);
5169 cond_resched();
5170 write_lock(&map_tree->lock);
5171 }
5172 }
5173 write_unlock(&map_tree->lock);
5174
5175 /*
5176 * Keep looping until we have no more ranges in the io tree.
5177 * We can have ongoing bios started by readpages (called from readahead)
5178 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5179 * still in progress (unlocked the pages in the bio but did not yet
5180 * unlocked the ranges in the io tree). Therefore this means some
5181 * ranges can still be locked and eviction started because before
5182 * submitting those bios, which are executed by a separate task (work
5183 * queue kthread), inode references (inode->i_count) were not taken
5184 * (which would be dropped in the end io callback of each bio).
5185 * Therefore here we effectively end up waiting for those bios and
5186 * anyone else holding locked ranges without having bumped the inode's
5187 * reference count - if we don't do it, when they access the inode's
5188 * io_tree to unlock a range it may be too late, leading to an
5189 * use-after-free issue.
5190 */
5191 spin_lock(&io_tree->lock);
5192 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5193 struct extent_state *state;
5194 struct extent_state *cached_state = NULL;
5195 u64 start;
5196 u64 end;
5197
5198 node = rb_first(&io_tree->state);
5199 state = rb_entry(node, struct extent_state, rb_node);
5200 start = state->start;
5201 end = state->end;
5202 spin_unlock(&io_tree->lock);
5203
5204 lock_extent_bits(io_tree, start, end, &cached_state);
5205
5206 /*
5207 * If still has DELALLOC flag, the extent didn't reach disk,
5208 * and its reserved space won't be freed by delayed_ref.
5209 * So we need to free its reserved space here.
5210 * (Refer to comment in btrfs_invalidatepage, case 2)
5211 *
5212 * Note, end is the bytenr of last byte, so we need + 1 here.
5213 */
5214 if (state->state & EXTENT_DELALLOC)
5215 btrfs_qgroup_free_data(inode, start, end - start + 1);
5216
5217 clear_extent_bit(io_tree, start, end,
5218 EXTENT_LOCKED | EXTENT_DIRTY |
5219 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
5220 EXTENT_DEFRAG, 1, 1,
5221 &cached_state, GFP_NOFS);
5222
5223 cond_resched();
5224 spin_lock(&io_tree->lock);
5225 }
5226 spin_unlock(&io_tree->lock);
5227 }
5228
5229 void btrfs_evict_inode(struct inode *inode)
5230 {
5231 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5232 struct btrfs_trans_handle *trans;
5233 struct btrfs_root *root = BTRFS_I(inode)->root;
5234 struct btrfs_block_rsv *rsv, *global_rsv;
5235 int steal_from_global = 0;
5236 u64 min_size;
5237 int ret;
5238
5239 trace_btrfs_inode_evict(inode);
5240
5241 if (!root) {
5242 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5243 return;
5244 }
5245
5246 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
5247
5248 evict_inode_truncate_pages(inode);
5249
5250 if (inode->i_nlink &&
5251 ((btrfs_root_refs(&root->root_item) != 0 &&
5252 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
5253 btrfs_is_free_space_inode(inode)))
5254 goto no_delete;
5255
5256 if (is_bad_inode(inode)) {
5257 btrfs_orphan_del(NULL, inode);
5258 goto no_delete;
5259 }
5260 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
5261 if (!special_file(inode->i_mode))
5262 btrfs_wait_ordered_range(inode, 0, (u64)-1);
5263
5264 btrfs_free_io_failure_record(inode, 0, (u64)-1);
5265
5266 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
5267 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
5268 &BTRFS_I(inode)->runtime_flags));
5269 goto no_delete;
5270 }
5271
5272 if (inode->i_nlink > 0) {
5273 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5274 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
5275 goto no_delete;
5276 }
5277
5278 ret = btrfs_commit_inode_delayed_inode(inode);
5279 if (ret) {
5280 btrfs_orphan_del(NULL, inode);
5281 goto no_delete;
5282 }
5283
5284 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
5285 if (!rsv) {
5286 btrfs_orphan_del(NULL, inode);
5287 goto no_delete;
5288 }
5289 rsv->size = min_size;
5290 rsv->failfast = 1;
5291 global_rsv = &fs_info->global_block_rsv;
5292
5293 btrfs_i_size_write(inode, 0);
5294
5295 /*
5296 * This is a bit simpler than btrfs_truncate since we've already
5297 * reserved our space for our orphan item in the unlink, so we just
5298 * need to reserve some slack space in case we add bytes and update
5299 * inode item when doing the truncate.
5300 */
5301 while (1) {
5302 ret = btrfs_block_rsv_refill(root, rsv, min_size,
5303 BTRFS_RESERVE_FLUSH_LIMIT);
5304
5305 /*
5306 * Try and steal from the global reserve since we will
5307 * likely not use this space anyway, we want to try as
5308 * hard as possible to get this to work.
5309 */
5310 if (ret)
5311 steal_from_global++;
5312 else
5313 steal_from_global = 0;
5314 ret = 0;
5315
5316 /*
5317 * steal_from_global == 0: we reserved stuff, hooray!
5318 * steal_from_global == 1: we didn't reserve stuff, boo!
5319 * steal_from_global == 2: we've committed, still not a lot of
5320 * room but maybe we'll have room in the global reserve this
5321 * time.
5322 * steal_from_global == 3: abandon all hope!
5323 */
5324 if (steal_from_global > 2) {
5325 btrfs_warn(fs_info,
5326 "Could not get space for a delete, will truncate on mount %d",
5327 ret);
5328 btrfs_orphan_del(NULL, inode);
5329 btrfs_free_block_rsv(fs_info, rsv);
5330 goto no_delete;
5331 }
5332
5333 trans = btrfs_join_transaction(root);
5334 if (IS_ERR(trans)) {
5335 btrfs_orphan_del(NULL, inode);
5336 btrfs_free_block_rsv(fs_info, rsv);
5337 goto no_delete;
5338 }
5339
5340 /*
5341 * We can't just steal from the global reserve, we need to make
5342 * sure there is room to do it, if not we need to commit and try
5343 * again.
5344 */
5345 if (steal_from_global) {
5346 if (!btrfs_check_space_for_delayed_refs(trans, fs_info))
5347 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
5348 min_size, 0);
5349 else
5350 ret = -ENOSPC;
5351 }
5352
5353 /*
5354 * Couldn't steal from the global reserve, we have too much
5355 * pending stuff built up, commit the transaction and try it
5356 * again.
5357 */
5358 if (ret) {
5359 ret = btrfs_commit_transaction(trans);
5360 if (ret) {
5361 btrfs_orphan_del(NULL, inode);
5362 btrfs_free_block_rsv(fs_info, rsv);
5363 goto no_delete;
5364 }
5365 continue;
5366 } else {
5367 steal_from_global = 0;
5368 }
5369
5370 trans->block_rsv = rsv;
5371
5372 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
5373 if (ret != -ENOSPC && ret != -EAGAIN)
5374 break;
5375
5376 trans->block_rsv = &fs_info->trans_block_rsv;
5377 btrfs_end_transaction(trans);
5378 trans = NULL;
5379 btrfs_btree_balance_dirty(fs_info);
5380 }
5381
5382 btrfs_free_block_rsv(fs_info, rsv);
5383
5384 /*
5385 * Errors here aren't a big deal, it just means we leave orphan items
5386 * in the tree. They will be cleaned up on the next mount.
5387 */
5388 if (ret == 0) {
5389 trans->block_rsv = root->orphan_block_rsv;
5390 btrfs_orphan_del(trans, inode);
5391 } else {
5392 btrfs_orphan_del(NULL, inode);
5393 }
5394
5395 trans->block_rsv = &fs_info->trans_block_rsv;
5396 if (!(root == fs_info->tree_root ||
5397 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
5398 btrfs_return_ino(root, btrfs_ino(inode));
5399
5400 btrfs_end_transaction(trans);
5401 btrfs_btree_balance_dirty(fs_info);
5402 no_delete:
5403 btrfs_remove_delayed_node(inode);
5404 clear_inode(inode);
5405 }
5406
5407 /*
5408 * this returns the key found in the dir entry in the location pointer.
5409 * If no dir entries were found, location->objectid is 0.
5410 */
5411 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5412 struct btrfs_key *location)
5413 {
5414 const char *name = dentry->d_name.name;
5415 int namelen = dentry->d_name.len;
5416 struct btrfs_dir_item *di;
5417 struct btrfs_path *path;
5418 struct btrfs_root *root = BTRFS_I(dir)->root;
5419 int ret = 0;
5420
5421 path = btrfs_alloc_path();
5422 if (!path)
5423 return -ENOMEM;
5424
5425 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
5426 namelen, 0);
5427 if (IS_ERR(di))
5428 ret = PTR_ERR(di);
5429
5430 if (IS_ERR_OR_NULL(di))
5431 goto out_err;
5432
5433 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
5434 out:
5435 btrfs_free_path(path);
5436 return ret;
5437 out_err:
5438 location->objectid = 0;
5439 goto out;
5440 }
5441
5442 /*
5443 * when we hit a tree root in a directory, the btrfs part of the inode
5444 * needs to be changed to reflect the root directory of the tree root. This
5445 * is kind of like crossing a mount point.
5446 */
5447 static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
5448 struct inode *dir,
5449 struct dentry *dentry,
5450 struct btrfs_key *location,
5451 struct btrfs_root **sub_root)
5452 {
5453 struct btrfs_path *path;
5454 struct btrfs_root *new_root;
5455 struct btrfs_root_ref *ref;
5456 struct extent_buffer *leaf;
5457 struct btrfs_key key;
5458 int ret;
5459 int err = 0;
5460
5461 path = btrfs_alloc_path();
5462 if (!path) {
5463 err = -ENOMEM;
5464 goto out;
5465 }
5466
5467 err = -ENOENT;
5468 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5469 key.type = BTRFS_ROOT_REF_KEY;
5470 key.offset = location->objectid;
5471
5472 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
5473 if (ret) {
5474 if (ret < 0)
5475 err = ret;
5476 goto out;
5477 }
5478
5479 leaf = path->nodes[0];
5480 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
5481 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
5482 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5483 goto out;
5484
5485 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5486 (unsigned long)(ref + 1),
5487 dentry->d_name.len);
5488 if (ret)
5489 goto out;
5490
5491 btrfs_release_path(path);
5492
5493 new_root = btrfs_read_fs_root_no_name(fs_info, location);
5494 if (IS_ERR(new_root)) {
5495 err = PTR_ERR(new_root);
5496 goto out;
5497 }
5498
5499 *sub_root = new_root;
5500 location->objectid = btrfs_root_dirid(&new_root->root_item);
5501 location->type = BTRFS_INODE_ITEM_KEY;
5502 location->offset = 0;
5503 err = 0;
5504 out:
5505 btrfs_free_path(path);
5506 return err;
5507 }
5508
5509 static void inode_tree_add(struct inode *inode)
5510 {
5511 struct btrfs_root *root = BTRFS_I(inode)->root;
5512 struct btrfs_inode *entry;
5513 struct rb_node **p;
5514 struct rb_node *parent;
5515 struct rb_node *new = &BTRFS_I(inode)->rb_node;
5516 u64 ino = btrfs_ino(inode);
5517
5518 if (inode_unhashed(inode))
5519 return;
5520 parent = NULL;
5521 spin_lock(&root->inode_lock);
5522 p = &root->inode_tree.rb_node;
5523 while (*p) {
5524 parent = *p;
5525 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5526
5527 if (ino < btrfs_ino(&entry->vfs_inode))
5528 p = &parent->rb_left;
5529 else if (ino > btrfs_ino(&entry->vfs_inode))
5530 p = &parent->rb_right;
5531 else {
5532 WARN_ON(!(entry->vfs_inode.i_state &
5533 (I_WILL_FREE | I_FREEING)));
5534 rb_replace_node(parent, new, &root->inode_tree);
5535 RB_CLEAR_NODE(parent);
5536 spin_unlock(&root->inode_lock);
5537 return;
5538 }
5539 }
5540 rb_link_node(new, parent, p);
5541 rb_insert_color(new, &root->inode_tree);
5542 spin_unlock(&root->inode_lock);
5543 }
5544
5545 static void inode_tree_del(struct inode *inode)
5546 {
5547 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5548 struct btrfs_root *root = BTRFS_I(inode)->root;
5549 int empty = 0;
5550
5551 spin_lock(&root->inode_lock);
5552 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
5553 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
5554 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
5555 empty = RB_EMPTY_ROOT(&root->inode_tree);
5556 }
5557 spin_unlock(&root->inode_lock);
5558
5559 if (empty && btrfs_root_refs(&root->root_item) == 0) {
5560 synchronize_srcu(&fs_info->subvol_srcu);
5561 spin_lock(&root->inode_lock);
5562 empty = RB_EMPTY_ROOT(&root->inode_tree);
5563 spin_unlock(&root->inode_lock);
5564 if (empty)
5565 btrfs_add_dead_root(root);
5566 }
5567 }
5568
5569 void btrfs_invalidate_inodes(struct btrfs_root *root)
5570 {
5571 struct btrfs_fs_info *fs_info = root->fs_info;
5572 struct rb_node *node;
5573 struct rb_node *prev;
5574 struct btrfs_inode *entry;
5575 struct inode *inode;
5576 u64 objectid = 0;
5577
5578 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
5579 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
5580
5581 spin_lock(&root->inode_lock);
5582 again:
5583 node = root->inode_tree.rb_node;
5584 prev = NULL;
5585 while (node) {
5586 prev = node;
5587 entry = rb_entry(node, struct btrfs_inode, rb_node);
5588
5589 if (objectid < btrfs_ino(&entry->vfs_inode))
5590 node = node->rb_left;
5591 else if (objectid > btrfs_ino(&entry->vfs_inode))
5592 node = node->rb_right;
5593 else
5594 break;
5595 }
5596 if (!node) {
5597 while (prev) {
5598 entry = rb_entry(prev, struct btrfs_inode, rb_node);
5599 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
5600 node = prev;
5601 break;
5602 }
5603 prev = rb_next(prev);
5604 }
5605 }
5606 while (node) {
5607 entry = rb_entry(node, struct btrfs_inode, rb_node);
5608 objectid = btrfs_ino(&entry->vfs_inode) + 1;
5609 inode = igrab(&entry->vfs_inode);
5610 if (inode) {
5611 spin_unlock(&root->inode_lock);
5612 if (atomic_read(&inode->i_count) > 1)
5613 d_prune_aliases(inode);
5614 /*
5615 * btrfs_drop_inode will have it removed from
5616 * the inode cache when its usage count
5617 * hits zero.
5618 */
5619 iput(inode);
5620 cond_resched();
5621 spin_lock(&root->inode_lock);
5622 goto again;
5623 }
5624
5625 if (cond_resched_lock(&root->inode_lock))
5626 goto again;
5627
5628 node = rb_next(node);
5629 }
5630 spin_unlock(&root->inode_lock);
5631 }
5632
5633 static int btrfs_init_locked_inode(struct inode *inode, void *p)
5634 {
5635 struct btrfs_iget_args *args = p;
5636 inode->i_ino = args->location->objectid;
5637 memcpy(&BTRFS_I(inode)->location, args->location,
5638 sizeof(*args->location));
5639 BTRFS_I(inode)->root = args->root;
5640 return 0;
5641 }
5642
5643 static int btrfs_find_actor(struct inode *inode, void *opaque)
5644 {
5645 struct btrfs_iget_args *args = opaque;
5646 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
5647 args->root == BTRFS_I(inode)->root;
5648 }
5649
5650 static struct inode *btrfs_iget_locked(struct super_block *s,
5651 struct btrfs_key *location,
5652 struct btrfs_root *root)
5653 {
5654 struct inode *inode;
5655 struct btrfs_iget_args args;
5656 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
5657
5658 args.location = location;
5659 args.root = root;
5660
5661 inode = iget5_locked(s, hashval, btrfs_find_actor,
5662 btrfs_init_locked_inode,
5663 (void *)&args);
5664 return inode;
5665 }
5666
5667 /* Get an inode object given its location and corresponding root.
5668 * Returns in *is_new if the inode was read from disk
5669 */
5670 struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
5671 struct btrfs_root *root, int *new)
5672 {
5673 struct inode *inode;
5674
5675 inode = btrfs_iget_locked(s, location, root);
5676 if (!inode)
5677 return ERR_PTR(-ENOMEM);
5678
5679 if (inode->i_state & I_NEW) {
5680 int ret;
5681
5682 ret = btrfs_read_locked_inode(inode);
5683 if (!is_bad_inode(inode)) {
5684 inode_tree_add(inode);
5685 unlock_new_inode(inode);
5686 if (new)
5687 *new = 1;
5688 } else {
5689 unlock_new_inode(inode);
5690 iput(inode);
5691 ASSERT(ret < 0);
5692 inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
5693 }
5694 }
5695
5696 return inode;
5697 }
5698
5699 static struct inode *new_simple_dir(struct super_block *s,
5700 struct btrfs_key *key,
5701 struct btrfs_root *root)
5702 {
5703 struct inode *inode = new_inode(s);
5704
5705 if (!inode)
5706 return ERR_PTR(-ENOMEM);
5707
5708 BTRFS_I(inode)->root = root;
5709 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
5710 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
5711
5712 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
5713 inode->i_op = &btrfs_dir_ro_inode_operations;
5714 inode->i_opflags &= ~IOP_XATTR;
5715 inode->i_fop = &simple_dir_operations;
5716 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
5717 inode->i_mtime = current_time(inode);
5718 inode->i_atime = inode->i_mtime;
5719 inode->i_ctime = inode->i_mtime;
5720 BTRFS_I(inode)->i_otime = inode->i_mtime;
5721
5722 return inode;
5723 }
5724
5725 struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
5726 {
5727 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
5728 struct inode *inode;
5729 struct btrfs_root *root = BTRFS_I(dir)->root;
5730 struct btrfs_root *sub_root = root;
5731 struct btrfs_key location;
5732 int index;
5733 int ret = 0;
5734
5735 if (dentry->d_name.len > BTRFS_NAME_LEN)
5736 return ERR_PTR(-ENAMETOOLONG);
5737
5738 ret = btrfs_inode_by_name(dir, dentry, &location);
5739 if (ret < 0)
5740 return ERR_PTR(ret);
5741
5742 if (location.objectid == 0)
5743 return ERR_PTR(-ENOENT);
5744
5745 if (location.type == BTRFS_INODE_ITEM_KEY) {
5746 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
5747 return inode;
5748 }
5749
5750 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5751
5752 index = srcu_read_lock(&fs_info->subvol_srcu);
5753 ret = fixup_tree_root_location(fs_info, dir, dentry,
5754 &location, &sub_root);
5755 if (ret < 0) {
5756 if (ret != -ENOENT)
5757 inode = ERR_PTR(ret);
5758 else
5759 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5760 } else {
5761 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
5762 }
5763 srcu_read_unlock(&fs_info->subvol_srcu, index);
5764
5765 if (!IS_ERR(inode) && root != sub_root) {
5766 down_read(&fs_info->cleanup_work_sem);
5767 if (!(inode->i_sb->s_flags & MS_RDONLY))
5768 ret = btrfs_orphan_cleanup(sub_root);
5769 up_read(&fs_info->cleanup_work_sem);
5770 if (ret) {
5771 iput(inode);
5772 inode = ERR_PTR(ret);
5773 }
5774 }
5775
5776 return inode;
5777 }
5778
5779 static int btrfs_dentry_delete(const struct dentry *dentry)
5780 {
5781 struct btrfs_root *root;
5782 struct inode *inode = d_inode(dentry);
5783
5784 if (!inode && !IS_ROOT(dentry))
5785 inode = d_inode(dentry->d_parent);
5786
5787 if (inode) {
5788 root = BTRFS_I(inode)->root;
5789 if (btrfs_root_refs(&root->root_item) == 0)
5790 return 1;
5791
5792 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5793 return 1;
5794 }
5795 return 0;
5796 }
5797
5798 static void btrfs_dentry_release(struct dentry *dentry)
5799 {
5800 kfree(dentry->d_fsdata);
5801 }
5802
5803 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
5804 unsigned int flags)
5805 {
5806 struct inode *inode;
5807
5808 inode = btrfs_lookup_dentry(dir, dentry);
5809 if (IS_ERR(inode)) {
5810 if (PTR_ERR(inode) == -ENOENT)
5811 inode = NULL;
5812 else
5813 return ERR_CAST(inode);
5814 }
5815
5816 return d_splice_alias(inode, dentry);
5817 }
5818
5819 unsigned char btrfs_filetype_table[] = {
5820 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5821 };
5822
5823 static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
5824 {
5825 struct inode *inode = file_inode(file);
5826 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5827 struct btrfs_root *root = BTRFS_I(inode)->root;
5828 struct btrfs_item *item;
5829 struct btrfs_dir_item *di;
5830 struct btrfs_key key;
5831 struct btrfs_key found_key;
5832 struct btrfs_path *path;
5833 struct list_head ins_list;
5834 struct list_head del_list;
5835 int ret;
5836 struct extent_buffer *leaf;
5837 int slot;
5838 unsigned char d_type;
5839 int over = 0;
5840 char tmp_name[32];
5841 char *name_ptr;
5842 int name_len;
5843 bool put = false;
5844 struct btrfs_key location;
5845
5846 if (!dir_emit_dots(file, ctx))
5847 return 0;
5848
5849 path = btrfs_alloc_path();
5850 if (!path)
5851 return -ENOMEM;
5852
5853 path->reada = READA_FORWARD;
5854
5855 INIT_LIST_HEAD(&ins_list);
5856 INIT_LIST_HEAD(&del_list);
5857 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
5858
5859 key.type = BTRFS_DIR_INDEX_KEY;
5860 key.offset = ctx->pos;
5861 key.objectid = btrfs_ino(inode);
5862
5863 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5864 if (ret < 0)
5865 goto err;
5866
5867 while (1) {
5868 leaf = path->nodes[0];
5869 slot = path->slots[0];
5870 if (slot >= btrfs_header_nritems(leaf)) {
5871 ret = btrfs_next_leaf(root, path);
5872 if (ret < 0)
5873 goto err;
5874 else if (ret > 0)
5875 break;
5876 continue;
5877 }
5878
5879 item = btrfs_item_nr(slot);
5880 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5881
5882 if (found_key.objectid != key.objectid)
5883 break;
5884 if (found_key.type != BTRFS_DIR_INDEX_KEY)
5885 break;
5886 if (found_key.offset < ctx->pos)
5887 goto next;
5888 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
5889 goto next;
5890
5891 ctx->pos = found_key.offset;
5892
5893 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5894 if (verify_dir_item(fs_info, leaf, di))
5895 goto next;
5896
5897 name_len = btrfs_dir_name_len(leaf, di);
5898 if (name_len <= sizeof(tmp_name)) {
5899 name_ptr = tmp_name;
5900 } else {
5901 name_ptr = kmalloc(name_len, GFP_KERNEL);
5902 if (!name_ptr) {
5903 ret = -ENOMEM;
5904 goto err;
5905 }
5906 }
5907 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5908 name_len);
5909
5910 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5911 btrfs_dir_item_key_to_cpu(leaf, di, &location);
5912
5913 over = !dir_emit(ctx, name_ptr, name_len, location.objectid,
5914 d_type);
5915
5916 if (name_ptr != tmp_name)
5917 kfree(name_ptr);
5918
5919 if (over)
5920 goto nopos;
5921 ctx->pos++;
5922 next:
5923 path->slots[0]++;
5924 }
5925
5926 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
5927 if (ret)
5928 goto nopos;
5929
5930 /*
5931 * Stop new entries from being returned after we return the last
5932 * entry.
5933 *
5934 * New directory entries are assigned a strictly increasing
5935 * offset. This means that new entries created during readdir
5936 * are *guaranteed* to be seen in the future by that readdir.
5937 * This has broken buggy programs which operate on names as
5938 * they're returned by readdir. Until we re-use freed offsets
5939 * we have this hack to stop new entries from being returned
5940 * under the assumption that they'll never reach this huge
5941 * offset.
5942 *
5943 * This is being careful not to overflow 32bit loff_t unless the
5944 * last entry requires it because doing so has broken 32bit apps
5945 * in the past.
5946 */
5947 if (ctx->pos >= INT_MAX)
5948 ctx->pos = LLONG_MAX;
5949 else
5950 ctx->pos = INT_MAX;
5951 nopos:
5952 ret = 0;
5953 err:
5954 if (put)
5955 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
5956 btrfs_free_path(path);
5957 return ret;
5958 }
5959
5960 int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
5961 {
5962 struct btrfs_root *root = BTRFS_I(inode)->root;
5963 struct btrfs_trans_handle *trans;
5964 int ret = 0;
5965 bool nolock = false;
5966
5967 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
5968 return 0;
5969
5970 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
5971 nolock = true;
5972
5973 if (wbc->sync_mode == WB_SYNC_ALL) {
5974 if (nolock)
5975 trans = btrfs_join_transaction_nolock(root);
5976 else
5977 trans = btrfs_join_transaction(root);
5978 if (IS_ERR(trans))
5979 return PTR_ERR(trans);
5980 ret = btrfs_commit_transaction(trans);
5981 }
5982 return ret;
5983 }
5984
5985 /*
5986 * This is somewhat expensive, updating the tree every time the
5987 * inode changes. But, it is most likely to find the inode in cache.
5988 * FIXME, needs more benchmarking...there are no reasons other than performance
5989 * to keep or drop this code.
5990 */
5991 static int btrfs_dirty_inode(struct inode *inode)
5992 {
5993 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5994 struct btrfs_root *root = BTRFS_I(inode)->root;
5995 struct btrfs_trans_handle *trans;
5996 int ret;
5997
5998 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
5999 return 0;
6000
6001 trans = btrfs_join_transaction(root);
6002 if (IS_ERR(trans))
6003 return PTR_ERR(trans);
6004
6005 ret = btrfs_update_inode(trans, root, inode);
6006 if (ret && ret == -ENOSPC) {
6007 /* whoops, lets try again with the full transaction */
6008 btrfs_end_transaction(trans);
6009 trans = btrfs_start_transaction(root, 1);
6010 if (IS_ERR(trans))
6011 return PTR_ERR(trans);
6012
6013 ret = btrfs_update_inode(trans, root, inode);
6014 }
6015 btrfs_end_transaction(trans);
6016 if (BTRFS_I(inode)->delayed_node)
6017 btrfs_balance_delayed_items(fs_info);
6018
6019 return ret;
6020 }
6021
6022 /*
6023 * This is a copy of file_update_time. We need this so we can return error on
6024 * ENOSPC for updating the inode in the case of file write and mmap writes.
6025 */
6026 static int btrfs_update_time(struct inode *inode, struct timespec *now,
6027 int flags)
6028 {
6029 struct btrfs_root *root = BTRFS_I(inode)->root;
6030
6031 if (btrfs_root_readonly(root))
6032 return -EROFS;
6033
6034 if (flags & S_VERSION)
6035 inode_inc_iversion(inode);
6036 if (flags & S_CTIME)
6037 inode->i_ctime = *now;
6038 if (flags & S_MTIME)
6039 inode->i_mtime = *now;
6040 if (flags & S_ATIME)
6041 inode->i_atime = *now;
6042 return btrfs_dirty_inode(inode);
6043 }
6044
6045 /*
6046 * find the highest existing sequence number in a directory
6047 * and then set the in-memory index_cnt variable to reflect
6048 * free sequence numbers
6049 */
6050 static int btrfs_set_inode_index_count(struct inode *inode)
6051 {
6052 struct btrfs_root *root = BTRFS_I(inode)->root;
6053 struct btrfs_key key, found_key;
6054 struct btrfs_path *path;
6055 struct extent_buffer *leaf;
6056 int ret;
6057
6058 key.objectid = btrfs_ino(inode);
6059 key.type = BTRFS_DIR_INDEX_KEY;
6060 key.offset = (u64)-1;
6061
6062 path = btrfs_alloc_path();
6063 if (!path)
6064 return -ENOMEM;
6065
6066 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6067 if (ret < 0)
6068 goto out;
6069 /* FIXME: we should be able to handle this */
6070 if (ret == 0)
6071 goto out;
6072 ret = 0;
6073
6074 /*
6075 * MAGIC NUMBER EXPLANATION:
6076 * since we search a directory based on f_pos we have to start at 2
6077 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6078 * else has to start at 2
6079 */
6080 if (path->slots[0] == 0) {
6081 BTRFS_I(inode)->index_cnt = 2;
6082 goto out;
6083 }
6084
6085 path->slots[0]--;
6086
6087 leaf = path->nodes[0];
6088 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6089
6090 if (found_key.objectid != btrfs_ino(inode) ||
6091 found_key.type != BTRFS_DIR_INDEX_KEY) {
6092 BTRFS_I(inode)->index_cnt = 2;
6093 goto out;
6094 }
6095
6096 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
6097 out:
6098 btrfs_free_path(path);
6099 return ret;
6100 }
6101
6102 /*
6103 * helper to find a free sequence number in a given directory. This current
6104 * code is very simple, later versions will do smarter things in the btree
6105 */
6106 int btrfs_set_inode_index(struct inode *dir, u64 *index)
6107 {
6108 int ret = 0;
6109
6110 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
6111 ret = btrfs_inode_delayed_dir_index_count(dir);
6112 if (ret) {
6113 ret = btrfs_set_inode_index_count(dir);
6114 if (ret)
6115 return ret;
6116 }
6117 }
6118
6119 *index = BTRFS_I(dir)->index_cnt;
6120 BTRFS_I(dir)->index_cnt++;
6121
6122 return ret;
6123 }
6124
6125 static int btrfs_insert_inode_locked(struct inode *inode)
6126 {
6127 struct btrfs_iget_args args;
6128 args.location = &BTRFS_I(inode)->location;
6129 args.root = BTRFS_I(inode)->root;
6130
6131 return insert_inode_locked4(inode,
6132 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6133 btrfs_find_actor, &args);
6134 }
6135
6136 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6137 struct btrfs_root *root,
6138 struct inode *dir,
6139 const char *name, int name_len,
6140 u64 ref_objectid, u64 objectid,
6141 umode_t mode, u64 *index)
6142 {
6143 struct btrfs_fs_info *fs_info = root->fs_info;
6144 struct inode *inode;
6145 struct btrfs_inode_item *inode_item;
6146 struct btrfs_key *location;
6147 struct btrfs_path *path;
6148 struct btrfs_inode_ref *ref;
6149 struct btrfs_key key[2];
6150 u32 sizes[2];
6151 int nitems = name ? 2 : 1;
6152 unsigned long ptr;
6153 int ret;
6154
6155 path = btrfs_alloc_path();
6156 if (!path)
6157 return ERR_PTR(-ENOMEM);
6158
6159 inode = new_inode(fs_info->sb);
6160 if (!inode) {
6161 btrfs_free_path(path);
6162 return ERR_PTR(-ENOMEM);
6163 }
6164
6165 /*
6166 * O_TMPFILE, set link count to 0, so that after this point,
6167 * we fill in an inode item with the correct link count.
6168 */
6169 if (!name)
6170 set_nlink(inode, 0);
6171
6172 /*
6173 * we have to initialize this early, so we can reclaim the inode
6174 * number if we fail afterwards in this function.
6175 */
6176 inode->i_ino = objectid;
6177
6178 if (dir && name) {
6179 trace_btrfs_inode_request(dir);
6180
6181 ret = btrfs_set_inode_index(dir, index);
6182 if (ret) {
6183 btrfs_free_path(path);
6184 iput(inode);
6185 return ERR_PTR(ret);
6186 }
6187 } else if (dir) {
6188 *index = 0;
6189 }
6190 /*
6191 * index_cnt is ignored for everything but a dir,
6192 * btrfs_get_inode_index_count has an explanation for the magic
6193 * number
6194 */
6195 BTRFS_I(inode)->index_cnt = 2;
6196 BTRFS_I(inode)->dir_index = *index;
6197 BTRFS_I(inode)->root = root;
6198 BTRFS_I(inode)->generation = trans->transid;
6199 inode->i_generation = BTRFS_I(inode)->generation;
6200
6201 /*
6202 * We could have gotten an inode number from somebody who was fsynced
6203 * and then removed in this same transaction, so let's just set full
6204 * sync since it will be a full sync anyway and this will blow away the
6205 * old info in the log.
6206 */
6207 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6208
6209 key[0].objectid = objectid;
6210 key[0].type = BTRFS_INODE_ITEM_KEY;
6211 key[0].offset = 0;
6212
6213 sizes[0] = sizeof(struct btrfs_inode_item);
6214
6215 if (name) {
6216 /*
6217 * Start new inodes with an inode_ref. This is slightly more
6218 * efficient for small numbers of hard links since they will
6219 * be packed into one item. Extended refs will kick in if we
6220 * add more hard links than can fit in the ref item.
6221 */
6222 key[1].objectid = objectid;
6223 key[1].type = BTRFS_INODE_REF_KEY;
6224 key[1].offset = ref_objectid;
6225
6226 sizes[1] = name_len + sizeof(*ref);
6227 }
6228
6229 location = &BTRFS_I(inode)->location;
6230 location->objectid = objectid;
6231 location->offset = 0;
6232 location->type = BTRFS_INODE_ITEM_KEY;
6233
6234 ret = btrfs_insert_inode_locked(inode);
6235 if (ret < 0)
6236 goto fail;
6237
6238 path->leave_spinning = 1;
6239 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
6240 if (ret != 0)
6241 goto fail_unlock;
6242
6243 inode_init_owner(inode, dir, mode);
6244 inode_set_bytes(inode, 0);
6245
6246 inode->i_mtime = current_time(inode);
6247 inode->i_atime = inode->i_mtime;
6248 inode->i_ctime = inode->i_mtime;
6249 BTRFS_I(inode)->i_otime = inode->i_mtime;
6250
6251 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6252 struct btrfs_inode_item);
6253 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
6254 sizeof(*inode_item));
6255 fill_inode_item(trans, path->nodes[0], inode_item, inode);
6256
6257 if (name) {
6258 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6259 struct btrfs_inode_ref);
6260 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6261 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6262 ptr = (unsigned long)(ref + 1);
6263 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6264 }
6265
6266 btrfs_mark_buffer_dirty(path->nodes[0]);
6267 btrfs_free_path(path);
6268
6269 btrfs_inherit_iflags(inode, dir);
6270
6271 if (S_ISREG(mode)) {
6272 if (btrfs_test_opt(fs_info, NODATASUM))
6273 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6274 if (btrfs_test_opt(fs_info, NODATACOW))
6275 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6276 BTRFS_INODE_NODATASUM;
6277 }
6278
6279 inode_tree_add(inode);
6280
6281 trace_btrfs_inode_new(inode);
6282 btrfs_set_inode_last_trans(trans, inode);
6283
6284 btrfs_update_root_times(trans, root);
6285
6286 ret = btrfs_inode_inherit_props(trans, inode, dir);
6287 if (ret)
6288 btrfs_err(fs_info,
6289 "error inheriting props for ino %llu (root %llu): %d",
6290 btrfs_ino(inode), root->root_key.objectid, ret);
6291
6292 return inode;
6293
6294 fail_unlock:
6295 unlock_new_inode(inode);
6296 fail:
6297 if (dir && name)
6298 BTRFS_I(dir)->index_cnt--;
6299 btrfs_free_path(path);
6300 iput(inode);
6301 return ERR_PTR(ret);
6302 }
6303
6304 static inline u8 btrfs_inode_type(struct inode *inode)
6305 {
6306 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6307 }
6308
6309 /*
6310 * utility function to add 'inode' into 'parent_inode' with
6311 * a give name and a given sequence number.
6312 * if 'add_backref' is true, also insert a backref from the
6313 * inode to the parent directory.
6314 */
6315 int btrfs_add_link(struct btrfs_trans_handle *trans,
6316 struct inode *parent_inode, struct inode *inode,
6317 const char *name, int name_len, int add_backref, u64 index)
6318 {
6319 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
6320 int ret = 0;
6321 struct btrfs_key key;
6322 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
6323 u64 ino = btrfs_ino(inode);
6324 u64 parent_ino = btrfs_ino(parent_inode);
6325
6326 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6327 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
6328 } else {
6329 key.objectid = ino;
6330 key.type = BTRFS_INODE_ITEM_KEY;
6331 key.offset = 0;
6332 }
6333
6334 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6335 ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
6336 root->root_key.objectid, parent_ino,
6337 index, name, name_len);
6338 } else if (add_backref) {
6339 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6340 parent_ino, index);
6341 }
6342
6343 /* Nothing to clean up yet */
6344 if (ret)
6345 return ret;
6346
6347 ret = btrfs_insert_dir_item(trans, root, name, name_len,
6348 parent_inode, &key,
6349 btrfs_inode_type(inode), index);
6350 if (ret == -EEXIST || ret == -EOVERFLOW)
6351 goto fail_dir_item;
6352 else if (ret) {
6353 btrfs_abort_transaction(trans, ret);
6354 return ret;
6355 }
6356
6357 btrfs_i_size_write(parent_inode, parent_inode->i_size +
6358 name_len * 2);
6359 inode_inc_iversion(parent_inode);
6360 parent_inode->i_mtime = parent_inode->i_ctime =
6361 current_time(parent_inode);
6362 ret = btrfs_update_inode(trans, root, parent_inode);
6363 if (ret)
6364 btrfs_abort_transaction(trans, ret);
6365 return ret;
6366
6367 fail_dir_item:
6368 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6369 u64 local_index;
6370 int err;
6371 err = btrfs_del_root_ref(trans, fs_info, key.objectid,
6372 root->root_key.objectid, parent_ino,
6373 &local_index, name, name_len);
6374
6375 } else if (add_backref) {
6376 u64 local_index;
6377 int err;
6378
6379 err = btrfs_del_inode_ref(trans, root, name, name_len,
6380 ino, parent_ino, &local_index);
6381 }
6382 return ret;
6383 }
6384
6385 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
6386 struct inode *dir, struct dentry *dentry,
6387 struct inode *inode, int backref, u64 index)
6388 {
6389 int err = btrfs_add_link(trans, dir, inode,
6390 dentry->d_name.name, dentry->d_name.len,
6391 backref, index);
6392 if (err > 0)
6393 err = -EEXIST;
6394 return err;
6395 }
6396
6397 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
6398 umode_t mode, dev_t rdev)
6399 {
6400 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
6401 struct btrfs_trans_handle *trans;
6402 struct btrfs_root *root = BTRFS_I(dir)->root;
6403 struct inode *inode = NULL;
6404 int err;
6405 int drop_inode = 0;
6406 u64 objectid;
6407 u64 index = 0;
6408
6409 /*
6410 * 2 for inode item and ref
6411 * 2 for dir items
6412 * 1 for xattr if selinux is on
6413 */
6414 trans = btrfs_start_transaction(root, 5);
6415 if (IS_ERR(trans))
6416 return PTR_ERR(trans);
6417
6418 err = btrfs_find_free_ino(root, &objectid);
6419 if (err)
6420 goto out_unlock;
6421
6422 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
6423 dentry->d_name.len, btrfs_ino(dir), objectid,
6424 mode, &index);
6425 if (IS_ERR(inode)) {
6426 err = PTR_ERR(inode);
6427 goto out_unlock;
6428 }
6429
6430 /*
6431 * If the active LSM wants to access the inode during
6432 * d_instantiate it needs these. Smack checks to see
6433 * if the filesystem supports xattrs by looking at the
6434 * ops vector.
6435 */
6436 inode->i_op = &btrfs_special_inode_operations;
6437 init_special_inode(inode, inode->i_mode, rdev);
6438
6439 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6440 if (err)
6441 goto out_unlock_inode;
6442
6443 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
6444 if (err) {
6445 goto out_unlock_inode;
6446 } else {
6447 btrfs_update_inode(trans, root, inode);
6448 unlock_new_inode(inode);
6449 d_instantiate(dentry, inode);
6450 }
6451
6452 out_unlock:
6453 btrfs_end_transaction(trans);
6454 btrfs_balance_delayed_items(fs_info);
6455 btrfs_btree_balance_dirty(fs_info);
6456 if (drop_inode) {
6457 inode_dec_link_count(inode);
6458 iput(inode);
6459 }
6460 return err;
6461
6462 out_unlock_inode:
6463 drop_inode = 1;
6464 unlock_new_inode(inode);
6465 goto out_unlock;
6466
6467 }
6468
6469 static int btrfs_create(struct inode *dir, struct dentry *dentry,
6470 umode_t mode, bool excl)
6471 {
6472 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
6473 struct btrfs_trans_handle *trans;
6474 struct btrfs_root *root = BTRFS_I(dir)->root;
6475 struct inode *inode = NULL;
6476 int drop_inode_on_err = 0;
6477 int err;
6478 u64 objectid;
6479 u64 index = 0;
6480
6481 /*
6482 * 2 for inode item and ref
6483 * 2 for dir items
6484 * 1 for xattr if selinux is on
6485 */
6486 trans = btrfs_start_transaction(root, 5);
6487 if (IS_ERR(trans))
6488 return PTR_ERR(trans);
6489
6490 err = btrfs_find_free_ino(root, &objectid);
6491 if (err)
6492 goto out_unlock;
6493
6494 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
6495 dentry->d_name.len, btrfs_ino(dir), objectid,
6496 mode, &index);
6497 if (IS_ERR(inode)) {
6498 err = PTR_ERR(inode);
6499 goto out_unlock;
6500 }
6501 drop_inode_on_err = 1;
6502 /*
6503 * If the active LSM wants to access the inode during
6504 * d_instantiate it needs these. Smack checks to see
6505 * if the filesystem supports xattrs by looking at the
6506 * ops vector.
6507 */
6508 inode->i_fop = &btrfs_file_operations;
6509 inode->i_op = &btrfs_file_inode_operations;
6510 inode->i_mapping->a_ops = &btrfs_aops;
6511
6512 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6513 if (err)
6514 goto out_unlock_inode;
6515
6516 err = btrfs_update_inode(trans, root, inode);
6517 if (err)
6518 goto out_unlock_inode;
6519
6520 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
6521 if (err)
6522 goto out_unlock_inode;
6523
6524 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
6525 unlock_new_inode(inode);
6526 d_instantiate(dentry, inode);
6527
6528 out_unlock:
6529 btrfs_end_transaction(trans);
6530 if (err && drop_inode_on_err) {
6531 inode_dec_link_count(inode);
6532 iput(inode);
6533 }
6534 btrfs_balance_delayed_items(fs_info);
6535 btrfs_btree_balance_dirty(fs_info);
6536 return err;
6537
6538 out_unlock_inode:
6539 unlock_new_inode(inode);
6540 goto out_unlock;
6541
6542 }
6543
6544 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6545 struct dentry *dentry)
6546 {
6547 struct btrfs_trans_handle *trans = NULL;
6548 struct btrfs_root *root = BTRFS_I(dir)->root;
6549 struct inode *inode = d_inode(old_dentry);
6550 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
6551 u64 index;
6552 int err;
6553 int drop_inode = 0;
6554
6555 /* do not allow sys_link's with other subvols of the same device */
6556 if (root->objectid != BTRFS_I(inode)->root->objectid)
6557 return -EXDEV;
6558
6559 if (inode->i_nlink >= BTRFS_LINK_MAX)
6560 return -EMLINK;
6561
6562 err = btrfs_set_inode_index(dir, &index);
6563 if (err)
6564 goto fail;
6565
6566 /*
6567 * 2 items for inode and inode ref
6568 * 2 items for dir items
6569 * 1 item for parent inode
6570 */
6571 trans = btrfs_start_transaction(root, 5);
6572 if (IS_ERR(trans)) {
6573 err = PTR_ERR(trans);
6574 trans = NULL;
6575 goto fail;
6576 }
6577
6578 /* There are several dir indexes for this inode, clear the cache. */
6579 BTRFS_I(inode)->dir_index = 0ULL;
6580 inc_nlink(inode);
6581 inode_inc_iversion(inode);
6582 inode->i_ctime = current_time(inode);
6583 ihold(inode);
6584 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
6585
6586 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
6587
6588 if (err) {
6589 drop_inode = 1;
6590 } else {
6591 struct dentry *parent = dentry->d_parent;
6592 err = btrfs_update_inode(trans, root, inode);
6593 if (err)
6594 goto fail;
6595 if (inode->i_nlink == 1) {
6596 /*
6597 * If new hard link count is 1, it's a file created
6598 * with open(2) O_TMPFILE flag.
6599 */
6600 err = btrfs_orphan_del(trans, inode);
6601 if (err)
6602 goto fail;
6603 }
6604 d_instantiate(dentry, inode);
6605 btrfs_log_new_name(trans, inode, NULL, parent);
6606 }
6607
6608 btrfs_balance_delayed_items(fs_info);
6609 fail:
6610 if (trans)
6611 btrfs_end_transaction(trans);
6612 if (drop_inode) {
6613 inode_dec_link_count(inode);
6614 iput(inode);
6615 }
6616 btrfs_btree_balance_dirty(fs_info);
6617 return err;
6618 }
6619
6620 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
6621 {
6622 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
6623 struct inode *inode = NULL;
6624 struct btrfs_trans_handle *trans;
6625 struct btrfs_root *root = BTRFS_I(dir)->root;
6626 int err = 0;
6627 int drop_on_err = 0;
6628 u64 objectid = 0;
6629 u64 index = 0;
6630
6631 /*
6632 * 2 items for inode and ref
6633 * 2 items for dir items
6634 * 1 for xattr if selinux is on
6635 */
6636 trans = btrfs_start_transaction(root, 5);
6637 if (IS_ERR(trans))
6638 return PTR_ERR(trans);
6639
6640 err = btrfs_find_free_ino(root, &objectid);
6641 if (err)
6642 goto out_fail;
6643
6644 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
6645 dentry->d_name.len, btrfs_ino(dir), objectid,
6646 S_IFDIR | mode, &index);
6647 if (IS_ERR(inode)) {
6648 err = PTR_ERR(inode);
6649 goto out_fail;
6650 }
6651
6652 drop_on_err = 1;
6653 /* these must be set before we unlock the inode */
6654 inode->i_op = &btrfs_dir_inode_operations;
6655 inode->i_fop = &btrfs_dir_file_operations;
6656
6657 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6658 if (err)
6659 goto out_fail_inode;
6660
6661 btrfs_i_size_write(inode, 0);
6662 err = btrfs_update_inode(trans, root, inode);
6663 if (err)
6664 goto out_fail_inode;
6665
6666 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6667 dentry->d_name.len, 0, index);
6668 if (err)
6669 goto out_fail_inode;
6670
6671 d_instantiate(dentry, inode);
6672 /*
6673 * mkdir is special. We're unlocking after we call d_instantiate
6674 * to avoid a race with nfsd calling d_instantiate.
6675 */
6676 unlock_new_inode(inode);
6677 drop_on_err = 0;
6678
6679 out_fail:
6680 btrfs_end_transaction(trans);
6681 if (drop_on_err) {
6682 inode_dec_link_count(inode);
6683 iput(inode);
6684 }
6685 btrfs_balance_delayed_items(fs_info);
6686 btrfs_btree_balance_dirty(fs_info);
6687 return err;
6688
6689 out_fail_inode:
6690 unlock_new_inode(inode);
6691 goto out_fail;
6692 }
6693
6694 /* Find next extent map of a given extent map, caller needs to ensure locks */
6695 static struct extent_map *next_extent_map(struct extent_map *em)
6696 {
6697 struct rb_node *next;
6698
6699 next = rb_next(&em->rb_node);
6700 if (!next)
6701 return NULL;
6702 return container_of(next, struct extent_map, rb_node);
6703 }
6704
6705 static struct extent_map *prev_extent_map(struct extent_map *em)
6706 {
6707 struct rb_node *prev;
6708
6709 prev = rb_prev(&em->rb_node);
6710 if (!prev)
6711 return NULL;
6712 return container_of(prev, struct extent_map, rb_node);
6713 }
6714
6715 /* helper for btfs_get_extent. Given an existing extent in the tree,
6716 * the existing extent is the nearest extent to map_start,
6717 * and an extent that you want to insert, deal with overlap and insert
6718 * the best fitted new extent into the tree.
6719 */
6720 static int merge_extent_mapping(struct extent_map_tree *em_tree,
6721 struct extent_map *existing,
6722 struct extent_map *em,
6723 u64 map_start)
6724 {
6725 struct extent_map *prev;
6726 struct extent_map *next;
6727 u64 start;
6728 u64 end;
6729 u64 start_diff;
6730
6731 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
6732
6733 if (existing->start > map_start) {
6734 next = existing;
6735 prev = prev_extent_map(next);
6736 } else {
6737 prev = existing;
6738 next = next_extent_map(prev);
6739 }
6740
6741 start = prev ? extent_map_end(prev) : em->start;
6742 start = max_t(u64, start, em->start);
6743 end = next ? next->start : extent_map_end(em);
6744 end = min_t(u64, end, extent_map_end(em));
6745 start_diff = start - em->start;
6746 em->start = start;
6747 em->len = end - start;
6748 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6749 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
6750 em->block_start += start_diff;
6751 em->block_len -= start_diff;
6752 }
6753 return add_extent_mapping(em_tree, em, 0);
6754 }
6755
6756 static noinline int uncompress_inline(struct btrfs_path *path,
6757 struct page *page,
6758 size_t pg_offset, u64 extent_offset,
6759 struct btrfs_file_extent_item *item)
6760 {
6761 int ret;
6762 struct extent_buffer *leaf = path->nodes[0];
6763 char *tmp;
6764 size_t max_size;
6765 unsigned long inline_size;
6766 unsigned long ptr;
6767 int compress_type;
6768
6769 WARN_ON(pg_offset != 0);
6770 compress_type = btrfs_file_extent_compression(leaf, item);
6771 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6772 inline_size = btrfs_file_extent_inline_item_len(leaf,
6773 btrfs_item_nr(path->slots[0]));
6774 tmp = kmalloc(inline_size, GFP_NOFS);
6775 if (!tmp)
6776 return -ENOMEM;
6777 ptr = btrfs_file_extent_inline_start(item);
6778
6779 read_extent_buffer(leaf, tmp, ptr, inline_size);
6780
6781 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
6782 ret = btrfs_decompress(compress_type, tmp, page,
6783 extent_offset, inline_size, max_size);
6784 kfree(tmp);
6785 return ret;
6786 }
6787
6788 /*
6789 * a bit scary, this does extent mapping from logical file offset to the disk.
6790 * the ugly parts come from merging extents from the disk with the in-ram
6791 * representation. This gets more complex because of the data=ordered code,
6792 * where the in-ram extents might be locked pending data=ordered completion.
6793 *
6794 * This also copies inline extents directly into the page.
6795 */
6796
6797 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
6798 size_t pg_offset, u64 start, u64 len,
6799 int create)
6800 {
6801 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
6802 int ret;
6803 int err = 0;
6804 u64 extent_start = 0;
6805 u64 extent_end = 0;
6806 u64 objectid = btrfs_ino(inode);
6807 u32 found_type;
6808 struct btrfs_path *path = NULL;
6809 struct btrfs_root *root = BTRFS_I(inode)->root;
6810 struct btrfs_file_extent_item *item;
6811 struct extent_buffer *leaf;
6812 struct btrfs_key found_key;
6813 struct extent_map *em = NULL;
6814 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
6815 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
6816 struct btrfs_trans_handle *trans = NULL;
6817 const bool new_inline = !page || create;
6818
6819 again:
6820 read_lock(&em_tree->lock);
6821 em = lookup_extent_mapping(em_tree, start, len);
6822 if (em)
6823 em->bdev = fs_info->fs_devices->latest_bdev;
6824 read_unlock(&em_tree->lock);
6825
6826 if (em) {
6827 if (em->start > start || em->start + em->len <= start)
6828 free_extent_map(em);
6829 else if (em->block_start == EXTENT_MAP_INLINE && page)
6830 free_extent_map(em);
6831 else
6832 goto out;
6833 }
6834 em = alloc_extent_map();
6835 if (!em) {
6836 err = -ENOMEM;
6837 goto out;
6838 }
6839 em->bdev = fs_info->fs_devices->latest_bdev;
6840 em->start = EXTENT_MAP_HOLE;
6841 em->orig_start = EXTENT_MAP_HOLE;
6842 em->len = (u64)-1;
6843 em->block_len = (u64)-1;
6844
6845 if (!path) {
6846 path = btrfs_alloc_path();
6847 if (!path) {
6848 err = -ENOMEM;
6849 goto out;
6850 }
6851 /*
6852 * Chances are we'll be called again, so go ahead and do
6853 * readahead
6854 */
6855 path->reada = READA_FORWARD;
6856 }
6857
6858 ret = btrfs_lookup_file_extent(trans, root, path,
6859 objectid, start, trans != NULL);
6860 if (ret < 0) {
6861 err = ret;
6862 goto out;
6863 }
6864
6865 if (ret != 0) {
6866 if (path->slots[0] == 0)
6867 goto not_found;
6868 path->slots[0]--;
6869 }
6870
6871 leaf = path->nodes[0];
6872 item = btrfs_item_ptr(leaf, path->slots[0],
6873 struct btrfs_file_extent_item);
6874 /* are we inside the extent that was found? */
6875 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6876 found_type = found_key.type;
6877 if (found_key.objectid != objectid ||
6878 found_type != BTRFS_EXTENT_DATA_KEY) {
6879 /*
6880 * If we backup past the first extent we want to move forward
6881 * and see if there is an extent in front of us, otherwise we'll
6882 * say there is a hole for our whole search range which can
6883 * cause problems.
6884 */
6885 extent_end = start;
6886 goto next;
6887 }
6888
6889 found_type = btrfs_file_extent_type(leaf, item);
6890 extent_start = found_key.offset;
6891 if (found_type == BTRFS_FILE_EXTENT_REG ||
6892 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
6893 extent_end = extent_start +
6894 btrfs_file_extent_num_bytes(leaf, item);
6895 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6896 size_t size;
6897 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
6898 extent_end = ALIGN(extent_start + size,
6899 fs_info->sectorsize);
6900 }
6901 next:
6902 if (start >= extent_end) {
6903 path->slots[0]++;
6904 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6905 ret = btrfs_next_leaf(root, path);
6906 if (ret < 0) {
6907 err = ret;
6908 goto out;
6909 }
6910 if (ret > 0)
6911 goto not_found;
6912 leaf = path->nodes[0];
6913 }
6914 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6915 if (found_key.objectid != objectid ||
6916 found_key.type != BTRFS_EXTENT_DATA_KEY)
6917 goto not_found;
6918 if (start + len <= found_key.offset)
6919 goto not_found;
6920 if (start > found_key.offset)
6921 goto next;
6922 em->start = start;
6923 em->orig_start = start;
6924 em->len = found_key.offset - start;
6925 goto not_found_em;
6926 }
6927
6928 btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
6929
6930 if (found_type == BTRFS_FILE_EXTENT_REG ||
6931 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
6932 goto insert;
6933 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6934 unsigned long ptr;
6935 char *map;
6936 size_t size;
6937 size_t extent_offset;
6938 size_t copy_size;
6939
6940 if (new_inline)
6941 goto out;
6942
6943 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
6944 extent_offset = page_offset(page) + pg_offset - extent_start;
6945 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6946 size - extent_offset);
6947 em->start = extent_start + extent_offset;
6948 em->len = ALIGN(copy_size, fs_info->sectorsize);
6949 em->orig_block_len = em->len;
6950 em->orig_start = em->start;
6951 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
6952 if (create == 0 && !PageUptodate(page)) {
6953 if (btrfs_file_extent_compression(leaf, item) !=
6954 BTRFS_COMPRESS_NONE) {
6955 ret = uncompress_inline(path, page, pg_offset,
6956 extent_offset, item);
6957 if (ret) {
6958 err = ret;
6959 goto out;
6960 }
6961 } else {
6962 map = kmap(page);
6963 read_extent_buffer(leaf, map + pg_offset, ptr,
6964 copy_size);
6965 if (pg_offset + copy_size < PAGE_SIZE) {
6966 memset(map + pg_offset + copy_size, 0,
6967 PAGE_SIZE - pg_offset -
6968 copy_size);
6969 }
6970 kunmap(page);
6971 }
6972 flush_dcache_page(page);
6973 } else if (create && PageUptodate(page)) {
6974 BUG();
6975 if (!trans) {
6976 kunmap(page);
6977 free_extent_map(em);
6978 em = NULL;
6979
6980 btrfs_release_path(path);
6981 trans = btrfs_join_transaction(root);
6982
6983 if (IS_ERR(trans))
6984 return ERR_CAST(trans);
6985 goto again;
6986 }
6987 map = kmap(page);
6988 write_extent_buffer(leaf, map + pg_offset, ptr,
6989 copy_size);
6990 kunmap(page);
6991 btrfs_mark_buffer_dirty(leaf);
6992 }
6993 set_extent_uptodate(io_tree, em->start,
6994 extent_map_end(em) - 1, NULL, GFP_NOFS);
6995 goto insert;
6996 }
6997 not_found:
6998 em->start = start;
6999 em->orig_start = start;
7000 em->len = len;
7001 not_found_em:
7002 em->block_start = EXTENT_MAP_HOLE;
7003 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
7004 insert:
7005 btrfs_release_path(path);
7006 if (em->start > start || extent_map_end(em) <= start) {
7007 btrfs_err(fs_info,
7008 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7009 em->start, em->len, start, len);
7010 err = -EIO;
7011 goto out;
7012 }
7013
7014 err = 0;
7015 write_lock(&em_tree->lock);
7016 ret = add_extent_mapping(em_tree, em, 0);
7017 /* it is possible that someone inserted the extent into the tree
7018 * while we had the lock dropped. It is also possible that
7019 * an overlapping map exists in the tree
7020 */
7021 if (ret == -EEXIST) {
7022 struct extent_map *existing;
7023
7024 ret = 0;
7025
7026 existing = search_extent_mapping(em_tree, start, len);
7027 /*
7028 * existing will always be non-NULL, since there must be
7029 * extent causing the -EEXIST.
7030 */
7031 if (existing->start == em->start &&
7032 extent_map_end(existing) >= extent_map_end(em) &&
7033 em->block_start == existing->block_start) {
7034 /*
7035 * The existing extent map already encompasses the
7036 * entire extent map we tried to add.
7037 */
7038 free_extent_map(em);
7039 em = existing;
7040 err = 0;
7041
7042 } else if (start >= extent_map_end(existing) ||
7043 start <= existing->start) {
7044 /*
7045 * The existing extent map is the one nearest to
7046 * the [start, start + len) range which overlaps
7047 */
7048 err = merge_extent_mapping(em_tree, existing,
7049 em, start);
7050 free_extent_map(existing);
7051 if (err) {
7052 free_extent_map(em);
7053 em = NULL;
7054 }
7055 } else {
7056 free_extent_map(em);
7057 em = existing;
7058 err = 0;
7059 }
7060 }
7061 write_unlock(&em_tree->lock);
7062 out:
7063
7064 trace_btrfs_get_extent(root, inode, em);
7065
7066 btrfs_free_path(path);
7067 if (trans) {
7068 ret = btrfs_end_transaction(trans);
7069 if (!err)
7070 err = ret;
7071 }
7072 if (err) {
7073 free_extent_map(em);
7074 return ERR_PTR(err);
7075 }
7076 BUG_ON(!em); /* Error is always set */
7077 return em;
7078 }
7079
7080 struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
7081 size_t pg_offset, u64 start, u64 len,
7082 int create)
7083 {
7084 struct extent_map *em;
7085 struct extent_map *hole_em = NULL;
7086 u64 range_start = start;
7087 u64 end;
7088 u64 found;
7089 u64 found_end;
7090 int err = 0;
7091
7092 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7093 if (IS_ERR(em))
7094 return em;
7095 if (em) {
7096 /*
7097 * if our em maps to
7098 * - a hole or
7099 * - a pre-alloc extent,
7100 * there might actually be delalloc bytes behind it.
7101 */
7102 if (em->block_start != EXTENT_MAP_HOLE &&
7103 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7104 return em;
7105 else
7106 hole_em = em;
7107 }
7108
7109 /* check to see if we've wrapped (len == -1 or similar) */
7110 end = start + len;
7111 if (end < start)
7112 end = (u64)-1;
7113 else
7114 end -= 1;
7115
7116 em = NULL;
7117
7118 /* ok, we didn't find anything, lets look for delalloc */
7119 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
7120 end, len, EXTENT_DELALLOC, 1);
7121 found_end = range_start + found;
7122 if (found_end < range_start)
7123 found_end = (u64)-1;
7124
7125 /*
7126 * we didn't find anything useful, return
7127 * the original results from get_extent()
7128 */
7129 if (range_start > end || found_end <= start) {
7130 em = hole_em;
7131 hole_em = NULL;
7132 goto out;
7133 }
7134
7135 /* adjust the range_start to make sure it doesn't
7136 * go backwards from the start they passed in
7137 */
7138 range_start = max(start, range_start);
7139 found = found_end - range_start;
7140
7141 if (found > 0) {
7142 u64 hole_start = start;
7143 u64 hole_len = len;
7144
7145 em = alloc_extent_map();
7146 if (!em) {
7147 err = -ENOMEM;
7148 goto out;
7149 }
7150 /*
7151 * when btrfs_get_extent can't find anything it
7152 * returns one huge hole
7153 *
7154 * make sure what it found really fits our range, and
7155 * adjust to make sure it is based on the start from
7156 * the caller
7157 */
7158 if (hole_em) {
7159 u64 calc_end = extent_map_end(hole_em);
7160
7161 if (calc_end <= start || (hole_em->start > end)) {
7162 free_extent_map(hole_em);
7163 hole_em = NULL;
7164 } else {
7165 hole_start = max(hole_em->start, start);
7166 hole_len = calc_end - hole_start;
7167 }
7168 }
7169 em->bdev = NULL;
7170 if (hole_em && range_start > hole_start) {
7171 /* our hole starts before our delalloc, so we
7172 * have to return just the parts of the hole
7173 * that go until the delalloc starts
7174 */
7175 em->len = min(hole_len,
7176 range_start - hole_start);
7177 em->start = hole_start;
7178 em->orig_start = hole_start;
7179 /*
7180 * don't adjust block start at all,
7181 * it is fixed at EXTENT_MAP_HOLE
7182 */
7183 em->block_start = hole_em->block_start;
7184 em->block_len = hole_len;
7185 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7186 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
7187 } else {
7188 em->start = range_start;
7189 em->len = found;
7190 em->orig_start = range_start;
7191 em->block_start = EXTENT_MAP_DELALLOC;
7192 em->block_len = found;
7193 }
7194 } else if (hole_em) {
7195 return hole_em;
7196 }
7197 out:
7198
7199 free_extent_map(hole_em);
7200 if (err) {
7201 free_extent_map(em);
7202 return ERR_PTR(err);
7203 }
7204 return em;
7205 }
7206
7207 static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7208 const u64 start,
7209 const u64 len,
7210 const u64 orig_start,
7211 const u64 block_start,
7212 const u64 block_len,
7213 const u64 orig_block_len,
7214 const u64 ram_bytes,
7215 const int type)
7216 {
7217 struct extent_map *em = NULL;
7218 int ret;
7219
7220 if (type != BTRFS_ORDERED_NOCOW) {
7221 em = create_pinned_em(inode, start, len, orig_start,
7222 block_start, block_len, orig_block_len,
7223 ram_bytes, type);
7224 if (IS_ERR(em))
7225 goto out;
7226 }
7227 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7228 len, block_len, type);
7229 if (ret) {
7230 if (em) {
7231 free_extent_map(em);
7232 btrfs_drop_extent_cache(inode, start,
7233 start + len - 1, 0);
7234 }
7235 em = ERR_PTR(ret);
7236 }
7237 out:
7238
7239 return em;
7240 }
7241
7242 static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7243 u64 start, u64 len)
7244 {
7245 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7246 struct btrfs_root *root = BTRFS_I(inode)->root;
7247 struct extent_map *em;
7248 struct btrfs_key ins;
7249 u64 alloc_hint;
7250 int ret;
7251
7252 alloc_hint = get_extent_allocation_hint(inode, start, len);
7253 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
7254 0, alloc_hint, &ins, 1, 1);
7255 if (ret)
7256 return ERR_PTR(ret);
7257
7258 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7259 ins.objectid, ins.offset, ins.offset,
7260 ins.offset, 0);
7261 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
7262 if (IS_ERR(em))
7263 btrfs_free_reserved_extent(fs_info, ins.objectid,
7264 ins.offset, 1);
7265
7266 return em;
7267 }
7268
7269 /*
7270 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7271 * block must be cow'd
7272 */
7273 noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
7274 u64 *orig_start, u64 *orig_block_len,
7275 u64 *ram_bytes)
7276 {
7277 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7278 struct btrfs_trans_handle *trans;
7279 struct btrfs_path *path;
7280 int ret;
7281 struct extent_buffer *leaf;
7282 struct btrfs_root *root = BTRFS_I(inode)->root;
7283 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7284 struct btrfs_file_extent_item *fi;
7285 struct btrfs_key key;
7286 u64 disk_bytenr;
7287 u64 backref_offset;
7288 u64 extent_end;
7289 u64 num_bytes;
7290 int slot;
7291 int found_type;
7292 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
7293
7294 path = btrfs_alloc_path();
7295 if (!path)
7296 return -ENOMEM;
7297
7298 ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(inode),
7299 offset, 0);
7300 if (ret < 0)
7301 goto out;
7302
7303 slot = path->slots[0];
7304 if (ret == 1) {
7305 if (slot == 0) {
7306 /* can't find the item, must cow */
7307 ret = 0;
7308 goto out;
7309 }
7310 slot--;
7311 }
7312 ret = 0;
7313 leaf = path->nodes[0];
7314 btrfs_item_key_to_cpu(leaf, &key, slot);
7315 if (key.objectid != btrfs_ino(inode) ||
7316 key.type != BTRFS_EXTENT_DATA_KEY) {
7317 /* not our file or wrong item type, must cow */
7318 goto out;
7319 }
7320
7321 if (key.offset > offset) {
7322 /* Wrong offset, must cow */
7323 goto out;
7324 }
7325
7326 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7327 found_type = btrfs_file_extent_type(leaf, fi);
7328 if (found_type != BTRFS_FILE_EXTENT_REG &&
7329 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7330 /* not a regular extent, must cow */
7331 goto out;
7332 }
7333
7334 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7335 goto out;
7336
7337 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7338 if (extent_end <= offset)
7339 goto out;
7340
7341 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
7342 if (disk_bytenr == 0)
7343 goto out;
7344
7345 if (btrfs_file_extent_compression(leaf, fi) ||
7346 btrfs_file_extent_encryption(leaf, fi) ||
7347 btrfs_file_extent_other_encoding(leaf, fi))
7348 goto out;
7349
7350 backref_offset = btrfs_file_extent_offset(leaf, fi);
7351
7352 if (orig_start) {
7353 *orig_start = key.offset - backref_offset;
7354 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7355 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7356 }
7357
7358 if (btrfs_extent_readonly(fs_info, disk_bytenr))
7359 goto out;
7360
7361 num_bytes = min(offset + *len, extent_end) - offset;
7362 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7363 u64 range_end;
7364
7365 range_end = round_up(offset + num_bytes,
7366 root->fs_info->sectorsize) - 1;
7367 ret = test_range_bit(io_tree, offset, range_end,
7368 EXTENT_DELALLOC, 0, NULL);
7369 if (ret) {
7370 ret = -EAGAIN;
7371 goto out;
7372 }
7373 }
7374
7375 btrfs_release_path(path);
7376
7377 /*
7378 * look for other files referencing this extent, if we
7379 * find any we must cow
7380 */
7381 trans = btrfs_join_transaction(root);
7382 if (IS_ERR(trans)) {
7383 ret = 0;
7384 goto out;
7385 }
7386
7387 ret = btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
7388 key.offset - backref_offset, disk_bytenr);
7389 btrfs_end_transaction(trans);
7390 if (ret) {
7391 ret = 0;
7392 goto out;
7393 }
7394
7395 /*
7396 * adjust disk_bytenr and num_bytes to cover just the bytes
7397 * in this extent we are about to write. If there
7398 * are any csums in that range we have to cow in order
7399 * to keep the csums correct
7400 */
7401 disk_bytenr += backref_offset;
7402 disk_bytenr += offset - key.offset;
7403 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7404 goto out;
7405 /*
7406 * all of the above have passed, it is safe to overwrite this extent
7407 * without cow
7408 */
7409 *len = num_bytes;
7410 ret = 1;
7411 out:
7412 btrfs_free_path(path);
7413 return ret;
7414 }
7415
7416 bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7417 {
7418 struct radix_tree_root *root = &inode->i_mapping->page_tree;
7419 int found = false;
7420 void **pagep = NULL;
7421 struct page *page = NULL;
7422 int start_idx;
7423 int end_idx;
7424
7425 start_idx = start >> PAGE_SHIFT;
7426
7427 /*
7428 * end is the last byte in the last page. end == start is legal
7429 */
7430 end_idx = end >> PAGE_SHIFT;
7431
7432 rcu_read_lock();
7433
7434 /* Most of the code in this while loop is lifted from
7435 * find_get_page. It's been modified to begin searching from a
7436 * page and return just the first page found in that range. If the
7437 * found idx is less than or equal to the end idx then we know that
7438 * a page exists. If no pages are found or if those pages are
7439 * outside of the range then we're fine (yay!) */
7440 while (page == NULL &&
7441 radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7442 page = radix_tree_deref_slot(pagep);
7443 if (unlikely(!page))
7444 break;
7445
7446 if (radix_tree_exception(page)) {
7447 if (radix_tree_deref_retry(page)) {
7448 page = NULL;
7449 continue;
7450 }
7451 /*
7452 * Otherwise, shmem/tmpfs must be storing a swap entry
7453 * here as an exceptional entry: so return it without
7454 * attempting to raise page count.
7455 */
7456 page = NULL;
7457 break; /* TODO: Is this relevant for this use case? */
7458 }
7459
7460 if (!page_cache_get_speculative(page)) {
7461 page = NULL;
7462 continue;
7463 }
7464
7465 /*
7466 * Has the page moved?
7467 * This is part of the lockless pagecache protocol. See
7468 * include/linux/pagemap.h for details.
7469 */
7470 if (unlikely(page != *pagep)) {
7471 put_page(page);
7472 page = NULL;
7473 }
7474 }
7475
7476 if (page) {
7477 if (page->index <= end_idx)
7478 found = true;
7479 put_page(page);
7480 }
7481
7482 rcu_read_unlock();
7483 return found;
7484 }
7485
7486 static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7487 struct extent_state **cached_state, int writing)
7488 {
7489 struct btrfs_ordered_extent *ordered;
7490 int ret = 0;
7491
7492 while (1) {
7493 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7494 cached_state);
7495 /*
7496 * We're concerned with the entire range that we're going to be
7497 * doing DIO to, so we need to make sure there's no ordered
7498 * extents in this range.
7499 */
7500 ordered = btrfs_lookup_ordered_range(inode, lockstart,
7501 lockend - lockstart + 1);
7502
7503 /*
7504 * We need to make sure there are no buffered pages in this
7505 * range either, we could have raced between the invalidate in
7506 * generic_file_direct_write and locking the extent. The
7507 * invalidate needs to happen so that reads after a write do not
7508 * get stale data.
7509 */
7510 if (!ordered &&
7511 (!writing ||
7512 !btrfs_page_exists_in_range(inode, lockstart, lockend)))
7513 break;
7514
7515 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7516 cached_state, GFP_NOFS);
7517
7518 if (ordered) {
7519 /*
7520 * If we are doing a DIO read and the ordered extent we
7521 * found is for a buffered write, we can not wait for it
7522 * to complete and retry, because if we do so we can
7523 * deadlock with concurrent buffered writes on page
7524 * locks. This happens only if our DIO read covers more
7525 * than one extent map, if at this point has already
7526 * created an ordered extent for a previous extent map
7527 * and locked its range in the inode's io tree, and a
7528 * concurrent write against that previous extent map's
7529 * range and this range started (we unlock the ranges
7530 * in the io tree only when the bios complete and
7531 * buffered writes always lock pages before attempting
7532 * to lock range in the io tree).
7533 */
7534 if (writing ||
7535 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7536 btrfs_start_ordered_extent(inode, ordered, 1);
7537 else
7538 ret = -ENOTBLK;
7539 btrfs_put_ordered_extent(ordered);
7540 } else {
7541 /*
7542 * We could trigger writeback for this range (and wait
7543 * for it to complete) and then invalidate the pages for
7544 * this range (through invalidate_inode_pages2_range()),
7545 * but that can lead us to a deadlock with a concurrent
7546 * call to readpages() (a buffered read or a defrag call
7547 * triggered a readahead) on a page lock due to an
7548 * ordered dio extent we created before but did not have
7549 * yet a corresponding bio submitted (whence it can not
7550 * complete), which makes readpages() wait for that
7551 * ordered extent to complete while holding a lock on
7552 * that page.
7553 */
7554 ret = -ENOTBLK;
7555 }
7556
7557 if (ret)
7558 break;
7559
7560 cond_resched();
7561 }
7562
7563 return ret;
7564 }
7565
7566 static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
7567 u64 len, u64 orig_start,
7568 u64 block_start, u64 block_len,
7569 u64 orig_block_len, u64 ram_bytes,
7570 int type)
7571 {
7572 struct extent_map_tree *em_tree;
7573 struct extent_map *em;
7574 struct btrfs_root *root = BTRFS_I(inode)->root;
7575 int ret;
7576
7577 em_tree = &BTRFS_I(inode)->extent_tree;
7578 em = alloc_extent_map();
7579 if (!em)
7580 return ERR_PTR(-ENOMEM);
7581
7582 em->start = start;
7583 em->orig_start = orig_start;
7584 em->mod_start = start;
7585 em->mod_len = len;
7586 em->len = len;
7587 em->block_len = block_len;
7588 em->block_start = block_start;
7589 em->bdev = root->fs_info->fs_devices->latest_bdev;
7590 em->orig_block_len = orig_block_len;
7591 em->ram_bytes = ram_bytes;
7592 em->generation = -1;
7593 set_bit(EXTENT_FLAG_PINNED, &em->flags);
7594 if (type == BTRFS_ORDERED_PREALLOC)
7595 set_bit(EXTENT_FLAG_FILLING, &em->flags);
7596
7597 do {
7598 btrfs_drop_extent_cache(inode, em->start,
7599 em->start + em->len - 1, 0);
7600 write_lock(&em_tree->lock);
7601 ret = add_extent_mapping(em_tree, em, 1);
7602 write_unlock(&em_tree->lock);
7603 } while (ret == -EEXIST);
7604
7605 if (ret) {
7606 free_extent_map(em);
7607 return ERR_PTR(ret);
7608 }
7609
7610 return em;
7611 }
7612
7613 static void adjust_dio_outstanding_extents(struct inode *inode,
7614 struct btrfs_dio_data *dio_data,
7615 const u64 len)
7616 {
7617 unsigned num_extents;
7618
7619 num_extents = (unsigned) div64_u64(len + BTRFS_MAX_EXTENT_SIZE - 1,
7620 BTRFS_MAX_EXTENT_SIZE);
7621 /*
7622 * If we have an outstanding_extents count still set then we're
7623 * within our reservation, otherwise we need to adjust our inode
7624 * counter appropriately.
7625 */
7626 if (dio_data->outstanding_extents >= num_extents) {
7627 dio_data->outstanding_extents -= num_extents;
7628 } else {
7629 /*
7630 * If dio write length has been split due to no large enough
7631 * contiguous space, we need to compensate our inode counter
7632 * appropriately.
7633 */
7634 u64 num_needed = num_extents - dio_data->outstanding_extents;
7635
7636 spin_lock(&BTRFS_I(inode)->lock);
7637 BTRFS_I(inode)->outstanding_extents += num_needed;
7638 spin_unlock(&BTRFS_I(inode)->lock);
7639 }
7640 }
7641
7642 static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7643 struct buffer_head *bh_result, int create)
7644 {
7645 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7646 struct extent_map *em;
7647 struct extent_state *cached_state = NULL;
7648 struct btrfs_dio_data *dio_data = NULL;
7649 u64 start = iblock << inode->i_blkbits;
7650 u64 lockstart, lockend;
7651 u64 len = bh_result->b_size;
7652 int unlock_bits = EXTENT_LOCKED;
7653 int ret = 0;
7654
7655 if (create)
7656 unlock_bits |= EXTENT_DIRTY;
7657 else
7658 len = min_t(u64, len, fs_info->sectorsize);
7659
7660 lockstart = start;
7661 lockend = start + len - 1;
7662
7663 if (current->journal_info) {
7664 /*
7665 * Need to pull our outstanding extents and set journal_info to NULL so
7666 * that anything that needs to check if there's a transaction doesn't get
7667 * confused.
7668 */
7669 dio_data = current->journal_info;
7670 current->journal_info = NULL;
7671 }
7672
7673 /*
7674 * If this errors out it's because we couldn't invalidate pagecache for
7675 * this range and we need to fallback to buffered.
7676 */
7677 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7678 create)) {
7679 ret = -ENOTBLK;
7680 goto err;
7681 }
7682
7683 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
7684 if (IS_ERR(em)) {
7685 ret = PTR_ERR(em);
7686 goto unlock_err;
7687 }
7688
7689 /*
7690 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7691 * io. INLINE is special, and we could probably kludge it in here, but
7692 * it's still buffered so for safety lets just fall back to the generic
7693 * buffered path.
7694 *
7695 * For COMPRESSED we _have_ to read the entire extent in so we can
7696 * decompress it, so there will be buffering required no matter what we
7697 * do, so go ahead and fallback to buffered.
7698 *
7699 * We return -ENOTBLK because that's what makes DIO go ahead and go back
7700 * to buffered IO. Don't blame me, this is the price we pay for using
7701 * the generic code.
7702 */
7703 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7704 em->block_start == EXTENT_MAP_INLINE) {
7705 free_extent_map(em);
7706 ret = -ENOTBLK;
7707 goto unlock_err;
7708 }
7709
7710 /* Just a good old fashioned hole, return */
7711 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7712 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7713 free_extent_map(em);
7714 goto unlock_err;
7715 }
7716
7717 /*
7718 * We don't allocate a new extent in the following cases
7719 *
7720 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7721 * existing extent.
7722 * 2) The extent is marked as PREALLOC. We're good to go here and can
7723 * just use the extent.
7724 *
7725 */
7726 if (!create) {
7727 len = min(len, em->len - (start - em->start));
7728 lockstart = start + len;
7729 goto unlock;
7730 }
7731
7732 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7733 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7734 em->block_start != EXTENT_MAP_HOLE)) {
7735 int type;
7736 u64 block_start, orig_start, orig_block_len, ram_bytes;
7737
7738 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7739 type = BTRFS_ORDERED_PREALLOC;
7740 else
7741 type = BTRFS_ORDERED_NOCOW;
7742 len = min(len, em->len - (start - em->start));
7743 block_start = em->block_start + (start - em->start);
7744
7745 if (can_nocow_extent(inode, start, &len, &orig_start,
7746 &orig_block_len, &ram_bytes) == 1 &&
7747 btrfs_inc_nocow_writers(fs_info, block_start)) {
7748 struct extent_map *em2;
7749
7750 em2 = btrfs_create_dio_extent(inode, start, len,
7751 orig_start, block_start,
7752 len, orig_block_len,
7753 ram_bytes, type);
7754 btrfs_dec_nocow_writers(fs_info, block_start);
7755 if (type == BTRFS_ORDERED_PREALLOC) {
7756 free_extent_map(em);
7757 em = em2;
7758 }
7759 if (em2 && IS_ERR(em2)) {
7760 ret = PTR_ERR(em2);
7761 goto unlock_err;
7762 }
7763 /*
7764 * For inode marked NODATACOW or extent marked PREALLOC,
7765 * use the existing or preallocated extent, so does not
7766 * need to adjust btrfs_space_info's bytes_may_use.
7767 */
7768 btrfs_free_reserved_data_space_noquota(inode,
7769 start, len);
7770 goto unlock;
7771 }
7772 }
7773
7774 /*
7775 * this will cow the extent, reset the len in case we changed
7776 * it above
7777 */
7778 len = bh_result->b_size;
7779 free_extent_map(em);
7780 em = btrfs_new_extent_direct(inode, start, len);
7781 if (IS_ERR(em)) {
7782 ret = PTR_ERR(em);
7783 goto unlock_err;
7784 }
7785 len = min(len, em->len - (start - em->start));
7786 unlock:
7787 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7788 inode->i_blkbits;
7789 bh_result->b_size = len;
7790 bh_result->b_bdev = em->bdev;
7791 set_buffer_mapped(bh_result);
7792 if (create) {
7793 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7794 set_buffer_new(bh_result);
7795
7796 /*
7797 * Need to update the i_size under the extent lock so buffered
7798 * readers will get the updated i_size when we unlock.
7799 */
7800 if (start + len > i_size_read(inode))
7801 i_size_write(inode, start + len);
7802
7803 adjust_dio_outstanding_extents(inode, dio_data, len);
7804 WARN_ON(dio_data->reserve < len);
7805 dio_data->reserve -= len;
7806 dio_data->unsubmitted_oe_range_end = start + len;
7807 current->journal_info = dio_data;
7808 }
7809
7810 /*
7811 * In the case of write we need to clear and unlock the entire range,
7812 * in the case of read we need to unlock only the end area that we
7813 * aren't using if there is any left over space.
7814 */
7815 if (lockstart < lockend) {
7816 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7817 lockend, unlock_bits, 1, 0,
7818 &cached_state, GFP_NOFS);
7819 } else {
7820 free_extent_state(cached_state);
7821 }
7822
7823 free_extent_map(em);
7824
7825 return 0;
7826
7827 unlock_err:
7828 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7829 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
7830 err:
7831 if (dio_data)
7832 current->journal_info = dio_data;
7833 /*
7834 * Compensate the delalloc release we do in btrfs_direct_IO() when we
7835 * write less data then expected, so that we don't underflow our inode's
7836 * outstanding extents counter.
7837 */
7838 if (create && dio_data)
7839 adjust_dio_outstanding_extents(inode, dio_data, len);
7840
7841 return ret;
7842 }
7843
7844 static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
7845 int mirror_num)
7846 {
7847 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7848 int ret;
7849
7850 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
7851
7852 bio_get(bio);
7853
7854 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
7855 if (ret)
7856 goto err;
7857
7858 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
7859 err:
7860 bio_put(bio);
7861 return ret;
7862 }
7863
7864 static int btrfs_check_dio_repairable(struct inode *inode,
7865 struct bio *failed_bio,
7866 struct io_failure_record *failrec,
7867 int failed_mirror)
7868 {
7869 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7870 int num_copies;
7871
7872 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
7873 if (num_copies == 1) {
7874 /*
7875 * we only have a single copy of the data, so don't bother with
7876 * all the retry and error correction code that follows. no
7877 * matter what the error is, it is very likely to persist.
7878 */
7879 btrfs_debug(fs_info,
7880 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7881 num_copies, failrec->this_mirror, failed_mirror);
7882 return 0;
7883 }
7884
7885 failrec->failed_mirror = failed_mirror;
7886 failrec->this_mirror++;
7887 if (failrec->this_mirror == failed_mirror)
7888 failrec->this_mirror++;
7889
7890 if (failrec->this_mirror > num_copies) {
7891 btrfs_debug(fs_info,
7892 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7893 num_copies, failrec->this_mirror, failed_mirror);
7894 return 0;
7895 }
7896
7897 return 1;
7898 }
7899
7900 static int dio_read_error(struct inode *inode, struct bio *failed_bio,
7901 struct page *page, unsigned int pgoff,
7902 u64 start, u64 end, int failed_mirror,
7903 bio_end_io_t *repair_endio, void *repair_arg)
7904 {
7905 struct io_failure_record *failrec;
7906 struct bio *bio;
7907 int isector;
7908 int read_mode = 0;
7909 int ret;
7910
7911 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
7912
7913 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7914 if (ret)
7915 return ret;
7916
7917 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7918 failed_mirror);
7919 if (!ret) {
7920 free_io_failure(inode, failrec);
7921 return -EIO;
7922 }
7923
7924 if ((failed_bio->bi_vcnt > 1)
7925 || (failed_bio->bi_io_vec->bv_len
7926 > btrfs_inode_sectorsize(inode)))
7927 read_mode |= REQ_FAILFAST_DEV;
7928
7929 isector = start - btrfs_io_bio(failed_bio)->logical;
7930 isector >>= inode->i_sb->s_blocksize_bits;
7931 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
7932 pgoff, isector, repair_endio, repair_arg);
7933 if (!bio) {
7934 free_io_failure(inode, failrec);
7935 return -EIO;
7936 }
7937 bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
7938
7939 btrfs_debug(BTRFS_I(inode)->root->fs_info,
7940 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
7941 read_mode, failrec->this_mirror, failrec->in_validation);
7942
7943 ret = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
7944 if (ret) {
7945 free_io_failure(inode, failrec);
7946 bio_put(bio);
7947 }
7948
7949 return ret;
7950 }
7951
7952 struct btrfs_retry_complete {
7953 struct completion done;
7954 struct inode *inode;
7955 u64 start;
7956 int uptodate;
7957 };
7958
7959 static void btrfs_retry_endio_nocsum(struct bio *bio)
7960 {
7961 struct btrfs_retry_complete *done = bio->bi_private;
7962 struct inode *inode;
7963 struct bio_vec *bvec;
7964 int i;
7965
7966 if (bio->bi_error)
7967 goto end;
7968
7969 ASSERT(bio->bi_vcnt == 1);
7970 inode = bio->bi_io_vec->bv_page->mapping->host;
7971 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
7972
7973 done->uptodate = 1;
7974 bio_for_each_segment_all(bvec, bio, i)
7975 clean_io_failure(done->inode, done->start, bvec->bv_page, 0);
7976 end:
7977 complete(&done->done);
7978 bio_put(bio);
7979 }
7980
7981 static int __btrfs_correct_data_nocsum(struct inode *inode,
7982 struct btrfs_io_bio *io_bio)
7983 {
7984 struct btrfs_fs_info *fs_info;
7985 struct bio_vec *bvec;
7986 struct btrfs_retry_complete done;
7987 u64 start;
7988 unsigned int pgoff;
7989 u32 sectorsize;
7990 int nr_sectors;
7991 int i;
7992 int ret;
7993
7994 fs_info = BTRFS_I(inode)->root->fs_info;
7995 sectorsize = fs_info->sectorsize;
7996
7997 start = io_bio->logical;
7998 done.inode = inode;
7999
8000 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
8001 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
8002 pgoff = bvec->bv_offset;
8003
8004 next_block_or_try_again:
8005 done.uptodate = 0;
8006 done.start = start;
8007 init_completion(&done.done);
8008
8009 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
8010 pgoff, start, start + sectorsize - 1,
8011 io_bio->mirror_num,
8012 btrfs_retry_endio_nocsum, &done);
8013 if (ret)
8014 return ret;
8015
8016 wait_for_completion(&done.done);
8017
8018 if (!done.uptodate) {
8019 /* We might have another mirror, so try again */
8020 goto next_block_or_try_again;
8021 }
8022
8023 start += sectorsize;
8024
8025 if (nr_sectors--) {
8026 pgoff += sectorsize;
8027 goto next_block_or_try_again;
8028 }
8029 }
8030
8031 return 0;
8032 }
8033
8034 static void btrfs_retry_endio(struct bio *bio)
8035 {
8036 struct btrfs_retry_complete *done = bio->bi_private;
8037 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8038 struct inode *inode;
8039 struct bio_vec *bvec;
8040 u64 start;
8041 int uptodate;
8042 int ret;
8043 int i;
8044
8045 if (bio->bi_error)
8046 goto end;
8047
8048 uptodate = 1;
8049
8050 start = done->start;
8051
8052 ASSERT(bio->bi_vcnt == 1);
8053 inode = bio->bi_io_vec->bv_page->mapping->host;
8054 ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
8055
8056 bio_for_each_segment_all(bvec, bio, i) {
8057 ret = __readpage_endio_check(done->inode, io_bio, i,
8058 bvec->bv_page, bvec->bv_offset,
8059 done->start, bvec->bv_len);
8060 if (!ret)
8061 clean_io_failure(done->inode, done->start,
8062 bvec->bv_page, bvec->bv_offset);
8063 else
8064 uptodate = 0;
8065 }
8066
8067 done->uptodate = uptodate;
8068 end:
8069 complete(&done->done);
8070 bio_put(bio);
8071 }
8072
8073 static int __btrfs_subio_endio_read(struct inode *inode,
8074 struct btrfs_io_bio *io_bio, int err)
8075 {
8076 struct btrfs_fs_info *fs_info;
8077 struct bio_vec *bvec;
8078 struct btrfs_retry_complete done;
8079 u64 start;
8080 u64 offset = 0;
8081 u32 sectorsize;
8082 int nr_sectors;
8083 unsigned int pgoff;
8084 int csum_pos;
8085 int i;
8086 int ret;
8087
8088 fs_info = BTRFS_I(inode)->root->fs_info;
8089 sectorsize = fs_info->sectorsize;
8090
8091 err = 0;
8092 start = io_bio->logical;
8093 done.inode = inode;
8094
8095 bio_for_each_segment_all(bvec, &io_bio->bio, i) {
8096 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
8097
8098 pgoff = bvec->bv_offset;
8099 next_block:
8100 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8101 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8102 bvec->bv_page, pgoff, start,
8103 sectorsize);
8104 if (likely(!ret))
8105 goto next;
8106 try_again:
8107 done.uptodate = 0;
8108 done.start = start;
8109 init_completion(&done.done);
8110
8111 ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
8112 pgoff, start, start + sectorsize - 1,
8113 io_bio->mirror_num,
8114 btrfs_retry_endio, &done);
8115 if (ret) {
8116 err = ret;
8117 goto next;
8118 }
8119
8120 wait_for_completion(&done.done);
8121
8122 if (!done.uptodate) {
8123 /* We might have another mirror, so try again */
8124 goto try_again;
8125 }
8126 next:
8127 offset += sectorsize;
8128 start += sectorsize;
8129
8130 ASSERT(nr_sectors);
8131
8132 if (--nr_sectors) {
8133 pgoff += sectorsize;
8134 goto next_block;
8135 }
8136 }
8137
8138 return err;
8139 }
8140
8141 static int btrfs_subio_endio_read(struct inode *inode,
8142 struct btrfs_io_bio *io_bio, int err)
8143 {
8144 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8145
8146 if (skip_csum) {
8147 if (unlikely(err))
8148 return __btrfs_correct_data_nocsum(inode, io_bio);
8149 else
8150 return 0;
8151 } else {
8152 return __btrfs_subio_endio_read(inode, io_bio, err);
8153 }
8154 }
8155
8156 static void btrfs_endio_direct_read(struct bio *bio)
8157 {
8158 struct btrfs_dio_private *dip = bio->bi_private;
8159 struct inode *inode = dip->inode;
8160 struct bio *dio_bio;
8161 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8162 int err = bio->bi_error;
8163
8164 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
8165 err = btrfs_subio_endio_read(inode, io_bio, err);
8166
8167 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
8168 dip->logical_offset + dip->bytes - 1);
8169 dio_bio = dip->dio_bio;
8170
8171 kfree(dip);
8172
8173 dio_bio->bi_error = bio->bi_error;
8174 dio_end_io(dio_bio, bio->bi_error);
8175
8176 if (io_bio->end_io)
8177 io_bio->end_io(io_bio, err);
8178 bio_put(bio);
8179 }
8180
8181 static void btrfs_endio_direct_write_update_ordered(struct inode *inode,
8182 const u64 offset,
8183 const u64 bytes,
8184 const int uptodate)
8185 {
8186 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
8187 struct btrfs_ordered_extent *ordered = NULL;
8188 u64 ordered_offset = offset;
8189 u64 ordered_bytes = bytes;
8190 int ret;
8191
8192 again:
8193 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8194 &ordered_offset,
8195 ordered_bytes,
8196 uptodate);
8197 if (!ret)
8198 goto out_test;
8199
8200 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
8201 finish_ordered_fn, NULL, NULL);
8202 btrfs_queue_work(fs_info->endio_write_workers, &ordered->work);
8203 out_test:
8204 /*
8205 * our bio might span multiple ordered extents. If we haven't
8206 * completed the accounting for the whole dio, go back and try again
8207 */
8208 if (ordered_offset < offset + bytes) {
8209 ordered_bytes = offset + bytes - ordered_offset;
8210 ordered = NULL;
8211 goto again;
8212 }
8213 }
8214
8215 static void btrfs_endio_direct_write(struct bio *bio)
8216 {
8217 struct btrfs_dio_private *dip = bio->bi_private;
8218 struct bio *dio_bio = dip->dio_bio;
8219
8220 btrfs_endio_direct_write_update_ordered(dip->inode,
8221 dip->logical_offset,
8222 dip->bytes,
8223 !bio->bi_error);
8224
8225 kfree(dip);
8226
8227 dio_bio->bi_error = bio->bi_error;
8228 dio_end_io(dio_bio, bio->bi_error);
8229 bio_put(bio);
8230 }
8231
8232 static int __btrfs_submit_bio_start_direct_io(struct inode *inode,
8233 struct bio *bio, int mirror_num,
8234 unsigned long bio_flags, u64 offset)
8235 {
8236 int ret;
8237 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
8238 BUG_ON(ret); /* -ENOMEM */
8239 return 0;
8240 }
8241
8242 static void btrfs_end_dio_bio(struct bio *bio)
8243 {
8244 struct btrfs_dio_private *dip = bio->bi_private;
8245 int err = bio->bi_error;
8246
8247 if (err)
8248 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
8249 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
8250 btrfs_ino(dip->inode), bio_op(bio), bio->bi_opf,
8251 (unsigned long long)bio->bi_iter.bi_sector,
8252 bio->bi_iter.bi_size, err);
8253
8254 if (dip->subio_endio)
8255 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
8256
8257 if (err) {
8258 dip->errors = 1;
8259
8260 /*
8261 * before atomic variable goto zero, we must make sure
8262 * dip->errors is perceived to be set.
8263 */
8264 smp_mb__before_atomic();
8265 }
8266
8267 /* if there are more bios still pending for this dio, just exit */
8268 if (!atomic_dec_and_test(&dip->pending_bios))
8269 goto out;
8270
8271 if (dip->errors) {
8272 bio_io_error(dip->orig_bio);
8273 } else {
8274 dip->dio_bio->bi_error = 0;
8275 bio_endio(dip->orig_bio);
8276 }
8277 out:
8278 bio_put(bio);
8279 }
8280
8281 static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
8282 u64 first_sector, gfp_t gfp_flags)
8283 {
8284 struct bio *bio;
8285 bio = btrfs_bio_alloc(bdev, first_sector, BIO_MAX_PAGES, gfp_flags);
8286 if (bio)
8287 bio_associate_current(bio);
8288 return bio;
8289 }
8290
8291 static inline int btrfs_lookup_and_bind_dio_csum(struct inode *inode,
8292 struct btrfs_dio_private *dip,
8293 struct bio *bio,
8294 u64 file_offset)
8295 {
8296 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8297 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
8298 int ret;
8299
8300 /*
8301 * We load all the csum data we need when we submit
8302 * the first bio to reduce the csum tree search and
8303 * contention.
8304 */
8305 if (dip->logical_offset == file_offset) {
8306 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
8307 file_offset);
8308 if (ret)
8309 return ret;
8310 }
8311
8312 if (bio == dip->orig_bio)
8313 return 0;
8314
8315 file_offset -= dip->logical_offset;
8316 file_offset >>= inode->i_sb->s_blocksize_bits;
8317 io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8318
8319 return 0;
8320 }
8321
8322 static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
8323 u64 file_offset, int skip_sum,
8324 int async_submit)
8325 {
8326 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
8327 struct btrfs_dio_private *dip = bio->bi_private;
8328 bool write = bio_op(bio) == REQ_OP_WRITE;
8329 int ret;
8330
8331 if (async_submit)
8332 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8333
8334 bio_get(bio);
8335
8336 if (!write) {
8337 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
8338 if (ret)
8339 goto err;
8340 }
8341
8342 if (skip_sum)
8343 goto map;
8344
8345 if (write && async_submit) {
8346 ret = btrfs_wq_submit_bio(fs_info, inode, bio, 0, 0,
8347 file_offset,
8348 __btrfs_submit_bio_start_direct_io,
8349 __btrfs_submit_bio_done);
8350 goto err;
8351 } else if (write) {
8352 /*
8353 * If we aren't doing async submit, calculate the csum of the
8354 * bio now.
8355 */
8356 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
8357 if (ret)
8358 goto err;
8359 } else {
8360 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
8361 file_offset);
8362 if (ret)
8363 goto err;
8364 }
8365 map:
8366 ret = btrfs_map_bio(fs_info, bio, 0, async_submit);
8367 err:
8368 bio_put(bio);
8369 return ret;
8370 }
8371
8372 static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip,
8373 int skip_sum)
8374 {
8375 struct inode *inode = dip->inode;
8376 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
8377 struct btrfs_root *root = BTRFS_I(inode)->root;
8378 struct bio *bio;
8379 struct bio *orig_bio = dip->orig_bio;
8380 struct bio_vec *bvec;
8381 u64 start_sector = orig_bio->bi_iter.bi_sector;
8382 u64 file_offset = dip->logical_offset;
8383 u64 submit_len = 0;
8384 u64 map_length;
8385 u32 blocksize = fs_info->sectorsize;
8386 int async_submit = 0;
8387 int nr_sectors;
8388 int ret;
8389 int i, j;
8390
8391 map_length = orig_bio->bi_iter.bi_size;
8392 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8393 &map_length, NULL, 0);
8394 if (ret)
8395 return -EIO;
8396
8397 if (map_length >= orig_bio->bi_iter.bi_size) {
8398 bio = orig_bio;
8399 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
8400 goto submit;
8401 }
8402
8403 /* async crcs make it difficult to collect full stripe writes. */
8404 if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK)
8405 async_submit = 0;
8406 else
8407 async_submit = 1;
8408
8409 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
8410 if (!bio)
8411 return -ENOMEM;
8412
8413 bio->bi_opf = orig_bio->bi_opf;
8414 bio->bi_private = dip;
8415 bio->bi_end_io = btrfs_end_dio_bio;
8416 btrfs_io_bio(bio)->logical = file_offset;
8417 atomic_inc(&dip->pending_bios);
8418
8419 bio_for_each_segment_all(bvec, orig_bio, j) {
8420 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
8421 i = 0;
8422 next_block:
8423 if (unlikely(map_length < submit_len + blocksize ||
8424 bio_add_page(bio, bvec->bv_page, blocksize,
8425 bvec->bv_offset + (i * blocksize)) < blocksize)) {
8426 /*
8427 * inc the count before we submit the bio so
8428 * we know the end IO handler won't happen before
8429 * we inc the count. Otherwise, the dip might get freed
8430 * before we're done setting it up
8431 */
8432 atomic_inc(&dip->pending_bios);
8433 ret = __btrfs_submit_dio_bio(bio, inode,
8434 file_offset, skip_sum,
8435 async_submit);
8436 if (ret) {
8437 bio_put(bio);
8438 atomic_dec(&dip->pending_bios);
8439 goto out_err;
8440 }
8441
8442 start_sector += submit_len >> 9;
8443 file_offset += submit_len;
8444
8445 submit_len = 0;
8446
8447 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
8448 start_sector, GFP_NOFS);
8449 if (!bio)
8450 goto out_err;
8451 bio->bi_opf = orig_bio->bi_opf;
8452 bio->bi_private = dip;
8453 bio->bi_end_io = btrfs_end_dio_bio;
8454 btrfs_io_bio(bio)->logical = file_offset;
8455
8456 map_length = orig_bio->bi_iter.bi_size;
8457 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
8458 start_sector << 9,
8459 &map_length, NULL, 0);
8460 if (ret) {
8461 bio_put(bio);
8462 goto out_err;
8463 }
8464
8465 goto next_block;
8466 } else {
8467 submit_len += blocksize;
8468 if (--nr_sectors) {
8469 i++;
8470 goto next_block;
8471 }
8472 }
8473 }
8474
8475 submit:
8476 ret = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum,
8477 async_submit);
8478 if (!ret)
8479 return 0;
8480
8481 bio_put(bio);
8482 out_err:
8483 dip->errors = 1;
8484 /*
8485 * before atomic variable goto zero, we must
8486 * make sure dip->errors is perceived to be set.
8487 */
8488 smp_mb__before_atomic();
8489 if (atomic_dec_and_test(&dip->pending_bios))
8490 bio_io_error(dip->orig_bio);
8491
8492 /* bio_end_io() will handle error, so we needn't return it */
8493 return 0;
8494 }
8495
8496 static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8497 loff_t file_offset)
8498 {
8499 struct btrfs_dio_private *dip = NULL;
8500 struct bio *io_bio = NULL;
8501 struct btrfs_io_bio *btrfs_bio;
8502 int skip_sum;
8503 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
8504 int ret = 0;
8505
8506 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8507
8508 io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
8509 if (!io_bio) {
8510 ret = -ENOMEM;
8511 goto free_ordered;
8512 }
8513
8514 dip = kzalloc(sizeof(*dip), GFP_NOFS);
8515 if (!dip) {
8516 ret = -ENOMEM;
8517 goto free_ordered;
8518 }
8519
8520 dip->private = dio_bio->bi_private;
8521 dip->inode = inode;
8522 dip->logical_offset = file_offset;
8523 dip->bytes = dio_bio->bi_iter.bi_size;
8524 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
8525 io_bio->bi_private = dip;
8526 dip->orig_bio = io_bio;
8527 dip->dio_bio = dio_bio;
8528 atomic_set(&dip->pending_bios, 0);
8529 btrfs_bio = btrfs_io_bio(io_bio);
8530 btrfs_bio->logical = file_offset;
8531
8532 if (write) {
8533 io_bio->bi_end_io = btrfs_endio_direct_write;
8534 } else {
8535 io_bio->bi_end_io = btrfs_endio_direct_read;
8536 dip->subio_endio = btrfs_subio_endio_read;
8537 }
8538
8539 /*
8540 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8541 * even if we fail to submit a bio, because in such case we do the
8542 * corresponding error handling below and it must not be done a second
8543 * time by btrfs_direct_IO().
8544 */
8545 if (write) {
8546 struct btrfs_dio_data *dio_data = current->journal_info;
8547
8548 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8549 dip->bytes;
8550 dio_data->unsubmitted_oe_range_start =
8551 dio_data->unsubmitted_oe_range_end;
8552 }
8553
8554 ret = btrfs_submit_direct_hook(dip, skip_sum);
8555 if (!ret)
8556 return;
8557
8558 if (btrfs_bio->end_io)
8559 btrfs_bio->end_io(btrfs_bio, ret);
8560
8561 free_ordered:
8562 /*
8563 * If we arrived here it means either we failed to submit the dip
8564 * or we either failed to clone the dio_bio or failed to allocate the
8565 * dip. If we cloned the dio_bio and allocated the dip, we can just
8566 * call bio_endio against our io_bio so that we get proper resource
8567 * cleanup if we fail to submit the dip, otherwise, we must do the
8568 * same as btrfs_endio_direct_[write|read] because we can't call these
8569 * callbacks - they require an allocated dip and a clone of dio_bio.
8570 */
8571 if (io_bio && dip) {
8572 io_bio->bi_error = -EIO;
8573 bio_endio(io_bio);
8574 /*
8575 * The end io callbacks free our dip, do the final put on io_bio
8576 * and all the cleanup and final put for dio_bio (through
8577 * dio_end_io()).
8578 */
8579 dip = NULL;
8580 io_bio = NULL;
8581 } else {
8582 if (write)
8583 btrfs_endio_direct_write_update_ordered(inode,
8584 file_offset,
8585 dio_bio->bi_iter.bi_size,
8586 0);
8587 else
8588 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8589 file_offset + dio_bio->bi_iter.bi_size - 1);
8590
8591 dio_bio->bi_error = -EIO;
8592 /*
8593 * Releases and cleans up our dio_bio, no need to bio_put()
8594 * nor bio_endio()/bio_io_error() against dio_bio.
8595 */
8596 dio_end_io(dio_bio, ret);
8597 }
8598 if (io_bio)
8599 bio_put(io_bio);
8600 kfree(dip);
8601 }
8602
8603 static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
8604 struct kiocb *iocb,
8605 const struct iov_iter *iter, loff_t offset)
8606 {
8607 int seg;
8608 int i;
8609 unsigned int blocksize_mask = fs_info->sectorsize - 1;
8610 ssize_t retval = -EINVAL;
8611
8612 if (offset & blocksize_mask)
8613 goto out;
8614
8615 if (iov_iter_alignment(iter) & blocksize_mask)
8616 goto out;
8617
8618 /* If this is a write we don't need to check anymore */
8619 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
8620 return 0;
8621 /*
8622 * Check to make sure we don't have duplicate iov_base's in this
8623 * iovec, if so return EINVAL, otherwise we'll get csum errors
8624 * when reading back.
8625 */
8626 for (seg = 0; seg < iter->nr_segs; seg++) {
8627 for (i = seg + 1; i < iter->nr_segs; i++) {
8628 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
8629 goto out;
8630 }
8631 }
8632 retval = 0;
8633 out:
8634 return retval;
8635 }
8636
8637 static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
8638 {
8639 struct file *file = iocb->ki_filp;
8640 struct inode *inode = file->f_mapping->host;
8641 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
8642 struct btrfs_dio_data dio_data = { 0 };
8643 loff_t offset = iocb->ki_pos;
8644 size_t count = 0;
8645 int flags = 0;
8646 bool wakeup = true;
8647 bool relock = false;
8648 ssize_t ret;
8649
8650 if (check_direct_IO(fs_info, iocb, iter, offset))
8651 return 0;
8652
8653 inode_dio_begin(inode);
8654 smp_mb__after_atomic();
8655
8656 /*
8657 * The generic stuff only does filemap_write_and_wait_range, which
8658 * isn't enough if we've written compressed pages to this area, so
8659 * we need to flush the dirty pages again to make absolutely sure
8660 * that any outstanding dirty pages are on disk.
8661 */
8662 count = iov_iter_count(iter);
8663 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8664 &BTRFS_I(inode)->runtime_flags))
8665 filemap_fdatawrite_range(inode->i_mapping, offset,
8666 offset + count - 1);
8667
8668 if (iov_iter_rw(iter) == WRITE) {
8669 /*
8670 * If the write DIO is beyond the EOF, we need update
8671 * the isize, but it is protected by i_mutex. So we can
8672 * not unlock the i_mutex at this case.
8673 */
8674 if (offset + count <= inode->i_size) {
8675 inode_unlock(inode);
8676 relock = true;
8677 }
8678 ret = btrfs_delalloc_reserve_space(inode, offset, count);
8679 if (ret)
8680 goto out;
8681 dio_data.outstanding_extents = div64_u64(count +
8682 BTRFS_MAX_EXTENT_SIZE - 1,
8683 BTRFS_MAX_EXTENT_SIZE);
8684
8685 /*
8686 * We need to know how many extents we reserved so that we can
8687 * do the accounting properly if we go over the number we
8688 * originally calculated. Abuse current->journal_info for this.
8689 */
8690 dio_data.reserve = round_up(count,
8691 fs_info->sectorsize);
8692 dio_data.unsubmitted_oe_range_start = (u64)offset;
8693 dio_data.unsubmitted_oe_range_end = (u64)offset;
8694 current->journal_info = &dio_data;
8695 down_read(&BTRFS_I(inode)->dio_sem);
8696 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8697 &BTRFS_I(inode)->runtime_flags)) {
8698 inode_dio_end(inode);
8699 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8700 wakeup = false;
8701 }
8702
8703 ret = __blockdev_direct_IO(iocb, inode,
8704 fs_info->fs_devices->latest_bdev,
8705 iter, btrfs_get_blocks_direct, NULL,
8706 btrfs_submit_direct, flags);
8707 if (iov_iter_rw(iter) == WRITE) {
8708 up_read(&BTRFS_I(inode)->dio_sem);
8709 current->journal_info = NULL;
8710 if (ret < 0 && ret != -EIOCBQUEUED) {
8711 if (dio_data.reserve)
8712 btrfs_delalloc_release_space(inode, offset,
8713 dio_data.reserve);
8714 /*
8715 * On error we might have left some ordered extents
8716 * without submitting corresponding bios for them, so
8717 * cleanup them up to avoid other tasks getting them
8718 * and waiting for them to complete forever.
8719 */
8720 if (dio_data.unsubmitted_oe_range_start <
8721 dio_data.unsubmitted_oe_range_end)
8722 btrfs_endio_direct_write_update_ordered(inode,
8723 dio_data.unsubmitted_oe_range_start,
8724 dio_data.unsubmitted_oe_range_end -
8725 dio_data.unsubmitted_oe_range_start,
8726 0);
8727 } else if (ret >= 0 && (size_t)ret < count)
8728 btrfs_delalloc_release_space(inode, offset,
8729 count - (size_t)ret);
8730 }
8731 out:
8732 if (wakeup)
8733 inode_dio_end(inode);
8734 if (relock)
8735 inode_lock(inode);
8736
8737 return ret;
8738 }
8739
8740 #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8741
8742 static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8743 __u64 start, __u64 len)
8744 {
8745 int ret;
8746
8747 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8748 if (ret)
8749 return ret;
8750
8751 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
8752 }
8753
8754 int btrfs_readpage(struct file *file, struct page *page)
8755 {
8756 struct extent_io_tree *tree;
8757 tree = &BTRFS_I(page->mapping->host)->io_tree;
8758 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
8759 }
8760
8761 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8762 {
8763 struct extent_io_tree *tree;
8764 struct inode *inode = page->mapping->host;
8765 int ret;
8766
8767 if (current->flags & PF_MEMALLOC) {
8768 redirty_page_for_writepage(wbc, page);
8769 unlock_page(page);
8770 return 0;
8771 }
8772
8773 /*
8774 * If we are under memory pressure we will call this directly from the
8775 * VM, we need to make sure we have the inode referenced for the ordered
8776 * extent. If not just return like we didn't do anything.
8777 */
8778 if (!igrab(inode)) {
8779 redirty_page_for_writepage(wbc, page);
8780 return AOP_WRITEPAGE_ACTIVATE;
8781 }
8782 tree = &BTRFS_I(page->mapping->host)->io_tree;
8783 ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8784 btrfs_add_delayed_iput(inode);
8785 return ret;
8786 }
8787
8788 static int btrfs_writepages(struct address_space *mapping,
8789 struct writeback_control *wbc)
8790 {
8791 struct extent_io_tree *tree;
8792
8793 tree = &BTRFS_I(mapping->host)->io_tree;
8794 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8795 }
8796
8797 static int
8798 btrfs_readpages(struct file *file, struct address_space *mapping,
8799 struct list_head *pages, unsigned nr_pages)
8800 {
8801 struct extent_io_tree *tree;
8802 tree = &BTRFS_I(mapping->host)->io_tree;
8803 return extent_readpages(tree, mapping, pages, nr_pages,
8804 btrfs_get_extent);
8805 }
8806 static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8807 {
8808 struct extent_io_tree *tree;
8809 struct extent_map_tree *map;
8810 int ret;
8811
8812 tree = &BTRFS_I(page->mapping->host)->io_tree;
8813 map = &BTRFS_I(page->mapping->host)->extent_tree;
8814 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
8815 if (ret == 1) {
8816 ClearPagePrivate(page);
8817 set_page_private(page, 0);
8818 put_page(page);
8819 }
8820 return ret;
8821 }
8822
8823 static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8824 {
8825 if (PageWriteback(page) || PageDirty(page))
8826 return 0;
8827 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
8828 }
8829
8830 static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8831 unsigned int length)
8832 {
8833 struct inode *inode = page->mapping->host;
8834 struct extent_io_tree *tree;
8835 struct btrfs_ordered_extent *ordered;
8836 struct extent_state *cached_state = NULL;
8837 u64 page_start = page_offset(page);
8838 u64 page_end = page_start + PAGE_SIZE - 1;
8839 u64 start;
8840 u64 end;
8841 int inode_evicting = inode->i_state & I_FREEING;
8842
8843 /*
8844 * we have the page locked, so new writeback can't start,
8845 * and the dirty bit won't be cleared while we are here.
8846 *
8847 * Wait for IO on this page so that we can safely clear
8848 * the PagePrivate2 bit and do ordered accounting
8849 */
8850 wait_on_page_writeback(page);
8851
8852 tree = &BTRFS_I(inode)->io_tree;
8853 if (offset) {
8854 btrfs_releasepage(page, GFP_NOFS);
8855 return;
8856 }
8857
8858 if (!inode_evicting)
8859 lock_extent_bits(tree, page_start, page_end, &cached_state);
8860 again:
8861 start = page_start;
8862 ordered = btrfs_lookup_ordered_range(inode, start,
8863 page_end - start + 1);
8864 if (ordered) {
8865 end = min(page_end, ordered->file_offset + ordered->len - 1);
8866 /*
8867 * IO on this page will never be started, so we need
8868 * to account for any ordered extents now
8869 */
8870 if (!inode_evicting)
8871 clear_extent_bit(tree, start, end,
8872 EXTENT_DIRTY | EXTENT_DELALLOC |
8873 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8874 EXTENT_DEFRAG, 1, 0, &cached_state,
8875 GFP_NOFS);
8876 /*
8877 * whoever cleared the private bit is responsible
8878 * for the finish_ordered_io
8879 */
8880 if (TestClearPagePrivate2(page)) {
8881 struct btrfs_ordered_inode_tree *tree;
8882 u64 new_len;
8883
8884 tree = &BTRFS_I(inode)->ordered_tree;
8885
8886 spin_lock_irq(&tree->lock);
8887 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
8888 new_len = start - ordered->file_offset;
8889 if (new_len < ordered->truncated_len)
8890 ordered->truncated_len = new_len;
8891 spin_unlock_irq(&tree->lock);
8892
8893 if (btrfs_dec_test_ordered_pending(inode, &ordered,
8894 start,
8895 end - start + 1, 1))
8896 btrfs_finish_ordered_io(ordered);
8897 }
8898 btrfs_put_ordered_extent(ordered);
8899 if (!inode_evicting) {
8900 cached_state = NULL;
8901 lock_extent_bits(tree, start, end,
8902 &cached_state);
8903 }
8904
8905 start = end + 1;
8906 if (start < page_end)
8907 goto again;
8908 }
8909
8910 /*
8911 * Qgroup reserved space handler
8912 * Page here will be either
8913 * 1) Already written to disk
8914 * In this case, its reserved space is released from data rsv map
8915 * and will be freed by delayed_ref handler finally.
8916 * So even we call qgroup_free_data(), it won't decrease reserved
8917 * space.
8918 * 2) Not written to disk
8919 * This means the reserved space should be freed here. However,
8920 * if a truncate invalidates the page (by clearing PageDirty)
8921 * and the page is accounted for while allocating extent
8922 * in btrfs_check_data_free_space() we let delayed_ref to
8923 * free the entire extent.
8924 */
8925 if (PageDirty(page))
8926 btrfs_qgroup_free_data(inode, page_start, PAGE_SIZE);
8927 if (!inode_evicting) {
8928 clear_extent_bit(tree, page_start, page_end,
8929 EXTENT_LOCKED | EXTENT_DIRTY |
8930 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8931 EXTENT_DEFRAG, 1, 1,
8932 &cached_state, GFP_NOFS);
8933
8934 __btrfs_releasepage(page, GFP_NOFS);
8935 }
8936
8937 ClearPageChecked(page);
8938 if (PagePrivate(page)) {
8939 ClearPagePrivate(page);
8940 set_page_private(page, 0);
8941 put_page(page);
8942 }
8943 }
8944
8945 /*
8946 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8947 * called from a page fault handler when a page is first dirtied. Hence we must
8948 * be careful to check for EOF conditions here. We set the page up correctly
8949 * for a written page which means we get ENOSPC checking when writing into
8950 * holes and correct delalloc and unwritten extent mapping on filesystems that
8951 * support these features.
8952 *
8953 * We are not allowed to take the i_mutex here so we have to play games to
8954 * protect against truncate races as the page could now be beyond EOF. Because
8955 * vmtruncate() writes the inode size before removing pages, once we have the
8956 * page lock we can determine safely if the page is beyond EOF. If it is not
8957 * beyond EOF, then the page is guaranteed safe against truncation until we
8958 * unlock the page.
8959 */
8960 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
8961 {
8962 struct page *page = vmf->page;
8963 struct inode *inode = file_inode(vma->vm_file);
8964 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
8965 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8966 struct btrfs_ordered_extent *ordered;
8967 struct extent_state *cached_state = NULL;
8968 char *kaddr;
8969 unsigned long zero_start;
8970 loff_t size;
8971 int ret;
8972 int reserved = 0;
8973 u64 reserved_space;
8974 u64 page_start;
8975 u64 page_end;
8976 u64 end;
8977
8978 reserved_space = PAGE_SIZE;
8979
8980 sb_start_pagefault(inode->i_sb);
8981 page_start = page_offset(page);
8982 page_end = page_start + PAGE_SIZE - 1;
8983 end = page_end;
8984
8985 /*
8986 * Reserving delalloc space after obtaining the page lock can lead to
8987 * deadlock. For example, if a dirty page is locked by this function
8988 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8989 * dirty page write out, then the btrfs_writepage() function could
8990 * end up waiting indefinitely to get a lock on the page currently
8991 * being processed by btrfs_page_mkwrite() function.
8992 */
8993 ret = btrfs_delalloc_reserve_space(inode, page_start,
8994 reserved_space);
8995 if (!ret) {
8996 ret = file_update_time(vma->vm_file);
8997 reserved = 1;
8998 }
8999 if (ret) {
9000 if (ret == -ENOMEM)
9001 ret = VM_FAULT_OOM;
9002 else /* -ENOSPC, -EIO, etc */
9003 ret = VM_FAULT_SIGBUS;
9004 if (reserved)
9005 goto out;
9006 goto out_noreserve;
9007 }
9008
9009 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
9010 again:
9011 lock_page(page);
9012 size = i_size_read(inode);
9013
9014 if ((page->mapping != inode->i_mapping) ||
9015 (page_start >= size)) {
9016 /* page got truncated out from underneath us */
9017 goto out_unlock;
9018 }
9019 wait_on_page_writeback(page);
9020
9021 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
9022 set_page_extent_mapped(page);
9023
9024 /*
9025 * we can't set the delalloc bits if there are pending ordered
9026 * extents. Drop our locks and wait for them to finish
9027 */
9028 ordered = btrfs_lookup_ordered_range(inode, page_start, page_end);
9029 if (ordered) {
9030 unlock_extent_cached(io_tree, page_start, page_end,
9031 &cached_state, GFP_NOFS);
9032 unlock_page(page);
9033 btrfs_start_ordered_extent(inode, ordered, 1);
9034 btrfs_put_ordered_extent(ordered);
9035 goto again;
9036 }
9037
9038 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
9039 reserved_space = round_up(size - page_start,
9040 fs_info->sectorsize);
9041 if (reserved_space < PAGE_SIZE) {
9042 end = page_start + reserved_space - 1;
9043 spin_lock(&BTRFS_I(inode)->lock);
9044 BTRFS_I(inode)->outstanding_extents++;
9045 spin_unlock(&BTRFS_I(inode)->lock);
9046 btrfs_delalloc_release_space(inode, page_start,
9047 PAGE_SIZE - reserved_space);
9048 }
9049 }
9050
9051 /*
9052 * XXX - page_mkwrite gets called every time the page is dirtied, even
9053 * if it was already dirty, so for space accounting reasons we need to
9054 * clear any delalloc bits for the range we are fixing to save. There
9055 * is probably a better way to do this, but for now keep consistent with
9056 * prepare_pages in the normal write path.
9057 */
9058 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
9059 EXTENT_DIRTY | EXTENT_DELALLOC |
9060 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
9061 0, 0, &cached_state, GFP_NOFS);
9062
9063 ret = btrfs_set_extent_delalloc(inode, page_start, end,
9064 &cached_state, 0);
9065 if (ret) {
9066 unlock_extent_cached(io_tree, page_start, page_end,
9067 &cached_state, GFP_NOFS);
9068 ret = VM_FAULT_SIGBUS;
9069 goto out_unlock;
9070 }
9071 ret = 0;
9072
9073 /* page is wholly or partially inside EOF */
9074 if (page_start + PAGE_SIZE > size)
9075 zero_start = size & ~PAGE_MASK;
9076 else
9077 zero_start = PAGE_SIZE;
9078
9079 if (zero_start != PAGE_SIZE) {
9080 kaddr = kmap(page);
9081 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
9082 flush_dcache_page(page);
9083 kunmap(page);
9084 }
9085 ClearPageChecked(page);
9086 set_page_dirty(page);
9087 SetPageUptodate(page);
9088
9089 BTRFS_I(inode)->last_trans = fs_info->generation;
9090 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
9091 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
9092
9093 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
9094
9095 out_unlock:
9096 if (!ret) {
9097 sb_end_pagefault(inode->i_sb);
9098 return VM_FAULT_LOCKED;
9099 }
9100 unlock_page(page);
9101 out:
9102 btrfs_delalloc_release_space(inode, page_start, reserved_space);
9103 out_noreserve:
9104 sb_end_pagefault(inode->i_sb);
9105 return ret;
9106 }
9107
9108 static int btrfs_truncate(struct inode *inode)
9109 {
9110 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
9111 struct btrfs_root *root = BTRFS_I(inode)->root;
9112 struct btrfs_block_rsv *rsv;
9113 int ret = 0;
9114 int err = 0;
9115 struct btrfs_trans_handle *trans;
9116 u64 mask = fs_info->sectorsize - 1;
9117 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
9118
9119 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9120 (u64)-1);
9121 if (ret)
9122 return ret;
9123
9124 /*
9125 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
9126 * 3 things going on here
9127 *
9128 * 1) We need to reserve space for our orphan item and the space to
9129 * delete our orphan item. Lord knows we don't want to have a dangling
9130 * orphan item because we didn't reserve space to remove it.
9131 *
9132 * 2) We need to reserve space to update our inode.
9133 *
9134 * 3) We need to have something to cache all the space that is going to
9135 * be free'd up by the truncate operation, but also have some slack
9136 * space reserved in case it uses space during the truncate (thank you
9137 * very much snapshotting).
9138 *
9139 * And we need these to all be separate. The fact is we can use a lot of
9140 * space doing the truncate, and we have no earthly idea how much space
9141 * we will use, so we need the truncate reservation to be separate so it
9142 * doesn't end up using space reserved for updating the inode or
9143 * removing the orphan item. We also need to be able to stop the
9144 * transaction and start a new one, which means we need to be able to
9145 * update the inode several times, and we have no idea of knowing how
9146 * many times that will be, so we can't just reserve 1 item for the
9147 * entirety of the operation, so that has to be done separately as well.
9148 * Then there is the orphan item, which does indeed need to be held on
9149 * to for the whole operation, and we need nobody to touch this reserved
9150 * space except the orphan code.
9151 *
9152 * So that leaves us with
9153 *
9154 * 1) root->orphan_block_rsv - for the orphan deletion.
9155 * 2) rsv - for the truncate reservation, which we will steal from the
9156 * transaction reservation.
9157 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9158 * updating the inode.
9159 */
9160 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
9161 if (!rsv)
9162 return -ENOMEM;
9163 rsv->size = min_size;
9164 rsv->failfast = 1;
9165
9166 /*
9167 * 1 for the truncate slack space
9168 * 1 for updating the inode.
9169 */
9170 trans = btrfs_start_transaction(root, 2);
9171 if (IS_ERR(trans)) {
9172 err = PTR_ERR(trans);
9173 goto out;
9174 }
9175
9176 /* Migrate the slack space for the truncate to our reserve */
9177 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
9178 min_size, 0);
9179 BUG_ON(ret);
9180
9181 /*
9182 * So if we truncate and then write and fsync we normally would just
9183 * write the extents that changed, which is a problem if we need to
9184 * first truncate that entire inode. So set this flag so we write out
9185 * all of the extents in the inode to the sync log so we're completely
9186 * safe.
9187 */
9188 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
9189 trans->block_rsv = rsv;
9190
9191 while (1) {
9192 ret = btrfs_truncate_inode_items(trans, root, inode,
9193 inode->i_size,
9194 BTRFS_EXTENT_DATA_KEY);
9195 if (ret != -ENOSPC && ret != -EAGAIN) {
9196 err = ret;
9197 break;
9198 }
9199
9200 trans->block_rsv = &fs_info->trans_block_rsv;
9201 ret = btrfs_update_inode(trans, root, inode);
9202 if (ret) {
9203 err = ret;
9204 break;
9205 }
9206
9207 btrfs_end_transaction(trans);
9208 btrfs_btree_balance_dirty(fs_info);
9209
9210 trans = btrfs_start_transaction(root, 2);
9211 if (IS_ERR(trans)) {
9212 ret = err = PTR_ERR(trans);
9213 trans = NULL;
9214 break;
9215 }
9216
9217 btrfs_block_rsv_release(fs_info, rsv, -1);
9218 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
9219 rsv, min_size, 0);
9220 BUG_ON(ret); /* shouldn't happen */
9221 trans->block_rsv = rsv;
9222 }
9223
9224 if (ret == 0 && inode->i_nlink > 0) {
9225 trans->block_rsv = root->orphan_block_rsv;
9226 ret = btrfs_orphan_del(trans, inode);
9227 if (ret)
9228 err = ret;
9229 }
9230
9231 if (trans) {
9232 trans->block_rsv = &fs_info->trans_block_rsv;
9233 ret = btrfs_update_inode(trans, root, inode);
9234 if (ret && !err)
9235 err = ret;
9236
9237 ret = btrfs_end_transaction(trans);
9238 btrfs_btree_balance_dirty(fs_info);
9239 }
9240 out:
9241 btrfs_free_block_rsv(fs_info, rsv);
9242
9243 if (ret && !err)
9244 err = ret;
9245
9246 return err;
9247 }
9248
9249 /*
9250 * create a new subvolume directory/inode (helper for the ioctl).
9251 */
9252 int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
9253 struct btrfs_root *new_root,
9254 struct btrfs_root *parent_root,
9255 u64 new_dirid)
9256 {
9257 struct inode *inode;
9258 int err;
9259 u64 index = 0;
9260
9261 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9262 new_dirid, new_dirid,
9263 S_IFDIR | (~current_umask() & S_IRWXUGO),
9264 &index);
9265 if (IS_ERR(inode))
9266 return PTR_ERR(inode);
9267 inode->i_op = &btrfs_dir_inode_operations;
9268 inode->i_fop = &btrfs_dir_file_operations;
9269
9270 set_nlink(inode, 1);
9271 btrfs_i_size_write(inode, 0);
9272 unlock_new_inode(inode);
9273
9274 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9275 if (err)
9276 btrfs_err(new_root->fs_info,
9277 "error inheriting subvolume %llu properties: %d",
9278 new_root->root_key.objectid, err);
9279
9280 err = btrfs_update_inode(trans, new_root, inode);
9281
9282 iput(inode);
9283 return err;
9284 }
9285
9286 struct inode *btrfs_alloc_inode(struct super_block *sb)
9287 {
9288 struct btrfs_inode *ei;
9289 struct inode *inode;
9290
9291 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
9292 if (!ei)
9293 return NULL;
9294
9295 ei->root = NULL;
9296 ei->generation = 0;
9297 ei->last_trans = 0;
9298 ei->last_sub_trans = 0;
9299 ei->logged_trans = 0;
9300 ei->delalloc_bytes = 0;
9301 ei->defrag_bytes = 0;
9302 ei->disk_i_size = 0;
9303 ei->flags = 0;
9304 ei->csum_bytes = 0;
9305 ei->index_cnt = (u64)-1;
9306 ei->dir_index = 0;
9307 ei->last_unlink_trans = 0;
9308 ei->last_log_commit = 0;
9309 ei->delayed_iput_count = 0;
9310
9311 spin_lock_init(&ei->lock);
9312 ei->outstanding_extents = 0;
9313 ei->reserved_extents = 0;
9314
9315 ei->runtime_flags = 0;
9316 ei->force_compress = BTRFS_COMPRESS_NONE;
9317
9318 ei->delayed_node = NULL;
9319
9320 ei->i_otime.tv_sec = 0;
9321 ei->i_otime.tv_nsec = 0;
9322
9323 inode = &ei->vfs_inode;
9324 extent_map_tree_init(&ei->extent_tree);
9325 extent_io_tree_init(&ei->io_tree, &inode->i_data);
9326 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
9327 ei->io_tree.track_uptodate = 1;
9328 ei->io_failure_tree.track_uptodate = 1;
9329 atomic_set(&ei->sync_writers, 0);
9330 mutex_init(&ei->log_mutex);
9331 mutex_init(&ei->delalloc_mutex);
9332 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
9333 INIT_LIST_HEAD(&ei->delalloc_inodes);
9334 INIT_LIST_HEAD(&ei->delayed_iput);
9335 RB_CLEAR_NODE(&ei->rb_node);
9336 init_rwsem(&ei->dio_sem);
9337
9338 return inode;
9339 }
9340
9341 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9342 void btrfs_test_destroy_inode(struct inode *inode)
9343 {
9344 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
9345 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9346 }
9347 #endif
9348
9349 static void btrfs_i_callback(struct rcu_head *head)
9350 {
9351 struct inode *inode = container_of(head, struct inode, i_rcu);
9352 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9353 }
9354
9355 void btrfs_destroy_inode(struct inode *inode)
9356 {
9357 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
9358 struct btrfs_ordered_extent *ordered;
9359 struct btrfs_root *root = BTRFS_I(inode)->root;
9360
9361 WARN_ON(!hlist_empty(&inode->i_dentry));
9362 WARN_ON(inode->i_data.nrpages);
9363 WARN_ON(BTRFS_I(inode)->outstanding_extents);
9364 WARN_ON(BTRFS_I(inode)->reserved_extents);
9365 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
9366 WARN_ON(BTRFS_I(inode)->csum_bytes);
9367 WARN_ON(BTRFS_I(inode)->defrag_bytes);
9368
9369 /*
9370 * This can happen where we create an inode, but somebody else also
9371 * created the same inode and we need to destroy the one we already
9372 * created.
9373 */
9374 if (!root)
9375 goto free;
9376
9377 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9378 &BTRFS_I(inode)->runtime_flags)) {
9379 btrfs_info(fs_info, "inode %llu still on the orphan list",
9380 btrfs_ino(inode));
9381 atomic_dec(&root->orphan_inodes);
9382 }
9383
9384 while (1) {
9385 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9386 if (!ordered)
9387 break;
9388 else {
9389 btrfs_err(fs_info,
9390 "found ordered extent %llu %llu on inode cleanup",
9391 ordered->file_offset, ordered->len);
9392 btrfs_remove_ordered_extent(inode, ordered);
9393 btrfs_put_ordered_extent(ordered);
9394 btrfs_put_ordered_extent(ordered);
9395 }
9396 }
9397 btrfs_qgroup_check_reserved_leak(inode);
9398 inode_tree_del(inode);
9399 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
9400 free:
9401 call_rcu(&inode->i_rcu, btrfs_i_callback);
9402 }
9403
9404 int btrfs_drop_inode(struct inode *inode)
9405 {
9406 struct btrfs_root *root = BTRFS_I(inode)->root;
9407
9408 if (root == NULL)
9409 return 1;
9410
9411 /* the snap/subvol tree is on deleting */
9412 if (btrfs_root_refs(&root->root_item) == 0)
9413 return 1;
9414 else
9415 return generic_drop_inode(inode);
9416 }
9417
9418 static void init_once(void *foo)
9419 {
9420 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9421
9422 inode_init_once(&ei->vfs_inode);
9423 }
9424
9425 void btrfs_destroy_cachep(void)
9426 {
9427 /*
9428 * Make sure all delayed rcu free inodes are flushed before we
9429 * destroy cache.
9430 */
9431 rcu_barrier();
9432 kmem_cache_destroy(btrfs_inode_cachep);
9433 kmem_cache_destroy(btrfs_trans_handle_cachep);
9434 kmem_cache_destroy(btrfs_transaction_cachep);
9435 kmem_cache_destroy(btrfs_path_cachep);
9436 kmem_cache_destroy(btrfs_free_space_cachep);
9437 }
9438
9439 int btrfs_init_cachep(void)
9440 {
9441 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
9442 sizeof(struct btrfs_inode), 0,
9443 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9444 init_once);
9445 if (!btrfs_inode_cachep)
9446 goto fail;
9447
9448 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
9449 sizeof(struct btrfs_trans_handle), 0,
9450 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
9451 if (!btrfs_trans_handle_cachep)
9452 goto fail;
9453
9454 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
9455 sizeof(struct btrfs_transaction), 0,
9456 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
9457 if (!btrfs_transaction_cachep)
9458 goto fail;
9459
9460 btrfs_path_cachep = kmem_cache_create("btrfs_path",
9461 sizeof(struct btrfs_path), 0,
9462 SLAB_MEM_SPREAD, NULL);
9463 if (!btrfs_path_cachep)
9464 goto fail;
9465
9466 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
9467 sizeof(struct btrfs_free_space), 0,
9468 SLAB_MEM_SPREAD, NULL);
9469 if (!btrfs_free_space_cachep)
9470 goto fail;
9471
9472 return 0;
9473 fail:
9474 btrfs_destroy_cachep();
9475 return -ENOMEM;
9476 }
9477
9478 static int btrfs_getattr(struct vfsmount *mnt,
9479 struct dentry *dentry, struct kstat *stat)
9480 {
9481 u64 delalloc_bytes;
9482 struct inode *inode = d_inode(dentry);
9483 u32 blocksize = inode->i_sb->s_blocksize;
9484
9485 generic_fillattr(inode, stat);
9486 stat->dev = BTRFS_I(inode)->root->anon_dev;
9487
9488 spin_lock(&BTRFS_I(inode)->lock);
9489 delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
9490 spin_unlock(&BTRFS_I(inode)->lock);
9491 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
9492 ALIGN(delalloc_bytes, blocksize)) >> 9;
9493 return 0;
9494 }
9495
9496 static int btrfs_rename_exchange(struct inode *old_dir,
9497 struct dentry *old_dentry,
9498 struct inode *new_dir,
9499 struct dentry *new_dentry)
9500 {
9501 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
9502 struct btrfs_trans_handle *trans;
9503 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9504 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
9505 struct inode *new_inode = new_dentry->d_inode;
9506 struct inode *old_inode = old_dentry->d_inode;
9507 struct timespec ctime = current_time(old_inode);
9508 struct dentry *parent;
9509 u64 old_ino = btrfs_ino(old_inode);
9510 u64 new_ino = btrfs_ino(new_inode);
9511 u64 old_idx = 0;
9512 u64 new_idx = 0;
9513 u64 root_objectid;
9514 int ret;
9515 bool root_log_pinned = false;
9516 bool dest_log_pinned = false;
9517
9518 /* we only allow rename subvolume link between subvolumes */
9519 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9520 return -EXDEV;
9521
9522 /* close the race window with snapshot create/destroy ioctl */
9523 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
9524 down_read(&fs_info->subvol_sem);
9525 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
9526 down_read(&fs_info->subvol_sem);
9527
9528 /*
9529 * We want to reserve the absolute worst case amount of items. So if
9530 * both inodes are subvols and we need to unlink them then that would
9531 * require 4 item modifications, but if they are both normal inodes it
9532 * would require 5 item modifications, so we'll assume their normal
9533 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9534 * should cover the worst case number of items we'll modify.
9535 */
9536 trans = btrfs_start_transaction(root, 12);
9537 if (IS_ERR(trans)) {
9538 ret = PTR_ERR(trans);
9539 goto out_notrans;
9540 }
9541
9542 /*
9543 * We need to find a free sequence number both in the source and
9544 * in the destination directory for the exchange.
9545 */
9546 ret = btrfs_set_inode_index(new_dir, &old_idx);
9547 if (ret)
9548 goto out_fail;
9549 ret = btrfs_set_inode_index(old_dir, &new_idx);
9550 if (ret)
9551 goto out_fail;
9552
9553 BTRFS_I(old_inode)->dir_index = 0ULL;
9554 BTRFS_I(new_inode)->dir_index = 0ULL;
9555
9556 /* Reference for the source. */
9557 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9558 /* force full log commit if subvolume involved. */
9559 btrfs_set_log_full_commit(fs_info, trans);
9560 } else {
9561 btrfs_pin_log_trans(root);
9562 root_log_pinned = true;
9563 ret = btrfs_insert_inode_ref(trans, dest,
9564 new_dentry->d_name.name,
9565 new_dentry->d_name.len,
9566 old_ino,
9567 btrfs_ino(new_dir), old_idx);
9568 if (ret)
9569 goto out_fail;
9570 }
9571
9572 /* And now for the dest. */
9573 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9574 /* force full log commit if subvolume involved. */
9575 btrfs_set_log_full_commit(fs_info, trans);
9576 } else {
9577 btrfs_pin_log_trans(dest);
9578 dest_log_pinned = true;
9579 ret = btrfs_insert_inode_ref(trans, root,
9580 old_dentry->d_name.name,
9581 old_dentry->d_name.len,
9582 new_ino,
9583 btrfs_ino(old_dir), new_idx);
9584 if (ret)
9585 goto out_fail;
9586 }
9587
9588 /* Update inode version and ctime/mtime. */
9589 inode_inc_iversion(old_dir);
9590 inode_inc_iversion(new_dir);
9591 inode_inc_iversion(old_inode);
9592 inode_inc_iversion(new_inode);
9593 old_dir->i_ctime = old_dir->i_mtime = ctime;
9594 new_dir->i_ctime = new_dir->i_mtime = ctime;
9595 old_inode->i_ctime = ctime;
9596 new_inode->i_ctime = ctime;
9597
9598 if (old_dentry->d_parent != new_dentry->d_parent) {
9599 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
9600 btrfs_record_unlink_dir(trans, new_dir, new_inode, 1);
9601 }
9602
9603 /* src is a subvolume */
9604 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9605 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9606 ret = btrfs_unlink_subvol(trans, root, old_dir,
9607 root_objectid,
9608 old_dentry->d_name.name,
9609 old_dentry->d_name.len);
9610 } else { /* src is an inode */
9611 ret = __btrfs_unlink_inode(trans, root, old_dir,
9612 old_dentry->d_inode,
9613 old_dentry->d_name.name,
9614 old_dentry->d_name.len);
9615 if (!ret)
9616 ret = btrfs_update_inode(trans, root, old_inode);
9617 }
9618 if (ret) {
9619 btrfs_abort_transaction(trans, ret);
9620 goto out_fail;
9621 }
9622
9623 /* dest is a subvolume */
9624 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9625 root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9626 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9627 root_objectid,
9628 new_dentry->d_name.name,
9629 new_dentry->d_name.len);
9630 } else { /* dest is an inode */
9631 ret = __btrfs_unlink_inode(trans, dest, new_dir,
9632 new_dentry->d_inode,
9633 new_dentry->d_name.name,
9634 new_dentry->d_name.len);
9635 if (!ret)
9636 ret = btrfs_update_inode(trans, dest, new_inode);
9637 }
9638 if (ret) {
9639 btrfs_abort_transaction(trans, ret);
9640 goto out_fail;
9641 }
9642
9643 ret = btrfs_add_link(trans, new_dir, old_inode,
9644 new_dentry->d_name.name,
9645 new_dentry->d_name.len, 0, old_idx);
9646 if (ret) {
9647 btrfs_abort_transaction(trans, ret);
9648 goto out_fail;
9649 }
9650
9651 ret = btrfs_add_link(trans, old_dir, new_inode,
9652 old_dentry->d_name.name,
9653 old_dentry->d_name.len, 0, new_idx);
9654 if (ret) {
9655 btrfs_abort_transaction(trans, ret);
9656 goto out_fail;
9657 }
9658
9659 if (old_inode->i_nlink == 1)
9660 BTRFS_I(old_inode)->dir_index = old_idx;
9661 if (new_inode->i_nlink == 1)
9662 BTRFS_I(new_inode)->dir_index = new_idx;
9663
9664 if (root_log_pinned) {
9665 parent = new_dentry->d_parent;
9666 btrfs_log_new_name(trans, old_inode, old_dir, parent);
9667 btrfs_end_log_trans(root);
9668 root_log_pinned = false;
9669 }
9670 if (dest_log_pinned) {
9671 parent = old_dentry->d_parent;
9672 btrfs_log_new_name(trans, new_inode, new_dir, parent);
9673 btrfs_end_log_trans(dest);
9674 dest_log_pinned = false;
9675 }
9676 out_fail:
9677 /*
9678 * If we have pinned a log and an error happened, we unpin tasks
9679 * trying to sync the log and force them to fallback to a transaction
9680 * commit if the log currently contains any of the inodes involved in
9681 * this rename operation (to ensure we do not persist a log with an
9682 * inconsistent state for any of these inodes or leading to any
9683 * inconsistencies when replayed). If the transaction was aborted, the
9684 * abortion reason is propagated to userspace when attempting to commit
9685 * the transaction. If the log does not contain any of these inodes, we
9686 * allow the tasks to sync it.
9687 */
9688 if (ret && (root_log_pinned || dest_log_pinned)) {
9689 if (btrfs_inode_in_log(old_dir, fs_info->generation) ||
9690 btrfs_inode_in_log(new_dir, fs_info->generation) ||
9691 btrfs_inode_in_log(old_inode, fs_info->generation) ||
9692 (new_inode &&
9693 btrfs_inode_in_log(new_inode, fs_info->generation)))
9694 btrfs_set_log_full_commit(fs_info, trans);
9695
9696 if (root_log_pinned) {
9697 btrfs_end_log_trans(root);
9698 root_log_pinned = false;
9699 }
9700 if (dest_log_pinned) {
9701 btrfs_end_log_trans(dest);
9702 dest_log_pinned = false;
9703 }
9704 }
9705 ret = btrfs_end_transaction(trans);
9706 out_notrans:
9707 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
9708 up_read(&fs_info->subvol_sem);
9709 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
9710 up_read(&fs_info->subvol_sem);
9711
9712 return ret;
9713 }
9714
9715 static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9716 struct btrfs_root *root,
9717 struct inode *dir,
9718 struct dentry *dentry)
9719 {
9720 int ret;
9721 struct inode *inode;
9722 u64 objectid;
9723 u64 index;
9724
9725 ret = btrfs_find_free_ino(root, &objectid);
9726 if (ret)
9727 return ret;
9728
9729 inode = btrfs_new_inode(trans, root, dir,
9730 dentry->d_name.name,
9731 dentry->d_name.len,
9732 btrfs_ino(dir),
9733 objectid,
9734 S_IFCHR | WHITEOUT_MODE,
9735 &index);
9736
9737 if (IS_ERR(inode)) {
9738 ret = PTR_ERR(inode);
9739 return ret;
9740 }
9741
9742 inode->i_op = &btrfs_special_inode_operations;
9743 init_special_inode(inode, inode->i_mode,
9744 WHITEOUT_DEV);
9745
9746 ret = btrfs_init_inode_security(trans, inode, dir,
9747 &dentry->d_name);
9748 if (ret)
9749 goto out;
9750
9751 ret = btrfs_add_nondir(trans, dir, dentry,
9752 inode, 0, index);
9753 if (ret)
9754 goto out;
9755
9756 ret = btrfs_update_inode(trans, root, inode);
9757 out:
9758 unlock_new_inode(inode);
9759 if (ret)
9760 inode_dec_link_count(inode);
9761 iput(inode);
9762
9763 return ret;
9764 }
9765
9766 static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
9767 struct inode *new_dir, struct dentry *new_dentry,
9768 unsigned int flags)
9769 {
9770 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
9771 struct btrfs_trans_handle *trans;
9772 unsigned int trans_num_items;
9773 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9774 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
9775 struct inode *new_inode = d_inode(new_dentry);
9776 struct inode *old_inode = d_inode(old_dentry);
9777 u64 index = 0;
9778 u64 root_objectid;
9779 int ret;
9780 u64 old_ino = btrfs_ino(old_inode);
9781 bool log_pinned = false;
9782
9783 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
9784 return -EPERM;
9785
9786 /* we only allow rename subvolume link between subvolumes */
9787 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9788 return -EXDEV;
9789
9790 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
9791 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
9792 return -ENOTEMPTY;
9793
9794 if (S_ISDIR(old_inode->i_mode) && new_inode &&
9795 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
9796 return -ENOTEMPTY;
9797
9798
9799 /* check for collisions, even if the name isn't there */
9800 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
9801 new_dentry->d_name.name,
9802 new_dentry->d_name.len);
9803
9804 if (ret) {
9805 if (ret == -EEXIST) {
9806 /* we shouldn't get
9807 * eexist without a new_inode */
9808 if (WARN_ON(!new_inode)) {
9809 return ret;
9810 }
9811 } else {
9812 /* maybe -EOVERFLOW */
9813 return ret;
9814 }
9815 }
9816 ret = 0;
9817
9818 /*
9819 * we're using rename to replace one file with another. Start IO on it
9820 * now so we don't add too much work to the end of the transaction
9821 */
9822 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
9823 filemap_flush(old_inode->i_mapping);
9824
9825 /* close the racy window with snapshot create/destroy ioctl */
9826 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
9827 down_read(&fs_info->subvol_sem);
9828 /*
9829 * We want to reserve the absolute worst case amount of items. So if
9830 * both inodes are subvols and we need to unlink them then that would
9831 * require 4 item modifications, but if they are both normal inodes it
9832 * would require 5 item modifications, so we'll assume they are normal
9833 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9834 * should cover the worst case number of items we'll modify.
9835 * If our rename has the whiteout flag, we need more 5 units for the
9836 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9837 * when selinux is enabled).
9838 */
9839 trans_num_items = 11;
9840 if (flags & RENAME_WHITEOUT)
9841 trans_num_items += 5;
9842 trans = btrfs_start_transaction(root, trans_num_items);
9843 if (IS_ERR(trans)) {
9844 ret = PTR_ERR(trans);
9845 goto out_notrans;
9846 }
9847
9848 if (dest != root)
9849 btrfs_record_root_in_trans(trans, dest);
9850
9851 ret = btrfs_set_inode_index(new_dir, &index);
9852 if (ret)
9853 goto out_fail;
9854
9855 BTRFS_I(old_inode)->dir_index = 0ULL;
9856 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
9857 /* force full log commit if subvolume involved. */
9858 btrfs_set_log_full_commit(fs_info, trans);
9859 } else {
9860 btrfs_pin_log_trans(root);
9861 log_pinned = true;
9862 ret = btrfs_insert_inode_ref(trans, dest,
9863 new_dentry->d_name.name,
9864 new_dentry->d_name.len,
9865 old_ino,
9866 btrfs_ino(new_dir), index);
9867 if (ret)
9868 goto out_fail;
9869 }
9870
9871 inode_inc_iversion(old_dir);
9872 inode_inc_iversion(new_dir);
9873 inode_inc_iversion(old_inode);
9874 old_dir->i_ctime = old_dir->i_mtime =
9875 new_dir->i_ctime = new_dir->i_mtime =
9876 old_inode->i_ctime = current_time(old_dir);
9877
9878 if (old_dentry->d_parent != new_dentry->d_parent)
9879 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
9880
9881 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
9882 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9883 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9884 old_dentry->d_name.name,
9885 old_dentry->d_name.len);
9886 } else {
9887 ret = __btrfs_unlink_inode(trans, root, old_dir,
9888 d_inode(old_dentry),
9889 old_dentry->d_name.name,
9890 old_dentry->d_name.len);
9891 if (!ret)
9892 ret = btrfs_update_inode(trans, root, old_inode);
9893 }
9894 if (ret) {
9895 btrfs_abort_transaction(trans, ret);
9896 goto out_fail;
9897 }
9898
9899 if (new_inode) {
9900 inode_inc_iversion(new_inode);
9901 new_inode->i_ctime = current_time(new_inode);
9902 if (unlikely(btrfs_ino(new_inode) ==
9903 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9904 root_objectid = BTRFS_I(new_inode)->location.objectid;
9905 ret = btrfs_unlink_subvol(trans, dest, new_dir,
9906 root_objectid,
9907 new_dentry->d_name.name,
9908 new_dentry->d_name.len);
9909 BUG_ON(new_inode->i_nlink == 0);
9910 } else {
9911 ret = btrfs_unlink_inode(trans, dest, new_dir,
9912 d_inode(new_dentry),
9913 new_dentry->d_name.name,
9914 new_dentry->d_name.len);
9915 }
9916 if (!ret && new_inode->i_nlink == 0)
9917 ret = btrfs_orphan_add(trans, d_inode(new_dentry));
9918 if (ret) {
9919 btrfs_abort_transaction(trans, ret);
9920 goto out_fail;
9921 }
9922 }
9923
9924 ret = btrfs_add_link(trans, new_dir, old_inode,
9925 new_dentry->d_name.name,
9926 new_dentry->d_name.len, 0, index);
9927 if (ret) {
9928 btrfs_abort_transaction(trans, ret);
9929 goto out_fail;
9930 }
9931
9932 if (old_inode->i_nlink == 1)
9933 BTRFS_I(old_inode)->dir_index = index;
9934
9935 if (log_pinned) {
9936 struct dentry *parent = new_dentry->d_parent;
9937
9938 btrfs_log_new_name(trans, old_inode, old_dir, parent);
9939 btrfs_end_log_trans(root);
9940 log_pinned = false;
9941 }
9942
9943 if (flags & RENAME_WHITEOUT) {
9944 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9945 old_dentry);
9946
9947 if (ret) {
9948 btrfs_abort_transaction(trans, ret);
9949 goto out_fail;
9950 }
9951 }
9952 out_fail:
9953 /*
9954 * If we have pinned the log and an error happened, we unpin tasks
9955 * trying to sync the log and force them to fallback to a transaction
9956 * commit if the log currently contains any of the inodes involved in
9957 * this rename operation (to ensure we do not persist a log with an
9958 * inconsistent state for any of these inodes or leading to any
9959 * inconsistencies when replayed). If the transaction was aborted, the
9960 * abortion reason is propagated to userspace when attempting to commit
9961 * the transaction. If the log does not contain any of these inodes, we
9962 * allow the tasks to sync it.
9963 */
9964 if (ret && log_pinned) {
9965 if (btrfs_inode_in_log(old_dir, fs_info->generation) ||
9966 btrfs_inode_in_log(new_dir, fs_info->generation) ||
9967 btrfs_inode_in_log(old_inode, fs_info->generation) ||
9968 (new_inode &&
9969 btrfs_inode_in_log(new_inode, fs_info->generation)))
9970 btrfs_set_log_full_commit(fs_info, trans);
9971
9972 btrfs_end_log_trans(root);
9973 log_pinned = false;
9974 }
9975 btrfs_end_transaction(trans);
9976 out_notrans:
9977 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
9978 up_read(&fs_info->subvol_sem);
9979
9980 return ret;
9981 }
9982
9983 static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9984 struct inode *new_dir, struct dentry *new_dentry,
9985 unsigned int flags)
9986 {
9987 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
9988 return -EINVAL;
9989
9990 if (flags & RENAME_EXCHANGE)
9991 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9992 new_dentry);
9993
9994 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
9995 }
9996
9997 static void btrfs_run_delalloc_work(struct btrfs_work *work)
9998 {
9999 struct btrfs_delalloc_work *delalloc_work;
10000 struct inode *inode;
10001
10002 delalloc_work = container_of(work, struct btrfs_delalloc_work,
10003 work);
10004 inode = delalloc_work->inode;
10005 filemap_flush(inode->i_mapping);
10006 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
10007 &BTRFS_I(inode)->runtime_flags))
10008 filemap_flush(inode->i_mapping);
10009
10010 if (delalloc_work->delay_iput)
10011 btrfs_add_delayed_iput(inode);
10012 else
10013 iput(inode);
10014 complete(&delalloc_work->completion);
10015 }
10016
10017 struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
10018 int delay_iput)
10019 {
10020 struct btrfs_delalloc_work *work;
10021
10022 work = kmalloc(sizeof(*work), GFP_NOFS);
10023 if (!work)
10024 return NULL;
10025
10026 init_completion(&work->completion);
10027 INIT_LIST_HEAD(&work->list);
10028 work->inode = inode;
10029 work->delay_iput = delay_iput;
10030 WARN_ON_ONCE(!inode);
10031 btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
10032 btrfs_run_delalloc_work, NULL, NULL);
10033
10034 return work;
10035 }
10036
10037 void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
10038 {
10039 wait_for_completion(&work->completion);
10040 kfree(work);
10041 }
10042
10043 /*
10044 * some fairly slow code that needs optimization. This walks the list
10045 * of all the inodes with pending delalloc and forces them to disk.
10046 */
10047 static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
10048 int nr)
10049 {
10050 struct btrfs_inode *binode;
10051 struct inode *inode;
10052 struct btrfs_delalloc_work *work, *next;
10053 struct list_head works;
10054 struct list_head splice;
10055 int ret = 0;
10056
10057 INIT_LIST_HEAD(&works);
10058 INIT_LIST_HEAD(&splice);
10059
10060 mutex_lock(&root->delalloc_mutex);
10061 spin_lock(&root->delalloc_lock);
10062 list_splice_init(&root->delalloc_inodes, &splice);
10063 while (!list_empty(&splice)) {
10064 binode = list_entry(splice.next, struct btrfs_inode,
10065 delalloc_inodes);
10066
10067 list_move_tail(&binode->delalloc_inodes,
10068 &root->delalloc_inodes);
10069 inode = igrab(&binode->vfs_inode);
10070 if (!inode) {
10071 cond_resched_lock(&root->delalloc_lock);
10072 continue;
10073 }
10074 spin_unlock(&root->delalloc_lock);
10075
10076 work = btrfs_alloc_delalloc_work(inode, delay_iput);
10077 if (!work) {
10078 if (delay_iput)
10079 btrfs_add_delayed_iput(inode);
10080 else
10081 iput(inode);
10082 ret = -ENOMEM;
10083 goto out;
10084 }
10085 list_add_tail(&work->list, &works);
10086 btrfs_queue_work(root->fs_info->flush_workers,
10087 &work->work);
10088 ret++;
10089 if (nr != -1 && ret >= nr)
10090 goto out;
10091 cond_resched();
10092 spin_lock(&root->delalloc_lock);
10093 }
10094 spin_unlock(&root->delalloc_lock);
10095
10096 out:
10097 list_for_each_entry_safe(work, next, &works, list) {
10098 list_del_init(&work->list);
10099 btrfs_wait_and_free_delalloc_work(work);
10100 }
10101
10102 if (!list_empty_careful(&splice)) {
10103 spin_lock(&root->delalloc_lock);
10104 list_splice_tail(&splice, &root->delalloc_inodes);
10105 spin_unlock(&root->delalloc_lock);
10106 }
10107 mutex_unlock(&root->delalloc_mutex);
10108 return ret;
10109 }
10110
10111 int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10112 {
10113 struct btrfs_fs_info *fs_info = root->fs_info;
10114 int ret;
10115
10116 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
10117 return -EROFS;
10118
10119 ret = __start_delalloc_inodes(root, delay_iput, -1);
10120 if (ret > 0)
10121 ret = 0;
10122 /*
10123 * the filemap_flush will queue IO into the worker threads, but
10124 * we have to make sure the IO is actually started and that
10125 * ordered extents get created before we return
10126 */
10127 atomic_inc(&fs_info->async_submit_draining);
10128 while (atomic_read(&fs_info->nr_async_submits) ||
10129 atomic_read(&fs_info->async_delalloc_pages)) {
10130 wait_event(fs_info->async_submit_wait,
10131 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10132 atomic_read(&fs_info->async_delalloc_pages) == 0));
10133 }
10134 atomic_dec(&fs_info->async_submit_draining);
10135 return ret;
10136 }
10137
10138 int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10139 int nr)
10140 {
10141 struct btrfs_root *root;
10142 struct list_head splice;
10143 int ret;
10144
10145 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
10146 return -EROFS;
10147
10148 INIT_LIST_HEAD(&splice);
10149
10150 mutex_lock(&fs_info->delalloc_root_mutex);
10151 spin_lock(&fs_info->delalloc_root_lock);
10152 list_splice_init(&fs_info->delalloc_roots, &splice);
10153 while (!list_empty(&splice) && nr) {
10154 root = list_first_entry(&splice, struct btrfs_root,
10155 delalloc_root);
10156 root = btrfs_grab_fs_root(root);
10157 BUG_ON(!root);
10158 list_move_tail(&root->delalloc_root,
10159 &fs_info->delalloc_roots);
10160 spin_unlock(&fs_info->delalloc_root_lock);
10161
10162 ret = __start_delalloc_inodes(root, delay_iput, nr);
10163 btrfs_put_fs_root(root);
10164 if (ret < 0)
10165 goto out;
10166
10167 if (nr != -1) {
10168 nr -= ret;
10169 WARN_ON(nr < 0);
10170 }
10171 spin_lock(&fs_info->delalloc_root_lock);
10172 }
10173 spin_unlock(&fs_info->delalloc_root_lock);
10174
10175 ret = 0;
10176 atomic_inc(&fs_info->async_submit_draining);
10177 while (atomic_read(&fs_info->nr_async_submits) ||
10178 atomic_read(&fs_info->async_delalloc_pages)) {
10179 wait_event(fs_info->async_submit_wait,
10180 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10181 atomic_read(&fs_info->async_delalloc_pages) == 0));
10182 }
10183 atomic_dec(&fs_info->async_submit_draining);
10184 out:
10185 if (!list_empty_careful(&splice)) {
10186 spin_lock(&fs_info->delalloc_root_lock);
10187 list_splice_tail(&splice, &fs_info->delalloc_roots);
10188 spin_unlock(&fs_info->delalloc_root_lock);
10189 }
10190 mutex_unlock(&fs_info->delalloc_root_mutex);
10191 return ret;
10192 }
10193
10194 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10195 const char *symname)
10196 {
10197 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
10198 struct btrfs_trans_handle *trans;
10199 struct btrfs_root *root = BTRFS_I(dir)->root;
10200 struct btrfs_path *path;
10201 struct btrfs_key key;
10202 struct inode *inode = NULL;
10203 int err;
10204 int drop_inode = 0;
10205 u64 objectid;
10206 u64 index = 0;
10207 int name_len;
10208 int datasize;
10209 unsigned long ptr;
10210 struct btrfs_file_extent_item *ei;
10211 struct extent_buffer *leaf;
10212
10213 name_len = strlen(symname);
10214 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
10215 return -ENAMETOOLONG;
10216
10217 /*
10218 * 2 items for inode item and ref
10219 * 2 items for dir items
10220 * 1 item for updating parent inode item
10221 * 1 item for the inline extent item
10222 * 1 item for xattr if selinux is on
10223 */
10224 trans = btrfs_start_transaction(root, 7);
10225 if (IS_ERR(trans))
10226 return PTR_ERR(trans);
10227
10228 err = btrfs_find_free_ino(root, &objectid);
10229 if (err)
10230 goto out_unlock;
10231
10232 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
10233 dentry->d_name.len, btrfs_ino(dir), objectid,
10234 S_IFLNK|S_IRWXUGO, &index);
10235 if (IS_ERR(inode)) {
10236 err = PTR_ERR(inode);
10237 goto out_unlock;
10238 }
10239
10240 /*
10241 * If the active LSM wants to access the inode during
10242 * d_instantiate it needs these. Smack checks to see
10243 * if the filesystem supports xattrs by looking at the
10244 * ops vector.
10245 */
10246 inode->i_fop = &btrfs_file_operations;
10247 inode->i_op = &btrfs_file_inode_operations;
10248 inode->i_mapping->a_ops = &btrfs_aops;
10249 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10250
10251 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10252 if (err)
10253 goto out_unlock_inode;
10254
10255 path = btrfs_alloc_path();
10256 if (!path) {
10257 err = -ENOMEM;
10258 goto out_unlock_inode;
10259 }
10260 key.objectid = btrfs_ino(inode);
10261 key.offset = 0;
10262 key.type = BTRFS_EXTENT_DATA_KEY;
10263 datasize = btrfs_file_extent_calc_inline_size(name_len);
10264 err = btrfs_insert_empty_item(trans, root, path, &key,
10265 datasize);
10266 if (err) {
10267 btrfs_free_path(path);
10268 goto out_unlock_inode;
10269 }
10270 leaf = path->nodes[0];
10271 ei = btrfs_item_ptr(leaf, path->slots[0],
10272 struct btrfs_file_extent_item);
10273 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10274 btrfs_set_file_extent_type(leaf, ei,
10275 BTRFS_FILE_EXTENT_INLINE);
10276 btrfs_set_file_extent_encryption(leaf, ei, 0);
10277 btrfs_set_file_extent_compression(leaf, ei, 0);
10278 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10279 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10280
10281 ptr = btrfs_file_extent_inline_start(ei);
10282 write_extent_buffer(leaf, symname, ptr, name_len);
10283 btrfs_mark_buffer_dirty(leaf);
10284 btrfs_free_path(path);
10285
10286 inode->i_op = &btrfs_symlink_inode_operations;
10287 inode_nohighmem(inode);
10288 inode->i_mapping->a_ops = &btrfs_symlink_aops;
10289 inode_set_bytes(inode, name_len);
10290 btrfs_i_size_write(inode, name_len);
10291 err = btrfs_update_inode(trans, root, inode);
10292 /*
10293 * Last step, add directory indexes for our symlink inode. This is the
10294 * last step to avoid extra cleanup of these indexes if an error happens
10295 * elsewhere above.
10296 */
10297 if (!err)
10298 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
10299 if (err) {
10300 drop_inode = 1;
10301 goto out_unlock_inode;
10302 }
10303
10304 unlock_new_inode(inode);
10305 d_instantiate(dentry, inode);
10306
10307 out_unlock:
10308 btrfs_end_transaction(trans);
10309 if (drop_inode) {
10310 inode_dec_link_count(inode);
10311 iput(inode);
10312 }
10313 btrfs_btree_balance_dirty(fs_info);
10314 return err;
10315
10316 out_unlock_inode:
10317 drop_inode = 1;
10318 unlock_new_inode(inode);
10319 goto out_unlock;
10320 }
10321
10322 static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10323 u64 start, u64 num_bytes, u64 min_size,
10324 loff_t actual_len, u64 *alloc_hint,
10325 struct btrfs_trans_handle *trans)
10326 {
10327 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
10328 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10329 struct extent_map *em;
10330 struct btrfs_root *root = BTRFS_I(inode)->root;
10331 struct btrfs_key ins;
10332 u64 cur_offset = start;
10333 u64 i_size;
10334 u64 cur_bytes;
10335 u64 last_alloc = (u64)-1;
10336 int ret = 0;
10337 bool own_trans = true;
10338 u64 end = start + num_bytes - 1;
10339
10340 if (trans)
10341 own_trans = false;
10342 while (num_bytes > 0) {
10343 if (own_trans) {
10344 trans = btrfs_start_transaction(root, 3);
10345 if (IS_ERR(trans)) {
10346 ret = PTR_ERR(trans);
10347 break;
10348 }
10349 }
10350
10351 cur_bytes = min_t(u64, num_bytes, SZ_256M);
10352 cur_bytes = max(cur_bytes, min_size);
10353 /*
10354 * If we are severely fragmented we could end up with really
10355 * small allocations, so if the allocator is returning small
10356 * chunks lets make its job easier by only searching for those
10357 * sized chunks.
10358 */
10359 cur_bytes = min(cur_bytes, last_alloc);
10360 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10361 min_size, 0, *alloc_hint, &ins, 1, 0);
10362 if (ret) {
10363 if (own_trans)
10364 btrfs_end_transaction(trans);
10365 break;
10366 }
10367 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
10368
10369 last_alloc = ins.offset;
10370 ret = insert_reserved_file_extent(trans, inode,
10371 cur_offset, ins.objectid,
10372 ins.offset, ins.offset,
10373 ins.offset, 0, 0, 0,
10374 BTRFS_FILE_EXTENT_PREALLOC);
10375 if (ret) {
10376 btrfs_free_reserved_extent(fs_info, ins.objectid,
10377 ins.offset, 0);
10378 btrfs_abort_transaction(trans, ret);
10379 if (own_trans)
10380 btrfs_end_transaction(trans);
10381 break;
10382 }
10383
10384 btrfs_drop_extent_cache(inode, cur_offset,
10385 cur_offset + ins.offset -1, 0);
10386
10387 em = alloc_extent_map();
10388 if (!em) {
10389 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10390 &BTRFS_I(inode)->runtime_flags);
10391 goto next;
10392 }
10393
10394 em->start = cur_offset;
10395 em->orig_start = cur_offset;
10396 em->len = ins.offset;
10397 em->block_start = ins.objectid;
10398 em->block_len = ins.offset;
10399 em->orig_block_len = ins.offset;
10400 em->ram_bytes = ins.offset;
10401 em->bdev = fs_info->fs_devices->latest_bdev;
10402 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10403 em->generation = trans->transid;
10404
10405 while (1) {
10406 write_lock(&em_tree->lock);
10407 ret = add_extent_mapping(em_tree, em, 1);
10408 write_unlock(&em_tree->lock);
10409 if (ret != -EEXIST)
10410 break;
10411 btrfs_drop_extent_cache(inode, cur_offset,
10412 cur_offset + ins.offset - 1,
10413 0);
10414 }
10415 free_extent_map(em);
10416 next:
10417 num_bytes -= ins.offset;
10418 cur_offset += ins.offset;
10419 *alloc_hint = ins.objectid + ins.offset;
10420
10421 inode_inc_iversion(inode);
10422 inode->i_ctime = current_time(inode);
10423 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
10424 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
10425 (actual_len > inode->i_size) &&
10426 (cur_offset > inode->i_size)) {
10427 if (cur_offset > actual_len)
10428 i_size = actual_len;
10429 else
10430 i_size = cur_offset;
10431 i_size_write(inode, i_size);
10432 btrfs_ordered_update_i_size(inode, i_size, NULL);
10433 }
10434
10435 ret = btrfs_update_inode(trans, root, inode);
10436
10437 if (ret) {
10438 btrfs_abort_transaction(trans, ret);
10439 if (own_trans)
10440 btrfs_end_transaction(trans);
10441 break;
10442 }
10443
10444 if (own_trans)
10445 btrfs_end_transaction(trans);
10446 }
10447 if (cur_offset < end)
10448 btrfs_free_reserved_data_space(inode, cur_offset,
10449 end - cur_offset + 1);
10450 return ret;
10451 }
10452
10453 int btrfs_prealloc_file_range(struct inode *inode, int mode,
10454 u64 start, u64 num_bytes, u64 min_size,
10455 loff_t actual_len, u64 *alloc_hint)
10456 {
10457 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10458 min_size, actual_len, alloc_hint,
10459 NULL);
10460 }
10461
10462 int btrfs_prealloc_file_range_trans(struct inode *inode,
10463 struct btrfs_trans_handle *trans, int mode,
10464 u64 start, u64 num_bytes, u64 min_size,
10465 loff_t actual_len, u64 *alloc_hint)
10466 {
10467 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10468 min_size, actual_len, alloc_hint, trans);
10469 }
10470
10471 static int btrfs_set_page_dirty(struct page *page)
10472 {
10473 return __set_page_dirty_nobuffers(page);
10474 }
10475
10476 static int btrfs_permission(struct inode *inode, int mask)
10477 {
10478 struct btrfs_root *root = BTRFS_I(inode)->root;
10479 umode_t mode = inode->i_mode;
10480
10481 if (mask & MAY_WRITE &&
10482 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10483 if (btrfs_root_readonly(root))
10484 return -EROFS;
10485 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10486 return -EACCES;
10487 }
10488 return generic_permission(inode, mask);
10489 }
10490
10491 static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10492 {
10493 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
10494 struct btrfs_trans_handle *trans;
10495 struct btrfs_root *root = BTRFS_I(dir)->root;
10496 struct inode *inode = NULL;
10497 u64 objectid;
10498 u64 index;
10499 int ret = 0;
10500
10501 /*
10502 * 5 units required for adding orphan entry
10503 */
10504 trans = btrfs_start_transaction(root, 5);
10505 if (IS_ERR(trans))
10506 return PTR_ERR(trans);
10507
10508 ret = btrfs_find_free_ino(root, &objectid);
10509 if (ret)
10510 goto out;
10511
10512 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
10513 btrfs_ino(dir), objectid, mode, &index);
10514 if (IS_ERR(inode)) {
10515 ret = PTR_ERR(inode);
10516 inode = NULL;
10517 goto out;
10518 }
10519
10520 inode->i_fop = &btrfs_file_operations;
10521 inode->i_op = &btrfs_file_inode_operations;
10522
10523 inode->i_mapping->a_ops = &btrfs_aops;
10524 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10525
10526 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10527 if (ret)
10528 goto out_inode;
10529
10530 ret = btrfs_update_inode(trans, root, inode);
10531 if (ret)
10532 goto out_inode;
10533 ret = btrfs_orphan_add(trans, inode);
10534 if (ret)
10535 goto out_inode;
10536
10537 /*
10538 * We set number of links to 0 in btrfs_new_inode(), and here we set
10539 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10540 * through:
10541 *
10542 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10543 */
10544 set_nlink(inode, 1);
10545 unlock_new_inode(inode);
10546 d_tmpfile(dentry, inode);
10547 mark_inode_dirty(inode);
10548
10549 out:
10550 btrfs_end_transaction(trans);
10551 if (ret)
10552 iput(inode);
10553 btrfs_balance_delayed_items(fs_info);
10554 btrfs_btree_balance_dirty(fs_info);
10555 return ret;
10556
10557 out_inode:
10558 unlock_new_inode(inode);
10559 goto out;
10560
10561 }
10562
10563 static const struct inode_operations btrfs_dir_inode_operations = {
10564 .getattr = btrfs_getattr,
10565 .lookup = btrfs_lookup,
10566 .create = btrfs_create,
10567 .unlink = btrfs_unlink,
10568 .link = btrfs_link,
10569 .mkdir = btrfs_mkdir,
10570 .rmdir = btrfs_rmdir,
10571 .rename = btrfs_rename2,
10572 .symlink = btrfs_symlink,
10573 .setattr = btrfs_setattr,
10574 .mknod = btrfs_mknod,
10575 .listxattr = btrfs_listxattr,
10576 .permission = btrfs_permission,
10577 .get_acl = btrfs_get_acl,
10578 .set_acl = btrfs_set_acl,
10579 .update_time = btrfs_update_time,
10580 .tmpfile = btrfs_tmpfile,
10581 };
10582 static const struct inode_operations btrfs_dir_ro_inode_operations = {
10583 .lookup = btrfs_lookup,
10584 .permission = btrfs_permission,
10585 .update_time = btrfs_update_time,
10586 };
10587
10588 static const struct file_operations btrfs_dir_file_operations = {
10589 .llseek = generic_file_llseek,
10590 .read = generic_read_dir,
10591 .iterate_shared = btrfs_real_readdir,
10592 .unlocked_ioctl = btrfs_ioctl,
10593 #ifdef CONFIG_COMPAT
10594 .compat_ioctl = btrfs_compat_ioctl,
10595 #endif
10596 .release = btrfs_release_file,
10597 .fsync = btrfs_sync_file,
10598 };
10599
10600 static const struct extent_io_ops btrfs_extent_io_ops = {
10601 .fill_delalloc = run_delalloc_range,
10602 .submit_bio_hook = btrfs_submit_bio_hook,
10603 .merge_bio_hook = btrfs_merge_bio_hook,
10604 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
10605 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
10606 .writepage_start_hook = btrfs_writepage_start_hook,
10607 .set_bit_hook = btrfs_set_bit_hook,
10608 .clear_bit_hook = btrfs_clear_bit_hook,
10609 .merge_extent_hook = btrfs_merge_extent_hook,
10610 .split_extent_hook = btrfs_split_extent_hook,
10611 };
10612
10613 /*
10614 * btrfs doesn't support the bmap operation because swapfiles
10615 * use bmap to make a mapping of extents in the file. They assume
10616 * these extents won't change over the life of the file and they
10617 * use the bmap result to do IO directly to the drive.
10618 *
10619 * the btrfs bmap call would return logical addresses that aren't
10620 * suitable for IO and they also will change frequently as COW
10621 * operations happen. So, swapfile + btrfs == corruption.
10622 *
10623 * For now we're avoiding this by dropping bmap.
10624 */
10625 static const struct address_space_operations btrfs_aops = {
10626 .readpage = btrfs_readpage,
10627 .writepage = btrfs_writepage,
10628 .writepages = btrfs_writepages,
10629 .readpages = btrfs_readpages,
10630 .direct_IO = btrfs_direct_IO,
10631 .invalidatepage = btrfs_invalidatepage,
10632 .releasepage = btrfs_releasepage,
10633 .set_page_dirty = btrfs_set_page_dirty,
10634 .error_remove_page = generic_error_remove_page,
10635 };
10636
10637 static const struct address_space_operations btrfs_symlink_aops = {
10638 .readpage = btrfs_readpage,
10639 .writepage = btrfs_writepage,
10640 .invalidatepage = btrfs_invalidatepage,
10641 .releasepage = btrfs_releasepage,
10642 };
10643
10644 static const struct inode_operations btrfs_file_inode_operations = {
10645 .getattr = btrfs_getattr,
10646 .setattr = btrfs_setattr,
10647 .listxattr = btrfs_listxattr,
10648 .permission = btrfs_permission,
10649 .fiemap = btrfs_fiemap,
10650 .get_acl = btrfs_get_acl,
10651 .set_acl = btrfs_set_acl,
10652 .update_time = btrfs_update_time,
10653 };
10654 static const struct inode_operations btrfs_special_inode_operations = {
10655 .getattr = btrfs_getattr,
10656 .setattr = btrfs_setattr,
10657 .permission = btrfs_permission,
10658 .listxattr = btrfs_listxattr,
10659 .get_acl = btrfs_get_acl,
10660 .set_acl = btrfs_set_acl,
10661 .update_time = btrfs_update_time,
10662 };
10663 static const struct inode_operations btrfs_symlink_inode_operations = {
10664 .get_link = page_get_link,
10665 .getattr = btrfs_getattr,
10666 .setattr = btrfs_setattr,
10667 .permission = btrfs_permission,
10668 .listxattr = btrfs_listxattr,
10669 .update_time = btrfs_update_time,
10670 };
10671
10672 const struct dentry_operations btrfs_dentry_operations = {
10673 .d_delete = btrfs_dentry_delete,
10674 .d_release = btrfs_dentry_release,
10675 };