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