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