Btrfs: optimize btrget/set/removexattr
[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/kernel.h>
20 #include <linux/bio.h>
21 #include <linux/buffer_head.h>
22 #include <linux/file.h>
23 #include <linux/fs.h>
24 #include <linux/pagemap.h>
25 #include <linux/highmem.h>
26 #include <linux/time.h>
27 #include <linux/init.h>
28 #include <linux/string.h>
29 #include <linux/smp_lock.h>
30 #include <linux/backing-dev.h>
31 #include <linux/mpage.h>
32 #include <linux/swap.h>
33 #include <linux/writeback.h>
34 #include <linux/statfs.h>
35 #include <linux/compat.h>
36 #include <linux/bit_spinlock.h>
37 #include <linux/version.h>
38 #include <linux/xattr.h>
39 #include <linux/posix_acl.h>
40 #include "ctree.h"
41 #include "disk-io.h"
42 #include "transaction.h"
43 #include "btrfs_inode.h"
44 #include "ioctl.h"
45 #include "print-tree.h"
46 #include "volumes.h"
47 #include "ordered-data.h"
48 #include "xattr.h"
49
50 struct btrfs_iget_args {
51 u64 ino;
52 struct btrfs_root *root;
53 };
54
55 static struct inode_operations btrfs_dir_inode_operations;
56 static struct inode_operations btrfs_symlink_inode_operations;
57 static struct inode_operations btrfs_dir_ro_inode_operations;
58 static struct inode_operations btrfs_special_inode_operations;
59 static struct inode_operations btrfs_file_inode_operations;
60 static struct address_space_operations btrfs_aops;
61 static struct address_space_operations btrfs_symlink_aops;
62 static struct file_operations btrfs_dir_file_operations;
63 static struct extent_io_ops btrfs_extent_io_ops;
64
65 static struct kmem_cache *btrfs_inode_cachep;
66 struct kmem_cache *btrfs_trans_handle_cachep;
67 struct kmem_cache *btrfs_transaction_cachep;
68 struct kmem_cache *btrfs_bit_radix_cachep;
69 struct kmem_cache *btrfs_path_cachep;
70
71 #define S_SHIFT 12
72 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
73 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
74 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
75 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
76 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
77 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
78 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
79 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
80 };
81
82 static void btrfs_truncate(struct inode *inode);
83
84 int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
85 int for_del)
86 {
87 u64 total;
88 u64 used;
89 u64 thresh;
90 unsigned long flags;
91 int ret = 0;
92
93 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
94 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
95 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
96 if (for_del)
97 thresh = total * 90;
98 else
99 thresh = total * 85;
100
101 do_div(thresh, 100);
102
103 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
104 ret = -ENOSPC;
105 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
106 return ret;
107 }
108
109 static int cow_file_range(struct inode *inode, u64 start, u64 end)
110 {
111 struct btrfs_root *root = BTRFS_I(inode)->root;
112 struct btrfs_trans_handle *trans;
113 u64 alloc_hint = 0;
114 u64 num_bytes;
115 u64 cur_alloc_size;
116 u64 blocksize = root->sectorsize;
117 u64 orig_num_bytes;
118 struct btrfs_key ins;
119 struct extent_map *em;
120 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
121 int ret = 0;
122
123 trans = btrfs_join_transaction(root, 1);
124 BUG_ON(!trans);
125 btrfs_set_trans_block_group(trans, inode);
126
127 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
128 num_bytes = max(blocksize, num_bytes);
129 orig_num_bytes = num_bytes;
130
131 if (alloc_hint == EXTENT_MAP_INLINE)
132 goto out;
133
134 BUG_ON(num_bytes > btrfs_super_total_bytes(&root->fs_info->super_copy));
135 mutex_lock(&BTRFS_I(inode)->extent_mutex);
136 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1);
137 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
138
139 while(num_bytes > 0) {
140 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
141 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
142 root->sectorsize, 0, 0,
143 (u64)-1, &ins, 1);
144 if (ret) {
145 WARN_ON(1);
146 goto out;
147 }
148 em = alloc_extent_map(GFP_NOFS);
149 em->start = start;
150 em->len = ins.offset;
151 em->block_start = ins.objectid;
152 em->bdev = root->fs_info->fs_devices->latest_bdev;
153 mutex_lock(&BTRFS_I(inode)->extent_mutex);
154 set_bit(EXTENT_FLAG_PINNED, &em->flags);
155 while(1) {
156 spin_lock(&em_tree->lock);
157 ret = add_extent_mapping(em_tree, em);
158 spin_unlock(&em_tree->lock);
159 if (ret != -EEXIST) {
160 free_extent_map(em);
161 break;
162 }
163 btrfs_drop_extent_cache(inode, start,
164 start + ins.offset - 1);
165 }
166 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
167
168 cur_alloc_size = ins.offset;
169 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
170 ins.offset, 0);
171 BUG_ON(ret);
172 if (num_bytes < cur_alloc_size) {
173 printk("num_bytes %Lu cur_alloc %Lu\n", num_bytes,
174 cur_alloc_size);
175 break;
176 }
177 num_bytes -= cur_alloc_size;
178 alloc_hint = ins.objectid + ins.offset;
179 start += cur_alloc_size;
180 }
181 out:
182 btrfs_end_transaction(trans, root);
183 return ret;
184 }
185
186 static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
187 {
188 u64 extent_start;
189 u64 extent_end;
190 u64 bytenr;
191 u64 loops = 0;
192 u64 total_fs_bytes;
193 struct btrfs_root *root = BTRFS_I(inode)->root;
194 struct btrfs_block_group_cache *block_group;
195 struct btrfs_trans_handle *trans;
196 struct extent_buffer *leaf;
197 int found_type;
198 struct btrfs_path *path;
199 struct btrfs_file_extent_item *item;
200 int ret;
201 int err = 0;
202 struct btrfs_key found_key;
203
204 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
205 path = btrfs_alloc_path();
206 BUG_ON(!path);
207 trans = btrfs_join_transaction(root, 1);
208 BUG_ON(!trans);
209 again:
210 ret = btrfs_lookup_file_extent(NULL, root, path,
211 inode->i_ino, start, 0);
212 if (ret < 0) {
213 err = ret;
214 goto out;
215 }
216
217 if (ret != 0) {
218 if (path->slots[0] == 0)
219 goto not_found;
220 path->slots[0]--;
221 }
222
223 leaf = path->nodes[0];
224 item = btrfs_item_ptr(leaf, path->slots[0],
225 struct btrfs_file_extent_item);
226
227 /* are we inside the extent that was found? */
228 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
229 found_type = btrfs_key_type(&found_key);
230 if (found_key.objectid != inode->i_ino ||
231 found_type != BTRFS_EXTENT_DATA_KEY)
232 goto not_found;
233
234 found_type = btrfs_file_extent_type(leaf, item);
235 extent_start = found_key.offset;
236 if (found_type == BTRFS_FILE_EXTENT_REG) {
237 u64 extent_num_bytes;
238
239 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
240 extent_end = extent_start + extent_num_bytes;
241 err = 0;
242
243 if (loops && start != extent_start)
244 goto not_found;
245
246 if (start < extent_start || start >= extent_end)
247 goto not_found;
248
249 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
250 if (bytenr == 0)
251 goto not_found;
252
253 if (btrfs_cross_ref_exists(trans, root, &found_key, bytenr))
254 goto not_found;
255 /*
256 * we may be called by the resizer, make sure we're inside
257 * the limits of the FS
258 */
259 block_group = btrfs_lookup_block_group(root->fs_info,
260 bytenr);
261 if (!block_group || block_group->ro)
262 goto not_found;
263
264 bytenr += btrfs_file_extent_offset(leaf, item);
265 extent_num_bytes = min(end + 1, extent_end) - start;
266 ret = btrfs_add_ordered_extent(inode, start, bytenr,
267 extent_num_bytes, 1);
268 if (ret) {
269 err = ret;
270 goto out;
271 }
272
273 btrfs_release_path(root, path);
274 start = extent_end;
275 if (start <= end) {
276 loops++;
277 goto again;
278 }
279 } else {
280 not_found:
281 btrfs_end_transaction(trans, root);
282 btrfs_free_path(path);
283 return cow_file_range(inode, start, end);
284 }
285 out:
286 WARN_ON(err);
287 btrfs_end_transaction(trans, root);
288 btrfs_free_path(path);
289 return err;
290 }
291
292 static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
293 {
294 struct btrfs_root *root = BTRFS_I(inode)->root;
295 int ret;
296
297 if (btrfs_test_opt(root, NODATACOW) ||
298 btrfs_test_flag(inode, NODATACOW))
299 ret = run_delalloc_nocow(inode, start, end);
300 else
301 ret = cow_file_range(inode, start, end);
302
303 return ret;
304 }
305
306 int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
307 unsigned long old, unsigned long bits)
308 {
309 unsigned long flags;
310 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
311 struct btrfs_root *root = BTRFS_I(inode)->root;
312 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
313 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
314 root->fs_info->delalloc_bytes += end - start + 1;
315 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
316 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
317 &root->fs_info->delalloc_inodes);
318 }
319 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
320 }
321 return 0;
322 }
323
324 int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
325 unsigned long old, unsigned long bits)
326 {
327 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
328 struct btrfs_root *root = BTRFS_I(inode)->root;
329 unsigned long flags;
330
331 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
332 if (end - start + 1 > root->fs_info->delalloc_bytes) {
333 printk("warning: delalloc account %Lu %Lu\n",
334 end - start + 1, root->fs_info->delalloc_bytes);
335 root->fs_info->delalloc_bytes = 0;
336 BTRFS_I(inode)->delalloc_bytes = 0;
337 } else {
338 root->fs_info->delalloc_bytes -= end - start + 1;
339 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
340 }
341 if (BTRFS_I(inode)->delalloc_bytes == 0 &&
342 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
343 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
344 }
345 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
346 }
347 return 0;
348 }
349
350 int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
351 size_t size, struct bio *bio)
352 {
353 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
354 struct btrfs_mapping_tree *map_tree;
355 u64 logical = bio->bi_sector << 9;
356 u64 length = 0;
357 u64 map_length;
358 int ret;
359
360 length = bio->bi_size;
361 map_tree = &root->fs_info->mapping_tree;
362 map_length = length;
363 ret = btrfs_map_block(map_tree, READ, logical,
364 &map_length, NULL, 0);
365
366 if (map_length < length + size) {
367 return 1;
368 }
369 return 0;
370 }
371
372 int __btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
373 int mirror_num)
374 {
375 struct btrfs_root *root = BTRFS_I(inode)->root;
376 int ret = 0;
377
378 ret = btrfs_csum_one_bio(root, inode, bio);
379 BUG_ON(ret);
380
381 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
382 }
383
384 int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
385 int mirror_num)
386 {
387 struct btrfs_root *root = BTRFS_I(inode)->root;
388 int ret = 0;
389
390 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
391 BUG_ON(ret);
392
393 if (btrfs_test_opt(root, NODATASUM) ||
394 btrfs_test_flag(inode, NODATASUM)) {
395 goto mapit;
396 }
397
398 if (!(rw & (1 << BIO_RW))) {
399 btrfs_lookup_bio_sums(root, inode, bio);
400 goto mapit;
401 }
402 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
403 inode, rw, bio, mirror_num,
404 __btrfs_submit_bio_hook);
405 mapit:
406 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
407 }
408
409 static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
410 struct inode *inode, u64 file_offset,
411 struct list_head *list)
412 {
413 struct list_head *cur;
414 struct btrfs_ordered_sum *sum;
415
416 btrfs_set_trans_block_group(trans, inode);
417 list_for_each(cur, list) {
418 sum = list_entry(cur, struct btrfs_ordered_sum, list);
419 btrfs_csum_file_blocks(trans, BTRFS_I(inode)->root,
420 inode, sum);
421 }
422 return 0;
423 }
424
425 int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end)
426 {
427 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
428 GFP_NOFS);
429 }
430
431 struct btrfs_writepage_fixup {
432 struct page *page;
433 struct btrfs_work work;
434 };
435
436 /* see btrfs_writepage_start_hook for details on why this is required */
437 void btrfs_writepage_fixup_worker(struct btrfs_work *work)
438 {
439 struct btrfs_writepage_fixup *fixup;
440 struct btrfs_ordered_extent *ordered;
441 struct page *page;
442 struct inode *inode;
443 u64 page_start;
444 u64 page_end;
445
446 fixup = container_of(work, struct btrfs_writepage_fixup, work);
447 page = fixup->page;
448 again:
449 lock_page(page);
450 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
451 ClearPageChecked(page);
452 goto out_page;
453 }
454
455 inode = page->mapping->host;
456 page_start = page_offset(page);
457 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
458
459 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
460
461 /* already ordered? We're done */
462 if (test_range_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
463 EXTENT_ORDERED, 0)) {
464 goto out;
465 }
466
467 ordered = btrfs_lookup_ordered_extent(inode, page_start);
468 if (ordered) {
469 unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
470 page_end, GFP_NOFS);
471 unlock_page(page);
472 btrfs_start_ordered_extent(inode, ordered, 1);
473 goto again;
474 }
475
476 btrfs_set_extent_delalloc(inode, page_start, page_end);
477 ClearPageChecked(page);
478 out:
479 unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
480 out_page:
481 unlock_page(page);
482 page_cache_release(page);
483 }
484
485 /*
486 * There are a few paths in the higher layers of the kernel that directly
487 * set the page dirty bit without asking the filesystem if it is a
488 * good idea. This causes problems because we want to make sure COW
489 * properly happens and the data=ordered rules are followed.
490 *
491 * In our case any range that doesn't have the EXTENT_ORDERED bit set
492 * hasn't been properly setup for IO. We kick off an async process
493 * to fix it up. The async helper will wait for ordered extents, set
494 * the delalloc bit and make it safe to write the page.
495 */
496 int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
497 {
498 struct inode *inode = page->mapping->host;
499 struct btrfs_writepage_fixup *fixup;
500 struct btrfs_root *root = BTRFS_I(inode)->root;
501 int ret;
502
503 ret = test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
504 EXTENT_ORDERED, 0);
505 if (ret)
506 return 0;
507
508 if (PageChecked(page))
509 return -EAGAIN;
510
511 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
512 if (!fixup)
513 return -EAGAIN;
514
515 SetPageChecked(page);
516 page_cache_get(page);
517 fixup->work.func = btrfs_writepage_fixup_worker;
518 fixup->page = page;
519 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
520 return -EAGAIN;
521 }
522
523 static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
524 {
525 struct btrfs_root *root = BTRFS_I(inode)->root;
526 struct btrfs_trans_handle *trans;
527 struct btrfs_ordered_extent *ordered_extent;
528 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
529 u64 alloc_hint = 0;
530 struct list_head list;
531 struct btrfs_key ins;
532 int ret;
533
534 ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
535 if (!ret)
536 return 0;
537
538 trans = btrfs_join_transaction(root, 1);
539
540 ordered_extent = btrfs_lookup_ordered_extent(inode, start);
541 BUG_ON(!ordered_extent);
542 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags))
543 goto nocow;
544
545 lock_extent(io_tree, ordered_extent->file_offset,
546 ordered_extent->file_offset + ordered_extent->len - 1,
547 GFP_NOFS);
548
549 INIT_LIST_HEAD(&list);
550
551 ins.objectid = ordered_extent->start;
552 ins.offset = ordered_extent->len;
553 ins.type = BTRFS_EXTENT_ITEM_KEY;
554
555 ret = btrfs_alloc_reserved_extent(trans, root, root->root_key.objectid,
556 trans->transid, inode->i_ino,
557 ordered_extent->file_offset, &ins);
558 BUG_ON(ret);
559
560 mutex_lock(&BTRFS_I(inode)->extent_mutex);
561
562 ret = btrfs_drop_extents(trans, root, inode,
563 ordered_extent->file_offset,
564 ordered_extent->file_offset +
565 ordered_extent->len,
566 ordered_extent->file_offset, &alloc_hint);
567 BUG_ON(ret);
568 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
569 ordered_extent->file_offset,
570 ordered_extent->start,
571 ordered_extent->len,
572 ordered_extent->len, 0);
573 BUG_ON(ret);
574
575 btrfs_drop_extent_cache(inode, ordered_extent->file_offset,
576 ordered_extent->file_offset +
577 ordered_extent->len - 1);
578 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
579
580 inode->i_blocks += ordered_extent->len >> 9;
581 unlock_extent(io_tree, ordered_extent->file_offset,
582 ordered_extent->file_offset + ordered_extent->len - 1,
583 GFP_NOFS);
584 nocow:
585 add_pending_csums(trans, inode, ordered_extent->file_offset,
586 &ordered_extent->list);
587
588 btrfs_ordered_update_i_size(inode, ordered_extent);
589 btrfs_remove_ordered_extent(inode, ordered_extent);
590
591 /* once for us */
592 btrfs_put_ordered_extent(ordered_extent);
593 /* once for the tree */
594 btrfs_put_ordered_extent(ordered_extent);
595
596 btrfs_update_inode(trans, root, inode);
597 btrfs_end_transaction(trans, root);
598 return 0;
599 }
600
601 int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
602 struct extent_state *state, int uptodate)
603 {
604 return btrfs_finish_ordered_io(page->mapping->host, start, end);
605 }
606
607 struct io_failure_record {
608 struct page *page;
609 u64 start;
610 u64 len;
611 u64 logical;
612 int last_mirror;
613 };
614
615 int btrfs_io_failed_hook(struct bio *failed_bio,
616 struct page *page, u64 start, u64 end,
617 struct extent_state *state)
618 {
619 struct io_failure_record *failrec = NULL;
620 u64 private;
621 struct extent_map *em;
622 struct inode *inode = page->mapping->host;
623 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
624 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
625 struct bio *bio;
626 int num_copies;
627 int ret;
628 int rw;
629 u64 logical;
630
631 ret = get_state_private(failure_tree, start, &private);
632 if (ret) {
633 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
634 if (!failrec)
635 return -ENOMEM;
636 failrec->start = start;
637 failrec->len = end - start + 1;
638 failrec->last_mirror = 0;
639
640 spin_lock(&em_tree->lock);
641 em = lookup_extent_mapping(em_tree, start, failrec->len);
642 if (em->start > start || em->start + em->len < start) {
643 free_extent_map(em);
644 em = NULL;
645 }
646 spin_unlock(&em_tree->lock);
647
648 if (!em || IS_ERR(em)) {
649 kfree(failrec);
650 return -EIO;
651 }
652 logical = start - em->start;
653 logical = em->block_start + logical;
654 failrec->logical = logical;
655 free_extent_map(em);
656 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
657 EXTENT_DIRTY, GFP_NOFS);
658 set_state_private(failure_tree, start,
659 (u64)(unsigned long)failrec);
660 } else {
661 failrec = (struct io_failure_record *)(unsigned long)private;
662 }
663 num_copies = btrfs_num_copies(
664 &BTRFS_I(inode)->root->fs_info->mapping_tree,
665 failrec->logical, failrec->len);
666 failrec->last_mirror++;
667 if (!state) {
668 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
669 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
670 failrec->start,
671 EXTENT_LOCKED);
672 if (state && state->start != failrec->start)
673 state = NULL;
674 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
675 }
676 if (!state || failrec->last_mirror > num_copies) {
677 set_state_private(failure_tree, failrec->start, 0);
678 clear_extent_bits(failure_tree, failrec->start,
679 failrec->start + failrec->len - 1,
680 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
681 kfree(failrec);
682 return -EIO;
683 }
684 bio = bio_alloc(GFP_NOFS, 1);
685 bio->bi_private = state;
686 bio->bi_end_io = failed_bio->bi_end_io;
687 bio->bi_sector = failrec->logical >> 9;
688 bio->bi_bdev = failed_bio->bi_bdev;
689 bio->bi_size = 0;
690 bio_add_page(bio, page, failrec->len, start - page_offset(page));
691 if (failed_bio->bi_rw & (1 << BIO_RW))
692 rw = WRITE;
693 else
694 rw = READ;
695
696 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
697 failrec->last_mirror);
698 return 0;
699 }
700
701 int btrfs_clean_io_failures(struct inode *inode, u64 start)
702 {
703 u64 private;
704 u64 private_failure;
705 struct io_failure_record *failure;
706 int ret;
707
708 private = 0;
709 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
710 (u64)-1, 1, EXTENT_DIRTY)) {
711 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
712 start, &private_failure);
713 if (ret == 0) {
714 failure = (struct io_failure_record *)(unsigned long)
715 private_failure;
716 set_state_private(&BTRFS_I(inode)->io_failure_tree,
717 failure->start, 0);
718 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
719 failure->start,
720 failure->start + failure->len - 1,
721 EXTENT_DIRTY | EXTENT_LOCKED,
722 GFP_NOFS);
723 kfree(failure);
724 }
725 }
726 return 0;
727 }
728
729 int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
730 struct extent_state *state)
731 {
732 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
733 struct inode *inode = page->mapping->host;
734 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
735 char *kaddr;
736 u64 private = ~(u32)0;
737 int ret;
738 struct btrfs_root *root = BTRFS_I(inode)->root;
739 u32 csum = ~(u32)0;
740 unsigned long flags;
741
742 if (btrfs_test_opt(root, NODATASUM) ||
743 btrfs_test_flag(inode, NODATASUM))
744 return 0;
745 if (state && state->start == start) {
746 private = state->private;
747 ret = 0;
748 } else {
749 ret = get_state_private(io_tree, start, &private);
750 }
751 local_irq_save(flags);
752 kaddr = kmap_atomic(page, KM_IRQ0);
753 if (ret) {
754 goto zeroit;
755 }
756 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
757 btrfs_csum_final(csum, (char *)&csum);
758 if (csum != private) {
759 goto zeroit;
760 }
761 kunmap_atomic(kaddr, KM_IRQ0);
762 local_irq_restore(flags);
763
764 /* if the io failure tree for this inode is non-empty,
765 * check to see if we've recovered from a failed IO
766 */
767 btrfs_clean_io_failures(inode, start);
768 return 0;
769
770 zeroit:
771 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
772 page->mapping->host->i_ino, (unsigned long long)start, csum,
773 private);
774 memset(kaddr + offset, 1, end - start + 1);
775 flush_dcache_page(page);
776 kunmap_atomic(kaddr, KM_IRQ0);
777 local_irq_restore(flags);
778 if (private == 0)
779 return 0;
780 return -EIO;
781 }
782
783 /*
784 * This creates an orphan entry for the given inode in case something goes
785 * wrong in the middle of an unlink/truncate.
786 */
787 int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
788 {
789 struct btrfs_root *root = BTRFS_I(inode)->root;
790 int ret = 0;
791
792 spin_lock(&root->list_lock);
793
794 /* already on the orphan list, we're good */
795 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
796 spin_unlock(&root->list_lock);
797 return 0;
798 }
799
800 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
801
802 spin_unlock(&root->list_lock);
803
804 /*
805 * insert an orphan item to track this unlinked/truncated file
806 */
807 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
808
809 return ret;
810 }
811
812 /*
813 * We have done the truncate/delete so we can go ahead and remove the orphan
814 * item for this particular inode.
815 */
816 int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
817 {
818 struct btrfs_root *root = BTRFS_I(inode)->root;
819 int ret = 0;
820
821 spin_lock(&root->list_lock);
822
823 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
824 spin_unlock(&root->list_lock);
825 return 0;
826 }
827
828 list_del_init(&BTRFS_I(inode)->i_orphan);
829 if (!trans) {
830 spin_unlock(&root->list_lock);
831 return 0;
832 }
833
834 spin_unlock(&root->list_lock);
835
836 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
837
838 return ret;
839 }
840
841 /*
842 * this cleans up any orphans that may be left on the list from the last use
843 * of this root.
844 */
845 void btrfs_orphan_cleanup(struct btrfs_root *root)
846 {
847 struct btrfs_path *path;
848 struct extent_buffer *leaf;
849 struct btrfs_item *item;
850 struct btrfs_key key, found_key;
851 struct btrfs_trans_handle *trans;
852 struct inode *inode;
853 int ret = 0, nr_unlink = 0, nr_truncate = 0;
854
855 /* don't do orphan cleanup if the fs is readonly. */
856 if (root->inode->i_sb->s_flags & MS_RDONLY)
857 return;
858
859 path = btrfs_alloc_path();
860 if (!path)
861 return;
862 path->reada = -1;
863
864 key.objectid = BTRFS_ORPHAN_OBJECTID;
865 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
866 key.offset = (u64)-1;
867
868 trans = btrfs_start_transaction(root, 1);
869 btrfs_set_trans_block_group(trans, root->inode);
870
871 while (1) {
872 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
873 if (ret < 0) {
874 printk(KERN_ERR "Error searching slot for orphan: %d"
875 "\n", ret);
876 break;
877 }
878
879 /*
880 * if ret == 0 means we found what we were searching for, which
881 * is weird, but possible, so only screw with path if we didnt
882 * find the key and see if we have stuff that matches
883 */
884 if (ret > 0) {
885 if (path->slots[0] == 0)
886 break;
887 path->slots[0]--;
888 }
889
890 /* pull out the item */
891 leaf = path->nodes[0];
892 item = btrfs_item_nr(leaf, path->slots[0]);
893 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
894
895 /* make sure the item matches what we want */
896 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
897 break;
898 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
899 break;
900
901 /* release the path since we're done with it */
902 btrfs_release_path(root, path);
903
904 /*
905 * this is where we are basically btrfs_lookup, without the
906 * crossing root thing. we store the inode number in the
907 * offset of the orphan item.
908 */
909 inode = btrfs_iget_locked(root->inode->i_sb,
910 found_key.offset, root);
911 if (!inode)
912 break;
913
914 if (inode->i_state & I_NEW) {
915 BTRFS_I(inode)->root = root;
916
917 /* have to set the location manually */
918 BTRFS_I(inode)->location.objectid = inode->i_ino;
919 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
920 BTRFS_I(inode)->location.offset = 0;
921
922 btrfs_read_locked_inode(inode);
923 unlock_new_inode(inode);
924 }
925
926 /*
927 * add this inode to the orphan list so btrfs_orphan_del does
928 * the proper thing when we hit it
929 */
930 spin_lock(&root->list_lock);
931 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
932 spin_unlock(&root->list_lock);
933
934 /*
935 * if this is a bad inode, means we actually succeeded in
936 * removing the inode, but not the orphan record, which means
937 * we need to manually delete the orphan since iput will just
938 * do a destroy_inode
939 */
940 if (is_bad_inode(inode)) {
941 btrfs_orphan_del(trans, inode);
942 iput(inode);
943 continue;
944 }
945
946 /* if we have links, this was a truncate, lets do that */
947 if (inode->i_nlink) {
948 nr_truncate++;
949 btrfs_truncate(inode);
950 } else {
951 nr_unlink++;
952 }
953
954 /* this will do delete_inode and everything for us */
955 iput(inode);
956 }
957
958 if (nr_unlink)
959 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
960 if (nr_truncate)
961 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
962
963 btrfs_free_path(path);
964 btrfs_end_transaction(trans, root);
965 }
966
967 void btrfs_read_locked_inode(struct inode *inode)
968 {
969 struct btrfs_path *path;
970 struct extent_buffer *leaf;
971 struct btrfs_inode_item *inode_item;
972 struct btrfs_timespec *tspec;
973 struct btrfs_root *root = BTRFS_I(inode)->root;
974 struct btrfs_key location;
975 u64 alloc_group_block;
976 u32 rdev;
977 int ret;
978
979 path = btrfs_alloc_path();
980 BUG_ON(!path);
981 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
982
983 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
984 if (ret)
985 goto make_bad;
986
987 leaf = path->nodes[0];
988 inode_item = btrfs_item_ptr(leaf, path->slots[0],
989 struct btrfs_inode_item);
990
991 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
992 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
993 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
994 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
995 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
996
997 tspec = btrfs_inode_atime(inode_item);
998 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
999 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1000
1001 tspec = btrfs_inode_mtime(inode_item);
1002 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1003 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1004
1005 tspec = btrfs_inode_ctime(inode_item);
1006 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1007 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1008
1009 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
1010 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
1011 inode->i_rdev = 0;
1012 rdev = btrfs_inode_rdev(leaf, inode_item);
1013
1014 BTRFS_I(inode)->index_cnt = (u64)-1;
1015
1016 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
1017 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
1018 alloc_group_block);
1019 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
1020 if (!BTRFS_I(inode)->block_group) {
1021 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
1022 NULL, 0,
1023 BTRFS_BLOCK_GROUP_METADATA, 0);
1024 }
1025 btrfs_free_path(path);
1026 inode_item = NULL;
1027
1028 switch (inode->i_mode & S_IFMT) {
1029 case S_IFREG:
1030 inode->i_mapping->a_ops = &btrfs_aops;
1031 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
1032 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
1033 inode->i_fop = &btrfs_file_operations;
1034 inode->i_op = &btrfs_file_inode_operations;
1035 break;
1036 case S_IFDIR:
1037 inode->i_fop = &btrfs_dir_file_operations;
1038 if (root == root->fs_info->tree_root)
1039 inode->i_op = &btrfs_dir_ro_inode_operations;
1040 else
1041 inode->i_op = &btrfs_dir_inode_operations;
1042 break;
1043 case S_IFLNK:
1044 inode->i_op = &btrfs_symlink_inode_operations;
1045 inode->i_mapping->a_ops = &btrfs_symlink_aops;
1046 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
1047 break;
1048 default:
1049 init_special_inode(inode, inode->i_mode, rdev);
1050 break;
1051 }
1052 return;
1053
1054 make_bad:
1055 btrfs_free_path(path);
1056 make_bad_inode(inode);
1057 }
1058
1059 static void fill_inode_item(struct extent_buffer *leaf,
1060 struct btrfs_inode_item *item,
1061 struct inode *inode)
1062 {
1063 btrfs_set_inode_uid(leaf, item, inode->i_uid);
1064 btrfs_set_inode_gid(leaf, item, inode->i_gid);
1065 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
1066 btrfs_set_inode_mode(leaf, item, inode->i_mode);
1067 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
1068
1069 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
1070 inode->i_atime.tv_sec);
1071 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
1072 inode->i_atime.tv_nsec);
1073
1074 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
1075 inode->i_mtime.tv_sec);
1076 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
1077 inode->i_mtime.tv_nsec);
1078
1079 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
1080 inode->i_ctime.tv_sec);
1081 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
1082 inode->i_ctime.tv_nsec);
1083
1084 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
1085 btrfs_set_inode_generation(leaf, item, inode->i_generation);
1086 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
1087 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
1088 btrfs_set_inode_block_group(leaf, item,
1089 BTRFS_I(inode)->block_group->key.objectid);
1090 }
1091
1092 int noinline btrfs_update_inode(struct btrfs_trans_handle *trans,
1093 struct btrfs_root *root,
1094 struct inode *inode)
1095 {
1096 struct btrfs_inode_item *inode_item;
1097 struct btrfs_path *path;
1098 struct extent_buffer *leaf;
1099 int ret;
1100
1101 path = btrfs_alloc_path();
1102 BUG_ON(!path);
1103 ret = btrfs_lookup_inode(trans, root, path,
1104 &BTRFS_I(inode)->location, 1);
1105 if (ret) {
1106 if (ret > 0)
1107 ret = -ENOENT;
1108 goto failed;
1109 }
1110
1111 leaf = path->nodes[0];
1112 inode_item = btrfs_item_ptr(leaf, path->slots[0],
1113 struct btrfs_inode_item);
1114
1115 fill_inode_item(leaf, inode_item, inode);
1116 btrfs_mark_buffer_dirty(leaf);
1117 btrfs_set_inode_last_trans(trans, inode);
1118 ret = 0;
1119 failed:
1120 btrfs_free_path(path);
1121 return ret;
1122 }
1123
1124
1125 static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
1126 struct btrfs_root *root,
1127 struct inode *dir,
1128 struct dentry *dentry)
1129 {
1130 struct btrfs_path *path;
1131 const char *name = dentry->d_name.name;
1132 int name_len = dentry->d_name.len;
1133 int ret = 0;
1134 struct extent_buffer *leaf;
1135 struct btrfs_dir_item *di;
1136 struct btrfs_key key;
1137 u64 index;
1138
1139 path = btrfs_alloc_path();
1140 if (!path) {
1141 ret = -ENOMEM;
1142 goto err;
1143 }
1144
1145 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
1146 name, name_len, -1);
1147 if (IS_ERR(di)) {
1148 ret = PTR_ERR(di);
1149 goto err;
1150 }
1151 if (!di) {
1152 ret = -ENOENT;
1153 goto err;
1154 }
1155 leaf = path->nodes[0];
1156 btrfs_dir_item_key_to_cpu(leaf, di, &key);
1157 ret = btrfs_delete_one_dir_name(trans, root, path, di);
1158 if (ret)
1159 goto err;
1160 btrfs_release_path(root, path);
1161
1162 ret = btrfs_del_inode_ref(trans, root, name, name_len,
1163 dentry->d_inode->i_ino,
1164 dentry->d_parent->d_inode->i_ino, &index);
1165 if (ret) {
1166 printk("failed to delete reference to %.*s, "
1167 "inode %lu parent %lu\n", name_len, name,
1168 dentry->d_inode->i_ino,
1169 dentry->d_parent->d_inode->i_ino);
1170 goto err;
1171 }
1172
1173 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
1174 index, name, name_len, -1);
1175 if (IS_ERR(di)) {
1176 ret = PTR_ERR(di);
1177 goto err;
1178 }
1179 if (!di) {
1180 ret = -ENOENT;
1181 goto err;
1182 }
1183 ret = btrfs_delete_one_dir_name(trans, root, path, di);
1184 btrfs_release_path(root, path);
1185
1186 dentry->d_inode->i_ctime = dir->i_ctime;
1187 err:
1188 btrfs_free_path(path);
1189 if (!ret) {
1190 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
1191 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1192 btrfs_update_inode(trans, root, dir);
1193 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1194 dentry->d_inode->i_nlink--;
1195 #else
1196 drop_nlink(dentry->d_inode);
1197 #endif
1198 ret = btrfs_update_inode(trans, root, dentry->d_inode);
1199 dir->i_sb->s_dirt = 1;
1200 }
1201 return ret;
1202 }
1203
1204 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
1205 {
1206 struct btrfs_root *root;
1207 struct btrfs_trans_handle *trans;
1208 struct inode *inode = dentry->d_inode;
1209 int ret;
1210 unsigned long nr = 0;
1211
1212 root = BTRFS_I(dir)->root;
1213
1214 ret = btrfs_check_free_space(root, 1, 1);
1215 if (ret)
1216 goto fail;
1217
1218 trans = btrfs_start_transaction(root, 1);
1219
1220 btrfs_set_trans_block_group(trans, dir);
1221 ret = btrfs_unlink_trans(trans, root, dir, dentry);
1222
1223 if (inode->i_nlink == 0)
1224 ret = btrfs_orphan_add(trans, inode);
1225
1226 nr = trans->blocks_used;
1227
1228 btrfs_end_transaction_throttle(trans, root);
1229 fail:
1230 btrfs_btree_balance_dirty(root, nr);
1231 return ret;
1232 }
1233
1234 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
1235 {
1236 struct inode *inode = dentry->d_inode;
1237 int err = 0;
1238 int ret;
1239 struct btrfs_root *root = BTRFS_I(dir)->root;
1240 struct btrfs_trans_handle *trans;
1241 unsigned long nr = 0;
1242
1243 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
1244 return -ENOTEMPTY;
1245 }
1246
1247 ret = btrfs_check_free_space(root, 1, 1);
1248 if (ret)
1249 goto fail;
1250
1251 trans = btrfs_start_transaction(root, 1);
1252 btrfs_set_trans_block_group(trans, dir);
1253
1254 err = btrfs_orphan_add(trans, inode);
1255 if (err)
1256 goto fail_trans;
1257
1258 /* now the directory is empty */
1259 err = btrfs_unlink_trans(trans, root, dir, dentry);
1260 if (!err) {
1261 btrfs_i_size_write(inode, 0);
1262 }
1263
1264 fail_trans:
1265 nr = trans->blocks_used;
1266 ret = btrfs_end_transaction_throttle(trans, root);
1267 fail:
1268 btrfs_btree_balance_dirty(root, nr);
1269
1270 if (ret && !err)
1271 err = ret;
1272 return err;
1273 }
1274
1275 /*
1276 * this can truncate away extent items, csum items and directory items.
1277 * It starts at a high offset and removes keys until it can't find
1278 * any higher than i_size.
1279 *
1280 * csum items that cross the new i_size are truncated to the new size
1281 * as well.
1282 *
1283 * min_type is the minimum key type to truncate down to. If set to 0, this
1284 * will kill all the items on this inode, including the INODE_ITEM_KEY.
1285 */
1286 static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
1287 struct btrfs_root *root,
1288 struct inode *inode,
1289 u32 min_type)
1290 {
1291 int ret;
1292 struct btrfs_path *path;
1293 struct btrfs_key key;
1294 struct btrfs_key found_key;
1295 u32 found_type;
1296 struct extent_buffer *leaf;
1297 struct btrfs_file_extent_item *fi;
1298 u64 extent_start = 0;
1299 u64 extent_num_bytes = 0;
1300 u64 item_end = 0;
1301 u64 root_gen = 0;
1302 u64 root_owner = 0;
1303 int found_extent;
1304 int del_item;
1305 int pending_del_nr = 0;
1306 int pending_del_slot = 0;
1307 int extent_type = -1;
1308 u64 mask = root->sectorsize - 1;
1309
1310 btrfs_drop_extent_cache(inode, inode->i_size & (~mask), (u64)-1);
1311 path = btrfs_alloc_path();
1312 path->reada = -1;
1313 BUG_ON(!path);
1314
1315 /* FIXME, add redo link to tree so we don't leak on crash */
1316 key.objectid = inode->i_ino;
1317 key.offset = (u64)-1;
1318 key.type = (u8)-1;
1319
1320 btrfs_init_path(path);
1321 search_again:
1322 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1323 if (ret < 0) {
1324 goto error;
1325 }
1326 if (ret > 0) {
1327 BUG_ON(path->slots[0] == 0);
1328 path->slots[0]--;
1329 }
1330
1331 while(1) {
1332 fi = NULL;
1333 leaf = path->nodes[0];
1334 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1335 found_type = btrfs_key_type(&found_key);
1336
1337 if (found_key.objectid != inode->i_ino)
1338 break;
1339
1340 if (found_type < min_type)
1341 break;
1342
1343 item_end = found_key.offset;
1344 if (found_type == BTRFS_EXTENT_DATA_KEY) {
1345 fi = btrfs_item_ptr(leaf, path->slots[0],
1346 struct btrfs_file_extent_item);
1347 extent_type = btrfs_file_extent_type(leaf, fi);
1348 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
1349 item_end +=
1350 btrfs_file_extent_num_bytes(leaf, fi);
1351 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1352 struct btrfs_item *item = btrfs_item_nr(leaf,
1353 path->slots[0]);
1354 item_end += btrfs_file_extent_inline_len(leaf,
1355 item);
1356 }
1357 item_end--;
1358 }
1359 if (found_type == BTRFS_CSUM_ITEM_KEY) {
1360 ret = btrfs_csum_truncate(trans, root, path,
1361 inode->i_size);
1362 BUG_ON(ret);
1363 }
1364 if (item_end < inode->i_size) {
1365 if (found_type == BTRFS_DIR_ITEM_KEY) {
1366 found_type = BTRFS_INODE_ITEM_KEY;
1367 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
1368 found_type = BTRFS_CSUM_ITEM_KEY;
1369 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
1370 found_type = BTRFS_XATTR_ITEM_KEY;
1371 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
1372 found_type = BTRFS_INODE_REF_KEY;
1373 } else if (found_type) {
1374 found_type--;
1375 } else {
1376 break;
1377 }
1378 btrfs_set_key_type(&key, found_type);
1379 goto next;
1380 }
1381 if (found_key.offset >= inode->i_size)
1382 del_item = 1;
1383 else
1384 del_item = 0;
1385 found_extent = 0;
1386
1387 /* FIXME, shrink the extent if the ref count is only 1 */
1388 if (found_type != BTRFS_EXTENT_DATA_KEY)
1389 goto delete;
1390
1391 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
1392 u64 num_dec;
1393 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
1394 if (!del_item) {
1395 u64 orig_num_bytes =
1396 btrfs_file_extent_num_bytes(leaf, fi);
1397 extent_num_bytes = inode->i_size -
1398 found_key.offset + root->sectorsize - 1;
1399 extent_num_bytes = extent_num_bytes &
1400 ~((u64)root->sectorsize - 1);
1401 btrfs_set_file_extent_num_bytes(leaf, fi,
1402 extent_num_bytes);
1403 num_dec = (orig_num_bytes -
1404 extent_num_bytes);
1405 if (extent_start != 0)
1406 dec_i_blocks(inode, num_dec);
1407 btrfs_mark_buffer_dirty(leaf);
1408 } else {
1409 extent_num_bytes =
1410 btrfs_file_extent_disk_num_bytes(leaf,
1411 fi);
1412 /* FIXME blocksize != 4096 */
1413 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
1414 if (extent_start != 0) {
1415 found_extent = 1;
1416 dec_i_blocks(inode, num_dec);
1417 }
1418 root_gen = btrfs_header_generation(leaf);
1419 root_owner = btrfs_header_owner(leaf);
1420 }
1421 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1422 if (!del_item) {
1423 u32 newsize = inode->i_size - found_key.offset;
1424 dec_i_blocks(inode, item_end + 1 -
1425 found_key.offset - newsize);
1426 newsize =
1427 btrfs_file_extent_calc_inline_size(newsize);
1428 ret = btrfs_truncate_item(trans, root, path,
1429 newsize, 1);
1430 BUG_ON(ret);
1431 } else {
1432 dec_i_blocks(inode, item_end + 1 -
1433 found_key.offset);
1434 }
1435 }
1436 delete:
1437 if (del_item) {
1438 if (!pending_del_nr) {
1439 /* no pending yet, add ourselves */
1440 pending_del_slot = path->slots[0];
1441 pending_del_nr = 1;
1442 } else if (pending_del_nr &&
1443 path->slots[0] + 1 == pending_del_slot) {
1444 /* hop on the pending chunk */
1445 pending_del_nr++;
1446 pending_del_slot = path->slots[0];
1447 } else {
1448 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
1449 }
1450 } else {
1451 break;
1452 }
1453 if (found_extent) {
1454 ret = btrfs_free_extent(trans, root, extent_start,
1455 extent_num_bytes,
1456 root_owner,
1457 root_gen, inode->i_ino,
1458 found_key.offset, 0);
1459 BUG_ON(ret);
1460 }
1461 next:
1462 if (path->slots[0] == 0) {
1463 if (pending_del_nr)
1464 goto del_pending;
1465 btrfs_release_path(root, path);
1466 goto search_again;
1467 }
1468
1469 path->slots[0]--;
1470 if (pending_del_nr &&
1471 path->slots[0] + 1 != pending_del_slot) {
1472 struct btrfs_key debug;
1473 del_pending:
1474 btrfs_item_key_to_cpu(path->nodes[0], &debug,
1475 pending_del_slot);
1476 ret = btrfs_del_items(trans, root, path,
1477 pending_del_slot,
1478 pending_del_nr);
1479 BUG_ON(ret);
1480 pending_del_nr = 0;
1481 btrfs_release_path(root, path);
1482 goto search_again;
1483 }
1484 }
1485 ret = 0;
1486 error:
1487 if (pending_del_nr) {
1488 ret = btrfs_del_items(trans, root, path, pending_del_slot,
1489 pending_del_nr);
1490 }
1491 btrfs_free_path(path);
1492 inode->i_sb->s_dirt = 1;
1493 return ret;
1494 }
1495
1496 /*
1497 * taken from block_truncate_page, but does cow as it zeros out
1498 * any bytes left in the last page in the file.
1499 */
1500 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
1501 {
1502 struct inode *inode = mapping->host;
1503 struct btrfs_root *root = BTRFS_I(inode)->root;
1504 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1505 struct btrfs_ordered_extent *ordered;
1506 char *kaddr;
1507 u32 blocksize = root->sectorsize;
1508 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1509 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1510 struct page *page;
1511 int ret = 0;
1512 u64 page_start;
1513 u64 page_end;
1514
1515 if ((offset & (blocksize - 1)) == 0)
1516 goto out;
1517
1518 ret = -ENOMEM;
1519 again:
1520 page = grab_cache_page(mapping, index);
1521 if (!page)
1522 goto out;
1523
1524 page_start = page_offset(page);
1525 page_end = page_start + PAGE_CACHE_SIZE - 1;
1526
1527 if (!PageUptodate(page)) {
1528 ret = btrfs_readpage(NULL, page);
1529 lock_page(page);
1530 if (page->mapping != mapping) {
1531 unlock_page(page);
1532 page_cache_release(page);
1533 goto again;
1534 }
1535 if (!PageUptodate(page)) {
1536 ret = -EIO;
1537 goto out_unlock;
1538 }
1539 }
1540 wait_on_page_writeback(page);
1541
1542 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
1543 set_page_extent_mapped(page);
1544
1545 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1546 if (ordered) {
1547 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
1548 unlock_page(page);
1549 page_cache_release(page);
1550 btrfs_start_ordered_extent(inode, ordered, 1);
1551 btrfs_put_ordered_extent(ordered);
1552 goto again;
1553 }
1554
1555 btrfs_set_extent_delalloc(inode, page_start, page_end);
1556 ret = 0;
1557 if (offset != PAGE_CACHE_SIZE) {
1558 kaddr = kmap(page);
1559 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1560 flush_dcache_page(page);
1561 kunmap(page);
1562 }
1563 ClearPageChecked(page);
1564 set_page_dirty(page);
1565 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
1566
1567 out_unlock:
1568 unlock_page(page);
1569 page_cache_release(page);
1570 out:
1571 return ret;
1572 }
1573
1574 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1575 {
1576 struct inode *inode = dentry->d_inode;
1577 int err;
1578
1579 err = inode_change_ok(inode, attr);
1580 if (err)
1581 return err;
1582
1583 if (S_ISREG(inode->i_mode) &&
1584 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1585 struct btrfs_trans_handle *trans;
1586 struct btrfs_root *root = BTRFS_I(inode)->root;
1587 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1588
1589 u64 mask = root->sectorsize - 1;
1590 u64 hole_start = (inode->i_size + mask) & ~mask;
1591 u64 block_end = (attr->ia_size + mask) & ~mask;
1592 u64 hole_size;
1593 u64 alloc_hint = 0;
1594
1595 if (attr->ia_size <= hole_start)
1596 goto out;
1597
1598 err = btrfs_check_free_space(root, 1, 0);
1599 if (err)
1600 goto fail;
1601
1602 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1603
1604 hole_size = block_end - hole_start;
1605 while(1) {
1606 struct btrfs_ordered_extent *ordered;
1607 btrfs_wait_ordered_range(inode, hole_start, hole_size);
1608
1609 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1610 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
1611 if (ordered) {
1612 unlock_extent(io_tree, hole_start,
1613 block_end - 1, GFP_NOFS);
1614 btrfs_put_ordered_extent(ordered);
1615 } else {
1616 break;
1617 }
1618 }
1619
1620 trans = btrfs_start_transaction(root, 1);
1621 btrfs_set_trans_block_group(trans, inode);
1622 mutex_lock(&BTRFS_I(inode)->extent_mutex);
1623 err = btrfs_drop_extents(trans, root, inode,
1624 hole_start, block_end, hole_start,
1625 &alloc_hint);
1626
1627 if (alloc_hint != EXTENT_MAP_INLINE) {
1628 err = btrfs_insert_file_extent(trans, root,
1629 inode->i_ino,
1630 hole_start, 0, 0,
1631 hole_size, 0);
1632 btrfs_drop_extent_cache(inode, hole_start,
1633 (u64)-1);
1634 btrfs_check_file(root, inode);
1635 }
1636 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
1637 btrfs_end_transaction(trans, root);
1638 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1639 if (err)
1640 return err;
1641 }
1642 out:
1643 err = inode_setattr(inode, attr);
1644
1645 if (!err && ((attr->ia_valid & ATTR_MODE)))
1646 err = btrfs_acl_chmod(inode);
1647 fail:
1648 return err;
1649 }
1650
1651 void btrfs_delete_inode(struct inode *inode)
1652 {
1653 struct btrfs_trans_handle *trans;
1654 struct btrfs_root *root = BTRFS_I(inode)->root;
1655 unsigned long nr;
1656 int ret;
1657
1658 truncate_inode_pages(&inode->i_data, 0);
1659 if (is_bad_inode(inode)) {
1660 btrfs_orphan_del(NULL, inode);
1661 goto no_delete;
1662 }
1663 btrfs_wait_ordered_range(inode, 0, (u64)-1);
1664
1665 btrfs_i_size_write(inode, 0);
1666 trans = btrfs_start_transaction(root, 1);
1667
1668 btrfs_set_trans_block_group(trans, inode);
1669 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
1670 if (ret) {
1671 btrfs_orphan_del(NULL, inode);
1672 goto no_delete_lock;
1673 }
1674
1675 btrfs_orphan_del(trans, inode);
1676
1677 nr = trans->blocks_used;
1678 clear_inode(inode);
1679
1680 btrfs_end_transaction(trans, root);
1681 btrfs_btree_balance_dirty(root, nr);
1682 return;
1683
1684 no_delete_lock:
1685 nr = trans->blocks_used;
1686 btrfs_end_transaction(trans, root);
1687 btrfs_btree_balance_dirty(root, nr);
1688 no_delete:
1689 clear_inode(inode);
1690 }
1691
1692 /*
1693 * this returns the key found in the dir entry in the location pointer.
1694 * If no dir entries were found, location->objectid is 0.
1695 */
1696 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1697 struct btrfs_key *location)
1698 {
1699 const char *name = dentry->d_name.name;
1700 int namelen = dentry->d_name.len;
1701 struct btrfs_dir_item *di;
1702 struct btrfs_path *path;
1703 struct btrfs_root *root = BTRFS_I(dir)->root;
1704 int ret = 0;
1705
1706 path = btrfs_alloc_path();
1707 BUG_ON(!path);
1708
1709 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1710 namelen, 0);
1711 if (IS_ERR(di))
1712 ret = PTR_ERR(di);
1713 if (!di || IS_ERR(di)) {
1714 goto out_err;
1715 }
1716 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
1717 out:
1718 btrfs_free_path(path);
1719 return ret;
1720 out_err:
1721 location->objectid = 0;
1722 goto out;
1723 }
1724
1725 /*
1726 * when we hit a tree root in a directory, the btrfs part of the inode
1727 * needs to be changed to reflect the root directory of the tree root. This
1728 * is kind of like crossing a mount point.
1729 */
1730 static int fixup_tree_root_location(struct btrfs_root *root,
1731 struct btrfs_key *location,
1732 struct btrfs_root **sub_root,
1733 struct dentry *dentry)
1734 {
1735 struct btrfs_root_item *ri;
1736
1737 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1738 return 0;
1739 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1740 return 0;
1741
1742 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1743 dentry->d_name.name,
1744 dentry->d_name.len);
1745 if (IS_ERR(*sub_root))
1746 return PTR_ERR(*sub_root);
1747
1748 ri = &(*sub_root)->root_item;
1749 location->objectid = btrfs_root_dirid(ri);
1750 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1751 location->offset = 0;
1752
1753 return 0;
1754 }
1755
1756 static int btrfs_init_locked_inode(struct inode *inode, void *p)
1757 {
1758 struct btrfs_iget_args *args = p;
1759 inode->i_ino = args->ino;
1760 BTRFS_I(inode)->root = args->root;
1761 BTRFS_I(inode)->delalloc_bytes = 0;
1762 inode->i_mapping->writeback_index = 0;
1763 BTRFS_I(inode)->disk_i_size = 0;
1764 BTRFS_I(inode)->index_cnt = (u64)-1;
1765 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1766 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1767 inode->i_mapping, GFP_NOFS);
1768 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1769 inode->i_mapping, GFP_NOFS);
1770 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
1771 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
1772 mutex_init(&BTRFS_I(inode)->csum_mutex);
1773 mutex_init(&BTRFS_I(inode)->extent_mutex);
1774 return 0;
1775 }
1776
1777 static int btrfs_find_actor(struct inode *inode, void *opaque)
1778 {
1779 struct btrfs_iget_args *args = opaque;
1780 return (args->ino == inode->i_ino &&
1781 args->root == BTRFS_I(inode)->root);
1782 }
1783
1784 struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1785 u64 root_objectid)
1786 {
1787 struct btrfs_iget_args args;
1788 args.ino = objectid;
1789 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1790
1791 if (!args.root)
1792 return NULL;
1793
1794 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1795 }
1796
1797 struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1798 struct btrfs_root *root)
1799 {
1800 struct inode *inode;
1801 struct btrfs_iget_args args;
1802 args.ino = objectid;
1803 args.root = root;
1804
1805 inode = iget5_locked(s, objectid, btrfs_find_actor,
1806 btrfs_init_locked_inode,
1807 (void *)&args);
1808 return inode;
1809 }
1810
1811 /* Get an inode object given its location and corresponding root.
1812 * Returns in *is_new if the inode was read from disk
1813 */
1814 struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
1815 struct btrfs_root *root, int *is_new)
1816 {
1817 struct inode *inode;
1818
1819 inode = btrfs_iget_locked(s, location->objectid, root);
1820 if (!inode)
1821 return ERR_PTR(-EACCES);
1822
1823 if (inode->i_state & I_NEW) {
1824 BTRFS_I(inode)->root = root;
1825 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
1826 btrfs_read_locked_inode(inode);
1827 unlock_new_inode(inode);
1828 if (is_new)
1829 *is_new = 1;
1830 } else {
1831 if (is_new)
1832 *is_new = 0;
1833 }
1834
1835 return inode;
1836 }
1837
1838 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1839 struct nameidata *nd)
1840 {
1841 struct inode * inode;
1842 struct btrfs_inode *bi = BTRFS_I(dir);
1843 struct btrfs_root *root = bi->root;
1844 struct btrfs_root *sub_root = root;
1845 struct btrfs_key location;
1846 int ret, new, do_orphan = 0;
1847
1848 if (dentry->d_name.len > BTRFS_NAME_LEN)
1849 return ERR_PTR(-ENAMETOOLONG);
1850
1851 ret = btrfs_inode_by_name(dir, dentry, &location);
1852
1853 if (ret < 0)
1854 return ERR_PTR(ret);
1855
1856 inode = NULL;
1857 if (location.objectid) {
1858 ret = fixup_tree_root_location(root, &location, &sub_root,
1859 dentry);
1860 if (ret < 0)
1861 return ERR_PTR(ret);
1862 if (ret > 0)
1863 return ERR_PTR(-ENOENT);
1864 inode = btrfs_iget(dir->i_sb, &location, sub_root, &new);
1865 if (IS_ERR(inode))
1866 return ERR_CAST(inode);
1867
1868 /* the inode and parent dir are two different roots */
1869 if (new && root != sub_root) {
1870 igrab(inode);
1871 sub_root->inode = inode;
1872 do_orphan = 1;
1873 }
1874 }
1875
1876 if (unlikely(do_orphan))
1877 btrfs_orphan_cleanup(sub_root);
1878
1879 return d_splice_alias(inode, dentry);
1880 }
1881
1882 static unsigned char btrfs_filetype_table[] = {
1883 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1884 };
1885
1886 static int btrfs_real_readdir(struct file *filp, void *dirent,
1887 filldir_t filldir)
1888 {
1889 struct inode *inode = filp->f_dentry->d_inode;
1890 struct btrfs_root *root = BTRFS_I(inode)->root;
1891 struct btrfs_item *item;
1892 struct btrfs_dir_item *di;
1893 struct btrfs_key key;
1894 struct btrfs_key found_key;
1895 struct btrfs_path *path;
1896 int ret;
1897 u32 nritems;
1898 struct extent_buffer *leaf;
1899 int slot;
1900 int advance;
1901 unsigned char d_type;
1902 int over = 0;
1903 u32 di_cur;
1904 u32 di_total;
1905 u32 di_len;
1906 int key_type = BTRFS_DIR_INDEX_KEY;
1907 char tmp_name[32];
1908 char *name_ptr;
1909 int name_len;
1910
1911 /* FIXME, use a real flag for deciding about the key type */
1912 if (root->fs_info->tree_root == root)
1913 key_type = BTRFS_DIR_ITEM_KEY;
1914
1915 /* special case for "." */
1916 if (filp->f_pos == 0) {
1917 over = filldir(dirent, ".", 1,
1918 1, inode->i_ino,
1919 DT_DIR);
1920 if (over)
1921 return 0;
1922 filp->f_pos = 1;
1923 }
1924 /* special case for .., just use the back ref */
1925 if (filp->f_pos == 1) {
1926 u64 pino = parent_ino(filp->f_path.dentry);
1927 over = filldir(dirent, "..", 2,
1928 2, pino, DT_DIR);
1929 if (over)
1930 return 0;
1931 filp->f_pos = 2;
1932 }
1933
1934 path = btrfs_alloc_path();
1935 path->reada = 2;
1936
1937 btrfs_set_key_type(&key, key_type);
1938 key.offset = filp->f_pos;
1939 key.objectid = inode->i_ino;
1940
1941 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1942 if (ret < 0)
1943 goto err;
1944 advance = 0;
1945
1946 while (1) {
1947 leaf = path->nodes[0];
1948 nritems = btrfs_header_nritems(leaf);
1949 slot = path->slots[0];
1950 if (advance || slot >= nritems) {
1951 if (slot >= nritems - 1) {
1952 ret = btrfs_next_leaf(root, path);
1953 if (ret)
1954 break;
1955 leaf = path->nodes[0];
1956 nritems = btrfs_header_nritems(leaf);
1957 slot = path->slots[0];
1958 } else {
1959 slot++;
1960 path->slots[0]++;
1961 }
1962 }
1963 advance = 1;
1964 item = btrfs_item_nr(leaf, slot);
1965 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1966
1967 if (found_key.objectid != key.objectid)
1968 break;
1969 if (btrfs_key_type(&found_key) != key_type)
1970 break;
1971 if (found_key.offset < filp->f_pos)
1972 continue;
1973
1974 filp->f_pos = found_key.offset;
1975
1976 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1977 di_cur = 0;
1978 di_total = btrfs_item_size(leaf, item);
1979
1980 while (di_cur < di_total) {
1981 struct btrfs_key location;
1982
1983 name_len = btrfs_dir_name_len(leaf, di);
1984 if (name_len <= sizeof(tmp_name)) {
1985 name_ptr = tmp_name;
1986 } else {
1987 name_ptr = kmalloc(name_len, GFP_NOFS);
1988 if (!name_ptr) {
1989 ret = -ENOMEM;
1990 goto err;
1991 }
1992 }
1993 read_extent_buffer(leaf, name_ptr,
1994 (unsigned long)(di + 1), name_len);
1995
1996 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1997 btrfs_dir_item_key_to_cpu(leaf, di, &location);
1998 over = filldir(dirent, name_ptr, name_len,
1999 found_key.offset, location.objectid,
2000 d_type);
2001
2002 if (name_ptr != tmp_name)
2003 kfree(name_ptr);
2004
2005 if (over)
2006 goto nopos;
2007
2008 di_len = btrfs_dir_name_len(leaf, di) +
2009 btrfs_dir_data_len(leaf, di) + sizeof(*di);
2010 di_cur += di_len;
2011 di = (struct btrfs_dir_item *)((char *)di + di_len);
2012 }
2013 }
2014
2015 /* Reached end of directory/root. Bump pos past the last item. */
2016 if (key_type == BTRFS_DIR_INDEX_KEY)
2017 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
2018 else
2019 filp->f_pos++;
2020 nopos:
2021 ret = 0;
2022 err:
2023 btrfs_free_path(path);
2024 return ret;
2025 }
2026
2027 /* Kernels earlier than 2.6.28 still have the NFS deadlock where nfsd
2028 will call the file system's ->lookup() method from within its
2029 filldir callback, which in turn was called from the file system's
2030 ->readdir() method. And will deadlock for many file systems. */
2031 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
2032
2033 struct nfshack_dirent {
2034 u64 ino;
2035 loff_t offset;
2036 int namlen;
2037 unsigned int d_type;
2038 char name[];
2039 };
2040
2041 struct nfshack_readdir {
2042 char *dirent;
2043 size_t used;
2044 int full;
2045 };
2046
2047
2048
2049 static int btrfs_nfshack_filldir(void *__buf, const char *name, int namlen,
2050 loff_t offset, u64 ino, unsigned int d_type)
2051 {
2052 struct nfshack_readdir *buf = __buf;
2053 struct nfshack_dirent *de = (void *)(buf->dirent + buf->used);
2054 unsigned int reclen;
2055
2056 reclen = ALIGN(sizeof(struct nfshack_dirent) + namlen, sizeof(u64));
2057 if (buf->used + reclen > PAGE_SIZE) {
2058 buf->full = 1;
2059 return -EINVAL;
2060 }
2061
2062 de->namlen = namlen;
2063 de->offset = offset;
2064 de->ino = ino;
2065 de->d_type = d_type;
2066 memcpy(de->name, name, namlen);
2067 buf->used += reclen;
2068
2069 return 0;
2070 }
2071
2072 static int btrfs_nfshack_readdir(struct file *file, void *dirent,
2073 filldir_t filldir)
2074 {
2075 struct nfshack_readdir buf;
2076 struct nfshack_dirent *de;
2077 int err;
2078 int size;
2079 loff_t offset;
2080
2081 buf.dirent = (void *)__get_free_page(GFP_KERNEL);
2082 if (!buf.dirent)
2083 return -ENOMEM;
2084
2085 offset = file->f_pos;
2086
2087 do {
2088 unsigned int reclen;
2089
2090 buf.used = 0;
2091 buf.full = 0;
2092 err = btrfs_real_readdir(file, &buf, btrfs_nfshack_filldir);
2093 if (err)
2094 break;
2095
2096 size = buf.used;
2097
2098 if (!size)
2099 break;
2100
2101 de = (struct nfshack_dirent *)buf.dirent;
2102 while (size > 0) {
2103 offset = de->offset;
2104
2105 if (filldir(dirent, de->name, de->namlen, de->offset,
2106 de->ino, de->d_type))
2107 goto done;
2108 offset = file->f_pos;
2109
2110 reclen = ALIGN(sizeof(*de) + de->namlen,
2111 sizeof(u64));
2112 size -= reclen;
2113 de = (struct nfshack_dirent *)((char *)de + reclen);
2114 }
2115 } while (buf.full);
2116
2117 done:
2118 free_page((unsigned long)buf.dirent);
2119 file->f_pos = offset;
2120
2121 return err;
2122 }
2123 #endif
2124
2125 int btrfs_write_inode(struct inode *inode, int wait)
2126 {
2127 struct btrfs_root *root = BTRFS_I(inode)->root;
2128 struct btrfs_trans_handle *trans;
2129 int ret = 0;
2130
2131 if (root->fs_info->closing > 1)
2132 return 0;
2133
2134 if (wait) {
2135 trans = btrfs_join_transaction(root, 1);
2136 btrfs_set_trans_block_group(trans, inode);
2137 ret = btrfs_commit_transaction(trans, root);
2138 }
2139 return ret;
2140 }
2141
2142 /*
2143 * This is somewhat expensive, updating the tree every time the
2144 * inode changes. But, it is most likely to find the inode in cache.
2145 * FIXME, needs more benchmarking...there are no reasons other than performance
2146 * to keep or drop this code.
2147 */
2148 void btrfs_dirty_inode(struct inode *inode)
2149 {
2150 struct btrfs_root *root = BTRFS_I(inode)->root;
2151 struct btrfs_trans_handle *trans;
2152
2153 trans = btrfs_join_transaction(root, 1);
2154 btrfs_set_trans_block_group(trans, inode);
2155 btrfs_update_inode(trans, root, inode);
2156 btrfs_end_transaction(trans, root);
2157 }
2158
2159 static int btrfs_set_inode_index_count(struct inode *inode)
2160 {
2161 struct btrfs_root *root = BTRFS_I(inode)->root;
2162 struct btrfs_key key, found_key;
2163 struct btrfs_path *path;
2164 struct extent_buffer *leaf;
2165 int ret;
2166
2167 key.objectid = inode->i_ino;
2168 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
2169 key.offset = (u64)-1;
2170
2171 path = btrfs_alloc_path();
2172 if (!path)
2173 return -ENOMEM;
2174
2175 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2176 if (ret < 0)
2177 goto out;
2178 /* FIXME: we should be able to handle this */
2179 if (ret == 0)
2180 goto out;
2181 ret = 0;
2182
2183 /*
2184 * MAGIC NUMBER EXPLANATION:
2185 * since we search a directory based on f_pos we have to start at 2
2186 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
2187 * else has to start at 2
2188 */
2189 if (path->slots[0] == 0) {
2190 BTRFS_I(inode)->index_cnt = 2;
2191 goto out;
2192 }
2193
2194 path->slots[0]--;
2195
2196 leaf = path->nodes[0];
2197 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2198
2199 if (found_key.objectid != inode->i_ino ||
2200 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
2201 BTRFS_I(inode)->index_cnt = 2;
2202 goto out;
2203 }
2204
2205 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
2206 out:
2207 btrfs_free_path(path);
2208 return ret;
2209 }
2210
2211 static int btrfs_set_inode_index(struct inode *dir, struct inode *inode,
2212 u64 *index)
2213 {
2214 int ret = 0;
2215
2216 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
2217 ret = btrfs_set_inode_index_count(dir);
2218 if (ret)
2219 return ret;
2220 }
2221
2222 *index = BTRFS_I(dir)->index_cnt;
2223 BTRFS_I(dir)->index_cnt++;
2224
2225 return ret;
2226 }
2227
2228 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
2229 struct btrfs_root *root,
2230 struct inode *dir,
2231 const char *name, int name_len,
2232 u64 ref_objectid,
2233 u64 objectid,
2234 struct btrfs_block_group_cache *group,
2235 int mode, u64 *index)
2236 {
2237 struct inode *inode;
2238 struct btrfs_inode_item *inode_item;
2239 struct btrfs_block_group_cache *new_inode_group;
2240 struct btrfs_key *location;
2241 struct btrfs_path *path;
2242 struct btrfs_inode_ref *ref;
2243 struct btrfs_key key[2];
2244 u32 sizes[2];
2245 unsigned long ptr;
2246 int ret;
2247 int owner;
2248
2249 path = btrfs_alloc_path();
2250 BUG_ON(!path);
2251
2252 inode = new_inode(root->fs_info->sb);
2253 if (!inode)
2254 return ERR_PTR(-ENOMEM);
2255
2256 if (dir) {
2257 ret = btrfs_set_inode_index(dir, inode, index);
2258 if (ret)
2259 return ERR_PTR(ret);
2260 }
2261 /*
2262 * index_cnt is ignored for everything but a dir,
2263 * btrfs_get_inode_index_count has an explanation for the magic
2264 * number
2265 */
2266 BTRFS_I(inode)->index_cnt = 2;
2267
2268 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2269 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
2270 inode->i_mapping, GFP_NOFS);
2271 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2272 inode->i_mapping, GFP_NOFS);
2273 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
2274 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
2275 mutex_init(&BTRFS_I(inode)->csum_mutex);
2276 mutex_init(&BTRFS_I(inode)->extent_mutex);
2277 BTRFS_I(inode)->delalloc_bytes = 0;
2278 inode->i_mapping->writeback_index = 0;
2279 BTRFS_I(inode)->disk_i_size = 0;
2280 BTRFS_I(inode)->root = root;
2281
2282 if (mode & S_IFDIR)
2283 owner = 0;
2284 else
2285 owner = 1;
2286 new_inode_group = btrfs_find_block_group(root, group, 0,
2287 BTRFS_BLOCK_GROUP_METADATA, owner);
2288 if (!new_inode_group) {
2289 printk("find_block group failed\n");
2290 new_inode_group = group;
2291 }
2292 BTRFS_I(inode)->block_group = new_inode_group;
2293 BTRFS_I(inode)->flags = 0;
2294
2295 key[0].objectid = objectid;
2296 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
2297 key[0].offset = 0;
2298
2299 key[1].objectid = objectid;
2300 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
2301 key[1].offset = ref_objectid;
2302
2303 sizes[0] = sizeof(struct btrfs_inode_item);
2304 sizes[1] = name_len + sizeof(*ref);
2305
2306 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
2307 if (ret != 0)
2308 goto fail;
2309
2310 if (objectid > root->highest_inode)
2311 root->highest_inode = objectid;
2312
2313 inode->i_uid = current->fsuid;
2314 inode->i_gid = current->fsgid;
2315 inode->i_mode = mode;
2316 inode->i_ino = objectid;
2317 inode->i_blocks = 0;
2318 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2319 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2320 struct btrfs_inode_item);
2321 fill_inode_item(path->nodes[0], inode_item, inode);
2322
2323 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
2324 struct btrfs_inode_ref);
2325 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
2326 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
2327 ptr = (unsigned long)(ref + 1);
2328 write_extent_buffer(path->nodes[0], name, ptr, name_len);
2329
2330 btrfs_mark_buffer_dirty(path->nodes[0]);
2331 btrfs_free_path(path);
2332
2333 location = &BTRFS_I(inode)->location;
2334 location->objectid = objectid;
2335 location->offset = 0;
2336 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
2337
2338 insert_inode_hash(inode);
2339 return inode;
2340 fail:
2341 if (dir)
2342 BTRFS_I(dir)->index_cnt--;
2343 btrfs_free_path(path);
2344 return ERR_PTR(ret);
2345 }
2346
2347 static inline u8 btrfs_inode_type(struct inode *inode)
2348 {
2349 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
2350 }
2351
2352 static int btrfs_add_link(struct btrfs_trans_handle *trans,
2353 struct dentry *dentry, struct inode *inode,
2354 int add_backref, u64 index)
2355 {
2356 int ret;
2357 struct btrfs_key key;
2358 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
2359 struct inode *parent_inode = dentry->d_parent->d_inode;
2360
2361 key.objectid = inode->i_ino;
2362 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
2363 key.offset = 0;
2364
2365 ret = btrfs_insert_dir_item(trans, root,
2366 dentry->d_name.name, dentry->d_name.len,
2367 dentry->d_parent->d_inode->i_ino,
2368 &key, btrfs_inode_type(inode),
2369 index);
2370 if (ret == 0) {
2371 if (add_backref) {
2372 ret = btrfs_insert_inode_ref(trans, root,
2373 dentry->d_name.name,
2374 dentry->d_name.len,
2375 inode->i_ino,
2376 parent_inode->i_ino,
2377 index);
2378 }
2379 btrfs_i_size_write(parent_inode, parent_inode->i_size +
2380 dentry->d_name.len * 2);
2381 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
2382 ret = btrfs_update_inode(trans, root,
2383 dentry->d_parent->d_inode);
2384 }
2385 return ret;
2386 }
2387
2388 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
2389 struct dentry *dentry, struct inode *inode,
2390 int backref, u64 index)
2391 {
2392 int err = btrfs_add_link(trans, dentry, inode, backref, index);
2393 if (!err) {
2394 d_instantiate(dentry, inode);
2395 return 0;
2396 }
2397 if (err > 0)
2398 err = -EEXIST;
2399 return err;
2400 }
2401
2402 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
2403 int mode, dev_t rdev)
2404 {
2405 struct btrfs_trans_handle *trans;
2406 struct btrfs_root *root = BTRFS_I(dir)->root;
2407 struct inode *inode = NULL;
2408 int err;
2409 int drop_inode = 0;
2410 u64 objectid;
2411 unsigned long nr = 0;
2412 u64 index = 0;
2413
2414 if (!new_valid_dev(rdev))
2415 return -EINVAL;
2416
2417 err = btrfs_check_free_space(root, 1, 0);
2418 if (err)
2419 goto fail;
2420
2421 trans = btrfs_start_transaction(root, 1);
2422 btrfs_set_trans_block_group(trans, dir);
2423
2424 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2425 if (err) {
2426 err = -ENOSPC;
2427 goto out_unlock;
2428 }
2429
2430 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
2431 dentry->d_name.len,
2432 dentry->d_parent->d_inode->i_ino, objectid,
2433 BTRFS_I(dir)->block_group, mode, &index);
2434 err = PTR_ERR(inode);
2435 if (IS_ERR(inode))
2436 goto out_unlock;
2437
2438 err = btrfs_init_acl(inode, dir);
2439 if (err) {
2440 drop_inode = 1;
2441 goto out_unlock;
2442 }
2443
2444 btrfs_set_trans_block_group(trans, inode);
2445 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
2446 if (err)
2447 drop_inode = 1;
2448 else {
2449 inode->i_op = &btrfs_special_inode_operations;
2450 init_special_inode(inode, inode->i_mode, rdev);
2451 btrfs_update_inode(trans, root, inode);
2452 }
2453 dir->i_sb->s_dirt = 1;
2454 btrfs_update_inode_block_group(trans, inode);
2455 btrfs_update_inode_block_group(trans, dir);
2456 out_unlock:
2457 nr = trans->blocks_used;
2458 btrfs_end_transaction_throttle(trans, root);
2459 fail:
2460 if (drop_inode) {
2461 inode_dec_link_count(inode);
2462 iput(inode);
2463 }
2464 btrfs_btree_balance_dirty(root, nr);
2465 return err;
2466 }
2467
2468 static int btrfs_create(struct inode *dir, struct dentry *dentry,
2469 int mode, struct nameidata *nd)
2470 {
2471 struct btrfs_trans_handle *trans;
2472 struct btrfs_root *root = BTRFS_I(dir)->root;
2473 struct inode *inode = NULL;
2474 int err;
2475 int drop_inode = 0;
2476 unsigned long nr = 0;
2477 u64 objectid;
2478 u64 index = 0;
2479
2480 err = btrfs_check_free_space(root, 1, 0);
2481 if (err)
2482 goto fail;
2483 trans = btrfs_start_transaction(root, 1);
2484 btrfs_set_trans_block_group(trans, dir);
2485
2486 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2487 if (err) {
2488 err = -ENOSPC;
2489 goto out_unlock;
2490 }
2491
2492 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
2493 dentry->d_name.len,
2494 dentry->d_parent->d_inode->i_ino,
2495 objectid, BTRFS_I(dir)->block_group, mode,
2496 &index);
2497 err = PTR_ERR(inode);
2498 if (IS_ERR(inode))
2499 goto out_unlock;
2500
2501 err = btrfs_init_acl(inode, dir);
2502 if (err) {
2503 drop_inode = 1;
2504 goto out_unlock;
2505 }
2506
2507 btrfs_set_trans_block_group(trans, inode);
2508 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
2509 if (err)
2510 drop_inode = 1;
2511 else {
2512 inode->i_mapping->a_ops = &btrfs_aops;
2513 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
2514 inode->i_fop = &btrfs_file_operations;
2515 inode->i_op = &btrfs_file_inode_operations;
2516 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2517 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
2518 inode->i_mapping, GFP_NOFS);
2519 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2520 inode->i_mapping, GFP_NOFS);
2521 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
2522 mutex_init(&BTRFS_I(inode)->csum_mutex);
2523 mutex_init(&BTRFS_I(inode)->extent_mutex);
2524 BTRFS_I(inode)->delalloc_bytes = 0;
2525 BTRFS_I(inode)->disk_i_size = 0;
2526 inode->i_mapping->writeback_index = 0;
2527 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
2528 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
2529 }
2530 dir->i_sb->s_dirt = 1;
2531 btrfs_update_inode_block_group(trans, inode);
2532 btrfs_update_inode_block_group(trans, dir);
2533 out_unlock:
2534 nr = trans->blocks_used;
2535 btrfs_end_transaction_throttle(trans, root);
2536 fail:
2537 if (drop_inode) {
2538 inode_dec_link_count(inode);
2539 iput(inode);
2540 }
2541 btrfs_btree_balance_dirty(root, nr);
2542 return err;
2543 }
2544
2545 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
2546 struct dentry *dentry)
2547 {
2548 struct btrfs_trans_handle *trans;
2549 struct btrfs_root *root = BTRFS_I(dir)->root;
2550 struct inode *inode = old_dentry->d_inode;
2551 u64 index;
2552 unsigned long nr = 0;
2553 int err;
2554 int drop_inode = 0;
2555
2556 if (inode->i_nlink == 0)
2557 return -ENOENT;
2558
2559 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2560 inode->i_nlink++;
2561 #else
2562 inc_nlink(inode);
2563 #endif
2564 err = btrfs_check_free_space(root, 1, 0);
2565 if (err)
2566 goto fail;
2567 err = btrfs_set_inode_index(dir, inode, &index);
2568 if (err)
2569 goto fail;
2570
2571 trans = btrfs_start_transaction(root, 1);
2572
2573 btrfs_set_trans_block_group(trans, dir);
2574 atomic_inc(&inode->i_count);
2575
2576 err = btrfs_add_nondir(trans, dentry, inode, 1, index);
2577
2578 if (err)
2579 drop_inode = 1;
2580
2581 dir->i_sb->s_dirt = 1;
2582 btrfs_update_inode_block_group(trans, dir);
2583 err = btrfs_update_inode(trans, root, inode);
2584
2585 if (err)
2586 drop_inode = 1;
2587
2588 nr = trans->blocks_used;
2589 btrfs_end_transaction_throttle(trans, root);
2590 fail:
2591 if (drop_inode) {
2592 inode_dec_link_count(inode);
2593 iput(inode);
2594 }
2595 btrfs_btree_balance_dirty(root, nr);
2596 return err;
2597 }
2598
2599 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2600 {
2601 struct inode *inode = NULL;
2602 struct btrfs_trans_handle *trans;
2603 struct btrfs_root *root = BTRFS_I(dir)->root;
2604 int err = 0;
2605 int drop_on_err = 0;
2606 u64 objectid = 0;
2607 u64 index = 0;
2608 unsigned long nr = 1;
2609
2610 err = btrfs_check_free_space(root, 1, 0);
2611 if (err)
2612 goto out_unlock;
2613
2614 trans = btrfs_start_transaction(root, 1);
2615 btrfs_set_trans_block_group(trans, dir);
2616
2617 if (IS_ERR(trans)) {
2618 err = PTR_ERR(trans);
2619 goto out_unlock;
2620 }
2621
2622 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2623 if (err) {
2624 err = -ENOSPC;
2625 goto out_unlock;
2626 }
2627
2628 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
2629 dentry->d_name.len,
2630 dentry->d_parent->d_inode->i_ino, objectid,
2631 BTRFS_I(dir)->block_group, S_IFDIR | mode,
2632 &index);
2633 if (IS_ERR(inode)) {
2634 err = PTR_ERR(inode);
2635 goto out_fail;
2636 }
2637
2638 drop_on_err = 1;
2639
2640 err = btrfs_init_acl(inode, dir);
2641 if (err)
2642 goto out_fail;
2643
2644 inode->i_op = &btrfs_dir_inode_operations;
2645 inode->i_fop = &btrfs_dir_file_operations;
2646 btrfs_set_trans_block_group(trans, inode);
2647
2648 btrfs_i_size_write(inode, 0);
2649 err = btrfs_update_inode(trans, root, inode);
2650 if (err)
2651 goto out_fail;
2652
2653 err = btrfs_add_link(trans, dentry, inode, 0, index);
2654 if (err)
2655 goto out_fail;
2656
2657 d_instantiate(dentry, inode);
2658 drop_on_err = 0;
2659 dir->i_sb->s_dirt = 1;
2660 btrfs_update_inode_block_group(trans, inode);
2661 btrfs_update_inode_block_group(trans, dir);
2662
2663 out_fail:
2664 nr = trans->blocks_used;
2665 btrfs_end_transaction_throttle(trans, root);
2666
2667 out_unlock:
2668 if (drop_on_err)
2669 iput(inode);
2670 btrfs_btree_balance_dirty(root, nr);
2671 return err;
2672 }
2673
2674 static int merge_extent_mapping(struct extent_map_tree *em_tree,
2675 struct extent_map *existing,
2676 struct extent_map *em,
2677 u64 map_start, u64 map_len)
2678 {
2679 u64 start_diff;
2680
2681 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
2682 start_diff = map_start - em->start;
2683 em->start = map_start;
2684 em->len = map_len;
2685 if (em->block_start < EXTENT_MAP_LAST_BYTE)
2686 em->block_start += start_diff;
2687 return add_extent_mapping(em_tree, em);
2688 }
2689
2690 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
2691 size_t pg_offset, u64 start, u64 len,
2692 int create)
2693 {
2694 int ret;
2695 int err = 0;
2696 u64 bytenr;
2697 u64 extent_start = 0;
2698 u64 extent_end = 0;
2699 u64 objectid = inode->i_ino;
2700 u32 found_type;
2701 struct btrfs_path *path = NULL;
2702 struct btrfs_root *root = BTRFS_I(inode)->root;
2703 struct btrfs_file_extent_item *item;
2704 struct extent_buffer *leaf;
2705 struct btrfs_key found_key;
2706 struct extent_map *em = NULL;
2707 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2708 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2709 struct btrfs_trans_handle *trans = NULL;
2710
2711 again:
2712 spin_lock(&em_tree->lock);
2713 em = lookup_extent_mapping(em_tree, start, len);
2714 if (em)
2715 em->bdev = root->fs_info->fs_devices->latest_bdev;
2716 spin_unlock(&em_tree->lock);
2717
2718 if (em) {
2719 if (em->start > start || em->start + em->len <= start)
2720 free_extent_map(em);
2721 else if (em->block_start == EXTENT_MAP_INLINE && page)
2722 free_extent_map(em);
2723 else
2724 goto out;
2725 }
2726 em = alloc_extent_map(GFP_NOFS);
2727 if (!em) {
2728 err = -ENOMEM;
2729 goto out;
2730 }
2731 em->bdev = root->fs_info->fs_devices->latest_bdev;
2732 em->start = EXTENT_MAP_HOLE;
2733 em->len = (u64)-1;
2734
2735 if (!path) {
2736 path = btrfs_alloc_path();
2737 BUG_ON(!path);
2738 }
2739
2740 ret = btrfs_lookup_file_extent(trans, root, path,
2741 objectid, start, trans != NULL);
2742 if (ret < 0) {
2743 err = ret;
2744 goto out;
2745 }
2746
2747 if (ret != 0) {
2748 if (path->slots[0] == 0)
2749 goto not_found;
2750 path->slots[0]--;
2751 }
2752
2753 leaf = path->nodes[0];
2754 item = btrfs_item_ptr(leaf, path->slots[0],
2755 struct btrfs_file_extent_item);
2756 /* are we inside the extent that was found? */
2757 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2758 found_type = btrfs_key_type(&found_key);
2759 if (found_key.objectid != objectid ||
2760 found_type != BTRFS_EXTENT_DATA_KEY) {
2761 goto not_found;
2762 }
2763
2764 found_type = btrfs_file_extent_type(leaf, item);
2765 extent_start = found_key.offset;
2766 if (found_type == BTRFS_FILE_EXTENT_REG) {
2767 extent_end = extent_start +
2768 btrfs_file_extent_num_bytes(leaf, item);
2769 err = 0;
2770 if (start < extent_start || start >= extent_end) {
2771 em->start = start;
2772 if (start < extent_start) {
2773 if (start + len <= extent_start)
2774 goto not_found;
2775 em->len = extent_end - extent_start;
2776 } else {
2777 em->len = len;
2778 }
2779 goto not_found_em;
2780 }
2781 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2782 if (bytenr == 0) {
2783 em->start = extent_start;
2784 em->len = extent_end - extent_start;
2785 em->block_start = EXTENT_MAP_HOLE;
2786 goto insert;
2787 }
2788 bytenr += btrfs_file_extent_offset(leaf, item);
2789 em->block_start = bytenr;
2790 em->start = extent_start;
2791 em->len = extent_end - extent_start;
2792 goto insert;
2793 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
2794 u64 page_start;
2795 unsigned long ptr;
2796 char *map;
2797 size_t size;
2798 size_t extent_offset;
2799 size_t copy_size;
2800
2801 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2802 path->slots[0]));
2803 extent_end = (extent_start + size + root->sectorsize - 1) &
2804 ~((u64)root->sectorsize - 1);
2805 if (start < extent_start || start >= extent_end) {
2806 em->start = start;
2807 if (start < extent_start) {
2808 if (start + len <= extent_start)
2809 goto not_found;
2810 em->len = extent_end - extent_start;
2811 } else {
2812 em->len = len;
2813 }
2814 goto not_found_em;
2815 }
2816 em->block_start = EXTENT_MAP_INLINE;
2817
2818 if (!page) {
2819 em->start = extent_start;
2820 em->len = size;
2821 goto out;
2822 }
2823
2824 page_start = page_offset(page) + pg_offset;
2825 extent_offset = page_start - extent_start;
2826 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
2827 size - extent_offset);
2828 em->start = extent_start + extent_offset;
2829 em->len = (copy_size + root->sectorsize - 1) &
2830 ~((u64)root->sectorsize - 1);
2831 map = kmap(page);
2832 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
2833 if (create == 0 && !PageUptodate(page)) {
2834 read_extent_buffer(leaf, map + pg_offset, ptr,
2835 copy_size);
2836 flush_dcache_page(page);
2837 } else if (create && PageUptodate(page)) {
2838 if (!trans) {
2839 kunmap(page);
2840 free_extent_map(em);
2841 em = NULL;
2842 btrfs_release_path(root, path);
2843 trans = btrfs_join_transaction(root, 1);
2844 goto again;
2845 }
2846 write_extent_buffer(leaf, map + pg_offset, ptr,
2847 copy_size);
2848 btrfs_mark_buffer_dirty(leaf);
2849 }
2850 kunmap(page);
2851 set_extent_uptodate(io_tree, em->start,
2852 extent_map_end(em) - 1, GFP_NOFS);
2853 goto insert;
2854 } else {
2855 printk("unkknown found_type %d\n", found_type);
2856 WARN_ON(1);
2857 }
2858 not_found:
2859 em->start = start;
2860 em->len = len;
2861 not_found_em:
2862 em->block_start = EXTENT_MAP_HOLE;
2863 insert:
2864 btrfs_release_path(root, path);
2865 if (em->start > start || extent_map_end(em) <= start) {
2866 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
2867 err = -EIO;
2868 goto out;
2869 }
2870
2871 err = 0;
2872 spin_lock(&em_tree->lock);
2873 ret = add_extent_mapping(em_tree, em);
2874 /* it is possible that someone inserted the extent into the tree
2875 * while we had the lock dropped. It is also possible that
2876 * an overlapping map exists in the tree
2877 */
2878 if (ret == -EEXIST) {
2879 struct extent_map *existing;
2880
2881 ret = 0;
2882
2883 existing = lookup_extent_mapping(em_tree, start, len);
2884 if (existing && (existing->start > start ||
2885 existing->start + existing->len <= start)) {
2886 free_extent_map(existing);
2887 existing = NULL;
2888 }
2889 if (!existing) {
2890 existing = lookup_extent_mapping(em_tree, em->start,
2891 em->len);
2892 if (existing) {
2893 err = merge_extent_mapping(em_tree, existing,
2894 em, start,
2895 root->sectorsize);
2896 free_extent_map(existing);
2897 if (err) {
2898 free_extent_map(em);
2899 em = NULL;
2900 }
2901 } else {
2902 err = -EIO;
2903 printk("failing to insert %Lu %Lu\n",
2904 start, len);
2905 free_extent_map(em);
2906 em = NULL;
2907 }
2908 } else {
2909 free_extent_map(em);
2910 em = existing;
2911 err = 0;
2912 }
2913 }
2914 spin_unlock(&em_tree->lock);
2915 out:
2916 if (path)
2917 btrfs_free_path(path);
2918 if (trans) {
2919 ret = btrfs_end_transaction(trans, root);
2920 if (!err) {
2921 err = ret;
2922 }
2923 }
2924 if (err) {
2925 free_extent_map(em);
2926 WARN_ON(1);
2927 return ERR_PTR(err);
2928 }
2929 return em;
2930 }
2931
2932 #if 0 /* waiting for O_DIRECT reads */
2933 static int btrfs_get_block(struct inode *inode, sector_t iblock,
2934 struct buffer_head *bh_result, int create)
2935 {
2936 struct extent_map *em;
2937 u64 start = (u64)iblock << inode->i_blkbits;
2938 struct btrfs_multi_bio *multi = NULL;
2939 struct btrfs_root *root = BTRFS_I(inode)->root;
2940 u64 len;
2941 u64 logical;
2942 u64 map_length;
2943 int ret = 0;
2944
2945 em = btrfs_get_extent(inode, NULL, 0, start, bh_result->b_size, 0);
2946
2947 if (!em || IS_ERR(em))
2948 goto out;
2949
2950 if (em->start > start || em->start + em->len <= start) {
2951 goto out;
2952 }
2953
2954 if (em->block_start == EXTENT_MAP_INLINE) {
2955 ret = -EINVAL;
2956 goto out;
2957 }
2958
2959 len = em->start + em->len - start;
2960 len = min_t(u64, len, INT_LIMIT(typeof(bh_result->b_size)));
2961
2962 if (em->block_start == EXTENT_MAP_HOLE ||
2963 em->block_start == EXTENT_MAP_DELALLOC) {
2964 bh_result->b_size = len;
2965 goto out;
2966 }
2967
2968 logical = start - em->start;
2969 logical = em->block_start + logical;
2970
2971 map_length = len;
2972 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2973 logical, &map_length, &multi, 0);
2974 BUG_ON(ret);
2975 bh_result->b_blocknr = multi->stripes[0].physical >> inode->i_blkbits;
2976 bh_result->b_size = min(map_length, len);
2977
2978 bh_result->b_bdev = multi->stripes[0].dev->bdev;
2979 set_buffer_mapped(bh_result);
2980 kfree(multi);
2981 out:
2982 free_extent_map(em);
2983 return ret;
2984 }
2985 #endif
2986
2987 static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
2988 const struct iovec *iov, loff_t offset,
2989 unsigned long nr_segs)
2990 {
2991 return -EINVAL;
2992 #if 0
2993 struct file *file = iocb->ki_filp;
2994 struct inode *inode = file->f_mapping->host;
2995
2996 if (rw == WRITE)
2997 return -EINVAL;
2998
2999 return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
3000 offset, nr_segs, btrfs_get_block, NULL);
3001 #endif
3002 }
3003
3004 static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
3005 {
3006 return extent_bmap(mapping, iblock, btrfs_get_extent);
3007 }
3008
3009 int btrfs_readpage(struct file *file, struct page *page)
3010 {
3011 struct extent_io_tree *tree;
3012 tree = &BTRFS_I(page->mapping->host)->io_tree;
3013 return extent_read_full_page(tree, page, btrfs_get_extent);
3014 }
3015
3016 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
3017 {
3018 struct extent_io_tree *tree;
3019
3020
3021 if (current->flags & PF_MEMALLOC) {
3022 redirty_page_for_writepage(wbc, page);
3023 unlock_page(page);
3024 return 0;
3025 }
3026 tree = &BTRFS_I(page->mapping->host)->io_tree;
3027 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
3028 }
3029
3030 int btrfs_writepages(struct address_space *mapping,
3031 struct writeback_control *wbc)
3032 {
3033 struct extent_io_tree *tree;
3034 tree = &BTRFS_I(mapping->host)->io_tree;
3035 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
3036 }
3037
3038 static int
3039 btrfs_readpages(struct file *file, struct address_space *mapping,
3040 struct list_head *pages, unsigned nr_pages)
3041 {
3042 struct extent_io_tree *tree;
3043 tree = &BTRFS_I(mapping->host)->io_tree;
3044 return extent_readpages(tree, mapping, pages, nr_pages,
3045 btrfs_get_extent);
3046 }
3047 static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
3048 {
3049 struct extent_io_tree *tree;
3050 struct extent_map_tree *map;
3051 int ret;
3052
3053 tree = &BTRFS_I(page->mapping->host)->io_tree;
3054 map = &BTRFS_I(page->mapping->host)->extent_tree;
3055 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
3056 if (ret == 1) {
3057 ClearPagePrivate(page);
3058 set_page_private(page, 0);
3059 page_cache_release(page);
3060 }
3061 return ret;
3062 }
3063
3064 static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
3065 {
3066 return __btrfs_releasepage(page, gfp_flags);
3067 }
3068
3069 static void btrfs_invalidatepage(struct page *page, unsigned long offset)
3070 {
3071 struct extent_io_tree *tree;
3072 struct btrfs_ordered_extent *ordered;
3073 u64 page_start = page_offset(page);
3074 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
3075
3076 wait_on_page_writeback(page);
3077 tree = &BTRFS_I(page->mapping->host)->io_tree;
3078 if (offset) {
3079 btrfs_releasepage(page, GFP_NOFS);
3080 return;
3081 }
3082
3083 lock_extent(tree, page_start, page_end, GFP_NOFS);
3084 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
3085 page_offset(page));
3086 if (ordered) {
3087 /*
3088 * IO on this page will never be started, so we need
3089 * to account for any ordered extents now
3090 */
3091 clear_extent_bit(tree, page_start, page_end,
3092 EXTENT_DIRTY | EXTENT_DELALLOC |
3093 EXTENT_LOCKED, 1, 0, GFP_NOFS);
3094 btrfs_finish_ordered_io(page->mapping->host,
3095 page_start, page_end);
3096 btrfs_put_ordered_extent(ordered);
3097 lock_extent(tree, page_start, page_end, GFP_NOFS);
3098 }
3099 clear_extent_bit(tree, page_start, page_end,
3100 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3101 EXTENT_ORDERED,
3102 1, 1, GFP_NOFS);
3103 __btrfs_releasepage(page, GFP_NOFS);
3104
3105 ClearPageChecked(page);
3106 if (PagePrivate(page)) {
3107 ClearPagePrivate(page);
3108 set_page_private(page, 0);
3109 page_cache_release(page);
3110 }
3111 }
3112
3113 /*
3114 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
3115 * called from a page fault handler when a page is first dirtied. Hence we must
3116 * be careful to check for EOF conditions here. We set the page up correctly
3117 * for a written page which means we get ENOSPC checking when writing into
3118 * holes and correct delalloc and unwritten extent mapping on filesystems that
3119 * support these features.
3120 *
3121 * We are not allowed to take the i_mutex here so we have to play games to
3122 * protect against truncate races as the page could now be beyond EOF. Because
3123 * vmtruncate() writes the inode size before removing pages, once we have the
3124 * page lock we can determine safely if the page is beyond EOF. If it is not
3125 * beyond EOF, then the page is guaranteed safe against truncation until we
3126 * unlock the page.
3127 */
3128 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
3129 {
3130 struct inode *inode = fdentry(vma->vm_file)->d_inode;
3131 struct btrfs_root *root = BTRFS_I(inode)->root;
3132 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3133 struct btrfs_ordered_extent *ordered;
3134 char *kaddr;
3135 unsigned long zero_start;
3136 loff_t size;
3137 int ret;
3138 u64 page_start;
3139 u64 page_end;
3140
3141 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
3142 if (ret)
3143 goto out;
3144
3145 ret = -EINVAL;
3146 again:
3147 lock_page(page);
3148 size = i_size_read(inode);
3149 page_start = page_offset(page);
3150 page_end = page_start + PAGE_CACHE_SIZE - 1;
3151
3152 if ((page->mapping != inode->i_mapping) ||
3153 (page_start >= size)) {
3154 /* page got truncated out from underneath us */
3155 goto out_unlock;
3156 }
3157 wait_on_page_writeback(page);
3158
3159 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
3160 set_page_extent_mapped(page);
3161
3162 /*
3163 * we can't set the delalloc bits if there are pending ordered
3164 * extents. Drop our locks and wait for them to finish
3165 */
3166 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3167 if (ordered) {
3168 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3169 unlock_page(page);
3170 btrfs_start_ordered_extent(inode, ordered, 1);
3171 btrfs_put_ordered_extent(ordered);
3172 goto again;
3173 }
3174
3175 btrfs_set_extent_delalloc(inode, page_start, page_end);
3176 ret = 0;
3177
3178 /* page is wholly or partially inside EOF */
3179 if (page_start + PAGE_CACHE_SIZE > size)
3180 zero_start = size & ~PAGE_CACHE_MASK;
3181 else
3182 zero_start = PAGE_CACHE_SIZE;
3183
3184 if (zero_start != PAGE_CACHE_SIZE) {
3185 kaddr = kmap(page);
3186 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
3187 flush_dcache_page(page);
3188 kunmap(page);
3189 }
3190 ClearPageChecked(page);
3191 set_page_dirty(page);
3192 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3193
3194 out_unlock:
3195 unlock_page(page);
3196 out:
3197 return ret;
3198 }
3199
3200 static void btrfs_truncate(struct inode *inode)
3201 {
3202 struct btrfs_root *root = BTRFS_I(inode)->root;
3203 int ret;
3204 struct btrfs_trans_handle *trans;
3205 unsigned long nr;
3206 u64 mask = root->sectorsize - 1;
3207
3208 if (!S_ISREG(inode->i_mode))
3209 return;
3210 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3211 return;
3212
3213 btrfs_truncate_page(inode->i_mapping, inode->i_size);
3214 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
3215
3216 trans = btrfs_start_transaction(root, 1);
3217 btrfs_set_trans_block_group(trans, inode);
3218 btrfs_i_size_write(inode, inode->i_size);
3219
3220 ret = btrfs_orphan_add(trans, inode);
3221 if (ret)
3222 goto out;
3223 /* FIXME, add redo link to tree so we don't leak on crash */
3224 ret = btrfs_truncate_in_trans(trans, root, inode,
3225 BTRFS_EXTENT_DATA_KEY);
3226 btrfs_update_inode(trans, root, inode);
3227
3228 ret = btrfs_orphan_del(trans, inode);
3229 BUG_ON(ret);
3230
3231 out:
3232 nr = trans->blocks_used;
3233 ret = btrfs_end_transaction_throttle(trans, root);
3234 BUG_ON(ret);
3235 btrfs_btree_balance_dirty(root, nr);
3236 }
3237
3238 /*
3239 * Invalidate a single dcache entry at the root of the filesystem.
3240 * Needed after creation of snapshot or subvolume.
3241 */
3242 void btrfs_invalidate_dcache_root(struct btrfs_root *root, char *name,
3243 int namelen)
3244 {
3245 struct dentry *alias, *entry;
3246 struct qstr qstr;
3247
3248 alias = d_find_alias(root->fs_info->sb->s_root->d_inode);
3249 if (alias) {
3250 qstr.name = name;
3251 qstr.len = namelen;
3252 /* change me if btrfs ever gets a d_hash operation */
3253 qstr.hash = full_name_hash(qstr.name, qstr.len);
3254 entry = d_lookup(alias, &qstr);
3255 dput(alias);
3256 if (entry) {
3257 d_invalidate(entry);
3258 dput(entry);
3259 }
3260 }
3261 }
3262
3263 int btrfs_create_subvol_root(struct btrfs_root *new_root,
3264 struct btrfs_trans_handle *trans, u64 new_dirid,
3265 struct btrfs_block_group_cache *block_group)
3266 {
3267 struct inode *inode;
3268 u64 index = 0;
3269
3270 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
3271 new_dirid, block_group, S_IFDIR | 0700, &index);
3272 if (IS_ERR(inode))
3273 return PTR_ERR(inode);
3274 inode->i_op = &btrfs_dir_inode_operations;
3275 inode->i_fop = &btrfs_dir_file_operations;
3276 new_root->inode = inode;
3277
3278 inode->i_nlink = 1;
3279 btrfs_i_size_write(inode, 0);
3280
3281 return btrfs_update_inode(trans, new_root, inode);
3282 }
3283
3284 unsigned long btrfs_force_ra(struct address_space *mapping,
3285 struct file_ra_state *ra, struct file *file,
3286 pgoff_t offset, pgoff_t last_index)
3287 {
3288 pgoff_t req_size = last_index - offset + 1;
3289
3290 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
3291 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
3292 return offset;
3293 #else
3294 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
3295 return offset + req_size;
3296 #endif
3297 }
3298
3299 struct inode *btrfs_alloc_inode(struct super_block *sb)
3300 {
3301 struct btrfs_inode *ei;
3302
3303 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
3304 if (!ei)
3305 return NULL;
3306 ei->last_trans = 0;
3307 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
3308 ei->i_acl = BTRFS_ACL_NOT_CACHED;
3309 ei->i_default_acl = BTRFS_ACL_NOT_CACHED;
3310 INIT_LIST_HEAD(&ei->i_orphan);
3311 return &ei->vfs_inode;
3312 }
3313
3314 void btrfs_destroy_inode(struct inode *inode)
3315 {
3316 struct btrfs_ordered_extent *ordered;
3317 WARN_ON(!list_empty(&inode->i_dentry));
3318 WARN_ON(inode->i_data.nrpages);
3319
3320 if (BTRFS_I(inode)->i_acl &&
3321 BTRFS_I(inode)->i_acl != BTRFS_ACL_NOT_CACHED)
3322 posix_acl_release(BTRFS_I(inode)->i_acl);
3323 if (BTRFS_I(inode)->i_default_acl &&
3324 BTRFS_I(inode)->i_default_acl != BTRFS_ACL_NOT_CACHED)
3325 posix_acl_release(BTRFS_I(inode)->i_default_acl);
3326
3327 spin_lock(&BTRFS_I(inode)->root->list_lock);
3328 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
3329 printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan"
3330 " list\n", inode->i_ino);
3331 dump_stack();
3332 }
3333 spin_unlock(&BTRFS_I(inode)->root->list_lock);
3334
3335 while(1) {
3336 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
3337 if (!ordered)
3338 break;
3339 else {
3340 printk("found ordered extent %Lu %Lu\n",
3341 ordered->file_offset, ordered->len);
3342 btrfs_remove_ordered_extent(inode, ordered);
3343 btrfs_put_ordered_extent(ordered);
3344 btrfs_put_ordered_extent(ordered);
3345 }
3346 }
3347 btrfs_drop_extent_cache(inode, 0, (u64)-1);
3348 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
3349 }
3350
3351 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
3352 static void init_once(void *foo)
3353 #elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3354 static void init_once(struct kmem_cache * cachep, void *foo)
3355 #else
3356 static void init_once(void * foo, struct kmem_cache * cachep,
3357 unsigned long flags)
3358 #endif
3359 {
3360 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
3361
3362 inode_init_once(&ei->vfs_inode);
3363 }
3364
3365 void btrfs_destroy_cachep(void)
3366 {
3367 if (btrfs_inode_cachep)
3368 kmem_cache_destroy(btrfs_inode_cachep);
3369 if (btrfs_trans_handle_cachep)
3370 kmem_cache_destroy(btrfs_trans_handle_cachep);
3371 if (btrfs_transaction_cachep)
3372 kmem_cache_destroy(btrfs_transaction_cachep);
3373 if (btrfs_bit_radix_cachep)
3374 kmem_cache_destroy(btrfs_bit_radix_cachep);
3375 if (btrfs_path_cachep)
3376 kmem_cache_destroy(btrfs_path_cachep);
3377 }
3378
3379 struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
3380 unsigned long extra_flags,
3381 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
3382 void (*ctor)(void *)
3383 #elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3384 void (*ctor)(struct kmem_cache *, void *)
3385 #else
3386 void (*ctor)(void *, struct kmem_cache *,
3387 unsigned long)
3388 #endif
3389 )
3390 {
3391 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
3392 SLAB_MEM_SPREAD | extra_flags), ctor
3393 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
3394 ,NULL
3395 #endif
3396 );
3397 }
3398
3399 int btrfs_init_cachep(void)
3400 {
3401 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
3402 sizeof(struct btrfs_inode),
3403 0, init_once);
3404 if (!btrfs_inode_cachep)
3405 goto fail;
3406 btrfs_trans_handle_cachep =
3407 btrfs_cache_create("btrfs_trans_handle_cache",
3408 sizeof(struct btrfs_trans_handle),
3409 0, NULL);
3410 if (!btrfs_trans_handle_cachep)
3411 goto fail;
3412 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
3413 sizeof(struct btrfs_transaction),
3414 0, NULL);
3415 if (!btrfs_transaction_cachep)
3416 goto fail;
3417 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
3418 sizeof(struct btrfs_path),
3419 0, NULL);
3420 if (!btrfs_path_cachep)
3421 goto fail;
3422 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
3423 SLAB_DESTROY_BY_RCU, NULL);
3424 if (!btrfs_bit_radix_cachep)
3425 goto fail;
3426 return 0;
3427 fail:
3428 btrfs_destroy_cachep();
3429 return -ENOMEM;
3430 }
3431
3432 static int btrfs_getattr(struct vfsmount *mnt,
3433 struct dentry *dentry, struct kstat *stat)
3434 {
3435 struct inode *inode = dentry->d_inode;
3436 generic_fillattr(inode, stat);
3437 stat->blksize = PAGE_CACHE_SIZE;
3438 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
3439 return 0;
3440 }
3441
3442 static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
3443 struct inode * new_dir,struct dentry *new_dentry)
3444 {
3445 struct btrfs_trans_handle *trans;
3446 struct btrfs_root *root = BTRFS_I(old_dir)->root;
3447 struct inode *new_inode = new_dentry->d_inode;
3448 struct inode *old_inode = old_dentry->d_inode;
3449 struct timespec ctime = CURRENT_TIME;
3450 u64 index = 0;
3451 int ret;
3452
3453 if (S_ISDIR(old_inode->i_mode) && new_inode &&
3454 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
3455 return -ENOTEMPTY;
3456 }
3457
3458 ret = btrfs_check_free_space(root, 1, 0);
3459 if (ret)
3460 goto out_unlock;
3461
3462 trans = btrfs_start_transaction(root, 1);
3463
3464 btrfs_set_trans_block_group(trans, new_dir);
3465
3466 old_dentry->d_inode->i_nlink++;
3467 old_dir->i_ctime = old_dir->i_mtime = ctime;
3468 new_dir->i_ctime = new_dir->i_mtime = ctime;
3469 old_inode->i_ctime = ctime;
3470
3471 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
3472 if (ret)
3473 goto out_fail;
3474
3475 if (new_inode) {
3476 new_inode->i_ctime = CURRENT_TIME;
3477 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
3478 if (ret)
3479 goto out_fail;
3480 if (new_inode->i_nlink == 0) {
3481 ret = btrfs_orphan_add(trans, new_inode);
3482 if (ret)
3483 goto out_fail;
3484 }
3485 }
3486 ret = btrfs_set_inode_index(new_dir, old_inode, &index);
3487 if (ret)
3488 goto out_fail;
3489
3490 ret = btrfs_add_link(trans, new_dentry, old_inode, 1, index);
3491 if (ret)
3492 goto out_fail;
3493
3494 out_fail:
3495 btrfs_end_transaction_throttle(trans, root);
3496 out_unlock:
3497 return ret;
3498 }
3499
3500 int btrfs_start_delalloc_inodes(struct btrfs_root *root)
3501 {
3502 struct list_head *head = &root->fs_info->delalloc_inodes;
3503 struct btrfs_inode *binode;
3504 unsigned long flags;
3505
3506 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
3507 while(!list_empty(head)) {
3508 binode = list_entry(head->next, struct btrfs_inode,
3509 delalloc_inodes);
3510 atomic_inc(&binode->vfs_inode.i_count);
3511 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
3512 filemap_write_and_wait(binode->vfs_inode.i_mapping);
3513 iput(&binode->vfs_inode);
3514 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
3515 }
3516 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
3517 return 0;
3518 }
3519
3520 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
3521 const char *symname)
3522 {
3523 struct btrfs_trans_handle *trans;
3524 struct btrfs_root *root = BTRFS_I(dir)->root;
3525 struct btrfs_path *path;
3526 struct btrfs_key key;
3527 struct inode *inode = NULL;
3528 int err;
3529 int drop_inode = 0;
3530 u64 objectid;
3531 u64 index = 0 ;
3532 int name_len;
3533 int datasize;
3534 unsigned long ptr;
3535 struct btrfs_file_extent_item *ei;
3536 struct extent_buffer *leaf;
3537 unsigned long nr = 0;
3538
3539 name_len = strlen(symname) + 1;
3540 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
3541 return -ENAMETOOLONG;
3542
3543 err = btrfs_check_free_space(root, 1, 0);
3544 if (err)
3545 goto out_fail;
3546
3547 trans = btrfs_start_transaction(root, 1);
3548 btrfs_set_trans_block_group(trans, dir);
3549
3550 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3551 if (err) {
3552 err = -ENOSPC;
3553 goto out_unlock;
3554 }
3555
3556 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
3557 dentry->d_name.len,
3558 dentry->d_parent->d_inode->i_ino, objectid,
3559 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
3560 &index);
3561 err = PTR_ERR(inode);
3562 if (IS_ERR(inode))
3563 goto out_unlock;
3564
3565 err = btrfs_init_acl(inode, dir);
3566 if (err) {
3567 drop_inode = 1;
3568 goto out_unlock;
3569 }
3570
3571 btrfs_set_trans_block_group(trans, inode);
3572 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
3573 if (err)
3574 drop_inode = 1;
3575 else {
3576 inode->i_mapping->a_ops = &btrfs_aops;
3577 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
3578 inode->i_fop = &btrfs_file_operations;
3579 inode->i_op = &btrfs_file_inode_operations;
3580 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3581 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
3582 inode->i_mapping, GFP_NOFS);
3583 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3584 inode->i_mapping, GFP_NOFS);
3585 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
3586 mutex_init(&BTRFS_I(inode)->csum_mutex);
3587 mutex_init(&BTRFS_I(inode)->extent_mutex);
3588 BTRFS_I(inode)->delalloc_bytes = 0;
3589 BTRFS_I(inode)->disk_i_size = 0;
3590 inode->i_mapping->writeback_index = 0;
3591 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
3592 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
3593 }
3594 dir->i_sb->s_dirt = 1;
3595 btrfs_update_inode_block_group(trans, inode);
3596 btrfs_update_inode_block_group(trans, dir);
3597 if (drop_inode)
3598 goto out_unlock;
3599
3600 path = btrfs_alloc_path();
3601 BUG_ON(!path);
3602 key.objectid = inode->i_ino;
3603 key.offset = 0;
3604 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
3605 datasize = btrfs_file_extent_calc_inline_size(name_len);
3606 err = btrfs_insert_empty_item(trans, root, path, &key,
3607 datasize);
3608 if (err) {
3609 drop_inode = 1;
3610 goto out_unlock;
3611 }
3612 leaf = path->nodes[0];
3613 ei = btrfs_item_ptr(leaf, path->slots[0],
3614 struct btrfs_file_extent_item);
3615 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
3616 btrfs_set_file_extent_type(leaf, ei,
3617 BTRFS_FILE_EXTENT_INLINE);
3618 ptr = btrfs_file_extent_inline_start(ei);
3619 write_extent_buffer(leaf, symname, ptr, name_len);
3620 btrfs_mark_buffer_dirty(leaf);
3621 btrfs_free_path(path);
3622
3623 inode->i_op = &btrfs_symlink_inode_operations;
3624 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3625 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
3626 btrfs_i_size_write(inode, name_len - 1);
3627 err = btrfs_update_inode(trans, root, inode);
3628 if (err)
3629 drop_inode = 1;
3630
3631 out_unlock:
3632 nr = trans->blocks_used;
3633 btrfs_end_transaction_throttle(trans, root);
3634 out_fail:
3635 if (drop_inode) {
3636 inode_dec_link_count(inode);
3637 iput(inode);
3638 }
3639 btrfs_btree_balance_dirty(root, nr);
3640 return err;
3641 }
3642
3643 static int btrfs_set_page_dirty(struct page *page)
3644 {
3645 return __set_page_dirty_nobuffers(page);
3646 }
3647
3648 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
3649 static int btrfs_permission(struct inode *inode, int mask)
3650 #else
3651 static int btrfs_permission(struct inode *inode, int mask,
3652 struct nameidata *nd)
3653 #endif
3654 {
3655 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
3656 return -EACCES;
3657 return generic_permission(inode, mask, btrfs_check_acl);
3658 }
3659
3660 static struct inode_operations btrfs_dir_inode_operations = {
3661 .lookup = btrfs_lookup,
3662 .create = btrfs_create,
3663 .unlink = btrfs_unlink,
3664 .link = btrfs_link,
3665 .mkdir = btrfs_mkdir,
3666 .rmdir = btrfs_rmdir,
3667 .rename = btrfs_rename,
3668 .symlink = btrfs_symlink,
3669 .setattr = btrfs_setattr,
3670 .mknod = btrfs_mknod,
3671 .setxattr = btrfs_setxattr,
3672 .getxattr = btrfs_getxattr,
3673 .listxattr = btrfs_listxattr,
3674 .removexattr = btrfs_removexattr,
3675 .permission = btrfs_permission,
3676 };
3677 static struct inode_operations btrfs_dir_ro_inode_operations = {
3678 .lookup = btrfs_lookup,
3679 .permission = btrfs_permission,
3680 };
3681 static struct file_operations btrfs_dir_file_operations = {
3682 .llseek = generic_file_llseek,
3683 .read = generic_read_dir,
3684 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
3685 .readdir = btrfs_nfshack_readdir,
3686 #else /* NFSd readdir/lookup deadlock is fixed */
3687 .readdir = btrfs_real_readdir,
3688 #endif
3689 .unlocked_ioctl = btrfs_ioctl,
3690 #ifdef CONFIG_COMPAT
3691 .compat_ioctl = btrfs_ioctl,
3692 #endif
3693 .release = btrfs_release_file,
3694 };
3695
3696 static struct extent_io_ops btrfs_extent_io_ops = {
3697 .fill_delalloc = run_delalloc_range,
3698 .submit_bio_hook = btrfs_submit_bio_hook,
3699 .merge_bio_hook = btrfs_merge_bio_hook,
3700 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
3701 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
3702 .writepage_start_hook = btrfs_writepage_start_hook,
3703 .readpage_io_failed_hook = btrfs_io_failed_hook,
3704 .set_bit_hook = btrfs_set_bit_hook,
3705 .clear_bit_hook = btrfs_clear_bit_hook,
3706 };
3707
3708 static struct address_space_operations btrfs_aops = {
3709 .readpage = btrfs_readpage,
3710 .writepage = btrfs_writepage,
3711 .writepages = btrfs_writepages,
3712 .readpages = btrfs_readpages,
3713 .sync_page = block_sync_page,
3714 .bmap = btrfs_bmap,
3715 .direct_IO = btrfs_direct_IO,
3716 .invalidatepage = btrfs_invalidatepage,
3717 .releasepage = btrfs_releasepage,
3718 .set_page_dirty = btrfs_set_page_dirty,
3719 };
3720
3721 static struct address_space_operations btrfs_symlink_aops = {
3722 .readpage = btrfs_readpage,
3723 .writepage = btrfs_writepage,
3724 .invalidatepage = btrfs_invalidatepage,
3725 .releasepage = btrfs_releasepage,
3726 };
3727
3728 static struct inode_operations btrfs_file_inode_operations = {
3729 .truncate = btrfs_truncate,
3730 .getattr = btrfs_getattr,
3731 .setattr = btrfs_setattr,
3732 .setxattr = btrfs_setxattr,
3733 .getxattr = btrfs_getxattr,
3734 .listxattr = btrfs_listxattr,
3735 .removexattr = btrfs_removexattr,
3736 .permission = btrfs_permission,
3737 };
3738 static struct inode_operations btrfs_special_inode_operations = {
3739 .getattr = btrfs_getattr,
3740 .setattr = btrfs_setattr,
3741 .permission = btrfs_permission,
3742 .setxattr = btrfs_setxattr,
3743 .getxattr = btrfs_getxattr,
3744 .listxattr = btrfs_listxattr,
3745 .removexattr = btrfs_removexattr,
3746 };
3747 static struct inode_operations btrfs_symlink_inode_operations = {
3748 .readlink = generic_readlink,
3749 .follow_link = page_follow_link_light,
3750 .put_link = page_put_link,
3751 .permission = btrfs_permission,
3752 };
This page took 0.129342 seconds and 6 git commands to generate.