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