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