Btrfs: fix possible corruption when fsyncing written prealloced extents
[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 }
7c735313
JB
1952 } else {
1953 btrfs_set_inode_last_trans(trans, inode);
1ef30be1
JB
1954 }
1955 ret = 0;
5fd02043
JB
1956out_unlock:
1957 unlock_extent_cached(io_tree, ordered_extent->file_offset,
1958 ordered_extent->file_offset +
1959 ordered_extent->len - 1, &cached_state, GFP_NOFS);
c2167754 1960out:
5b0e95bf 1961 if (root != root->fs_info->tree_root)
0cb59c99 1962 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
5b0e95bf
JB
1963 if (trans) {
1964 if (nolock)
0cb59c99 1965 btrfs_end_transaction_nolock(trans, root);
5b0e95bf 1966 else
0cb59c99
JB
1967 btrfs_end_transaction(trans, root);
1968 }
1969
5fd02043
JB
1970 if (ret)
1971 clear_extent_uptodate(io_tree, ordered_extent->file_offset,
1972 ordered_extent->file_offset +
1973 ordered_extent->len - 1, NULL, GFP_NOFS);
1974
1975 /*
1976 * This needs to be dont to make sure anybody waiting knows we are done
1977 * upating everything for this ordered extent.
1978 */
1979 btrfs_remove_ordered_extent(inode, ordered_extent);
1980
e6dcd2dc
CM
1981 /* once for us */
1982 btrfs_put_ordered_extent(ordered_extent);
1983 /* once for the tree */
1984 btrfs_put_ordered_extent(ordered_extent);
1985
5fd02043
JB
1986 return ret;
1987}
1988
1989static void finish_ordered_fn(struct btrfs_work *work)
1990{
1991 struct btrfs_ordered_extent *ordered_extent;
1992 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
1993 btrfs_finish_ordered_io(ordered_extent);
e6dcd2dc
CM
1994}
1995
b2950863 1996static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
211f90e6
CM
1997 struct extent_state *state, int uptodate)
1998{
5fd02043
JB
1999 struct inode *inode = page->mapping->host;
2000 struct btrfs_root *root = BTRFS_I(inode)->root;
2001 struct btrfs_ordered_extent *ordered_extent = NULL;
2002 struct btrfs_workers *workers;
2003
1abe9b8a 2004 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2005
8b62b72b 2006 ClearPagePrivate2(page);
5fd02043
JB
2007 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2008 end - start + 1, uptodate))
2009 return 0;
2010
2011 ordered_extent->work.func = finish_ordered_fn;
2012 ordered_extent->work.flags = 0;
2013
83eea1f1 2014 if (btrfs_is_free_space_inode(inode))
5fd02043
JB
2015 workers = &root->fs_info->endio_freespace_worker;
2016 else
2017 workers = &root->fs_info->endio_write_workers;
2018 btrfs_queue_worker(workers, &ordered_extent->work);
2019
2020 return 0;
211f90e6
CM
2021}
2022
d352ac68
CM
2023/*
2024 * when reads are done, we need to check csums to verify the data is correct
4a54c8c1
JS
2025 * if there's a match, we allow the bio to finish. If not, the code in
2026 * extent_io.c will try to find good copies for us.
d352ac68 2027 */
b2950863 2028static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
5cf1ab56 2029 struct extent_state *state, int mirror)
07157aac 2030{
35ebb934 2031 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
07157aac 2032 struct inode *inode = page->mapping->host;
d1310b2e 2033 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
07157aac 2034 char *kaddr;
aadfeb6e 2035 u64 private = ~(u32)0;
07157aac 2036 int ret;
ff79f819
CM
2037 struct btrfs_root *root = BTRFS_I(inode)->root;
2038 u32 csum = ~(u32)0;
d1310b2e 2039
d20f7043
CM
2040 if (PageChecked(page)) {
2041 ClearPageChecked(page);
2042 goto good;
2043 }
6cbff00f
CH
2044
2045 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
08d2f347 2046 goto good;
17d217fe
YZ
2047
2048 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
9655d298 2049 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
17d217fe
YZ
2050 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2051 GFP_NOFS);
b6cda9bc 2052 return 0;
17d217fe 2053 }
d20f7043 2054
c2e639f0 2055 if (state && state->start == start) {
70dec807
CM
2056 private = state->private;
2057 ret = 0;
2058 } else {
2059 ret = get_state_private(io_tree, start, &private);
2060 }
7ac687d9 2061 kaddr = kmap_atomic(page);
d397712b 2062 if (ret)
07157aac 2063 goto zeroit;
d397712b 2064
ff79f819
CM
2065 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
2066 btrfs_csum_final(csum, (char *)&csum);
d397712b 2067 if (csum != private)
07157aac 2068 goto zeroit;
d397712b 2069
7ac687d9 2070 kunmap_atomic(kaddr);
d20f7043 2071good:
07157aac
CM
2072 return 0;
2073
2074zeroit:
945d8962 2075 printk_ratelimited(KERN_INFO "btrfs csum failed ino %llu off %llu csum %u "
33345d01
LZ
2076 "private %llu\n",
2077 (unsigned long long)btrfs_ino(page->mapping->host),
193f284d
CM
2078 (unsigned long long)start, csum,
2079 (unsigned long long)private);
db94535d
CM
2080 memset(kaddr + offset, 1, end - start + 1);
2081 flush_dcache_page(page);
7ac687d9 2082 kunmap_atomic(kaddr);
3b951516
CM
2083 if (private == 0)
2084 return 0;
7e38326f 2085 return -EIO;
07157aac 2086}
b888db2b 2087
24bbcf04
YZ
2088struct delayed_iput {
2089 struct list_head list;
2090 struct inode *inode;
2091};
2092
79787eaa
JM
2093/* JDM: If this is fs-wide, why can't we add a pointer to
2094 * btrfs_inode instead and avoid the allocation? */
24bbcf04
YZ
2095void btrfs_add_delayed_iput(struct inode *inode)
2096{
2097 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2098 struct delayed_iput *delayed;
2099
2100 if (atomic_add_unless(&inode->i_count, -1, 1))
2101 return;
2102
2103 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2104 delayed->inode = inode;
2105
2106 spin_lock(&fs_info->delayed_iput_lock);
2107 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2108 spin_unlock(&fs_info->delayed_iput_lock);
2109}
2110
2111void btrfs_run_delayed_iputs(struct btrfs_root *root)
2112{
2113 LIST_HEAD(list);
2114 struct btrfs_fs_info *fs_info = root->fs_info;
2115 struct delayed_iput *delayed;
2116 int empty;
2117
2118 spin_lock(&fs_info->delayed_iput_lock);
2119 empty = list_empty(&fs_info->delayed_iputs);
2120 spin_unlock(&fs_info->delayed_iput_lock);
2121 if (empty)
2122 return;
2123
2124 down_read(&root->fs_info->cleanup_work_sem);
2125 spin_lock(&fs_info->delayed_iput_lock);
2126 list_splice_init(&fs_info->delayed_iputs, &list);
2127 spin_unlock(&fs_info->delayed_iput_lock);
2128
2129 while (!list_empty(&list)) {
2130 delayed = list_entry(list.next, struct delayed_iput, list);
2131 list_del(&delayed->list);
2132 iput(delayed->inode);
2133 kfree(delayed);
2134 }
2135 up_read(&root->fs_info->cleanup_work_sem);
2136}
2137
d68fc57b
YZ
2138enum btrfs_orphan_cleanup_state {
2139 ORPHAN_CLEANUP_STARTED = 1,
2140 ORPHAN_CLEANUP_DONE = 2,
2141};
2142
2143/*
42b2aa86 2144 * This is called in transaction commit time. If there are no orphan
d68fc57b
YZ
2145 * files in the subvolume, it removes orphan item and frees block_rsv
2146 * structure.
2147 */
2148void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2149 struct btrfs_root *root)
2150{
90290e19 2151 struct btrfs_block_rsv *block_rsv;
d68fc57b
YZ
2152 int ret;
2153
8a35d95f 2154 if (atomic_read(&root->orphan_inodes) ||
d68fc57b
YZ
2155 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2156 return;
2157
90290e19 2158 spin_lock(&root->orphan_lock);
8a35d95f 2159 if (atomic_read(&root->orphan_inodes)) {
90290e19
JB
2160 spin_unlock(&root->orphan_lock);
2161 return;
2162 }
2163
2164 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2165 spin_unlock(&root->orphan_lock);
2166 return;
2167 }
2168
2169 block_rsv = root->orphan_block_rsv;
2170 root->orphan_block_rsv = NULL;
2171 spin_unlock(&root->orphan_lock);
2172
d68fc57b
YZ
2173 if (root->orphan_item_inserted &&
2174 btrfs_root_refs(&root->root_item) > 0) {
2175 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2176 root->root_key.objectid);
2177 BUG_ON(ret);
2178 root->orphan_item_inserted = 0;
2179 }
2180
90290e19
JB
2181 if (block_rsv) {
2182 WARN_ON(block_rsv->size > 0);
2183 btrfs_free_block_rsv(root, block_rsv);
d68fc57b
YZ
2184 }
2185}
2186
7b128766
JB
2187/*
2188 * This creates an orphan entry for the given inode in case something goes
2189 * wrong in the middle of an unlink/truncate.
d68fc57b
YZ
2190 *
2191 * NOTE: caller of this function should reserve 5 units of metadata for
2192 * this function.
7b128766
JB
2193 */
2194int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2195{
2196 struct btrfs_root *root = BTRFS_I(inode)->root;
d68fc57b
YZ
2197 struct btrfs_block_rsv *block_rsv = NULL;
2198 int reserve = 0;
2199 int insert = 0;
2200 int ret;
7b128766 2201
d68fc57b
YZ
2202 if (!root->orphan_block_rsv) {
2203 block_rsv = btrfs_alloc_block_rsv(root);
b532402e
TI
2204 if (!block_rsv)
2205 return -ENOMEM;
d68fc57b 2206 }
7b128766 2207
d68fc57b
YZ
2208 spin_lock(&root->orphan_lock);
2209 if (!root->orphan_block_rsv) {
2210 root->orphan_block_rsv = block_rsv;
2211 } else if (block_rsv) {
2212 btrfs_free_block_rsv(root, block_rsv);
2213 block_rsv = NULL;
7b128766 2214 }
7b128766 2215
8a35d95f
JB
2216 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2217 &BTRFS_I(inode)->runtime_flags)) {
d68fc57b
YZ
2218#if 0
2219 /*
2220 * For proper ENOSPC handling, we should do orphan
2221 * cleanup when mounting. But this introduces backward
2222 * compatibility issue.
2223 */
2224 if (!xchg(&root->orphan_item_inserted, 1))
2225 insert = 2;
2226 else
2227 insert = 1;
2228#endif
2229 insert = 1;
8a35d95f 2230 atomic_dec(&root->orphan_inodes);
7b128766
JB
2231 }
2232
72ac3c0d
JB
2233 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2234 &BTRFS_I(inode)->runtime_flags))
d68fc57b 2235 reserve = 1;
d68fc57b 2236 spin_unlock(&root->orphan_lock);
7b128766 2237
d68fc57b
YZ
2238 /* grab metadata reservation from transaction handle */
2239 if (reserve) {
2240 ret = btrfs_orphan_reserve_metadata(trans, inode);
79787eaa 2241 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
d68fc57b 2242 }
7b128766 2243
d68fc57b
YZ
2244 /* insert an orphan item to track this unlinked/truncated file */
2245 if (insert >= 1) {
33345d01 2246 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
79787eaa 2247 if (ret && ret != -EEXIST) {
8a35d95f
JB
2248 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2249 &BTRFS_I(inode)->runtime_flags);
79787eaa
JM
2250 btrfs_abort_transaction(trans, root, ret);
2251 return ret;
2252 }
2253 ret = 0;
d68fc57b
YZ
2254 }
2255
2256 /* insert an orphan item to track subvolume contains orphan files */
2257 if (insert >= 2) {
2258 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2259 root->root_key.objectid);
79787eaa
JM
2260 if (ret && ret != -EEXIST) {
2261 btrfs_abort_transaction(trans, root, ret);
2262 return ret;
2263 }
d68fc57b
YZ
2264 }
2265 return 0;
7b128766
JB
2266}
2267
2268/*
2269 * We have done the truncate/delete so we can go ahead and remove the orphan
2270 * item for this particular inode.
2271 */
2272int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2273{
2274 struct btrfs_root *root = BTRFS_I(inode)->root;
d68fc57b
YZ
2275 int delete_item = 0;
2276 int release_rsv = 0;
7b128766
JB
2277 int ret = 0;
2278
d68fc57b 2279 spin_lock(&root->orphan_lock);
8a35d95f
JB
2280 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2281 &BTRFS_I(inode)->runtime_flags))
d68fc57b 2282 delete_item = 1;
7b128766 2283
72ac3c0d
JB
2284 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2285 &BTRFS_I(inode)->runtime_flags))
d68fc57b 2286 release_rsv = 1;
d68fc57b 2287 spin_unlock(&root->orphan_lock);
7b128766 2288
d68fc57b 2289 if (trans && delete_item) {
33345d01 2290 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
79787eaa 2291 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
d68fc57b 2292 }
7b128766 2293
8a35d95f 2294 if (release_rsv) {
d68fc57b 2295 btrfs_orphan_release_metadata(inode);
8a35d95f
JB
2296 atomic_dec(&root->orphan_inodes);
2297 }
7b128766 2298
d68fc57b 2299 return 0;
7b128766
JB
2300}
2301
2302/*
2303 * this cleans up any orphans that may be left on the list from the last use
2304 * of this root.
2305 */
66b4ffd1 2306int btrfs_orphan_cleanup(struct btrfs_root *root)
7b128766
JB
2307{
2308 struct btrfs_path *path;
2309 struct extent_buffer *leaf;
7b128766
JB
2310 struct btrfs_key key, found_key;
2311 struct btrfs_trans_handle *trans;
2312 struct inode *inode;
8f6d7f4f 2313 u64 last_objectid = 0;
7b128766
JB
2314 int ret = 0, nr_unlink = 0, nr_truncate = 0;
2315
d68fc57b 2316 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
66b4ffd1 2317 return 0;
c71bf099
YZ
2318
2319 path = btrfs_alloc_path();
66b4ffd1
JB
2320 if (!path) {
2321 ret = -ENOMEM;
2322 goto out;
2323 }
7b128766
JB
2324 path->reada = -1;
2325
2326 key.objectid = BTRFS_ORPHAN_OBJECTID;
2327 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
2328 key.offset = (u64)-1;
2329
7b128766
JB
2330 while (1) {
2331 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
66b4ffd1
JB
2332 if (ret < 0)
2333 goto out;
7b128766
JB
2334
2335 /*
2336 * if ret == 0 means we found what we were searching for, which
25985edc 2337 * is weird, but possible, so only screw with path if we didn't
7b128766
JB
2338 * find the key and see if we have stuff that matches
2339 */
2340 if (ret > 0) {
66b4ffd1 2341 ret = 0;
7b128766
JB
2342 if (path->slots[0] == 0)
2343 break;
2344 path->slots[0]--;
2345 }
2346
2347 /* pull out the item */
2348 leaf = path->nodes[0];
7b128766
JB
2349 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2350
2351 /* make sure the item matches what we want */
2352 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2353 break;
2354 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
2355 break;
2356
2357 /* release the path since we're done with it */
b3b4aa74 2358 btrfs_release_path(path);
7b128766
JB
2359
2360 /*
2361 * this is where we are basically btrfs_lookup, without the
2362 * crossing root thing. we store the inode number in the
2363 * offset of the orphan item.
2364 */
8f6d7f4f
JB
2365
2366 if (found_key.offset == last_objectid) {
2367 printk(KERN_ERR "btrfs: Error removing orphan entry, "
2368 "stopping orphan cleanup\n");
2369 ret = -EINVAL;
2370 goto out;
2371 }
2372
2373 last_objectid = found_key.offset;
2374
5d4f98a2
YZ
2375 found_key.objectid = found_key.offset;
2376 found_key.type = BTRFS_INODE_ITEM_KEY;
2377 found_key.offset = 0;
73f73415 2378 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
a8c9e576
JB
2379 ret = PTR_RET(inode);
2380 if (ret && ret != -ESTALE)
66b4ffd1 2381 goto out;
7b128766 2382
f8e9e0b0
AJ
2383 if (ret == -ESTALE && root == root->fs_info->tree_root) {
2384 struct btrfs_root *dead_root;
2385 struct btrfs_fs_info *fs_info = root->fs_info;
2386 int is_dead_root = 0;
2387
2388 /*
2389 * this is an orphan in the tree root. Currently these
2390 * could come from 2 sources:
2391 * a) a snapshot deletion in progress
2392 * b) a free space cache inode
2393 * We need to distinguish those two, as the snapshot
2394 * orphan must not get deleted.
2395 * find_dead_roots already ran before us, so if this
2396 * is a snapshot deletion, we should find the root
2397 * in the dead_roots list
2398 */
2399 spin_lock(&fs_info->trans_lock);
2400 list_for_each_entry(dead_root, &fs_info->dead_roots,
2401 root_list) {
2402 if (dead_root->root_key.objectid ==
2403 found_key.objectid) {
2404 is_dead_root = 1;
2405 break;
2406 }
2407 }
2408 spin_unlock(&fs_info->trans_lock);
2409 if (is_dead_root) {
2410 /* prevent this orphan from being found again */
2411 key.offset = found_key.objectid - 1;
2412 continue;
2413 }
2414 }
7b128766 2415 /*
a8c9e576
JB
2416 * Inode is already gone but the orphan item is still there,
2417 * kill the orphan item.
7b128766 2418 */
a8c9e576
JB
2419 if (ret == -ESTALE) {
2420 trans = btrfs_start_transaction(root, 1);
66b4ffd1
JB
2421 if (IS_ERR(trans)) {
2422 ret = PTR_ERR(trans);
2423 goto out;
2424 }
8a35d95f
JB
2425 printk(KERN_ERR "auto deleting %Lu\n",
2426 found_key.objectid);
a8c9e576
JB
2427 ret = btrfs_del_orphan_item(trans, root,
2428 found_key.objectid);
79787eaa 2429 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
5b21f2ed 2430 btrfs_end_transaction(trans, root);
7b128766
JB
2431 continue;
2432 }
2433
a8c9e576
JB
2434 /*
2435 * add this inode to the orphan list so btrfs_orphan_del does
2436 * the proper thing when we hit it
2437 */
8a35d95f
JB
2438 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2439 &BTRFS_I(inode)->runtime_flags);
a8c9e576 2440
7b128766
JB
2441 /* if we have links, this was a truncate, lets do that */
2442 if (inode->i_nlink) {
a41ad394
JB
2443 if (!S_ISREG(inode->i_mode)) {
2444 WARN_ON(1);
2445 iput(inode);
2446 continue;
2447 }
7b128766 2448 nr_truncate++;
66b4ffd1 2449 ret = btrfs_truncate(inode);
7b128766
JB
2450 } else {
2451 nr_unlink++;
2452 }
2453
2454 /* this will do delete_inode and everything for us */
2455 iput(inode);
66b4ffd1
JB
2456 if (ret)
2457 goto out;
7b128766 2458 }
3254c876
MX
2459 /* release the path since we're done with it */
2460 btrfs_release_path(path);
2461
d68fc57b
YZ
2462 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
2463
2464 if (root->orphan_block_rsv)
2465 btrfs_block_rsv_release(root, root->orphan_block_rsv,
2466 (u64)-1);
2467
2468 if (root->orphan_block_rsv || root->orphan_item_inserted) {
7a7eaa40 2469 trans = btrfs_join_transaction(root);
66b4ffd1
JB
2470 if (!IS_ERR(trans))
2471 btrfs_end_transaction(trans, root);
d68fc57b 2472 }
7b128766
JB
2473
2474 if (nr_unlink)
2475 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2476 if (nr_truncate)
2477 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
66b4ffd1
JB
2478
2479out:
2480 if (ret)
2481 printk(KERN_CRIT "btrfs: could not do orphan cleanup %d\n", ret);
2482 btrfs_free_path(path);
2483 return ret;
7b128766
JB
2484}
2485
46a53cca
CM
2486/*
2487 * very simple check to peek ahead in the leaf looking for xattrs. If we
2488 * don't find any xattrs, we know there can't be any acls.
2489 *
2490 * slot is the slot the inode is in, objectid is the objectid of the inode
2491 */
2492static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2493 int slot, u64 objectid)
2494{
2495 u32 nritems = btrfs_header_nritems(leaf);
2496 struct btrfs_key found_key;
2497 int scanned = 0;
2498
2499 slot++;
2500 while (slot < nritems) {
2501 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2502
2503 /* we found a different objectid, there must not be acls */
2504 if (found_key.objectid != objectid)
2505 return 0;
2506
2507 /* we found an xattr, assume we've got an acl */
2508 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
2509 return 1;
2510
2511 /*
2512 * we found a key greater than an xattr key, there can't
2513 * be any acls later on
2514 */
2515 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
2516 return 0;
2517
2518 slot++;
2519 scanned++;
2520
2521 /*
2522 * it goes inode, inode backrefs, xattrs, extents,
2523 * so if there are a ton of hard links to an inode there can
2524 * be a lot of backrefs. Don't waste time searching too hard,
2525 * this is just an optimization
2526 */
2527 if (scanned >= 8)
2528 break;
2529 }
2530 /* we hit the end of the leaf before we found an xattr or
2531 * something larger than an xattr. We have to assume the inode
2532 * has acls
2533 */
2534 return 1;
2535}
2536
d352ac68
CM
2537/*
2538 * read an inode from the btree into the in-memory inode
2539 */
5d4f98a2 2540static void btrfs_read_locked_inode(struct inode *inode)
39279cc3
CM
2541{
2542 struct btrfs_path *path;
5f39d397 2543 struct extent_buffer *leaf;
39279cc3 2544 struct btrfs_inode_item *inode_item;
0b86a832 2545 struct btrfs_timespec *tspec;
39279cc3
CM
2546 struct btrfs_root *root = BTRFS_I(inode)->root;
2547 struct btrfs_key location;
46a53cca 2548 int maybe_acls;
618e21d5 2549 u32 rdev;
39279cc3 2550 int ret;
2f7e33d4
MX
2551 bool filled = false;
2552
2553 ret = btrfs_fill_inode(inode, &rdev);
2554 if (!ret)
2555 filled = true;
39279cc3
CM
2556
2557 path = btrfs_alloc_path();
1748f843
MF
2558 if (!path)
2559 goto make_bad;
2560
d90c7321 2561 path->leave_spinning = 1;
39279cc3 2562 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
dc17ff8f 2563
39279cc3 2564 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
5f39d397 2565 if (ret)
39279cc3 2566 goto make_bad;
39279cc3 2567
5f39d397 2568 leaf = path->nodes[0];
2f7e33d4
MX
2569
2570 if (filled)
2571 goto cache_acl;
2572
5f39d397
CM
2573 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2574 struct btrfs_inode_item);
5f39d397 2575 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
bfe86848 2576 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
5f39d397
CM
2577 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
2578 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
dbe674a9 2579 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
5f39d397
CM
2580
2581 tspec = btrfs_inode_atime(inode_item);
2582 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2583 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2584
2585 tspec = btrfs_inode_mtime(inode_item);
2586 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2587 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2588
2589 tspec = btrfs_inode_ctime(inode_item);
2590 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2591 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2592
a76a3cd4 2593 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
e02119d5 2594 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
0c4d2d95 2595 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
e02119d5 2596 inode->i_generation = BTRFS_I(inode)->generation;
618e21d5 2597 inode->i_rdev = 0;
5f39d397
CM
2598 rdev = btrfs_inode_rdev(leaf, inode_item);
2599
aec7477b 2600 BTRFS_I(inode)->index_cnt = (u64)-1;
d2fb3437 2601 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
2f7e33d4 2602cache_acl:
46a53cca
CM
2603 /*
2604 * try to precache a NULL acl entry for files that don't have
2605 * any xattrs or acls
2606 */
33345d01
LZ
2607 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
2608 btrfs_ino(inode));
72c04902
AV
2609 if (!maybe_acls)
2610 cache_no_acl(inode);
46a53cca 2611
39279cc3 2612 btrfs_free_path(path);
39279cc3 2613
39279cc3 2614 switch (inode->i_mode & S_IFMT) {
39279cc3
CM
2615 case S_IFREG:
2616 inode->i_mapping->a_ops = &btrfs_aops;
04160088 2617 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
d1310b2e 2618 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
39279cc3
CM
2619 inode->i_fop = &btrfs_file_operations;
2620 inode->i_op = &btrfs_file_inode_operations;
2621 break;
2622 case S_IFDIR:
2623 inode->i_fop = &btrfs_dir_file_operations;
2624 if (root == root->fs_info->tree_root)
2625 inode->i_op = &btrfs_dir_ro_inode_operations;
2626 else
2627 inode->i_op = &btrfs_dir_inode_operations;
2628 break;
2629 case S_IFLNK:
2630 inode->i_op = &btrfs_symlink_inode_operations;
2631 inode->i_mapping->a_ops = &btrfs_symlink_aops;
04160088 2632 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
39279cc3 2633 break;
618e21d5 2634 default:
0279b4cd 2635 inode->i_op = &btrfs_special_inode_operations;
618e21d5
JB
2636 init_special_inode(inode, inode->i_mode, rdev);
2637 break;
39279cc3 2638 }
6cbff00f
CH
2639
2640 btrfs_update_iflags(inode);
39279cc3
CM
2641 return;
2642
2643make_bad:
39279cc3 2644 btrfs_free_path(path);
39279cc3
CM
2645 make_bad_inode(inode);
2646}
2647
d352ac68
CM
2648/*
2649 * given a leaf and an inode, copy the inode fields into the leaf
2650 */
e02119d5
CM
2651static void fill_inode_item(struct btrfs_trans_handle *trans,
2652 struct extent_buffer *leaf,
5f39d397 2653 struct btrfs_inode_item *item,
39279cc3
CM
2654 struct inode *inode)
2655{
5f39d397
CM
2656 btrfs_set_inode_uid(leaf, item, inode->i_uid);
2657 btrfs_set_inode_gid(leaf, item, inode->i_gid);
dbe674a9 2658 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
5f39d397
CM
2659 btrfs_set_inode_mode(leaf, item, inode->i_mode);
2660 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2661
2662 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2663 inode->i_atime.tv_sec);
2664 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2665 inode->i_atime.tv_nsec);
2666
2667 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2668 inode->i_mtime.tv_sec);
2669 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2670 inode->i_mtime.tv_nsec);
2671
2672 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2673 inode->i_ctime.tv_sec);
2674 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2675 inode->i_ctime.tv_nsec);
2676
a76a3cd4 2677 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
e02119d5 2678 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
0c4d2d95 2679 btrfs_set_inode_sequence(leaf, item, inode->i_version);
e02119d5 2680 btrfs_set_inode_transid(leaf, item, trans->transid);
5f39d397 2681 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
b98b6767 2682 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
d82a6f1d 2683 btrfs_set_inode_block_group(leaf, item, 0);
39279cc3
CM
2684}
2685
d352ac68
CM
2686/*
2687 * copy everything in the in-memory inode into the btree.
2688 */
2115133f 2689static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
d397712b 2690 struct btrfs_root *root, struct inode *inode)
39279cc3
CM
2691{
2692 struct btrfs_inode_item *inode_item;
2693 struct btrfs_path *path;
5f39d397 2694 struct extent_buffer *leaf;
39279cc3
CM
2695 int ret;
2696
2697 path = btrfs_alloc_path();
16cdcec7
MX
2698 if (!path)
2699 return -ENOMEM;
2700
b9473439 2701 path->leave_spinning = 1;
16cdcec7
MX
2702 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
2703 1);
39279cc3
CM
2704 if (ret) {
2705 if (ret > 0)
2706 ret = -ENOENT;
2707 goto failed;
2708 }
2709
b4ce94de 2710 btrfs_unlock_up_safe(path, 1);
5f39d397
CM
2711 leaf = path->nodes[0];
2712 inode_item = btrfs_item_ptr(leaf, path->slots[0],
16cdcec7 2713 struct btrfs_inode_item);
39279cc3 2714
e02119d5 2715 fill_inode_item(trans, leaf, inode_item, inode);
5f39d397 2716 btrfs_mark_buffer_dirty(leaf);
15ee9bc7 2717 btrfs_set_inode_last_trans(trans, inode);
39279cc3
CM
2718 ret = 0;
2719failed:
39279cc3
CM
2720 btrfs_free_path(path);
2721 return ret;
2722}
2723
2115133f
CM
2724/*
2725 * copy everything in the in-memory inode into the btree.
2726 */
2727noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2728 struct btrfs_root *root, struct inode *inode)
2729{
2730 int ret;
2731
2732 /*
2733 * If the inode is a free space inode, we can deadlock during commit
2734 * if we put it into the delayed code.
2735 *
2736 * The data relocation inode should also be directly updated
2737 * without delay
2738 */
83eea1f1 2739 if (!btrfs_is_free_space_inode(inode)
2115133f 2740 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
8ea05e3a
AB
2741 btrfs_update_root_times(trans, root);
2742
2115133f
CM
2743 ret = btrfs_delayed_update_inode(trans, root, inode);
2744 if (!ret)
2745 btrfs_set_inode_last_trans(trans, inode);
2746 return ret;
2747 }
2748
2749 return btrfs_update_inode_item(trans, root, inode);
2750}
2751
2752static noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
2753 struct btrfs_root *root, struct inode *inode)
2754{
2755 int ret;
2756
2757 ret = btrfs_update_inode(trans, root, inode);
2758 if (ret == -ENOSPC)
2759 return btrfs_update_inode_item(trans, root, inode);
2760 return ret;
2761}
2762
d352ac68
CM
2763/*
2764 * unlink helper that gets used here in inode.c and in the tree logging
2765 * recovery code. It remove a link in a directory with a given name, and
2766 * also drops the back refs in the inode to the directory
2767 */
92986796
AV
2768static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2769 struct btrfs_root *root,
2770 struct inode *dir, struct inode *inode,
2771 const char *name, int name_len)
39279cc3
CM
2772{
2773 struct btrfs_path *path;
39279cc3 2774 int ret = 0;
5f39d397 2775 struct extent_buffer *leaf;
39279cc3 2776 struct btrfs_dir_item *di;
5f39d397 2777 struct btrfs_key key;
aec7477b 2778 u64 index;
33345d01
LZ
2779 u64 ino = btrfs_ino(inode);
2780 u64 dir_ino = btrfs_ino(dir);
39279cc3
CM
2781
2782 path = btrfs_alloc_path();
54aa1f4d
CM
2783 if (!path) {
2784 ret = -ENOMEM;
554233a6 2785 goto out;
54aa1f4d
CM
2786 }
2787
b9473439 2788 path->leave_spinning = 1;
33345d01 2789 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
39279cc3
CM
2790 name, name_len, -1);
2791 if (IS_ERR(di)) {
2792 ret = PTR_ERR(di);
2793 goto err;
2794 }
2795 if (!di) {
2796 ret = -ENOENT;
2797 goto err;
2798 }
5f39d397
CM
2799 leaf = path->nodes[0];
2800 btrfs_dir_item_key_to_cpu(leaf, di, &key);
39279cc3 2801 ret = btrfs_delete_one_dir_name(trans, root, path, di);
54aa1f4d
CM
2802 if (ret)
2803 goto err;
b3b4aa74 2804 btrfs_release_path(path);
39279cc3 2805
33345d01
LZ
2806 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
2807 dir_ino, &index);
aec7477b 2808 if (ret) {
d397712b 2809 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
33345d01
LZ
2810 "inode %llu parent %llu\n", name_len, name,
2811 (unsigned long long)ino, (unsigned long long)dir_ino);
79787eaa 2812 btrfs_abort_transaction(trans, root, ret);
aec7477b
JB
2813 goto err;
2814 }
2815
16cdcec7 2816 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
79787eaa
JM
2817 if (ret) {
2818 btrfs_abort_transaction(trans, root, ret);
39279cc3 2819 goto err;
79787eaa 2820 }
39279cc3 2821
e02119d5 2822 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
33345d01 2823 inode, dir_ino);
79787eaa
JM
2824 if (ret != 0 && ret != -ENOENT) {
2825 btrfs_abort_transaction(trans, root, ret);
2826 goto err;
2827 }
e02119d5
CM
2828
2829 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2830 dir, index);
6418c961
CM
2831 if (ret == -ENOENT)
2832 ret = 0;
39279cc3
CM
2833err:
2834 btrfs_free_path(path);
e02119d5
CM
2835 if (ret)
2836 goto out;
2837
2838 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
0c4d2d95
JB
2839 inode_inc_iversion(inode);
2840 inode_inc_iversion(dir);
e02119d5 2841 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
b9959295 2842 ret = btrfs_update_inode(trans, root, dir);
e02119d5 2843out:
39279cc3
CM
2844 return ret;
2845}
2846
92986796
AV
2847int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2848 struct btrfs_root *root,
2849 struct inode *dir, struct inode *inode,
2850 const char *name, int name_len)
2851{
2852 int ret;
2853 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
2854 if (!ret) {
2855 btrfs_drop_nlink(inode);
2856 ret = btrfs_update_inode(trans, root, inode);
2857 }
2858 return ret;
2859}
2860
2861
a22285a6
YZ
2862/* helper to check if there is any shared block in the path */
2863static int check_path_shared(struct btrfs_root *root,
2864 struct btrfs_path *path)
39279cc3 2865{
a22285a6
YZ
2866 struct extent_buffer *eb;
2867 int level;
0e4dcbef 2868 u64 refs = 1;
5df6a9f6 2869
a22285a6 2870 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
dedefd72
JB
2871 int ret;
2872
a22285a6
YZ
2873 if (!path->nodes[level])
2874 break;
2875 eb = path->nodes[level];
2876 if (!btrfs_block_can_be_shared(root, eb))
2877 continue;
2878 ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
2879 &refs, NULL);
2880 if (refs > 1)
2881 return 1;
5df6a9f6 2882 }
dedefd72 2883 return 0;
39279cc3
CM
2884}
2885
a22285a6
YZ
2886/*
2887 * helper to start transaction for unlink and rmdir.
2888 *
2889 * unlink and rmdir are special in btrfs, they do not always free space.
2890 * so in enospc case, we should make sure they will free space before
2891 * allowing them to use the global metadata reservation.
2892 */
2893static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2894 struct dentry *dentry)
4df27c4d 2895{
39279cc3 2896 struct btrfs_trans_handle *trans;
a22285a6 2897 struct btrfs_root *root = BTRFS_I(dir)->root;
4df27c4d 2898 struct btrfs_path *path;
a22285a6 2899 struct btrfs_inode_ref *ref;
4df27c4d 2900 struct btrfs_dir_item *di;
7b128766 2901 struct inode *inode = dentry->d_inode;
4df27c4d 2902 u64 index;
a22285a6
YZ
2903 int check_link = 1;
2904 int err = -ENOSPC;
4df27c4d 2905 int ret;
33345d01
LZ
2906 u64 ino = btrfs_ino(inode);
2907 u64 dir_ino = btrfs_ino(dir);
4df27c4d 2908
e70bea5f
JB
2909 /*
2910 * 1 for the possible orphan item
2911 * 1 for the dir item
2912 * 1 for the dir index
2913 * 1 for the inode ref
2914 * 1 for the inode ref in the tree log
2915 * 2 for the dir entries in the log
2916 * 1 for the inode
2917 */
2918 trans = btrfs_start_transaction(root, 8);
a22285a6
YZ
2919 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
2920 return trans;
4df27c4d 2921
33345d01 2922 if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
a22285a6 2923 return ERR_PTR(-ENOSPC);
4df27c4d 2924
a22285a6
YZ
2925 /* check if there is someone else holds reference */
2926 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
2927 return ERR_PTR(-ENOSPC);
4df27c4d 2928
a22285a6
YZ
2929 if (atomic_read(&inode->i_count) > 2)
2930 return ERR_PTR(-ENOSPC);
4df27c4d 2931
a22285a6
YZ
2932 if (xchg(&root->fs_info->enospc_unlink, 1))
2933 return ERR_PTR(-ENOSPC);
2934
2935 path = btrfs_alloc_path();
2936 if (!path) {
2937 root->fs_info->enospc_unlink = 0;
2938 return ERR_PTR(-ENOMEM);
4df27c4d
YZ
2939 }
2940
3880a1b4
JB
2941 /* 1 for the orphan item */
2942 trans = btrfs_start_transaction(root, 1);
5df6a9f6 2943 if (IS_ERR(trans)) {
a22285a6
YZ
2944 btrfs_free_path(path);
2945 root->fs_info->enospc_unlink = 0;
2946 return trans;
2947 }
4df27c4d 2948
a22285a6
YZ
2949 path->skip_locking = 1;
2950 path->search_commit_root = 1;
4df27c4d 2951
a22285a6
YZ
2952 ret = btrfs_lookup_inode(trans, root, path,
2953 &BTRFS_I(dir)->location, 0);
2954 if (ret < 0) {
2955 err = ret;
2956 goto out;
2957 }
2958 if (ret == 0) {
2959 if (check_path_shared(root, path))
2960 goto out;
2961 } else {
2962 check_link = 0;
5df6a9f6 2963 }
b3b4aa74 2964 btrfs_release_path(path);
a22285a6
YZ
2965
2966 ret = btrfs_lookup_inode(trans, root, path,
2967 &BTRFS_I(inode)->location, 0);
2968 if (ret < 0) {
2969 err = ret;
2970 goto out;
2971 }
2972 if (ret == 0) {
2973 if (check_path_shared(root, path))
2974 goto out;
2975 } else {
2976 check_link = 0;
2977 }
b3b4aa74 2978 btrfs_release_path(path);
a22285a6
YZ
2979
2980 if (ret == 0 && S_ISREG(inode->i_mode)) {
2981 ret = btrfs_lookup_file_extent(trans, root, path,
33345d01 2982 ino, (u64)-1, 0);
a22285a6
YZ
2983 if (ret < 0) {
2984 err = ret;
2985 goto out;
2986 }
79787eaa 2987 BUG_ON(ret == 0); /* Corruption */
a22285a6
YZ
2988 if (check_path_shared(root, path))
2989 goto out;
b3b4aa74 2990 btrfs_release_path(path);
a22285a6
YZ
2991 }
2992
2993 if (!check_link) {
2994 err = 0;
2995 goto out;
2996 }
2997
33345d01 2998 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
a22285a6
YZ
2999 dentry->d_name.name, dentry->d_name.len, 0);
3000 if (IS_ERR(di)) {
3001 err = PTR_ERR(di);
3002 goto out;
3003 }
3004 if (di) {
3005 if (check_path_shared(root, path))
3006 goto out;
3007 } else {
3008 err = 0;
3009 goto out;
3010 }
b3b4aa74 3011 btrfs_release_path(path);
a22285a6
YZ
3012
3013 ref = btrfs_lookup_inode_ref(trans, root, path,
3014 dentry->d_name.name, dentry->d_name.len,
33345d01 3015 ino, dir_ino, 0);
a22285a6
YZ
3016 if (IS_ERR(ref)) {
3017 err = PTR_ERR(ref);
3018 goto out;
3019 }
79787eaa 3020 BUG_ON(!ref); /* Logic error */
a22285a6
YZ
3021 if (check_path_shared(root, path))
3022 goto out;
3023 index = btrfs_inode_ref_index(path->nodes[0], ref);
b3b4aa74 3024 btrfs_release_path(path);
a22285a6 3025
16cdcec7
MX
3026 /*
3027 * This is a commit root search, if we can lookup inode item and other
3028 * relative items in the commit root, it means the transaction of
3029 * dir/file creation has been committed, and the dir index item that we
3030 * delay to insert has also been inserted into the commit root. So
3031 * we needn't worry about the delayed insertion of the dir index item
3032 * here.
3033 */
33345d01 3034 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index,
a22285a6
YZ
3035 dentry->d_name.name, dentry->d_name.len, 0);
3036 if (IS_ERR(di)) {
3037 err = PTR_ERR(di);
3038 goto out;
3039 }
3040 BUG_ON(ret == -ENOENT);
3041 if (check_path_shared(root, path))
3042 goto out;
3043
3044 err = 0;
3045out:
3046 btrfs_free_path(path);
3880a1b4
JB
3047 /* Migrate the orphan reservation over */
3048 if (!err)
3049 err = btrfs_block_rsv_migrate(trans->block_rsv,
3050 &root->fs_info->global_block_rsv,
5a77d76c 3051 trans->bytes_reserved);
3880a1b4 3052
a22285a6
YZ
3053 if (err) {
3054 btrfs_end_transaction(trans, root);
3055 root->fs_info->enospc_unlink = 0;
3056 return ERR_PTR(err);
3057 }
3058
3059 trans->block_rsv = &root->fs_info->global_block_rsv;
3060 return trans;
3061}
3062
3063static void __unlink_end_trans(struct btrfs_trans_handle *trans,
3064 struct btrfs_root *root)
3065{
3066 if (trans->block_rsv == &root->fs_info->global_block_rsv) {
5a77d76c
JB
3067 btrfs_block_rsv_release(root, trans->block_rsv,
3068 trans->bytes_reserved);
3069 trans->block_rsv = &root->fs_info->trans_block_rsv;
a22285a6
YZ
3070 BUG_ON(!root->fs_info->enospc_unlink);
3071 root->fs_info->enospc_unlink = 0;
3072 }
7ad85bb7 3073 btrfs_end_transaction(trans, root);
a22285a6
YZ
3074}
3075
3076static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3077{
3078 struct btrfs_root *root = BTRFS_I(dir)->root;
3079 struct btrfs_trans_handle *trans;
3080 struct inode *inode = dentry->d_inode;
3081 int ret;
3082 unsigned long nr = 0;
3083
3084 trans = __unlink_start_trans(dir, dentry);
3085 if (IS_ERR(trans))
3086 return PTR_ERR(trans);
5f39d397 3087
12fcfd22
CM
3088 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3089
e02119d5
CM
3090 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3091 dentry->d_name.name, dentry->d_name.len);
b532402e
TI
3092 if (ret)
3093 goto out;
7b128766 3094
a22285a6 3095 if (inode->i_nlink == 0) {
7b128766 3096 ret = btrfs_orphan_add(trans, inode);
b532402e
TI
3097 if (ret)
3098 goto out;
a22285a6 3099 }
7b128766 3100
b532402e 3101out:
d3c2fdcf 3102 nr = trans->blocks_used;
a22285a6 3103 __unlink_end_trans(trans, root);
d3c2fdcf 3104 btrfs_btree_balance_dirty(root, nr);
39279cc3
CM
3105 return ret;
3106}
3107
4df27c4d
YZ
3108int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3109 struct btrfs_root *root,
3110 struct inode *dir, u64 objectid,
3111 const char *name, int name_len)
3112{
3113 struct btrfs_path *path;
3114 struct extent_buffer *leaf;
3115 struct btrfs_dir_item *di;
3116 struct btrfs_key key;
3117 u64 index;
3118 int ret;
33345d01 3119 u64 dir_ino = btrfs_ino(dir);
4df27c4d
YZ
3120
3121 path = btrfs_alloc_path();
3122 if (!path)
3123 return -ENOMEM;
3124
33345d01 3125 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
4df27c4d 3126 name, name_len, -1);
79787eaa
JM
3127 if (IS_ERR_OR_NULL(di)) {
3128 if (!di)
3129 ret = -ENOENT;
3130 else
3131 ret = PTR_ERR(di);
3132 goto out;
3133 }
4df27c4d
YZ
3134
3135 leaf = path->nodes[0];
3136 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3137 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3138 ret = btrfs_delete_one_dir_name(trans, root, path, di);
79787eaa
JM
3139 if (ret) {
3140 btrfs_abort_transaction(trans, root, ret);
3141 goto out;
3142 }
b3b4aa74 3143 btrfs_release_path(path);
4df27c4d
YZ
3144
3145 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3146 objectid, root->root_key.objectid,
33345d01 3147 dir_ino, &index, name, name_len);
4df27c4d 3148 if (ret < 0) {
79787eaa
JM
3149 if (ret != -ENOENT) {
3150 btrfs_abort_transaction(trans, root, ret);
3151 goto out;
3152 }
33345d01 3153 di = btrfs_search_dir_index_item(root, path, dir_ino,
4df27c4d 3154 name, name_len);
79787eaa
JM
3155 if (IS_ERR_OR_NULL(di)) {
3156 if (!di)
3157 ret = -ENOENT;
3158 else
3159 ret = PTR_ERR(di);
3160 btrfs_abort_transaction(trans, root, ret);
3161 goto out;
3162 }
4df27c4d
YZ
3163
3164 leaf = path->nodes[0];
3165 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
b3b4aa74 3166 btrfs_release_path(path);
4df27c4d
YZ
3167 index = key.offset;
3168 }
945d8962 3169 btrfs_release_path(path);
4df27c4d 3170
16cdcec7 3171 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
79787eaa
JM
3172 if (ret) {
3173 btrfs_abort_transaction(trans, root, ret);
3174 goto out;
3175 }
4df27c4d
YZ
3176
3177 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
0c4d2d95 3178 inode_inc_iversion(dir);
4df27c4d 3179 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
5a24e84c 3180 ret = btrfs_update_inode_fallback(trans, root, dir);
79787eaa
JM
3181 if (ret)
3182 btrfs_abort_transaction(trans, root, ret);
3183out:
71d7aed0 3184 btrfs_free_path(path);
79787eaa 3185 return ret;
4df27c4d
YZ
3186}
3187
39279cc3
CM
3188static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3189{
3190 struct inode *inode = dentry->d_inode;
1832a6d5 3191 int err = 0;
39279cc3 3192 struct btrfs_root *root = BTRFS_I(dir)->root;
39279cc3 3193 struct btrfs_trans_handle *trans;
1832a6d5 3194 unsigned long nr = 0;
39279cc3 3195
3394e160 3196 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
33345d01 3197 btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
134d4512
Y
3198 return -ENOTEMPTY;
3199
a22285a6
YZ
3200 trans = __unlink_start_trans(dir, dentry);
3201 if (IS_ERR(trans))
5df6a9f6 3202 return PTR_ERR(trans);
5df6a9f6 3203
33345d01 3204 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
4df27c4d
YZ
3205 err = btrfs_unlink_subvol(trans, root, dir,
3206 BTRFS_I(inode)->location.objectid,
3207 dentry->d_name.name,
3208 dentry->d_name.len);
3209 goto out;
3210 }
3211
7b128766
JB
3212 err = btrfs_orphan_add(trans, inode);
3213 if (err)
4df27c4d 3214 goto out;
7b128766 3215
39279cc3 3216 /* now the directory is empty */
e02119d5
CM
3217 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3218 dentry->d_name.name, dentry->d_name.len);
d397712b 3219 if (!err)
dbe674a9 3220 btrfs_i_size_write(inode, 0);
4df27c4d 3221out:
d3c2fdcf 3222 nr = trans->blocks_used;
a22285a6 3223 __unlink_end_trans(trans, root);
d3c2fdcf 3224 btrfs_btree_balance_dirty(root, nr);
3954401f 3225
39279cc3
CM
3226 return err;
3227}
3228
39279cc3
CM
3229/*
3230 * this can truncate away extent items, csum items and directory items.
3231 * It starts at a high offset and removes keys until it can't find
d352ac68 3232 * any higher than new_size
39279cc3
CM
3233 *
3234 * csum items that cross the new i_size are truncated to the new size
3235 * as well.
7b128766
JB
3236 *
3237 * min_type is the minimum key type to truncate down to. If set to 0, this
3238 * will kill all the items on this inode, including the INODE_ITEM_KEY.
39279cc3 3239 */
8082510e
YZ
3240int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3241 struct btrfs_root *root,
3242 struct inode *inode,
3243 u64 new_size, u32 min_type)
39279cc3 3244{
39279cc3 3245 struct btrfs_path *path;
5f39d397 3246 struct extent_buffer *leaf;
39279cc3 3247 struct btrfs_file_extent_item *fi;
8082510e
YZ
3248 struct btrfs_key key;
3249 struct btrfs_key found_key;
39279cc3 3250 u64 extent_start = 0;
db94535d 3251 u64 extent_num_bytes = 0;
5d4f98a2 3252 u64 extent_offset = 0;
39279cc3 3253 u64 item_end = 0;
8082510e
YZ
3254 u64 mask = root->sectorsize - 1;
3255 u32 found_type = (u8)-1;
39279cc3
CM
3256 int found_extent;
3257 int del_item;
85e21bac
CM
3258 int pending_del_nr = 0;
3259 int pending_del_slot = 0;
179e29e4 3260 int extent_type = -1;
8082510e
YZ
3261 int ret;
3262 int err = 0;
33345d01 3263 u64 ino = btrfs_ino(inode);
8082510e
YZ
3264
3265 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
39279cc3 3266
0eb0e19c
MF
3267 path = btrfs_alloc_path();
3268 if (!path)
3269 return -ENOMEM;
3270 path->reada = -1;
3271
0af3d00b 3272 if (root->ref_cows || root == root->fs_info->tree_root)
5b21f2ed 3273 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
8082510e 3274
16cdcec7
MX
3275 /*
3276 * This function is also used to drop the items in the log tree before
3277 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3278 * it is used to drop the loged items. So we shouldn't kill the delayed
3279 * items.
3280 */
3281 if (min_type == 0 && root == BTRFS_I(inode)->root)
3282 btrfs_kill_delayed_inode_items(inode);
3283
33345d01 3284 key.objectid = ino;
39279cc3 3285 key.offset = (u64)-1;
5f39d397
CM
3286 key.type = (u8)-1;
3287
85e21bac 3288search_again:
b9473439 3289 path->leave_spinning = 1;
85e21bac 3290 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
8082510e
YZ
3291 if (ret < 0) {
3292 err = ret;
3293 goto out;
3294 }
d397712b 3295
85e21bac 3296 if (ret > 0) {
e02119d5
CM
3297 /* there are no items in the tree for us to truncate, we're
3298 * done
3299 */
8082510e
YZ
3300 if (path->slots[0] == 0)
3301 goto out;
85e21bac
CM
3302 path->slots[0]--;
3303 }
3304
d397712b 3305 while (1) {
39279cc3 3306 fi = NULL;
5f39d397
CM
3307 leaf = path->nodes[0];
3308 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3309 found_type = btrfs_key_type(&found_key);
39279cc3 3310
33345d01 3311 if (found_key.objectid != ino)
39279cc3 3312 break;
5f39d397 3313
85e21bac 3314 if (found_type < min_type)
39279cc3
CM
3315 break;
3316
5f39d397 3317 item_end = found_key.offset;
39279cc3 3318 if (found_type == BTRFS_EXTENT_DATA_KEY) {
5f39d397 3319 fi = btrfs_item_ptr(leaf, path->slots[0],
39279cc3 3320 struct btrfs_file_extent_item);
179e29e4
CM
3321 extent_type = btrfs_file_extent_type(leaf, fi);
3322 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
5f39d397 3323 item_end +=
db94535d 3324 btrfs_file_extent_num_bytes(leaf, fi);
179e29e4 3325 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
179e29e4 3326 item_end += btrfs_file_extent_inline_len(leaf,
c8b97818 3327 fi);
39279cc3 3328 }
008630c1 3329 item_end--;
39279cc3 3330 }
8082510e
YZ
3331 if (found_type > min_type) {
3332 del_item = 1;
3333 } else {
3334 if (item_end < new_size)
b888db2b 3335 break;
8082510e
YZ
3336 if (found_key.offset >= new_size)
3337 del_item = 1;
3338 else
3339 del_item = 0;
39279cc3 3340 }
39279cc3 3341 found_extent = 0;
39279cc3 3342 /* FIXME, shrink the extent if the ref count is only 1 */
179e29e4
CM
3343 if (found_type != BTRFS_EXTENT_DATA_KEY)
3344 goto delete;
3345
3346 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
39279cc3 3347 u64 num_dec;
db94535d 3348 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
f70a9a6b 3349 if (!del_item) {
db94535d
CM
3350 u64 orig_num_bytes =
3351 btrfs_file_extent_num_bytes(leaf, fi);
e02119d5 3352 extent_num_bytes = new_size -
5f39d397 3353 found_key.offset + root->sectorsize - 1;
b1632b10
Y
3354 extent_num_bytes = extent_num_bytes &
3355 ~((u64)root->sectorsize - 1);
db94535d
CM
3356 btrfs_set_file_extent_num_bytes(leaf, fi,
3357 extent_num_bytes);
3358 num_dec = (orig_num_bytes -
9069218d 3359 extent_num_bytes);
e02119d5 3360 if (root->ref_cows && extent_start != 0)
a76a3cd4 3361 inode_sub_bytes(inode, num_dec);
5f39d397 3362 btrfs_mark_buffer_dirty(leaf);
39279cc3 3363 } else {
db94535d
CM
3364 extent_num_bytes =
3365 btrfs_file_extent_disk_num_bytes(leaf,
3366 fi);
5d4f98a2
YZ
3367 extent_offset = found_key.offset -
3368 btrfs_file_extent_offset(leaf, fi);
3369
39279cc3 3370 /* FIXME blocksize != 4096 */
9069218d 3371 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
39279cc3
CM
3372 if (extent_start != 0) {
3373 found_extent = 1;
e02119d5 3374 if (root->ref_cows)
a76a3cd4 3375 inode_sub_bytes(inode, num_dec);
e02119d5 3376 }
39279cc3 3377 }
9069218d 3378 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
c8b97818
CM
3379 /*
3380 * we can't truncate inline items that have had
3381 * special encodings
3382 */
3383 if (!del_item &&
3384 btrfs_file_extent_compression(leaf, fi) == 0 &&
3385 btrfs_file_extent_encryption(leaf, fi) == 0 &&
3386 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
e02119d5
CM
3387 u32 size = new_size - found_key.offset;
3388
3389 if (root->ref_cows) {
a76a3cd4
YZ
3390 inode_sub_bytes(inode, item_end + 1 -
3391 new_size);
e02119d5
CM
3392 }
3393 size =
3394 btrfs_file_extent_calc_inline_size(size);
143bede5
JM
3395 btrfs_truncate_item(trans, root, path,
3396 size, 1);
e02119d5 3397 } else if (root->ref_cows) {
a76a3cd4
YZ
3398 inode_sub_bytes(inode, item_end + 1 -
3399 found_key.offset);
9069218d 3400 }
39279cc3 3401 }
179e29e4 3402delete:
39279cc3 3403 if (del_item) {
85e21bac
CM
3404 if (!pending_del_nr) {
3405 /* no pending yet, add ourselves */
3406 pending_del_slot = path->slots[0];
3407 pending_del_nr = 1;
3408 } else if (pending_del_nr &&
3409 path->slots[0] + 1 == pending_del_slot) {
3410 /* hop on the pending chunk */
3411 pending_del_nr++;
3412 pending_del_slot = path->slots[0];
3413 } else {
d397712b 3414 BUG();
85e21bac 3415 }
39279cc3
CM
3416 } else {
3417 break;
3418 }
0af3d00b
JB
3419 if (found_extent && (root->ref_cows ||
3420 root == root->fs_info->tree_root)) {
b9473439 3421 btrfs_set_path_blocking(path);
39279cc3 3422 ret = btrfs_free_extent(trans, root, extent_start,
5d4f98a2
YZ
3423 extent_num_bytes, 0,
3424 btrfs_header_owner(leaf),
66d7e7f0 3425 ino, extent_offset, 0);
39279cc3
CM
3426 BUG_ON(ret);
3427 }
85e21bac 3428
8082510e
YZ
3429 if (found_type == BTRFS_INODE_ITEM_KEY)
3430 break;
3431
3432 if (path->slots[0] == 0 ||
3433 path->slots[0] != pending_del_slot) {
82d5902d
LZ
3434 if (root->ref_cows &&
3435 BTRFS_I(inode)->location.objectid !=
3436 BTRFS_FREE_INO_OBJECTID) {
8082510e
YZ
3437 err = -EAGAIN;
3438 goto out;
3439 }
3440 if (pending_del_nr) {
3441 ret = btrfs_del_items(trans, root, path,
3442 pending_del_slot,
3443 pending_del_nr);
79787eaa
JM
3444 if (ret) {
3445 btrfs_abort_transaction(trans,
3446 root, ret);
3447 goto error;
3448 }
8082510e
YZ
3449 pending_del_nr = 0;
3450 }
b3b4aa74 3451 btrfs_release_path(path);
85e21bac 3452 goto search_again;
8082510e
YZ
3453 } else {
3454 path->slots[0]--;
85e21bac 3455 }
39279cc3 3456 }
8082510e 3457out:
85e21bac
CM
3458 if (pending_del_nr) {
3459 ret = btrfs_del_items(trans, root, path, pending_del_slot,
3460 pending_del_nr);
79787eaa
JM
3461 if (ret)
3462 btrfs_abort_transaction(trans, root, ret);
85e21bac 3463 }
79787eaa 3464error:
39279cc3 3465 btrfs_free_path(path);
8082510e 3466 return err;
39279cc3
CM
3467}
3468
3469/*
3470 * taken from block_truncate_page, but does cow as it zeros out
3471 * any bytes left in the last page in the file.
3472 */
3473static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
3474{
3475 struct inode *inode = mapping->host;
db94535d 3476 struct btrfs_root *root = BTRFS_I(inode)->root;
e6dcd2dc
CM
3477 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3478 struct btrfs_ordered_extent *ordered;
2ac55d41 3479 struct extent_state *cached_state = NULL;
e6dcd2dc 3480 char *kaddr;
db94535d 3481 u32 blocksize = root->sectorsize;
39279cc3
CM
3482 pgoff_t index = from >> PAGE_CACHE_SHIFT;
3483 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3484 struct page *page;
3b16a4e3 3485 gfp_t mask = btrfs_alloc_write_mask(mapping);
39279cc3 3486 int ret = 0;
a52d9a80 3487 u64 page_start;
e6dcd2dc 3488 u64 page_end;
39279cc3
CM
3489
3490 if ((offset & (blocksize - 1)) == 0)
3491 goto out;
0ca1f7ce 3492 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
5d5e103a
JB
3493 if (ret)
3494 goto out;
39279cc3
CM
3495
3496 ret = -ENOMEM;
211c17f5 3497again:
3b16a4e3 3498 page = find_or_create_page(mapping, index, mask);
5d5e103a 3499 if (!page) {
0ca1f7ce 3500 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
39279cc3 3501 goto out;
5d5e103a 3502 }
e6dcd2dc
CM
3503
3504 page_start = page_offset(page);
3505 page_end = page_start + PAGE_CACHE_SIZE - 1;
3506
39279cc3 3507 if (!PageUptodate(page)) {
9ebefb18 3508 ret = btrfs_readpage(NULL, page);
39279cc3 3509 lock_page(page);
211c17f5
CM
3510 if (page->mapping != mapping) {
3511 unlock_page(page);
3512 page_cache_release(page);
3513 goto again;
3514 }
39279cc3
CM
3515 if (!PageUptodate(page)) {
3516 ret = -EIO;
89642229 3517 goto out_unlock;
39279cc3
CM
3518 }
3519 }
211c17f5 3520 wait_on_page_writeback(page);
e6dcd2dc 3521
d0082371 3522 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
e6dcd2dc
CM
3523 set_page_extent_mapped(page);
3524
3525 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3526 if (ordered) {
2ac55d41
JB
3527 unlock_extent_cached(io_tree, page_start, page_end,
3528 &cached_state, GFP_NOFS);
e6dcd2dc
CM
3529 unlock_page(page);
3530 page_cache_release(page);
eb84ae03 3531 btrfs_start_ordered_extent(inode, ordered, 1);
e6dcd2dc
CM
3532 btrfs_put_ordered_extent(ordered);
3533 goto again;
3534 }
3535
2ac55d41 3536 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
5d5e103a 3537 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
2ac55d41 3538 0, 0, &cached_state, GFP_NOFS);
5d5e103a 3539
2ac55d41
JB
3540 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
3541 &cached_state);
9ed74f2d 3542 if (ret) {
2ac55d41
JB
3543 unlock_extent_cached(io_tree, page_start, page_end,
3544 &cached_state, GFP_NOFS);
9ed74f2d
JB
3545 goto out_unlock;
3546 }
3547
e6dcd2dc
CM
3548 ret = 0;
3549 if (offset != PAGE_CACHE_SIZE) {
3550 kaddr = kmap(page);
3551 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
3552 flush_dcache_page(page);
3553 kunmap(page);
3554 }
247e743c 3555 ClearPageChecked(page);
e6dcd2dc 3556 set_page_dirty(page);
2ac55d41
JB
3557 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
3558 GFP_NOFS);
39279cc3 3559
89642229 3560out_unlock:
5d5e103a 3561 if (ret)
0ca1f7ce 3562 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
39279cc3
CM
3563 unlock_page(page);
3564 page_cache_release(page);
3565out:
3566 return ret;
3567}
3568
695a0d0d
JB
3569/*
3570 * This function puts in dummy file extents for the area we're creating a hole
3571 * for. So if we are truncating this file to a larger size we need to insert
3572 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
3573 * the range between oldsize and size
3574 */
a41ad394 3575int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
39279cc3 3576{
9036c102
YZ
3577 struct btrfs_trans_handle *trans;
3578 struct btrfs_root *root = BTRFS_I(inode)->root;
3579 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
a22285a6 3580 struct extent_map *em = NULL;
2ac55d41 3581 struct extent_state *cached_state = NULL;
9036c102 3582 u64 mask = root->sectorsize - 1;
a41ad394 3583 u64 hole_start = (oldsize + mask) & ~mask;
9036c102
YZ
3584 u64 block_end = (size + mask) & ~mask;
3585 u64 last_byte;
3586 u64 cur_offset;
3587 u64 hole_size;
9ed74f2d 3588 int err = 0;
39279cc3 3589
9036c102
YZ
3590 if (size <= hole_start)
3591 return 0;
3592
9036c102
YZ
3593 while (1) {
3594 struct btrfs_ordered_extent *ordered;
3595 btrfs_wait_ordered_range(inode, hole_start,
3596 block_end - hole_start);
2ac55d41 3597 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
d0082371 3598 &cached_state);
9036c102
YZ
3599 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
3600 if (!ordered)
3601 break;
2ac55d41
JB
3602 unlock_extent_cached(io_tree, hole_start, block_end - 1,
3603 &cached_state, GFP_NOFS);
9036c102
YZ
3604 btrfs_put_ordered_extent(ordered);
3605 }
39279cc3 3606
9036c102
YZ
3607 cur_offset = hole_start;
3608 while (1) {
3609 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3610 block_end - cur_offset, 0);
79787eaa
JM
3611 if (IS_ERR(em)) {
3612 err = PTR_ERR(em);
3613 break;
3614 }
9036c102
YZ
3615 last_byte = min(extent_map_end(em), block_end);
3616 last_byte = (last_byte + mask) & ~mask;
8082510e 3617 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
771ed689 3618 u64 hint_byte = 0;
9036c102 3619 hole_size = last_byte - cur_offset;
9ed74f2d 3620
3642320e 3621 trans = btrfs_start_transaction(root, 3);
a22285a6
YZ
3622 if (IS_ERR(trans)) {
3623 err = PTR_ERR(trans);
9ed74f2d 3624 break;
a22285a6 3625 }
8082510e
YZ
3626
3627 err = btrfs_drop_extents(trans, inode, cur_offset,
3628 cur_offset + hole_size,
3629 &hint_byte, 1);
5b397377 3630 if (err) {
79787eaa 3631 btrfs_abort_transaction(trans, root, err);
5b397377 3632 btrfs_end_transaction(trans, root);
3893e33b 3633 break;
5b397377 3634 }
8082510e 3635
9036c102 3636 err = btrfs_insert_file_extent(trans, root,
33345d01 3637 btrfs_ino(inode), cur_offset, 0,
9036c102
YZ
3638 0, hole_size, 0, hole_size,
3639 0, 0, 0);
5b397377 3640 if (err) {
79787eaa 3641 btrfs_abort_transaction(trans, root, err);
5b397377 3642 btrfs_end_transaction(trans, root);
3893e33b 3643 break;
5b397377 3644 }
8082510e 3645
9036c102
YZ
3646 btrfs_drop_extent_cache(inode, hole_start,
3647 last_byte - 1, 0);
8082510e 3648
3642320e 3649 btrfs_update_inode(trans, root, inode);
8082510e 3650 btrfs_end_transaction(trans, root);
9036c102
YZ
3651 }
3652 free_extent_map(em);
a22285a6 3653 em = NULL;
9036c102 3654 cur_offset = last_byte;
8082510e 3655 if (cur_offset >= block_end)
9036c102
YZ
3656 break;
3657 }
1832a6d5 3658
a22285a6 3659 free_extent_map(em);
2ac55d41
JB
3660 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
3661 GFP_NOFS);
9036c102
YZ
3662 return err;
3663}
39279cc3 3664
a41ad394 3665static int btrfs_setsize(struct inode *inode, loff_t newsize)
8082510e 3666{
f4a2f4c5
MX
3667 struct btrfs_root *root = BTRFS_I(inode)->root;
3668 struct btrfs_trans_handle *trans;
a41ad394 3669 loff_t oldsize = i_size_read(inode);
8082510e
YZ
3670 int ret;
3671
a41ad394 3672 if (newsize == oldsize)
8082510e
YZ
3673 return 0;
3674
a41ad394 3675 if (newsize > oldsize) {
a41ad394
JB
3676 truncate_pagecache(inode, oldsize, newsize);
3677 ret = btrfs_cont_expand(inode, oldsize, newsize);
f4a2f4c5 3678 if (ret)
8082510e 3679 return ret;
8082510e 3680
f4a2f4c5
MX
3681 trans = btrfs_start_transaction(root, 1);
3682 if (IS_ERR(trans))
3683 return PTR_ERR(trans);
3684
3685 i_size_write(inode, newsize);
3686 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
3687 ret = btrfs_update_inode(trans, root, inode);
7ad85bb7 3688 btrfs_end_transaction(trans, root);
a41ad394 3689 } else {
8082510e 3690
a41ad394
JB
3691 /*
3692 * We're truncating a file that used to have good data down to
3693 * zero. Make sure it gets into the ordered flush list so that
3694 * any new writes get down to disk quickly.
3695 */
3696 if (newsize == 0)
72ac3c0d
JB
3697 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
3698 &BTRFS_I(inode)->runtime_flags);
8082510e 3699
a41ad394
JB
3700 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3701 truncate_setsize(inode, newsize);
3702 ret = btrfs_truncate(inode);
8082510e
YZ
3703 }
3704
a41ad394 3705 return ret;
8082510e
YZ
3706}
3707
9036c102
YZ
3708static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3709{
3710 struct inode *inode = dentry->d_inode;
b83cc969 3711 struct btrfs_root *root = BTRFS_I(inode)->root;
9036c102 3712 int err;
39279cc3 3713
b83cc969
LZ
3714 if (btrfs_root_readonly(root))
3715 return -EROFS;
3716
9036c102
YZ
3717 err = inode_change_ok(inode, attr);
3718 if (err)
3719 return err;
2bf5a725 3720
5a3f23d5 3721 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
a41ad394 3722 err = btrfs_setsize(inode, attr->ia_size);
8082510e
YZ
3723 if (err)
3724 return err;
39279cc3 3725 }
9036c102 3726
1025774c
CH
3727 if (attr->ia_valid) {
3728 setattr_copy(inode, attr);
0c4d2d95 3729 inode_inc_iversion(inode);
22c44fe6 3730 err = btrfs_dirty_inode(inode);
1025774c 3731
22c44fe6 3732 if (!err && attr->ia_valid & ATTR_MODE)
1025774c
CH
3733 err = btrfs_acl_chmod(inode);
3734 }
33268eaf 3735
39279cc3
CM
3736 return err;
3737}
61295eb8 3738
bd555975 3739void btrfs_evict_inode(struct inode *inode)
39279cc3
CM
3740{
3741 struct btrfs_trans_handle *trans;
3742 struct btrfs_root *root = BTRFS_I(inode)->root;
726c35fa 3743 struct btrfs_block_rsv *rsv, *global_rsv;
07127184 3744 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
d3c2fdcf 3745 unsigned long nr;
39279cc3
CM
3746 int ret;
3747
1abe9b8a 3748 trace_btrfs_inode_evict(inode);
3749
39279cc3 3750 truncate_inode_pages(&inode->i_data, 0);
0af3d00b 3751 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
83eea1f1 3752 btrfs_is_free_space_inode(inode)))
bd555975
AV
3753 goto no_delete;
3754
39279cc3 3755 if (is_bad_inode(inode)) {
7b128766 3756 btrfs_orphan_del(NULL, inode);
39279cc3
CM
3757 goto no_delete;
3758 }
bd555975 3759 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
4a096752 3760 btrfs_wait_ordered_range(inode, 0, (u64)-1);
5f39d397 3761
c71bf099 3762 if (root->fs_info->log_root_recovering) {
6bf02314 3763 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
8a35d95f 3764 &BTRFS_I(inode)->runtime_flags));
c71bf099
YZ
3765 goto no_delete;
3766 }
3767
76dda93c
YZ
3768 if (inode->i_nlink > 0) {
3769 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3770 goto no_delete;
3771 }
3772
4289a667
JB
3773 rsv = btrfs_alloc_block_rsv(root);
3774 if (!rsv) {
3775 btrfs_orphan_del(NULL, inode);
3776 goto no_delete;
3777 }
4a338542 3778 rsv->size = min_size;
726c35fa 3779 global_rsv = &root->fs_info->global_block_rsv;
4289a667 3780
dbe674a9 3781 btrfs_i_size_write(inode, 0);
5f39d397 3782
4289a667
JB
3783 /*
3784 * This is a bit simpler than btrfs_truncate since
3785 *
3786 * 1) We've already reserved our space for our orphan item in the
3787 * unlink.
3788 * 2) We're going to delete the inode item, so we don't need to update
3789 * it at all.
3790 *
3791 * So we just need to reserve some slack space in case we add bytes when
3792 * doing the truncate.
3793 */
8082510e 3794 while (1) {
aa38a711 3795 ret = btrfs_block_rsv_refill_noflush(root, rsv, min_size);
726c35fa
JB
3796
3797 /*
3798 * Try and steal from the global reserve since we will
3799 * likely not use this space anyway, we want to try as
3800 * hard as possible to get this to work.
3801 */
3802 if (ret)
3803 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
d68fc57b 3804
d68fc57b 3805 if (ret) {
4289a667 3806 printk(KERN_WARNING "Could not get space for a "
482e6dc5 3807 "delete, will truncate on mount %d\n", ret);
4289a667
JB
3808 btrfs_orphan_del(NULL, inode);
3809 btrfs_free_block_rsv(root, rsv);
3810 goto no_delete;
d68fc57b 3811 }
7b128766 3812
4289a667
JB
3813 trans = btrfs_start_transaction(root, 0);
3814 if (IS_ERR(trans)) {
3815 btrfs_orphan_del(NULL, inode);
3816 btrfs_free_block_rsv(root, rsv);
3817 goto no_delete;
d68fc57b 3818 }
7b128766 3819
4289a667
JB
3820 trans->block_rsv = rsv;
3821
d68fc57b 3822 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
8082510e
YZ
3823 if (ret != -EAGAIN)
3824 break;
85e21bac 3825
8082510e
YZ
3826 nr = trans->blocks_used;
3827 btrfs_end_transaction(trans, root);
3828 trans = NULL;
3829 btrfs_btree_balance_dirty(root, nr);
3830 }
5f39d397 3831
4289a667
JB
3832 btrfs_free_block_rsv(root, rsv);
3833
8082510e 3834 if (ret == 0) {
4289a667 3835 trans->block_rsv = root->orphan_block_rsv;
8082510e
YZ
3836 ret = btrfs_orphan_del(trans, inode);
3837 BUG_ON(ret);
3838 }
54aa1f4d 3839
4289a667 3840 trans->block_rsv = &root->fs_info->trans_block_rsv;
581bb050
LZ
3841 if (!(root == root->fs_info->tree_root ||
3842 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
33345d01 3843 btrfs_return_ino(root, btrfs_ino(inode));
581bb050 3844
d3c2fdcf 3845 nr = trans->blocks_used;
54aa1f4d 3846 btrfs_end_transaction(trans, root);
d3c2fdcf 3847 btrfs_btree_balance_dirty(root, nr);
39279cc3 3848no_delete:
dbd5768f 3849 clear_inode(inode);
8082510e 3850 return;
39279cc3
CM
3851}
3852
3853/*
3854 * this returns the key found in the dir entry in the location pointer.
3855 * If no dir entries were found, location->objectid is 0.
3856 */
3857static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
3858 struct btrfs_key *location)
3859{
3860 const char *name = dentry->d_name.name;
3861 int namelen = dentry->d_name.len;
3862 struct btrfs_dir_item *di;
3863 struct btrfs_path *path;
3864 struct btrfs_root *root = BTRFS_I(dir)->root;
0d9f7f3e 3865 int ret = 0;
39279cc3
CM
3866
3867 path = btrfs_alloc_path();
d8926bb3
MF
3868 if (!path)
3869 return -ENOMEM;
3954401f 3870
33345d01 3871 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
39279cc3 3872 namelen, 0);
0d9f7f3e
Y
3873 if (IS_ERR(di))
3874 ret = PTR_ERR(di);
d397712b 3875
c704005d 3876 if (IS_ERR_OR_NULL(di))
3954401f 3877 goto out_err;
d397712b 3878
5f39d397 3879 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
39279cc3 3880out:
39279cc3
CM
3881 btrfs_free_path(path);
3882 return ret;
3954401f
CM
3883out_err:
3884 location->objectid = 0;
3885 goto out;
39279cc3
CM
3886}
3887
3888/*
3889 * when we hit a tree root in a directory, the btrfs part of the inode
3890 * needs to be changed to reflect the root directory of the tree root. This
3891 * is kind of like crossing a mount point.
3892 */
3893static int fixup_tree_root_location(struct btrfs_root *root,
4df27c4d
YZ
3894 struct inode *dir,
3895 struct dentry *dentry,
3896 struct btrfs_key *location,
3897 struct btrfs_root **sub_root)
39279cc3 3898{
4df27c4d
YZ
3899 struct btrfs_path *path;
3900 struct btrfs_root *new_root;
3901 struct btrfs_root_ref *ref;
3902 struct extent_buffer *leaf;
3903 int ret;
3904 int err = 0;
39279cc3 3905
4df27c4d
YZ
3906 path = btrfs_alloc_path();
3907 if (!path) {
3908 err = -ENOMEM;
3909 goto out;
3910 }
39279cc3 3911
4df27c4d
YZ
3912 err = -ENOENT;
3913 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
3914 BTRFS_I(dir)->root->root_key.objectid,
3915 location->objectid);
3916 if (ret) {
3917 if (ret < 0)
3918 err = ret;
3919 goto out;
3920 }
39279cc3 3921
4df27c4d
YZ
3922 leaf = path->nodes[0];
3923 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
33345d01 3924 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
4df27c4d
YZ
3925 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
3926 goto out;
39279cc3 3927
4df27c4d
YZ
3928 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
3929 (unsigned long)(ref + 1),
3930 dentry->d_name.len);
3931 if (ret)
3932 goto out;
3933
b3b4aa74 3934 btrfs_release_path(path);
4df27c4d
YZ
3935
3936 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
3937 if (IS_ERR(new_root)) {
3938 err = PTR_ERR(new_root);
3939 goto out;
3940 }
3941
3942 if (btrfs_root_refs(&new_root->root_item) == 0) {
3943 err = -ENOENT;
3944 goto out;
3945 }
3946
3947 *sub_root = new_root;
3948 location->objectid = btrfs_root_dirid(&new_root->root_item);
3949 location->type = BTRFS_INODE_ITEM_KEY;
3950 location->offset = 0;
3951 err = 0;
3952out:
3953 btrfs_free_path(path);
3954 return err;
39279cc3
CM
3955}
3956
5d4f98a2
YZ
3957static void inode_tree_add(struct inode *inode)
3958{
3959 struct btrfs_root *root = BTRFS_I(inode)->root;
3960 struct btrfs_inode *entry;
03e860bd
FNP
3961 struct rb_node **p;
3962 struct rb_node *parent;
33345d01 3963 u64 ino = btrfs_ino(inode);
03e860bd
FNP
3964again:
3965 p = &root->inode_tree.rb_node;
3966 parent = NULL;
5d4f98a2 3967
1d3382cb 3968 if (inode_unhashed(inode))
76dda93c
YZ
3969 return;
3970
5d4f98a2
YZ
3971 spin_lock(&root->inode_lock);
3972 while (*p) {
3973 parent = *p;
3974 entry = rb_entry(parent, struct btrfs_inode, rb_node);
3975
33345d01 3976 if (ino < btrfs_ino(&entry->vfs_inode))
03e860bd 3977 p = &parent->rb_left;
33345d01 3978 else if (ino > btrfs_ino(&entry->vfs_inode))
03e860bd 3979 p = &parent->rb_right;
5d4f98a2
YZ
3980 else {
3981 WARN_ON(!(entry->vfs_inode.i_state &
a4ffdde6 3982 (I_WILL_FREE | I_FREEING)));
03e860bd
FNP
3983 rb_erase(parent, &root->inode_tree);
3984 RB_CLEAR_NODE(parent);
3985 spin_unlock(&root->inode_lock);
3986 goto again;
5d4f98a2
YZ
3987 }
3988 }
3989 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
3990 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
3991 spin_unlock(&root->inode_lock);
3992}
3993
3994static void inode_tree_del(struct inode *inode)
3995{
3996 struct btrfs_root *root = BTRFS_I(inode)->root;
76dda93c 3997 int empty = 0;
5d4f98a2 3998
03e860bd 3999 spin_lock(&root->inode_lock);
5d4f98a2 4000 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
5d4f98a2 4001 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
5d4f98a2 4002 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
76dda93c 4003 empty = RB_EMPTY_ROOT(&root->inode_tree);
5d4f98a2 4004 }
03e860bd 4005 spin_unlock(&root->inode_lock);
76dda93c 4006
0af3d00b
JB
4007 /*
4008 * Free space cache has inodes in the tree root, but the tree root has a
4009 * root_refs of 0, so this could end up dropping the tree root as a
4010 * snapshot, so we need the extra !root->fs_info->tree_root check to
4011 * make sure we don't drop it.
4012 */
4013 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
4014 root != root->fs_info->tree_root) {
76dda93c
YZ
4015 synchronize_srcu(&root->fs_info->subvol_srcu);
4016 spin_lock(&root->inode_lock);
4017 empty = RB_EMPTY_ROOT(&root->inode_tree);
4018 spin_unlock(&root->inode_lock);
4019 if (empty)
4020 btrfs_add_dead_root(root);
4021 }
4022}
4023
143bede5 4024void btrfs_invalidate_inodes(struct btrfs_root *root)
76dda93c
YZ
4025{
4026 struct rb_node *node;
4027 struct rb_node *prev;
4028 struct btrfs_inode *entry;
4029 struct inode *inode;
4030 u64 objectid = 0;
4031
4032 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4033
4034 spin_lock(&root->inode_lock);
4035again:
4036 node = root->inode_tree.rb_node;
4037 prev = NULL;
4038 while (node) {
4039 prev = node;
4040 entry = rb_entry(node, struct btrfs_inode, rb_node);
4041
33345d01 4042 if (objectid < btrfs_ino(&entry->vfs_inode))
76dda93c 4043 node = node->rb_left;
33345d01 4044 else if (objectid > btrfs_ino(&entry->vfs_inode))
76dda93c
YZ
4045 node = node->rb_right;
4046 else
4047 break;
4048 }
4049 if (!node) {
4050 while (prev) {
4051 entry = rb_entry(prev, struct btrfs_inode, rb_node);
33345d01 4052 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
76dda93c
YZ
4053 node = prev;
4054 break;
4055 }
4056 prev = rb_next(prev);
4057 }
4058 }
4059 while (node) {
4060 entry = rb_entry(node, struct btrfs_inode, rb_node);
33345d01 4061 objectid = btrfs_ino(&entry->vfs_inode) + 1;
76dda93c
YZ
4062 inode = igrab(&entry->vfs_inode);
4063 if (inode) {
4064 spin_unlock(&root->inode_lock);
4065 if (atomic_read(&inode->i_count) > 1)
4066 d_prune_aliases(inode);
4067 /*
45321ac5 4068 * btrfs_drop_inode will have it removed from
76dda93c
YZ
4069 * the inode cache when its usage count
4070 * hits zero.
4071 */
4072 iput(inode);
4073 cond_resched();
4074 spin_lock(&root->inode_lock);
4075 goto again;
4076 }
4077
4078 if (cond_resched_lock(&root->inode_lock))
4079 goto again;
4080
4081 node = rb_next(node);
4082 }
4083 spin_unlock(&root->inode_lock);
5d4f98a2
YZ
4084}
4085
e02119d5
CM
4086static int btrfs_init_locked_inode(struct inode *inode, void *p)
4087{
4088 struct btrfs_iget_args *args = p;
4089 inode->i_ino = args->ino;
e02119d5 4090 BTRFS_I(inode)->root = args->root;
39279cc3
CM
4091 return 0;
4092}
4093
4094static int btrfs_find_actor(struct inode *inode, void *opaque)
4095{
4096 struct btrfs_iget_args *args = opaque;
33345d01 4097 return args->ino == btrfs_ino(inode) &&
d397712b 4098 args->root == BTRFS_I(inode)->root;
39279cc3
CM
4099}
4100
5d4f98a2
YZ
4101static struct inode *btrfs_iget_locked(struct super_block *s,
4102 u64 objectid,
4103 struct btrfs_root *root)
39279cc3
CM
4104{
4105 struct inode *inode;
4106 struct btrfs_iget_args args;
4107 args.ino = objectid;
4108 args.root = root;
4109
4110 inode = iget5_locked(s, objectid, btrfs_find_actor,
4111 btrfs_init_locked_inode,
4112 (void *)&args);
4113 return inode;
4114}
4115
1a54ef8c
BR
4116/* Get an inode object given its location and corresponding root.
4117 * Returns in *is_new if the inode was read from disk
4118 */
4119struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
73f73415 4120 struct btrfs_root *root, int *new)
1a54ef8c
BR
4121{
4122 struct inode *inode;
4123
4124 inode = btrfs_iget_locked(s, location->objectid, root);
4125 if (!inode)
5d4f98a2 4126 return ERR_PTR(-ENOMEM);
1a54ef8c
BR
4127
4128 if (inode->i_state & I_NEW) {
4129 BTRFS_I(inode)->root = root;
4130 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4131 btrfs_read_locked_inode(inode);
1748f843
MF
4132 if (!is_bad_inode(inode)) {
4133 inode_tree_add(inode);
4134 unlock_new_inode(inode);
4135 if (new)
4136 *new = 1;
4137 } else {
e0b6d65b
ST
4138 unlock_new_inode(inode);
4139 iput(inode);
4140 inode = ERR_PTR(-ESTALE);
1748f843
MF
4141 }
4142 }
4143
1a54ef8c
BR
4144 return inode;
4145}
4146
4df27c4d
YZ
4147static struct inode *new_simple_dir(struct super_block *s,
4148 struct btrfs_key *key,
4149 struct btrfs_root *root)
4150{
4151 struct inode *inode = new_inode(s);
4152
4153 if (!inode)
4154 return ERR_PTR(-ENOMEM);
4155
4df27c4d
YZ
4156 BTRFS_I(inode)->root = root;
4157 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
72ac3c0d 4158 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
4df27c4d
YZ
4159
4160 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
848cce0d 4161 inode->i_op = &btrfs_dir_ro_inode_operations;
4df27c4d
YZ
4162 inode->i_fop = &simple_dir_operations;
4163 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
4164 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4165
4166 return inode;
4167}
4168
3de4586c 4169struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
39279cc3 4170{
d397712b 4171 struct inode *inode;
4df27c4d 4172 struct btrfs_root *root = BTRFS_I(dir)->root;
39279cc3
CM
4173 struct btrfs_root *sub_root = root;
4174 struct btrfs_key location;
76dda93c 4175 int index;
b4aff1f8 4176 int ret = 0;
39279cc3
CM
4177
4178 if (dentry->d_name.len > BTRFS_NAME_LEN)
4179 return ERR_PTR(-ENAMETOOLONG);
5f39d397 4180
b4aff1f8
JB
4181 if (unlikely(d_need_lookup(dentry))) {
4182 memcpy(&location, dentry->d_fsdata, sizeof(struct btrfs_key));
4183 kfree(dentry->d_fsdata);
4184 dentry->d_fsdata = NULL;
a66e7cc6
JB
4185 /* This thing is hashed, drop it for now */
4186 d_drop(dentry);
b4aff1f8
JB
4187 } else {
4188 ret = btrfs_inode_by_name(dir, dentry, &location);
4189 }
5f39d397 4190
39279cc3
CM
4191 if (ret < 0)
4192 return ERR_PTR(ret);
5f39d397 4193
4df27c4d
YZ
4194 if (location.objectid == 0)
4195 return NULL;
4196
4197 if (location.type == BTRFS_INODE_ITEM_KEY) {
73f73415 4198 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
4df27c4d
YZ
4199 return inode;
4200 }
4201
4202 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4203
76dda93c 4204 index = srcu_read_lock(&root->fs_info->subvol_srcu);
4df27c4d
YZ
4205 ret = fixup_tree_root_location(root, dir, dentry,
4206 &location, &sub_root);
4207 if (ret < 0) {
4208 if (ret != -ENOENT)
4209 inode = ERR_PTR(ret);
4210 else
4211 inode = new_simple_dir(dir->i_sb, &location, sub_root);
4212 } else {
73f73415 4213 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
39279cc3 4214 }
76dda93c
YZ
4215 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4216
34d19bad 4217 if (!IS_ERR(inode) && root != sub_root) {
c71bf099
YZ
4218 down_read(&root->fs_info->cleanup_work_sem);
4219 if (!(inode->i_sb->s_flags & MS_RDONLY))
66b4ffd1 4220 ret = btrfs_orphan_cleanup(sub_root);
c71bf099 4221 up_read(&root->fs_info->cleanup_work_sem);
66b4ffd1
JB
4222 if (ret)
4223 inode = ERR_PTR(ret);
c71bf099
YZ
4224 }
4225
3de4586c
CM
4226 return inode;
4227}
4228
fe15ce44 4229static int btrfs_dentry_delete(const struct dentry *dentry)
76dda93c
YZ
4230{
4231 struct btrfs_root *root;
848cce0d 4232 struct inode *inode = dentry->d_inode;
76dda93c 4233
848cce0d
LZ
4234 if (!inode && !IS_ROOT(dentry))
4235 inode = dentry->d_parent->d_inode;
76dda93c 4236
848cce0d
LZ
4237 if (inode) {
4238 root = BTRFS_I(inode)->root;
efefb143
YZ
4239 if (btrfs_root_refs(&root->root_item) == 0)
4240 return 1;
848cce0d
LZ
4241
4242 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
4243 return 1;
efefb143 4244 }
76dda93c
YZ
4245 return 0;
4246}
4247
b4aff1f8
JB
4248static void btrfs_dentry_release(struct dentry *dentry)
4249{
4250 if (dentry->d_fsdata)
4251 kfree(dentry->d_fsdata);
4252}
4253
3de4586c 4254static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
00cd8dd3 4255 unsigned int flags)
3de4586c 4256{
a66e7cc6
JB
4257 struct dentry *ret;
4258
4259 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
4260 if (unlikely(d_need_lookup(dentry))) {
4261 spin_lock(&dentry->d_lock);
4262 dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
4263 spin_unlock(&dentry->d_lock);
4264 }
4265 return ret;
39279cc3
CM
4266}
4267
16cdcec7 4268unsigned char btrfs_filetype_table[] = {
39279cc3
CM
4269 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
4270};
4271
cbdf5a24
DW
4272static int btrfs_real_readdir(struct file *filp, void *dirent,
4273 filldir_t filldir)
39279cc3 4274{
6da6abae 4275 struct inode *inode = filp->f_dentry->d_inode;
39279cc3
CM
4276 struct btrfs_root *root = BTRFS_I(inode)->root;
4277 struct btrfs_item *item;
4278 struct btrfs_dir_item *di;
4279 struct btrfs_key key;
5f39d397 4280 struct btrfs_key found_key;
39279cc3 4281 struct btrfs_path *path;
16cdcec7
MX
4282 struct list_head ins_list;
4283 struct list_head del_list;
39279cc3 4284 int ret;
5f39d397 4285 struct extent_buffer *leaf;
39279cc3 4286 int slot;
39279cc3
CM
4287 unsigned char d_type;
4288 int over = 0;
4289 u32 di_cur;
4290 u32 di_total;
4291 u32 di_len;
4292 int key_type = BTRFS_DIR_INDEX_KEY;
5f39d397
CM
4293 char tmp_name[32];
4294 char *name_ptr;
4295 int name_len;
16cdcec7 4296 int is_curr = 0; /* filp->f_pos points to the current index? */
39279cc3
CM
4297
4298 /* FIXME, use a real flag for deciding about the key type */
4299 if (root->fs_info->tree_root == root)
4300 key_type = BTRFS_DIR_ITEM_KEY;
5f39d397 4301
3954401f
CM
4302 /* special case for "." */
4303 if (filp->f_pos == 0) {
3765fefa
HS
4304 over = filldir(dirent, ".", 1,
4305 filp->f_pos, btrfs_ino(inode), DT_DIR);
3954401f
CM
4306 if (over)
4307 return 0;
4308 filp->f_pos = 1;
4309 }
3954401f
CM
4310 /* special case for .., just use the back ref */
4311 if (filp->f_pos == 1) {
5ecc7e5d 4312 u64 pino = parent_ino(filp->f_path.dentry);
3954401f 4313 over = filldir(dirent, "..", 2,
3765fefa 4314 filp->f_pos, pino, DT_DIR);
3954401f 4315 if (over)
49593bfa 4316 return 0;
3954401f
CM
4317 filp->f_pos = 2;
4318 }
49593bfa 4319 path = btrfs_alloc_path();
16cdcec7
MX
4320 if (!path)
4321 return -ENOMEM;
ff5714cc 4322
026fd317 4323 path->reada = 1;
49593bfa 4324
16cdcec7
MX
4325 if (key_type == BTRFS_DIR_INDEX_KEY) {
4326 INIT_LIST_HEAD(&ins_list);
4327 INIT_LIST_HEAD(&del_list);
4328 btrfs_get_delayed_items(inode, &ins_list, &del_list);
4329 }
4330
39279cc3
CM
4331 btrfs_set_key_type(&key, key_type);
4332 key.offset = filp->f_pos;
33345d01 4333 key.objectid = btrfs_ino(inode);
5f39d397 4334
39279cc3
CM
4335 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4336 if (ret < 0)
4337 goto err;
49593bfa
DW
4338
4339 while (1) {
5f39d397 4340 leaf = path->nodes[0];
39279cc3 4341 slot = path->slots[0];
b9e03af0
LZ
4342 if (slot >= btrfs_header_nritems(leaf)) {
4343 ret = btrfs_next_leaf(root, path);
4344 if (ret < 0)
4345 goto err;
4346 else if (ret > 0)
4347 break;
4348 continue;
39279cc3 4349 }
3de4586c 4350
5f39d397
CM
4351 item = btrfs_item_nr(leaf, slot);
4352 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4353
4354 if (found_key.objectid != key.objectid)
39279cc3 4355 break;
5f39d397 4356 if (btrfs_key_type(&found_key) != key_type)
39279cc3 4357 break;
5f39d397 4358 if (found_key.offset < filp->f_pos)
b9e03af0 4359 goto next;
16cdcec7
MX
4360 if (key_type == BTRFS_DIR_INDEX_KEY &&
4361 btrfs_should_delete_dir_index(&del_list,
4362 found_key.offset))
4363 goto next;
5f39d397
CM
4364
4365 filp->f_pos = found_key.offset;
16cdcec7 4366 is_curr = 1;
49593bfa 4367
39279cc3
CM
4368 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
4369 di_cur = 0;
5f39d397 4370 di_total = btrfs_item_size(leaf, item);
49593bfa
DW
4371
4372 while (di_cur < di_total) {
5f39d397
CM
4373 struct btrfs_key location;
4374
22a94d44
JB
4375 if (verify_dir_item(root, leaf, di))
4376 break;
4377
5f39d397 4378 name_len = btrfs_dir_name_len(leaf, di);
49593bfa 4379 if (name_len <= sizeof(tmp_name)) {
5f39d397
CM
4380 name_ptr = tmp_name;
4381 } else {
4382 name_ptr = kmalloc(name_len, GFP_NOFS);
49593bfa
DW
4383 if (!name_ptr) {
4384 ret = -ENOMEM;
4385 goto err;
4386 }
5f39d397
CM
4387 }
4388 read_extent_buffer(leaf, name_ptr,
4389 (unsigned long)(di + 1), name_len);
4390
4391 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
4392 btrfs_dir_item_key_to_cpu(leaf, di, &location);
3de4586c 4393
fede766f 4394
3de4586c 4395 /* is this a reference to our own snapshot? If so
8c9c2bf7
AJ
4396 * skip it.
4397 *
4398 * In contrast to old kernels, we insert the snapshot's
4399 * dir item and dir index after it has been created, so
4400 * we won't find a reference to our own snapshot. We
4401 * still keep the following code for backward
4402 * compatibility.
3de4586c
CM
4403 */
4404 if (location.type == BTRFS_ROOT_ITEM_KEY &&
4405 location.objectid == root->root_key.objectid) {
4406 over = 0;
4407 goto skip;
4408 }
5f39d397 4409 over = filldir(dirent, name_ptr, name_len,
49593bfa 4410 found_key.offset, location.objectid,
39279cc3 4411 d_type);
5f39d397 4412
3de4586c 4413skip:
5f39d397
CM
4414 if (name_ptr != tmp_name)
4415 kfree(name_ptr);
4416
39279cc3
CM
4417 if (over)
4418 goto nopos;
5103e947 4419 di_len = btrfs_dir_name_len(leaf, di) +
49593bfa 4420 btrfs_dir_data_len(leaf, di) + sizeof(*di);
39279cc3
CM
4421 di_cur += di_len;
4422 di = (struct btrfs_dir_item *)((char *)di + di_len);
4423 }
b9e03af0
LZ
4424next:
4425 path->slots[0]++;
39279cc3 4426 }
49593bfa 4427
16cdcec7
MX
4428 if (key_type == BTRFS_DIR_INDEX_KEY) {
4429 if (is_curr)
4430 filp->f_pos++;
4431 ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir,
4432 &ins_list);
4433 if (ret)
4434 goto nopos;
4435 }
4436
49593bfa 4437 /* Reached end of directory/root. Bump pos past the last item. */
5e591a07 4438 if (key_type == BTRFS_DIR_INDEX_KEY)
406266ab
JE
4439 /*
4440 * 32-bit glibc will use getdents64, but then strtol -
4441 * so the last number we can serve is this.
4442 */
4443 filp->f_pos = 0x7fffffff;
5e591a07
YZ
4444 else
4445 filp->f_pos++;
39279cc3
CM
4446nopos:
4447 ret = 0;
4448err:
16cdcec7
MX
4449 if (key_type == BTRFS_DIR_INDEX_KEY)
4450 btrfs_put_delayed_items(&ins_list, &del_list);
39279cc3 4451 btrfs_free_path(path);
39279cc3
CM
4452 return ret;
4453}
4454
a9185b41 4455int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
39279cc3
CM
4456{
4457 struct btrfs_root *root = BTRFS_I(inode)->root;
4458 struct btrfs_trans_handle *trans;
4459 int ret = 0;
0af3d00b 4460 bool nolock = false;
39279cc3 4461
72ac3c0d 4462 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
4ca8b41e
CM
4463 return 0;
4464
83eea1f1 4465 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
82d5902d 4466 nolock = true;
0af3d00b 4467
a9185b41 4468 if (wbc->sync_mode == WB_SYNC_ALL) {
0af3d00b 4469 if (nolock)
7a7eaa40 4470 trans = btrfs_join_transaction_nolock(root);
0af3d00b 4471 else
7a7eaa40 4472 trans = btrfs_join_transaction(root);
3612b495
TI
4473 if (IS_ERR(trans))
4474 return PTR_ERR(trans);
0af3d00b
JB
4475 if (nolock)
4476 ret = btrfs_end_transaction_nolock(trans, root);
4477 else
4478 ret = btrfs_commit_transaction(trans, root);
39279cc3
CM
4479 }
4480 return ret;
4481}
4482
4483/*
54aa1f4d 4484 * This is somewhat expensive, updating the tree every time the
39279cc3
CM
4485 * inode changes. But, it is most likely to find the inode in cache.
4486 * FIXME, needs more benchmarking...there are no reasons other than performance
4487 * to keep or drop this code.
4488 */
22c44fe6 4489int btrfs_dirty_inode(struct inode *inode)
39279cc3
CM
4490{
4491 struct btrfs_root *root = BTRFS_I(inode)->root;
4492 struct btrfs_trans_handle *trans;
8929ecfa
YZ
4493 int ret;
4494
72ac3c0d 4495 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
22c44fe6 4496 return 0;
39279cc3 4497
7a7eaa40 4498 trans = btrfs_join_transaction(root);
22c44fe6
JB
4499 if (IS_ERR(trans))
4500 return PTR_ERR(trans);
8929ecfa
YZ
4501
4502 ret = btrfs_update_inode(trans, root, inode);
94b60442
CM
4503 if (ret && ret == -ENOSPC) {
4504 /* whoops, lets try again with the full transaction */
4505 btrfs_end_transaction(trans, root);
4506 trans = btrfs_start_transaction(root, 1);
22c44fe6
JB
4507 if (IS_ERR(trans))
4508 return PTR_ERR(trans);
8929ecfa 4509
94b60442 4510 ret = btrfs_update_inode(trans, root, inode);
94b60442 4511 }
39279cc3 4512 btrfs_end_transaction(trans, root);
16cdcec7
MX
4513 if (BTRFS_I(inode)->delayed_node)
4514 btrfs_balance_delayed_items(root);
22c44fe6
JB
4515
4516 return ret;
4517}
4518
4519/*
4520 * This is a copy of file_update_time. We need this so we can return error on
4521 * ENOSPC for updating the inode in the case of file write and mmap writes.
4522 */
e41f941a
JB
4523static int btrfs_update_time(struct inode *inode, struct timespec *now,
4524 int flags)
22c44fe6 4525{
2bc55652
AB
4526 struct btrfs_root *root = BTRFS_I(inode)->root;
4527
4528 if (btrfs_root_readonly(root))
4529 return -EROFS;
4530
e41f941a 4531 if (flags & S_VERSION)
22c44fe6 4532 inode_inc_iversion(inode);
e41f941a
JB
4533 if (flags & S_CTIME)
4534 inode->i_ctime = *now;
4535 if (flags & S_MTIME)
4536 inode->i_mtime = *now;
4537 if (flags & S_ATIME)
4538 inode->i_atime = *now;
4539 return btrfs_dirty_inode(inode);
39279cc3
CM
4540}
4541
d352ac68
CM
4542/*
4543 * find the highest existing sequence number in a directory
4544 * and then set the in-memory index_cnt variable to reflect
4545 * free sequence numbers
4546 */
aec7477b
JB
4547static int btrfs_set_inode_index_count(struct inode *inode)
4548{
4549 struct btrfs_root *root = BTRFS_I(inode)->root;
4550 struct btrfs_key key, found_key;
4551 struct btrfs_path *path;
4552 struct extent_buffer *leaf;
4553 int ret;
4554
33345d01 4555 key.objectid = btrfs_ino(inode);
aec7477b
JB
4556 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
4557 key.offset = (u64)-1;
4558
4559 path = btrfs_alloc_path();
4560 if (!path)
4561 return -ENOMEM;
4562
4563 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4564 if (ret < 0)
4565 goto out;
4566 /* FIXME: we should be able to handle this */
4567 if (ret == 0)
4568 goto out;
4569 ret = 0;
4570
4571 /*
4572 * MAGIC NUMBER EXPLANATION:
4573 * since we search a directory based on f_pos we have to start at 2
4574 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4575 * else has to start at 2
4576 */
4577 if (path->slots[0] == 0) {
4578 BTRFS_I(inode)->index_cnt = 2;
4579 goto out;
4580 }
4581
4582 path->slots[0]--;
4583
4584 leaf = path->nodes[0];
4585 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4586
33345d01 4587 if (found_key.objectid != btrfs_ino(inode) ||
aec7477b
JB
4588 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
4589 BTRFS_I(inode)->index_cnt = 2;
4590 goto out;
4591 }
4592
4593 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
4594out:
4595 btrfs_free_path(path);
4596 return ret;
4597}
4598
d352ac68
CM
4599/*
4600 * helper to find a free sequence number in a given directory. This current
4601 * code is very simple, later versions will do smarter things in the btree
4602 */
3de4586c 4603int btrfs_set_inode_index(struct inode *dir, u64 *index)
aec7477b
JB
4604{
4605 int ret = 0;
4606
4607 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
16cdcec7
MX
4608 ret = btrfs_inode_delayed_dir_index_count(dir);
4609 if (ret) {
4610 ret = btrfs_set_inode_index_count(dir);
4611 if (ret)
4612 return ret;
4613 }
aec7477b
JB
4614 }
4615
00e4e6b3 4616 *index = BTRFS_I(dir)->index_cnt;
aec7477b
JB
4617 BTRFS_I(dir)->index_cnt++;
4618
4619 return ret;
4620}
4621
39279cc3
CM
4622static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4623 struct btrfs_root *root,
aec7477b 4624 struct inode *dir,
9c58309d 4625 const char *name, int name_len,
175a4eb7
AV
4626 u64 ref_objectid, u64 objectid,
4627 umode_t mode, u64 *index)
39279cc3
CM
4628{
4629 struct inode *inode;
5f39d397 4630 struct btrfs_inode_item *inode_item;
39279cc3 4631 struct btrfs_key *location;
5f39d397 4632 struct btrfs_path *path;
9c58309d
CM
4633 struct btrfs_inode_ref *ref;
4634 struct btrfs_key key[2];
4635 u32 sizes[2];
4636 unsigned long ptr;
39279cc3
CM
4637 int ret;
4638 int owner;
4639
5f39d397 4640 path = btrfs_alloc_path();
d8926bb3
MF
4641 if (!path)
4642 return ERR_PTR(-ENOMEM);
5f39d397 4643
39279cc3 4644 inode = new_inode(root->fs_info->sb);
8fb27640
YS
4645 if (!inode) {
4646 btrfs_free_path(path);
39279cc3 4647 return ERR_PTR(-ENOMEM);
8fb27640 4648 }
39279cc3 4649
581bb050
LZ
4650 /*
4651 * we have to initialize this early, so we can reclaim the inode
4652 * number if we fail afterwards in this function.
4653 */
4654 inode->i_ino = objectid;
4655
aec7477b 4656 if (dir) {
1abe9b8a 4657 trace_btrfs_inode_request(dir);
4658
3de4586c 4659 ret = btrfs_set_inode_index(dir, index);
09771430 4660 if (ret) {
8fb27640 4661 btrfs_free_path(path);
09771430 4662 iput(inode);
aec7477b 4663 return ERR_PTR(ret);
09771430 4664 }
aec7477b
JB
4665 }
4666 /*
4667 * index_cnt is ignored for everything but a dir,
4668 * btrfs_get_inode_index_count has an explanation for the magic
4669 * number
4670 */
4671 BTRFS_I(inode)->index_cnt = 2;
39279cc3 4672 BTRFS_I(inode)->root = root;
e02119d5 4673 BTRFS_I(inode)->generation = trans->transid;
76195853 4674 inode->i_generation = BTRFS_I(inode)->generation;
b888db2b 4675
569254b0 4676 if (S_ISDIR(mode))
39279cc3
CM
4677 owner = 0;
4678 else
4679 owner = 1;
9c58309d
CM
4680
4681 key[0].objectid = objectid;
4682 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
4683 key[0].offset = 0;
4684
4685 key[1].objectid = objectid;
4686 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
4687 key[1].offset = ref_objectid;
4688
4689 sizes[0] = sizeof(struct btrfs_inode_item);
4690 sizes[1] = name_len + sizeof(*ref);
4691
b9473439 4692 path->leave_spinning = 1;
9c58309d
CM
4693 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
4694 if (ret != 0)
5f39d397
CM
4695 goto fail;
4696
ecc11fab 4697 inode_init_owner(inode, dir, mode);
a76a3cd4 4698 inode_set_bytes(inode, 0);
39279cc3 4699 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
5f39d397
CM
4700 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4701 struct btrfs_inode_item);
293f7e07
LZ
4702 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
4703 sizeof(*inode_item));
e02119d5 4704 fill_inode_item(trans, path->nodes[0], inode_item, inode);
9c58309d
CM
4705
4706 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
4707 struct btrfs_inode_ref);
4708 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
00e4e6b3 4709 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
9c58309d
CM
4710 ptr = (unsigned long)(ref + 1);
4711 write_extent_buffer(path->nodes[0], name, ptr, name_len);
4712
5f39d397
CM
4713 btrfs_mark_buffer_dirty(path->nodes[0]);
4714 btrfs_free_path(path);
4715
39279cc3
CM
4716 location = &BTRFS_I(inode)->location;
4717 location->objectid = objectid;
39279cc3
CM
4718 location->offset = 0;
4719 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
4720
6cbff00f
CH
4721 btrfs_inherit_iflags(inode, dir);
4722
569254b0 4723 if (S_ISREG(mode)) {
94272164
CM
4724 if (btrfs_test_opt(root, NODATASUM))
4725 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
75e7cb7f
LB
4726 if (btrfs_test_opt(root, NODATACOW) ||
4727 (BTRFS_I(dir)->flags & BTRFS_INODE_NODATACOW))
94272164
CM
4728 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
4729 }
4730
39279cc3 4731 insert_inode_hash(inode);
5d4f98a2 4732 inode_tree_add(inode);
1abe9b8a 4733
4734 trace_btrfs_inode_new(inode);
1973f0fa 4735 btrfs_set_inode_last_trans(trans, inode);
1abe9b8a 4736
8ea05e3a
AB
4737 btrfs_update_root_times(trans, root);
4738
39279cc3 4739 return inode;
5f39d397 4740fail:
aec7477b
JB
4741 if (dir)
4742 BTRFS_I(dir)->index_cnt--;
5f39d397 4743 btrfs_free_path(path);
09771430 4744 iput(inode);
5f39d397 4745 return ERR_PTR(ret);
39279cc3
CM
4746}
4747
4748static inline u8 btrfs_inode_type(struct inode *inode)
4749{
4750 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
4751}
4752
d352ac68
CM
4753/*
4754 * utility function to add 'inode' into 'parent_inode' with
4755 * a give name and a given sequence number.
4756 * if 'add_backref' is true, also insert a backref from the
4757 * inode to the parent directory.
4758 */
e02119d5
CM
4759int btrfs_add_link(struct btrfs_trans_handle *trans,
4760 struct inode *parent_inode, struct inode *inode,
4761 const char *name, int name_len, int add_backref, u64 index)
39279cc3 4762{
4df27c4d 4763 int ret = 0;
39279cc3 4764 struct btrfs_key key;
e02119d5 4765 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
33345d01
LZ
4766 u64 ino = btrfs_ino(inode);
4767 u64 parent_ino = btrfs_ino(parent_inode);
5f39d397 4768
33345d01 4769 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
4df27c4d
YZ
4770 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4771 } else {
33345d01 4772 key.objectid = ino;
4df27c4d
YZ
4773 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4774 key.offset = 0;
4775 }
4776
33345d01 4777 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
4df27c4d
YZ
4778 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4779 key.objectid, root->root_key.objectid,
33345d01 4780 parent_ino, index, name, name_len);
4df27c4d 4781 } else if (add_backref) {
33345d01
LZ
4782 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
4783 parent_ino, index);
4df27c4d 4784 }
39279cc3 4785
79787eaa
JM
4786 /* Nothing to clean up yet */
4787 if (ret)
4788 return ret;
4df27c4d 4789
79787eaa
JM
4790 ret = btrfs_insert_dir_item(trans, root, name, name_len,
4791 parent_inode, &key,
4792 btrfs_inode_type(inode), index);
4793 if (ret == -EEXIST)
4794 goto fail_dir_item;
4795 else if (ret) {
4796 btrfs_abort_transaction(trans, root, ret);
4797 return ret;
39279cc3 4798 }
79787eaa
JM
4799
4800 btrfs_i_size_write(parent_inode, parent_inode->i_size +
4801 name_len * 2);
0c4d2d95 4802 inode_inc_iversion(parent_inode);
79787eaa
JM
4803 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
4804 ret = btrfs_update_inode(trans, root, parent_inode);
4805 if (ret)
4806 btrfs_abort_transaction(trans, root, ret);
39279cc3 4807 return ret;
fe66a05a
CM
4808
4809fail_dir_item:
4810 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
4811 u64 local_index;
4812 int err;
4813 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
4814 key.objectid, root->root_key.objectid,
4815 parent_ino, &local_index, name, name_len);
4816
4817 } else if (add_backref) {
4818 u64 local_index;
4819 int err;
4820
4821 err = btrfs_del_inode_ref(trans, root, name, name_len,
4822 ino, parent_ino, &local_index);
4823 }
4824 return ret;
39279cc3
CM
4825}
4826
4827static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
a1b075d2
JB
4828 struct inode *dir, struct dentry *dentry,
4829 struct inode *inode, int backref, u64 index)
39279cc3 4830{
a1b075d2
JB
4831 int err = btrfs_add_link(trans, dir, inode,
4832 dentry->d_name.name, dentry->d_name.len,
4833 backref, index);
39279cc3
CM
4834 if (err > 0)
4835 err = -EEXIST;
4836 return err;
4837}
4838
618e21d5 4839static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1a67aafb 4840 umode_t mode, dev_t rdev)
618e21d5
JB
4841{
4842 struct btrfs_trans_handle *trans;
4843 struct btrfs_root *root = BTRFS_I(dir)->root;
1832a6d5 4844 struct inode *inode = NULL;
618e21d5
JB
4845 int err;
4846 int drop_inode = 0;
4847 u64 objectid;
1832a6d5 4848 unsigned long nr = 0;
00e4e6b3 4849 u64 index = 0;
618e21d5
JB
4850
4851 if (!new_valid_dev(rdev))
4852 return -EINVAL;
4853
9ed74f2d
JB
4854 /*
4855 * 2 for inode item and ref
4856 * 2 for dir items
4857 * 1 for xattr if selinux is on
4858 */
a22285a6
YZ
4859 trans = btrfs_start_transaction(root, 5);
4860 if (IS_ERR(trans))
4861 return PTR_ERR(trans);
1832a6d5 4862
581bb050
LZ
4863 err = btrfs_find_free_ino(root, &objectid);
4864 if (err)
4865 goto out_unlock;
4866
aec7477b 4867 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
33345d01 4868 dentry->d_name.len, btrfs_ino(dir), objectid,
d82a6f1d 4869 mode, &index);
7cf96da3
TI
4870 if (IS_ERR(inode)) {
4871 err = PTR_ERR(inode);
618e21d5 4872 goto out_unlock;
7cf96da3 4873 }
618e21d5 4874
2a7dba39 4875 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
33268eaf
JB
4876 if (err) {
4877 drop_inode = 1;
4878 goto out_unlock;
4879 }
4880
ad19db71
CS
4881 /*
4882 * If the active LSM wants to access the inode during
4883 * d_instantiate it needs these. Smack checks to see
4884 * if the filesystem supports xattrs by looking at the
4885 * ops vector.
4886 */
4887
4888 inode->i_op = &btrfs_special_inode_operations;
a1b075d2 4889 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
618e21d5
JB
4890 if (err)
4891 drop_inode = 1;
4892 else {
618e21d5 4893 init_special_inode(inode, inode->i_mode, rdev);
1b4ab1bb 4894 btrfs_update_inode(trans, root, inode);
08c422c2 4895 d_instantiate(dentry, inode);
618e21d5 4896 }
618e21d5 4897out_unlock:
d3c2fdcf 4898 nr = trans->blocks_used;
7ad85bb7 4899 btrfs_end_transaction(trans, root);
a22285a6 4900 btrfs_btree_balance_dirty(root, nr);
618e21d5
JB
4901 if (drop_inode) {
4902 inode_dec_link_count(inode);
4903 iput(inode);
4904 }
618e21d5
JB
4905 return err;
4906}
4907
39279cc3 4908static int btrfs_create(struct inode *dir, struct dentry *dentry,
ebfc3b49 4909 umode_t mode, bool excl)
39279cc3
CM
4910{
4911 struct btrfs_trans_handle *trans;
4912 struct btrfs_root *root = BTRFS_I(dir)->root;
1832a6d5 4913 struct inode *inode = NULL;
39279cc3 4914 int drop_inode = 0;
a22285a6 4915 int err;
1832a6d5 4916 unsigned long nr = 0;
39279cc3 4917 u64 objectid;
00e4e6b3 4918 u64 index = 0;
39279cc3 4919
9ed74f2d
JB
4920 /*
4921 * 2 for inode item and ref
4922 * 2 for dir items
4923 * 1 for xattr if selinux is on
4924 */
a22285a6
YZ
4925 trans = btrfs_start_transaction(root, 5);
4926 if (IS_ERR(trans))
4927 return PTR_ERR(trans);
9ed74f2d 4928
581bb050
LZ
4929 err = btrfs_find_free_ino(root, &objectid);
4930 if (err)
4931 goto out_unlock;
4932
aec7477b 4933 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
33345d01 4934 dentry->d_name.len, btrfs_ino(dir), objectid,
d82a6f1d 4935 mode, &index);
7cf96da3
TI
4936 if (IS_ERR(inode)) {
4937 err = PTR_ERR(inode);
39279cc3 4938 goto out_unlock;
7cf96da3 4939 }
39279cc3 4940
2a7dba39 4941 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
33268eaf
JB
4942 if (err) {
4943 drop_inode = 1;
4944 goto out_unlock;
4945 }
4946
ad19db71
CS
4947 /*
4948 * If the active LSM wants to access the inode during
4949 * d_instantiate it needs these. Smack checks to see
4950 * if the filesystem supports xattrs by looking at the
4951 * ops vector.
4952 */
4953 inode->i_fop = &btrfs_file_operations;
4954 inode->i_op = &btrfs_file_inode_operations;
4955
a1b075d2 4956 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
39279cc3
CM
4957 if (err)
4958 drop_inode = 1;
4959 else {
4960 inode->i_mapping->a_ops = &btrfs_aops;
04160088 4961 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
d1310b2e 4962 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
08c422c2 4963 d_instantiate(dentry, inode);
39279cc3 4964 }
39279cc3 4965out_unlock:
d3c2fdcf 4966 nr = trans->blocks_used;
7ad85bb7 4967 btrfs_end_transaction(trans, root);
39279cc3
CM
4968 if (drop_inode) {
4969 inode_dec_link_count(inode);
4970 iput(inode);
4971 }
d3c2fdcf 4972 btrfs_btree_balance_dirty(root, nr);
39279cc3
CM
4973 return err;
4974}
4975
4976static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
4977 struct dentry *dentry)
4978{
4979 struct btrfs_trans_handle *trans;
4980 struct btrfs_root *root = BTRFS_I(dir)->root;
4981 struct inode *inode = old_dentry->d_inode;
00e4e6b3 4982 u64 index;
1832a6d5 4983 unsigned long nr = 0;
39279cc3
CM
4984 int err;
4985 int drop_inode = 0;
4986
4a8be425
TH
4987 /* do not allow sys_link's with other subvols of the same device */
4988 if (root->objectid != BTRFS_I(inode)->root->objectid)
3ab3564f 4989 return -EXDEV;
4a8be425 4990
c055e99e
AV
4991 if (inode->i_nlink == ~0U)
4992 return -EMLINK;
4a8be425 4993
3de4586c 4994 err = btrfs_set_inode_index(dir, &index);
aec7477b
JB
4995 if (err)
4996 goto fail;
4997
a22285a6 4998 /*
7e6b6465 4999 * 2 items for inode and inode ref
a22285a6 5000 * 2 items for dir items
7e6b6465 5001 * 1 item for parent inode
a22285a6 5002 */
7e6b6465 5003 trans = btrfs_start_transaction(root, 5);
a22285a6
YZ
5004 if (IS_ERR(trans)) {
5005 err = PTR_ERR(trans);
5006 goto fail;
5007 }
5f39d397 5008
3153495d 5009 btrfs_inc_nlink(inode);
0c4d2d95 5010 inode_inc_iversion(inode);
3153495d 5011 inode->i_ctime = CURRENT_TIME;
7de9c6ee 5012 ihold(inode);
aec7477b 5013
a1b075d2 5014 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
5f39d397 5015
a5719521 5016 if (err) {
54aa1f4d 5017 drop_inode = 1;
a5719521 5018 } else {
10d9f309 5019 struct dentry *parent = dentry->d_parent;
a5719521 5020 err = btrfs_update_inode(trans, root, inode);
79787eaa
JM
5021 if (err)
5022 goto fail;
08c422c2 5023 d_instantiate(dentry, inode);
6a912213 5024 btrfs_log_new_name(trans, inode, NULL, parent);
a5719521 5025 }
39279cc3 5026
d3c2fdcf 5027 nr = trans->blocks_used;
7ad85bb7 5028 btrfs_end_transaction(trans, root);
1832a6d5 5029fail:
39279cc3
CM
5030 if (drop_inode) {
5031 inode_dec_link_count(inode);
5032 iput(inode);
5033 }
d3c2fdcf 5034 btrfs_btree_balance_dirty(root, nr);
39279cc3
CM
5035 return err;
5036}
5037
18bb1db3 5038static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
39279cc3 5039{
b9d86667 5040 struct inode *inode = NULL;
39279cc3
CM
5041 struct btrfs_trans_handle *trans;
5042 struct btrfs_root *root = BTRFS_I(dir)->root;
5043 int err = 0;
5044 int drop_on_err = 0;
b9d86667 5045 u64 objectid = 0;
00e4e6b3 5046 u64 index = 0;
d3c2fdcf 5047 unsigned long nr = 1;
39279cc3 5048
9ed74f2d
JB
5049 /*
5050 * 2 items for inode and ref
5051 * 2 items for dir items
5052 * 1 for xattr if selinux is on
5053 */
a22285a6
YZ
5054 trans = btrfs_start_transaction(root, 5);
5055 if (IS_ERR(trans))
5056 return PTR_ERR(trans);
39279cc3 5057
581bb050
LZ
5058 err = btrfs_find_free_ino(root, &objectid);
5059 if (err)
5060 goto out_fail;
5061
aec7477b 5062 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
33345d01 5063 dentry->d_name.len, btrfs_ino(dir), objectid,
d82a6f1d 5064 S_IFDIR | mode, &index);
39279cc3
CM
5065 if (IS_ERR(inode)) {
5066 err = PTR_ERR(inode);
5067 goto out_fail;
5068 }
5f39d397 5069
39279cc3 5070 drop_on_err = 1;
33268eaf 5071
2a7dba39 5072 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
33268eaf
JB
5073 if (err)
5074 goto out_fail;
5075
39279cc3
CM
5076 inode->i_op = &btrfs_dir_inode_operations;
5077 inode->i_fop = &btrfs_dir_file_operations;
39279cc3 5078
dbe674a9 5079 btrfs_i_size_write(inode, 0);
39279cc3
CM
5080 err = btrfs_update_inode(trans, root, inode);
5081 if (err)
5082 goto out_fail;
5f39d397 5083
a1b075d2
JB
5084 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5085 dentry->d_name.len, 0, index);
39279cc3
CM
5086 if (err)
5087 goto out_fail;
5f39d397 5088
39279cc3
CM
5089 d_instantiate(dentry, inode);
5090 drop_on_err = 0;
39279cc3
CM
5091
5092out_fail:
d3c2fdcf 5093 nr = trans->blocks_used;
7ad85bb7 5094 btrfs_end_transaction(trans, root);
39279cc3
CM
5095 if (drop_on_err)
5096 iput(inode);
d3c2fdcf 5097 btrfs_btree_balance_dirty(root, nr);
39279cc3
CM
5098 return err;
5099}
5100
d352ac68
CM
5101/* helper for btfs_get_extent. Given an existing extent in the tree,
5102 * and an extent that you want to insert, deal with overlap and insert
5103 * the new extent into the tree.
5104 */
3b951516
CM
5105static int merge_extent_mapping(struct extent_map_tree *em_tree,
5106 struct extent_map *existing,
e6dcd2dc
CM
5107 struct extent_map *em,
5108 u64 map_start, u64 map_len)
3b951516
CM
5109{
5110 u64 start_diff;
3b951516 5111
e6dcd2dc
CM
5112 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5113 start_diff = map_start - em->start;
5114 em->start = map_start;
5115 em->len = map_len;
c8b97818
CM
5116 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5117 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
e6dcd2dc 5118 em->block_start += start_diff;
c8b97818
CM
5119 em->block_len -= start_diff;
5120 }
e6dcd2dc 5121 return add_extent_mapping(em_tree, em);
3b951516
CM
5122}
5123
c8b97818
CM
5124static noinline int uncompress_inline(struct btrfs_path *path,
5125 struct inode *inode, struct page *page,
5126 size_t pg_offset, u64 extent_offset,
5127 struct btrfs_file_extent_item *item)
5128{
5129 int ret;
5130 struct extent_buffer *leaf = path->nodes[0];
5131 char *tmp;
5132 size_t max_size;
5133 unsigned long inline_size;
5134 unsigned long ptr;
261507a0 5135 int compress_type;
c8b97818
CM
5136
5137 WARN_ON(pg_offset != 0);
261507a0 5138 compress_type = btrfs_file_extent_compression(leaf, item);
c8b97818
CM
5139 max_size = btrfs_file_extent_ram_bytes(leaf, item);
5140 inline_size = btrfs_file_extent_inline_item_len(leaf,
5141 btrfs_item_nr(leaf, path->slots[0]));
5142 tmp = kmalloc(inline_size, GFP_NOFS);
8d413713
TI
5143 if (!tmp)
5144 return -ENOMEM;
c8b97818
CM
5145 ptr = btrfs_file_extent_inline_start(item);
5146
5147 read_extent_buffer(leaf, tmp, ptr, inline_size);
5148
5b050f04 5149 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
261507a0
LZ
5150 ret = btrfs_decompress(compress_type, tmp, page,
5151 extent_offset, inline_size, max_size);
c8b97818 5152 if (ret) {
7ac687d9 5153 char *kaddr = kmap_atomic(page);
c8b97818
CM
5154 unsigned long copy_size = min_t(u64,
5155 PAGE_CACHE_SIZE - pg_offset,
5156 max_size - extent_offset);
5157 memset(kaddr + pg_offset, 0, copy_size);
7ac687d9 5158 kunmap_atomic(kaddr);
c8b97818
CM
5159 }
5160 kfree(tmp);
5161 return 0;
5162}
5163
d352ac68
CM
5164/*
5165 * a bit scary, this does extent mapping from logical file offset to the disk.
d397712b
CM
5166 * the ugly parts come from merging extents from the disk with the in-ram
5167 * representation. This gets more complex because of the data=ordered code,
d352ac68
CM
5168 * where the in-ram extents might be locked pending data=ordered completion.
5169 *
5170 * This also copies inline extents directly into the page.
5171 */
d397712b 5172
a52d9a80 5173struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
70dec807 5174 size_t pg_offset, u64 start, u64 len,
a52d9a80
CM
5175 int create)
5176{
5177 int ret;
5178 int err = 0;
db94535d 5179 u64 bytenr;
a52d9a80
CM
5180 u64 extent_start = 0;
5181 u64 extent_end = 0;
33345d01 5182 u64 objectid = btrfs_ino(inode);
a52d9a80 5183 u32 found_type;
f421950f 5184 struct btrfs_path *path = NULL;
a52d9a80
CM
5185 struct btrfs_root *root = BTRFS_I(inode)->root;
5186 struct btrfs_file_extent_item *item;
5f39d397
CM
5187 struct extent_buffer *leaf;
5188 struct btrfs_key found_key;
a52d9a80
CM
5189 struct extent_map *em = NULL;
5190 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
d1310b2e 5191 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
a52d9a80 5192 struct btrfs_trans_handle *trans = NULL;
261507a0 5193 int compress_type;
a52d9a80 5194
a52d9a80 5195again:
890871be 5196 read_lock(&em_tree->lock);
d1310b2e 5197 em = lookup_extent_mapping(em_tree, start, len);
a061fc8d
CM
5198 if (em)
5199 em->bdev = root->fs_info->fs_devices->latest_bdev;
890871be 5200 read_unlock(&em_tree->lock);
d1310b2e 5201
a52d9a80 5202 if (em) {
e1c4b745
CM
5203 if (em->start > start || em->start + em->len <= start)
5204 free_extent_map(em);
5205 else if (em->block_start == EXTENT_MAP_INLINE && page)
70dec807
CM
5206 free_extent_map(em);
5207 else
5208 goto out;
a52d9a80 5209 }
172ddd60 5210 em = alloc_extent_map();
a52d9a80 5211 if (!em) {
d1310b2e
CM
5212 err = -ENOMEM;
5213 goto out;
a52d9a80 5214 }
e6dcd2dc 5215 em->bdev = root->fs_info->fs_devices->latest_bdev;
d1310b2e 5216 em->start = EXTENT_MAP_HOLE;
445a6944 5217 em->orig_start = EXTENT_MAP_HOLE;
d1310b2e 5218 em->len = (u64)-1;
c8b97818 5219 em->block_len = (u64)-1;
f421950f
CM
5220
5221 if (!path) {
5222 path = btrfs_alloc_path();
026fd317
JB
5223 if (!path) {
5224 err = -ENOMEM;
5225 goto out;
5226 }
5227 /*
5228 * Chances are we'll be called again, so go ahead and do
5229 * readahead
5230 */
5231 path->reada = 1;
f421950f
CM
5232 }
5233
179e29e4
CM
5234 ret = btrfs_lookup_file_extent(trans, root, path,
5235 objectid, start, trans != NULL);
a52d9a80
CM
5236 if (ret < 0) {
5237 err = ret;
5238 goto out;
5239 }
5240
5241 if (ret != 0) {
5242 if (path->slots[0] == 0)
5243 goto not_found;
5244 path->slots[0]--;
5245 }
5246
5f39d397
CM
5247 leaf = path->nodes[0];
5248 item = btrfs_item_ptr(leaf, path->slots[0],
a52d9a80 5249 struct btrfs_file_extent_item);
a52d9a80 5250 /* are we inside the extent that was found? */
5f39d397
CM
5251 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5252 found_type = btrfs_key_type(&found_key);
5253 if (found_key.objectid != objectid ||
a52d9a80
CM
5254 found_type != BTRFS_EXTENT_DATA_KEY) {
5255 goto not_found;
5256 }
5257
5f39d397
CM
5258 found_type = btrfs_file_extent_type(leaf, item);
5259 extent_start = found_key.offset;
261507a0 5260 compress_type = btrfs_file_extent_compression(leaf, item);
d899e052
YZ
5261 if (found_type == BTRFS_FILE_EXTENT_REG ||
5262 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
a52d9a80 5263 extent_end = extent_start +
db94535d 5264 btrfs_file_extent_num_bytes(leaf, item);
9036c102
YZ
5265 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
5266 size_t size;
5267 size = btrfs_file_extent_inline_len(leaf, item);
5268 extent_end = (extent_start + size + root->sectorsize - 1) &
5269 ~((u64)root->sectorsize - 1);
5270 }
5271
5272 if (start >= extent_end) {
5273 path->slots[0]++;
5274 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
5275 ret = btrfs_next_leaf(root, path);
5276 if (ret < 0) {
5277 err = ret;
5278 goto out;
a52d9a80 5279 }
9036c102
YZ
5280 if (ret > 0)
5281 goto not_found;
5282 leaf = path->nodes[0];
a52d9a80 5283 }
9036c102
YZ
5284 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5285 if (found_key.objectid != objectid ||
5286 found_key.type != BTRFS_EXTENT_DATA_KEY)
5287 goto not_found;
5288 if (start + len <= found_key.offset)
5289 goto not_found;
5290 em->start = start;
5291 em->len = found_key.offset - start;
5292 goto not_found_em;
5293 }
5294
d899e052
YZ
5295 if (found_type == BTRFS_FILE_EXTENT_REG ||
5296 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
9036c102
YZ
5297 em->start = extent_start;
5298 em->len = extent_end - extent_start;
ff5b7ee3
YZ
5299 em->orig_start = extent_start -
5300 btrfs_file_extent_offset(leaf, item);
db94535d
CM
5301 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
5302 if (bytenr == 0) {
5f39d397 5303 em->block_start = EXTENT_MAP_HOLE;
a52d9a80
CM
5304 goto insert;
5305 }
261507a0 5306 if (compress_type != BTRFS_COMPRESS_NONE) {
c8b97818 5307 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
261507a0 5308 em->compress_type = compress_type;
c8b97818
CM
5309 em->block_start = bytenr;
5310 em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
5311 item);
5312 } else {
5313 bytenr += btrfs_file_extent_offset(leaf, item);
5314 em->block_start = bytenr;
5315 em->block_len = em->len;
d899e052
YZ
5316 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
5317 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
c8b97818 5318 }
a52d9a80
CM
5319 goto insert;
5320 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
5f39d397 5321 unsigned long ptr;
a52d9a80 5322 char *map;
3326d1b0
CM
5323 size_t size;
5324 size_t extent_offset;
5325 size_t copy_size;
a52d9a80 5326
689f9346 5327 em->block_start = EXTENT_MAP_INLINE;
c8b97818 5328 if (!page || create) {
689f9346 5329 em->start = extent_start;
9036c102 5330 em->len = extent_end - extent_start;
689f9346
Y
5331 goto out;
5332 }
5f39d397 5333
9036c102
YZ
5334 size = btrfs_file_extent_inline_len(leaf, item);
5335 extent_offset = page_offset(page) + pg_offset - extent_start;
70dec807 5336 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
3326d1b0 5337 size - extent_offset);
3326d1b0 5338 em->start = extent_start + extent_offset;
70dec807
CM
5339 em->len = (copy_size + root->sectorsize - 1) &
5340 ~((u64)root->sectorsize - 1);
ff5b7ee3 5341 em->orig_start = EXTENT_MAP_INLINE;
261507a0 5342 if (compress_type) {
c8b97818 5343 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
261507a0
LZ
5344 em->compress_type = compress_type;
5345 }
689f9346 5346 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
179e29e4 5347 if (create == 0 && !PageUptodate(page)) {
261507a0
LZ
5348 if (btrfs_file_extent_compression(leaf, item) !=
5349 BTRFS_COMPRESS_NONE) {
c8b97818
CM
5350 ret = uncompress_inline(path, inode, page,
5351 pg_offset,
5352 extent_offset, item);
79787eaa 5353 BUG_ON(ret); /* -ENOMEM */
c8b97818
CM
5354 } else {
5355 map = kmap(page);
5356 read_extent_buffer(leaf, map + pg_offset, ptr,
5357 copy_size);
93c82d57
CM
5358 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
5359 memset(map + pg_offset + copy_size, 0,
5360 PAGE_CACHE_SIZE - pg_offset -
5361 copy_size);
5362 }
c8b97818
CM
5363 kunmap(page);
5364 }
179e29e4
CM
5365 flush_dcache_page(page);
5366 } else if (create && PageUptodate(page)) {
6bf7e080 5367 BUG();
179e29e4
CM
5368 if (!trans) {
5369 kunmap(page);
5370 free_extent_map(em);
5371 em = NULL;
ff5714cc 5372
b3b4aa74 5373 btrfs_release_path(path);
7a7eaa40 5374 trans = btrfs_join_transaction(root);
ff5714cc 5375
3612b495
TI
5376 if (IS_ERR(trans))
5377 return ERR_CAST(trans);
179e29e4
CM
5378 goto again;
5379 }
c8b97818 5380 map = kmap(page);
70dec807 5381 write_extent_buffer(leaf, map + pg_offset, ptr,
179e29e4 5382 copy_size);
c8b97818 5383 kunmap(page);
179e29e4 5384 btrfs_mark_buffer_dirty(leaf);
a52d9a80 5385 }
d1310b2e 5386 set_extent_uptodate(io_tree, em->start,
507903b8 5387 extent_map_end(em) - 1, NULL, GFP_NOFS);
a52d9a80
CM
5388 goto insert;
5389 } else {
d397712b 5390 printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
a52d9a80
CM
5391 WARN_ON(1);
5392 }
5393not_found:
5394 em->start = start;
d1310b2e 5395 em->len = len;
a52d9a80 5396not_found_em:
5f39d397 5397 em->block_start = EXTENT_MAP_HOLE;
9036c102 5398 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
a52d9a80 5399insert:
b3b4aa74 5400 btrfs_release_path(path);
d1310b2e 5401 if (em->start > start || extent_map_end(em) <= start) {
d397712b
CM
5402 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
5403 "[%llu %llu]\n", (unsigned long long)em->start,
5404 (unsigned long long)em->len,
5405 (unsigned long long)start,
5406 (unsigned long long)len);
a52d9a80
CM
5407 err = -EIO;
5408 goto out;
5409 }
d1310b2e
CM
5410
5411 err = 0;
890871be 5412 write_lock(&em_tree->lock);
a52d9a80 5413 ret = add_extent_mapping(em_tree, em);
3b951516
CM
5414 /* it is possible that someone inserted the extent into the tree
5415 * while we had the lock dropped. It is also possible that
5416 * an overlapping map exists in the tree
5417 */
a52d9a80 5418 if (ret == -EEXIST) {
3b951516 5419 struct extent_map *existing;
e6dcd2dc
CM
5420
5421 ret = 0;
5422
3b951516 5423 existing = lookup_extent_mapping(em_tree, start, len);
e1c4b745
CM
5424 if (existing && (existing->start > start ||
5425 existing->start + existing->len <= start)) {
5426 free_extent_map(existing);
5427 existing = NULL;
5428 }
3b951516
CM
5429 if (!existing) {
5430 existing = lookup_extent_mapping(em_tree, em->start,
5431 em->len);
5432 if (existing) {
5433 err = merge_extent_mapping(em_tree, existing,
e6dcd2dc
CM
5434 em, start,
5435 root->sectorsize);
3b951516
CM
5436 free_extent_map(existing);
5437 if (err) {
5438 free_extent_map(em);
5439 em = NULL;
5440 }
5441 } else {
5442 err = -EIO;
3b951516
CM
5443 free_extent_map(em);
5444 em = NULL;
5445 }
5446 } else {
5447 free_extent_map(em);
5448 em = existing;
e6dcd2dc 5449 err = 0;
a52d9a80 5450 }
a52d9a80 5451 }
890871be 5452 write_unlock(&em_tree->lock);
a52d9a80 5453out:
1abe9b8a 5454
5455 trace_btrfs_get_extent(root, em);
5456
f421950f
CM
5457 if (path)
5458 btrfs_free_path(path);
a52d9a80
CM
5459 if (trans) {
5460 ret = btrfs_end_transaction(trans, root);
d397712b 5461 if (!err)
a52d9a80
CM
5462 err = ret;
5463 }
a52d9a80
CM
5464 if (err) {
5465 free_extent_map(em);
a52d9a80
CM
5466 return ERR_PTR(err);
5467 }
79787eaa 5468 BUG_ON(!em); /* Error is always set */
a52d9a80
CM
5469 return em;
5470}
5471
ec29ed5b
CM
5472struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
5473 size_t pg_offset, u64 start, u64 len,
5474 int create)
5475{
5476 struct extent_map *em;
5477 struct extent_map *hole_em = NULL;
5478 u64 range_start = start;
5479 u64 end;
5480 u64 found;
5481 u64 found_end;
5482 int err = 0;
5483
5484 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
5485 if (IS_ERR(em))
5486 return em;
5487 if (em) {
5488 /*
5489 * if our em maps to a hole, there might
5490 * actually be delalloc bytes behind it
5491 */
5492 if (em->block_start != EXTENT_MAP_HOLE)
5493 return em;
5494 else
5495 hole_em = em;
5496 }
5497
5498 /* check to see if we've wrapped (len == -1 or similar) */
5499 end = start + len;
5500 if (end < start)
5501 end = (u64)-1;
5502 else
5503 end -= 1;
5504
5505 em = NULL;
5506
5507 /* ok, we didn't find anything, lets look for delalloc */
5508 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
5509 end, len, EXTENT_DELALLOC, 1);
5510 found_end = range_start + found;
5511 if (found_end < range_start)
5512 found_end = (u64)-1;
5513
5514 /*
5515 * we didn't find anything useful, return
5516 * the original results from get_extent()
5517 */
5518 if (range_start > end || found_end <= start) {
5519 em = hole_em;
5520 hole_em = NULL;
5521 goto out;
5522 }
5523
5524 /* adjust the range_start to make sure it doesn't
5525 * go backwards from the start they passed in
5526 */
5527 range_start = max(start,range_start);
5528 found = found_end - range_start;
5529
5530 if (found > 0) {
5531 u64 hole_start = start;
5532 u64 hole_len = len;
5533
172ddd60 5534 em = alloc_extent_map();
ec29ed5b
CM
5535 if (!em) {
5536 err = -ENOMEM;
5537 goto out;
5538 }
5539 /*
5540 * when btrfs_get_extent can't find anything it
5541 * returns one huge hole
5542 *
5543 * make sure what it found really fits our range, and
5544 * adjust to make sure it is based on the start from
5545 * the caller
5546 */
5547 if (hole_em) {
5548 u64 calc_end = extent_map_end(hole_em);
5549
5550 if (calc_end <= start || (hole_em->start > end)) {
5551 free_extent_map(hole_em);
5552 hole_em = NULL;
5553 } else {
5554 hole_start = max(hole_em->start, start);
5555 hole_len = calc_end - hole_start;
5556 }
5557 }
5558 em->bdev = NULL;
5559 if (hole_em && range_start > hole_start) {
5560 /* our hole starts before our delalloc, so we
5561 * have to return just the parts of the hole
5562 * that go until the delalloc starts
5563 */
5564 em->len = min(hole_len,
5565 range_start - hole_start);
5566 em->start = hole_start;
5567 em->orig_start = hole_start;
5568 /*
5569 * don't adjust block start at all,
5570 * it is fixed at EXTENT_MAP_HOLE
5571 */
5572 em->block_start = hole_em->block_start;
5573 em->block_len = hole_len;
5574 } else {
5575 em->start = range_start;
5576 em->len = found;
5577 em->orig_start = range_start;
5578 em->block_start = EXTENT_MAP_DELALLOC;
5579 em->block_len = found;
5580 }
5581 } else if (hole_em) {
5582 return hole_em;
5583 }
5584out:
5585
5586 free_extent_map(hole_em);
5587 if (err) {
5588 free_extent_map(em);
5589 return ERR_PTR(err);
5590 }
5591 return em;
5592}
5593
4b46fce2 5594static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
16d299ac 5595 struct extent_map *em,
4b46fce2
JB
5596 u64 start, u64 len)
5597{
5598 struct btrfs_root *root = BTRFS_I(inode)->root;
5599 struct btrfs_trans_handle *trans;
4b46fce2
JB
5600 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
5601 struct btrfs_key ins;
5602 u64 alloc_hint;
5603 int ret;
16d299ac 5604 bool insert = false;
4b46fce2 5605
16d299ac
JB
5606 /*
5607 * Ok if the extent map we looked up is a hole and is for the exact
5608 * range we want, there is no reason to allocate a new one, however if
5609 * it is not right then we need to free this one and drop the cache for
5610 * our range.
5611 */
5612 if (em->block_start != EXTENT_MAP_HOLE || em->start != start ||
5613 em->len != len) {
5614 free_extent_map(em);
5615 em = NULL;
5616 insert = true;
5617 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
5618 }
4b46fce2 5619
7a7eaa40 5620 trans = btrfs_join_transaction(root);
3612b495
TI
5621 if (IS_ERR(trans))
5622 return ERR_CAST(trans);
4b46fce2 5623
4cb5300b
CM
5624 if (start <= BTRFS_I(inode)->disk_i_size && len < 64 * 1024)
5625 btrfs_add_inode_defrag(trans, inode);
5626
4b46fce2
JB
5627 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5628
5629 alloc_hint = get_extent_allocation_hint(inode, start, len);
5630 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
81c9ad23 5631 alloc_hint, &ins, 1);
4b46fce2
JB
5632 if (ret) {
5633 em = ERR_PTR(ret);
5634 goto out;
5635 }
5636
4b46fce2 5637 if (!em) {
172ddd60 5638 em = alloc_extent_map();
16d299ac
JB
5639 if (!em) {
5640 em = ERR_PTR(-ENOMEM);
5641 goto out;
5642 }
4b46fce2
JB
5643 }
5644
5645 em->start = start;
5646 em->orig_start = em->start;
5647 em->len = ins.offset;
5648
5649 em->block_start = ins.objectid;
5650 em->block_len = ins.offset;
5651 em->bdev = root->fs_info->fs_devices->latest_bdev;
16d299ac
JB
5652
5653 /*
5654 * We need to do this because if we're using the original em we searched
5655 * for, we could have EXTENT_FLAG_VACANCY set, and we don't want that.
5656 */
5657 em->flags = 0;
4b46fce2
JB
5658 set_bit(EXTENT_FLAG_PINNED, &em->flags);
5659
16d299ac 5660 while (insert) {
4b46fce2
JB
5661 write_lock(&em_tree->lock);
5662 ret = add_extent_mapping(em_tree, em);
5663 write_unlock(&em_tree->lock);
5664 if (ret != -EEXIST)
5665 break;
5666 btrfs_drop_extent_cache(inode, start, start + em->len - 1, 0);
5667 }
5668
5669 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
5670 ins.offset, ins.offset, 0);
5671 if (ret) {
5672 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
5673 em = ERR_PTR(ret);
5674 }
5675out:
5676 btrfs_end_transaction(trans, root);
5677 return em;
5678}
5679
46bfbb5c
CM
5680/*
5681 * returns 1 when the nocow is safe, < 1 on error, 0 if the
5682 * block must be cow'd
5683 */
5684static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
5685 struct inode *inode, u64 offset, u64 len)
5686{
5687 struct btrfs_path *path;
5688 int ret;
5689 struct extent_buffer *leaf;
5690 struct btrfs_root *root = BTRFS_I(inode)->root;
5691 struct btrfs_file_extent_item *fi;
5692 struct btrfs_key key;
5693 u64 disk_bytenr;
5694 u64 backref_offset;
5695 u64 extent_end;
5696 u64 num_bytes;
5697 int slot;
5698 int found_type;
5699
5700 path = btrfs_alloc_path();
5701 if (!path)
5702 return -ENOMEM;
5703
33345d01 5704 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
46bfbb5c
CM
5705 offset, 0);
5706 if (ret < 0)
5707 goto out;
5708
5709 slot = path->slots[0];
5710 if (ret == 1) {
5711 if (slot == 0) {
5712 /* can't find the item, must cow */
5713 ret = 0;
5714 goto out;
5715 }
5716 slot--;
5717 }
5718 ret = 0;
5719 leaf = path->nodes[0];
5720 btrfs_item_key_to_cpu(leaf, &key, slot);
33345d01 5721 if (key.objectid != btrfs_ino(inode) ||
46bfbb5c
CM
5722 key.type != BTRFS_EXTENT_DATA_KEY) {
5723 /* not our file or wrong item type, must cow */
5724 goto out;
5725 }
5726
5727 if (key.offset > offset) {
5728 /* Wrong offset, must cow */
5729 goto out;
5730 }
5731
5732 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5733 found_type = btrfs_file_extent_type(leaf, fi);
5734 if (found_type != BTRFS_FILE_EXTENT_REG &&
5735 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
5736 /* not a regular extent, must cow */
5737 goto out;
5738 }
5739 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
5740 backref_offset = btrfs_file_extent_offset(leaf, fi);
5741
5742 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
5743 if (extent_end < offset + len) {
5744 /* extent doesn't include our full range, must cow */
5745 goto out;
5746 }
5747
5748 if (btrfs_extent_readonly(root, disk_bytenr))
5749 goto out;
5750
5751 /*
5752 * look for other files referencing this extent, if we
5753 * find any we must cow
5754 */
33345d01 5755 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
46bfbb5c
CM
5756 key.offset - backref_offset, disk_bytenr))
5757 goto out;
5758
5759 /*
5760 * adjust disk_bytenr and num_bytes to cover just the bytes
5761 * in this extent we are about to write. If there
5762 * are any csums in that range we have to cow in order
5763 * to keep the csums correct
5764 */
5765 disk_bytenr += backref_offset;
5766 disk_bytenr += offset - key.offset;
5767 num_bytes = min(offset + len, extent_end) - offset;
5768 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
5769 goto out;
5770 /*
5771 * all of the above have passed, it is safe to overwrite this extent
5772 * without cow
5773 */
5774 ret = 1;
5775out:
5776 btrfs_free_path(path);
5777 return ret;
5778}
5779
eb838e73
JB
5780static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
5781 struct extent_state **cached_state, int writing)
5782{
5783 struct btrfs_ordered_extent *ordered;
5784 int ret = 0;
5785
5786 while (1) {
5787 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
5788 0, cached_state);
5789 /*
5790 * We're concerned with the entire range that we're going to be
5791 * doing DIO to, so we need to make sure theres no ordered
5792 * extents in this range.
5793 */
5794 ordered = btrfs_lookup_ordered_range(inode, lockstart,
5795 lockend - lockstart + 1);
5796
5797 /*
5798 * We need to make sure there are no buffered pages in this
5799 * range either, we could have raced between the invalidate in
5800 * generic_file_direct_write and locking the extent. The
5801 * invalidate needs to happen so that reads after a write do not
5802 * get stale data.
5803 */
5804 if (!ordered && (!writing ||
5805 !test_range_bit(&BTRFS_I(inode)->io_tree,
5806 lockstart, lockend, EXTENT_UPTODATE, 0,
5807 *cached_state)))
5808 break;
5809
5810 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
5811 cached_state, GFP_NOFS);
5812
5813 if (ordered) {
5814 btrfs_start_ordered_extent(inode, ordered, 1);
5815 btrfs_put_ordered_extent(ordered);
5816 } else {
5817 /* Screw you mmap */
5818 ret = filemap_write_and_wait_range(inode->i_mapping,
5819 lockstart,
5820 lockend);
5821 if (ret)
5822 break;
5823
5824 /*
5825 * If we found a page that couldn't be invalidated just
5826 * fall back to buffered.
5827 */
5828 ret = invalidate_inode_pages2_range(inode->i_mapping,
5829 lockstart >> PAGE_CACHE_SHIFT,
5830 lockend >> PAGE_CACHE_SHIFT);
5831 if (ret)
5832 break;
5833 }
5834
5835 cond_resched();
5836 }
5837
5838 return ret;
5839}
5840
4b46fce2
JB
5841static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
5842 struct buffer_head *bh_result, int create)
5843{
5844 struct extent_map *em;
5845 struct btrfs_root *root = BTRFS_I(inode)->root;
eb838e73 5846 struct extent_state *cached_state = NULL;
4b46fce2 5847 u64 start = iblock << inode->i_blkbits;
eb838e73 5848 u64 lockstart, lockend;
4b46fce2 5849 u64 len = bh_result->b_size;
46bfbb5c 5850 struct btrfs_trans_handle *trans;
eb838e73
JB
5851 int unlock_bits = EXTENT_LOCKED;
5852 int ret;
5853
eb838e73
JB
5854 if (create) {
5855 ret = btrfs_delalloc_reserve_space(inode, len);
5856 if (ret)
5857 return ret;
5858 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
c329861d
JB
5859 } else {
5860 len = min_t(u64, len, root->sectorsize);
eb838e73
JB
5861 }
5862
c329861d
JB
5863 lockstart = start;
5864 lockend = start + len - 1;
5865
eb838e73
JB
5866 /*
5867 * If this errors out it's because we couldn't invalidate pagecache for
5868 * this range and we need to fallback to buffered.
5869 */
5870 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
5871 return -ENOTBLK;
5872
5873 if (create) {
5874 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
5875 lockend, EXTENT_DELALLOC, NULL,
5876 &cached_state, GFP_NOFS);
5877 if (ret)
5878 goto unlock_err;
5879 }
4b46fce2
JB
5880
5881 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
eb838e73
JB
5882 if (IS_ERR(em)) {
5883 ret = PTR_ERR(em);
5884 goto unlock_err;
5885 }
4b46fce2
JB
5886
5887 /*
5888 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
5889 * io. INLINE is special, and we could probably kludge it in here, but
5890 * it's still buffered so for safety lets just fall back to the generic
5891 * buffered path.
5892 *
5893 * For COMPRESSED we _have_ to read the entire extent in so we can
5894 * decompress it, so there will be buffering required no matter what we
5895 * do, so go ahead and fallback to buffered.
5896 *
5897 * We return -ENOTBLK because thats what makes DIO go ahead and go back
5898 * to buffered IO. Don't blame me, this is the price we pay for using
5899 * the generic code.
5900 */
5901 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
5902 em->block_start == EXTENT_MAP_INLINE) {
5903 free_extent_map(em);
eb838e73
JB
5904 ret = -ENOTBLK;
5905 goto unlock_err;
4b46fce2
JB
5906 }
5907
5908 /* Just a good old fashioned hole, return */
5909 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
5910 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
5911 free_extent_map(em);
eb838e73
JB
5912 ret = 0;
5913 goto unlock_err;
4b46fce2
JB
5914 }
5915
5916 /*
5917 * We don't allocate a new extent in the following cases
5918 *
5919 * 1) The inode is marked as NODATACOW. In this case we'll just use the
5920 * existing extent.
5921 * 2) The extent is marked as PREALLOC. We're good to go here and can
5922 * just use the extent.
5923 *
5924 */
46bfbb5c 5925 if (!create) {
eb838e73
JB
5926 len = min(len, em->len - (start - em->start));
5927 lockstart = start + len;
5928 goto unlock;
46bfbb5c 5929 }
4b46fce2
JB
5930
5931 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
5932 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
5933 em->block_start != EXTENT_MAP_HOLE)) {
4b46fce2
JB
5934 int type;
5935 int ret;
46bfbb5c 5936 u64 block_start;
4b46fce2
JB
5937
5938 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5939 type = BTRFS_ORDERED_PREALLOC;
5940 else
5941 type = BTRFS_ORDERED_NOCOW;
46bfbb5c 5942 len = min(len, em->len - (start - em->start));
4b46fce2 5943 block_start = em->block_start + (start - em->start);
46bfbb5c
CM
5944
5945 /*
5946 * we're not going to log anything, but we do need
5947 * to make sure the current transaction stays open
5948 * while we look for nocow cross refs
5949 */
7a7eaa40 5950 trans = btrfs_join_transaction(root);
3612b495 5951 if (IS_ERR(trans))
46bfbb5c
CM
5952 goto must_cow;
5953
5954 if (can_nocow_odirect(trans, inode, start, len) == 1) {
5955 ret = btrfs_add_ordered_extent_dio(inode, start,
5956 block_start, len, len, type);
5957 btrfs_end_transaction(trans, root);
5958 if (ret) {
5959 free_extent_map(em);
eb838e73 5960 goto unlock_err;
46bfbb5c
CM
5961 }
5962 goto unlock;
4b46fce2 5963 }
46bfbb5c 5964 btrfs_end_transaction(trans, root);
4b46fce2 5965 }
46bfbb5c
CM
5966must_cow:
5967 /*
5968 * this will cow the extent, reset the len in case we changed
5969 * it above
5970 */
5971 len = bh_result->b_size;
16d299ac 5972 em = btrfs_new_extent_direct(inode, em, start, len);
eb838e73
JB
5973 if (IS_ERR(em)) {
5974 ret = PTR_ERR(em);
5975 goto unlock_err;
5976 }
46bfbb5c
CM
5977 len = min(len, em->len - (start - em->start));
5978unlock:
4b46fce2
JB
5979 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
5980 inode->i_blkbits;
46bfbb5c 5981 bh_result->b_size = len;
4b46fce2
JB
5982 bh_result->b_bdev = em->bdev;
5983 set_buffer_mapped(bh_result);
c3473e83
JB
5984 if (create) {
5985 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5986 set_buffer_new(bh_result);
5987
5988 /*
5989 * Need to update the i_size under the extent lock so buffered
5990 * readers will get the updated i_size when we unlock.
5991 */
5992 if (start + len > i_size_read(inode))
5993 i_size_write(inode, start + len);
5994 }
4b46fce2 5995
eb838e73
JB
5996 /*
5997 * In the case of write we need to clear and unlock the entire range,
5998 * in the case of read we need to unlock only the end area that we
5999 * aren't using if there is any left over space.
6000 */
24c03fa5
LB
6001 if (lockstart < lockend) {
6002 if (create && len < lockend - lockstart) {
6003 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6004 lockstart + len - 1, unlock_bits, 1, 0,
6005 &cached_state, GFP_NOFS);
6006 /*
6007 * Beside unlock, we also need to cleanup reserved space
6008 * for the left range by attaching EXTENT_DO_ACCOUNTING.
6009 */
6010 clear_extent_bit(&BTRFS_I(inode)->io_tree,
6011 lockstart + len, lockend,
6012 unlock_bits | EXTENT_DO_ACCOUNTING,
6013 1, 0, NULL, GFP_NOFS);
6014 } else {
6015 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6016 lockend, unlock_bits, 1, 0,
6017 &cached_state, GFP_NOFS);
6018 }
6019 } else {
eb838e73 6020 free_extent_state(cached_state);
24c03fa5 6021 }
eb838e73 6022
4b46fce2
JB
6023 free_extent_map(em);
6024
6025 return 0;
eb838e73
JB
6026
6027unlock_err:
6028 if (create)
6029 unlock_bits |= EXTENT_DO_ACCOUNTING;
6030
6031 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6032 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
6033 return ret;
4b46fce2
JB
6034}
6035
6036struct btrfs_dio_private {
6037 struct inode *inode;
6038 u64 logical_offset;
6039 u64 disk_bytenr;
6040 u64 bytes;
4b46fce2 6041 void *private;
e65e1535
MX
6042
6043 /* number of bios pending for this dio */
6044 atomic_t pending_bios;
6045
6046 /* IO errors */
6047 int errors;
6048
6049 struct bio *orig_bio;
4b46fce2
JB
6050};
6051
6052static void btrfs_endio_direct_read(struct bio *bio, int err)
6053{
e65e1535 6054 struct btrfs_dio_private *dip = bio->bi_private;
4b46fce2
JB
6055 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
6056 struct bio_vec *bvec = bio->bi_io_vec;
4b46fce2
JB
6057 struct inode *inode = dip->inode;
6058 struct btrfs_root *root = BTRFS_I(inode)->root;
6059 u64 start;
4b46fce2
JB
6060
6061 start = dip->logical_offset;
6062 do {
6063 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
6064 struct page *page = bvec->bv_page;
6065 char *kaddr;
6066 u32 csum = ~(u32)0;
c329861d 6067 u64 private = ~(u32)0;
4b46fce2
JB
6068 unsigned long flags;
6069
c329861d
JB
6070 if (get_state_private(&BTRFS_I(inode)->io_tree,
6071 start, &private))
6072 goto failed;
4b46fce2 6073 local_irq_save(flags);
7ac687d9 6074 kaddr = kmap_atomic(page);
4b46fce2
JB
6075 csum = btrfs_csum_data(root, kaddr + bvec->bv_offset,
6076 csum, bvec->bv_len);
6077 btrfs_csum_final(csum, (char *)&csum);
7ac687d9 6078 kunmap_atomic(kaddr);
4b46fce2
JB
6079 local_irq_restore(flags);
6080
6081 flush_dcache_page(bvec->bv_page);
c329861d
JB
6082 if (csum != private) {
6083failed:
33345d01 6084 printk(KERN_ERR "btrfs csum failed ino %llu off"
4b46fce2 6085 " %llu csum %u private %u\n",
33345d01
LZ
6086 (unsigned long long)btrfs_ino(inode),
6087 (unsigned long long)start,
c329861d 6088 csum, (unsigned)private);
4b46fce2
JB
6089 err = -EIO;
6090 }
6091 }
6092
6093 start += bvec->bv_len;
4b46fce2
JB
6094 bvec++;
6095 } while (bvec <= bvec_end);
6096
6097 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
d0082371 6098 dip->logical_offset + dip->bytes - 1);
4b46fce2
JB
6099 bio->bi_private = dip->private;
6100
4b46fce2 6101 kfree(dip);
c0da7aa1
JB
6102
6103 /* If we had a csum failure make sure to clear the uptodate flag */
6104 if (err)
6105 clear_bit(BIO_UPTODATE, &bio->bi_flags);
4b46fce2
JB
6106 dio_end_io(bio, err);
6107}
6108
6109static void btrfs_endio_direct_write(struct bio *bio, int err)
6110{
6111 struct btrfs_dio_private *dip = bio->bi_private;
6112 struct inode *inode = dip->inode;
6113 struct btrfs_root *root = BTRFS_I(inode)->root;
4b46fce2 6114 struct btrfs_ordered_extent *ordered = NULL;
163cf09c
CM
6115 u64 ordered_offset = dip->logical_offset;
6116 u64 ordered_bytes = dip->bytes;
4b46fce2
JB
6117 int ret;
6118
6119 if (err)
6120 goto out_done;
163cf09c
CM
6121again:
6122 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
6123 &ordered_offset,
5fd02043 6124 ordered_bytes, !err);
4b46fce2 6125 if (!ret)
163cf09c 6126 goto out_test;
4b46fce2 6127
5fd02043
JB
6128 ordered->work.func = finish_ordered_fn;
6129 ordered->work.flags = 0;
6130 btrfs_queue_worker(&root->fs_info->endio_write_workers,
6131 &ordered->work);
163cf09c
CM
6132out_test:
6133 /*
6134 * our bio might span multiple ordered extents. If we haven't
6135 * completed the accounting for the whole dio, go back and try again
6136 */
6137 if (ordered_offset < dip->logical_offset + dip->bytes) {
6138 ordered_bytes = dip->logical_offset + dip->bytes -
6139 ordered_offset;
5fd02043 6140 ordered = NULL;
163cf09c
CM
6141 goto again;
6142 }
4b46fce2
JB
6143out_done:
6144 bio->bi_private = dip->private;
6145
4b46fce2 6146 kfree(dip);
c0da7aa1
JB
6147
6148 /* If we had an error make sure to clear the uptodate flag */
6149 if (err)
6150 clear_bit(BIO_UPTODATE, &bio->bi_flags);
4b46fce2
JB
6151 dio_end_io(bio, err);
6152}
6153
eaf25d93
CM
6154static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
6155 struct bio *bio, int mirror_num,
6156 unsigned long bio_flags, u64 offset)
6157{
6158 int ret;
6159 struct btrfs_root *root = BTRFS_I(inode)->root;
6160 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
79787eaa 6161 BUG_ON(ret); /* -ENOMEM */
eaf25d93
CM
6162 return 0;
6163}
6164
e65e1535
MX
6165static void btrfs_end_dio_bio(struct bio *bio, int err)
6166{
6167 struct btrfs_dio_private *dip = bio->bi_private;
6168
6169 if (err) {
33345d01 6170 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
3dd1462e 6171 "sector %#Lx len %u err no %d\n",
33345d01 6172 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
3dd1462e 6173 (unsigned long long)bio->bi_sector, bio->bi_size, err);
e65e1535
MX
6174 dip->errors = 1;
6175
6176 /*
6177 * before atomic variable goto zero, we must make sure
6178 * dip->errors is perceived to be set.
6179 */
6180 smp_mb__before_atomic_dec();
6181 }
6182
6183 /* if there are more bios still pending for this dio, just exit */
6184 if (!atomic_dec_and_test(&dip->pending_bios))
6185 goto out;
6186
6187 if (dip->errors)
6188 bio_io_error(dip->orig_bio);
6189 else {
6190 set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags);
6191 bio_endio(dip->orig_bio, 0);
6192 }
6193out:
6194 bio_put(bio);
6195}
6196
6197static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
6198 u64 first_sector, gfp_t gfp_flags)
6199{
6200 int nr_vecs = bio_get_nr_vecs(bdev);
6201 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
6202}
6203
6204static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
6205 int rw, u64 file_offset, int skip_sum,
c329861d 6206 int async_submit)
e65e1535
MX
6207{
6208 int write = rw & REQ_WRITE;
6209 struct btrfs_root *root = BTRFS_I(inode)->root;
6210 int ret;
6211
6212 bio_get(bio);
5fd02043
JB
6213
6214 if (!write) {
6215 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
6216 if (ret)
6217 goto err;
6218 }
e65e1535 6219
1ae39938
JB
6220 if (skip_sum)
6221 goto map;
6222
6223 if (write && async_submit) {
e65e1535
MX
6224 ret = btrfs_wq_submit_bio(root->fs_info,
6225 inode, rw, bio, 0, 0,
6226 file_offset,
6227 __btrfs_submit_bio_start_direct_io,
6228 __btrfs_submit_bio_done);
6229 goto err;
1ae39938
JB
6230 } else if (write) {
6231 /*
6232 * If we aren't doing async submit, calculate the csum of the
6233 * bio now.
6234 */
6235 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
6236 if (ret)
6237 goto err;
c2db1073 6238 } else if (!skip_sum) {
c329861d 6239 ret = btrfs_lookup_bio_sums_dio(root, inode, bio, file_offset);
c2db1073
TI
6240 if (ret)
6241 goto err;
6242 }
e65e1535 6243
1ae39938
JB
6244map:
6245 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
e65e1535
MX
6246err:
6247 bio_put(bio);
6248 return ret;
6249}
6250
6251static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
6252 int skip_sum)
6253{
6254 struct inode *inode = dip->inode;
6255 struct btrfs_root *root = BTRFS_I(inode)->root;
6256 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
6257 struct bio *bio;
6258 struct bio *orig_bio = dip->orig_bio;
6259 struct bio_vec *bvec = orig_bio->bi_io_vec;
6260 u64 start_sector = orig_bio->bi_sector;
6261 u64 file_offset = dip->logical_offset;
6262 u64 submit_len = 0;
6263 u64 map_length;
6264 int nr_pages = 0;
e65e1535 6265 int ret = 0;
1ae39938 6266 int async_submit = 0;
e65e1535 6267
e65e1535
MX
6268 map_length = orig_bio->bi_size;
6269 ret = btrfs_map_block(map_tree, READ, start_sector << 9,
6270 &map_length, NULL, 0);
6271 if (ret) {
64728bbb 6272 bio_put(orig_bio);
e65e1535
MX
6273 return -EIO;
6274 }
6275
02f57c7a
JB
6276 if (map_length >= orig_bio->bi_size) {
6277 bio = orig_bio;
6278 goto submit;
6279 }
6280
1ae39938 6281 async_submit = 1;
02f57c7a
JB
6282 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
6283 if (!bio)
6284 return -ENOMEM;
6285 bio->bi_private = dip;
6286 bio->bi_end_io = btrfs_end_dio_bio;
6287 atomic_inc(&dip->pending_bios);
6288
e65e1535
MX
6289 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
6290 if (unlikely(map_length < submit_len + bvec->bv_len ||
6291 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
6292 bvec->bv_offset) < bvec->bv_len)) {
6293 /*
6294 * inc the count before we submit the bio so
6295 * we know the end IO handler won't happen before
6296 * we inc the count. Otherwise, the dip might get freed
6297 * before we're done setting it up
6298 */
6299 atomic_inc(&dip->pending_bios);
6300 ret = __btrfs_submit_dio_bio(bio, inode, rw,
6301 file_offset, skip_sum,
c329861d 6302 async_submit);
e65e1535
MX
6303 if (ret) {
6304 bio_put(bio);
6305 atomic_dec(&dip->pending_bios);
6306 goto out_err;
6307 }
6308
e65e1535
MX
6309 start_sector += submit_len >> 9;
6310 file_offset += submit_len;
6311
6312 submit_len = 0;
6313 nr_pages = 0;
6314
6315 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
6316 start_sector, GFP_NOFS);
6317 if (!bio)
6318 goto out_err;
6319 bio->bi_private = dip;
6320 bio->bi_end_io = btrfs_end_dio_bio;
6321
6322 map_length = orig_bio->bi_size;
6323 ret = btrfs_map_block(map_tree, READ, start_sector << 9,
6324 &map_length, NULL, 0);
6325 if (ret) {
6326 bio_put(bio);
6327 goto out_err;
6328 }
6329 } else {
6330 submit_len += bvec->bv_len;
6331 nr_pages ++;
6332 bvec++;
6333 }
6334 }
6335
02f57c7a 6336submit:
e65e1535 6337 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
c329861d 6338 async_submit);
e65e1535
MX
6339 if (!ret)
6340 return 0;
6341
6342 bio_put(bio);
6343out_err:
6344 dip->errors = 1;
6345 /*
6346 * before atomic variable goto zero, we must
6347 * make sure dip->errors is perceived to be set.
6348 */
6349 smp_mb__before_atomic_dec();
6350 if (atomic_dec_and_test(&dip->pending_bios))
6351 bio_io_error(dip->orig_bio);
6352
6353 /* bio_end_io() will handle error, so we needn't return it */
6354 return 0;
6355}
6356
4b46fce2
JB
6357static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
6358 loff_t file_offset)
6359{
6360 struct btrfs_root *root = BTRFS_I(inode)->root;
6361 struct btrfs_dio_private *dip;
6362 struct bio_vec *bvec = bio->bi_io_vec;
4b46fce2 6363 int skip_sum;
7b6d91da 6364 int write = rw & REQ_WRITE;
4b46fce2
JB
6365 int ret = 0;
6366
6367 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
6368
6369 dip = kmalloc(sizeof(*dip), GFP_NOFS);
6370 if (!dip) {
6371 ret = -ENOMEM;
6372 goto free_ordered;
6373 }
4b46fce2
JB
6374
6375 dip->private = bio->bi_private;
6376 dip->inode = inode;
6377 dip->logical_offset = file_offset;
6378
4b46fce2
JB
6379 dip->bytes = 0;
6380 do {
6381 dip->bytes += bvec->bv_len;
6382 bvec++;
6383 } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
6384
46bfbb5c 6385 dip->disk_bytenr = (u64)bio->bi_sector << 9;
4b46fce2 6386 bio->bi_private = dip;
e65e1535
MX
6387 dip->errors = 0;
6388 dip->orig_bio = bio;
6389 atomic_set(&dip->pending_bios, 0);
4b46fce2
JB
6390
6391 if (write)
6392 bio->bi_end_io = btrfs_endio_direct_write;
6393 else
6394 bio->bi_end_io = btrfs_endio_direct_read;
6395
e65e1535
MX
6396 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
6397 if (!ret)
eaf25d93 6398 return;
4b46fce2
JB
6399free_ordered:
6400 /*
6401 * If this is a write, we need to clean up the reserved space and kill
6402 * the ordered extent.
6403 */
6404 if (write) {
6405 struct btrfs_ordered_extent *ordered;
955256f2 6406 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
4b46fce2
JB
6407 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
6408 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
6409 btrfs_free_reserved_extent(root, ordered->start,
6410 ordered->disk_len);
6411 btrfs_put_ordered_extent(ordered);
6412 btrfs_put_ordered_extent(ordered);
6413 }
6414 bio_endio(bio, ret);
6415}
6416
5a5f79b5
CM
6417static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
6418 const struct iovec *iov, loff_t offset,
6419 unsigned long nr_segs)
6420{
6421 int seg;
a1b75f7d 6422 int i;
5a5f79b5
CM
6423 size_t size;
6424 unsigned long addr;
6425 unsigned blocksize_mask = root->sectorsize - 1;
6426 ssize_t retval = -EINVAL;
6427 loff_t end = offset;
6428
6429 if (offset & blocksize_mask)
6430 goto out;
6431
6432 /* Check the memory alignment. Blocks cannot straddle pages */
6433 for (seg = 0; seg < nr_segs; seg++) {
6434 addr = (unsigned long)iov[seg].iov_base;
6435 size = iov[seg].iov_len;
6436 end += size;
a1b75f7d 6437 if ((addr & blocksize_mask) || (size & blocksize_mask))
5a5f79b5 6438 goto out;
a1b75f7d
JB
6439
6440 /* If this is a write we don't need to check anymore */
6441 if (rw & WRITE)
6442 continue;
6443
6444 /*
6445 * Check to make sure we don't have duplicate iov_base's in this
6446 * iovec, if so return EINVAL, otherwise we'll get csum errors
6447 * when reading back.
6448 */
6449 for (i = seg + 1; i < nr_segs; i++) {
6450 if (iov[seg].iov_base == iov[i].iov_base)
6451 goto out;
6452 }
5a5f79b5
CM
6453 }
6454 retval = 0;
6455out:
6456 return retval;
6457}
eb838e73 6458
16432985
CM
6459static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
6460 const struct iovec *iov, loff_t offset,
6461 unsigned long nr_segs)
6462{
4b46fce2
JB
6463 struct file *file = iocb->ki_filp;
6464 struct inode *inode = file->f_mapping->host;
4b46fce2 6465
5a5f79b5 6466 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
eb838e73 6467 offset, nr_segs))
5a5f79b5 6468 return 0;
3f7c579c 6469
eb838e73 6470 return __blockdev_direct_IO(rw, iocb, inode,
5a5f79b5
CM
6471 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
6472 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
6473 btrfs_submit_direct, 0);
16432985
CM
6474}
6475
1506fcc8
YS
6476static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
6477 __u64 start, __u64 len)
6478{
ec29ed5b 6479 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
1506fcc8
YS
6480}
6481
a52d9a80 6482int btrfs_readpage(struct file *file, struct page *page)
9ebefb18 6483{
d1310b2e
CM
6484 struct extent_io_tree *tree;
6485 tree = &BTRFS_I(page->mapping->host)->io_tree;
8ddc7d9c 6486 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
9ebefb18 6487}
1832a6d5 6488
a52d9a80 6489static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
39279cc3 6490{
d1310b2e 6491 struct extent_io_tree *tree;
b888db2b
CM
6492
6493
6494 if (current->flags & PF_MEMALLOC) {
6495 redirty_page_for_writepage(wbc, page);
6496 unlock_page(page);
6497 return 0;
6498 }
d1310b2e 6499 tree = &BTRFS_I(page->mapping->host)->io_tree;
a52d9a80 6500 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
9ebefb18
CM
6501}
6502
f421950f
CM
6503int btrfs_writepages(struct address_space *mapping,
6504 struct writeback_control *wbc)
b293f02e 6505{
d1310b2e 6506 struct extent_io_tree *tree;
771ed689 6507
d1310b2e 6508 tree = &BTRFS_I(mapping->host)->io_tree;
b293f02e
CM
6509 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
6510}
6511
3ab2fb5a
CM
6512static int
6513btrfs_readpages(struct file *file, struct address_space *mapping,
6514 struct list_head *pages, unsigned nr_pages)
6515{
d1310b2e
CM
6516 struct extent_io_tree *tree;
6517 tree = &BTRFS_I(mapping->host)->io_tree;
3ab2fb5a
CM
6518 return extent_readpages(tree, mapping, pages, nr_pages,
6519 btrfs_get_extent);
6520}
e6dcd2dc 6521static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
9ebefb18 6522{
d1310b2e
CM
6523 struct extent_io_tree *tree;
6524 struct extent_map_tree *map;
a52d9a80 6525 int ret;
8c2383c3 6526
d1310b2e
CM
6527 tree = &BTRFS_I(page->mapping->host)->io_tree;
6528 map = &BTRFS_I(page->mapping->host)->extent_tree;
70dec807 6529 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
a52d9a80
CM
6530 if (ret == 1) {
6531 ClearPagePrivate(page);
6532 set_page_private(page, 0);
6533 page_cache_release(page);
39279cc3 6534 }
a52d9a80 6535 return ret;
39279cc3
CM
6536}
6537
e6dcd2dc
CM
6538static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
6539{
98509cfc
CM
6540 if (PageWriteback(page) || PageDirty(page))
6541 return 0;
b335b003 6542 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
e6dcd2dc
CM
6543}
6544
a52d9a80 6545static void btrfs_invalidatepage(struct page *page, unsigned long offset)
39279cc3 6546{
5fd02043 6547 struct inode *inode = page->mapping->host;
d1310b2e 6548 struct extent_io_tree *tree;
e6dcd2dc 6549 struct btrfs_ordered_extent *ordered;
2ac55d41 6550 struct extent_state *cached_state = NULL;
e6dcd2dc
CM
6551 u64 page_start = page_offset(page);
6552 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
39279cc3 6553
8b62b72b
CM
6554 /*
6555 * we have the page locked, so new writeback can't start,
6556 * and the dirty bit won't be cleared while we are here.
6557 *
6558 * Wait for IO on this page so that we can safely clear
6559 * the PagePrivate2 bit and do ordered accounting
6560 */
e6dcd2dc 6561 wait_on_page_writeback(page);
8b62b72b 6562
5fd02043 6563 tree = &BTRFS_I(inode)->io_tree;
e6dcd2dc
CM
6564 if (offset) {
6565 btrfs_releasepage(page, GFP_NOFS);
6566 return;
6567 }
d0082371 6568 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
5fd02043 6569 ordered = btrfs_lookup_ordered_extent(inode,
e6dcd2dc
CM
6570 page_offset(page));
6571 if (ordered) {
eb84ae03
CM
6572 /*
6573 * IO on this page will never be started, so we need
6574 * to account for any ordered extents now
6575 */
e6dcd2dc
CM
6576 clear_extent_bit(tree, page_start, page_end,
6577 EXTENT_DIRTY | EXTENT_DELALLOC |
32c00aff 6578 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0,
2ac55d41 6579 &cached_state, GFP_NOFS);
8b62b72b
CM
6580 /*
6581 * whoever cleared the private bit is responsible
6582 * for the finish_ordered_io
6583 */
5fd02043
JB
6584 if (TestClearPagePrivate2(page) &&
6585 btrfs_dec_test_ordered_pending(inode, &ordered, page_start,
6586 PAGE_CACHE_SIZE, 1)) {
6587 btrfs_finish_ordered_io(ordered);
8b62b72b 6588 }
e6dcd2dc 6589 btrfs_put_ordered_extent(ordered);
2ac55d41 6590 cached_state = NULL;
d0082371 6591 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
e6dcd2dc
CM
6592 }
6593 clear_extent_bit(tree, page_start, page_end,
32c00aff 6594 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
2ac55d41 6595 EXTENT_DO_ACCOUNTING, 1, 1, &cached_state, GFP_NOFS);
e6dcd2dc
CM
6596 __btrfs_releasepage(page, GFP_NOFS);
6597
4a096752 6598 ClearPageChecked(page);
9ad6b7bc 6599 if (PagePrivate(page)) {
9ad6b7bc
CM
6600 ClearPagePrivate(page);
6601 set_page_private(page, 0);
6602 page_cache_release(page);
6603 }
39279cc3
CM
6604}
6605
9ebefb18
CM
6606/*
6607 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
6608 * called from a page fault handler when a page is first dirtied. Hence we must
6609 * be careful to check for EOF conditions here. We set the page up correctly
6610 * for a written page which means we get ENOSPC checking when writing into
6611 * holes and correct delalloc and unwritten extent mapping on filesystems that
6612 * support these features.
6613 *
6614 * We are not allowed to take the i_mutex here so we have to play games to
6615 * protect against truncate races as the page could now be beyond EOF. Because
6616 * vmtruncate() writes the inode size before removing pages, once we have the
6617 * page lock we can determine safely if the page is beyond EOF. If it is not
6618 * beyond EOF, then the page is guaranteed safe against truncation until we
6619 * unlock the page.
6620 */
c2ec175c 6621int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
9ebefb18 6622{
c2ec175c 6623 struct page *page = vmf->page;
6da6abae 6624 struct inode *inode = fdentry(vma->vm_file)->d_inode;
1832a6d5 6625 struct btrfs_root *root = BTRFS_I(inode)->root;
e6dcd2dc
CM
6626 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
6627 struct btrfs_ordered_extent *ordered;
2ac55d41 6628 struct extent_state *cached_state = NULL;
e6dcd2dc
CM
6629 char *kaddr;
6630 unsigned long zero_start;
9ebefb18 6631 loff_t size;
1832a6d5 6632 int ret;
9998eb70 6633 int reserved = 0;
a52d9a80 6634 u64 page_start;
e6dcd2dc 6635 u64 page_end;
9ebefb18 6636
b2b5ef5c 6637 sb_start_pagefault(inode->i_sb);
0ca1f7ce 6638 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
9998eb70 6639 if (!ret) {
e41f941a 6640 ret = file_update_time(vma->vm_file);
9998eb70
CM
6641 reserved = 1;
6642 }
56a76f82
NP
6643 if (ret) {
6644 if (ret == -ENOMEM)
6645 ret = VM_FAULT_OOM;
6646 else /* -ENOSPC, -EIO, etc */
6647 ret = VM_FAULT_SIGBUS;
9998eb70
CM
6648 if (reserved)
6649 goto out;
6650 goto out_noreserve;
56a76f82 6651 }
1832a6d5 6652
56a76f82 6653 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
e6dcd2dc 6654again:
9ebefb18 6655 lock_page(page);
9ebefb18 6656 size = i_size_read(inode);
e6dcd2dc
CM
6657 page_start = page_offset(page);
6658 page_end = page_start + PAGE_CACHE_SIZE - 1;
a52d9a80 6659
9ebefb18 6660 if ((page->mapping != inode->i_mapping) ||
e6dcd2dc 6661 (page_start >= size)) {
9ebefb18
CM
6662 /* page got truncated out from underneath us */
6663 goto out_unlock;
6664 }
e6dcd2dc
CM
6665 wait_on_page_writeback(page);
6666
d0082371 6667 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
e6dcd2dc
CM
6668 set_page_extent_mapped(page);
6669
eb84ae03
CM
6670 /*
6671 * we can't set the delalloc bits if there are pending ordered
6672 * extents. Drop our locks and wait for them to finish
6673 */
e6dcd2dc
CM
6674 ordered = btrfs_lookup_ordered_extent(inode, page_start);
6675 if (ordered) {
2ac55d41
JB
6676 unlock_extent_cached(io_tree, page_start, page_end,
6677 &cached_state, GFP_NOFS);
e6dcd2dc 6678 unlock_page(page);
eb84ae03 6679 btrfs_start_ordered_extent(inode, ordered, 1);
e6dcd2dc
CM
6680 btrfs_put_ordered_extent(ordered);
6681 goto again;
6682 }
6683
fbf19087
JB
6684 /*
6685 * XXX - page_mkwrite gets called every time the page is dirtied, even
6686 * if it was already dirty, so for space accounting reasons we need to
6687 * clear any delalloc bits for the range we are fixing to save. There
6688 * is probably a better way to do this, but for now keep consistent with
6689 * prepare_pages in the normal write path.
6690 */
2ac55d41 6691 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
32c00aff 6692 EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
2ac55d41 6693 0, 0, &cached_state, GFP_NOFS);
fbf19087 6694
2ac55d41
JB
6695 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
6696 &cached_state);
9ed74f2d 6697 if (ret) {
2ac55d41
JB
6698 unlock_extent_cached(io_tree, page_start, page_end,
6699 &cached_state, GFP_NOFS);
9ed74f2d
JB
6700 ret = VM_FAULT_SIGBUS;
6701 goto out_unlock;
6702 }
e6dcd2dc 6703 ret = 0;
9ebefb18
CM
6704
6705 /* page is wholly or partially inside EOF */
a52d9a80 6706 if (page_start + PAGE_CACHE_SIZE > size)
e6dcd2dc 6707 zero_start = size & ~PAGE_CACHE_MASK;
9ebefb18 6708 else
e6dcd2dc 6709 zero_start = PAGE_CACHE_SIZE;
9ebefb18 6710
e6dcd2dc
CM
6711 if (zero_start != PAGE_CACHE_SIZE) {
6712 kaddr = kmap(page);
6713 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
6714 flush_dcache_page(page);
6715 kunmap(page);
6716 }
247e743c 6717 ClearPageChecked(page);
e6dcd2dc 6718 set_page_dirty(page);
50a9b214 6719 SetPageUptodate(page);
5a3f23d5 6720
257c62e1
CM
6721 BTRFS_I(inode)->last_trans = root->fs_info->generation;
6722 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
6723
2ac55d41 6724 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
9ebefb18
CM
6725
6726out_unlock:
b2b5ef5c
JK
6727 if (!ret) {
6728 sb_end_pagefault(inode->i_sb);
50a9b214 6729 return VM_FAULT_LOCKED;
b2b5ef5c 6730 }
9ebefb18 6731 unlock_page(page);
1832a6d5 6732out:
ec39e180 6733 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
9998eb70 6734out_noreserve:
b2b5ef5c 6735 sb_end_pagefault(inode->i_sb);
9ebefb18
CM
6736 return ret;
6737}
6738
a41ad394 6739static int btrfs_truncate(struct inode *inode)
39279cc3
CM
6740{
6741 struct btrfs_root *root = BTRFS_I(inode)->root;
fcb80c2a 6742 struct btrfs_block_rsv *rsv;
39279cc3 6743 int ret;
3893e33b 6744 int err = 0;
39279cc3 6745 struct btrfs_trans_handle *trans;
d3c2fdcf 6746 unsigned long nr;
dbe674a9 6747 u64 mask = root->sectorsize - 1;
07127184 6748 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
39279cc3 6749
5d5e103a
JB
6750 ret = btrfs_truncate_page(inode->i_mapping, inode->i_size);
6751 if (ret)
a41ad394 6752 return ret;
8082510e 6753
4a096752 6754 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
8082510e 6755 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
39279cc3 6756
fcb80c2a
JB
6757 /*
6758 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
6759 * 3 things going on here
6760 *
6761 * 1) We need to reserve space for our orphan item and the space to
6762 * delete our orphan item. Lord knows we don't want to have a dangling
6763 * orphan item because we didn't reserve space to remove it.
6764 *
6765 * 2) We need to reserve space to update our inode.
6766 *
6767 * 3) We need to have something to cache all the space that is going to
6768 * be free'd up by the truncate operation, but also have some slack
6769 * space reserved in case it uses space during the truncate (thank you
6770 * very much snapshotting).
6771 *
6772 * And we need these to all be seperate. The fact is we can use alot of
6773 * space doing the truncate, and we have no earthly idea how much space
6774 * we will use, so we need the truncate reservation to be seperate so it
6775 * doesn't end up using space reserved for updating the inode or
6776 * removing the orphan item. We also need to be able to stop the
6777 * transaction and start a new one, which means we need to be able to
6778 * update the inode several times, and we have no idea of knowing how
6779 * many times that will be, so we can't just reserve 1 item for the
6780 * entirety of the opration, so that has to be done seperately as well.
6781 * Then there is the orphan item, which does indeed need to be held on
6782 * to for the whole operation, and we need nobody to touch this reserved
6783 * space except the orphan code.
6784 *
6785 * So that leaves us with
6786 *
6787 * 1) root->orphan_block_rsv - for the orphan deletion.
6788 * 2) rsv - for the truncate reservation, which we will steal from the
6789 * transaction reservation.
6790 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
6791 * updating the inode.
6792 */
6793 rsv = btrfs_alloc_block_rsv(root);
6794 if (!rsv)
6795 return -ENOMEM;
4a338542 6796 rsv->size = min_size;
f0cd846e 6797
907cbceb 6798 /*
07127184 6799 * 1 for the truncate slack space
907cbceb
JB
6800 * 1 for the orphan item we're going to add
6801 * 1 for the orphan item deletion
6802 * 1 for updating the inode.
6803 */
fcb80c2a
JB
6804 trans = btrfs_start_transaction(root, 4);
6805 if (IS_ERR(trans)) {
6806 err = PTR_ERR(trans);
6807 goto out;
6808 }
f0cd846e 6809
907cbceb
JB
6810 /* Migrate the slack space for the truncate to our reserve */
6811 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
6812 min_size);
fcb80c2a 6813 BUG_ON(ret);
f0cd846e
JB
6814
6815 ret = btrfs_orphan_add(trans, inode);
6816 if (ret) {
6817 btrfs_end_transaction(trans, root);
fcb80c2a 6818 goto out;
f0cd846e
JB
6819 }
6820
5a3f23d5
CM
6821 /*
6822 * setattr is responsible for setting the ordered_data_close flag,
6823 * but that is only tested during the last file release. That
6824 * could happen well after the next commit, leaving a great big
6825 * window where new writes may get lost if someone chooses to write
6826 * to this file after truncating to zero
6827 *
6828 * The inode doesn't have any dirty data here, and so if we commit
6829 * this is a noop. If someone immediately starts writing to the inode
6830 * it is very likely we'll catch some of their writes in this
6831 * transaction, and the commit will find this file on the ordered
6832 * data list with good things to send down.
6833 *
6834 * This is a best effort solution, there is still a window where
6835 * using truncate to replace the contents of the file will
6836 * end up with a zero length file after a crash.
6837 */
72ac3c0d
JB
6838 if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
6839 &BTRFS_I(inode)->runtime_flags))
5a3f23d5
CM
6840 btrfs_add_ordered_operation(trans, root, inode);
6841
8082510e 6842 while (1) {
36ba022a 6843 ret = btrfs_block_rsv_refill(root, rsv, min_size);
907cbceb
JB
6844 if (ret) {
6845 /*
6846 * This can only happen with the original transaction we
6847 * started above, every other time we shouldn't have a
6848 * transaction started yet.
6849 */
6850 if (ret == -EAGAIN)
6851 goto end_trans;
6852 err = ret;
6853 break;
6854 }
6855
d68fc57b 6856 if (!trans) {
907cbceb
JB
6857 /* Just need the 1 for updating the inode */
6858 trans = btrfs_start_transaction(root, 1);
fcb80c2a 6859 if (IS_ERR(trans)) {
7041ee97
JB
6860 ret = err = PTR_ERR(trans);
6861 trans = NULL;
6862 break;
fcb80c2a 6863 }
d68fc57b
YZ
6864 }
6865
907cbceb
JB
6866 trans->block_rsv = rsv;
6867
8082510e
YZ
6868 ret = btrfs_truncate_inode_items(trans, root, inode,
6869 inode->i_size,
6870 BTRFS_EXTENT_DATA_KEY);
3893e33b
JB
6871 if (ret != -EAGAIN) {
6872 err = ret;
8082510e 6873 break;
3893e33b 6874 }
39279cc3 6875
fcb80c2a 6876 trans->block_rsv = &root->fs_info->trans_block_rsv;
8082510e 6877 ret = btrfs_update_inode(trans, root, inode);
3893e33b
JB
6878 if (ret) {
6879 err = ret;
6880 break;
6881 }
907cbceb 6882end_trans:
8082510e
YZ
6883 nr = trans->blocks_used;
6884 btrfs_end_transaction(trans, root);
d68fc57b 6885 trans = NULL;
8082510e 6886 btrfs_btree_balance_dirty(root, nr);
8082510e
YZ
6887 }
6888
6889 if (ret == 0 && inode->i_nlink > 0) {
fcb80c2a 6890 trans->block_rsv = root->orphan_block_rsv;
8082510e 6891 ret = btrfs_orphan_del(trans, inode);
3893e33b
JB
6892 if (ret)
6893 err = ret;
ded5db9d
JB
6894 } else if (ret && inode->i_nlink > 0) {
6895 /*
6896 * Failed to do the truncate, remove us from the in memory
6897 * orphan list.
6898 */
6899 ret = btrfs_orphan_del(NULL, inode);
8082510e
YZ
6900 }
6901
917c16b2
CM
6902 if (trans) {
6903 trans->block_rsv = &root->fs_info->trans_block_rsv;
6904 ret = btrfs_update_inode(trans, root, inode);
6905 if (ret && !err)
6906 err = ret;
7b128766 6907
917c16b2 6908 nr = trans->blocks_used;
7ad85bb7 6909 ret = btrfs_end_transaction(trans, root);
917c16b2
CM
6910 btrfs_btree_balance_dirty(root, nr);
6911 }
fcb80c2a
JB
6912
6913out:
6914 btrfs_free_block_rsv(root, rsv);
6915
3893e33b
JB
6916 if (ret && !err)
6917 err = ret;
a41ad394 6918
3893e33b 6919 return err;
39279cc3
CM
6920}
6921
d352ac68
CM
6922/*
6923 * create a new subvolume directory/inode (helper for the ioctl).
6924 */
d2fb3437 6925int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
d82a6f1d 6926 struct btrfs_root *new_root, u64 new_dirid)
39279cc3 6927{
39279cc3 6928 struct inode *inode;
76dda93c 6929 int err;
00e4e6b3 6930 u64 index = 0;
39279cc3 6931
12fc9d09
FA
6932 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
6933 new_dirid, new_dirid,
6934 S_IFDIR | (~current_umask() & S_IRWXUGO),
6935 &index);
54aa1f4d 6936 if (IS_ERR(inode))
f46b5a66 6937 return PTR_ERR(inode);
39279cc3
CM
6938 inode->i_op = &btrfs_dir_inode_operations;
6939 inode->i_fop = &btrfs_dir_file_operations;
6940
bfe86848 6941 set_nlink(inode, 1);
dbe674a9 6942 btrfs_i_size_write(inode, 0);
3b96362c 6943
76dda93c 6944 err = btrfs_update_inode(trans, new_root, inode);
cb8e7090 6945
76dda93c 6946 iput(inode);
ce598979 6947 return err;
39279cc3
CM
6948}
6949
39279cc3
CM
6950struct inode *btrfs_alloc_inode(struct super_block *sb)
6951{
6952 struct btrfs_inode *ei;
2ead6ae7 6953 struct inode *inode;
39279cc3
CM
6954
6955 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
6956 if (!ei)
6957 return NULL;
2ead6ae7
YZ
6958
6959 ei->root = NULL;
2ead6ae7 6960 ei->generation = 0;
15ee9bc7 6961 ei->last_trans = 0;
257c62e1 6962 ei->last_sub_trans = 0;
e02119d5 6963 ei->logged_trans = 0;
2ead6ae7 6964 ei->delalloc_bytes = 0;
2ead6ae7
YZ
6965 ei->disk_i_size = 0;
6966 ei->flags = 0;
7709cde3 6967 ei->csum_bytes = 0;
2ead6ae7
YZ
6968 ei->index_cnt = (u64)-1;
6969 ei->last_unlink_trans = 0;
6970
9e0baf60
JB
6971 spin_lock_init(&ei->lock);
6972 ei->outstanding_extents = 0;
6973 ei->reserved_extents = 0;
2ead6ae7 6974
72ac3c0d 6975 ei->runtime_flags = 0;
261507a0 6976 ei->force_compress = BTRFS_COMPRESS_NONE;
2ead6ae7 6977
16cdcec7
MX
6978 ei->delayed_node = NULL;
6979
2ead6ae7 6980 inode = &ei->vfs_inode;
a8067e02 6981 extent_map_tree_init(&ei->extent_tree);
f993c883
DS
6982 extent_io_tree_init(&ei->io_tree, &inode->i_data);
6983 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
0b32f4bb
JB
6984 ei->io_tree.track_uptodate = 1;
6985 ei->io_failure_tree.track_uptodate = 1;
2ead6ae7 6986 mutex_init(&ei->log_mutex);
f248679e 6987 mutex_init(&ei->delalloc_mutex);
e6dcd2dc 6988 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
2ead6ae7 6989 INIT_LIST_HEAD(&ei->delalloc_inodes);
5a3f23d5 6990 INIT_LIST_HEAD(&ei->ordered_operations);
2ead6ae7
YZ
6991 RB_CLEAR_NODE(&ei->rb_node);
6992
6993 return inode;
39279cc3
CM
6994}
6995
fa0d7e3d
NP
6996static void btrfs_i_callback(struct rcu_head *head)
6997{
6998 struct inode *inode = container_of(head, struct inode, i_rcu);
fa0d7e3d
NP
6999 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
7000}
7001
39279cc3
CM
7002void btrfs_destroy_inode(struct inode *inode)
7003{
e6dcd2dc 7004 struct btrfs_ordered_extent *ordered;
5a3f23d5
CM
7005 struct btrfs_root *root = BTRFS_I(inode)->root;
7006
b3d9b7a3 7007 WARN_ON(!hlist_empty(&inode->i_dentry));
39279cc3 7008 WARN_ON(inode->i_data.nrpages);
9e0baf60
JB
7009 WARN_ON(BTRFS_I(inode)->outstanding_extents);
7010 WARN_ON(BTRFS_I(inode)->reserved_extents);
7709cde3
JB
7011 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
7012 WARN_ON(BTRFS_I(inode)->csum_bytes);
39279cc3 7013
a6dbd429
JB
7014 /*
7015 * This can happen where we create an inode, but somebody else also
7016 * created the same inode and we need to destroy the one we already
7017 * created.
7018 */
7019 if (!root)
7020 goto free;
7021
5a3f23d5
CM
7022 /*
7023 * Make sure we're properly removed from the ordered operation
7024 * lists.
7025 */
7026 smp_mb();
7027 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
7028 spin_lock(&root->fs_info->ordered_extent_lock);
7029 list_del_init(&BTRFS_I(inode)->ordered_operations);
7030 spin_unlock(&root->fs_info->ordered_extent_lock);
7031 }
7032
8a35d95f
JB
7033 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
7034 &BTRFS_I(inode)->runtime_flags)) {
33345d01
LZ
7035 printk(KERN_INFO "BTRFS: inode %llu still on the orphan list\n",
7036 (unsigned long long)btrfs_ino(inode));
8a35d95f 7037 atomic_dec(&root->orphan_inodes);
7b128766 7038 }
7b128766 7039
d397712b 7040 while (1) {
e6dcd2dc
CM
7041 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
7042 if (!ordered)
7043 break;
7044 else {
d397712b
CM
7045 printk(KERN_ERR "btrfs found ordered "
7046 "extent %llu %llu on inode cleanup\n",
7047 (unsigned long long)ordered->file_offset,
7048 (unsigned long long)ordered->len);
e6dcd2dc
CM
7049 btrfs_remove_ordered_extent(inode, ordered);
7050 btrfs_put_ordered_extent(ordered);
7051 btrfs_put_ordered_extent(ordered);
7052 }
7053 }
5d4f98a2 7054 inode_tree_del(inode);
5b21f2ed 7055 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
a6dbd429 7056free:
16cdcec7 7057 btrfs_remove_delayed_node(inode);
fa0d7e3d 7058 call_rcu(&inode->i_rcu, btrfs_i_callback);
39279cc3
CM
7059}
7060
45321ac5 7061int btrfs_drop_inode(struct inode *inode)
76dda93c
YZ
7062{
7063 struct btrfs_root *root = BTRFS_I(inode)->root;
45321ac5 7064
0af3d00b 7065 if (btrfs_root_refs(&root->root_item) == 0 &&
83eea1f1 7066 !btrfs_is_free_space_inode(inode))
45321ac5 7067 return 1;
76dda93c 7068 else
45321ac5 7069 return generic_drop_inode(inode);
76dda93c
YZ
7070}
7071
0ee0fda0 7072static void init_once(void *foo)
39279cc3
CM
7073{
7074 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
7075
7076 inode_init_once(&ei->vfs_inode);
7077}
7078
7079void btrfs_destroy_cachep(void)
7080{
7081 if (btrfs_inode_cachep)
7082 kmem_cache_destroy(btrfs_inode_cachep);
7083 if (btrfs_trans_handle_cachep)
7084 kmem_cache_destroy(btrfs_trans_handle_cachep);
7085 if (btrfs_transaction_cachep)
7086 kmem_cache_destroy(btrfs_transaction_cachep);
39279cc3
CM
7087 if (btrfs_path_cachep)
7088 kmem_cache_destroy(btrfs_path_cachep);
dc89e982
JB
7089 if (btrfs_free_space_cachep)
7090 kmem_cache_destroy(btrfs_free_space_cachep);
39279cc3
CM
7091}
7092
7093int btrfs_init_cachep(void)
7094{
9601e3f6
CH
7095 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
7096 sizeof(struct btrfs_inode), 0,
7097 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
39279cc3
CM
7098 if (!btrfs_inode_cachep)
7099 goto fail;
9601e3f6
CH
7100
7101 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
7102 sizeof(struct btrfs_trans_handle), 0,
7103 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
39279cc3
CM
7104 if (!btrfs_trans_handle_cachep)
7105 goto fail;
9601e3f6
CH
7106
7107 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
7108 sizeof(struct btrfs_transaction), 0,
7109 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
39279cc3
CM
7110 if (!btrfs_transaction_cachep)
7111 goto fail;
9601e3f6
CH
7112
7113 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
7114 sizeof(struct btrfs_path), 0,
7115 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
39279cc3
CM
7116 if (!btrfs_path_cachep)
7117 goto fail;
9601e3f6 7118
dc89e982
JB
7119 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space_cache",
7120 sizeof(struct btrfs_free_space), 0,
7121 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7122 if (!btrfs_free_space_cachep)
7123 goto fail;
7124
39279cc3
CM
7125 return 0;
7126fail:
7127 btrfs_destroy_cachep();
7128 return -ENOMEM;
7129}
7130
7131static int btrfs_getattr(struct vfsmount *mnt,
7132 struct dentry *dentry, struct kstat *stat)
7133{
7134 struct inode *inode = dentry->d_inode;
fadc0d8b
DS
7135 u32 blocksize = inode->i_sb->s_blocksize;
7136
39279cc3 7137 generic_fillattr(inode, stat);
0ee5dc67 7138 stat->dev = BTRFS_I(inode)->root->anon_dev;
d6667462 7139 stat->blksize = PAGE_CACHE_SIZE;
fadc0d8b
DS
7140 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
7141 ALIGN(BTRFS_I(inode)->delalloc_bytes, blocksize)) >> 9;
39279cc3
CM
7142 return 0;
7143}
7144
75e7cb7f
LB
7145/*
7146 * If a file is moved, it will inherit the cow and compression flags of the new
7147 * directory.
7148 */
7149static void fixup_inode_flags(struct inode *dir, struct inode *inode)
7150{
7151 struct btrfs_inode *b_dir = BTRFS_I(dir);
7152 struct btrfs_inode *b_inode = BTRFS_I(inode);
7153
7154 if (b_dir->flags & BTRFS_INODE_NODATACOW)
7155 b_inode->flags |= BTRFS_INODE_NODATACOW;
7156 else
7157 b_inode->flags &= ~BTRFS_INODE_NODATACOW;
7158
bc178237 7159 if (b_dir->flags & BTRFS_INODE_COMPRESS) {
75e7cb7f 7160 b_inode->flags |= BTRFS_INODE_COMPRESS;
bc178237
LB
7161 b_inode->flags &= ~BTRFS_INODE_NOCOMPRESS;
7162 } else {
7163 b_inode->flags &= ~(BTRFS_INODE_COMPRESS |
7164 BTRFS_INODE_NOCOMPRESS);
7165 }
75e7cb7f
LB
7166}
7167
d397712b
CM
7168static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7169 struct inode *new_dir, struct dentry *new_dentry)
39279cc3
CM
7170{
7171 struct btrfs_trans_handle *trans;
7172 struct btrfs_root *root = BTRFS_I(old_dir)->root;
4df27c4d 7173 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
39279cc3
CM
7174 struct inode *new_inode = new_dentry->d_inode;
7175 struct inode *old_inode = old_dentry->d_inode;
7176 struct timespec ctime = CURRENT_TIME;
00e4e6b3 7177 u64 index = 0;
4df27c4d 7178 u64 root_objectid;
39279cc3 7179 int ret;
33345d01 7180 u64 old_ino = btrfs_ino(old_inode);
39279cc3 7181
33345d01 7182 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
f679a840
YZ
7183 return -EPERM;
7184
4df27c4d 7185 /* we only allow rename subvolume link between subvolumes */
33345d01 7186 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
3394e160
CM
7187 return -EXDEV;
7188
33345d01
LZ
7189 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
7190 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
39279cc3 7191 return -ENOTEMPTY;
5f39d397 7192
4df27c4d
YZ
7193 if (S_ISDIR(old_inode->i_mode) && new_inode &&
7194 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
7195 return -ENOTEMPTY;
5a3f23d5
CM
7196 /*
7197 * we're using rename to replace one file with another.
7198 * and the replacement file is large. Start IO on it now so
7199 * we don't add too much work to the end of the transaction
7200 */
4baf8c92 7201 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
5a3f23d5
CM
7202 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
7203 filemap_flush(old_inode->i_mapping);
7204
76dda93c 7205 /* close the racy window with snapshot create/destroy ioctl */
33345d01 7206 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
76dda93c 7207 down_read(&root->fs_info->subvol_sem);
a22285a6
YZ
7208 /*
7209 * We want to reserve the absolute worst case amount of items. So if
7210 * both inodes are subvols and we need to unlink them then that would
7211 * require 4 item modifications, but if they are both normal inodes it
7212 * would require 5 item modifications, so we'll assume their normal
7213 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
7214 * should cover the worst case number of items we'll modify.
7215 */
7216 trans = btrfs_start_transaction(root, 20);
b44c59a8
JL
7217 if (IS_ERR(trans)) {
7218 ret = PTR_ERR(trans);
7219 goto out_notrans;
7220 }
76dda93c 7221
4df27c4d
YZ
7222 if (dest != root)
7223 btrfs_record_root_in_trans(trans, dest);
5f39d397 7224
a5719521
YZ
7225 ret = btrfs_set_inode_index(new_dir, &index);
7226 if (ret)
7227 goto out_fail;
5a3f23d5 7228
33345d01 7229 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
4df27c4d
YZ
7230 /* force full log commit if subvolume involved. */
7231 root->fs_info->last_trans_log_full_commit = trans->transid;
7232 } else {
a5719521
YZ
7233 ret = btrfs_insert_inode_ref(trans, dest,
7234 new_dentry->d_name.name,
7235 new_dentry->d_name.len,
33345d01
LZ
7236 old_ino,
7237 btrfs_ino(new_dir), index);
a5719521
YZ
7238 if (ret)
7239 goto out_fail;
4df27c4d
YZ
7240 /*
7241 * this is an ugly little race, but the rename is required
7242 * to make sure that if we crash, the inode is either at the
7243 * old name or the new one. pinning the log transaction lets
7244 * us make sure we don't allow a log commit to come in after
7245 * we unlink the name but before we add the new name back in.
7246 */
7247 btrfs_pin_log_trans(root);
7248 }
5a3f23d5
CM
7249 /*
7250 * make sure the inode gets flushed if it is replacing
7251 * something.
7252 */
33345d01 7253 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
5a3f23d5 7254 btrfs_add_ordered_operation(trans, root, old_inode);
5a3f23d5 7255
0c4d2d95
JB
7256 inode_inc_iversion(old_dir);
7257 inode_inc_iversion(new_dir);
7258 inode_inc_iversion(old_inode);
39279cc3
CM
7259 old_dir->i_ctime = old_dir->i_mtime = ctime;
7260 new_dir->i_ctime = new_dir->i_mtime = ctime;
7261 old_inode->i_ctime = ctime;
5f39d397 7262
12fcfd22
CM
7263 if (old_dentry->d_parent != new_dentry->d_parent)
7264 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
7265
33345d01 7266 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
4df27c4d
YZ
7267 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
7268 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
7269 old_dentry->d_name.name,
7270 old_dentry->d_name.len);
7271 } else {
92986796
AV
7272 ret = __btrfs_unlink_inode(trans, root, old_dir,
7273 old_dentry->d_inode,
7274 old_dentry->d_name.name,
7275 old_dentry->d_name.len);
7276 if (!ret)
7277 ret = btrfs_update_inode(trans, root, old_inode);
4df27c4d 7278 }
79787eaa
JM
7279 if (ret) {
7280 btrfs_abort_transaction(trans, root, ret);
7281 goto out_fail;
7282 }
39279cc3
CM
7283
7284 if (new_inode) {
0c4d2d95 7285 inode_inc_iversion(new_inode);
39279cc3 7286 new_inode->i_ctime = CURRENT_TIME;
33345d01 7287 if (unlikely(btrfs_ino(new_inode) ==
4df27c4d
YZ
7288 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
7289 root_objectid = BTRFS_I(new_inode)->location.objectid;
7290 ret = btrfs_unlink_subvol(trans, dest, new_dir,
7291 root_objectid,
7292 new_dentry->d_name.name,
7293 new_dentry->d_name.len);
7294 BUG_ON(new_inode->i_nlink == 0);
7295 } else {
7296 ret = btrfs_unlink_inode(trans, dest, new_dir,
7297 new_dentry->d_inode,
7298 new_dentry->d_name.name,
7299 new_dentry->d_name.len);
7300 }
79787eaa 7301 if (!ret && new_inode->i_nlink == 0) {
e02119d5 7302 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
4df27c4d 7303 BUG_ON(ret);
7b128766 7304 }
79787eaa
JM
7305 if (ret) {
7306 btrfs_abort_transaction(trans, root, ret);
7307 goto out_fail;
7308 }
39279cc3 7309 }
aec7477b 7310
75e7cb7f
LB
7311 fixup_inode_flags(new_dir, old_inode);
7312
4df27c4d
YZ
7313 ret = btrfs_add_link(trans, new_dir, old_inode,
7314 new_dentry->d_name.name,
a5719521 7315 new_dentry->d_name.len, 0, index);
79787eaa
JM
7316 if (ret) {
7317 btrfs_abort_transaction(trans, root, ret);
7318 goto out_fail;
7319 }
39279cc3 7320
33345d01 7321 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
10d9f309 7322 struct dentry *parent = new_dentry->d_parent;
6a912213 7323 btrfs_log_new_name(trans, old_inode, old_dir, parent);
4df27c4d
YZ
7324 btrfs_end_log_trans(root);
7325 }
39279cc3 7326out_fail:
7ad85bb7 7327 btrfs_end_transaction(trans, root);
b44c59a8 7328out_notrans:
33345d01 7329 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
76dda93c 7330 up_read(&root->fs_info->subvol_sem);
9ed74f2d 7331
39279cc3
CM
7332 return ret;
7333}
7334
d352ac68
CM
7335/*
7336 * some fairly slow code that needs optimization. This walks the list
7337 * of all the inodes with pending delalloc and forces them to disk.
7338 */
24bbcf04 7339int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
ea8c2819
CM
7340{
7341 struct list_head *head = &root->fs_info->delalloc_inodes;
7342 struct btrfs_inode *binode;
5b21f2ed 7343 struct inode *inode;
ea8c2819 7344
c146afad
YZ
7345 if (root->fs_info->sb->s_flags & MS_RDONLY)
7346 return -EROFS;
7347
75eff68e 7348 spin_lock(&root->fs_info->delalloc_lock);
d397712b 7349 while (!list_empty(head)) {
ea8c2819
CM
7350 binode = list_entry(head->next, struct btrfs_inode,
7351 delalloc_inodes);
5b21f2ed
ZY
7352 inode = igrab(&binode->vfs_inode);
7353 if (!inode)
7354 list_del_init(&binode->delalloc_inodes);
75eff68e 7355 spin_unlock(&root->fs_info->delalloc_lock);
5b21f2ed 7356 if (inode) {
8c8bee1d 7357 filemap_flush(inode->i_mapping);
24bbcf04
YZ
7358 if (delay_iput)
7359 btrfs_add_delayed_iput(inode);
7360 else
7361 iput(inode);
5b21f2ed
ZY
7362 }
7363 cond_resched();
75eff68e 7364 spin_lock(&root->fs_info->delalloc_lock);
ea8c2819 7365 }
75eff68e 7366 spin_unlock(&root->fs_info->delalloc_lock);
8c8bee1d
CM
7367
7368 /* the filemap_flush will queue IO into the worker threads, but
7369 * we have to make sure the IO is actually started and that
7370 * ordered extents get created before we return
7371 */
7372 atomic_inc(&root->fs_info->async_submit_draining);
d397712b 7373 while (atomic_read(&root->fs_info->nr_async_submits) ||
771ed689 7374 atomic_read(&root->fs_info->async_delalloc_pages)) {
8c8bee1d 7375 wait_event(root->fs_info->async_submit_wait,
771ed689
CM
7376 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
7377 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
8c8bee1d
CM
7378 }
7379 atomic_dec(&root->fs_info->async_submit_draining);
ea8c2819
CM
7380 return 0;
7381}
7382
39279cc3
CM
7383static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7384 const char *symname)
7385{
7386 struct btrfs_trans_handle *trans;
7387 struct btrfs_root *root = BTRFS_I(dir)->root;
7388 struct btrfs_path *path;
7389 struct btrfs_key key;
1832a6d5 7390 struct inode *inode = NULL;
39279cc3
CM
7391 int err;
7392 int drop_inode = 0;
7393 u64 objectid;
00e4e6b3 7394 u64 index = 0 ;
39279cc3
CM
7395 int name_len;
7396 int datasize;
5f39d397 7397 unsigned long ptr;
39279cc3 7398 struct btrfs_file_extent_item *ei;
5f39d397 7399 struct extent_buffer *leaf;
1832a6d5 7400 unsigned long nr = 0;
39279cc3
CM
7401
7402 name_len = strlen(symname) + 1;
7403 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
7404 return -ENAMETOOLONG;
1832a6d5 7405
9ed74f2d
JB
7406 /*
7407 * 2 items for inode item and ref
7408 * 2 items for dir items
7409 * 1 item for xattr if selinux is on
7410 */
a22285a6
YZ
7411 trans = btrfs_start_transaction(root, 5);
7412 if (IS_ERR(trans))
7413 return PTR_ERR(trans);
1832a6d5 7414
581bb050
LZ
7415 err = btrfs_find_free_ino(root, &objectid);
7416 if (err)
7417 goto out_unlock;
7418
aec7477b 7419 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
33345d01 7420 dentry->d_name.len, btrfs_ino(dir), objectid,
d82a6f1d 7421 S_IFLNK|S_IRWXUGO, &index);
7cf96da3
TI
7422 if (IS_ERR(inode)) {
7423 err = PTR_ERR(inode);
39279cc3 7424 goto out_unlock;
7cf96da3 7425 }
39279cc3 7426
2a7dba39 7427 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
33268eaf
JB
7428 if (err) {
7429 drop_inode = 1;
7430 goto out_unlock;
7431 }
7432
ad19db71
CS
7433 /*
7434 * If the active LSM wants to access the inode during
7435 * d_instantiate it needs these. Smack checks to see
7436 * if the filesystem supports xattrs by looking at the
7437 * ops vector.
7438 */
7439 inode->i_fop = &btrfs_file_operations;
7440 inode->i_op = &btrfs_file_inode_operations;
7441
a1b075d2 7442 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
39279cc3
CM
7443 if (err)
7444 drop_inode = 1;
7445 else {
7446 inode->i_mapping->a_ops = &btrfs_aops;
04160088 7447 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
d1310b2e 7448 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
39279cc3 7449 }
39279cc3
CM
7450 if (drop_inode)
7451 goto out_unlock;
7452
7453 path = btrfs_alloc_path();
d8926bb3
MF
7454 if (!path) {
7455 err = -ENOMEM;
7456 drop_inode = 1;
7457 goto out_unlock;
7458 }
33345d01 7459 key.objectid = btrfs_ino(inode);
39279cc3 7460 key.offset = 0;
39279cc3
CM
7461 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
7462 datasize = btrfs_file_extent_calc_inline_size(name_len);
7463 err = btrfs_insert_empty_item(trans, root, path, &key,
7464 datasize);
54aa1f4d
CM
7465 if (err) {
7466 drop_inode = 1;
b0839166 7467 btrfs_free_path(path);
54aa1f4d
CM
7468 goto out_unlock;
7469 }
5f39d397
CM
7470 leaf = path->nodes[0];
7471 ei = btrfs_item_ptr(leaf, path->slots[0],
7472 struct btrfs_file_extent_item);
7473 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
7474 btrfs_set_file_extent_type(leaf, ei,
39279cc3 7475 BTRFS_FILE_EXTENT_INLINE);
c8b97818
CM
7476 btrfs_set_file_extent_encryption(leaf, ei, 0);
7477 btrfs_set_file_extent_compression(leaf, ei, 0);
7478 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
7479 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
7480
39279cc3 7481 ptr = btrfs_file_extent_inline_start(ei);
5f39d397
CM
7482 write_extent_buffer(leaf, symname, ptr, name_len);
7483 btrfs_mark_buffer_dirty(leaf);
39279cc3 7484 btrfs_free_path(path);
5f39d397 7485
39279cc3
CM
7486 inode->i_op = &btrfs_symlink_inode_operations;
7487 inode->i_mapping->a_ops = &btrfs_symlink_aops;
04160088 7488 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
d899e052 7489 inode_set_bytes(inode, name_len);
dbe674a9 7490 btrfs_i_size_write(inode, name_len - 1);
54aa1f4d
CM
7491 err = btrfs_update_inode(trans, root, inode);
7492 if (err)
7493 drop_inode = 1;
39279cc3
CM
7494
7495out_unlock:
08c422c2
AV
7496 if (!err)
7497 d_instantiate(dentry, inode);
d3c2fdcf 7498 nr = trans->blocks_used;
7ad85bb7 7499 btrfs_end_transaction(trans, root);
39279cc3
CM
7500 if (drop_inode) {
7501 inode_dec_link_count(inode);
7502 iput(inode);
7503 }
d3c2fdcf 7504 btrfs_btree_balance_dirty(root, nr);
39279cc3
CM
7505 return err;
7506}
16432985 7507
0af3d00b
JB
7508static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
7509 u64 start, u64 num_bytes, u64 min_size,
7510 loff_t actual_len, u64 *alloc_hint,
7511 struct btrfs_trans_handle *trans)
d899e052 7512{
d899e052
YZ
7513 struct btrfs_root *root = BTRFS_I(inode)->root;
7514 struct btrfs_key ins;
d899e052 7515 u64 cur_offset = start;
55a61d1d 7516 u64 i_size;
d899e052 7517 int ret = 0;
0af3d00b 7518 bool own_trans = true;
d899e052 7519
0af3d00b
JB
7520 if (trans)
7521 own_trans = false;
d899e052 7522 while (num_bytes > 0) {
0af3d00b
JB
7523 if (own_trans) {
7524 trans = btrfs_start_transaction(root, 3);
7525 if (IS_ERR(trans)) {
7526 ret = PTR_ERR(trans);
7527 break;
7528 }
5a303d5d
YZ
7529 }
7530
efa56464 7531 ret = btrfs_reserve_extent(trans, root, num_bytes, min_size,
81c9ad23 7532 0, *alloc_hint, &ins, 1);
5a303d5d 7533 if (ret) {
0af3d00b
JB
7534 if (own_trans)
7535 btrfs_end_transaction(trans, root);
a22285a6 7536 break;
d899e052 7537 }
5a303d5d 7538
d899e052
YZ
7539 ret = insert_reserved_file_extent(trans, inode,
7540 cur_offset, ins.objectid,
7541 ins.offset, ins.offset,
920bbbfb 7542 ins.offset, 0, 0, 0,
d899e052 7543 BTRFS_FILE_EXTENT_PREALLOC);
79787eaa
JM
7544 if (ret) {
7545 btrfs_abort_transaction(trans, root, ret);
7546 if (own_trans)
7547 btrfs_end_transaction(trans, root);
7548 break;
7549 }
a1ed835e
CM
7550 btrfs_drop_extent_cache(inode, cur_offset,
7551 cur_offset + ins.offset -1, 0);
5a303d5d 7552
d899e052
YZ
7553 num_bytes -= ins.offset;
7554 cur_offset += ins.offset;
efa56464 7555 *alloc_hint = ins.objectid + ins.offset;
5a303d5d 7556
0c4d2d95 7557 inode_inc_iversion(inode);
d899e052 7558 inode->i_ctime = CURRENT_TIME;
6cbff00f 7559 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
d899e052 7560 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
efa56464
YZ
7561 (actual_len > inode->i_size) &&
7562 (cur_offset > inode->i_size)) {
d1ea6a61 7563 if (cur_offset > actual_len)
55a61d1d 7564 i_size = actual_len;
d1ea6a61 7565 else
55a61d1d
JB
7566 i_size = cur_offset;
7567 i_size_write(inode, i_size);
7568 btrfs_ordered_update_i_size(inode, i_size, NULL);
5a303d5d
YZ
7569 }
7570
d899e052 7571 ret = btrfs_update_inode(trans, root, inode);
79787eaa
JM
7572
7573 if (ret) {
7574 btrfs_abort_transaction(trans, root, ret);
7575 if (own_trans)
7576 btrfs_end_transaction(trans, root);
7577 break;
7578 }
d899e052 7579
0af3d00b
JB
7580 if (own_trans)
7581 btrfs_end_transaction(trans, root);
5a303d5d 7582 }
d899e052
YZ
7583 return ret;
7584}
7585
0af3d00b
JB
7586int btrfs_prealloc_file_range(struct inode *inode, int mode,
7587 u64 start, u64 num_bytes, u64 min_size,
7588 loff_t actual_len, u64 *alloc_hint)
7589{
7590 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7591 min_size, actual_len, alloc_hint,
7592 NULL);
7593}
7594
7595int btrfs_prealloc_file_range_trans(struct inode *inode,
7596 struct btrfs_trans_handle *trans, int mode,
7597 u64 start, u64 num_bytes, u64 min_size,
7598 loff_t actual_len, u64 *alloc_hint)
7599{
7600 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7601 min_size, actual_len, alloc_hint, trans);
7602}
7603
e6dcd2dc
CM
7604static int btrfs_set_page_dirty(struct page *page)
7605{
e6dcd2dc
CM
7606 return __set_page_dirty_nobuffers(page);
7607}
7608
10556cb2 7609static int btrfs_permission(struct inode *inode, int mask)
fdebe2bd 7610{
b83cc969 7611 struct btrfs_root *root = BTRFS_I(inode)->root;
cb6db4e5 7612 umode_t mode = inode->i_mode;
b83cc969 7613
cb6db4e5
JM
7614 if (mask & MAY_WRITE &&
7615 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
7616 if (btrfs_root_readonly(root))
7617 return -EROFS;
7618 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
7619 return -EACCES;
7620 }
2830ba7f 7621 return generic_permission(inode, mask);
fdebe2bd 7622}
39279cc3 7623
6e1d5dcc 7624static const struct inode_operations btrfs_dir_inode_operations = {
3394e160 7625 .getattr = btrfs_getattr,
39279cc3
CM
7626 .lookup = btrfs_lookup,
7627 .create = btrfs_create,
7628 .unlink = btrfs_unlink,
7629 .link = btrfs_link,
7630 .mkdir = btrfs_mkdir,
7631 .rmdir = btrfs_rmdir,
7632 .rename = btrfs_rename,
7633 .symlink = btrfs_symlink,
7634 .setattr = btrfs_setattr,
618e21d5 7635 .mknod = btrfs_mknod,
95819c05
CH
7636 .setxattr = btrfs_setxattr,
7637 .getxattr = btrfs_getxattr,
5103e947 7638 .listxattr = btrfs_listxattr,
95819c05 7639 .removexattr = btrfs_removexattr,
fdebe2bd 7640 .permission = btrfs_permission,
4e34e719 7641 .get_acl = btrfs_get_acl,
39279cc3 7642};
6e1d5dcc 7643static const struct inode_operations btrfs_dir_ro_inode_operations = {
39279cc3 7644 .lookup = btrfs_lookup,
fdebe2bd 7645 .permission = btrfs_permission,
4e34e719 7646 .get_acl = btrfs_get_acl,
39279cc3 7647};
76dda93c 7648
828c0950 7649static const struct file_operations btrfs_dir_file_operations = {
39279cc3
CM
7650 .llseek = generic_file_llseek,
7651 .read = generic_read_dir,
cbdf5a24 7652 .readdir = btrfs_real_readdir,
34287aa3 7653 .unlocked_ioctl = btrfs_ioctl,
39279cc3 7654#ifdef CONFIG_COMPAT
34287aa3 7655 .compat_ioctl = btrfs_ioctl,
39279cc3 7656#endif
6bf13c0c 7657 .release = btrfs_release_file,
e02119d5 7658 .fsync = btrfs_sync_file,
39279cc3
CM
7659};
7660
d1310b2e 7661static struct extent_io_ops btrfs_extent_io_ops = {
07157aac 7662 .fill_delalloc = run_delalloc_range,
065631f6 7663 .submit_bio_hook = btrfs_submit_bio_hook,
239b14b3 7664 .merge_bio_hook = btrfs_merge_bio_hook,
07157aac 7665 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
e6dcd2dc 7666 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
247e743c 7667 .writepage_start_hook = btrfs_writepage_start_hook,
b0c68f8b
CM
7668 .set_bit_hook = btrfs_set_bit_hook,
7669 .clear_bit_hook = btrfs_clear_bit_hook,
9ed74f2d
JB
7670 .merge_extent_hook = btrfs_merge_extent_hook,
7671 .split_extent_hook = btrfs_split_extent_hook,
07157aac
CM
7672};
7673
35054394
CM
7674/*
7675 * btrfs doesn't support the bmap operation because swapfiles
7676 * use bmap to make a mapping of extents in the file. They assume
7677 * these extents won't change over the life of the file and they
7678 * use the bmap result to do IO directly to the drive.
7679 *
7680 * the btrfs bmap call would return logical addresses that aren't
7681 * suitable for IO and they also will change frequently as COW
7682 * operations happen. So, swapfile + btrfs == corruption.
7683 *
7684 * For now we're avoiding this by dropping bmap.
7685 */
7f09410b 7686static const struct address_space_operations btrfs_aops = {
39279cc3
CM
7687 .readpage = btrfs_readpage,
7688 .writepage = btrfs_writepage,
b293f02e 7689 .writepages = btrfs_writepages,
3ab2fb5a 7690 .readpages = btrfs_readpages,
16432985 7691 .direct_IO = btrfs_direct_IO,
a52d9a80
CM
7692 .invalidatepage = btrfs_invalidatepage,
7693 .releasepage = btrfs_releasepage,
e6dcd2dc 7694 .set_page_dirty = btrfs_set_page_dirty,
465fdd97 7695 .error_remove_page = generic_error_remove_page,
39279cc3
CM
7696};
7697
7f09410b 7698static const struct address_space_operations btrfs_symlink_aops = {
39279cc3
CM
7699 .readpage = btrfs_readpage,
7700 .writepage = btrfs_writepage,
2bf5a725
CM
7701 .invalidatepage = btrfs_invalidatepage,
7702 .releasepage = btrfs_releasepage,
39279cc3
CM
7703};
7704
6e1d5dcc 7705static const struct inode_operations btrfs_file_inode_operations = {
39279cc3
CM
7706 .getattr = btrfs_getattr,
7707 .setattr = btrfs_setattr,
95819c05
CH
7708 .setxattr = btrfs_setxattr,
7709 .getxattr = btrfs_getxattr,
5103e947 7710 .listxattr = btrfs_listxattr,
95819c05 7711 .removexattr = btrfs_removexattr,
fdebe2bd 7712 .permission = btrfs_permission,
1506fcc8 7713 .fiemap = btrfs_fiemap,
4e34e719 7714 .get_acl = btrfs_get_acl,
e41f941a 7715 .update_time = btrfs_update_time,
39279cc3 7716};
6e1d5dcc 7717static const struct inode_operations btrfs_special_inode_operations = {
618e21d5
JB
7718 .getattr = btrfs_getattr,
7719 .setattr = btrfs_setattr,
fdebe2bd 7720 .permission = btrfs_permission,
95819c05
CH
7721 .setxattr = btrfs_setxattr,
7722 .getxattr = btrfs_getxattr,
33268eaf 7723 .listxattr = btrfs_listxattr,
95819c05 7724 .removexattr = btrfs_removexattr,
4e34e719 7725 .get_acl = btrfs_get_acl,
e41f941a 7726 .update_time = btrfs_update_time,
618e21d5 7727};
6e1d5dcc 7728static const struct inode_operations btrfs_symlink_inode_operations = {
39279cc3
CM
7729 .readlink = generic_readlink,
7730 .follow_link = page_follow_link_light,
7731 .put_link = page_put_link,
f209561a 7732 .getattr = btrfs_getattr,
22c44fe6 7733 .setattr = btrfs_setattr,
fdebe2bd 7734 .permission = btrfs_permission,
0279b4cd
JO
7735 .setxattr = btrfs_setxattr,
7736 .getxattr = btrfs_getxattr,
7737 .listxattr = btrfs_listxattr,
7738 .removexattr = btrfs_removexattr,
4e34e719 7739 .get_acl = btrfs_get_acl,
e41f941a 7740 .update_time = btrfs_update_time,
39279cc3 7741};
76dda93c 7742
82d339d9 7743const struct dentry_operations btrfs_dentry_operations = {
76dda93c 7744 .d_delete = btrfs_dentry_delete,
b4aff1f8 7745 .d_release = btrfs_dentry_release,
76dda93c 7746};