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