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