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