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