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