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