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