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