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