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