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