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