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