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