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