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