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