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