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