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