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