[concurrent-pagecache] fix nrpages stuff in btrfs/aufs/reiser4
[linux-2.6/zen-sources.git] / fs / btrfs / inode.c
blob84b4c5c438061c23ae3ff8637ef8da3b5afe82b8
1 /*
2 * Copyright (C) 2007 Oracle. All rights reserved.
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.
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.
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.
19 #include <linux/bio.h>
20 #include <linux/buffer_head.h>
21 #include <linux/fs.h>
22 #include <linux/pagemap.h>
23 #include <linux/highmem.h>
24 #include <linux/time.h>
25 #include <linux/init.h>
26 #include <linux/string.h>
27 #include <linux/smp_lock.h>
28 #include <linux/backing-dev.h>
29 #include <linux/mpage.h>
30 #include <linux/swap.h>
31 #include <linux/writeback.h>
32 #include <linux/statfs.h>
33 #include <linux/compat.h>
34 #include <linux/bit_spinlock.h>
35 #include <linux/version.h>
36 #include <linux/xattr.h>
37 #include "ctree.h"
38 #include "disk-io.h"
39 #include "transaction.h"
40 #include "btrfs_inode.h"
41 #include "ioctl.h"
42 #include "print-tree.h"
44 struct btrfs_iget_args {
45 u64 ino;
46 struct btrfs_root *root;
49 static struct inode_operations btrfs_dir_inode_operations;
50 static struct inode_operations btrfs_symlink_inode_operations;
51 static struct inode_operations btrfs_dir_ro_inode_operations;
52 static struct inode_operations btrfs_special_inode_operations;
53 static struct inode_operations btrfs_file_inode_operations;
54 static struct address_space_operations btrfs_aops;
55 static struct address_space_operations btrfs_symlink_aops;
56 static struct file_operations btrfs_dir_file_operations;
57 static struct extent_io_ops btrfs_extent_io_ops;
59 static struct kmem_cache *btrfs_inode_cachep;
60 struct kmem_cache *btrfs_trans_handle_cachep;
61 struct kmem_cache *btrfs_transaction_cachep;
62 struct kmem_cache *btrfs_bit_radix_cachep;
63 struct kmem_cache *btrfs_path_cachep;
65 #define S_SHIFT 12
66 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
67 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
68 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
69 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
70 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
71 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
72 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
73 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
76 int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
77 int for_del)
79 u64 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
80 u64 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
81 u64 thresh;
82 int ret = 0;
84 if (for_del)
85 thresh = total * 90;
86 else
87 thresh = total * 85;
89 do_div(thresh, 100);
91 spin_lock(&root->fs_info->delalloc_lock);
92 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
93 ret = -ENOSPC;
94 spin_unlock(&root->fs_info->delalloc_lock);
95 return ret;
98 static int cow_file_range(struct inode *inode, u64 start, u64 end)
100 struct btrfs_root *root = BTRFS_I(inode)->root;
101 struct btrfs_trans_handle *trans;
102 u64 alloc_hint = 0;
103 u64 num_bytes;
104 u64 cur_alloc_size;
105 u64 blocksize = root->sectorsize;
106 u64 orig_start = start;
107 u64 orig_num_bytes;
108 struct btrfs_key ins;
109 int ret;
111 trans = btrfs_start_transaction(root, 1);
112 BUG_ON(!trans);
113 btrfs_set_trans_block_group(trans, inode);
115 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
116 num_bytes = max(blocksize, num_bytes);
117 ret = btrfs_drop_extents(trans, root, inode,
118 start, start + num_bytes, start, &alloc_hint);
119 orig_num_bytes = num_bytes;
121 if (alloc_hint == EXTENT_MAP_INLINE)
122 goto out;
124 while(num_bytes > 0) {
125 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
126 ret = btrfs_alloc_extent(trans, root, cur_alloc_size,
127 root->root_key.objectid,
128 trans->transid,
129 inode->i_ino, start, 0,
130 alloc_hint, (u64)-1, &ins, 1);
131 if (ret) {
132 WARN_ON(1);
133 goto out;
135 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
136 start, ins.objectid, ins.offset,
137 ins.offset);
138 inode->i_blocks += ins.offset >> 9;
139 btrfs_check_file(root, inode);
140 num_bytes -= cur_alloc_size;
141 alloc_hint = ins.objectid + ins.offset;
142 start += cur_alloc_size;
144 btrfs_drop_extent_cache(inode, orig_start,
145 orig_start + orig_num_bytes - 1);
146 btrfs_add_ordered_inode(inode);
147 btrfs_update_inode(trans, root, inode);
148 out:
149 btrfs_end_transaction(trans, root);
150 return ret;
153 static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
155 u64 extent_start;
156 u64 extent_end;
157 u64 bytenr;
158 u64 cow_end;
159 u64 loops = 0;
160 u64 total_fs_bytes;
161 struct btrfs_root *root = BTRFS_I(inode)->root;
162 struct extent_buffer *leaf;
163 int found_type;
164 struct btrfs_path *path;
165 struct btrfs_file_extent_item *item;
166 int ret;
167 int err;
168 struct btrfs_key found_key;
170 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
171 path = btrfs_alloc_path();
172 BUG_ON(!path);
173 again:
174 ret = btrfs_lookup_file_extent(NULL, root, path,
175 inode->i_ino, start, 0);
176 if (ret < 0) {
177 btrfs_free_path(path);
178 return ret;
181 cow_end = end;
182 if (ret != 0) {
183 if (path->slots[0] == 0)
184 goto not_found;
185 path->slots[0]--;
188 leaf = path->nodes[0];
189 item = btrfs_item_ptr(leaf, path->slots[0],
190 struct btrfs_file_extent_item);
192 /* are we inside the extent that was found? */
193 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
194 found_type = btrfs_key_type(&found_key);
195 if (found_key.objectid != inode->i_ino ||
196 found_type != BTRFS_EXTENT_DATA_KEY) {
197 goto not_found;
200 found_type = btrfs_file_extent_type(leaf, item);
201 extent_start = found_key.offset;
202 if (found_type == BTRFS_FILE_EXTENT_REG) {
203 u64 extent_num_bytes;
205 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
206 extent_end = extent_start + extent_num_bytes;
207 err = 0;
209 if (loops && start != extent_start)
210 goto not_found;
212 if (start < extent_start || start >= extent_end)
213 goto not_found;
215 cow_end = min(end, extent_end - 1);
216 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
217 if (bytenr == 0)
218 goto not_found;
221 * we may be called by the resizer, make sure we're inside
222 * the limits of the FS
224 if (bytenr + extent_num_bytes > total_fs_bytes)
225 goto not_found;
227 if (btrfs_count_snapshots_in_path(root, path, bytenr) != 1) {
228 goto not_found;
231 start = extent_end;
232 } else {
233 goto not_found;
235 loop:
236 if (start > end) {
237 btrfs_free_path(path);
238 return 0;
240 btrfs_release_path(root, path);
241 loops++;
242 goto again;
244 not_found:
245 cow_file_range(inode, start, cow_end);
246 start = cow_end + 1;
247 goto loop;
250 static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
252 struct btrfs_root *root = BTRFS_I(inode)->root;
253 int ret;
254 mutex_lock(&root->fs_info->fs_mutex);
255 if (btrfs_test_opt(root, NODATACOW) ||
256 btrfs_test_flag(inode, NODATACOW))
257 ret = run_delalloc_nocow(inode, start, end);
258 else
259 ret = cow_file_range(inode, start, end);
261 mutex_unlock(&root->fs_info->fs_mutex);
262 return ret;
265 int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
266 unsigned long old, unsigned long bits)
268 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
269 struct btrfs_root *root = BTRFS_I(inode)->root;
270 spin_lock(&root->fs_info->delalloc_lock);
271 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
272 root->fs_info->delalloc_bytes += end - start + 1;
273 spin_unlock(&root->fs_info->delalloc_lock);
275 return 0;
278 int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
279 unsigned long old, unsigned long bits)
281 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
282 struct btrfs_root *root = BTRFS_I(inode)->root;
283 spin_lock(&root->fs_info->delalloc_lock);
284 if (end - start + 1 > root->fs_info->delalloc_bytes) {
285 printk("warning: delalloc account %Lu %Lu\n",
286 end - start + 1, root->fs_info->delalloc_bytes);
287 root->fs_info->delalloc_bytes = 0;
288 BTRFS_I(inode)->delalloc_bytes = 0;
289 } else {
290 root->fs_info->delalloc_bytes -= end - start + 1;
291 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
293 spin_unlock(&root->fs_info->delalloc_lock);
295 return 0;
298 int btrfs_submit_bio_hook(int rw, struct bio *bio)
300 struct bio_vec *bvec = bio->bi_io_vec;
301 struct inode *inode = bvec->bv_page->mapping->host;
302 struct btrfs_root *root = BTRFS_I(inode)->root;
303 struct btrfs_trans_handle *trans;
304 int ret = 0;
306 if (rw != WRITE)
307 return 0;
309 if (btrfs_test_opt(root, NODATASUM) ||
310 btrfs_test_flag(inode, NODATASUM))
311 return 0;
313 mutex_lock(&root->fs_info->fs_mutex);
314 trans = btrfs_start_transaction(root, 1);
315 btrfs_set_trans_block_group(trans, inode);
316 btrfs_csum_file_blocks(trans, root, inode, bio);
317 ret = btrfs_end_transaction(trans, root);
318 BUG_ON(ret);
319 mutex_unlock(&root->fs_info->fs_mutex);
320 return ret;
323 int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
325 int ret = 0;
326 struct inode *inode = page->mapping->host;
327 struct btrfs_root *root = BTRFS_I(inode)->root;
328 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
329 struct btrfs_csum_item *item;
330 struct btrfs_path *path = NULL;
331 u32 csum;
332 if (btrfs_test_opt(root, NODATASUM) ||
333 btrfs_test_flag(inode, NODATASUM))
334 return 0;
335 mutex_lock(&root->fs_info->fs_mutex);
336 path = btrfs_alloc_path();
337 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
338 if (IS_ERR(item)) {
339 ret = PTR_ERR(item);
340 /* a csum that isn't present is a preallocated region. */
341 if (ret == -ENOENT || ret == -EFBIG)
342 ret = 0;
343 csum = 0;
344 printk("no csum found for inode %lu start %Lu\n", inode->i_ino, start);
345 goto out;
347 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
348 BTRFS_CRC32_SIZE);
349 set_state_private(io_tree, start, csum);
350 out:
351 if (path)
352 btrfs_free_path(path);
353 mutex_unlock(&root->fs_info->fs_mutex);
354 return ret;
357 int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
358 struct extent_state *state)
360 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
361 struct inode *inode = page->mapping->host;
362 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
363 char *kaddr;
364 u64 private = ~(u32)0;
365 int ret;
366 struct btrfs_root *root = BTRFS_I(inode)->root;
367 u32 csum = ~(u32)0;
368 unsigned long flags;
370 if (btrfs_test_opt(root, NODATASUM) ||
371 btrfs_test_flag(inode, NODATASUM))
372 return 0;
373 if (state && state->start == start) {
374 private = state->private;
375 ret = 0;
376 } else {
377 ret = get_state_private(io_tree, start, &private);
379 local_irq_save(flags);
380 kaddr = kmap_atomic(page, KM_IRQ0);
381 if (ret) {
382 goto zeroit;
384 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
385 btrfs_csum_final(csum, (char *)&csum);
386 if (csum != private) {
387 goto zeroit;
389 kunmap_atomic(kaddr, KM_IRQ0);
390 local_irq_restore(flags);
391 return 0;
393 zeroit:
394 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
395 page->mapping->host->i_ino, (unsigned long long)start, csum,
396 private);
397 memset(kaddr + offset, 1, end - start + 1);
398 flush_dcache_page(page);
399 kunmap_atomic(kaddr, KM_IRQ0);
400 local_irq_restore(flags);
401 return 0;
404 void btrfs_read_locked_inode(struct inode *inode)
406 struct btrfs_path *path;
407 struct extent_buffer *leaf;
408 struct btrfs_inode_item *inode_item;
409 struct btrfs_inode_timespec *tspec;
410 struct btrfs_root *root = BTRFS_I(inode)->root;
411 struct btrfs_key location;
412 u64 alloc_group_block;
413 u32 rdev;
414 int ret;
416 path = btrfs_alloc_path();
417 BUG_ON(!path);
418 mutex_lock(&root->fs_info->fs_mutex);
419 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
421 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
422 if (ret)
423 goto make_bad;
425 leaf = path->nodes[0];
426 inode_item = btrfs_item_ptr(leaf, path->slots[0],
427 struct btrfs_inode_item);
429 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
430 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
431 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
432 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
433 inode->i_size = btrfs_inode_size(leaf, inode_item);
435 tspec = btrfs_inode_atime(inode_item);
436 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
437 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
439 tspec = btrfs_inode_mtime(inode_item);
440 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
441 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
443 tspec = btrfs_inode_ctime(inode_item);
444 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
445 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
447 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
448 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
449 inode->i_rdev = 0;
450 rdev = btrfs_inode_rdev(leaf, inode_item);
452 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
453 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
454 alloc_group_block);
455 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
456 if (!BTRFS_I(inode)->block_group) {
457 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
458 NULL, 0, 0, 0);
460 btrfs_free_path(path);
461 inode_item = NULL;
463 mutex_unlock(&root->fs_info->fs_mutex);
465 switch (inode->i_mode & S_IFMT) {
466 case S_IFREG:
467 inode->i_mapping->a_ops = &btrfs_aops;
468 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
469 inode->i_fop = &btrfs_file_operations;
470 inode->i_op = &btrfs_file_inode_operations;
471 break;
472 case S_IFDIR:
473 inode->i_fop = &btrfs_dir_file_operations;
474 if (root == root->fs_info->tree_root)
475 inode->i_op = &btrfs_dir_ro_inode_operations;
476 else
477 inode->i_op = &btrfs_dir_inode_operations;
478 break;
479 case S_IFLNK:
480 inode->i_op = &btrfs_symlink_inode_operations;
481 inode->i_mapping->a_ops = &btrfs_symlink_aops;
482 break;
483 default:
484 init_special_inode(inode, inode->i_mode, rdev);
485 break;
487 return;
489 make_bad:
490 btrfs_release_path(root, path);
491 btrfs_free_path(path);
492 mutex_unlock(&root->fs_info->fs_mutex);
493 make_bad_inode(inode);
496 static void fill_inode_item(struct extent_buffer *leaf,
497 struct btrfs_inode_item *item,
498 struct inode *inode)
500 btrfs_set_inode_uid(leaf, item, inode->i_uid);
501 btrfs_set_inode_gid(leaf, item, inode->i_gid);
502 btrfs_set_inode_size(leaf, item, inode->i_size);
503 btrfs_set_inode_mode(leaf, item, inode->i_mode);
504 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
506 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
507 inode->i_atime.tv_sec);
508 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
509 inode->i_atime.tv_nsec);
511 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
512 inode->i_mtime.tv_sec);
513 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
514 inode->i_mtime.tv_nsec);
516 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
517 inode->i_ctime.tv_sec);
518 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
519 inode->i_ctime.tv_nsec);
521 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
522 btrfs_set_inode_generation(leaf, item, inode->i_generation);
523 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
524 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
525 btrfs_set_inode_block_group(leaf, item,
526 BTRFS_I(inode)->block_group->key.objectid);
529 int btrfs_update_inode(struct btrfs_trans_handle *trans,
530 struct btrfs_root *root,
531 struct inode *inode)
533 struct btrfs_inode_item *inode_item;
534 struct btrfs_path *path;
535 struct extent_buffer *leaf;
536 int ret;
538 path = btrfs_alloc_path();
539 BUG_ON(!path);
540 ret = btrfs_lookup_inode(trans, root, path,
541 &BTRFS_I(inode)->location, 1);
542 if (ret) {
543 if (ret > 0)
544 ret = -ENOENT;
545 goto failed;
548 leaf = path->nodes[0];
549 inode_item = btrfs_item_ptr(leaf, path->slots[0],
550 struct btrfs_inode_item);
552 fill_inode_item(leaf, inode_item, inode);
553 btrfs_mark_buffer_dirty(leaf);
554 btrfs_set_inode_last_trans(trans, inode);
555 ret = 0;
556 failed:
557 btrfs_release_path(root, path);
558 btrfs_free_path(path);
559 return ret;
563 static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
564 struct btrfs_root *root,
565 struct inode *dir,
566 struct dentry *dentry)
568 struct btrfs_path *path;
569 const char *name = dentry->d_name.name;
570 int name_len = dentry->d_name.len;
571 int ret = 0;
572 struct extent_buffer *leaf;
573 struct btrfs_dir_item *di;
574 struct btrfs_key key;
576 path = btrfs_alloc_path();
577 if (!path) {
578 ret = -ENOMEM;
579 goto err;
582 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
583 name, name_len, -1);
584 if (IS_ERR(di)) {
585 ret = PTR_ERR(di);
586 goto err;
588 if (!di) {
589 ret = -ENOENT;
590 goto err;
592 leaf = path->nodes[0];
593 btrfs_dir_item_key_to_cpu(leaf, di, &key);
594 ret = btrfs_delete_one_dir_name(trans, root, path, di);
595 if (ret)
596 goto err;
597 btrfs_release_path(root, path);
599 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
600 key.objectid, name, name_len, -1);
601 if (IS_ERR(di)) {
602 ret = PTR_ERR(di);
603 goto err;
605 if (!di) {
606 ret = -ENOENT;
607 goto err;
609 ret = btrfs_delete_one_dir_name(trans, root, path, di);
611 dentry->d_inode->i_ctime = dir->i_ctime;
612 ret = btrfs_del_inode_ref(trans, root, name, name_len,
613 dentry->d_inode->i_ino,
614 dentry->d_parent->d_inode->i_ino);
615 if (ret) {
616 printk("failed to delete reference to %.*s, "
617 "inode %lu parent %lu\n", name_len, name,
618 dentry->d_inode->i_ino,
619 dentry->d_parent->d_inode->i_ino);
621 err:
622 btrfs_free_path(path);
623 if (!ret) {
624 dir->i_size -= name_len * 2;
625 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
626 btrfs_update_inode(trans, root, dir);
627 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
628 dentry->d_inode->i_nlink--;
629 #else
630 drop_nlink(dentry->d_inode);
631 #endif
632 ret = btrfs_update_inode(trans, root, dentry->d_inode);
633 dir->i_sb->s_dirt = 1;
635 return ret;
638 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
640 struct btrfs_root *root;
641 struct btrfs_trans_handle *trans;
642 struct inode *inode = dentry->d_inode;
643 int ret;
644 unsigned long nr = 0;
646 root = BTRFS_I(dir)->root;
647 mutex_lock(&root->fs_info->fs_mutex);
649 ret = btrfs_check_free_space(root, 1, 1);
650 if (ret)
651 goto fail;
653 trans = btrfs_start_transaction(root, 1);
655 btrfs_set_trans_block_group(trans, dir);
656 ret = btrfs_unlink_trans(trans, root, dir, dentry);
657 nr = trans->blocks_used;
659 if (inode->i_nlink == 0) {
660 int found;
661 /* if the inode isn't linked anywhere,
662 * we don't need to worry about
663 * data=ordered
665 found = btrfs_del_ordered_inode(inode);
666 if (found == 1) {
667 atomic_dec(&inode->i_count);
671 btrfs_end_transaction(trans, root);
672 fail:
673 mutex_unlock(&root->fs_info->fs_mutex);
674 btrfs_btree_balance_dirty(root, nr);
675 btrfs_throttle(root);
676 return ret;
679 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
681 struct inode *inode = dentry->d_inode;
682 int err = 0;
683 int ret;
684 struct btrfs_root *root = BTRFS_I(dir)->root;
685 struct btrfs_trans_handle *trans;
686 unsigned long nr = 0;
688 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
689 return -ENOTEMPTY;
691 mutex_lock(&root->fs_info->fs_mutex);
692 ret = btrfs_check_free_space(root, 1, 1);
693 if (ret)
694 goto fail;
696 trans = btrfs_start_transaction(root, 1);
697 btrfs_set_trans_block_group(trans, dir);
699 /* now the directory is empty */
700 err = btrfs_unlink_trans(trans, root, dir, dentry);
701 if (!err) {
702 inode->i_size = 0;
705 nr = trans->blocks_used;
706 ret = btrfs_end_transaction(trans, root);
707 fail:
708 mutex_unlock(&root->fs_info->fs_mutex);
709 btrfs_btree_balance_dirty(root, nr);
710 btrfs_throttle(root);
712 if (ret && !err)
713 err = ret;
714 return err;
718 * this can truncate away extent items, csum items and directory items.
719 * It starts at a high offset and removes keys until it can't find
720 * any higher than i_size.
722 * csum items that cross the new i_size are truncated to the new size
723 * as well.
725 static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
726 struct btrfs_root *root,
727 struct inode *inode,
728 u32 min_type)
730 int ret;
731 struct btrfs_path *path;
732 struct btrfs_key key;
733 struct btrfs_key found_key;
734 u32 found_type;
735 struct extent_buffer *leaf;
736 struct btrfs_file_extent_item *fi;
737 u64 extent_start = 0;
738 u64 extent_num_bytes = 0;
739 u64 item_end = 0;
740 u64 root_gen = 0;
741 u64 root_owner = 0;
742 int found_extent;
743 int del_item;
744 int pending_del_nr = 0;
745 int pending_del_slot = 0;
746 int extent_type = -1;
748 btrfs_drop_extent_cache(inode, inode->i_size, (u64)-1);
749 path = btrfs_alloc_path();
750 path->reada = -1;
751 BUG_ON(!path);
753 /* FIXME, add redo link to tree so we don't leak on crash */
754 key.objectid = inode->i_ino;
755 key.offset = (u64)-1;
756 key.type = (u8)-1;
758 btrfs_init_path(path);
759 search_again:
760 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
761 if (ret < 0) {
762 goto error;
764 if (ret > 0) {
765 BUG_ON(path->slots[0] == 0);
766 path->slots[0]--;
769 while(1) {
770 fi = NULL;
771 leaf = path->nodes[0];
772 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
773 found_type = btrfs_key_type(&found_key);
775 if (found_key.objectid != inode->i_ino)
776 break;
778 if (found_type < min_type)
779 break;
781 item_end = found_key.offset;
782 if (found_type == BTRFS_EXTENT_DATA_KEY) {
783 fi = btrfs_item_ptr(leaf, path->slots[0],
784 struct btrfs_file_extent_item);
785 extent_type = btrfs_file_extent_type(leaf, fi);
786 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
787 item_end +=
788 btrfs_file_extent_num_bytes(leaf, fi);
789 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
790 struct btrfs_item *item = btrfs_item_nr(leaf,
791 path->slots[0]);
792 item_end += btrfs_file_extent_inline_len(leaf,
793 item);
795 item_end--;
797 if (found_type == BTRFS_CSUM_ITEM_KEY) {
798 ret = btrfs_csum_truncate(trans, root, path,
799 inode->i_size);
800 BUG_ON(ret);
802 if (item_end < inode->i_size) {
803 if (found_type == BTRFS_DIR_ITEM_KEY) {
804 found_type = BTRFS_INODE_ITEM_KEY;
805 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
806 found_type = BTRFS_CSUM_ITEM_KEY;
807 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
808 found_type = BTRFS_XATTR_ITEM_KEY;
809 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
810 found_type = BTRFS_INODE_REF_KEY;
811 } else if (found_type) {
812 found_type--;
813 } else {
814 break;
816 btrfs_set_key_type(&key, found_type);
817 goto next;
819 if (found_key.offset >= inode->i_size)
820 del_item = 1;
821 else
822 del_item = 0;
823 found_extent = 0;
825 /* FIXME, shrink the extent if the ref count is only 1 */
826 if (found_type != BTRFS_EXTENT_DATA_KEY)
827 goto delete;
829 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
830 u64 num_dec;
831 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
832 if (!del_item) {
833 u64 orig_num_bytes =
834 btrfs_file_extent_num_bytes(leaf, fi);
835 extent_num_bytes = inode->i_size -
836 found_key.offset + root->sectorsize - 1;
837 extent_num_bytes = extent_num_bytes &
838 ~((u64)root->sectorsize - 1);
839 btrfs_set_file_extent_num_bytes(leaf, fi,
840 extent_num_bytes);
841 num_dec = (orig_num_bytes -
842 extent_num_bytes);
843 if (extent_start != 0)
844 dec_i_blocks(inode, num_dec);
845 btrfs_mark_buffer_dirty(leaf);
846 } else {
847 extent_num_bytes =
848 btrfs_file_extent_disk_num_bytes(leaf,
849 fi);
850 /* FIXME blocksize != 4096 */
851 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
852 if (extent_start != 0) {
853 found_extent = 1;
854 dec_i_blocks(inode, num_dec);
856 root_gen = btrfs_header_generation(leaf);
857 root_owner = btrfs_header_owner(leaf);
859 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
860 if (!del_item) {
861 u32 newsize = inode->i_size - found_key.offset;
862 dec_i_blocks(inode, item_end + 1 -
863 found_key.offset - newsize);
864 newsize =
865 btrfs_file_extent_calc_inline_size(newsize);
866 ret = btrfs_truncate_item(trans, root, path,
867 newsize, 1);
868 BUG_ON(ret);
869 } else {
870 dec_i_blocks(inode, item_end + 1 -
871 found_key.offset);
874 delete:
875 if (del_item) {
876 if (!pending_del_nr) {
877 /* no pending yet, add ourselves */
878 pending_del_slot = path->slots[0];
879 pending_del_nr = 1;
880 } else if (pending_del_nr &&
881 path->slots[0] + 1 == pending_del_slot) {
882 /* hop on the pending chunk */
883 pending_del_nr++;
884 pending_del_slot = path->slots[0];
885 } else {
886 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
888 } else {
889 break;
891 if (found_extent) {
892 ret = btrfs_free_extent(trans, root, extent_start,
893 extent_num_bytes,
894 root_owner,
895 root_gen, inode->i_ino,
896 found_key.offset, 0);
897 BUG_ON(ret);
899 next:
900 if (path->slots[0] == 0) {
901 if (pending_del_nr)
902 goto del_pending;
903 btrfs_release_path(root, path);
904 goto search_again;
907 path->slots[0]--;
908 if (pending_del_nr &&
909 path->slots[0] + 1 != pending_del_slot) {
910 struct btrfs_key debug;
911 del_pending:
912 btrfs_item_key_to_cpu(path->nodes[0], &debug,
913 pending_del_slot);
914 ret = btrfs_del_items(trans, root, path,
915 pending_del_slot,
916 pending_del_nr);
917 BUG_ON(ret);
918 pending_del_nr = 0;
919 btrfs_release_path(root, path);
920 goto search_again;
923 ret = 0;
924 error:
925 if (pending_del_nr) {
926 ret = btrfs_del_items(trans, root, path, pending_del_slot,
927 pending_del_nr);
929 btrfs_release_path(root, path);
930 btrfs_free_path(path);
931 inode->i_sb->s_dirt = 1;
932 return ret;
935 static int btrfs_cow_one_page(struct inode *inode, struct page *page,
936 size_t zero_start)
938 char *kaddr;
939 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
940 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
941 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
942 int ret = 0;
944 WARN_ON(!PageLocked(page));
945 set_page_extent_mapped(page);
947 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
948 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
949 page_end, GFP_NOFS);
951 if (zero_start != PAGE_CACHE_SIZE) {
952 kaddr = kmap(page);
953 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
954 flush_dcache_page(page);
955 kunmap(page);
957 set_page_dirty(page);
958 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
960 return ret;
964 * taken from block_truncate_page, but does cow as it zeros out
965 * any bytes left in the last page in the file.
967 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
969 struct inode *inode = mapping->host;
970 struct btrfs_root *root = BTRFS_I(inode)->root;
971 u32 blocksize = root->sectorsize;
972 pgoff_t index = from >> PAGE_CACHE_SHIFT;
973 unsigned offset = from & (PAGE_CACHE_SIZE-1);
974 struct page *page;
975 int ret = 0;
976 u64 page_start;
978 if ((offset & (blocksize - 1)) == 0)
979 goto out;
981 ret = -ENOMEM;
982 page = grab_cache_page(mapping, index);
983 if (!page)
984 goto out;
985 if (!PageUptodate(page)) {
986 ret = btrfs_readpage(NULL, page);
987 lock_page(page);
988 if (!PageUptodate(page)) {
989 ret = -EIO;
990 goto out;
993 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
995 ret = btrfs_cow_one_page(inode, page, offset);
997 unlock_page(page);
998 page_cache_release(page);
999 out:
1000 return ret;
1003 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1005 struct inode *inode = dentry->d_inode;
1006 int err;
1008 err = inode_change_ok(inode, attr);
1009 if (err)
1010 return err;
1012 if (S_ISREG(inode->i_mode) &&
1013 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1014 struct btrfs_trans_handle *trans;
1015 struct btrfs_root *root = BTRFS_I(inode)->root;
1016 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1018 u64 mask = root->sectorsize - 1;
1019 u64 hole_start = (inode->i_size + mask) & ~mask;
1020 u64 block_end = (attr->ia_size + mask) & ~mask;
1021 u64 hole_size;
1022 u64 alloc_hint = 0;
1024 if (attr->ia_size <= hole_start)
1025 goto out;
1027 mutex_lock(&root->fs_info->fs_mutex);
1028 err = btrfs_check_free_space(root, 1, 0);
1029 mutex_unlock(&root->fs_info->fs_mutex);
1030 if (err)
1031 goto fail;
1033 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1035 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1036 hole_size = block_end - hole_start;
1038 mutex_lock(&root->fs_info->fs_mutex);
1039 trans = btrfs_start_transaction(root, 1);
1040 btrfs_set_trans_block_group(trans, inode);
1041 err = btrfs_drop_extents(trans, root, inode,
1042 hole_start, block_end, hole_start,
1043 &alloc_hint);
1045 if (alloc_hint != EXTENT_MAP_INLINE) {
1046 err = btrfs_insert_file_extent(trans, root,
1047 inode->i_ino,
1048 hole_start, 0, 0,
1049 hole_size);
1050 btrfs_drop_extent_cache(inode, hole_start,
1051 hole_size - 1);
1052 btrfs_check_file(root, inode);
1054 btrfs_end_transaction(trans, root);
1055 mutex_unlock(&root->fs_info->fs_mutex);
1056 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1057 if (err)
1058 return err;
1060 out:
1061 err = inode_setattr(inode, attr);
1062 fail:
1063 return err;
1066 void btrfs_put_inode(struct inode *inode)
1068 int ret;
1070 if (!BTRFS_I(inode)->ordered_trans) {
1071 return;
1074 if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY) ||
1075 mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))
1076 return;
1078 ret = btrfs_del_ordered_inode(inode);
1079 if (ret == 1) {
1080 atomic_dec(&inode->i_count);
1084 void btrfs_delete_inode(struct inode *inode)
1086 struct btrfs_trans_handle *trans;
1087 struct btrfs_root *root = BTRFS_I(inode)->root;
1088 unsigned long nr;
1089 int ret;
1091 truncate_inode_pages(&inode->i_data, 0);
1092 if (is_bad_inode(inode)) {
1093 goto no_delete;
1096 inode->i_size = 0;
1097 mutex_lock(&root->fs_info->fs_mutex);
1098 trans = btrfs_start_transaction(root, 1);
1100 btrfs_set_trans_block_group(trans, inode);
1101 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
1102 if (ret)
1103 goto no_delete_lock;
1105 nr = trans->blocks_used;
1106 clear_inode(inode);
1108 btrfs_end_transaction(trans, root);
1109 mutex_unlock(&root->fs_info->fs_mutex);
1110 btrfs_btree_balance_dirty(root, nr);
1111 btrfs_throttle(root);
1112 return;
1114 no_delete_lock:
1115 nr = trans->blocks_used;
1116 btrfs_end_transaction(trans, root);
1117 mutex_unlock(&root->fs_info->fs_mutex);
1118 btrfs_btree_balance_dirty(root, nr);
1119 btrfs_throttle(root);
1120 no_delete:
1121 clear_inode(inode);
1125 * this returns the key found in the dir entry in the location pointer.
1126 * If no dir entries were found, location->objectid is 0.
1128 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1129 struct btrfs_key *location)
1131 const char *name = dentry->d_name.name;
1132 int namelen = dentry->d_name.len;
1133 struct btrfs_dir_item *di;
1134 struct btrfs_path *path;
1135 struct btrfs_root *root = BTRFS_I(dir)->root;
1136 int ret = 0;
1138 if (namelen == 1 && strcmp(name, ".") == 0) {
1139 location->objectid = dir->i_ino;
1140 location->type = BTRFS_INODE_ITEM_KEY;
1141 location->offset = 0;
1142 return 0;
1144 path = btrfs_alloc_path();
1145 BUG_ON(!path);
1147 if (namelen == 2 && strcmp(name, "..") == 0) {
1148 struct btrfs_key key;
1149 struct extent_buffer *leaf;
1150 u32 nritems;
1151 int slot;
1153 key.objectid = dir->i_ino;
1154 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1155 key.offset = 0;
1156 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1157 BUG_ON(ret == 0);
1158 ret = 0;
1160 leaf = path->nodes[0];
1161 slot = path->slots[0];
1162 nritems = btrfs_header_nritems(leaf);
1163 if (slot >= nritems)
1164 goto out_err;
1166 btrfs_item_key_to_cpu(leaf, &key, slot);
1167 if (key.objectid != dir->i_ino ||
1168 key.type != BTRFS_INODE_REF_KEY) {
1169 goto out_err;
1171 location->objectid = key.offset;
1172 location->type = BTRFS_INODE_ITEM_KEY;
1173 location->offset = 0;
1174 goto out;
1177 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1178 namelen, 0);
1179 if (IS_ERR(di))
1180 ret = PTR_ERR(di);
1181 if (!di || IS_ERR(di)) {
1182 goto out_err;
1184 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
1185 out:
1186 btrfs_free_path(path);
1187 return ret;
1188 out_err:
1189 location->objectid = 0;
1190 goto out;
1194 * when we hit a tree root in a directory, the btrfs part of the inode
1195 * needs to be changed to reflect the root directory of the tree root. This
1196 * is kind of like crossing a mount point.
1198 static int fixup_tree_root_location(struct btrfs_root *root,
1199 struct btrfs_key *location,
1200 struct btrfs_root **sub_root,
1201 struct dentry *dentry)
1203 struct btrfs_path *path;
1204 struct btrfs_root_item *ri;
1206 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1207 return 0;
1208 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1209 return 0;
1211 path = btrfs_alloc_path();
1212 BUG_ON(!path);
1213 mutex_lock(&root->fs_info->fs_mutex);
1215 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1216 dentry->d_name.name,
1217 dentry->d_name.len);
1218 if (IS_ERR(*sub_root))
1219 return PTR_ERR(*sub_root);
1221 ri = &(*sub_root)->root_item;
1222 location->objectid = btrfs_root_dirid(ri);
1223 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1224 location->offset = 0;
1226 btrfs_free_path(path);
1227 mutex_unlock(&root->fs_info->fs_mutex);
1228 return 0;
1231 static int btrfs_init_locked_inode(struct inode *inode, void *p)
1233 struct btrfs_iget_args *args = p;
1234 inode->i_ino = args->ino;
1235 BTRFS_I(inode)->root = args->root;
1236 BTRFS_I(inode)->delalloc_bytes = 0;
1237 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1238 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1239 inode->i_mapping, GFP_NOFS);
1240 return 0;
1243 static int btrfs_find_actor(struct inode *inode, void *opaque)
1245 struct btrfs_iget_args *args = opaque;
1246 return (args->ino == inode->i_ino &&
1247 args->root == BTRFS_I(inode)->root);
1250 struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1251 u64 root_objectid)
1253 struct btrfs_iget_args args;
1254 args.ino = objectid;
1255 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1257 if (!args.root)
1258 return NULL;
1260 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1263 struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1264 struct btrfs_root *root)
1266 struct inode *inode;
1267 struct btrfs_iget_args args;
1268 args.ino = objectid;
1269 args.root = root;
1271 inode = iget5_locked(s, objectid, btrfs_find_actor,
1272 btrfs_init_locked_inode,
1273 (void *)&args);
1274 return inode;
1277 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1278 struct nameidata *nd)
1280 struct inode * inode;
1281 struct btrfs_inode *bi = BTRFS_I(dir);
1282 struct btrfs_root *root = bi->root;
1283 struct btrfs_root *sub_root = root;
1284 struct btrfs_key location;
1285 int ret;
1287 if (dentry->d_name.len > BTRFS_NAME_LEN)
1288 return ERR_PTR(-ENAMETOOLONG);
1290 mutex_lock(&root->fs_info->fs_mutex);
1291 ret = btrfs_inode_by_name(dir, dentry, &location);
1292 mutex_unlock(&root->fs_info->fs_mutex);
1294 if (ret < 0)
1295 return ERR_PTR(ret);
1297 inode = NULL;
1298 if (location.objectid) {
1299 ret = fixup_tree_root_location(root, &location, &sub_root,
1300 dentry);
1301 if (ret < 0)
1302 return ERR_PTR(ret);
1303 if (ret > 0)
1304 return ERR_PTR(-ENOENT);
1305 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1306 sub_root);
1307 if (!inode)
1308 return ERR_PTR(-EACCES);
1309 if (inode->i_state & I_NEW) {
1310 /* the inode and parent dir are two different roots */
1311 if (sub_root != root) {
1312 igrab(inode);
1313 sub_root->inode = inode;
1315 BTRFS_I(inode)->root = sub_root;
1316 memcpy(&BTRFS_I(inode)->location, &location,
1317 sizeof(location));
1318 btrfs_read_locked_inode(inode);
1319 unlock_new_inode(inode);
1322 return d_splice_alias(inode, dentry);
1325 static unsigned char btrfs_filetype_table[] = {
1326 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1329 static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1331 struct inode *inode = filp->f_dentry->d_inode;
1332 struct btrfs_root *root = BTRFS_I(inode)->root;
1333 struct btrfs_item *item;
1334 struct btrfs_dir_item *di;
1335 struct btrfs_key key;
1336 struct btrfs_key found_key;
1337 struct btrfs_path *path;
1338 int ret;
1339 u32 nritems;
1340 struct extent_buffer *leaf;
1341 int slot;
1342 int advance;
1343 unsigned char d_type;
1344 int over = 0;
1345 u32 di_cur;
1346 u32 di_total;
1347 u32 di_len;
1348 int key_type = BTRFS_DIR_INDEX_KEY;
1349 char tmp_name[32];
1350 char *name_ptr;
1351 int name_len;
1353 /* FIXME, use a real flag for deciding about the key type */
1354 if (root->fs_info->tree_root == root)
1355 key_type = BTRFS_DIR_ITEM_KEY;
1357 /* special case for "." */
1358 if (filp->f_pos == 0) {
1359 over = filldir(dirent, ".", 1,
1360 1, inode->i_ino,
1361 DT_DIR);
1362 if (over)
1363 return 0;
1364 filp->f_pos = 1;
1367 mutex_lock(&root->fs_info->fs_mutex);
1368 key.objectid = inode->i_ino;
1369 path = btrfs_alloc_path();
1370 path->reada = 2;
1372 /* special case for .., just use the back ref */
1373 if (filp->f_pos == 1) {
1374 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1375 key.offset = 0;
1376 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1377 BUG_ON(ret == 0);
1378 leaf = path->nodes[0];
1379 slot = path->slots[0];
1380 nritems = btrfs_header_nritems(leaf);
1381 if (slot >= nritems) {
1382 btrfs_release_path(root, path);
1383 goto read_dir_items;
1385 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1386 btrfs_release_path(root, path);
1387 if (found_key.objectid != key.objectid ||
1388 found_key.type != BTRFS_INODE_REF_KEY)
1389 goto read_dir_items;
1390 over = filldir(dirent, "..", 2,
1391 2, found_key.offset, DT_DIR);
1392 if (over)
1393 goto nopos;
1394 filp->f_pos = 2;
1397 read_dir_items:
1398 btrfs_set_key_type(&key, key_type);
1399 key.offset = filp->f_pos;
1401 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1402 if (ret < 0)
1403 goto err;
1404 advance = 0;
1405 while(1) {
1406 leaf = path->nodes[0];
1407 nritems = btrfs_header_nritems(leaf);
1408 slot = path->slots[0];
1409 if (advance || slot >= nritems) {
1410 if (slot >= nritems -1) {
1411 ret = btrfs_next_leaf(root, path);
1412 if (ret)
1413 break;
1414 leaf = path->nodes[0];
1415 nritems = btrfs_header_nritems(leaf);
1416 slot = path->slots[0];
1417 } else {
1418 slot++;
1419 path->slots[0]++;
1422 advance = 1;
1423 item = btrfs_item_nr(leaf, slot);
1424 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1426 if (found_key.objectid != key.objectid)
1427 break;
1428 if (btrfs_key_type(&found_key) != key_type)
1429 break;
1430 if (found_key.offset < filp->f_pos)
1431 continue;
1433 filp->f_pos = found_key.offset;
1434 advance = 1;
1435 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1436 di_cur = 0;
1437 di_total = btrfs_item_size(leaf, item);
1438 while(di_cur < di_total) {
1439 struct btrfs_key location;
1441 name_len = btrfs_dir_name_len(leaf, di);
1442 if (name_len < 32) {
1443 name_ptr = tmp_name;
1444 } else {
1445 name_ptr = kmalloc(name_len, GFP_NOFS);
1446 BUG_ON(!name_ptr);
1448 read_extent_buffer(leaf, name_ptr,
1449 (unsigned long)(di + 1), name_len);
1451 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1452 btrfs_dir_item_key_to_cpu(leaf, di, &location);
1453 over = filldir(dirent, name_ptr, name_len,
1454 found_key.offset,
1455 location.objectid,
1456 d_type);
1458 if (name_ptr != tmp_name)
1459 kfree(name_ptr);
1461 if (over)
1462 goto nopos;
1463 di_len = btrfs_dir_name_len(leaf, di) +
1464 btrfs_dir_data_len(leaf, di) +sizeof(*di);
1465 di_cur += di_len;
1466 di = (struct btrfs_dir_item *)((char *)di + di_len);
1469 if (key_type == BTRFS_DIR_INDEX_KEY)
1470 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
1471 else
1472 filp->f_pos++;
1473 nopos:
1474 ret = 0;
1475 err:
1476 btrfs_release_path(root, path);
1477 btrfs_free_path(path);
1478 mutex_unlock(&root->fs_info->fs_mutex);
1479 return ret;
1482 int btrfs_write_inode(struct inode *inode, int wait)
1484 struct btrfs_root *root = BTRFS_I(inode)->root;
1485 struct btrfs_trans_handle *trans;
1486 int ret = 0;
1488 if (wait) {
1489 mutex_lock(&root->fs_info->fs_mutex);
1490 trans = btrfs_start_transaction(root, 1);
1491 btrfs_set_trans_block_group(trans, inode);
1492 ret = btrfs_commit_transaction(trans, root);
1493 mutex_unlock(&root->fs_info->fs_mutex);
1495 return ret;
1499 * This is somewhat expensive, updating the tree every time the
1500 * inode changes. But, it is most likely to find the inode in cache.
1501 * FIXME, needs more benchmarking...there are no reasons other than performance
1502 * to keep or drop this code.
1504 void btrfs_dirty_inode(struct inode *inode)
1506 struct btrfs_root *root = BTRFS_I(inode)->root;
1507 struct btrfs_trans_handle *trans;
1509 mutex_lock(&root->fs_info->fs_mutex);
1510 trans = btrfs_start_transaction(root, 1);
1511 btrfs_set_trans_block_group(trans, inode);
1512 btrfs_update_inode(trans, root, inode);
1513 btrfs_end_transaction(trans, root);
1514 mutex_unlock(&root->fs_info->fs_mutex);
1517 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1518 struct btrfs_root *root,
1519 const char *name, int name_len,
1520 u64 ref_objectid,
1521 u64 objectid,
1522 struct btrfs_block_group_cache *group,
1523 int mode)
1525 struct inode *inode;
1526 struct btrfs_inode_item *inode_item;
1527 struct btrfs_key *location;
1528 struct btrfs_path *path;
1529 struct btrfs_inode_ref *ref;
1530 struct btrfs_key key[2];
1531 u32 sizes[2];
1532 unsigned long ptr;
1533 int ret;
1534 int owner;
1536 path = btrfs_alloc_path();
1537 BUG_ON(!path);
1539 inode = new_inode(root->fs_info->sb);
1540 if (!inode)
1541 return ERR_PTR(-ENOMEM);
1543 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1544 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1545 inode->i_mapping, GFP_NOFS);
1546 BTRFS_I(inode)->delalloc_bytes = 0;
1547 BTRFS_I(inode)->root = root;
1549 if (mode & S_IFDIR)
1550 owner = 0;
1551 else
1552 owner = 1;
1553 group = btrfs_find_block_group(root, group, 0, 0, owner);
1554 BTRFS_I(inode)->block_group = group;
1555 BTRFS_I(inode)->flags = 0;
1557 key[0].objectid = objectid;
1558 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
1559 key[0].offset = 0;
1561 key[1].objectid = objectid;
1562 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
1563 key[1].offset = ref_objectid;
1565 sizes[0] = sizeof(struct btrfs_inode_item);
1566 sizes[1] = name_len + sizeof(*ref);
1568 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
1569 if (ret != 0)
1570 goto fail;
1572 if (objectid > root->highest_inode)
1573 root->highest_inode = objectid;
1575 inode->i_uid = current->fsuid;
1576 inode->i_gid = current->fsgid;
1577 inode->i_mode = mode;
1578 inode->i_ino = objectid;
1579 inode->i_blocks = 0;
1580 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1581 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1582 struct btrfs_inode_item);
1583 fill_inode_item(path->nodes[0], inode_item, inode);
1585 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
1586 struct btrfs_inode_ref);
1587 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
1588 ptr = (unsigned long)(ref + 1);
1589 write_extent_buffer(path->nodes[0], name, ptr, name_len);
1591 btrfs_mark_buffer_dirty(path->nodes[0]);
1592 btrfs_free_path(path);
1594 location = &BTRFS_I(inode)->location;
1595 location->objectid = objectid;
1596 location->offset = 0;
1597 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1599 insert_inode_hash(inode);
1600 return inode;
1601 fail:
1602 btrfs_free_path(path);
1603 return ERR_PTR(ret);
1606 static inline u8 btrfs_inode_type(struct inode *inode)
1608 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1611 static int btrfs_add_link(struct btrfs_trans_handle *trans,
1612 struct dentry *dentry, struct inode *inode,
1613 int add_backref)
1615 int ret;
1616 struct btrfs_key key;
1617 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
1618 struct inode *parent_inode;
1620 key.objectid = inode->i_ino;
1621 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1622 key.offset = 0;
1624 ret = btrfs_insert_dir_item(trans, root,
1625 dentry->d_name.name, dentry->d_name.len,
1626 dentry->d_parent->d_inode->i_ino,
1627 &key, btrfs_inode_type(inode));
1628 if (ret == 0) {
1629 if (add_backref) {
1630 ret = btrfs_insert_inode_ref(trans, root,
1631 dentry->d_name.name,
1632 dentry->d_name.len,
1633 inode->i_ino,
1634 dentry->d_parent->d_inode->i_ino);
1636 parent_inode = dentry->d_parent->d_inode;
1637 parent_inode->i_size += dentry->d_name.len * 2;
1638 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
1639 ret = btrfs_update_inode(trans, root,
1640 dentry->d_parent->d_inode);
1642 return ret;
1645 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1646 struct dentry *dentry, struct inode *inode,
1647 int backref)
1649 int err = btrfs_add_link(trans, dentry, inode, backref);
1650 if (!err) {
1651 d_instantiate(dentry, inode);
1652 return 0;
1654 if (err > 0)
1655 err = -EEXIST;
1656 return err;
1659 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1660 int mode, dev_t rdev)
1662 struct btrfs_trans_handle *trans;
1663 struct btrfs_root *root = BTRFS_I(dir)->root;
1664 struct inode *inode = NULL;
1665 int err;
1666 int drop_inode = 0;
1667 u64 objectid;
1668 unsigned long nr = 0;
1670 if (!new_valid_dev(rdev))
1671 return -EINVAL;
1673 mutex_lock(&root->fs_info->fs_mutex);
1674 err = btrfs_check_free_space(root, 1, 0);
1675 if (err)
1676 goto fail;
1678 trans = btrfs_start_transaction(root, 1);
1679 btrfs_set_trans_block_group(trans, dir);
1681 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1682 if (err) {
1683 err = -ENOSPC;
1684 goto out_unlock;
1687 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1688 dentry->d_name.len,
1689 dentry->d_parent->d_inode->i_ino, objectid,
1690 BTRFS_I(dir)->block_group, mode);
1691 err = PTR_ERR(inode);
1692 if (IS_ERR(inode))
1693 goto out_unlock;
1695 btrfs_set_trans_block_group(trans, inode);
1696 err = btrfs_add_nondir(trans, dentry, inode, 0);
1697 if (err)
1698 drop_inode = 1;
1699 else {
1700 inode->i_op = &btrfs_special_inode_operations;
1701 init_special_inode(inode, inode->i_mode, rdev);
1702 btrfs_update_inode(trans, root, inode);
1704 dir->i_sb->s_dirt = 1;
1705 btrfs_update_inode_block_group(trans, inode);
1706 btrfs_update_inode_block_group(trans, dir);
1707 out_unlock:
1708 nr = trans->blocks_used;
1709 btrfs_end_transaction(trans, root);
1710 fail:
1711 mutex_unlock(&root->fs_info->fs_mutex);
1713 if (drop_inode) {
1714 inode_dec_link_count(inode);
1715 iput(inode);
1717 btrfs_btree_balance_dirty(root, nr);
1718 btrfs_throttle(root);
1719 return err;
1722 static int btrfs_create(struct inode *dir, struct dentry *dentry,
1723 int mode, struct nameidata *nd)
1725 struct btrfs_trans_handle *trans;
1726 struct btrfs_root *root = BTRFS_I(dir)->root;
1727 struct inode *inode = NULL;
1728 int err;
1729 int drop_inode = 0;
1730 unsigned long nr = 0;
1731 u64 objectid;
1733 mutex_lock(&root->fs_info->fs_mutex);
1734 err = btrfs_check_free_space(root, 1, 0);
1735 if (err)
1736 goto fail;
1737 trans = btrfs_start_transaction(root, 1);
1738 btrfs_set_trans_block_group(trans, dir);
1740 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1741 if (err) {
1742 err = -ENOSPC;
1743 goto out_unlock;
1746 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1747 dentry->d_name.len,
1748 dentry->d_parent->d_inode->i_ino,
1749 objectid, BTRFS_I(dir)->block_group, mode);
1750 err = PTR_ERR(inode);
1751 if (IS_ERR(inode))
1752 goto out_unlock;
1754 btrfs_set_trans_block_group(trans, inode);
1755 err = btrfs_add_nondir(trans, dentry, inode, 0);
1756 if (err)
1757 drop_inode = 1;
1758 else {
1759 inode->i_mapping->a_ops = &btrfs_aops;
1760 inode->i_fop = &btrfs_file_operations;
1761 inode->i_op = &btrfs_file_inode_operations;
1762 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1763 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1764 inode->i_mapping, GFP_NOFS);
1765 BTRFS_I(inode)->delalloc_bytes = 0;
1766 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
1768 dir->i_sb->s_dirt = 1;
1769 btrfs_update_inode_block_group(trans, inode);
1770 btrfs_update_inode_block_group(trans, dir);
1771 out_unlock:
1772 nr = trans->blocks_used;
1773 btrfs_end_transaction(trans, root);
1774 fail:
1775 mutex_unlock(&root->fs_info->fs_mutex);
1777 if (drop_inode) {
1778 inode_dec_link_count(inode);
1779 iput(inode);
1781 btrfs_btree_balance_dirty(root, nr);
1782 btrfs_throttle(root);
1783 return err;
1786 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1787 struct dentry *dentry)
1789 struct btrfs_trans_handle *trans;
1790 struct btrfs_root *root = BTRFS_I(dir)->root;
1791 struct inode *inode = old_dentry->d_inode;
1792 unsigned long nr = 0;
1793 int err;
1794 int drop_inode = 0;
1796 if (inode->i_nlink == 0)
1797 return -ENOENT;
1799 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1800 inode->i_nlink++;
1801 #else
1802 inc_nlink(inode);
1803 #endif
1804 mutex_lock(&root->fs_info->fs_mutex);
1805 err = btrfs_check_free_space(root, 1, 0);
1806 if (err)
1807 goto fail;
1808 trans = btrfs_start_transaction(root, 1);
1810 btrfs_set_trans_block_group(trans, dir);
1811 atomic_inc(&inode->i_count);
1812 err = btrfs_add_nondir(trans, dentry, inode, 1);
1814 if (err)
1815 drop_inode = 1;
1817 dir->i_sb->s_dirt = 1;
1818 btrfs_update_inode_block_group(trans, dir);
1819 err = btrfs_update_inode(trans, root, inode);
1821 if (err)
1822 drop_inode = 1;
1824 nr = trans->blocks_used;
1825 btrfs_end_transaction(trans, root);
1826 fail:
1827 mutex_unlock(&root->fs_info->fs_mutex);
1829 if (drop_inode) {
1830 inode_dec_link_count(inode);
1831 iput(inode);
1833 btrfs_btree_balance_dirty(root, nr);
1834 btrfs_throttle(root);
1835 return err;
1838 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1840 struct inode *inode;
1841 struct btrfs_trans_handle *trans;
1842 struct btrfs_root *root = BTRFS_I(dir)->root;
1843 int err = 0;
1844 int drop_on_err = 0;
1845 u64 objectid;
1846 unsigned long nr = 1;
1848 mutex_lock(&root->fs_info->fs_mutex);
1849 err = btrfs_check_free_space(root, 1, 0);
1850 if (err)
1851 goto out_unlock;
1853 trans = btrfs_start_transaction(root, 1);
1854 btrfs_set_trans_block_group(trans, dir);
1856 if (IS_ERR(trans)) {
1857 err = PTR_ERR(trans);
1858 goto out_unlock;
1861 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1862 if (err) {
1863 err = -ENOSPC;
1864 goto out_unlock;
1867 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1868 dentry->d_name.len,
1869 dentry->d_parent->d_inode->i_ino, objectid,
1870 BTRFS_I(dir)->block_group, S_IFDIR | mode);
1871 if (IS_ERR(inode)) {
1872 err = PTR_ERR(inode);
1873 goto out_fail;
1876 drop_on_err = 1;
1877 inode->i_op = &btrfs_dir_inode_operations;
1878 inode->i_fop = &btrfs_dir_file_operations;
1879 btrfs_set_trans_block_group(trans, inode);
1881 inode->i_size = 0;
1882 err = btrfs_update_inode(trans, root, inode);
1883 if (err)
1884 goto out_fail;
1886 err = btrfs_add_link(trans, dentry, inode, 0);
1887 if (err)
1888 goto out_fail;
1890 d_instantiate(dentry, inode);
1891 drop_on_err = 0;
1892 dir->i_sb->s_dirt = 1;
1893 btrfs_update_inode_block_group(trans, inode);
1894 btrfs_update_inode_block_group(trans, dir);
1896 out_fail:
1897 nr = trans->blocks_used;
1898 btrfs_end_transaction(trans, root);
1900 out_unlock:
1901 mutex_unlock(&root->fs_info->fs_mutex);
1902 if (drop_on_err)
1903 iput(inode);
1904 btrfs_btree_balance_dirty(root, nr);
1905 btrfs_throttle(root);
1906 return err;
1909 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
1910 size_t pg_offset, u64 start, u64 len,
1911 int create)
1913 int ret;
1914 int err = 0;
1915 u64 bytenr;
1916 u64 extent_start = 0;
1917 u64 extent_end = 0;
1918 u64 objectid = inode->i_ino;
1919 u32 found_type;
1920 struct btrfs_path *path;
1921 struct btrfs_root *root = BTRFS_I(inode)->root;
1922 struct btrfs_file_extent_item *item;
1923 struct extent_buffer *leaf;
1924 struct btrfs_key found_key;
1925 struct extent_map *em = NULL;
1926 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1927 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1928 struct btrfs_trans_handle *trans = NULL;
1930 path = btrfs_alloc_path();
1931 BUG_ON(!path);
1932 mutex_lock(&root->fs_info->fs_mutex);
1934 again:
1935 spin_lock(&em_tree->lock);
1936 em = lookup_extent_mapping(em_tree, start, len);
1937 spin_unlock(&em_tree->lock);
1939 if (em) {
1940 if (em->start > start) {
1941 printk("get_extent lookup [%Lu %Lu] em [%Lu %Lu]\n",
1942 start, len, em->start, em->len);
1943 WARN_ON(1);
1945 if (em->block_start == EXTENT_MAP_INLINE && page)
1946 free_extent_map(em);
1947 else
1948 goto out;
1950 em = alloc_extent_map(GFP_NOFS);
1951 if (!em) {
1952 err = -ENOMEM;
1953 goto out;
1956 em->start = EXTENT_MAP_HOLE;
1957 em->len = (u64)-1;
1958 em->bdev = inode->i_sb->s_bdev;
1959 ret = btrfs_lookup_file_extent(trans, root, path,
1960 objectid, start, trans != NULL);
1961 if (ret < 0) {
1962 err = ret;
1963 goto out;
1966 if (ret != 0) {
1967 if (path->slots[0] == 0)
1968 goto not_found;
1969 path->slots[0]--;
1972 leaf = path->nodes[0];
1973 item = btrfs_item_ptr(leaf, path->slots[0],
1974 struct btrfs_file_extent_item);
1975 /* are we inside the extent that was found? */
1976 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1977 found_type = btrfs_key_type(&found_key);
1978 if (found_key.objectid != objectid ||
1979 found_type != BTRFS_EXTENT_DATA_KEY) {
1980 goto not_found;
1983 found_type = btrfs_file_extent_type(leaf, item);
1984 extent_start = found_key.offset;
1985 if (found_type == BTRFS_FILE_EXTENT_REG) {
1986 extent_end = extent_start +
1987 btrfs_file_extent_num_bytes(leaf, item);
1988 err = 0;
1989 if (start < extent_start || start >= extent_end) {
1990 em->start = start;
1991 if (start < extent_start) {
1992 if (start + len <= extent_start)
1993 goto not_found;
1994 em->len = extent_end - extent_start;
1995 } else {
1996 em->len = len;
1998 goto not_found_em;
2000 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2001 if (bytenr == 0) {
2002 em->start = extent_start;
2003 em->len = extent_end - extent_start;
2004 em->block_start = EXTENT_MAP_HOLE;
2005 goto insert;
2007 bytenr += btrfs_file_extent_offset(leaf, item);
2008 em->block_start = bytenr;
2009 em->start = extent_start;
2010 em->len = extent_end - extent_start;
2011 goto insert;
2012 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
2013 u64 page_start;
2014 unsigned long ptr;
2015 char *map;
2016 size_t size;
2017 size_t extent_offset;
2018 size_t copy_size;
2020 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2021 path->slots[0]));
2022 extent_end = (extent_start + size + root->sectorsize - 1) &
2023 ~((u64)root->sectorsize - 1);
2024 if (start < extent_start || start >= extent_end) {
2025 em->start = start;
2026 if (start < extent_start) {
2027 if (start + len <= extent_start)
2028 goto not_found;
2029 em->len = extent_end - extent_start;
2030 } else {
2031 em->len = len;
2033 goto not_found_em;
2035 em->block_start = EXTENT_MAP_INLINE;
2037 if (!page) {
2038 em->start = extent_start;
2039 em->len = size;
2040 goto out;
2043 page_start = page_offset(page) + pg_offset;
2044 extent_offset = page_start - extent_start;
2045 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
2046 size - extent_offset);
2047 em->start = extent_start + extent_offset;
2048 em->len = (copy_size + root->sectorsize - 1) &
2049 ~((u64)root->sectorsize - 1);
2050 map = kmap(page);
2051 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
2052 if (create == 0 && !PageUptodate(page)) {
2053 read_extent_buffer(leaf, map + pg_offset, ptr,
2054 copy_size);
2055 flush_dcache_page(page);
2056 } else if (create && PageUptodate(page)) {
2057 if (!trans) {
2058 kunmap(page);
2059 free_extent_map(em);
2060 em = NULL;
2061 btrfs_release_path(root, path);
2062 trans = btrfs_start_transaction(root, 1);
2063 goto again;
2065 write_extent_buffer(leaf, map + pg_offset, ptr,
2066 copy_size);
2067 btrfs_mark_buffer_dirty(leaf);
2069 kunmap(page);
2070 set_extent_uptodate(io_tree, em->start,
2071 extent_map_end(em) - 1, GFP_NOFS);
2072 goto insert;
2073 } else {
2074 printk("unkknown found_type %d\n", found_type);
2075 WARN_ON(1);
2077 not_found:
2078 em->start = start;
2079 em->len = len;
2080 not_found_em:
2081 em->block_start = EXTENT_MAP_HOLE;
2082 insert:
2083 btrfs_release_path(root, path);
2084 if (em->start > start || extent_map_end(em) <= start) {
2085 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
2086 err = -EIO;
2087 goto out;
2090 err = 0;
2091 spin_lock(&em_tree->lock);
2092 ret = add_extent_mapping(em_tree, em);
2093 if (ret == -EEXIST) {
2094 free_extent_map(em);
2095 em = lookup_extent_mapping(em_tree, start, len);
2096 if (!em) {
2097 err = -EIO;
2098 printk("failing to insert %Lu %Lu\n", start, len);
2101 spin_unlock(&em_tree->lock);
2102 out:
2103 btrfs_free_path(path);
2104 if (trans) {
2105 ret = btrfs_end_transaction(trans, root);
2106 if (!err)
2107 err = ret;
2109 mutex_unlock(&root->fs_info->fs_mutex);
2110 if (err) {
2111 free_extent_map(em);
2112 WARN_ON(1);
2113 return ERR_PTR(err);
2115 return em;
2118 static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
2120 return extent_bmap(mapping, iblock, btrfs_get_extent);
2123 int btrfs_readpage(struct file *file, struct page *page)
2125 struct extent_io_tree *tree;
2126 tree = &BTRFS_I(page->mapping->host)->io_tree;
2127 return extent_read_full_page(tree, page, btrfs_get_extent);
2130 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2132 struct extent_io_tree *tree;
2135 if (current->flags & PF_MEMALLOC) {
2136 redirty_page_for_writepage(wbc, page);
2137 unlock_page(page);
2138 return 0;
2140 tree = &BTRFS_I(page->mapping->host)->io_tree;
2141 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2144 static int btrfs_writepages(struct address_space *mapping,
2145 struct writeback_control *wbc)
2147 struct extent_io_tree *tree;
2148 tree = &BTRFS_I(mapping->host)->io_tree;
2149 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2152 static int
2153 btrfs_readpages(struct file *file, struct address_space *mapping,
2154 struct list_head *pages, unsigned nr_pages)
2156 struct extent_io_tree *tree;
2157 tree = &BTRFS_I(mapping->host)->io_tree;
2158 return extent_readpages(tree, mapping, pages, nr_pages,
2159 btrfs_get_extent);
2162 static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
2164 struct extent_io_tree *tree;
2165 struct extent_map_tree *map;
2166 int ret;
2168 tree = &BTRFS_I(page->mapping->host)->io_tree;
2169 map = &BTRFS_I(page->mapping->host)->extent_tree;
2170 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
2171 if (ret == 1) {
2172 ClearPagePrivate(page);
2173 set_page_private(page, 0);
2174 page_cache_release(page);
2176 return ret;
2179 static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2181 struct extent_io_tree *tree;
2183 tree = &BTRFS_I(page->mapping->host)->io_tree;
2184 extent_invalidatepage(tree, page, offset);
2185 btrfs_releasepage(page, GFP_NOFS);
2189 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2190 * called from a page fault handler when a page is first dirtied. Hence we must
2191 * be careful to check for EOF conditions here. We set the page up correctly
2192 * for a written page which means we get ENOSPC checking when writing into
2193 * holes and correct delalloc and unwritten extent mapping on filesystems that
2194 * support these features.
2196 * We are not allowed to take the i_mutex here so we have to play games to
2197 * protect against truncate races as the page could now be beyond EOF. Because
2198 * vmtruncate() writes the inode size before removing pages, once we have the
2199 * page lock we can determine safely if the page is beyond EOF. If it is not
2200 * beyond EOF, then the page is guaranteed safe against truncation until we
2201 * unlock the page.
2203 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2205 struct inode *inode = fdentry(vma->vm_file)->d_inode;
2206 struct btrfs_root *root = BTRFS_I(inode)->root;
2207 unsigned long end;
2208 loff_t size;
2209 int ret;
2210 u64 page_start;
2212 mutex_lock(&root->fs_info->fs_mutex);
2213 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
2214 mutex_unlock(&root->fs_info->fs_mutex);
2215 if (ret)
2216 goto out;
2218 ret = -EINVAL;
2220 lock_page(page);
2221 wait_on_page_writeback(page);
2222 size = i_size_read(inode);
2223 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2225 if ((page->mapping != inode->i_mapping) ||
2226 (page_start > size)) {
2227 /* page got truncated out from underneath us */
2228 goto out_unlock;
2231 /* page is wholly or partially inside EOF */
2232 if (page_start + PAGE_CACHE_SIZE > size)
2233 end = size & ~PAGE_CACHE_MASK;
2234 else
2235 end = PAGE_CACHE_SIZE;
2237 ret = btrfs_cow_one_page(inode, page, end);
2239 out_unlock:
2240 unlock_page(page);
2241 out:
2242 return ret;
2245 static void btrfs_truncate(struct inode *inode)
2247 struct btrfs_root *root = BTRFS_I(inode)->root;
2248 int ret;
2249 struct btrfs_trans_handle *trans;
2250 unsigned long nr;
2252 if (!S_ISREG(inode->i_mode))
2253 return;
2254 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2255 return;
2257 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2259 mutex_lock(&root->fs_info->fs_mutex);
2260 trans = btrfs_start_transaction(root, 1);
2261 btrfs_set_trans_block_group(trans, inode);
2263 /* FIXME, add redo link to tree so we don't leak on crash */
2264 ret = btrfs_truncate_in_trans(trans, root, inode,
2265 BTRFS_EXTENT_DATA_KEY);
2266 btrfs_update_inode(trans, root, inode);
2267 nr = trans->blocks_used;
2269 ret = btrfs_end_transaction(trans, root);
2270 BUG_ON(ret);
2271 mutex_unlock(&root->fs_info->fs_mutex);
2272 btrfs_btree_balance_dirty(root, nr);
2273 btrfs_throttle(root);
2276 static int noinline create_subvol(struct btrfs_root *root, char *name,
2277 int namelen)
2279 struct btrfs_trans_handle *trans;
2280 struct btrfs_key key;
2281 struct btrfs_root_item root_item;
2282 struct btrfs_inode_item *inode_item;
2283 struct extent_buffer *leaf;
2284 struct btrfs_root *new_root = root;
2285 struct inode *inode;
2286 struct inode *dir;
2287 int ret;
2288 int err;
2289 u64 objectid;
2290 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
2291 unsigned long nr = 1;
2293 mutex_lock(&root->fs_info->fs_mutex);
2294 ret = btrfs_check_free_space(root, 1, 0);
2295 if (ret)
2296 goto fail_commit;
2298 trans = btrfs_start_transaction(root, 1);
2299 BUG_ON(!trans);
2301 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2302 0, &objectid);
2303 if (ret)
2304 goto fail;
2306 leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
2307 objectid, trans->transid, 0, 0,
2308 0, 0);
2309 if (IS_ERR(leaf))
2310 return PTR_ERR(leaf);
2312 btrfs_set_header_nritems(leaf, 0);
2313 btrfs_set_header_level(leaf, 0);
2314 btrfs_set_header_bytenr(leaf, leaf->start);
2315 btrfs_set_header_generation(leaf, trans->transid);
2316 btrfs_set_header_owner(leaf, objectid);
2318 write_extent_buffer(leaf, root->fs_info->fsid,
2319 (unsigned long)btrfs_header_fsid(leaf),
2320 BTRFS_FSID_SIZE);
2321 btrfs_mark_buffer_dirty(leaf);
2323 inode_item = &root_item.inode;
2324 memset(inode_item, 0, sizeof(*inode_item));
2325 inode_item->generation = cpu_to_le64(1);
2326 inode_item->size = cpu_to_le64(3);
2327 inode_item->nlink = cpu_to_le32(1);
2328 inode_item->nblocks = cpu_to_le64(1);
2329 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
2331 btrfs_set_root_bytenr(&root_item, leaf->start);
2332 btrfs_set_root_level(&root_item, 0);
2333 btrfs_set_root_refs(&root_item, 1);
2334 btrfs_set_root_used(&root_item, 0);
2336 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2337 root_item.drop_level = 0;
2339 free_extent_buffer(leaf);
2340 leaf = NULL;
2342 btrfs_set_root_dirid(&root_item, new_dirid);
2344 key.objectid = objectid;
2345 key.offset = 1;
2346 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2347 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2348 &root_item);
2349 if (ret)
2350 goto fail;
2353 * insert the directory item
2355 key.offset = (u64)-1;
2356 dir = root->fs_info->sb->s_root->d_inode;
2357 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2358 name, namelen, dir->i_ino, &key,
2359 BTRFS_FT_DIR);
2360 if (ret)
2361 goto fail;
2363 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2364 name, namelen, objectid,
2365 root->fs_info->sb->s_root->d_inode->i_ino);
2366 if (ret)
2367 goto fail;
2369 ret = btrfs_commit_transaction(trans, root);
2370 if (ret)
2371 goto fail_commit;
2373 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
2374 BUG_ON(!new_root);
2376 trans = btrfs_start_transaction(new_root, 1);
2377 BUG_ON(!trans);
2379 inode = btrfs_new_inode(trans, new_root, "..", 2, new_dirid,
2380 new_dirid,
2381 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
2382 if (IS_ERR(inode))
2383 goto fail;
2384 inode->i_op = &btrfs_dir_inode_operations;
2385 inode->i_fop = &btrfs_dir_file_operations;
2386 new_root->inode = inode;
2388 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2389 new_dirid);
2390 inode->i_nlink = 1;
2391 inode->i_size = 0;
2392 ret = btrfs_update_inode(trans, new_root, inode);
2393 if (ret)
2394 goto fail;
2395 fail:
2396 nr = trans->blocks_used;
2397 err = btrfs_commit_transaction(trans, new_root);
2398 if (err && !ret)
2399 ret = err;
2400 fail_commit:
2401 mutex_unlock(&root->fs_info->fs_mutex);
2402 btrfs_btree_balance_dirty(root, nr);
2403 btrfs_throttle(root);
2404 return ret;
2407 static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2409 struct btrfs_pending_snapshot *pending_snapshot;
2410 struct btrfs_trans_handle *trans;
2411 int ret;
2412 int err;
2413 unsigned long nr = 0;
2415 if (!root->ref_cows)
2416 return -EINVAL;
2418 mutex_lock(&root->fs_info->fs_mutex);
2419 ret = btrfs_check_free_space(root, 1, 0);
2420 if (ret)
2421 goto fail_unlock;
2423 pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS);
2424 if (!pending_snapshot) {
2425 ret = -ENOMEM;
2426 goto fail_unlock;
2428 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
2429 if (!pending_snapshot->name) {
2430 ret = -ENOMEM;
2431 kfree(pending_snapshot);
2432 goto fail_unlock;
2434 memcpy(pending_snapshot->name, name, namelen);
2435 pending_snapshot->name[namelen] = '\0';
2436 trans = btrfs_start_transaction(root, 1);
2437 BUG_ON(!trans);
2438 pending_snapshot->root = root;
2439 list_add(&pending_snapshot->list,
2440 &trans->transaction->pending_snapshots);
2441 ret = btrfs_update_inode(trans, root, root->inode);
2442 err = btrfs_commit_transaction(trans, root);
2444 fail_unlock:
2445 mutex_unlock(&root->fs_info->fs_mutex);
2446 btrfs_btree_balance_dirty(root, nr);
2447 btrfs_throttle(root);
2448 return ret;
2451 unsigned long btrfs_force_ra(struct address_space *mapping,
2452 struct file_ra_state *ra, struct file *file,
2453 pgoff_t offset, pgoff_t last_index)
2455 pgoff_t req_size;
2457 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2458 req_size = last_index - offset + 1;
2459 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2460 return offset;
2461 #else
2462 req_size = min(last_index - offset + 1, (pgoff_t)128);
2463 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2464 return offset + req_size;
2465 #endif
2468 int btrfs_defrag_file(struct file *file) {
2469 struct inode *inode = fdentry(file)->d_inode;
2470 struct btrfs_root *root = BTRFS_I(inode)->root;
2471 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2472 struct page *page;
2473 unsigned long last_index;
2474 unsigned long ra_index = 0;
2475 u64 page_start;
2476 u64 page_end;
2477 unsigned long i;
2478 int ret;
2480 mutex_lock(&root->fs_info->fs_mutex);
2481 ret = btrfs_check_free_space(root, inode->i_size, 0);
2482 mutex_unlock(&root->fs_info->fs_mutex);
2483 if (ret)
2484 return -ENOSPC;
2486 mutex_lock(&inode->i_mutex);
2487 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2488 for (i = 0; i <= last_index; i++) {
2489 if (i == ra_index) {
2490 ra_index = btrfs_force_ra(inode->i_mapping,
2491 &file->f_ra,
2492 file, ra_index, last_index);
2494 page = grab_cache_page(inode->i_mapping, i);
2495 if (!page)
2496 goto out_unlock;
2497 if (!PageUptodate(page)) {
2498 btrfs_readpage(NULL, page);
2499 lock_page(page);
2500 if (!PageUptodate(page)) {
2501 unlock_page(page);
2502 page_cache_release(page);
2503 goto out_unlock;
2506 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2507 page_end = page_start + PAGE_CACHE_SIZE - 1;
2509 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
2510 set_extent_delalloc(io_tree, page_start,
2511 page_end, GFP_NOFS);
2513 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2514 set_page_dirty(page);
2515 unlock_page(page);
2516 page_cache_release(page);
2517 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2520 out_unlock:
2521 mutex_unlock(&inode->i_mutex);
2522 return 0;
2525 static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
2527 u64 new_size;
2528 u64 old_size;
2529 struct btrfs_ioctl_vol_args *vol_args;
2530 struct btrfs_trans_handle *trans;
2531 char *sizestr;
2532 int ret = 0;
2533 int namelen;
2534 int mod = 0;
2536 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2538 if (!vol_args)
2539 return -ENOMEM;
2541 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2542 ret = -EFAULT;
2543 goto out;
2545 namelen = strlen(vol_args->name);
2546 if (namelen > BTRFS_VOL_NAME_MAX) {
2547 ret = -EINVAL;
2548 goto out;
2551 sizestr = vol_args->name;
2552 if (!strcmp(sizestr, "max"))
2553 new_size = root->fs_info->sb->s_bdev->bd_inode->i_size;
2554 else {
2555 if (sizestr[0] == '-') {
2556 mod = -1;
2557 sizestr++;
2558 } else if (sizestr[0] == '+') {
2559 mod = 1;
2560 sizestr++;
2562 new_size = btrfs_parse_size(sizestr);
2563 if (new_size == 0) {
2564 ret = -EINVAL;
2565 goto out;
2569 mutex_lock(&root->fs_info->fs_mutex);
2570 old_size = btrfs_super_total_bytes(&root->fs_info->super_copy);
2572 if (mod < 0) {
2573 if (new_size > old_size) {
2574 ret = -EINVAL;
2575 goto out_unlock;
2577 new_size = old_size - new_size;
2578 } else if (mod > 0) {
2579 new_size = old_size + new_size;
2582 if (new_size < 256 * 1024 * 1024) {
2583 ret = -EINVAL;
2584 goto out_unlock;
2586 if (new_size > root->fs_info->sb->s_bdev->bd_inode->i_size) {
2587 ret = -EFBIG;
2588 goto out_unlock;
2591 do_div(new_size, root->sectorsize);
2592 new_size *= root->sectorsize;
2594 printk("new size is %Lu\n", new_size);
2595 if (new_size > old_size) {
2596 trans = btrfs_start_transaction(root, 1);
2597 ret = btrfs_grow_extent_tree(trans, root, new_size);
2598 btrfs_commit_transaction(trans, root);
2599 } else {
2600 ret = btrfs_shrink_extent_tree(root, new_size);
2603 out_unlock:
2604 mutex_unlock(&root->fs_info->fs_mutex);
2605 out:
2606 kfree(vol_args);
2607 return ret;
2610 static int noinline btrfs_ioctl_snap_create(struct btrfs_root *root,
2611 void __user *arg)
2613 struct btrfs_ioctl_vol_args *vol_args;
2614 struct btrfs_dir_item *di;
2615 struct btrfs_path *path;
2616 u64 root_dirid;
2617 int namelen;
2618 int ret;
2620 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2622 if (!vol_args)
2623 return -ENOMEM;
2625 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2626 ret = -EFAULT;
2627 goto out;
2630 namelen = strlen(vol_args->name);
2631 if (namelen > BTRFS_VOL_NAME_MAX) {
2632 ret = -EINVAL;
2633 goto out;
2635 if (strchr(vol_args->name, '/')) {
2636 ret = -EINVAL;
2637 goto out;
2640 path = btrfs_alloc_path();
2641 if (!path) {
2642 ret = -ENOMEM;
2643 goto out;
2646 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2647 mutex_lock(&root->fs_info->fs_mutex);
2648 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2649 path, root_dirid,
2650 vol_args->name, namelen, 0);
2651 mutex_unlock(&root->fs_info->fs_mutex);
2652 btrfs_free_path(path);
2654 if (di && !IS_ERR(di)) {
2655 ret = -EEXIST;
2656 goto out;
2659 if (IS_ERR(di)) {
2660 ret = PTR_ERR(di);
2661 goto out;
2664 if (root == root->fs_info->tree_root)
2665 ret = create_subvol(root, vol_args->name, namelen);
2666 else
2667 ret = create_snapshot(root, vol_args->name, namelen);
2668 out:
2669 kfree(vol_args);
2670 return ret;
2673 static int btrfs_ioctl_defrag(struct file *file)
2675 struct inode *inode = fdentry(file)->d_inode;
2676 struct btrfs_root *root = BTRFS_I(inode)->root;
2678 switch (inode->i_mode & S_IFMT) {
2679 case S_IFDIR:
2680 mutex_lock(&root->fs_info->fs_mutex);
2681 btrfs_defrag_root(root, 0);
2682 btrfs_defrag_root(root->fs_info->extent_root, 0);
2683 mutex_unlock(&root->fs_info->fs_mutex);
2684 break;
2685 case S_IFREG:
2686 btrfs_defrag_file(file);
2687 break;
2690 return 0;
2693 long btrfs_ioctl(struct file *file, unsigned int
2694 cmd, unsigned long arg)
2696 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
2698 switch (cmd) {
2699 case BTRFS_IOC_SNAP_CREATE:
2700 return btrfs_ioctl_snap_create(root, (void __user *)arg);
2701 case BTRFS_IOC_DEFRAG:
2702 return btrfs_ioctl_defrag(file);
2703 case BTRFS_IOC_RESIZE:
2704 return btrfs_ioctl_resize(root, (void __user *)arg);
2707 return -ENOTTY;
2711 * Called inside transaction, so use GFP_NOFS
2713 struct inode *btrfs_alloc_inode(struct super_block *sb)
2715 struct btrfs_inode *ei;
2717 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2718 if (!ei)
2719 return NULL;
2720 ei->last_trans = 0;
2721 ei->ordered_trans = 0;
2722 return &ei->vfs_inode;
2725 void btrfs_destroy_inode(struct inode *inode)
2727 WARN_ON(!list_empty(&inode->i_dentry));
2728 WARN_ON(mapping_nrpages(&inode->i_data));
2730 btrfs_drop_extent_cache(inode, 0, (u64)-1);
2731 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2734 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2735 static void init_once(struct kmem_cache * cachep, void *foo)
2736 #else
2737 static void init_once(void * foo, struct kmem_cache * cachep,
2738 unsigned long flags)
2739 #endif
2741 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2743 inode_init_once(&ei->vfs_inode);
2746 void btrfs_destroy_cachep(void)
2748 if (btrfs_inode_cachep)
2749 kmem_cache_destroy(btrfs_inode_cachep);
2750 if (btrfs_trans_handle_cachep)
2751 kmem_cache_destroy(btrfs_trans_handle_cachep);
2752 if (btrfs_transaction_cachep)
2753 kmem_cache_destroy(btrfs_transaction_cachep);
2754 if (btrfs_bit_radix_cachep)
2755 kmem_cache_destroy(btrfs_bit_radix_cachep);
2756 if (btrfs_path_cachep)
2757 kmem_cache_destroy(btrfs_path_cachep);
2760 struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
2761 unsigned long extra_flags,
2762 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2763 void (*ctor)(struct kmem_cache *, void *)
2764 #else
2765 void (*ctor)(void *, struct kmem_cache *,
2766 unsigned long)
2767 #endif
2770 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2771 SLAB_MEM_SPREAD | extra_flags), ctor
2772 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2773 ,NULL
2774 #endif
2778 int btrfs_init_cachep(void)
2780 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
2781 sizeof(struct btrfs_inode),
2782 0, init_once);
2783 if (!btrfs_inode_cachep)
2784 goto fail;
2785 btrfs_trans_handle_cachep =
2786 btrfs_cache_create("btrfs_trans_handle_cache",
2787 sizeof(struct btrfs_trans_handle),
2788 0, NULL);
2789 if (!btrfs_trans_handle_cachep)
2790 goto fail;
2791 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
2792 sizeof(struct btrfs_transaction),
2793 0, NULL);
2794 if (!btrfs_transaction_cachep)
2795 goto fail;
2796 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
2797 sizeof(struct btrfs_path),
2798 0, NULL);
2799 if (!btrfs_path_cachep)
2800 goto fail;
2801 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
2802 SLAB_DESTROY_BY_RCU, NULL);
2803 if (!btrfs_bit_radix_cachep)
2804 goto fail;
2805 return 0;
2806 fail:
2807 btrfs_destroy_cachep();
2808 return -ENOMEM;
2811 static int btrfs_getattr(struct vfsmount *mnt,
2812 struct dentry *dentry, struct kstat *stat)
2814 struct inode *inode = dentry->d_inode;
2815 generic_fillattr(inode, stat);
2816 stat->blksize = PAGE_CACHE_SIZE;
2817 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
2818 return 0;
2821 static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2822 struct inode * new_dir,struct dentry *new_dentry)
2824 struct btrfs_trans_handle *trans;
2825 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2826 struct inode *new_inode = new_dentry->d_inode;
2827 struct inode *old_inode = old_dentry->d_inode;
2828 struct timespec ctime = CURRENT_TIME;
2829 struct btrfs_path *path;
2830 int ret;
2832 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2833 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2834 return -ENOTEMPTY;
2837 mutex_lock(&root->fs_info->fs_mutex);
2838 ret = btrfs_check_free_space(root, 1, 0);
2839 if (ret)
2840 goto out_unlock;
2842 trans = btrfs_start_transaction(root, 1);
2844 btrfs_set_trans_block_group(trans, new_dir);
2845 path = btrfs_alloc_path();
2846 if (!path) {
2847 ret = -ENOMEM;
2848 goto out_fail;
2851 old_dentry->d_inode->i_nlink++;
2852 old_dir->i_ctime = old_dir->i_mtime = ctime;
2853 new_dir->i_ctime = new_dir->i_mtime = ctime;
2854 old_inode->i_ctime = ctime;
2856 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2857 if (ret)
2858 goto out_fail;
2860 if (new_inode) {
2861 new_inode->i_ctime = CURRENT_TIME;
2862 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2863 if (ret)
2864 goto out_fail;
2866 ret = btrfs_add_link(trans, new_dentry, old_inode, 1);
2867 if (ret)
2868 goto out_fail;
2870 out_fail:
2871 btrfs_free_path(path);
2872 btrfs_end_transaction(trans, root);
2873 out_unlock:
2874 mutex_unlock(&root->fs_info->fs_mutex);
2875 return ret;
2878 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2879 const char *symname)
2881 struct btrfs_trans_handle *trans;
2882 struct btrfs_root *root = BTRFS_I(dir)->root;
2883 struct btrfs_path *path;
2884 struct btrfs_key key;
2885 struct inode *inode = NULL;
2886 int err;
2887 int drop_inode = 0;
2888 u64 objectid;
2889 int name_len;
2890 int datasize;
2891 unsigned long ptr;
2892 struct btrfs_file_extent_item *ei;
2893 struct extent_buffer *leaf;
2894 unsigned long nr = 0;
2896 name_len = strlen(symname) + 1;
2897 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2898 return -ENAMETOOLONG;
2900 mutex_lock(&root->fs_info->fs_mutex);
2901 err = btrfs_check_free_space(root, 1, 0);
2902 if (err)
2903 goto out_fail;
2905 trans = btrfs_start_transaction(root, 1);
2906 btrfs_set_trans_block_group(trans, dir);
2908 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2909 if (err) {
2910 err = -ENOSPC;
2911 goto out_unlock;
2914 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2915 dentry->d_name.len,
2916 dentry->d_parent->d_inode->i_ino, objectid,
2917 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2918 err = PTR_ERR(inode);
2919 if (IS_ERR(inode))
2920 goto out_unlock;
2922 btrfs_set_trans_block_group(trans, inode);
2923 err = btrfs_add_nondir(trans, dentry, inode, 0);
2924 if (err)
2925 drop_inode = 1;
2926 else {
2927 inode->i_mapping->a_ops = &btrfs_aops;
2928 inode->i_fop = &btrfs_file_operations;
2929 inode->i_op = &btrfs_file_inode_operations;
2930 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2931 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
2932 inode->i_mapping, GFP_NOFS);
2933 BTRFS_I(inode)->delalloc_bytes = 0;
2934 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
2936 dir->i_sb->s_dirt = 1;
2937 btrfs_update_inode_block_group(trans, inode);
2938 btrfs_update_inode_block_group(trans, dir);
2939 if (drop_inode)
2940 goto out_unlock;
2942 path = btrfs_alloc_path();
2943 BUG_ON(!path);
2944 key.objectid = inode->i_ino;
2945 key.offset = 0;
2946 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2947 datasize = btrfs_file_extent_calc_inline_size(name_len);
2948 err = btrfs_insert_empty_item(trans, root, path, &key,
2949 datasize);
2950 if (err) {
2951 drop_inode = 1;
2952 goto out_unlock;
2954 leaf = path->nodes[0];
2955 ei = btrfs_item_ptr(leaf, path->slots[0],
2956 struct btrfs_file_extent_item);
2957 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
2958 btrfs_set_file_extent_type(leaf, ei,
2959 BTRFS_FILE_EXTENT_INLINE);
2960 ptr = btrfs_file_extent_inline_start(ei);
2961 write_extent_buffer(leaf, symname, ptr, name_len);
2962 btrfs_mark_buffer_dirty(leaf);
2963 btrfs_free_path(path);
2965 inode->i_op = &btrfs_symlink_inode_operations;
2966 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2967 inode->i_size = name_len - 1;
2968 err = btrfs_update_inode(trans, root, inode);
2969 if (err)
2970 drop_inode = 1;
2972 out_unlock:
2973 nr = trans->blocks_used;
2974 btrfs_end_transaction(trans, root);
2975 out_fail:
2976 mutex_unlock(&root->fs_info->fs_mutex);
2977 if (drop_inode) {
2978 inode_dec_link_count(inode);
2979 iput(inode);
2981 btrfs_btree_balance_dirty(root, nr);
2982 btrfs_throttle(root);
2983 return err;
2985 static int btrfs_permission(struct inode *inode, int mask,
2986 struct nameidata *nd)
2988 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
2989 return -EACCES;
2990 return generic_permission(inode, mask, NULL);
2993 static struct inode_operations btrfs_dir_inode_operations = {
2994 .lookup = btrfs_lookup,
2995 .create = btrfs_create,
2996 .unlink = btrfs_unlink,
2997 .link = btrfs_link,
2998 .mkdir = btrfs_mkdir,
2999 .rmdir = btrfs_rmdir,
3000 .rename = btrfs_rename,
3001 .symlink = btrfs_symlink,
3002 .setattr = btrfs_setattr,
3003 .mknod = btrfs_mknod,
3004 .setxattr = generic_setxattr,
3005 .getxattr = generic_getxattr,
3006 .listxattr = btrfs_listxattr,
3007 .removexattr = generic_removexattr,
3008 .permission = btrfs_permission,
3010 static struct inode_operations btrfs_dir_ro_inode_operations = {
3011 .lookup = btrfs_lookup,
3012 .permission = btrfs_permission,
3014 static struct file_operations btrfs_dir_file_operations = {
3015 .llseek = generic_file_llseek,
3016 .read = generic_read_dir,
3017 .readdir = btrfs_readdir,
3018 .unlocked_ioctl = btrfs_ioctl,
3019 #ifdef CONFIG_COMPAT
3020 .compat_ioctl = btrfs_ioctl,
3021 #endif
3024 static struct extent_io_ops btrfs_extent_io_ops = {
3025 .fill_delalloc = run_delalloc_range,
3026 .submit_bio_hook = btrfs_submit_bio_hook,
3027 .readpage_io_hook = btrfs_readpage_io_hook,
3028 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
3029 .set_bit_hook = btrfs_set_bit_hook,
3030 .clear_bit_hook = btrfs_clear_bit_hook,
3033 static struct address_space_operations btrfs_aops = {
3034 .readpage = btrfs_readpage,
3035 .writepage = btrfs_writepage,
3036 .writepages = btrfs_writepages,
3037 .readpages = btrfs_readpages,
3038 .sync_page = block_sync_page,
3039 .bmap = btrfs_bmap,
3040 .invalidatepage = btrfs_invalidatepage,
3041 .releasepage = btrfs_releasepage,
3042 .set_page_dirty = __set_page_dirty_nobuffers,
3045 static struct address_space_operations btrfs_symlink_aops = {
3046 .readpage = btrfs_readpage,
3047 .writepage = btrfs_writepage,
3048 .invalidatepage = btrfs_invalidatepage,
3049 .releasepage = btrfs_releasepage,
3052 static struct inode_operations btrfs_file_inode_operations = {
3053 .truncate = btrfs_truncate,
3054 .getattr = btrfs_getattr,
3055 .setattr = btrfs_setattr,
3056 .setxattr = generic_setxattr,
3057 .getxattr = generic_getxattr,
3058 .listxattr = btrfs_listxattr,
3059 .removexattr = generic_removexattr,
3060 .permission = btrfs_permission,
3062 static struct inode_operations btrfs_special_inode_operations = {
3063 .getattr = btrfs_getattr,
3064 .setattr = btrfs_setattr,
3065 .permission = btrfs_permission,
3067 static struct inode_operations btrfs_symlink_inode_operations = {
3068 .readlink = generic_readlink,
3069 .follow_link = page_follow_link_light,
3070 .put_link = page_put_link,
3071 .permission = btrfs_permission,