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