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