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