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