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