Btrfs: convert to the new truncate sequence
[linux-2.6-block.git] / fs / btrfs / inode.c
CommitLineData
6cbd5570
CM
1/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
8f18cf13 19#include <linux/kernel.h>
065631f6 20#include <linux/bio.h>
39279cc3 21#include <linux/buffer_head.h>
f2eb0a24 22#include <linux/file.h>
39279cc3
CM
23#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
39279cc3
CM
29#include <linux/backing-dev.h>
30#include <linux/mpage.h>
31#include <linux/swap.h>
32#include <linux/writeback.h>
33#include <linux/statfs.h>
34#include <linux/compat.h>
9ebefb18 35#include <linux/bit_spinlock.h>
5103e947 36#include <linux/xattr.h>
33268eaf 37#include <linux/posix_acl.h>
d899e052 38#include <linux/falloc.h>
5a0e3ad6 39#include <linux/slab.h>
4b4e25f2 40#include "compat.h"
39279cc3
CM
41#include "ctree.h"
42#include "disk-io.h"
43#include "transaction.h"
44#include "btrfs_inode.h"
45#include "ioctl.h"
46#include "print-tree.h"
0b86a832 47#include "volumes.h"
e6dcd2dc 48#include "ordered-data.h"
95819c05 49#include "xattr.h"
e02119d5 50#include "tree-log.h"
c8b97818 51#include "compression.h"
b4ce94de 52#include "locking.h"
dc89e982 53#include "free-space-cache.h"
39279cc3
CM
54
55struct btrfs_iget_args {
56 u64 ino;
57 struct btrfs_root *root;
58};
59
6e1d5dcc
AD
60static const struct inode_operations btrfs_dir_inode_operations;
61static const struct inode_operations btrfs_symlink_inode_operations;
62static const struct inode_operations btrfs_dir_ro_inode_operations;
63static const struct inode_operations btrfs_special_inode_operations;
64static const struct inode_operations btrfs_file_inode_operations;
7f09410b
AD
65static const struct address_space_operations btrfs_aops;
66static const struct address_space_operations btrfs_symlink_aops;
828c0950 67static const struct file_operations btrfs_dir_file_operations;
d1310b2e 68static struct extent_io_ops btrfs_extent_io_ops;
39279cc3
CM
69
70static struct kmem_cache *btrfs_inode_cachep;
71struct kmem_cache *btrfs_trans_handle_cachep;
72struct kmem_cache *btrfs_transaction_cachep;
39279cc3 73struct kmem_cache *btrfs_path_cachep;
dc89e982 74struct kmem_cache *btrfs_free_space_cachep;
39279cc3
CM
75
76#define S_SHIFT 12
77static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
78 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
79 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
80 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
81 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
82 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
83 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
84 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
85};
86
a41ad394
JB
87static int btrfs_setsize(struct inode *inode, loff_t newsize);
88static int btrfs_truncate(struct inode *inode);
c8b97818 89static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end);
771ed689
CM
90static noinline int cow_file_range(struct inode *inode,
91 struct page *locked_page,
92 u64 start, u64 end, int *page_started,
93 unsigned long *nr_written, int unlock);
7b128766 94
f34f57a3
YZ
95static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
96 struct inode *inode, struct inode *dir)
0279b4cd
JO
97{
98 int err;
99
f34f57a3 100 err = btrfs_init_acl(trans, inode, dir);
0279b4cd 101 if (!err)
f34f57a3 102 err = btrfs_xattr_security_init(trans, inode, dir);
0279b4cd
JO
103 return err;
104}
105
c8b97818
CM
106/*
107 * this does all the hard work for inserting an inline extent into
108 * the btree. The caller should have done a btrfs_drop_extents so that
109 * no overlapping inline items exist in the btree
110 */
d397712b 111static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
c8b97818
CM
112 struct btrfs_root *root, struct inode *inode,
113 u64 start, size_t size, size_t compressed_size,
114 struct page **compressed_pages)
115{
116 struct btrfs_key key;
117 struct btrfs_path *path;
118 struct extent_buffer *leaf;
119 struct page *page = NULL;
120 char *kaddr;
121 unsigned long ptr;
122 struct btrfs_file_extent_item *ei;
123 int err = 0;
124 int ret;
125 size_t cur_size = size;
126 size_t datasize;
127 unsigned long offset;
261507a0 128 int compress_type = BTRFS_COMPRESS_NONE;
c8b97818
CM
129
130 if (compressed_size && compressed_pages) {
261507a0 131 compress_type = root->fs_info->compress_type;
c8b97818
CM
132 cur_size = compressed_size;
133 }
134
d397712b
CM
135 path = btrfs_alloc_path();
136 if (!path)
c8b97818
CM
137 return -ENOMEM;
138
b9473439 139 path->leave_spinning = 1;
c8b97818
CM
140 btrfs_set_trans_block_group(trans, inode);
141
142 key.objectid = inode->i_ino;
143 key.offset = start;
144 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
c8b97818
CM
145 datasize = btrfs_file_extent_calc_inline_size(cur_size);
146
147 inode_add_bytes(inode, size);
148 ret = btrfs_insert_empty_item(trans, root, path, &key,
149 datasize);
150 BUG_ON(ret);
151 if (ret) {
152 err = ret;
c8b97818
CM
153 goto fail;
154 }
155 leaf = path->nodes[0];
156 ei = btrfs_item_ptr(leaf, path->slots[0],
157 struct btrfs_file_extent_item);
158 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
159 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
160 btrfs_set_file_extent_encryption(leaf, ei, 0);
161 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
162 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
163 ptr = btrfs_file_extent_inline_start(ei);
164
261507a0 165 if (compress_type != BTRFS_COMPRESS_NONE) {
c8b97818
CM
166 struct page *cpage;
167 int i = 0;
d397712b 168 while (compressed_size > 0) {
c8b97818 169 cpage = compressed_pages[i];
5b050f04 170 cur_size = min_t(unsigned long, compressed_size,
c8b97818
CM
171 PAGE_CACHE_SIZE);
172
b9473439 173 kaddr = kmap_atomic(cpage, KM_USER0);
c8b97818 174 write_extent_buffer(leaf, kaddr, ptr, cur_size);
b9473439 175 kunmap_atomic(kaddr, KM_USER0);
c8b97818
CM
176
177 i++;
178 ptr += cur_size;
179 compressed_size -= cur_size;
180 }
181 btrfs_set_file_extent_compression(leaf, ei,
261507a0 182 compress_type);
c8b97818
CM
183 } else {
184 page = find_get_page(inode->i_mapping,
185 start >> PAGE_CACHE_SHIFT);
186 btrfs_set_file_extent_compression(leaf, ei, 0);
187 kaddr = kmap_atomic(page, KM_USER0);
188 offset = start & (PAGE_CACHE_SIZE - 1);
189 write_extent_buffer(leaf, kaddr + offset, ptr, size);
190 kunmap_atomic(kaddr, KM_USER0);
191 page_cache_release(page);
192 }
193 btrfs_mark_buffer_dirty(leaf);
194 btrfs_free_path(path);
195
c2167754
YZ
196 /*
197 * we're an inline extent, so nobody can
198 * extend the file past i_size without locking
199 * a page we already have locked.
200 *
201 * We must do any isize and inode updates
202 * before we unlock the pages. Otherwise we
203 * could end up racing with unlink.
204 */
c8b97818
CM
205 BTRFS_I(inode)->disk_i_size = inode->i_size;
206 btrfs_update_inode(trans, root, inode);
c2167754 207
c8b97818
CM
208 return 0;
209fail:
210 btrfs_free_path(path);
211 return err;
212}
213
214
215/*
216 * conditionally insert an inline extent into the file. This
217 * does the checks required to make sure the data is small enough
218 * to fit as an inline extent.
219 */
7f366cfe 220static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
c8b97818
CM
221 struct btrfs_root *root,
222 struct inode *inode, u64 start, u64 end,
223 size_t compressed_size,
224 struct page **compressed_pages)
225{
226 u64 isize = i_size_read(inode);
227 u64 actual_end = min(end + 1, isize);
228 u64 inline_len = actual_end - start;
229 u64 aligned_end = (end + root->sectorsize - 1) &
230 ~((u64)root->sectorsize - 1);
231 u64 hint_byte;
232 u64 data_len = inline_len;
233 int ret;
234
235 if (compressed_size)
236 data_len = compressed_size;
237
238 if (start > 0 ||
70b99e69 239 actual_end >= PAGE_CACHE_SIZE ||
c8b97818
CM
240 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
241 (!compressed_size &&
242 (actual_end & (root->sectorsize - 1)) == 0) ||
243 end + 1 < isize ||
244 data_len > root->fs_info->max_inline) {
245 return 1;
246 }
247
920bbbfb 248 ret = btrfs_drop_extents(trans, inode, start, aligned_end,
a1ed835e 249 &hint_byte, 1);
c8b97818
CM
250 BUG_ON(ret);
251
252 if (isize > actual_end)
253 inline_len = min_t(u64, isize, actual_end);
254 ret = insert_inline_extent(trans, root, inode, start,
255 inline_len, compressed_size,
256 compressed_pages);
257 BUG_ON(ret);
0ca1f7ce 258 btrfs_delalloc_release_metadata(inode, end + 1 - start);
a1ed835e 259 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
c8b97818
CM
260 return 0;
261}
262
771ed689
CM
263struct async_extent {
264 u64 start;
265 u64 ram_size;
266 u64 compressed_size;
267 struct page **pages;
268 unsigned long nr_pages;
261507a0 269 int compress_type;
771ed689
CM
270 struct list_head list;
271};
272
273struct async_cow {
274 struct inode *inode;
275 struct btrfs_root *root;
276 struct page *locked_page;
277 u64 start;
278 u64 end;
279 struct list_head extents;
280 struct btrfs_work work;
281};
282
283static noinline int add_async_extent(struct async_cow *cow,
284 u64 start, u64 ram_size,
285 u64 compressed_size,
286 struct page **pages,
261507a0
LZ
287 unsigned long nr_pages,
288 int compress_type)
771ed689
CM
289{
290 struct async_extent *async_extent;
291
292 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
293 async_extent->start = start;
294 async_extent->ram_size = ram_size;
295 async_extent->compressed_size = compressed_size;
296 async_extent->pages = pages;
297 async_extent->nr_pages = nr_pages;
261507a0 298 async_extent->compress_type = compress_type;
771ed689
CM
299 list_add_tail(&async_extent->list, &cow->extents);
300 return 0;
301}
302
d352ac68 303/*
771ed689
CM
304 * we create compressed extents in two phases. The first
305 * phase compresses a range of pages that have already been
306 * locked (both pages and state bits are locked).
c8b97818 307 *
771ed689
CM
308 * This is done inside an ordered work queue, and the compression
309 * is spread across many cpus. The actual IO submission is step
310 * two, and the ordered work queue takes care of making sure that
311 * happens in the same order things were put onto the queue by
312 * writepages and friends.
c8b97818 313 *
771ed689
CM
314 * If this code finds it can't get good compression, it puts an
315 * entry onto the work queue to write the uncompressed bytes. This
316 * makes sure that both compressed inodes and uncompressed inodes
317 * are written in the same order that pdflush sent them down.
d352ac68 318 */
771ed689
CM
319static noinline int compress_file_range(struct inode *inode,
320 struct page *locked_page,
321 u64 start, u64 end,
322 struct async_cow *async_cow,
323 int *num_added)
b888db2b
CM
324{
325 struct btrfs_root *root = BTRFS_I(inode)->root;
326 struct btrfs_trans_handle *trans;
db94535d 327 u64 num_bytes;
db94535d 328 u64 blocksize = root->sectorsize;
c8b97818 329 u64 actual_end;
42dc7bab 330 u64 isize = i_size_read(inode);
e6dcd2dc 331 int ret = 0;
c8b97818
CM
332 struct page **pages = NULL;
333 unsigned long nr_pages;
334 unsigned long nr_pages_ret = 0;
335 unsigned long total_compressed = 0;
336 unsigned long total_in = 0;
337 unsigned long max_compressed = 128 * 1024;
771ed689 338 unsigned long max_uncompressed = 128 * 1024;
c8b97818
CM
339 int i;
340 int will_compress;
261507a0 341 int compress_type = root->fs_info->compress_type;
b888db2b 342
42dc7bab 343 actual_end = min_t(u64, isize, end + 1);
c8b97818
CM
344again:
345 will_compress = 0;
346 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
347 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
be20aa9d 348
f03d9301
CM
349 /*
350 * we don't want to send crud past the end of i_size through
351 * compression, that's just a waste of CPU time. So, if the
352 * end of the file is before the start of our current
353 * requested range of bytes, we bail out to the uncompressed
354 * cleanup code that can deal with all of this.
355 *
356 * It isn't really the fastest way to fix things, but this is a
357 * very uncommon corner.
358 */
359 if (actual_end <= start)
360 goto cleanup_and_bail_uncompressed;
361
c8b97818
CM
362 total_compressed = actual_end - start;
363
364 /* we want to make sure that amount of ram required to uncompress
365 * an extent is reasonable, so we limit the total size in ram
771ed689
CM
366 * of a compressed extent to 128k. This is a crucial number
367 * because it also controls how easily we can spread reads across
368 * cpus for decompression.
369 *
370 * We also want to make sure the amount of IO required to do
371 * a random read is reasonably small, so we limit the size of
372 * a compressed extent to 128k.
c8b97818
CM
373 */
374 total_compressed = min(total_compressed, max_uncompressed);
db94535d 375 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
be20aa9d 376 num_bytes = max(blocksize, num_bytes);
c8b97818
CM
377 total_in = 0;
378 ret = 0;
db94535d 379
771ed689
CM
380 /*
381 * we do compression for mount -o compress and when the
382 * inode has not been flagged as nocompress. This flag can
383 * change at any time if we discover bad compression ratios.
c8b97818 384 */
6cbff00f 385 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
1e701a32
CM
386 (btrfs_test_opt(root, COMPRESS) ||
387 (BTRFS_I(inode)->force_compress))) {
c8b97818 388 WARN_ON(pages);
cfbc246e 389 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
c8b97818 390
261507a0
LZ
391 if (BTRFS_I(inode)->force_compress)
392 compress_type = BTRFS_I(inode)->force_compress;
393
394 ret = btrfs_compress_pages(compress_type,
395 inode->i_mapping, start,
396 total_compressed, pages,
397 nr_pages, &nr_pages_ret,
398 &total_in,
399 &total_compressed,
400 max_compressed);
c8b97818
CM
401
402 if (!ret) {
403 unsigned long offset = total_compressed &
404 (PAGE_CACHE_SIZE - 1);
405 struct page *page = pages[nr_pages_ret - 1];
406 char *kaddr;
407
408 /* zero the tail end of the last page, we might be
409 * sending it down to disk
410 */
411 if (offset) {
412 kaddr = kmap_atomic(page, KM_USER0);
413 memset(kaddr + offset, 0,
414 PAGE_CACHE_SIZE - offset);
415 kunmap_atomic(kaddr, KM_USER0);
416 }
417 will_compress = 1;
418 }
419 }
420 if (start == 0) {
771ed689 421 trans = btrfs_join_transaction(root, 1);
3612b495 422 BUG_ON(IS_ERR(trans));
771ed689 423 btrfs_set_trans_block_group(trans, inode);
0ca1f7ce 424 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
771ed689 425
c8b97818 426 /* lets try to make an inline extent */
771ed689 427 if (ret || total_in < (actual_end - start)) {
c8b97818 428 /* we didn't compress the entire range, try
771ed689 429 * to make an uncompressed inline extent.
c8b97818
CM
430 */
431 ret = cow_file_range_inline(trans, root, inode,
432 start, end, 0, NULL);
433 } else {
771ed689 434 /* try making a compressed inline extent */
c8b97818
CM
435 ret = cow_file_range_inline(trans, root, inode,
436 start, end,
437 total_compressed, pages);
438 }
439 if (ret == 0) {
771ed689
CM
440 /*
441 * inline extent creation worked, we don't need
442 * to create any more async work items. Unlock
443 * and free up our temp pages.
444 */
c8b97818 445 extent_clear_unlock_delalloc(inode,
a791e35e
CM
446 &BTRFS_I(inode)->io_tree,
447 start, end, NULL,
448 EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
a3429ab7 449 EXTENT_CLEAR_DELALLOC |
a791e35e 450 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
c2167754
YZ
451
452 btrfs_end_transaction(trans, root);
c8b97818
CM
453 goto free_pages_out;
454 }
c2167754 455 btrfs_end_transaction(trans, root);
c8b97818
CM
456 }
457
458 if (will_compress) {
459 /*
460 * we aren't doing an inline extent round the compressed size
461 * up to a block size boundary so the allocator does sane
462 * things
463 */
464 total_compressed = (total_compressed + blocksize - 1) &
465 ~(blocksize - 1);
466
467 /*
468 * one last check to make sure the compression is really a
469 * win, compare the page count read with the blocks on disk
470 */
471 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
472 ~(PAGE_CACHE_SIZE - 1);
473 if (total_compressed >= total_in) {
474 will_compress = 0;
475 } else {
c8b97818
CM
476 num_bytes = total_in;
477 }
478 }
479 if (!will_compress && pages) {
480 /*
481 * the compression code ran but failed to make things smaller,
482 * free any pages it allocated and our page pointer array
483 */
484 for (i = 0; i < nr_pages_ret; i++) {
70b99e69 485 WARN_ON(pages[i]->mapping);
c8b97818
CM
486 page_cache_release(pages[i]);
487 }
488 kfree(pages);
489 pages = NULL;
490 total_compressed = 0;
491 nr_pages_ret = 0;
492
493 /* flag the file so we don't compress in the future */
1e701a32
CM
494 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
495 !(BTRFS_I(inode)->force_compress)) {
a555f810 496 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
1e701a32 497 }
c8b97818 498 }
771ed689
CM
499 if (will_compress) {
500 *num_added += 1;
c8b97818 501
771ed689
CM
502 /* the async work queues will take care of doing actual
503 * allocation on disk for these compressed pages,
504 * and will submit them to the elevator.
505 */
506 add_async_extent(async_cow, start, num_bytes,
261507a0
LZ
507 total_compressed, pages, nr_pages_ret,
508 compress_type);
179e29e4 509
24ae6365 510 if (start + num_bytes < end) {
771ed689
CM
511 start += num_bytes;
512 pages = NULL;
513 cond_resched();
514 goto again;
515 }
516 } else {
f03d9301 517cleanup_and_bail_uncompressed:
771ed689
CM
518 /*
519 * No compression, but we still need to write the pages in
520 * the file we've been given so far. redirty the locked
521 * page if it corresponds to our extent and set things up
522 * for the async work queue to run cow_file_range to do
523 * the normal delalloc dance
524 */
525 if (page_offset(locked_page) >= start &&
526 page_offset(locked_page) <= end) {
527 __set_page_dirty_nobuffers(locked_page);
528 /* unlocked later on in the async handlers */
529 }
261507a0
LZ
530 add_async_extent(async_cow, start, end - start + 1,
531 0, NULL, 0, BTRFS_COMPRESS_NONE);
771ed689
CM
532 *num_added += 1;
533 }
3b951516 534
771ed689
CM
535out:
536 return 0;
537
538free_pages_out:
539 for (i = 0; i < nr_pages_ret; i++) {
540 WARN_ON(pages[i]->mapping);
541 page_cache_release(pages[i]);
542 }
d397712b 543 kfree(pages);
771ed689
CM
544
545 goto out;
546}
547
548/*
549 * phase two of compressed writeback. This is the ordered portion
550 * of the code, which only gets called in the order the work was
551 * queued. We walk all the async extents created by compress_file_range
552 * and send them down to the disk.
553 */
554static noinline int submit_compressed_extents(struct inode *inode,
555 struct async_cow *async_cow)
556{
557 struct async_extent *async_extent;
558 u64 alloc_hint = 0;
559 struct btrfs_trans_handle *trans;
560 struct btrfs_key ins;
561 struct extent_map *em;
562 struct btrfs_root *root = BTRFS_I(inode)->root;
563 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
564 struct extent_io_tree *io_tree;
f5a84ee3 565 int ret = 0;
771ed689
CM
566
567 if (list_empty(&async_cow->extents))
568 return 0;
569
771ed689 570
d397712b 571 while (!list_empty(&async_cow->extents)) {
771ed689
CM
572 async_extent = list_entry(async_cow->extents.next,
573 struct async_extent, list);
574 list_del(&async_extent->list);
c8b97818 575
771ed689
CM
576 io_tree = &BTRFS_I(inode)->io_tree;
577
f5a84ee3 578retry:
771ed689
CM
579 /* did the compression code fall back to uncompressed IO? */
580 if (!async_extent->pages) {
581 int page_started = 0;
582 unsigned long nr_written = 0;
583
584 lock_extent(io_tree, async_extent->start,
2ac55d41
JB
585 async_extent->start +
586 async_extent->ram_size - 1, GFP_NOFS);
771ed689
CM
587
588 /* allocate blocks */
f5a84ee3
JB
589 ret = cow_file_range(inode, async_cow->locked_page,
590 async_extent->start,
591 async_extent->start +
592 async_extent->ram_size - 1,
593 &page_started, &nr_written, 0);
771ed689
CM
594
595 /*
596 * if page_started, cow_file_range inserted an
597 * inline extent and took care of all the unlocking
598 * and IO for us. Otherwise, we need to submit
599 * all those pages down to the drive.
600 */
f5a84ee3 601 if (!page_started && !ret)
771ed689
CM
602 extent_write_locked_range(io_tree,
603 inode, async_extent->start,
d397712b 604 async_extent->start +
771ed689
CM
605 async_extent->ram_size - 1,
606 btrfs_get_extent,
607 WB_SYNC_ALL);
608 kfree(async_extent);
609 cond_resched();
610 continue;
611 }
612
613 lock_extent(io_tree, async_extent->start,
614 async_extent->start + async_extent->ram_size - 1,
615 GFP_NOFS);
771ed689 616
c2167754 617 trans = btrfs_join_transaction(root, 1);
3612b495 618 BUG_ON(IS_ERR(trans));
771ed689
CM
619 ret = btrfs_reserve_extent(trans, root,
620 async_extent->compressed_size,
621 async_extent->compressed_size,
622 0, alloc_hint,
623 (u64)-1, &ins, 1);
c2167754
YZ
624 btrfs_end_transaction(trans, root);
625
f5a84ee3
JB
626 if (ret) {
627 int i;
628 for (i = 0; i < async_extent->nr_pages; i++) {
629 WARN_ON(async_extent->pages[i]->mapping);
630 page_cache_release(async_extent->pages[i]);
631 }
632 kfree(async_extent->pages);
633 async_extent->nr_pages = 0;
634 async_extent->pages = NULL;
635 unlock_extent(io_tree, async_extent->start,
636 async_extent->start +
637 async_extent->ram_size - 1, GFP_NOFS);
638 goto retry;
639 }
640
c2167754
YZ
641 /*
642 * here we're doing allocation and writeback of the
643 * compressed pages
644 */
645 btrfs_drop_extent_cache(inode, async_extent->start,
646 async_extent->start +
647 async_extent->ram_size - 1, 0);
648
771ed689 649 em = alloc_extent_map(GFP_NOFS);
c26a9203 650 BUG_ON(!em);
771ed689
CM
651 em->start = async_extent->start;
652 em->len = async_extent->ram_size;
445a6944 653 em->orig_start = em->start;
c8b97818 654
771ed689
CM
655 em->block_start = ins.objectid;
656 em->block_len = ins.offset;
657 em->bdev = root->fs_info->fs_devices->latest_bdev;
261507a0 658 em->compress_type = async_extent->compress_type;
771ed689
CM
659 set_bit(EXTENT_FLAG_PINNED, &em->flags);
660 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
661
d397712b 662 while (1) {
890871be 663 write_lock(&em_tree->lock);
771ed689 664 ret = add_extent_mapping(em_tree, em);
890871be 665 write_unlock(&em_tree->lock);
771ed689
CM
666 if (ret != -EEXIST) {
667 free_extent_map(em);
668 break;
669 }
670 btrfs_drop_extent_cache(inode, async_extent->start,
671 async_extent->start +
672 async_extent->ram_size - 1, 0);
673 }
674
261507a0
LZ
675 ret = btrfs_add_ordered_extent_compress(inode,
676 async_extent->start,
677 ins.objectid,
678 async_extent->ram_size,
679 ins.offset,
680 BTRFS_ORDERED_COMPRESSED,
681 async_extent->compress_type);
771ed689
CM
682 BUG_ON(ret);
683
771ed689
CM
684 /*
685 * clear dirty, set writeback and unlock the pages.
686 */
687 extent_clear_unlock_delalloc(inode,
a791e35e
CM
688 &BTRFS_I(inode)->io_tree,
689 async_extent->start,
690 async_extent->start +
691 async_extent->ram_size - 1,
692 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
693 EXTENT_CLEAR_UNLOCK |
a3429ab7 694 EXTENT_CLEAR_DELALLOC |
a791e35e 695 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
771ed689
CM
696
697 ret = btrfs_submit_compressed_write(inode,
d397712b
CM
698 async_extent->start,
699 async_extent->ram_size,
700 ins.objectid,
701 ins.offset, async_extent->pages,
702 async_extent->nr_pages);
771ed689
CM
703
704 BUG_ON(ret);
771ed689
CM
705 alloc_hint = ins.objectid + ins.offset;
706 kfree(async_extent);
707 cond_resched();
708 }
709
771ed689
CM
710 return 0;
711}
712
4b46fce2
JB
713static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
714 u64 num_bytes)
715{
716 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
717 struct extent_map *em;
718 u64 alloc_hint = 0;
719
720 read_lock(&em_tree->lock);
721 em = search_extent_mapping(em_tree, start, num_bytes);
722 if (em) {
723 /*
724 * if block start isn't an actual block number then find the
725 * first block in this inode and use that as a hint. If that
726 * block is also bogus then just don't worry about it.
727 */
728 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
729 free_extent_map(em);
730 em = search_extent_mapping(em_tree, 0, 0);
731 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
732 alloc_hint = em->block_start;
733 if (em)
734 free_extent_map(em);
735 } else {
736 alloc_hint = em->block_start;
737 free_extent_map(em);
738 }
739 }
740 read_unlock(&em_tree->lock);
741
742 return alloc_hint;
743}
744
771ed689
CM
745/*
746 * when extent_io.c finds a delayed allocation range in the file,
747 * the call backs end up in this code. The basic idea is to
748 * allocate extents on disk for the range, and create ordered data structs
749 * in ram to track those extents.
750 *
751 * locked_page is the page that writepage had locked already. We use
752 * it to make sure we don't do extra locks or unlocks.
753 *
754 * *page_started is set to one if we unlock locked_page and do everything
755 * required to start IO on it. It may be clean and already done with
756 * IO when we return.
757 */
758static noinline int cow_file_range(struct inode *inode,
759 struct page *locked_page,
760 u64 start, u64 end, int *page_started,
761 unsigned long *nr_written,
762 int unlock)
763{
764 struct btrfs_root *root = BTRFS_I(inode)->root;
765 struct btrfs_trans_handle *trans;
766 u64 alloc_hint = 0;
767 u64 num_bytes;
768 unsigned long ram_size;
769 u64 disk_num_bytes;
770 u64 cur_alloc_size;
771 u64 blocksize = root->sectorsize;
771ed689
CM
772 struct btrfs_key ins;
773 struct extent_map *em;
774 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
775 int ret = 0;
776
0cb59c99 777 BUG_ON(root == root->fs_info->tree_root);
771ed689 778 trans = btrfs_join_transaction(root, 1);
3612b495 779 BUG_ON(IS_ERR(trans));
771ed689 780 btrfs_set_trans_block_group(trans, inode);
0ca1f7ce 781 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
771ed689 782
771ed689
CM
783 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
784 num_bytes = max(blocksize, num_bytes);
785 disk_num_bytes = num_bytes;
786 ret = 0;
787
788 if (start == 0) {
789 /* lets try to make an inline extent */
790 ret = cow_file_range_inline(trans, root, inode,
791 start, end, 0, NULL);
792 if (ret == 0) {
793 extent_clear_unlock_delalloc(inode,
a791e35e
CM
794 &BTRFS_I(inode)->io_tree,
795 start, end, NULL,
796 EXTENT_CLEAR_UNLOCK_PAGE |
797 EXTENT_CLEAR_UNLOCK |
798 EXTENT_CLEAR_DELALLOC |
799 EXTENT_CLEAR_DIRTY |
800 EXTENT_SET_WRITEBACK |
801 EXTENT_END_WRITEBACK);
c2167754 802
771ed689
CM
803 *nr_written = *nr_written +
804 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
805 *page_started = 1;
806 ret = 0;
807 goto out;
808 }
809 }
810
811 BUG_ON(disk_num_bytes >
812 btrfs_super_total_bytes(&root->fs_info->super_copy));
813
4b46fce2 814 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
771ed689
CM
815 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
816
d397712b 817 while (disk_num_bytes > 0) {
a791e35e
CM
818 unsigned long op;
819
287a0ab9 820 cur_alloc_size = disk_num_bytes;
e6dcd2dc 821 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
771ed689 822 root->sectorsize, 0, alloc_hint,
e6dcd2dc 823 (u64)-1, &ins, 1);
d397712b
CM
824 BUG_ON(ret);
825
e6dcd2dc 826 em = alloc_extent_map(GFP_NOFS);
c26a9203 827 BUG_ON(!em);
e6dcd2dc 828 em->start = start;
445a6944 829 em->orig_start = em->start;
771ed689
CM
830 ram_size = ins.offset;
831 em->len = ins.offset;
c8b97818 832
e6dcd2dc 833 em->block_start = ins.objectid;
c8b97818 834 em->block_len = ins.offset;
e6dcd2dc 835 em->bdev = root->fs_info->fs_devices->latest_bdev;
7f3c74fb 836 set_bit(EXTENT_FLAG_PINNED, &em->flags);
c8b97818 837
d397712b 838 while (1) {
890871be 839 write_lock(&em_tree->lock);
e6dcd2dc 840 ret = add_extent_mapping(em_tree, em);
890871be 841 write_unlock(&em_tree->lock);
e6dcd2dc
CM
842 if (ret != -EEXIST) {
843 free_extent_map(em);
844 break;
845 }
846 btrfs_drop_extent_cache(inode, start,
c8b97818 847 start + ram_size - 1, 0);
e6dcd2dc
CM
848 }
849
98d20f67 850 cur_alloc_size = ins.offset;
e6dcd2dc 851 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
771ed689 852 ram_size, cur_alloc_size, 0);
e6dcd2dc 853 BUG_ON(ret);
c8b97818 854
17d217fe
YZ
855 if (root->root_key.objectid ==
856 BTRFS_DATA_RELOC_TREE_OBJECTID) {
857 ret = btrfs_reloc_clone_csums(inode, start,
858 cur_alloc_size);
859 BUG_ON(ret);
860 }
861
d397712b 862 if (disk_num_bytes < cur_alloc_size)
3b951516 863 break;
d397712b 864
c8b97818
CM
865 /* we're not doing compressed IO, don't unlock the first
866 * page (which the caller expects to stay locked), don't
867 * clear any dirty bits and don't set any writeback bits
8b62b72b
CM
868 *
869 * Do set the Private2 bit so we know this page was properly
870 * setup for writepage
c8b97818 871 */
a791e35e
CM
872 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
873 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
874 EXTENT_SET_PRIVATE2;
875
c8b97818
CM
876 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
877 start, start + ram_size - 1,
a791e35e 878 locked_page, op);
c8b97818 879 disk_num_bytes -= cur_alloc_size;
c59f8951
CM
880 num_bytes -= cur_alloc_size;
881 alloc_hint = ins.objectid + ins.offset;
882 start += cur_alloc_size;
b888db2b 883 }
b888db2b 884out:
771ed689 885 ret = 0;
b888db2b 886 btrfs_end_transaction(trans, root);
c8b97818 887
be20aa9d 888 return ret;
771ed689 889}
c8b97818 890
771ed689
CM
891/*
892 * work queue call back to started compression on a file and pages
893 */
894static noinline void async_cow_start(struct btrfs_work *work)
895{
896 struct async_cow *async_cow;
897 int num_added = 0;
898 async_cow = container_of(work, struct async_cow, work);
899
900 compress_file_range(async_cow->inode, async_cow->locked_page,
901 async_cow->start, async_cow->end, async_cow,
902 &num_added);
903 if (num_added == 0)
904 async_cow->inode = NULL;
905}
906
907/*
908 * work queue call back to submit previously compressed pages
909 */
910static noinline void async_cow_submit(struct btrfs_work *work)
911{
912 struct async_cow *async_cow;
913 struct btrfs_root *root;
914 unsigned long nr_pages;
915
916 async_cow = container_of(work, struct async_cow, work);
917
918 root = async_cow->root;
919 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
920 PAGE_CACHE_SHIFT;
921
922 atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages);
923
924 if (atomic_read(&root->fs_info->async_delalloc_pages) <
925 5 * 1042 * 1024 &&
926 waitqueue_active(&root->fs_info->async_submit_wait))
927 wake_up(&root->fs_info->async_submit_wait);
928
d397712b 929 if (async_cow->inode)
771ed689 930 submit_compressed_extents(async_cow->inode, async_cow);
771ed689 931}
c8b97818 932
771ed689
CM
933static noinline void async_cow_free(struct btrfs_work *work)
934{
935 struct async_cow *async_cow;
936 async_cow = container_of(work, struct async_cow, work);
937 kfree(async_cow);
938}
939
940static int cow_file_range_async(struct inode *inode, struct page *locked_page,
941 u64 start, u64 end, int *page_started,
942 unsigned long *nr_written)
943{
944 struct async_cow *async_cow;
945 struct btrfs_root *root = BTRFS_I(inode)->root;
946 unsigned long nr_pages;
947 u64 cur_end;
948 int limit = 10 * 1024 * 1042;
949
a3429ab7
CM
950 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
951 1, 0, NULL, GFP_NOFS);
d397712b 952 while (start < end) {
771ed689
CM
953 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
954 async_cow->inode = inode;
955 async_cow->root = root;
956 async_cow->locked_page = locked_page;
957 async_cow->start = start;
958
6cbff00f 959 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
771ed689
CM
960 cur_end = end;
961 else
962 cur_end = min(end, start + 512 * 1024 - 1);
963
964 async_cow->end = cur_end;
965 INIT_LIST_HEAD(&async_cow->extents);
966
967 async_cow->work.func = async_cow_start;
968 async_cow->work.ordered_func = async_cow_submit;
969 async_cow->work.ordered_free = async_cow_free;
970 async_cow->work.flags = 0;
971
771ed689
CM
972 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
973 PAGE_CACHE_SHIFT;
974 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
975
976 btrfs_queue_worker(&root->fs_info->delalloc_workers,
977 &async_cow->work);
978
979 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
980 wait_event(root->fs_info->async_submit_wait,
981 (atomic_read(&root->fs_info->async_delalloc_pages) <
982 limit));
983 }
984
d397712b 985 while (atomic_read(&root->fs_info->async_submit_draining) &&
771ed689
CM
986 atomic_read(&root->fs_info->async_delalloc_pages)) {
987 wait_event(root->fs_info->async_submit_wait,
988 (atomic_read(&root->fs_info->async_delalloc_pages) ==
989 0));
990 }
991
992 *nr_written += nr_pages;
993 start = cur_end + 1;
994 }
995 *page_started = 1;
996 return 0;
be20aa9d
CM
997}
998
d397712b 999static noinline int csum_exist_in_range(struct btrfs_root *root,
17d217fe
YZ
1000 u64 bytenr, u64 num_bytes)
1001{
1002 int ret;
1003 struct btrfs_ordered_sum *sums;
1004 LIST_HEAD(list);
1005
07d400a6
YZ
1006 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
1007 bytenr + num_bytes - 1, &list);
17d217fe
YZ
1008 if (ret == 0 && list_empty(&list))
1009 return 0;
1010
1011 while (!list_empty(&list)) {
1012 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1013 list_del(&sums->list);
1014 kfree(sums);
1015 }
1016 return 1;
1017}
1018
d352ac68
CM
1019/*
1020 * when nowcow writeback call back. This checks for snapshots or COW copies
1021 * of the extents that exist in the file, and COWs the file as required.
1022 *
1023 * If no cow copies or snapshots exist, we write directly to the existing
1024 * blocks on disk
1025 */
7f366cfe
CM
1026static noinline int run_delalloc_nocow(struct inode *inode,
1027 struct page *locked_page,
771ed689
CM
1028 u64 start, u64 end, int *page_started, int force,
1029 unsigned long *nr_written)
be20aa9d 1030{
be20aa9d 1031 struct btrfs_root *root = BTRFS_I(inode)->root;
7ea394f1 1032 struct btrfs_trans_handle *trans;
be20aa9d 1033 struct extent_buffer *leaf;
be20aa9d 1034 struct btrfs_path *path;
80ff3856 1035 struct btrfs_file_extent_item *fi;
be20aa9d 1036 struct btrfs_key found_key;
80ff3856
YZ
1037 u64 cow_start;
1038 u64 cur_offset;
1039 u64 extent_end;
5d4f98a2 1040 u64 extent_offset;
80ff3856
YZ
1041 u64 disk_bytenr;
1042 u64 num_bytes;
1043 int extent_type;
1044 int ret;
d899e052 1045 int type;
80ff3856
YZ
1046 int nocow;
1047 int check_prev = 1;
0cb59c99 1048 bool nolock = false;
be20aa9d
CM
1049
1050 path = btrfs_alloc_path();
1051 BUG_ON(!path);
0cb59c99
JB
1052 if (root == root->fs_info->tree_root) {
1053 nolock = true;
1054 trans = btrfs_join_transaction_nolock(root, 1);
1055 } else {
1056 trans = btrfs_join_transaction(root, 1);
1057 }
3612b495 1058 BUG_ON(IS_ERR(trans));
be20aa9d 1059
80ff3856
YZ
1060 cow_start = (u64)-1;
1061 cur_offset = start;
1062 while (1) {
1063 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
1064 cur_offset, 0);
1065 BUG_ON(ret < 0);
1066 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1067 leaf = path->nodes[0];
1068 btrfs_item_key_to_cpu(leaf, &found_key,
1069 path->slots[0] - 1);
1070 if (found_key.objectid == inode->i_ino &&
1071 found_key.type == BTRFS_EXTENT_DATA_KEY)
1072 path->slots[0]--;
1073 }
1074 check_prev = 0;
1075next_slot:
1076 leaf = path->nodes[0];
1077 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1078 ret = btrfs_next_leaf(root, path);
1079 if (ret < 0)
1080 BUG_ON(1);
1081 if (ret > 0)
1082 break;
1083 leaf = path->nodes[0];
1084 }
be20aa9d 1085
80ff3856
YZ
1086 nocow = 0;
1087 disk_bytenr = 0;
17d217fe 1088 num_bytes = 0;
80ff3856
YZ
1089 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1090
1091 if (found_key.objectid > inode->i_ino ||
1092 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1093 found_key.offset > end)
1094 break;
1095
1096 if (found_key.offset > cur_offset) {
1097 extent_end = found_key.offset;
e9061e21 1098 extent_type = 0;
80ff3856
YZ
1099 goto out_check;
1100 }
1101
1102 fi = btrfs_item_ptr(leaf, path->slots[0],
1103 struct btrfs_file_extent_item);
1104 extent_type = btrfs_file_extent_type(leaf, fi);
1105
d899e052
YZ
1106 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1107 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
80ff3856 1108 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
5d4f98a2 1109 extent_offset = btrfs_file_extent_offset(leaf, fi);
80ff3856
YZ
1110 extent_end = found_key.offset +
1111 btrfs_file_extent_num_bytes(leaf, fi);
1112 if (extent_end <= start) {
1113 path->slots[0]++;
1114 goto next_slot;
1115 }
17d217fe
YZ
1116 if (disk_bytenr == 0)
1117 goto out_check;
80ff3856
YZ
1118 if (btrfs_file_extent_compression(leaf, fi) ||
1119 btrfs_file_extent_encryption(leaf, fi) ||
1120 btrfs_file_extent_other_encoding(leaf, fi))
1121 goto out_check;
d899e052
YZ
1122 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1123 goto out_check;
d2fb3437 1124 if (btrfs_extent_readonly(root, disk_bytenr))
80ff3856 1125 goto out_check;
17d217fe 1126 if (btrfs_cross_ref_exist(trans, root, inode->i_ino,
5d4f98a2
YZ
1127 found_key.offset -
1128 extent_offset, disk_bytenr))
17d217fe 1129 goto out_check;
5d4f98a2 1130 disk_bytenr += extent_offset;
17d217fe
YZ
1131 disk_bytenr += cur_offset - found_key.offset;
1132 num_bytes = min(end + 1, extent_end) - cur_offset;
1133 /*
1134 * force cow if csum exists in the range.
1135 * this ensure that csum for a given extent are
1136 * either valid or do not exist.
1137 */
1138 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1139 goto out_check;
80ff3856
YZ
1140 nocow = 1;
1141 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1142 extent_end = found_key.offset +
1143 btrfs_file_extent_inline_len(leaf, fi);
1144 extent_end = ALIGN(extent_end, root->sectorsize);
1145 } else {
1146 BUG_ON(1);
1147 }
1148out_check:
1149 if (extent_end <= start) {
1150 path->slots[0]++;
1151 goto next_slot;
1152 }
1153 if (!nocow) {
1154 if (cow_start == (u64)-1)
1155 cow_start = cur_offset;
1156 cur_offset = extent_end;
1157 if (cur_offset > end)
1158 break;
1159 path->slots[0]++;
1160 goto next_slot;
7ea394f1
YZ
1161 }
1162
1163 btrfs_release_path(root, path);
80ff3856
YZ
1164 if (cow_start != (u64)-1) {
1165 ret = cow_file_range(inode, locked_page, cow_start,
771ed689
CM
1166 found_key.offset - 1, page_started,
1167 nr_written, 1);
80ff3856
YZ
1168 BUG_ON(ret);
1169 cow_start = (u64)-1;
7ea394f1 1170 }
80ff3856 1171
d899e052
YZ
1172 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1173 struct extent_map *em;
1174 struct extent_map_tree *em_tree;
1175 em_tree = &BTRFS_I(inode)->extent_tree;
1176 em = alloc_extent_map(GFP_NOFS);
c26a9203 1177 BUG_ON(!em);
d899e052 1178 em->start = cur_offset;
445a6944 1179 em->orig_start = em->start;
d899e052
YZ
1180 em->len = num_bytes;
1181 em->block_len = num_bytes;
1182 em->block_start = disk_bytenr;
1183 em->bdev = root->fs_info->fs_devices->latest_bdev;
1184 set_bit(EXTENT_FLAG_PINNED, &em->flags);
1185 while (1) {
890871be 1186 write_lock(&em_tree->lock);
d899e052 1187 ret = add_extent_mapping(em_tree, em);
890871be 1188 write_unlock(&em_tree->lock);
d899e052
YZ
1189 if (ret != -EEXIST) {
1190 free_extent_map(em);
1191 break;
1192 }
1193 btrfs_drop_extent_cache(inode, em->start,
1194 em->start + em->len - 1, 0);
1195 }
1196 type = BTRFS_ORDERED_PREALLOC;
1197 } else {
1198 type = BTRFS_ORDERED_NOCOW;
1199 }
80ff3856
YZ
1200
1201 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
d899e052
YZ
1202 num_bytes, num_bytes, type);
1203 BUG_ON(ret);
771ed689 1204
efa56464
YZ
1205 if (root->root_key.objectid ==
1206 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1207 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1208 num_bytes);
1209 BUG_ON(ret);
1210 }
1211
d899e052 1212 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
a791e35e
CM
1213 cur_offset, cur_offset + num_bytes - 1,
1214 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1215 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1216 EXTENT_SET_PRIVATE2);
80ff3856
YZ
1217 cur_offset = extent_end;
1218 if (cur_offset > end)
1219 break;
be20aa9d 1220 }
80ff3856
YZ
1221 btrfs_release_path(root, path);
1222
1223 if (cur_offset <= end && cow_start == (u64)-1)
1224 cow_start = cur_offset;
1225 if (cow_start != (u64)-1) {
1226 ret = cow_file_range(inode, locked_page, cow_start, end,
771ed689 1227 page_started, nr_written, 1);
80ff3856
YZ
1228 BUG_ON(ret);
1229 }
1230
0cb59c99
JB
1231 if (nolock) {
1232 ret = btrfs_end_transaction_nolock(trans, root);
1233 BUG_ON(ret);
1234 } else {
1235 ret = btrfs_end_transaction(trans, root);
1236 BUG_ON(ret);
1237 }
7ea394f1 1238 btrfs_free_path(path);
80ff3856 1239 return 0;
be20aa9d
CM
1240}
1241
d352ac68
CM
1242/*
1243 * extent_io.c call back to do delayed allocation processing
1244 */
c8b97818 1245static int run_delalloc_range(struct inode *inode, struct page *locked_page,
771ed689
CM
1246 u64 start, u64 end, int *page_started,
1247 unsigned long *nr_written)
be20aa9d 1248{
be20aa9d 1249 int ret;
7f366cfe 1250 struct btrfs_root *root = BTRFS_I(inode)->root;
a2135011 1251
6cbff00f 1252 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)
c8b97818 1253 ret = run_delalloc_nocow(inode, locked_page, start, end,
d397712b 1254 page_started, 1, nr_written);
6cbff00f 1255 else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)
d899e052 1256 ret = run_delalloc_nocow(inode, locked_page, start, end,
d397712b 1257 page_started, 0, nr_written);
1e701a32
CM
1258 else if (!btrfs_test_opt(root, COMPRESS) &&
1259 !(BTRFS_I(inode)->force_compress))
7f366cfe
CM
1260 ret = cow_file_range(inode, locked_page, start, end,
1261 page_started, nr_written, 1);
be20aa9d 1262 else
771ed689 1263 ret = cow_file_range_async(inode, locked_page, start, end,
d397712b 1264 page_started, nr_written);
b888db2b
CM
1265 return ret;
1266}
1267
9ed74f2d 1268static int btrfs_split_extent_hook(struct inode *inode,
0ca1f7ce 1269 struct extent_state *orig, u64 split)
9ed74f2d 1270{
0ca1f7ce 1271 /* not delalloc, ignore it */
9ed74f2d
JB
1272 if (!(orig->state & EXTENT_DELALLOC))
1273 return 0;
1274
0ca1f7ce 1275 atomic_inc(&BTRFS_I(inode)->outstanding_extents);
9ed74f2d
JB
1276 return 0;
1277}
1278
1279/*
1280 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1281 * extents so we can keep track of new extents that are just merged onto old
1282 * extents, such as when we are doing sequential writes, so we can properly
1283 * account for the metadata space we'll need.
1284 */
1285static int btrfs_merge_extent_hook(struct inode *inode,
1286 struct extent_state *new,
1287 struct extent_state *other)
1288{
9ed74f2d
JB
1289 /* not delalloc, ignore it */
1290 if (!(other->state & EXTENT_DELALLOC))
1291 return 0;
1292
0ca1f7ce 1293 atomic_dec(&BTRFS_I(inode)->outstanding_extents);
9ed74f2d
JB
1294 return 0;
1295}
1296
d352ac68
CM
1297/*
1298 * extent_io.c set_bit_hook, used to track delayed allocation
1299 * bytes in this file, and to maintain the list of inodes that
1300 * have pending delalloc work to be done.
1301 */
0ca1f7ce
YZ
1302static int btrfs_set_bit_hook(struct inode *inode,
1303 struct extent_state *state, int *bits)
291d673e 1304{
9ed74f2d 1305
75eff68e
CM
1306 /*
1307 * set_bit and clear bit hooks normally require _irqsave/restore
1308 * but in this case, we are only testeing for the DELALLOC
1309 * bit, which is only set or cleared with irqs on
1310 */
0ca1f7ce 1311 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
291d673e 1312 struct btrfs_root *root = BTRFS_I(inode)->root;
0ca1f7ce 1313 u64 len = state->end + 1 - state->start;
0cb59c99
JB
1314 int do_list = (root->root_key.objectid !=
1315 BTRFS_ROOT_TREE_OBJECTID);
9ed74f2d 1316
0ca1f7ce
YZ
1317 if (*bits & EXTENT_FIRST_DELALLOC)
1318 *bits &= ~EXTENT_FIRST_DELALLOC;
1319 else
1320 atomic_inc(&BTRFS_I(inode)->outstanding_extents);
287a0ab9 1321
75eff68e 1322 spin_lock(&root->fs_info->delalloc_lock);
0ca1f7ce
YZ
1323 BTRFS_I(inode)->delalloc_bytes += len;
1324 root->fs_info->delalloc_bytes += len;
0cb59c99 1325 if (do_list && list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
ea8c2819
CM
1326 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1327 &root->fs_info->delalloc_inodes);
1328 }
75eff68e 1329 spin_unlock(&root->fs_info->delalloc_lock);
291d673e
CM
1330 }
1331 return 0;
1332}
1333
d352ac68
CM
1334/*
1335 * extent_io.c clear_bit_hook, see set_bit_hook for why
1336 */
9ed74f2d 1337static int btrfs_clear_bit_hook(struct inode *inode,
0ca1f7ce 1338 struct extent_state *state, int *bits)
291d673e 1339{
75eff68e
CM
1340 /*
1341 * set_bit and clear bit hooks normally require _irqsave/restore
1342 * but in this case, we are only testeing for the DELALLOC
1343 * bit, which is only set or cleared with irqs on
1344 */
0ca1f7ce 1345 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
291d673e 1346 struct btrfs_root *root = BTRFS_I(inode)->root;
0ca1f7ce 1347 u64 len = state->end + 1 - state->start;
0cb59c99
JB
1348 int do_list = (root->root_key.objectid !=
1349 BTRFS_ROOT_TREE_OBJECTID);
bcbfce8a 1350
0ca1f7ce
YZ
1351 if (*bits & EXTENT_FIRST_DELALLOC)
1352 *bits &= ~EXTENT_FIRST_DELALLOC;
1353 else if (!(*bits & EXTENT_DO_ACCOUNTING))
1354 atomic_dec(&BTRFS_I(inode)->outstanding_extents);
1355
1356 if (*bits & EXTENT_DO_ACCOUNTING)
1357 btrfs_delalloc_release_metadata(inode, len);
1358
0cb59c99
JB
1359 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1360 && do_list)
0ca1f7ce 1361 btrfs_free_reserved_data_space(inode, len);
9ed74f2d 1362
75eff68e 1363 spin_lock(&root->fs_info->delalloc_lock);
0ca1f7ce
YZ
1364 root->fs_info->delalloc_bytes -= len;
1365 BTRFS_I(inode)->delalloc_bytes -= len;
1366
0cb59c99 1367 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
ea8c2819
CM
1368 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1369 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1370 }
75eff68e 1371 spin_unlock(&root->fs_info->delalloc_lock);
291d673e
CM
1372 }
1373 return 0;
1374}
1375
d352ac68
CM
1376/*
1377 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1378 * we don't create bios that span stripes or chunks
1379 */
239b14b3 1380int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
c8b97818
CM
1381 size_t size, struct bio *bio,
1382 unsigned long bio_flags)
239b14b3
CM
1383{
1384 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1385 struct btrfs_mapping_tree *map_tree;
a62b9401 1386 u64 logical = (u64)bio->bi_sector << 9;
239b14b3
CM
1387 u64 length = 0;
1388 u64 map_length;
239b14b3
CM
1389 int ret;
1390
771ed689
CM
1391 if (bio_flags & EXTENT_BIO_COMPRESSED)
1392 return 0;
1393
f2d8d74d 1394 length = bio->bi_size;
239b14b3
CM
1395 map_tree = &root->fs_info->mapping_tree;
1396 map_length = length;
cea9e445 1397 ret = btrfs_map_block(map_tree, READ, logical,
f188591e 1398 &map_length, NULL, 0);
cea9e445 1399
d397712b 1400 if (map_length < length + size)
239b14b3 1401 return 1;
411fc6bc 1402 return ret;
239b14b3
CM
1403}
1404
d352ac68
CM
1405/*
1406 * in order to insert checksums into the metadata in large chunks,
1407 * we wait until bio submission time. All the pages in the bio are
1408 * checksummed and sums are attached onto the ordered extent record.
1409 *
1410 * At IO completion time the cums attached on the ordered extent record
1411 * are inserted into the btree
1412 */
d397712b
CM
1413static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1414 struct bio *bio, int mirror_num,
eaf25d93
CM
1415 unsigned long bio_flags,
1416 u64 bio_offset)
065631f6 1417{
065631f6 1418 struct btrfs_root *root = BTRFS_I(inode)->root;
065631f6 1419 int ret = 0;
e015640f 1420
d20f7043 1421 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
44b8bd7e 1422 BUG_ON(ret);
4a69a410
CM
1423 return 0;
1424}
e015640f 1425
4a69a410
CM
1426/*
1427 * in order to insert checksums into the metadata in large chunks,
1428 * we wait until bio submission time. All the pages in the bio are
1429 * checksummed and sums are attached onto the ordered extent record.
1430 *
1431 * At IO completion time the cums attached on the ordered extent record
1432 * are inserted into the btree
1433 */
b2950863 1434static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
eaf25d93
CM
1435 int mirror_num, unsigned long bio_flags,
1436 u64 bio_offset)
4a69a410
CM
1437{
1438 struct btrfs_root *root = BTRFS_I(inode)->root;
8b712842 1439 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
44b8bd7e
CM
1440}
1441
d352ac68 1442/*
cad321ad
CM
1443 * extent_io.c submission hook. This does the right thing for csum calculation
1444 * on write, or reading the csums from the tree before a read
d352ac68 1445 */
b2950863 1446static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
eaf25d93
CM
1447 int mirror_num, unsigned long bio_flags,
1448 u64 bio_offset)
44b8bd7e
CM
1449{
1450 struct btrfs_root *root = BTRFS_I(inode)->root;
1451 int ret = 0;
19b9bdb0 1452 int skip_sum;
44b8bd7e 1453
6cbff00f 1454 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
cad321ad 1455
0cb59c99
JB
1456 if (root == root->fs_info->tree_root)
1457 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 2);
1458 else
1459 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
e6dcd2dc 1460 BUG_ON(ret);
065631f6 1461
7b6d91da 1462 if (!(rw & REQ_WRITE)) {
d20f7043 1463 if (bio_flags & EXTENT_BIO_COMPRESSED) {
c8b97818
CM
1464 return btrfs_submit_compressed_read(inode, bio,
1465 mirror_num, bio_flags);
d20f7043
CM
1466 } else if (!skip_sum)
1467 btrfs_lookup_bio_sums(root, inode, bio, NULL);
4d1b5fb4 1468 goto mapit;
19b9bdb0 1469 } else if (!skip_sum) {
17d217fe
YZ
1470 /* csum items have already been cloned */
1471 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1472 goto mapit;
19b9bdb0
CM
1473 /* we're doing a write, do the async checksumming */
1474 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
44b8bd7e 1475 inode, rw, bio, mirror_num,
eaf25d93
CM
1476 bio_flags, bio_offset,
1477 __btrfs_submit_bio_start,
4a69a410 1478 __btrfs_submit_bio_done);
19b9bdb0
CM
1479 }
1480
0b86a832 1481mapit:
8b712842 1482 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
065631f6 1483}
6885f308 1484
d352ac68
CM
1485/*
1486 * given a list of ordered sums record them in the inode. This happens
1487 * at IO completion time based on sums calculated at bio submission time.
1488 */
ba1da2f4 1489static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
e6dcd2dc
CM
1490 struct inode *inode, u64 file_offset,
1491 struct list_head *list)
1492{
e6dcd2dc
CM
1493 struct btrfs_ordered_sum *sum;
1494
1495 btrfs_set_trans_block_group(trans, inode);
c6e30871
QF
1496
1497 list_for_each_entry(sum, list, list) {
d20f7043
CM
1498 btrfs_csum_file_blocks(trans,
1499 BTRFS_I(inode)->root->fs_info->csum_root, sum);
e6dcd2dc
CM
1500 }
1501 return 0;
1502}
1503
2ac55d41
JB
1504int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1505 struct extent_state **cached_state)
ea8c2819 1506{
d397712b 1507 if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
771ed689 1508 WARN_ON(1);
ea8c2819 1509 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
2ac55d41 1510 cached_state, GFP_NOFS);
ea8c2819
CM
1511}
1512
d352ac68 1513/* see btrfs_writepage_start_hook for details on why this is required */
247e743c
CM
1514struct btrfs_writepage_fixup {
1515 struct page *page;
1516 struct btrfs_work work;
1517};
1518
b2950863 1519static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
247e743c
CM
1520{
1521 struct btrfs_writepage_fixup *fixup;
1522 struct btrfs_ordered_extent *ordered;
2ac55d41 1523 struct extent_state *cached_state = NULL;
247e743c
CM
1524 struct page *page;
1525 struct inode *inode;
1526 u64 page_start;
1527 u64 page_end;
1528
1529 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1530 page = fixup->page;
4a096752 1531again:
247e743c
CM
1532 lock_page(page);
1533 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1534 ClearPageChecked(page);
1535 goto out_page;
1536 }
1537
1538 inode = page->mapping->host;
1539 page_start = page_offset(page);
1540 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1541
2ac55d41
JB
1542 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
1543 &cached_state, GFP_NOFS);
4a096752
CM
1544
1545 /* already ordered? We're done */
8b62b72b 1546 if (PagePrivate2(page))
247e743c 1547 goto out;
4a096752
CM
1548
1549 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1550 if (ordered) {
2ac55d41
JB
1551 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1552 page_end, &cached_state, GFP_NOFS);
4a096752
CM
1553 unlock_page(page);
1554 btrfs_start_ordered_extent(inode, ordered, 1);
1555 goto again;
1556 }
247e743c 1557
0ca1f7ce 1558 BUG();
2ac55d41 1559 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
247e743c
CM
1560 ClearPageChecked(page);
1561out:
2ac55d41
JB
1562 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1563 &cached_state, GFP_NOFS);
247e743c
CM
1564out_page:
1565 unlock_page(page);
1566 page_cache_release(page);
b897abec 1567 kfree(fixup);
247e743c
CM
1568}
1569
1570/*
1571 * There are a few paths in the higher layers of the kernel that directly
1572 * set the page dirty bit without asking the filesystem if it is a
1573 * good idea. This causes problems because we want to make sure COW
1574 * properly happens and the data=ordered rules are followed.
1575 *
c8b97818 1576 * In our case any range that doesn't have the ORDERED bit set
247e743c
CM
1577 * hasn't been properly setup for IO. We kick off an async process
1578 * to fix it up. The async helper will wait for ordered extents, set
1579 * the delalloc bit and make it safe to write the page.
1580 */
b2950863 1581static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
247e743c
CM
1582{
1583 struct inode *inode = page->mapping->host;
1584 struct btrfs_writepage_fixup *fixup;
1585 struct btrfs_root *root = BTRFS_I(inode)->root;
247e743c 1586
8b62b72b
CM
1587 /* this page is properly in the ordered list */
1588 if (TestClearPagePrivate2(page))
247e743c
CM
1589 return 0;
1590
1591 if (PageChecked(page))
1592 return -EAGAIN;
1593
1594 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1595 if (!fixup)
1596 return -EAGAIN;
f421950f 1597
247e743c
CM
1598 SetPageChecked(page);
1599 page_cache_get(page);
1600 fixup->work.func = btrfs_writepage_fixup_worker;
1601 fixup->page = page;
1602 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
1603 return -EAGAIN;
1604}
1605
d899e052
YZ
1606static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1607 struct inode *inode, u64 file_pos,
1608 u64 disk_bytenr, u64 disk_num_bytes,
1609 u64 num_bytes, u64 ram_bytes,
1610 u8 compression, u8 encryption,
1611 u16 other_encoding, int extent_type)
1612{
1613 struct btrfs_root *root = BTRFS_I(inode)->root;
1614 struct btrfs_file_extent_item *fi;
1615 struct btrfs_path *path;
1616 struct extent_buffer *leaf;
1617 struct btrfs_key ins;
1618 u64 hint;
1619 int ret;
1620
1621 path = btrfs_alloc_path();
1622 BUG_ON(!path);
1623
b9473439 1624 path->leave_spinning = 1;
a1ed835e
CM
1625
1626 /*
1627 * we may be replacing one extent in the tree with another.
1628 * The new extent is pinned in the extent map, and we don't want
1629 * to drop it from the cache until it is completely in the btree.
1630 *
1631 * So, tell btrfs_drop_extents to leave this extent in the cache.
1632 * the caller is expected to unpin it and allow it to be merged
1633 * with the others.
1634 */
920bbbfb
YZ
1635 ret = btrfs_drop_extents(trans, inode, file_pos, file_pos + num_bytes,
1636 &hint, 0);
d899e052
YZ
1637 BUG_ON(ret);
1638
1639 ins.objectid = inode->i_ino;
1640 ins.offset = file_pos;
1641 ins.type = BTRFS_EXTENT_DATA_KEY;
1642 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
1643 BUG_ON(ret);
1644 leaf = path->nodes[0];
1645 fi = btrfs_item_ptr(leaf, path->slots[0],
1646 struct btrfs_file_extent_item);
1647 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1648 btrfs_set_file_extent_type(leaf, fi, extent_type);
1649 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1650 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1651 btrfs_set_file_extent_offset(leaf, fi, 0);
1652 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1653 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1654 btrfs_set_file_extent_compression(leaf, fi, compression);
1655 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1656 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
b9473439
CM
1657
1658 btrfs_unlock_up_safe(path, 1);
1659 btrfs_set_lock_blocking(leaf);
1660
d899e052
YZ
1661 btrfs_mark_buffer_dirty(leaf);
1662
1663 inode_add_bytes(inode, num_bytes);
d899e052
YZ
1664
1665 ins.objectid = disk_bytenr;
1666 ins.offset = disk_num_bytes;
1667 ins.type = BTRFS_EXTENT_ITEM_KEY;
5d4f98a2
YZ
1668 ret = btrfs_alloc_reserved_file_extent(trans, root,
1669 root->root_key.objectid,
1670 inode->i_ino, file_pos, &ins);
d899e052 1671 BUG_ON(ret);
d899e052 1672 btrfs_free_path(path);
b9473439 1673
d899e052
YZ
1674 return 0;
1675}
1676
5d13a98f
CM
1677/*
1678 * helper function for btrfs_finish_ordered_io, this
1679 * just reads in some of the csum leaves to prime them into ram
1680 * before we start the transaction. It limits the amount of btree
1681 * reads required while inside the transaction.
1682 */
d352ac68
CM
1683/* as ordered data IO finishes, this gets called so we can finish
1684 * an ordered extent if the range of bytes in the file it covers are
1685 * fully written.
1686 */
211f90e6 1687static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
e6dcd2dc 1688{
e6dcd2dc 1689 struct btrfs_root *root = BTRFS_I(inode)->root;
0ca1f7ce 1690 struct btrfs_trans_handle *trans = NULL;
5d13a98f 1691 struct btrfs_ordered_extent *ordered_extent = NULL;
e6dcd2dc 1692 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2ac55d41 1693 struct extent_state *cached_state = NULL;
261507a0 1694 int compress_type = 0;
e6dcd2dc 1695 int ret;
0cb59c99 1696 bool nolock = false;
e6dcd2dc 1697
5a1a3df1
JB
1698 ret = btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
1699 end - start + 1);
ba1da2f4 1700 if (!ret)
e6dcd2dc 1701 return 0;
e6dcd2dc 1702 BUG_ON(!ordered_extent);
efd049fb 1703
0cb59c99
JB
1704 nolock = (root == root->fs_info->tree_root);
1705
c2167754
YZ
1706 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
1707 BUG_ON(!list_empty(&ordered_extent->list));
1708 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1709 if (!ret) {
0cb59c99
JB
1710 if (nolock)
1711 trans = btrfs_join_transaction_nolock(root, 1);
1712 else
1713 trans = btrfs_join_transaction(root, 1);
3612b495 1714 BUG_ON(IS_ERR(trans));
0ca1f7ce
YZ
1715 btrfs_set_trans_block_group(trans, inode);
1716 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
c2167754
YZ
1717 ret = btrfs_update_inode(trans, root, inode);
1718 BUG_ON(ret);
c2167754
YZ
1719 }
1720 goto out;
1721 }
e6dcd2dc 1722
2ac55d41
JB
1723 lock_extent_bits(io_tree, ordered_extent->file_offset,
1724 ordered_extent->file_offset + ordered_extent->len - 1,
1725 0, &cached_state, GFP_NOFS);
e6dcd2dc 1726
0cb59c99
JB
1727 if (nolock)
1728 trans = btrfs_join_transaction_nolock(root, 1);
1729 else
1730 trans = btrfs_join_transaction(root, 1);
3612b495 1731 BUG_ON(IS_ERR(trans));
0ca1f7ce
YZ
1732 btrfs_set_trans_block_group(trans, inode);
1733 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
c2167754 1734
c8b97818 1735 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
261507a0 1736 compress_type = ordered_extent->compress_type;
d899e052 1737 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
261507a0 1738 BUG_ON(compress_type);
920bbbfb 1739 ret = btrfs_mark_extent_written(trans, inode,
d899e052
YZ
1740 ordered_extent->file_offset,
1741 ordered_extent->file_offset +
1742 ordered_extent->len);
1743 BUG_ON(ret);
1744 } else {
0af3d00b 1745 BUG_ON(root == root->fs_info->tree_root);
d899e052
YZ
1746 ret = insert_reserved_file_extent(trans, inode,
1747 ordered_extent->file_offset,
1748 ordered_extent->start,
1749 ordered_extent->disk_len,
1750 ordered_extent->len,
1751 ordered_extent->len,
261507a0 1752 compress_type, 0, 0,
d899e052 1753 BTRFS_FILE_EXTENT_REG);
a1ed835e
CM
1754 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
1755 ordered_extent->file_offset,
1756 ordered_extent->len);
d899e052
YZ
1757 BUG_ON(ret);
1758 }
2ac55d41
JB
1759 unlock_extent_cached(io_tree, ordered_extent->file_offset,
1760 ordered_extent->file_offset +
1761 ordered_extent->len - 1, &cached_state, GFP_NOFS);
1762
e6dcd2dc
CM
1763 add_pending_csums(trans, inode, ordered_extent->file_offset,
1764 &ordered_extent->list);
1765
c2167754
YZ
1766 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1767 ret = btrfs_update_inode(trans, root, inode);
1768 BUG_ON(ret);
c2167754 1769out:
0cb59c99
JB
1770 if (nolock) {
1771 if (trans)
1772 btrfs_end_transaction_nolock(trans, root);
1773 } else {
1774 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
1775 if (trans)
1776 btrfs_end_transaction(trans, root);
1777 }
1778
e6dcd2dc
CM
1779 /* once for us */
1780 btrfs_put_ordered_extent(ordered_extent);
1781 /* once for the tree */
1782 btrfs_put_ordered_extent(ordered_extent);
1783
e6dcd2dc
CM
1784 return 0;
1785}
1786
b2950863 1787static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
211f90e6
CM
1788 struct extent_state *state, int uptodate)
1789{
8b62b72b 1790 ClearPagePrivate2(page);
211f90e6
CM
1791 return btrfs_finish_ordered_io(page->mapping->host, start, end);
1792}
1793
d352ac68
CM
1794/*
1795 * When IO fails, either with EIO or csum verification fails, we
1796 * try other mirrors that might have a good copy of the data. This
1797 * io_failure_record is used to record state as we go through all the
1798 * mirrors. If another mirror has good data, the page is set up to date
1799 * and things continue. If a good mirror can't be found, the original
1800 * bio end_io callback is called to indicate things have failed.
1801 */
7e38326f
CM
1802struct io_failure_record {
1803 struct page *page;
1804 u64 start;
1805 u64 len;
1806 u64 logical;
d20f7043 1807 unsigned long bio_flags;
7e38326f
CM
1808 int last_mirror;
1809};
1810
b2950863 1811static int btrfs_io_failed_hook(struct bio *failed_bio,
1259ab75
CM
1812 struct page *page, u64 start, u64 end,
1813 struct extent_state *state)
7e38326f
CM
1814{
1815 struct io_failure_record *failrec = NULL;
1816 u64 private;
1817 struct extent_map *em;
1818 struct inode *inode = page->mapping->host;
1819 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
3b951516 1820 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
7e38326f
CM
1821 struct bio *bio;
1822 int num_copies;
1823 int ret;
1259ab75 1824 int rw;
7e38326f
CM
1825 u64 logical;
1826
1827 ret = get_state_private(failure_tree, start, &private);
1828 if (ret) {
7e38326f
CM
1829 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
1830 if (!failrec)
1831 return -ENOMEM;
1832 failrec->start = start;
1833 failrec->len = end - start + 1;
1834 failrec->last_mirror = 0;
d20f7043 1835 failrec->bio_flags = 0;
7e38326f 1836
890871be 1837 read_lock(&em_tree->lock);
3b951516
CM
1838 em = lookup_extent_mapping(em_tree, start, failrec->len);
1839 if (em->start > start || em->start + em->len < start) {
1840 free_extent_map(em);
1841 em = NULL;
1842 }
890871be 1843 read_unlock(&em_tree->lock);
7e38326f
CM
1844
1845 if (!em || IS_ERR(em)) {
1846 kfree(failrec);
1847 return -EIO;
1848 }
1849 logical = start - em->start;
1850 logical = em->block_start + logical;
d20f7043
CM
1851 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
1852 logical = em->block_start;
1853 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
261507a0
LZ
1854 extent_set_compress_type(&failrec->bio_flags,
1855 em->compress_type);
d20f7043 1856 }
7e38326f
CM
1857 failrec->logical = logical;
1858 free_extent_map(em);
1859 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
1860 EXTENT_DIRTY, GFP_NOFS);
587f7704
CM
1861 set_state_private(failure_tree, start,
1862 (u64)(unsigned long)failrec);
7e38326f 1863 } else {
587f7704 1864 failrec = (struct io_failure_record *)(unsigned long)private;
7e38326f
CM
1865 }
1866 num_copies = btrfs_num_copies(
1867 &BTRFS_I(inode)->root->fs_info->mapping_tree,
1868 failrec->logical, failrec->len);
1869 failrec->last_mirror++;
1870 if (!state) {
cad321ad 1871 spin_lock(&BTRFS_I(inode)->io_tree.lock);
7e38326f
CM
1872 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1873 failrec->start,
1874 EXTENT_LOCKED);
1875 if (state && state->start != failrec->start)
1876 state = NULL;
cad321ad 1877 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
7e38326f
CM
1878 }
1879 if (!state || failrec->last_mirror > num_copies) {
1880 set_state_private(failure_tree, failrec->start, 0);
1881 clear_extent_bits(failure_tree, failrec->start,
1882 failrec->start + failrec->len - 1,
1883 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1884 kfree(failrec);
1885 return -EIO;
1886 }
1887 bio = bio_alloc(GFP_NOFS, 1);
1888 bio->bi_private = state;
1889 bio->bi_end_io = failed_bio->bi_end_io;
1890 bio->bi_sector = failrec->logical >> 9;
1891 bio->bi_bdev = failed_bio->bi_bdev;
e1c4b745 1892 bio->bi_size = 0;
d20f7043 1893
7e38326f 1894 bio_add_page(bio, page, failrec->len, start - page_offset(page));
7b6d91da 1895 if (failed_bio->bi_rw & REQ_WRITE)
1259ab75
CM
1896 rw = WRITE;
1897 else
1898 rw = READ;
1899
1900 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
c8b97818 1901 failrec->last_mirror,
eaf25d93 1902 failrec->bio_flags, 0);
1259ab75
CM
1903 return 0;
1904}
1905
d352ac68
CM
1906/*
1907 * each time an IO finishes, we do a fast check in the IO failure tree
1908 * to see if we need to process or clean up an io_failure_record
1909 */
b2950863 1910static int btrfs_clean_io_failures(struct inode *inode, u64 start)
1259ab75
CM
1911{
1912 u64 private;
1913 u64 private_failure;
1914 struct io_failure_record *failure;
1915 int ret;
1916
1917 private = 0;
1918 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
ec29ed5b 1919 (u64)-1, 1, EXTENT_DIRTY, 0)) {
1259ab75
CM
1920 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
1921 start, &private_failure);
1922 if (ret == 0) {
1923 failure = (struct io_failure_record *)(unsigned long)
1924 private_failure;
1925 set_state_private(&BTRFS_I(inode)->io_failure_tree,
1926 failure->start, 0);
1927 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
1928 failure->start,
1929 failure->start + failure->len - 1,
1930 EXTENT_DIRTY | EXTENT_LOCKED,
1931 GFP_NOFS);
1932 kfree(failure);
1933 }
1934 }
7e38326f
CM
1935 return 0;
1936}
1937
d352ac68
CM
1938/*
1939 * when reads are done, we need to check csums to verify the data is correct
1940 * if there's a match, we allow the bio to finish. If not, we go through
1941 * the io_failure_record routines to find good copies
1942 */
b2950863 1943static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
70dec807 1944 struct extent_state *state)
07157aac 1945{
35ebb934 1946 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
07157aac 1947 struct inode *inode = page->mapping->host;
d1310b2e 1948 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
07157aac 1949 char *kaddr;
aadfeb6e 1950 u64 private = ~(u32)0;
07157aac 1951 int ret;
ff79f819
CM
1952 struct btrfs_root *root = BTRFS_I(inode)->root;
1953 u32 csum = ~(u32)0;
d1310b2e 1954
d20f7043
CM
1955 if (PageChecked(page)) {
1956 ClearPageChecked(page);
1957 goto good;
1958 }
6cbff00f
CH
1959
1960 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
17d217fe
YZ
1961 return 0;
1962
1963 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
9655d298 1964 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
17d217fe
YZ
1965 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
1966 GFP_NOFS);
b6cda9bc 1967 return 0;
17d217fe 1968 }
d20f7043 1969
c2e639f0 1970 if (state && state->start == start) {
70dec807
CM
1971 private = state->private;
1972 ret = 0;
1973 } else {
1974 ret = get_state_private(io_tree, start, &private);
1975 }
9ab86c8e 1976 kaddr = kmap_atomic(page, KM_USER0);
d397712b 1977 if (ret)
07157aac 1978 goto zeroit;
d397712b 1979
ff79f819
CM
1980 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
1981 btrfs_csum_final(csum, (char *)&csum);
d397712b 1982 if (csum != private)
07157aac 1983 goto zeroit;
d397712b 1984
9ab86c8e 1985 kunmap_atomic(kaddr, KM_USER0);
d20f7043 1986good:
7e38326f
CM
1987 /* if the io failure tree for this inode is non-empty,
1988 * check to see if we've recovered from a failed IO
1989 */
1259ab75 1990 btrfs_clean_io_failures(inode, start);
07157aac
CM
1991 return 0;
1992
1993zeroit:
193f284d
CM
1994 if (printk_ratelimit()) {
1995 printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u "
1996 "private %llu\n", page->mapping->host->i_ino,
1997 (unsigned long long)start, csum,
1998 (unsigned long long)private);
1999 }
db94535d
CM
2000 memset(kaddr + offset, 1, end - start + 1);
2001 flush_dcache_page(page);
9ab86c8e 2002 kunmap_atomic(kaddr, KM_USER0);
3b951516
CM
2003 if (private == 0)
2004 return 0;
7e38326f 2005 return -EIO;
07157aac 2006}
b888db2b 2007
24bbcf04
YZ
2008struct delayed_iput {
2009 struct list_head list;
2010 struct inode *inode;
2011};
2012
2013void btrfs_add_delayed_iput(struct inode *inode)
2014{
2015 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2016 struct delayed_iput *delayed;
2017
2018 if (atomic_add_unless(&inode->i_count, -1, 1))
2019 return;
2020
2021 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2022 delayed->inode = inode;
2023
2024 spin_lock(&fs_info->delayed_iput_lock);
2025 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2026 spin_unlock(&fs_info->delayed_iput_lock);
2027}
2028
2029void btrfs_run_delayed_iputs(struct btrfs_root *root)
2030{
2031 LIST_HEAD(list);
2032 struct btrfs_fs_info *fs_info = root->fs_info;
2033 struct delayed_iput *delayed;
2034 int empty;
2035
2036 spin_lock(&fs_info->delayed_iput_lock);
2037 empty = list_empty(&fs_info->delayed_iputs);
2038 spin_unlock(&fs_info->delayed_iput_lock);
2039 if (empty)
2040 return;
2041
2042 down_read(&root->fs_info->cleanup_work_sem);
2043 spin_lock(&fs_info->delayed_iput_lock);
2044 list_splice_init(&fs_info->delayed_iputs, &list);
2045 spin_unlock(&fs_info->delayed_iput_lock);
2046
2047 while (!list_empty(&list)) {
2048 delayed = list_entry(list.next, struct delayed_iput, list);
2049 list_del(&delayed->list);
2050 iput(delayed->inode);
2051 kfree(delayed);
2052 }
2053 up_read(&root->fs_info->cleanup_work_sem);
2054}
2055
d68fc57b
YZ
2056/*
2057 * calculate extra metadata reservation when snapshotting a subvolume
2058 * contains orphan files.
2059 */
2060void btrfs_orphan_pre_snapshot(struct btrfs_trans_handle *trans,
2061 struct btrfs_pending_snapshot *pending,
2062 u64 *bytes_to_reserve)
2063{
2064 struct btrfs_root *root;
2065 struct btrfs_block_rsv *block_rsv;
2066 u64 num_bytes;
2067 int index;
2068
2069 root = pending->root;
2070 if (!root->orphan_block_rsv || list_empty(&root->orphan_list))
2071 return;
2072
2073 block_rsv = root->orphan_block_rsv;
2074
2075 /* orphan block reservation for the snapshot */
2076 num_bytes = block_rsv->size;
2077
2078 /*
2079 * after the snapshot is created, COWing tree blocks may use more
2080 * space than it frees. So we should make sure there is enough
2081 * reserved space.
2082 */
2083 index = trans->transid & 0x1;
2084 if (block_rsv->reserved + block_rsv->freed[index] < block_rsv->size) {
2085 num_bytes += block_rsv->size -
2086 (block_rsv->reserved + block_rsv->freed[index]);
2087 }
2088
2089 *bytes_to_reserve += num_bytes;
2090}
2091
2092void btrfs_orphan_post_snapshot(struct btrfs_trans_handle *trans,
2093 struct btrfs_pending_snapshot *pending)
2094{
2095 struct btrfs_root *root = pending->root;
2096 struct btrfs_root *snap = pending->snap;
2097 struct btrfs_block_rsv *block_rsv;
2098 u64 num_bytes;
2099 int index;
2100 int ret;
2101
2102 if (!root->orphan_block_rsv || list_empty(&root->orphan_list))
2103 return;
2104
2105 /* refill source subvolume's orphan block reservation */
2106 block_rsv = root->orphan_block_rsv;
2107 index = trans->transid & 0x1;
2108 if (block_rsv->reserved + block_rsv->freed[index] < block_rsv->size) {
2109 num_bytes = block_rsv->size -
2110 (block_rsv->reserved + block_rsv->freed[index]);
2111 ret = btrfs_block_rsv_migrate(&pending->block_rsv,
2112 root->orphan_block_rsv,
2113 num_bytes);
2114 BUG_ON(ret);
2115 }
2116
2117 /* setup orphan block reservation for the snapshot */
2118 block_rsv = btrfs_alloc_block_rsv(snap);
2119 BUG_ON(!block_rsv);
2120
2121 btrfs_add_durable_block_rsv(root->fs_info, block_rsv);
2122 snap->orphan_block_rsv = block_rsv;
2123
2124 num_bytes = root->orphan_block_rsv->size;
2125 ret = btrfs_block_rsv_migrate(&pending->block_rsv,
2126 block_rsv, num_bytes);
2127 BUG_ON(ret);
2128
2129#if 0
2130 /* insert orphan item for the snapshot */
2131 WARN_ON(!root->orphan_item_inserted);
2132 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2133 snap->root_key.objectid);
2134 BUG_ON(ret);
2135 snap->orphan_item_inserted = 1;
2136#endif
2137}
2138
2139enum btrfs_orphan_cleanup_state {
2140 ORPHAN_CLEANUP_STARTED = 1,
2141 ORPHAN_CLEANUP_DONE = 2,
2142};
2143
2144/*
2145 * This is called in transaction commmit time. If there are no orphan
2146 * files in the subvolume, it removes orphan item and frees block_rsv
2147 * structure.
2148 */
2149void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2150 struct btrfs_root *root)
2151{
2152 int ret;
2153
2154 if (!list_empty(&root->orphan_list) ||
2155 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2156 return;
2157
2158 if (root->orphan_item_inserted &&
2159 btrfs_root_refs(&root->root_item) > 0) {
2160 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2161 root->root_key.objectid);
2162 BUG_ON(ret);
2163 root->orphan_item_inserted = 0;
2164 }
2165
2166 if (root->orphan_block_rsv) {
2167 WARN_ON(root->orphan_block_rsv->size > 0);
2168 btrfs_free_block_rsv(root, root->orphan_block_rsv);
2169 root->orphan_block_rsv = NULL;
2170 }
2171}
2172
7b128766
JB
2173/*
2174 * This creates an orphan entry for the given inode in case something goes
2175 * wrong in the middle of an unlink/truncate.
d68fc57b
YZ
2176 *
2177 * NOTE: caller of this function should reserve 5 units of metadata for
2178 * this function.
7b128766
JB
2179 */
2180int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2181{
2182 struct btrfs_root *root = BTRFS_I(inode)->root;
d68fc57b
YZ
2183 struct btrfs_block_rsv *block_rsv = NULL;
2184 int reserve = 0;
2185 int insert = 0;
2186 int ret;
7b128766 2187
d68fc57b
YZ
2188 if (!root->orphan_block_rsv) {
2189 block_rsv = btrfs_alloc_block_rsv(root);
2190 BUG_ON(!block_rsv);
2191 }
7b128766 2192
d68fc57b
YZ
2193 spin_lock(&root->orphan_lock);
2194 if (!root->orphan_block_rsv) {
2195 root->orphan_block_rsv = block_rsv;
2196 } else if (block_rsv) {
2197 btrfs_free_block_rsv(root, block_rsv);
2198 block_rsv = NULL;
7b128766 2199 }
7b128766 2200
d68fc57b
YZ
2201 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
2202 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
2203#if 0
2204 /*
2205 * For proper ENOSPC handling, we should do orphan
2206 * cleanup when mounting. But this introduces backward
2207 * compatibility issue.
2208 */
2209 if (!xchg(&root->orphan_item_inserted, 1))
2210 insert = 2;
2211 else
2212 insert = 1;
2213#endif
2214 insert = 1;
2215 } else {
2216 WARN_ON(!BTRFS_I(inode)->orphan_meta_reserved);
7b128766
JB
2217 }
2218
d68fc57b
YZ
2219 if (!BTRFS_I(inode)->orphan_meta_reserved) {
2220 BTRFS_I(inode)->orphan_meta_reserved = 1;
2221 reserve = 1;
2222 }
2223 spin_unlock(&root->orphan_lock);
7b128766 2224
d68fc57b
YZ
2225 if (block_rsv)
2226 btrfs_add_durable_block_rsv(root->fs_info, block_rsv);
7b128766 2227
d68fc57b
YZ
2228 /* grab metadata reservation from transaction handle */
2229 if (reserve) {
2230 ret = btrfs_orphan_reserve_metadata(trans, inode);
2231 BUG_ON(ret);
2232 }
7b128766 2233
d68fc57b
YZ
2234 /* insert an orphan item to track this unlinked/truncated file */
2235 if (insert >= 1) {
2236 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
2237 BUG_ON(ret);
2238 }
2239
2240 /* insert an orphan item to track subvolume contains orphan files */
2241 if (insert >= 2) {
2242 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2243 root->root_key.objectid);
2244 BUG_ON(ret);
2245 }
2246 return 0;
7b128766
JB
2247}
2248
2249/*
2250 * We have done the truncate/delete so we can go ahead and remove the orphan
2251 * item for this particular inode.
2252 */
2253int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2254{
2255 struct btrfs_root *root = BTRFS_I(inode)->root;
d68fc57b
YZ
2256 int delete_item = 0;
2257 int release_rsv = 0;
7b128766
JB
2258 int ret = 0;
2259
d68fc57b
YZ
2260 spin_lock(&root->orphan_lock);
2261 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
2262 list_del_init(&BTRFS_I(inode)->i_orphan);
2263 delete_item = 1;
7b128766
JB
2264 }
2265
d68fc57b
YZ
2266 if (BTRFS_I(inode)->orphan_meta_reserved) {
2267 BTRFS_I(inode)->orphan_meta_reserved = 0;
2268 release_rsv = 1;
7b128766 2269 }
d68fc57b 2270 spin_unlock(&root->orphan_lock);
7b128766 2271
d68fc57b
YZ
2272 if (trans && delete_item) {
2273 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
2274 BUG_ON(ret);
2275 }
7b128766 2276
d68fc57b
YZ
2277 if (release_rsv)
2278 btrfs_orphan_release_metadata(inode);
7b128766 2279
d68fc57b 2280 return 0;
7b128766
JB
2281}
2282
2283/*
2284 * this cleans up any orphans that may be left on the list from the last use
2285 * of this root.
2286 */
2287void btrfs_orphan_cleanup(struct btrfs_root *root)
2288{
2289 struct btrfs_path *path;
2290 struct extent_buffer *leaf;
7b128766
JB
2291 struct btrfs_key key, found_key;
2292 struct btrfs_trans_handle *trans;
2293 struct inode *inode;
2294 int ret = 0, nr_unlink = 0, nr_truncate = 0;
2295
d68fc57b 2296 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
7b128766 2297 return;
c71bf099
YZ
2298
2299 path = btrfs_alloc_path();
2300 BUG_ON(!path);
7b128766
JB
2301 path->reada = -1;
2302
2303 key.objectid = BTRFS_ORPHAN_OBJECTID;
2304 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
2305 key.offset = (u64)-1;
2306
7b128766
JB
2307 while (1) {
2308 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2309 if (ret < 0) {
2310 printk(KERN_ERR "Error searching slot for orphan: %d"
2311 "\n", ret);
2312 break;
2313 }
2314
2315 /*
2316 * if ret == 0 means we found what we were searching for, which
2317 * is weird, but possible, so only screw with path if we didnt
2318 * find the key and see if we have stuff that matches
2319 */
2320 if (ret > 0) {
2321 if (path->slots[0] == 0)
2322 break;
2323 path->slots[0]--;
2324 }
2325
2326 /* pull out the item */
2327 leaf = path->nodes[0];
7b128766
JB
2328 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2329
2330 /* make sure the item matches what we want */
2331 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2332 break;
2333 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
2334 break;
2335
2336 /* release the path since we're done with it */
2337 btrfs_release_path(root, path);
2338
2339 /*
2340 * this is where we are basically btrfs_lookup, without the
2341 * crossing root thing. we store the inode number in the
2342 * offset of the orphan item.
2343 */
5d4f98a2
YZ
2344 found_key.objectid = found_key.offset;
2345 found_key.type = BTRFS_INODE_ITEM_KEY;
2346 found_key.offset = 0;
73f73415 2347 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
d68fc57b 2348 BUG_ON(IS_ERR(inode));
7b128766 2349
7b128766
JB
2350 /*
2351 * add this inode to the orphan list so btrfs_orphan_del does
2352 * the proper thing when we hit it
2353 */
d68fc57b 2354 spin_lock(&root->orphan_lock);
7b128766 2355 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
d68fc57b 2356 spin_unlock(&root->orphan_lock);
7b128766
JB
2357
2358 /*
2359 * if this is a bad inode, means we actually succeeded in
2360 * removing the inode, but not the orphan record, which means
2361 * we need to manually delete the orphan since iput will just
2362 * do a destroy_inode
2363 */
2364 if (is_bad_inode(inode)) {
a22285a6 2365 trans = btrfs_start_transaction(root, 0);
98d5dc13 2366 BUG_ON(IS_ERR(trans));
7b128766 2367 btrfs_orphan_del(trans, inode);
5b21f2ed 2368 btrfs_end_transaction(trans, root);
7b128766
JB
2369 iput(inode);
2370 continue;
2371 }
2372
2373 /* if we have links, this was a truncate, lets do that */
2374 if (inode->i_nlink) {
a41ad394
JB
2375 if (!S_ISREG(inode->i_mode)) {
2376 WARN_ON(1);
2377 iput(inode);
2378 continue;
2379 }
7b128766
JB
2380 nr_truncate++;
2381 btrfs_truncate(inode);
2382 } else {
2383 nr_unlink++;
2384 }
2385
2386 /* this will do delete_inode and everything for us */
2387 iput(inode);
2388 }
d68fc57b
YZ
2389 btrfs_free_path(path);
2390
2391 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
2392
2393 if (root->orphan_block_rsv)
2394 btrfs_block_rsv_release(root, root->orphan_block_rsv,
2395 (u64)-1);
2396
2397 if (root->orphan_block_rsv || root->orphan_item_inserted) {
2398 trans = btrfs_join_transaction(root, 1);
3612b495 2399 BUG_ON(IS_ERR(trans));
d68fc57b
YZ
2400 btrfs_end_transaction(trans, root);
2401 }
7b128766
JB
2402
2403 if (nr_unlink)
2404 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2405 if (nr_truncate)
2406 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
7b128766
JB
2407}
2408
46a53cca
CM
2409/*
2410 * very simple check to peek ahead in the leaf looking for xattrs. If we
2411 * don't find any xattrs, we know there can't be any acls.
2412 *
2413 * slot is the slot the inode is in, objectid is the objectid of the inode
2414 */
2415static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2416 int slot, u64 objectid)
2417{
2418 u32 nritems = btrfs_header_nritems(leaf);
2419 struct btrfs_key found_key;
2420 int scanned = 0;
2421
2422 slot++;
2423 while (slot < nritems) {
2424 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2425
2426 /* we found a different objectid, there must not be acls */
2427 if (found_key.objectid != objectid)
2428 return 0;
2429
2430 /* we found an xattr, assume we've got an acl */
2431 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
2432 return 1;
2433
2434 /*
2435 * we found a key greater than an xattr key, there can't
2436 * be any acls later on
2437 */
2438 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
2439 return 0;
2440
2441 slot++;
2442 scanned++;
2443
2444 /*
2445 * it goes inode, inode backrefs, xattrs, extents,
2446 * so if there are a ton of hard links to an inode there can
2447 * be a lot of backrefs. Don't waste time searching too hard,
2448 * this is just an optimization
2449 */
2450 if (scanned >= 8)
2451 break;
2452 }
2453 /* we hit the end of the leaf before we found an xattr or
2454 * something larger than an xattr. We have to assume the inode
2455 * has acls
2456 */
2457 return 1;
2458}
2459
d352ac68
CM
2460/*
2461 * read an inode from the btree into the in-memory inode
2462 */
5d4f98a2 2463static void btrfs_read_locked_inode(struct inode *inode)
39279cc3
CM
2464{
2465 struct btrfs_path *path;
5f39d397 2466 struct extent_buffer *leaf;
39279cc3 2467 struct btrfs_inode_item *inode_item;
0b86a832 2468 struct btrfs_timespec *tspec;
39279cc3
CM
2469 struct btrfs_root *root = BTRFS_I(inode)->root;
2470 struct btrfs_key location;
46a53cca 2471 int maybe_acls;
39279cc3 2472 u64 alloc_group_block;
618e21d5 2473 u32 rdev;
39279cc3
CM
2474 int ret;
2475
2476 path = btrfs_alloc_path();
2477 BUG_ON(!path);
39279cc3 2478 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
dc17ff8f 2479
39279cc3 2480 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
5f39d397 2481 if (ret)
39279cc3 2482 goto make_bad;
39279cc3 2483
5f39d397
CM
2484 leaf = path->nodes[0];
2485 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2486 struct btrfs_inode_item);
2487
2488 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
2489 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
2490 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
2491 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
dbe674a9 2492 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
5f39d397
CM
2493
2494 tspec = btrfs_inode_atime(inode_item);
2495 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2496 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2497
2498 tspec = btrfs_inode_mtime(inode_item);
2499 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2500 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2501
2502 tspec = btrfs_inode_ctime(inode_item);
2503 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2504 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2505
a76a3cd4 2506 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
e02119d5 2507 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
c3027eb5 2508 BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
e02119d5 2509 inode->i_generation = BTRFS_I(inode)->generation;
618e21d5 2510 inode->i_rdev = 0;
5f39d397
CM
2511 rdev = btrfs_inode_rdev(leaf, inode_item);
2512
aec7477b 2513 BTRFS_I(inode)->index_cnt = (u64)-1;
d2fb3437 2514 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
aec7477b 2515
5f39d397 2516 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
b4ce94de 2517
46a53cca
CM
2518 /*
2519 * try to precache a NULL acl entry for files that don't have
2520 * any xattrs or acls
2521 */
2522 maybe_acls = acls_after_inode_item(leaf, path->slots[0], inode->i_ino);
72c04902
AV
2523 if (!maybe_acls)
2524 cache_no_acl(inode);
46a53cca 2525
d2fb3437
YZ
2526 BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0,
2527 alloc_group_block, 0);
39279cc3
CM
2528 btrfs_free_path(path);
2529 inode_item = NULL;
2530
39279cc3 2531 switch (inode->i_mode & S_IFMT) {
39279cc3
CM
2532 case S_IFREG:
2533 inode->i_mapping->a_ops = &btrfs_aops;
04160088 2534 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
d1310b2e 2535 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
39279cc3
CM
2536 inode->i_fop = &btrfs_file_operations;
2537 inode->i_op = &btrfs_file_inode_operations;
2538 break;
2539 case S_IFDIR:
2540 inode->i_fop = &btrfs_dir_file_operations;
2541 if (root == root->fs_info->tree_root)
2542 inode->i_op = &btrfs_dir_ro_inode_operations;
2543 else
2544 inode->i_op = &btrfs_dir_inode_operations;
2545 break;
2546 case S_IFLNK:
2547 inode->i_op = &btrfs_symlink_inode_operations;
2548 inode->i_mapping->a_ops = &btrfs_symlink_aops;
04160088 2549 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
39279cc3 2550 break;
618e21d5 2551 default:
0279b4cd 2552 inode->i_op = &btrfs_special_inode_operations;
618e21d5
JB
2553 init_special_inode(inode, inode->i_mode, rdev);
2554 break;
39279cc3 2555 }
6cbff00f
CH
2556
2557 btrfs_update_iflags(inode);
39279cc3
CM
2558 return;
2559
2560make_bad:
39279cc3 2561 btrfs_free_path(path);
39279cc3
CM
2562 make_bad_inode(inode);
2563}
2564
d352ac68
CM
2565/*
2566 * given a leaf and an inode, copy the inode fields into the leaf
2567 */
e02119d5
CM
2568static void fill_inode_item(struct btrfs_trans_handle *trans,
2569 struct extent_buffer *leaf,
5f39d397 2570 struct btrfs_inode_item *item,
39279cc3
CM
2571 struct inode *inode)
2572{
5f39d397
CM
2573 btrfs_set_inode_uid(leaf, item, inode->i_uid);
2574 btrfs_set_inode_gid(leaf, item, inode->i_gid);
dbe674a9 2575 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
5f39d397
CM
2576 btrfs_set_inode_mode(leaf, item, inode->i_mode);
2577 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2578
2579 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2580 inode->i_atime.tv_sec);
2581 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2582 inode->i_atime.tv_nsec);
2583
2584 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2585 inode->i_mtime.tv_sec);
2586 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2587 inode->i_mtime.tv_nsec);
2588
2589 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2590 inode->i_ctime.tv_sec);
2591 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2592 inode->i_ctime.tv_nsec);
2593
a76a3cd4 2594 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
e02119d5 2595 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
c3027eb5 2596 btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
e02119d5 2597 btrfs_set_inode_transid(leaf, item, trans->transid);
5f39d397 2598 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
b98b6767 2599 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
d2fb3437 2600 btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group);
39279cc3
CM
2601}
2602
d352ac68
CM
2603/*
2604 * copy everything in the in-memory inode into the btree.
2605 */
d397712b
CM
2606noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2607 struct btrfs_root *root, struct inode *inode)
39279cc3
CM
2608{
2609 struct btrfs_inode_item *inode_item;
2610 struct btrfs_path *path;
5f39d397 2611 struct extent_buffer *leaf;
39279cc3
CM
2612 int ret;
2613
2614 path = btrfs_alloc_path();
2615 BUG_ON(!path);
b9473439 2616 path->leave_spinning = 1;
39279cc3
CM
2617 ret = btrfs_lookup_inode(trans, root, path,
2618 &BTRFS_I(inode)->location, 1);
2619 if (ret) {
2620 if (ret > 0)
2621 ret = -ENOENT;
2622 goto failed;
2623 }
2624
b4ce94de 2625 btrfs_unlock_up_safe(path, 1);
5f39d397
CM
2626 leaf = path->nodes[0];
2627 inode_item = btrfs_item_ptr(leaf, path->slots[0],
39279cc3
CM
2628 struct btrfs_inode_item);
2629
e02119d5 2630 fill_inode_item(trans, leaf, inode_item, inode);
5f39d397 2631 btrfs_mark_buffer_dirty(leaf);
15ee9bc7 2632 btrfs_set_inode_last_trans(trans, inode);
39279cc3
CM
2633 ret = 0;
2634failed:
39279cc3
CM
2635 btrfs_free_path(path);
2636 return ret;
2637}
2638
2639
d352ac68
CM
2640/*
2641 * unlink helper that gets used here in inode.c and in the tree logging
2642 * recovery code. It remove a link in a directory with a given name, and
2643 * also drops the back refs in the inode to the directory
2644 */
e02119d5
CM
2645int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2646 struct btrfs_root *root,
2647 struct inode *dir, struct inode *inode,
2648 const char *name, int name_len)
39279cc3
CM
2649{
2650 struct btrfs_path *path;
39279cc3 2651 int ret = 0;
5f39d397 2652 struct extent_buffer *leaf;
39279cc3 2653 struct btrfs_dir_item *di;
5f39d397 2654 struct btrfs_key key;
aec7477b 2655 u64 index;
39279cc3
CM
2656
2657 path = btrfs_alloc_path();
54aa1f4d
CM
2658 if (!path) {
2659 ret = -ENOMEM;
554233a6 2660 goto out;
54aa1f4d
CM
2661 }
2662
b9473439 2663 path->leave_spinning = 1;
39279cc3
CM
2664 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2665 name, name_len, -1);
2666 if (IS_ERR(di)) {
2667 ret = PTR_ERR(di);
2668 goto err;
2669 }
2670 if (!di) {
2671 ret = -ENOENT;
2672 goto err;
2673 }
5f39d397
CM
2674 leaf = path->nodes[0];
2675 btrfs_dir_item_key_to_cpu(leaf, di, &key);
39279cc3 2676 ret = btrfs_delete_one_dir_name(trans, root, path, di);
54aa1f4d
CM
2677 if (ret)
2678 goto err;
39279cc3
CM
2679 btrfs_release_path(root, path);
2680
aec7477b 2681 ret = btrfs_del_inode_ref(trans, root, name, name_len,
e02119d5
CM
2682 inode->i_ino,
2683 dir->i_ino, &index);
aec7477b 2684 if (ret) {
d397712b 2685 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
aec7477b 2686 "inode %lu parent %lu\n", name_len, name,
e02119d5 2687 inode->i_ino, dir->i_ino);
aec7477b
JB
2688 goto err;
2689 }
2690
39279cc3 2691 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
aec7477b 2692 index, name, name_len, -1);
39279cc3
CM
2693 if (IS_ERR(di)) {
2694 ret = PTR_ERR(di);
2695 goto err;
2696 }
2697 if (!di) {
2698 ret = -ENOENT;
2699 goto err;
2700 }
2701 ret = btrfs_delete_one_dir_name(trans, root, path, di);
925baedd 2702 btrfs_release_path(root, path);
39279cc3 2703
e02119d5
CM
2704 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
2705 inode, dir->i_ino);
49eb7e46 2706 BUG_ON(ret != 0 && ret != -ENOENT);
e02119d5
CM
2707
2708 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2709 dir, index);
6418c961
CM
2710 if (ret == -ENOENT)
2711 ret = 0;
39279cc3
CM
2712err:
2713 btrfs_free_path(path);
e02119d5
CM
2714 if (ret)
2715 goto out;
2716
2717 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2718 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2719 btrfs_update_inode(trans, root, dir);
2720 btrfs_drop_nlink(inode);
2721 ret = btrfs_update_inode(trans, root, inode);
e02119d5 2722out:
39279cc3
CM
2723 return ret;
2724}
2725
a22285a6
YZ
2726/* helper to check if there is any shared block in the path */
2727static int check_path_shared(struct btrfs_root *root,
2728 struct btrfs_path *path)
39279cc3 2729{
a22285a6
YZ
2730 struct extent_buffer *eb;
2731 int level;
0e4dcbef 2732 u64 refs = 1;
5df6a9f6 2733
a22285a6 2734 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
dedefd72
JB
2735 int ret;
2736
a22285a6
YZ
2737 if (!path->nodes[level])
2738 break;
2739 eb = path->nodes[level];
2740 if (!btrfs_block_can_be_shared(root, eb))
2741 continue;
2742 ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
2743 &refs, NULL);
2744 if (refs > 1)
2745 return 1;
5df6a9f6 2746 }
dedefd72 2747 return 0;
39279cc3
CM
2748}
2749
a22285a6
YZ
2750/*
2751 * helper to start transaction for unlink and rmdir.
2752 *
2753 * unlink and rmdir are special in btrfs, they do not always free space.
2754 * so in enospc case, we should make sure they will free space before
2755 * allowing them to use the global metadata reservation.
2756 */
2757static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2758 struct dentry *dentry)
4df27c4d 2759{
39279cc3 2760 struct btrfs_trans_handle *trans;
a22285a6 2761 struct btrfs_root *root = BTRFS_I(dir)->root;
4df27c4d 2762 struct btrfs_path *path;
a22285a6 2763 struct btrfs_inode_ref *ref;
4df27c4d 2764 struct btrfs_dir_item *di;
7b128766 2765 struct inode *inode = dentry->d_inode;
4df27c4d 2766 u64 index;
a22285a6
YZ
2767 int check_link = 1;
2768 int err = -ENOSPC;
4df27c4d
YZ
2769 int ret;
2770
a22285a6
YZ
2771 trans = btrfs_start_transaction(root, 10);
2772 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
2773 return trans;
4df27c4d 2774
a22285a6
YZ
2775 if (inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
2776 return ERR_PTR(-ENOSPC);
4df27c4d 2777
a22285a6
YZ
2778 /* check if there is someone else holds reference */
2779 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
2780 return ERR_PTR(-ENOSPC);
4df27c4d 2781
a22285a6
YZ
2782 if (atomic_read(&inode->i_count) > 2)
2783 return ERR_PTR(-ENOSPC);
4df27c4d 2784
a22285a6
YZ
2785 if (xchg(&root->fs_info->enospc_unlink, 1))
2786 return ERR_PTR(-ENOSPC);
2787
2788 path = btrfs_alloc_path();
2789 if (!path) {
2790 root->fs_info->enospc_unlink = 0;
2791 return ERR_PTR(-ENOMEM);
4df27c4d
YZ
2792 }
2793
a22285a6 2794 trans = btrfs_start_transaction(root, 0);
5df6a9f6 2795 if (IS_ERR(trans)) {
a22285a6
YZ
2796 btrfs_free_path(path);
2797 root->fs_info->enospc_unlink = 0;
2798 return trans;
2799 }
4df27c4d 2800
a22285a6
YZ
2801 path->skip_locking = 1;
2802 path->search_commit_root = 1;
4df27c4d 2803
a22285a6
YZ
2804 ret = btrfs_lookup_inode(trans, root, path,
2805 &BTRFS_I(dir)->location, 0);
2806 if (ret < 0) {
2807 err = ret;
2808 goto out;
2809 }
2810 if (ret == 0) {
2811 if (check_path_shared(root, path))
2812 goto out;
2813 } else {
2814 check_link = 0;
5df6a9f6 2815 }
a22285a6
YZ
2816 btrfs_release_path(root, path);
2817
2818 ret = btrfs_lookup_inode(trans, root, path,
2819 &BTRFS_I(inode)->location, 0);
2820 if (ret < 0) {
2821 err = ret;
2822 goto out;
2823 }
2824 if (ret == 0) {
2825 if (check_path_shared(root, path))
2826 goto out;
2827 } else {
2828 check_link = 0;
2829 }
2830 btrfs_release_path(root, path);
2831
2832 if (ret == 0 && S_ISREG(inode->i_mode)) {
2833 ret = btrfs_lookup_file_extent(trans, root, path,
2834 inode->i_ino, (u64)-1, 0);
2835 if (ret < 0) {
2836 err = ret;
2837 goto out;
2838 }
2839 BUG_ON(ret == 0);
2840 if (check_path_shared(root, path))
2841 goto out;
2842 btrfs_release_path(root, path);
2843 }
2844
2845 if (!check_link) {
2846 err = 0;
2847 goto out;
2848 }
2849
2850 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2851 dentry->d_name.name, dentry->d_name.len, 0);
2852 if (IS_ERR(di)) {
2853 err = PTR_ERR(di);
2854 goto out;
2855 }
2856 if (di) {
2857 if (check_path_shared(root, path))
2858 goto out;
2859 } else {
2860 err = 0;
2861 goto out;
2862 }
2863 btrfs_release_path(root, path);
2864
2865 ref = btrfs_lookup_inode_ref(trans, root, path,
2866 dentry->d_name.name, dentry->d_name.len,
2867 inode->i_ino, dir->i_ino, 0);
2868 if (IS_ERR(ref)) {
2869 err = PTR_ERR(ref);
2870 goto out;
2871 }
2872 BUG_ON(!ref);
2873 if (check_path_shared(root, path))
2874 goto out;
2875 index = btrfs_inode_ref_index(path->nodes[0], ref);
2876 btrfs_release_path(root, path);
2877
2878 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, index,
2879 dentry->d_name.name, dentry->d_name.len, 0);
2880 if (IS_ERR(di)) {
2881 err = PTR_ERR(di);
2882 goto out;
2883 }
2884 BUG_ON(ret == -ENOENT);
2885 if (check_path_shared(root, path))
2886 goto out;
2887
2888 err = 0;
2889out:
2890 btrfs_free_path(path);
2891 if (err) {
2892 btrfs_end_transaction(trans, root);
2893 root->fs_info->enospc_unlink = 0;
2894 return ERR_PTR(err);
2895 }
2896
2897 trans->block_rsv = &root->fs_info->global_block_rsv;
2898 return trans;
2899}
2900
2901static void __unlink_end_trans(struct btrfs_trans_handle *trans,
2902 struct btrfs_root *root)
2903{
2904 if (trans->block_rsv == &root->fs_info->global_block_rsv) {
2905 BUG_ON(!root->fs_info->enospc_unlink);
2906 root->fs_info->enospc_unlink = 0;
2907 }
2908 btrfs_end_transaction_throttle(trans, root);
2909}
2910
2911static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2912{
2913 struct btrfs_root *root = BTRFS_I(dir)->root;
2914 struct btrfs_trans_handle *trans;
2915 struct inode *inode = dentry->d_inode;
2916 int ret;
2917 unsigned long nr = 0;
2918
2919 trans = __unlink_start_trans(dir, dentry);
2920 if (IS_ERR(trans))
2921 return PTR_ERR(trans);
5f39d397 2922
39279cc3 2923 btrfs_set_trans_block_group(trans, dir);
12fcfd22
CM
2924
2925 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
2926
e02119d5
CM
2927 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2928 dentry->d_name.name, dentry->d_name.len);
a22285a6 2929 BUG_ON(ret);
7b128766 2930
a22285a6 2931 if (inode->i_nlink == 0) {
7b128766 2932 ret = btrfs_orphan_add(trans, inode);
a22285a6
YZ
2933 BUG_ON(ret);
2934 }
7b128766 2935
d3c2fdcf 2936 nr = trans->blocks_used;
a22285a6 2937 __unlink_end_trans(trans, root);
d3c2fdcf 2938 btrfs_btree_balance_dirty(root, nr);
39279cc3
CM
2939 return ret;
2940}
2941
4df27c4d
YZ
2942int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
2943 struct btrfs_root *root,
2944 struct inode *dir, u64 objectid,
2945 const char *name, int name_len)
2946{
2947 struct btrfs_path *path;
2948 struct extent_buffer *leaf;
2949 struct btrfs_dir_item *di;
2950 struct btrfs_key key;
2951 u64 index;
2952 int ret;
2953
2954 path = btrfs_alloc_path();
2955 if (!path)
2956 return -ENOMEM;
2957
2958 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2959 name, name_len, -1);
2960 BUG_ON(!di || IS_ERR(di));
2961
2962 leaf = path->nodes[0];
2963 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2964 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
2965 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2966 BUG_ON(ret);
2967 btrfs_release_path(root, path);
2968
2969 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
2970 objectid, root->root_key.objectid,
2971 dir->i_ino, &index, name, name_len);
2972 if (ret < 0) {
2973 BUG_ON(ret != -ENOENT);
2974 di = btrfs_search_dir_index_item(root, path, dir->i_ino,
2975 name, name_len);
2976 BUG_ON(!di || IS_ERR(di));
2977
2978 leaf = path->nodes[0];
2979 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2980 btrfs_release_path(root, path);
2981 index = key.offset;
2982 }
2983
2984 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
2985 index, name, name_len, -1);
2986 BUG_ON(!di || IS_ERR(di));
2987
2988 leaf = path->nodes[0];
2989 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2990 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
2991 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2992 BUG_ON(ret);
2993 btrfs_release_path(root, path);
2994
2995 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2996 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2997 ret = btrfs_update_inode(trans, root, dir);
2998 BUG_ON(ret);
4df27c4d
YZ
2999
3000 btrfs_free_path(path);
3001 return 0;
3002}
3003
39279cc3
CM
3004static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3005{
3006 struct inode *inode = dentry->d_inode;
1832a6d5 3007 int err = 0;
39279cc3 3008 struct btrfs_root *root = BTRFS_I(dir)->root;
39279cc3 3009 struct btrfs_trans_handle *trans;
1832a6d5 3010 unsigned long nr = 0;
39279cc3 3011
3394e160 3012 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
4df27c4d 3013 inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
134d4512
Y
3014 return -ENOTEMPTY;
3015
a22285a6
YZ
3016 trans = __unlink_start_trans(dir, dentry);
3017 if (IS_ERR(trans))
5df6a9f6 3018 return PTR_ERR(trans);
5df6a9f6 3019
39279cc3 3020 btrfs_set_trans_block_group(trans, dir);
39279cc3 3021
4df27c4d
YZ
3022 if (unlikely(inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
3023 err = btrfs_unlink_subvol(trans, root, dir,
3024 BTRFS_I(inode)->location.objectid,
3025 dentry->d_name.name,
3026 dentry->d_name.len);
3027 goto out;
3028 }
3029
7b128766
JB
3030 err = btrfs_orphan_add(trans, inode);
3031 if (err)
4df27c4d 3032 goto out;
7b128766 3033
39279cc3 3034 /* now the directory is empty */
e02119d5
CM
3035 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3036 dentry->d_name.name, dentry->d_name.len);
d397712b 3037 if (!err)
dbe674a9 3038 btrfs_i_size_write(inode, 0);
4df27c4d 3039out:
d3c2fdcf 3040 nr = trans->blocks_used;
a22285a6 3041 __unlink_end_trans(trans, root);
d3c2fdcf 3042 btrfs_btree_balance_dirty(root, nr);
3954401f 3043
39279cc3
CM
3044 return err;
3045}
3046
d20f7043 3047#if 0
323ac95b
CM
3048/*
3049 * when truncating bytes in a file, it is possible to avoid reading
3050 * the leaves that contain only checksum items. This can be the
3051 * majority of the IO required to delete a large file, but it must
3052 * be done carefully.
3053 *
3054 * The keys in the level just above the leaves are checked to make sure
3055 * the lowest key in a given leaf is a csum key, and starts at an offset
3056 * after the new size.
3057 *
3058 * Then the key for the next leaf is checked to make sure it also has
3059 * a checksum item for the same file. If it does, we know our target leaf
3060 * contains only checksum items, and it can be safely freed without reading
3061 * it.
3062 *
3063 * This is just an optimization targeted at large files. It may do
3064 * nothing. It will return 0 unless things went badly.
3065 */
3066static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
3067 struct btrfs_root *root,
3068 struct btrfs_path *path,
3069 struct inode *inode, u64 new_size)
3070{
3071 struct btrfs_key key;
3072 int ret;
3073 int nritems;
3074 struct btrfs_key found_key;
3075 struct btrfs_key other_key;
5b84e8d6
YZ
3076 struct btrfs_leaf_ref *ref;
3077 u64 leaf_gen;
3078 u64 leaf_start;
323ac95b
CM
3079
3080 path->lowest_level = 1;
3081 key.objectid = inode->i_ino;
3082 key.type = BTRFS_CSUM_ITEM_KEY;
3083 key.offset = new_size;
3084again:
3085 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3086 if (ret < 0)
3087 goto out;
3088
3089 if (path->nodes[1] == NULL) {
3090 ret = 0;
3091 goto out;
3092 }
3093 ret = 0;
3094 btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
3095 nritems = btrfs_header_nritems(path->nodes[1]);
3096
3097 if (!nritems)
3098 goto out;
3099
3100 if (path->slots[1] >= nritems)
3101 goto next_node;
3102
3103 /* did we find a key greater than anything we want to delete? */
3104 if (found_key.objectid > inode->i_ino ||
3105 (found_key.objectid == inode->i_ino && found_key.type > key.type))
3106 goto out;
3107
3108 /* we check the next key in the node to make sure the leave contains
3109 * only checksum items. This comparison doesn't work if our
3110 * leaf is the last one in the node
3111 */
3112 if (path->slots[1] + 1 >= nritems) {
3113next_node:
3114 /* search forward from the last key in the node, this
3115 * will bring us into the next node in the tree
3116 */
3117 btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
3118
3119 /* unlikely, but we inc below, so check to be safe */
3120 if (found_key.offset == (u64)-1)
3121 goto out;
3122
3123 /* search_forward needs a path with locks held, do the
3124 * search again for the original key. It is possible
3125 * this will race with a balance and return a path that
3126 * we could modify, but this drop is just an optimization
3127 * and is allowed to miss some leaves.
3128 */
3129 btrfs_release_path(root, path);
3130 found_key.offset++;
3131
3132 /* setup a max key for search_forward */
3133 other_key.offset = (u64)-1;
3134 other_key.type = key.type;
3135 other_key.objectid = key.objectid;
3136
3137 path->keep_locks = 1;
3138 ret = btrfs_search_forward(root, &found_key, &other_key,
3139 path, 0, 0);
3140 path->keep_locks = 0;
3141 if (ret || found_key.objectid != key.objectid ||
3142 found_key.type != key.type) {
3143 ret = 0;
3144 goto out;
3145 }
3146
3147 key.offset = found_key.offset;
3148 btrfs_release_path(root, path);
3149 cond_resched();
3150 goto again;
3151 }
3152
3153 /* we know there's one more slot after us in the tree,
3154 * read that key so we can verify it is also a checksum item
3155 */
3156 btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
3157
3158 if (found_key.objectid < inode->i_ino)
3159 goto next_key;
3160
3161 if (found_key.type != key.type || found_key.offset < new_size)
3162 goto next_key;
3163
3164 /*
3165 * if the key for the next leaf isn't a csum key from this objectid,
3166 * we can't be sure there aren't good items inside this leaf.
3167 * Bail out
3168 */
3169 if (other_key.objectid != inode->i_ino || other_key.type != key.type)
3170 goto out;
3171
5b84e8d6
YZ
3172 leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
3173 leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
323ac95b
CM
3174 /*
3175 * it is safe to delete this leaf, it contains only
3176 * csum items from this inode at an offset >= new_size
3177 */
5b84e8d6 3178 ret = btrfs_del_leaf(trans, root, path, leaf_start);
323ac95b
CM
3179 BUG_ON(ret);
3180
5b84e8d6
YZ
3181 if (root->ref_cows && leaf_gen < trans->transid) {
3182 ref = btrfs_alloc_leaf_ref(root, 0);
3183 if (ref) {
3184 ref->root_gen = root->root_key.offset;
3185 ref->bytenr = leaf_start;
3186 ref->owner = 0;
3187 ref->generation = leaf_gen;
3188 ref->nritems = 0;
3189
bd56b302
CM
3190 btrfs_sort_leaf_ref(ref);
3191
5b84e8d6
YZ
3192 ret = btrfs_add_leaf_ref(root, ref, 0);
3193 WARN_ON(ret);
3194 btrfs_free_leaf_ref(root, ref);
3195 } else {
3196 WARN_ON(1);
3197 }
3198 }
323ac95b
CM
3199next_key:
3200 btrfs_release_path(root, path);
3201
3202 if (other_key.objectid == inode->i_ino &&
3203 other_key.type == key.type && other_key.offset > key.offset) {
3204 key.offset = other_key.offset;
3205 cond_resched();
3206 goto again;
3207 }
3208 ret = 0;
3209out:
3210 /* fixup any changes we've made to the path */
3211 path->lowest_level = 0;
3212 path->keep_locks = 0;
3213 btrfs_release_path(root, path);
3214 return ret;
3215}
3216
d20f7043
CM
3217#endif
3218
39279cc3
CM
3219/*
3220 * this can truncate away extent items, csum items and directory items.
3221 * It starts at a high offset and removes keys until it can't find
d352ac68 3222 * any higher than new_size
39279cc3
CM
3223 *
3224 * csum items that cross the new i_size are truncated to the new size
3225 * as well.
7b128766
JB
3226 *
3227 * min_type is the minimum key type to truncate down to. If set to 0, this
3228 * will kill all the items on this inode, including the INODE_ITEM_KEY.
39279cc3 3229 */
8082510e
YZ
3230int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3231 struct btrfs_root *root,
3232 struct inode *inode,
3233 u64 new_size, u32 min_type)
39279cc3 3234{
39279cc3 3235 struct btrfs_path *path;
5f39d397 3236 struct extent_buffer *leaf;
39279cc3 3237 struct btrfs_file_extent_item *fi;
8082510e
YZ
3238 struct btrfs_key key;
3239 struct btrfs_key found_key;
39279cc3 3240 u64 extent_start = 0;
db94535d 3241 u64 extent_num_bytes = 0;
5d4f98a2 3242 u64 extent_offset = 0;
39279cc3 3243 u64 item_end = 0;
8082510e
YZ
3244 u64 mask = root->sectorsize - 1;
3245 u32 found_type = (u8)-1;
39279cc3
CM
3246 int found_extent;
3247 int del_item;
85e21bac
CM
3248 int pending_del_nr = 0;
3249 int pending_del_slot = 0;
179e29e4 3250 int extent_type = -1;
771ed689 3251 int encoding;
8082510e
YZ
3252 int ret;
3253 int err = 0;
3254
3255 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
39279cc3 3256
0af3d00b 3257 if (root->ref_cows || root == root->fs_info->tree_root)
5b21f2ed 3258 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
8082510e 3259
39279cc3
CM
3260 path = btrfs_alloc_path();
3261 BUG_ON(!path);
33c17ad5 3262 path->reada = -1;
5f39d397 3263
39279cc3
CM
3264 key.objectid = inode->i_ino;
3265 key.offset = (u64)-1;
5f39d397
CM
3266 key.type = (u8)-1;
3267
85e21bac 3268search_again:
b9473439 3269 path->leave_spinning = 1;
85e21bac 3270 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
8082510e
YZ
3271 if (ret < 0) {
3272 err = ret;
3273 goto out;
3274 }
d397712b 3275
85e21bac 3276 if (ret > 0) {
e02119d5
CM
3277 /* there are no items in the tree for us to truncate, we're
3278 * done
3279 */
8082510e
YZ
3280 if (path->slots[0] == 0)
3281 goto out;
85e21bac
CM
3282 path->slots[0]--;
3283 }
3284
d397712b 3285 while (1) {
39279cc3 3286 fi = NULL;
5f39d397
CM
3287 leaf = path->nodes[0];
3288 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3289 found_type = btrfs_key_type(&found_key);
771ed689 3290 encoding = 0;
39279cc3 3291
5f39d397 3292 if (found_key.objectid != inode->i_ino)
39279cc3 3293 break;
5f39d397 3294
85e21bac 3295 if (found_type < min_type)
39279cc3
CM
3296 break;
3297
5f39d397 3298 item_end = found_key.offset;
39279cc3 3299 if (found_type == BTRFS_EXTENT_DATA_KEY) {
5f39d397 3300 fi = btrfs_item_ptr(leaf, path->slots[0],
39279cc3 3301 struct btrfs_file_extent_item);
179e29e4 3302 extent_type = btrfs_file_extent_type(leaf, fi);
771ed689
CM
3303 encoding = btrfs_file_extent_compression(leaf, fi);
3304 encoding |= btrfs_file_extent_encryption(leaf, fi);
3305 encoding |= btrfs_file_extent_other_encoding(leaf, fi);
3306
179e29e4 3307 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
5f39d397 3308 item_end +=
db94535d 3309 btrfs_file_extent_num_bytes(leaf, fi);
179e29e4 3310 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
179e29e4 3311 item_end += btrfs_file_extent_inline_len(leaf,
c8b97818 3312 fi);
39279cc3 3313 }
008630c1 3314 item_end--;
39279cc3 3315 }
8082510e
YZ
3316 if (found_type > min_type) {
3317 del_item = 1;
3318 } else {
3319 if (item_end < new_size)
b888db2b 3320 break;
8082510e
YZ
3321 if (found_key.offset >= new_size)
3322 del_item = 1;
3323 else
3324 del_item = 0;
39279cc3 3325 }
39279cc3 3326 found_extent = 0;
39279cc3 3327 /* FIXME, shrink the extent if the ref count is only 1 */
179e29e4
CM
3328 if (found_type != BTRFS_EXTENT_DATA_KEY)
3329 goto delete;
3330
3331 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
39279cc3 3332 u64 num_dec;
db94535d 3333 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
771ed689 3334 if (!del_item && !encoding) {
db94535d
CM
3335 u64 orig_num_bytes =
3336 btrfs_file_extent_num_bytes(leaf, fi);
e02119d5 3337 extent_num_bytes = new_size -
5f39d397 3338 found_key.offset + root->sectorsize - 1;
b1632b10
Y
3339 extent_num_bytes = extent_num_bytes &
3340 ~((u64)root->sectorsize - 1);
db94535d
CM
3341 btrfs_set_file_extent_num_bytes(leaf, fi,
3342 extent_num_bytes);
3343 num_dec = (orig_num_bytes -
9069218d 3344 extent_num_bytes);
e02119d5 3345 if (root->ref_cows && extent_start != 0)
a76a3cd4 3346 inode_sub_bytes(inode, num_dec);
5f39d397 3347 btrfs_mark_buffer_dirty(leaf);
39279cc3 3348 } else {
db94535d
CM
3349 extent_num_bytes =
3350 btrfs_file_extent_disk_num_bytes(leaf,
3351 fi);
5d4f98a2
YZ
3352 extent_offset = found_key.offset -
3353 btrfs_file_extent_offset(leaf, fi);
3354
39279cc3 3355 /* FIXME blocksize != 4096 */
9069218d 3356 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
39279cc3
CM
3357 if (extent_start != 0) {
3358 found_extent = 1;
e02119d5 3359 if (root->ref_cows)
a76a3cd4 3360 inode_sub_bytes(inode, num_dec);
e02119d5 3361 }
39279cc3 3362 }
9069218d 3363 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
c8b97818
CM
3364 /*
3365 * we can't truncate inline items that have had
3366 * special encodings
3367 */
3368 if (!del_item &&
3369 btrfs_file_extent_compression(leaf, fi) == 0 &&
3370 btrfs_file_extent_encryption(leaf, fi) == 0 &&
3371 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
e02119d5
CM
3372 u32 size = new_size - found_key.offset;
3373
3374 if (root->ref_cows) {
a76a3cd4
YZ
3375 inode_sub_bytes(inode, item_end + 1 -
3376 new_size);
e02119d5
CM
3377 }
3378 size =
3379 btrfs_file_extent_calc_inline_size(size);
9069218d 3380 ret = btrfs_truncate_item(trans, root, path,
e02119d5 3381 size, 1);
9069218d 3382 BUG_ON(ret);
e02119d5 3383 } else if (root->ref_cows) {
a76a3cd4
YZ
3384 inode_sub_bytes(inode, item_end + 1 -
3385 found_key.offset);
9069218d 3386 }
39279cc3 3387 }
179e29e4 3388delete:
39279cc3 3389 if (del_item) {
85e21bac
CM
3390 if (!pending_del_nr) {
3391 /* no pending yet, add ourselves */
3392 pending_del_slot = path->slots[0];
3393 pending_del_nr = 1;
3394 } else if (pending_del_nr &&
3395 path->slots[0] + 1 == pending_del_slot) {
3396 /* hop on the pending chunk */
3397 pending_del_nr++;
3398 pending_del_slot = path->slots[0];
3399 } else {
d397712b 3400 BUG();
85e21bac 3401 }
39279cc3
CM
3402 } else {
3403 break;
3404 }
0af3d00b
JB
3405 if (found_extent && (root->ref_cows ||
3406 root == root->fs_info->tree_root)) {
b9473439 3407 btrfs_set_path_blocking(path);
39279cc3 3408 ret = btrfs_free_extent(trans, root, extent_start,
5d4f98a2
YZ
3409 extent_num_bytes, 0,
3410 btrfs_header_owner(leaf),
3411 inode->i_ino, extent_offset);
39279cc3
CM
3412 BUG_ON(ret);
3413 }
85e21bac 3414
8082510e
YZ
3415 if (found_type == BTRFS_INODE_ITEM_KEY)
3416 break;
3417
3418 if (path->slots[0] == 0 ||
3419 path->slots[0] != pending_del_slot) {
3420 if (root->ref_cows) {
3421 err = -EAGAIN;
3422 goto out;
3423 }
3424 if (pending_del_nr) {
3425 ret = btrfs_del_items(trans, root, path,
3426 pending_del_slot,
3427 pending_del_nr);
3428 BUG_ON(ret);
3429 pending_del_nr = 0;
3430 }
85e21bac
CM
3431 btrfs_release_path(root, path);
3432 goto search_again;
8082510e
YZ
3433 } else {
3434 path->slots[0]--;
85e21bac 3435 }
39279cc3 3436 }
8082510e 3437out:
85e21bac
CM
3438 if (pending_del_nr) {
3439 ret = btrfs_del_items(trans, root, path, pending_del_slot,
3440 pending_del_nr);
d68fc57b 3441 BUG_ON(ret);
85e21bac 3442 }
39279cc3 3443 btrfs_free_path(path);
8082510e 3444 return err;
39279cc3
CM
3445}
3446
3447/*
3448 * taken from block_truncate_page, but does cow as it zeros out
3449 * any bytes left in the last page in the file.
3450 */
3451static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
3452{
3453 struct inode *inode = mapping->host;
db94535d 3454 struct btrfs_root *root = BTRFS_I(inode)->root;
e6dcd2dc
CM
3455 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3456 struct btrfs_ordered_extent *ordered;
2ac55d41 3457 struct extent_state *cached_state = NULL;
e6dcd2dc 3458 char *kaddr;
db94535d 3459 u32 blocksize = root->sectorsize;
39279cc3
CM
3460 pgoff_t index = from >> PAGE_CACHE_SHIFT;
3461 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3462 struct page *page;
39279cc3 3463 int ret = 0;
a52d9a80 3464 u64 page_start;
e6dcd2dc 3465 u64 page_end;
39279cc3
CM
3466
3467 if ((offset & (blocksize - 1)) == 0)
3468 goto out;
0ca1f7ce 3469 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
5d5e103a
JB
3470 if (ret)
3471 goto out;
39279cc3
CM
3472
3473 ret = -ENOMEM;
211c17f5 3474again:
39279cc3 3475 page = grab_cache_page(mapping, index);
5d5e103a 3476 if (!page) {
0ca1f7ce 3477 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
39279cc3 3478 goto out;
5d5e103a 3479 }
e6dcd2dc
CM
3480
3481 page_start = page_offset(page);
3482 page_end = page_start + PAGE_CACHE_SIZE - 1;
3483
39279cc3 3484 if (!PageUptodate(page)) {
9ebefb18 3485 ret = btrfs_readpage(NULL, page);
39279cc3 3486 lock_page(page);
211c17f5
CM
3487 if (page->mapping != mapping) {
3488 unlock_page(page);
3489 page_cache_release(page);
3490 goto again;
3491 }
39279cc3
CM
3492 if (!PageUptodate(page)) {
3493 ret = -EIO;
89642229 3494 goto out_unlock;
39279cc3
CM
3495 }
3496 }
211c17f5 3497 wait_on_page_writeback(page);
e6dcd2dc 3498
2ac55d41
JB
3499 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state,
3500 GFP_NOFS);
e6dcd2dc
CM
3501 set_page_extent_mapped(page);
3502
3503 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3504 if (ordered) {
2ac55d41
JB
3505 unlock_extent_cached(io_tree, page_start, page_end,
3506 &cached_state, GFP_NOFS);
e6dcd2dc
CM
3507 unlock_page(page);
3508 page_cache_release(page);
eb84ae03 3509 btrfs_start_ordered_extent(inode, ordered, 1);
e6dcd2dc
CM
3510 btrfs_put_ordered_extent(ordered);
3511 goto again;
3512 }
3513
2ac55d41 3514 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
5d5e103a 3515 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
2ac55d41 3516 0, 0, &cached_state, GFP_NOFS);
5d5e103a 3517
2ac55d41
JB
3518 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
3519 &cached_state);
9ed74f2d 3520 if (ret) {
2ac55d41
JB
3521 unlock_extent_cached(io_tree, page_start, page_end,
3522 &cached_state, GFP_NOFS);
9ed74f2d
JB
3523 goto out_unlock;
3524 }
3525
e6dcd2dc
CM
3526 ret = 0;
3527 if (offset != PAGE_CACHE_SIZE) {
3528 kaddr = kmap(page);
3529 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
3530 flush_dcache_page(page);
3531 kunmap(page);
3532 }
247e743c 3533 ClearPageChecked(page);
e6dcd2dc 3534 set_page_dirty(page);
2ac55d41
JB
3535 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
3536 GFP_NOFS);
39279cc3 3537
89642229 3538out_unlock:
5d5e103a 3539 if (ret)
0ca1f7ce 3540 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
39279cc3
CM
3541 unlock_page(page);
3542 page_cache_release(page);
3543out:
3544 return ret;
3545}
3546
a41ad394 3547int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
39279cc3 3548{
9036c102
YZ
3549 struct btrfs_trans_handle *trans;
3550 struct btrfs_root *root = BTRFS_I(inode)->root;
3551 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
a22285a6 3552 struct extent_map *em = NULL;
2ac55d41 3553 struct extent_state *cached_state = NULL;
9036c102 3554 u64 mask = root->sectorsize - 1;
a41ad394 3555 u64 hole_start = (oldsize + mask) & ~mask;
9036c102
YZ
3556 u64 block_end = (size + mask) & ~mask;
3557 u64 last_byte;
3558 u64 cur_offset;
3559 u64 hole_size;
9ed74f2d 3560 int err = 0;
39279cc3 3561
9036c102
YZ
3562 if (size <= hole_start)
3563 return 0;
3564
9036c102
YZ
3565 while (1) {
3566 struct btrfs_ordered_extent *ordered;
3567 btrfs_wait_ordered_range(inode, hole_start,
3568 block_end - hole_start);
2ac55d41
JB
3569 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
3570 &cached_state, GFP_NOFS);
9036c102
YZ
3571 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
3572 if (!ordered)
3573 break;
2ac55d41
JB
3574 unlock_extent_cached(io_tree, hole_start, block_end - 1,
3575 &cached_state, GFP_NOFS);
9036c102
YZ
3576 btrfs_put_ordered_extent(ordered);
3577 }
39279cc3 3578
9036c102
YZ
3579 cur_offset = hole_start;
3580 while (1) {
3581 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3582 block_end - cur_offset, 0);
3583 BUG_ON(IS_ERR(em) || !em);
3584 last_byte = min(extent_map_end(em), block_end);
3585 last_byte = (last_byte + mask) & ~mask;
8082510e 3586 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
771ed689 3587 u64 hint_byte = 0;
9036c102 3588 hole_size = last_byte - cur_offset;
9ed74f2d 3589
a22285a6
YZ
3590 trans = btrfs_start_transaction(root, 2);
3591 if (IS_ERR(trans)) {
3592 err = PTR_ERR(trans);
9ed74f2d 3593 break;
a22285a6 3594 }
8082510e
YZ
3595 btrfs_set_trans_block_group(trans, inode);
3596
3597 err = btrfs_drop_extents(trans, inode, cur_offset,
3598 cur_offset + hole_size,
3599 &hint_byte, 1);
3600 BUG_ON(err);
3601
9036c102
YZ
3602 err = btrfs_insert_file_extent(trans, root,
3603 inode->i_ino, cur_offset, 0,
3604 0, hole_size, 0, hole_size,
3605 0, 0, 0);
8082510e
YZ
3606 BUG_ON(err);
3607
9036c102
YZ
3608 btrfs_drop_extent_cache(inode, hole_start,
3609 last_byte - 1, 0);
8082510e
YZ
3610
3611 btrfs_end_transaction(trans, root);
9036c102
YZ
3612 }
3613 free_extent_map(em);
a22285a6 3614 em = NULL;
9036c102 3615 cur_offset = last_byte;
8082510e 3616 if (cur_offset >= block_end)
9036c102
YZ
3617 break;
3618 }
1832a6d5 3619
a22285a6 3620 free_extent_map(em);
2ac55d41
JB
3621 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
3622 GFP_NOFS);
9036c102
YZ
3623 return err;
3624}
39279cc3 3625
a41ad394 3626static int btrfs_setsize(struct inode *inode, loff_t newsize)
8082510e
YZ
3627{
3628 struct btrfs_root *root = BTRFS_I(inode)->root;
3629 struct btrfs_trans_handle *trans;
a41ad394 3630 loff_t oldsize = i_size_read(inode);
8082510e
YZ
3631 unsigned long nr;
3632 int ret;
3633
a41ad394 3634 if (newsize == oldsize)
8082510e
YZ
3635 return 0;
3636
d68fc57b
YZ
3637 trans = btrfs_start_transaction(root, 5);
3638 if (IS_ERR(trans))
3639 return PTR_ERR(trans);
8082510e 3640
8082510e
YZ
3641 btrfs_set_trans_block_group(trans, inode);
3642
3643 ret = btrfs_orphan_add(trans, inode);
3644 BUG_ON(ret);
3645
3646 nr = trans->blocks_used;
3647 btrfs_end_transaction(trans, root);
8082510e
YZ
3648 btrfs_btree_balance_dirty(root, nr);
3649
a41ad394
JB
3650 if (newsize > oldsize) {
3651 i_size_write(inode, newsize);
3652 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
3653 truncate_pagecache(inode, oldsize, newsize);
3654 ret = btrfs_cont_expand(inode, oldsize, newsize);
8082510e 3655 if (ret) {
a41ad394 3656 btrfs_setsize(inode, oldsize);
8082510e
YZ
3657 return ret;
3658 }
3659
d68fc57b
YZ
3660 trans = btrfs_start_transaction(root, 0);
3661 BUG_ON(IS_ERR(trans));
8082510e 3662 btrfs_set_trans_block_group(trans, inode);
d68fc57b
YZ
3663 trans->block_rsv = root->orphan_block_rsv;
3664 BUG_ON(!trans->block_rsv);
8082510e
YZ
3665
3666 ret = btrfs_update_inode(trans, root, inode);
3667 BUG_ON(ret);
3668 if (inode->i_nlink > 0) {
3669 ret = btrfs_orphan_del(trans, inode);
3670 BUG_ON(ret);
3671 }
3672 nr = trans->blocks_used;
3673 btrfs_end_transaction(trans, root);
3674 btrfs_btree_balance_dirty(root, nr);
a41ad394 3675 } else {
8082510e 3676
a41ad394
JB
3677 /*
3678 * We're truncating a file that used to have good data down to
3679 * zero. Make sure it gets into the ordered flush list so that
3680 * any new writes get down to disk quickly.
3681 */
3682 if (newsize == 0)
3683 BTRFS_I(inode)->ordered_data_close = 1;
8082510e 3684
a41ad394
JB
3685 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3686 truncate_setsize(inode, newsize);
3687 ret = btrfs_truncate(inode);
3688 }
8082510e 3689
a41ad394 3690 return ret;
8082510e
YZ
3691}
3692
9036c102
YZ
3693static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3694{
3695 struct inode *inode = dentry->d_inode;
b83cc969 3696 struct btrfs_root *root = BTRFS_I(inode)->root;
9036c102 3697 int err;
39279cc3 3698
b83cc969
LZ
3699 if (btrfs_root_readonly(root))
3700 return -EROFS;
3701
9036c102
YZ
3702 err = inode_change_ok(inode, attr);
3703 if (err)
3704 return err;
2bf5a725 3705
5a3f23d5 3706 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
a41ad394 3707 err = btrfs_setsize(inode, attr->ia_size);
8082510e
YZ
3708 if (err)
3709 return err;
39279cc3 3710 }
9036c102 3711
1025774c
CH
3712 if (attr->ia_valid) {
3713 setattr_copy(inode, attr);
3714 mark_inode_dirty(inode);
3715
3716 if (attr->ia_valid & ATTR_MODE)
3717 err = btrfs_acl_chmod(inode);
3718 }
33268eaf 3719
39279cc3
CM
3720 return err;
3721}
61295eb8 3722
bd555975 3723void btrfs_evict_inode(struct inode *inode)
39279cc3
CM
3724{
3725 struct btrfs_trans_handle *trans;
3726 struct btrfs_root *root = BTRFS_I(inode)->root;
d3c2fdcf 3727 unsigned long nr;
39279cc3
CM
3728 int ret;
3729
3730 truncate_inode_pages(&inode->i_data, 0);
0af3d00b
JB
3731 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
3732 root == root->fs_info->tree_root))
bd555975
AV
3733 goto no_delete;
3734
39279cc3 3735 if (is_bad_inode(inode)) {
7b128766 3736 btrfs_orphan_del(NULL, inode);
39279cc3
CM
3737 goto no_delete;
3738 }
bd555975 3739 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
4a096752 3740 btrfs_wait_ordered_range(inode, 0, (u64)-1);
5f39d397 3741
c71bf099
YZ
3742 if (root->fs_info->log_root_recovering) {
3743 BUG_ON(!list_empty(&BTRFS_I(inode)->i_orphan));
3744 goto no_delete;
3745 }
3746
76dda93c
YZ
3747 if (inode->i_nlink > 0) {
3748 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3749 goto no_delete;
3750 }
3751
dbe674a9 3752 btrfs_i_size_write(inode, 0);
5f39d397 3753
8082510e 3754 while (1) {
d68fc57b
YZ
3755 trans = btrfs_start_transaction(root, 0);
3756 BUG_ON(IS_ERR(trans));
8082510e 3757 btrfs_set_trans_block_group(trans, inode);
d68fc57b
YZ
3758 trans->block_rsv = root->orphan_block_rsv;
3759
3760 ret = btrfs_block_rsv_check(trans, root,
3761 root->orphan_block_rsv, 0, 5);
3762 if (ret) {
3763 BUG_ON(ret != -EAGAIN);
3764 ret = btrfs_commit_transaction(trans, root);
3765 BUG_ON(ret);
3766 continue;
3767 }
7b128766 3768
d68fc57b 3769 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
8082510e
YZ
3770 if (ret != -EAGAIN)
3771 break;
85e21bac 3772
8082510e
YZ
3773 nr = trans->blocks_used;
3774 btrfs_end_transaction(trans, root);
3775 trans = NULL;
3776 btrfs_btree_balance_dirty(root, nr);
d68fc57b 3777
8082510e 3778 }
5f39d397 3779
8082510e
YZ
3780 if (ret == 0) {
3781 ret = btrfs_orphan_del(trans, inode);
3782 BUG_ON(ret);
3783 }
54aa1f4d 3784
d3c2fdcf 3785 nr = trans->blocks_used;
54aa1f4d 3786 btrfs_end_transaction(trans, root);
d3c2fdcf 3787 btrfs_btree_balance_dirty(root, nr);
39279cc3 3788no_delete:
bd555975 3789 end_writeback(inode);
8082510e 3790 return;
39279cc3
CM
3791}
3792
3793/*
3794 * this returns the key found in the dir entry in the location pointer.
3795 * If no dir entries were found, location->objectid is 0.
3796 */
3797static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
3798 struct btrfs_key *location)
3799{
3800 const char *name = dentry->d_name.name;
3801 int namelen = dentry->d_name.len;
3802 struct btrfs_dir_item *di;
3803 struct btrfs_path *path;
3804 struct btrfs_root *root = BTRFS_I(dir)->root;
0d9f7f3e 3805 int ret = 0;
39279cc3
CM
3806
3807 path = btrfs_alloc_path();
3808 BUG_ON(!path);
3954401f 3809
39279cc3
CM
3810 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
3811 namelen, 0);
0d9f7f3e
Y
3812 if (IS_ERR(di))
3813 ret = PTR_ERR(di);
d397712b
CM
3814
3815 if (!di || IS_ERR(di))
3954401f 3816 goto out_err;
d397712b 3817
5f39d397 3818 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
39279cc3 3819out:
39279cc3
CM
3820 btrfs_free_path(path);
3821 return ret;
3954401f
CM
3822out_err:
3823 location->objectid = 0;
3824 goto out;
39279cc3
CM
3825}
3826
3827/*
3828 * when we hit a tree root in a directory, the btrfs part of the inode
3829 * needs to be changed to reflect the root directory of the tree root. This
3830 * is kind of like crossing a mount point.
3831 */
3832static int fixup_tree_root_location(struct btrfs_root *root,
4df27c4d
YZ
3833 struct inode *dir,
3834 struct dentry *dentry,
3835 struct btrfs_key *location,
3836 struct btrfs_root **sub_root)
39279cc3 3837{
4df27c4d
YZ
3838 struct btrfs_path *path;
3839 struct btrfs_root *new_root;
3840 struct btrfs_root_ref *ref;
3841 struct extent_buffer *leaf;
3842 int ret;
3843 int err = 0;
39279cc3 3844
4df27c4d
YZ
3845 path = btrfs_alloc_path();
3846 if (!path) {
3847 err = -ENOMEM;
3848 goto out;
3849 }
39279cc3 3850
4df27c4d
YZ
3851 err = -ENOENT;
3852 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
3853 BTRFS_I(dir)->root->root_key.objectid,
3854 location->objectid);
3855 if (ret) {
3856 if (ret < 0)
3857 err = ret;
3858 goto out;
3859 }
39279cc3 3860
4df27c4d
YZ
3861 leaf = path->nodes[0];
3862 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
3863 if (btrfs_root_ref_dirid(leaf, ref) != dir->i_ino ||
3864 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
3865 goto out;
39279cc3 3866
4df27c4d
YZ
3867 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
3868 (unsigned long)(ref + 1),
3869 dentry->d_name.len);
3870 if (ret)
3871 goto out;
3872
3873 btrfs_release_path(root->fs_info->tree_root, path);
3874
3875 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
3876 if (IS_ERR(new_root)) {
3877 err = PTR_ERR(new_root);
3878 goto out;
3879 }
3880
3881 if (btrfs_root_refs(&new_root->root_item) == 0) {
3882 err = -ENOENT;
3883 goto out;
3884 }
3885
3886 *sub_root = new_root;
3887 location->objectid = btrfs_root_dirid(&new_root->root_item);
3888 location->type = BTRFS_INODE_ITEM_KEY;
3889 location->offset = 0;
3890 err = 0;
3891out:
3892 btrfs_free_path(path);
3893 return err;
39279cc3
CM
3894}
3895
5d4f98a2
YZ
3896static void inode_tree_add(struct inode *inode)
3897{
3898 struct btrfs_root *root = BTRFS_I(inode)->root;
3899 struct btrfs_inode *entry;
03e860bd
FNP
3900 struct rb_node **p;
3901 struct rb_node *parent;
03e860bd
FNP
3902again:
3903 p = &root->inode_tree.rb_node;
3904 parent = NULL;
5d4f98a2 3905
1d3382cb 3906 if (inode_unhashed(inode))
76dda93c
YZ
3907 return;
3908
5d4f98a2
YZ
3909 spin_lock(&root->inode_lock);
3910 while (*p) {
3911 parent = *p;
3912 entry = rb_entry(parent, struct btrfs_inode, rb_node);
3913
3914 if (inode->i_ino < entry->vfs_inode.i_ino)
03e860bd 3915 p = &parent->rb_left;
5d4f98a2 3916 else if (inode->i_ino > entry->vfs_inode.i_ino)
03e860bd 3917 p = &parent->rb_right;
5d4f98a2
YZ
3918 else {
3919 WARN_ON(!(entry->vfs_inode.i_state &
a4ffdde6 3920 (I_WILL_FREE | I_FREEING)));
03e860bd
FNP
3921 rb_erase(parent, &root->inode_tree);
3922 RB_CLEAR_NODE(parent);
3923 spin_unlock(&root->inode_lock);
3924 goto again;
5d4f98a2
YZ
3925 }
3926 }
3927 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
3928 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
3929 spin_unlock(&root->inode_lock);
3930}
3931
3932static void inode_tree_del(struct inode *inode)
3933{
3934 struct btrfs_root *root = BTRFS_I(inode)->root;
76dda93c 3935 int empty = 0;
5d4f98a2 3936
03e860bd 3937 spin_lock(&root->inode_lock);
5d4f98a2 3938 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
5d4f98a2 3939 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
5d4f98a2 3940 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
76dda93c 3941 empty = RB_EMPTY_ROOT(&root->inode_tree);
5d4f98a2 3942 }
03e860bd 3943 spin_unlock(&root->inode_lock);
76dda93c 3944
0af3d00b
JB
3945 /*
3946 * Free space cache has inodes in the tree root, but the tree root has a
3947 * root_refs of 0, so this could end up dropping the tree root as a
3948 * snapshot, so we need the extra !root->fs_info->tree_root check to
3949 * make sure we don't drop it.
3950 */
3951 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
3952 root != root->fs_info->tree_root) {
76dda93c
YZ
3953 synchronize_srcu(&root->fs_info->subvol_srcu);
3954 spin_lock(&root->inode_lock);
3955 empty = RB_EMPTY_ROOT(&root->inode_tree);
3956 spin_unlock(&root->inode_lock);
3957 if (empty)
3958 btrfs_add_dead_root(root);
3959 }
3960}
3961
3962int btrfs_invalidate_inodes(struct btrfs_root *root)
3963{
3964 struct rb_node *node;
3965 struct rb_node *prev;
3966 struct btrfs_inode *entry;
3967 struct inode *inode;
3968 u64 objectid = 0;
3969
3970 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
3971
3972 spin_lock(&root->inode_lock);
3973again:
3974 node = root->inode_tree.rb_node;
3975 prev = NULL;
3976 while (node) {
3977 prev = node;
3978 entry = rb_entry(node, struct btrfs_inode, rb_node);
3979
3980 if (objectid < entry->vfs_inode.i_ino)
3981 node = node->rb_left;
3982 else if (objectid > entry->vfs_inode.i_ino)
3983 node = node->rb_right;
3984 else
3985 break;
3986 }
3987 if (!node) {
3988 while (prev) {
3989 entry = rb_entry(prev, struct btrfs_inode, rb_node);
3990 if (objectid <= entry->vfs_inode.i_ino) {
3991 node = prev;
3992 break;
3993 }
3994 prev = rb_next(prev);
3995 }
3996 }
3997 while (node) {
3998 entry = rb_entry(node, struct btrfs_inode, rb_node);
3999 objectid = entry->vfs_inode.i_ino + 1;
4000 inode = igrab(&entry->vfs_inode);
4001 if (inode) {
4002 spin_unlock(&root->inode_lock);
4003 if (atomic_read(&inode->i_count) > 1)
4004 d_prune_aliases(inode);
4005 /*
45321ac5 4006 * btrfs_drop_inode will have it removed from
76dda93c
YZ
4007 * the inode cache when its usage count
4008 * hits zero.
4009 */
4010 iput(inode);
4011 cond_resched();
4012 spin_lock(&root->inode_lock);
4013 goto again;
4014 }
4015
4016 if (cond_resched_lock(&root->inode_lock))
4017 goto again;
4018
4019 node = rb_next(node);
4020 }
4021 spin_unlock(&root->inode_lock);
4022 return 0;
5d4f98a2
YZ
4023}
4024
e02119d5
CM
4025static int btrfs_init_locked_inode(struct inode *inode, void *p)
4026{
4027 struct btrfs_iget_args *args = p;
4028 inode->i_ino = args->ino;
e02119d5 4029 BTRFS_I(inode)->root = args->root;
6a63209f 4030 btrfs_set_inode_space_info(args->root, inode);
39279cc3
CM
4031 return 0;
4032}
4033
4034static int btrfs_find_actor(struct inode *inode, void *opaque)
4035{
4036 struct btrfs_iget_args *args = opaque;
d397712b
CM
4037 return args->ino == inode->i_ino &&
4038 args->root == BTRFS_I(inode)->root;
39279cc3
CM
4039}
4040
5d4f98a2
YZ
4041static struct inode *btrfs_iget_locked(struct super_block *s,
4042 u64 objectid,
4043 struct btrfs_root *root)
39279cc3
CM
4044{
4045 struct inode *inode;
4046 struct btrfs_iget_args args;
4047 args.ino = objectid;
4048 args.root = root;
4049
4050 inode = iget5_locked(s, objectid, btrfs_find_actor,
4051 btrfs_init_locked_inode,
4052 (void *)&args);
4053 return inode;
4054}
4055
1a54ef8c
BR
4056/* Get an inode object given its location and corresponding root.
4057 * Returns in *is_new if the inode was read from disk
4058 */
4059struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
73f73415 4060 struct btrfs_root *root, int *new)
1a54ef8c
BR
4061{
4062 struct inode *inode;
4063
4064 inode = btrfs_iget_locked(s, location->objectid, root);
4065 if (!inode)
5d4f98a2 4066 return ERR_PTR(-ENOMEM);
1a54ef8c
BR
4067
4068 if (inode->i_state & I_NEW) {
4069 BTRFS_I(inode)->root = root;
4070 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4071 btrfs_read_locked_inode(inode);
5d4f98a2
YZ
4072
4073 inode_tree_add(inode);
1a54ef8c 4074 unlock_new_inode(inode);
73f73415
JB
4075 if (new)
4076 *new = 1;
1a54ef8c
BR
4077 }
4078
4079 return inode;
4080}
4081
4df27c4d
YZ
4082static struct inode *new_simple_dir(struct super_block *s,
4083 struct btrfs_key *key,
4084 struct btrfs_root *root)
4085{
4086 struct inode *inode = new_inode(s);
4087
4088 if (!inode)
4089 return ERR_PTR(-ENOMEM);
4090
4df27c4d
YZ
4091 BTRFS_I(inode)->root = root;
4092 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
4093 BTRFS_I(inode)->dummy_inode = 1;
4094
4095 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
4096 inode->i_op = &simple_dir_inode_operations;
4097 inode->i_fop = &simple_dir_operations;
4098 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
4099 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4100
4101 return inode;
4102}
4103
3de4586c 4104struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
39279cc3 4105{
d397712b 4106 struct inode *inode;
4df27c4d 4107 struct btrfs_root *root = BTRFS_I(dir)->root;
39279cc3
CM
4108 struct btrfs_root *sub_root = root;
4109 struct btrfs_key location;
76dda93c 4110 int index;
5d4f98a2 4111 int ret;
39279cc3
CM
4112
4113 if (dentry->d_name.len > BTRFS_NAME_LEN)
4114 return ERR_PTR(-ENAMETOOLONG);
5f39d397 4115
39279cc3 4116 ret = btrfs_inode_by_name(dir, dentry, &location);
5f39d397 4117
39279cc3
CM
4118 if (ret < 0)
4119 return ERR_PTR(ret);
5f39d397 4120
4df27c4d
YZ
4121 if (location.objectid == 0)
4122 return NULL;
4123
4124 if (location.type == BTRFS_INODE_ITEM_KEY) {
73f73415 4125 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
4df27c4d
YZ
4126 return inode;
4127 }
4128
4129 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4130
76dda93c 4131 index = srcu_read_lock(&root->fs_info->subvol_srcu);
4df27c4d
YZ
4132 ret = fixup_tree_root_location(root, dir, dentry,
4133 &location, &sub_root);
4134 if (ret < 0) {
4135 if (ret != -ENOENT)
4136 inode = ERR_PTR(ret);
4137 else
4138 inode = new_simple_dir(dir->i_sb, &location, sub_root);
4139 } else {
73f73415 4140 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
39279cc3 4141 }
76dda93c
YZ
4142 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4143
34d19bad 4144 if (!IS_ERR(inode) && root != sub_root) {
c71bf099
YZ
4145 down_read(&root->fs_info->cleanup_work_sem);
4146 if (!(inode->i_sb->s_flags & MS_RDONLY))
4147 btrfs_orphan_cleanup(sub_root);
4148 up_read(&root->fs_info->cleanup_work_sem);
4149 }
4150
3de4586c
CM
4151 return inode;
4152}
4153
fe15ce44 4154static int btrfs_dentry_delete(const struct dentry *dentry)
76dda93c
YZ
4155{
4156 struct btrfs_root *root;
4157
efefb143
YZ
4158 if (!dentry->d_inode && !IS_ROOT(dentry))
4159 dentry = dentry->d_parent;
76dda93c 4160
efefb143
YZ
4161 if (dentry->d_inode) {
4162 root = BTRFS_I(dentry->d_inode)->root;
4163 if (btrfs_root_refs(&root->root_item) == 0)
4164 return 1;
4165 }
76dda93c
YZ
4166 return 0;
4167}
4168
3de4586c
CM
4169static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
4170 struct nameidata *nd)
4171{
4172 struct inode *inode;
4173
3de4586c
CM
4174 inode = btrfs_lookup_dentry(dir, dentry);
4175 if (IS_ERR(inode))
4176 return ERR_CAST(inode);
7b128766 4177
39279cc3
CM
4178 return d_splice_alias(inode, dentry);
4179}
4180
39279cc3
CM
4181static unsigned char btrfs_filetype_table[] = {
4182 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
4183};
4184
cbdf5a24
DW
4185static int btrfs_real_readdir(struct file *filp, void *dirent,
4186 filldir_t filldir)
39279cc3 4187{
6da6abae 4188 struct inode *inode = filp->f_dentry->d_inode;
39279cc3
CM
4189 struct btrfs_root *root = BTRFS_I(inode)->root;
4190 struct btrfs_item *item;
4191 struct btrfs_dir_item *di;
4192 struct btrfs_key key;
5f39d397 4193 struct btrfs_key found_key;
39279cc3
CM
4194 struct btrfs_path *path;
4195 int ret;
4196 u32 nritems;
5f39d397 4197 struct extent_buffer *leaf;
39279cc3
CM
4198 int slot;
4199 int advance;
4200 unsigned char d_type;
4201 int over = 0;
4202 u32 di_cur;
4203 u32 di_total;
4204 u32 di_len;
4205 int key_type = BTRFS_DIR_INDEX_KEY;
5f39d397
CM
4206 char tmp_name[32];
4207 char *name_ptr;
4208 int name_len;
39279cc3
CM
4209
4210 /* FIXME, use a real flag for deciding about the key type */
4211 if (root->fs_info->tree_root == root)
4212 key_type = BTRFS_DIR_ITEM_KEY;
5f39d397 4213
3954401f
CM
4214 /* special case for "." */
4215 if (filp->f_pos == 0) {
4216 over = filldir(dirent, ".", 1,
4217 1, inode->i_ino,
4218 DT_DIR);
4219 if (over)
4220 return 0;
4221 filp->f_pos = 1;
4222 }
3954401f
CM
4223 /* special case for .., just use the back ref */
4224 if (filp->f_pos == 1) {
5ecc7e5d 4225 u64 pino = parent_ino(filp->f_path.dentry);
3954401f 4226 over = filldir(dirent, "..", 2,
5ecc7e5d 4227 2, pino, DT_DIR);
3954401f 4228 if (over)
49593bfa 4229 return 0;
3954401f
CM
4230 filp->f_pos = 2;
4231 }
49593bfa
DW
4232 path = btrfs_alloc_path();
4233 path->reada = 2;
4234
39279cc3
CM
4235 btrfs_set_key_type(&key, key_type);
4236 key.offset = filp->f_pos;
49593bfa 4237 key.objectid = inode->i_ino;
5f39d397 4238
39279cc3
CM
4239 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4240 if (ret < 0)
4241 goto err;
4242 advance = 0;
49593bfa
DW
4243
4244 while (1) {
5f39d397
CM
4245 leaf = path->nodes[0];
4246 nritems = btrfs_header_nritems(leaf);
39279cc3
CM
4247 slot = path->slots[0];
4248 if (advance || slot >= nritems) {
49593bfa 4249 if (slot >= nritems - 1) {
39279cc3
CM
4250 ret = btrfs_next_leaf(root, path);
4251 if (ret)
4252 break;
5f39d397
CM
4253 leaf = path->nodes[0];
4254 nritems = btrfs_header_nritems(leaf);
39279cc3
CM
4255 slot = path->slots[0];
4256 } else {
4257 slot++;
4258 path->slots[0]++;
4259 }
4260 }
3de4586c 4261
39279cc3 4262 advance = 1;
5f39d397
CM
4263 item = btrfs_item_nr(leaf, slot);
4264 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4265
4266 if (found_key.objectid != key.objectid)
39279cc3 4267 break;
5f39d397 4268 if (btrfs_key_type(&found_key) != key_type)
39279cc3 4269 break;
5f39d397 4270 if (found_key.offset < filp->f_pos)
39279cc3 4271 continue;
5f39d397
CM
4272
4273 filp->f_pos = found_key.offset;
49593bfa 4274
39279cc3
CM
4275 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
4276 di_cur = 0;
5f39d397 4277 di_total = btrfs_item_size(leaf, item);
49593bfa
DW
4278
4279 while (di_cur < di_total) {
5f39d397
CM
4280 struct btrfs_key location;
4281
4282 name_len = btrfs_dir_name_len(leaf, di);
49593bfa 4283 if (name_len <= sizeof(tmp_name)) {
5f39d397
CM
4284 name_ptr = tmp_name;
4285 } else {
4286 name_ptr = kmalloc(name_len, GFP_NOFS);
49593bfa
DW
4287 if (!name_ptr) {
4288 ret = -ENOMEM;
4289 goto err;
4290 }
5f39d397
CM
4291 }
4292 read_extent_buffer(leaf, name_ptr,
4293 (unsigned long)(di + 1), name_len);
4294
4295 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
4296 btrfs_dir_item_key_to_cpu(leaf, di, &location);
3de4586c
CM
4297
4298 /* is this a reference to our own snapshot? If so
4299 * skip it
4300 */
4301 if (location.type == BTRFS_ROOT_ITEM_KEY &&
4302 location.objectid == root->root_key.objectid) {
4303 over = 0;
4304 goto skip;
4305 }
5f39d397 4306 over = filldir(dirent, name_ptr, name_len,
49593bfa 4307 found_key.offset, location.objectid,
39279cc3 4308 d_type);
5f39d397 4309
3de4586c 4310skip:
5f39d397
CM
4311 if (name_ptr != tmp_name)
4312 kfree(name_ptr);
4313
39279cc3
CM
4314 if (over)
4315 goto nopos;
5103e947 4316 di_len = btrfs_dir_name_len(leaf, di) +
49593bfa 4317 btrfs_dir_data_len(leaf, di) + sizeof(*di);
39279cc3
CM
4318 di_cur += di_len;
4319 di = (struct btrfs_dir_item *)((char *)di + di_len);
4320 }
4321 }
49593bfa
DW
4322
4323 /* Reached end of directory/root. Bump pos past the last item. */
5e591a07 4324 if (key_type == BTRFS_DIR_INDEX_KEY)
406266ab
JE
4325 /*
4326 * 32-bit glibc will use getdents64, but then strtol -
4327 * so the last number we can serve is this.
4328 */
4329 filp->f_pos = 0x7fffffff;
5e591a07
YZ
4330 else
4331 filp->f_pos++;
39279cc3
CM
4332nopos:
4333 ret = 0;
4334err:
39279cc3 4335 btrfs_free_path(path);
39279cc3
CM
4336 return ret;
4337}
4338
a9185b41 4339int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
39279cc3
CM
4340{
4341 struct btrfs_root *root = BTRFS_I(inode)->root;
4342 struct btrfs_trans_handle *trans;
4343 int ret = 0;
0af3d00b 4344 bool nolock = false;
39279cc3 4345
8929ecfa 4346 if (BTRFS_I(inode)->dummy_inode)
4ca8b41e
CM
4347 return 0;
4348
0af3d00b
JB
4349 smp_mb();
4350 nolock = (root->fs_info->closing && root == root->fs_info->tree_root);
4351
a9185b41 4352 if (wbc->sync_mode == WB_SYNC_ALL) {
0af3d00b
JB
4353 if (nolock)
4354 trans = btrfs_join_transaction_nolock(root, 1);
4355 else
4356 trans = btrfs_join_transaction(root, 1);
3612b495
TI
4357 if (IS_ERR(trans))
4358 return PTR_ERR(trans);
39279cc3 4359 btrfs_set_trans_block_group(trans, inode);
0af3d00b
JB
4360 if (nolock)
4361 ret = btrfs_end_transaction_nolock(trans, root);
4362 else
4363 ret = btrfs_commit_transaction(trans, root);
39279cc3
CM
4364 }
4365 return ret;
4366}
4367
4368/*
54aa1f4d 4369 * This is somewhat expensive, updating the tree every time the
39279cc3
CM
4370 * inode changes. But, it is most likely to find the inode in cache.
4371 * FIXME, needs more benchmarking...there are no reasons other than performance
4372 * to keep or drop this code.
4373 */
4374void btrfs_dirty_inode(struct inode *inode)
4375{
4376 struct btrfs_root *root = BTRFS_I(inode)->root;
4377 struct btrfs_trans_handle *trans;
8929ecfa
YZ
4378 int ret;
4379
4380 if (BTRFS_I(inode)->dummy_inode)
4381 return;
39279cc3 4382
f9295749 4383 trans = btrfs_join_transaction(root, 1);
3612b495 4384 BUG_ON(IS_ERR(trans));
39279cc3 4385 btrfs_set_trans_block_group(trans, inode);
8929ecfa
YZ
4386
4387 ret = btrfs_update_inode(trans, root, inode);
94b60442
CM
4388 if (ret && ret == -ENOSPC) {
4389 /* whoops, lets try again with the full transaction */
4390 btrfs_end_transaction(trans, root);
4391 trans = btrfs_start_transaction(root, 1);
9aeead73
CM
4392 if (IS_ERR(trans)) {
4393 if (printk_ratelimit()) {
4394 printk(KERN_ERR "btrfs: fail to "
4395 "dirty inode %lu error %ld\n",
4396 inode->i_ino, PTR_ERR(trans));
4397 }
4398 return;
4399 }
94b60442 4400 btrfs_set_trans_block_group(trans, inode);
8929ecfa 4401
94b60442
CM
4402 ret = btrfs_update_inode(trans, root, inode);
4403 if (ret) {
9aeead73
CM
4404 if (printk_ratelimit()) {
4405 printk(KERN_ERR "btrfs: fail to "
4406 "dirty inode %lu error %d\n",
4407 inode->i_ino, ret);
4408 }
94b60442
CM
4409 }
4410 }
39279cc3 4411 btrfs_end_transaction(trans, root);
39279cc3
CM
4412}
4413
d352ac68
CM
4414/*
4415 * find the highest existing sequence number in a directory
4416 * and then set the in-memory index_cnt variable to reflect
4417 * free sequence numbers
4418 */
aec7477b
JB
4419static int btrfs_set_inode_index_count(struct inode *inode)
4420{
4421 struct btrfs_root *root = BTRFS_I(inode)->root;
4422 struct btrfs_key key, found_key;
4423 struct btrfs_path *path;
4424 struct extent_buffer *leaf;
4425 int ret;
4426
4427 key.objectid = inode->i_ino;
4428 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
4429 key.offset = (u64)-1;
4430
4431 path = btrfs_alloc_path();
4432 if (!path)
4433 return -ENOMEM;
4434
4435 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4436 if (ret < 0)
4437 goto out;
4438 /* FIXME: we should be able to handle this */
4439 if (ret == 0)
4440 goto out;
4441 ret = 0;
4442
4443 /*
4444 * MAGIC NUMBER EXPLANATION:
4445 * since we search a directory based on f_pos we have to start at 2
4446 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4447 * else has to start at 2
4448 */
4449 if (path->slots[0] == 0) {
4450 BTRFS_I(inode)->index_cnt = 2;
4451 goto out;
4452 }
4453
4454 path->slots[0]--;
4455
4456 leaf = path->nodes[0];
4457 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4458
4459 if (found_key.objectid != inode->i_ino ||
4460 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
4461 BTRFS_I(inode)->index_cnt = 2;
4462 goto out;
4463 }
4464
4465 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
4466out:
4467 btrfs_free_path(path);
4468 return ret;
4469}
4470
d352ac68
CM
4471/*
4472 * helper to find a free sequence number in a given directory. This current
4473 * code is very simple, later versions will do smarter things in the btree
4474 */
3de4586c 4475int btrfs_set_inode_index(struct inode *dir, u64 *index)
aec7477b
JB
4476{
4477 int ret = 0;
4478
4479 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
4480 ret = btrfs_set_inode_index_count(dir);
d397712b 4481 if (ret)
aec7477b
JB
4482 return ret;
4483 }
4484
00e4e6b3 4485 *index = BTRFS_I(dir)->index_cnt;
aec7477b
JB
4486 BTRFS_I(dir)->index_cnt++;
4487
4488 return ret;
4489}
4490
39279cc3
CM
4491static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4492 struct btrfs_root *root,
aec7477b 4493 struct inode *dir,
9c58309d 4494 const char *name, int name_len,
d2fb3437
YZ
4495 u64 ref_objectid, u64 objectid,
4496 u64 alloc_hint, int mode, u64 *index)
39279cc3
CM
4497{
4498 struct inode *inode;
5f39d397 4499 struct btrfs_inode_item *inode_item;
39279cc3 4500 struct btrfs_key *location;
5f39d397 4501 struct btrfs_path *path;
9c58309d
CM
4502 struct btrfs_inode_ref *ref;
4503 struct btrfs_key key[2];
4504 u32 sizes[2];
4505 unsigned long ptr;
39279cc3
CM
4506 int ret;
4507 int owner;
4508
5f39d397
CM
4509 path = btrfs_alloc_path();
4510 BUG_ON(!path);
4511
39279cc3
CM
4512 inode = new_inode(root->fs_info->sb);
4513 if (!inode)
4514 return ERR_PTR(-ENOMEM);
4515
aec7477b 4516 if (dir) {
3de4586c 4517 ret = btrfs_set_inode_index(dir, index);
09771430
SF
4518 if (ret) {
4519 iput(inode);
aec7477b 4520 return ERR_PTR(ret);
09771430 4521 }
aec7477b
JB
4522 }
4523 /*
4524 * index_cnt is ignored for everything but a dir,
4525 * btrfs_get_inode_index_count has an explanation for the magic
4526 * number
4527 */
4528 BTRFS_I(inode)->index_cnt = 2;
39279cc3 4529 BTRFS_I(inode)->root = root;
e02119d5 4530 BTRFS_I(inode)->generation = trans->transid;
76195853 4531 inode->i_generation = BTRFS_I(inode)->generation;
6a63209f 4532 btrfs_set_inode_space_info(root, inode);
b888db2b 4533
39279cc3
CM
4534 if (mode & S_IFDIR)
4535 owner = 0;
4536 else
4537 owner = 1;
d2fb3437
YZ
4538 BTRFS_I(inode)->block_group =
4539 btrfs_find_block_group(root, 0, alloc_hint, owner);
9c58309d
CM
4540
4541 key[0].objectid = objectid;
4542 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
4543 key[0].offset = 0;
4544
4545 key[1].objectid = objectid;
4546 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
4547 key[1].offset = ref_objectid;
4548
4549 sizes[0] = sizeof(struct btrfs_inode_item);
4550 sizes[1] = name_len + sizeof(*ref);
4551
b9473439 4552 path->leave_spinning = 1;
9c58309d
CM
4553 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
4554 if (ret != 0)
5f39d397
CM
4555 goto fail;
4556
ecc11fab 4557 inode_init_owner(inode, dir, mode);
39279cc3 4558 inode->i_ino = objectid;
a76a3cd4 4559 inode_set_bytes(inode, 0);
39279cc3 4560 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
5f39d397
CM
4561 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4562 struct btrfs_inode_item);
e02119d5 4563 fill_inode_item(trans, path->nodes[0], inode_item, inode);
9c58309d
CM
4564
4565 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
4566 struct btrfs_inode_ref);
4567 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
00e4e6b3 4568 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
9c58309d
CM
4569 ptr = (unsigned long)(ref + 1);
4570 write_extent_buffer(path->nodes[0], name, ptr, name_len);
4571
5f39d397
CM
4572 btrfs_mark_buffer_dirty(path->nodes[0]);
4573 btrfs_free_path(path);
4574
39279cc3
CM
4575 location = &BTRFS_I(inode)->location;
4576 location->objectid = objectid;
39279cc3
CM
4577 location->offset = 0;
4578 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
4579
6cbff00f
CH
4580 btrfs_inherit_iflags(inode, dir);
4581
94272164
CM
4582 if ((mode & S_IFREG)) {
4583 if (btrfs_test_opt(root, NODATASUM))
4584 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
4585 if (btrfs_test_opt(root, NODATACOW))
4586 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
4587 }
4588
39279cc3 4589 insert_inode_hash(inode);
5d4f98a2 4590 inode_tree_add(inode);
39279cc3 4591 return inode;
5f39d397 4592fail:
aec7477b
JB
4593 if (dir)
4594 BTRFS_I(dir)->index_cnt--;
5f39d397 4595 btrfs_free_path(path);
09771430 4596 iput(inode);
5f39d397 4597 return ERR_PTR(ret);
39279cc3
CM
4598}
4599
4600static inline u8 btrfs_inode_type(struct inode *inode)
4601{
4602 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
4603}
4604
d352ac68
CM
4605/*
4606 * utility function to add 'inode' into 'parent_inode' with
4607 * a give name and a given sequence number.
4608 * if 'add_backref' is true, also insert a backref from the
4609 * inode to the parent directory.
4610 */
e02119d5
CM
4611int btrfs_add_link(struct btrfs_trans_handle *trans,
4612 struct inode *parent_inode, struct inode *inode,
4613 const char *name, int name_len, int add_backref, u64 index)
39279cc3 4614{
4df27c4d 4615 int ret = 0;
39279cc3 4616 struct btrfs_key key;
e02119d5 4617 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
5f39d397 4618
4df27c4d
YZ
4619 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
4620 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4621 } else {
4622 key.objectid = inode->i_ino;
4623 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4624 key.offset = 0;
4625 }
4626
4627 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
4628 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4629 key.objectid, root->root_key.objectid,
4630 parent_inode->i_ino,
4631 index, name, name_len);
4632 } else if (add_backref) {
4633 ret = btrfs_insert_inode_ref(trans, root,
4634 name, name_len, inode->i_ino,
4635 parent_inode->i_ino, index);
4636 }
39279cc3 4637
39279cc3 4638 if (ret == 0) {
4df27c4d
YZ
4639 ret = btrfs_insert_dir_item(trans, root, name, name_len,
4640 parent_inode->i_ino, &key,
4641 btrfs_inode_type(inode), index);
4642 BUG_ON(ret);
4643
dbe674a9 4644 btrfs_i_size_write(parent_inode, parent_inode->i_size +
e02119d5 4645 name_len * 2);
79c44584 4646 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
e02119d5 4647 ret = btrfs_update_inode(trans, root, parent_inode);
39279cc3
CM
4648 }
4649 return ret;
4650}
4651
4652static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
a1b075d2
JB
4653 struct inode *dir, struct dentry *dentry,
4654 struct inode *inode, int backref, u64 index)
39279cc3 4655{
a1b075d2
JB
4656 int err = btrfs_add_link(trans, dir, inode,
4657 dentry->d_name.name, dentry->d_name.len,
4658 backref, index);
39279cc3
CM
4659 if (!err) {
4660 d_instantiate(dentry, inode);
4661 return 0;
4662 }
4663 if (err > 0)
4664 err = -EEXIST;
4665 return err;
4666}
4667
618e21d5
JB
4668static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
4669 int mode, dev_t rdev)
4670{
4671 struct btrfs_trans_handle *trans;
4672 struct btrfs_root *root = BTRFS_I(dir)->root;
1832a6d5 4673 struct inode *inode = NULL;
618e21d5
JB
4674 int err;
4675 int drop_inode = 0;
4676 u64 objectid;
1832a6d5 4677 unsigned long nr = 0;
00e4e6b3 4678 u64 index = 0;
618e21d5
JB
4679
4680 if (!new_valid_dev(rdev))
4681 return -EINVAL;
4682
a22285a6
YZ
4683 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4684 if (err)
4685 return err;
4686
9ed74f2d
JB
4687 /*
4688 * 2 for inode item and ref
4689 * 2 for dir items
4690 * 1 for xattr if selinux is on
4691 */
a22285a6
YZ
4692 trans = btrfs_start_transaction(root, 5);
4693 if (IS_ERR(trans))
4694 return PTR_ERR(trans);
1832a6d5 4695
618e21d5
JB
4696 btrfs_set_trans_block_group(trans, dir);
4697
aec7477b 4698 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
a1b075d2 4699 dentry->d_name.len, dir->i_ino, objectid,
00e4e6b3 4700 BTRFS_I(dir)->block_group, mode, &index);
618e21d5
JB
4701 err = PTR_ERR(inode);
4702 if (IS_ERR(inode))
4703 goto out_unlock;
4704
f34f57a3 4705 err = btrfs_init_inode_security(trans, inode, dir);
33268eaf
JB
4706 if (err) {
4707 drop_inode = 1;
4708 goto out_unlock;
4709 }
4710
618e21d5 4711 btrfs_set_trans_block_group(trans, inode);
a1b075d2 4712 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
618e21d5
JB
4713 if (err)
4714 drop_inode = 1;
4715 else {
4716 inode->i_op = &btrfs_special_inode_operations;
4717 init_special_inode(inode, inode->i_mode, rdev);
1b4ab1bb 4718 btrfs_update_inode(trans, root, inode);
618e21d5 4719 }
618e21d5
JB
4720 btrfs_update_inode_block_group(trans, inode);
4721 btrfs_update_inode_block_group(trans, dir);
4722out_unlock:
d3c2fdcf 4723 nr = trans->blocks_used;
89ce8a63 4724 btrfs_end_transaction_throttle(trans, root);
a22285a6 4725 btrfs_btree_balance_dirty(root, nr);
618e21d5
JB
4726 if (drop_inode) {
4727 inode_dec_link_count(inode);
4728 iput(inode);
4729 }
618e21d5
JB
4730 return err;
4731}
4732
39279cc3
CM
4733static int btrfs_create(struct inode *dir, struct dentry *dentry,
4734 int mode, struct nameidata *nd)
4735{
4736 struct btrfs_trans_handle *trans;
4737 struct btrfs_root *root = BTRFS_I(dir)->root;
1832a6d5 4738 struct inode *inode = NULL;
39279cc3 4739 int drop_inode = 0;
a22285a6 4740 int err;
1832a6d5 4741 unsigned long nr = 0;
39279cc3 4742 u64 objectid;
00e4e6b3 4743 u64 index = 0;
39279cc3 4744
a22285a6
YZ
4745 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4746 if (err)
4747 return err;
9ed74f2d
JB
4748 /*
4749 * 2 for inode item and ref
4750 * 2 for dir items
4751 * 1 for xattr if selinux is on
4752 */
a22285a6
YZ
4753 trans = btrfs_start_transaction(root, 5);
4754 if (IS_ERR(trans))
4755 return PTR_ERR(trans);
9ed74f2d 4756
39279cc3
CM
4757 btrfs_set_trans_block_group(trans, dir);
4758
aec7477b 4759 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
a1b075d2
JB
4760 dentry->d_name.len, dir->i_ino, objectid,
4761 BTRFS_I(dir)->block_group, mode, &index);
39279cc3
CM
4762 err = PTR_ERR(inode);
4763 if (IS_ERR(inode))
4764 goto out_unlock;
4765
f34f57a3 4766 err = btrfs_init_inode_security(trans, inode, dir);
33268eaf
JB
4767 if (err) {
4768 drop_inode = 1;
4769 goto out_unlock;
4770 }
4771
39279cc3 4772 btrfs_set_trans_block_group(trans, inode);
a1b075d2 4773 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
39279cc3
CM
4774 if (err)
4775 drop_inode = 1;
4776 else {
4777 inode->i_mapping->a_ops = &btrfs_aops;
04160088 4778 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
39279cc3
CM
4779 inode->i_fop = &btrfs_file_operations;
4780 inode->i_op = &btrfs_file_inode_operations;
d1310b2e 4781 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
39279cc3 4782 }
39279cc3
CM
4783 btrfs_update_inode_block_group(trans, inode);
4784 btrfs_update_inode_block_group(trans, dir);
4785out_unlock:
d3c2fdcf 4786 nr = trans->blocks_used;
ab78c84d 4787 btrfs_end_transaction_throttle(trans, root);
39279cc3
CM
4788 if (drop_inode) {
4789 inode_dec_link_count(inode);
4790 iput(inode);
4791 }
d3c2fdcf 4792 btrfs_btree_balance_dirty(root, nr);
39279cc3
CM
4793 return err;
4794}
4795
4796static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
4797 struct dentry *dentry)
4798{
4799 struct btrfs_trans_handle *trans;
4800 struct btrfs_root *root = BTRFS_I(dir)->root;
4801 struct inode *inode = old_dentry->d_inode;
00e4e6b3 4802 u64 index;
1832a6d5 4803 unsigned long nr = 0;
39279cc3
CM
4804 int err;
4805 int drop_inode = 0;
4806
4807 if (inode->i_nlink == 0)
4808 return -ENOENT;
4809
4a8be425
TH
4810 /* do not allow sys_link's with other subvols of the same device */
4811 if (root->objectid != BTRFS_I(inode)->root->objectid)
4812 return -EPERM;
4813
9ed74f2d 4814 btrfs_inc_nlink(inode);
bc1cbf1f 4815 inode->i_ctime = CURRENT_TIME;
9ed74f2d 4816
3de4586c 4817 err = btrfs_set_inode_index(dir, &index);
aec7477b
JB
4818 if (err)
4819 goto fail;
4820
a22285a6 4821 /*
7e6b6465 4822 * 2 items for inode and inode ref
a22285a6 4823 * 2 items for dir items
7e6b6465 4824 * 1 item for parent inode
a22285a6 4825 */
7e6b6465 4826 trans = btrfs_start_transaction(root, 5);
a22285a6
YZ
4827 if (IS_ERR(trans)) {
4828 err = PTR_ERR(trans);
4829 goto fail;
4830 }
5f39d397 4831
39279cc3 4832 btrfs_set_trans_block_group(trans, dir);
7de9c6ee 4833 ihold(inode);
aec7477b 4834
a1b075d2 4835 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
5f39d397 4836
a5719521 4837 if (err) {
54aa1f4d 4838 drop_inode = 1;
a5719521 4839 } else {
6a912213 4840 struct dentry *parent = dget_parent(dentry);
a5719521
YZ
4841 btrfs_update_inode_block_group(trans, dir);
4842 err = btrfs_update_inode(trans, root, inode);
4843 BUG_ON(err);
6a912213
JB
4844 btrfs_log_new_name(trans, inode, NULL, parent);
4845 dput(parent);
a5719521 4846 }
39279cc3 4847
d3c2fdcf 4848 nr = trans->blocks_used;
ab78c84d 4849 btrfs_end_transaction_throttle(trans, root);
1832a6d5 4850fail:
39279cc3
CM
4851 if (drop_inode) {
4852 inode_dec_link_count(inode);
4853 iput(inode);
4854 }
d3c2fdcf 4855 btrfs_btree_balance_dirty(root, nr);
39279cc3
CM
4856 return err;
4857}
4858
39279cc3
CM
4859static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
4860{
b9d86667 4861 struct inode *inode = NULL;
39279cc3
CM
4862 struct btrfs_trans_handle *trans;
4863 struct btrfs_root *root = BTRFS_I(dir)->root;
4864 int err = 0;
4865 int drop_on_err = 0;
b9d86667 4866 u64 objectid = 0;
00e4e6b3 4867 u64 index = 0;
d3c2fdcf 4868 unsigned long nr = 1;
39279cc3 4869
a22285a6
YZ
4870 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4871 if (err)
4872 return err;
4873
9ed74f2d
JB
4874 /*
4875 * 2 items for inode and ref
4876 * 2 items for dir items
4877 * 1 for xattr if selinux is on
4878 */
a22285a6
YZ
4879 trans = btrfs_start_transaction(root, 5);
4880 if (IS_ERR(trans))
4881 return PTR_ERR(trans);
9ed74f2d 4882 btrfs_set_trans_block_group(trans, dir);
39279cc3 4883
aec7477b 4884 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
a1b075d2 4885 dentry->d_name.len, dir->i_ino, objectid,
00e4e6b3
CM
4886 BTRFS_I(dir)->block_group, S_IFDIR | mode,
4887 &index);
39279cc3
CM
4888 if (IS_ERR(inode)) {
4889 err = PTR_ERR(inode);
4890 goto out_fail;
4891 }
5f39d397 4892
39279cc3 4893 drop_on_err = 1;
33268eaf 4894
f34f57a3 4895 err = btrfs_init_inode_security(trans, inode, dir);
33268eaf
JB
4896 if (err)
4897 goto out_fail;
4898
39279cc3
CM
4899 inode->i_op = &btrfs_dir_inode_operations;
4900 inode->i_fop = &btrfs_dir_file_operations;
4901 btrfs_set_trans_block_group(trans, inode);
4902
dbe674a9 4903 btrfs_i_size_write(inode, 0);
39279cc3
CM
4904 err = btrfs_update_inode(trans, root, inode);
4905 if (err)
4906 goto out_fail;
5f39d397 4907
a1b075d2
JB
4908 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
4909 dentry->d_name.len, 0, index);
39279cc3
CM
4910 if (err)
4911 goto out_fail;
5f39d397 4912
39279cc3
CM
4913 d_instantiate(dentry, inode);
4914 drop_on_err = 0;
39279cc3
CM
4915 btrfs_update_inode_block_group(trans, inode);
4916 btrfs_update_inode_block_group(trans, dir);
4917
4918out_fail:
d3c2fdcf 4919 nr = trans->blocks_used;
ab78c84d 4920 btrfs_end_transaction_throttle(trans, root);
39279cc3
CM
4921 if (drop_on_err)
4922 iput(inode);
d3c2fdcf 4923 btrfs_btree_balance_dirty(root, nr);
39279cc3
CM
4924 return err;
4925}
4926
d352ac68
CM
4927/* helper for btfs_get_extent. Given an existing extent in the tree,
4928 * and an extent that you want to insert, deal with overlap and insert
4929 * the new extent into the tree.
4930 */
3b951516
CM
4931static int merge_extent_mapping(struct extent_map_tree *em_tree,
4932 struct extent_map *existing,
e6dcd2dc
CM
4933 struct extent_map *em,
4934 u64 map_start, u64 map_len)
3b951516
CM
4935{
4936 u64 start_diff;
3b951516 4937
e6dcd2dc
CM
4938 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
4939 start_diff = map_start - em->start;
4940 em->start = map_start;
4941 em->len = map_len;
c8b97818
CM
4942 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
4943 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
e6dcd2dc 4944 em->block_start += start_diff;
c8b97818
CM
4945 em->block_len -= start_diff;
4946 }
e6dcd2dc 4947 return add_extent_mapping(em_tree, em);
3b951516
CM
4948}
4949
c8b97818
CM
4950static noinline int uncompress_inline(struct btrfs_path *path,
4951 struct inode *inode, struct page *page,
4952 size_t pg_offset, u64 extent_offset,
4953 struct btrfs_file_extent_item *item)
4954{
4955 int ret;
4956 struct extent_buffer *leaf = path->nodes[0];
4957 char *tmp;
4958 size_t max_size;
4959 unsigned long inline_size;
4960 unsigned long ptr;
261507a0 4961 int compress_type;
c8b97818
CM
4962
4963 WARN_ON(pg_offset != 0);
261507a0 4964 compress_type = btrfs_file_extent_compression(leaf, item);
c8b97818
CM
4965 max_size = btrfs_file_extent_ram_bytes(leaf, item);
4966 inline_size = btrfs_file_extent_inline_item_len(leaf,
4967 btrfs_item_nr(leaf, path->slots[0]));
4968 tmp = kmalloc(inline_size, GFP_NOFS);
4969 ptr = btrfs_file_extent_inline_start(item);
4970
4971 read_extent_buffer(leaf, tmp, ptr, inline_size);
4972
5b050f04 4973 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
261507a0
LZ
4974 ret = btrfs_decompress(compress_type, tmp, page,
4975 extent_offset, inline_size, max_size);
c8b97818
CM
4976 if (ret) {
4977 char *kaddr = kmap_atomic(page, KM_USER0);
4978 unsigned long copy_size = min_t(u64,
4979 PAGE_CACHE_SIZE - pg_offset,
4980 max_size - extent_offset);
4981 memset(kaddr + pg_offset, 0, copy_size);
4982 kunmap_atomic(kaddr, KM_USER0);
4983 }
4984 kfree(tmp);
4985 return 0;
4986}
4987
d352ac68
CM
4988/*
4989 * a bit scary, this does extent mapping from logical file offset to the disk.
d397712b
CM
4990 * the ugly parts come from merging extents from the disk with the in-ram
4991 * representation. This gets more complex because of the data=ordered code,
d352ac68
CM
4992 * where the in-ram extents might be locked pending data=ordered completion.
4993 *
4994 * This also copies inline extents directly into the page.
4995 */
d397712b 4996
a52d9a80 4997struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
70dec807 4998 size_t pg_offset, u64 start, u64 len,
a52d9a80
CM
4999 int create)
5000{
5001 int ret;
5002 int err = 0;
db94535d 5003 u64 bytenr;
a52d9a80
CM
5004 u64 extent_start = 0;
5005 u64 extent_end = 0;
5006 u64 objectid = inode->i_ino;
5007 u32 found_type;
f421950f 5008 struct btrfs_path *path = NULL;
a52d9a80
CM
5009 struct btrfs_root *root = BTRFS_I(inode)->root;
5010 struct btrfs_file_extent_item *item;
5f39d397
CM
5011 struct extent_buffer *leaf;
5012 struct btrfs_key found_key;
a52d9a80
CM
5013 struct extent_map *em = NULL;
5014 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
d1310b2e 5015 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
a52d9a80 5016 struct btrfs_trans_handle *trans = NULL;
261507a0 5017 int compress_type;
a52d9a80 5018
a52d9a80 5019again:
890871be 5020 read_lock(&em_tree->lock);
d1310b2e 5021 em = lookup_extent_mapping(em_tree, start, len);
a061fc8d
CM
5022 if (em)
5023 em->bdev = root->fs_info->fs_devices->latest_bdev;
890871be 5024 read_unlock(&em_tree->lock);
d1310b2e 5025
a52d9a80 5026 if (em) {
e1c4b745
CM
5027 if (em->start > start || em->start + em->len <= start)
5028 free_extent_map(em);
5029 else if (em->block_start == EXTENT_MAP_INLINE && page)
70dec807
CM
5030 free_extent_map(em);
5031 else
5032 goto out;
a52d9a80 5033 }
d1310b2e 5034 em = alloc_extent_map(GFP_NOFS);
a52d9a80 5035 if (!em) {
d1310b2e
CM
5036 err = -ENOMEM;
5037 goto out;
a52d9a80 5038 }
e6dcd2dc 5039 em->bdev = root->fs_info->fs_devices->latest_bdev;
d1310b2e 5040 em->start = EXTENT_MAP_HOLE;
445a6944 5041 em->orig_start = EXTENT_MAP_HOLE;
d1310b2e 5042 em->len = (u64)-1;
c8b97818 5043 em->block_len = (u64)-1;
f421950f
CM
5044
5045 if (!path) {
5046 path = btrfs_alloc_path();
5047 BUG_ON(!path);
5048 }
5049
179e29e4
CM
5050 ret = btrfs_lookup_file_extent(trans, root, path,
5051 objectid, start, trans != NULL);
a52d9a80
CM
5052 if (ret < 0) {
5053 err = ret;
5054 goto out;
5055 }
5056
5057 if (ret != 0) {
5058 if (path->slots[0] == 0)
5059 goto not_found;
5060 path->slots[0]--;
5061 }
5062
5f39d397
CM
5063 leaf = path->nodes[0];
5064 item = btrfs_item_ptr(leaf, path->slots[0],
a52d9a80 5065 struct btrfs_file_extent_item);
a52d9a80 5066 /* are we inside the extent that was found? */
5f39d397
CM
5067 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5068 found_type = btrfs_key_type(&found_key);
5069 if (found_key.objectid != objectid ||
a52d9a80
CM
5070 found_type != BTRFS_EXTENT_DATA_KEY) {
5071 goto not_found;
5072 }
5073
5f39d397
CM
5074 found_type = btrfs_file_extent_type(leaf, item);
5075 extent_start = found_key.offset;
261507a0 5076 compress_type = btrfs_file_extent_compression(leaf, item);
d899e052
YZ
5077 if (found_type == BTRFS_FILE_EXTENT_REG ||
5078 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
a52d9a80 5079 extent_end = extent_start +
db94535d 5080 btrfs_file_extent_num_bytes(leaf, item);
9036c102
YZ
5081 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
5082 size_t size;
5083 size = btrfs_file_extent_inline_len(leaf, item);
5084 extent_end = (extent_start + size + root->sectorsize - 1) &
5085 ~((u64)root->sectorsize - 1);
5086 }
5087
5088 if (start >= extent_end) {
5089 path->slots[0]++;
5090 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
5091 ret = btrfs_next_leaf(root, path);
5092 if (ret < 0) {
5093 err = ret;
5094 goto out;
a52d9a80 5095 }
9036c102
YZ
5096 if (ret > 0)
5097 goto not_found;
5098 leaf = path->nodes[0];
a52d9a80 5099 }
9036c102
YZ
5100 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5101 if (found_key.objectid != objectid ||
5102 found_key.type != BTRFS_EXTENT_DATA_KEY)
5103 goto not_found;
5104 if (start + len <= found_key.offset)
5105 goto not_found;
5106 em->start = start;
5107 em->len = found_key.offset - start;
5108 goto not_found_em;
5109 }
5110
d899e052
YZ
5111 if (found_type == BTRFS_FILE_EXTENT_REG ||
5112 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
9036c102
YZ
5113 em->start = extent_start;
5114 em->len = extent_end - extent_start;
ff5b7ee3
YZ
5115 em->orig_start = extent_start -
5116 btrfs_file_extent_offset(leaf, item);
db94535d
CM
5117 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
5118 if (bytenr == 0) {
5f39d397 5119 em->block_start = EXTENT_MAP_HOLE;
a52d9a80
CM
5120 goto insert;
5121 }
261507a0 5122 if (compress_type != BTRFS_COMPRESS_NONE) {
c8b97818 5123 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
261507a0 5124 em->compress_type = compress_type;
c8b97818
CM
5125 em->block_start = bytenr;
5126 em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
5127 item);
5128 } else {
5129 bytenr += btrfs_file_extent_offset(leaf, item);
5130 em->block_start = bytenr;
5131 em->block_len = em->len;
d899e052
YZ
5132 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
5133 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
c8b97818 5134 }
a52d9a80
CM
5135 goto insert;
5136 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
5f39d397 5137 unsigned long ptr;
a52d9a80 5138 char *map;
3326d1b0
CM
5139 size_t size;
5140 size_t extent_offset;
5141 size_t copy_size;
a52d9a80 5142
689f9346 5143 em->block_start = EXTENT_MAP_INLINE;
c8b97818 5144 if (!page || create) {
689f9346 5145 em->start = extent_start;
9036c102 5146 em->len = extent_end - extent_start;
689f9346
Y
5147 goto out;
5148 }
5f39d397 5149
9036c102
YZ
5150 size = btrfs_file_extent_inline_len(leaf, item);
5151 extent_offset = page_offset(page) + pg_offset - extent_start;
70dec807 5152 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
3326d1b0 5153 size - extent_offset);
3326d1b0 5154 em->start = extent_start + extent_offset;
70dec807
CM
5155 em->len = (copy_size + root->sectorsize - 1) &
5156 ~((u64)root->sectorsize - 1);
ff5b7ee3 5157 em->orig_start = EXTENT_MAP_INLINE;
261507a0 5158 if (compress_type) {
c8b97818 5159 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
261507a0
LZ
5160 em->compress_type = compress_type;
5161 }
689f9346 5162 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
179e29e4 5163 if (create == 0 && !PageUptodate(page)) {
261507a0
LZ
5164 if (btrfs_file_extent_compression(leaf, item) !=
5165 BTRFS_COMPRESS_NONE) {
c8b97818
CM
5166 ret = uncompress_inline(path, inode, page,
5167 pg_offset,
5168 extent_offset, item);
5169 BUG_ON(ret);
5170 } else {
5171 map = kmap(page);
5172 read_extent_buffer(leaf, map + pg_offset, ptr,
5173 copy_size);
93c82d57
CM
5174 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
5175 memset(map + pg_offset + copy_size, 0,
5176 PAGE_CACHE_SIZE - pg_offset -
5177 copy_size);
5178 }
c8b97818
CM
5179 kunmap(page);
5180 }
179e29e4
CM
5181 flush_dcache_page(page);
5182 } else if (create && PageUptodate(page)) {
0ca1f7ce 5183 WARN_ON(1);
179e29e4
CM
5184 if (!trans) {
5185 kunmap(page);
5186 free_extent_map(em);
5187 em = NULL;
5188 btrfs_release_path(root, path);
f9295749 5189 trans = btrfs_join_transaction(root, 1);
3612b495
TI
5190 if (IS_ERR(trans))
5191 return ERR_CAST(trans);
179e29e4
CM
5192 goto again;
5193 }
c8b97818 5194 map = kmap(page);
70dec807 5195 write_extent_buffer(leaf, map + pg_offset, ptr,
179e29e4 5196 copy_size);
c8b97818 5197 kunmap(page);
179e29e4 5198 btrfs_mark_buffer_dirty(leaf);
a52d9a80 5199 }
d1310b2e
CM
5200 set_extent_uptodate(io_tree, em->start,
5201 extent_map_end(em) - 1, GFP_NOFS);
a52d9a80
CM
5202 goto insert;
5203 } else {
d397712b 5204 printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
a52d9a80
CM
5205 WARN_ON(1);
5206 }
5207not_found:
5208 em->start = start;
d1310b2e 5209 em->len = len;
a52d9a80 5210not_found_em:
5f39d397 5211 em->block_start = EXTENT_MAP_HOLE;
9036c102 5212 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
a52d9a80
CM
5213insert:
5214 btrfs_release_path(root, path);
d1310b2e 5215 if (em->start > start || extent_map_end(em) <= start) {
d397712b
CM
5216 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
5217 "[%llu %llu]\n", (unsigned long long)em->start,
5218 (unsigned long long)em->len,
5219 (unsigned long long)start,
5220 (unsigned long long)len);
a52d9a80
CM
5221 err = -EIO;
5222 goto out;
5223 }
d1310b2e
CM
5224
5225 err = 0;
890871be 5226 write_lock(&em_tree->lock);
a52d9a80 5227 ret = add_extent_mapping(em_tree, em);
3b951516
CM
5228 /* it is possible that someone inserted the extent into the tree
5229 * while we had the lock dropped. It is also possible that
5230 * an overlapping map exists in the tree
5231 */
a52d9a80 5232 if (ret == -EEXIST) {
3b951516 5233 struct extent_map *existing;
e6dcd2dc
CM
5234
5235 ret = 0;
5236
3b951516 5237 existing = lookup_extent_mapping(em_tree, start, len);
e1c4b745
CM
5238 if (existing && (existing->start > start ||
5239 existing->start + existing->len <= start)) {
5240 free_extent_map(existing);
5241 existing = NULL;
5242 }
3b951516
CM
5243 if (!existing) {
5244 existing = lookup_extent_mapping(em_tree, em->start,
5245 em->len);
5246 if (existing) {
5247 err = merge_extent_mapping(em_tree, existing,
e6dcd2dc
CM
5248 em, start,
5249 root->sectorsize);
3b951516
CM
5250 free_extent_map(existing);
5251 if (err) {
5252 free_extent_map(em);
5253 em = NULL;
5254 }
5255 } else {
5256 err = -EIO;
3b951516
CM
5257 free_extent_map(em);
5258 em = NULL;
5259 }
5260 } else {
5261 free_extent_map(em);
5262 em = existing;
e6dcd2dc 5263 err = 0;
a52d9a80 5264 }
a52d9a80 5265 }
890871be 5266 write_unlock(&em_tree->lock);
a52d9a80 5267out:
f421950f
CM
5268 if (path)
5269 btrfs_free_path(path);
a52d9a80
CM
5270 if (trans) {
5271 ret = btrfs_end_transaction(trans, root);
d397712b 5272 if (!err)
a52d9a80
CM
5273 err = ret;
5274 }
a52d9a80
CM
5275 if (err) {
5276 free_extent_map(em);
a52d9a80
CM
5277 return ERR_PTR(err);
5278 }
5279 return em;
5280}
5281
ec29ed5b
CM
5282struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
5283 size_t pg_offset, u64 start, u64 len,
5284 int create)
5285{
5286 struct extent_map *em;
5287 struct extent_map *hole_em = NULL;
5288 u64 range_start = start;
5289 u64 end;
5290 u64 found;
5291 u64 found_end;
5292 int err = 0;
5293
5294 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
5295 if (IS_ERR(em))
5296 return em;
5297 if (em) {
5298 /*
5299 * if our em maps to a hole, there might
5300 * actually be delalloc bytes behind it
5301 */
5302 if (em->block_start != EXTENT_MAP_HOLE)
5303 return em;
5304 else
5305 hole_em = em;
5306 }
5307
5308 /* check to see if we've wrapped (len == -1 or similar) */
5309 end = start + len;
5310 if (end < start)
5311 end = (u64)-1;
5312 else
5313 end -= 1;
5314
5315 em = NULL;
5316
5317 /* ok, we didn't find anything, lets look for delalloc */
5318 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
5319 end, len, EXTENT_DELALLOC, 1);
5320 found_end = range_start + found;
5321 if (found_end < range_start)
5322 found_end = (u64)-1;
5323
5324 /*
5325 * we didn't find anything useful, return
5326 * the original results from get_extent()
5327 */
5328 if (range_start > end || found_end <= start) {
5329 em = hole_em;
5330 hole_em = NULL;
5331 goto out;
5332 }
5333
5334 /* adjust the range_start to make sure it doesn't
5335 * go backwards from the start they passed in
5336 */
5337 range_start = max(start,range_start);
5338 found = found_end - range_start;
5339
5340 if (found > 0) {
5341 u64 hole_start = start;
5342 u64 hole_len = len;
5343
5344 em = alloc_extent_map(GFP_NOFS);
5345 if (!em) {
5346 err = -ENOMEM;
5347 goto out;
5348 }
5349 /*
5350 * when btrfs_get_extent can't find anything it
5351 * returns one huge hole
5352 *
5353 * make sure what it found really fits our range, and
5354 * adjust to make sure it is based on the start from
5355 * the caller
5356 */
5357 if (hole_em) {
5358 u64 calc_end = extent_map_end(hole_em);
5359
5360 if (calc_end <= start || (hole_em->start > end)) {
5361 free_extent_map(hole_em);
5362 hole_em = NULL;
5363 } else {
5364 hole_start = max(hole_em->start, start);
5365 hole_len = calc_end - hole_start;
5366 }
5367 }
5368 em->bdev = NULL;
5369 if (hole_em && range_start > hole_start) {
5370 /* our hole starts before our delalloc, so we
5371 * have to return just the parts of the hole
5372 * that go until the delalloc starts
5373 */
5374 em->len = min(hole_len,
5375 range_start - hole_start);
5376 em->start = hole_start;
5377 em->orig_start = hole_start;
5378 /*
5379 * don't adjust block start at all,
5380 * it is fixed at EXTENT_MAP_HOLE
5381 */
5382 em->block_start = hole_em->block_start;
5383 em->block_len = hole_len;
5384 } else {
5385 em->start = range_start;
5386 em->len = found;
5387 em->orig_start = range_start;
5388 em->block_start = EXTENT_MAP_DELALLOC;
5389 em->block_len = found;
5390 }
5391 } else if (hole_em) {
5392 return hole_em;
5393 }
5394out:
5395
5396 free_extent_map(hole_em);
5397 if (err) {
5398 free_extent_map(em);
5399 return ERR_PTR(err);
5400 }
5401 return em;
5402}
5403
4b46fce2
JB
5404static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
5405 u64 start, u64 len)
5406{
5407 struct btrfs_root *root = BTRFS_I(inode)->root;
5408 struct btrfs_trans_handle *trans;
5409 struct extent_map *em;
5410 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
5411 struct btrfs_key ins;
5412 u64 alloc_hint;
5413 int ret;
5414
5415 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
5416
5417 trans = btrfs_join_transaction(root, 0);
3612b495
TI
5418 if (IS_ERR(trans))
5419 return ERR_CAST(trans);
4b46fce2
JB
5420
5421 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5422
5423 alloc_hint = get_extent_allocation_hint(inode, start, len);
5424 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
5425 alloc_hint, (u64)-1, &ins, 1);
5426 if (ret) {
5427 em = ERR_PTR(ret);
5428 goto out;
5429 }
5430
5431 em = alloc_extent_map(GFP_NOFS);
5432 if (!em) {
5433 em = ERR_PTR(-ENOMEM);
5434 goto out;
5435 }
5436
5437 em->start = start;
5438 em->orig_start = em->start;
5439 em->len = ins.offset;
5440
5441 em->block_start = ins.objectid;
5442 em->block_len = ins.offset;
5443 em->bdev = root->fs_info->fs_devices->latest_bdev;
5444 set_bit(EXTENT_FLAG_PINNED, &em->flags);
5445
5446 while (1) {
5447 write_lock(&em_tree->lock);
5448 ret = add_extent_mapping(em_tree, em);
5449 write_unlock(&em_tree->lock);
5450 if (ret != -EEXIST)
5451 break;
5452 btrfs_drop_extent_cache(inode, start, start + em->len - 1, 0);
5453 }
5454
5455 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
5456 ins.offset, ins.offset, 0);
5457 if (ret) {
5458 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
5459 em = ERR_PTR(ret);
5460 }
5461out:
5462 btrfs_end_transaction(trans, root);
5463 return em;
5464}
5465
46bfbb5c
CM
5466/*
5467 * returns 1 when the nocow is safe, < 1 on error, 0 if the
5468 * block must be cow'd
5469 */
5470static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
5471 struct inode *inode, u64 offset, u64 len)
5472{
5473 struct btrfs_path *path;
5474 int ret;
5475 struct extent_buffer *leaf;
5476 struct btrfs_root *root = BTRFS_I(inode)->root;
5477 struct btrfs_file_extent_item *fi;
5478 struct btrfs_key key;
5479 u64 disk_bytenr;
5480 u64 backref_offset;
5481 u64 extent_end;
5482 u64 num_bytes;
5483 int slot;
5484 int found_type;
5485
5486 path = btrfs_alloc_path();
5487 if (!path)
5488 return -ENOMEM;
5489
5490 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
5491 offset, 0);
5492 if (ret < 0)
5493 goto out;
5494
5495 slot = path->slots[0];
5496 if (ret == 1) {
5497 if (slot == 0) {
5498 /* can't find the item, must cow */
5499 ret = 0;
5500 goto out;
5501 }
5502 slot--;
5503 }
5504 ret = 0;
5505 leaf = path->nodes[0];
5506 btrfs_item_key_to_cpu(leaf, &key, slot);
5507 if (key.objectid != inode->i_ino ||
5508 key.type != BTRFS_EXTENT_DATA_KEY) {
5509 /* not our file or wrong item type, must cow */
5510 goto out;
5511 }
5512
5513 if (key.offset > offset) {
5514 /* Wrong offset, must cow */
5515 goto out;
5516 }
5517
5518 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5519 found_type = btrfs_file_extent_type(leaf, fi);
5520 if (found_type != BTRFS_FILE_EXTENT_REG &&
5521 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
5522 /* not a regular extent, must cow */
5523 goto out;
5524 }
5525 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
5526 backref_offset = btrfs_file_extent_offset(leaf, fi);
5527
5528 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
5529 if (extent_end < offset + len) {
5530 /* extent doesn't include our full range, must cow */
5531 goto out;
5532 }
5533
5534 if (btrfs_extent_readonly(root, disk_bytenr))
5535 goto out;
5536
5537 /*
5538 * look for other files referencing this extent, if we
5539 * find any we must cow
5540 */
5541 if (btrfs_cross_ref_exist(trans, root, inode->i_ino,
5542 key.offset - backref_offset, disk_bytenr))
5543 goto out;
5544
5545 /*
5546 * adjust disk_bytenr and num_bytes to cover just the bytes
5547 * in this extent we are about to write. If there
5548 * are any csums in that range we have to cow in order
5549 * to keep the csums correct
5550 */
5551 disk_bytenr += backref_offset;
5552 disk_bytenr += offset - key.offset;
5553 num_bytes = min(offset + len, extent_end) - offset;
5554 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
5555 goto out;
5556 /*
5557 * all of the above have passed, it is safe to overwrite this extent
5558 * without cow
5559 */
5560 ret = 1;
5561out:
5562 btrfs_free_path(path);
5563 return ret;
5564}
5565
4b46fce2
JB
5566static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
5567 struct buffer_head *bh_result, int create)
5568{
5569 struct extent_map *em;
5570 struct btrfs_root *root = BTRFS_I(inode)->root;
5571 u64 start = iblock << inode->i_blkbits;
5572 u64 len = bh_result->b_size;
46bfbb5c 5573 struct btrfs_trans_handle *trans;
4b46fce2
JB
5574
5575 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
5576 if (IS_ERR(em))
5577 return PTR_ERR(em);
5578
5579 /*
5580 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
5581 * io. INLINE is special, and we could probably kludge it in here, but
5582 * it's still buffered so for safety lets just fall back to the generic
5583 * buffered path.
5584 *
5585 * For COMPRESSED we _have_ to read the entire extent in so we can
5586 * decompress it, so there will be buffering required no matter what we
5587 * do, so go ahead and fallback to buffered.
5588 *
5589 * We return -ENOTBLK because thats what makes DIO go ahead and go back
5590 * to buffered IO. Don't blame me, this is the price we pay for using
5591 * the generic code.
5592 */
5593 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
5594 em->block_start == EXTENT_MAP_INLINE) {
5595 free_extent_map(em);
5596 return -ENOTBLK;
5597 }
5598
5599 /* Just a good old fashioned hole, return */
5600 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
5601 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
5602 free_extent_map(em);
5603 /* DIO will do one hole at a time, so just unlock a sector */
5604 unlock_extent(&BTRFS_I(inode)->io_tree, start,
5605 start + root->sectorsize - 1, GFP_NOFS);
5606 return 0;
5607 }
5608
5609 /*
5610 * We don't allocate a new extent in the following cases
5611 *
5612 * 1) The inode is marked as NODATACOW. In this case we'll just use the
5613 * existing extent.
5614 * 2) The extent is marked as PREALLOC. We're good to go here and can
5615 * just use the extent.
5616 *
5617 */
46bfbb5c
CM
5618 if (!create) {
5619 len = em->len - (start - em->start);
4b46fce2 5620 goto map;
46bfbb5c 5621 }
4b46fce2
JB
5622
5623 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
5624 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
5625 em->block_start != EXTENT_MAP_HOLE)) {
4b46fce2
JB
5626 int type;
5627 int ret;
46bfbb5c 5628 u64 block_start;
4b46fce2
JB
5629
5630 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5631 type = BTRFS_ORDERED_PREALLOC;
5632 else
5633 type = BTRFS_ORDERED_NOCOW;
46bfbb5c 5634 len = min(len, em->len - (start - em->start));
4b46fce2 5635 block_start = em->block_start + (start - em->start);
46bfbb5c
CM
5636
5637 /*
5638 * we're not going to log anything, but we do need
5639 * to make sure the current transaction stays open
5640 * while we look for nocow cross refs
5641 */
5642 trans = btrfs_join_transaction(root, 0);
3612b495 5643 if (IS_ERR(trans))
46bfbb5c
CM
5644 goto must_cow;
5645
5646 if (can_nocow_odirect(trans, inode, start, len) == 1) {
5647 ret = btrfs_add_ordered_extent_dio(inode, start,
5648 block_start, len, len, type);
5649 btrfs_end_transaction(trans, root);
5650 if (ret) {
5651 free_extent_map(em);
5652 return ret;
5653 }
5654 goto unlock;
4b46fce2 5655 }
46bfbb5c 5656 btrfs_end_transaction(trans, root);
4b46fce2 5657 }
46bfbb5c
CM
5658must_cow:
5659 /*
5660 * this will cow the extent, reset the len in case we changed
5661 * it above
5662 */
5663 len = bh_result->b_size;
5664 free_extent_map(em);
5665 em = btrfs_new_extent_direct(inode, start, len);
5666 if (IS_ERR(em))
5667 return PTR_ERR(em);
5668 len = min(len, em->len - (start - em->start));
5669unlock:
4845e44f
CM
5670 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, start + len - 1,
5671 EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DIRTY, 1,
5672 0, NULL, GFP_NOFS);
4b46fce2
JB
5673map:
5674 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
5675 inode->i_blkbits;
46bfbb5c 5676 bh_result->b_size = len;
4b46fce2
JB
5677 bh_result->b_bdev = em->bdev;
5678 set_buffer_mapped(bh_result);
5679 if (create && !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5680 set_buffer_new(bh_result);
5681
5682 free_extent_map(em);
5683
5684 return 0;
5685}
5686
5687struct btrfs_dio_private {
5688 struct inode *inode;
5689 u64 logical_offset;
5690 u64 disk_bytenr;
5691 u64 bytes;
5692 u32 *csums;
5693 void *private;
e65e1535
MX
5694
5695 /* number of bios pending for this dio */
5696 atomic_t pending_bios;
5697
5698 /* IO errors */
5699 int errors;
5700
5701 struct bio *orig_bio;
4b46fce2
JB
5702};
5703
5704static void btrfs_endio_direct_read(struct bio *bio, int err)
5705{
e65e1535 5706 struct btrfs_dio_private *dip = bio->bi_private;
4b46fce2
JB
5707 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
5708 struct bio_vec *bvec = bio->bi_io_vec;
4b46fce2
JB
5709 struct inode *inode = dip->inode;
5710 struct btrfs_root *root = BTRFS_I(inode)->root;
5711 u64 start;
5712 u32 *private = dip->csums;
5713
5714 start = dip->logical_offset;
5715 do {
5716 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
5717 struct page *page = bvec->bv_page;
5718 char *kaddr;
5719 u32 csum = ~(u32)0;
5720 unsigned long flags;
5721
5722 local_irq_save(flags);
5723 kaddr = kmap_atomic(page, KM_IRQ0);
5724 csum = btrfs_csum_data(root, kaddr + bvec->bv_offset,
5725 csum, bvec->bv_len);
5726 btrfs_csum_final(csum, (char *)&csum);
5727 kunmap_atomic(kaddr, KM_IRQ0);
5728 local_irq_restore(flags);
5729
5730 flush_dcache_page(bvec->bv_page);
5731 if (csum != *private) {
5732 printk(KERN_ERR "btrfs csum failed ino %lu off"
5733 " %llu csum %u private %u\n",
5734 inode->i_ino, (unsigned long long)start,
5735 csum, *private);
5736 err = -EIO;
5737 }
5738 }
5739
5740 start += bvec->bv_len;
5741 private++;
5742 bvec++;
5743 } while (bvec <= bvec_end);
5744
5745 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
5746 dip->logical_offset + dip->bytes - 1, GFP_NOFS);
5747 bio->bi_private = dip->private;
5748
5749 kfree(dip->csums);
5750 kfree(dip);
5751 dio_end_io(bio, err);
5752}
5753
5754static void btrfs_endio_direct_write(struct bio *bio, int err)
5755{
5756 struct btrfs_dio_private *dip = bio->bi_private;
5757 struct inode *inode = dip->inode;
5758 struct btrfs_root *root = BTRFS_I(inode)->root;
5759 struct btrfs_trans_handle *trans;
5760 struct btrfs_ordered_extent *ordered = NULL;
5761 struct extent_state *cached_state = NULL;
163cf09c
CM
5762 u64 ordered_offset = dip->logical_offset;
5763 u64 ordered_bytes = dip->bytes;
4b46fce2
JB
5764 int ret;
5765
5766 if (err)
5767 goto out_done;
163cf09c
CM
5768again:
5769 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
5770 &ordered_offset,
5771 ordered_bytes);
4b46fce2 5772 if (!ret)
163cf09c 5773 goto out_test;
4b46fce2
JB
5774
5775 BUG_ON(!ordered);
5776
5777 trans = btrfs_join_transaction(root, 1);
3612b495 5778 if (IS_ERR(trans)) {
4b46fce2
JB
5779 err = -ENOMEM;
5780 goto out;
5781 }
5782 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5783
5784 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) {
5785 ret = btrfs_ordered_update_i_size(inode, 0, ordered);
5786 if (!ret)
5787 ret = btrfs_update_inode(trans, root, inode);
5788 err = ret;
5789 goto out;
5790 }
5791
5792 lock_extent_bits(&BTRFS_I(inode)->io_tree, ordered->file_offset,
5793 ordered->file_offset + ordered->len - 1, 0,
5794 &cached_state, GFP_NOFS);
5795
5796 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags)) {
5797 ret = btrfs_mark_extent_written(trans, inode,
5798 ordered->file_offset,
5799 ordered->file_offset +
5800 ordered->len);
5801 if (ret) {
5802 err = ret;
5803 goto out_unlock;
5804 }
5805 } else {
5806 ret = insert_reserved_file_extent(trans, inode,
5807 ordered->file_offset,
5808 ordered->start,
5809 ordered->disk_len,
5810 ordered->len,
5811 ordered->len,
5812 0, 0, 0,
5813 BTRFS_FILE_EXTENT_REG);
5814 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
5815 ordered->file_offset, ordered->len);
5816 if (ret) {
5817 err = ret;
5818 WARN_ON(1);
5819 goto out_unlock;
5820 }
5821 }
5822
5823 add_pending_csums(trans, inode, ordered->file_offset, &ordered->list);
5824 btrfs_ordered_update_i_size(inode, 0, ordered);
5825 btrfs_update_inode(trans, root, inode);
5826out_unlock:
5827 unlock_extent_cached(&BTRFS_I(inode)->io_tree, ordered->file_offset,
5828 ordered->file_offset + ordered->len - 1,
5829 &cached_state, GFP_NOFS);
5830out:
5831 btrfs_delalloc_release_metadata(inode, ordered->len);
5832 btrfs_end_transaction(trans, root);
163cf09c 5833 ordered_offset = ordered->file_offset + ordered->len;
4b46fce2
JB
5834 btrfs_put_ordered_extent(ordered);
5835 btrfs_put_ordered_extent(ordered);
163cf09c
CM
5836
5837out_test:
5838 /*
5839 * our bio might span multiple ordered extents. If we haven't
5840 * completed the accounting for the whole dio, go back and try again
5841 */
5842 if (ordered_offset < dip->logical_offset + dip->bytes) {
5843 ordered_bytes = dip->logical_offset + dip->bytes -
5844 ordered_offset;
5845 goto again;
5846 }
4b46fce2
JB
5847out_done:
5848 bio->bi_private = dip->private;
5849
5850 kfree(dip->csums);
5851 kfree(dip);
5852 dio_end_io(bio, err);
5853}
5854
eaf25d93
CM
5855static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
5856 struct bio *bio, int mirror_num,
5857 unsigned long bio_flags, u64 offset)
5858{
5859 int ret;
5860 struct btrfs_root *root = BTRFS_I(inode)->root;
5861 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
5862 BUG_ON(ret);
5863 return 0;
5864}
5865
e65e1535
MX
5866static void btrfs_end_dio_bio(struct bio *bio, int err)
5867{
5868 struct btrfs_dio_private *dip = bio->bi_private;
5869
5870 if (err) {
5871 printk(KERN_ERR "btrfs direct IO failed ino %lu rw %lu "
3dd1462e
JB
5872 "sector %#Lx len %u err no %d\n",
5873 dip->inode->i_ino, bio->bi_rw,
5874 (unsigned long long)bio->bi_sector, bio->bi_size, err);
e65e1535
MX
5875 dip->errors = 1;
5876
5877 /*
5878 * before atomic variable goto zero, we must make sure
5879 * dip->errors is perceived to be set.
5880 */
5881 smp_mb__before_atomic_dec();
5882 }
5883
5884 /* if there are more bios still pending for this dio, just exit */
5885 if (!atomic_dec_and_test(&dip->pending_bios))
5886 goto out;
5887
5888 if (dip->errors)
5889 bio_io_error(dip->orig_bio);
5890 else {
5891 set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags);
5892 bio_endio(dip->orig_bio, 0);
5893 }
5894out:
5895 bio_put(bio);
5896}
5897
5898static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
5899 u64 first_sector, gfp_t gfp_flags)
5900{
5901 int nr_vecs = bio_get_nr_vecs(bdev);
5902 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
5903}
5904
5905static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
5906 int rw, u64 file_offset, int skip_sum,
5907 u32 *csums)
5908{
5909 int write = rw & REQ_WRITE;
5910 struct btrfs_root *root = BTRFS_I(inode)->root;
5911 int ret;
5912
5913 bio_get(bio);
5914 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
5915 if (ret)
5916 goto err;
5917
5918 if (write && !skip_sum) {
5919 ret = btrfs_wq_submit_bio(root->fs_info,
5920 inode, rw, bio, 0, 0,
5921 file_offset,
5922 __btrfs_submit_bio_start_direct_io,
5923 __btrfs_submit_bio_done);
5924 goto err;
5925 } else if (!skip_sum)
5926 btrfs_lookup_bio_sums_dio(root, inode, bio,
5927 file_offset, csums);
5928
5929 ret = btrfs_map_bio(root, rw, bio, 0, 1);
5930err:
5931 bio_put(bio);
5932 return ret;
5933}
5934
5935static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
5936 int skip_sum)
5937{
5938 struct inode *inode = dip->inode;
5939 struct btrfs_root *root = BTRFS_I(inode)->root;
5940 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
5941 struct bio *bio;
5942 struct bio *orig_bio = dip->orig_bio;
5943 struct bio_vec *bvec = orig_bio->bi_io_vec;
5944 u64 start_sector = orig_bio->bi_sector;
5945 u64 file_offset = dip->logical_offset;
5946 u64 submit_len = 0;
5947 u64 map_length;
5948 int nr_pages = 0;
5949 u32 *csums = dip->csums;
5950 int ret = 0;
5951
5952 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
5953 if (!bio)
5954 return -ENOMEM;
5955 bio->bi_private = dip;
5956 bio->bi_end_io = btrfs_end_dio_bio;
5957 atomic_inc(&dip->pending_bios);
5958
5959 map_length = orig_bio->bi_size;
5960 ret = btrfs_map_block(map_tree, READ, start_sector << 9,
5961 &map_length, NULL, 0);
5962 if (ret) {
5963 bio_put(bio);
5964 return -EIO;
5965 }
5966
5967 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
5968 if (unlikely(map_length < submit_len + bvec->bv_len ||
5969 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
5970 bvec->bv_offset) < bvec->bv_len)) {
5971 /*
5972 * inc the count before we submit the bio so
5973 * we know the end IO handler won't happen before
5974 * we inc the count. Otherwise, the dip might get freed
5975 * before we're done setting it up
5976 */
5977 atomic_inc(&dip->pending_bios);
5978 ret = __btrfs_submit_dio_bio(bio, inode, rw,
5979 file_offset, skip_sum,
5980 csums);
5981 if (ret) {
5982 bio_put(bio);
5983 atomic_dec(&dip->pending_bios);
5984 goto out_err;
5985 }
5986
5987 if (!skip_sum)
5988 csums = csums + nr_pages;
5989 start_sector += submit_len >> 9;
5990 file_offset += submit_len;
5991
5992 submit_len = 0;
5993 nr_pages = 0;
5994
5995 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
5996 start_sector, GFP_NOFS);
5997 if (!bio)
5998 goto out_err;
5999 bio->bi_private = dip;
6000 bio->bi_end_io = btrfs_end_dio_bio;
6001
6002 map_length = orig_bio->bi_size;
6003 ret = btrfs_map_block(map_tree, READ, start_sector << 9,
6004 &map_length, NULL, 0);
6005 if (ret) {
6006 bio_put(bio);
6007 goto out_err;
6008 }
6009 } else {
6010 submit_len += bvec->bv_len;
6011 nr_pages ++;
6012 bvec++;
6013 }
6014 }
6015
6016 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
6017 csums);
6018 if (!ret)
6019 return 0;
6020
6021 bio_put(bio);
6022out_err:
6023 dip->errors = 1;
6024 /*
6025 * before atomic variable goto zero, we must
6026 * make sure dip->errors is perceived to be set.
6027 */
6028 smp_mb__before_atomic_dec();
6029 if (atomic_dec_and_test(&dip->pending_bios))
6030 bio_io_error(dip->orig_bio);
6031
6032 /* bio_end_io() will handle error, so we needn't return it */
6033 return 0;
6034}
6035
4b46fce2
JB
6036static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
6037 loff_t file_offset)
6038{
6039 struct btrfs_root *root = BTRFS_I(inode)->root;
6040 struct btrfs_dio_private *dip;
6041 struct bio_vec *bvec = bio->bi_io_vec;
4b46fce2 6042 int skip_sum;
7b6d91da 6043 int write = rw & REQ_WRITE;
4b46fce2
JB
6044 int ret = 0;
6045
6046 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
6047
6048 dip = kmalloc(sizeof(*dip), GFP_NOFS);
6049 if (!dip) {
6050 ret = -ENOMEM;
6051 goto free_ordered;
6052 }
6053 dip->csums = NULL;
6054
6055 if (!skip_sum) {
6056 dip->csums = kmalloc(sizeof(u32) * bio->bi_vcnt, GFP_NOFS);
6057 if (!dip->csums) {
b4966b77 6058 kfree(dip);
4b46fce2
JB
6059 ret = -ENOMEM;
6060 goto free_ordered;
6061 }
6062 }
6063
6064 dip->private = bio->bi_private;
6065 dip->inode = inode;
6066 dip->logical_offset = file_offset;
6067
4b46fce2
JB
6068 dip->bytes = 0;
6069 do {
6070 dip->bytes += bvec->bv_len;
6071 bvec++;
6072 } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
6073
46bfbb5c 6074 dip->disk_bytenr = (u64)bio->bi_sector << 9;
4b46fce2 6075 bio->bi_private = dip;
e65e1535
MX
6076 dip->errors = 0;
6077 dip->orig_bio = bio;
6078 atomic_set(&dip->pending_bios, 0);
4b46fce2
JB
6079
6080 if (write)
6081 bio->bi_end_io = btrfs_endio_direct_write;
6082 else
6083 bio->bi_end_io = btrfs_endio_direct_read;
6084
e65e1535
MX
6085 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
6086 if (!ret)
eaf25d93 6087 return;
4b46fce2
JB
6088free_ordered:
6089 /*
6090 * If this is a write, we need to clean up the reserved space and kill
6091 * the ordered extent.
6092 */
6093 if (write) {
6094 struct btrfs_ordered_extent *ordered;
955256f2 6095 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
4b46fce2
JB
6096 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
6097 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
6098 btrfs_free_reserved_extent(root, ordered->start,
6099 ordered->disk_len);
6100 btrfs_put_ordered_extent(ordered);
6101 btrfs_put_ordered_extent(ordered);
6102 }
6103 bio_endio(bio, ret);
6104}
6105
5a5f79b5
CM
6106static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
6107 const struct iovec *iov, loff_t offset,
6108 unsigned long nr_segs)
6109{
6110 int seg;
6111 size_t size;
6112 unsigned long addr;
6113 unsigned blocksize_mask = root->sectorsize - 1;
6114 ssize_t retval = -EINVAL;
6115 loff_t end = offset;
6116
6117 if (offset & blocksize_mask)
6118 goto out;
6119
6120 /* Check the memory alignment. Blocks cannot straddle pages */
6121 for (seg = 0; seg < nr_segs; seg++) {
6122 addr = (unsigned long)iov[seg].iov_base;
6123 size = iov[seg].iov_len;
6124 end += size;
6125 if ((addr & blocksize_mask) || (size & blocksize_mask))
6126 goto out;
6127 }
6128 retval = 0;
6129out:
6130 return retval;
6131}
16432985
CM
6132static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
6133 const struct iovec *iov, loff_t offset,
6134 unsigned long nr_segs)
6135{
4b46fce2
JB
6136 struct file *file = iocb->ki_filp;
6137 struct inode *inode = file->f_mapping->host;
6138 struct btrfs_ordered_extent *ordered;
4845e44f 6139 struct extent_state *cached_state = NULL;
4b46fce2
JB
6140 u64 lockstart, lockend;
6141 ssize_t ret;
4845e44f
CM
6142 int writing = rw & WRITE;
6143 int write_bits = 0;
3f7c579c 6144 size_t count = iov_length(iov, nr_segs);
4b46fce2 6145
5a5f79b5
CM
6146 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
6147 offset, nr_segs)) {
6148 return 0;
6149 }
6150
4b46fce2 6151 lockstart = offset;
3f7c579c
CM
6152 lockend = offset + count - 1;
6153
6154 if (writing) {
6155 ret = btrfs_delalloc_reserve_space(inode, count);
6156 if (ret)
6157 goto out;
6158 }
4845e44f 6159
4b46fce2 6160 while (1) {
4845e44f
CM
6161 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6162 0, &cached_state, GFP_NOFS);
4b46fce2
JB
6163 /*
6164 * We're concerned with the entire range that we're going to be
6165 * doing DIO to, so we need to make sure theres no ordered
6166 * extents in this range.
6167 */
6168 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6169 lockend - lockstart + 1);
6170 if (!ordered)
6171 break;
4845e44f
CM
6172 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6173 &cached_state, GFP_NOFS);
4b46fce2
JB
6174 btrfs_start_ordered_extent(inode, ordered, 1);
6175 btrfs_put_ordered_extent(ordered);
6176 cond_resched();
6177 }
6178
4845e44f
CM
6179 /*
6180 * we don't use btrfs_set_extent_delalloc because we don't want
6181 * the dirty or uptodate bits
6182 */
6183 if (writing) {
6184 write_bits = EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING;
6185 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6186 EXTENT_DELALLOC, 0, NULL, &cached_state,
6187 GFP_NOFS);
6188 if (ret) {
6189 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6190 lockend, EXTENT_LOCKED | write_bits,
6191 1, 0, &cached_state, GFP_NOFS);
6192 goto out;
6193 }
6194 }
6195
6196 free_extent_state(cached_state);
6197 cached_state = NULL;
6198
5a5f79b5
CM
6199 ret = __blockdev_direct_IO(rw, iocb, inode,
6200 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
6201 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
6202 btrfs_submit_direct, 0);
4b46fce2
JB
6203
6204 if (ret < 0 && ret != -EIOCBQUEUED) {
4845e44f
CM
6205 clear_extent_bit(&BTRFS_I(inode)->io_tree, offset,
6206 offset + iov_length(iov, nr_segs) - 1,
6207 EXTENT_LOCKED | write_bits, 1, 0,
6208 &cached_state, GFP_NOFS);
4b46fce2
JB
6209 } else if (ret >= 0 && ret < iov_length(iov, nr_segs)) {
6210 /*
6211 * We're falling back to buffered, unlock the section we didn't
6212 * do IO on.
6213 */
4845e44f
CM
6214 clear_extent_bit(&BTRFS_I(inode)->io_tree, offset + ret,
6215 offset + iov_length(iov, nr_segs) - 1,
6216 EXTENT_LOCKED | write_bits, 1, 0,
6217 &cached_state, GFP_NOFS);
4b46fce2 6218 }
4845e44f
CM
6219out:
6220 free_extent_state(cached_state);
4b46fce2 6221 return ret;
16432985
CM
6222}
6223
1506fcc8
YS
6224static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
6225 __u64 start, __u64 len)
6226{
ec29ed5b 6227 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
1506fcc8
YS
6228}
6229
a52d9a80 6230int btrfs_readpage(struct file *file, struct page *page)
9ebefb18 6231{
d1310b2e
CM
6232 struct extent_io_tree *tree;
6233 tree = &BTRFS_I(page->mapping->host)->io_tree;
a52d9a80 6234 return extent_read_full_page(tree, page, btrfs_get_extent);
9ebefb18 6235}
1832a6d5 6236
a52d9a80 6237static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
39279cc3 6238{
d1310b2e 6239 struct extent_io_tree *tree;
b888db2b
CM
6240
6241
6242 if (current->flags & PF_MEMALLOC) {
6243 redirty_page_for_writepage(wbc, page);
6244 unlock_page(page);
6245 return 0;
6246 }
d1310b2e 6247 tree = &BTRFS_I(page->mapping->host)->io_tree;
a52d9a80 6248 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
9ebefb18
CM
6249}
6250
f421950f
CM
6251int btrfs_writepages(struct address_space *mapping,
6252 struct writeback_control *wbc)
b293f02e 6253{
d1310b2e 6254 struct extent_io_tree *tree;
771ed689 6255
d1310b2e 6256 tree = &BTRFS_I(mapping->host)->io_tree;
b293f02e
CM
6257 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
6258}
6259
3ab2fb5a
CM
6260static int
6261btrfs_readpages(struct file *file, struct address_space *mapping,
6262 struct list_head *pages, unsigned nr_pages)
6263{
d1310b2e
CM
6264 struct extent_io_tree *tree;
6265 tree = &BTRFS_I(mapping->host)->io_tree;
3ab2fb5a
CM
6266 return extent_readpages(tree, mapping, pages, nr_pages,
6267 btrfs_get_extent);
6268}
e6dcd2dc 6269static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
9ebefb18 6270{
d1310b2e
CM
6271 struct extent_io_tree *tree;
6272 struct extent_map_tree *map;
a52d9a80 6273 int ret;
8c2383c3 6274
d1310b2e
CM
6275 tree = &BTRFS_I(page->mapping->host)->io_tree;
6276 map = &BTRFS_I(page->mapping->host)->extent_tree;
70dec807 6277 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
a52d9a80
CM
6278 if (ret == 1) {
6279 ClearPagePrivate(page);
6280 set_page_private(page, 0);
6281 page_cache_release(page);
39279cc3 6282 }
a52d9a80 6283 return ret;
39279cc3
CM
6284}
6285
e6dcd2dc
CM
6286static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
6287{
98509cfc
CM
6288 if (PageWriteback(page) || PageDirty(page))
6289 return 0;
b335b003 6290 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
e6dcd2dc
CM
6291}
6292
a52d9a80 6293static void btrfs_invalidatepage(struct page *page, unsigned long offset)
39279cc3 6294{
d1310b2e 6295 struct extent_io_tree *tree;
e6dcd2dc 6296 struct btrfs_ordered_extent *ordered;
2ac55d41 6297 struct extent_state *cached_state = NULL;
e6dcd2dc
CM
6298 u64 page_start = page_offset(page);
6299 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
39279cc3 6300
8b62b72b
CM
6301
6302 /*
6303 * we have the page locked, so new writeback can't start,
6304 * and the dirty bit won't be cleared while we are here.
6305 *
6306 * Wait for IO on this page so that we can safely clear
6307 * the PagePrivate2 bit and do ordered accounting
6308 */
e6dcd2dc 6309 wait_on_page_writeback(page);
8b62b72b 6310
d1310b2e 6311 tree = &BTRFS_I(page->mapping->host)->io_tree;
e6dcd2dc
CM
6312 if (offset) {
6313 btrfs_releasepage(page, GFP_NOFS);
6314 return;
6315 }
2ac55d41
JB
6316 lock_extent_bits(tree, page_start, page_end, 0, &cached_state,
6317 GFP_NOFS);
e6dcd2dc
CM
6318 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
6319 page_offset(page));
6320 if (ordered) {
eb84ae03
CM
6321 /*
6322 * IO on this page will never be started, so we need
6323 * to account for any ordered extents now
6324 */
e6dcd2dc
CM
6325 clear_extent_bit(tree, page_start, page_end,
6326 EXTENT_DIRTY | EXTENT_DELALLOC |
32c00aff 6327 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0,
2ac55d41 6328 &cached_state, GFP_NOFS);
8b62b72b
CM
6329 /*
6330 * whoever cleared the private bit is responsible
6331 * for the finish_ordered_io
6332 */
6333 if (TestClearPagePrivate2(page)) {
6334 btrfs_finish_ordered_io(page->mapping->host,
6335 page_start, page_end);
6336 }
e6dcd2dc 6337 btrfs_put_ordered_extent(ordered);
2ac55d41
JB
6338 cached_state = NULL;
6339 lock_extent_bits(tree, page_start, page_end, 0, &cached_state,
6340 GFP_NOFS);
e6dcd2dc
CM
6341 }
6342 clear_extent_bit(tree, page_start, page_end,
32c00aff 6343 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
2ac55d41 6344 EXTENT_DO_ACCOUNTING, 1, 1, &cached_state, GFP_NOFS);
e6dcd2dc
CM
6345 __btrfs_releasepage(page, GFP_NOFS);
6346
4a096752 6347 ClearPageChecked(page);
9ad6b7bc 6348 if (PagePrivate(page)) {
9ad6b7bc
CM
6349 ClearPagePrivate(page);
6350 set_page_private(page, 0);
6351 page_cache_release(page);
6352 }
39279cc3
CM
6353}
6354
9ebefb18
CM
6355/*
6356 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
6357 * called from a page fault handler when a page is first dirtied. Hence we must
6358 * be careful to check for EOF conditions here. We set the page up correctly
6359 * for a written page which means we get ENOSPC checking when writing into
6360 * holes and correct delalloc and unwritten extent mapping on filesystems that
6361 * support these features.
6362 *
6363 * We are not allowed to take the i_mutex here so we have to play games to
6364 * protect against truncate races as the page could now be beyond EOF. Because
6365 * vmtruncate() writes the inode size before removing pages, once we have the
6366 * page lock we can determine safely if the page is beyond EOF. If it is not
6367 * beyond EOF, then the page is guaranteed safe against truncation until we
6368 * unlock the page.
6369 */
c2ec175c 6370int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
9ebefb18 6371{
c2ec175c 6372 struct page *page = vmf->page;
6da6abae 6373 struct inode *inode = fdentry(vma->vm_file)->d_inode;
1832a6d5 6374 struct btrfs_root *root = BTRFS_I(inode)->root;
e6dcd2dc
CM
6375 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
6376 struct btrfs_ordered_extent *ordered;
2ac55d41 6377 struct extent_state *cached_state = NULL;
e6dcd2dc
CM
6378 char *kaddr;
6379 unsigned long zero_start;
9ebefb18 6380 loff_t size;
1832a6d5 6381 int ret;
a52d9a80 6382 u64 page_start;
e6dcd2dc 6383 u64 page_end;
9ebefb18 6384
0ca1f7ce 6385 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
56a76f82
NP
6386 if (ret) {
6387 if (ret == -ENOMEM)
6388 ret = VM_FAULT_OOM;
6389 else /* -ENOSPC, -EIO, etc */
6390 ret = VM_FAULT_SIGBUS;
1832a6d5 6391 goto out;
56a76f82 6392 }
1832a6d5 6393
56a76f82 6394 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
e6dcd2dc 6395again:
9ebefb18 6396 lock_page(page);
9ebefb18 6397 size = i_size_read(inode);
e6dcd2dc
CM
6398 page_start = page_offset(page);
6399 page_end = page_start + PAGE_CACHE_SIZE - 1;
a52d9a80 6400
9ebefb18 6401 if ((page->mapping != inode->i_mapping) ||
e6dcd2dc 6402 (page_start >= size)) {
9ebefb18
CM
6403 /* page got truncated out from underneath us */
6404 goto out_unlock;
6405 }
e6dcd2dc
CM
6406 wait_on_page_writeback(page);
6407
2ac55d41
JB
6408 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state,
6409 GFP_NOFS);
e6dcd2dc
CM
6410 set_page_extent_mapped(page);
6411
eb84ae03
CM
6412 /*
6413 * we can't set the delalloc bits if there are pending ordered
6414 * extents. Drop our locks and wait for them to finish
6415 */
e6dcd2dc
CM
6416 ordered = btrfs_lookup_ordered_extent(inode, page_start);
6417 if (ordered) {
2ac55d41
JB
6418 unlock_extent_cached(io_tree, page_start, page_end,
6419 &cached_state, GFP_NOFS);
e6dcd2dc 6420 unlock_page(page);
eb84ae03 6421 btrfs_start_ordered_extent(inode, ordered, 1);
e6dcd2dc
CM
6422 btrfs_put_ordered_extent(ordered);
6423 goto again;
6424 }
6425
fbf19087
JB
6426 /*
6427 * XXX - page_mkwrite gets called every time the page is dirtied, even
6428 * if it was already dirty, so for space accounting reasons we need to
6429 * clear any delalloc bits for the range we are fixing to save. There
6430 * is probably a better way to do this, but for now keep consistent with
6431 * prepare_pages in the normal write path.
6432 */
2ac55d41 6433 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
32c00aff 6434 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
2ac55d41 6435 0, 0, &cached_state, GFP_NOFS);
fbf19087 6436
2ac55d41
JB
6437 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
6438 &cached_state);
9ed74f2d 6439 if (ret) {
2ac55d41
JB
6440 unlock_extent_cached(io_tree, page_start, page_end,
6441 &cached_state, GFP_NOFS);
9ed74f2d
JB
6442 ret = VM_FAULT_SIGBUS;
6443 goto out_unlock;
6444 }
e6dcd2dc 6445 ret = 0;
9ebefb18
CM
6446
6447 /* page is wholly or partially inside EOF */
a52d9a80 6448 if (page_start + PAGE_CACHE_SIZE > size)
e6dcd2dc 6449 zero_start = size & ~PAGE_CACHE_MASK;
9ebefb18 6450 else
e6dcd2dc 6451 zero_start = PAGE_CACHE_SIZE;
9ebefb18 6452
e6dcd2dc
CM
6453 if (zero_start != PAGE_CACHE_SIZE) {
6454 kaddr = kmap(page);
6455 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
6456 flush_dcache_page(page);
6457 kunmap(page);
6458 }
247e743c 6459 ClearPageChecked(page);
e6dcd2dc 6460 set_page_dirty(page);
50a9b214 6461 SetPageUptodate(page);
5a3f23d5 6462
257c62e1
CM
6463 BTRFS_I(inode)->last_trans = root->fs_info->generation;
6464 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
6465
2ac55d41 6466 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
9ebefb18
CM
6467
6468out_unlock:
50a9b214
CM
6469 if (!ret)
6470 return VM_FAULT_LOCKED;
9ebefb18 6471 unlock_page(page);
0ca1f7ce 6472 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
1832a6d5 6473out:
9ebefb18
CM
6474 return ret;
6475}
6476
a41ad394 6477static int btrfs_truncate(struct inode *inode)
39279cc3
CM
6478{
6479 struct btrfs_root *root = BTRFS_I(inode)->root;
6480 int ret;
6481 struct btrfs_trans_handle *trans;
d3c2fdcf 6482 unsigned long nr;
dbe674a9 6483 u64 mask = root->sectorsize - 1;
39279cc3 6484
5d5e103a
JB
6485 ret = btrfs_truncate_page(inode->i_mapping, inode->i_size);
6486 if (ret)
a41ad394 6487 return ret;
8082510e 6488
4a096752 6489 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
8082510e 6490 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
39279cc3 6491
d68fc57b
YZ
6492 trans = btrfs_start_transaction(root, 0);
6493 BUG_ON(IS_ERR(trans));
8082510e 6494 btrfs_set_trans_block_group(trans, inode);
d68fc57b 6495 trans->block_rsv = root->orphan_block_rsv;
5a3f23d5
CM
6496
6497 /*
6498 * setattr is responsible for setting the ordered_data_close flag,
6499 * but that is only tested during the last file release. That
6500 * could happen well after the next commit, leaving a great big
6501 * window where new writes may get lost if someone chooses to write
6502 * to this file after truncating to zero
6503 *
6504 * The inode doesn't have any dirty data here, and so if we commit
6505 * this is a noop. If someone immediately starts writing to the inode
6506 * it is very likely we'll catch some of their writes in this
6507 * transaction, and the commit will find this file on the ordered
6508 * data list with good things to send down.
6509 *
6510 * This is a best effort solution, there is still a window where
6511 * using truncate to replace the contents of the file will
6512 * end up with a zero length file after a crash.
6513 */
6514 if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close)
6515 btrfs_add_ordered_operation(trans, root, inode);
6516
8082510e 6517 while (1) {
d68fc57b
YZ
6518 if (!trans) {
6519 trans = btrfs_start_transaction(root, 0);
6520 BUG_ON(IS_ERR(trans));
6521 btrfs_set_trans_block_group(trans, inode);
6522 trans->block_rsv = root->orphan_block_rsv;
6523 }
6524
6525 ret = btrfs_block_rsv_check(trans, root,
6526 root->orphan_block_rsv, 0, 5);
6527 if (ret) {
6528 BUG_ON(ret != -EAGAIN);
6529 ret = btrfs_commit_transaction(trans, root);
6530 BUG_ON(ret);
6531 trans = NULL;
6532 continue;
6533 }
6534
8082510e
YZ
6535 ret = btrfs_truncate_inode_items(trans, root, inode,
6536 inode->i_size,
6537 BTRFS_EXTENT_DATA_KEY);
6538 if (ret != -EAGAIN)
6539 break;
39279cc3 6540
8082510e
YZ
6541 ret = btrfs_update_inode(trans, root, inode);
6542 BUG_ON(ret);
5f39d397 6543
8082510e
YZ
6544 nr = trans->blocks_used;
6545 btrfs_end_transaction(trans, root);
d68fc57b 6546 trans = NULL;
8082510e 6547 btrfs_btree_balance_dirty(root, nr);
8082510e
YZ
6548 }
6549
6550 if (ret == 0 && inode->i_nlink > 0) {
6551 ret = btrfs_orphan_del(trans, inode);
6552 BUG_ON(ret);
6553 }
6554
6555 ret = btrfs_update_inode(trans, root, inode);
7b128766
JB
6556 BUG_ON(ret);
6557
7b128766 6558 nr = trans->blocks_used;
89ce8a63 6559 ret = btrfs_end_transaction_throttle(trans, root);
39279cc3 6560 BUG_ON(ret);
d3c2fdcf 6561 btrfs_btree_balance_dirty(root, nr);
a41ad394
JB
6562
6563 return ret;
39279cc3
CM
6564}
6565
d352ac68
CM
6566/*
6567 * create a new subvolume directory/inode (helper for the ioctl).
6568 */
d2fb3437 6569int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
76dda93c 6570 struct btrfs_root *new_root,
d2fb3437 6571 u64 new_dirid, u64 alloc_hint)
39279cc3 6572{
39279cc3 6573 struct inode *inode;
76dda93c 6574 int err;
00e4e6b3 6575 u64 index = 0;
39279cc3 6576
aec7477b 6577 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
d2fb3437 6578 new_dirid, alloc_hint, S_IFDIR | 0700, &index);
54aa1f4d 6579 if (IS_ERR(inode))
f46b5a66 6580 return PTR_ERR(inode);
39279cc3
CM
6581 inode->i_op = &btrfs_dir_inode_operations;
6582 inode->i_fop = &btrfs_dir_file_operations;
6583
39279cc3 6584 inode->i_nlink = 1;
dbe674a9 6585 btrfs_i_size_write(inode, 0);
3b96362c 6586
76dda93c
YZ
6587 err = btrfs_update_inode(trans, new_root, inode);
6588 BUG_ON(err);
cb8e7090 6589
76dda93c 6590 iput(inode);
cb8e7090 6591 return 0;
39279cc3
CM
6592}
6593
d352ac68
CM
6594/* helper function for file defrag and space balancing. This
6595 * forces readahead on a given range of bytes in an inode
6596 */
edbd8d4e 6597unsigned long btrfs_force_ra(struct address_space *mapping,
86479a04
CM
6598 struct file_ra_state *ra, struct file *file,
6599 pgoff_t offset, pgoff_t last_index)
6600{
8e7bf94f 6601 pgoff_t req_size = last_index - offset + 1;
86479a04 6602
86479a04
CM
6603 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
6604 return offset + req_size;
86479a04
CM
6605}
6606
39279cc3
CM
6607struct inode *btrfs_alloc_inode(struct super_block *sb)
6608{
6609 struct btrfs_inode *ei;
2ead6ae7 6610 struct inode *inode;
39279cc3
CM
6611
6612 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
6613 if (!ei)
6614 return NULL;
2ead6ae7
YZ
6615
6616 ei->root = NULL;
6617 ei->space_info = NULL;
6618 ei->generation = 0;
6619 ei->sequence = 0;
15ee9bc7 6620 ei->last_trans = 0;
257c62e1 6621 ei->last_sub_trans = 0;
e02119d5 6622 ei->logged_trans = 0;
2ead6ae7
YZ
6623 ei->delalloc_bytes = 0;
6624 ei->reserved_bytes = 0;
6625 ei->disk_i_size = 0;
6626 ei->flags = 0;
6627 ei->index_cnt = (u64)-1;
6628 ei->last_unlink_trans = 0;
6629
0ca1f7ce 6630 atomic_set(&ei->outstanding_extents, 0);
57a45ced 6631 atomic_set(&ei->reserved_extents, 0);
2ead6ae7
YZ
6632
6633 ei->ordered_data_close = 0;
d68fc57b 6634 ei->orphan_meta_reserved = 0;
2ead6ae7 6635 ei->dummy_inode = 0;
261507a0 6636 ei->force_compress = BTRFS_COMPRESS_NONE;
2ead6ae7
YZ
6637
6638 inode = &ei->vfs_inode;
6639 extent_map_tree_init(&ei->extent_tree, GFP_NOFS);
6640 extent_io_tree_init(&ei->io_tree, &inode->i_data, GFP_NOFS);
6641 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data, GFP_NOFS);
6642 mutex_init(&ei->log_mutex);
e6dcd2dc 6643 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
7b128766 6644 INIT_LIST_HEAD(&ei->i_orphan);
2ead6ae7 6645 INIT_LIST_HEAD(&ei->delalloc_inodes);
5a3f23d5 6646 INIT_LIST_HEAD(&ei->ordered_operations);
2ead6ae7
YZ
6647 RB_CLEAR_NODE(&ei->rb_node);
6648
6649 return inode;
39279cc3
CM
6650}
6651
fa0d7e3d
NP
6652static void btrfs_i_callback(struct rcu_head *head)
6653{
6654 struct inode *inode = container_of(head, struct inode, i_rcu);
6655 INIT_LIST_HEAD(&inode->i_dentry);
6656 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
6657}
6658
39279cc3
CM
6659void btrfs_destroy_inode(struct inode *inode)
6660{
e6dcd2dc 6661 struct btrfs_ordered_extent *ordered;
5a3f23d5
CM
6662 struct btrfs_root *root = BTRFS_I(inode)->root;
6663
39279cc3
CM
6664 WARN_ON(!list_empty(&inode->i_dentry));
6665 WARN_ON(inode->i_data.nrpages);
0ca1f7ce 6666 WARN_ON(atomic_read(&BTRFS_I(inode)->outstanding_extents));
57a45ced 6667 WARN_ON(atomic_read(&BTRFS_I(inode)->reserved_extents));
39279cc3 6668
a6dbd429
JB
6669 /*
6670 * This can happen where we create an inode, but somebody else also
6671 * created the same inode and we need to destroy the one we already
6672 * created.
6673 */
6674 if (!root)
6675 goto free;
6676
5a3f23d5
CM
6677 /*
6678 * Make sure we're properly removed from the ordered operation
6679 * lists.
6680 */
6681 smp_mb();
6682 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
6683 spin_lock(&root->fs_info->ordered_extent_lock);
6684 list_del_init(&BTRFS_I(inode)->ordered_operations);
6685 spin_unlock(&root->fs_info->ordered_extent_lock);
6686 }
6687
0af3d00b
JB
6688 if (root == root->fs_info->tree_root) {
6689 struct btrfs_block_group_cache *block_group;
6690
6691 block_group = btrfs_lookup_block_group(root->fs_info,
6692 BTRFS_I(inode)->block_group);
6693 if (block_group && block_group->inode == inode) {
6694 spin_lock(&block_group->lock);
6695 block_group->inode = NULL;
6696 spin_unlock(&block_group->lock);
6697 btrfs_put_block_group(block_group);
6698 } else if (block_group) {
6699 btrfs_put_block_group(block_group);
6700 }
6701 }
6702
d68fc57b 6703 spin_lock(&root->orphan_lock);
7b128766 6704 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
8082510e
YZ
6705 printk(KERN_INFO "BTRFS: inode %lu still on the orphan list\n",
6706 inode->i_ino);
6707 list_del_init(&BTRFS_I(inode)->i_orphan);
7b128766 6708 }
d68fc57b 6709 spin_unlock(&root->orphan_lock);
7b128766 6710
d397712b 6711 while (1) {
e6dcd2dc
CM
6712 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
6713 if (!ordered)
6714 break;
6715 else {
d397712b
CM
6716 printk(KERN_ERR "btrfs found ordered "
6717 "extent %llu %llu on inode cleanup\n",
6718 (unsigned long long)ordered->file_offset,
6719 (unsigned long long)ordered->len);
e6dcd2dc
CM
6720 btrfs_remove_ordered_extent(inode, ordered);
6721 btrfs_put_ordered_extent(ordered);
6722 btrfs_put_ordered_extent(ordered);
6723 }
6724 }
5d4f98a2 6725 inode_tree_del(inode);
5b21f2ed 6726 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
a6dbd429 6727free:
fa0d7e3d 6728 call_rcu(&inode->i_rcu, btrfs_i_callback);
39279cc3
CM
6729}
6730
45321ac5 6731int btrfs_drop_inode(struct inode *inode)
76dda93c
YZ
6732{
6733 struct btrfs_root *root = BTRFS_I(inode)->root;
45321ac5 6734
0af3d00b
JB
6735 if (btrfs_root_refs(&root->root_item) == 0 &&
6736 root != root->fs_info->tree_root)
45321ac5 6737 return 1;
76dda93c 6738 else
45321ac5 6739 return generic_drop_inode(inode);
76dda93c
YZ
6740}
6741
0ee0fda0 6742static void init_once(void *foo)
39279cc3
CM
6743{
6744 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
6745
6746 inode_init_once(&ei->vfs_inode);
6747}
6748
6749void btrfs_destroy_cachep(void)
6750{
6751 if (btrfs_inode_cachep)
6752 kmem_cache_destroy(btrfs_inode_cachep);
6753 if (btrfs_trans_handle_cachep)
6754 kmem_cache_destroy(btrfs_trans_handle_cachep);
6755 if (btrfs_transaction_cachep)
6756 kmem_cache_destroy(btrfs_transaction_cachep);
39279cc3
CM
6757 if (btrfs_path_cachep)
6758 kmem_cache_destroy(btrfs_path_cachep);
dc89e982
JB
6759 if (btrfs_free_space_cachep)
6760 kmem_cache_destroy(btrfs_free_space_cachep);
39279cc3
CM
6761}
6762
6763int btrfs_init_cachep(void)
6764{
9601e3f6
CH
6765 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
6766 sizeof(struct btrfs_inode), 0,
6767 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
39279cc3
CM
6768 if (!btrfs_inode_cachep)
6769 goto fail;
9601e3f6
CH
6770
6771 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
6772 sizeof(struct btrfs_trans_handle), 0,
6773 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
39279cc3
CM
6774 if (!btrfs_trans_handle_cachep)
6775 goto fail;
9601e3f6
CH
6776
6777 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
6778 sizeof(struct btrfs_transaction), 0,
6779 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
39279cc3
CM
6780 if (!btrfs_transaction_cachep)
6781 goto fail;
9601e3f6
CH
6782
6783 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
6784 sizeof(struct btrfs_path), 0,
6785 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
39279cc3
CM
6786 if (!btrfs_path_cachep)
6787 goto fail;
9601e3f6 6788
dc89e982
JB
6789 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space_cache",
6790 sizeof(struct btrfs_free_space), 0,
6791 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
6792 if (!btrfs_free_space_cachep)
6793 goto fail;
6794
39279cc3
CM
6795 return 0;
6796fail:
6797 btrfs_destroy_cachep();
6798 return -ENOMEM;
6799}
6800
6801static int btrfs_getattr(struct vfsmount *mnt,
6802 struct dentry *dentry, struct kstat *stat)
6803{
6804 struct inode *inode = dentry->d_inode;
6805 generic_fillattr(inode, stat);
3394e160 6806 stat->dev = BTRFS_I(inode)->root->anon_super.s_dev;
d6667462 6807 stat->blksize = PAGE_CACHE_SIZE;
a76a3cd4
YZ
6808 stat->blocks = (inode_get_bytes(inode) +
6809 BTRFS_I(inode)->delalloc_bytes) >> 9;
39279cc3
CM
6810 return 0;
6811}
6812
d397712b
CM
6813static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
6814 struct inode *new_dir, struct dentry *new_dentry)
39279cc3
CM
6815{
6816 struct btrfs_trans_handle *trans;
6817 struct btrfs_root *root = BTRFS_I(old_dir)->root;
4df27c4d 6818 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
39279cc3
CM
6819 struct inode *new_inode = new_dentry->d_inode;
6820 struct inode *old_inode = old_dentry->d_inode;
6821 struct timespec ctime = CURRENT_TIME;
00e4e6b3 6822 u64 index = 0;
4df27c4d 6823 u64 root_objectid;
39279cc3
CM
6824 int ret;
6825
f679a840
YZ
6826 if (new_dir->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
6827 return -EPERM;
6828
4df27c4d
YZ
6829 /* we only allow rename subvolume link between subvolumes */
6830 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
3394e160
CM
6831 return -EXDEV;
6832
4df27c4d
YZ
6833 if (old_inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
6834 (new_inode && new_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID))
39279cc3 6835 return -ENOTEMPTY;
5f39d397 6836
4df27c4d
YZ
6837 if (S_ISDIR(old_inode->i_mode) && new_inode &&
6838 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
6839 return -ENOTEMPTY;
5a3f23d5
CM
6840 /*
6841 * we're using rename to replace one file with another.
6842 * and the replacement file is large. Start IO on it now so
6843 * we don't add too much work to the end of the transaction
6844 */
4baf8c92 6845 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
5a3f23d5
CM
6846 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
6847 filemap_flush(old_inode->i_mapping);
6848
76dda93c
YZ
6849 /* close the racy window with snapshot create/destroy ioctl */
6850 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
6851 down_read(&root->fs_info->subvol_sem);
a22285a6
YZ
6852 /*
6853 * We want to reserve the absolute worst case amount of items. So if
6854 * both inodes are subvols and we need to unlink them then that would
6855 * require 4 item modifications, but if they are both normal inodes it
6856 * would require 5 item modifications, so we'll assume their normal
6857 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
6858 * should cover the worst case number of items we'll modify.
6859 */
6860 trans = btrfs_start_transaction(root, 20);
6861 if (IS_ERR(trans))
6862 return PTR_ERR(trans);
76dda93c 6863
a5719521 6864 btrfs_set_trans_block_group(trans, new_dir);
5f39d397 6865
4df27c4d
YZ
6866 if (dest != root)
6867 btrfs_record_root_in_trans(trans, dest);
5f39d397 6868
a5719521
YZ
6869 ret = btrfs_set_inode_index(new_dir, &index);
6870 if (ret)
6871 goto out_fail;
5a3f23d5 6872
a5719521 6873 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
4df27c4d
YZ
6874 /* force full log commit if subvolume involved. */
6875 root->fs_info->last_trans_log_full_commit = trans->transid;
6876 } else {
a5719521
YZ
6877 ret = btrfs_insert_inode_ref(trans, dest,
6878 new_dentry->d_name.name,
6879 new_dentry->d_name.len,
6880 old_inode->i_ino,
6881 new_dir->i_ino, index);
6882 if (ret)
6883 goto out_fail;
4df27c4d
YZ
6884 /*
6885 * this is an ugly little race, but the rename is required
6886 * to make sure that if we crash, the inode is either at the
6887 * old name or the new one. pinning the log transaction lets
6888 * us make sure we don't allow a log commit to come in after
6889 * we unlink the name but before we add the new name back in.
6890 */
6891 btrfs_pin_log_trans(root);
6892 }
5a3f23d5
CM
6893 /*
6894 * make sure the inode gets flushed if it is replacing
6895 * something.
6896 */
6897 if (new_inode && new_inode->i_size &&
6898 old_inode && S_ISREG(old_inode->i_mode)) {
6899 btrfs_add_ordered_operation(trans, root, old_inode);
6900 }
6901
39279cc3
CM
6902 old_dir->i_ctime = old_dir->i_mtime = ctime;
6903 new_dir->i_ctime = new_dir->i_mtime = ctime;
6904 old_inode->i_ctime = ctime;
5f39d397 6905
12fcfd22
CM
6906 if (old_dentry->d_parent != new_dentry->d_parent)
6907 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
6908
4df27c4d
YZ
6909 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
6910 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
6911 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
6912 old_dentry->d_name.name,
6913 old_dentry->d_name.len);
6914 } else {
6915 btrfs_inc_nlink(old_dentry->d_inode);
6916 ret = btrfs_unlink_inode(trans, root, old_dir,
6917 old_dentry->d_inode,
6918 old_dentry->d_name.name,
6919 old_dentry->d_name.len);
6920 }
6921 BUG_ON(ret);
39279cc3
CM
6922
6923 if (new_inode) {
6924 new_inode->i_ctime = CURRENT_TIME;
4df27c4d
YZ
6925 if (unlikely(new_inode->i_ino ==
6926 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
6927 root_objectid = BTRFS_I(new_inode)->location.objectid;
6928 ret = btrfs_unlink_subvol(trans, dest, new_dir,
6929 root_objectid,
6930 new_dentry->d_name.name,
6931 new_dentry->d_name.len);
6932 BUG_ON(new_inode->i_nlink == 0);
6933 } else {
6934 ret = btrfs_unlink_inode(trans, dest, new_dir,
6935 new_dentry->d_inode,
6936 new_dentry->d_name.name,
6937 new_dentry->d_name.len);
6938 }
6939 BUG_ON(ret);
7b128766 6940 if (new_inode->i_nlink == 0) {
e02119d5 6941 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
4df27c4d 6942 BUG_ON(ret);
7b128766 6943 }
39279cc3 6944 }
aec7477b 6945
4df27c4d
YZ
6946 ret = btrfs_add_link(trans, new_dir, old_inode,
6947 new_dentry->d_name.name,
a5719521 6948 new_dentry->d_name.len, 0, index);
4df27c4d 6949 BUG_ON(ret);
39279cc3 6950
4df27c4d 6951 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
6a912213
JB
6952 struct dentry *parent = dget_parent(new_dentry);
6953 btrfs_log_new_name(trans, old_inode, old_dir, parent);
6954 dput(parent);
4df27c4d
YZ
6955 btrfs_end_log_trans(root);
6956 }
39279cc3 6957out_fail:
ab78c84d 6958 btrfs_end_transaction_throttle(trans, root);
4df27c4d 6959
76dda93c
YZ
6960 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
6961 up_read(&root->fs_info->subvol_sem);
9ed74f2d 6962
39279cc3
CM
6963 return ret;
6964}
6965
d352ac68
CM
6966/*
6967 * some fairly slow code that needs optimization. This walks the list
6968 * of all the inodes with pending delalloc and forces them to disk.
6969 */
24bbcf04 6970int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
ea8c2819
CM
6971{
6972 struct list_head *head = &root->fs_info->delalloc_inodes;
6973 struct btrfs_inode *binode;
5b21f2ed 6974 struct inode *inode;
ea8c2819 6975
c146afad
YZ
6976 if (root->fs_info->sb->s_flags & MS_RDONLY)
6977 return -EROFS;
6978
75eff68e 6979 spin_lock(&root->fs_info->delalloc_lock);
d397712b 6980 while (!list_empty(head)) {
ea8c2819
CM
6981 binode = list_entry(head->next, struct btrfs_inode,
6982 delalloc_inodes);
5b21f2ed
ZY
6983 inode = igrab(&binode->vfs_inode);
6984 if (!inode)
6985 list_del_init(&binode->delalloc_inodes);
75eff68e 6986 spin_unlock(&root->fs_info->delalloc_lock);
5b21f2ed 6987 if (inode) {
8c8bee1d 6988 filemap_flush(inode->i_mapping);
24bbcf04
YZ
6989 if (delay_iput)
6990 btrfs_add_delayed_iput(inode);
6991 else
6992 iput(inode);
5b21f2ed
ZY
6993 }
6994 cond_resched();
75eff68e 6995 spin_lock(&root->fs_info->delalloc_lock);
ea8c2819 6996 }
75eff68e 6997 spin_unlock(&root->fs_info->delalloc_lock);
8c8bee1d
CM
6998
6999 /* the filemap_flush will queue IO into the worker threads, but
7000 * we have to make sure the IO is actually started and that
7001 * ordered extents get created before we return
7002 */
7003 atomic_inc(&root->fs_info->async_submit_draining);
d397712b 7004 while (atomic_read(&root->fs_info->nr_async_submits) ||
771ed689 7005 atomic_read(&root->fs_info->async_delalloc_pages)) {
8c8bee1d 7006 wait_event(root->fs_info->async_submit_wait,
771ed689
CM
7007 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
7008 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
8c8bee1d
CM
7009 }
7010 atomic_dec(&root->fs_info->async_submit_draining);
ea8c2819
CM
7011 return 0;
7012}
7013
0019f10d
JB
7014int btrfs_start_one_delalloc_inode(struct btrfs_root *root, int delay_iput,
7015 int sync)
5da9d01b
YZ
7016{
7017 struct btrfs_inode *binode;
7018 struct inode *inode = NULL;
7019
7020 spin_lock(&root->fs_info->delalloc_lock);
7021 while (!list_empty(&root->fs_info->delalloc_inodes)) {
7022 binode = list_entry(root->fs_info->delalloc_inodes.next,
7023 struct btrfs_inode, delalloc_inodes);
7024 inode = igrab(&binode->vfs_inode);
7025 if (inode) {
7026 list_move_tail(&binode->delalloc_inodes,
7027 &root->fs_info->delalloc_inodes);
7028 break;
7029 }
7030
7031 list_del_init(&binode->delalloc_inodes);
7032 cond_resched_lock(&root->fs_info->delalloc_lock);
7033 }
7034 spin_unlock(&root->fs_info->delalloc_lock);
7035
7036 if (inode) {
0019f10d
JB
7037 if (sync) {
7038 filemap_write_and_wait(inode->i_mapping);
7039 /*
7040 * We have to do this because compression doesn't
7041 * actually set PG_writeback until it submits the pages
7042 * for IO, which happens in an async thread, so we could
7043 * race and not actually wait for any writeback pages
7044 * because they've not been submitted yet. Technically
7045 * this could still be the case for the ordered stuff
7046 * since the async thread may not have started to do its
7047 * work yet. If this becomes the case then we need to
7048 * figure out a way to make sure that in writepage we
7049 * wait for any async pages to be submitted before
7050 * returning so that fdatawait does what its supposed to
7051 * do.
7052 */
7053 btrfs_wait_ordered_range(inode, 0, (u64)-1);
7054 } else {
7055 filemap_flush(inode->i_mapping);
7056 }
5da9d01b
YZ
7057 if (delay_iput)
7058 btrfs_add_delayed_iput(inode);
7059 else
7060 iput(inode);
7061 return 1;
7062 }
7063 return 0;
7064}
7065
39279cc3
CM
7066static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7067 const char *symname)
7068{
7069 struct btrfs_trans_handle *trans;
7070 struct btrfs_root *root = BTRFS_I(dir)->root;
7071 struct btrfs_path *path;
7072 struct btrfs_key key;
1832a6d5 7073 struct inode *inode = NULL;
39279cc3
CM
7074 int err;
7075 int drop_inode = 0;
7076 u64 objectid;
00e4e6b3 7077 u64 index = 0 ;
39279cc3
CM
7078 int name_len;
7079 int datasize;
5f39d397 7080 unsigned long ptr;
39279cc3 7081 struct btrfs_file_extent_item *ei;
5f39d397 7082 struct extent_buffer *leaf;
1832a6d5 7083 unsigned long nr = 0;
39279cc3
CM
7084
7085 name_len = strlen(symname) + 1;
7086 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
7087 return -ENAMETOOLONG;
1832a6d5 7088
a22285a6
YZ
7089 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
7090 if (err)
7091 return err;
9ed74f2d
JB
7092 /*
7093 * 2 items for inode item and ref
7094 * 2 items for dir items
7095 * 1 item for xattr if selinux is on
7096 */
a22285a6
YZ
7097 trans = btrfs_start_transaction(root, 5);
7098 if (IS_ERR(trans))
7099 return PTR_ERR(trans);
1832a6d5 7100
39279cc3
CM
7101 btrfs_set_trans_block_group(trans, dir);
7102
aec7477b 7103 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
a1b075d2 7104 dentry->d_name.len, dir->i_ino, objectid,
00e4e6b3
CM
7105 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
7106 &index);
39279cc3
CM
7107 err = PTR_ERR(inode);
7108 if (IS_ERR(inode))
7109 goto out_unlock;
7110
f34f57a3 7111 err = btrfs_init_inode_security(trans, inode, dir);
33268eaf
JB
7112 if (err) {
7113 drop_inode = 1;
7114 goto out_unlock;
7115 }
7116
39279cc3 7117 btrfs_set_trans_block_group(trans, inode);
a1b075d2 7118 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
39279cc3
CM
7119 if (err)
7120 drop_inode = 1;
7121 else {
7122 inode->i_mapping->a_ops = &btrfs_aops;
04160088 7123 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
39279cc3
CM
7124 inode->i_fop = &btrfs_file_operations;
7125 inode->i_op = &btrfs_file_inode_operations;
d1310b2e 7126 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
39279cc3 7127 }
39279cc3
CM
7128 btrfs_update_inode_block_group(trans, inode);
7129 btrfs_update_inode_block_group(trans, dir);
7130 if (drop_inode)
7131 goto out_unlock;
7132
7133 path = btrfs_alloc_path();
7134 BUG_ON(!path);
7135 key.objectid = inode->i_ino;
7136 key.offset = 0;
39279cc3
CM
7137 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
7138 datasize = btrfs_file_extent_calc_inline_size(name_len);
7139 err = btrfs_insert_empty_item(trans, root, path, &key,
7140 datasize);
54aa1f4d
CM
7141 if (err) {
7142 drop_inode = 1;
7143 goto out_unlock;
7144 }
5f39d397
CM
7145 leaf = path->nodes[0];
7146 ei = btrfs_item_ptr(leaf, path->slots[0],
7147 struct btrfs_file_extent_item);
7148 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
7149 btrfs_set_file_extent_type(leaf, ei,
39279cc3 7150 BTRFS_FILE_EXTENT_INLINE);
c8b97818
CM
7151 btrfs_set_file_extent_encryption(leaf, ei, 0);
7152 btrfs_set_file_extent_compression(leaf, ei, 0);
7153 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
7154 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
7155
39279cc3 7156 ptr = btrfs_file_extent_inline_start(ei);
5f39d397
CM
7157 write_extent_buffer(leaf, symname, ptr, name_len);
7158 btrfs_mark_buffer_dirty(leaf);
39279cc3 7159 btrfs_free_path(path);
5f39d397 7160
39279cc3
CM
7161 inode->i_op = &btrfs_symlink_inode_operations;
7162 inode->i_mapping->a_ops = &btrfs_symlink_aops;
04160088 7163 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
d899e052 7164 inode_set_bytes(inode, name_len);
dbe674a9 7165 btrfs_i_size_write(inode, name_len - 1);
54aa1f4d
CM
7166 err = btrfs_update_inode(trans, root, inode);
7167 if (err)
7168 drop_inode = 1;
39279cc3
CM
7169
7170out_unlock:
d3c2fdcf 7171 nr = trans->blocks_used;
ab78c84d 7172 btrfs_end_transaction_throttle(trans, root);
39279cc3
CM
7173 if (drop_inode) {
7174 inode_dec_link_count(inode);
7175 iput(inode);
7176 }
d3c2fdcf 7177 btrfs_btree_balance_dirty(root, nr);
39279cc3
CM
7178 return err;
7179}
16432985 7180
0af3d00b
JB
7181static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
7182 u64 start, u64 num_bytes, u64 min_size,
7183 loff_t actual_len, u64 *alloc_hint,
7184 struct btrfs_trans_handle *trans)
d899e052 7185{
d899e052
YZ
7186 struct btrfs_root *root = BTRFS_I(inode)->root;
7187 struct btrfs_key ins;
d899e052 7188 u64 cur_offset = start;
55a61d1d 7189 u64 i_size;
d899e052 7190 int ret = 0;
0af3d00b 7191 bool own_trans = true;
d899e052 7192
0af3d00b
JB
7193 if (trans)
7194 own_trans = false;
d899e052 7195 while (num_bytes > 0) {
0af3d00b
JB
7196 if (own_trans) {
7197 trans = btrfs_start_transaction(root, 3);
7198 if (IS_ERR(trans)) {
7199 ret = PTR_ERR(trans);
7200 break;
7201 }
5a303d5d
YZ
7202 }
7203
efa56464
YZ
7204 ret = btrfs_reserve_extent(trans, root, num_bytes, min_size,
7205 0, *alloc_hint, (u64)-1, &ins, 1);
5a303d5d 7206 if (ret) {
0af3d00b
JB
7207 if (own_trans)
7208 btrfs_end_transaction(trans, root);
a22285a6 7209 break;
d899e052 7210 }
5a303d5d 7211
d899e052
YZ
7212 ret = insert_reserved_file_extent(trans, inode,
7213 cur_offset, ins.objectid,
7214 ins.offset, ins.offset,
920bbbfb 7215 ins.offset, 0, 0, 0,
d899e052
YZ
7216 BTRFS_FILE_EXTENT_PREALLOC);
7217 BUG_ON(ret);
a1ed835e
CM
7218 btrfs_drop_extent_cache(inode, cur_offset,
7219 cur_offset + ins.offset -1, 0);
5a303d5d 7220
d899e052
YZ
7221 num_bytes -= ins.offset;
7222 cur_offset += ins.offset;
efa56464 7223 *alloc_hint = ins.objectid + ins.offset;
5a303d5d 7224
d899e052 7225 inode->i_ctime = CURRENT_TIME;
6cbff00f 7226 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
d899e052 7227 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
efa56464
YZ
7228 (actual_len > inode->i_size) &&
7229 (cur_offset > inode->i_size)) {
d1ea6a61 7230 if (cur_offset > actual_len)
55a61d1d 7231 i_size = actual_len;
d1ea6a61 7232 else
55a61d1d
JB
7233 i_size = cur_offset;
7234 i_size_write(inode, i_size);
7235 btrfs_ordered_update_i_size(inode, i_size, NULL);
5a303d5d
YZ
7236 }
7237
d899e052
YZ
7238 ret = btrfs_update_inode(trans, root, inode);
7239 BUG_ON(ret);
d899e052 7240
0af3d00b
JB
7241 if (own_trans)
7242 btrfs_end_transaction(trans, root);
5a303d5d 7243 }
d899e052
YZ
7244 return ret;
7245}
7246
0af3d00b
JB
7247int btrfs_prealloc_file_range(struct inode *inode, int mode,
7248 u64 start, u64 num_bytes, u64 min_size,
7249 loff_t actual_len, u64 *alloc_hint)
7250{
7251 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7252 min_size, actual_len, alloc_hint,
7253 NULL);
7254}
7255
7256int btrfs_prealloc_file_range_trans(struct inode *inode,
7257 struct btrfs_trans_handle *trans, int mode,
7258 u64 start, u64 num_bytes, u64 min_size,
7259 loff_t actual_len, u64 *alloc_hint)
7260{
7261 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7262 min_size, actual_len, alloc_hint, trans);
7263}
7264
e6dcd2dc
CM
7265static int btrfs_set_page_dirty(struct page *page)
7266{
e6dcd2dc
CM
7267 return __set_page_dirty_nobuffers(page);
7268}
7269
b74c79e9 7270static int btrfs_permission(struct inode *inode, int mask, unsigned int flags)
fdebe2bd 7271{
b83cc969
LZ
7272 struct btrfs_root *root = BTRFS_I(inode)->root;
7273
7274 if (btrfs_root_readonly(root) && (mask & MAY_WRITE))
7275 return -EROFS;
6cbff00f 7276 if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE))
fdebe2bd 7277 return -EACCES;
b74c79e9 7278 return generic_permission(inode, mask, flags, btrfs_check_acl);
fdebe2bd 7279}
39279cc3 7280
6e1d5dcc 7281static const struct inode_operations btrfs_dir_inode_operations = {
3394e160 7282 .getattr = btrfs_getattr,
39279cc3
CM
7283 .lookup = btrfs_lookup,
7284 .create = btrfs_create,
7285 .unlink = btrfs_unlink,
7286 .link = btrfs_link,
7287 .mkdir = btrfs_mkdir,
7288 .rmdir = btrfs_rmdir,
7289 .rename = btrfs_rename,
7290 .symlink = btrfs_symlink,
7291 .setattr = btrfs_setattr,
618e21d5 7292 .mknod = btrfs_mknod,
95819c05
CH
7293 .setxattr = btrfs_setxattr,
7294 .getxattr = btrfs_getxattr,
5103e947 7295 .listxattr = btrfs_listxattr,
95819c05 7296 .removexattr = btrfs_removexattr,
fdebe2bd 7297 .permission = btrfs_permission,
39279cc3 7298};
6e1d5dcc 7299static const struct inode_operations btrfs_dir_ro_inode_operations = {
39279cc3 7300 .lookup = btrfs_lookup,
fdebe2bd 7301 .permission = btrfs_permission,
39279cc3 7302};
76dda93c 7303
828c0950 7304static const struct file_operations btrfs_dir_file_operations = {
39279cc3
CM
7305 .llseek = generic_file_llseek,
7306 .read = generic_read_dir,
cbdf5a24 7307 .readdir = btrfs_real_readdir,
34287aa3 7308 .unlocked_ioctl = btrfs_ioctl,
39279cc3 7309#ifdef CONFIG_COMPAT
34287aa3 7310 .compat_ioctl = btrfs_ioctl,
39279cc3 7311#endif
6bf13c0c 7312 .release = btrfs_release_file,
e02119d5 7313 .fsync = btrfs_sync_file,
39279cc3
CM
7314};
7315
d1310b2e 7316static struct extent_io_ops btrfs_extent_io_ops = {
07157aac 7317 .fill_delalloc = run_delalloc_range,
065631f6 7318 .submit_bio_hook = btrfs_submit_bio_hook,
239b14b3 7319 .merge_bio_hook = btrfs_merge_bio_hook,
07157aac 7320 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
e6dcd2dc 7321 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
247e743c 7322 .writepage_start_hook = btrfs_writepage_start_hook,
1259ab75 7323 .readpage_io_failed_hook = btrfs_io_failed_hook,
b0c68f8b
CM
7324 .set_bit_hook = btrfs_set_bit_hook,
7325 .clear_bit_hook = btrfs_clear_bit_hook,
9ed74f2d
JB
7326 .merge_extent_hook = btrfs_merge_extent_hook,
7327 .split_extent_hook = btrfs_split_extent_hook,
07157aac
CM
7328};
7329
35054394
CM
7330/*
7331 * btrfs doesn't support the bmap operation because swapfiles
7332 * use bmap to make a mapping of extents in the file. They assume
7333 * these extents won't change over the life of the file and they
7334 * use the bmap result to do IO directly to the drive.
7335 *
7336 * the btrfs bmap call would return logical addresses that aren't
7337 * suitable for IO and they also will change frequently as COW
7338 * operations happen. So, swapfile + btrfs == corruption.
7339 *
7340 * For now we're avoiding this by dropping bmap.
7341 */
7f09410b 7342static const struct address_space_operations btrfs_aops = {
39279cc3
CM
7343 .readpage = btrfs_readpage,
7344 .writepage = btrfs_writepage,
b293f02e 7345 .writepages = btrfs_writepages,
3ab2fb5a 7346 .readpages = btrfs_readpages,
39279cc3 7347 .sync_page = block_sync_page,
16432985 7348 .direct_IO = btrfs_direct_IO,
a52d9a80
CM
7349 .invalidatepage = btrfs_invalidatepage,
7350 .releasepage = btrfs_releasepage,
e6dcd2dc 7351 .set_page_dirty = btrfs_set_page_dirty,
465fdd97 7352 .error_remove_page = generic_error_remove_page,
39279cc3
CM
7353};
7354
7f09410b 7355static const struct address_space_operations btrfs_symlink_aops = {
39279cc3
CM
7356 .readpage = btrfs_readpage,
7357 .writepage = btrfs_writepage,
2bf5a725
CM
7358 .invalidatepage = btrfs_invalidatepage,
7359 .releasepage = btrfs_releasepage,
39279cc3
CM
7360};
7361
6e1d5dcc 7362static const struct inode_operations btrfs_file_inode_operations = {
39279cc3
CM
7363 .getattr = btrfs_getattr,
7364 .setattr = btrfs_setattr,
95819c05
CH
7365 .setxattr = btrfs_setxattr,
7366 .getxattr = btrfs_getxattr,
5103e947 7367 .listxattr = btrfs_listxattr,
95819c05 7368 .removexattr = btrfs_removexattr,
fdebe2bd 7369 .permission = btrfs_permission,
1506fcc8 7370 .fiemap = btrfs_fiemap,
39279cc3 7371};
6e1d5dcc 7372static const struct inode_operations btrfs_special_inode_operations = {
618e21d5
JB
7373 .getattr = btrfs_getattr,
7374 .setattr = btrfs_setattr,
fdebe2bd 7375 .permission = btrfs_permission,
95819c05
CH
7376 .setxattr = btrfs_setxattr,
7377 .getxattr = btrfs_getxattr,
33268eaf 7378 .listxattr = btrfs_listxattr,
95819c05 7379 .removexattr = btrfs_removexattr,
618e21d5 7380};
6e1d5dcc 7381static const struct inode_operations btrfs_symlink_inode_operations = {
39279cc3
CM
7382 .readlink = generic_readlink,
7383 .follow_link = page_follow_link_light,
7384 .put_link = page_put_link,
f209561a 7385 .getattr = btrfs_getattr,
fdebe2bd 7386 .permission = btrfs_permission,
0279b4cd
JO
7387 .setxattr = btrfs_setxattr,
7388 .getxattr = btrfs_getxattr,
7389 .listxattr = btrfs_listxattr,
7390 .removexattr = btrfs_removexattr,
39279cc3 7391};
76dda93c 7392
82d339d9 7393const struct dentry_operations btrfs_dentry_operations = {
76dda93c
YZ
7394 .d_delete = btrfs_dentry_delete,
7395};