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