]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/btrfs/inode.c
Btrfs: remove extra drop_extent_cache call
[net-next-2.6.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
39279cc3
CM
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>
9ebefb18 33#include <linux/bit_spinlock.h>
92fee66d 34#include <linux/version.h>
39279cc3
CM
35#include "ctree.h"
36#include "disk-io.h"
37#include "transaction.h"
38#include "btrfs_inode.h"
39#include "ioctl.h"
40#include "print-tree.h"
41
42struct btrfs_iget_args {
43 u64 ino;
44 struct btrfs_root *root;
45};
46
47static struct inode_operations btrfs_dir_inode_operations;
48static struct inode_operations btrfs_symlink_inode_operations;
49static struct inode_operations btrfs_dir_ro_inode_operations;
618e21d5 50static struct inode_operations btrfs_special_inode_operations;
39279cc3
CM
51static struct inode_operations btrfs_file_inode_operations;
52static struct address_space_operations btrfs_aops;
53static struct address_space_operations btrfs_symlink_aops;
54static struct file_operations btrfs_dir_file_operations;
07157aac 55static struct extent_map_ops btrfs_extent_map_ops;
39279cc3
CM
56
57static struct kmem_cache *btrfs_inode_cachep;
58struct kmem_cache *btrfs_trans_handle_cachep;
59struct kmem_cache *btrfs_transaction_cachep;
60struct kmem_cache *btrfs_bit_radix_cachep;
61struct kmem_cache *btrfs_path_cachep;
62
63#define S_SHIFT 12
64static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
65 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
66 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
67 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
68 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
69 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
70 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
71 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
72};
73
b888db2b
CM
74static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
75{
76 struct btrfs_root *root = BTRFS_I(inode)->root;
77 struct btrfs_trans_handle *trans;
78 struct btrfs_key ins;
79 u64 alloc_hint = 0;
80 u64 num_blocks;
81 int ret;
82 u64 blocksize = 1 << inode->i_blkbits;
83
84 mutex_lock(&root->fs_info->fs_mutex);
85 trans = btrfs_start_transaction(root, 1);
86 btrfs_set_trans_block_group(trans, inode);
87 BUG_ON(!trans);
88 num_blocks = (end - start + blocksize) & ~(blocksize - 1);
89 ret = btrfs_drop_extents(trans, root, inode,
90 start, start + num_blocks, &alloc_hint);
91 num_blocks = num_blocks >> inode->i_blkbits;
92 ret = btrfs_alloc_extent(trans, root, inode->i_ino, num_blocks, 0,
93 alloc_hint, (u64)-1, &ins, 1);
94 if (ret) {
95 WARN_ON(1);
96 goto out;
97 }
98 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
99 start, ins.objectid, ins.offset,
100 ins.offset);
101out:
102 btrfs_end_transaction(trans, root);
103 mutex_unlock(&root->fs_info->fs_mutex);
104 return ret;
105}
106
07157aac
CM
107int btrfs_writepage_io_hook(struct page *page, u64 start, u64 end)
108{
109 struct inode *inode = page->mapping->host;
110 struct btrfs_root *root = BTRFS_I(inode)->root;
111 struct btrfs_trans_handle *trans;
112 char *kaddr;
113 int ret;
114 u64 page_start = page->index << PAGE_CACHE_SHIFT;
115 size_t offset = start - page_start;
116
117 mutex_lock(&root->fs_info->fs_mutex);
118 trans = btrfs_start_transaction(root, 1);
119 btrfs_set_trans_block_group(trans, inode);
120 kaddr = kmap(page);
121 btrfs_csum_file_block(trans, root, inode->i_ino,
122 start, kaddr + offset, end - start + 1);
123 kunmap(page);
124 ret = btrfs_end_transaction(trans, root);
125 BUG_ON(ret);
126 mutex_unlock(&root->fs_info->fs_mutex);
127 return ret;
128}
129
130int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
131{
132 int ret = 0;
133 struct inode *inode = page->mapping->host;
134 struct btrfs_root *root = BTRFS_I(inode)->root;
135 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
136 struct btrfs_csum_item *item;
137 struct btrfs_path *path = NULL;
138 u64 private;
139
140 mutex_lock(&root->fs_info->fs_mutex);
141 path = btrfs_alloc_path();
142 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
143 if (IS_ERR(item)) {
144 ret = PTR_ERR(item);
145 /* a csum that isn't present is a preallocated region. */
146 if (ret == -ENOENT || ret == -EFBIG)
147 ret = 0;
148 private = 0;
149 goto out;
150 }
151 memcpy((char *)&private, &item->csum, BTRFS_CRC32_SIZE);
152 set_state_private(em_tree, start, private);
153out:
154 if (path)
155 btrfs_free_path(path);
156 mutex_unlock(&root->fs_info->fs_mutex);
157 return ret;
158}
159
160int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end)
161{
162 char csum[BTRFS_CRC32_SIZE];
163 size_t offset = start - (page->index << PAGE_CACHE_SHIFT);
164 struct inode *inode = page->mapping->host;
165 struct btrfs_root *root = BTRFS_I(inode)->root;
166 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
167 char *kaddr;
168 u64 private;
169 int ret;
170
171 ret = get_state_private(em_tree, start, &private);
172 kaddr = kmap_atomic(page, KM_IRQ0);
173 if (ret) {
174 goto zeroit;
175 }
176 ret = btrfs_csum_data(root, kaddr + offset, end - start + 1, csum);
177 BUG_ON(ret);
178 if (memcmp(csum, &private, BTRFS_CRC32_SIZE)) {
179 goto zeroit;
180 }
181 kunmap_atomic(kaddr, KM_IRQ0);
182 return 0;
183
184zeroit:
185 printk("btrfs csum failed ino %lu off %llu\n",
186 page->mapping->host->i_ino, (unsigned long long)start);
187 memset(kaddr + offset, 1, end - start + 1); flush_dcache_page(page);
188 kunmap_atomic(kaddr, KM_IRQ0);
189 return 0;
190}
b888db2b 191
39279cc3
CM
192void btrfs_read_locked_inode(struct inode *inode)
193{
194 struct btrfs_path *path;
195 struct btrfs_inode_item *inode_item;
196 struct btrfs_root *root = BTRFS_I(inode)->root;
197 struct btrfs_key location;
198 u64 alloc_group_block;
618e21d5 199 u32 rdev;
39279cc3
CM
200 int ret;
201
202 path = btrfs_alloc_path();
203 BUG_ON(!path);
39279cc3
CM
204 mutex_lock(&root->fs_info->fs_mutex);
205
206 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
207 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
208 if (ret) {
209 btrfs_free_path(path);
210 goto make_bad;
211 }
212 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
213 path->slots[0],
214 struct btrfs_inode_item);
215
216 inode->i_mode = btrfs_inode_mode(inode_item);
217 inode->i_nlink = btrfs_inode_nlink(inode_item);
218 inode->i_uid = btrfs_inode_uid(inode_item);
219 inode->i_gid = btrfs_inode_gid(inode_item);
220 inode->i_size = btrfs_inode_size(inode_item);
221 inode->i_atime.tv_sec = btrfs_timespec_sec(&inode_item->atime);
222 inode->i_atime.tv_nsec = btrfs_timespec_nsec(&inode_item->atime);
223 inode->i_mtime.tv_sec = btrfs_timespec_sec(&inode_item->mtime);
224 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(&inode_item->mtime);
225 inode->i_ctime.tv_sec = btrfs_timespec_sec(&inode_item->ctime);
226 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(&inode_item->ctime);
227 inode->i_blocks = btrfs_inode_nblocks(inode_item);
228 inode->i_generation = btrfs_inode_generation(inode_item);
618e21d5
JB
229 inode->i_rdev = 0;
230 rdev = btrfs_inode_rdev(inode_item);
39279cc3
CM
231 alloc_group_block = btrfs_inode_block_group(inode_item);
232 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
233 alloc_group_block);
234
235 btrfs_free_path(path);
236 inode_item = NULL;
237
238 mutex_unlock(&root->fs_info->fs_mutex);
239
240 switch (inode->i_mode & S_IFMT) {
39279cc3
CM
241 case S_IFREG:
242 inode->i_mapping->a_ops = &btrfs_aops;
07157aac 243 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
39279cc3
CM
244 inode->i_fop = &btrfs_file_operations;
245 inode->i_op = &btrfs_file_inode_operations;
246 break;
247 case S_IFDIR:
248 inode->i_fop = &btrfs_dir_file_operations;
249 if (root == root->fs_info->tree_root)
250 inode->i_op = &btrfs_dir_ro_inode_operations;
251 else
252 inode->i_op = &btrfs_dir_inode_operations;
253 break;
254 case S_IFLNK:
255 inode->i_op = &btrfs_symlink_inode_operations;
256 inode->i_mapping->a_ops = &btrfs_symlink_aops;
257 break;
618e21d5
JB
258 default:
259 init_special_inode(inode, inode->i_mode, rdev);
260 break;
39279cc3
CM
261 }
262 return;
263
264make_bad:
265 btrfs_release_path(root, path);
266 btrfs_free_path(path);
267 mutex_unlock(&root->fs_info->fs_mutex);
268 make_bad_inode(inode);
269}
270
271static void fill_inode_item(struct btrfs_inode_item *item,
272 struct inode *inode)
273{
274 btrfs_set_inode_uid(item, inode->i_uid);
275 btrfs_set_inode_gid(item, inode->i_gid);
276 btrfs_set_inode_size(item, inode->i_size);
277 btrfs_set_inode_mode(item, inode->i_mode);
278 btrfs_set_inode_nlink(item, inode->i_nlink);
279 btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
280 btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec);
281 btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec);
282 btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec);
283 btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec);
284 btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
285 btrfs_set_inode_nblocks(item, inode->i_blocks);
286 btrfs_set_inode_generation(item, inode->i_generation);
618e21d5 287 btrfs_set_inode_rdev(item, inode->i_rdev);
39279cc3
CM
288 btrfs_set_inode_block_group(item,
289 BTRFS_I(inode)->block_group->key.objectid);
290}
291
a52d9a80 292int btrfs_update_inode(struct btrfs_trans_handle *trans,
39279cc3
CM
293 struct btrfs_root *root,
294 struct inode *inode)
295{
296 struct btrfs_inode_item *inode_item;
297 struct btrfs_path *path;
298 int ret;
299
300 path = btrfs_alloc_path();
301 BUG_ON(!path);
39279cc3
CM
302 ret = btrfs_lookup_inode(trans, root, path,
303 &BTRFS_I(inode)->location, 1);
304 if (ret) {
305 if (ret > 0)
306 ret = -ENOENT;
307 goto failed;
308 }
309
310 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
311 path->slots[0],
312 struct btrfs_inode_item);
313
314 fill_inode_item(inode_item, inode);
315 btrfs_mark_buffer_dirty(path->nodes[0]);
15ee9bc7 316 btrfs_set_inode_last_trans(trans, inode);
39279cc3
CM
317 ret = 0;
318failed:
319 btrfs_release_path(root, path);
320 btrfs_free_path(path);
321 return ret;
322}
323
324
325static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
326 struct btrfs_root *root,
327 struct inode *dir,
328 struct dentry *dentry)
329{
330 struct btrfs_path *path;
331 const char *name = dentry->d_name.name;
332 int name_len = dentry->d_name.len;
333 int ret = 0;
334 u64 objectid;
335 struct btrfs_dir_item *di;
336
337 path = btrfs_alloc_path();
54aa1f4d
CM
338 if (!path) {
339 ret = -ENOMEM;
340 goto err;
341 }
342
39279cc3
CM
343 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
344 name, name_len, -1);
345 if (IS_ERR(di)) {
346 ret = PTR_ERR(di);
347 goto err;
348 }
349 if (!di) {
350 ret = -ENOENT;
351 goto err;
352 }
353 objectid = btrfs_disk_key_objectid(&di->location);
354 ret = btrfs_delete_one_dir_name(trans, root, path, di);
54aa1f4d
CM
355 if (ret)
356 goto err;
39279cc3
CM
357 btrfs_release_path(root, path);
358
359 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
360 objectid, name, name_len, -1);
361 if (IS_ERR(di)) {
362 ret = PTR_ERR(di);
363 goto err;
364 }
365 if (!di) {
366 ret = -ENOENT;
367 goto err;
368 }
369 ret = btrfs_delete_one_dir_name(trans, root, path, di);
39279cc3
CM
370
371 dentry->d_inode->i_ctime = dir->i_ctime;
372err:
373 btrfs_free_path(path);
374 if (!ret) {
375 dir->i_size -= name_len * 2;
79c44584 376 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
39279cc3
CM
377 btrfs_update_inode(trans, root, dir);
378 drop_nlink(dentry->d_inode);
54aa1f4d 379 ret = btrfs_update_inode(trans, root, dentry->d_inode);
39279cc3
CM
380 dir->i_sb->s_dirt = 1;
381 }
382 return ret;
383}
384
385static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
386{
387 struct btrfs_root *root;
388 struct btrfs_trans_handle *trans;
389 int ret;
390
391 root = BTRFS_I(dir)->root;
392 mutex_lock(&root->fs_info->fs_mutex);
393 trans = btrfs_start_transaction(root, 1);
394 btrfs_set_trans_block_group(trans, dir);
395 ret = btrfs_unlink_trans(trans, root, dir, dentry);
396 btrfs_end_transaction(trans, root);
397 mutex_unlock(&root->fs_info->fs_mutex);
398 btrfs_btree_balance_dirty(root);
399 return ret;
400}
401
402static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
403{
404 struct inode *inode = dentry->d_inode;
405 int err;
406 int ret;
407 struct btrfs_root *root = BTRFS_I(dir)->root;
408 struct btrfs_path *path;
409 struct btrfs_key key;
410 struct btrfs_trans_handle *trans;
411 struct btrfs_key found_key;
412 int found_type;
413 struct btrfs_leaf *leaf;
414 char *goodnames = "..";
415
416 path = btrfs_alloc_path();
417 BUG_ON(!path);
39279cc3
CM
418 mutex_lock(&root->fs_info->fs_mutex);
419 trans = btrfs_start_transaction(root, 1);
420 btrfs_set_trans_block_group(trans, dir);
421 key.objectid = inode->i_ino;
422 key.offset = (u64)-1;
423 key.flags = (u32)-1;
424 while(1) {
425 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
426 if (ret < 0) {
427 err = ret;
428 goto out;
429 }
430 BUG_ON(ret == 0);
431 if (path->slots[0] == 0) {
432 err = -ENOENT;
433 goto out;
434 }
435 path->slots[0]--;
436 leaf = btrfs_buffer_leaf(path->nodes[0]);
437 btrfs_disk_key_to_cpu(&found_key,
438 &leaf->items[path->slots[0]].key);
439 found_type = btrfs_key_type(&found_key);
440 if (found_key.objectid != inode->i_ino) {
441 err = -ENOENT;
442 goto out;
443 }
444 if ((found_type != BTRFS_DIR_ITEM_KEY &&
445 found_type != BTRFS_DIR_INDEX_KEY) ||
446 (!btrfs_match_dir_item_name(root, path, goodnames, 2) &&
447 !btrfs_match_dir_item_name(root, path, goodnames, 1))) {
448 err = -ENOTEMPTY;
449 goto out;
450 }
451 ret = btrfs_del_item(trans, root, path);
452 BUG_ON(ret);
453
454 if (found_type == BTRFS_DIR_ITEM_KEY && found_key.offset == 1)
455 break;
456 btrfs_release_path(root, path);
457 }
458 ret = 0;
459 btrfs_release_path(root, path);
460
461 /* now the directory is empty */
462 err = btrfs_unlink_trans(trans, root, dir, dentry);
463 if (!err) {
464 inode->i_size = 0;
465 }
466out:
467 btrfs_release_path(root, path);
468 btrfs_free_path(path);
469 mutex_unlock(&root->fs_info->fs_mutex);
470 ret = btrfs_end_transaction(trans, root);
471 btrfs_btree_balance_dirty(root);
472 if (ret && !err)
473 err = ret;
474 return err;
475}
476
477static int btrfs_free_inode(struct btrfs_trans_handle *trans,
478 struct btrfs_root *root,
479 struct inode *inode)
480{
481 struct btrfs_path *path;
482 int ret;
483
484 clear_inode(inode);
485
486 path = btrfs_alloc_path();
487 BUG_ON(!path);
39279cc3
CM
488 ret = btrfs_lookup_inode(trans, root, path,
489 &BTRFS_I(inode)->location, -1);
54aa1f4d
CM
490 if (ret > 0)
491 ret = -ENOENT;
492 if (!ret)
493 ret = btrfs_del_item(trans, root, path);
39279cc3
CM
494 btrfs_free_path(path);
495 return ret;
496}
497
39279cc3
CM
498/*
499 * this can truncate away extent items, csum items and directory items.
500 * It starts at a high offset and removes keys until it can't find
501 * any higher than i_size.
502 *
503 * csum items that cross the new i_size are truncated to the new size
504 * as well.
505 */
506static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
507 struct btrfs_root *root,
508 struct inode *inode)
509{
510 int ret;
511 struct btrfs_path *path;
512 struct btrfs_key key;
513 struct btrfs_disk_key *found_key;
514 u32 found_type;
515 struct btrfs_leaf *leaf;
516 struct btrfs_file_extent_item *fi;
517 u64 extent_start = 0;
518 u64 extent_num_blocks = 0;
519 u64 item_end = 0;
520 int found_extent;
521 int del_item;
522
a52d9a80 523 btrfs_drop_extent_cache(inode, inode->i_size, (u64)-1);
39279cc3 524 path = btrfs_alloc_path();
3c69faec 525 path->reada = -1;
39279cc3
CM
526 BUG_ON(!path);
527 /* FIXME, add redo link to tree so we don't leak on crash */
528 key.objectid = inode->i_ino;
529 key.offset = (u64)-1;
530 key.flags = (u32)-1;
531 while(1) {
532 btrfs_init_path(path);
533 fi = NULL;
534 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
535 if (ret < 0) {
536 goto error;
537 }
538 if (ret > 0) {
539 BUG_ON(path->slots[0] == 0);
540 path->slots[0]--;
541 }
39279cc3
CM
542 leaf = btrfs_buffer_leaf(path->nodes[0]);
543 found_key = &leaf->items[path->slots[0]].key;
544 found_type = btrfs_disk_key_type(found_key);
545
546 if (btrfs_disk_key_objectid(found_key) != inode->i_ino)
547 break;
548 if (found_type != BTRFS_CSUM_ITEM_KEY &&
549 found_type != BTRFS_DIR_ITEM_KEY &&
550 found_type != BTRFS_DIR_INDEX_KEY &&
551 found_type != BTRFS_EXTENT_DATA_KEY)
552 break;
553
554 item_end = btrfs_disk_key_offset(found_key);
555 if (found_type == BTRFS_EXTENT_DATA_KEY) {
556 fi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
557 path->slots[0],
558 struct btrfs_file_extent_item);
559 if (btrfs_file_extent_type(fi) !=
560 BTRFS_FILE_EXTENT_INLINE) {
561 item_end += btrfs_file_extent_num_blocks(fi) <<
562 inode->i_blkbits;
563 }
564 }
565 if (found_type == BTRFS_CSUM_ITEM_KEY) {
566 ret = btrfs_csum_truncate(trans, root, path,
567 inode->i_size);
568 BUG_ON(ret);
569 }
570 if (item_end < inode->i_size) {
b888db2b
CM
571 if (found_type == BTRFS_DIR_ITEM_KEY) {
572 found_type = BTRFS_INODE_ITEM_KEY;
573 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
574 found_type = BTRFS_CSUM_ITEM_KEY;
575 } else if (found_type) {
576 found_type--;
577 } else {
578 break;
39279cc3 579 }
b888db2b
CM
580 btrfs_set_key_type(&key, found_type - 1);
581 continue;
39279cc3
CM
582 }
583 if (btrfs_disk_key_offset(found_key) >= inode->i_size)
584 del_item = 1;
585 else
586 del_item = 0;
587 found_extent = 0;
588
589 /* FIXME, shrink the extent if the ref count is only 1 */
590 if (found_type == BTRFS_EXTENT_DATA_KEY &&
591 btrfs_file_extent_type(fi) !=
592 BTRFS_FILE_EXTENT_INLINE) {
593 u64 num_dec;
594 if (!del_item) {
595 u64 orig_num_blocks =
596 btrfs_file_extent_num_blocks(fi);
597 extent_num_blocks = inode->i_size -
598 btrfs_disk_key_offset(found_key) +
599 root->blocksize - 1;
600 extent_num_blocks >>= inode->i_blkbits;
601 btrfs_set_file_extent_num_blocks(fi,
602 extent_num_blocks);
603 inode->i_blocks -= (orig_num_blocks -
604 extent_num_blocks) << 3;
ccd467d6 605 btrfs_mark_buffer_dirty(path->nodes[0]);
39279cc3
CM
606 } else {
607 extent_start =
608 btrfs_file_extent_disk_blocknr(fi);
609 extent_num_blocks =
610 btrfs_file_extent_disk_num_blocks(fi);
611 /* FIXME blocksize != 4096 */
612 num_dec = btrfs_file_extent_num_blocks(fi) << 3;
613 if (extent_start != 0) {
614 found_extent = 1;
615 inode->i_blocks -= num_dec;
616 }
617 }
618 }
619 if (del_item) {
620 ret = btrfs_del_item(trans, root, path);
54aa1f4d
CM
621 if (ret)
622 goto error;
39279cc3
CM
623 } else {
624 break;
625 }
626 btrfs_release_path(root, path);
627 if (found_extent) {
628 ret = btrfs_free_extent(trans, root, extent_start,
629 extent_num_blocks, 0);
630 BUG_ON(ret);
631 }
632 }
633 ret = 0;
634error:
635 btrfs_release_path(root, path);
636 btrfs_free_path(path);
637 inode->i_sb->s_dirt = 1;
638 return ret;
639}
640
b888db2b 641static int btrfs_cow_one_page(struct inode *inode, struct page *page,
a52d9a80
CM
642 size_t zero_start)
643{
644 char *kaddr;
645 int ret = 0;
b888db2b 646 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
a52d9a80 647 u64 page_start = page->index << PAGE_CACHE_SHIFT;
b888db2b 648 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
a52d9a80
CM
649
650 if (!PagePrivate(page)) {
651 SetPagePrivate(page);
652 set_page_private(page, 1);
b888db2b 653 WARN_ON(!page->mapping->a_ops->invalidatepage);
a52d9a80
CM
654 page_cache_get(page);
655 }
656
b888db2b
CM
657 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
658 set_extent_delalloc(&BTRFS_I(inode)->extent_tree, page_start,
659 page_end, GFP_NOFS);
a52d9a80 660 if (zero_start != PAGE_CACHE_SIZE) {
b888db2b 661 kaddr = kmap(page);
a52d9a80
CM
662 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
663 flush_dcache_page(page);
b888db2b 664 kunmap(page);
a52d9a80 665 }
b888db2b
CM
666 set_page_dirty(page);
667 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
a52d9a80 668
a52d9a80
CM
669 return ret;
670}
671
39279cc3
CM
672/*
673 * taken from block_truncate_page, but does cow as it zeros out
674 * any bytes left in the last page in the file.
675 */
676static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
677{
678 struct inode *inode = mapping->host;
679 unsigned blocksize = 1 << inode->i_blkbits;
680 pgoff_t index = from >> PAGE_CACHE_SHIFT;
681 unsigned offset = from & (PAGE_CACHE_SIZE-1);
682 struct page *page;
39279cc3 683 int ret = 0;
a52d9a80 684 u64 page_start;
39279cc3
CM
685
686 if ((offset & (blocksize - 1)) == 0)
687 goto out;
688
689 ret = -ENOMEM;
690 page = grab_cache_page(mapping, index);
691 if (!page)
692 goto out;
39279cc3 693 if (!PageUptodate(page)) {
9ebefb18 694 ret = btrfs_readpage(NULL, page);
39279cc3
CM
695 lock_page(page);
696 if (!PageUptodate(page)) {
697 ret = -EIO;
698 goto out;
699 }
700 }
a52d9a80
CM
701 page_start = page->index << PAGE_CACHE_SHIFT;
702
b888db2b 703 ret = btrfs_cow_one_page(inode, page, offset);
39279cc3 704
39279cc3
CM
705 unlock_page(page);
706 page_cache_release(page);
707out:
708 return ret;
709}
710
711static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
712{
713 struct inode *inode = dentry->d_inode;
714 int err;
715
716 err = inode_change_ok(inode, attr);
717 if (err)
718 return err;
719
720 if (S_ISREG(inode->i_mode) &&
721 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
722 struct btrfs_trans_handle *trans;
723 struct btrfs_root *root = BTRFS_I(inode)->root;
2bf5a725
CM
724 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
725
39279cc3
CM
726 u64 mask = root->blocksize - 1;
727 u64 pos = (inode->i_size + mask) & ~mask;
2bf5a725 728 u64 block_end = attr->ia_size | mask;
39279cc3 729 u64 hole_size;
2bf5a725 730 u64 alloc_hint;
39279cc3
CM
731
732 if (attr->ia_size <= pos)
733 goto out;
734
735 btrfs_truncate_page(inode->i_mapping, inode->i_size);
736
2bf5a725 737 lock_extent(em_tree, pos, block_end, GFP_NOFS);
39279cc3 738 hole_size = (attr->ia_size - pos + mask) & ~mask;
39279cc3
CM
739
740 mutex_lock(&root->fs_info->fs_mutex);
741 trans = btrfs_start_transaction(root, 1);
742 btrfs_set_trans_block_group(trans, inode);
2bf5a725
CM
743 err = btrfs_drop_extents(trans, root, inode,
744 pos, pos + hole_size, &alloc_hint);
745
746 hole_size >>= inode->i_blkbits;
747
39279cc3
CM
748 err = btrfs_insert_file_extent(trans, root, inode->i_ino,
749 pos, 0, 0, hole_size);
39279cc3
CM
750 btrfs_end_transaction(trans, root);
751 mutex_unlock(&root->fs_info->fs_mutex);
2bf5a725 752 unlock_extent(em_tree, pos, block_end, GFP_NOFS);
54aa1f4d
CM
753 if (err)
754 return err;
39279cc3
CM
755 }
756out:
757 err = inode_setattr(inode, attr);
758
759 return err;
760}
761void btrfs_delete_inode(struct inode *inode)
762{
763 struct btrfs_trans_handle *trans;
764 struct btrfs_root *root = BTRFS_I(inode)->root;
765 int ret;
766
767 truncate_inode_pages(&inode->i_data, 0);
768 if (is_bad_inode(inode)) {
769 goto no_delete;
770 }
771 inode->i_size = 0;
772 mutex_lock(&root->fs_info->fs_mutex);
773 trans = btrfs_start_transaction(root, 1);
774 btrfs_set_trans_block_group(trans, inode);
775 ret = btrfs_truncate_in_trans(trans, root, inode);
54aa1f4d
CM
776 if (ret)
777 goto no_delete_lock;
778 ret = btrfs_free_inode(trans, root, inode);
779 if (ret)
780 goto no_delete_lock;
39279cc3
CM
781 btrfs_end_transaction(trans, root);
782 mutex_unlock(&root->fs_info->fs_mutex);
783 btrfs_btree_balance_dirty(root);
784 return;
54aa1f4d
CM
785
786no_delete_lock:
787 btrfs_end_transaction(trans, root);
788 mutex_unlock(&root->fs_info->fs_mutex);
789 btrfs_btree_balance_dirty(root);
39279cc3
CM
790no_delete:
791 clear_inode(inode);
792}
793
794/*
795 * this returns the key found in the dir entry in the location pointer.
796 * If no dir entries were found, location->objectid is 0.
797 */
798static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
799 struct btrfs_key *location)
800{
801 const char *name = dentry->d_name.name;
802 int namelen = dentry->d_name.len;
803 struct btrfs_dir_item *di;
804 struct btrfs_path *path;
805 struct btrfs_root *root = BTRFS_I(dir)->root;
806 int ret;
807
808 path = btrfs_alloc_path();
809 BUG_ON(!path);
39279cc3
CM
810 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
811 namelen, 0);
812 if (!di || IS_ERR(di)) {
813 location->objectid = 0;
814 ret = 0;
815 goto out;
816 }
817 btrfs_disk_key_to_cpu(location, &di->location);
818out:
819 btrfs_release_path(root, path);
820 btrfs_free_path(path);
821 return ret;
822}
823
824/*
825 * when we hit a tree root in a directory, the btrfs part of the inode
826 * needs to be changed to reflect the root directory of the tree root. This
827 * is kind of like crossing a mount point.
828 */
829static int fixup_tree_root_location(struct btrfs_root *root,
830 struct btrfs_key *location,
58176a96
JB
831 struct btrfs_root **sub_root,
832 struct dentry *dentry)
39279cc3
CM
833{
834 struct btrfs_path *path;
835 struct btrfs_root_item *ri;
836
837 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
838 return 0;
839 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
840 return 0;
841
842 path = btrfs_alloc_path();
843 BUG_ON(!path);
844 mutex_lock(&root->fs_info->fs_mutex);
845
58176a96
JB
846 *sub_root = btrfs_read_fs_root(root->fs_info, location,
847 dentry->d_name.name,
848 dentry->d_name.len);
39279cc3
CM
849 if (IS_ERR(*sub_root))
850 return PTR_ERR(*sub_root);
851
852 ri = &(*sub_root)->root_item;
853 location->objectid = btrfs_root_dirid(ri);
854 location->flags = 0;
855 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
856 location->offset = 0;
857
858 btrfs_free_path(path);
859 mutex_unlock(&root->fs_info->fs_mutex);
860 return 0;
861}
862
863static int btrfs_init_locked_inode(struct inode *inode, void *p)
864{
865 struct btrfs_iget_args *args = p;
866 inode->i_ino = args->ino;
867 BTRFS_I(inode)->root = args->root;
b888db2b
CM
868 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
869 inode->i_mapping, GFP_NOFS);
39279cc3
CM
870 return 0;
871}
872
873static int btrfs_find_actor(struct inode *inode, void *opaque)
874{
875 struct btrfs_iget_args *args = opaque;
876 return (args->ino == inode->i_ino &&
877 args->root == BTRFS_I(inode)->root);
878}
879
880struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
881 struct btrfs_root *root)
882{
883 struct inode *inode;
884 struct btrfs_iget_args args;
885 args.ino = objectid;
886 args.root = root;
887
888 inode = iget5_locked(s, objectid, btrfs_find_actor,
889 btrfs_init_locked_inode,
890 (void *)&args);
891 return inode;
892}
893
894static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
895 struct nameidata *nd)
896{
897 struct inode * inode;
898 struct btrfs_inode *bi = BTRFS_I(dir);
899 struct btrfs_root *root = bi->root;
900 struct btrfs_root *sub_root = root;
901 struct btrfs_key location;
902 int ret;
903
904 if (dentry->d_name.len > BTRFS_NAME_LEN)
905 return ERR_PTR(-ENAMETOOLONG);
906 mutex_lock(&root->fs_info->fs_mutex);
907 ret = btrfs_inode_by_name(dir, dentry, &location);
908 mutex_unlock(&root->fs_info->fs_mutex);
909 if (ret < 0)
910 return ERR_PTR(ret);
911 inode = NULL;
912 if (location.objectid) {
58176a96
JB
913 ret = fixup_tree_root_location(root, &location, &sub_root,
914 dentry);
39279cc3
CM
915 if (ret < 0)
916 return ERR_PTR(ret);
917 if (ret > 0)
918 return ERR_PTR(-ENOENT);
919 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
920 sub_root);
921 if (!inode)
922 return ERR_PTR(-EACCES);
923 if (inode->i_state & I_NEW) {
924 /* the inode and parent dir are two different roots */
925 if (sub_root != root) {
926 igrab(inode);
927 sub_root->inode = inode;
928 }
929 BTRFS_I(inode)->root = sub_root;
930 memcpy(&BTRFS_I(inode)->location, &location,
931 sizeof(location));
932 btrfs_read_locked_inode(inode);
933 unlock_new_inode(inode);
934 }
935 }
936 return d_splice_alias(inode, dentry);
937}
938
39279cc3
CM
939static unsigned char btrfs_filetype_table[] = {
940 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
941};
942
943static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
944{
945 struct inode *inode = filp->f_path.dentry->d_inode;
946 struct btrfs_root *root = BTRFS_I(inode)->root;
947 struct btrfs_item *item;
948 struct btrfs_dir_item *di;
949 struct btrfs_key key;
950 struct btrfs_path *path;
951 int ret;
952 u32 nritems;
953 struct btrfs_leaf *leaf;
954 int slot;
955 int advance;
956 unsigned char d_type;
957 int over = 0;
958 u32 di_cur;
959 u32 di_total;
960 u32 di_len;
961 int key_type = BTRFS_DIR_INDEX_KEY;
962
963 /* FIXME, use a real flag for deciding about the key type */
964 if (root->fs_info->tree_root == root)
965 key_type = BTRFS_DIR_ITEM_KEY;
966 mutex_lock(&root->fs_info->fs_mutex);
967 key.objectid = inode->i_ino;
968 key.flags = 0;
969 btrfs_set_key_type(&key, key_type);
970 key.offset = filp->f_pos;
971 path = btrfs_alloc_path();
2cc58cf2 972 path->reada = 2;
39279cc3
CM
973 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
974 if (ret < 0)
975 goto err;
976 advance = 0;
39279cc3
CM
977 while(1) {
978 leaf = btrfs_buffer_leaf(path->nodes[0]);
979 nritems = btrfs_header_nritems(&leaf->header);
980 slot = path->slots[0];
981 if (advance || slot >= nritems) {
982 if (slot >= nritems -1) {
39279cc3
CM
983 ret = btrfs_next_leaf(root, path);
984 if (ret)
985 break;
986 leaf = btrfs_buffer_leaf(path->nodes[0]);
987 nritems = btrfs_header_nritems(&leaf->header);
988 slot = path->slots[0];
989 } else {
990 slot++;
991 path->slots[0]++;
992 }
993 }
994 advance = 1;
995 item = leaf->items + slot;
996 if (btrfs_disk_key_objectid(&item->key) != key.objectid)
997 break;
998 if (btrfs_disk_key_type(&item->key) != key_type)
999 break;
1000 if (btrfs_disk_key_offset(&item->key) < filp->f_pos)
1001 continue;
1002 filp->f_pos = btrfs_disk_key_offset(&item->key);
1003 advance = 1;
1004 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1005 di_cur = 0;
1006 di_total = btrfs_item_size(leaf->items + slot);
1007 while(di_cur < di_total) {
1008 d_type = btrfs_filetype_table[btrfs_dir_type(di)];
1009 over = filldir(dirent, (const char *)(di + 1),
1010 btrfs_dir_name_len(di),
1011 btrfs_disk_key_offset(&item->key),
1012 btrfs_disk_key_objectid(&di->location),
1013 d_type);
1014 if (over)
1015 goto nopos;
1016 di_len = btrfs_dir_name_len(di) + sizeof(*di);
1017 di_cur += di_len;
1018 di = (struct btrfs_dir_item *)((char *)di + di_len);
1019 }
1020 }
1021 filp->f_pos++;
1022nopos:
1023 ret = 0;
1024err:
1025 btrfs_release_path(root, path);
1026 btrfs_free_path(path);
1027 mutex_unlock(&root->fs_info->fs_mutex);
1028 return ret;
1029}
1030
1031int btrfs_write_inode(struct inode *inode, int wait)
1032{
1033 struct btrfs_root *root = BTRFS_I(inode)->root;
1034 struct btrfs_trans_handle *trans;
1035 int ret = 0;
1036
1037 if (wait) {
1038 mutex_lock(&root->fs_info->fs_mutex);
1039 trans = btrfs_start_transaction(root, 1);
1040 btrfs_set_trans_block_group(trans, inode);
1041 ret = btrfs_commit_transaction(trans, root);
1042 mutex_unlock(&root->fs_info->fs_mutex);
1043 }
1044 return ret;
1045}
1046
1047/*
54aa1f4d 1048 * This is somewhat expensive, updating the tree every time the
39279cc3
CM
1049 * inode changes. But, it is most likely to find the inode in cache.
1050 * FIXME, needs more benchmarking...there are no reasons other than performance
1051 * to keep or drop this code.
1052 */
1053void btrfs_dirty_inode(struct inode *inode)
1054{
1055 struct btrfs_root *root = BTRFS_I(inode)->root;
1056 struct btrfs_trans_handle *trans;
1057
1058 mutex_lock(&root->fs_info->fs_mutex);
1059 trans = btrfs_start_transaction(root, 1);
1060 btrfs_set_trans_block_group(trans, inode);
1061 btrfs_update_inode(trans, root, inode);
1062 btrfs_end_transaction(trans, root);
1063 mutex_unlock(&root->fs_info->fs_mutex);
39279cc3
CM
1064}
1065
1066static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1067 struct btrfs_root *root,
1068 u64 objectid,
1069 struct btrfs_block_group_cache *group,
1070 int mode)
1071{
1072 struct inode *inode;
1073 struct btrfs_inode_item inode_item;
1074 struct btrfs_key *location;
1075 int ret;
1076 int owner;
1077
1078 inode = new_inode(root->fs_info->sb);
1079 if (!inode)
1080 return ERR_PTR(-ENOMEM);
1081
b888db2b
CM
1082 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1083 inode->i_mapping, GFP_NOFS);
39279cc3 1084 BTRFS_I(inode)->root = root;
b888db2b 1085
39279cc3
CM
1086 if (mode & S_IFDIR)
1087 owner = 0;
1088 else
1089 owner = 1;
1090 group = btrfs_find_block_group(root, group, 0, 0, owner);
1091 BTRFS_I(inode)->block_group = group;
1092
1093 inode->i_uid = current->fsuid;
1094 inode->i_gid = current->fsgid;
1095 inode->i_mode = mode;
1096 inode->i_ino = objectid;
1097 inode->i_blocks = 0;
1098 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1099 fill_inode_item(&inode_item, inode);
1100 location = &BTRFS_I(inode)->location;
1101 location->objectid = objectid;
1102 location->flags = 0;
1103 location->offset = 0;
1104 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1105
1106 ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
54aa1f4d
CM
1107 if (ret)
1108 return ERR_PTR(ret);
39279cc3
CM
1109 insert_inode_hash(inode);
1110 return inode;
1111}
1112
1113static inline u8 btrfs_inode_type(struct inode *inode)
1114{
1115 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1116}
1117
1118static int btrfs_add_link(struct btrfs_trans_handle *trans,
1119 struct dentry *dentry, struct inode *inode)
1120{
1121 int ret;
1122 struct btrfs_key key;
1123 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
79c44584 1124 struct inode *parent_inode;
39279cc3
CM
1125 key.objectid = inode->i_ino;
1126 key.flags = 0;
1127 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1128 key.offset = 0;
1129
1130 ret = btrfs_insert_dir_item(trans, root,
1131 dentry->d_name.name, dentry->d_name.len,
1132 dentry->d_parent->d_inode->i_ino,
1133 &key, btrfs_inode_type(inode));
1134 if (ret == 0) {
79c44584
CM
1135 parent_inode = dentry->d_parent->d_inode;
1136 parent_inode->i_size += dentry->d_name.len * 2;
1137 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
39279cc3
CM
1138 ret = btrfs_update_inode(trans, root,
1139 dentry->d_parent->d_inode);
1140 }
1141 return ret;
1142}
1143
1144static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1145 struct dentry *dentry, struct inode *inode)
1146{
1147 int err = btrfs_add_link(trans, dentry, inode);
1148 if (!err) {
1149 d_instantiate(dentry, inode);
1150 return 0;
1151 }
1152 if (err > 0)
1153 err = -EEXIST;
1154 return err;
1155}
1156
618e21d5
JB
1157static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1158 int mode, dev_t rdev)
1159{
1160 struct btrfs_trans_handle *trans;
1161 struct btrfs_root *root = BTRFS_I(dir)->root;
1162 struct inode *inode;
1163 int err;
1164 int drop_inode = 0;
1165 u64 objectid;
1166
1167 if (!new_valid_dev(rdev))
1168 return -EINVAL;
1169
1170 mutex_lock(&root->fs_info->fs_mutex);
1171 trans = btrfs_start_transaction(root, 1);
1172 btrfs_set_trans_block_group(trans, dir);
1173
1174 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1175 if (err) {
1176 err = -ENOSPC;
1177 goto out_unlock;
1178 }
1179
1180 inode = btrfs_new_inode(trans, root, objectid,
1181 BTRFS_I(dir)->block_group, mode);
1182 err = PTR_ERR(inode);
1183 if (IS_ERR(inode))
1184 goto out_unlock;
1185
1186 btrfs_set_trans_block_group(trans, inode);
1187 err = btrfs_add_nondir(trans, dentry, inode);
1188 if (err)
1189 drop_inode = 1;
1190 else {
1191 inode->i_op = &btrfs_special_inode_operations;
1192 init_special_inode(inode, inode->i_mode, rdev);
1b4ab1bb 1193 btrfs_update_inode(trans, root, inode);
618e21d5
JB
1194 }
1195 dir->i_sb->s_dirt = 1;
1196 btrfs_update_inode_block_group(trans, inode);
1197 btrfs_update_inode_block_group(trans, dir);
1198out_unlock:
1199 btrfs_end_transaction(trans, root);
1200 mutex_unlock(&root->fs_info->fs_mutex);
1201
1202 if (drop_inode) {
1203 inode_dec_link_count(inode);
1204 iput(inode);
1205 }
1206 btrfs_btree_balance_dirty(root);
1207 return err;
1208}
1209
39279cc3
CM
1210static int btrfs_create(struct inode *dir, struct dentry *dentry,
1211 int mode, struct nameidata *nd)
1212{
1213 struct btrfs_trans_handle *trans;
1214 struct btrfs_root *root = BTRFS_I(dir)->root;
1215 struct inode *inode;
1216 int err;
1217 int drop_inode = 0;
1218 u64 objectid;
1219
1220 mutex_lock(&root->fs_info->fs_mutex);
1221 trans = btrfs_start_transaction(root, 1);
1222 btrfs_set_trans_block_group(trans, dir);
1223
1224 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1225 if (err) {
1226 err = -ENOSPC;
1227 goto out_unlock;
1228 }
1229
1230 inode = btrfs_new_inode(trans, root, objectid,
1231 BTRFS_I(dir)->block_group, mode);
1232 err = PTR_ERR(inode);
1233 if (IS_ERR(inode))
1234 goto out_unlock;
1235
1236 btrfs_set_trans_block_group(trans, inode);
1237 err = btrfs_add_nondir(trans, dentry, inode);
1238 if (err)
1239 drop_inode = 1;
1240 else {
1241 inode->i_mapping->a_ops = &btrfs_aops;
1242 inode->i_fop = &btrfs_file_operations;
1243 inode->i_op = &btrfs_file_inode_operations;
a52d9a80
CM
1244 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1245 inode->i_mapping, GFP_NOFS);
07157aac 1246 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
39279cc3
CM
1247 }
1248 dir->i_sb->s_dirt = 1;
1249 btrfs_update_inode_block_group(trans, inode);
1250 btrfs_update_inode_block_group(trans, dir);
1251out_unlock:
1252 btrfs_end_transaction(trans, root);
1253 mutex_unlock(&root->fs_info->fs_mutex);
1254
1255 if (drop_inode) {
1256 inode_dec_link_count(inode);
1257 iput(inode);
1258 }
1259 btrfs_btree_balance_dirty(root);
1260 return err;
1261}
1262
1263static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1264 struct dentry *dentry)
1265{
1266 struct btrfs_trans_handle *trans;
1267 struct btrfs_root *root = BTRFS_I(dir)->root;
1268 struct inode *inode = old_dentry->d_inode;
1269 int err;
1270 int drop_inode = 0;
1271
1272 if (inode->i_nlink == 0)
1273 return -ENOENT;
1274
1275 inc_nlink(inode);
1276 mutex_lock(&root->fs_info->fs_mutex);
1277 trans = btrfs_start_transaction(root, 1);
1278 btrfs_set_trans_block_group(trans, dir);
1279 atomic_inc(&inode->i_count);
1280 err = btrfs_add_nondir(trans, dentry, inode);
1281 if (err)
1282 drop_inode = 1;
1283 dir->i_sb->s_dirt = 1;
1284 btrfs_update_inode_block_group(trans, dir);
54aa1f4d
CM
1285 err = btrfs_update_inode(trans, root, inode);
1286 if (err)
1287 drop_inode = 1;
39279cc3
CM
1288
1289 btrfs_end_transaction(trans, root);
1290 mutex_unlock(&root->fs_info->fs_mutex);
1291
1292 if (drop_inode) {
1293 inode_dec_link_count(inode);
1294 iput(inode);
1295 }
1296 btrfs_btree_balance_dirty(root);
1297 return err;
1298}
1299
1300static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
1301 struct btrfs_root *root,
1302 u64 objectid, u64 dirid)
1303{
1304 int ret;
1305 char buf[2];
1306 struct btrfs_key key;
1307
1308 buf[0] = '.';
1309 buf[1] = '.';
1310
1311 key.objectid = objectid;
1312 key.offset = 0;
1313 key.flags = 0;
1314 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1315
1316 ret = btrfs_insert_dir_item(trans, root, buf, 1, objectid,
1317 &key, BTRFS_FT_DIR);
1318 if (ret)
1319 goto error;
1320 key.objectid = dirid;
1321 ret = btrfs_insert_dir_item(trans, root, buf, 2, objectid,
1322 &key, BTRFS_FT_DIR);
1323 if (ret)
1324 goto error;
1325error:
1326 return ret;
1327}
1328
1329static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1330{
1331 struct inode *inode;
1332 struct btrfs_trans_handle *trans;
1333 struct btrfs_root *root = BTRFS_I(dir)->root;
1334 int err = 0;
1335 int drop_on_err = 0;
1336 u64 objectid;
1337
1338 mutex_lock(&root->fs_info->fs_mutex);
1339 trans = btrfs_start_transaction(root, 1);
1340 btrfs_set_trans_block_group(trans, dir);
1341 if (IS_ERR(trans)) {
1342 err = PTR_ERR(trans);
1343 goto out_unlock;
1344 }
1345
1346 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1347 if (err) {
1348 err = -ENOSPC;
1349 goto out_unlock;
1350 }
1351
1352 inode = btrfs_new_inode(trans, root, objectid,
1353 BTRFS_I(dir)->block_group, S_IFDIR | mode);
1354 if (IS_ERR(inode)) {
1355 err = PTR_ERR(inode);
1356 goto out_fail;
1357 }
1358 drop_on_err = 1;
1359 inode->i_op = &btrfs_dir_inode_operations;
1360 inode->i_fop = &btrfs_dir_file_operations;
1361 btrfs_set_trans_block_group(trans, inode);
1362
1363 err = btrfs_make_empty_dir(trans, root, inode->i_ino, dir->i_ino);
1364 if (err)
1365 goto out_fail;
1366
1367 inode->i_size = 6;
1368 err = btrfs_update_inode(trans, root, inode);
1369 if (err)
1370 goto out_fail;
1371 err = btrfs_add_link(trans, dentry, inode);
1372 if (err)
1373 goto out_fail;
1374 d_instantiate(dentry, inode);
1375 drop_on_err = 0;
1376 dir->i_sb->s_dirt = 1;
1377 btrfs_update_inode_block_group(trans, inode);
1378 btrfs_update_inode_block_group(trans, dir);
1379
1380out_fail:
1381 btrfs_end_transaction(trans, root);
1382out_unlock:
1383 mutex_unlock(&root->fs_info->fs_mutex);
1384 if (drop_on_err)
1385 iput(inode);
1386 btrfs_btree_balance_dirty(root);
1387 return err;
1388}
1389
a52d9a80
CM
1390struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
1391 size_t page_offset, u64 start, u64 end,
1392 int create)
1393{
1394 int ret;
1395 int err = 0;
1396 u64 blocknr;
1397 u64 extent_start = 0;
1398 u64 extent_end = 0;
1399 u64 objectid = inode->i_ino;
1400 u32 found_type;
1401 int failed_insert = 0;
1402 struct btrfs_path *path;
1403 struct btrfs_root *root = BTRFS_I(inode)->root;
1404 struct btrfs_file_extent_item *item;
1405 struct btrfs_leaf *leaf;
1406 struct btrfs_disk_key *found_key;
1407 struct extent_map *em = NULL;
1408 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1409 struct btrfs_trans_handle *trans = NULL;
1410
1411 path = btrfs_alloc_path();
1412 BUG_ON(!path);
1413 mutex_lock(&root->fs_info->fs_mutex);
1414
1415again:
1416 em = lookup_extent_mapping(em_tree, start, end);
1417 if (em) {
1418 goto out;
1419 }
1420 if (!em) {
1421 em = alloc_extent_map(GFP_NOFS);
1422 if (!em) {
1423 err = -ENOMEM;
1424 goto out;
1425 }
1426 em->start = 0;
1427 em->end = 0;
1428 }
1429 em->bdev = inode->i_sb->s_bdev;
1430 ret = btrfs_lookup_file_extent(NULL, root, path,
1431 objectid, start, 0);
1432 if (ret < 0) {
1433 err = ret;
1434 goto out;
1435 }
1436
1437 if (ret != 0) {
1438 if (path->slots[0] == 0)
1439 goto not_found;
1440 path->slots[0]--;
1441 }
1442
1443 item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
1444 struct btrfs_file_extent_item);
1445 leaf = btrfs_buffer_leaf(path->nodes[0]);
1446 blocknr = btrfs_file_extent_disk_blocknr(item);
1447 blocknr += btrfs_file_extent_offset(item);
1448
1449 /* are we inside the extent that was found? */
1450 found_key = &leaf->items[path->slots[0]].key;
1451 found_type = btrfs_disk_key_type(found_key);
1452 if (btrfs_disk_key_objectid(found_key) != objectid ||
1453 found_type != BTRFS_EXTENT_DATA_KEY) {
1454 goto not_found;
1455 }
1456
1457 found_type = btrfs_file_extent_type(item);
1458 extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key);
1459 if (found_type == BTRFS_FILE_EXTENT_REG) {
1460 extent_end = extent_start +
1461 (btrfs_file_extent_num_blocks(item) << inode->i_blkbits);
1462 err = 0;
b888db2b 1463 if (start < extent_start || start >= extent_end) {
a52d9a80
CM
1464 em->start = start;
1465 if (start < extent_start) {
b888db2b
CM
1466 if (end < extent_start)
1467 goto not_found;
a52d9a80
CM
1468 em->end = extent_end - 1;
1469 } else {
1470 em->end = end;
1471 }
1472 goto not_found_em;
1473 }
1474 if (btrfs_file_extent_disk_blocknr(item) == 0) {
1475 em->start = extent_start;
1476 em->end = extent_end - 1;
1477 em->block_start = 0;
1478 em->block_end = 0;
1479 goto insert;
1480 }
1481 em->block_start = blocknr << inode->i_blkbits;
1482 em->block_end = em->block_start +
1483 (btrfs_file_extent_num_blocks(item) <<
1484 inode->i_blkbits) - 1;
1485 em->start = extent_start;
1486 em->end = extent_end - 1;
1487 goto insert;
1488 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
1489 char *ptr;
1490 char *map;
1491 u32 size;
1492
1493 size = btrfs_file_extent_inline_len(leaf->items +
1494 path->slots[0]);
1495 extent_end = extent_start + size;
b888db2b 1496 if (start < extent_start || start >= extent_end) {
a52d9a80
CM
1497 em->start = start;
1498 if (start < extent_start) {
b888db2b
CM
1499 if (end < extent_start)
1500 goto not_found;
a52d9a80
CM
1501 em->end = extent_end - 1;
1502 } else {
1503 em->end = end;
1504 }
1505 goto not_found_em;
1506 }
1507 em->block_start = EXTENT_MAP_INLINE;
1508 em->block_end = EXTENT_MAP_INLINE;
1509 em->start = extent_start;
1510 em->end = extent_end - 1;
1511 if (!page) {
1512 goto insert;
1513 }
1514 ptr = btrfs_file_extent_inline_start(item);
1515 map = kmap(page);
1516 memcpy(map + page_offset, ptr, size);
1517 flush_dcache_page(result->b_page);
1518 kunmap(page);
1519 set_extent_uptodate(em_tree, extent_start,
1520 extent_end, GFP_NOFS);
1521 goto insert;
1522 } else {
1523 printk("unkknown found_type %d\n", found_type);
1524 WARN_ON(1);
1525 }
1526not_found:
1527 em->start = start;
1528 em->end = end;
1529not_found_em:
1530 em->block_start = 0;
1531 em->block_end = 0;
1532insert:
1533 btrfs_release_path(root, path);
1534 if (em->start > start || em->end < start) {
b888db2b 1535 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->end, start, end);
a52d9a80
CM
1536 err = -EIO;
1537 goto out;
1538 }
1539 ret = add_extent_mapping(em_tree, em);
1540 if (ret == -EEXIST) {
1541 free_extent_map(em);
2bf5a725 1542 em = NULL;
a52d9a80
CM
1543 failed_insert++;
1544 if (failed_insert > 5) {
1545 printk("failing to insert %Lu %Lu\n", start, end);
1546 err = -EIO;
1547 goto out;
1548 }
a52d9a80
CM
1549 goto again;
1550 }
1551 err = 0;
1552out:
1553 btrfs_free_path(path);
1554 if (trans) {
1555 ret = btrfs_end_transaction(trans, root);
1556 if (!err)
1557 err = ret;
1558 }
1559 mutex_unlock(&root->fs_info->fs_mutex);
1560 if (err) {
1561 free_extent_map(em);
1562 WARN_ON(1);
1563 return ERR_PTR(err);
1564 }
1565 return em;
1566}
1567
39279cc3
CM
1568static int btrfs_get_block_bmap(struct inode *inode, sector_t iblock,
1569 struct buffer_head *result, int create)
1570{
1571 struct btrfs_root *root = BTRFS_I(inode)->root;
a52d9a80
CM
1572 u64 start = iblock << inode->i_blkbits;
1573 u64 end = start + root->blocksize -1;
1574 struct extent_map *em;
1575
1576 em = btrfs_get_extent(inode, NULL, 0, start, end, 0);
1577 if (em && !IS_ERR(em) && em->block_start != EXTENT_MAP_INLINE &&
1578 em->block_start != 0) {
1579 u64 offset;
1580 offset = start - em->start;
1581 start = (em->block_start + offset) >> inode->i_blkbits;
1582 btrfs_map_bh_to_logical(root, result, start);
1583 }
39279cc3
CM
1584 return 0;
1585}
1586
1587static sector_t btrfs_bmap(struct address_space *as, sector_t block)
1588{
1589 return generic_block_bmap(as, block, btrfs_get_block_bmap);
1590}
1591
1592static int btrfs_prepare_write(struct file *file, struct page *page,
1593 unsigned from, unsigned to)
1594{
a52d9a80
CM
1595 return extent_prepare_write(&BTRFS_I(page->mapping->host)->extent_tree,
1596 page->mapping->host, page, from, to,
1597 btrfs_get_extent);
39279cc3
CM
1598}
1599
a52d9a80 1600int btrfs_readpage(struct file *file, struct page *page)
9ebefb18 1601{
a52d9a80
CM
1602 struct extent_map_tree *tree;
1603 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1604 return extent_read_full_page(tree, page, btrfs_get_extent);
9ebefb18 1605}
a52d9a80 1606static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
39279cc3 1607{
a52d9a80 1608 struct extent_map_tree *tree;
b888db2b
CM
1609
1610
1611 if (current->flags & PF_MEMALLOC) {
1612 redirty_page_for_writepage(wbc, page);
1613 unlock_page(page);
1614 return 0;
1615 }
a52d9a80
CM
1616 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1617 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
9ebefb18
CM
1618}
1619
a52d9a80 1620static int btrfs_releasepage(struct page *page, gfp_t unused_gfp_flags)
9ebefb18 1621{
a52d9a80
CM
1622 struct extent_map_tree *tree;
1623 int ret;
8c2383c3 1624
a52d9a80
CM
1625 if (page->private != 1) {
1626 WARN_ON(1);
1627 return try_to_free_buffers(page);
39279cc3 1628 }
a52d9a80
CM
1629 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1630 ret = try_release_extent_mapping(tree, page);
1631 if (ret == 1) {
1632 ClearPagePrivate(page);
1633 set_page_private(page, 0);
1634 page_cache_release(page);
39279cc3 1635 }
a52d9a80 1636 return ret;
39279cc3
CM
1637}
1638
a52d9a80 1639static void btrfs_invalidatepage(struct page *page, unsigned long offset)
39279cc3 1640{
a52d9a80 1641 struct extent_map_tree *tree;
39279cc3 1642
a52d9a80
CM
1643 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1644 extent_invalidatepage(tree, page, offset);
1645 btrfs_releasepage(page, GFP_NOFS);
39279cc3
CM
1646}
1647
9ebefb18
CM
1648/*
1649 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
1650 * called from a page fault handler when a page is first dirtied. Hence we must
1651 * be careful to check for EOF conditions here. We set the page up correctly
1652 * for a written page which means we get ENOSPC checking when writing into
1653 * holes and correct delalloc and unwritten extent mapping on filesystems that
1654 * support these features.
1655 *
1656 * We are not allowed to take the i_mutex here so we have to play games to
1657 * protect against truncate races as the page could now be beyond EOF. Because
1658 * vmtruncate() writes the inode size before removing pages, once we have the
1659 * page lock we can determine safely if the page is beyond EOF. If it is not
1660 * beyond EOF, then the page is guaranteed safe against truncation until we
1661 * unlock the page.
1662 */
1663int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
1664{
1665 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1666 unsigned long end;
1667 loff_t size;
1668 int ret = -EINVAL;
a52d9a80 1669 u64 page_start;
9ebefb18
CM
1670
1671 lock_page(page);
1672 wait_on_page_writeback(page);
9ebefb18 1673 size = i_size_read(inode);
a52d9a80
CM
1674 page_start = page->index << PAGE_CACHE_SHIFT;
1675
9ebefb18 1676 if ((page->mapping != inode->i_mapping) ||
a52d9a80 1677 (page_start > size)) {
9ebefb18
CM
1678 /* page got truncated out from underneath us */
1679 goto out_unlock;
1680 }
1681
1682 /* page is wholly or partially inside EOF */
a52d9a80 1683 if (page_start + PAGE_CACHE_SIZE > size)
9ebefb18
CM
1684 end = size & ~PAGE_CACHE_MASK;
1685 else
1686 end = PAGE_CACHE_SIZE;
1687
b888db2b 1688 ret = btrfs_cow_one_page(inode, page, end);
9ebefb18
CM
1689
1690out_unlock:
1691 unlock_page(page);
1692 return ret;
1693}
1694
39279cc3
CM
1695static void btrfs_truncate(struct inode *inode)
1696{
1697 struct btrfs_root *root = BTRFS_I(inode)->root;
1698 int ret;
1699 struct btrfs_trans_handle *trans;
1700
1701 if (!S_ISREG(inode->i_mode))
1702 return;
1703 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1704 return;
1705
1706 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1707
1708 mutex_lock(&root->fs_info->fs_mutex);
1709 trans = btrfs_start_transaction(root, 1);
1710 btrfs_set_trans_block_group(trans, inode);
1711
1712 /* FIXME, add redo link to tree so we don't leak on crash */
1713 ret = btrfs_truncate_in_trans(trans, root, inode);
39279cc3
CM
1714 btrfs_update_inode(trans, root, inode);
1715 ret = btrfs_end_transaction(trans, root);
1716 BUG_ON(ret);
1717 mutex_unlock(&root->fs_info->fs_mutex);
1718 btrfs_btree_balance_dirty(root);
1719}
1720
1721int btrfs_commit_write(struct file *file, struct page *page,
1722 unsigned from, unsigned to)
1723{
a52d9a80
CM
1724 return extent_commit_write(&BTRFS_I(page->mapping->host)->extent_tree,
1725 page->mapping->host, page, from, to);
39279cc3
CM
1726}
1727
1728static int create_subvol(struct btrfs_root *root, char *name, int namelen)
1729{
1730 struct btrfs_trans_handle *trans;
1731 struct btrfs_key key;
1732 struct btrfs_root_item root_item;
1733 struct btrfs_inode_item *inode_item;
1734 struct buffer_head *subvol;
1735 struct btrfs_leaf *leaf;
1736 struct btrfs_root *new_root;
1737 struct inode *inode;
1738 struct inode *dir;
1739 int ret;
54aa1f4d 1740 int err;
39279cc3
CM
1741 u64 objectid;
1742 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
1743
1744 mutex_lock(&root->fs_info->fs_mutex);
1745 trans = btrfs_start_transaction(root, 1);
1746 BUG_ON(!trans);
1747
6702ed49 1748 subvol = btrfs_alloc_free_block(trans, root, 0, 0);
54aa1f4d
CM
1749 if (IS_ERR(subvol))
1750 return PTR_ERR(subvol);
39279cc3
CM
1751 leaf = btrfs_buffer_leaf(subvol);
1752 btrfs_set_header_nritems(&leaf->header, 0);
1753 btrfs_set_header_level(&leaf->header, 0);
1754 btrfs_set_header_blocknr(&leaf->header, bh_blocknr(subvol));
1755 btrfs_set_header_generation(&leaf->header, trans->transid);
1756 btrfs_set_header_owner(&leaf->header, root->root_key.objectid);
1757 memcpy(leaf->header.fsid, root->fs_info->disk_super->fsid,
1758 sizeof(leaf->header.fsid));
ccd467d6 1759 btrfs_mark_buffer_dirty(subvol);
39279cc3
CM
1760
1761 inode_item = &root_item.inode;
1762 memset(inode_item, 0, sizeof(*inode_item));
1763 btrfs_set_inode_generation(inode_item, 1);
1764 btrfs_set_inode_size(inode_item, 3);
1765 btrfs_set_inode_nlink(inode_item, 1);
1766 btrfs_set_inode_nblocks(inode_item, 1);
1767 btrfs_set_inode_mode(inode_item, S_IFDIR | 0755);
1768
1769 btrfs_set_root_blocknr(&root_item, bh_blocknr(subvol));
1770 btrfs_set_root_refs(&root_item, 1);
58176a96 1771 btrfs_set_root_blocks_used(&root_item, 0);
5eda7b5e
CM
1772 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
1773 root_item.drop_level = 0;
39279cc3
CM
1774 brelse(subvol);
1775 subvol = NULL;
1776
1777 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
1778 0, &objectid);
54aa1f4d
CM
1779 if (ret)
1780 goto fail;
39279cc3
CM
1781
1782 btrfs_set_root_dirid(&root_item, new_dirid);
1783
1784 key.objectid = objectid;
1785 key.offset = 1;
1786 key.flags = 0;
1787 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
1788 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
1789 &root_item);
54aa1f4d
CM
1790 if (ret)
1791 goto fail;
39279cc3
CM
1792
1793 /*
1794 * insert the directory item
1795 */
1796 key.offset = (u64)-1;
1797 dir = root->fs_info->sb->s_root->d_inode;
1798 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
1799 name, namelen, dir->i_ino, &key,
1800 BTRFS_FT_DIR);
54aa1f4d
CM
1801 if (ret)
1802 goto fail;
39279cc3
CM
1803
1804 ret = btrfs_commit_transaction(trans, root);
54aa1f4d
CM
1805 if (ret)
1806 goto fail_commit;
39279cc3 1807
58176a96 1808 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
39279cc3
CM
1809 BUG_ON(!new_root);
1810
1811 trans = btrfs_start_transaction(new_root, 1);
1812 BUG_ON(!trans);
1813
1814 inode = btrfs_new_inode(trans, new_root, new_dirid,
1815 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
54aa1f4d
CM
1816 if (IS_ERR(inode))
1817 goto fail;
39279cc3
CM
1818 inode->i_op = &btrfs_dir_inode_operations;
1819 inode->i_fop = &btrfs_dir_file_operations;
34088780 1820 new_root->inode = inode;
39279cc3
CM
1821
1822 ret = btrfs_make_empty_dir(trans, new_root, new_dirid, new_dirid);
54aa1f4d
CM
1823 if (ret)
1824 goto fail;
39279cc3
CM
1825
1826 inode->i_nlink = 1;
1827 inode->i_size = 6;
1828 ret = btrfs_update_inode(trans, new_root, inode);
54aa1f4d
CM
1829 if (ret)
1830 goto fail;
1831fail:
1832 err = btrfs_commit_transaction(trans, root);
1833 if (err && !ret)
1834 ret = err;
1835fail_commit:
39279cc3
CM
1836 mutex_unlock(&root->fs_info->fs_mutex);
1837 btrfs_btree_balance_dirty(root);
54aa1f4d 1838 return ret;
39279cc3
CM
1839}
1840
1841static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
1842{
1843 struct btrfs_trans_handle *trans;
1844 struct btrfs_key key;
1845 struct btrfs_root_item new_root_item;
83df7c1d 1846 struct buffer_head *tmp;
39279cc3 1847 int ret;
54aa1f4d 1848 int err;
39279cc3
CM
1849 u64 objectid;
1850
1851 if (!root->ref_cows)
1852 return -EINVAL;
1853
1854 mutex_lock(&root->fs_info->fs_mutex);
1855 trans = btrfs_start_transaction(root, 1);
1856 BUG_ON(!trans);
1857
1858 ret = btrfs_update_inode(trans, root, root->inode);
54aa1f4d
CM
1859 if (ret)
1860 goto fail;
39279cc3
CM
1861
1862 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
1863 0, &objectid);
54aa1f4d
CM
1864 if (ret)
1865 goto fail;
39279cc3
CM
1866
1867 memcpy(&new_root_item, &root->root_item,
1868 sizeof(new_root_item));
1869
1870 key.objectid = objectid;
1871 key.offset = 1;
1872 key.flags = 0;
1873 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
83df7c1d 1874 btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp);
39279cc3
CM
1875 btrfs_set_root_blocknr(&new_root_item, bh_blocknr(root->node));
1876
1877 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
1878 &new_root_item);
54aa1f4d
CM
1879 if (ret)
1880 goto fail;
39279cc3
CM
1881
1882 /*
1883 * insert the directory item
1884 */
1885 key.offset = (u64)-1;
1886 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
1887 name, namelen,
1888 root->fs_info->sb->s_root->d_inode->i_ino,
1889 &key, BTRFS_FT_DIR);
1890
54aa1f4d
CM
1891 if (ret)
1892 goto fail;
39279cc3
CM
1893
1894 ret = btrfs_inc_root_ref(trans, root);
54aa1f4d
CM
1895 if (ret)
1896 goto fail;
39279cc3 1897
54aa1f4d
CM
1898fail:
1899 err = btrfs_commit_transaction(trans, root);
1900 if (err && !ret)
1901 ret = err;
39279cc3
CM
1902 mutex_unlock(&root->fs_info->fs_mutex);
1903 btrfs_btree_balance_dirty(root);
54aa1f4d 1904 return ret;
39279cc3
CM
1905}
1906
1907int btrfs_ioctl(struct inode *inode, struct file *filp, unsigned int
1908 cmd, unsigned long arg)
1909{
1910 struct btrfs_root *root = BTRFS_I(inode)->root;
1911 struct btrfs_ioctl_vol_args vol_args;
1912 int ret = 0;
1913 struct btrfs_dir_item *di;
1914 int namelen;
1915 struct btrfs_path *path;
1916 u64 root_dirid;
1917
1918 switch (cmd) {
1919 case BTRFS_IOC_SNAP_CREATE:
1920 if (copy_from_user(&vol_args,
1921 (struct btrfs_ioctl_vol_args __user *)arg,
1922 sizeof(vol_args)))
1923 return -EFAULT;
1924 namelen = strlen(vol_args.name);
1925 if (namelen > BTRFS_VOL_NAME_MAX)
1926 return -EINVAL;
8a712645
CM
1927 if (strchr(vol_args.name, '/'))
1928 return -EINVAL;
39279cc3
CM
1929 path = btrfs_alloc_path();
1930 if (!path)
1931 return -ENOMEM;
1932 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
1933 mutex_lock(&root->fs_info->fs_mutex);
1934 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
1935 path, root_dirid,
1936 vol_args.name, namelen, 0);
1937 mutex_unlock(&root->fs_info->fs_mutex);
1938 btrfs_free_path(path);
1939 if (di && !IS_ERR(di))
1940 return -EEXIST;
54aa1f4d
CM
1941 if (IS_ERR(di))
1942 return PTR_ERR(di);
39279cc3
CM
1943
1944 if (root == root->fs_info->tree_root)
1945 ret = create_subvol(root, vol_args.name, namelen);
1946 else
1947 ret = create_snapshot(root, vol_args.name, namelen);
39279cc3 1948 break;
6702ed49
CM
1949
1950 case BTRFS_IOC_DEFRAG:
1951 mutex_lock(&root->fs_info->fs_mutex);
e9d0b13b
CM
1952 btrfs_defrag_root(root, 0);
1953 btrfs_defrag_root(root->fs_info->extent_root, 0);
6702ed49
CM
1954 mutex_unlock(&root->fs_info->fs_mutex);
1955 ret = 0;
1956 break;
39279cc3
CM
1957 default:
1958 return -ENOTTY;
1959 }
1960 return ret;
1961}
1962
1963#ifdef CONFIG_COMPAT
1964long btrfs_compat_ioctl(struct file *file, unsigned int cmd,
1965 unsigned long arg)
1966{
1967 struct inode *inode = file->f_path.dentry->d_inode;
1968 int ret;
1969 lock_kernel();
1970 ret = btrfs_ioctl(inode, file, cmd, (unsigned long) compat_ptr(arg));
1971 unlock_kernel();
1972 return ret;
1973
1974}
1975#endif
1976
1977/*
1978 * Called inside transaction, so use GFP_NOFS
1979 */
1980struct inode *btrfs_alloc_inode(struct super_block *sb)
1981{
1982 struct btrfs_inode *ei;
1983
1984 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
1985 if (!ei)
1986 return NULL;
15ee9bc7 1987 ei->last_trans = 0;
39279cc3
CM
1988 return &ei->vfs_inode;
1989}
1990
1991void btrfs_destroy_inode(struct inode *inode)
1992{
1993 WARN_ON(!list_empty(&inode->i_dentry));
1994 WARN_ON(inode->i_data.nrpages);
1995
1996 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
1997}
1998
1999static void init_once(void * foo, struct kmem_cache * cachep,
2000 unsigned long flags)
2001{
2002 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2003
2004 inode_init_once(&ei->vfs_inode);
2005}
2006
2007void btrfs_destroy_cachep(void)
2008{
2009 if (btrfs_inode_cachep)
2010 kmem_cache_destroy(btrfs_inode_cachep);
2011 if (btrfs_trans_handle_cachep)
2012 kmem_cache_destroy(btrfs_trans_handle_cachep);
2013 if (btrfs_transaction_cachep)
2014 kmem_cache_destroy(btrfs_transaction_cachep);
2015 if (btrfs_bit_radix_cachep)
2016 kmem_cache_destroy(btrfs_bit_radix_cachep);
2017 if (btrfs_path_cachep)
2018 kmem_cache_destroy(btrfs_path_cachep);
2019}
2020
92fee66d
CM
2021static struct kmem_cache *cache_create(const char *name, size_t size,
2022 unsigned long extra_flags,
2023 void (*ctor)(void *, struct kmem_cache *,
2024 unsigned long))
2025{
2026 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2027 SLAB_MEM_SPREAD | extra_flags), ctor
2028#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2029 ,NULL
2030#endif
2031 );
2032}
2033
39279cc3
CM
2034int btrfs_init_cachep(void)
2035{
92fee66d
CM
2036 btrfs_inode_cachep = cache_create("btrfs_inode_cache",
2037 sizeof(struct btrfs_inode),
2038 0, init_once);
39279cc3
CM
2039 if (!btrfs_inode_cachep)
2040 goto fail;
92fee66d 2041 btrfs_trans_handle_cachep = cache_create("btrfs_trans_handle_cache",
39279cc3 2042 sizeof(struct btrfs_trans_handle),
92fee66d 2043 0, NULL);
39279cc3
CM
2044 if (!btrfs_trans_handle_cachep)
2045 goto fail;
92fee66d 2046 btrfs_transaction_cachep = cache_create("btrfs_transaction_cache",
39279cc3 2047 sizeof(struct btrfs_transaction),
92fee66d 2048 0, NULL);
39279cc3
CM
2049 if (!btrfs_transaction_cachep)
2050 goto fail;
92fee66d
CM
2051 btrfs_path_cachep = cache_create("btrfs_path_cache",
2052 sizeof(struct btrfs_transaction),
2053 0, NULL);
39279cc3
CM
2054 if (!btrfs_path_cachep)
2055 goto fail;
92fee66d
CM
2056 btrfs_bit_radix_cachep = cache_create("btrfs_radix", 256,
2057 SLAB_DESTROY_BY_RCU, NULL);
39279cc3
CM
2058 if (!btrfs_bit_radix_cachep)
2059 goto fail;
2060 return 0;
2061fail:
2062 btrfs_destroy_cachep();
2063 return -ENOMEM;
2064}
2065
2066static int btrfs_getattr(struct vfsmount *mnt,
2067 struct dentry *dentry, struct kstat *stat)
2068{
2069 struct inode *inode = dentry->d_inode;
2070 generic_fillattr(inode, stat);
2071 stat->blksize = 256 * 1024;
2072 return 0;
2073}
2074
2075static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2076 struct inode * new_dir,struct dentry *new_dentry)
2077{
2078 struct btrfs_trans_handle *trans;
2079 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2080 struct inode *new_inode = new_dentry->d_inode;
2081 struct inode *old_inode = old_dentry->d_inode;
2082 struct timespec ctime = CURRENT_TIME;
2083 struct btrfs_path *path;
2084 struct btrfs_dir_item *di;
2085 int ret;
2086
2087 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2088 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2089 return -ENOTEMPTY;
2090 }
2091 mutex_lock(&root->fs_info->fs_mutex);
2092 trans = btrfs_start_transaction(root, 1);
2093 btrfs_set_trans_block_group(trans, new_dir);
2094 path = btrfs_alloc_path();
2095 if (!path) {
2096 ret = -ENOMEM;
2097 goto out_fail;
2098 }
2099
2100 old_dentry->d_inode->i_nlink++;
2101 old_dir->i_ctime = old_dir->i_mtime = ctime;
2102 new_dir->i_ctime = new_dir->i_mtime = ctime;
2103 old_inode->i_ctime = ctime;
2104 if (S_ISDIR(old_inode->i_mode) && old_dir != new_dir) {
2105 struct btrfs_key *location = &BTRFS_I(new_dir)->location;
2106 u64 old_parent_oid;
2107 di = btrfs_lookup_dir_item(trans, root, path, old_inode->i_ino,
2108 "..", 2, -1);
2109 if (IS_ERR(di)) {
2110 ret = PTR_ERR(di);
2111 goto out_fail;
2112 }
2113 if (!di) {
2114 ret = -ENOENT;
2115 goto out_fail;
2116 }
2117 old_parent_oid = btrfs_disk_key_objectid(&di->location);
2118 ret = btrfs_del_item(trans, root, path);
2119 if (ret) {
39279cc3
CM
2120 goto out_fail;
2121 }
2122 btrfs_release_path(root, path);
2123
2124 di = btrfs_lookup_dir_index_item(trans, root, path,
2125 old_inode->i_ino,
2126 old_parent_oid,
2127 "..", 2, -1);
2128 if (IS_ERR(di)) {
2129 ret = PTR_ERR(di);
2130 goto out_fail;
2131 }
2132 if (!di) {
2133 ret = -ENOENT;
2134 goto out_fail;
2135 }
2136 ret = btrfs_del_item(trans, root, path);
2137 if (ret) {
39279cc3
CM
2138 goto out_fail;
2139 }
2140 btrfs_release_path(root, path);
2141
2142 ret = btrfs_insert_dir_item(trans, root, "..", 2,
2143 old_inode->i_ino, location,
2144 BTRFS_FT_DIR);
2145 if (ret)
2146 goto out_fail;
2147 }
2148
2149
2150 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2151 if (ret)
2152 goto out_fail;
2153
2154 if (new_inode) {
2155 new_inode->i_ctime = CURRENT_TIME;
2156 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2157 if (ret)
2158 goto out_fail;
2159 if (S_ISDIR(new_inode->i_mode))
2160 clear_nlink(new_inode);
2161 else
2162 drop_nlink(new_inode);
54aa1f4d
CM
2163 ret = btrfs_update_inode(trans, root, new_inode);
2164 if (ret)
2165 goto out_fail;
39279cc3
CM
2166 }
2167 ret = btrfs_add_link(trans, new_dentry, old_inode);
2168 if (ret)
2169 goto out_fail;
2170
2171out_fail:
2172 btrfs_free_path(path);
2173 btrfs_end_transaction(trans, root);
2174 mutex_unlock(&root->fs_info->fs_mutex);
2175 return ret;
2176}
2177
2178static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2179 const char *symname)
2180{
2181 struct btrfs_trans_handle *trans;
2182 struct btrfs_root *root = BTRFS_I(dir)->root;
2183 struct btrfs_path *path;
2184 struct btrfs_key key;
2185 struct inode *inode;
2186 int err;
2187 int drop_inode = 0;
2188 u64 objectid;
2189 int name_len;
2190 int datasize;
2191 char *ptr;
2192 struct btrfs_file_extent_item *ei;
2193
2194 name_len = strlen(symname) + 1;
2195 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2196 return -ENAMETOOLONG;
2197 mutex_lock(&root->fs_info->fs_mutex);
2198 trans = btrfs_start_transaction(root, 1);
2199 btrfs_set_trans_block_group(trans, dir);
2200
2201 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2202 if (err) {
2203 err = -ENOSPC;
2204 goto out_unlock;
2205 }
2206
2207 inode = btrfs_new_inode(trans, root, objectid,
2208 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2209 err = PTR_ERR(inode);
2210 if (IS_ERR(inode))
2211 goto out_unlock;
2212
2213 btrfs_set_trans_block_group(trans, inode);
2214 err = btrfs_add_nondir(trans, dentry, inode);
2215 if (err)
2216 drop_inode = 1;
2217 else {
2218 inode->i_mapping->a_ops = &btrfs_aops;
2219 inode->i_fop = &btrfs_file_operations;
2220 inode->i_op = &btrfs_file_inode_operations;
a52d9a80
CM
2221 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
2222 inode->i_mapping, GFP_NOFS);
07157aac 2223 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
39279cc3
CM
2224 }
2225 dir->i_sb->s_dirt = 1;
2226 btrfs_update_inode_block_group(trans, inode);
2227 btrfs_update_inode_block_group(trans, dir);
2228 if (drop_inode)
2229 goto out_unlock;
2230
2231 path = btrfs_alloc_path();
2232 BUG_ON(!path);
2233 key.objectid = inode->i_ino;
2234 key.offset = 0;
2235 key.flags = 0;
2236 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2237 datasize = btrfs_file_extent_calc_inline_size(name_len);
2238 err = btrfs_insert_empty_item(trans, root, path, &key,
2239 datasize);
54aa1f4d
CM
2240 if (err) {
2241 drop_inode = 1;
2242 goto out_unlock;
2243 }
39279cc3
CM
2244 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
2245 path->slots[0], struct btrfs_file_extent_item);
2246 btrfs_set_file_extent_generation(ei, trans->transid);
2247 btrfs_set_file_extent_type(ei,
2248 BTRFS_FILE_EXTENT_INLINE);
2249 ptr = btrfs_file_extent_inline_start(ei);
2250 btrfs_memcpy(root, path->nodes[0]->b_data,
2251 ptr, symname, name_len);
ccd467d6 2252 btrfs_mark_buffer_dirty(path->nodes[0]);
39279cc3
CM
2253 btrfs_free_path(path);
2254 inode->i_op = &btrfs_symlink_inode_operations;
2255 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2256 inode->i_size = name_len - 1;
54aa1f4d
CM
2257 err = btrfs_update_inode(trans, root, inode);
2258 if (err)
2259 drop_inode = 1;
39279cc3
CM
2260
2261out_unlock:
2262 btrfs_end_transaction(trans, root);
2263 mutex_unlock(&root->fs_info->fs_mutex);
39279cc3
CM
2264 if (drop_inode) {
2265 inode_dec_link_count(inode);
2266 iput(inode);
2267 }
2268 btrfs_btree_balance_dirty(root);
2269 return err;
2270}
2271
2272static struct inode_operations btrfs_dir_inode_operations = {
2273 .lookup = btrfs_lookup,
2274 .create = btrfs_create,
2275 .unlink = btrfs_unlink,
2276 .link = btrfs_link,
2277 .mkdir = btrfs_mkdir,
2278 .rmdir = btrfs_rmdir,
2279 .rename = btrfs_rename,
2280 .symlink = btrfs_symlink,
2281 .setattr = btrfs_setattr,
618e21d5 2282 .mknod = btrfs_mknod,
39279cc3
CM
2283};
2284
2285static struct inode_operations btrfs_dir_ro_inode_operations = {
2286 .lookup = btrfs_lookup,
2287};
2288
2289static struct file_operations btrfs_dir_file_operations = {
2290 .llseek = generic_file_llseek,
2291 .read = generic_read_dir,
2292 .readdir = btrfs_readdir,
2293 .ioctl = btrfs_ioctl,
2294#ifdef CONFIG_COMPAT
2295 .compat_ioctl = btrfs_compat_ioctl,
2296#endif
2297};
2298
07157aac
CM
2299static struct extent_map_ops btrfs_extent_map_ops = {
2300 .fill_delalloc = run_delalloc_range,
2301 .writepage_io_hook = btrfs_writepage_io_hook,
2302 .readpage_io_hook = btrfs_readpage_io_hook,
2303 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
2304};
2305
39279cc3
CM
2306static struct address_space_operations btrfs_aops = {
2307 .readpage = btrfs_readpage,
2308 .writepage = btrfs_writepage,
2309 .sync_page = block_sync_page,
2310 .prepare_write = btrfs_prepare_write,
2311 .commit_write = btrfs_commit_write,
2312 .bmap = btrfs_bmap,
a52d9a80
CM
2313 .invalidatepage = btrfs_invalidatepage,
2314 .releasepage = btrfs_releasepage,
2315 .set_page_dirty = __set_page_dirty_nobuffers,
39279cc3
CM
2316};
2317
2318static struct address_space_operations btrfs_symlink_aops = {
2319 .readpage = btrfs_readpage,
2320 .writepage = btrfs_writepage,
2bf5a725
CM
2321 .invalidatepage = btrfs_invalidatepage,
2322 .releasepage = btrfs_releasepage,
39279cc3
CM
2323};
2324
2325static struct inode_operations btrfs_file_inode_operations = {
2326 .truncate = btrfs_truncate,
2327 .getattr = btrfs_getattr,
2328 .setattr = btrfs_setattr,
2329};
2330
618e21d5
JB
2331static struct inode_operations btrfs_special_inode_operations = {
2332 .getattr = btrfs_getattr,
2333 .setattr = btrfs_setattr,
2334};
2335
39279cc3
CM
2336static struct inode_operations btrfs_symlink_inode_operations = {
2337 .readlink = generic_readlink,
2338 .follow_link = page_follow_link_light,
2339 .put_link = page_put_link,
2340};