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