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