Merge branch 'master' of ssh://mason@master.kernel.org/pub/scm/linux/kernel/git/mason...
[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>
29#include <linux/smp_lock.h>
30#include <linux/backing-dev.h>
31#include <linux/mpage.h>
32#include <linux/swap.h>
33#include <linux/writeback.h>
34#include <linux/statfs.h>
35#include <linux/compat.h>
9ebefb18 36#include <linux/bit_spinlock.h>
92fee66d 37#include <linux/version.h>
5103e947 38#include <linux/xattr.h>
33268eaf 39#include <linux/posix_acl.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
CM
49#include "compat.h"
50#include "tree-log.h"
5b84e8d6 51#include "ref-cache.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;
71struct kmem_cache *btrfs_bit_radix_cachep;
72struct kmem_cache *btrfs_path_cachep;
73
74#define S_SHIFT 12
75static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
76 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
77 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
78 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
79 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
80 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
81 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
82 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
83};
84
7b128766
JB
85static void btrfs_truncate(struct inode *inode);
86
d352ac68
CM
87/*
88 * a very lame attempt at stopping writes when the FS is 85% full. There
89 * are countless ways this is incorrect, but it is better than nothing.
90 */
1832a6d5
CM
91int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
92 int for_del)
93{
a2135011
CM
94 u64 total;
95 u64 used;
1832a6d5 96 u64 thresh;
bcbfce8a 97 unsigned long flags;
1832a6d5
CM
98 int ret = 0;
99
a2135011
CM
100 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
101 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
102 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
1832a6d5 103 if (for_del)
f9ef6604 104 thresh = total * 90;
1832a6d5 105 else
f9ef6604
CM
106 thresh = total * 85;
107
108 do_div(thresh, 100);
1832a6d5 109
1832a6d5
CM
110 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
111 ret = -ENOSPC;
bcbfce8a 112 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
1832a6d5
CM
113 return ret;
114}
115
d352ac68
CM
116/*
117 * when extent_io.c finds a delayed allocation range in the file,
118 * the call backs end up in this code. The basic idea is to
119 * allocate extents on disk for the range, and create ordered data structs
120 * in ram to track those extents.
121 */
be20aa9d 122static int cow_file_range(struct inode *inode, u64 start, u64 end)
b888db2b
CM
123{
124 struct btrfs_root *root = BTRFS_I(inode)->root;
125 struct btrfs_trans_handle *trans;
b888db2b 126 u64 alloc_hint = 0;
db94535d 127 u64 num_bytes;
c59f8951 128 u64 cur_alloc_size;
db94535d 129 u64 blocksize = root->sectorsize;
d1310b2e 130 u64 orig_num_bytes;
be20aa9d 131 struct btrfs_key ins;
e6dcd2dc
CM
132 struct extent_map *em;
133 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
134 int ret = 0;
b888db2b 135
f9295749 136 trans = btrfs_join_transaction(root, 1);
b888db2b 137 BUG_ON(!trans);
be20aa9d
CM
138 btrfs_set_trans_block_group(trans, inode);
139
db94535d 140 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
be20aa9d 141 num_bytes = max(blocksize, num_bytes);
d1310b2e 142 orig_num_bytes = num_bytes;
db94535d 143
179e29e4
CM
144 if (alloc_hint == EXTENT_MAP_INLINE)
145 goto out;
146
3b951516 147 BUG_ON(num_bytes > btrfs_super_total_bytes(&root->fs_info->super_copy));
e5a2217e 148 mutex_lock(&BTRFS_I(inode)->extent_mutex);
5b21f2ed 149 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
e5a2217e 150 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
3b951516 151
c59f8951
CM
152 while(num_bytes > 0) {
153 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
e6dcd2dc 154 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
0f9dd46c 155 root->sectorsize, 0, alloc_hint,
e6dcd2dc 156 (u64)-1, &ins, 1);
c59f8951
CM
157 if (ret) {
158 WARN_ON(1);
159 goto out;
160 }
e6dcd2dc
CM
161 em = alloc_extent_map(GFP_NOFS);
162 em->start = start;
163 em->len = ins.offset;
164 em->block_start = ins.objectid;
165 em->bdev = root->fs_info->fs_devices->latest_bdev;
e5a2217e 166 mutex_lock(&BTRFS_I(inode)->extent_mutex);
7f3c74fb 167 set_bit(EXTENT_FLAG_PINNED, &em->flags);
e6dcd2dc
CM
168 while(1) {
169 spin_lock(&em_tree->lock);
170 ret = add_extent_mapping(em_tree, em);
171 spin_unlock(&em_tree->lock);
172 if (ret != -EEXIST) {
173 free_extent_map(em);
174 break;
175 }
176 btrfs_drop_extent_cache(inode, start,
5b21f2ed 177 start + ins.offset - 1, 0);
e6dcd2dc 178 }
e5a2217e 179 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
e6dcd2dc 180
98d20f67 181 cur_alloc_size = ins.offset;
e6dcd2dc 182 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
7ea394f1 183 ins.offset, 0);
e6dcd2dc 184 BUG_ON(ret);
3b951516
CM
185 if (num_bytes < cur_alloc_size) {
186 printk("num_bytes %Lu cur_alloc %Lu\n", num_bytes,
187 cur_alloc_size);
188 break;
189 }
c59f8951
CM
190 num_bytes -= cur_alloc_size;
191 alloc_hint = ins.objectid + ins.offset;
192 start += cur_alloc_size;
b888db2b 193 }
b888db2b
CM
194out:
195 btrfs_end_transaction(trans, root);
be20aa9d
CM
196 return ret;
197}
198
d352ac68
CM
199/*
200 * when nowcow writeback call back. This checks for snapshots or COW copies
201 * of the extents that exist in the file, and COWs the file as required.
202 *
203 * If no cow copies or snapshots exist, we write directly to the existing
204 * blocks on disk
205 */
be20aa9d
CM
206static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
207{
208 u64 extent_start;
209 u64 extent_end;
210 u64 bytenr;
1832a6d5 211 u64 loops = 0;
c31f8830 212 u64 total_fs_bytes;
be20aa9d 213 struct btrfs_root *root = BTRFS_I(inode)->root;
a68d5933 214 struct btrfs_block_group_cache *block_group;
7ea394f1 215 struct btrfs_trans_handle *trans;
be20aa9d
CM
216 struct extent_buffer *leaf;
217 int found_type;
218 struct btrfs_path *path;
219 struct btrfs_file_extent_item *item;
220 int ret;
7ea394f1 221 int err = 0;
be20aa9d
CM
222 struct btrfs_key found_key;
223
c31f8830 224 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
be20aa9d
CM
225 path = btrfs_alloc_path();
226 BUG_ON(!path);
7ea394f1
YZ
227 trans = btrfs_join_transaction(root, 1);
228 BUG_ON(!trans);
be20aa9d
CM
229again:
230 ret = btrfs_lookup_file_extent(NULL, root, path,
231 inode->i_ino, start, 0);
232 if (ret < 0) {
7ea394f1
YZ
233 err = ret;
234 goto out;
be20aa9d
CM
235 }
236
be20aa9d
CM
237 if (ret != 0) {
238 if (path->slots[0] == 0)
239 goto not_found;
240 path->slots[0]--;
241 }
242
243 leaf = path->nodes[0];
244 item = btrfs_item_ptr(leaf, path->slots[0],
245 struct btrfs_file_extent_item);
246
247 /* are we inside the extent that was found? */
248 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
249 found_type = btrfs_key_type(&found_key);
250 if (found_key.objectid != inode->i_ino ||
bbaf549e 251 found_type != BTRFS_EXTENT_DATA_KEY)
be20aa9d 252 goto not_found;
be20aa9d
CM
253
254 found_type = btrfs_file_extent_type(leaf, item);
255 extent_start = found_key.offset;
256 if (found_type == BTRFS_FILE_EXTENT_REG) {
c31f8830
CM
257 u64 extent_num_bytes;
258
259 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
260 extent_end = extent_start + extent_num_bytes;
be20aa9d
CM
261 err = 0;
262
1832a6d5
CM
263 if (loops && start != extent_start)
264 goto not_found;
265
be20aa9d
CM
266 if (start < extent_start || start >= extent_end)
267 goto not_found;
268
be20aa9d
CM
269 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
270 if (bytenr == 0)
271 goto not_found;
272
7ea394f1 273 if (btrfs_cross_ref_exists(trans, root, &found_key, bytenr))
a68d5933 274 goto not_found;
c31f8830
CM
275 /*
276 * we may be called by the resizer, make sure we're inside
277 * the limits of the FS
278 */
a68d5933
CM
279 block_group = btrfs_lookup_block_group(root->fs_info,
280 bytenr);
281 if (!block_group || block_group->ro)
c31f8830
CM
282 goto not_found;
283
7ea394f1
YZ
284 bytenr += btrfs_file_extent_offset(leaf, item);
285 extent_num_bytes = min(end + 1, extent_end) - start;
286 ret = btrfs_add_ordered_extent(inode, start, bytenr,
287 extent_num_bytes, 1);
288 if (ret) {
289 err = ret;
290 goto out;
291 }
292
293 btrfs_release_path(root, path);
be20aa9d 294 start = extent_end;
7ea394f1
YZ
295 if (start <= end) {
296 loops++;
297 goto again;
298 }
bd09835d 299 } else {
7ea394f1
YZ
300not_found:
301 btrfs_end_transaction(trans, root);
be20aa9d 302 btrfs_free_path(path);
7ea394f1 303 return cow_file_range(inode, start, end);
be20aa9d 304 }
7ea394f1
YZ
305out:
306 WARN_ON(err);
307 btrfs_end_transaction(trans, root);
308 btrfs_free_path(path);
309 return err;
be20aa9d
CM
310}
311
d352ac68
CM
312/*
313 * extent_io.c call back to do delayed allocation processing
314 */
be20aa9d
CM
315static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
316{
317 struct btrfs_root *root = BTRFS_I(inode)->root;
318 int ret;
a2135011 319
b98b6767
Y
320 if (btrfs_test_opt(root, NODATACOW) ||
321 btrfs_test_flag(inode, NODATACOW))
be20aa9d
CM
322 ret = run_delalloc_nocow(inode, start, end);
323 else
324 ret = cow_file_range(inode, start, end);
1832a6d5 325
b888db2b
CM
326 return ret;
327}
328
d352ac68
CM
329/*
330 * extent_io.c set_bit_hook, used to track delayed allocation
331 * bytes in this file, and to maintain the list of inodes that
332 * have pending delalloc work to be done.
333 */
291d673e 334int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
b0c68f8b 335 unsigned long old, unsigned long bits)
291d673e 336{
bcbfce8a 337 unsigned long flags;
b0c68f8b 338 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
291d673e 339 struct btrfs_root *root = BTRFS_I(inode)->root;
bcbfce8a 340 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
9069218d 341 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
291d673e 342 root->fs_info->delalloc_bytes += end - start + 1;
ea8c2819
CM
343 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
344 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
345 &root->fs_info->delalloc_inodes);
346 }
bcbfce8a 347 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
291d673e
CM
348 }
349 return 0;
350}
351
d352ac68
CM
352/*
353 * extent_io.c clear_bit_hook, see set_bit_hook for why
354 */
291d673e 355int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
b0c68f8b 356 unsigned long old, unsigned long bits)
291d673e 357{
b0c68f8b 358 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
291d673e 359 struct btrfs_root *root = BTRFS_I(inode)->root;
bcbfce8a
CM
360 unsigned long flags;
361
362 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
b0c68f8b
CM
363 if (end - start + 1 > root->fs_info->delalloc_bytes) {
364 printk("warning: delalloc account %Lu %Lu\n",
365 end - start + 1, root->fs_info->delalloc_bytes);
366 root->fs_info->delalloc_bytes = 0;
9069218d 367 BTRFS_I(inode)->delalloc_bytes = 0;
b0c68f8b
CM
368 } else {
369 root->fs_info->delalloc_bytes -= end - start + 1;
9069218d 370 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
b0c68f8b 371 }
ea8c2819
CM
372 if (BTRFS_I(inode)->delalloc_bytes == 0 &&
373 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
374 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
375 }
bcbfce8a 376 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
291d673e
CM
377 }
378 return 0;
379}
380
d352ac68
CM
381/*
382 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
383 * we don't create bios that span stripes or chunks
384 */
239b14b3
CM
385int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
386 size_t size, struct bio *bio)
387{
388 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
389 struct btrfs_mapping_tree *map_tree;
a62b9401 390 u64 logical = (u64)bio->bi_sector << 9;
239b14b3
CM
391 u64 length = 0;
392 u64 map_length;
239b14b3
CM
393 int ret;
394
f2d8d74d 395 length = bio->bi_size;
239b14b3
CM
396 map_tree = &root->fs_info->mapping_tree;
397 map_length = length;
cea9e445 398 ret = btrfs_map_block(map_tree, READ, logical,
f188591e 399 &map_length, NULL, 0);
cea9e445 400
239b14b3 401 if (map_length < length + size) {
239b14b3
CM
402 return 1;
403 }
404 return 0;
405}
406
d352ac68
CM
407/*
408 * in order to insert checksums into the metadata in large chunks,
409 * we wait until bio submission time. All the pages in the bio are
410 * checksummed and sums are attached onto the ordered extent record.
411 *
412 * At IO completion time the cums attached on the ordered extent record
413 * are inserted into the btree
414 */
44b8bd7e 415int __btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
f188591e 416 int mirror_num)
065631f6 417{
065631f6 418 struct btrfs_root *root = BTRFS_I(inode)->root;
065631f6 419 int ret = 0;
e015640f 420
3edf7d33 421 ret = btrfs_csum_one_bio(root, inode, bio);
44b8bd7e 422 BUG_ON(ret);
e015640f 423
8b712842 424 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
44b8bd7e
CM
425}
426
d352ac68
CM
427/*
428 * extent_io.c submission hook. This does the right thing for csum calculation on write,
429 * or reading the csums from the tree before a read
430 */
44b8bd7e
CM
431int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
432 int mirror_num)
433{
434 struct btrfs_root *root = BTRFS_I(inode)->root;
435 int ret = 0;
436
e6dcd2dc
CM
437 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
438 BUG_ON(ret);
065631f6 439
7ea394f1
YZ
440 if (btrfs_test_opt(root, NODATASUM) ||
441 btrfs_test_flag(inode, NODATASUM)) {
442 goto mapit;
443 }
444
4d1b5fb4
CM
445 if (!(rw & (1 << BIO_RW))) {
446 btrfs_lookup_bio_sums(root, inode, bio);
447 goto mapit;
448 }
44b8bd7e
CM
449 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
450 inode, rw, bio, mirror_num,
451 __btrfs_submit_bio_hook);
0b86a832 452mapit:
8b712842 453 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
065631f6 454}
6885f308 455
d352ac68
CM
456/*
457 * given a list of ordered sums record them in the inode. This happens
458 * at IO completion time based on sums calculated at bio submission time.
459 */
ba1da2f4 460static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
e6dcd2dc
CM
461 struct inode *inode, u64 file_offset,
462 struct list_head *list)
463{
464 struct list_head *cur;
465 struct btrfs_ordered_sum *sum;
466
467 btrfs_set_trans_block_group(trans, inode);
ba1da2f4 468 list_for_each(cur, list) {
e6dcd2dc 469 sum = list_entry(cur, struct btrfs_ordered_sum, list);
e6dcd2dc
CM
470 btrfs_csum_file_blocks(trans, BTRFS_I(inode)->root,
471 inode, sum);
e6dcd2dc
CM
472 }
473 return 0;
474}
475
ea8c2819
CM
476int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end)
477{
478 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
479 GFP_NOFS);
480}
481
d352ac68 482/* see btrfs_writepage_start_hook for details on why this is required */
247e743c
CM
483struct btrfs_writepage_fixup {
484 struct page *page;
485 struct btrfs_work work;
486};
487
247e743c
CM
488void btrfs_writepage_fixup_worker(struct btrfs_work *work)
489{
490 struct btrfs_writepage_fixup *fixup;
491 struct btrfs_ordered_extent *ordered;
492 struct page *page;
493 struct inode *inode;
494 u64 page_start;
495 u64 page_end;
496
497 fixup = container_of(work, struct btrfs_writepage_fixup, work);
498 page = fixup->page;
4a096752 499again:
247e743c
CM
500 lock_page(page);
501 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
502 ClearPageChecked(page);
503 goto out_page;
504 }
505
506 inode = page->mapping->host;
507 page_start = page_offset(page);
508 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
509
510 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
4a096752
CM
511
512 /* already ordered? We're done */
513 if (test_range_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
514 EXTENT_ORDERED, 0)) {
247e743c 515 goto out;
4a096752
CM
516 }
517
518 ordered = btrfs_lookup_ordered_extent(inode, page_start);
519 if (ordered) {
520 unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
521 page_end, GFP_NOFS);
522 unlock_page(page);
523 btrfs_start_ordered_extent(inode, ordered, 1);
524 goto again;
525 }
247e743c 526
ea8c2819 527 btrfs_set_extent_delalloc(inode, page_start, page_end);
247e743c
CM
528 ClearPageChecked(page);
529out:
530 unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
531out_page:
532 unlock_page(page);
533 page_cache_release(page);
534}
535
536/*
537 * There are a few paths in the higher layers of the kernel that directly
538 * set the page dirty bit without asking the filesystem if it is a
539 * good idea. This causes problems because we want to make sure COW
540 * properly happens and the data=ordered rules are followed.
541 *
542 * In our case any range that doesn't have the EXTENT_ORDERED bit set
543 * hasn't been properly setup for IO. We kick off an async process
544 * to fix it up. The async helper will wait for ordered extents, set
545 * the delalloc bit and make it safe to write the page.
546 */
547int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
548{
549 struct inode *inode = page->mapping->host;
550 struct btrfs_writepage_fixup *fixup;
551 struct btrfs_root *root = BTRFS_I(inode)->root;
552 int ret;
553
554 ret = test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
555 EXTENT_ORDERED, 0);
556 if (ret)
557 return 0;
558
559 if (PageChecked(page))
560 return -EAGAIN;
561
562 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
563 if (!fixup)
564 return -EAGAIN;
f421950f 565
247e743c
CM
566 SetPageChecked(page);
567 page_cache_get(page);
568 fixup->work.func = btrfs_writepage_fixup_worker;
569 fixup->page = page;
570 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
571 return -EAGAIN;
572}
573
d352ac68
CM
574/* as ordered data IO finishes, this gets called so we can finish
575 * an ordered extent if the range of bytes in the file it covers are
576 * fully written.
577 */
211f90e6 578static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
e6dcd2dc 579{
e6dcd2dc
CM
580 struct btrfs_root *root = BTRFS_I(inode)->root;
581 struct btrfs_trans_handle *trans;
582 struct btrfs_ordered_extent *ordered_extent;
583 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
31840ae1
ZY
584 struct btrfs_file_extent_item *extent_item;
585 struct btrfs_path *path = NULL;
586 struct extent_buffer *leaf;
e6dcd2dc
CM
587 u64 alloc_hint = 0;
588 struct list_head list;
589 struct btrfs_key ins;
590 int ret;
591
592 ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
ba1da2f4 593 if (!ret)
e6dcd2dc 594 return 0;
e6dcd2dc 595
f9295749 596 trans = btrfs_join_transaction(root, 1);
e6dcd2dc
CM
597
598 ordered_extent = btrfs_lookup_ordered_extent(inode, start);
599 BUG_ON(!ordered_extent);
7ea394f1
YZ
600 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags))
601 goto nocow;
e6dcd2dc 602
31840ae1
ZY
603 path = btrfs_alloc_path();
604 BUG_ON(!path);
605
e6dcd2dc
CM
606 lock_extent(io_tree, ordered_extent->file_offset,
607 ordered_extent->file_offset + ordered_extent->len - 1,
608 GFP_NOFS);
609
610 INIT_LIST_HEAD(&list);
611
ee6e6504 612 mutex_lock(&BTRFS_I(inode)->extent_mutex);
e5a2217e 613
e6dcd2dc
CM
614 ret = btrfs_drop_extents(trans, root, inode,
615 ordered_extent->file_offset,
616 ordered_extent->file_offset +
617 ordered_extent->len,
618 ordered_extent->file_offset, &alloc_hint);
619 BUG_ON(ret);
31840ae1
ZY
620
621 ins.objectid = inode->i_ino;
622 ins.offset = ordered_extent->file_offset;
623 ins.type = BTRFS_EXTENT_DATA_KEY;
624 ret = btrfs_insert_empty_item(trans, root, path, &ins,
625 sizeof(*extent_item));
e6dcd2dc 626 BUG_ON(ret);
31840ae1
ZY
627 leaf = path->nodes[0];
628 extent_item = btrfs_item_ptr(leaf, path->slots[0],
629 struct btrfs_file_extent_item);
630 btrfs_set_file_extent_generation(leaf, extent_item, trans->transid);
631 btrfs_set_file_extent_type(leaf, extent_item, BTRFS_FILE_EXTENT_REG);
632 btrfs_set_file_extent_disk_bytenr(leaf, extent_item,
633 ordered_extent->start);
634 btrfs_set_file_extent_disk_num_bytes(leaf, extent_item,
635 ordered_extent->len);
636 btrfs_set_file_extent_offset(leaf, extent_item, 0);
637 btrfs_set_file_extent_num_bytes(leaf, extent_item,
638 ordered_extent->len);
639 btrfs_mark_buffer_dirty(leaf);
7f3c74fb 640
e6dcd2dc
CM
641 btrfs_drop_extent_cache(inode, ordered_extent->file_offset,
642 ordered_extent->file_offset +
5b21f2ed 643 ordered_extent->len - 1, 0);
ee6e6504
CM
644 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
645
31840ae1
ZY
646 ins.objectid = ordered_extent->start;
647 ins.offset = ordered_extent->len;
648 ins.type = BTRFS_EXTENT_ITEM_KEY;
649 ret = btrfs_alloc_reserved_extent(trans, root, leaf->start,
650 root->root_key.objectid,
3bb1a1bc 651 trans->transid, inode->i_ino, &ins);
31840ae1
ZY
652 BUG_ON(ret);
653 btrfs_release_path(root, path);
654
a76a3cd4 655 inode_add_bytes(inode, ordered_extent->len);
e6dcd2dc
CM
656 unlock_extent(io_tree, ordered_extent->file_offset,
657 ordered_extent->file_offset + ordered_extent->len - 1,
658 GFP_NOFS);
7ea394f1 659nocow:
e6dcd2dc
CM
660 add_pending_csums(trans, inode, ordered_extent->file_offset,
661 &ordered_extent->list);
662
34353029 663 mutex_lock(&BTRFS_I(inode)->extent_mutex);
dbe674a9 664 btrfs_ordered_update_i_size(inode, ordered_extent);
e02119d5 665 btrfs_update_inode(trans, root, inode);
e6dcd2dc 666 btrfs_remove_ordered_extent(inode, ordered_extent);
34353029 667 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
7f3c74fb 668
e6dcd2dc
CM
669 /* once for us */
670 btrfs_put_ordered_extent(ordered_extent);
671 /* once for the tree */
672 btrfs_put_ordered_extent(ordered_extent);
673
e6dcd2dc 674 btrfs_end_transaction(trans, root);
31840ae1
ZY
675 if (path)
676 btrfs_free_path(path);
e6dcd2dc
CM
677 return 0;
678}
679
211f90e6
CM
680int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
681 struct extent_state *state, int uptodate)
682{
683 return btrfs_finish_ordered_io(page->mapping->host, start, end);
684}
685
d352ac68
CM
686/*
687 * When IO fails, either with EIO or csum verification fails, we
688 * try other mirrors that might have a good copy of the data. This
689 * io_failure_record is used to record state as we go through all the
690 * mirrors. If another mirror has good data, the page is set up to date
691 * and things continue. If a good mirror can't be found, the original
692 * bio end_io callback is called to indicate things have failed.
693 */
7e38326f
CM
694struct io_failure_record {
695 struct page *page;
696 u64 start;
697 u64 len;
698 u64 logical;
699 int last_mirror;
700};
701
1259ab75
CM
702int btrfs_io_failed_hook(struct bio *failed_bio,
703 struct page *page, u64 start, u64 end,
704 struct extent_state *state)
7e38326f
CM
705{
706 struct io_failure_record *failrec = NULL;
707 u64 private;
708 struct extent_map *em;
709 struct inode *inode = page->mapping->host;
710 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
3b951516 711 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
7e38326f
CM
712 struct bio *bio;
713 int num_copies;
714 int ret;
1259ab75 715 int rw;
7e38326f
CM
716 u64 logical;
717
718 ret = get_state_private(failure_tree, start, &private);
719 if (ret) {
7e38326f
CM
720 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
721 if (!failrec)
722 return -ENOMEM;
723 failrec->start = start;
724 failrec->len = end - start + 1;
725 failrec->last_mirror = 0;
726
3b951516
CM
727 spin_lock(&em_tree->lock);
728 em = lookup_extent_mapping(em_tree, start, failrec->len);
729 if (em->start > start || em->start + em->len < start) {
730 free_extent_map(em);
731 em = NULL;
732 }
733 spin_unlock(&em_tree->lock);
7e38326f
CM
734
735 if (!em || IS_ERR(em)) {
736 kfree(failrec);
737 return -EIO;
738 }
739 logical = start - em->start;
740 logical = em->block_start + logical;
741 failrec->logical = logical;
742 free_extent_map(em);
743 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
744 EXTENT_DIRTY, GFP_NOFS);
587f7704
CM
745 set_state_private(failure_tree, start,
746 (u64)(unsigned long)failrec);
7e38326f 747 } else {
587f7704 748 failrec = (struct io_failure_record *)(unsigned long)private;
7e38326f
CM
749 }
750 num_copies = btrfs_num_copies(
751 &BTRFS_I(inode)->root->fs_info->mapping_tree,
752 failrec->logical, failrec->len);
753 failrec->last_mirror++;
754 if (!state) {
755 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
756 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
757 failrec->start,
758 EXTENT_LOCKED);
759 if (state && state->start != failrec->start)
760 state = NULL;
761 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
762 }
763 if (!state || failrec->last_mirror > num_copies) {
764 set_state_private(failure_tree, failrec->start, 0);
765 clear_extent_bits(failure_tree, failrec->start,
766 failrec->start + failrec->len - 1,
767 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
768 kfree(failrec);
769 return -EIO;
770 }
771 bio = bio_alloc(GFP_NOFS, 1);
772 bio->bi_private = state;
773 bio->bi_end_io = failed_bio->bi_end_io;
774 bio->bi_sector = failrec->logical >> 9;
775 bio->bi_bdev = failed_bio->bi_bdev;
e1c4b745 776 bio->bi_size = 0;
7e38326f 777 bio_add_page(bio, page, failrec->len, start - page_offset(page));
1259ab75
CM
778 if (failed_bio->bi_rw & (1 << BIO_RW))
779 rw = WRITE;
780 else
781 rw = READ;
782
783 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
784 failrec->last_mirror);
785 return 0;
786}
787
d352ac68
CM
788/*
789 * each time an IO finishes, we do a fast check in the IO failure tree
790 * to see if we need to process or clean up an io_failure_record
791 */
1259ab75
CM
792int btrfs_clean_io_failures(struct inode *inode, u64 start)
793{
794 u64 private;
795 u64 private_failure;
796 struct io_failure_record *failure;
797 int ret;
798
799 private = 0;
800 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
801 (u64)-1, 1, EXTENT_DIRTY)) {
802 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
803 start, &private_failure);
804 if (ret == 0) {
805 failure = (struct io_failure_record *)(unsigned long)
806 private_failure;
807 set_state_private(&BTRFS_I(inode)->io_failure_tree,
808 failure->start, 0);
809 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
810 failure->start,
811 failure->start + failure->len - 1,
812 EXTENT_DIRTY | EXTENT_LOCKED,
813 GFP_NOFS);
814 kfree(failure);
815 }
816 }
7e38326f
CM
817 return 0;
818}
819
d352ac68
CM
820/*
821 * when reads are done, we need to check csums to verify the data is correct
822 * if there's a match, we allow the bio to finish. If not, we go through
823 * the io_failure_record routines to find good copies
824 */
70dec807
CM
825int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
826 struct extent_state *state)
07157aac 827{
35ebb934 828 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
07157aac 829 struct inode *inode = page->mapping->host;
d1310b2e 830 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
07157aac 831 char *kaddr;
aadfeb6e 832 u64 private = ~(u32)0;
07157aac 833 int ret;
ff79f819
CM
834 struct btrfs_root *root = BTRFS_I(inode)->root;
835 u32 csum = ~(u32)0;
bbf0d006 836 unsigned long flags;
d1310b2e 837
b98b6767
Y
838 if (btrfs_test_opt(root, NODATASUM) ||
839 btrfs_test_flag(inode, NODATASUM))
b6cda9bc 840 return 0;
c2e639f0 841 if (state && state->start == start) {
70dec807
CM
842 private = state->private;
843 ret = 0;
844 } else {
845 ret = get_state_private(io_tree, start, &private);
846 }
bbf0d006 847 local_irq_save(flags);
07157aac
CM
848 kaddr = kmap_atomic(page, KM_IRQ0);
849 if (ret) {
850 goto zeroit;
851 }
ff79f819
CM
852 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
853 btrfs_csum_final(csum, (char *)&csum);
854 if (csum != private) {
07157aac
CM
855 goto zeroit;
856 }
857 kunmap_atomic(kaddr, KM_IRQ0);
bbf0d006 858 local_irq_restore(flags);
7e38326f
CM
859
860 /* if the io failure tree for this inode is non-empty,
861 * check to see if we've recovered from a failed IO
862 */
1259ab75 863 btrfs_clean_io_failures(inode, start);
07157aac
CM
864 return 0;
865
866zeroit:
aadfeb6e
CM
867 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
868 page->mapping->host->i_ino, (unsigned long long)start, csum,
869 private);
db94535d
CM
870 memset(kaddr + offset, 1, end - start + 1);
871 flush_dcache_page(page);
07157aac 872 kunmap_atomic(kaddr, KM_IRQ0);
bbf0d006 873 local_irq_restore(flags);
3b951516
CM
874 if (private == 0)
875 return 0;
7e38326f 876 return -EIO;
07157aac 877}
b888db2b 878
7b128766
JB
879/*
880 * This creates an orphan entry for the given inode in case something goes
881 * wrong in the middle of an unlink/truncate.
882 */
883int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
884{
885 struct btrfs_root *root = BTRFS_I(inode)->root;
886 int ret = 0;
887
bcc63abb 888 spin_lock(&root->list_lock);
7b128766
JB
889
890 /* already on the orphan list, we're good */
891 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
bcc63abb 892 spin_unlock(&root->list_lock);
7b128766
JB
893 return 0;
894 }
895
896 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
897
bcc63abb 898 spin_unlock(&root->list_lock);
7b128766
JB
899
900 /*
901 * insert an orphan item to track this unlinked/truncated file
902 */
903 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
904
905 return ret;
906}
907
908/*
909 * We have done the truncate/delete so we can go ahead and remove the orphan
910 * item for this particular inode.
911 */
912int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
913{
914 struct btrfs_root *root = BTRFS_I(inode)->root;
915 int ret = 0;
916
bcc63abb 917 spin_lock(&root->list_lock);
7b128766
JB
918
919 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
bcc63abb 920 spin_unlock(&root->list_lock);
7b128766
JB
921 return 0;
922 }
923
924 list_del_init(&BTRFS_I(inode)->i_orphan);
925 if (!trans) {
bcc63abb 926 spin_unlock(&root->list_lock);
7b128766
JB
927 return 0;
928 }
929
bcc63abb 930 spin_unlock(&root->list_lock);
7b128766
JB
931
932 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
933
934 return ret;
935}
936
937/*
938 * this cleans up any orphans that may be left on the list from the last use
939 * of this root.
940 */
941void btrfs_orphan_cleanup(struct btrfs_root *root)
942{
943 struct btrfs_path *path;
944 struct extent_buffer *leaf;
945 struct btrfs_item *item;
946 struct btrfs_key key, found_key;
947 struct btrfs_trans_handle *trans;
948 struct inode *inode;
949 int ret = 0, nr_unlink = 0, nr_truncate = 0;
950
951 /* don't do orphan cleanup if the fs is readonly. */
5b21f2ed 952 if (root->fs_info->sb->s_flags & MS_RDONLY)
7b128766
JB
953 return;
954
955 path = btrfs_alloc_path();
956 if (!path)
957 return;
958 path->reada = -1;
959
960 key.objectid = BTRFS_ORPHAN_OBJECTID;
961 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
962 key.offset = (u64)-1;
963
7b128766
JB
964
965 while (1) {
966 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
967 if (ret < 0) {
968 printk(KERN_ERR "Error searching slot for orphan: %d"
969 "\n", ret);
970 break;
971 }
972
973 /*
974 * if ret == 0 means we found what we were searching for, which
975 * is weird, but possible, so only screw with path if we didnt
976 * find the key and see if we have stuff that matches
977 */
978 if (ret > 0) {
979 if (path->slots[0] == 0)
980 break;
981 path->slots[0]--;
982 }
983
984 /* pull out the item */
985 leaf = path->nodes[0];
986 item = btrfs_item_nr(leaf, path->slots[0]);
987 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
988
989 /* make sure the item matches what we want */
990 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
991 break;
992 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
993 break;
994
995 /* release the path since we're done with it */
996 btrfs_release_path(root, path);
997
998 /*
999 * this is where we are basically btrfs_lookup, without the
1000 * crossing root thing. we store the inode number in the
1001 * offset of the orphan item.
1002 */
5b21f2ed 1003 inode = btrfs_iget_locked(root->fs_info->sb,
7b128766
JB
1004 found_key.offset, root);
1005 if (!inode)
1006 break;
1007
1008 if (inode->i_state & I_NEW) {
1009 BTRFS_I(inode)->root = root;
1010
1011 /* have to set the location manually */
1012 BTRFS_I(inode)->location.objectid = inode->i_ino;
1013 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
1014 BTRFS_I(inode)->location.offset = 0;
1015
1016 btrfs_read_locked_inode(inode);
1017 unlock_new_inode(inode);
1018 }
1019
1020 /*
1021 * add this inode to the orphan list so btrfs_orphan_del does
1022 * the proper thing when we hit it
1023 */
bcc63abb 1024 spin_lock(&root->list_lock);
7b128766 1025 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
bcc63abb 1026 spin_unlock(&root->list_lock);
7b128766
JB
1027
1028 /*
1029 * if this is a bad inode, means we actually succeeded in
1030 * removing the inode, but not the orphan record, which means
1031 * we need to manually delete the orphan since iput will just
1032 * do a destroy_inode
1033 */
1034 if (is_bad_inode(inode)) {
5b21f2ed 1035 trans = btrfs_start_transaction(root, 1);
7b128766 1036 btrfs_orphan_del(trans, inode);
5b21f2ed 1037 btrfs_end_transaction(trans, root);
7b128766
JB
1038 iput(inode);
1039 continue;
1040 }
1041
1042 /* if we have links, this was a truncate, lets do that */
1043 if (inode->i_nlink) {
1044 nr_truncate++;
1045 btrfs_truncate(inode);
1046 } else {
1047 nr_unlink++;
1048 }
1049
1050 /* this will do delete_inode and everything for us */
1051 iput(inode);
1052 }
1053
1054 if (nr_unlink)
1055 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
1056 if (nr_truncate)
1057 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
1058
1059 btrfs_free_path(path);
7b128766
JB
1060}
1061
d352ac68
CM
1062/*
1063 * read an inode from the btree into the in-memory inode
1064 */
39279cc3
CM
1065void btrfs_read_locked_inode(struct inode *inode)
1066{
1067 struct btrfs_path *path;
5f39d397 1068 struct extent_buffer *leaf;
39279cc3 1069 struct btrfs_inode_item *inode_item;
0b86a832 1070 struct btrfs_timespec *tspec;
39279cc3
CM
1071 struct btrfs_root *root = BTRFS_I(inode)->root;
1072 struct btrfs_key location;
1073 u64 alloc_group_block;
618e21d5 1074 u32 rdev;
39279cc3
CM
1075 int ret;
1076
1077 path = btrfs_alloc_path();
1078 BUG_ON(!path);
39279cc3 1079 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
dc17ff8f 1080
39279cc3 1081 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
5f39d397 1082 if (ret)
39279cc3 1083 goto make_bad;
39279cc3 1084
5f39d397
CM
1085 leaf = path->nodes[0];
1086 inode_item = btrfs_item_ptr(leaf, path->slots[0],
1087 struct btrfs_inode_item);
1088
1089 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
1090 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
1091 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
1092 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
dbe674a9 1093 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
5f39d397
CM
1094
1095 tspec = btrfs_inode_atime(inode_item);
1096 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1097 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1098
1099 tspec = btrfs_inode_mtime(inode_item);
1100 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1101 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1102
1103 tspec = btrfs_inode_ctime(inode_item);
1104 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1105 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1106
a76a3cd4 1107 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
e02119d5
CM
1108 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
1109 inode->i_generation = BTRFS_I(inode)->generation;
618e21d5 1110 inode->i_rdev = 0;
5f39d397
CM
1111 rdev = btrfs_inode_rdev(leaf, inode_item);
1112
aec7477b
JB
1113 BTRFS_I(inode)->index_cnt = (u64)-1;
1114
5f39d397 1115 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
39279cc3
CM
1116 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
1117 alloc_group_block);
b98b6767 1118 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
e52ec0eb
CM
1119 if (!BTRFS_I(inode)->block_group) {
1120 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
0b86a832
CM
1121 NULL, 0,
1122 BTRFS_BLOCK_GROUP_METADATA, 0);
e52ec0eb 1123 }
39279cc3
CM
1124 btrfs_free_path(path);
1125 inode_item = NULL;
1126
39279cc3 1127 switch (inode->i_mode & S_IFMT) {
39279cc3
CM
1128 case S_IFREG:
1129 inode->i_mapping->a_ops = &btrfs_aops;
04160088 1130 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
d1310b2e 1131 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
39279cc3
CM
1132 inode->i_fop = &btrfs_file_operations;
1133 inode->i_op = &btrfs_file_inode_operations;
1134 break;
1135 case S_IFDIR:
1136 inode->i_fop = &btrfs_dir_file_operations;
1137 if (root == root->fs_info->tree_root)
1138 inode->i_op = &btrfs_dir_ro_inode_operations;
1139 else
1140 inode->i_op = &btrfs_dir_inode_operations;
1141 break;
1142 case S_IFLNK:
1143 inode->i_op = &btrfs_symlink_inode_operations;
1144 inode->i_mapping->a_ops = &btrfs_symlink_aops;
04160088 1145 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
39279cc3 1146 break;
618e21d5
JB
1147 default:
1148 init_special_inode(inode, inode->i_mode, rdev);
1149 break;
39279cc3
CM
1150 }
1151 return;
1152
1153make_bad:
39279cc3 1154 btrfs_free_path(path);
39279cc3
CM
1155 make_bad_inode(inode);
1156}
1157
d352ac68
CM
1158/*
1159 * given a leaf and an inode, copy the inode fields into the leaf
1160 */
e02119d5
CM
1161static void fill_inode_item(struct btrfs_trans_handle *trans,
1162 struct extent_buffer *leaf,
5f39d397 1163 struct btrfs_inode_item *item,
39279cc3
CM
1164 struct inode *inode)
1165{
5f39d397
CM
1166 btrfs_set_inode_uid(leaf, item, inode->i_uid);
1167 btrfs_set_inode_gid(leaf, item, inode->i_gid);
dbe674a9 1168 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
5f39d397
CM
1169 btrfs_set_inode_mode(leaf, item, inode->i_mode);
1170 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
1171
1172 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
1173 inode->i_atime.tv_sec);
1174 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
1175 inode->i_atime.tv_nsec);
1176
1177 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
1178 inode->i_mtime.tv_sec);
1179 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
1180 inode->i_mtime.tv_nsec);
1181
1182 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
1183 inode->i_ctime.tv_sec);
1184 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
1185 inode->i_ctime.tv_nsec);
1186
a76a3cd4 1187 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
e02119d5
CM
1188 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
1189 btrfs_set_inode_transid(leaf, item, trans->transid);
5f39d397 1190 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
b98b6767 1191 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
5f39d397 1192 btrfs_set_inode_block_group(leaf, item,
39279cc3
CM
1193 BTRFS_I(inode)->block_group->key.objectid);
1194}
1195
d352ac68
CM
1196/*
1197 * copy everything in the in-memory inode into the btree.
1198 */
ba1da2f4 1199int noinline btrfs_update_inode(struct btrfs_trans_handle *trans,
39279cc3
CM
1200 struct btrfs_root *root,
1201 struct inode *inode)
1202{
1203 struct btrfs_inode_item *inode_item;
1204 struct btrfs_path *path;
5f39d397 1205 struct extent_buffer *leaf;
39279cc3
CM
1206 int ret;
1207
1208 path = btrfs_alloc_path();
1209 BUG_ON(!path);
39279cc3
CM
1210 ret = btrfs_lookup_inode(trans, root, path,
1211 &BTRFS_I(inode)->location, 1);
1212 if (ret) {
1213 if (ret > 0)
1214 ret = -ENOENT;
1215 goto failed;
1216 }
1217
5f39d397
CM
1218 leaf = path->nodes[0];
1219 inode_item = btrfs_item_ptr(leaf, path->slots[0],
39279cc3
CM
1220 struct btrfs_inode_item);
1221
e02119d5 1222 fill_inode_item(trans, leaf, inode_item, inode);
5f39d397 1223 btrfs_mark_buffer_dirty(leaf);
15ee9bc7 1224 btrfs_set_inode_last_trans(trans, inode);
39279cc3
CM
1225 ret = 0;
1226failed:
39279cc3
CM
1227 btrfs_free_path(path);
1228 return ret;
1229}
1230
1231
d352ac68
CM
1232/*
1233 * unlink helper that gets used here in inode.c and in the tree logging
1234 * recovery code. It remove a link in a directory with a given name, and
1235 * also drops the back refs in the inode to the directory
1236 */
e02119d5
CM
1237int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
1238 struct btrfs_root *root,
1239 struct inode *dir, struct inode *inode,
1240 const char *name, int name_len)
39279cc3
CM
1241{
1242 struct btrfs_path *path;
39279cc3 1243 int ret = 0;
5f39d397 1244 struct extent_buffer *leaf;
39279cc3 1245 struct btrfs_dir_item *di;
5f39d397 1246 struct btrfs_key key;
aec7477b 1247 u64 index;
39279cc3
CM
1248
1249 path = btrfs_alloc_path();
54aa1f4d
CM
1250 if (!path) {
1251 ret = -ENOMEM;
1252 goto err;
1253 }
1254
39279cc3
CM
1255 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
1256 name, name_len, -1);
1257 if (IS_ERR(di)) {
1258 ret = PTR_ERR(di);
1259 goto err;
1260 }
1261 if (!di) {
1262 ret = -ENOENT;
1263 goto err;
1264 }
5f39d397
CM
1265 leaf = path->nodes[0];
1266 btrfs_dir_item_key_to_cpu(leaf, di, &key);
39279cc3 1267 ret = btrfs_delete_one_dir_name(trans, root, path, di);
54aa1f4d
CM
1268 if (ret)
1269 goto err;
39279cc3
CM
1270 btrfs_release_path(root, path);
1271
aec7477b 1272 ret = btrfs_del_inode_ref(trans, root, name, name_len,
e02119d5
CM
1273 inode->i_ino,
1274 dir->i_ino, &index);
aec7477b
JB
1275 if (ret) {
1276 printk("failed to delete reference to %.*s, "
1277 "inode %lu parent %lu\n", name_len, name,
e02119d5 1278 inode->i_ino, dir->i_ino);
aec7477b
JB
1279 goto err;
1280 }
1281
39279cc3 1282 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
aec7477b 1283 index, name, name_len, -1);
39279cc3
CM
1284 if (IS_ERR(di)) {
1285 ret = PTR_ERR(di);
1286 goto err;
1287 }
1288 if (!di) {
1289 ret = -ENOENT;
1290 goto err;
1291 }
1292 ret = btrfs_delete_one_dir_name(trans, root, path, di);
925baedd 1293 btrfs_release_path(root, path);
39279cc3 1294
e02119d5
CM
1295 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
1296 inode, dir->i_ino);
49eb7e46
CM
1297 BUG_ON(ret != 0 && ret != -ENOENT);
1298 if (ret != -ENOENT)
1299 BTRFS_I(dir)->log_dirty_trans = trans->transid;
e02119d5
CM
1300
1301 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
1302 dir, index);
1303 BUG_ON(ret);
39279cc3
CM
1304err:
1305 btrfs_free_path(path);
e02119d5
CM
1306 if (ret)
1307 goto out;
1308
1309 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
1310 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1311 btrfs_update_inode(trans, root, dir);
1312 btrfs_drop_nlink(inode);
1313 ret = btrfs_update_inode(trans, root, inode);
1314 dir->i_sb->s_dirt = 1;
1315out:
39279cc3
CM
1316 return ret;
1317}
1318
1319static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
1320{
1321 struct btrfs_root *root;
1322 struct btrfs_trans_handle *trans;
7b128766 1323 struct inode *inode = dentry->d_inode;
39279cc3 1324 int ret;
1832a6d5 1325 unsigned long nr = 0;
39279cc3
CM
1326
1327 root = BTRFS_I(dir)->root;
1832a6d5
CM
1328
1329 ret = btrfs_check_free_space(root, 1, 1);
1330 if (ret)
1331 goto fail;
1332
39279cc3 1333 trans = btrfs_start_transaction(root, 1);
5f39d397 1334
39279cc3 1335 btrfs_set_trans_block_group(trans, dir);
e02119d5
CM
1336 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
1337 dentry->d_name.name, dentry->d_name.len);
7b128766
JB
1338
1339 if (inode->i_nlink == 0)
1340 ret = btrfs_orphan_add(trans, inode);
1341
d3c2fdcf 1342 nr = trans->blocks_used;
5f39d397 1343
89ce8a63 1344 btrfs_end_transaction_throttle(trans, root);
1832a6d5 1345fail:
d3c2fdcf 1346 btrfs_btree_balance_dirty(root, nr);
39279cc3
CM
1347 return ret;
1348}
1349
1350static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
1351{
1352 struct inode *inode = dentry->d_inode;
1832a6d5 1353 int err = 0;
39279cc3
CM
1354 int ret;
1355 struct btrfs_root *root = BTRFS_I(dir)->root;
39279cc3 1356 struct btrfs_trans_handle *trans;
1832a6d5 1357 unsigned long nr = 0;
39279cc3 1358
925baedd 1359 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
134d4512 1360 return -ENOTEMPTY;
925baedd 1361 }
134d4512 1362
1832a6d5
CM
1363 ret = btrfs_check_free_space(root, 1, 1);
1364 if (ret)
1365 goto fail;
1366
39279cc3
CM
1367 trans = btrfs_start_transaction(root, 1);
1368 btrfs_set_trans_block_group(trans, dir);
39279cc3 1369
7b128766
JB
1370 err = btrfs_orphan_add(trans, inode);
1371 if (err)
1372 goto fail_trans;
1373
39279cc3 1374 /* now the directory is empty */
e02119d5
CM
1375 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
1376 dentry->d_name.name, dentry->d_name.len);
39279cc3 1377 if (!err) {
dbe674a9 1378 btrfs_i_size_write(inode, 0);
39279cc3 1379 }
3954401f 1380
7b128766 1381fail_trans:
d3c2fdcf 1382 nr = trans->blocks_used;
89ce8a63 1383 ret = btrfs_end_transaction_throttle(trans, root);
1832a6d5 1384fail:
d3c2fdcf 1385 btrfs_btree_balance_dirty(root, nr);
3954401f 1386
39279cc3
CM
1387 if (ret && !err)
1388 err = ret;
1389 return err;
1390}
1391
323ac95b
CM
1392/*
1393 * when truncating bytes in a file, it is possible to avoid reading
1394 * the leaves that contain only checksum items. This can be the
1395 * majority of the IO required to delete a large file, but it must
1396 * be done carefully.
1397 *
1398 * The keys in the level just above the leaves are checked to make sure
1399 * the lowest key in a given leaf is a csum key, and starts at an offset
1400 * after the new size.
1401 *
1402 * Then the key for the next leaf is checked to make sure it also has
1403 * a checksum item for the same file. If it does, we know our target leaf
1404 * contains only checksum items, and it can be safely freed without reading
1405 * it.
1406 *
1407 * This is just an optimization targeted at large files. It may do
1408 * nothing. It will return 0 unless things went badly.
1409 */
1410static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
1411 struct btrfs_root *root,
1412 struct btrfs_path *path,
1413 struct inode *inode, u64 new_size)
1414{
1415 struct btrfs_key key;
1416 int ret;
1417 int nritems;
1418 struct btrfs_key found_key;
1419 struct btrfs_key other_key;
5b84e8d6
YZ
1420 struct btrfs_leaf_ref *ref;
1421 u64 leaf_gen;
1422 u64 leaf_start;
323ac95b
CM
1423
1424 path->lowest_level = 1;
1425 key.objectid = inode->i_ino;
1426 key.type = BTRFS_CSUM_ITEM_KEY;
1427 key.offset = new_size;
1428again:
1429 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1430 if (ret < 0)
1431 goto out;
1432
1433 if (path->nodes[1] == NULL) {
1434 ret = 0;
1435 goto out;
1436 }
1437 ret = 0;
1438 btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
1439 nritems = btrfs_header_nritems(path->nodes[1]);
1440
1441 if (!nritems)
1442 goto out;
1443
1444 if (path->slots[1] >= nritems)
1445 goto next_node;
1446
1447 /* did we find a key greater than anything we want to delete? */
1448 if (found_key.objectid > inode->i_ino ||
1449 (found_key.objectid == inode->i_ino && found_key.type > key.type))
1450 goto out;
1451
1452 /* we check the next key in the node to make sure the leave contains
1453 * only checksum items. This comparison doesn't work if our
1454 * leaf is the last one in the node
1455 */
1456 if (path->slots[1] + 1 >= nritems) {
1457next_node:
1458 /* search forward from the last key in the node, this
1459 * will bring us into the next node in the tree
1460 */
1461 btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
1462
1463 /* unlikely, but we inc below, so check to be safe */
1464 if (found_key.offset == (u64)-1)
1465 goto out;
1466
1467 /* search_forward needs a path with locks held, do the
1468 * search again for the original key. It is possible
1469 * this will race with a balance and return a path that
1470 * we could modify, but this drop is just an optimization
1471 * and is allowed to miss some leaves.
1472 */
1473 btrfs_release_path(root, path);
1474 found_key.offset++;
1475
1476 /* setup a max key for search_forward */
1477 other_key.offset = (u64)-1;
1478 other_key.type = key.type;
1479 other_key.objectid = key.objectid;
1480
1481 path->keep_locks = 1;
1482 ret = btrfs_search_forward(root, &found_key, &other_key,
1483 path, 0, 0);
1484 path->keep_locks = 0;
1485 if (ret || found_key.objectid != key.objectid ||
1486 found_key.type != key.type) {
1487 ret = 0;
1488 goto out;
1489 }
1490
1491 key.offset = found_key.offset;
1492 btrfs_release_path(root, path);
1493 cond_resched();
1494 goto again;
1495 }
1496
1497 /* we know there's one more slot after us in the tree,
1498 * read that key so we can verify it is also a checksum item
1499 */
1500 btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
1501
1502 if (found_key.objectid < inode->i_ino)
1503 goto next_key;
1504
1505 if (found_key.type != key.type || found_key.offset < new_size)
1506 goto next_key;
1507
1508 /*
1509 * if the key for the next leaf isn't a csum key from this objectid,
1510 * we can't be sure there aren't good items inside this leaf.
1511 * Bail out
1512 */
1513 if (other_key.objectid != inode->i_ino || other_key.type != key.type)
1514 goto out;
1515
5b84e8d6
YZ
1516 leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
1517 leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
323ac95b
CM
1518 /*
1519 * it is safe to delete this leaf, it contains only
1520 * csum items from this inode at an offset >= new_size
1521 */
5b84e8d6 1522 ret = btrfs_del_leaf(trans, root, path, leaf_start);
323ac95b
CM
1523 BUG_ON(ret);
1524
5b84e8d6
YZ
1525 if (root->ref_cows && leaf_gen < trans->transid) {
1526 ref = btrfs_alloc_leaf_ref(root, 0);
1527 if (ref) {
1528 ref->root_gen = root->root_key.offset;
1529 ref->bytenr = leaf_start;
1530 ref->owner = 0;
1531 ref->generation = leaf_gen;
1532 ref->nritems = 0;
1533
1534 ret = btrfs_add_leaf_ref(root, ref, 0);
1535 WARN_ON(ret);
1536 btrfs_free_leaf_ref(root, ref);
1537 } else {
1538 WARN_ON(1);
1539 }
1540 }
323ac95b
CM
1541next_key:
1542 btrfs_release_path(root, path);
1543
1544 if (other_key.objectid == inode->i_ino &&
1545 other_key.type == key.type && other_key.offset > key.offset) {
1546 key.offset = other_key.offset;
1547 cond_resched();
1548 goto again;
1549 }
1550 ret = 0;
1551out:
1552 /* fixup any changes we've made to the path */
1553 path->lowest_level = 0;
1554 path->keep_locks = 0;
1555 btrfs_release_path(root, path);
1556 return ret;
1557}
1558
39279cc3
CM
1559/*
1560 * this can truncate away extent items, csum items and directory items.
1561 * It starts at a high offset and removes keys until it can't find
d352ac68 1562 * any higher than new_size
39279cc3
CM
1563 *
1564 * csum items that cross the new i_size are truncated to the new size
1565 * as well.
7b128766
JB
1566 *
1567 * min_type is the minimum key type to truncate down to. If set to 0, this
1568 * will kill all the items on this inode, including the INODE_ITEM_KEY.
39279cc3 1569 */
e02119d5
CM
1570noinline int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
1571 struct btrfs_root *root,
1572 struct inode *inode,
1573 u64 new_size, u32 min_type)
39279cc3
CM
1574{
1575 int ret;
1576 struct btrfs_path *path;
1577 struct btrfs_key key;
5f39d397 1578 struct btrfs_key found_key;
39279cc3 1579 u32 found_type;
5f39d397 1580 struct extent_buffer *leaf;
39279cc3
CM
1581 struct btrfs_file_extent_item *fi;
1582 u64 extent_start = 0;
db94535d 1583 u64 extent_num_bytes = 0;
39279cc3 1584 u64 item_end = 0;
7bb86316 1585 u64 root_gen = 0;
d8d5f3e1 1586 u64 root_owner = 0;
39279cc3
CM
1587 int found_extent;
1588 int del_item;
85e21bac
CM
1589 int pending_del_nr = 0;
1590 int pending_del_slot = 0;
179e29e4 1591 int extent_type = -1;
3b951516 1592 u64 mask = root->sectorsize - 1;
39279cc3 1593
e02119d5 1594 if (root->ref_cows)
5b21f2ed 1595 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
39279cc3 1596 path = btrfs_alloc_path();
3c69faec 1597 path->reada = -1;
39279cc3 1598 BUG_ON(!path);
5f39d397 1599
39279cc3
CM
1600 /* FIXME, add redo link to tree so we don't leak on crash */
1601 key.objectid = inode->i_ino;
1602 key.offset = (u64)-1;
5f39d397
CM
1603 key.type = (u8)-1;
1604
85e21bac 1605 btrfs_init_path(path);
323ac95b
CM
1606
1607 ret = drop_csum_leaves(trans, root, path, inode, new_size);
1608 BUG_ON(ret);
1609
85e21bac
CM
1610search_again:
1611 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1612 if (ret < 0) {
1613 goto error;
1614 }
1615 if (ret > 0) {
e02119d5
CM
1616 /* there are no items in the tree for us to truncate, we're
1617 * done
1618 */
1619 if (path->slots[0] == 0) {
1620 ret = 0;
1621 goto error;
1622 }
85e21bac
CM
1623 path->slots[0]--;
1624 }
1625
39279cc3 1626 while(1) {
39279cc3 1627 fi = NULL;
5f39d397
CM
1628 leaf = path->nodes[0];
1629 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1630 found_type = btrfs_key_type(&found_key);
39279cc3 1631
5f39d397 1632 if (found_key.objectid != inode->i_ino)
39279cc3 1633 break;
5f39d397 1634
85e21bac 1635 if (found_type < min_type)
39279cc3
CM
1636 break;
1637
5f39d397 1638 item_end = found_key.offset;
39279cc3 1639 if (found_type == BTRFS_EXTENT_DATA_KEY) {
5f39d397 1640 fi = btrfs_item_ptr(leaf, path->slots[0],
39279cc3 1641 struct btrfs_file_extent_item);
179e29e4
CM
1642 extent_type = btrfs_file_extent_type(leaf, fi);
1643 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
5f39d397 1644 item_end +=
db94535d 1645 btrfs_file_extent_num_bytes(leaf, fi);
179e29e4
CM
1646 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1647 struct btrfs_item *item = btrfs_item_nr(leaf,
1648 path->slots[0]);
1649 item_end += btrfs_file_extent_inline_len(leaf,
1650 item);
39279cc3 1651 }
008630c1 1652 item_end--;
39279cc3
CM
1653 }
1654 if (found_type == BTRFS_CSUM_ITEM_KEY) {
1655 ret = btrfs_csum_truncate(trans, root, path,
e02119d5 1656 new_size);
39279cc3
CM
1657 BUG_ON(ret);
1658 }
e02119d5 1659 if (item_end < new_size) {
b888db2b
CM
1660 if (found_type == BTRFS_DIR_ITEM_KEY) {
1661 found_type = BTRFS_INODE_ITEM_KEY;
1662 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
1663 found_type = BTRFS_CSUM_ITEM_KEY;
85e21bac
CM
1664 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
1665 found_type = BTRFS_XATTR_ITEM_KEY;
1666 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
1667 found_type = BTRFS_INODE_REF_KEY;
b888db2b
CM
1668 } else if (found_type) {
1669 found_type--;
1670 } else {
1671 break;
39279cc3 1672 }
a61721d5 1673 btrfs_set_key_type(&key, found_type);
85e21bac 1674 goto next;
39279cc3 1675 }
e02119d5 1676 if (found_key.offset >= new_size)
39279cc3
CM
1677 del_item = 1;
1678 else
1679 del_item = 0;
1680 found_extent = 0;
1681
1682 /* FIXME, shrink the extent if the ref count is only 1 */
179e29e4
CM
1683 if (found_type != BTRFS_EXTENT_DATA_KEY)
1684 goto delete;
1685
1686 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
39279cc3 1687 u64 num_dec;
db94535d 1688 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
39279cc3 1689 if (!del_item) {
db94535d
CM
1690 u64 orig_num_bytes =
1691 btrfs_file_extent_num_bytes(leaf, fi);
e02119d5 1692 extent_num_bytes = new_size -
5f39d397 1693 found_key.offset + root->sectorsize - 1;
b1632b10
Y
1694 extent_num_bytes = extent_num_bytes &
1695 ~((u64)root->sectorsize - 1);
db94535d
CM
1696 btrfs_set_file_extent_num_bytes(leaf, fi,
1697 extent_num_bytes);
1698 num_dec = (orig_num_bytes -
9069218d 1699 extent_num_bytes);
e02119d5 1700 if (root->ref_cows && extent_start != 0)
a76a3cd4 1701 inode_sub_bytes(inode, num_dec);
5f39d397 1702 btrfs_mark_buffer_dirty(leaf);
39279cc3 1703 } else {
db94535d
CM
1704 extent_num_bytes =
1705 btrfs_file_extent_disk_num_bytes(leaf,
1706 fi);
39279cc3 1707 /* FIXME blocksize != 4096 */
9069218d 1708 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
39279cc3
CM
1709 if (extent_start != 0) {
1710 found_extent = 1;
e02119d5 1711 if (root->ref_cows)
a76a3cd4 1712 inode_sub_bytes(inode, num_dec);
e02119d5 1713 }
31840ae1 1714 root_gen = btrfs_header_generation(leaf);
d8d5f3e1 1715 root_owner = btrfs_header_owner(leaf);
39279cc3 1716 }
9069218d
CM
1717 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1718 if (!del_item) {
e02119d5
CM
1719 u32 size = new_size - found_key.offset;
1720
1721 if (root->ref_cows) {
a76a3cd4
YZ
1722 inode_sub_bytes(inode, item_end + 1 -
1723 new_size);
e02119d5
CM
1724 }
1725 size =
1726 btrfs_file_extent_calc_inline_size(size);
9069218d 1727 ret = btrfs_truncate_item(trans, root, path,
e02119d5 1728 size, 1);
9069218d 1729 BUG_ON(ret);
e02119d5 1730 } else if (root->ref_cows) {
a76a3cd4
YZ
1731 inode_sub_bytes(inode, item_end + 1 -
1732 found_key.offset);
9069218d 1733 }
39279cc3 1734 }
179e29e4 1735delete:
39279cc3 1736 if (del_item) {
85e21bac
CM
1737 if (!pending_del_nr) {
1738 /* no pending yet, add ourselves */
1739 pending_del_slot = path->slots[0];
1740 pending_del_nr = 1;
1741 } else if (pending_del_nr &&
1742 path->slots[0] + 1 == pending_del_slot) {
1743 /* hop on the pending chunk */
1744 pending_del_nr++;
1745 pending_del_slot = path->slots[0];
1746 } else {
1747 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
1748 }
39279cc3
CM
1749 } else {
1750 break;
1751 }
39279cc3
CM
1752 if (found_extent) {
1753 ret = btrfs_free_extent(trans, root, extent_start,
7bb86316 1754 extent_num_bytes,
31840ae1 1755 leaf->start, root_owner,
3bb1a1bc 1756 root_gen, inode->i_ino, 0);
39279cc3
CM
1757 BUG_ON(ret);
1758 }
85e21bac
CM
1759next:
1760 if (path->slots[0] == 0) {
1761 if (pending_del_nr)
1762 goto del_pending;
1763 btrfs_release_path(root, path);
1764 goto search_again;
1765 }
1766
1767 path->slots[0]--;
1768 if (pending_del_nr &&
1769 path->slots[0] + 1 != pending_del_slot) {
1770 struct btrfs_key debug;
1771del_pending:
1772 btrfs_item_key_to_cpu(path->nodes[0], &debug,
1773 pending_del_slot);
1774 ret = btrfs_del_items(trans, root, path,
1775 pending_del_slot,
1776 pending_del_nr);
1777 BUG_ON(ret);
1778 pending_del_nr = 0;
1779 btrfs_release_path(root, path);
1780 goto search_again;
1781 }
39279cc3
CM
1782 }
1783 ret = 0;
1784error:
85e21bac
CM
1785 if (pending_del_nr) {
1786 ret = btrfs_del_items(trans, root, path, pending_del_slot,
1787 pending_del_nr);
1788 }
39279cc3
CM
1789 btrfs_free_path(path);
1790 inode->i_sb->s_dirt = 1;
1791 return ret;
1792}
1793
1794/*
1795 * taken from block_truncate_page, but does cow as it zeros out
1796 * any bytes left in the last page in the file.
1797 */
1798static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
1799{
1800 struct inode *inode = mapping->host;
db94535d 1801 struct btrfs_root *root = BTRFS_I(inode)->root;
e6dcd2dc
CM
1802 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1803 struct btrfs_ordered_extent *ordered;
1804 char *kaddr;
db94535d 1805 u32 blocksize = root->sectorsize;
39279cc3
CM
1806 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1807 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1808 struct page *page;
39279cc3 1809 int ret = 0;
a52d9a80 1810 u64 page_start;
e6dcd2dc 1811 u64 page_end;
39279cc3
CM
1812
1813 if ((offset & (blocksize - 1)) == 0)
1814 goto out;
1815
1816 ret = -ENOMEM;
211c17f5 1817again:
39279cc3
CM
1818 page = grab_cache_page(mapping, index);
1819 if (!page)
1820 goto out;
e6dcd2dc
CM
1821
1822 page_start = page_offset(page);
1823 page_end = page_start + PAGE_CACHE_SIZE - 1;
1824
39279cc3 1825 if (!PageUptodate(page)) {
9ebefb18 1826 ret = btrfs_readpage(NULL, page);
39279cc3 1827 lock_page(page);
211c17f5
CM
1828 if (page->mapping != mapping) {
1829 unlock_page(page);
1830 page_cache_release(page);
1831 goto again;
1832 }
39279cc3
CM
1833 if (!PageUptodate(page)) {
1834 ret = -EIO;
89642229 1835 goto out_unlock;
39279cc3
CM
1836 }
1837 }
211c17f5 1838 wait_on_page_writeback(page);
e6dcd2dc
CM
1839
1840 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
1841 set_page_extent_mapped(page);
1842
1843 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1844 if (ordered) {
1845 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
1846 unlock_page(page);
1847 page_cache_release(page);
eb84ae03 1848 btrfs_start_ordered_extent(inode, ordered, 1);
e6dcd2dc
CM
1849 btrfs_put_ordered_extent(ordered);
1850 goto again;
1851 }
1852
ea8c2819 1853 btrfs_set_extent_delalloc(inode, page_start, page_end);
e6dcd2dc
CM
1854 ret = 0;
1855 if (offset != PAGE_CACHE_SIZE) {
1856 kaddr = kmap(page);
1857 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1858 flush_dcache_page(page);
1859 kunmap(page);
1860 }
247e743c 1861 ClearPageChecked(page);
e6dcd2dc
CM
1862 set_page_dirty(page);
1863 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
39279cc3 1864
89642229 1865out_unlock:
39279cc3
CM
1866 unlock_page(page);
1867 page_cache_release(page);
1868out:
1869 return ret;
1870}
1871
1872static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1873{
1874 struct inode *inode = dentry->d_inode;
1875 int err;
1876
1877 err = inode_change_ok(inode, attr);
1878 if (err)
1879 return err;
1880
1881 if (S_ISREG(inode->i_mode) &&
1882 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1883 struct btrfs_trans_handle *trans;
1884 struct btrfs_root *root = BTRFS_I(inode)->root;
d1310b2e 1885 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2bf5a725 1886
5f39d397 1887 u64 mask = root->sectorsize - 1;
1b0f7c29 1888 u64 hole_start = (inode->i_size + mask) & ~mask;
f392a938 1889 u64 block_end = (attr->ia_size + mask) & ~mask;
39279cc3 1890 u64 hole_size;
179e29e4 1891 u64 alloc_hint = 0;
39279cc3 1892
1b0f7c29 1893 if (attr->ia_size <= hole_start)
39279cc3
CM
1894 goto out;
1895
1832a6d5 1896 err = btrfs_check_free_space(root, 1, 0);
1832a6d5
CM
1897 if (err)
1898 goto fail;
1899
39279cc3
CM
1900 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1901
5f56406a 1902 hole_size = block_end - hole_start;
7c2fe32a
CM
1903 while(1) {
1904 struct btrfs_ordered_extent *ordered;
1905 btrfs_wait_ordered_range(inode, hole_start, hole_size);
1906
1907 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1908 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
1909 if (ordered) {
1910 unlock_extent(io_tree, hole_start,
1911 block_end - 1, GFP_NOFS);
1912 btrfs_put_ordered_extent(ordered);
1913 } else {
1914 break;
1915 }
1916 }
39279cc3 1917
39279cc3
CM
1918 trans = btrfs_start_transaction(root, 1);
1919 btrfs_set_trans_block_group(trans, inode);
ee6e6504 1920 mutex_lock(&BTRFS_I(inode)->extent_mutex);
2bf5a725 1921 err = btrfs_drop_extents(trans, root, inode,
1b0f7c29 1922 hole_start, block_end, hole_start,
3326d1b0 1923 &alloc_hint);
2bf5a725 1924
179e29e4
CM
1925 if (alloc_hint != EXTENT_MAP_INLINE) {
1926 err = btrfs_insert_file_extent(trans, root,
1927 inode->i_ino,
5f56406a 1928 hole_start, 0, 0,
f2eb0a24 1929 hole_size, 0);
d1310b2e 1930 btrfs_drop_extent_cache(inode, hole_start,
5b21f2ed 1931 (u64)-1, 0);
5f56406a 1932 btrfs_check_file(root, inode);
179e29e4 1933 }
ee6e6504 1934 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
39279cc3 1935 btrfs_end_transaction(trans, root);
1b0f7c29 1936 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
54aa1f4d
CM
1937 if (err)
1938 return err;
39279cc3
CM
1939 }
1940out:
1941 err = inode_setattr(inode, attr);
33268eaf
JB
1942
1943 if (!err && ((attr->ia_valid & ATTR_MODE)))
1944 err = btrfs_acl_chmod(inode);
1832a6d5 1945fail:
39279cc3
CM
1946 return err;
1947}
61295eb8 1948
39279cc3
CM
1949void btrfs_delete_inode(struct inode *inode)
1950{
1951 struct btrfs_trans_handle *trans;
1952 struct btrfs_root *root = BTRFS_I(inode)->root;
d3c2fdcf 1953 unsigned long nr;
39279cc3
CM
1954 int ret;
1955
1956 truncate_inode_pages(&inode->i_data, 0);
1957 if (is_bad_inode(inode)) {
7b128766 1958 btrfs_orphan_del(NULL, inode);
39279cc3
CM
1959 goto no_delete;
1960 }
4a096752 1961 btrfs_wait_ordered_range(inode, 0, (u64)-1);
5f39d397 1962
dbe674a9 1963 btrfs_i_size_write(inode, 0);
39279cc3 1964 trans = btrfs_start_transaction(root, 1);
5f39d397 1965
39279cc3 1966 btrfs_set_trans_block_group(trans, inode);
e02119d5 1967 ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size, 0);
7b128766
JB
1968 if (ret) {
1969 btrfs_orphan_del(NULL, inode);
54aa1f4d 1970 goto no_delete_lock;
7b128766
JB
1971 }
1972
1973 btrfs_orphan_del(trans, inode);
85e21bac 1974
d3c2fdcf 1975 nr = trans->blocks_used;
85e21bac 1976 clear_inode(inode);
5f39d397 1977
39279cc3 1978 btrfs_end_transaction(trans, root);
d3c2fdcf 1979 btrfs_btree_balance_dirty(root, nr);
39279cc3 1980 return;
54aa1f4d
CM
1981
1982no_delete_lock:
d3c2fdcf 1983 nr = trans->blocks_used;
54aa1f4d 1984 btrfs_end_transaction(trans, root);
d3c2fdcf 1985 btrfs_btree_balance_dirty(root, nr);
39279cc3
CM
1986no_delete:
1987 clear_inode(inode);
1988}
1989
1990/*
1991 * this returns the key found in the dir entry in the location pointer.
1992 * If no dir entries were found, location->objectid is 0.
1993 */
1994static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1995 struct btrfs_key *location)
1996{
1997 const char *name = dentry->d_name.name;
1998 int namelen = dentry->d_name.len;
1999 struct btrfs_dir_item *di;
2000 struct btrfs_path *path;
2001 struct btrfs_root *root = BTRFS_I(dir)->root;
0d9f7f3e 2002 int ret = 0;
39279cc3
CM
2003
2004 path = btrfs_alloc_path();
2005 BUG_ON(!path);
3954401f 2006
39279cc3
CM
2007 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
2008 namelen, 0);
0d9f7f3e
Y
2009 if (IS_ERR(di))
2010 ret = PTR_ERR(di);
39279cc3 2011 if (!di || IS_ERR(di)) {
3954401f 2012 goto out_err;
39279cc3 2013 }
5f39d397 2014 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
39279cc3 2015out:
39279cc3
CM
2016 btrfs_free_path(path);
2017 return ret;
3954401f
CM
2018out_err:
2019 location->objectid = 0;
2020 goto out;
39279cc3
CM
2021}
2022
2023/*
2024 * when we hit a tree root in a directory, the btrfs part of the inode
2025 * needs to be changed to reflect the root directory of the tree root. This
2026 * is kind of like crossing a mount point.
2027 */
2028static int fixup_tree_root_location(struct btrfs_root *root,
2029 struct btrfs_key *location,
58176a96
JB
2030 struct btrfs_root **sub_root,
2031 struct dentry *dentry)
39279cc3 2032{
39279cc3
CM
2033 struct btrfs_root_item *ri;
2034
2035 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
2036 return 0;
2037 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
2038 return 0;
2039
58176a96
JB
2040 *sub_root = btrfs_read_fs_root(root->fs_info, location,
2041 dentry->d_name.name,
2042 dentry->d_name.len);
39279cc3
CM
2043 if (IS_ERR(*sub_root))
2044 return PTR_ERR(*sub_root);
2045
2046 ri = &(*sub_root)->root_item;
2047 location->objectid = btrfs_root_dirid(ri);
39279cc3
CM
2048 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
2049 location->offset = 0;
2050
39279cc3
CM
2051 return 0;
2052}
2053
e02119d5 2054static noinline void init_btrfs_i(struct inode *inode)
39279cc3 2055{
e02119d5
CM
2056 struct btrfs_inode *bi = BTRFS_I(inode);
2057
2058 bi->i_acl = NULL;
2059 bi->i_default_acl = NULL;
2060
2061 bi->generation = 0;
2062 bi->last_trans = 0;
2063 bi->logged_trans = 0;
2064 bi->delalloc_bytes = 0;
2065 bi->disk_i_size = 0;
2066 bi->flags = 0;
2067 bi->index_cnt = (u64)-1;
49eb7e46 2068 bi->log_dirty_trans = 0;
d1310b2e
CM
2069 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2070 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
b888db2b 2071 inode->i_mapping, GFP_NOFS);
7e38326f
CM
2072 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2073 inode->i_mapping, GFP_NOFS);
ea8c2819 2074 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
ba1da2f4 2075 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
1b1e2135 2076 mutex_init(&BTRFS_I(inode)->csum_mutex);
ee6e6504 2077 mutex_init(&BTRFS_I(inode)->extent_mutex);
e02119d5
CM
2078 mutex_init(&BTRFS_I(inode)->log_mutex);
2079}
2080
2081static int btrfs_init_locked_inode(struct inode *inode, void *p)
2082{
2083 struct btrfs_iget_args *args = p;
2084 inode->i_ino = args->ino;
2085 init_btrfs_i(inode);
2086 BTRFS_I(inode)->root = args->root;
39279cc3
CM
2087 return 0;
2088}
2089
2090static int btrfs_find_actor(struct inode *inode, void *opaque)
2091{
2092 struct btrfs_iget_args *args = opaque;
2093 return (args->ino == inode->i_ino &&
2094 args->root == BTRFS_I(inode)->root);
2095}
2096
5b21f2ed
ZY
2097struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
2098 struct btrfs_root *root, int wait)
2099{
2100 struct inode *inode;
2101 struct btrfs_iget_args args;
2102 args.ino = objectid;
2103 args.root = root;
2104
2105 if (wait) {
2106 inode = ilookup5(s, objectid, btrfs_find_actor,
2107 (void *)&args);
2108 } else {
2109 inode = ilookup5_nowait(s, objectid, btrfs_find_actor,
2110 (void *)&args);
2111 }
2112 return inode;
2113}
2114
39279cc3
CM
2115struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
2116 struct btrfs_root *root)
2117{
2118 struct inode *inode;
2119 struct btrfs_iget_args args;
2120 args.ino = objectid;
2121 args.root = root;
2122
2123 inode = iget5_locked(s, objectid, btrfs_find_actor,
2124 btrfs_init_locked_inode,
2125 (void *)&args);
2126 return inode;
2127}
2128
1a54ef8c
BR
2129/* Get an inode object given its location and corresponding root.
2130 * Returns in *is_new if the inode was read from disk
2131 */
2132struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
2133 struct btrfs_root *root, int *is_new)
2134{
2135 struct inode *inode;
2136
2137 inode = btrfs_iget_locked(s, location->objectid, root);
2138 if (!inode)
2139 return ERR_PTR(-EACCES);
2140
2141 if (inode->i_state & I_NEW) {
2142 BTRFS_I(inode)->root = root;
2143 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
2144 btrfs_read_locked_inode(inode);
2145 unlock_new_inode(inode);
2146 if (is_new)
2147 *is_new = 1;
2148 } else {
2149 if (is_new)
2150 *is_new = 0;
2151 }
2152
2153 return inode;
2154}
2155
39279cc3
CM
2156static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
2157 struct nameidata *nd)
2158{
2159 struct inode * inode;
2160 struct btrfs_inode *bi = BTRFS_I(dir);
2161 struct btrfs_root *root = bi->root;
2162 struct btrfs_root *sub_root = root;
2163 struct btrfs_key location;
1a54ef8c 2164 int ret, new, do_orphan = 0;
39279cc3
CM
2165
2166 if (dentry->d_name.len > BTRFS_NAME_LEN)
2167 return ERR_PTR(-ENAMETOOLONG);
5f39d397 2168
39279cc3 2169 ret = btrfs_inode_by_name(dir, dentry, &location);
5f39d397 2170
39279cc3
CM
2171 if (ret < 0)
2172 return ERR_PTR(ret);
5f39d397 2173
39279cc3
CM
2174 inode = NULL;
2175 if (location.objectid) {
58176a96
JB
2176 ret = fixup_tree_root_location(root, &location, &sub_root,
2177 dentry);
39279cc3
CM
2178 if (ret < 0)
2179 return ERR_PTR(ret);
2180 if (ret > 0)
2181 return ERR_PTR(-ENOENT);
1a54ef8c
BR
2182 inode = btrfs_iget(dir->i_sb, &location, sub_root, &new);
2183 if (IS_ERR(inode))
2184 return ERR_CAST(inode);
2185
2186 /* the inode and parent dir are two different roots */
2187 if (new && root != sub_root) {
2188 igrab(inode);
2189 sub_root->inode = inode;
2190 do_orphan = 1;
39279cc3
CM
2191 }
2192 }
7b128766
JB
2193
2194 if (unlikely(do_orphan))
2195 btrfs_orphan_cleanup(sub_root);
2196
39279cc3
CM
2197 return d_splice_alias(inode, dentry);
2198}
2199
39279cc3
CM
2200static unsigned char btrfs_filetype_table[] = {
2201 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
2202};
2203
cbdf5a24
DW
2204static int btrfs_real_readdir(struct file *filp, void *dirent,
2205 filldir_t filldir)
39279cc3 2206{
6da6abae 2207 struct inode *inode = filp->f_dentry->d_inode;
39279cc3
CM
2208 struct btrfs_root *root = BTRFS_I(inode)->root;
2209 struct btrfs_item *item;
2210 struct btrfs_dir_item *di;
2211 struct btrfs_key key;
5f39d397 2212 struct btrfs_key found_key;
39279cc3
CM
2213 struct btrfs_path *path;
2214 int ret;
2215 u32 nritems;
5f39d397 2216 struct extent_buffer *leaf;
39279cc3
CM
2217 int slot;
2218 int advance;
2219 unsigned char d_type;
2220 int over = 0;
2221 u32 di_cur;
2222 u32 di_total;
2223 u32 di_len;
2224 int key_type = BTRFS_DIR_INDEX_KEY;
5f39d397
CM
2225 char tmp_name[32];
2226 char *name_ptr;
2227 int name_len;
39279cc3
CM
2228
2229 /* FIXME, use a real flag for deciding about the key type */
2230 if (root->fs_info->tree_root == root)
2231 key_type = BTRFS_DIR_ITEM_KEY;
5f39d397 2232
3954401f
CM
2233 /* special case for "." */
2234 if (filp->f_pos == 0) {
2235 over = filldir(dirent, ".", 1,
2236 1, inode->i_ino,
2237 DT_DIR);
2238 if (over)
2239 return 0;
2240 filp->f_pos = 1;
2241 }
3954401f
CM
2242 /* special case for .., just use the back ref */
2243 if (filp->f_pos == 1) {
5ecc7e5d 2244 u64 pino = parent_ino(filp->f_path.dentry);
3954401f 2245 over = filldir(dirent, "..", 2,
5ecc7e5d 2246 2, pino, DT_DIR);
3954401f 2247 if (over)
49593bfa 2248 return 0;
3954401f
CM
2249 filp->f_pos = 2;
2250 }
2251
49593bfa
DW
2252 path = btrfs_alloc_path();
2253 path->reada = 2;
2254
39279cc3
CM
2255 btrfs_set_key_type(&key, key_type);
2256 key.offset = filp->f_pos;
49593bfa 2257 key.objectid = inode->i_ino;
5f39d397 2258
39279cc3
CM
2259 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2260 if (ret < 0)
2261 goto err;
2262 advance = 0;
49593bfa
DW
2263
2264 while (1) {
5f39d397
CM
2265 leaf = path->nodes[0];
2266 nritems = btrfs_header_nritems(leaf);
39279cc3
CM
2267 slot = path->slots[0];
2268 if (advance || slot >= nritems) {
49593bfa 2269 if (slot >= nritems - 1) {
39279cc3
CM
2270 ret = btrfs_next_leaf(root, path);
2271 if (ret)
2272 break;
5f39d397
CM
2273 leaf = path->nodes[0];
2274 nritems = btrfs_header_nritems(leaf);
39279cc3
CM
2275 slot = path->slots[0];
2276 } else {
2277 slot++;
2278 path->slots[0]++;
2279 }
2280 }
2281 advance = 1;
5f39d397
CM
2282 item = btrfs_item_nr(leaf, slot);
2283 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2284
2285 if (found_key.objectid != key.objectid)
39279cc3 2286 break;
5f39d397 2287 if (btrfs_key_type(&found_key) != key_type)
39279cc3 2288 break;
5f39d397 2289 if (found_key.offset < filp->f_pos)
39279cc3 2290 continue;
5f39d397
CM
2291
2292 filp->f_pos = found_key.offset;
49593bfa 2293
39279cc3
CM
2294 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
2295 di_cur = 0;
5f39d397 2296 di_total = btrfs_item_size(leaf, item);
49593bfa
DW
2297
2298 while (di_cur < di_total) {
5f39d397
CM
2299 struct btrfs_key location;
2300
2301 name_len = btrfs_dir_name_len(leaf, di);
49593bfa 2302 if (name_len <= sizeof(tmp_name)) {
5f39d397
CM
2303 name_ptr = tmp_name;
2304 } else {
2305 name_ptr = kmalloc(name_len, GFP_NOFS);
49593bfa
DW
2306 if (!name_ptr) {
2307 ret = -ENOMEM;
2308 goto err;
2309 }
5f39d397
CM
2310 }
2311 read_extent_buffer(leaf, name_ptr,
2312 (unsigned long)(di + 1), name_len);
2313
2314 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
2315 btrfs_dir_item_key_to_cpu(leaf, di, &location);
5f39d397 2316 over = filldir(dirent, name_ptr, name_len,
49593bfa 2317 found_key.offset, location.objectid,
39279cc3 2318 d_type);
5f39d397
CM
2319
2320 if (name_ptr != tmp_name)
2321 kfree(name_ptr);
2322
39279cc3
CM
2323 if (over)
2324 goto nopos;
49593bfa 2325
5103e947 2326 di_len = btrfs_dir_name_len(leaf, di) +
49593bfa 2327 btrfs_dir_data_len(leaf, di) + sizeof(*di);
39279cc3
CM
2328 di_cur += di_len;
2329 di = (struct btrfs_dir_item *)((char *)di + di_len);
2330 }
2331 }
49593bfa
DW
2332
2333 /* Reached end of directory/root. Bump pos past the last item. */
5e591a07
YZ
2334 if (key_type == BTRFS_DIR_INDEX_KEY)
2335 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
2336 else
2337 filp->f_pos++;
39279cc3
CM
2338nopos:
2339 ret = 0;
2340err:
39279cc3 2341 btrfs_free_path(path);
39279cc3
CM
2342 return ret;
2343}
2344
2345int btrfs_write_inode(struct inode *inode, int wait)
2346{
2347 struct btrfs_root *root = BTRFS_I(inode)->root;
2348 struct btrfs_trans_handle *trans;
2349 int ret = 0;
2350
4ca8b41e
CM
2351 if (root->fs_info->closing > 1)
2352 return 0;
2353
39279cc3 2354 if (wait) {
f9295749 2355 trans = btrfs_join_transaction(root, 1);
39279cc3
CM
2356 btrfs_set_trans_block_group(trans, inode);
2357 ret = btrfs_commit_transaction(trans, root);
39279cc3
CM
2358 }
2359 return ret;
2360}
2361
2362/*
54aa1f4d 2363 * This is somewhat expensive, updating the tree every time the
39279cc3
CM
2364 * inode changes. But, it is most likely to find the inode in cache.
2365 * FIXME, needs more benchmarking...there are no reasons other than performance
2366 * to keep or drop this code.
2367 */
2368void btrfs_dirty_inode(struct inode *inode)
2369{
2370 struct btrfs_root *root = BTRFS_I(inode)->root;
2371 struct btrfs_trans_handle *trans;
2372
f9295749 2373 trans = btrfs_join_transaction(root, 1);
39279cc3
CM
2374 btrfs_set_trans_block_group(trans, inode);
2375 btrfs_update_inode(trans, root, inode);
2376 btrfs_end_transaction(trans, root);
39279cc3
CM
2377}
2378
d352ac68
CM
2379/*
2380 * find the highest existing sequence number in a directory
2381 * and then set the in-memory index_cnt variable to reflect
2382 * free sequence numbers
2383 */
aec7477b
JB
2384static int btrfs_set_inode_index_count(struct inode *inode)
2385{
2386 struct btrfs_root *root = BTRFS_I(inode)->root;
2387 struct btrfs_key key, found_key;
2388 struct btrfs_path *path;
2389 struct extent_buffer *leaf;
2390 int ret;
2391
2392 key.objectid = inode->i_ino;
2393 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
2394 key.offset = (u64)-1;
2395
2396 path = btrfs_alloc_path();
2397 if (!path)
2398 return -ENOMEM;
2399
2400 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2401 if (ret < 0)
2402 goto out;
2403 /* FIXME: we should be able to handle this */
2404 if (ret == 0)
2405 goto out;
2406 ret = 0;
2407
2408 /*
2409 * MAGIC NUMBER EXPLANATION:
2410 * since we search a directory based on f_pos we have to start at 2
2411 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
2412 * else has to start at 2
2413 */
2414 if (path->slots[0] == 0) {
2415 BTRFS_I(inode)->index_cnt = 2;
2416 goto out;
2417 }
2418
2419 path->slots[0]--;
2420
2421 leaf = path->nodes[0];
2422 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2423
2424 if (found_key.objectid != inode->i_ino ||
2425 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
2426 BTRFS_I(inode)->index_cnt = 2;
2427 goto out;
2428 }
2429
2430 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
2431out:
2432 btrfs_free_path(path);
2433 return ret;
2434}
2435
d352ac68
CM
2436/*
2437 * helper to find a free sequence number in a given directory. This current
2438 * code is very simple, later versions will do smarter things in the btree
2439 */
00e4e6b3
CM
2440static int btrfs_set_inode_index(struct inode *dir, struct inode *inode,
2441 u64 *index)
aec7477b
JB
2442{
2443 int ret = 0;
2444
2445 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
2446 ret = btrfs_set_inode_index_count(dir);
8d5bf1cb 2447 if (ret) {
aec7477b 2448 return ret;
8d5bf1cb 2449 }
aec7477b
JB
2450 }
2451
00e4e6b3 2452 *index = BTRFS_I(dir)->index_cnt;
aec7477b
JB
2453 BTRFS_I(dir)->index_cnt++;
2454
2455 return ret;
2456}
2457
39279cc3
CM
2458static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
2459 struct btrfs_root *root,
aec7477b 2460 struct inode *dir,
9c58309d
CM
2461 const char *name, int name_len,
2462 u64 ref_objectid,
39279cc3
CM
2463 u64 objectid,
2464 struct btrfs_block_group_cache *group,
00e4e6b3 2465 int mode, u64 *index)
39279cc3
CM
2466{
2467 struct inode *inode;
5f39d397 2468 struct btrfs_inode_item *inode_item;
6324fbf3 2469 struct btrfs_block_group_cache *new_inode_group;
39279cc3 2470 struct btrfs_key *location;
5f39d397 2471 struct btrfs_path *path;
9c58309d
CM
2472 struct btrfs_inode_ref *ref;
2473 struct btrfs_key key[2];
2474 u32 sizes[2];
2475 unsigned long ptr;
39279cc3
CM
2476 int ret;
2477 int owner;
2478
5f39d397
CM
2479 path = btrfs_alloc_path();
2480 BUG_ON(!path);
2481
39279cc3
CM
2482 inode = new_inode(root->fs_info->sb);
2483 if (!inode)
2484 return ERR_PTR(-ENOMEM);
2485
aec7477b 2486 if (dir) {
00e4e6b3 2487 ret = btrfs_set_inode_index(dir, inode, index);
aec7477b
JB
2488 if (ret)
2489 return ERR_PTR(ret);
aec7477b
JB
2490 }
2491 /*
2492 * index_cnt is ignored for everything but a dir,
2493 * btrfs_get_inode_index_count has an explanation for the magic
2494 * number
2495 */
e02119d5 2496 init_btrfs_i(inode);
aec7477b 2497 BTRFS_I(inode)->index_cnt = 2;
39279cc3 2498 BTRFS_I(inode)->root = root;
e02119d5 2499 BTRFS_I(inode)->generation = trans->transid;
b888db2b 2500
39279cc3
CM
2501 if (mode & S_IFDIR)
2502 owner = 0;
2503 else
2504 owner = 1;
6324fbf3 2505 new_inode_group = btrfs_find_block_group(root, group, 0,
0b86a832 2506 BTRFS_BLOCK_GROUP_METADATA, owner);
6324fbf3
CM
2507 if (!new_inode_group) {
2508 printk("find_block group failed\n");
2509 new_inode_group = group;
2510 }
2511 BTRFS_I(inode)->block_group = new_inode_group;
9c58309d
CM
2512
2513 key[0].objectid = objectid;
2514 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
2515 key[0].offset = 0;
2516
2517 key[1].objectid = objectid;
2518 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
2519 key[1].offset = ref_objectid;
2520
2521 sizes[0] = sizeof(struct btrfs_inode_item);
2522 sizes[1] = name_len + sizeof(*ref);
2523
2524 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
2525 if (ret != 0)
5f39d397
CM
2526 goto fail;
2527
9c58309d
CM
2528 if (objectid > root->highest_inode)
2529 root->highest_inode = objectid;
2530
39279cc3
CM
2531 inode->i_uid = current->fsuid;
2532 inode->i_gid = current->fsgid;
2533 inode->i_mode = mode;
2534 inode->i_ino = objectid;
a76a3cd4 2535 inode_set_bytes(inode, 0);
39279cc3 2536 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
5f39d397
CM
2537 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2538 struct btrfs_inode_item);
e02119d5 2539 fill_inode_item(trans, path->nodes[0], inode_item, inode);
9c58309d
CM
2540
2541 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
2542 struct btrfs_inode_ref);
2543 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
00e4e6b3 2544 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
9c58309d
CM
2545 ptr = (unsigned long)(ref + 1);
2546 write_extent_buffer(path->nodes[0], name, ptr, name_len);
2547
5f39d397
CM
2548 btrfs_mark_buffer_dirty(path->nodes[0]);
2549 btrfs_free_path(path);
2550
39279cc3
CM
2551 location = &BTRFS_I(inode)->location;
2552 location->objectid = objectid;
39279cc3
CM
2553 location->offset = 0;
2554 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
2555
39279cc3
CM
2556 insert_inode_hash(inode);
2557 return inode;
5f39d397 2558fail:
aec7477b
JB
2559 if (dir)
2560 BTRFS_I(dir)->index_cnt--;
5f39d397
CM
2561 btrfs_free_path(path);
2562 return ERR_PTR(ret);
39279cc3
CM
2563}
2564
2565static inline u8 btrfs_inode_type(struct inode *inode)
2566{
2567 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
2568}
2569
d352ac68
CM
2570/*
2571 * utility function to add 'inode' into 'parent_inode' with
2572 * a give name and a given sequence number.
2573 * if 'add_backref' is true, also insert a backref from the
2574 * inode to the parent directory.
2575 */
e02119d5
CM
2576int btrfs_add_link(struct btrfs_trans_handle *trans,
2577 struct inode *parent_inode, struct inode *inode,
2578 const char *name, int name_len, int add_backref, u64 index)
39279cc3
CM
2579{
2580 int ret;
2581 struct btrfs_key key;
e02119d5 2582 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
5f39d397 2583
39279cc3 2584 key.objectid = inode->i_ino;
39279cc3
CM
2585 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
2586 key.offset = 0;
2587
e02119d5
CM
2588 ret = btrfs_insert_dir_item(trans, root, name, name_len,
2589 parent_inode->i_ino,
aec7477b 2590 &key, btrfs_inode_type(inode),
00e4e6b3 2591 index);
39279cc3 2592 if (ret == 0) {
9c58309d
CM
2593 if (add_backref) {
2594 ret = btrfs_insert_inode_ref(trans, root,
e02119d5
CM
2595 name, name_len,
2596 inode->i_ino,
2597 parent_inode->i_ino,
2598 index);
9c58309d 2599 }
dbe674a9 2600 btrfs_i_size_write(parent_inode, parent_inode->i_size +
e02119d5 2601 name_len * 2);
79c44584 2602 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
e02119d5 2603 ret = btrfs_update_inode(trans, root, parent_inode);
39279cc3
CM
2604 }
2605 return ret;
2606}
2607
2608static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
9c58309d 2609 struct dentry *dentry, struct inode *inode,
00e4e6b3 2610 int backref, u64 index)
39279cc3 2611{
e02119d5
CM
2612 int err = btrfs_add_link(trans, dentry->d_parent->d_inode,
2613 inode, dentry->d_name.name,
2614 dentry->d_name.len, backref, index);
39279cc3
CM
2615 if (!err) {
2616 d_instantiate(dentry, inode);
2617 return 0;
2618 }
2619 if (err > 0)
2620 err = -EEXIST;
2621 return err;
2622}
2623
618e21d5
JB
2624static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
2625 int mode, dev_t rdev)
2626{
2627 struct btrfs_trans_handle *trans;
2628 struct btrfs_root *root = BTRFS_I(dir)->root;
1832a6d5 2629 struct inode *inode = NULL;
618e21d5
JB
2630 int err;
2631 int drop_inode = 0;
2632 u64 objectid;
1832a6d5 2633 unsigned long nr = 0;
00e4e6b3 2634 u64 index = 0;
618e21d5
JB
2635
2636 if (!new_valid_dev(rdev))
2637 return -EINVAL;
2638
1832a6d5
CM
2639 err = btrfs_check_free_space(root, 1, 0);
2640 if (err)
2641 goto fail;
2642
618e21d5
JB
2643 trans = btrfs_start_transaction(root, 1);
2644 btrfs_set_trans_block_group(trans, dir);
2645
2646 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2647 if (err) {
2648 err = -ENOSPC;
2649 goto out_unlock;
2650 }
2651
aec7477b 2652 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
9c58309d
CM
2653 dentry->d_name.len,
2654 dentry->d_parent->d_inode->i_ino, objectid,
00e4e6b3 2655 BTRFS_I(dir)->block_group, mode, &index);
618e21d5
JB
2656 err = PTR_ERR(inode);
2657 if (IS_ERR(inode))
2658 goto out_unlock;
2659
33268eaf
JB
2660 err = btrfs_init_acl(inode, dir);
2661 if (err) {
2662 drop_inode = 1;
2663 goto out_unlock;
2664 }
2665
618e21d5 2666 btrfs_set_trans_block_group(trans, inode);
00e4e6b3 2667 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
618e21d5
JB
2668 if (err)
2669 drop_inode = 1;
2670 else {
2671 inode->i_op = &btrfs_special_inode_operations;
2672 init_special_inode(inode, inode->i_mode, rdev);
1b4ab1bb 2673 btrfs_update_inode(trans, root, inode);
618e21d5
JB
2674 }
2675 dir->i_sb->s_dirt = 1;
2676 btrfs_update_inode_block_group(trans, inode);
2677 btrfs_update_inode_block_group(trans, dir);
2678out_unlock:
d3c2fdcf 2679 nr = trans->blocks_used;
89ce8a63 2680 btrfs_end_transaction_throttle(trans, root);
1832a6d5 2681fail:
618e21d5
JB
2682 if (drop_inode) {
2683 inode_dec_link_count(inode);
2684 iput(inode);
2685 }
d3c2fdcf 2686 btrfs_btree_balance_dirty(root, nr);
618e21d5
JB
2687 return err;
2688}
2689
39279cc3
CM
2690static int btrfs_create(struct inode *dir, struct dentry *dentry,
2691 int mode, struct nameidata *nd)
2692{
2693 struct btrfs_trans_handle *trans;
2694 struct btrfs_root *root = BTRFS_I(dir)->root;
1832a6d5 2695 struct inode *inode = NULL;
39279cc3
CM
2696 int err;
2697 int drop_inode = 0;
1832a6d5 2698 unsigned long nr = 0;
39279cc3 2699 u64 objectid;
00e4e6b3 2700 u64 index = 0;
39279cc3 2701
1832a6d5
CM
2702 err = btrfs_check_free_space(root, 1, 0);
2703 if (err)
2704 goto fail;
39279cc3
CM
2705 trans = btrfs_start_transaction(root, 1);
2706 btrfs_set_trans_block_group(trans, dir);
2707
2708 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2709 if (err) {
2710 err = -ENOSPC;
2711 goto out_unlock;
2712 }
2713
aec7477b 2714 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
9c58309d
CM
2715 dentry->d_name.len,
2716 dentry->d_parent->d_inode->i_ino,
00e4e6b3
CM
2717 objectid, BTRFS_I(dir)->block_group, mode,
2718 &index);
39279cc3
CM
2719 err = PTR_ERR(inode);
2720 if (IS_ERR(inode))
2721 goto out_unlock;
2722
33268eaf
JB
2723 err = btrfs_init_acl(inode, dir);
2724 if (err) {
2725 drop_inode = 1;
2726 goto out_unlock;
2727 }
2728
39279cc3 2729 btrfs_set_trans_block_group(trans, inode);
00e4e6b3 2730 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
39279cc3
CM
2731 if (err)
2732 drop_inode = 1;
2733 else {
2734 inode->i_mapping->a_ops = &btrfs_aops;
04160088 2735 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
39279cc3
CM
2736 inode->i_fop = &btrfs_file_operations;
2737 inode->i_op = &btrfs_file_inode_operations;
d1310b2e 2738 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
39279cc3
CM
2739 }
2740 dir->i_sb->s_dirt = 1;
2741 btrfs_update_inode_block_group(trans, inode);
2742 btrfs_update_inode_block_group(trans, dir);
2743out_unlock:
d3c2fdcf 2744 nr = trans->blocks_used;
ab78c84d 2745 btrfs_end_transaction_throttle(trans, root);
1832a6d5 2746fail:
39279cc3
CM
2747 if (drop_inode) {
2748 inode_dec_link_count(inode);
2749 iput(inode);
2750 }
d3c2fdcf 2751 btrfs_btree_balance_dirty(root, nr);
39279cc3
CM
2752 return err;
2753}
2754
2755static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
2756 struct dentry *dentry)
2757{
2758 struct btrfs_trans_handle *trans;
2759 struct btrfs_root *root = BTRFS_I(dir)->root;
2760 struct inode *inode = old_dentry->d_inode;
00e4e6b3 2761 u64 index;
1832a6d5 2762 unsigned long nr = 0;
39279cc3
CM
2763 int err;
2764 int drop_inode = 0;
2765
2766 if (inode->i_nlink == 0)
2767 return -ENOENT;
2768
e02119d5 2769 btrfs_inc_nlink(inode);
1832a6d5
CM
2770 err = btrfs_check_free_space(root, 1, 0);
2771 if (err)
2772 goto fail;
00e4e6b3 2773 err = btrfs_set_inode_index(dir, inode, &index);
aec7477b
JB
2774 if (err)
2775 goto fail;
2776
39279cc3 2777 trans = btrfs_start_transaction(root, 1);
5f39d397 2778
39279cc3
CM
2779 btrfs_set_trans_block_group(trans, dir);
2780 atomic_inc(&inode->i_count);
aec7477b 2781
00e4e6b3 2782 err = btrfs_add_nondir(trans, dentry, inode, 1, index);
5f39d397 2783
39279cc3
CM
2784 if (err)
2785 drop_inode = 1;
5f39d397 2786
39279cc3
CM
2787 dir->i_sb->s_dirt = 1;
2788 btrfs_update_inode_block_group(trans, dir);
54aa1f4d 2789 err = btrfs_update_inode(trans, root, inode);
5f39d397 2790
54aa1f4d
CM
2791 if (err)
2792 drop_inode = 1;
39279cc3 2793
d3c2fdcf 2794 nr = trans->blocks_used;
ab78c84d 2795 btrfs_end_transaction_throttle(trans, root);
1832a6d5 2796fail:
39279cc3
CM
2797 if (drop_inode) {
2798 inode_dec_link_count(inode);
2799 iput(inode);
2800 }
d3c2fdcf 2801 btrfs_btree_balance_dirty(root, nr);
39279cc3
CM
2802 return err;
2803}
2804
39279cc3
CM
2805static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2806{
b9d86667 2807 struct inode *inode = NULL;
39279cc3
CM
2808 struct btrfs_trans_handle *trans;
2809 struct btrfs_root *root = BTRFS_I(dir)->root;
2810 int err = 0;
2811 int drop_on_err = 0;
b9d86667 2812 u64 objectid = 0;
00e4e6b3 2813 u64 index = 0;
d3c2fdcf 2814 unsigned long nr = 1;
39279cc3 2815
1832a6d5
CM
2816 err = btrfs_check_free_space(root, 1, 0);
2817 if (err)
2818 goto out_unlock;
2819
39279cc3
CM
2820 trans = btrfs_start_transaction(root, 1);
2821 btrfs_set_trans_block_group(trans, dir);
5f39d397 2822
39279cc3
CM
2823 if (IS_ERR(trans)) {
2824 err = PTR_ERR(trans);
2825 goto out_unlock;
2826 }
2827
2828 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2829 if (err) {
2830 err = -ENOSPC;
2831 goto out_unlock;
2832 }
2833
aec7477b 2834 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
9c58309d
CM
2835 dentry->d_name.len,
2836 dentry->d_parent->d_inode->i_ino, objectid,
00e4e6b3
CM
2837 BTRFS_I(dir)->block_group, S_IFDIR | mode,
2838 &index);
39279cc3
CM
2839 if (IS_ERR(inode)) {
2840 err = PTR_ERR(inode);
2841 goto out_fail;
2842 }
5f39d397 2843
39279cc3 2844 drop_on_err = 1;
33268eaf
JB
2845
2846 err = btrfs_init_acl(inode, dir);
2847 if (err)
2848 goto out_fail;
2849
39279cc3
CM
2850 inode->i_op = &btrfs_dir_inode_operations;
2851 inode->i_fop = &btrfs_dir_file_operations;
2852 btrfs_set_trans_block_group(trans, inode);
2853
dbe674a9 2854 btrfs_i_size_write(inode, 0);
39279cc3
CM
2855 err = btrfs_update_inode(trans, root, inode);
2856 if (err)
2857 goto out_fail;
5f39d397 2858
e02119d5
CM
2859 err = btrfs_add_link(trans, dentry->d_parent->d_inode,
2860 inode, dentry->d_name.name,
2861 dentry->d_name.len, 0, index);
39279cc3
CM
2862 if (err)
2863 goto out_fail;
5f39d397 2864
39279cc3
CM
2865 d_instantiate(dentry, inode);
2866 drop_on_err = 0;
2867 dir->i_sb->s_dirt = 1;
2868 btrfs_update_inode_block_group(trans, inode);
2869 btrfs_update_inode_block_group(trans, dir);
2870
2871out_fail:
d3c2fdcf 2872 nr = trans->blocks_used;
ab78c84d 2873 btrfs_end_transaction_throttle(trans, root);
5f39d397 2874
39279cc3 2875out_unlock:
39279cc3
CM
2876 if (drop_on_err)
2877 iput(inode);
d3c2fdcf 2878 btrfs_btree_balance_dirty(root, nr);
39279cc3
CM
2879 return err;
2880}
2881
d352ac68
CM
2882/* helper for btfs_get_extent. Given an existing extent in the tree,
2883 * and an extent that you want to insert, deal with overlap and insert
2884 * the new extent into the tree.
2885 */
3b951516
CM
2886static int merge_extent_mapping(struct extent_map_tree *em_tree,
2887 struct extent_map *existing,
e6dcd2dc
CM
2888 struct extent_map *em,
2889 u64 map_start, u64 map_len)
3b951516
CM
2890{
2891 u64 start_diff;
3b951516 2892
e6dcd2dc
CM
2893 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
2894 start_diff = map_start - em->start;
2895 em->start = map_start;
2896 em->len = map_len;
2897 if (em->block_start < EXTENT_MAP_LAST_BYTE)
2898 em->block_start += start_diff;
2899 return add_extent_mapping(em_tree, em);
3b951516
CM
2900}
2901
d352ac68
CM
2902/*
2903 * a bit scary, this does extent mapping from logical file offset to the disk.
2904 * the ugly parts come from merging extents from the disk with the
2905 * in-ram representation. This gets more complex because of the data=ordered code,
2906 * where the in-ram extents might be locked pending data=ordered completion.
2907 *
2908 * This also copies inline extents directly into the page.
2909 */
a52d9a80 2910struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
70dec807 2911 size_t pg_offset, u64 start, u64 len,
a52d9a80
CM
2912 int create)
2913{
2914 int ret;
2915 int err = 0;
db94535d 2916 u64 bytenr;
a52d9a80
CM
2917 u64 extent_start = 0;
2918 u64 extent_end = 0;
2919 u64 objectid = inode->i_ino;
2920 u32 found_type;
f421950f 2921 struct btrfs_path *path = NULL;
a52d9a80
CM
2922 struct btrfs_root *root = BTRFS_I(inode)->root;
2923 struct btrfs_file_extent_item *item;
5f39d397
CM
2924 struct extent_buffer *leaf;
2925 struct btrfs_key found_key;
a52d9a80
CM
2926 struct extent_map *em = NULL;
2927 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
d1310b2e 2928 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
a52d9a80
CM
2929 struct btrfs_trans_handle *trans = NULL;
2930
a52d9a80 2931again:
d1310b2e
CM
2932 spin_lock(&em_tree->lock);
2933 em = lookup_extent_mapping(em_tree, start, len);
a061fc8d
CM
2934 if (em)
2935 em->bdev = root->fs_info->fs_devices->latest_bdev;
d1310b2e
CM
2936 spin_unlock(&em_tree->lock);
2937
a52d9a80 2938 if (em) {
e1c4b745
CM
2939 if (em->start > start || em->start + em->len <= start)
2940 free_extent_map(em);
2941 else if (em->block_start == EXTENT_MAP_INLINE && page)
70dec807
CM
2942 free_extent_map(em);
2943 else
2944 goto out;
a52d9a80 2945 }
d1310b2e 2946 em = alloc_extent_map(GFP_NOFS);
a52d9a80 2947 if (!em) {
d1310b2e
CM
2948 err = -ENOMEM;
2949 goto out;
a52d9a80 2950 }
e6dcd2dc 2951 em->bdev = root->fs_info->fs_devices->latest_bdev;
d1310b2e
CM
2952 em->start = EXTENT_MAP_HOLE;
2953 em->len = (u64)-1;
f421950f
CM
2954
2955 if (!path) {
2956 path = btrfs_alloc_path();
2957 BUG_ON(!path);
2958 }
2959
179e29e4
CM
2960 ret = btrfs_lookup_file_extent(trans, root, path,
2961 objectid, start, trans != NULL);
a52d9a80
CM
2962 if (ret < 0) {
2963 err = ret;
2964 goto out;
2965 }
2966
2967 if (ret != 0) {
2968 if (path->slots[0] == 0)
2969 goto not_found;
2970 path->slots[0]--;
2971 }
2972
5f39d397
CM
2973 leaf = path->nodes[0];
2974 item = btrfs_item_ptr(leaf, path->slots[0],
a52d9a80 2975 struct btrfs_file_extent_item);
a52d9a80 2976 /* are we inside the extent that was found? */
5f39d397
CM
2977 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2978 found_type = btrfs_key_type(&found_key);
2979 if (found_key.objectid != objectid ||
a52d9a80
CM
2980 found_type != BTRFS_EXTENT_DATA_KEY) {
2981 goto not_found;
2982 }
2983
5f39d397
CM
2984 found_type = btrfs_file_extent_type(leaf, item);
2985 extent_start = found_key.offset;
a52d9a80
CM
2986 if (found_type == BTRFS_FILE_EXTENT_REG) {
2987 extent_end = extent_start +
db94535d 2988 btrfs_file_extent_num_bytes(leaf, item);
a52d9a80 2989 err = 0;
b888db2b 2990 if (start < extent_start || start >= extent_end) {
a52d9a80
CM
2991 em->start = start;
2992 if (start < extent_start) {
d1310b2e 2993 if (start + len <= extent_start)
b888db2b 2994 goto not_found;
d1310b2e 2995 em->len = extent_end - extent_start;
a52d9a80 2996 } else {
d1310b2e 2997 em->len = len;
a52d9a80
CM
2998 }
2999 goto not_found_em;
3000 }
db94535d
CM
3001 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
3002 if (bytenr == 0) {
a52d9a80 3003 em->start = extent_start;
d1310b2e 3004 em->len = extent_end - extent_start;
5f39d397 3005 em->block_start = EXTENT_MAP_HOLE;
a52d9a80
CM
3006 goto insert;
3007 }
db94535d
CM
3008 bytenr += btrfs_file_extent_offset(leaf, item);
3009 em->block_start = bytenr;
a52d9a80 3010 em->start = extent_start;
d1310b2e 3011 em->len = extent_end - extent_start;
a52d9a80
CM
3012 goto insert;
3013 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
70dec807 3014 u64 page_start;
5f39d397 3015 unsigned long ptr;
a52d9a80 3016 char *map;
3326d1b0
CM
3017 size_t size;
3018 size_t extent_offset;
3019 size_t copy_size;
a52d9a80 3020
5f39d397
CM
3021 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
3022 path->slots[0]));
d1310b2e
CM
3023 extent_end = (extent_start + size + root->sectorsize - 1) &
3024 ~((u64)root->sectorsize - 1);
b888db2b 3025 if (start < extent_start || start >= extent_end) {
a52d9a80
CM
3026 em->start = start;
3027 if (start < extent_start) {
d1310b2e 3028 if (start + len <= extent_start)
b888db2b 3029 goto not_found;
d1310b2e 3030 em->len = extent_end - extent_start;
a52d9a80 3031 } else {
d1310b2e 3032 em->len = len;
a52d9a80
CM
3033 }
3034 goto not_found_em;
3035 }
689f9346 3036 em->block_start = EXTENT_MAP_INLINE;
689f9346
Y
3037
3038 if (!page) {
3039 em->start = extent_start;
d1310b2e 3040 em->len = size;
689f9346
Y
3041 goto out;
3042 }
5f39d397 3043
70dec807
CM
3044 page_start = page_offset(page) + pg_offset;
3045 extent_offset = page_start - extent_start;
3046 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
3326d1b0 3047 size - extent_offset);
3326d1b0 3048 em->start = extent_start + extent_offset;
70dec807
CM
3049 em->len = (copy_size + root->sectorsize - 1) &
3050 ~((u64)root->sectorsize - 1);
689f9346
Y
3051 map = kmap(page);
3052 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
179e29e4 3053 if (create == 0 && !PageUptodate(page)) {
70dec807 3054 read_extent_buffer(leaf, map + pg_offset, ptr,
179e29e4
CM
3055 copy_size);
3056 flush_dcache_page(page);
3057 } else if (create && PageUptodate(page)) {
3058 if (!trans) {
3059 kunmap(page);
3060 free_extent_map(em);
3061 em = NULL;
3062 btrfs_release_path(root, path);
f9295749 3063 trans = btrfs_join_transaction(root, 1);
179e29e4
CM
3064 goto again;
3065 }
70dec807 3066 write_extent_buffer(leaf, map + pg_offset, ptr,
179e29e4
CM
3067 copy_size);
3068 btrfs_mark_buffer_dirty(leaf);
a52d9a80 3069 }
a52d9a80 3070 kunmap(page);
d1310b2e
CM
3071 set_extent_uptodate(io_tree, em->start,
3072 extent_map_end(em) - 1, GFP_NOFS);
a52d9a80
CM
3073 goto insert;
3074 } else {
3075 printk("unkknown found_type %d\n", found_type);
3076 WARN_ON(1);
3077 }
3078not_found:
3079 em->start = start;
d1310b2e 3080 em->len = len;
a52d9a80 3081not_found_em:
5f39d397 3082 em->block_start = EXTENT_MAP_HOLE;
a52d9a80
CM
3083insert:
3084 btrfs_release_path(root, path);
d1310b2e
CM
3085 if (em->start > start || extent_map_end(em) <= start) {
3086 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
a52d9a80
CM
3087 err = -EIO;
3088 goto out;
3089 }
d1310b2e
CM
3090
3091 err = 0;
3092 spin_lock(&em_tree->lock);
a52d9a80 3093 ret = add_extent_mapping(em_tree, em);
3b951516
CM
3094 /* it is possible that someone inserted the extent into the tree
3095 * while we had the lock dropped. It is also possible that
3096 * an overlapping map exists in the tree
3097 */
a52d9a80 3098 if (ret == -EEXIST) {
3b951516 3099 struct extent_map *existing;
e6dcd2dc
CM
3100
3101 ret = 0;
3102
3b951516 3103 existing = lookup_extent_mapping(em_tree, start, len);
e1c4b745
CM
3104 if (existing && (existing->start > start ||
3105 existing->start + existing->len <= start)) {
3106 free_extent_map(existing);
3107 existing = NULL;
3108 }
3b951516
CM
3109 if (!existing) {
3110 existing = lookup_extent_mapping(em_tree, em->start,
3111 em->len);
3112 if (existing) {
3113 err = merge_extent_mapping(em_tree, existing,
e6dcd2dc
CM
3114 em, start,
3115 root->sectorsize);
3b951516
CM
3116 free_extent_map(existing);
3117 if (err) {
3118 free_extent_map(em);
3119 em = NULL;
3120 }
3121 } else {
3122 err = -EIO;
3123 printk("failing to insert %Lu %Lu\n",
3124 start, len);
3125 free_extent_map(em);
3126 em = NULL;
3127 }
3128 } else {
3129 free_extent_map(em);
3130 em = existing;
e6dcd2dc 3131 err = 0;
a52d9a80 3132 }
a52d9a80 3133 }
d1310b2e 3134 spin_unlock(&em_tree->lock);
a52d9a80 3135out:
f421950f
CM
3136 if (path)
3137 btrfs_free_path(path);
a52d9a80
CM
3138 if (trans) {
3139 ret = btrfs_end_transaction(trans, root);
e6dcd2dc 3140 if (!err) {
a52d9a80 3141 err = ret;
e6dcd2dc 3142 }
a52d9a80 3143 }
a52d9a80
CM
3144 if (err) {
3145 free_extent_map(em);
3146 WARN_ON(1);
3147 return ERR_PTR(err);
3148 }
3149 return em;
3150}
3151
16432985
CM
3152static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
3153 const struct iovec *iov, loff_t offset,
3154 unsigned long nr_segs)
3155{
e1c4b745 3156 return -EINVAL;
16432985
CM
3157}
3158
d396c6f5 3159static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
39279cc3 3160{
d396c6f5 3161 return extent_bmap(mapping, iblock, btrfs_get_extent);
39279cc3
CM
3162}
3163
a52d9a80 3164int btrfs_readpage(struct file *file, struct page *page)
9ebefb18 3165{
d1310b2e
CM
3166 struct extent_io_tree *tree;
3167 tree = &BTRFS_I(page->mapping->host)->io_tree;
a52d9a80 3168 return extent_read_full_page(tree, page, btrfs_get_extent);
9ebefb18 3169}
1832a6d5 3170
a52d9a80 3171static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
39279cc3 3172{
d1310b2e 3173 struct extent_io_tree *tree;
b888db2b
CM
3174
3175
3176 if (current->flags & PF_MEMALLOC) {
3177 redirty_page_for_writepage(wbc, page);
3178 unlock_page(page);
3179 return 0;
3180 }
d1310b2e 3181 tree = &BTRFS_I(page->mapping->host)->io_tree;
a52d9a80 3182 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
9ebefb18
CM
3183}
3184
f421950f
CM
3185int btrfs_writepages(struct address_space *mapping,
3186 struct writeback_control *wbc)
b293f02e 3187{
d1310b2e
CM
3188 struct extent_io_tree *tree;
3189 tree = &BTRFS_I(mapping->host)->io_tree;
b293f02e
CM
3190 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
3191}
3192
3ab2fb5a
CM
3193static int
3194btrfs_readpages(struct file *file, struct address_space *mapping,
3195 struct list_head *pages, unsigned nr_pages)
3196{
d1310b2e
CM
3197 struct extent_io_tree *tree;
3198 tree = &BTRFS_I(mapping->host)->io_tree;
3ab2fb5a
CM
3199 return extent_readpages(tree, mapping, pages, nr_pages,
3200 btrfs_get_extent);
3201}
e6dcd2dc 3202static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
9ebefb18 3203{
d1310b2e
CM
3204 struct extent_io_tree *tree;
3205 struct extent_map_tree *map;
a52d9a80 3206 int ret;
8c2383c3 3207
d1310b2e
CM
3208 tree = &BTRFS_I(page->mapping->host)->io_tree;
3209 map = &BTRFS_I(page->mapping->host)->extent_tree;
70dec807 3210 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
a52d9a80
CM
3211 if (ret == 1) {
3212 ClearPagePrivate(page);
3213 set_page_private(page, 0);
3214 page_cache_release(page);
39279cc3 3215 }
a52d9a80 3216 return ret;
39279cc3
CM
3217}
3218
e6dcd2dc
CM
3219static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
3220{
98509cfc
CM
3221 if (PageWriteback(page) || PageDirty(page))
3222 return 0;
e6dcd2dc
CM
3223 return __btrfs_releasepage(page, gfp_flags);
3224}
3225
a52d9a80 3226static void btrfs_invalidatepage(struct page *page, unsigned long offset)
39279cc3 3227{
d1310b2e 3228 struct extent_io_tree *tree;
e6dcd2dc
CM
3229 struct btrfs_ordered_extent *ordered;
3230 u64 page_start = page_offset(page);
3231 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
39279cc3 3232
e6dcd2dc 3233 wait_on_page_writeback(page);
d1310b2e 3234 tree = &BTRFS_I(page->mapping->host)->io_tree;
e6dcd2dc
CM
3235 if (offset) {
3236 btrfs_releasepage(page, GFP_NOFS);
3237 return;
3238 }
3239
3240 lock_extent(tree, page_start, page_end, GFP_NOFS);
3241 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
3242 page_offset(page));
3243 if (ordered) {
eb84ae03
CM
3244 /*
3245 * IO on this page will never be started, so we need
3246 * to account for any ordered extents now
3247 */
e6dcd2dc
CM
3248 clear_extent_bit(tree, page_start, page_end,
3249 EXTENT_DIRTY | EXTENT_DELALLOC |
3250 EXTENT_LOCKED, 1, 0, GFP_NOFS);
211f90e6
CM
3251 btrfs_finish_ordered_io(page->mapping->host,
3252 page_start, page_end);
e6dcd2dc
CM
3253 btrfs_put_ordered_extent(ordered);
3254 lock_extent(tree, page_start, page_end, GFP_NOFS);
3255 }
3256 clear_extent_bit(tree, page_start, page_end,
3257 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3258 EXTENT_ORDERED,
3259 1, 1, GFP_NOFS);
3260 __btrfs_releasepage(page, GFP_NOFS);
3261
4a096752 3262 ClearPageChecked(page);
9ad6b7bc 3263 if (PagePrivate(page)) {
9ad6b7bc
CM
3264 ClearPagePrivate(page);
3265 set_page_private(page, 0);
3266 page_cache_release(page);
3267 }
39279cc3
CM
3268}
3269
9ebefb18
CM
3270/*
3271 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
3272 * called from a page fault handler when a page is first dirtied. Hence we must
3273 * be careful to check for EOF conditions here. We set the page up correctly
3274 * for a written page which means we get ENOSPC checking when writing into
3275 * holes and correct delalloc and unwritten extent mapping on filesystems that
3276 * support these features.
3277 *
3278 * We are not allowed to take the i_mutex here so we have to play games to
3279 * protect against truncate races as the page could now be beyond EOF. Because
3280 * vmtruncate() writes the inode size before removing pages, once we have the
3281 * page lock we can determine safely if the page is beyond EOF. If it is not
3282 * beyond EOF, then the page is guaranteed safe against truncation until we
3283 * unlock the page.
3284 */
3285int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
3286{
6da6abae 3287 struct inode *inode = fdentry(vma->vm_file)->d_inode;
1832a6d5 3288 struct btrfs_root *root = BTRFS_I(inode)->root;
e6dcd2dc
CM
3289 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3290 struct btrfs_ordered_extent *ordered;
3291 char *kaddr;
3292 unsigned long zero_start;
9ebefb18 3293 loff_t size;
1832a6d5 3294 int ret;
a52d9a80 3295 u64 page_start;
e6dcd2dc 3296 u64 page_end;
9ebefb18 3297
1832a6d5 3298 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
1832a6d5
CM
3299 if (ret)
3300 goto out;
3301
3302 ret = -EINVAL;
e6dcd2dc 3303again:
9ebefb18 3304 lock_page(page);
9ebefb18 3305 size = i_size_read(inode);
e6dcd2dc
CM
3306 page_start = page_offset(page);
3307 page_end = page_start + PAGE_CACHE_SIZE - 1;
a52d9a80 3308
9ebefb18 3309 if ((page->mapping != inode->i_mapping) ||
e6dcd2dc 3310 (page_start >= size)) {
9ebefb18
CM
3311 /* page got truncated out from underneath us */
3312 goto out_unlock;
3313 }
e6dcd2dc
CM
3314 wait_on_page_writeback(page);
3315
3316 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
3317 set_page_extent_mapped(page);
3318
eb84ae03
CM
3319 /*
3320 * we can't set the delalloc bits if there are pending ordered
3321 * extents. Drop our locks and wait for them to finish
3322 */
e6dcd2dc
CM
3323 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3324 if (ordered) {
3325 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3326 unlock_page(page);
eb84ae03 3327 btrfs_start_ordered_extent(inode, ordered, 1);
e6dcd2dc
CM
3328 btrfs_put_ordered_extent(ordered);
3329 goto again;
3330 }
3331
ea8c2819 3332 btrfs_set_extent_delalloc(inode, page_start, page_end);
e6dcd2dc 3333 ret = 0;
9ebefb18
CM
3334
3335 /* page is wholly or partially inside EOF */
a52d9a80 3336 if (page_start + PAGE_CACHE_SIZE > size)
e6dcd2dc 3337 zero_start = size & ~PAGE_CACHE_MASK;
9ebefb18 3338 else
e6dcd2dc 3339 zero_start = PAGE_CACHE_SIZE;
9ebefb18 3340
e6dcd2dc
CM
3341 if (zero_start != PAGE_CACHE_SIZE) {
3342 kaddr = kmap(page);
3343 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
3344 flush_dcache_page(page);
3345 kunmap(page);
3346 }
247e743c 3347 ClearPageChecked(page);
e6dcd2dc
CM
3348 set_page_dirty(page);
3349 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
9ebefb18
CM
3350
3351out_unlock:
3352 unlock_page(page);
1832a6d5 3353out:
9ebefb18
CM
3354 return ret;
3355}
3356
39279cc3
CM
3357static void btrfs_truncate(struct inode *inode)
3358{
3359 struct btrfs_root *root = BTRFS_I(inode)->root;
3360 int ret;
3361 struct btrfs_trans_handle *trans;
d3c2fdcf 3362 unsigned long nr;
dbe674a9 3363 u64 mask = root->sectorsize - 1;
39279cc3
CM
3364
3365 if (!S_ISREG(inode->i_mode))
3366 return;
3367 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3368 return;
3369
3370 btrfs_truncate_page(inode->i_mapping, inode->i_size);
4a096752 3371 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
39279cc3 3372
39279cc3
CM
3373 trans = btrfs_start_transaction(root, 1);
3374 btrfs_set_trans_block_group(trans, inode);
dbe674a9 3375 btrfs_i_size_write(inode, inode->i_size);
39279cc3 3376
7b128766
JB
3377 ret = btrfs_orphan_add(trans, inode);
3378 if (ret)
3379 goto out;
39279cc3 3380 /* FIXME, add redo link to tree so we don't leak on crash */
e02119d5 3381 ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size,
85e21bac 3382 BTRFS_EXTENT_DATA_KEY);
39279cc3 3383 btrfs_update_inode(trans, root, inode);
5f39d397 3384
7b128766
JB
3385 ret = btrfs_orphan_del(trans, inode);
3386 BUG_ON(ret);
3387
3388out:
3389 nr = trans->blocks_used;
89ce8a63 3390 ret = btrfs_end_transaction_throttle(trans, root);
39279cc3 3391 BUG_ON(ret);
d3c2fdcf 3392 btrfs_btree_balance_dirty(root, nr);
39279cc3
CM
3393}
3394
3b96362c
SW
3395/*
3396 * Invalidate a single dcache entry at the root of the filesystem.
3397 * Needed after creation of snapshot or subvolume.
3398 */
3399void btrfs_invalidate_dcache_root(struct btrfs_root *root, char *name,
3400 int namelen)
3401{
3402 struct dentry *alias, *entry;
3403 struct qstr qstr;
3404
3405 alias = d_find_alias(root->fs_info->sb->s_root->d_inode);
3406 if (alias) {
3407 qstr.name = name;
3408 qstr.len = namelen;
3409 /* change me if btrfs ever gets a d_hash operation */
3410 qstr.hash = full_name_hash(qstr.name, qstr.len);
3411 entry = d_lookup(alias, &qstr);
3412 dput(alias);
3413 if (entry) {
3414 d_invalidate(entry);
3415 dput(entry);
3416 }
3417 }
3418}
3419
d352ac68
CM
3420/*
3421 * create a new subvolume directory/inode (helper for the ioctl).
3422 */
cb8e7090 3423int btrfs_create_subvol_root(struct btrfs_root *new_root, struct dentry *dentry,
f46b5a66
CH
3424 struct btrfs_trans_handle *trans, u64 new_dirid,
3425 struct btrfs_block_group_cache *block_group)
39279cc3 3426{
39279cc3 3427 struct inode *inode;
cb8e7090 3428 int error;
00e4e6b3 3429 u64 index = 0;
39279cc3 3430
aec7477b 3431 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
00e4e6b3 3432 new_dirid, block_group, S_IFDIR | 0700, &index);
54aa1f4d 3433 if (IS_ERR(inode))
f46b5a66 3434 return PTR_ERR(inode);
39279cc3
CM
3435 inode->i_op = &btrfs_dir_inode_operations;
3436 inode->i_fop = &btrfs_dir_file_operations;
34088780 3437 new_root->inode = inode;
39279cc3 3438
39279cc3 3439 inode->i_nlink = 1;
dbe674a9 3440 btrfs_i_size_write(inode, 0);
3b96362c 3441
cb8e7090
CH
3442 error = btrfs_update_inode(trans, new_root, inode);
3443 if (error)
3444 return error;
3445
3446 d_instantiate(dentry, inode);
3447 return 0;
39279cc3
CM
3448}
3449
d352ac68
CM
3450/* helper function for file defrag and space balancing. This
3451 * forces readahead on a given range of bytes in an inode
3452 */
edbd8d4e 3453unsigned long btrfs_force_ra(struct address_space *mapping,
86479a04
CM
3454 struct file_ra_state *ra, struct file *file,
3455 pgoff_t offset, pgoff_t last_index)
3456{
8e7bf94f 3457 pgoff_t req_size = last_index - offset + 1;
86479a04 3458
86479a04
CM
3459 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
3460 return offset + req_size;
86479a04
CM
3461}
3462
39279cc3
CM
3463struct inode *btrfs_alloc_inode(struct super_block *sb)
3464{
3465 struct btrfs_inode *ei;
3466
3467 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
3468 if (!ei)
3469 return NULL;
15ee9bc7 3470 ei->last_trans = 0;
e02119d5 3471 ei->logged_trans = 0;
e6dcd2dc 3472 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
33268eaf
JB
3473 ei->i_acl = BTRFS_ACL_NOT_CACHED;
3474 ei->i_default_acl = BTRFS_ACL_NOT_CACHED;
7b128766 3475 INIT_LIST_HEAD(&ei->i_orphan);
39279cc3
CM
3476 return &ei->vfs_inode;
3477}
3478
3479void btrfs_destroy_inode(struct inode *inode)
3480{
e6dcd2dc 3481 struct btrfs_ordered_extent *ordered;
39279cc3
CM
3482 WARN_ON(!list_empty(&inode->i_dentry));
3483 WARN_ON(inode->i_data.nrpages);
3484
33268eaf
JB
3485 if (BTRFS_I(inode)->i_acl &&
3486 BTRFS_I(inode)->i_acl != BTRFS_ACL_NOT_CACHED)
3487 posix_acl_release(BTRFS_I(inode)->i_acl);
3488 if (BTRFS_I(inode)->i_default_acl &&
3489 BTRFS_I(inode)->i_default_acl != BTRFS_ACL_NOT_CACHED)
3490 posix_acl_release(BTRFS_I(inode)->i_default_acl);
3491
bcc63abb 3492 spin_lock(&BTRFS_I(inode)->root->list_lock);
7b128766
JB
3493 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
3494 printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan"
3495 " list\n", inode->i_ino);
3496 dump_stack();
3497 }
bcc63abb 3498 spin_unlock(&BTRFS_I(inode)->root->list_lock);
7b128766 3499
e6dcd2dc
CM
3500 while(1) {
3501 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
3502 if (!ordered)
3503 break;
3504 else {
3505 printk("found ordered extent %Lu %Lu\n",
3506 ordered->file_offset, ordered->len);
3507 btrfs_remove_ordered_extent(inode, ordered);
3508 btrfs_put_ordered_extent(ordered);
3509 btrfs_put_ordered_extent(ordered);
3510 }
3511 }
5b21f2ed 3512 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
39279cc3
CM
3513 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
3514}
3515
0ee0fda0 3516static void init_once(void *foo)
39279cc3
CM
3517{
3518 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
3519
3520 inode_init_once(&ei->vfs_inode);
3521}
3522
3523void btrfs_destroy_cachep(void)
3524{
3525 if (btrfs_inode_cachep)
3526 kmem_cache_destroy(btrfs_inode_cachep);
3527 if (btrfs_trans_handle_cachep)
3528 kmem_cache_destroy(btrfs_trans_handle_cachep);
3529 if (btrfs_transaction_cachep)
3530 kmem_cache_destroy(btrfs_transaction_cachep);
3531 if (btrfs_bit_radix_cachep)
3532 kmem_cache_destroy(btrfs_bit_radix_cachep);
3533 if (btrfs_path_cachep)
3534 kmem_cache_destroy(btrfs_path_cachep);
3535}
3536
86479a04 3537struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
92fee66d 3538 unsigned long extra_flags,
2b1f55b0 3539 void (*ctor)(void *))
92fee66d
CM
3540{
3541 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2b1f55b0 3542 SLAB_MEM_SPREAD | extra_flags), ctor);
92fee66d
CM
3543}
3544
39279cc3
CM
3545int btrfs_init_cachep(void)
3546{
86479a04 3547 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
92fee66d
CM
3548 sizeof(struct btrfs_inode),
3549 0, init_once);
39279cc3
CM
3550 if (!btrfs_inode_cachep)
3551 goto fail;
86479a04
CM
3552 btrfs_trans_handle_cachep =
3553 btrfs_cache_create("btrfs_trans_handle_cache",
3554 sizeof(struct btrfs_trans_handle),
3555 0, NULL);
39279cc3
CM
3556 if (!btrfs_trans_handle_cachep)
3557 goto fail;
86479a04 3558 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
39279cc3 3559 sizeof(struct btrfs_transaction),
92fee66d 3560 0, NULL);
39279cc3
CM
3561 if (!btrfs_transaction_cachep)
3562 goto fail;
86479a04 3563 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
23223584 3564 sizeof(struct btrfs_path),
92fee66d 3565 0, NULL);
39279cc3
CM
3566 if (!btrfs_path_cachep)
3567 goto fail;
86479a04 3568 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
92fee66d 3569 SLAB_DESTROY_BY_RCU, NULL);
39279cc3
CM
3570 if (!btrfs_bit_radix_cachep)
3571 goto fail;
3572 return 0;
3573fail:
3574 btrfs_destroy_cachep();
3575 return -ENOMEM;
3576}
3577
3578static int btrfs_getattr(struct vfsmount *mnt,
3579 struct dentry *dentry, struct kstat *stat)
3580{
3581 struct inode *inode = dentry->d_inode;
3582 generic_fillattr(inode, stat);
d6667462 3583 stat->blksize = PAGE_CACHE_SIZE;
a76a3cd4
YZ
3584 stat->blocks = (inode_get_bytes(inode) +
3585 BTRFS_I(inode)->delalloc_bytes) >> 9;
39279cc3
CM
3586 return 0;
3587}
3588
3589static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
3590 struct inode * new_dir,struct dentry *new_dentry)
3591{
3592 struct btrfs_trans_handle *trans;
3593 struct btrfs_root *root = BTRFS_I(old_dir)->root;
3594 struct inode *new_inode = new_dentry->d_inode;
3595 struct inode *old_inode = old_dentry->d_inode;
3596 struct timespec ctime = CURRENT_TIME;
00e4e6b3 3597 u64 index = 0;
39279cc3
CM
3598 int ret;
3599
3600 if (S_ISDIR(old_inode->i_mode) && new_inode &&
3601 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
3602 return -ENOTEMPTY;
3603 }
5f39d397 3604
1832a6d5
CM
3605 ret = btrfs_check_free_space(root, 1, 0);
3606 if (ret)
3607 goto out_unlock;
3608
39279cc3 3609 trans = btrfs_start_transaction(root, 1);
5f39d397 3610
39279cc3 3611 btrfs_set_trans_block_group(trans, new_dir);
39279cc3 3612
e02119d5 3613 btrfs_inc_nlink(old_dentry->d_inode);
39279cc3
CM
3614 old_dir->i_ctime = old_dir->i_mtime = ctime;
3615 new_dir->i_ctime = new_dir->i_mtime = ctime;
3616 old_inode->i_ctime = ctime;
5f39d397 3617
e02119d5
CM
3618 ret = btrfs_unlink_inode(trans, root, old_dir, old_dentry->d_inode,
3619 old_dentry->d_name.name,
3620 old_dentry->d_name.len);
39279cc3
CM
3621 if (ret)
3622 goto out_fail;
3623
3624 if (new_inode) {
3625 new_inode->i_ctime = CURRENT_TIME;
e02119d5
CM
3626 ret = btrfs_unlink_inode(trans, root, new_dir,
3627 new_dentry->d_inode,
3628 new_dentry->d_name.name,
3629 new_dentry->d_name.len);
39279cc3
CM
3630 if (ret)
3631 goto out_fail;
7b128766 3632 if (new_inode->i_nlink == 0) {
e02119d5 3633 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
7b128766
JB
3634 if (ret)
3635 goto out_fail;
3636 }
e02119d5 3637
39279cc3 3638 }
00e4e6b3 3639 ret = btrfs_set_inode_index(new_dir, old_inode, &index);
aec7477b
JB
3640 if (ret)
3641 goto out_fail;
3642
e02119d5
CM
3643 ret = btrfs_add_link(trans, new_dentry->d_parent->d_inode,
3644 old_inode, new_dentry->d_name.name,
3645 new_dentry->d_name.len, 1, index);
39279cc3
CM
3646 if (ret)
3647 goto out_fail;
3648
3649out_fail:
ab78c84d 3650 btrfs_end_transaction_throttle(trans, root);
1832a6d5 3651out_unlock:
39279cc3
CM
3652 return ret;
3653}
3654
d352ac68
CM
3655/*
3656 * some fairly slow code that needs optimization. This walks the list
3657 * of all the inodes with pending delalloc and forces them to disk.
3658 */
ea8c2819
CM
3659int btrfs_start_delalloc_inodes(struct btrfs_root *root)
3660{
3661 struct list_head *head = &root->fs_info->delalloc_inodes;
3662 struct btrfs_inode *binode;
5b21f2ed 3663 struct inode *inode;
ea8c2819
CM
3664 unsigned long flags;
3665
3666 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
3667 while(!list_empty(head)) {
3668 binode = list_entry(head->next, struct btrfs_inode,
3669 delalloc_inodes);
5b21f2ed
ZY
3670 inode = igrab(&binode->vfs_inode);
3671 if (!inode)
3672 list_del_init(&binode->delalloc_inodes);
ea8c2819 3673 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
5b21f2ed 3674 if (inode) {
8c8bee1d 3675 filemap_flush(inode->i_mapping);
5b21f2ed
ZY
3676 iput(inode);
3677 }
3678 cond_resched();
ea8c2819
CM
3679 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
3680 }
3681 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
8c8bee1d
CM
3682
3683 /* the filemap_flush will queue IO into the worker threads, but
3684 * we have to make sure the IO is actually started and that
3685 * ordered extents get created before we return
3686 */
3687 atomic_inc(&root->fs_info->async_submit_draining);
3688 while(atomic_read(&root->fs_info->nr_async_submits)) {
3689 wait_event(root->fs_info->async_submit_wait,
3690 (atomic_read(&root->fs_info->nr_async_submits) == 0));
3691 }
3692 atomic_dec(&root->fs_info->async_submit_draining);
ea8c2819
CM
3693 return 0;
3694}
3695
39279cc3
CM
3696static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
3697 const char *symname)
3698{
3699 struct btrfs_trans_handle *trans;
3700 struct btrfs_root *root = BTRFS_I(dir)->root;
3701 struct btrfs_path *path;
3702 struct btrfs_key key;
1832a6d5 3703 struct inode *inode = NULL;
39279cc3
CM
3704 int err;
3705 int drop_inode = 0;
3706 u64 objectid;
00e4e6b3 3707 u64 index = 0 ;
39279cc3
CM
3708 int name_len;
3709 int datasize;
5f39d397 3710 unsigned long ptr;
39279cc3 3711 struct btrfs_file_extent_item *ei;
5f39d397 3712 struct extent_buffer *leaf;
1832a6d5 3713 unsigned long nr = 0;
39279cc3
CM
3714
3715 name_len = strlen(symname) + 1;
3716 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
3717 return -ENAMETOOLONG;
1832a6d5 3718
1832a6d5
CM
3719 err = btrfs_check_free_space(root, 1, 0);
3720 if (err)
3721 goto out_fail;
3722
39279cc3
CM
3723 trans = btrfs_start_transaction(root, 1);
3724 btrfs_set_trans_block_group(trans, dir);
3725
3726 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3727 if (err) {
3728 err = -ENOSPC;
3729 goto out_unlock;
3730 }
3731
aec7477b 3732 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
9c58309d
CM
3733 dentry->d_name.len,
3734 dentry->d_parent->d_inode->i_ino, objectid,
00e4e6b3
CM
3735 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
3736 &index);
39279cc3
CM
3737 err = PTR_ERR(inode);
3738 if (IS_ERR(inode))
3739 goto out_unlock;
3740
33268eaf
JB
3741 err = btrfs_init_acl(inode, dir);
3742 if (err) {
3743 drop_inode = 1;
3744 goto out_unlock;
3745 }
3746
39279cc3 3747 btrfs_set_trans_block_group(trans, inode);
00e4e6b3 3748 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
39279cc3
CM
3749 if (err)
3750 drop_inode = 1;
3751 else {
3752 inode->i_mapping->a_ops = &btrfs_aops;
04160088 3753 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
39279cc3
CM
3754 inode->i_fop = &btrfs_file_operations;
3755 inode->i_op = &btrfs_file_inode_operations;
d1310b2e 3756 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
39279cc3
CM
3757 }
3758 dir->i_sb->s_dirt = 1;
3759 btrfs_update_inode_block_group(trans, inode);
3760 btrfs_update_inode_block_group(trans, dir);
3761 if (drop_inode)
3762 goto out_unlock;
3763
3764 path = btrfs_alloc_path();
3765 BUG_ON(!path);
3766 key.objectid = inode->i_ino;
3767 key.offset = 0;
39279cc3
CM
3768 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
3769 datasize = btrfs_file_extent_calc_inline_size(name_len);
3770 err = btrfs_insert_empty_item(trans, root, path, &key,
3771 datasize);
54aa1f4d
CM
3772 if (err) {
3773 drop_inode = 1;
3774 goto out_unlock;
3775 }
5f39d397
CM
3776 leaf = path->nodes[0];
3777 ei = btrfs_item_ptr(leaf, path->slots[0],
3778 struct btrfs_file_extent_item);
3779 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
3780 btrfs_set_file_extent_type(leaf, ei,
39279cc3
CM
3781 BTRFS_FILE_EXTENT_INLINE);
3782 ptr = btrfs_file_extent_inline_start(ei);
5f39d397
CM
3783 write_extent_buffer(leaf, symname, ptr, name_len);
3784 btrfs_mark_buffer_dirty(leaf);
39279cc3 3785 btrfs_free_path(path);
5f39d397 3786
39279cc3
CM
3787 inode->i_op = &btrfs_symlink_inode_operations;
3788 inode->i_mapping->a_ops = &btrfs_symlink_aops;
04160088 3789 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
dbe674a9 3790 btrfs_i_size_write(inode, name_len - 1);
54aa1f4d
CM
3791 err = btrfs_update_inode(trans, root, inode);
3792 if (err)
3793 drop_inode = 1;
39279cc3
CM
3794
3795out_unlock:
d3c2fdcf 3796 nr = trans->blocks_used;
ab78c84d 3797 btrfs_end_transaction_throttle(trans, root);
1832a6d5 3798out_fail:
39279cc3
CM
3799 if (drop_inode) {
3800 inode_dec_link_count(inode);
3801 iput(inode);
3802 }
d3c2fdcf 3803 btrfs_btree_balance_dirty(root, nr);
39279cc3
CM
3804 return err;
3805}
16432985 3806
e6dcd2dc
CM
3807static int btrfs_set_page_dirty(struct page *page)
3808{
e6dcd2dc
CM
3809 return __set_page_dirty_nobuffers(page);
3810}
3811
0ee0fda0 3812static int btrfs_permission(struct inode *inode, int mask)
fdebe2bd
Y
3813{
3814 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
3815 return -EACCES;
33268eaf 3816 return generic_permission(inode, mask, btrfs_check_acl);
fdebe2bd 3817}
39279cc3
CM
3818
3819static struct inode_operations btrfs_dir_inode_operations = {
3820 .lookup = btrfs_lookup,
3821 .create = btrfs_create,
3822 .unlink = btrfs_unlink,
3823 .link = btrfs_link,
3824 .mkdir = btrfs_mkdir,
3825 .rmdir = btrfs_rmdir,
3826 .rename = btrfs_rename,
3827 .symlink = btrfs_symlink,
3828 .setattr = btrfs_setattr,
618e21d5 3829 .mknod = btrfs_mknod,
95819c05
CH
3830 .setxattr = btrfs_setxattr,
3831 .getxattr = btrfs_getxattr,
5103e947 3832 .listxattr = btrfs_listxattr,
95819c05 3833 .removexattr = btrfs_removexattr,
fdebe2bd 3834 .permission = btrfs_permission,
39279cc3 3835};
39279cc3
CM
3836static struct inode_operations btrfs_dir_ro_inode_operations = {
3837 .lookup = btrfs_lookup,
fdebe2bd 3838 .permission = btrfs_permission,
39279cc3 3839};
39279cc3
CM
3840static struct file_operations btrfs_dir_file_operations = {
3841 .llseek = generic_file_llseek,
3842 .read = generic_read_dir,
cbdf5a24 3843 .readdir = btrfs_real_readdir,
34287aa3 3844 .unlocked_ioctl = btrfs_ioctl,
39279cc3 3845#ifdef CONFIG_COMPAT
34287aa3 3846 .compat_ioctl = btrfs_ioctl,
39279cc3 3847#endif
6bf13c0c 3848 .release = btrfs_release_file,
e02119d5 3849 .fsync = btrfs_sync_file,
39279cc3
CM
3850};
3851
d1310b2e 3852static struct extent_io_ops btrfs_extent_io_ops = {
07157aac 3853 .fill_delalloc = run_delalloc_range,
065631f6 3854 .submit_bio_hook = btrfs_submit_bio_hook,
239b14b3 3855 .merge_bio_hook = btrfs_merge_bio_hook,
07157aac 3856 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
e6dcd2dc 3857 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
247e743c 3858 .writepage_start_hook = btrfs_writepage_start_hook,
1259ab75 3859 .readpage_io_failed_hook = btrfs_io_failed_hook,
b0c68f8b
CM
3860 .set_bit_hook = btrfs_set_bit_hook,
3861 .clear_bit_hook = btrfs_clear_bit_hook,
07157aac
CM
3862};
3863
39279cc3
CM
3864static struct address_space_operations btrfs_aops = {
3865 .readpage = btrfs_readpage,
3866 .writepage = btrfs_writepage,
b293f02e 3867 .writepages = btrfs_writepages,
3ab2fb5a 3868 .readpages = btrfs_readpages,
39279cc3 3869 .sync_page = block_sync_page,
39279cc3 3870 .bmap = btrfs_bmap,
16432985 3871 .direct_IO = btrfs_direct_IO,
a52d9a80
CM
3872 .invalidatepage = btrfs_invalidatepage,
3873 .releasepage = btrfs_releasepage,
e6dcd2dc 3874 .set_page_dirty = btrfs_set_page_dirty,
39279cc3
CM
3875};
3876
3877static struct address_space_operations btrfs_symlink_aops = {
3878 .readpage = btrfs_readpage,
3879 .writepage = btrfs_writepage,
2bf5a725
CM
3880 .invalidatepage = btrfs_invalidatepage,
3881 .releasepage = btrfs_releasepage,
39279cc3
CM
3882};
3883
3884static struct inode_operations btrfs_file_inode_operations = {
3885 .truncate = btrfs_truncate,
3886 .getattr = btrfs_getattr,
3887 .setattr = btrfs_setattr,
95819c05
CH
3888 .setxattr = btrfs_setxattr,
3889 .getxattr = btrfs_getxattr,
5103e947 3890 .listxattr = btrfs_listxattr,
95819c05 3891 .removexattr = btrfs_removexattr,
fdebe2bd 3892 .permission = btrfs_permission,
39279cc3 3893};
618e21d5
JB
3894static struct inode_operations btrfs_special_inode_operations = {
3895 .getattr = btrfs_getattr,
3896 .setattr = btrfs_setattr,
fdebe2bd 3897 .permission = btrfs_permission,
95819c05
CH
3898 .setxattr = btrfs_setxattr,
3899 .getxattr = btrfs_getxattr,
33268eaf 3900 .listxattr = btrfs_listxattr,
95819c05 3901 .removexattr = btrfs_removexattr,
618e21d5 3902};
39279cc3
CM
3903static struct inode_operations btrfs_symlink_inode_operations = {
3904 .readlink = generic_readlink,
3905 .follow_link = page_follow_link_light,
3906 .put_link = page_put_link,
fdebe2bd 3907 .permission = btrfs_permission,
39279cc3 3908};