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