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