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