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