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