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