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