Btrfs: check size of inode backref before adding hardlink
[linux-2.6-block.git] / fs / btrfs / inode.c
CommitLineData
6cbd5570
CM
1/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
8f18cf13 19#include <linux/kernel.h>
065631f6 20#include <linux/bio.h>
39279cc3 21#include <linux/buffer_head.h>
f2eb0a24 22#include <linux/file.h>
39279cc3
CM
23#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
39279cc3
CM
29#include <linux/backing-dev.h>
30#include <linux/mpage.h>
31#include <linux/swap.h>
32#include <linux/writeback.h>
33#include <linux/statfs.h>
34#include <linux/compat.h>
9ebefb18 35#include <linux/bit_spinlock.h>
5103e947 36#include <linux/xattr.h>
33268eaf 37#include <linux/posix_acl.h>
d899e052 38#include <linux/falloc.h>
4b4e25f2 39#include "compat.h"
39279cc3
CM
40#include "ctree.h"
41#include "disk-io.h"
42#include "transaction.h"
43#include "btrfs_inode.h"
44#include "ioctl.h"
45#include "print-tree.h"
0b86a832 46#include "volumes.h"
e6dcd2dc 47#include "ordered-data.h"
95819c05 48#include "xattr.h"
e02119d5 49#include "tree-log.h"
c8b97818 50#include "compression.h"
b4ce94de 51#include "locking.h"
39279cc3
CM
52
53struct btrfs_iget_args {
54 u64 ino;
55 struct btrfs_root *root;
56};
57
58static struct inode_operations btrfs_dir_inode_operations;
59static struct inode_operations btrfs_symlink_inode_operations;
60static struct inode_operations btrfs_dir_ro_inode_operations;
618e21d5 61static struct inode_operations btrfs_special_inode_operations;
39279cc3
CM
62static struct inode_operations btrfs_file_inode_operations;
63static struct address_space_operations btrfs_aops;
64static struct address_space_operations btrfs_symlink_aops;
65static struct file_operations btrfs_dir_file_operations;
d1310b2e 66static struct extent_io_ops btrfs_extent_io_ops;
39279cc3
CM
67
68static struct kmem_cache *btrfs_inode_cachep;
69struct kmem_cache *btrfs_trans_handle_cachep;
70struct kmem_cache *btrfs_transaction_cachep;
39279cc3
CM
71struct kmem_cache *btrfs_path_cachep;
72
73#define S_SHIFT 12
74static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
75 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
76 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
77 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
78 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
79 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
80 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
81 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
82};
83
7b128766 84static void btrfs_truncate(struct inode *inode);
c8b97818 85static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end);
771ed689
CM
86static noinline int cow_file_range(struct inode *inode,
87 struct page *locked_page,
88 u64 start, u64 end, int *page_started,
89 unsigned long *nr_written, int unlock);
7b128766 90
0279b4cd
JO
91static int btrfs_init_inode_security(struct inode *inode, struct inode *dir)
92{
93 int err;
94
95 err = btrfs_init_acl(inode, dir);
96 if (!err)
97 err = btrfs_xattr_security_init(inode, dir);
98 return err;
99}
100
c8b97818
CM
101/*
102 * this does all the hard work for inserting an inline extent into
103 * the btree. The caller should have done a btrfs_drop_extents so that
104 * no overlapping inline items exist in the btree
105 */
d397712b 106static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
c8b97818
CM
107 struct btrfs_root *root, struct inode *inode,
108 u64 start, size_t size, size_t compressed_size,
109 struct page **compressed_pages)
110{
111 struct btrfs_key key;
112 struct btrfs_path *path;
113 struct extent_buffer *leaf;
114 struct page *page = NULL;
115 char *kaddr;
116 unsigned long ptr;
117 struct btrfs_file_extent_item *ei;
118 int err = 0;
119 int ret;
120 size_t cur_size = size;
121 size_t datasize;
122 unsigned long offset;
123 int use_compress = 0;
124
125 if (compressed_size && compressed_pages) {
126 use_compress = 1;
127 cur_size = compressed_size;
128 }
129
d397712b
CM
130 path = btrfs_alloc_path();
131 if (!path)
c8b97818
CM
132 return -ENOMEM;
133
b9473439 134 path->leave_spinning = 1;
c8b97818
CM
135 btrfs_set_trans_block_group(trans, inode);
136
137 key.objectid = inode->i_ino;
138 key.offset = start;
139 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
c8b97818
CM
140 datasize = btrfs_file_extent_calc_inline_size(cur_size);
141
142 inode_add_bytes(inode, size);
143 ret = btrfs_insert_empty_item(trans, root, path, &key,
144 datasize);
145 BUG_ON(ret);
146 if (ret) {
147 err = ret;
c8b97818
CM
148 goto fail;
149 }
150 leaf = path->nodes[0];
151 ei = btrfs_item_ptr(leaf, path->slots[0],
152 struct btrfs_file_extent_item);
153 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
154 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
155 btrfs_set_file_extent_encryption(leaf, ei, 0);
156 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
157 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
158 ptr = btrfs_file_extent_inline_start(ei);
159
160 if (use_compress) {
161 struct page *cpage;
162 int i = 0;
d397712b 163 while (compressed_size > 0) {
c8b97818 164 cpage = compressed_pages[i];
5b050f04 165 cur_size = min_t(unsigned long, compressed_size,
c8b97818
CM
166 PAGE_CACHE_SIZE);
167
b9473439 168 kaddr = kmap_atomic(cpage, KM_USER0);
c8b97818 169 write_extent_buffer(leaf, kaddr, ptr, cur_size);
b9473439 170 kunmap_atomic(kaddr, KM_USER0);
c8b97818
CM
171
172 i++;
173 ptr += cur_size;
174 compressed_size -= cur_size;
175 }
176 btrfs_set_file_extent_compression(leaf, ei,
177 BTRFS_COMPRESS_ZLIB);
178 } else {
179 page = find_get_page(inode->i_mapping,
180 start >> PAGE_CACHE_SHIFT);
181 btrfs_set_file_extent_compression(leaf, ei, 0);
182 kaddr = kmap_atomic(page, KM_USER0);
183 offset = start & (PAGE_CACHE_SIZE - 1);
184 write_extent_buffer(leaf, kaddr + offset, ptr, size);
185 kunmap_atomic(kaddr, KM_USER0);
186 page_cache_release(page);
187 }
188 btrfs_mark_buffer_dirty(leaf);
189 btrfs_free_path(path);
190
191 BTRFS_I(inode)->disk_i_size = inode->i_size;
192 btrfs_update_inode(trans, root, inode);
193 return 0;
194fail:
195 btrfs_free_path(path);
196 return err;
197}
198
199
200/*
201 * conditionally insert an inline extent into the file. This
202 * does the checks required to make sure the data is small enough
203 * to fit as an inline extent.
204 */
7f366cfe 205static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
c8b97818
CM
206 struct btrfs_root *root,
207 struct inode *inode, u64 start, u64 end,
208 size_t compressed_size,
209 struct page **compressed_pages)
210{
211 u64 isize = i_size_read(inode);
212 u64 actual_end = min(end + 1, isize);
213 u64 inline_len = actual_end - start;
214 u64 aligned_end = (end + root->sectorsize - 1) &
215 ~((u64)root->sectorsize - 1);
216 u64 hint_byte;
217 u64 data_len = inline_len;
218 int ret;
219
220 if (compressed_size)
221 data_len = compressed_size;
222
223 if (start > 0 ||
70b99e69 224 actual_end >= PAGE_CACHE_SIZE ||
c8b97818
CM
225 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
226 (!compressed_size &&
227 (actual_end & (root->sectorsize - 1)) == 0) ||
228 end + 1 < isize ||
229 data_len > root->fs_info->max_inline) {
230 return 1;
231 }
232
c8b97818 233 ret = btrfs_drop_extents(trans, root, inode, start,
a1ed835e
CM
234 aligned_end, aligned_end, start,
235 &hint_byte, 1);
c8b97818
CM
236 BUG_ON(ret);
237
238 if (isize > actual_end)
239 inline_len = min_t(u64, isize, actual_end);
240 ret = insert_inline_extent(trans, root, inode, start,
241 inline_len, compressed_size,
242 compressed_pages);
243 BUG_ON(ret);
a1ed835e 244 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
c8b97818
CM
245 return 0;
246}
247
771ed689
CM
248struct async_extent {
249 u64 start;
250 u64 ram_size;
251 u64 compressed_size;
252 struct page **pages;
253 unsigned long nr_pages;
254 struct list_head list;
255};
256
257struct async_cow {
258 struct inode *inode;
259 struct btrfs_root *root;
260 struct page *locked_page;
261 u64 start;
262 u64 end;
263 struct list_head extents;
264 struct btrfs_work work;
265};
266
267static noinline int add_async_extent(struct async_cow *cow,
268 u64 start, u64 ram_size,
269 u64 compressed_size,
270 struct page **pages,
271 unsigned long nr_pages)
272{
273 struct async_extent *async_extent;
274
275 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
276 async_extent->start = start;
277 async_extent->ram_size = ram_size;
278 async_extent->compressed_size = compressed_size;
279 async_extent->pages = pages;
280 async_extent->nr_pages = nr_pages;
281 list_add_tail(&async_extent->list, &cow->extents);
282 return 0;
283}
284
d352ac68 285/*
771ed689
CM
286 * we create compressed extents in two phases. The first
287 * phase compresses a range of pages that have already been
288 * locked (both pages and state bits are locked).
c8b97818 289 *
771ed689
CM
290 * This is done inside an ordered work queue, and the compression
291 * is spread across many cpus. The actual IO submission is step
292 * two, and the ordered work queue takes care of making sure that
293 * happens in the same order things were put onto the queue by
294 * writepages and friends.
c8b97818 295 *
771ed689
CM
296 * If this code finds it can't get good compression, it puts an
297 * entry onto the work queue to write the uncompressed bytes. This
298 * makes sure that both compressed inodes and uncompressed inodes
299 * are written in the same order that pdflush sent them down.
d352ac68 300 */
771ed689
CM
301static noinline int compress_file_range(struct inode *inode,
302 struct page *locked_page,
303 u64 start, u64 end,
304 struct async_cow *async_cow,
305 int *num_added)
b888db2b
CM
306{
307 struct btrfs_root *root = BTRFS_I(inode)->root;
308 struct btrfs_trans_handle *trans;
db94535d 309 u64 num_bytes;
c8b97818
CM
310 u64 orig_start;
311 u64 disk_num_bytes;
db94535d 312 u64 blocksize = root->sectorsize;
c8b97818 313 u64 actual_end;
42dc7bab 314 u64 isize = i_size_read(inode);
e6dcd2dc 315 int ret = 0;
c8b97818
CM
316 struct page **pages = NULL;
317 unsigned long nr_pages;
318 unsigned long nr_pages_ret = 0;
319 unsigned long total_compressed = 0;
320 unsigned long total_in = 0;
321 unsigned long max_compressed = 128 * 1024;
771ed689 322 unsigned long max_uncompressed = 128 * 1024;
c8b97818
CM
323 int i;
324 int will_compress;
b888db2b 325
c8b97818
CM
326 orig_start = start;
327
42dc7bab 328 actual_end = min_t(u64, isize, end + 1);
c8b97818
CM
329again:
330 will_compress = 0;
331 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
332 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
be20aa9d 333
f03d9301
CM
334 /*
335 * we don't want to send crud past the end of i_size through
336 * compression, that's just a waste of CPU time. So, if the
337 * end of the file is before the start of our current
338 * requested range of bytes, we bail out to the uncompressed
339 * cleanup code that can deal with all of this.
340 *
341 * It isn't really the fastest way to fix things, but this is a
342 * very uncommon corner.
343 */
344 if (actual_end <= start)
345 goto cleanup_and_bail_uncompressed;
346
c8b97818
CM
347 total_compressed = actual_end - start;
348
349 /* we want to make sure that amount of ram required to uncompress
350 * an extent is reasonable, so we limit the total size in ram
771ed689
CM
351 * of a compressed extent to 128k. This is a crucial number
352 * because it also controls how easily we can spread reads across
353 * cpus for decompression.
354 *
355 * We also want to make sure the amount of IO required to do
356 * a random read is reasonably small, so we limit the size of
357 * a compressed extent to 128k.
c8b97818
CM
358 */
359 total_compressed = min(total_compressed, max_uncompressed);
db94535d 360 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
be20aa9d 361 num_bytes = max(blocksize, num_bytes);
c8b97818
CM
362 disk_num_bytes = num_bytes;
363 total_in = 0;
364 ret = 0;
db94535d 365
771ed689
CM
366 /*
367 * we do compression for mount -o compress and when the
368 * inode has not been flagged as nocompress. This flag can
369 * change at any time if we discover bad compression ratios.
c8b97818 370 */
6cbff00f 371 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
c8b97818
CM
372 btrfs_test_opt(root, COMPRESS)) {
373 WARN_ON(pages);
cfbc246e 374 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
c8b97818 375
c8b97818
CM
376 ret = btrfs_zlib_compress_pages(inode->i_mapping, start,
377 total_compressed, pages,
378 nr_pages, &nr_pages_ret,
379 &total_in,
380 &total_compressed,
381 max_compressed);
382
383 if (!ret) {
384 unsigned long offset = total_compressed &
385 (PAGE_CACHE_SIZE - 1);
386 struct page *page = pages[nr_pages_ret - 1];
387 char *kaddr;
388
389 /* zero the tail end of the last page, we might be
390 * sending it down to disk
391 */
392 if (offset) {
393 kaddr = kmap_atomic(page, KM_USER0);
394 memset(kaddr + offset, 0,
395 PAGE_CACHE_SIZE - offset);
396 kunmap_atomic(kaddr, KM_USER0);
397 }
398 will_compress = 1;
399 }
400 }
401 if (start == 0) {
771ed689
CM
402 trans = btrfs_join_transaction(root, 1);
403 BUG_ON(!trans);
404 btrfs_set_trans_block_group(trans, inode);
405
c8b97818 406 /* lets try to make an inline extent */
771ed689 407 if (ret || total_in < (actual_end - start)) {
c8b97818 408 /* we didn't compress the entire range, try
771ed689 409 * to make an uncompressed inline extent.
c8b97818
CM
410 */
411 ret = cow_file_range_inline(trans, root, inode,
412 start, end, 0, NULL);
413 } else {
771ed689 414 /* try making a compressed inline extent */
c8b97818
CM
415 ret = cow_file_range_inline(trans, root, inode,
416 start, end,
417 total_compressed, pages);
418 }
771ed689 419 btrfs_end_transaction(trans, root);
c8b97818 420 if (ret == 0) {
771ed689
CM
421 /*
422 * inline extent creation worked, we don't need
423 * to create any more async work items. Unlock
424 * and free up our temp pages.
425 */
c8b97818
CM
426 extent_clear_unlock_delalloc(inode,
427 &BTRFS_I(inode)->io_tree,
771ed689 428 start, end, NULL, 1, 0,
8b62b72b 429 0, 1, 1, 1, 0);
c8b97818
CM
430 ret = 0;
431 goto free_pages_out;
432 }
433 }
434
435 if (will_compress) {
436 /*
437 * we aren't doing an inline extent round the compressed size
438 * up to a block size boundary so the allocator does sane
439 * things
440 */
441 total_compressed = (total_compressed + blocksize - 1) &
442 ~(blocksize - 1);
443
444 /*
445 * one last check to make sure the compression is really a
446 * win, compare the page count read with the blocks on disk
447 */
448 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
449 ~(PAGE_CACHE_SIZE - 1);
450 if (total_compressed >= total_in) {
451 will_compress = 0;
452 } else {
453 disk_num_bytes = total_compressed;
454 num_bytes = total_in;
455 }
456 }
457 if (!will_compress && pages) {
458 /*
459 * the compression code ran but failed to make things smaller,
460 * free any pages it allocated and our page pointer array
461 */
462 for (i = 0; i < nr_pages_ret; i++) {
70b99e69 463 WARN_ON(pages[i]->mapping);
c8b97818
CM
464 page_cache_release(pages[i]);
465 }
466 kfree(pages);
467 pages = NULL;
468 total_compressed = 0;
469 nr_pages_ret = 0;
470
471 /* flag the file so we don't compress in the future */
6cbff00f 472 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
c8b97818 473 }
771ed689
CM
474 if (will_compress) {
475 *num_added += 1;
c8b97818 476
771ed689
CM
477 /* the async work queues will take care of doing actual
478 * allocation on disk for these compressed pages,
479 * and will submit them to the elevator.
480 */
481 add_async_extent(async_cow, start, num_bytes,
482 total_compressed, pages, nr_pages_ret);
179e29e4 483
42dc7bab 484 if (start + num_bytes < end && start + num_bytes < actual_end) {
771ed689
CM
485 start += num_bytes;
486 pages = NULL;
487 cond_resched();
488 goto again;
489 }
490 } else {
f03d9301 491cleanup_and_bail_uncompressed:
771ed689
CM
492 /*
493 * No compression, but we still need to write the pages in
494 * the file we've been given so far. redirty the locked
495 * page if it corresponds to our extent and set things up
496 * for the async work queue to run cow_file_range to do
497 * the normal delalloc dance
498 */
499 if (page_offset(locked_page) >= start &&
500 page_offset(locked_page) <= end) {
501 __set_page_dirty_nobuffers(locked_page);
502 /* unlocked later on in the async handlers */
503 }
504 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0);
505 *num_added += 1;
506 }
3b951516 507
771ed689
CM
508out:
509 return 0;
510
511free_pages_out:
512 for (i = 0; i < nr_pages_ret; i++) {
513 WARN_ON(pages[i]->mapping);
514 page_cache_release(pages[i]);
515 }
d397712b 516 kfree(pages);
771ed689
CM
517
518 goto out;
519}
520
521/*
522 * phase two of compressed writeback. This is the ordered portion
523 * of the code, which only gets called in the order the work was
524 * queued. We walk all the async extents created by compress_file_range
525 * and send them down to the disk.
526 */
527static noinline int submit_compressed_extents(struct inode *inode,
528 struct async_cow *async_cow)
529{
530 struct async_extent *async_extent;
531 u64 alloc_hint = 0;
532 struct btrfs_trans_handle *trans;
533 struct btrfs_key ins;
534 struct extent_map *em;
535 struct btrfs_root *root = BTRFS_I(inode)->root;
536 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
537 struct extent_io_tree *io_tree;
538 int ret;
539
540 if (list_empty(&async_cow->extents))
541 return 0;
542
543 trans = btrfs_join_transaction(root, 1);
544
d397712b 545 while (!list_empty(&async_cow->extents)) {
771ed689
CM
546 async_extent = list_entry(async_cow->extents.next,
547 struct async_extent, list);
548 list_del(&async_extent->list);
c8b97818 549
771ed689
CM
550 io_tree = &BTRFS_I(inode)->io_tree;
551
552 /* did the compression code fall back to uncompressed IO? */
553 if (!async_extent->pages) {
554 int page_started = 0;
555 unsigned long nr_written = 0;
556
557 lock_extent(io_tree, async_extent->start,
d397712b
CM
558 async_extent->start +
559 async_extent->ram_size - 1, GFP_NOFS);
771ed689
CM
560
561 /* allocate blocks */
562 cow_file_range(inode, async_cow->locked_page,
563 async_extent->start,
564 async_extent->start +
565 async_extent->ram_size - 1,
566 &page_started, &nr_written, 0);
567
568 /*
569 * if page_started, cow_file_range inserted an
570 * inline extent and took care of all the unlocking
571 * and IO for us. Otherwise, we need to submit
572 * all those pages down to the drive.
573 */
574 if (!page_started)
575 extent_write_locked_range(io_tree,
576 inode, async_extent->start,
d397712b 577 async_extent->start +
771ed689
CM
578 async_extent->ram_size - 1,
579 btrfs_get_extent,
580 WB_SYNC_ALL);
581 kfree(async_extent);
582 cond_resched();
583 continue;
584 }
585
586 lock_extent(io_tree, async_extent->start,
587 async_extent->start + async_extent->ram_size - 1,
588 GFP_NOFS);
c8b97818 589 /*
771ed689
CM
590 * here we're doing allocation and writeback of the
591 * compressed pages
c8b97818 592 */
771ed689
CM
593 btrfs_drop_extent_cache(inode, async_extent->start,
594 async_extent->start +
595 async_extent->ram_size - 1, 0);
596
597 ret = btrfs_reserve_extent(trans, root,
598 async_extent->compressed_size,
599 async_extent->compressed_size,
600 0, alloc_hint,
601 (u64)-1, &ins, 1);
602 BUG_ON(ret);
603 em = alloc_extent_map(GFP_NOFS);
604 em->start = async_extent->start;
605 em->len = async_extent->ram_size;
445a6944 606 em->orig_start = em->start;
c8b97818 607
771ed689
CM
608 em->block_start = ins.objectid;
609 em->block_len = ins.offset;
610 em->bdev = root->fs_info->fs_devices->latest_bdev;
611 set_bit(EXTENT_FLAG_PINNED, &em->flags);
612 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
613
d397712b 614 while (1) {
890871be 615 write_lock(&em_tree->lock);
771ed689 616 ret = add_extent_mapping(em_tree, em);
890871be 617 write_unlock(&em_tree->lock);
771ed689
CM
618 if (ret != -EEXIST) {
619 free_extent_map(em);
620 break;
621 }
622 btrfs_drop_extent_cache(inode, async_extent->start,
623 async_extent->start +
624 async_extent->ram_size - 1, 0);
625 }
626
627 ret = btrfs_add_ordered_extent(inode, async_extent->start,
628 ins.objectid,
629 async_extent->ram_size,
630 ins.offset,
631 BTRFS_ORDERED_COMPRESSED);
632 BUG_ON(ret);
633
634 btrfs_end_transaction(trans, root);
635
636 /*
637 * clear dirty, set writeback and unlock the pages.
638 */
639 extent_clear_unlock_delalloc(inode,
640 &BTRFS_I(inode)->io_tree,
641 async_extent->start,
642 async_extent->start +
643 async_extent->ram_size - 1,
8b62b72b 644 NULL, 1, 1, 0, 1, 1, 0, 0);
771ed689
CM
645
646 ret = btrfs_submit_compressed_write(inode,
d397712b
CM
647 async_extent->start,
648 async_extent->ram_size,
649 ins.objectid,
650 ins.offset, async_extent->pages,
651 async_extent->nr_pages);
771ed689
CM
652
653 BUG_ON(ret);
654 trans = btrfs_join_transaction(root, 1);
655 alloc_hint = ins.objectid + ins.offset;
656 kfree(async_extent);
657 cond_resched();
658 }
659
660 btrfs_end_transaction(trans, root);
661 return 0;
662}
663
664/*
665 * when extent_io.c finds a delayed allocation range in the file,
666 * the call backs end up in this code. The basic idea is to
667 * allocate extents on disk for the range, and create ordered data structs
668 * in ram to track those extents.
669 *
670 * locked_page is the page that writepage had locked already. We use
671 * it to make sure we don't do extra locks or unlocks.
672 *
673 * *page_started is set to one if we unlock locked_page and do everything
674 * required to start IO on it. It may be clean and already done with
675 * IO when we return.
676 */
677static noinline int cow_file_range(struct inode *inode,
678 struct page *locked_page,
679 u64 start, u64 end, int *page_started,
680 unsigned long *nr_written,
681 int unlock)
682{
683 struct btrfs_root *root = BTRFS_I(inode)->root;
684 struct btrfs_trans_handle *trans;
685 u64 alloc_hint = 0;
686 u64 num_bytes;
687 unsigned long ram_size;
688 u64 disk_num_bytes;
689 u64 cur_alloc_size;
690 u64 blocksize = root->sectorsize;
691 u64 actual_end;
42dc7bab 692 u64 isize = i_size_read(inode);
771ed689
CM
693 struct btrfs_key ins;
694 struct extent_map *em;
695 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
696 int ret = 0;
697
698 trans = btrfs_join_transaction(root, 1);
699 BUG_ON(!trans);
700 btrfs_set_trans_block_group(trans, inode);
701
42dc7bab 702 actual_end = min_t(u64, isize, end + 1);
771ed689
CM
703
704 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
705 num_bytes = max(blocksize, num_bytes);
706 disk_num_bytes = num_bytes;
707 ret = 0;
708
709 if (start == 0) {
710 /* lets try to make an inline extent */
711 ret = cow_file_range_inline(trans, root, inode,
712 start, end, 0, NULL);
713 if (ret == 0) {
714 extent_clear_unlock_delalloc(inode,
715 &BTRFS_I(inode)->io_tree,
716 start, end, NULL, 1, 1,
8b62b72b 717 1, 1, 1, 1, 0);
771ed689
CM
718 *nr_written = *nr_written +
719 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
720 *page_started = 1;
721 ret = 0;
722 goto out;
723 }
724 }
725
726 BUG_ON(disk_num_bytes >
727 btrfs_super_total_bytes(&root->fs_info->super_copy));
728
b917b7c3
CM
729
730 read_lock(&BTRFS_I(inode)->extent_tree.lock);
731 em = search_extent_mapping(&BTRFS_I(inode)->extent_tree,
732 start, num_bytes);
733 if (em) {
734 alloc_hint = em->block_start;
735 free_extent_map(em);
736 }
737 read_unlock(&BTRFS_I(inode)->extent_tree.lock);
771ed689
CM
738 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
739
d397712b 740 while (disk_num_bytes > 0) {
c8b97818 741 cur_alloc_size = min(disk_num_bytes, root->fs_info->max_extent);
e6dcd2dc 742 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
771ed689 743 root->sectorsize, 0, alloc_hint,
e6dcd2dc 744 (u64)-1, &ins, 1);
d397712b
CM
745 BUG_ON(ret);
746
e6dcd2dc
CM
747 em = alloc_extent_map(GFP_NOFS);
748 em->start = start;
445a6944 749 em->orig_start = em->start;
771ed689
CM
750 ram_size = ins.offset;
751 em->len = ins.offset;
c8b97818 752
e6dcd2dc 753 em->block_start = ins.objectid;
c8b97818 754 em->block_len = ins.offset;
e6dcd2dc 755 em->bdev = root->fs_info->fs_devices->latest_bdev;
7f3c74fb 756 set_bit(EXTENT_FLAG_PINNED, &em->flags);
c8b97818 757
d397712b 758 while (1) {
890871be 759 write_lock(&em_tree->lock);
e6dcd2dc 760 ret = add_extent_mapping(em_tree, em);
890871be 761 write_unlock(&em_tree->lock);
e6dcd2dc
CM
762 if (ret != -EEXIST) {
763 free_extent_map(em);
764 break;
765 }
766 btrfs_drop_extent_cache(inode, start,
c8b97818 767 start + ram_size - 1, 0);
e6dcd2dc
CM
768 }
769
98d20f67 770 cur_alloc_size = ins.offset;
e6dcd2dc 771 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
771ed689 772 ram_size, cur_alloc_size, 0);
e6dcd2dc 773 BUG_ON(ret);
c8b97818 774
17d217fe
YZ
775 if (root->root_key.objectid ==
776 BTRFS_DATA_RELOC_TREE_OBJECTID) {
777 ret = btrfs_reloc_clone_csums(inode, start,
778 cur_alloc_size);
779 BUG_ON(ret);
780 }
781
d397712b 782 if (disk_num_bytes < cur_alloc_size)
3b951516 783 break;
d397712b 784
c8b97818
CM
785 /* we're not doing compressed IO, don't unlock the first
786 * page (which the caller expects to stay locked), don't
787 * clear any dirty bits and don't set any writeback bits
8b62b72b
CM
788 *
789 * Do set the Private2 bit so we know this page was properly
790 * setup for writepage
c8b97818
CM
791 */
792 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
793 start, start + ram_size - 1,
771ed689 794 locked_page, unlock, 1,
8b62b72b 795 1, 0, 0, 0, 1);
c8b97818 796 disk_num_bytes -= cur_alloc_size;
c59f8951
CM
797 num_bytes -= cur_alloc_size;
798 alloc_hint = ins.objectid + ins.offset;
799 start += cur_alloc_size;
b888db2b 800 }
b888db2b 801out:
771ed689 802 ret = 0;
b888db2b 803 btrfs_end_transaction(trans, root);
c8b97818 804
be20aa9d 805 return ret;
771ed689 806}
c8b97818 807
771ed689
CM
808/*
809 * work queue call back to started compression on a file and pages
810 */
811static noinline void async_cow_start(struct btrfs_work *work)
812{
813 struct async_cow *async_cow;
814 int num_added = 0;
815 async_cow = container_of(work, struct async_cow, work);
816
817 compress_file_range(async_cow->inode, async_cow->locked_page,
818 async_cow->start, async_cow->end, async_cow,
819 &num_added);
820 if (num_added == 0)
821 async_cow->inode = NULL;
822}
823
824/*
825 * work queue call back to submit previously compressed pages
826 */
827static noinline void async_cow_submit(struct btrfs_work *work)
828{
829 struct async_cow *async_cow;
830 struct btrfs_root *root;
831 unsigned long nr_pages;
832
833 async_cow = container_of(work, struct async_cow, work);
834
835 root = async_cow->root;
836 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
837 PAGE_CACHE_SHIFT;
838
839 atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages);
840
841 if (atomic_read(&root->fs_info->async_delalloc_pages) <
842 5 * 1042 * 1024 &&
843 waitqueue_active(&root->fs_info->async_submit_wait))
844 wake_up(&root->fs_info->async_submit_wait);
845
d397712b 846 if (async_cow->inode)
771ed689 847 submit_compressed_extents(async_cow->inode, async_cow);
771ed689 848}
c8b97818 849
771ed689
CM
850static noinline void async_cow_free(struct btrfs_work *work)
851{
852 struct async_cow *async_cow;
853 async_cow = container_of(work, struct async_cow, work);
854 kfree(async_cow);
855}
856
857static int cow_file_range_async(struct inode *inode, struct page *locked_page,
858 u64 start, u64 end, int *page_started,
859 unsigned long *nr_written)
860{
861 struct async_cow *async_cow;
862 struct btrfs_root *root = BTRFS_I(inode)->root;
863 unsigned long nr_pages;
864 u64 cur_end;
865 int limit = 10 * 1024 * 1042;
866
771ed689 867 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED |
2c64c53d 868 EXTENT_DELALLOC, 1, 0, NULL, GFP_NOFS);
d397712b 869 while (start < end) {
771ed689
CM
870 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
871 async_cow->inode = inode;
872 async_cow->root = root;
873 async_cow->locked_page = locked_page;
874 async_cow->start = start;
875
6cbff00f 876 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
771ed689
CM
877 cur_end = end;
878 else
879 cur_end = min(end, start + 512 * 1024 - 1);
880
881 async_cow->end = cur_end;
882 INIT_LIST_HEAD(&async_cow->extents);
883
884 async_cow->work.func = async_cow_start;
885 async_cow->work.ordered_func = async_cow_submit;
886 async_cow->work.ordered_free = async_cow_free;
887 async_cow->work.flags = 0;
888
771ed689
CM
889 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
890 PAGE_CACHE_SHIFT;
891 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
892
893 btrfs_queue_worker(&root->fs_info->delalloc_workers,
894 &async_cow->work);
895
896 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
897 wait_event(root->fs_info->async_submit_wait,
898 (atomic_read(&root->fs_info->async_delalloc_pages) <
899 limit));
900 }
901
d397712b 902 while (atomic_read(&root->fs_info->async_submit_draining) &&
771ed689
CM
903 atomic_read(&root->fs_info->async_delalloc_pages)) {
904 wait_event(root->fs_info->async_submit_wait,
905 (atomic_read(&root->fs_info->async_delalloc_pages) ==
906 0));
907 }
908
909 *nr_written += nr_pages;
910 start = cur_end + 1;
911 }
912 *page_started = 1;
913 return 0;
be20aa9d
CM
914}
915
d397712b 916static noinline int csum_exist_in_range(struct btrfs_root *root,
17d217fe
YZ
917 u64 bytenr, u64 num_bytes)
918{
919 int ret;
920 struct btrfs_ordered_sum *sums;
921 LIST_HEAD(list);
922
07d400a6
YZ
923 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
924 bytenr + num_bytes - 1, &list);
17d217fe
YZ
925 if (ret == 0 && list_empty(&list))
926 return 0;
927
928 while (!list_empty(&list)) {
929 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
930 list_del(&sums->list);
931 kfree(sums);
932 }
933 return 1;
934}
935
d352ac68
CM
936/*
937 * when nowcow writeback call back. This checks for snapshots or COW copies
938 * of the extents that exist in the file, and COWs the file as required.
939 *
940 * If no cow copies or snapshots exist, we write directly to the existing
941 * blocks on disk
942 */
7f366cfe
CM
943static noinline int run_delalloc_nocow(struct inode *inode,
944 struct page *locked_page,
771ed689
CM
945 u64 start, u64 end, int *page_started, int force,
946 unsigned long *nr_written)
be20aa9d 947{
be20aa9d 948 struct btrfs_root *root = BTRFS_I(inode)->root;
7ea394f1 949 struct btrfs_trans_handle *trans;
be20aa9d 950 struct extent_buffer *leaf;
be20aa9d 951 struct btrfs_path *path;
80ff3856 952 struct btrfs_file_extent_item *fi;
be20aa9d 953 struct btrfs_key found_key;
80ff3856
YZ
954 u64 cow_start;
955 u64 cur_offset;
956 u64 extent_end;
5d4f98a2 957 u64 extent_offset;
80ff3856
YZ
958 u64 disk_bytenr;
959 u64 num_bytes;
960 int extent_type;
961 int ret;
d899e052 962 int type;
80ff3856
YZ
963 int nocow;
964 int check_prev = 1;
be20aa9d
CM
965
966 path = btrfs_alloc_path();
967 BUG_ON(!path);
7ea394f1
YZ
968 trans = btrfs_join_transaction(root, 1);
969 BUG_ON(!trans);
be20aa9d 970
80ff3856
YZ
971 cow_start = (u64)-1;
972 cur_offset = start;
973 while (1) {
974 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
975 cur_offset, 0);
976 BUG_ON(ret < 0);
977 if (ret > 0 && path->slots[0] > 0 && check_prev) {
978 leaf = path->nodes[0];
979 btrfs_item_key_to_cpu(leaf, &found_key,
980 path->slots[0] - 1);
981 if (found_key.objectid == inode->i_ino &&
982 found_key.type == BTRFS_EXTENT_DATA_KEY)
983 path->slots[0]--;
984 }
985 check_prev = 0;
986next_slot:
987 leaf = path->nodes[0];
988 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
989 ret = btrfs_next_leaf(root, path);
990 if (ret < 0)
991 BUG_ON(1);
992 if (ret > 0)
993 break;
994 leaf = path->nodes[0];
995 }
be20aa9d 996
80ff3856
YZ
997 nocow = 0;
998 disk_bytenr = 0;
17d217fe 999 num_bytes = 0;
80ff3856
YZ
1000 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1001
1002 if (found_key.objectid > inode->i_ino ||
1003 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1004 found_key.offset > end)
1005 break;
1006
1007 if (found_key.offset > cur_offset) {
1008 extent_end = found_key.offset;
1009 goto out_check;
1010 }
1011
1012 fi = btrfs_item_ptr(leaf, path->slots[0],
1013 struct btrfs_file_extent_item);
1014 extent_type = btrfs_file_extent_type(leaf, fi);
1015
d899e052
YZ
1016 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1017 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
80ff3856 1018 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
5d4f98a2 1019 extent_offset = btrfs_file_extent_offset(leaf, fi);
80ff3856
YZ
1020 extent_end = found_key.offset +
1021 btrfs_file_extent_num_bytes(leaf, fi);
1022 if (extent_end <= start) {
1023 path->slots[0]++;
1024 goto next_slot;
1025 }
17d217fe
YZ
1026 if (disk_bytenr == 0)
1027 goto out_check;
80ff3856
YZ
1028 if (btrfs_file_extent_compression(leaf, fi) ||
1029 btrfs_file_extent_encryption(leaf, fi) ||
1030 btrfs_file_extent_other_encoding(leaf, fi))
1031 goto out_check;
d899e052
YZ
1032 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1033 goto out_check;
d2fb3437 1034 if (btrfs_extent_readonly(root, disk_bytenr))
80ff3856 1035 goto out_check;
17d217fe 1036 if (btrfs_cross_ref_exist(trans, root, inode->i_ino,
5d4f98a2
YZ
1037 found_key.offset -
1038 extent_offset, disk_bytenr))
17d217fe 1039 goto out_check;
5d4f98a2 1040 disk_bytenr += extent_offset;
17d217fe
YZ
1041 disk_bytenr += cur_offset - found_key.offset;
1042 num_bytes = min(end + 1, extent_end) - cur_offset;
1043 /*
1044 * force cow if csum exists in the range.
1045 * this ensure that csum for a given extent are
1046 * either valid or do not exist.
1047 */
1048 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1049 goto out_check;
80ff3856
YZ
1050 nocow = 1;
1051 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1052 extent_end = found_key.offset +
1053 btrfs_file_extent_inline_len(leaf, fi);
1054 extent_end = ALIGN(extent_end, root->sectorsize);
1055 } else {
1056 BUG_ON(1);
1057 }
1058out_check:
1059 if (extent_end <= start) {
1060 path->slots[0]++;
1061 goto next_slot;
1062 }
1063 if (!nocow) {
1064 if (cow_start == (u64)-1)
1065 cow_start = cur_offset;
1066 cur_offset = extent_end;
1067 if (cur_offset > end)
1068 break;
1069 path->slots[0]++;
1070 goto next_slot;
7ea394f1
YZ
1071 }
1072
1073 btrfs_release_path(root, path);
80ff3856
YZ
1074 if (cow_start != (u64)-1) {
1075 ret = cow_file_range(inode, locked_page, cow_start,
771ed689
CM
1076 found_key.offset - 1, page_started,
1077 nr_written, 1);
80ff3856
YZ
1078 BUG_ON(ret);
1079 cow_start = (u64)-1;
7ea394f1 1080 }
80ff3856 1081
d899e052
YZ
1082 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1083 struct extent_map *em;
1084 struct extent_map_tree *em_tree;
1085 em_tree = &BTRFS_I(inode)->extent_tree;
1086 em = alloc_extent_map(GFP_NOFS);
1087 em->start = cur_offset;
445a6944 1088 em->orig_start = em->start;
d899e052
YZ
1089 em->len = num_bytes;
1090 em->block_len = num_bytes;
1091 em->block_start = disk_bytenr;
1092 em->bdev = root->fs_info->fs_devices->latest_bdev;
1093 set_bit(EXTENT_FLAG_PINNED, &em->flags);
1094 while (1) {
890871be 1095 write_lock(&em_tree->lock);
d899e052 1096 ret = add_extent_mapping(em_tree, em);
890871be 1097 write_unlock(&em_tree->lock);
d899e052
YZ
1098 if (ret != -EEXIST) {
1099 free_extent_map(em);
1100 break;
1101 }
1102 btrfs_drop_extent_cache(inode, em->start,
1103 em->start + em->len - 1, 0);
1104 }
1105 type = BTRFS_ORDERED_PREALLOC;
1106 } else {
1107 type = BTRFS_ORDERED_NOCOW;
1108 }
80ff3856
YZ
1109
1110 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
d899e052
YZ
1111 num_bytes, num_bytes, type);
1112 BUG_ON(ret);
771ed689 1113
d899e052
YZ
1114 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
1115 cur_offset, cur_offset + num_bytes - 1,
8b62b72b 1116 locked_page, 1, 1, 1, 0, 0, 0, 1);
80ff3856
YZ
1117 cur_offset = extent_end;
1118 if (cur_offset > end)
1119 break;
be20aa9d 1120 }
80ff3856
YZ
1121 btrfs_release_path(root, path);
1122
1123 if (cur_offset <= end && cow_start == (u64)-1)
1124 cow_start = cur_offset;
1125 if (cow_start != (u64)-1) {
1126 ret = cow_file_range(inode, locked_page, cow_start, end,
771ed689 1127 page_started, nr_written, 1);
80ff3856
YZ
1128 BUG_ON(ret);
1129 }
1130
1131 ret = btrfs_end_transaction(trans, root);
1132 BUG_ON(ret);
7ea394f1 1133 btrfs_free_path(path);
80ff3856 1134 return 0;
be20aa9d
CM
1135}
1136
d352ac68
CM
1137/*
1138 * extent_io.c call back to do delayed allocation processing
1139 */
c8b97818 1140static int run_delalloc_range(struct inode *inode, struct page *locked_page,
771ed689
CM
1141 u64 start, u64 end, int *page_started,
1142 unsigned long *nr_written)
be20aa9d 1143{
be20aa9d 1144 int ret;
7f366cfe 1145 struct btrfs_root *root = BTRFS_I(inode)->root;
a2135011 1146
6cbff00f 1147 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)
c8b97818 1148 ret = run_delalloc_nocow(inode, locked_page, start, end,
d397712b 1149 page_started, 1, nr_written);
6cbff00f 1150 else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)
d899e052 1151 ret = run_delalloc_nocow(inode, locked_page, start, end,
d397712b 1152 page_started, 0, nr_written);
7f366cfe
CM
1153 else if (!btrfs_test_opt(root, COMPRESS))
1154 ret = cow_file_range(inode, locked_page, start, end,
1155 page_started, nr_written, 1);
be20aa9d 1156 else
771ed689 1157 ret = cow_file_range_async(inode, locked_page, start, end,
d397712b 1158 page_started, nr_written);
b888db2b
CM
1159 return ret;
1160}
1161
d352ac68
CM
1162/*
1163 * extent_io.c set_bit_hook, used to track delayed allocation
1164 * bytes in this file, and to maintain the list of inodes that
1165 * have pending delalloc work to be done.
1166 */
b2950863 1167static int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
b0c68f8b 1168 unsigned long old, unsigned long bits)
291d673e 1169{
75eff68e
CM
1170 /*
1171 * set_bit and clear bit hooks normally require _irqsave/restore
1172 * but in this case, we are only testeing for the DELALLOC
1173 * bit, which is only set or cleared with irqs on
1174 */
b0c68f8b 1175 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
291d673e 1176 struct btrfs_root *root = BTRFS_I(inode)->root;
6a63209f 1177 btrfs_delalloc_reserve_space(root, inode, end - start + 1);
75eff68e 1178 spin_lock(&root->fs_info->delalloc_lock);
9069218d 1179 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
291d673e 1180 root->fs_info->delalloc_bytes += end - start + 1;
ea8c2819
CM
1181 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1182 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1183 &root->fs_info->delalloc_inodes);
1184 }
75eff68e 1185 spin_unlock(&root->fs_info->delalloc_lock);
291d673e
CM
1186 }
1187 return 0;
1188}
1189
d352ac68
CM
1190/*
1191 * extent_io.c clear_bit_hook, see set_bit_hook for why
1192 */
b2950863 1193static int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
b0c68f8b 1194 unsigned long old, unsigned long bits)
291d673e 1195{
75eff68e
CM
1196 /*
1197 * set_bit and clear bit hooks normally require _irqsave/restore
1198 * but in this case, we are only testeing for the DELALLOC
1199 * bit, which is only set or cleared with irqs on
1200 */
b0c68f8b 1201 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
291d673e 1202 struct btrfs_root *root = BTRFS_I(inode)->root;
bcbfce8a 1203
75eff68e 1204 spin_lock(&root->fs_info->delalloc_lock);
b0c68f8b 1205 if (end - start + 1 > root->fs_info->delalloc_bytes) {
d397712b
CM
1206 printk(KERN_INFO "btrfs warning: delalloc account "
1207 "%llu %llu\n",
1208 (unsigned long long)end - start + 1,
1209 (unsigned long long)
1210 root->fs_info->delalloc_bytes);
6a63209f 1211 btrfs_delalloc_free_space(root, inode, (u64)-1);
b0c68f8b 1212 root->fs_info->delalloc_bytes = 0;
9069218d 1213 BTRFS_I(inode)->delalloc_bytes = 0;
b0c68f8b 1214 } else {
6a63209f
JB
1215 btrfs_delalloc_free_space(root, inode,
1216 end - start + 1);
b0c68f8b 1217 root->fs_info->delalloc_bytes -= end - start + 1;
9069218d 1218 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
b0c68f8b 1219 }
ea8c2819
CM
1220 if (BTRFS_I(inode)->delalloc_bytes == 0 &&
1221 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1222 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1223 }
75eff68e 1224 spin_unlock(&root->fs_info->delalloc_lock);
291d673e
CM
1225 }
1226 return 0;
1227}
1228
d352ac68
CM
1229/*
1230 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1231 * we don't create bios that span stripes or chunks
1232 */
239b14b3 1233int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
c8b97818
CM
1234 size_t size, struct bio *bio,
1235 unsigned long bio_flags)
239b14b3
CM
1236{
1237 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1238 struct btrfs_mapping_tree *map_tree;
a62b9401 1239 u64 logical = (u64)bio->bi_sector << 9;
239b14b3
CM
1240 u64 length = 0;
1241 u64 map_length;
239b14b3
CM
1242 int ret;
1243
771ed689
CM
1244 if (bio_flags & EXTENT_BIO_COMPRESSED)
1245 return 0;
1246
f2d8d74d 1247 length = bio->bi_size;
239b14b3
CM
1248 map_tree = &root->fs_info->mapping_tree;
1249 map_length = length;
cea9e445 1250 ret = btrfs_map_block(map_tree, READ, logical,
f188591e 1251 &map_length, NULL, 0);
cea9e445 1252
d397712b 1253 if (map_length < length + size)
239b14b3 1254 return 1;
239b14b3
CM
1255 return 0;
1256}
1257
d352ac68
CM
1258/*
1259 * in order to insert checksums into the metadata in large chunks,
1260 * we wait until bio submission time. All the pages in the bio are
1261 * checksummed and sums are attached onto the ordered extent record.
1262 *
1263 * At IO completion time the cums attached on the ordered extent record
1264 * are inserted into the btree
1265 */
d397712b
CM
1266static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1267 struct bio *bio, int mirror_num,
1268 unsigned long bio_flags)
065631f6 1269{
065631f6 1270 struct btrfs_root *root = BTRFS_I(inode)->root;
065631f6 1271 int ret = 0;
e015640f 1272
d20f7043 1273 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
44b8bd7e 1274 BUG_ON(ret);
4a69a410
CM
1275 return 0;
1276}
e015640f 1277
4a69a410
CM
1278/*
1279 * in order to insert checksums into the metadata in large chunks,
1280 * we wait until bio submission time. All the pages in the bio are
1281 * checksummed and sums are attached onto the ordered extent record.
1282 *
1283 * At IO completion time the cums attached on the ordered extent record
1284 * are inserted into the btree
1285 */
b2950863 1286static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
4a69a410
CM
1287 int mirror_num, unsigned long bio_flags)
1288{
1289 struct btrfs_root *root = BTRFS_I(inode)->root;
8b712842 1290 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
44b8bd7e
CM
1291}
1292
d352ac68 1293/*
cad321ad
CM
1294 * extent_io.c submission hook. This does the right thing for csum calculation
1295 * on write, or reading the csums from the tree before a read
d352ac68 1296 */
b2950863 1297static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
c8b97818 1298 int mirror_num, unsigned long bio_flags)
44b8bd7e
CM
1299{
1300 struct btrfs_root *root = BTRFS_I(inode)->root;
1301 int ret = 0;
19b9bdb0 1302 int skip_sum;
44b8bd7e 1303
6cbff00f 1304 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
cad321ad 1305
e6dcd2dc
CM
1306 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
1307 BUG_ON(ret);
065631f6 1308
4d1b5fb4 1309 if (!(rw & (1 << BIO_RW))) {
d20f7043 1310 if (bio_flags & EXTENT_BIO_COMPRESSED) {
c8b97818
CM
1311 return btrfs_submit_compressed_read(inode, bio,
1312 mirror_num, bio_flags);
d20f7043
CM
1313 } else if (!skip_sum)
1314 btrfs_lookup_bio_sums(root, inode, bio, NULL);
4d1b5fb4 1315 goto mapit;
19b9bdb0 1316 } else if (!skip_sum) {
17d217fe
YZ
1317 /* csum items have already been cloned */
1318 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1319 goto mapit;
19b9bdb0
CM
1320 /* we're doing a write, do the async checksumming */
1321 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
44b8bd7e 1322 inode, rw, bio, mirror_num,
4a69a410
CM
1323 bio_flags, __btrfs_submit_bio_start,
1324 __btrfs_submit_bio_done);
19b9bdb0
CM
1325 }
1326
0b86a832 1327mapit:
8b712842 1328 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
065631f6 1329}
6885f308 1330
d352ac68
CM
1331/*
1332 * given a list of ordered sums record them in the inode. This happens
1333 * at IO completion time based on sums calculated at bio submission time.
1334 */
ba1da2f4 1335static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
e6dcd2dc
CM
1336 struct inode *inode, u64 file_offset,
1337 struct list_head *list)
1338{
e6dcd2dc
CM
1339 struct btrfs_ordered_sum *sum;
1340
1341 btrfs_set_trans_block_group(trans, inode);
c6e30871
QF
1342
1343 list_for_each_entry(sum, list, list) {
d20f7043
CM
1344 btrfs_csum_file_blocks(trans,
1345 BTRFS_I(inode)->root->fs_info->csum_root, sum);
e6dcd2dc
CM
1346 }
1347 return 0;
1348}
1349
ea8c2819
CM
1350int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end)
1351{
d397712b 1352 if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
771ed689 1353 WARN_ON(1);
ea8c2819
CM
1354 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
1355 GFP_NOFS);
1356}
1357
d352ac68 1358/* see btrfs_writepage_start_hook for details on why this is required */
247e743c
CM
1359struct btrfs_writepage_fixup {
1360 struct page *page;
1361 struct btrfs_work work;
1362};
1363
b2950863 1364static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
247e743c
CM
1365{
1366 struct btrfs_writepage_fixup *fixup;
1367 struct btrfs_ordered_extent *ordered;
1368 struct page *page;
1369 struct inode *inode;
1370 u64 page_start;
1371 u64 page_end;
1372
1373 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1374 page = fixup->page;
4a096752 1375again:
247e743c
CM
1376 lock_page(page);
1377 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1378 ClearPageChecked(page);
1379 goto out_page;
1380 }
1381
1382 inode = page->mapping->host;
1383 page_start = page_offset(page);
1384 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1385
1386 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
4a096752
CM
1387
1388 /* already ordered? We're done */
8b62b72b 1389 if (PagePrivate2(page))
247e743c 1390 goto out;
4a096752
CM
1391
1392 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1393 if (ordered) {
1394 unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
1395 page_end, GFP_NOFS);
1396 unlock_page(page);
1397 btrfs_start_ordered_extent(inode, ordered, 1);
1398 goto again;
1399 }
247e743c 1400
ea8c2819 1401 btrfs_set_extent_delalloc(inode, page_start, page_end);
247e743c
CM
1402 ClearPageChecked(page);
1403out:
1404 unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
1405out_page:
1406 unlock_page(page);
1407 page_cache_release(page);
1408}
1409
1410/*
1411 * There are a few paths in the higher layers of the kernel that directly
1412 * set the page dirty bit without asking the filesystem if it is a
1413 * good idea. This causes problems because we want to make sure COW
1414 * properly happens and the data=ordered rules are followed.
1415 *
c8b97818 1416 * In our case any range that doesn't have the ORDERED bit set
247e743c
CM
1417 * hasn't been properly setup for IO. We kick off an async process
1418 * to fix it up. The async helper will wait for ordered extents, set
1419 * the delalloc bit and make it safe to write the page.
1420 */
b2950863 1421static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
247e743c
CM
1422{
1423 struct inode *inode = page->mapping->host;
1424 struct btrfs_writepage_fixup *fixup;
1425 struct btrfs_root *root = BTRFS_I(inode)->root;
247e743c 1426
8b62b72b
CM
1427 /* this page is properly in the ordered list */
1428 if (TestClearPagePrivate2(page))
247e743c
CM
1429 return 0;
1430
1431 if (PageChecked(page))
1432 return -EAGAIN;
1433
1434 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1435 if (!fixup)
1436 return -EAGAIN;
f421950f 1437
247e743c
CM
1438 SetPageChecked(page);
1439 page_cache_get(page);
1440 fixup->work.func = btrfs_writepage_fixup_worker;
1441 fixup->page = page;
1442 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
1443 return -EAGAIN;
1444}
1445
d899e052
YZ
1446static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1447 struct inode *inode, u64 file_pos,
1448 u64 disk_bytenr, u64 disk_num_bytes,
1449 u64 num_bytes, u64 ram_bytes,
e980b50c 1450 u64 locked_end,
d899e052
YZ
1451 u8 compression, u8 encryption,
1452 u16 other_encoding, int extent_type)
1453{
1454 struct btrfs_root *root = BTRFS_I(inode)->root;
1455 struct btrfs_file_extent_item *fi;
1456 struct btrfs_path *path;
1457 struct extent_buffer *leaf;
1458 struct btrfs_key ins;
1459 u64 hint;
1460 int ret;
1461
1462 path = btrfs_alloc_path();
1463 BUG_ON(!path);
1464
b9473439 1465 path->leave_spinning = 1;
a1ed835e
CM
1466
1467 /*
1468 * we may be replacing one extent in the tree with another.
1469 * The new extent is pinned in the extent map, and we don't want
1470 * to drop it from the cache until it is completely in the btree.
1471 *
1472 * So, tell btrfs_drop_extents to leave this extent in the cache.
1473 * the caller is expected to unpin it and allow it to be merged
1474 * with the others.
1475 */
d899e052 1476 ret = btrfs_drop_extents(trans, root, inode, file_pos,
e980b50c 1477 file_pos + num_bytes, locked_end,
a1ed835e 1478 file_pos, &hint, 0);
d899e052
YZ
1479 BUG_ON(ret);
1480
1481 ins.objectid = inode->i_ino;
1482 ins.offset = file_pos;
1483 ins.type = BTRFS_EXTENT_DATA_KEY;
1484 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
1485 BUG_ON(ret);
1486 leaf = path->nodes[0];
1487 fi = btrfs_item_ptr(leaf, path->slots[0],
1488 struct btrfs_file_extent_item);
1489 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1490 btrfs_set_file_extent_type(leaf, fi, extent_type);
1491 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1492 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1493 btrfs_set_file_extent_offset(leaf, fi, 0);
1494 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1495 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1496 btrfs_set_file_extent_compression(leaf, fi, compression);
1497 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1498 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
b9473439
CM
1499
1500 btrfs_unlock_up_safe(path, 1);
1501 btrfs_set_lock_blocking(leaf);
1502
d899e052
YZ
1503 btrfs_mark_buffer_dirty(leaf);
1504
1505 inode_add_bytes(inode, num_bytes);
d899e052
YZ
1506
1507 ins.objectid = disk_bytenr;
1508 ins.offset = disk_num_bytes;
1509 ins.type = BTRFS_EXTENT_ITEM_KEY;
5d4f98a2
YZ
1510 ret = btrfs_alloc_reserved_file_extent(trans, root,
1511 root->root_key.objectid,
1512 inode->i_ino, file_pos, &ins);
d899e052 1513 BUG_ON(ret);
d899e052 1514 btrfs_free_path(path);
b9473439 1515
d899e052
YZ
1516 return 0;
1517}
1518
5d13a98f
CM
1519/*
1520 * helper function for btrfs_finish_ordered_io, this
1521 * just reads in some of the csum leaves to prime them into ram
1522 * before we start the transaction. It limits the amount of btree
1523 * reads required while inside the transaction.
1524 */
1525static noinline void reada_csum(struct btrfs_root *root,
1526 struct btrfs_path *path,
1527 struct btrfs_ordered_extent *ordered_extent)
1528{
1529 struct btrfs_ordered_sum *sum;
1530 u64 bytenr;
1531
1532 sum = list_entry(ordered_extent->list.next, struct btrfs_ordered_sum,
1533 list);
1534 bytenr = sum->sums[0].bytenr;
1535
1536 /*
1537 * we don't care about the results, the point of this search is
1538 * just to get the btree leaves into ram
1539 */
1540 btrfs_lookup_csum(NULL, root->fs_info->csum_root, path, bytenr, 0);
1541}
1542
d352ac68
CM
1543/* as ordered data IO finishes, this gets called so we can finish
1544 * an ordered extent if the range of bytes in the file it covers are
1545 * fully written.
1546 */
211f90e6 1547static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
e6dcd2dc 1548{
e6dcd2dc
CM
1549 struct btrfs_root *root = BTRFS_I(inode)->root;
1550 struct btrfs_trans_handle *trans;
5d13a98f 1551 struct btrfs_ordered_extent *ordered_extent = NULL;
e6dcd2dc 1552 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
b7ec40d7 1553 struct btrfs_path *path;
d899e052 1554 int compressed = 0;
e6dcd2dc
CM
1555 int ret;
1556
1557 ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
ba1da2f4 1558 if (!ret)
e6dcd2dc 1559 return 0;
e6dcd2dc 1560
b7ec40d7
CM
1561 /*
1562 * before we join the transaction, try to do some of our IO.
1563 * This will limit the amount of IO that we have to do with
1564 * the transaction running. We're unlikely to need to do any
1565 * IO if the file extents are new, the disk_i_size checks
1566 * covers the most common case.
1567 */
1568 if (start < BTRFS_I(inode)->disk_i_size) {
1569 path = btrfs_alloc_path();
1570 if (path) {
1571 ret = btrfs_lookup_file_extent(NULL, root, path,
1572 inode->i_ino,
1573 start, 0);
5d13a98f
CM
1574 ordered_extent = btrfs_lookup_ordered_extent(inode,
1575 start);
1576 if (!list_empty(&ordered_extent->list)) {
1577 btrfs_release_path(root, path);
1578 reada_csum(root, path, ordered_extent);
1579 }
b7ec40d7
CM
1580 btrfs_free_path(path);
1581 }
1582 }
1583
f9295749 1584 trans = btrfs_join_transaction(root, 1);
e6dcd2dc 1585
5d13a98f
CM
1586 if (!ordered_extent)
1587 ordered_extent = btrfs_lookup_ordered_extent(inode, start);
e6dcd2dc 1588 BUG_ON(!ordered_extent);
7ea394f1
YZ
1589 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags))
1590 goto nocow;
e6dcd2dc
CM
1591
1592 lock_extent(io_tree, ordered_extent->file_offset,
1593 ordered_extent->file_offset + ordered_extent->len - 1,
1594 GFP_NOFS);
1595
c8b97818 1596 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
d899e052
YZ
1597 compressed = 1;
1598 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
1599 BUG_ON(compressed);
1600 ret = btrfs_mark_extent_written(trans, root, inode,
1601 ordered_extent->file_offset,
1602 ordered_extent->file_offset +
1603 ordered_extent->len);
1604 BUG_ON(ret);
1605 } else {
1606 ret = insert_reserved_file_extent(trans, inode,
1607 ordered_extent->file_offset,
1608 ordered_extent->start,
1609 ordered_extent->disk_len,
1610 ordered_extent->len,
1611 ordered_extent->len,
e980b50c
CM
1612 ordered_extent->file_offset +
1613 ordered_extent->len,
d899e052
YZ
1614 compressed, 0, 0,
1615 BTRFS_FILE_EXTENT_REG);
a1ed835e
CM
1616 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
1617 ordered_extent->file_offset,
1618 ordered_extent->len);
d899e052
YZ
1619 BUG_ON(ret);
1620 }
e6dcd2dc
CM
1621 unlock_extent(io_tree, ordered_extent->file_offset,
1622 ordered_extent->file_offset + ordered_extent->len - 1,
1623 GFP_NOFS);
7ea394f1 1624nocow:
e6dcd2dc
CM
1625 add_pending_csums(trans, inode, ordered_extent->file_offset,
1626 &ordered_extent->list);
1627
34353029 1628 mutex_lock(&BTRFS_I(inode)->extent_mutex);
dbe674a9 1629 btrfs_ordered_update_i_size(inode, ordered_extent);
e02119d5 1630 btrfs_update_inode(trans, root, inode);
e6dcd2dc 1631 btrfs_remove_ordered_extent(inode, ordered_extent);
34353029 1632 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
7f3c74fb 1633
e6dcd2dc
CM
1634 /* once for us */
1635 btrfs_put_ordered_extent(ordered_extent);
1636 /* once for the tree */
1637 btrfs_put_ordered_extent(ordered_extent);
1638
e6dcd2dc
CM
1639 btrfs_end_transaction(trans, root);
1640 return 0;
1641}
1642
b2950863 1643static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
211f90e6
CM
1644 struct extent_state *state, int uptodate)
1645{
8b62b72b 1646 ClearPagePrivate2(page);
211f90e6
CM
1647 return btrfs_finish_ordered_io(page->mapping->host, start, end);
1648}
1649
d352ac68
CM
1650/*
1651 * When IO fails, either with EIO or csum verification fails, we
1652 * try other mirrors that might have a good copy of the data. This
1653 * io_failure_record is used to record state as we go through all the
1654 * mirrors. If another mirror has good data, the page is set up to date
1655 * and things continue. If a good mirror can't be found, the original
1656 * bio end_io callback is called to indicate things have failed.
1657 */
7e38326f
CM
1658struct io_failure_record {
1659 struct page *page;
1660 u64 start;
1661 u64 len;
1662 u64 logical;
d20f7043 1663 unsigned long bio_flags;
7e38326f
CM
1664 int last_mirror;
1665};
1666
b2950863 1667static int btrfs_io_failed_hook(struct bio *failed_bio,
1259ab75
CM
1668 struct page *page, u64 start, u64 end,
1669 struct extent_state *state)
7e38326f
CM
1670{
1671 struct io_failure_record *failrec = NULL;
1672 u64 private;
1673 struct extent_map *em;
1674 struct inode *inode = page->mapping->host;
1675 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
3b951516 1676 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
7e38326f
CM
1677 struct bio *bio;
1678 int num_copies;
1679 int ret;
1259ab75 1680 int rw;
7e38326f
CM
1681 u64 logical;
1682
1683 ret = get_state_private(failure_tree, start, &private);
1684 if (ret) {
7e38326f
CM
1685 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
1686 if (!failrec)
1687 return -ENOMEM;
1688 failrec->start = start;
1689 failrec->len = end - start + 1;
1690 failrec->last_mirror = 0;
d20f7043 1691 failrec->bio_flags = 0;
7e38326f 1692
890871be 1693 read_lock(&em_tree->lock);
3b951516
CM
1694 em = lookup_extent_mapping(em_tree, start, failrec->len);
1695 if (em->start > start || em->start + em->len < start) {
1696 free_extent_map(em);
1697 em = NULL;
1698 }
890871be 1699 read_unlock(&em_tree->lock);
7e38326f
CM
1700
1701 if (!em || IS_ERR(em)) {
1702 kfree(failrec);
1703 return -EIO;
1704 }
1705 logical = start - em->start;
1706 logical = em->block_start + logical;
d20f7043
CM
1707 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
1708 logical = em->block_start;
1709 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
1710 }
7e38326f
CM
1711 failrec->logical = logical;
1712 free_extent_map(em);
1713 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
1714 EXTENT_DIRTY, GFP_NOFS);
587f7704
CM
1715 set_state_private(failure_tree, start,
1716 (u64)(unsigned long)failrec);
7e38326f 1717 } else {
587f7704 1718 failrec = (struct io_failure_record *)(unsigned long)private;
7e38326f
CM
1719 }
1720 num_copies = btrfs_num_copies(
1721 &BTRFS_I(inode)->root->fs_info->mapping_tree,
1722 failrec->logical, failrec->len);
1723 failrec->last_mirror++;
1724 if (!state) {
cad321ad 1725 spin_lock(&BTRFS_I(inode)->io_tree.lock);
7e38326f
CM
1726 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1727 failrec->start,
1728 EXTENT_LOCKED);
1729 if (state && state->start != failrec->start)
1730 state = NULL;
cad321ad 1731 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
7e38326f
CM
1732 }
1733 if (!state || failrec->last_mirror > num_copies) {
1734 set_state_private(failure_tree, failrec->start, 0);
1735 clear_extent_bits(failure_tree, failrec->start,
1736 failrec->start + failrec->len - 1,
1737 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1738 kfree(failrec);
1739 return -EIO;
1740 }
1741 bio = bio_alloc(GFP_NOFS, 1);
1742 bio->bi_private = state;
1743 bio->bi_end_io = failed_bio->bi_end_io;
1744 bio->bi_sector = failrec->logical >> 9;
1745 bio->bi_bdev = failed_bio->bi_bdev;
e1c4b745 1746 bio->bi_size = 0;
d20f7043 1747
7e38326f 1748 bio_add_page(bio, page, failrec->len, start - page_offset(page));
1259ab75
CM
1749 if (failed_bio->bi_rw & (1 << BIO_RW))
1750 rw = WRITE;
1751 else
1752 rw = READ;
1753
1754 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
c8b97818 1755 failrec->last_mirror,
d20f7043 1756 failrec->bio_flags);
1259ab75
CM
1757 return 0;
1758}
1759
d352ac68
CM
1760/*
1761 * each time an IO finishes, we do a fast check in the IO failure tree
1762 * to see if we need to process or clean up an io_failure_record
1763 */
b2950863 1764static int btrfs_clean_io_failures(struct inode *inode, u64 start)
1259ab75
CM
1765{
1766 u64 private;
1767 u64 private_failure;
1768 struct io_failure_record *failure;
1769 int ret;
1770
1771 private = 0;
1772 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1773 (u64)-1, 1, EXTENT_DIRTY)) {
1774 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
1775 start, &private_failure);
1776 if (ret == 0) {
1777 failure = (struct io_failure_record *)(unsigned long)
1778 private_failure;
1779 set_state_private(&BTRFS_I(inode)->io_failure_tree,
1780 failure->start, 0);
1781 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
1782 failure->start,
1783 failure->start + failure->len - 1,
1784 EXTENT_DIRTY | EXTENT_LOCKED,
1785 GFP_NOFS);
1786 kfree(failure);
1787 }
1788 }
7e38326f
CM
1789 return 0;
1790}
1791
d352ac68
CM
1792/*
1793 * when reads are done, we need to check csums to verify the data is correct
1794 * if there's a match, we allow the bio to finish. If not, we go through
1795 * the io_failure_record routines to find good copies
1796 */
b2950863 1797static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
70dec807 1798 struct extent_state *state)
07157aac 1799{
35ebb934 1800 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
07157aac 1801 struct inode *inode = page->mapping->host;
d1310b2e 1802 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
07157aac 1803 char *kaddr;
aadfeb6e 1804 u64 private = ~(u32)0;
07157aac 1805 int ret;
ff79f819
CM
1806 struct btrfs_root *root = BTRFS_I(inode)->root;
1807 u32 csum = ~(u32)0;
d1310b2e 1808
d20f7043
CM
1809 if (PageChecked(page)) {
1810 ClearPageChecked(page);
1811 goto good;
1812 }
6cbff00f
CH
1813
1814 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
17d217fe
YZ
1815 return 0;
1816
1817 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
9655d298 1818 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
17d217fe
YZ
1819 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
1820 GFP_NOFS);
b6cda9bc 1821 return 0;
17d217fe 1822 }
d20f7043 1823
c2e639f0 1824 if (state && state->start == start) {
70dec807
CM
1825 private = state->private;
1826 ret = 0;
1827 } else {
1828 ret = get_state_private(io_tree, start, &private);
1829 }
9ab86c8e 1830 kaddr = kmap_atomic(page, KM_USER0);
d397712b 1831 if (ret)
07157aac 1832 goto zeroit;
d397712b 1833
ff79f819
CM
1834 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
1835 btrfs_csum_final(csum, (char *)&csum);
d397712b 1836 if (csum != private)
07157aac 1837 goto zeroit;
d397712b 1838
9ab86c8e 1839 kunmap_atomic(kaddr, KM_USER0);
d20f7043 1840good:
7e38326f
CM
1841 /* if the io failure tree for this inode is non-empty,
1842 * check to see if we've recovered from a failed IO
1843 */
1259ab75 1844 btrfs_clean_io_failures(inode, start);
07157aac
CM
1845 return 0;
1846
1847zeroit:
193f284d
CM
1848 if (printk_ratelimit()) {
1849 printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u "
1850 "private %llu\n", page->mapping->host->i_ino,
1851 (unsigned long long)start, csum,
1852 (unsigned long long)private);
1853 }
db94535d
CM
1854 memset(kaddr + offset, 1, end - start + 1);
1855 flush_dcache_page(page);
9ab86c8e 1856 kunmap_atomic(kaddr, KM_USER0);
3b951516
CM
1857 if (private == 0)
1858 return 0;
7e38326f 1859 return -EIO;
07157aac 1860}
b888db2b 1861
7b128766
JB
1862/*
1863 * This creates an orphan entry for the given inode in case something goes
1864 * wrong in the middle of an unlink/truncate.
1865 */
1866int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
1867{
1868 struct btrfs_root *root = BTRFS_I(inode)->root;
1869 int ret = 0;
1870
bcc63abb 1871 spin_lock(&root->list_lock);
7b128766
JB
1872
1873 /* already on the orphan list, we're good */
1874 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
bcc63abb 1875 spin_unlock(&root->list_lock);
7b128766
JB
1876 return 0;
1877 }
1878
1879 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
1880
bcc63abb 1881 spin_unlock(&root->list_lock);
7b128766
JB
1882
1883 /*
1884 * insert an orphan item to track this unlinked/truncated file
1885 */
1886 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
1887
1888 return ret;
1889}
1890
1891/*
1892 * We have done the truncate/delete so we can go ahead and remove the orphan
1893 * item for this particular inode.
1894 */
1895int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
1896{
1897 struct btrfs_root *root = BTRFS_I(inode)->root;
1898 int ret = 0;
1899
bcc63abb 1900 spin_lock(&root->list_lock);
7b128766
JB
1901
1902 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
bcc63abb 1903 spin_unlock(&root->list_lock);
7b128766
JB
1904 return 0;
1905 }
1906
1907 list_del_init(&BTRFS_I(inode)->i_orphan);
1908 if (!trans) {
bcc63abb 1909 spin_unlock(&root->list_lock);
7b128766
JB
1910 return 0;
1911 }
1912
bcc63abb 1913 spin_unlock(&root->list_lock);
7b128766
JB
1914
1915 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
1916
1917 return ret;
1918}
1919
1920/*
1921 * this cleans up any orphans that may be left on the list from the last use
1922 * of this root.
1923 */
1924void btrfs_orphan_cleanup(struct btrfs_root *root)
1925{
1926 struct btrfs_path *path;
1927 struct extent_buffer *leaf;
1928 struct btrfs_item *item;
1929 struct btrfs_key key, found_key;
1930 struct btrfs_trans_handle *trans;
1931 struct inode *inode;
1932 int ret = 0, nr_unlink = 0, nr_truncate = 0;
1933
7b128766
JB
1934 path = btrfs_alloc_path();
1935 if (!path)
1936 return;
1937 path->reada = -1;
1938
1939 key.objectid = BTRFS_ORPHAN_OBJECTID;
1940 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
1941 key.offset = (u64)-1;
1942
7b128766
JB
1943
1944 while (1) {
1945 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1946 if (ret < 0) {
1947 printk(KERN_ERR "Error searching slot for orphan: %d"
1948 "\n", ret);
1949 break;
1950 }
1951
1952 /*
1953 * if ret == 0 means we found what we were searching for, which
1954 * is weird, but possible, so only screw with path if we didnt
1955 * find the key and see if we have stuff that matches
1956 */
1957 if (ret > 0) {
1958 if (path->slots[0] == 0)
1959 break;
1960 path->slots[0]--;
1961 }
1962
1963 /* pull out the item */
1964 leaf = path->nodes[0];
1965 item = btrfs_item_nr(leaf, path->slots[0]);
1966 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1967
1968 /* make sure the item matches what we want */
1969 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
1970 break;
1971 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
1972 break;
1973
1974 /* release the path since we're done with it */
1975 btrfs_release_path(root, path);
1976
1977 /*
1978 * this is where we are basically btrfs_lookup, without the
1979 * crossing root thing. we store the inode number in the
1980 * offset of the orphan item.
1981 */
5d4f98a2
YZ
1982 found_key.objectid = found_key.offset;
1983 found_key.type = BTRFS_INODE_ITEM_KEY;
1984 found_key.offset = 0;
1985 inode = btrfs_iget(root->fs_info->sb, &found_key, root);
1986 if (IS_ERR(inode))
7b128766
JB
1987 break;
1988
7b128766
JB
1989 /*
1990 * add this inode to the orphan list so btrfs_orphan_del does
1991 * the proper thing when we hit it
1992 */
bcc63abb 1993 spin_lock(&root->list_lock);
7b128766 1994 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
bcc63abb 1995 spin_unlock(&root->list_lock);
7b128766
JB
1996
1997 /*
1998 * if this is a bad inode, means we actually succeeded in
1999 * removing the inode, but not the orphan record, which means
2000 * we need to manually delete the orphan since iput will just
2001 * do a destroy_inode
2002 */
2003 if (is_bad_inode(inode)) {
5b21f2ed 2004 trans = btrfs_start_transaction(root, 1);
7b128766 2005 btrfs_orphan_del(trans, inode);
5b21f2ed 2006 btrfs_end_transaction(trans, root);
7b128766
JB
2007 iput(inode);
2008 continue;
2009 }
2010
2011 /* if we have links, this was a truncate, lets do that */
2012 if (inode->i_nlink) {
2013 nr_truncate++;
2014 btrfs_truncate(inode);
2015 } else {
2016 nr_unlink++;
2017 }
2018
2019 /* this will do delete_inode and everything for us */
2020 iput(inode);
2021 }
2022
2023 if (nr_unlink)
2024 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2025 if (nr_truncate)
2026 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
2027
2028 btrfs_free_path(path);
7b128766
JB
2029}
2030
46a53cca
CM
2031/*
2032 * very simple check to peek ahead in the leaf looking for xattrs. If we
2033 * don't find any xattrs, we know there can't be any acls.
2034 *
2035 * slot is the slot the inode is in, objectid is the objectid of the inode
2036 */
2037static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2038 int slot, u64 objectid)
2039{
2040 u32 nritems = btrfs_header_nritems(leaf);
2041 struct btrfs_key found_key;
2042 int scanned = 0;
2043
2044 slot++;
2045 while (slot < nritems) {
2046 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2047
2048 /* we found a different objectid, there must not be acls */
2049 if (found_key.objectid != objectid)
2050 return 0;
2051
2052 /* we found an xattr, assume we've got an acl */
2053 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
2054 return 1;
2055
2056 /*
2057 * we found a key greater than an xattr key, there can't
2058 * be any acls later on
2059 */
2060 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
2061 return 0;
2062
2063 slot++;
2064 scanned++;
2065
2066 /*
2067 * it goes inode, inode backrefs, xattrs, extents,
2068 * so if there are a ton of hard links to an inode there can
2069 * be a lot of backrefs. Don't waste time searching too hard,
2070 * this is just an optimization
2071 */
2072 if (scanned >= 8)
2073 break;
2074 }
2075 /* we hit the end of the leaf before we found an xattr or
2076 * something larger than an xattr. We have to assume the inode
2077 * has acls
2078 */
2079 return 1;
2080}
2081
d352ac68
CM
2082/*
2083 * read an inode from the btree into the in-memory inode
2084 */
5d4f98a2 2085static void btrfs_read_locked_inode(struct inode *inode)
39279cc3
CM
2086{
2087 struct btrfs_path *path;
5f39d397 2088 struct extent_buffer *leaf;
39279cc3 2089 struct btrfs_inode_item *inode_item;
0b86a832 2090 struct btrfs_timespec *tspec;
39279cc3
CM
2091 struct btrfs_root *root = BTRFS_I(inode)->root;
2092 struct btrfs_key location;
46a53cca 2093 int maybe_acls;
39279cc3 2094 u64 alloc_group_block;
618e21d5 2095 u32 rdev;
39279cc3
CM
2096 int ret;
2097
2098 path = btrfs_alloc_path();
2099 BUG_ON(!path);
39279cc3 2100 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
dc17ff8f 2101
39279cc3 2102 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
5f39d397 2103 if (ret)
39279cc3 2104 goto make_bad;
39279cc3 2105
5f39d397
CM
2106 leaf = path->nodes[0];
2107 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2108 struct btrfs_inode_item);
2109
2110 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
2111 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
2112 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
2113 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
dbe674a9 2114 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
5f39d397
CM
2115
2116 tspec = btrfs_inode_atime(inode_item);
2117 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2118 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2119
2120 tspec = btrfs_inode_mtime(inode_item);
2121 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2122 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2123
2124 tspec = btrfs_inode_ctime(inode_item);
2125 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2126 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2127
a76a3cd4 2128 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
e02119d5 2129 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
c3027eb5 2130 BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
e02119d5 2131 inode->i_generation = BTRFS_I(inode)->generation;
618e21d5 2132 inode->i_rdev = 0;
5f39d397
CM
2133 rdev = btrfs_inode_rdev(leaf, inode_item);
2134
aec7477b 2135 BTRFS_I(inode)->index_cnt = (u64)-1;
d2fb3437 2136 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
aec7477b 2137
5f39d397 2138 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
b4ce94de 2139
46a53cca
CM
2140 /*
2141 * try to precache a NULL acl entry for files that don't have
2142 * any xattrs or acls
2143 */
2144 maybe_acls = acls_after_inode_item(leaf, path->slots[0], inode->i_ino);
72c04902
AV
2145 if (!maybe_acls)
2146 cache_no_acl(inode);
46a53cca 2147
d2fb3437
YZ
2148 BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0,
2149 alloc_group_block, 0);
39279cc3
CM
2150 btrfs_free_path(path);
2151 inode_item = NULL;
2152
39279cc3 2153 switch (inode->i_mode & S_IFMT) {
39279cc3
CM
2154 case S_IFREG:
2155 inode->i_mapping->a_ops = &btrfs_aops;
04160088 2156 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
d1310b2e 2157 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
39279cc3
CM
2158 inode->i_fop = &btrfs_file_operations;
2159 inode->i_op = &btrfs_file_inode_operations;
2160 break;
2161 case S_IFDIR:
2162 inode->i_fop = &btrfs_dir_file_operations;
2163 if (root == root->fs_info->tree_root)
2164 inode->i_op = &btrfs_dir_ro_inode_operations;
2165 else
2166 inode->i_op = &btrfs_dir_inode_operations;
2167 break;
2168 case S_IFLNK:
2169 inode->i_op = &btrfs_symlink_inode_operations;
2170 inode->i_mapping->a_ops = &btrfs_symlink_aops;
04160088 2171 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
39279cc3 2172 break;
618e21d5 2173 default:
0279b4cd 2174 inode->i_op = &btrfs_special_inode_operations;
618e21d5
JB
2175 init_special_inode(inode, inode->i_mode, rdev);
2176 break;
39279cc3 2177 }
6cbff00f
CH
2178
2179 btrfs_update_iflags(inode);
39279cc3
CM
2180 return;
2181
2182make_bad:
39279cc3 2183 btrfs_free_path(path);
39279cc3
CM
2184 make_bad_inode(inode);
2185}
2186
d352ac68
CM
2187/*
2188 * given a leaf and an inode, copy the inode fields into the leaf
2189 */
e02119d5
CM
2190static void fill_inode_item(struct btrfs_trans_handle *trans,
2191 struct extent_buffer *leaf,
5f39d397 2192 struct btrfs_inode_item *item,
39279cc3
CM
2193 struct inode *inode)
2194{
5f39d397
CM
2195 btrfs_set_inode_uid(leaf, item, inode->i_uid);
2196 btrfs_set_inode_gid(leaf, item, inode->i_gid);
dbe674a9 2197 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
5f39d397
CM
2198 btrfs_set_inode_mode(leaf, item, inode->i_mode);
2199 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2200
2201 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2202 inode->i_atime.tv_sec);
2203 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2204 inode->i_atime.tv_nsec);
2205
2206 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2207 inode->i_mtime.tv_sec);
2208 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2209 inode->i_mtime.tv_nsec);
2210
2211 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2212 inode->i_ctime.tv_sec);
2213 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2214 inode->i_ctime.tv_nsec);
2215
a76a3cd4 2216 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
e02119d5 2217 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
c3027eb5 2218 btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
e02119d5 2219 btrfs_set_inode_transid(leaf, item, trans->transid);
5f39d397 2220 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
b98b6767 2221 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
d2fb3437 2222 btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group);
39279cc3
CM
2223}
2224
d352ac68
CM
2225/*
2226 * copy everything in the in-memory inode into the btree.
2227 */
d397712b
CM
2228noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2229 struct btrfs_root *root, struct inode *inode)
39279cc3
CM
2230{
2231 struct btrfs_inode_item *inode_item;
2232 struct btrfs_path *path;
5f39d397 2233 struct extent_buffer *leaf;
39279cc3
CM
2234 int ret;
2235
2236 path = btrfs_alloc_path();
2237 BUG_ON(!path);
b9473439 2238 path->leave_spinning = 1;
39279cc3
CM
2239 ret = btrfs_lookup_inode(trans, root, path,
2240 &BTRFS_I(inode)->location, 1);
2241 if (ret) {
2242 if (ret > 0)
2243 ret = -ENOENT;
2244 goto failed;
2245 }
2246
b4ce94de 2247 btrfs_unlock_up_safe(path, 1);
5f39d397
CM
2248 leaf = path->nodes[0];
2249 inode_item = btrfs_item_ptr(leaf, path->slots[0],
39279cc3
CM
2250 struct btrfs_inode_item);
2251
e02119d5 2252 fill_inode_item(trans, leaf, inode_item, inode);
5f39d397 2253 btrfs_mark_buffer_dirty(leaf);
15ee9bc7 2254 btrfs_set_inode_last_trans(trans, inode);
39279cc3
CM
2255 ret = 0;
2256failed:
39279cc3
CM
2257 btrfs_free_path(path);
2258 return ret;
2259}
2260
2261
d352ac68
CM
2262/*
2263 * unlink helper that gets used here in inode.c and in the tree logging
2264 * recovery code. It remove a link in a directory with a given name, and
2265 * also drops the back refs in the inode to the directory
2266 */
e02119d5
CM
2267int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2268 struct btrfs_root *root,
2269 struct inode *dir, struct inode *inode,
2270 const char *name, int name_len)
39279cc3
CM
2271{
2272 struct btrfs_path *path;
39279cc3 2273 int ret = 0;
5f39d397 2274 struct extent_buffer *leaf;
39279cc3 2275 struct btrfs_dir_item *di;
5f39d397 2276 struct btrfs_key key;
aec7477b 2277 u64 index;
39279cc3
CM
2278
2279 path = btrfs_alloc_path();
54aa1f4d
CM
2280 if (!path) {
2281 ret = -ENOMEM;
2282 goto err;
2283 }
2284
b9473439 2285 path->leave_spinning = 1;
39279cc3
CM
2286 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2287 name, name_len, -1);
2288 if (IS_ERR(di)) {
2289 ret = PTR_ERR(di);
2290 goto err;
2291 }
2292 if (!di) {
2293 ret = -ENOENT;
2294 goto err;
2295 }
5f39d397
CM
2296 leaf = path->nodes[0];
2297 btrfs_dir_item_key_to_cpu(leaf, di, &key);
39279cc3 2298 ret = btrfs_delete_one_dir_name(trans, root, path, di);
54aa1f4d
CM
2299 if (ret)
2300 goto err;
39279cc3
CM
2301 btrfs_release_path(root, path);
2302
aec7477b 2303 ret = btrfs_del_inode_ref(trans, root, name, name_len,
e02119d5
CM
2304 inode->i_ino,
2305 dir->i_ino, &index);
aec7477b 2306 if (ret) {
d397712b 2307 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
aec7477b 2308 "inode %lu parent %lu\n", name_len, name,
e02119d5 2309 inode->i_ino, dir->i_ino);
aec7477b
JB
2310 goto err;
2311 }
2312
39279cc3 2313 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
aec7477b 2314 index, name, name_len, -1);
39279cc3
CM
2315 if (IS_ERR(di)) {
2316 ret = PTR_ERR(di);
2317 goto err;
2318 }
2319 if (!di) {
2320 ret = -ENOENT;
2321 goto err;
2322 }
2323 ret = btrfs_delete_one_dir_name(trans, root, path, di);
925baedd 2324 btrfs_release_path(root, path);
39279cc3 2325
e02119d5
CM
2326 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
2327 inode, dir->i_ino);
49eb7e46 2328 BUG_ON(ret != 0 && ret != -ENOENT);
e02119d5
CM
2329
2330 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2331 dir, index);
2332 BUG_ON(ret);
39279cc3
CM
2333err:
2334 btrfs_free_path(path);
e02119d5
CM
2335 if (ret)
2336 goto out;
2337
2338 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2339 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2340 btrfs_update_inode(trans, root, dir);
2341 btrfs_drop_nlink(inode);
2342 ret = btrfs_update_inode(trans, root, inode);
e02119d5 2343out:
39279cc3
CM
2344 return ret;
2345}
2346
2347static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2348{
2349 struct btrfs_root *root;
2350 struct btrfs_trans_handle *trans;
7b128766 2351 struct inode *inode = dentry->d_inode;
39279cc3 2352 int ret;
1832a6d5 2353 unsigned long nr = 0;
39279cc3
CM
2354
2355 root = BTRFS_I(dir)->root;
1832a6d5 2356
39279cc3 2357 trans = btrfs_start_transaction(root, 1);
5f39d397 2358
39279cc3 2359 btrfs_set_trans_block_group(trans, dir);
12fcfd22
CM
2360
2361 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
2362
e02119d5
CM
2363 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2364 dentry->d_name.name, dentry->d_name.len);
7b128766
JB
2365
2366 if (inode->i_nlink == 0)
2367 ret = btrfs_orphan_add(trans, inode);
2368
d3c2fdcf 2369 nr = trans->blocks_used;
5f39d397 2370
89ce8a63 2371 btrfs_end_transaction_throttle(trans, root);
d3c2fdcf 2372 btrfs_btree_balance_dirty(root, nr);
39279cc3
CM
2373 return ret;
2374}
2375
4df27c4d
YZ
2376int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
2377 struct btrfs_root *root,
2378 struct inode *dir, u64 objectid,
2379 const char *name, int name_len)
2380{
2381 struct btrfs_path *path;
2382 struct extent_buffer *leaf;
2383 struct btrfs_dir_item *di;
2384 struct btrfs_key key;
2385 u64 index;
2386 int ret;
2387
2388 path = btrfs_alloc_path();
2389 if (!path)
2390 return -ENOMEM;
2391
2392 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2393 name, name_len, -1);
2394 BUG_ON(!di || IS_ERR(di));
2395
2396 leaf = path->nodes[0];
2397 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2398 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
2399 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2400 BUG_ON(ret);
2401 btrfs_release_path(root, path);
2402
2403 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
2404 objectid, root->root_key.objectid,
2405 dir->i_ino, &index, name, name_len);
2406 if (ret < 0) {
2407 BUG_ON(ret != -ENOENT);
2408 di = btrfs_search_dir_index_item(root, path, dir->i_ino,
2409 name, name_len);
2410 BUG_ON(!di || IS_ERR(di));
2411
2412 leaf = path->nodes[0];
2413 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2414 btrfs_release_path(root, path);
2415 index = key.offset;
2416 }
2417
2418 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
2419 index, name, name_len, -1);
2420 BUG_ON(!di || IS_ERR(di));
2421
2422 leaf = path->nodes[0];
2423 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2424 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
2425 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2426 BUG_ON(ret);
2427 btrfs_release_path(root, path);
2428
2429 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2430 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2431 ret = btrfs_update_inode(trans, root, dir);
2432 BUG_ON(ret);
2433 dir->i_sb->s_dirt = 1;
2434
2435 btrfs_free_path(path);
2436 return 0;
2437}
2438
39279cc3
CM
2439static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
2440{
2441 struct inode *inode = dentry->d_inode;
1832a6d5 2442 int err = 0;
39279cc3
CM
2443 int ret;
2444 struct btrfs_root *root = BTRFS_I(dir)->root;
39279cc3 2445 struct btrfs_trans_handle *trans;
1832a6d5 2446 unsigned long nr = 0;
39279cc3 2447
3394e160 2448 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
4df27c4d 2449 inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
134d4512
Y
2450 return -ENOTEMPTY;
2451
39279cc3
CM
2452 trans = btrfs_start_transaction(root, 1);
2453 btrfs_set_trans_block_group(trans, dir);
39279cc3 2454
4df27c4d
YZ
2455 if (unlikely(inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
2456 err = btrfs_unlink_subvol(trans, root, dir,
2457 BTRFS_I(inode)->location.objectid,
2458 dentry->d_name.name,
2459 dentry->d_name.len);
2460 goto out;
2461 }
2462
7b128766
JB
2463 err = btrfs_orphan_add(trans, inode);
2464 if (err)
4df27c4d 2465 goto out;
7b128766 2466
39279cc3 2467 /* now the directory is empty */
e02119d5
CM
2468 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2469 dentry->d_name.name, dentry->d_name.len);
d397712b 2470 if (!err)
dbe674a9 2471 btrfs_i_size_write(inode, 0);
4df27c4d 2472out:
d3c2fdcf 2473 nr = trans->blocks_used;
89ce8a63 2474 ret = btrfs_end_transaction_throttle(trans, root);
d3c2fdcf 2475 btrfs_btree_balance_dirty(root, nr);
3954401f 2476
39279cc3
CM
2477 if (ret && !err)
2478 err = ret;
2479 return err;
2480}
2481
d20f7043 2482#if 0
323ac95b
CM
2483/*
2484 * when truncating bytes in a file, it is possible to avoid reading
2485 * the leaves that contain only checksum items. This can be the
2486 * majority of the IO required to delete a large file, but it must
2487 * be done carefully.
2488 *
2489 * The keys in the level just above the leaves are checked to make sure
2490 * the lowest key in a given leaf is a csum key, and starts at an offset
2491 * after the new size.
2492 *
2493 * Then the key for the next leaf is checked to make sure it also has
2494 * a checksum item for the same file. If it does, we know our target leaf
2495 * contains only checksum items, and it can be safely freed without reading
2496 * it.
2497 *
2498 * This is just an optimization targeted at large files. It may do
2499 * nothing. It will return 0 unless things went badly.
2500 */
2501static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
2502 struct btrfs_root *root,
2503 struct btrfs_path *path,
2504 struct inode *inode, u64 new_size)
2505{
2506 struct btrfs_key key;
2507 int ret;
2508 int nritems;
2509 struct btrfs_key found_key;
2510 struct btrfs_key other_key;
5b84e8d6
YZ
2511 struct btrfs_leaf_ref *ref;
2512 u64 leaf_gen;
2513 u64 leaf_start;
323ac95b
CM
2514
2515 path->lowest_level = 1;
2516 key.objectid = inode->i_ino;
2517 key.type = BTRFS_CSUM_ITEM_KEY;
2518 key.offset = new_size;
2519again:
2520 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2521 if (ret < 0)
2522 goto out;
2523
2524 if (path->nodes[1] == NULL) {
2525 ret = 0;
2526 goto out;
2527 }
2528 ret = 0;
2529 btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
2530 nritems = btrfs_header_nritems(path->nodes[1]);
2531
2532 if (!nritems)
2533 goto out;
2534
2535 if (path->slots[1] >= nritems)
2536 goto next_node;
2537
2538 /* did we find a key greater than anything we want to delete? */
2539 if (found_key.objectid > inode->i_ino ||
2540 (found_key.objectid == inode->i_ino && found_key.type > key.type))
2541 goto out;
2542
2543 /* we check the next key in the node to make sure the leave contains
2544 * only checksum items. This comparison doesn't work if our
2545 * leaf is the last one in the node
2546 */
2547 if (path->slots[1] + 1 >= nritems) {
2548next_node:
2549 /* search forward from the last key in the node, this
2550 * will bring us into the next node in the tree
2551 */
2552 btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
2553
2554 /* unlikely, but we inc below, so check to be safe */
2555 if (found_key.offset == (u64)-1)
2556 goto out;
2557
2558 /* search_forward needs a path with locks held, do the
2559 * search again for the original key. It is possible
2560 * this will race with a balance and return a path that
2561 * we could modify, but this drop is just an optimization
2562 * and is allowed to miss some leaves.
2563 */
2564 btrfs_release_path(root, path);
2565 found_key.offset++;
2566
2567 /* setup a max key for search_forward */
2568 other_key.offset = (u64)-1;
2569 other_key.type = key.type;
2570 other_key.objectid = key.objectid;
2571
2572 path->keep_locks = 1;
2573 ret = btrfs_search_forward(root, &found_key, &other_key,
2574 path, 0, 0);
2575 path->keep_locks = 0;
2576 if (ret || found_key.objectid != key.objectid ||
2577 found_key.type != key.type) {
2578 ret = 0;
2579 goto out;
2580 }
2581
2582 key.offset = found_key.offset;
2583 btrfs_release_path(root, path);
2584 cond_resched();
2585 goto again;
2586 }
2587
2588 /* we know there's one more slot after us in the tree,
2589 * read that key so we can verify it is also a checksum item
2590 */
2591 btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
2592
2593 if (found_key.objectid < inode->i_ino)
2594 goto next_key;
2595
2596 if (found_key.type != key.type || found_key.offset < new_size)
2597 goto next_key;
2598
2599 /*
2600 * if the key for the next leaf isn't a csum key from this objectid,
2601 * we can't be sure there aren't good items inside this leaf.
2602 * Bail out
2603 */
2604 if (other_key.objectid != inode->i_ino || other_key.type != key.type)
2605 goto out;
2606
5b84e8d6
YZ
2607 leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
2608 leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
323ac95b
CM
2609 /*
2610 * it is safe to delete this leaf, it contains only
2611 * csum items from this inode at an offset >= new_size
2612 */
5b84e8d6 2613 ret = btrfs_del_leaf(trans, root, path, leaf_start);
323ac95b
CM
2614 BUG_ON(ret);
2615
5b84e8d6
YZ
2616 if (root->ref_cows && leaf_gen < trans->transid) {
2617 ref = btrfs_alloc_leaf_ref(root, 0);
2618 if (ref) {
2619 ref->root_gen = root->root_key.offset;
2620 ref->bytenr = leaf_start;
2621 ref->owner = 0;
2622 ref->generation = leaf_gen;
2623 ref->nritems = 0;
2624
bd56b302
CM
2625 btrfs_sort_leaf_ref(ref);
2626
5b84e8d6
YZ
2627 ret = btrfs_add_leaf_ref(root, ref, 0);
2628 WARN_ON(ret);
2629 btrfs_free_leaf_ref(root, ref);
2630 } else {
2631 WARN_ON(1);
2632 }
2633 }
323ac95b
CM
2634next_key:
2635 btrfs_release_path(root, path);
2636
2637 if (other_key.objectid == inode->i_ino &&
2638 other_key.type == key.type && other_key.offset > key.offset) {
2639 key.offset = other_key.offset;
2640 cond_resched();
2641 goto again;
2642 }
2643 ret = 0;
2644out:
2645 /* fixup any changes we've made to the path */
2646 path->lowest_level = 0;
2647 path->keep_locks = 0;
2648 btrfs_release_path(root, path);
2649 return ret;
2650}
2651
d20f7043
CM
2652#endif
2653
39279cc3
CM
2654/*
2655 * this can truncate away extent items, csum items and directory items.
2656 * It starts at a high offset and removes keys until it can't find
d352ac68 2657 * any higher than new_size
39279cc3
CM
2658 *
2659 * csum items that cross the new i_size are truncated to the new size
2660 * as well.
7b128766
JB
2661 *
2662 * min_type is the minimum key type to truncate down to. If set to 0, this
2663 * will kill all the items on this inode, including the INODE_ITEM_KEY.
39279cc3 2664 */
e02119d5
CM
2665noinline int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
2666 struct btrfs_root *root,
2667 struct inode *inode,
2668 u64 new_size, u32 min_type)
39279cc3
CM
2669{
2670 int ret;
2671 struct btrfs_path *path;
2672 struct btrfs_key key;
5f39d397 2673 struct btrfs_key found_key;
06d9a8d7 2674 u32 found_type = (u8)-1;
5f39d397 2675 struct extent_buffer *leaf;
39279cc3
CM
2676 struct btrfs_file_extent_item *fi;
2677 u64 extent_start = 0;
db94535d 2678 u64 extent_num_bytes = 0;
5d4f98a2 2679 u64 extent_offset = 0;
39279cc3
CM
2680 u64 item_end = 0;
2681 int found_extent;
2682 int del_item;
85e21bac
CM
2683 int pending_del_nr = 0;
2684 int pending_del_slot = 0;
179e29e4 2685 int extent_type = -1;
771ed689 2686 int encoding;
3b951516 2687 u64 mask = root->sectorsize - 1;
39279cc3 2688
e02119d5 2689 if (root->ref_cows)
5b21f2ed 2690 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
39279cc3
CM
2691 path = btrfs_alloc_path();
2692 BUG_ON(!path);
33c17ad5 2693 path->reada = -1;
5f39d397 2694
39279cc3
CM
2695 /* FIXME, add redo link to tree so we don't leak on crash */
2696 key.objectid = inode->i_ino;
2697 key.offset = (u64)-1;
5f39d397
CM
2698 key.type = (u8)-1;
2699
85e21bac 2700search_again:
b9473439 2701 path->leave_spinning = 1;
85e21bac 2702 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
d397712b 2703 if (ret < 0)
85e21bac 2704 goto error;
d397712b 2705
85e21bac 2706 if (ret > 0) {
e02119d5
CM
2707 /* there are no items in the tree for us to truncate, we're
2708 * done
2709 */
2710 if (path->slots[0] == 0) {
2711 ret = 0;
2712 goto error;
2713 }
85e21bac
CM
2714 path->slots[0]--;
2715 }
2716
d397712b 2717 while (1) {
39279cc3 2718 fi = NULL;
5f39d397
CM
2719 leaf = path->nodes[0];
2720 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2721 found_type = btrfs_key_type(&found_key);
771ed689 2722 encoding = 0;
39279cc3 2723
5f39d397 2724 if (found_key.objectid != inode->i_ino)
39279cc3 2725 break;
5f39d397 2726
85e21bac 2727 if (found_type < min_type)
39279cc3
CM
2728 break;
2729
5f39d397 2730 item_end = found_key.offset;
39279cc3 2731 if (found_type == BTRFS_EXTENT_DATA_KEY) {
5f39d397 2732 fi = btrfs_item_ptr(leaf, path->slots[0],
39279cc3 2733 struct btrfs_file_extent_item);
179e29e4 2734 extent_type = btrfs_file_extent_type(leaf, fi);
771ed689
CM
2735 encoding = btrfs_file_extent_compression(leaf, fi);
2736 encoding |= btrfs_file_extent_encryption(leaf, fi);
2737 encoding |= btrfs_file_extent_other_encoding(leaf, fi);
2738
179e29e4 2739 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
5f39d397 2740 item_end +=
db94535d 2741 btrfs_file_extent_num_bytes(leaf, fi);
179e29e4 2742 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
179e29e4 2743 item_end += btrfs_file_extent_inline_len(leaf,
c8b97818 2744 fi);
39279cc3 2745 }
008630c1 2746 item_end--;
39279cc3 2747 }
e02119d5 2748 if (item_end < new_size) {
d397712b 2749 if (found_type == BTRFS_DIR_ITEM_KEY)
b888db2b 2750 found_type = BTRFS_INODE_ITEM_KEY;
d397712b 2751 else if (found_type == BTRFS_EXTENT_ITEM_KEY)
d20f7043 2752 found_type = BTRFS_EXTENT_DATA_KEY;
d397712b 2753 else if (found_type == BTRFS_EXTENT_DATA_KEY)
85e21bac 2754 found_type = BTRFS_XATTR_ITEM_KEY;
d397712b 2755 else if (found_type == BTRFS_XATTR_ITEM_KEY)
85e21bac 2756 found_type = BTRFS_INODE_REF_KEY;
d397712b 2757 else if (found_type)
b888db2b 2758 found_type--;
d397712b 2759 else
b888db2b 2760 break;
a61721d5 2761 btrfs_set_key_type(&key, found_type);
85e21bac 2762 goto next;
39279cc3 2763 }
e02119d5 2764 if (found_key.offset >= new_size)
39279cc3
CM
2765 del_item = 1;
2766 else
2767 del_item = 0;
2768 found_extent = 0;
2769
2770 /* FIXME, shrink the extent if the ref count is only 1 */
179e29e4
CM
2771 if (found_type != BTRFS_EXTENT_DATA_KEY)
2772 goto delete;
2773
2774 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
39279cc3 2775 u64 num_dec;
db94535d 2776 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
771ed689 2777 if (!del_item && !encoding) {
db94535d
CM
2778 u64 orig_num_bytes =
2779 btrfs_file_extent_num_bytes(leaf, fi);
e02119d5 2780 extent_num_bytes = new_size -
5f39d397 2781 found_key.offset + root->sectorsize - 1;
b1632b10
Y
2782 extent_num_bytes = extent_num_bytes &
2783 ~((u64)root->sectorsize - 1);
db94535d
CM
2784 btrfs_set_file_extent_num_bytes(leaf, fi,
2785 extent_num_bytes);
2786 num_dec = (orig_num_bytes -
9069218d 2787 extent_num_bytes);
e02119d5 2788 if (root->ref_cows && extent_start != 0)
a76a3cd4 2789 inode_sub_bytes(inode, num_dec);
5f39d397 2790 btrfs_mark_buffer_dirty(leaf);
39279cc3 2791 } else {
db94535d
CM
2792 extent_num_bytes =
2793 btrfs_file_extent_disk_num_bytes(leaf,
2794 fi);
5d4f98a2
YZ
2795 extent_offset = found_key.offset -
2796 btrfs_file_extent_offset(leaf, fi);
2797
39279cc3 2798 /* FIXME blocksize != 4096 */
9069218d 2799 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
39279cc3
CM
2800 if (extent_start != 0) {
2801 found_extent = 1;
e02119d5 2802 if (root->ref_cows)
a76a3cd4 2803 inode_sub_bytes(inode, num_dec);
e02119d5 2804 }
39279cc3 2805 }
9069218d 2806 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
c8b97818
CM
2807 /*
2808 * we can't truncate inline items that have had
2809 * special encodings
2810 */
2811 if (!del_item &&
2812 btrfs_file_extent_compression(leaf, fi) == 0 &&
2813 btrfs_file_extent_encryption(leaf, fi) == 0 &&
2814 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
e02119d5
CM
2815 u32 size = new_size - found_key.offset;
2816
2817 if (root->ref_cows) {
a76a3cd4
YZ
2818 inode_sub_bytes(inode, item_end + 1 -
2819 new_size);
e02119d5
CM
2820 }
2821 size =
2822 btrfs_file_extent_calc_inline_size(size);
9069218d 2823 ret = btrfs_truncate_item(trans, root, path,
e02119d5 2824 size, 1);
9069218d 2825 BUG_ON(ret);
e02119d5 2826 } else if (root->ref_cows) {
a76a3cd4
YZ
2827 inode_sub_bytes(inode, item_end + 1 -
2828 found_key.offset);
9069218d 2829 }
39279cc3 2830 }
179e29e4 2831delete:
39279cc3 2832 if (del_item) {
85e21bac
CM
2833 if (!pending_del_nr) {
2834 /* no pending yet, add ourselves */
2835 pending_del_slot = path->slots[0];
2836 pending_del_nr = 1;
2837 } else if (pending_del_nr &&
2838 path->slots[0] + 1 == pending_del_slot) {
2839 /* hop on the pending chunk */
2840 pending_del_nr++;
2841 pending_del_slot = path->slots[0];
2842 } else {
d397712b 2843 BUG();
85e21bac 2844 }
39279cc3
CM
2845 } else {
2846 break;
2847 }
5d4f98a2 2848 if (found_extent && root->ref_cows) {
b9473439 2849 btrfs_set_path_blocking(path);
39279cc3 2850 ret = btrfs_free_extent(trans, root, extent_start,
5d4f98a2
YZ
2851 extent_num_bytes, 0,
2852 btrfs_header_owner(leaf),
2853 inode->i_ino, extent_offset);
39279cc3
CM
2854 BUG_ON(ret);
2855 }
85e21bac
CM
2856next:
2857 if (path->slots[0] == 0) {
2858 if (pending_del_nr)
2859 goto del_pending;
2860 btrfs_release_path(root, path);
06d9a8d7
CM
2861 if (found_type == BTRFS_INODE_ITEM_KEY)
2862 break;
85e21bac
CM
2863 goto search_again;
2864 }
2865
2866 path->slots[0]--;
2867 if (pending_del_nr &&
2868 path->slots[0] + 1 != pending_del_slot) {
2869 struct btrfs_key debug;
2870del_pending:
2871 btrfs_item_key_to_cpu(path->nodes[0], &debug,
2872 pending_del_slot);
2873 ret = btrfs_del_items(trans, root, path,
2874 pending_del_slot,
2875 pending_del_nr);
2876 BUG_ON(ret);
2877 pending_del_nr = 0;
2878 btrfs_release_path(root, path);
06d9a8d7
CM
2879 if (found_type == BTRFS_INODE_ITEM_KEY)
2880 break;
85e21bac
CM
2881 goto search_again;
2882 }
39279cc3
CM
2883 }
2884 ret = 0;
2885error:
85e21bac
CM
2886 if (pending_del_nr) {
2887 ret = btrfs_del_items(trans, root, path, pending_del_slot,
2888 pending_del_nr);
2889 }
39279cc3 2890 btrfs_free_path(path);
39279cc3
CM
2891 return ret;
2892}
2893
2894/*
2895 * taken from block_truncate_page, but does cow as it zeros out
2896 * any bytes left in the last page in the file.
2897 */
2898static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
2899{
2900 struct inode *inode = mapping->host;
db94535d 2901 struct btrfs_root *root = BTRFS_I(inode)->root;
e6dcd2dc
CM
2902 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2903 struct btrfs_ordered_extent *ordered;
2904 char *kaddr;
db94535d 2905 u32 blocksize = root->sectorsize;
39279cc3
CM
2906 pgoff_t index = from >> PAGE_CACHE_SHIFT;
2907 unsigned offset = from & (PAGE_CACHE_SIZE-1);
2908 struct page *page;
39279cc3 2909 int ret = 0;
a52d9a80 2910 u64 page_start;
e6dcd2dc 2911 u64 page_end;
39279cc3
CM
2912
2913 if ((offset & (blocksize - 1)) == 0)
2914 goto out;
2915
2916 ret = -ENOMEM;
211c17f5 2917again:
39279cc3
CM
2918 page = grab_cache_page(mapping, index);
2919 if (!page)
2920 goto out;
e6dcd2dc
CM
2921
2922 page_start = page_offset(page);
2923 page_end = page_start + PAGE_CACHE_SIZE - 1;
2924
39279cc3 2925 if (!PageUptodate(page)) {
9ebefb18 2926 ret = btrfs_readpage(NULL, page);
39279cc3 2927 lock_page(page);
211c17f5
CM
2928 if (page->mapping != mapping) {
2929 unlock_page(page);
2930 page_cache_release(page);
2931 goto again;
2932 }
39279cc3
CM
2933 if (!PageUptodate(page)) {
2934 ret = -EIO;
89642229 2935 goto out_unlock;
39279cc3
CM
2936 }
2937 }
211c17f5 2938 wait_on_page_writeback(page);
e6dcd2dc
CM
2939
2940 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
2941 set_page_extent_mapped(page);
2942
2943 ordered = btrfs_lookup_ordered_extent(inode, page_start);
2944 if (ordered) {
2945 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2946 unlock_page(page);
2947 page_cache_release(page);
eb84ae03 2948 btrfs_start_ordered_extent(inode, ordered, 1);
e6dcd2dc
CM
2949 btrfs_put_ordered_extent(ordered);
2950 goto again;
2951 }
2952
ea8c2819 2953 btrfs_set_extent_delalloc(inode, page_start, page_end);
e6dcd2dc
CM
2954 ret = 0;
2955 if (offset != PAGE_CACHE_SIZE) {
2956 kaddr = kmap(page);
2957 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
2958 flush_dcache_page(page);
2959 kunmap(page);
2960 }
247e743c 2961 ClearPageChecked(page);
e6dcd2dc
CM
2962 set_page_dirty(page);
2963 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
39279cc3 2964
89642229 2965out_unlock:
39279cc3
CM
2966 unlock_page(page);
2967 page_cache_release(page);
2968out:
2969 return ret;
2970}
2971
9036c102 2972int btrfs_cont_expand(struct inode *inode, loff_t size)
39279cc3 2973{
9036c102
YZ
2974 struct btrfs_trans_handle *trans;
2975 struct btrfs_root *root = BTRFS_I(inode)->root;
2976 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2977 struct extent_map *em;
2978 u64 mask = root->sectorsize - 1;
2979 u64 hole_start = (inode->i_size + mask) & ~mask;
2980 u64 block_end = (size + mask) & ~mask;
2981 u64 last_byte;
2982 u64 cur_offset;
2983 u64 hole_size;
39279cc3
CM
2984 int err;
2985
9036c102
YZ
2986 if (size <= hole_start)
2987 return 0;
2988
6a63209f 2989 err = btrfs_check_metadata_free_space(root);
39279cc3
CM
2990 if (err)
2991 return err;
2992
9036c102 2993 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2bf5a725 2994
9036c102
YZ
2995 while (1) {
2996 struct btrfs_ordered_extent *ordered;
2997 btrfs_wait_ordered_range(inode, hole_start,
2998 block_end - hole_start);
2999 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
3000 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
3001 if (!ordered)
3002 break;
3003 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
3004 btrfs_put_ordered_extent(ordered);
3005 }
39279cc3 3006
9036c102
YZ
3007 trans = btrfs_start_transaction(root, 1);
3008 btrfs_set_trans_block_group(trans, inode);
39279cc3 3009
9036c102
YZ
3010 cur_offset = hole_start;
3011 while (1) {
3012 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3013 block_end - cur_offset, 0);
3014 BUG_ON(IS_ERR(em) || !em);
3015 last_byte = min(extent_map_end(em), block_end);
3016 last_byte = (last_byte + mask) & ~mask;
3017 if (test_bit(EXTENT_FLAG_VACANCY, &em->flags)) {
771ed689 3018 u64 hint_byte = 0;
9036c102 3019 hole_size = last_byte - cur_offset;
771ed689
CM
3020 err = btrfs_drop_extents(trans, root, inode,
3021 cur_offset,
3022 cur_offset + hole_size,
e980b50c 3023 block_end,
a1ed835e 3024 cur_offset, &hint_byte, 1);
771ed689
CM
3025 if (err)
3026 break;
9036c102
YZ
3027 err = btrfs_insert_file_extent(trans, root,
3028 inode->i_ino, cur_offset, 0,
3029 0, hole_size, 0, hole_size,
3030 0, 0, 0);
3031 btrfs_drop_extent_cache(inode, hole_start,
3032 last_byte - 1, 0);
3033 }
3034 free_extent_map(em);
3035 cur_offset = last_byte;
3036 if (err || cur_offset >= block_end)
3037 break;
3038 }
1832a6d5 3039
9036c102
YZ
3040 btrfs_end_transaction(trans, root);
3041 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
3042 return err;
3043}
39279cc3 3044
9036c102
YZ
3045static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3046{
3047 struct inode *inode = dentry->d_inode;
3048 int err;
39279cc3 3049
9036c102
YZ
3050 err = inode_change_ok(inode, attr);
3051 if (err)
3052 return err;
2bf5a725 3053
5a3f23d5
CM
3054 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
3055 if (attr->ia_size > inode->i_size) {
3056 err = btrfs_cont_expand(inode, attr->ia_size);
3057 if (err)
3058 return err;
3059 } else if (inode->i_size > 0 &&
3060 attr->ia_size == 0) {
3061
3062 /* we're truncating a file that used to have good
3063 * data down to zero. Make sure it gets into
3064 * the ordered flush list so that any new writes
3065 * get down to disk quickly.
3066 */
3067 BTRFS_I(inode)->ordered_data_close = 1;
3068 }
39279cc3 3069 }
9036c102 3070
39279cc3 3071 err = inode_setattr(inode, attr);
33268eaf
JB
3072
3073 if (!err && ((attr->ia_valid & ATTR_MODE)))
3074 err = btrfs_acl_chmod(inode);
39279cc3
CM
3075 return err;
3076}
61295eb8 3077
39279cc3
CM
3078void btrfs_delete_inode(struct inode *inode)
3079{
3080 struct btrfs_trans_handle *trans;
3081 struct btrfs_root *root = BTRFS_I(inode)->root;
d3c2fdcf 3082 unsigned long nr;
39279cc3
CM
3083 int ret;
3084
3085 truncate_inode_pages(&inode->i_data, 0);
3086 if (is_bad_inode(inode)) {
7b128766 3087 btrfs_orphan_del(NULL, inode);
39279cc3
CM
3088 goto no_delete;
3089 }
4a096752 3090 btrfs_wait_ordered_range(inode, 0, (u64)-1);
5f39d397 3091
76dda93c
YZ
3092 if (inode->i_nlink > 0) {
3093 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3094 goto no_delete;
3095 }
3096
dbe674a9 3097 btrfs_i_size_write(inode, 0);
180591bc 3098 trans = btrfs_join_transaction(root, 1);
5f39d397 3099
39279cc3 3100 btrfs_set_trans_block_group(trans, inode);
e02119d5 3101 ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size, 0);
7b128766
JB
3102 if (ret) {
3103 btrfs_orphan_del(NULL, inode);
54aa1f4d 3104 goto no_delete_lock;
7b128766
JB
3105 }
3106
3107 btrfs_orphan_del(trans, inode);
85e21bac 3108
d3c2fdcf 3109 nr = trans->blocks_used;
85e21bac 3110 clear_inode(inode);
5f39d397 3111
39279cc3 3112 btrfs_end_transaction(trans, root);
d3c2fdcf 3113 btrfs_btree_balance_dirty(root, nr);
39279cc3 3114 return;
54aa1f4d
CM
3115
3116no_delete_lock:
d3c2fdcf 3117 nr = trans->blocks_used;
54aa1f4d 3118 btrfs_end_transaction(trans, root);
d3c2fdcf 3119 btrfs_btree_balance_dirty(root, nr);
39279cc3
CM
3120no_delete:
3121 clear_inode(inode);
3122}
3123
3124/*
3125 * this returns the key found in the dir entry in the location pointer.
3126 * If no dir entries were found, location->objectid is 0.
3127 */
3128static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
3129 struct btrfs_key *location)
3130{
3131 const char *name = dentry->d_name.name;
3132 int namelen = dentry->d_name.len;
3133 struct btrfs_dir_item *di;
3134 struct btrfs_path *path;
3135 struct btrfs_root *root = BTRFS_I(dir)->root;
0d9f7f3e 3136 int ret = 0;
39279cc3
CM
3137
3138 path = btrfs_alloc_path();
3139 BUG_ON(!path);
3954401f 3140
39279cc3
CM
3141 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
3142 namelen, 0);
0d9f7f3e
Y
3143 if (IS_ERR(di))
3144 ret = PTR_ERR(di);
d397712b
CM
3145
3146 if (!di || IS_ERR(di))
3954401f 3147 goto out_err;
d397712b 3148
5f39d397 3149 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
39279cc3 3150out:
39279cc3
CM
3151 btrfs_free_path(path);
3152 return ret;
3954401f
CM
3153out_err:
3154 location->objectid = 0;
3155 goto out;
39279cc3
CM
3156}
3157
3158/*
3159 * when we hit a tree root in a directory, the btrfs part of the inode
3160 * needs to be changed to reflect the root directory of the tree root. This
3161 * is kind of like crossing a mount point.
3162 */
3163static int fixup_tree_root_location(struct btrfs_root *root,
4df27c4d
YZ
3164 struct inode *dir,
3165 struct dentry *dentry,
3166 struct btrfs_key *location,
3167 struct btrfs_root **sub_root)
39279cc3 3168{
4df27c4d
YZ
3169 struct btrfs_path *path;
3170 struct btrfs_root *new_root;
3171 struct btrfs_root_ref *ref;
3172 struct extent_buffer *leaf;
3173 int ret;
3174 int err = 0;
39279cc3 3175
4df27c4d
YZ
3176 path = btrfs_alloc_path();
3177 if (!path) {
3178 err = -ENOMEM;
3179 goto out;
3180 }
39279cc3 3181
4df27c4d
YZ
3182 err = -ENOENT;
3183 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
3184 BTRFS_I(dir)->root->root_key.objectid,
3185 location->objectid);
3186 if (ret) {
3187 if (ret < 0)
3188 err = ret;
3189 goto out;
3190 }
39279cc3 3191
4df27c4d
YZ
3192 leaf = path->nodes[0];
3193 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
3194 if (btrfs_root_ref_dirid(leaf, ref) != dir->i_ino ||
3195 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
3196 goto out;
39279cc3 3197
4df27c4d
YZ
3198 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
3199 (unsigned long)(ref + 1),
3200 dentry->d_name.len);
3201 if (ret)
3202 goto out;
3203
3204 btrfs_release_path(root->fs_info->tree_root, path);
3205
3206 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
3207 if (IS_ERR(new_root)) {
3208 err = PTR_ERR(new_root);
3209 goto out;
3210 }
3211
3212 if (btrfs_root_refs(&new_root->root_item) == 0) {
3213 err = -ENOENT;
3214 goto out;
3215 }
3216
3217 *sub_root = new_root;
3218 location->objectid = btrfs_root_dirid(&new_root->root_item);
3219 location->type = BTRFS_INODE_ITEM_KEY;
3220 location->offset = 0;
3221 err = 0;
3222out:
3223 btrfs_free_path(path);
3224 return err;
39279cc3
CM
3225}
3226
5d4f98a2
YZ
3227static void inode_tree_add(struct inode *inode)
3228{
3229 struct btrfs_root *root = BTRFS_I(inode)->root;
3230 struct btrfs_inode *entry;
03e860bd
FNP
3231 struct rb_node **p;
3232 struct rb_node *parent;
03e860bd
FNP
3233again:
3234 p = &root->inode_tree.rb_node;
3235 parent = NULL;
5d4f98a2 3236
76dda93c
YZ
3237 if (hlist_unhashed(&inode->i_hash))
3238 return;
3239
5d4f98a2
YZ
3240 spin_lock(&root->inode_lock);
3241 while (*p) {
3242 parent = *p;
3243 entry = rb_entry(parent, struct btrfs_inode, rb_node);
3244
3245 if (inode->i_ino < entry->vfs_inode.i_ino)
03e860bd 3246 p = &parent->rb_left;
5d4f98a2 3247 else if (inode->i_ino > entry->vfs_inode.i_ino)
03e860bd 3248 p = &parent->rb_right;
5d4f98a2
YZ
3249 else {
3250 WARN_ON(!(entry->vfs_inode.i_state &
3251 (I_WILL_FREE | I_FREEING | I_CLEAR)));
03e860bd
FNP
3252 rb_erase(parent, &root->inode_tree);
3253 RB_CLEAR_NODE(parent);
3254 spin_unlock(&root->inode_lock);
3255 goto again;
5d4f98a2
YZ
3256 }
3257 }
3258 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
3259 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
3260 spin_unlock(&root->inode_lock);
3261}
3262
3263static void inode_tree_del(struct inode *inode)
3264{
3265 struct btrfs_root *root = BTRFS_I(inode)->root;
76dda93c 3266 int empty = 0;
5d4f98a2 3267
03e860bd 3268 spin_lock(&root->inode_lock);
5d4f98a2 3269 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
5d4f98a2 3270 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
5d4f98a2 3271 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
76dda93c 3272 empty = RB_EMPTY_ROOT(&root->inode_tree);
5d4f98a2 3273 }
03e860bd 3274 spin_unlock(&root->inode_lock);
76dda93c
YZ
3275
3276 if (empty && btrfs_root_refs(&root->root_item) == 0) {
3277 synchronize_srcu(&root->fs_info->subvol_srcu);
3278 spin_lock(&root->inode_lock);
3279 empty = RB_EMPTY_ROOT(&root->inode_tree);
3280 spin_unlock(&root->inode_lock);
3281 if (empty)
3282 btrfs_add_dead_root(root);
3283 }
3284}
3285
3286int btrfs_invalidate_inodes(struct btrfs_root *root)
3287{
3288 struct rb_node *node;
3289 struct rb_node *prev;
3290 struct btrfs_inode *entry;
3291 struct inode *inode;
3292 u64 objectid = 0;
3293
3294 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
3295
3296 spin_lock(&root->inode_lock);
3297again:
3298 node = root->inode_tree.rb_node;
3299 prev = NULL;
3300 while (node) {
3301 prev = node;
3302 entry = rb_entry(node, struct btrfs_inode, rb_node);
3303
3304 if (objectid < entry->vfs_inode.i_ino)
3305 node = node->rb_left;
3306 else if (objectid > entry->vfs_inode.i_ino)
3307 node = node->rb_right;
3308 else
3309 break;
3310 }
3311 if (!node) {
3312 while (prev) {
3313 entry = rb_entry(prev, struct btrfs_inode, rb_node);
3314 if (objectid <= entry->vfs_inode.i_ino) {
3315 node = prev;
3316 break;
3317 }
3318 prev = rb_next(prev);
3319 }
3320 }
3321 while (node) {
3322 entry = rb_entry(node, struct btrfs_inode, rb_node);
3323 objectid = entry->vfs_inode.i_ino + 1;
3324 inode = igrab(&entry->vfs_inode);
3325 if (inode) {
3326 spin_unlock(&root->inode_lock);
3327 if (atomic_read(&inode->i_count) > 1)
3328 d_prune_aliases(inode);
3329 /*
3330 * btrfs_drop_inode will remove it from
3331 * the inode cache when its usage count
3332 * hits zero.
3333 */
3334 iput(inode);
3335 cond_resched();
3336 spin_lock(&root->inode_lock);
3337 goto again;
3338 }
3339
3340 if (cond_resched_lock(&root->inode_lock))
3341 goto again;
3342
3343 node = rb_next(node);
3344 }
3345 spin_unlock(&root->inode_lock);
3346 return 0;
5d4f98a2
YZ
3347}
3348
e02119d5 3349static noinline void init_btrfs_i(struct inode *inode)
39279cc3 3350{
e02119d5
CM
3351 struct btrfs_inode *bi = BTRFS_I(inode);
3352
e02119d5 3353 bi->generation = 0;
c3027eb5 3354 bi->sequence = 0;
e02119d5
CM
3355 bi->last_trans = 0;
3356 bi->logged_trans = 0;
3357 bi->delalloc_bytes = 0;
6a63209f 3358 bi->reserved_bytes = 0;
e02119d5
CM
3359 bi->disk_i_size = 0;
3360 bi->flags = 0;
3361 bi->index_cnt = (u64)-1;
12fcfd22 3362 bi->last_unlink_trans = 0;
2757495c 3363 bi->ordered_data_close = 0;
d1310b2e
CM
3364 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3365 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
b888db2b 3366 inode->i_mapping, GFP_NOFS);
7e38326f
CM
3367 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3368 inode->i_mapping, GFP_NOFS);
ea8c2819 3369 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
5a3f23d5 3370 INIT_LIST_HEAD(&BTRFS_I(inode)->ordered_operations);
5d4f98a2 3371 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
ba1da2f4 3372 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
ee6e6504 3373 mutex_init(&BTRFS_I(inode)->extent_mutex);
e02119d5
CM
3374 mutex_init(&BTRFS_I(inode)->log_mutex);
3375}
3376
3377static int btrfs_init_locked_inode(struct inode *inode, void *p)
3378{
3379 struct btrfs_iget_args *args = p;
3380 inode->i_ino = args->ino;
3381 init_btrfs_i(inode);
3382 BTRFS_I(inode)->root = args->root;
6a63209f 3383 btrfs_set_inode_space_info(args->root, inode);
39279cc3
CM
3384 return 0;
3385}
3386
3387static int btrfs_find_actor(struct inode *inode, void *opaque)
3388{
3389 struct btrfs_iget_args *args = opaque;
d397712b
CM
3390 return args->ino == inode->i_ino &&
3391 args->root == BTRFS_I(inode)->root;
39279cc3
CM
3392}
3393
5d4f98a2
YZ
3394static struct inode *btrfs_iget_locked(struct super_block *s,
3395 u64 objectid,
3396 struct btrfs_root *root)
39279cc3
CM
3397{
3398 struct inode *inode;
3399 struct btrfs_iget_args args;
3400 args.ino = objectid;
3401 args.root = root;
3402
3403 inode = iget5_locked(s, objectid, btrfs_find_actor,
3404 btrfs_init_locked_inode,
3405 (void *)&args);
3406 return inode;
3407}
3408
1a54ef8c
BR
3409/* Get an inode object given its location and corresponding root.
3410 * Returns in *is_new if the inode was read from disk
3411 */
3412struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
5d4f98a2 3413 struct btrfs_root *root)
1a54ef8c
BR
3414{
3415 struct inode *inode;
3416
3417 inode = btrfs_iget_locked(s, location->objectid, root);
3418 if (!inode)
5d4f98a2 3419 return ERR_PTR(-ENOMEM);
1a54ef8c
BR
3420
3421 if (inode->i_state & I_NEW) {
3422 BTRFS_I(inode)->root = root;
3423 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
3424 btrfs_read_locked_inode(inode);
5d4f98a2
YZ
3425
3426 inode_tree_add(inode);
1a54ef8c 3427 unlock_new_inode(inode);
1a54ef8c
BR
3428 }
3429
3430 return inode;
3431}
3432
4df27c4d
YZ
3433static struct inode *new_simple_dir(struct super_block *s,
3434 struct btrfs_key *key,
3435 struct btrfs_root *root)
3436{
3437 struct inode *inode = new_inode(s);
3438
3439 if (!inode)
3440 return ERR_PTR(-ENOMEM);
3441
3442 init_btrfs_i(inode);
3443
3444 BTRFS_I(inode)->root = root;
3445 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
3446 BTRFS_I(inode)->dummy_inode = 1;
3447
3448 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
3449 inode->i_op = &simple_dir_inode_operations;
3450 inode->i_fop = &simple_dir_operations;
3451 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
3452 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
3453
3454 return inode;
3455}
3456
3de4586c 3457struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
39279cc3 3458{
d397712b 3459 struct inode *inode;
4df27c4d 3460 struct btrfs_root *root = BTRFS_I(dir)->root;
39279cc3
CM
3461 struct btrfs_root *sub_root = root;
3462 struct btrfs_key location;
76dda93c 3463 int index;
5d4f98a2 3464 int ret;
39279cc3 3465
76dda93c
YZ
3466 dentry->d_op = &btrfs_dentry_operations;
3467
39279cc3
CM
3468 if (dentry->d_name.len > BTRFS_NAME_LEN)
3469 return ERR_PTR(-ENAMETOOLONG);
5f39d397 3470
39279cc3 3471 ret = btrfs_inode_by_name(dir, dentry, &location);
5f39d397 3472
39279cc3
CM
3473 if (ret < 0)
3474 return ERR_PTR(ret);
5f39d397 3475
4df27c4d
YZ
3476 if (location.objectid == 0)
3477 return NULL;
3478
3479 if (location.type == BTRFS_INODE_ITEM_KEY) {
3480 inode = btrfs_iget(dir->i_sb, &location, root);
3481 return inode;
3482 }
3483
3484 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
3485
76dda93c 3486 index = srcu_read_lock(&root->fs_info->subvol_srcu);
4df27c4d
YZ
3487 ret = fixup_tree_root_location(root, dir, dentry,
3488 &location, &sub_root);
3489 if (ret < 0) {
3490 if (ret != -ENOENT)
3491 inode = ERR_PTR(ret);
3492 else
3493 inode = new_simple_dir(dir->i_sb, &location, sub_root);
3494 } else {
5d4f98a2 3495 inode = btrfs_iget(dir->i_sb, &location, sub_root);
39279cc3 3496 }
76dda93c
YZ
3497 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
3498
3de4586c
CM
3499 return inode;
3500}
3501
76dda93c
YZ
3502static int btrfs_dentry_delete(struct dentry *dentry)
3503{
3504 struct btrfs_root *root;
3505
3506 if (!dentry->d_inode)
3507 return 0;
3508
3509 root = BTRFS_I(dentry->d_inode)->root;
3510 if (btrfs_root_refs(&root->root_item) == 0)
3511 return 1;
3512 return 0;
3513}
3514
3de4586c
CM
3515static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
3516 struct nameidata *nd)
3517{
3518 struct inode *inode;
3519
3de4586c
CM
3520 inode = btrfs_lookup_dentry(dir, dentry);
3521 if (IS_ERR(inode))
3522 return ERR_CAST(inode);
7b128766 3523
39279cc3
CM
3524 return d_splice_alias(inode, dentry);
3525}
3526
39279cc3
CM
3527static unsigned char btrfs_filetype_table[] = {
3528 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
3529};
3530
cbdf5a24
DW
3531static int btrfs_real_readdir(struct file *filp, void *dirent,
3532 filldir_t filldir)
39279cc3 3533{
6da6abae 3534 struct inode *inode = filp->f_dentry->d_inode;
39279cc3
CM
3535 struct btrfs_root *root = BTRFS_I(inode)->root;
3536 struct btrfs_item *item;
3537 struct btrfs_dir_item *di;
3538 struct btrfs_key key;
5f39d397 3539 struct btrfs_key found_key;
39279cc3
CM
3540 struct btrfs_path *path;
3541 int ret;
3542 u32 nritems;
5f39d397 3543 struct extent_buffer *leaf;
39279cc3
CM
3544 int slot;
3545 int advance;
3546 unsigned char d_type;
3547 int over = 0;
3548 u32 di_cur;
3549 u32 di_total;
3550 u32 di_len;
3551 int key_type = BTRFS_DIR_INDEX_KEY;
5f39d397
CM
3552 char tmp_name[32];
3553 char *name_ptr;
3554 int name_len;
39279cc3
CM
3555
3556 /* FIXME, use a real flag for deciding about the key type */
3557 if (root->fs_info->tree_root == root)
3558 key_type = BTRFS_DIR_ITEM_KEY;
5f39d397 3559
3954401f
CM
3560 /* special case for "." */
3561 if (filp->f_pos == 0) {
3562 over = filldir(dirent, ".", 1,
3563 1, inode->i_ino,
3564 DT_DIR);
3565 if (over)
3566 return 0;
3567 filp->f_pos = 1;
3568 }
3954401f
CM
3569 /* special case for .., just use the back ref */
3570 if (filp->f_pos == 1) {
5ecc7e5d 3571 u64 pino = parent_ino(filp->f_path.dentry);
3954401f 3572 over = filldir(dirent, "..", 2,
5ecc7e5d 3573 2, pino, DT_DIR);
3954401f 3574 if (over)
49593bfa 3575 return 0;
3954401f
CM
3576 filp->f_pos = 2;
3577 }
49593bfa
DW
3578 path = btrfs_alloc_path();
3579 path->reada = 2;
3580
39279cc3
CM
3581 btrfs_set_key_type(&key, key_type);
3582 key.offset = filp->f_pos;
49593bfa 3583 key.objectid = inode->i_ino;
5f39d397 3584
39279cc3
CM
3585 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3586 if (ret < 0)
3587 goto err;
3588 advance = 0;
49593bfa
DW
3589
3590 while (1) {
5f39d397
CM
3591 leaf = path->nodes[0];
3592 nritems = btrfs_header_nritems(leaf);
39279cc3
CM
3593 slot = path->slots[0];
3594 if (advance || slot >= nritems) {
49593bfa 3595 if (slot >= nritems - 1) {
39279cc3
CM
3596 ret = btrfs_next_leaf(root, path);
3597 if (ret)
3598 break;
5f39d397
CM
3599 leaf = path->nodes[0];
3600 nritems = btrfs_header_nritems(leaf);
39279cc3
CM
3601 slot = path->slots[0];
3602 } else {
3603 slot++;
3604 path->slots[0]++;
3605 }
3606 }
3de4586c 3607
39279cc3 3608 advance = 1;
5f39d397
CM
3609 item = btrfs_item_nr(leaf, slot);
3610 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3611
3612 if (found_key.objectid != key.objectid)
39279cc3 3613 break;
5f39d397 3614 if (btrfs_key_type(&found_key) != key_type)
39279cc3 3615 break;
5f39d397 3616 if (found_key.offset < filp->f_pos)
39279cc3 3617 continue;
5f39d397
CM
3618
3619 filp->f_pos = found_key.offset;
49593bfa 3620
39279cc3
CM
3621 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
3622 di_cur = 0;
5f39d397 3623 di_total = btrfs_item_size(leaf, item);
49593bfa
DW
3624
3625 while (di_cur < di_total) {
5f39d397
CM
3626 struct btrfs_key location;
3627
3628 name_len = btrfs_dir_name_len(leaf, di);
49593bfa 3629 if (name_len <= sizeof(tmp_name)) {
5f39d397
CM
3630 name_ptr = tmp_name;
3631 } else {
3632 name_ptr = kmalloc(name_len, GFP_NOFS);
49593bfa
DW
3633 if (!name_ptr) {
3634 ret = -ENOMEM;
3635 goto err;
3636 }
5f39d397
CM
3637 }
3638 read_extent_buffer(leaf, name_ptr,
3639 (unsigned long)(di + 1), name_len);
3640
3641 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
3642 btrfs_dir_item_key_to_cpu(leaf, di, &location);
3de4586c
CM
3643
3644 /* is this a reference to our own snapshot? If so
3645 * skip it
3646 */
3647 if (location.type == BTRFS_ROOT_ITEM_KEY &&
3648 location.objectid == root->root_key.objectid) {
3649 over = 0;
3650 goto skip;
3651 }
5f39d397 3652 over = filldir(dirent, name_ptr, name_len,
49593bfa 3653 found_key.offset, location.objectid,
39279cc3 3654 d_type);
5f39d397 3655
3de4586c 3656skip:
5f39d397
CM
3657 if (name_ptr != tmp_name)
3658 kfree(name_ptr);
3659
39279cc3
CM
3660 if (over)
3661 goto nopos;
5103e947 3662 di_len = btrfs_dir_name_len(leaf, di) +
49593bfa 3663 btrfs_dir_data_len(leaf, di) + sizeof(*di);
39279cc3
CM
3664 di_cur += di_len;
3665 di = (struct btrfs_dir_item *)((char *)di + di_len);
3666 }
3667 }
49593bfa
DW
3668
3669 /* Reached end of directory/root. Bump pos past the last item. */
5e591a07 3670 if (key_type == BTRFS_DIR_INDEX_KEY)
89f135d8 3671 filp->f_pos = INT_LIMIT(off_t);
5e591a07
YZ
3672 else
3673 filp->f_pos++;
39279cc3
CM
3674nopos:
3675 ret = 0;
3676err:
39279cc3 3677 btrfs_free_path(path);
39279cc3
CM
3678 return ret;
3679}
3680
3681int btrfs_write_inode(struct inode *inode, int wait)
3682{
3683 struct btrfs_root *root = BTRFS_I(inode)->root;
3684 struct btrfs_trans_handle *trans;
3685 int ret = 0;
3686
c146afad 3687 if (root->fs_info->btree_inode == inode)
4ca8b41e
CM
3688 return 0;
3689
39279cc3 3690 if (wait) {
f9295749 3691 trans = btrfs_join_transaction(root, 1);
39279cc3
CM
3692 btrfs_set_trans_block_group(trans, inode);
3693 ret = btrfs_commit_transaction(trans, root);
39279cc3
CM
3694 }
3695 return ret;
3696}
3697
3698/*
54aa1f4d 3699 * This is somewhat expensive, updating the tree every time the
39279cc3
CM
3700 * inode changes. But, it is most likely to find the inode in cache.
3701 * FIXME, needs more benchmarking...there are no reasons other than performance
3702 * to keep or drop this code.
3703 */
3704void btrfs_dirty_inode(struct inode *inode)
3705{
3706 struct btrfs_root *root = BTRFS_I(inode)->root;
3707 struct btrfs_trans_handle *trans;
3708
f9295749 3709 trans = btrfs_join_transaction(root, 1);
39279cc3
CM
3710 btrfs_set_trans_block_group(trans, inode);
3711 btrfs_update_inode(trans, root, inode);
3712 btrfs_end_transaction(trans, root);
39279cc3
CM
3713}
3714
d352ac68
CM
3715/*
3716 * find the highest existing sequence number in a directory
3717 * and then set the in-memory index_cnt variable to reflect
3718 * free sequence numbers
3719 */
aec7477b
JB
3720static int btrfs_set_inode_index_count(struct inode *inode)
3721{
3722 struct btrfs_root *root = BTRFS_I(inode)->root;
3723 struct btrfs_key key, found_key;
3724 struct btrfs_path *path;
3725 struct extent_buffer *leaf;
3726 int ret;
3727
3728 key.objectid = inode->i_ino;
3729 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
3730 key.offset = (u64)-1;
3731
3732 path = btrfs_alloc_path();
3733 if (!path)
3734 return -ENOMEM;
3735
3736 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3737 if (ret < 0)
3738 goto out;
3739 /* FIXME: we should be able to handle this */
3740 if (ret == 0)
3741 goto out;
3742 ret = 0;
3743
3744 /*
3745 * MAGIC NUMBER EXPLANATION:
3746 * since we search a directory based on f_pos we have to start at 2
3747 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
3748 * else has to start at 2
3749 */
3750 if (path->slots[0] == 0) {
3751 BTRFS_I(inode)->index_cnt = 2;
3752 goto out;
3753 }
3754
3755 path->slots[0]--;
3756
3757 leaf = path->nodes[0];
3758 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3759
3760 if (found_key.objectid != inode->i_ino ||
3761 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
3762 BTRFS_I(inode)->index_cnt = 2;
3763 goto out;
3764 }
3765
3766 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
3767out:
3768 btrfs_free_path(path);
3769 return ret;
3770}
3771
d352ac68
CM
3772/*
3773 * helper to find a free sequence number in a given directory. This current
3774 * code is very simple, later versions will do smarter things in the btree
3775 */
3de4586c 3776int btrfs_set_inode_index(struct inode *dir, u64 *index)
aec7477b
JB
3777{
3778 int ret = 0;
3779
3780 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
3781 ret = btrfs_set_inode_index_count(dir);
d397712b 3782 if (ret)
aec7477b
JB
3783 return ret;
3784 }
3785
00e4e6b3 3786 *index = BTRFS_I(dir)->index_cnt;
aec7477b
JB
3787 BTRFS_I(dir)->index_cnt++;
3788
3789 return ret;
3790}
3791
39279cc3
CM
3792static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
3793 struct btrfs_root *root,
aec7477b 3794 struct inode *dir,
9c58309d 3795 const char *name, int name_len,
d2fb3437
YZ
3796 u64 ref_objectid, u64 objectid,
3797 u64 alloc_hint, int mode, u64 *index)
39279cc3
CM
3798{
3799 struct inode *inode;
5f39d397 3800 struct btrfs_inode_item *inode_item;
39279cc3 3801 struct btrfs_key *location;
5f39d397 3802 struct btrfs_path *path;
9c58309d
CM
3803 struct btrfs_inode_ref *ref;
3804 struct btrfs_key key[2];
3805 u32 sizes[2];
3806 unsigned long ptr;
39279cc3
CM
3807 int ret;
3808 int owner;
3809
5f39d397
CM
3810 path = btrfs_alloc_path();
3811 BUG_ON(!path);
3812
39279cc3
CM
3813 inode = new_inode(root->fs_info->sb);
3814 if (!inode)
3815 return ERR_PTR(-ENOMEM);
3816
aec7477b 3817 if (dir) {
3de4586c 3818 ret = btrfs_set_inode_index(dir, index);
09771430
SF
3819 if (ret) {
3820 iput(inode);
aec7477b 3821 return ERR_PTR(ret);
09771430 3822 }
aec7477b
JB
3823 }
3824 /*
3825 * index_cnt is ignored for everything but a dir,
3826 * btrfs_get_inode_index_count has an explanation for the magic
3827 * number
3828 */
e02119d5 3829 init_btrfs_i(inode);
aec7477b 3830 BTRFS_I(inode)->index_cnt = 2;
39279cc3 3831 BTRFS_I(inode)->root = root;
e02119d5 3832 BTRFS_I(inode)->generation = trans->transid;
6a63209f 3833 btrfs_set_inode_space_info(root, inode);
b888db2b 3834
39279cc3
CM
3835 if (mode & S_IFDIR)
3836 owner = 0;
3837 else
3838 owner = 1;
d2fb3437
YZ
3839 BTRFS_I(inode)->block_group =
3840 btrfs_find_block_group(root, 0, alloc_hint, owner);
9c58309d
CM
3841
3842 key[0].objectid = objectid;
3843 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
3844 key[0].offset = 0;
3845
3846 key[1].objectid = objectid;
3847 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
3848 key[1].offset = ref_objectid;
3849
3850 sizes[0] = sizeof(struct btrfs_inode_item);
3851 sizes[1] = name_len + sizeof(*ref);
3852
b9473439 3853 path->leave_spinning = 1;
9c58309d
CM
3854 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
3855 if (ret != 0)
5f39d397
CM
3856 goto fail;
3857
79683f2d 3858 inode->i_uid = current_fsuid();
8c087b51 3859
42f15d77 3860 if (dir && (dir->i_mode & S_ISGID)) {
8c087b51
CB
3861 inode->i_gid = dir->i_gid;
3862 if (S_ISDIR(mode))
3863 mode |= S_ISGID;
3864 } else
3865 inode->i_gid = current_fsgid();
3866
39279cc3
CM
3867 inode->i_mode = mode;
3868 inode->i_ino = objectid;
a76a3cd4 3869 inode_set_bytes(inode, 0);
39279cc3 3870 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
5f39d397
CM
3871 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3872 struct btrfs_inode_item);
e02119d5 3873 fill_inode_item(trans, path->nodes[0], inode_item, inode);
9c58309d
CM
3874
3875 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
3876 struct btrfs_inode_ref);
3877 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
00e4e6b3 3878 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
9c58309d
CM
3879 ptr = (unsigned long)(ref + 1);
3880 write_extent_buffer(path->nodes[0], name, ptr, name_len);
3881
5f39d397
CM
3882 btrfs_mark_buffer_dirty(path->nodes[0]);
3883 btrfs_free_path(path);
3884
39279cc3
CM
3885 location = &BTRFS_I(inode)->location;
3886 location->objectid = objectid;
39279cc3
CM
3887 location->offset = 0;
3888 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
3889
6cbff00f
CH
3890 btrfs_inherit_iflags(inode, dir);
3891
94272164
CM
3892 if ((mode & S_IFREG)) {
3893 if (btrfs_test_opt(root, NODATASUM))
3894 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
3895 if (btrfs_test_opt(root, NODATACOW))
3896 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
3897 }
3898
39279cc3 3899 insert_inode_hash(inode);
5d4f98a2 3900 inode_tree_add(inode);
39279cc3 3901 return inode;
5f39d397 3902fail:
aec7477b
JB
3903 if (dir)
3904 BTRFS_I(dir)->index_cnt--;
5f39d397 3905 btrfs_free_path(path);
09771430 3906 iput(inode);
5f39d397 3907 return ERR_PTR(ret);
39279cc3
CM
3908}
3909
3910static inline u8 btrfs_inode_type(struct inode *inode)
3911{
3912 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
3913}
3914
d352ac68
CM
3915/*
3916 * utility function to add 'inode' into 'parent_inode' with
3917 * a give name and a given sequence number.
3918 * if 'add_backref' is true, also insert a backref from the
3919 * inode to the parent directory.
3920 */
e02119d5
CM
3921int btrfs_add_link(struct btrfs_trans_handle *trans,
3922 struct inode *parent_inode, struct inode *inode,
3923 const char *name, int name_len, int add_backref, u64 index)
39279cc3 3924{
4df27c4d 3925 int ret = 0;
39279cc3 3926 struct btrfs_key key;
e02119d5 3927 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
5f39d397 3928
4df27c4d
YZ
3929 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
3930 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
3931 } else {
3932 key.objectid = inode->i_ino;
3933 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
3934 key.offset = 0;
3935 }
3936
3937 if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
3938 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
3939 key.objectid, root->root_key.objectid,
3940 parent_inode->i_ino,
3941 index, name, name_len);
3942 } else if (add_backref) {
3943 ret = btrfs_insert_inode_ref(trans, root,
3944 name, name_len, inode->i_ino,
3945 parent_inode->i_ino, index);
3946 }
39279cc3 3947
39279cc3 3948 if (ret == 0) {
4df27c4d
YZ
3949 ret = btrfs_insert_dir_item(trans, root, name, name_len,
3950 parent_inode->i_ino, &key,
3951 btrfs_inode_type(inode), index);
3952 BUG_ON(ret);
3953
dbe674a9 3954 btrfs_i_size_write(parent_inode, parent_inode->i_size +
e02119d5 3955 name_len * 2);
79c44584 3956 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
e02119d5 3957 ret = btrfs_update_inode(trans, root, parent_inode);
39279cc3
CM
3958 }
3959 return ret;
3960}
3961
3962static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
9c58309d 3963 struct dentry *dentry, struct inode *inode,
00e4e6b3 3964 int backref, u64 index)
39279cc3 3965{
e02119d5
CM
3966 int err = btrfs_add_link(trans, dentry->d_parent->d_inode,
3967 inode, dentry->d_name.name,
3968 dentry->d_name.len, backref, index);
39279cc3
CM
3969 if (!err) {
3970 d_instantiate(dentry, inode);
3971 return 0;
3972 }
3973 if (err > 0)
3974 err = -EEXIST;
3975 return err;
3976}
3977
618e21d5
JB
3978static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
3979 int mode, dev_t rdev)
3980{
3981 struct btrfs_trans_handle *trans;
3982 struct btrfs_root *root = BTRFS_I(dir)->root;
1832a6d5 3983 struct inode *inode = NULL;
618e21d5
JB
3984 int err;
3985 int drop_inode = 0;
3986 u64 objectid;
1832a6d5 3987 unsigned long nr = 0;
00e4e6b3 3988 u64 index = 0;
618e21d5
JB
3989
3990 if (!new_valid_dev(rdev))
3991 return -EINVAL;
3992
6a63209f 3993 err = btrfs_check_metadata_free_space(root);
1832a6d5
CM
3994 if (err)
3995 goto fail;
3996
618e21d5
JB
3997 trans = btrfs_start_transaction(root, 1);
3998 btrfs_set_trans_block_group(trans, dir);
3999
4000 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4001 if (err) {
4002 err = -ENOSPC;
4003 goto out_unlock;
4004 }
4005
aec7477b 4006 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
9c58309d
CM
4007 dentry->d_name.len,
4008 dentry->d_parent->d_inode->i_ino, objectid,
00e4e6b3 4009 BTRFS_I(dir)->block_group, mode, &index);
618e21d5
JB
4010 err = PTR_ERR(inode);
4011 if (IS_ERR(inode))
4012 goto out_unlock;
4013
0279b4cd 4014 err = btrfs_init_inode_security(inode, dir);
33268eaf
JB
4015 if (err) {
4016 drop_inode = 1;
4017 goto out_unlock;
4018 }
4019
618e21d5 4020 btrfs_set_trans_block_group(trans, inode);
00e4e6b3 4021 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
618e21d5
JB
4022 if (err)
4023 drop_inode = 1;
4024 else {
4025 inode->i_op = &btrfs_special_inode_operations;
4026 init_special_inode(inode, inode->i_mode, rdev);
1b4ab1bb 4027 btrfs_update_inode(trans, root, inode);
618e21d5 4028 }
618e21d5
JB
4029 btrfs_update_inode_block_group(trans, inode);
4030 btrfs_update_inode_block_group(trans, dir);
4031out_unlock:
d3c2fdcf 4032 nr = trans->blocks_used;
89ce8a63 4033 btrfs_end_transaction_throttle(trans, root);
1832a6d5 4034fail:
618e21d5
JB
4035 if (drop_inode) {
4036 inode_dec_link_count(inode);
4037 iput(inode);
4038 }
d3c2fdcf 4039 btrfs_btree_balance_dirty(root, nr);
618e21d5
JB
4040 return err;
4041}
4042
39279cc3
CM
4043static int btrfs_create(struct inode *dir, struct dentry *dentry,
4044 int mode, struct nameidata *nd)
4045{
4046 struct btrfs_trans_handle *trans;
4047 struct btrfs_root *root = BTRFS_I(dir)->root;
1832a6d5 4048 struct inode *inode = NULL;
39279cc3
CM
4049 int err;
4050 int drop_inode = 0;
1832a6d5 4051 unsigned long nr = 0;
39279cc3 4052 u64 objectid;
00e4e6b3 4053 u64 index = 0;
39279cc3 4054
6a63209f 4055 err = btrfs_check_metadata_free_space(root);
1832a6d5
CM
4056 if (err)
4057 goto fail;
39279cc3
CM
4058 trans = btrfs_start_transaction(root, 1);
4059 btrfs_set_trans_block_group(trans, dir);
4060
4061 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4062 if (err) {
4063 err = -ENOSPC;
4064 goto out_unlock;
4065 }
4066
aec7477b 4067 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
9c58309d
CM
4068 dentry->d_name.len,
4069 dentry->d_parent->d_inode->i_ino,
00e4e6b3
CM
4070 objectid, BTRFS_I(dir)->block_group, mode,
4071 &index);
39279cc3
CM
4072 err = PTR_ERR(inode);
4073 if (IS_ERR(inode))
4074 goto out_unlock;
4075
0279b4cd 4076 err = btrfs_init_inode_security(inode, dir);
33268eaf
JB
4077 if (err) {
4078 drop_inode = 1;
4079 goto out_unlock;
4080 }
4081
39279cc3 4082 btrfs_set_trans_block_group(trans, inode);
00e4e6b3 4083 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
39279cc3
CM
4084 if (err)
4085 drop_inode = 1;
4086 else {
4087 inode->i_mapping->a_ops = &btrfs_aops;
04160088 4088 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
39279cc3
CM
4089 inode->i_fop = &btrfs_file_operations;
4090 inode->i_op = &btrfs_file_inode_operations;
d1310b2e 4091 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
39279cc3 4092 }
39279cc3
CM
4093 btrfs_update_inode_block_group(trans, inode);
4094 btrfs_update_inode_block_group(trans, dir);
4095out_unlock:
d3c2fdcf 4096 nr = trans->blocks_used;
ab78c84d 4097 btrfs_end_transaction_throttle(trans, root);
1832a6d5 4098fail:
39279cc3
CM
4099 if (drop_inode) {
4100 inode_dec_link_count(inode);
4101 iput(inode);
4102 }
d3c2fdcf 4103 btrfs_btree_balance_dirty(root, nr);
39279cc3
CM
4104 return err;
4105}
4106
4107static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
4108 struct dentry *dentry)
4109{
4110 struct btrfs_trans_handle *trans;
4111 struct btrfs_root *root = BTRFS_I(dir)->root;
4112 struct inode *inode = old_dentry->d_inode;
00e4e6b3 4113 u64 index;
1832a6d5 4114 unsigned long nr = 0;
39279cc3
CM
4115 int err;
4116 int drop_inode = 0;
4117
4118 if (inode->i_nlink == 0)
4119 return -ENOENT;
4120
e02119d5 4121 btrfs_inc_nlink(inode);
6a63209f 4122 err = btrfs_check_metadata_free_space(root);
1832a6d5
CM
4123 if (err)
4124 goto fail;
3de4586c 4125 err = btrfs_set_inode_index(dir, &index);
aec7477b
JB
4126 if (err)
4127 goto fail;
4128
39279cc3 4129 trans = btrfs_start_transaction(root, 1);
5f39d397 4130
39279cc3
CM
4131 btrfs_set_trans_block_group(trans, dir);
4132 atomic_inc(&inode->i_count);
aec7477b 4133
00e4e6b3 4134 err = btrfs_add_nondir(trans, dentry, inode, 1, index);
5f39d397 4135
a5719521 4136 if (err) {
54aa1f4d 4137 drop_inode = 1;
a5719521
YZ
4138 } else {
4139 btrfs_update_inode_block_group(trans, dir);
4140 err = btrfs_update_inode(trans, root, inode);
4141 BUG_ON(err);
4142 btrfs_log_new_name(trans, inode, NULL, dentry->d_parent);
4143 }
39279cc3 4144
d3c2fdcf 4145 nr = trans->blocks_used;
ab78c84d 4146 btrfs_end_transaction_throttle(trans, root);
1832a6d5 4147fail:
39279cc3
CM
4148 if (drop_inode) {
4149 inode_dec_link_count(inode);
4150 iput(inode);
4151 }
d3c2fdcf 4152 btrfs_btree_balance_dirty(root, nr);
39279cc3
CM
4153 return err;
4154}
4155
39279cc3
CM
4156static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
4157{
b9d86667 4158 struct inode *inode = NULL;
39279cc3
CM
4159 struct btrfs_trans_handle *trans;
4160 struct btrfs_root *root = BTRFS_I(dir)->root;
4161 int err = 0;
4162 int drop_on_err = 0;
b9d86667 4163 u64 objectid = 0;
00e4e6b3 4164 u64 index = 0;
d3c2fdcf 4165 unsigned long nr = 1;
39279cc3 4166
6a63209f 4167 err = btrfs_check_metadata_free_space(root);
1832a6d5
CM
4168 if (err)
4169 goto out_unlock;
4170
39279cc3
CM
4171 trans = btrfs_start_transaction(root, 1);
4172 btrfs_set_trans_block_group(trans, dir);
5f39d397 4173
39279cc3
CM
4174 if (IS_ERR(trans)) {
4175 err = PTR_ERR(trans);
4176 goto out_unlock;
4177 }
4178
4179 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4180 if (err) {
4181 err = -ENOSPC;
4182 goto out_unlock;
4183 }
4184
aec7477b 4185 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
9c58309d
CM
4186 dentry->d_name.len,
4187 dentry->d_parent->d_inode->i_ino, objectid,
00e4e6b3
CM
4188 BTRFS_I(dir)->block_group, S_IFDIR | mode,
4189 &index);
39279cc3
CM
4190 if (IS_ERR(inode)) {
4191 err = PTR_ERR(inode);
4192 goto out_fail;
4193 }
5f39d397 4194
39279cc3 4195 drop_on_err = 1;
33268eaf 4196
0279b4cd 4197 err = btrfs_init_inode_security(inode, dir);
33268eaf
JB
4198 if (err)
4199 goto out_fail;
4200
39279cc3
CM
4201 inode->i_op = &btrfs_dir_inode_operations;
4202 inode->i_fop = &btrfs_dir_file_operations;
4203 btrfs_set_trans_block_group(trans, inode);
4204
dbe674a9 4205 btrfs_i_size_write(inode, 0);
39279cc3
CM
4206 err = btrfs_update_inode(trans, root, inode);
4207 if (err)
4208 goto out_fail;
5f39d397 4209
e02119d5
CM
4210 err = btrfs_add_link(trans, dentry->d_parent->d_inode,
4211 inode, dentry->d_name.name,
4212 dentry->d_name.len, 0, index);
39279cc3
CM
4213 if (err)
4214 goto out_fail;
5f39d397 4215
39279cc3
CM
4216 d_instantiate(dentry, inode);
4217 drop_on_err = 0;
39279cc3
CM
4218 btrfs_update_inode_block_group(trans, inode);
4219 btrfs_update_inode_block_group(trans, dir);
4220
4221out_fail:
d3c2fdcf 4222 nr = trans->blocks_used;
ab78c84d 4223 btrfs_end_transaction_throttle(trans, root);
5f39d397 4224
39279cc3 4225out_unlock:
39279cc3
CM
4226 if (drop_on_err)
4227 iput(inode);
d3c2fdcf 4228 btrfs_btree_balance_dirty(root, nr);
39279cc3
CM
4229 return err;
4230}
4231
d352ac68
CM
4232/* helper for btfs_get_extent. Given an existing extent in the tree,
4233 * and an extent that you want to insert, deal with overlap and insert
4234 * the new extent into the tree.
4235 */
3b951516
CM
4236static int merge_extent_mapping(struct extent_map_tree *em_tree,
4237 struct extent_map *existing,
e6dcd2dc
CM
4238 struct extent_map *em,
4239 u64 map_start, u64 map_len)
3b951516
CM
4240{
4241 u64 start_diff;
3b951516 4242
e6dcd2dc
CM
4243 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
4244 start_diff = map_start - em->start;
4245 em->start = map_start;
4246 em->len = map_len;
c8b97818
CM
4247 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
4248 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
e6dcd2dc 4249 em->block_start += start_diff;
c8b97818
CM
4250 em->block_len -= start_diff;
4251 }
e6dcd2dc 4252 return add_extent_mapping(em_tree, em);
3b951516
CM
4253}
4254
c8b97818
CM
4255static noinline int uncompress_inline(struct btrfs_path *path,
4256 struct inode *inode, struct page *page,
4257 size_t pg_offset, u64 extent_offset,
4258 struct btrfs_file_extent_item *item)
4259{
4260 int ret;
4261 struct extent_buffer *leaf = path->nodes[0];
4262 char *tmp;
4263 size_t max_size;
4264 unsigned long inline_size;
4265 unsigned long ptr;
4266
4267 WARN_ON(pg_offset != 0);
4268 max_size = btrfs_file_extent_ram_bytes(leaf, item);
4269 inline_size = btrfs_file_extent_inline_item_len(leaf,
4270 btrfs_item_nr(leaf, path->slots[0]));
4271 tmp = kmalloc(inline_size, GFP_NOFS);
4272 ptr = btrfs_file_extent_inline_start(item);
4273
4274 read_extent_buffer(leaf, tmp, ptr, inline_size);
4275
5b050f04 4276 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
c8b97818
CM
4277 ret = btrfs_zlib_decompress(tmp, page, extent_offset,
4278 inline_size, max_size);
4279 if (ret) {
4280 char *kaddr = kmap_atomic(page, KM_USER0);
4281 unsigned long copy_size = min_t(u64,
4282 PAGE_CACHE_SIZE - pg_offset,
4283 max_size - extent_offset);
4284 memset(kaddr + pg_offset, 0, copy_size);
4285 kunmap_atomic(kaddr, KM_USER0);
4286 }
4287 kfree(tmp);
4288 return 0;
4289}
4290
d352ac68
CM
4291/*
4292 * a bit scary, this does extent mapping from logical file offset to the disk.
d397712b
CM
4293 * the ugly parts come from merging extents from the disk with the in-ram
4294 * representation. This gets more complex because of the data=ordered code,
d352ac68
CM
4295 * where the in-ram extents might be locked pending data=ordered completion.
4296 *
4297 * This also copies inline extents directly into the page.
4298 */
d397712b 4299
a52d9a80 4300struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
70dec807 4301 size_t pg_offset, u64 start, u64 len,
a52d9a80
CM
4302 int create)
4303{
4304 int ret;
4305 int err = 0;
db94535d 4306 u64 bytenr;
a52d9a80
CM
4307 u64 extent_start = 0;
4308 u64 extent_end = 0;
4309 u64 objectid = inode->i_ino;
4310 u32 found_type;
f421950f 4311 struct btrfs_path *path = NULL;
a52d9a80
CM
4312 struct btrfs_root *root = BTRFS_I(inode)->root;
4313 struct btrfs_file_extent_item *item;
5f39d397
CM
4314 struct extent_buffer *leaf;
4315 struct btrfs_key found_key;
a52d9a80
CM
4316 struct extent_map *em = NULL;
4317 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
d1310b2e 4318 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
a52d9a80 4319 struct btrfs_trans_handle *trans = NULL;
c8b97818 4320 int compressed;
a52d9a80 4321
a52d9a80 4322again:
890871be 4323 read_lock(&em_tree->lock);
d1310b2e 4324 em = lookup_extent_mapping(em_tree, start, len);
a061fc8d
CM
4325 if (em)
4326 em->bdev = root->fs_info->fs_devices->latest_bdev;
890871be 4327 read_unlock(&em_tree->lock);
d1310b2e 4328
a52d9a80 4329 if (em) {
e1c4b745
CM
4330 if (em->start > start || em->start + em->len <= start)
4331 free_extent_map(em);
4332 else if (em->block_start == EXTENT_MAP_INLINE && page)
70dec807
CM
4333 free_extent_map(em);
4334 else
4335 goto out;
a52d9a80 4336 }
d1310b2e 4337 em = alloc_extent_map(GFP_NOFS);
a52d9a80 4338 if (!em) {
d1310b2e
CM
4339 err = -ENOMEM;
4340 goto out;
a52d9a80 4341 }
e6dcd2dc 4342 em->bdev = root->fs_info->fs_devices->latest_bdev;
d1310b2e 4343 em->start = EXTENT_MAP_HOLE;
445a6944 4344 em->orig_start = EXTENT_MAP_HOLE;
d1310b2e 4345 em->len = (u64)-1;
c8b97818 4346 em->block_len = (u64)-1;
f421950f
CM
4347
4348 if (!path) {
4349 path = btrfs_alloc_path();
4350 BUG_ON(!path);
4351 }
4352
179e29e4
CM
4353 ret = btrfs_lookup_file_extent(trans, root, path,
4354 objectid, start, trans != NULL);
a52d9a80
CM
4355 if (ret < 0) {
4356 err = ret;
4357 goto out;
4358 }
4359
4360 if (ret != 0) {
4361 if (path->slots[0] == 0)
4362 goto not_found;
4363 path->slots[0]--;
4364 }
4365
5f39d397
CM
4366 leaf = path->nodes[0];
4367 item = btrfs_item_ptr(leaf, path->slots[0],
a52d9a80 4368 struct btrfs_file_extent_item);
a52d9a80 4369 /* are we inside the extent that was found? */
5f39d397
CM
4370 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4371 found_type = btrfs_key_type(&found_key);
4372 if (found_key.objectid != objectid ||
a52d9a80
CM
4373 found_type != BTRFS_EXTENT_DATA_KEY) {
4374 goto not_found;
4375 }
4376
5f39d397
CM
4377 found_type = btrfs_file_extent_type(leaf, item);
4378 extent_start = found_key.offset;
c8b97818 4379 compressed = btrfs_file_extent_compression(leaf, item);
d899e052
YZ
4380 if (found_type == BTRFS_FILE_EXTENT_REG ||
4381 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
a52d9a80 4382 extent_end = extent_start +
db94535d 4383 btrfs_file_extent_num_bytes(leaf, item);
9036c102
YZ
4384 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
4385 size_t size;
4386 size = btrfs_file_extent_inline_len(leaf, item);
4387 extent_end = (extent_start + size + root->sectorsize - 1) &
4388 ~((u64)root->sectorsize - 1);
4389 }
4390
4391 if (start >= extent_end) {
4392 path->slots[0]++;
4393 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
4394 ret = btrfs_next_leaf(root, path);
4395 if (ret < 0) {
4396 err = ret;
4397 goto out;
a52d9a80 4398 }
9036c102
YZ
4399 if (ret > 0)
4400 goto not_found;
4401 leaf = path->nodes[0];
a52d9a80 4402 }
9036c102
YZ
4403 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4404 if (found_key.objectid != objectid ||
4405 found_key.type != BTRFS_EXTENT_DATA_KEY)
4406 goto not_found;
4407 if (start + len <= found_key.offset)
4408 goto not_found;
4409 em->start = start;
4410 em->len = found_key.offset - start;
4411 goto not_found_em;
4412 }
4413
d899e052
YZ
4414 if (found_type == BTRFS_FILE_EXTENT_REG ||
4415 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
9036c102
YZ
4416 em->start = extent_start;
4417 em->len = extent_end - extent_start;
ff5b7ee3
YZ
4418 em->orig_start = extent_start -
4419 btrfs_file_extent_offset(leaf, item);
db94535d
CM
4420 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
4421 if (bytenr == 0) {
5f39d397 4422 em->block_start = EXTENT_MAP_HOLE;
a52d9a80
CM
4423 goto insert;
4424 }
c8b97818
CM
4425 if (compressed) {
4426 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
4427 em->block_start = bytenr;
4428 em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
4429 item);
4430 } else {
4431 bytenr += btrfs_file_extent_offset(leaf, item);
4432 em->block_start = bytenr;
4433 em->block_len = em->len;
d899e052
YZ
4434 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
4435 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
c8b97818 4436 }
a52d9a80
CM
4437 goto insert;
4438 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
5f39d397 4439 unsigned long ptr;
a52d9a80 4440 char *map;
3326d1b0
CM
4441 size_t size;
4442 size_t extent_offset;
4443 size_t copy_size;
a52d9a80 4444
689f9346 4445 em->block_start = EXTENT_MAP_INLINE;
c8b97818 4446 if (!page || create) {
689f9346 4447 em->start = extent_start;
9036c102 4448 em->len = extent_end - extent_start;
689f9346
Y
4449 goto out;
4450 }
5f39d397 4451
9036c102
YZ
4452 size = btrfs_file_extent_inline_len(leaf, item);
4453 extent_offset = page_offset(page) + pg_offset - extent_start;
70dec807 4454 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
3326d1b0 4455 size - extent_offset);
3326d1b0 4456 em->start = extent_start + extent_offset;
70dec807
CM
4457 em->len = (copy_size + root->sectorsize - 1) &
4458 ~((u64)root->sectorsize - 1);
ff5b7ee3 4459 em->orig_start = EXTENT_MAP_INLINE;
c8b97818
CM
4460 if (compressed)
4461 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
689f9346 4462 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
179e29e4 4463 if (create == 0 && !PageUptodate(page)) {
c8b97818
CM
4464 if (btrfs_file_extent_compression(leaf, item) ==
4465 BTRFS_COMPRESS_ZLIB) {
4466 ret = uncompress_inline(path, inode, page,
4467 pg_offset,
4468 extent_offset, item);
4469 BUG_ON(ret);
4470 } else {
4471 map = kmap(page);
4472 read_extent_buffer(leaf, map + pg_offset, ptr,
4473 copy_size);
93c82d57
CM
4474 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
4475 memset(map + pg_offset + copy_size, 0,
4476 PAGE_CACHE_SIZE - pg_offset -
4477 copy_size);
4478 }
c8b97818
CM
4479 kunmap(page);
4480 }
179e29e4
CM
4481 flush_dcache_page(page);
4482 } else if (create && PageUptodate(page)) {
4483 if (!trans) {
4484 kunmap(page);
4485 free_extent_map(em);
4486 em = NULL;
4487 btrfs_release_path(root, path);
f9295749 4488 trans = btrfs_join_transaction(root, 1);
179e29e4
CM
4489 goto again;
4490 }
c8b97818 4491 map = kmap(page);
70dec807 4492 write_extent_buffer(leaf, map + pg_offset, ptr,
179e29e4 4493 copy_size);
c8b97818 4494 kunmap(page);
179e29e4 4495 btrfs_mark_buffer_dirty(leaf);
a52d9a80 4496 }
d1310b2e
CM
4497 set_extent_uptodate(io_tree, em->start,
4498 extent_map_end(em) - 1, GFP_NOFS);
a52d9a80
CM
4499 goto insert;
4500 } else {
d397712b 4501 printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
a52d9a80
CM
4502 WARN_ON(1);
4503 }
4504not_found:
4505 em->start = start;
d1310b2e 4506 em->len = len;
a52d9a80 4507not_found_em:
5f39d397 4508 em->block_start = EXTENT_MAP_HOLE;
9036c102 4509 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
a52d9a80
CM
4510insert:
4511 btrfs_release_path(root, path);
d1310b2e 4512 if (em->start > start || extent_map_end(em) <= start) {
d397712b
CM
4513 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
4514 "[%llu %llu]\n", (unsigned long long)em->start,
4515 (unsigned long long)em->len,
4516 (unsigned long long)start,
4517 (unsigned long long)len);
a52d9a80
CM
4518 err = -EIO;
4519 goto out;
4520 }
d1310b2e
CM
4521
4522 err = 0;
890871be 4523 write_lock(&em_tree->lock);
a52d9a80 4524 ret = add_extent_mapping(em_tree, em);
3b951516
CM
4525 /* it is possible that someone inserted the extent into the tree
4526 * while we had the lock dropped. It is also possible that
4527 * an overlapping map exists in the tree
4528 */
a52d9a80 4529 if (ret == -EEXIST) {
3b951516 4530 struct extent_map *existing;
e6dcd2dc
CM
4531
4532 ret = 0;
4533
3b951516 4534 existing = lookup_extent_mapping(em_tree, start, len);
e1c4b745
CM
4535 if (existing && (existing->start > start ||
4536 existing->start + existing->len <= start)) {
4537 free_extent_map(existing);
4538 existing = NULL;
4539 }
3b951516
CM
4540 if (!existing) {
4541 existing = lookup_extent_mapping(em_tree, em->start,
4542 em->len);
4543 if (existing) {
4544 err = merge_extent_mapping(em_tree, existing,
e6dcd2dc
CM
4545 em, start,
4546 root->sectorsize);
3b951516
CM
4547 free_extent_map(existing);
4548 if (err) {
4549 free_extent_map(em);
4550 em = NULL;
4551 }
4552 } else {
4553 err = -EIO;
3b951516
CM
4554 free_extent_map(em);
4555 em = NULL;
4556 }
4557 } else {
4558 free_extent_map(em);
4559 em = existing;
e6dcd2dc 4560 err = 0;
a52d9a80 4561 }
a52d9a80 4562 }
890871be 4563 write_unlock(&em_tree->lock);
a52d9a80 4564out:
f421950f
CM
4565 if (path)
4566 btrfs_free_path(path);
a52d9a80
CM
4567 if (trans) {
4568 ret = btrfs_end_transaction(trans, root);
d397712b 4569 if (!err)
a52d9a80
CM
4570 err = ret;
4571 }
a52d9a80
CM
4572 if (err) {
4573 free_extent_map(em);
a52d9a80
CM
4574 return ERR_PTR(err);
4575 }
4576 return em;
4577}
4578
16432985
CM
4579static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
4580 const struct iovec *iov, loff_t offset,
4581 unsigned long nr_segs)
4582{
e1c4b745 4583 return -EINVAL;
16432985
CM
4584}
4585
1506fcc8
YS
4586static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4587 __u64 start, __u64 len)
4588{
4589 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent);
4590}
4591
a52d9a80 4592int btrfs_readpage(struct file *file, struct page *page)
9ebefb18 4593{
d1310b2e
CM
4594 struct extent_io_tree *tree;
4595 tree = &BTRFS_I(page->mapping->host)->io_tree;
a52d9a80 4596 return extent_read_full_page(tree, page, btrfs_get_extent);
9ebefb18 4597}
1832a6d5 4598
a52d9a80 4599static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
39279cc3 4600{
d1310b2e 4601 struct extent_io_tree *tree;
b888db2b
CM
4602
4603
4604 if (current->flags & PF_MEMALLOC) {
4605 redirty_page_for_writepage(wbc, page);
4606 unlock_page(page);
4607 return 0;
4608 }
d1310b2e 4609 tree = &BTRFS_I(page->mapping->host)->io_tree;
a52d9a80 4610 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
9ebefb18
CM
4611}
4612
f421950f
CM
4613int btrfs_writepages(struct address_space *mapping,
4614 struct writeback_control *wbc)
b293f02e 4615{
d1310b2e 4616 struct extent_io_tree *tree;
771ed689 4617
d1310b2e 4618 tree = &BTRFS_I(mapping->host)->io_tree;
b293f02e
CM
4619 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
4620}
4621
3ab2fb5a
CM
4622static int
4623btrfs_readpages(struct file *file, struct address_space *mapping,
4624 struct list_head *pages, unsigned nr_pages)
4625{
d1310b2e
CM
4626 struct extent_io_tree *tree;
4627 tree = &BTRFS_I(mapping->host)->io_tree;
3ab2fb5a
CM
4628 return extent_readpages(tree, mapping, pages, nr_pages,
4629 btrfs_get_extent);
4630}
e6dcd2dc 4631static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
9ebefb18 4632{
d1310b2e
CM
4633 struct extent_io_tree *tree;
4634 struct extent_map_tree *map;
a52d9a80 4635 int ret;
8c2383c3 4636
d1310b2e
CM
4637 tree = &BTRFS_I(page->mapping->host)->io_tree;
4638 map = &BTRFS_I(page->mapping->host)->extent_tree;
70dec807 4639 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
a52d9a80
CM
4640 if (ret == 1) {
4641 ClearPagePrivate(page);
4642 set_page_private(page, 0);
4643 page_cache_release(page);
39279cc3 4644 }
a52d9a80 4645 return ret;
39279cc3
CM
4646}
4647
e6dcd2dc
CM
4648static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
4649{
98509cfc
CM
4650 if (PageWriteback(page) || PageDirty(page))
4651 return 0;
b335b003 4652 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
e6dcd2dc
CM
4653}
4654
a52d9a80 4655static void btrfs_invalidatepage(struct page *page, unsigned long offset)
39279cc3 4656{
d1310b2e 4657 struct extent_io_tree *tree;
e6dcd2dc
CM
4658 struct btrfs_ordered_extent *ordered;
4659 u64 page_start = page_offset(page);
4660 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
39279cc3 4661
8b62b72b
CM
4662
4663 /*
4664 * we have the page locked, so new writeback can't start,
4665 * and the dirty bit won't be cleared while we are here.
4666 *
4667 * Wait for IO on this page so that we can safely clear
4668 * the PagePrivate2 bit and do ordered accounting
4669 */
e6dcd2dc 4670 wait_on_page_writeback(page);
8b62b72b 4671
d1310b2e 4672 tree = &BTRFS_I(page->mapping->host)->io_tree;
e6dcd2dc
CM
4673 if (offset) {
4674 btrfs_releasepage(page, GFP_NOFS);
4675 return;
4676 }
e6dcd2dc
CM
4677 lock_extent(tree, page_start, page_end, GFP_NOFS);
4678 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
4679 page_offset(page));
4680 if (ordered) {
eb84ae03
CM
4681 /*
4682 * IO on this page will never be started, so we need
4683 * to account for any ordered extents now
4684 */
e6dcd2dc
CM
4685 clear_extent_bit(tree, page_start, page_end,
4686 EXTENT_DIRTY | EXTENT_DELALLOC |
2c64c53d 4687 EXTENT_LOCKED, 1, 0, NULL, GFP_NOFS);
8b62b72b
CM
4688 /*
4689 * whoever cleared the private bit is responsible
4690 * for the finish_ordered_io
4691 */
4692 if (TestClearPagePrivate2(page)) {
4693 btrfs_finish_ordered_io(page->mapping->host,
4694 page_start, page_end);
4695 }
e6dcd2dc
CM
4696 btrfs_put_ordered_extent(ordered);
4697 lock_extent(tree, page_start, page_end, GFP_NOFS);
4698 }
4699 clear_extent_bit(tree, page_start, page_end,
8b62b72b 4700 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC,
2c64c53d 4701 1, 1, NULL, GFP_NOFS);
e6dcd2dc
CM
4702 __btrfs_releasepage(page, GFP_NOFS);
4703
4a096752 4704 ClearPageChecked(page);
9ad6b7bc 4705 if (PagePrivate(page)) {
9ad6b7bc
CM
4706 ClearPagePrivate(page);
4707 set_page_private(page, 0);
4708 page_cache_release(page);
4709 }
39279cc3
CM
4710}
4711
9ebefb18
CM
4712/*
4713 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
4714 * called from a page fault handler when a page is first dirtied. Hence we must
4715 * be careful to check for EOF conditions here. We set the page up correctly
4716 * for a written page which means we get ENOSPC checking when writing into
4717 * holes and correct delalloc and unwritten extent mapping on filesystems that
4718 * support these features.
4719 *
4720 * We are not allowed to take the i_mutex here so we have to play games to
4721 * protect against truncate races as the page could now be beyond EOF. Because
4722 * vmtruncate() writes the inode size before removing pages, once we have the
4723 * page lock we can determine safely if the page is beyond EOF. If it is not
4724 * beyond EOF, then the page is guaranteed safe against truncation until we
4725 * unlock the page.
4726 */
c2ec175c 4727int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
9ebefb18 4728{
c2ec175c 4729 struct page *page = vmf->page;
6da6abae 4730 struct inode *inode = fdentry(vma->vm_file)->d_inode;
1832a6d5 4731 struct btrfs_root *root = BTRFS_I(inode)->root;
e6dcd2dc
CM
4732 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4733 struct btrfs_ordered_extent *ordered;
4734 char *kaddr;
4735 unsigned long zero_start;
9ebefb18 4736 loff_t size;
1832a6d5 4737 int ret;
a52d9a80 4738 u64 page_start;
e6dcd2dc 4739 u64 page_end;
9ebefb18 4740
6a63209f 4741 ret = btrfs_check_data_free_space(root, inode, PAGE_CACHE_SIZE);
56a76f82
NP
4742 if (ret) {
4743 if (ret == -ENOMEM)
4744 ret = VM_FAULT_OOM;
4745 else /* -ENOSPC, -EIO, etc */
4746 ret = VM_FAULT_SIGBUS;
1832a6d5 4747 goto out;
56a76f82 4748 }
1832a6d5 4749
56a76f82 4750 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
e6dcd2dc 4751again:
9ebefb18 4752 lock_page(page);
9ebefb18 4753 size = i_size_read(inode);
e6dcd2dc
CM
4754 page_start = page_offset(page);
4755 page_end = page_start + PAGE_CACHE_SIZE - 1;
a52d9a80 4756
9ebefb18 4757 if ((page->mapping != inode->i_mapping) ||
e6dcd2dc 4758 (page_start >= size)) {
6a63209f 4759 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
9ebefb18
CM
4760 /* page got truncated out from underneath us */
4761 goto out_unlock;
4762 }
e6dcd2dc
CM
4763 wait_on_page_writeback(page);
4764
4765 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
4766 set_page_extent_mapped(page);
4767
eb84ae03
CM
4768 /*
4769 * we can't set the delalloc bits if there are pending ordered
4770 * extents. Drop our locks and wait for them to finish
4771 */
e6dcd2dc
CM
4772 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4773 if (ordered) {
4774 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
4775 unlock_page(page);
eb84ae03 4776 btrfs_start_ordered_extent(inode, ordered, 1);
e6dcd2dc
CM
4777 btrfs_put_ordered_extent(ordered);
4778 goto again;
4779 }
4780
ea8c2819 4781 btrfs_set_extent_delalloc(inode, page_start, page_end);
e6dcd2dc 4782 ret = 0;
9ebefb18
CM
4783
4784 /* page is wholly or partially inside EOF */
a52d9a80 4785 if (page_start + PAGE_CACHE_SIZE > size)
e6dcd2dc 4786 zero_start = size & ~PAGE_CACHE_MASK;
9ebefb18 4787 else
e6dcd2dc 4788 zero_start = PAGE_CACHE_SIZE;
9ebefb18 4789
e6dcd2dc
CM
4790 if (zero_start != PAGE_CACHE_SIZE) {
4791 kaddr = kmap(page);
4792 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
4793 flush_dcache_page(page);
4794 kunmap(page);
4795 }
247e743c 4796 ClearPageChecked(page);
e6dcd2dc 4797 set_page_dirty(page);
50a9b214 4798 SetPageUptodate(page);
5a3f23d5
CM
4799
4800 BTRFS_I(inode)->last_trans = root->fs_info->generation + 1;
e6dcd2dc 4801 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
9ebefb18
CM
4802
4803out_unlock:
50a9b214
CM
4804 if (!ret)
4805 return VM_FAULT_LOCKED;
9ebefb18 4806 unlock_page(page);
1832a6d5 4807out:
9ebefb18
CM
4808 return ret;
4809}
4810
39279cc3
CM
4811static void btrfs_truncate(struct inode *inode)
4812{
4813 struct btrfs_root *root = BTRFS_I(inode)->root;
4814 int ret;
4815 struct btrfs_trans_handle *trans;
d3c2fdcf 4816 unsigned long nr;
dbe674a9 4817 u64 mask = root->sectorsize - 1;
39279cc3
CM
4818
4819 if (!S_ISREG(inode->i_mode))
4820 return;
4821 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
4822 return;
4823
4824 btrfs_truncate_page(inode->i_mapping, inode->i_size);
4a096752 4825 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
39279cc3 4826
39279cc3 4827 trans = btrfs_start_transaction(root, 1);
5a3f23d5
CM
4828
4829 /*
4830 * setattr is responsible for setting the ordered_data_close flag,
4831 * but that is only tested during the last file release. That
4832 * could happen well after the next commit, leaving a great big
4833 * window where new writes may get lost if someone chooses to write
4834 * to this file after truncating to zero
4835 *
4836 * The inode doesn't have any dirty data here, and so if we commit
4837 * this is a noop. If someone immediately starts writing to the inode
4838 * it is very likely we'll catch some of their writes in this
4839 * transaction, and the commit will find this file on the ordered
4840 * data list with good things to send down.
4841 *
4842 * This is a best effort solution, there is still a window where
4843 * using truncate to replace the contents of the file will
4844 * end up with a zero length file after a crash.
4845 */
4846 if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close)
4847 btrfs_add_ordered_operation(trans, root, inode);
4848
39279cc3 4849 btrfs_set_trans_block_group(trans, inode);
dbe674a9 4850 btrfs_i_size_write(inode, inode->i_size);
39279cc3 4851
7b128766
JB
4852 ret = btrfs_orphan_add(trans, inode);
4853 if (ret)
4854 goto out;
39279cc3 4855 /* FIXME, add redo link to tree so we don't leak on crash */
e02119d5 4856 ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size,
85e21bac 4857 BTRFS_EXTENT_DATA_KEY);
39279cc3 4858 btrfs_update_inode(trans, root, inode);
5f39d397 4859
7b128766
JB
4860 ret = btrfs_orphan_del(trans, inode);
4861 BUG_ON(ret);
4862
4863out:
4864 nr = trans->blocks_used;
89ce8a63 4865 ret = btrfs_end_transaction_throttle(trans, root);
39279cc3 4866 BUG_ON(ret);
d3c2fdcf 4867 btrfs_btree_balance_dirty(root, nr);
39279cc3
CM
4868}
4869
d352ac68
CM
4870/*
4871 * create a new subvolume directory/inode (helper for the ioctl).
4872 */
d2fb3437 4873int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
76dda93c 4874 struct btrfs_root *new_root,
d2fb3437 4875 u64 new_dirid, u64 alloc_hint)
39279cc3 4876{
39279cc3 4877 struct inode *inode;
76dda93c 4878 int err;
00e4e6b3 4879 u64 index = 0;
39279cc3 4880
aec7477b 4881 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
d2fb3437 4882 new_dirid, alloc_hint, S_IFDIR | 0700, &index);
54aa1f4d 4883 if (IS_ERR(inode))
f46b5a66 4884 return PTR_ERR(inode);
39279cc3
CM
4885 inode->i_op = &btrfs_dir_inode_operations;
4886 inode->i_fop = &btrfs_dir_file_operations;
4887
39279cc3 4888 inode->i_nlink = 1;
dbe674a9 4889 btrfs_i_size_write(inode, 0);
3b96362c 4890
76dda93c
YZ
4891 err = btrfs_update_inode(trans, new_root, inode);
4892 BUG_ON(err);
cb8e7090 4893
76dda93c 4894 iput(inode);
cb8e7090 4895 return 0;
39279cc3
CM
4896}
4897
d352ac68
CM
4898/* helper function for file defrag and space balancing. This
4899 * forces readahead on a given range of bytes in an inode
4900 */
edbd8d4e 4901unsigned long btrfs_force_ra(struct address_space *mapping,
86479a04
CM
4902 struct file_ra_state *ra, struct file *file,
4903 pgoff_t offset, pgoff_t last_index)
4904{
8e7bf94f 4905 pgoff_t req_size = last_index - offset + 1;
86479a04 4906
86479a04
CM
4907 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
4908 return offset + req_size;
86479a04
CM
4909}
4910
39279cc3
CM
4911struct inode *btrfs_alloc_inode(struct super_block *sb)
4912{
4913 struct btrfs_inode *ei;
4914
4915 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
4916 if (!ei)
4917 return NULL;
15ee9bc7 4918 ei->last_trans = 0;
e02119d5 4919 ei->logged_trans = 0;
e6dcd2dc 4920 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
7b128766 4921 INIT_LIST_HEAD(&ei->i_orphan);
5a3f23d5 4922 INIT_LIST_HEAD(&ei->ordered_operations);
39279cc3
CM
4923 return &ei->vfs_inode;
4924}
4925
4926void btrfs_destroy_inode(struct inode *inode)
4927{
e6dcd2dc 4928 struct btrfs_ordered_extent *ordered;
5a3f23d5
CM
4929 struct btrfs_root *root = BTRFS_I(inode)->root;
4930
39279cc3
CM
4931 WARN_ON(!list_empty(&inode->i_dentry));
4932 WARN_ON(inode->i_data.nrpages);
4933
5a3f23d5
CM
4934 /*
4935 * Make sure we're properly removed from the ordered operation
4936 * lists.
4937 */
4938 smp_mb();
4939 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
4940 spin_lock(&root->fs_info->ordered_extent_lock);
4941 list_del_init(&BTRFS_I(inode)->ordered_operations);
4942 spin_unlock(&root->fs_info->ordered_extent_lock);
4943 }
4944
4945 spin_lock(&root->list_lock);
7b128766
JB
4946 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
4947 printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan"
4948 " list\n", inode->i_ino);
4949 dump_stack();
4950 }
5a3f23d5 4951 spin_unlock(&root->list_lock);
7b128766 4952
d397712b 4953 while (1) {
e6dcd2dc
CM
4954 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
4955 if (!ordered)
4956 break;
4957 else {
d397712b
CM
4958 printk(KERN_ERR "btrfs found ordered "
4959 "extent %llu %llu on inode cleanup\n",
4960 (unsigned long long)ordered->file_offset,
4961 (unsigned long long)ordered->len);
e6dcd2dc
CM
4962 btrfs_remove_ordered_extent(inode, ordered);
4963 btrfs_put_ordered_extent(ordered);
4964 btrfs_put_ordered_extent(ordered);
4965 }
4966 }
5d4f98a2 4967 inode_tree_del(inode);
5b21f2ed 4968 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
39279cc3
CM
4969 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
4970}
4971
76dda93c
YZ
4972void btrfs_drop_inode(struct inode *inode)
4973{
4974 struct btrfs_root *root = BTRFS_I(inode)->root;
4975
4976 if (inode->i_nlink > 0 && btrfs_root_refs(&root->root_item) == 0)
4977 generic_delete_inode(inode);
4978 else
4979 generic_drop_inode(inode);
4980}
4981
0ee0fda0 4982static void init_once(void *foo)
39279cc3
CM
4983{
4984 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
4985
4986 inode_init_once(&ei->vfs_inode);
4987}
4988
4989void btrfs_destroy_cachep(void)
4990{
4991 if (btrfs_inode_cachep)
4992 kmem_cache_destroy(btrfs_inode_cachep);
4993 if (btrfs_trans_handle_cachep)
4994 kmem_cache_destroy(btrfs_trans_handle_cachep);
4995 if (btrfs_transaction_cachep)
4996 kmem_cache_destroy(btrfs_transaction_cachep);
39279cc3
CM
4997 if (btrfs_path_cachep)
4998 kmem_cache_destroy(btrfs_path_cachep);
4999}
5000
5001int btrfs_init_cachep(void)
5002{
9601e3f6
CH
5003 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
5004 sizeof(struct btrfs_inode), 0,
5005 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
39279cc3
CM
5006 if (!btrfs_inode_cachep)
5007 goto fail;
9601e3f6
CH
5008
5009 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
5010 sizeof(struct btrfs_trans_handle), 0,
5011 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
39279cc3
CM
5012 if (!btrfs_trans_handle_cachep)
5013 goto fail;
9601e3f6
CH
5014
5015 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
5016 sizeof(struct btrfs_transaction), 0,
5017 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
39279cc3
CM
5018 if (!btrfs_transaction_cachep)
5019 goto fail;
9601e3f6
CH
5020
5021 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
5022 sizeof(struct btrfs_path), 0,
5023 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
39279cc3
CM
5024 if (!btrfs_path_cachep)
5025 goto fail;
9601e3f6 5026
39279cc3
CM
5027 return 0;
5028fail:
5029 btrfs_destroy_cachep();
5030 return -ENOMEM;
5031}
5032
5033static int btrfs_getattr(struct vfsmount *mnt,
5034 struct dentry *dentry, struct kstat *stat)
5035{
5036 struct inode *inode = dentry->d_inode;
5037 generic_fillattr(inode, stat);
3394e160 5038 stat->dev = BTRFS_I(inode)->root->anon_super.s_dev;
d6667462 5039 stat->blksize = PAGE_CACHE_SIZE;
a76a3cd4
YZ
5040 stat->blocks = (inode_get_bytes(inode) +
5041 BTRFS_I(inode)->delalloc_bytes) >> 9;
39279cc3
CM
5042 return 0;
5043}
5044
d397712b
CM
5045static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
5046 struct inode *new_dir, struct dentry *new_dentry)
39279cc3
CM
5047{
5048 struct btrfs_trans_handle *trans;
5049 struct btrfs_root *root = BTRFS_I(old_dir)->root;
4df27c4d 5050 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
39279cc3
CM
5051 struct inode *new_inode = new_dentry->d_inode;
5052 struct inode *old_inode = old_dentry->d_inode;
5053 struct timespec ctime = CURRENT_TIME;
00e4e6b3 5054 u64 index = 0;
4df27c4d 5055 u64 root_objectid;
39279cc3
CM
5056 int ret;
5057
4df27c4d
YZ
5058 /* we only allow rename subvolume link between subvolumes */
5059 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
3394e160
CM
5060 return -EXDEV;
5061
4df27c4d
YZ
5062 if (old_inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
5063 (new_inode && new_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID))
39279cc3 5064 return -ENOTEMPTY;
5f39d397 5065
4df27c4d
YZ
5066 if (S_ISDIR(old_inode->i_mode) && new_inode &&
5067 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
5068 return -ENOTEMPTY;
0660b5af 5069
6a63209f 5070 ret = btrfs_check_metadata_free_space(root);
1832a6d5 5071 if (ret)
4df27c4d 5072 return ret;
1832a6d5 5073
5a3f23d5
CM
5074 /*
5075 * we're using rename to replace one file with another.
5076 * and the replacement file is large. Start IO on it now so
5077 * we don't add too much work to the end of the transaction
5078 */
4baf8c92 5079 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
5a3f23d5
CM
5080 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
5081 filemap_flush(old_inode->i_mapping);
5082
76dda93c
YZ
5083 /* close the racy window with snapshot create/destroy ioctl */
5084 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
5085 down_read(&root->fs_info->subvol_sem);
5086
39279cc3 5087 trans = btrfs_start_transaction(root, 1);
a5719521 5088 btrfs_set_trans_block_group(trans, new_dir);
5f39d397 5089
4df27c4d
YZ
5090 if (dest != root)
5091 btrfs_record_root_in_trans(trans, dest);
5092
a5719521
YZ
5093 ret = btrfs_set_inode_index(new_dir, &index);
5094 if (ret)
5095 goto out_fail;
5a3f23d5 5096
a5719521 5097 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
4df27c4d
YZ
5098 /* force full log commit if subvolume involved. */
5099 root->fs_info->last_trans_log_full_commit = trans->transid;
5100 } else {
a5719521
YZ
5101 ret = btrfs_insert_inode_ref(trans, dest,
5102 new_dentry->d_name.name,
5103 new_dentry->d_name.len,
5104 old_inode->i_ino,
5105 new_dir->i_ino, index);
5106 if (ret)
5107 goto out_fail;
4df27c4d
YZ
5108 /*
5109 * this is an ugly little race, but the rename is required
5110 * to make sure that if we crash, the inode is either at the
5111 * old name or the new one. pinning the log transaction lets
5112 * us make sure we don't allow a log commit to come in after
5113 * we unlink the name but before we add the new name back in.
5114 */
5115 btrfs_pin_log_trans(root);
5116 }
a5719521
YZ
5117 /*
5118 * make sure the inode gets flushed if it is replacing
5119 * something.
5120 */
5121 if (new_inode && new_inode->i_size &&
5122 old_inode && S_ISREG(old_inode->i_mode)) {
5123 btrfs_add_ordered_operation(trans, root, old_inode);
5124 }
39279cc3 5125
39279cc3
CM
5126 old_dir->i_ctime = old_dir->i_mtime = ctime;
5127 new_dir->i_ctime = new_dir->i_mtime = ctime;
5128 old_inode->i_ctime = ctime;
5f39d397 5129
12fcfd22
CM
5130 if (old_dentry->d_parent != new_dentry->d_parent)
5131 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
5132
4df27c4d
YZ
5133 if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
5134 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
5135 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
5136 old_dentry->d_name.name,
5137 old_dentry->d_name.len);
5138 } else {
5139 btrfs_inc_nlink(old_dentry->d_inode);
5140 ret = btrfs_unlink_inode(trans, root, old_dir,
5141 old_dentry->d_inode,
5142 old_dentry->d_name.name,
5143 old_dentry->d_name.len);
5144 }
5145 BUG_ON(ret);
39279cc3
CM
5146
5147 if (new_inode) {
5148 new_inode->i_ctime = CURRENT_TIME;
4df27c4d
YZ
5149 if (unlikely(new_inode->i_ino ==
5150 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
5151 root_objectid = BTRFS_I(new_inode)->location.objectid;
5152 ret = btrfs_unlink_subvol(trans, dest, new_dir,
5153 root_objectid,
5154 new_dentry->d_name.name,
5155 new_dentry->d_name.len);
5156 BUG_ON(new_inode->i_nlink == 0);
5157 } else {
5158 ret = btrfs_unlink_inode(trans, dest, new_dir,
5159 new_dentry->d_inode,
5160 new_dentry->d_name.name,
5161 new_dentry->d_name.len);
5162 }
5163 BUG_ON(ret);
7b128766 5164 if (new_inode->i_nlink == 0) {
e02119d5 5165 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
4df27c4d 5166 BUG_ON(ret);
7b128766 5167 }
39279cc3 5168 }
aec7477b 5169
4df27c4d
YZ
5170 ret = btrfs_add_link(trans, new_dir, old_inode,
5171 new_dentry->d_name.name,
a5719521 5172 new_dentry->d_name.len, 0, index);
4df27c4d 5173 BUG_ON(ret);
39279cc3 5174
4df27c4d
YZ
5175 if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
5176 btrfs_log_new_name(trans, old_inode, old_dir,
5177 new_dentry->d_parent);
5178 btrfs_end_log_trans(root);
5179 }
a5719521 5180out_fail:
ab78c84d 5181 btrfs_end_transaction_throttle(trans, root);
4df27c4d 5182
76dda93c
YZ
5183 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
5184 up_read(&root->fs_info->subvol_sem);
39279cc3
CM
5185 return ret;
5186}
5187
d352ac68
CM
5188/*
5189 * some fairly slow code that needs optimization. This walks the list
5190 * of all the inodes with pending delalloc and forces them to disk.
5191 */
ea8c2819
CM
5192int btrfs_start_delalloc_inodes(struct btrfs_root *root)
5193{
5194 struct list_head *head = &root->fs_info->delalloc_inodes;
5195 struct btrfs_inode *binode;
5b21f2ed 5196 struct inode *inode;
ea8c2819 5197
c146afad
YZ
5198 if (root->fs_info->sb->s_flags & MS_RDONLY)
5199 return -EROFS;
5200
75eff68e 5201 spin_lock(&root->fs_info->delalloc_lock);
d397712b 5202 while (!list_empty(head)) {
ea8c2819
CM
5203 binode = list_entry(head->next, struct btrfs_inode,
5204 delalloc_inodes);
5b21f2ed
ZY
5205 inode = igrab(&binode->vfs_inode);
5206 if (!inode)
5207 list_del_init(&binode->delalloc_inodes);
75eff68e 5208 spin_unlock(&root->fs_info->delalloc_lock);
5b21f2ed 5209 if (inode) {
8c8bee1d 5210 filemap_flush(inode->i_mapping);
5b21f2ed
ZY
5211 iput(inode);
5212 }
5213 cond_resched();
75eff68e 5214 spin_lock(&root->fs_info->delalloc_lock);
ea8c2819 5215 }
75eff68e 5216 spin_unlock(&root->fs_info->delalloc_lock);
8c8bee1d
CM
5217
5218 /* the filemap_flush will queue IO into the worker threads, but
5219 * we have to make sure the IO is actually started and that
5220 * ordered extents get created before we return
5221 */
5222 atomic_inc(&root->fs_info->async_submit_draining);
d397712b 5223 while (atomic_read(&root->fs_info->nr_async_submits) ||
771ed689 5224 atomic_read(&root->fs_info->async_delalloc_pages)) {
8c8bee1d 5225 wait_event(root->fs_info->async_submit_wait,
771ed689
CM
5226 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
5227 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
8c8bee1d
CM
5228 }
5229 atomic_dec(&root->fs_info->async_submit_draining);
ea8c2819
CM
5230 return 0;
5231}
5232
39279cc3
CM
5233static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
5234 const char *symname)
5235{
5236 struct btrfs_trans_handle *trans;
5237 struct btrfs_root *root = BTRFS_I(dir)->root;
5238 struct btrfs_path *path;
5239 struct btrfs_key key;
1832a6d5 5240 struct inode *inode = NULL;
39279cc3
CM
5241 int err;
5242 int drop_inode = 0;
5243 u64 objectid;
00e4e6b3 5244 u64 index = 0 ;
39279cc3
CM
5245 int name_len;
5246 int datasize;
5f39d397 5247 unsigned long ptr;
39279cc3 5248 struct btrfs_file_extent_item *ei;
5f39d397 5249 struct extent_buffer *leaf;
1832a6d5 5250 unsigned long nr = 0;
39279cc3
CM
5251
5252 name_len = strlen(symname) + 1;
5253 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
5254 return -ENAMETOOLONG;
1832a6d5 5255
6a63209f 5256 err = btrfs_check_metadata_free_space(root);
1832a6d5
CM
5257 if (err)
5258 goto out_fail;
5259
39279cc3
CM
5260 trans = btrfs_start_transaction(root, 1);
5261 btrfs_set_trans_block_group(trans, dir);
5262
5263 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
5264 if (err) {
5265 err = -ENOSPC;
5266 goto out_unlock;
5267 }
5268
aec7477b 5269 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
9c58309d
CM
5270 dentry->d_name.len,
5271 dentry->d_parent->d_inode->i_ino, objectid,
00e4e6b3
CM
5272 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
5273 &index);
39279cc3
CM
5274 err = PTR_ERR(inode);
5275 if (IS_ERR(inode))
5276 goto out_unlock;
5277
0279b4cd 5278 err = btrfs_init_inode_security(inode, dir);
33268eaf
JB
5279 if (err) {
5280 drop_inode = 1;
5281 goto out_unlock;
5282 }
5283
39279cc3 5284 btrfs_set_trans_block_group(trans, inode);
00e4e6b3 5285 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
39279cc3
CM
5286 if (err)
5287 drop_inode = 1;
5288 else {
5289 inode->i_mapping->a_ops = &btrfs_aops;
04160088 5290 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
39279cc3
CM
5291 inode->i_fop = &btrfs_file_operations;
5292 inode->i_op = &btrfs_file_inode_operations;
d1310b2e 5293 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
39279cc3 5294 }
39279cc3
CM
5295 btrfs_update_inode_block_group(trans, inode);
5296 btrfs_update_inode_block_group(trans, dir);
5297 if (drop_inode)
5298 goto out_unlock;
5299
5300 path = btrfs_alloc_path();
5301 BUG_ON(!path);
5302 key.objectid = inode->i_ino;
5303 key.offset = 0;
39279cc3
CM
5304 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
5305 datasize = btrfs_file_extent_calc_inline_size(name_len);
5306 err = btrfs_insert_empty_item(trans, root, path, &key,
5307 datasize);
54aa1f4d
CM
5308 if (err) {
5309 drop_inode = 1;
5310 goto out_unlock;
5311 }
5f39d397
CM
5312 leaf = path->nodes[0];
5313 ei = btrfs_item_ptr(leaf, path->slots[0],
5314 struct btrfs_file_extent_item);
5315 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
5316 btrfs_set_file_extent_type(leaf, ei,
39279cc3 5317 BTRFS_FILE_EXTENT_INLINE);
c8b97818
CM
5318 btrfs_set_file_extent_encryption(leaf, ei, 0);
5319 btrfs_set_file_extent_compression(leaf, ei, 0);
5320 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
5321 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
5322
39279cc3 5323 ptr = btrfs_file_extent_inline_start(ei);
5f39d397
CM
5324 write_extent_buffer(leaf, symname, ptr, name_len);
5325 btrfs_mark_buffer_dirty(leaf);
39279cc3 5326 btrfs_free_path(path);
5f39d397 5327
39279cc3
CM
5328 inode->i_op = &btrfs_symlink_inode_operations;
5329 inode->i_mapping->a_ops = &btrfs_symlink_aops;
04160088 5330 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
d899e052 5331 inode_set_bytes(inode, name_len);
dbe674a9 5332 btrfs_i_size_write(inode, name_len - 1);
54aa1f4d
CM
5333 err = btrfs_update_inode(trans, root, inode);
5334 if (err)
5335 drop_inode = 1;
39279cc3
CM
5336
5337out_unlock:
d3c2fdcf 5338 nr = trans->blocks_used;
ab78c84d 5339 btrfs_end_transaction_throttle(trans, root);
1832a6d5 5340out_fail:
39279cc3
CM
5341 if (drop_inode) {
5342 inode_dec_link_count(inode);
5343 iput(inode);
5344 }
d3c2fdcf 5345 btrfs_btree_balance_dirty(root, nr);
39279cc3
CM
5346 return err;
5347}
16432985 5348
546888da
CM
5349static int prealloc_file_range(struct btrfs_trans_handle *trans,
5350 struct inode *inode, u64 start, u64 end,
e980b50c 5351 u64 locked_end, u64 alloc_hint, int mode)
d899e052 5352{
d899e052
YZ
5353 struct btrfs_root *root = BTRFS_I(inode)->root;
5354 struct btrfs_key ins;
5355 u64 alloc_size;
5356 u64 cur_offset = start;
5357 u64 num_bytes = end - start;
5358 int ret = 0;
5359
d899e052
YZ
5360 while (num_bytes > 0) {
5361 alloc_size = min(num_bytes, root->fs_info->max_extent);
5362 ret = btrfs_reserve_extent(trans, root, alloc_size,
5363 root->sectorsize, 0, alloc_hint,
5364 (u64)-1, &ins, 1);
5365 if (ret) {
5366 WARN_ON(1);
5367 goto out;
5368 }
5369 ret = insert_reserved_file_extent(trans, inode,
5370 cur_offset, ins.objectid,
5371 ins.offset, ins.offset,
e980b50c
CM
5372 ins.offset, locked_end,
5373 0, 0, 0,
d899e052
YZ
5374 BTRFS_FILE_EXTENT_PREALLOC);
5375 BUG_ON(ret);
a1ed835e
CM
5376 btrfs_drop_extent_cache(inode, cur_offset,
5377 cur_offset + ins.offset -1, 0);
d899e052
YZ
5378 num_bytes -= ins.offset;
5379 cur_offset += ins.offset;
5380 alloc_hint = ins.objectid + ins.offset;
5381 }
5382out:
5383 if (cur_offset > start) {
5384 inode->i_ctime = CURRENT_TIME;
6cbff00f 5385 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
d899e052
YZ
5386 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
5387 cur_offset > i_size_read(inode))
5388 btrfs_i_size_write(inode, cur_offset);
5389 ret = btrfs_update_inode(trans, root, inode);
5390 BUG_ON(ret);
5391 }
5392
d899e052
YZ
5393 return ret;
5394}
5395
5396static long btrfs_fallocate(struct inode *inode, int mode,
5397 loff_t offset, loff_t len)
5398{
5399 u64 cur_offset;
5400 u64 last_byte;
5401 u64 alloc_start;
5402 u64 alloc_end;
5403 u64 alloc_hint = 0;
e980b50c 5404 u64 locked_end;
d899e052
YZ
5405 u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
5406 struct extent_map *em;
546888da 5407 struct btrfs_trans_handle *trans;
a970b0a1 5408 struct btrfs_root *root;
d899e052
YZ
5409 int ret;
5410
5411 alloc_start = offset & ~mask;
5412 alloc_end = (offset + len + mask) & ~mask;
5413
546888da
CM
5414 /*
5415 * wait for ordered IO before we have any locks. We'll loop again
5416 * below with the locks held.
5417 */
5418 btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start);
5419
d899e052
YZ
5420 mutex_lock(&inode->i_mutex);
5421 if (alloc_start > inode->i_size) {
5422 ret = btrfs_cont_expand(inode, alloc_start);
5423 if (ret)
5424 goto out;
5425 }
5426
a970b0a1
JB
5427 root = BTRFS_I(inode)->root;
5428
5429 ret = btrfs_check_data_free_space(root, inode,
5430 alloc_end - alloc_start);
5431 if (ret)
5432 goto out;
5433
e980b50c 5434 locked_end = alloc_end - 1;
d899e052
YZ
5435 while (1) {
5436 struct btrfs_ordered_extent *ordered;
546888da
CM
5437
5438 trans = btrfs_start_transaction(BTRFS_I(inode)->root, 1);
5439 if (!trans) {
5440 ret = -EIO;
a970b0a1 5441 goto out_free;
546888da
CM
5442 }
5443
5444 /* the extent lock is ordered inside the running
5445 * transaction
5446 */
e980b50c
CM
5447 lock_extent(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
5448 GFP_NOFS);
d899e052
YZ
5449 ordered = btrfs_lookup_first_ordered_extent(inode,
5450 alloc_end - 1);
5451 if (ordered &&
5452 ordered->file_offset + ordered->len > alloc_start &&
5453 ordered->file_offset < alloc_end) {
5454 btrfs_put_ordered_extent(ordered);
5455 unlock_extent(&BTRFS_I(inode)->io_tree,
e980b50c 5456 alloc_start, locked_end, GFP_NOFS);
546888da
CM
5457 btrfs_end_transaction(trans, BTRFS_I(inode)->root);
5458
5459 /*
5460 * we can't wait on the range with the transaction
5461 * running or with the extent lock held
5462 */
d899e052
YZ
5463 btrfs_wait_ordered_range(inode, alloc_start,
5464 alloc_end - alloc_start);
5465 } else {
5466 if (ordered)
5467 btrfs_put_ordered_extent(ordered);
5468 break;
5469 }
5470 }
5471
5472 cur_offset = alloc_start;
5473 while (1) {
5474 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
5475 alloc_end - cur_offset, 0);
5476 BUG_ON(IS_ERR(em) || !em);
5477 last_byte = min(extent_map_end(em), alloc_end);
5478 last_byte = (last_byte + mask) & ~mask;
5479 if (em->block_start == EXTENT_MAP_HOLE) {
546888da 5480 ret = prealloc_file_range(trans, inode, cur_offset,
e980b50c
CM
5481 last_byte, locked_end + 1,
5482 alloc_hint, mode);
d899e052
YZ
5483 if (ret < 0) {
5484 free_extent_map(em);
5485 break;
5486 }
5487 }
5488 if (em->block_start <= EXTENT_MAP_LAST_BYTE)
5489 alloc_hint = em->block_start;
5490 free_extent_map(em);
5491
5492 cur_offset = last_byte;
5493 if (cur_offset >= alloc_end) {
5494 ret = 0;
5495 break;
5496 }
5497 }
e980b50c 5498 unlock_extent(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
d899e052 5499 GFP_NOFS);
546888da
CM
5500
5501 btrfs_end_transaction(trans, BTRFS_I(inode)->root);
a970b0a1
JB
5502out_free:
5503 btrfs_free_reserved_data_space(root, inode, alloc_end - alloc_start);
d899e052
YZ
5504out:
5505 mutex_unlock(&inode->i_mutex);
5506 return ret;
5507}
5508
e6dcd2dc
CM
5509static int btrfs_set_page_dirty(struct page *page)
5510{
e6dcd2dc
CM
5511 return __set_page_dirty_nobuffers(page);
5512}
5513
0ee0fda0 5514static int btrfs_permission(struct inode *inode, int mask)
fdebe2bd 5515{
6cbff00f 5516 if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE))
fdebe2bd 5517 return -EACCES;
33268eaf 5518 return generic_permission(inode, mask, btrfs_check_acl);
fdebe2bd 5519}
39279cc3
CM
5520
5521static struct inode_operations btrfs_dir_inode_operations = {
3394e160 5522 .getattr = btrfs_getattr,
39279cc3
CM
5523 .lookup = btrfs_lookup,
5524 .create = btrfs_create,
5525 .unlink = btrfs_unlink,
5526 .link = btrfs_link,
5527 .mkdir = btrfs_mkdir,
5528 .rmdir = btrfs_rmdir,
5529 .rename = btrfs_rename,
5530 .symlink = btrfs_symlink,
5531 .setattr = btrfs_setattr,
618e21d5 5532 .mknod = btrfs_mknod,
95819c05
CH
5533 .setxattr = btrfs_setxattr,
5534 .getxattr = btrfs_getxattr,
5103e947 5535 .listxattr = btrfs_listxattr,
95819c05 5536 .removexattr = btrfs_removexattr,
fdebe2bd 5537 .permission = btrfs_permission,
39279cc3 5538};
39279cc3
CM
5539static struct inode_operations btrfs_dir_ro_inode_operations = {
5540 .lookup = btrfs_lookup,
fdebe2bd 5541 .permission = btrfs_permission,
39279cc3 5542};
76dda93c 5543
39279cc3
CM
5544static struct file_operations btrfs_dir_file_operations = {
5545 .llseek = generic_file_llseek,
5546 .read = generic_read_dir,
cbdf5a24 5547 .readdir = btrfs_real_readdir,
34287aa3 5548 .unlocked_ioctl = btrfs_ioctl,
39279cc3 5549#ifdef CONFIG_COMPAT
34287aa3 5550 .compat_ioctl = btrfs_ioctl,
39279cc3 5551#endif
6bf13c0c 5552 .release = btrfs_release_file,
e02119d5 5553 .fsync = btrfs_sync_file,
39279cc3
CM
5554};
5555
d1310b2e 5556static struct extent_io_ops btrfs_extent_io_ops = {
07157aac 5557 .fill_delalloc = run_delalloc_range,
065631f6 5558 .submit_bio_hook = btrfs_submit_bio_hook,
239b14b3 5559 .merge_bio_hook = btrfs_merge_bio_hook,
07157aac 5560 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
e6dcd2dc 5561 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
247e743c 5562 .writepage_start_hook = btrfs_writepage_start_hook,
1259ab75 5563 .readpage_io_failed_hook = btrfs_io_failed_hook,
b0c68f8b
CM
5564 .set_bit_hook = btrfs_set_bit_hook,
5565 .clear_bit_hook = btrfs_clear_bit_hook,
07157aac
CM
5566};
5567
35054394
CM
5568/*
5569 * btrfs doesn't support the bmap operation because swapfiles
5570 * use bmap to make a mapping of extents in the file. They assume
5571 * these extents won't change over the life of the file and they
5572 * use the bmap result to do IO directly to the drive.
5573 *
5574 * the btrfs bmap call would return logical addresses that aren't
5575 * suitable for IO and they also will change frequently as COW
5576 * operations happen. So, swapfile + btrfs == corruption.
5577 *
5578 * For now we're avoiding this by dropping bmap.
5579 */
39279cc3
CM
5580static struct address_space_operations btrfs_aops = {
5581 .readpage = btrfs_readpage,
5582 .writepage = btrfs_writepage,
b293f02e 5583 .writepages = btrfs_writepages,
3ab2fb5a 5584 .readpages = btrfs_readpages,
39279cc3 5585 .sync_page = block_sync_page,
16432985 5586 .direct_IO = btrfs_direct_IO,
a52d9a80
CM
5587 .invalidatepage = btrfs_invalidatepage,
5588 .releasepage = btrfs_releasepage,
e6dcd2dc 5589 .set_page_dirty = btrfs_set_page_dirty,
39279cc3
CM
5590};
5591
5592static struct address_space_operations btrfs_symlink_aops = {
5593 .readpage = btrfs_readpage,
5594 .writepage = btrfs_writepage,
2bf5a725
CM
5595 .invalidatepage = btrfs_invalidatepage,
5596 .releasepage = btrfs_releasepage,
39279cc3
CM
5597};
5598
5599static struct inode_operations btrfs_file_inode_operations = {
5600 .truncate = btrfs_truncate,
5601 .getattr = btrfs_getattr,
5602 .setattr = btrfs_setattr,
95819c05
CH
5603 .setxattr = btrfs_setxattr,
5604 .getxattr = btrfs_getxattr,
5103e947 5605 .listxattr = btrfs_listxattr,
95819c05 5606 .removexattr = btrfs_removexattr,
fdebe2bd 5607 .permission = btrfs_permission,
d899e052 5608 .fallocate = btrfs_fallocate,
1506fcc8 5609 .fiemap = btrfs_fiemap,
39279cc3 5610};
618e21d5
JB
5611static struct inode_operations btrfs_special_inode_operations = {
5612 .getattr = btrfs_getattr,
5613 .setattr = btrfs_setattr,
fdebe2bd 5614 .permission = btrfs_permission,
95819c05
CH
5615 .setxattr = btrfs_setxattr,
5616 .getxattr = btrfs_getxattr,
33268eaf 5617 .listxattr = btrfs_listxattr,
95819c05 5618 .removexattr = btrfs_removexattr,
618e21d5 5619};
39279cc3
CM
5620static struct inode_operations btrfs_symlink_inode_operations = {
5621 .readlink = generic_readlink,
5622 .follow_link = page_follow_link_light,
5623 .put_link = page_put_link,
fdebe2bd 5624 .permission = btrfs_permission,
0279b4cd
JO
5625 .setxattr = btrfs_setxattr,
5626 .getxattr = btrfs_getxattr,
5627 .listxattr = btrfs_listxattr,
5628 .removexattr = btrfs_removexattr,
39279cc3 5629};
76dda93c
YZ
5630
5631struct dentry_operations btrfs_dentry_operations = {
5632 .d_delete = btrfs_dentry_delete,
5633};