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