blob: 41b0c7de7a5916570183e8ed100177c58030e426 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
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
Chris Mason39279cc2007-06-12 06:35:45 -040019#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>
Chris Mason9ebefb182007-06-15 13:50:00 -040033#include <linux/bit_spinlock.h>
Chris Mason92fee662007-07-25 12:31:35 -040034#include <linux/version.h>
Josef Bacik5103e942007-11-16 11:45:54 -050035#include <linux/xattr.h>
Chris Mason39279cc2007-06-12 06:35:45 -040036#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;
Josef Bacik618e21d2007-07-11 10:18:17 -040051static struct inode_operations btrfs_special_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -040052static 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;
Chris Mason07157aa2007-08-30 08:50:51 -040056static struct extent_map_ops btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040057
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
Chris Masonb888db22007-08-27 16:49:44 -040075static 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;
Chris Masondb945352007-10-15 16:15:53 -040081 u64 num_bytes;
Chris Masonb888db22007-08-27 16:49:44 -040082 int ret;
Chris Masondb945352007-10-15 16:15:53 -040083 u64 blocksize = root->sectorsize;
Chris Masonb888db22007-08-27 16:49:44 -040084
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);
Chris Masondb945352007-10-15 16:15:53 -040089 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonb888db22007-08-27 16:49:44 -040090 ret = btrfs_drop_extents(trans, root, inode,
Chris Mason3326d1b2007-10-15 16:18:25 -040091 start, start + num_bytes, start, &alloc_hint);
Chris Masondb945352007-10-15 16:15:53 -040092
Chris Mason179e29e2007-11-01 11:28:41 -040093 if (alloc_hint == EXTENT_MAP_INLINE)
94 goto out;
95
Chris Mason7bb86312007-12-11 09:25:06 -050096 ret = btrfs_alloc_extent(trans, root, num_bytes,
97 root->root_key.objectid, trans->transid,
98 inode->i_ino, start, 0,
Chris Masonb888db22007-08-27 16:49:44 -040099 alloc_hint, (u64)-1, &ins, 1);
100 if (ret) {
101 WARN_ON(1);
102 goto out;
103 }
104 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
105 start, ins.objectid, ins.offset,
106 ins.offset);
107out:
108 btrfs_end_transaction(trans, root);
109 mutex_unlock(&root->fs_info->fs_mutex);
110 return ret;
111}
112
Chris Mason07157aa2007-08-30 08:50:51 -0400113int btrfs_writepage_io_hook(struct page *page, u64 start, u64 end)
114{
115 struct inode *inode = page->mapping->host;
116 struct btrfs_root *root = BTRFS_I(inode)->root;
117 struct btrfs_trans_handle *trans;
118 char *kaddr;
119 int ret;
Chris Mason35ebb932007-10-30 16:56:53 -0400120 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason07157aa2007-08-30 08:50:51 -0400121 size_t offset = start - page_start;
122
123 mutex_lock(&root->fs_info->fs_mutex);
124 trans = btrfs_start_transaction(root, 1);
125 btrfs_set_trans_block_group(trans, inode);
126 kaddr = kmap(page);
Chris Masonf578d4b2007-10-25 15:42:56 -0400127 btrfs_csum_file_block(trans, root, inode, inode->i_ino,
Chris Mason07157aa2007-08-30 08:50:51 -0400128 start, kaddr + offset, end - start + 1);
129 kunmap(page);
130 ret = btrfs_end_transaction(trans, root);
131 BUG_ON(ret);
132 mutex_unlock(&root->fs_info->fs_mutex);
133 return ret;
134}
135
136int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
137{
138 int ret = 0;
139 struct inode *inode = page->mapping->host;
140 struct btrfs_root *root = BTRFS_I(inode)->root;
141 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
142 struct btrfs_csum_item *item;
143 struct btrfs_path *path = NULL;
Chris Masonff79f812007-10-15 16:22:25 -0400144 u32 csum;
Chris Mason07157aa2007-08-30 08:50:51 -0400145
146 mutex_lock(&root->fs_info->fs_mutex);
147 path = btrfs_alloc_path();
148 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
149 if (IS_ERR(item)) {
150 ret = PTR_ERR(item);
151 /* a csum that isn't present is a preallocated region. */
152 if (ret == -ENOENT || ret == -EFBIG)
153 ret = 0;
Chris Masonff79f812007-10-15 16:22:25 -0400154 csum = 0;
Chris Mason07157aa2007-08-30 08:50:51 -0400155 goto out;
156 }
Chris Masonff79f812007-10-15 16:22:25 -0400157 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
158 BTRFS_CRC32_SIZE);
159 set_state_private(em_tree, start, csum);
Chris Mason07157aa2007-08-30 08:50:51 -0400160out:
161 if (path)
162 btrfs_free_path(path);
163 mutex_unlock(&root->fs_info->fs_mutex);
164 return ret;
165}
166
167int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end)
168{
Chris Mason35ebb932007-10-30 16:56:53 -0400169 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -0400170 struct inode *inode = page->mapping->host;
Chris Mason07157aa2007-08-30 08:50:51 -0400171 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
172 char *kaddr;
173 u64 private;
174 int ret;
Chris Masonff79f812007-10-15 16:22:25 -0400175 struct btrfs_root *root = BTRFS_I(inode)->root;
176 u32 csum = ~(u32)0;
Jens Axboebbf0d002007-10-19 09:23:07 -0400177 unsigned long flags;
Chris Mason07157aa2007-08-30 08:50:51 -0400178
179 ret = get_state_private(em_tree, start, &private);
Jens Axboebbf0d002007-10-19 09:23:07 -0400180 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400181 kaddr = kmap_atomic(page, KM_IRQ0);
182 if (ret) {
183 goto zeroit;
184 }
Chris Masonff79f812007-10-15 16:22:25 -0400185 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
186 btrfs_csum_final(csum, (char *)&csum);
187 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -0400188 goto zeroit;
189 }
190 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400191 local_irq_restore(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400192 return 0;
193
194zeroit:
195 printk("btrfs csum failed ino %lu off %llu\n",
196 page->mapping->host->i_ino, (unsigned long long)start);
Chris Masondb945352007-10-15 16:15:53 -0400197 memset(kaddr + offset, 1, end - start + 1);
198 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -0400199 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400200 local_irq_restore(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400201 return 0;
202}
Chris Masonb888db22007-08-27 16:49:44 -0400203
Chris Mason39279cc2007-06-12 06:35:45 -0400204void btrfs_read_locked_inode(struct inode *inode)
205{
206 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400207 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400208 struct btrfs_inode_item *inode_item;
Chris Mason5f39d392007-10-15 16:14:19 -0400209 struct btrfs_inode_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -0400210 struct btrfs_root *root = BTRFS_I(inode)->root;
211 struct btrfs_key location;
212 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -0400213 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -0400214 int ret;
215
216 path = btrfs_alloc_path();
217 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400218 mutex_lock(&root->fs_info->fs_mutex);
219
220 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
221 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400222 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -0400223 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -0400224
Chris Mason5f39d392007-10-15 16:14:19 -0400225 leaf = path->nodes[0];
226 inode_item = btrfs_item_ptr(leaf, path->slots[0],
227 struct btrfs_inode_item);
228
229 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
230 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
231 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
232 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
233 inode->i_size = btrfs_inode_size(leaf, inode_item);
234
235 tspec = btrfs_inode_atime(inode_item);
236 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
237 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
238
239 tspec = btrfs_inode_mtime(inode_item);
240 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
241 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
242
243 tspec = btrfs_inode_ctime(inode_item);
244 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
245 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
246
247 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
248 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik618e21d2007-07-11 10:18:17 -0400249 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400250 rdev = btrfs_inode_rdev(leaf, inode_item);
251
252 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -0400253 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
254 alloc_group_block);
255
256 btrfs_free_path(path);
257 inode_item = NULL;
258
259 mutex_unlock(&root->fs_info->fs_mutex);
260
261 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -0400262 case S_IFREG:
263 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason07157aa2007-08-30 08:50:51 -0400264 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -0400265 inode->i_fop = &btrfs_file_operations;
266 inode->i_op = &btrfs_file_inode_operations;
267 break;
268 case S_IFDIR:
269 inode->i_fop = &btrfs_dir_file_operations;
270 if (root == root->fs_info->tree_root)
271 inode->i_op = &btrfs_dir_ro_inode_operations;
272 else
273 inode->i_op = &btrfs_dir_inode_operations;
274 break;
275 case S_IFLNK:
276 inode->i_op = &btrfs_symlink_inode_operations;
277 inode->i_mapping->a_ops = &btrfs_symlink_aops;
278 break;
Josef Bacik618e21d2007-07-11 10:18:17 -0400279 default:
280 init_special_inode(inode, inode->i_mode, rdev);
281 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400282 }
283 return;
284
285make_bad:
286 btrfs_release_path(root, path);
287 btrfs_free_path(path);
288 mutex_unlock(&root->fs_info->fs_mutex);
289 make_bad_inode(inode);
290}
291
Chris Mason5f39d392007-10-15 16:14:19 -0400292static void fill_inode_item(struct extent_buffer *leaf,
293 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -0400294 struct inode *inode)
295{
Chris Mason5f39d392007-10-15 16:14:19 -0400296 btrfs_set_inode_uid(leaf, item, inode->i_uid);
297 btrfs_set_inode_gid(leaf, item, inode->i_gid);
298 btrfs_set_inode_size(leaf, item, inode->i_size);
299 btrfs_set_inode_mode(leaf, item, inode->i_mode);
300 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
301
302 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
303 inode->i_atime.tv_sec);
304 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
305 inode->i_atime.tv_nsec);
306
307 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
308 inode->i_mtime.tv_sec);
309 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
310 inode->i_mtime.tv_nsec);
311
312 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
313 inode->i_ctime.tv_sec);
314 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
315 inode->i_ctime.tv_nsec);
316
317 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
318 btrfs_set_inode_generation(leaf, item, inode->i_generation);
319 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
320 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -0400321 BTRFS_I(inode)->block_group->key.objectid);
322}
323
Chris Masona52d9a82007-08-27 16:49:44 -0400324int btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -0400325 struct btrfs_root *root,
326 struct inode *inode)
327{
328 struct btrfs_inode_item *inode_item;
329 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400330 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400331 int ret;
332
333 path = btrfs_alloc_path();
334 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400335 ret = btrfs_lookup_inode(trans, root, path,
336 &BTRFS_I(inode)->location, 1);
337 if (ret) {
338 if (ret > 0)
339 ret = -ENOENT;
340 goto failed;
341 }
342
Chris Mason5f39d392007-10-15 16:14:19 -0400343 leaf = path->nodes[0];
344 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400345 struct btrfs_inode_item);
346
Chris Mason5f39d392007-10-15 16:14:19 -0400347 fill_inode_item(leaf, inode_item, inode);
348 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400349 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400350 ret = 0;
351failed:
352 btrfs_release_path(root, path);
353 btrfs_free_path(path);
354 return ret;
355}
356
357
358static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
359 struct btrfs_root *root,
360 struct inode *dir,
361 struct dentry *dentry)
362{
363 struct btrfs_path *path;
364 const char *name = dentry->d_name.name;
365 int name_len = dentry->d_name.len;
366 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400367 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400368 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -0400369 struct btrfs_key key;
Chris Mason39279cc2007-06-12 06:35:45 -0400370
371 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400372 if (!path) {
373 ret = -ENOMEM;
374 goto err;
375 }
376
Chris Mason39279cc2007-06-12 06:35:45 -0400377 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
378 name, name_len, -1);
379 if (IS_ERR(di)) {
380 ret = PTR_ERR(di);
381 goto err;
382 }
383 if (!di) {
384 ret = -ENOENT;
385 goto err;
386 }
Chris Mason5f39d392007-10-15 16:14:19 -0400387 leaf = path->nodes[0];
388 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -0400389 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -0400390 if (ret)
391 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -0400392 btrfs_release_path(root, path);
393
394 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Chris Mason5f39d392007-10-15 16:14:19 -0400395 key.objectid, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -0400396 if (IS_ERR(di)) {
397 ret = PTR_ERR(di);
398 goto err;
399 }
400 if (!di) {
401 ret = -ENOENT;
402 goto err;
403 }
404 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason39279cc2007-06-12 06:35:45 -0400405
406 dentry->d_inode->i_ctime = dir->i_ctime;
Chris Mason76fea002007-12-13 09:06:01 -0500407 ret = btrfs_del_inode_ref(trans, root, name, name_len,
408 dentry->d_inode->i_ino,
409 dentry->d_parent->d_inode->i_ino);
410 if (ret) {
411 printk("failed to delete reference to %.*s, "
412 "inode %lu parent %lu\n", name_len, name,
413 dentry->d_inode->i_ino,
414 dentry->d_parent->d_inode->i_ino);
Chris Mason39544012007-12-12 14:38:19 -0500415 }
Chris Mason39279cc2007-06-12 06:35:45 -0400416err:
417 btrfs_free_path(path);
418 if (!ret) {
419 dir->i_size -= name_len * 2;
Chris Mason79c44582007-06-25 10:09:33 -0400420 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -0400421 btrfs_update_inode(trans, root, dir);
422 drop_nlink(dentry->d_inode);
Chris Mason54aa1f42007-06-22 14:16:25 -0400423 ret = btrfs_update_inode(trans, root, dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400424 dir->i_sb->s_dirt = 1;
425 }
426 return ret;
427}
428
429static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
430{
431 struct btrfs_root *root;
432 struct btrfs_trans_handle *trans;
433 int ret;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400434 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -0400435
436 root = BTRFS_I(dir)->root;
437 mutex_lock(&root->fs_info->fs_mutex);
438 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400439
Chris Mason39279cc2007-06-12 06:35:45 -0400440 btrfs_set_trans_block_group(trans, dir);
441 ret = btrfs_unlink_trans(trans, root, dir, dentry);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400442 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -0400443
Chris Mason39279cc2007-06-12 06:35:45 -0400444 btrfs_end_transaction(trans, root);
445 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400446 btrfs_btree_balance_dirty(root, nr);
Chris Mason5f39d392007-10-15 16:14:19 -0400447
Chris Mason39279cc2007-06-12 06:35:45 -0400448 return ret;
449}
450
451static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
452{
453 struct inode *inode = dentry->d_inode;
454 int err;
455 int ret;
456 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -0400457 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400458 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -0400459
Yan134d4512007-10-25 15:49:25 -0400460 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
461 return -ENOTEMPTY;
462
Chris Mason39279cc2007-06-12 06:35:45 -0400463 mutex_lock(&root->fs_info->fs_mutex);
464 trans = btrfs_start_transaction(root, 1);
465 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -0400466
467 /* now the directory is empty */
468 err = btrfs_unlink_trans(trans, root, dir, dentry);
469 if (!err) {
470 inode->i_size = 0;
471 }
Chris Mason39544012007-12-12 14:38:19 -0500472
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400473 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -0400474 ret = btrfs_end_transaction(trans, root);
Yan134d4512007-10-25 15:49:25 -0400475 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400476 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -0500477
Chris Mason39279cc2007-06-12 06:35:45 -0400478 if (ret && !err)
479 err = ret;
480 return err;
481}
482
483static int btrfs_free_inode(struct btrfs_trans_handle *trans,
484 struct btrfs_root *root,
485 struct inode *inode)
486{
487 struct btrfs_path *path;
488 int ret;
489
490 clear_inode(inode);
491
492 path = btrfs_alloc_path();
493 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400494 ret = btrfs_lookup_inode(trans, root, path,
495 &BTRFS_I(inode)->location, -1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400496 if (ret > 0)
497 ret = -ENOENT;
498 if (!ret)
499 ret = btrfs_del_item(trans, root, path);
Chris Mason39279cc2007-06-12 06:35:45 -0400500 btrfs_free_path(path);
501 return ret;
502}
503
504/*
Chris Mason39279cc2007-06-12 06:35:45 -0400505 * this can truncate away extent items, csum items and directory items.
506 * It starts at a high offset and removes keys until it can't find
507 * any higher than i_size.
508 *
509 * csum items that cross the new i_size are truncated to the new size
510 * as well.
511 */
512static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
513 struct btrfs_root *root,
514 struct inode *inode)
515{
516 int ret;
517 struct btrfs_path *path;
518 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400519 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -0400520 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -0400521 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400522 struct btrfs_file_extent_item *fi;
523 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -0400524 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400525 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500526 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -0500527 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400528 int found_extent;
529 int del_item;
Chris Mason179e29e2007-11-01 11:28:41 -0400530 int extent_type = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400531
Chris Masona52d9a82007-08-27 16:49:44 -0400532 btrfs_drop_extent_cache(inode, inode->i_size, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -0400533 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -0400534 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400535 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -0400536
Chris Mason39279cc2007-06-12 06:35:45 -0400537 /* FIXME, add redo link to tree so we don't leak on crash */
538 key.objectid = inode->i_ino;
539 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -0400540 key.type = (u8)-1;
541
Chris Mason39279cc2007-06-12 06:35:45 -0400542 while(1) {
543 btrfs_init_path(path);
544 fi = NULL;
545 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
546 if (ret < 0) {
547 goto error;
548 }
549 if (ret > 0) {
550 BUG_ON(path->slots[0] == 0);
551 path->slots[0]--;
552 }
Chris Mason5f39d392007-10-15 16:14:19 -0400553 leaf = path->nodes[0];
554 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
555 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -0400556
Chris Mason5f39d392007-10-15 16:14:19 -0400557 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -0400558 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400559
Chris Mason39279cc2007-06-12 06:35:45 -0400560 if (found_type != BTRFS_CSUM_ITEM_KEY &&
561 found_type != BTRFS_DIR_ITEM_KEY &&
562 found_type != BTRFS_DIR_INDEX_KEY &&
563 found_type != BTRFS_EXTENT_DATA_KEY)
564 break;
565
Chris Mason5f39d392007-10-15 16:14:19 -0400566 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -0400567 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -0400568 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400569 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -0400570 extent_type = btrfs_file_extent_type(leaf, fi);
571 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -0400572 item_end +=
Chris Masondb945352007-10-15 16:15:53 -0400573 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -0400574 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
575 struct btrfs_item *item = btrfs_item_nr(leaf,
576 path->slots[0]);
577 item_end += btrfs_file_extent_inline_len(leaf,
578 item);
Chris Mason39279cc2007-06-12 06:35:45 -0400579 }
Yan008630c2007-11-07 13:31:09 -0500580 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -0400581 }
582 if (found_type == BTRFS_CSUM_ITEM_KEY) {
583 ret = btrfs_csum_truncate(trans, root, path,
584 inode->i_size);
585 BUG_ON(ret);
586 }
Yan008630c2007-11-07 13:31:09 -0500587 if (item_end < inode->i_size) {
Chris Masonb888db22007-08-27 16:49:44 -0400588 if (found_type == BTRFS_DIR_ITEM_KEY) {
589 found_type = BTRFS_INODE_ITEM_KEY;
590 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
591 found_type = BTRFS_CSUM_ITEM_KEY;
592 } else if (found_type) {
593 found_type--;
594 } else {
595 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400596 }
Yana61721d2007-09-17 11:08:38 -0400597 btrfs_set_key_type(&key, found_type);
Yan65555a02007-10-25 15:42:57 -0400598 btrfs_release_path(root, path);
Chris Masonb888db22007-08-27 16:49:44 -0400599 continue;
Chris Mason39279cc2007-06-12 06:35:45 -0400600 }
Chris Mason5f39d392007-10-15 16:14:19 -0400601 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -0400602 del_item = 1;
603 else
604 del_item = 0;
605 found_extent = 0;
606
607 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -0400608 if (found_type != BTRFS_EXTENT_DATA_KEY)
609 goto delete;
610
611 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -0400612 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -0400613 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400614 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -0400615 u64 orig_num_bytes =
616 btrfs_file_extent_num_bytes(leaf, fi);
617 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -0400618 found_key.offset + root->sectorsize - 1;
Chris Masondb945352007-10-15 16:15:53 -0400619 btrfs_set_file_extent_num_bytes(leaf, fi,
620 extent_num_bytes);
621 num_dec = (orig_num_bytes -
622 extent_num_bytes) >> 9;
Yanbab9fb02007-09-17 11:13:11 -0400623 if (extent_start != 0) {
624 inode->i_blocks -= num_dec;
625 }
Chris Mason5f39d392007-10-15 16:14:19 -0400626 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -0400627 } else {
Chris Masondb945352007-10-15 16:15:53 -0400628 extent_num_bytes =
629 btrfs_file_extent_disk_num_bytes(leaf,
630 fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400631 /* FIXME blocksize != 4096 */
Chris Masondb945352007-10-15 16:15:53 -0400632 num_dec = btrfs_file_extent_num_bytes(leaf,
633 fi) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -0400634 if (extent_start != 0) {
635 found_extent = 1;
636 inode->i_blocks -= num_dec;
637 }
Chris Masond8d5f3e2007-12-11 12:42:00 -0500638 root_gen = btrfs_header_generation(leaf);
639 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -0400640 }
Chris Mason179e29e2007-11-01 11:28:41 -0400641 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE &&
642 !del_item) {
643 u32 newsize = inode->i_size - found_key.offset;
644 newsize = btrfs_file_extent_calc_inline_size(newsize);
645 ret = btrfs_truncate_item(trans, root, path,
646 newsize, 1);
647 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -0400648 }
Chris Mason179e29e2007-11-01 11:28:41 -0400649delete:
Chris Mason39279cc2007-06-12 06:35:45 -0400650 if (del_item) {
651 ret = btrfs_del_item(trans, root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400652 if (ret)
653 goto error;
Chris Mason39279cc2007-06-12 06:35:45 -0400654 } else {
655 break;
656 }
657 btrfs_release_path(root, path);
658 if (found_extent) {
659 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -0500660 extent_num_bytes,
Chris Masond8d5f3e2007-12-11 12:42:00 -0500661 root_owner,
Chris Mason7bb86312007-12-11 09:25:06 -0500662 root_gen, inode->i_ino,
663 found_key.offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -0400664 BUG_ON(ret);
665 }
666 }
667 ret = 0;
668error:
669 btrfs_release_path(root, path);
670 btrfs_free_path(path);
671 inode->i_sb->s_dirt = 1;
672 return ret;
673}
674
Chris Masonb888db22007-08-27 16:49:44 -0400675static int btrfs_cow_one_page(struct inode *inode, struct page *page,
Chris Masona52d9a82007-08-27 16:49:44 -0400676 size_t zero_start)
Chris Mason39279cc2007-06-12 06:35:45 -0400677{
Chris Mason39279cc2007-06-12 06:35:45 -0400678 char *kaddr;
679 int ret = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400680 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason35ebb932007-10-30 16:56:53 -0400681 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masonb888db22007-08-27 16:49:44 -0400682 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Mason39279cc2007-06-12 06:35:45 -0400683
Christoph Hellwigb3cfa352007-09-17 11:25:58 -0400684 set_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400685
Chris Masonb888db22007-08-27 16:49:44 -0400686 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
687 set_extent_delalloc(&BTRFS_I(inode)->extent_tree, page_start,
688 page_end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -0400689 if (zero_start != PAGE_CACHE_SIZE) {
Chris Masonb888db22007-08-27 16:49:44 -0400690 kaddr = kmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400691 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
692 flush_dcache_page(page);
Chris Masonb888db22007-08-27 16:49:44 -0400693 kunmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400694 }
Chris Masonb888db22007-08-27 16:49:44 -0400695 set_page_dirty(page);
696 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -0400697
Chris Masona52d9a82007-08-27 16:49:44 -0400698 return ret;
699}
700
701/*
702 * taken from block_truncate_page, but does cow as it zeros out
703 * any bytes left in the last page in the file.
704 */
705static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
706{
707 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -0400708 struct btrfs_root *root = BTRFS_I(inode)->root;
709 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -0400710 pgoff_t index = from >> PAGE_CACHE_SHIFT;
711 unsigned offset = from & (PAGE_CACHE_SIZE-1);
712 struct page *page;
713 int ret = 0;
714 u64 page_start;
715
716 if ((offset & (blocksize - 1)) == 0)
717 goto out;
718
Chris Masondb945352007-10-15 16:15:53 -0400719 down_read(&root->snap_sem);
Chris Masona52d9a82007-08-27 16:49:44 -0400720 ret = -ENOMEM;
721 page = grab_cache_page(mapping, index);
722 if (!page)
723 goto out;
724 if (!PageUptodate(page)) {
725 ret = btrfs_readpage(NULL, page);
726 lock_page(page);
727 if (!PageUptodate(page)) {
728 ret = -EIO;
729 goto out;
730 }
731 }
Chris Mason35ebb932007-10-30 16:56:53 -0400732 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -0400733
Chris Masonb888db22007-08-27 16:49:44 -0400734 ret = btrfs_cow_one_page(inode, page, offset);
Chris Mason39279cc2007-06-12 06:35:45 -0400735
Chris Mason39279cc2007-06-12 06:35:45 -0400736 unlock_page(page);
737 page_cache_release(page);
Chris Mason011410b2007-09-10 19:58:36 -0400738 up_read(&BTRFS_I(inode)->root->snap_sem);
Chris Mason39279cc2007-06-12 06:35:45 -0400739out:
740 return ret;
741}
742
743static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
744{
745 struct inode *inode = dentry->d_inode;
746 int err;
747
748 err = inode_change_ok(inode, attr);
749 if (err)
750 return err;
751
752 if (S_ISREG(inode->i_mode) &&
753 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
754 struct btrfs_trans_handle *trans;
755 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason2bf5a722007-08-30 11:54:02 -0400756 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
757
Chris Mason5f39d392007-10-15 16:14:19 -0400758 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -0400759 u64 pos = (inode->i_size + mask) & ~mask;
Chris Mason2bf5a722007-08-30 11:54:02 -0400760 u64 block_end = attr->ia_size | mask;
Chris Mason39279cc2007-06-12 06:35:45 -0400761 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -0400762 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400763
764 if (attr->ia_size <= pos)
765 goto out;
766
767 btrfs_truncate_page(inode->i_mapping, inode->i_size);
768
Chris Mason2bf5a722007-08-30 11:54:02 -0400769 lock_extent(em_tree, pos, block_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -0400770 hole_size = (attr->ia_size - pos + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -0400771
772 mutex_lock(&root->fs_info->fs_mutex);
773 trans = btrfs_start_transaction(root, 1);
774 btrfs_set_trans_block_group(trans, inode);
Chris Mason2bf5a722007-08-30 11:54:02 -0400775 err = btrfs_drop_extents(trans, root, inode,
Chris Mason3326d1b2007-10-15 16:18:25 -0400776 pos, pos + hole_size, pos,
777 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -0400778
Chris Mason179e29e2007-11-01 11:28:41 -0400779 if (alloc_hint != EXTENT_MAP_INLINE) {
780 err = btrfs_insert_file_extent(trans, root,
781 inode->i_ino,
782 pos, 0, 0, hole_size);
783 }
Chris Mason39279cc2007-06-12 06:35:45 -0400784 btrfs_end_transaction(trans, root);
785 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason2bf5a722007-08-30 11:54:02 -0400786 unlock_extent(em_tree, pos, block_end, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -0400787 if (err)
788 return err;
Chris Mason39279cc2007-06-12 06:35:45 -0400789 }
790out:
791 err = inode_setattr(inode, attr);
792
793 return err;
794}
795void btrfs_delete_inode(struct inode *inode)
796{
797 struct btrfs_trans_handle *trans;
798 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400799 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -0400800 int ret;
801
802 truncate_inode_pages(&inode->i_data, 0);
803 if (is_bad_inode(inode)) {
804 goto no_delete;
805 }
Chris Mason5f39d392007-10-15 16:14:19 -0400806
Chris Mason39279cc2007-06-12 06:35:45 -0400807 inode->i_size = 0;
808 mutex_lock(&root->fs_info->fs_mutex);
809 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400810
Chris Mason39279cc2007-06-12 06:35:45 -0400811 btrfs_set_trans_block_group(trans, inode);
812 ret = btrfs_truncate_in_trans(trans, root, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -0400813 if (ret)
814 goto no_delete_lock;
Josef Bacik5103e942007-11-16 11:45:54 -0500815 ret = btrfs_delete_xattrs(trans, root, inode);
816 if (ret)
817 goto no_delete_lock;
Chris Mason54aa1f42007-06-22 14:16:25 -0400818 ret = btrfs_free_inode(trans, root, inode);
819 if (ret)
820 goto no_delete_lock;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400821 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -0400822
Chris Mason39279cc2007-06-12 06:35:45 -0400823 btrfs_end_transaction(trans, root);
824 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400825 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -0400826 return;
Chris Mason54aa1f42007-06-22 14:16:25 -0400827
828no_delete_lock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400829 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -0400830 btrfs_end_transaction(trans, root);
831 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400832 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -0400833no_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;
Yan0d9f7f32007-10-25 15:48:28 -0400849 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400850
Chris Mason39544012007-12-12 14:38:19 -0500851 if (namelen == 1 && strcmp(name, ".") == 0) {
852 location->objectid = dir->i_ino;
853 location->type = BTRFS_INODE_ITEM_KEY;
854 location->offset = 0;
855 return 0;
856 }
Chris Mason39279cc2007-06-12 06:35:45 -0400857 path = btrfs_alloc_path();
858 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -0500859
Chris Mason7a720532007-12-13 09:06:59 -0500860 if (namelen == 2 && strcmp(name, "..") == 0) {
Chris Mason39544012007-12-12 14:38:19 -0500861 struct btrfs_key key;
862 struct extent_buffer *leaf;
863 u32 nritems;
864 int slot;
865
866 key.objectid = dir->i_ino;
867 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
868 key.offset = 0;
869 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
870 BUG_ON(ret == 0);
871 ret = 0;
872
873 leaf = path->nodes[0];
874 slot = path->slots[0];
875 nritems = btrfs_header_nritems(leaf);
876 if (slot >= nritems)
877 goto out_err;
878
879 btrfs_item_key_to_cpu(leaf, &key, slot);
880 if (key.objectid != dir->i_ino ||
881 key.type != BTRFS_INODE_REF_KEY) {
882 goto out_err;
883 }
884 location->objectid = key.offset;
885 location->type = BTRFS_INODE_ITEM_KEY;
886 location->offset = 0;
887 goto out;
888 }
889
Chris Mason39279cc2007-06-12 06:35:45 -0400890 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
891 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -0400892 if (IS_ERR(di))
893 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -0400894 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -0500895 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -0400896 }
Chris Mason5f39d392007-10-15 16:14:19 -0400897 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -0400898out:
Chris Mason39279cc2007-06-12 06:35:45 -0400899 btrfs_free_path(path);
900 return ret;
Chris Mason39544012007-12-12 14:38:19 -0500901out_err:
902 location->objectid = 0;
903 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -0400904}
905
906/*
907 * when we hit a tree root in a directory, the btrfs part of the inode
908 * needs to be changed to reflect the root directory of the tree root. This
909 * is kind of like crossing a mount point.
910 */
911static int fixup_tree_root_location(struct btrfs_root *root,
912 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -0400913 struct btrfs_root **sub_root,
914 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -0400915{
916 struct btrfs_path *path;
917 struct btrfs_root_item *ri;
918
919 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
920 return 0;
921 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
922 return 0;
923
924 path = btrfs_alloc_path();
925 BUG_ON(!path);
926 mutex_lock(&root->fs_info->fs_mutex);
927
Josef Bacik58176a92007-08-29 15:47:34 -0400928 *sub_root = btrfs_read_fs_root(root->fs_info, location,
929 dentry->d_name.name,
930 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -0400931 if (IS_ERR(*sub_root))
932 return PTR_ERR(*sub_root);
933
934 ri = &(*sub_root)->root_item;
935 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -0400936 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
937 location->offset = 0;
938
939 btrfs_free_path(path);
940 mutex_unlock(&root->fs_info->fs_mutex);
941 return 0;
942}
943
944static int btrfs_init_locked_inode(struct inode *inode, void *p)
945{
946 struct btrfs_iget_args *args = p;
947 inode->i_ino = args->ino;
948 BTRFS_I(inode)->root = args->root;
Chris Masonb888db22007-08-27 16:49:44 -0400949 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
950 inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -0400951 return 0;
952}
953
954static int btrfs_find_actor(struct inode *inode, void *opaque)
955{
956 struct btrfs_iget_args *args = opaque;
957 return (args->ino == inode->i_ino &&
958 args->root == BTRFS_I(inode)->root);
959}
960
961struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
962 struct btrfs_root *root)
963{
964 struct inode *inode;
965 struct btrfs_iget_args args;
966 args.ino = objectid;
967 args.root = root;
968
969 inode = iget5_locked(s, objectid, btrfs_find_actor,
970 btrfs_init_locked_inode,
971 (void *)&args);
972 return inode;
973}
974
975static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
976 struct nameidata *nd)
977{
978 struct inode * inode;
979 struct btrfs_inode *bi = BTRFS_I(dir);
980 struct btrfs_root *root = bi->root;
981 struct btrfs_root *sub_root = root;
982 struct btrfs_key location;
983 int ret;
984
985 if (dentry->d_name.len > BTRFS_NAME_LEN)
986 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -0400987
Chris Mason39279cc2007-06-12 06:35:45 -0400988 mutex_lock(&root->fs_info->fs_mutex);
989 ret = btrfs_inode_by_name(dir, dentry, &location);
990 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason5f39d392007-10-15 16:14:19 -0400991
Chris Mason39279cc2007-06-12 06:35:45 -0400992 if (ret < 0)
993 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -0400994
Chris Mason39279cc2007-06-12 06:35:45 -0400995 inode = NULL;
996 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -0400997 ret = fixup_tree_root_location(root, &location, &sub_root,
998 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -0400999 if (ret < 0)
1000 return ERR_PTR(ret);
1001 if (ret > 0)
1002 return ERR_PTR(-ENOENT);
1003 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1004 sub_root);
1005 if (!inode)
1006 return ERR_PTR(-EACCES);
1007 if (inode->i_state & I_NEW) {
1008 /* the inode and parent dir are two different roots */
1009 if (sub_root != root) {
1010 igrab(inode);
1011 sub_root->inode = inode;
1012 }
1013 BTRFS_I(inode)->root = sub_root;
1014 memcpy(&BTRFS_I(inode)->location, &location,
1015 sizeof(location));
1016 btrfs_read_locked_inode(inode);
1017 unlock_new_inode(inode);
1018 }
1019 }
1020 return d_splice_alias(inode, dentry);
1021}
1022
Chris Mason39279cc2007-06-12 06:35:45 -04001023static unsigned char btrfs_filetype_table[] = {
1024 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1025};
1026
1027static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1028{
1029 struct inode *inode = filp->f_path.dentry->d_inode;
1030 struct btrfs_root *root = BTRFS_I(inode)->root;
1031 struct btrfs_item *item;
1032 struct btrfs_dir_item *di;
1033 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001034 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001035 struct btrfs_path *path;
1036 int ret;
1037 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001038 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001039 int slot;
1040 int advance;
1041 unsigned char d_type;
1042 int over = 0;
1043 u32 di_cur;
1044 u32 di_total;
1045 u32 di_len;
1046 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001047 char tmp_name[32];
1048 char *name_ptr;
1049 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001050
1051 /* FIXME, use a real flag for deciding about the key type */
1052 if (root->fs_info->tree_root == root)
1053 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001054
Chris Mason39544012007-12-12 14:38:19 -05001055 /* special case for "." */
1056 if (filp->f_pos == 0) {
1057 over = filldir(dirent, ".", 1,
1058 1, inode->i_ino,
1059 DT_DIR);
1060 if (over)
1061 return 0;
1062 filp->f_pos = 1;
1063 }
1064
Chris Mason39279cc2007-06-12 06:35:45 -04001065 mutex_lock(&root->fs_info->fs_mutex);
1066 key.objectid = inode->i_ino;
Chris Mason39544012007-12-12 14:38:19 -05001067 path = btrfs_alloc_path();
1068 path->reada = 2;
1069
1070 /* special case for .., just use the back ref */
1071 if (filp->f_pos == 1) {
1072 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1073 key.offset = 0;
1074 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1075 BUG_ON(ret == 0);
1076 leaf = path->nodes[0];
1077 slot = path->slots[0];
1078 nritems = btrfs_header_nritems(leaf);
1079 if (slot >= nritems) {
1080 btrfs_release_path(root, path);
1081 goto read_dir_items;
1082 }
1083 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1084 btrfs_release_path(root, path);
1085 if (found_key.objectid != key.objectid ||
1086 found_key.type != BTRFS_INODE_REF_KEY)
1087 goto read_dir_items;
1088 over = filldir(dirent, "..", 2,
1089 2, found_key.offset, DT_DIR);
1090 if (over)
1091 goto nopos;
1092 filp->f_pos = 2;
1093 }
1094
1095read_dir_items:
Chris Mason39279cc2007-06-12 06:35:45 -04001096 btrfs_set_key_type(&key, key_type);
1097 key.offset = filp->f_pos;
Chris Mason5f39d392007-10-15 16:14:19 -04001098
Chris Mason39279cc2007-06-12 06:35:45 -04001099 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1100 if (ret < 0)
1101 goto err;
1102 advance = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001103 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001104 leaf = path->nodes[0];
1105 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001106 slot = path->slots[0];
1107 if (advance || slot >= nritems) {
1108 if (slot >= nritems -1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001109 ret = btrfs_next_leaf(root, path);
1110 if (ret)
1111 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001112 leaf = path->nodes[0];
1113 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001114 slot = path->slots[0];
1115 } else {
1116 slot++;
1117 path->slots[0]++;
1118 }
1119 }
1120 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001121 item = btrfs_item_nr(leaf, slot);
1122 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1123
1124 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04001125 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001126 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001127 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001128 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04001129 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001130
1131 filp->f_pos = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001132 advance = 1;
1133 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1134 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001135 di_total = btrfs_item_size(leaf, item);
Chris Mason39279cc2007-06-12 06:35:45 -04001136 while(di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04001137 struct btrfs_key location;
1138
1139 name_len = btrfs_dir_name_len(leaf, di);
1140 if (name_len < 32) {
1141 name_ptr = tmp_name;
1142 } else {
1143 name_ptr = kmalloc(name_len, GFP_NOFS);
1144 BUG_ON(!name_ptr);
1145 }
1146 read_extent_buffer(leaf, name_ptr,
1147 (unsigned long)(di + 1), name_len);
1148
1149 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1150 btrfs_dir_item_key_to_cpu(leaf, di, &location);
1151
1152 over = filldir(dirent, name_ptr, name_len,
1153 found_key.offset,
1154 location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001155 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04001156
1157 if (name_ptr != tmp_name)
1158 kfree(name_ptr);
1159
Chris Mason39279cc2007-06-12 06:35:45 -04001160 if (over)
1161 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05001162 di_len = btrfs_dir_name_len(leaf, di) +
1163 btrfs_dir_data_len(leaf, di) +sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04001164 di_cur += di_len;
1165 di = (struct btrfs_dir_item *)((char *)di + di_len);
1166 }
1167 }
1168 filp->f_pos++;
1169nopos:
1170 ret = 0;
1171err:
1172 btrfs_release_path(root, path);
1173 btrfs_free_path(path);
1174 mutex_unlock(&root->fs_info->fs_mutex);
1175 return ret;
1176}
1177
1178int btrfs_write_inode(struct inode *inode, int wait)
1179{
1180 struct btrfs_root *root = BTRFS_I(inode)->root;
1181 struct btrfs_trans_handle *trans;
1182 int ret = 0;
1183
1184 if (wait) {
1185 mutex_lock(&root->fs_info->fs_mutex);
1186 trans = btrfs_start_transaction(root, 1);
1187 btrfs_set_trans_block_group(trans, inode);
1188 ret = btrfs_commit_transaction(trans, root);
1189 mutex_unlock(&root->fs_info->fs_mutex);
1190 }
1191 return ret;
1192}
1193
1194/*
Chris Mason54aa1f42007-06-22 14:16:25 -04001195 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04001196 * inode changes. But, it is most likely to find the inode in cache.
1197 * FIXME, needs more benchmarking...there are no reasons other than performance
1198 * to keep or drop this code.
1199 */
1200void btrfs_dirty_inode(struct inode *inode)
1201{
1202 struct btrfs_root *root = BTRFS_I(inode)->root;
1203 struct btrfs_trans_handle *trans;
1204
1205 mutex_lock(&root->fs_info->fs_mutex);
1206 trans = btrfs_start_transaction(root, 1);
1207 btrfs_set_trans_block_group(trans, inode);
1208 btrfs_update_inode(trans, root, inode);
1209 btrfs_end_transaction(trans, root);
1210 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001211}
1212
1213static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1214 struct btrfs_root *root,
1215 u64 objectid,
1216 struct btrfs_block_group_cache *group,
1217 int mode)
1218{
1219 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001220 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04001221 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04001222 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04001223 int ret;
1224 int owner;
1225
Chris Mason5f39d392007-10-15 16:14:19 -04001226 path = btrfs_alloc_path();
1227 BUG_ON(!path);
1228
Chris Mason39279cc2007-06-12 06:35:45 -04001229 inode = new_inode(root->fs_info->sb);
1230 if (!inode)
1231 return ERR_PTR(-ENOMEM);
1232
Chris Masonb888db22007-08-27 16:49:44 -04001233 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1234 inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001235 BTRFS_I(inode)->root = root;
Chris Masonb888db22007-08-27 16:49:44 -04001236
Chris Mason39279cc2007-06-12 06:35:45 -04001237 if (mode & S_IFDIR)
1238 owner = 0;
1239 else
1240 owner = 1;
1241 group = btrfs_find_block_group(root, group, 0, 0, owner);
1242 BTRFS_I(inode)->block_group = group;
1243
Chris Mason5f39d392007-10-15 16:14:19 -04001244 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
1245 if (ret)
1246 goto fail;
1247
Chris Mason39279cc2007-06-12 06:35:45 -04001248 inode->i_uid = current->fsuid;
1249 inode->i_gid = current->fsgid;
1250 inode->i_mode = mode;
1251 inode->i_ino = objectid;
1252 inode->i_blocks = 0;
1253 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04001254 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1255 struct btrfs_inode_item);
1256 fill_inode_item(path->nodes[0], inode_item, inode);
1257 btrfs_mark_buffer_dirty(path->nodes[0]);
1258 btrfs_free_path(path);
1259
Chris Mason39279cc2007-06-12 06:35:45 -04001260 location = &BTRFS_I(inode)->location;
1261 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04001262 location->offset = 0;
1263 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1264
Chris Mason39279cc2007-06-12 06:35:45 -04001265 insert_inode_hash(inode);
1266 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001267fail:
1268 btrfs_free_path(path);
1269 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04001270}
1271
1272static inline u8 btrfs_inode_type(struct inode *inode)
1273{
1274 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1275}
1276
1277static int btrfs_add_link(struct btrfs_trans_handle *trans,
1278 struct dentry *dentry, struct inode *inode)
1279{
1280 int ret;
1281 struct btrfs_key key;
1282 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Chris Mason79c44582007-06-25 10:09:33 -04001283 struct inode *parent_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001284
Chris Mason39279cc2007-06-12 06:35:45 -04001285 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04001286 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1287 key.offset = 0;
1288
1289 ret = btrfs_insert_dir_item(trans, root,
1290 dentry->d_name.name, dentry->d_name.len,
1291 dentry->d_parent->d_inode->i_ino,
1292 &key, btrfs_inode_type(inode));
1293 if (ret == 0) {
Chris Mason76fea002007-12-13 09:06:01 -05001294 ret = btrfs_insert_inode_ref(trans, root,
1295 dentry->d_name.name,
1296 dentry->d_name.len,
1297 inode->i_ino,
1298 dentry->d_parent->d_inode->i_ino);
Chris Mason79c44582007-06-25 10:09:33 -04001299 parent_inode = dentry->d_parent->d_inode;
1300 parent_inode->i_size += dentry->d_name.len * 2;
1301 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001302 ret = btrfs_update_inode(trans, root,
1303 dentry->d_parent->d_inode);
1304 }
1305 return ret;
1306}
1307
1308static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1309 struct dentry *dentry, struct inode *inode)
1310{
1311 int err = btrfs_add_link(trans, dentry, inode);
1312 if (!err) {
1313 d_instantiate(dentry, inode);
1314 return 0;
1315 }
1316 if (err > 0)
1317 err = -EEXIST;
1318 return err;
1319}
1320
Josef Bacik618e21d2007-07-11 10:18:17 -04001321static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1322 int mode, dev_t rdev)
1323{
1324 struct btrfs_trans_handle *trans;
1325 struct btrfs_root *root = BTRFS_I(dir)->root;
1326 struct inode *inode;
1327 int err;
1328 int drop_inode = 0;
1329 u64 objectid;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001330 unsigned long nr;
Josef Bacik618e21d2007-07-11 10:18:17 -04001331
1332 if (!new_valid_dev(rdev))
1333 return -EINVAL;
1334
1335 mutex_lock(&root->fs_info->fs_mutex);
1336 trans = btrfs_start_transaction(root, 1);
1337 btrfs_set_trans_block_group(trans, dir);
1338
1339 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1340 if (err) {
1341 err = -ENOSPC;
1342 goto out_unlock;
1343 }
1344
1345 inode = btrfs_new_inode(trans, root, objectid,
1346 BTRFS_I(dir)->block_group, mode);
1347 err = PTR_ERR(inode);
1348 if (IS_ERR(inode))
1349 goto out_unlock;
1350
1351 btrfs_set_trans_block_group(trans, inode);
1352 err = btrfs_add_nondir(trans, dentry, inode);
1353 if (err)
1354 drop_inode = 1;
1355 else {
1356 inode->i_op = &btrfs_special_inode_operations;
1357 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04001358 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04001359 }
1360 dir->i_sb->s_dirt = 1;
1361 btrfs_update_inode_block_group(trans, inode);
1362 btrfs_update_inode_block_group(trans, dir);
1363out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001364 nr = trans->blocks_used;
Josef Bacik618e21d2007-07-11 10:18:17 -04001365 btrfs_end_transaction(trans, root);
1366 mutex_unlock(&root->fs_info->fs_mutex);
1367
1368 if (drop_inode) {
1369 inode_dec_link_count(inode);
1370 iput(inode);
1371 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001372 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04001373 return err;
1374}
1375
Chris Mason39279cc2007-06-12 06:35:45 -04001376static int btrfs_create(struct inode *dir, struct dentry *dentry,
1377 int mode, struct nameidata *nd)
1378{
1379 struct btrfs_trans_handle *trans;
1380 struct btrfs_root *root = BTRFS_I(dir)->root;
1381 struct inode *inode;
1382 int err;
1383 int drop_inode = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001384 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001385 u64 objectid;
1386
1387 mutex_lock(&root->fs_info->fs_mutex);
1388 trans = btrfs_start_transaction(root, 1);
1389 btrfs_set_trans_block_group(trans, dir);
1390
1391 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1392 if (err) {
1393 err = -ENOSPC;
1394 goto out_unlock;
1395 }
1396
1397 inode = btrfs_new_inode(trans, root, objectid,
1398 BTRFS_I(dir)->block_group, mode);
1399 err = PTR_ERR(inode);
1400 if (IS_ERR(inode))
1401 goto out_unlock;
1402
1403 btrfs_set_trans_block_group(trans, inode);
1404 err = btrfs_add_nondir(trans, dentry, inode);
1405 if (err)
1406 drop_inode = 1;
1407 else {
1408 inode->i_mapping->a_ops = &btrfs_aops;
1409 inode->i_fop = &btrfs_file_operations;
1410 inode->i_op = &btrfs_file_inode_operations;
Chris Masona52d9a82007-08-27 16:49:44 -04001411 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1412 inode->i_mapping, GFP_NOFS);
Chris Mason07157aa2007-08-30 08:50:51 -04001413 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04001414 }
1415 dir->i_sb->s_dirt = 1;
1416 btrfs_update_inode_block_group(trans, inode);
1417 btrfs_update_inode_block_group(trans, dir);
1418out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001419 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001420 btrfs_end_transaction(trans, root);
1421 mutex_unlock(&root->fs_info->fs_mutex);
1422
1423 if (drop_inode) {
1424 inode_dec_link_count(inode);
1425 iput(inode);
1426 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001427 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001428 return err;
1429}
1430
1431static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1432 struct dentry *dentry)
1433{
1434 struct btrfs_trans_handle *trans;
1435 struct btrfs_root *root = BTRFS_I(dir)->root;
1436 struct inode *inode = old_dentry->d_inode;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001437 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001438 int err;
1439 int drop_inode = 0;
1440
1441 if (inode->i_nlink == 0)
1442 return -ENOENT;
1443
1444 inc_nlink(inode);
1445 mutex_lock(&root->fs_info->fs_mutex);
1446 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001447
Chris Mason39279cc2007-06-12 06:35:45 -04001448 btrfs_set_trans_block_group(trans, dir);
1449 atomic_inc(&inode->i_count);
1450 err = btrfs_add_nondir(trans, dentry, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001451
Chris Mason39279cc2007-06-12 06:35:45 -04001452 if (err)
1453 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001454
Chris Mason39279cc2007-06-12 06:35:45 -04001455 dir->i_sb->s_dirt = 1;
1456 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04001457 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001458
Chris Mason54aa1f42007-06-22 14:16:25 -04001459 if (err)
1460 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001461
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001462 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001463 btrfs_end_transaction(trans, root);
1464 mutex_unlock(&root->fs_info->fs_mutex);
1465
1466 if (drop_inode) {
1467 inode_dec_link_count(inode);
1468 iput(inode);
1469 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001470 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001471 return err;
1472}
1473
Chris Mason39279cc2007-06-12 06:35:45 -04001474static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1475{
1476 struct inode *inode;
1477 struct btrfs_trans_handle *trans;
1478 struct btrfs_root *root = BTRFS_I(dir)->root;
1479 int err = 0;
1480 int drop_on_err = 0;
1481 u64 objectid;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001482 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001483
1484 mutex_lock(&root->fs_info->fs_mutex);
1485 trans = btrfs_start_transaction(root, 1);
1486 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04001487
Chris Mason39279cc2007-06-12 06:35:45 -04001488 if (IS_ERR(trans)) {
1489 err = PTR_ERR(trans);
1490 goto out_unlock;
1491 }
1492
1493 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1494 if (err) {
1495 err = -ENOSPC;
1496 goto out_unlock;
1497 }
1498
1499 inode = btrfs_new_inode(trans, root, objectid,
1500 BTRFS_I(dir)->block_group, S_IFDIR | mode);
1501 if (IS_ERR(inode)) {
1502 err = PTR_ERR(inode);
1503 goto out_fail;
1504 }
Chris Mason5f39d392007-10-15 16:14:19 -04001505
Chris Mason39279cc2007-06-12 06:35:45 -04001506 drop_on_err = 1;
1507 inode->i_op = &btrfs_dir_inode_operations;
1508 inode->i_fop = &btrfs_dir_file_operations;
1509 btrfs_set_trans_block_group(trans, inode);
1510
Chris Mason39544012007-12-12 14:38:19 -05001511 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001512 err = btrfs_update_inode(trans, root, inode);
1513 if (err)
1514 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04001515
Chris Mason39279cc2007-06-12 06:35:45 -04001516 err = btrfs_add_link(trans, dentry, inode);
1517 if (err)
1518 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04001519
Chris Mason39279cc2007-06-12 06:35:45 -04001520 d_instantiate(dentry, inode);
1521 drop_on_err = 0;
1522 dir->i_sb->s_dirt = 1;
1523 btrfs_update_inode_block_group(trans, inode);
1524 btrfs_update_inode_block_group(trans, dir);
1525
1526out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001527 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001528 btrfs_end_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04001529
Chris Mason39279cc2007-06-12 06:35:45 -04001530out_unlock:
1531 mutex_unlock(&root->fs_info->fs_mutex);
1532 if (drop_on_err)
1533 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001534 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001535 return err;
1536}
1537
Chris Masona52d9a82007-08-27 16:49:44 -04001538struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
1539 size_t page_offset, u64 start, u64 end,
1540 int create)
1541{
1542 int ret;
1543 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04001544 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04001545 u64 extent_start = 0;
1546 u64 extent_end = 0;
1547 u64 objectid = inode->i_ino;
1548 u32 found_type;
1549 int failed_insert = 0;
1550 struct btrfs_path *path;
1551 struct btrfs_root *root = BTRFS_I(inode)->root;
1552 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04001553 struct extent_buffer *leaf;
1554 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04001555 struct extent_map *em = NULL;
1556 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1557 struct btrfs_trans_handle *trans = NULL;
1558
1559 path = btrfs_alloc_path();
1560 BUG_ON(!path);
1561 mutex_lock(&root->fs_info->fs_mutex);
1562
1563again:
1564 em = lookup_extent_mapping(em_tree, start, end);
1565 if (em) {
1566 goto out;
1567 }
1568 if (!em) {
1569 em = alloc_extent_map(GFP_NOFS);
1570 if (!em) {
1571 err = -ENOMEM;
1572 goto out;
1573 }
Chris Mason5f39d392007-10-15 16:14:19 -04001574 em->start = EXTENT_MAP_HOLE;
1575 em->end = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001576 }
1577 em->bdev = inode->i_sb->s_bdev;
Chris Mason179e29e2007-11-01 11:28:41 -04001578 ret = btrfs_lookup_file_extent(trans, root, path,
1579 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04001580 if (ret < 0) {
1581 err = ret;
1582 goto out;
1583 }
1584
1585 if (ret != 0) {
1586 if (path->slots[0] == 0)
1587 goto not_found;
1588 path->slots[0]--;
1589 }
1590
Chris Mason5f39d392007-10-15 16:14:19 -04001591 leaf = path->nodes[0];
1592 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04001593 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04001594 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04001595 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1596 found_type = btrfs_key_type(&found_key);
1597 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04001598 found_type != BTRFS_EXTENT_DATA_KEY) {
1599 goto not_found;
1600 }
1601
Chris Mason5f39d392007-10-15 16:14:19 -04001602 found_type = btrfs_file_extent_type(leaf, item);
1603 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04001604 if (found_type == BTRFS_FILE_EXTENT_REG) {
1605 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04001606 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04001607 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04001608 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04001609 em->start = start;
1610 if (start < extent_start) {
Chris Masonb888db22007-08-27 16:49:44 -04001611 if (end < extent_start)
1612 goto not_found;
Chris Masona52d9a82007-08-27 16:49:44 -04001613 em->end = extent_end - 1;
1614 } else {
1615 em->end = end;
1616 }
1617 goto not_found_em;
1618 }
Chris Masondb945352007-10-15 16:15:53 -04001619 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
1620 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04001621 em->start = extent_start;
1622 em->end = extent_end - 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001623 em->block_start = EXTENT_MAP_HOLE;
1624 em->block_end = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001625 goto insert;
1626 }
Chris Masondb945352007-10-15 16:15:53 -04001627 bytenr += btrfs_file_extent_offset(leaf, item);
1628 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04001629 em->block_end = em->block_start +
Chris Masondb945352007-10-15 16:15:53 -04001630 btrfs_file_extent_num_bytes(leaf, item) - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04001631 em->start = extent_start;
1632 em->end = extent_end - 1;
1633 goto insert;
1634 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04001635 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04001636 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04001637 size_t size;
1638 size_t extent_offset;
1639 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04001640
Chris Mason5f39d392007-10-15 16:14:19 -04001641 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
1642 path->slots[0]));
Yan689f9342007-10-29 11:41:07 -04001643 extent_end = (extent_start + size - 1) |
Chris Masondb945352007-10-15 16:15:53 -04001644 ((u64)root->sectorsize - 1);
Chris Masonb888db22007-08-27 16:49:44 -04001645 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04001646 em->start = start;
1647 if (start < extent_start) {
Chris Masonb888db22007-08-27 16:49:44 -04001648 if (end < extent_start)
1649 goto not_found;
Chris Mason50b78c22007-09-20 14:14:42 -04001650 em->end = extent_end;
Chris Masona52d9a82007-08-27 16:49:44 -04001651 } else {
1652 em->end = end;
1653 }
1654 goto not_found_em;
1655 }
1656 em->block_start = EXTENT_MAP_INLINE;
1657 em->block_end = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04001658
1659 if (!page) {
1660 em->start = extent_start;
1661 em->end = extent_start + size - 1;
1662 goto out;
1663 }
1664
Chris Mason35ebb932007-10-30 16:56:53 -04001665 extent_offset = ((u64)page->index << PAGE_CACHE_SHIFT) -
Yan689f9342007-10-29 11:41:07 -04001666 extent_start + page_offset;
1667 copy_size = min_t(u64, PAGE_CACHE_SIZE - page_offset,
1668 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04001669 em->start = extent_start + extent_offset;
1670 em->end = (em->start + copy_size -1) |
1671 ((u64)root->sectorsize -1);
Yan689f9342007-10-29 11:41:07 -04001672 map = kmap(page);
1673 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04001674 if (create == 0 && !PageUptodate(page)) {
1675 read_extent_buffer(leaf, map + page_offset, ptr,
1676 copy_size);
1677 flush_dcache_page(page);
1678 } else if (create && PageUptodate(page)) {
1679 if (!trans) {
1680 kunmap(page);
1681 free_extent_map(em);
1682 em = NULL;
1683 btrfs_release_path(root, path);
1684 trans = btrfs_start_transaction(root, 1);
1685 goto again;
1686 }
1687 write_extent_buffer(leaf, map + page_offset, ptr,
1688 copy_size);
1689 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04001690 }
Chris Masona52d9a82007-08-27 16:49:44 -04001691 kunmap(page);
Chris Mason3326d1b2007-10-15 16:18:25 -04001692 set_extent_uptodate(em_tree, em->start, em->end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04001693 goto insert;
1694 } else {
1695 printk("unkknown found_type %d\n", found_type);
1696 WARN_ON(1);
1697 }
1698not_found:
1699 em->start = start;
1700 em->end = end;
1701not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04001702 em->block_start = EXTENT_MAP_HOLE;
1703 em->block_end = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001704insert:
1705 btrfs_release_path(root, path);
1706 if (em->start > start || em->end < start) {
Chris Masonb888db22007-08-27 16:49:44 -04001707 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->end, start, end);
Chris Masona52d9a82007-08-27 16:49:44 -04001708 err = -EIO;
1709 goto out;
1710 }
1711 ret = add_extent_mapping(em_tree, em);
1712 if (ret == -EEXIST) {
1713 free_extent_map(em);
Chris Mason2bf5a722007-08-30 11:54:02 -04001714 em = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04001715 failed_insert++;
1716 if (failed_insert > 5) {
1717 printk("failing to insert %Lu %Lu\n", start, end);
1718 err = -EIO;
1719 goto out;
1720 }
Chris Masona52d9a82007-08-27 16:49:44 -04001721 goto again;
1722 }
1723 err = 0;
1724out:
1725 btrfs_free_path(path);
1726 if (trans) {
1727 ret = btrfs_end_transaction(trans, root);
1728 if (!err)
1729 err = ret;
1730 }
1731 mutex_unlock(&root->fs_info->fs_mutex);
1732 if (err) {
1733 free_extent_map(em);
1734 WARN_ON(1);
1735 return ERR_PTR(err);
1736 }
1737 return em;
1738}
1739
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04001740static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04001741{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04001742 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04001743}
1744
1745static int btrfs_prepare_write(struct file *file, struct page *page,
1746 unsigned from, unsigned to)
1747{
Chris Masona52d9a82007-08-27 16:49:44 -04001748 return extent_prepare_write(&BTRFS_I(page->mapping->host)->extent_tree,
1749 page->mapping->host, page, from, to,
1750 btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04001751}
1752
Chris Mason9ebefb182007-06-15 13:50:00 -04001753int btrfs_readpage(struct file *file, struct page *page)
1754{
Chris Masona52d9a82007-08-27 16:49:44 -04001755 struct extent_map_tree *tree;
1756 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1757 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04001758}
Chris Mason39279cc2007-06-12 06:35:45 -04001759static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1760{
Chris Masona52d9a82007-08-27 16:49:44 -04001761 struct extent_map_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04001762
1763
1764 if (current->flags & PF_MEMALLOC) {
1765 redirty_page_for_writepage(wbc, page);
1766 unlock_page(page);
1767 return 0;
1768 }
Chris Masona52d9a82007-08-27 16:49:44 -04001769 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1770 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
1771}
Chris Mason39279cc2007-06-12 06:35:45 -04001772
Chris Masonb293f022007-11-01 19:45:34 -04001773static int btrfs_writepages(struct address_space *mapping,
1774 struct writeback_control *wbc)
1775{
1776 struct extent_map_tree *tree;
1777 tree = &BTRFS_I(mapping->host)->extent_tree;
1778 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
1779}
1780
Chris Mason3ab2fb52007-11-08 10:59:22 -05001781static int
1782btrfs_readpages(struct file *file, struct address_space *mapping,
1783 struct list_head *pages, unsigned nr_pages)
1784{
1785 struct extent_map_tree *tree;
1786 tree = &BTRFS_I(mapping->host)->extent_tree;
1787 return extent_readpages(tree, mapping, pages, nr_pages,
1788 btrfs_get_extent);
1789}
1790
Chris Masona52d9a82007-08-27 16:49:44 -04001791static int btrfs_releasepage(struct page *page, gfp_t unused_gfp_flags)
1792{
1793 struct extent_map_tree *tree;
1794 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04001795
Chris Masona52d9a82007-08-27 16:49:44 -04001796 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1797 ret = try_release_extent_mapping(tree, page);
1798 if (ret == 1) {
1799 ClearPagePrivate(page);
1800 set_page_private(page, 0);
1801 page_cache_release(page);
1802 }
1803 return ret;
1804}
Chris Mason39279cc2007-06-12 06:35:45 -04001805
Chris Masona52d9a82007-08-27 16:49:44 -04001806static void btrfs_invalidatepage(struct page *page, unsigned long offset)
1807{
1808 struct extent_map_tree *tree;
1809
1810 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1811 extent_invalidatepage(tree, page, offset);
1812 btrfs_releasepage(page, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001813}
1814
Chris Mason9ebefb182007-06-15 13:50:00 -04001815/*
1816 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
1817 * called from a page fault handler when a page is first dirtied. Hence we must
1818 * be careful to check for EOF conditions here. We set the page up correctly
1819 * for a written page which means we get ENOSPC checking when writing into
1820 * holes and correct delalloc and unwritten extent mapping on filesystems that
1821 * support these features.
1822 *
1823 * We are not allowed to take the i_mutex here so we have to play games to
1824 * protect against truncate races as the page could now be beyond EOF. Because
1825 * vmtruncate() writes the inode size before removing pages, once we have the
1826 * page lock we can determine safely if the page is beyond EOF. If it is not
1827 * beyond EOF, then the page is guaranteed safe against truncation until we
1828 * unlock the page.
1829 */
1830int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
1831{
1832 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1833 unsigned long end;
1834 loff_t size;
1835 int ret = -EINVAL;
Chris Masona52d9a82007-08-27 16:49:44 -04001836 u64 page_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04001837
Chris Mason011410b2007-09-10 19:58:36 -04001838 down_read(&BTRFS_I(inode)->root->snap_sem);
Chris Mason9ebefb182007-06-15 13:50:00 -04001839 lock_page(page);
1840 wait_on_page_writeback(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04001841 size = i_size_read(inode);
Chris Mason35ebb932007-10-30 16:56:53 -04001842 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04001843
Chris Mason9ebefb182007-06-15 13:50:00 -04001844 if ((page->mapping != inode->i_mapping) ||
Chris Masona52d9a82007-08-27 16:49:44 -04001845 (page_start > size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04001846 /* page got truncated out from underneath us */
1847 goto out_unlock;
1848 }
1849
1850 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04001851 if (page_start + PAGE_CACHE_SIZE > size)
Chris Mason9ebefb182007-06-15 13:50:00 -04001852 end = size & ~PAGE_CACHE_MASK;
1853 else
1854 end = PAGE_CACHE_SIZE;
1855
Chris Masonb888db22007-08-27 16:49:44 -04001856 ret = btrfs_cow_one_page(inode, page, end);
Chris Mason9ebefb182007-06-15 13:50:00 -04001857
1858out_unlock:
Chris Mason011410b2007-09-10 19:58:36 -04001859 up_read(&BTRFS_I(inode)->root->snap_sem);
Chris Mason9ebefb182007-06-15 13:50:00 -04001860 unlock_page(page);
1861 return ret;
1862}
1863
Chris Mason39279cc2007-06-12 06:35:45 -04001864static void btrfs_truncate(struct inode *inode)
1865{
1866 struct btrfs_root *root = BTRFS_I(inode)->root;
1867 int ret;
1868 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001869 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001870
1871 if (!S_ISREG(inode->i_mode))
1872 return;
1873 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1874 return;
1875
1876 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1877
1878 mutex_lock(&root->fs_info->fs_mutex);
1879 trans = btrfs_start_transaction(root, 1);
1880 btrfs_set_trans_block_group(trans, inode);
1881
1882 /* FIXME, add redo link to tree so we don't leak on crash */
1883 ret = btrfs_truncate_in_trans(trans, root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001884 btrfs_update_inode(trans, root, inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001885 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04001886
Chris Mason39279cc2007-06-12 06:35:45 -04001887 ret = btrfs_end_transaction(trans, root);
1888 BUG_ON(ret);
1889 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001890 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001891}
1892
1893int btrfs_commit_write(struct file *file, struct page *page,
1894 unsigned from, unsigned to)
1895{
Chris Masona52d9a82007-08-27 16:49:44 -04001896 return extent_commit_write(&BTRFS_I(page->mapping->host)->extent_tree,
1897 page->mapping->host, page, from, to);
Chris Mason39279cc2007-06-12 06:35:45 -04001898}
1899
1900static int create_subvol(struct btrfs_root *root, char *name, int namelen)
1901{
1902 struct btrfs_trans_handle *trans;
1903 struct btrfs_key key;
1904 struct btrfs_root_item root_item;
1905 struct btrfs_inode_item *inode_item;
Chris Mason5f39d392007-10-15 16:14:19 -04001906 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001907 struct btrfs_root *new_root;
1908 struct inode *inode;
1909 struct inode *dir;
1910 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001911 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04001912 u64 objectid;
1913 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001914 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001915
1916 mutex_lock(&root->fs_info->fs_mutex);
1917 trans = btrfs_start_transaction(root, 1);
1918 BUG_ON(!trans);
1919
Chris Mason7bb86312007-12-11 09:25:06 -05001920 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
1921 0, &objectid);
1922 if (ret)
1923 goto fail;
1924
1925 leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
1926 objectid, trans->transid, 0, 0,
1927 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001928 if (IS_ERR(leaf))
1929 return PTR_ERR(leaf);
1930
1931 btrfs_set_header_nritems(leaf, 0);
1932 btrfs_set_header_level(leaf, 0);
Chris Masondb945352007-10-15 16:15:53 -04001933 btrfs_set_header_bytenr(leaf, leaf->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001934 btrfs_set_header_generation(leaf, trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05001935 btrfs_set_header_owner(leaf, objectid);
1936
Chris Mason5f39d392007-10-15 16:14:19 -04001937 write_extent_buffer(leaf, root->fs_info->fsid,
1938 (unsigned long)btrfs_header_fsid(leaf),
1939 BTRFS_FSID_SIZE);
1940 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001941
1942 inode_item = &root_item.inode;
1943 memset(inode_item, 0, sizeof(*inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04001944 inode_item->generation = cpu_to_le64(1);
1945 inode_item->size = cpu_to_le64(3);
1946 inode_item->nlink = cpu_to_le32(1);
1947 inode_item->nblocks = cpu_to_le64(1);
1948 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
Chris Mason39279cc2007-06-12 06:35:45 -04001949
Chris Masondb945352007-10-15 16:15:53 -04001950 btrfs_set_root_bytenr(&root_item, leaf->start);
1951 btrfs_set_root_level(&root_item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001952 btrfs_set_root_refs(&root_item, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001953 btrfs_set_root_used(&root_item, 0);
1954
Chris Mason5eda7b52007-06-22 14:16:25 -04001955 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
1956 root_item.drop_level = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001957
1958 free_extent_buffer(leaf);
1959 leaf = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001960
Chris Mason39279cc2007-06-12 06:35:45 -04001961 btrfs_set_root_dirid(&root_item, new_dirid);
1962
1963 key.objectid = objectid;
1964 key.offset = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001965 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
1966 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
1967 &root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -04001968 if (ret)
1969 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001970
1971 /*
1972 * insert the directory item
1973 */
1974 key.offset = (u64)-1;
1975 dir = root->fs_info->sb->s_root->d_inode;
1976 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
1977 name, namelen, dir->i_ino, &key,
1978 BTRFS_FT_DIR);
Chris Mason54aa1f42007-06-22 14:16:25 -04001979 if (ret)
1980 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001981
Chris Mason39544012007-12-12 14:38:19 -05001982 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
1983 name, namelen, objectid,
1984 root->fs_info->sb->s_root->d_inode->i_ino);
1985 if (ret)
1986 goto fail;
1987
Chris Mason39279cc2007-06-12 06:35:45 -04001988 ret = btrfs_commit_transaction(trans, root);
Chris Mason54aa1f42007-06-22 14:16:25 -04001989 if (ret)
1990 goto fail_commit;
Chris Mason39279cc2007-06-12 06:35:45 -04001991
Josef Bacik58176a92007-08-29 15:47:34 -04001992 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
Chris Mason39279cc2007-06-12 06:35:45 -04001993 BUG_ON(!new_root);
1994
1995 trans = btrfs_start_transaction(new_root, 1);
1996 BUG_ON(!trans);
1997
1998 inode = btrfs_new_inode(trans, new_root, new_dirid,
1999 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
Chris Mason54aa1f42007-06-22 14:16:25 -04002000 if (IS_ERR(inode))
2001 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002002 inode->i_op = &btrfs_dir_inode_operations;
2003 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04002004 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002005
Chris Mason39544012007-12-12 14:38:19 -05002006 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2007 new_dirid);
Chris Mason39279cc2007-06-12 06:35:45 -04002008 inode->i_nlink = 1;
Chris Mason39544012007-12-12 14:38:19 -05002009 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002010 ret = btrfs_update_inode(trans, new_root, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002011 if (ret)
2012 goto fail;
2013fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002014 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04002015 err = btrfs_commit_transaction(trans, root);
2016 if (err && !ret)
2017 ret = err;
2018fail_commit:
Chris Mason39279cc2007-06-12 06:35:45 -04002019 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002020 btrfs_btree_balance_dirty(root, nr);
Chris Mason54aa1f42007-06-22 14:16:25 -04002021 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002022}
2023
2024static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2025{
2026 struct btrfs_trans_handle *trans;
2027 struct btrfs_key key;
2028 struct btrfs_root_item new_root_item;
Chris Mason5f39d392007-10-15 16:14:19 -04002029 struct extent_buffer *tmp;
Chris Mason39279cc2007-06-12 06:35:45 -04002030 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002031 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04002032 u64 objectid;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002033 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002034
2035 if (!root->ref_cows)
2036 return -EINVAL;
2037
Chris Mason011410b2007-09-10 19:58:36 -04002038 down_write(&root->snap_sem);
2039 freeze_bdev(root->fs_info->sb->s_bdev);
2040 thaw_bdev(root->fs_info->sb->s_bdev, root->fs_info->sb);
2041
Chris Mason39279cc2007-06-12 06:35:45 -04002042 mutex_lock(&root->fs_info->fs_mutex);
2043 trans = btrfs_start_transaction(root, 1);
2044 BUG_ON(!trans);
2045
2046 ret = btrfs_update_inode(trans, root, root->inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002047 if (ret)
2048 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002049
2050 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2051 0, &objectid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002052 if (ret)
2053 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002054
2055 memcpy(&new_root_item, &root->root_item,
2056 sizeof(new_root_item));
2057
2058 key.objectid = objectid;
2059 key.offset = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002060 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
Yan96919752007-12-04 13:20:20 -05002061 extent_buffer_get(root->node);
Chris Mason83df7c12007-08-27 16:49:44 -04002062 btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp);
Yan96919752007-12-04 13:20:20 -05002063 free_extent_buffer(tmp);
Chris Masondb945352007-10-15 16:15:53 -04002064 btrfs_set_root_bytenr(&new_root_item, root->node->start);
2065 btrfs_set_root_level(&new_root_item, btrfs_header_level(root->node));
Chris Mason39279cc2007-06-12 06:35:45 -04002066 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2067 &new_root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -04002068 if (ret)
2069 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002070
2071 /*
2072 * insert the directory item
2073 */
2074 key.offset = (u64)-1;
2075 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2076 name, namelen,
2077 root->fs_info->sb->s_root->d_inode->i_ino,
2078 &key, BTRFS_FT_DIR);
2079
Chris Mason54aa1f42007-06-22 14:16:25 -04002080 if (ret)
2081 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002082
Chris Mason39544012007-12-12 14:38:19 -05002083 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2084 name, namelen, objectid,
2085 root->fs_info->sb->s_root->d_inode->i_ino);
2086
2087 if (ret)
2088 goto fail;
2089
Chris Mason7bb86312007-12-11 09:25:06 -05002090 ret = btrfs_inc_root_ref(trans, root, objectid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002091 if (ret)
2092 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002093fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002094 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04002095 err = btrfs_commit_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002096
Chris Mason54aa1f42007-06-22 14:16:25 -04002097 if (err && !ret)
2098 ret = err;
Chris Mason5f39d392007-10-15 16:14:19 -04002099
Chris Mason39279cc2007-06-12 06:35:45 -04002100 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason011410b2007-09-10 19:58:36 -04002101 up_write(&root->snap_sem);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002102 btrfs_btree_balance_dirty(root, nr);
Chris Mason54aa1f42007-06-22 14:16:25 -04002103 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002104}
2105
Chris Mason86479a02007-09-10 19:58:16 -04002106static unsigned long force_ra(struct address_space *mapping,
2107 struct file_ra_state *ra, struct file *file,
2108 pgoff_t offset, pgoff_t last_index)
2109{
2110 pgoff_t req_size;
2111
2112#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2113 req_size = last_index - offset + 1;
2114 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2115 return offset;
2116#else
2117 req_size = min(last_index - offset + 1, (pgoff_t)128);
2118 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2119 return offset + req_size;
2120#endif
2121}
2122
2123int btrfs_defrag_file(struct file *file) {
2124 struct inode *inode = file->f_path.dentry->d_inode;
2125 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2126 struct page *page;
2127 unsigned long last_index;
2128 unsigned long ra_index = 0;
2129 u64 page_start;
2130 u64 page_end;
2131 unsigned long i;
2132
2133 mutex_lock(&inode->i_mutex);
2134 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2135 for (i = 0; i <= last_index; i++) {
2136 if (i == ra_index) {
2137 ra_index = force_ra(inode->i_mapping, &file->f_ra,
2138 file, ra_index, last_index);
2139 }
2140 page = grab_cache_page(inode->i_mapping, i);
2141 if (!page)
2142 goto out_unlock;
2143 if (!PageUptodate(page)) {
2144 btrfs_readpage(NULL, page);
2145 lock_page(page);
2146 if (!PageUptodate(page)) {
2147 unlock_page(page);
2148 page_cache_release(page);
2149 goto out_unlock;
2150 }
2151 }
Chris Mason35ebb932007-10-30 16:56:53 -04002152 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason86479a02007-09-10 19:58:16 -04002153 page_end = page_start + PAGE_CACHE_SIZE - 1;
2154
2155 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
2156 set_extent_delalloc(em_tree, page_start,
2157 page_end, GFP_NOFS);
2158 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
2159 set_page_dirty(page);
2160 unlock_page(page);
2161 page_cache_release(page);
2162 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2163 }
2164
2165out_unlock:
2166 mutex_unlock(&inode->i_mutex);
2167 return 0;
2168}
2169
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002170static int btrfs_ioctl_snap_create(struct btrfs_root *root, void __user *arg)
2171{
2172 struct btrfs_ioctl_vol_args vol_args;
2173 struct btrfs_dir_item *di;
2174 struct btrfs_path *path;
2175 int namelen;
2176 u64 root_dirid;
2177
2178 if (copy_from_user(&vol_args, arg, sizeof(vol_args)))
2179 return -EFAULT;
Chris Mason5f39d392007-10-15 16:14:19 -04002180
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002181 namelen = strlen(vol_args.name);
2182 if (namelen > BTRFS_VOL_NAME_MAX)
2183 return -EINVAL;
2184 if (strchr(vol_args.name, '/'))
2185 return -EINVAL;
2186
2187 path = btrfs_alloc_path();
2188 if (!path)
2189 return -ENOMEM;
2190
2191 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2192 mutex_lock(&root->fs_info->fs_mutex);
2193 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2194 path, root_dirid,
2195 vol_args.name, namelen, 0);
2196 mutex_unlock(&root->fs_info->fs_mutex);
2197 btrfs_free_path(path);
2198 if (di && !IS_ERR(di))
2199 return -EEXIST;
2200 if (IS_ERR(di))
2201 return PTR_ERR(di);
2202
2203 if (root == root->fs_info->tree_root)
2204 return create_subvol(root, vol_args.name, namelen);
2205 return create_snapshot(root, vol_args.name, namelen);
2206}
2207
2208static int btrfs_ioctl_defrag(struct file *file)
Chris Mason39279cc2007-06-12 06:35:45 -04002209{
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002210 struct inode *inode = file->f_path.dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002211 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002212
2213 switch (inode->i_mode & S_IFMT) {
2214 case S_IFDIR:
2215 mutex_lock(&root->fs_info->fs_mutex);
2216 btrfs_defrag_root(root, 0);
2217 btrfs_defrag_root(root->fs_info->extent_root, 0);
2218 mutex_unlock(&root->fs_info->fs_mutex);
2219 break;
2220 case S_IFREG:
2221 btrfs_defrag_file(file);
2222 break;
2223 }
2224
2225 return 0;
2226}
2227
2228long btrfs_ioctl(struct file *file, unsigned int
2229 cmd, unsigned long arg)
2230{
2231 struct btrfs_root *root = BTRFS_I(file->f_path.dentry->d_inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002232
2233 switch (cmd) {
2234 case BTRFS_IOC_SNAP_CREATE:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002235 return btrfs_ioctl_snap_create(root, (void __user *)arg);
Chris Mason6702ed42007-08-07 16:15:09 -04002236 case BTRFS_IOC_DEFRAG:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002237 return btrfs_ioctl_defrag(file);
Chris Mason39279cc2007-06-12 06:35:45 -04002238 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002239
2240 return -ENOTTY;
Chris Mason39279cc2007-06-12 06:35:45 -04002241}
2242
Chris Mason39279cc2007-06-12 06:35:45 -04002243/*
2244 * Called inside transaction, so use GFP_NOFS
2245 */
2246struct inode *btrfs_alloc_inode(struct super_block *sb)
2247{
2248 struct btrfs_inode *ei;
2249
2250 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2251 if (!ei)
2252 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002253 ei->last_trans = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002254 return &ei->vfs_inode;
2255}
2256
2257void btrfs_destroy_inode(struct inode *inode)
2258{
2259 WARN_ON(!list_empty(&inode->i_dentry));
2260 WARN_ON(inode->i_data.nrpages);
2261
2262 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2263}
2264
Chris Mason44ec0b72007-10-29 10:55:05 -04002265#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2266static void init_once(struct kmem_cache * cachep, void *foo)
2267#else
Chris Mason39279cc2007-06-12 06:35:45 -04002268static void init_once(void * foo, struct kmem_cache * cachep,
2269 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04002270#endif
Chris Mason39279cc2007-06-12 06:35:45 -04002271{
2272 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2273
2274 inode_init_once(&ei->vfs_inode);
2275}
2276
2277void btrfs_destroy_cachep(void)
2278{
2279 if (btrfs_inode_cachep)
2280 kmem_cache_destroy(btrfs_inode_cachep);
2281 if (btrfs_trans_handle_cachep)
2282 kmem_cache_destroy(btrfs_trans_handle_cachep);
2283 if (btrfs_transaction_cachep)
2284 kmem_cache_destroy(btrfs_transaction_cachep);
2285 if (btrfs_bit_radix_cachep)
2286 kmem_cache_destroy(btrfs_bit_radix_cachep);
2287 if (btrfs_path_cachep)
2288 kmem_cache_destroy(btrfs_path_cachep);
2289}
2290
Chris Mason86479a02007-09-10 19:58:16 -04002291struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04002292 unsigned long extra_flags,
Chris Mason44ec0b72007-10-29 10:55:05 -04002293#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2294 void (*ctor)(struct kmem_cache *, void *)
2295#else
Chris Mason92fee662007-07-25 12:31:35 -04002296 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04002297 unsigned long)
2298#endif
2299 )
Chris Mason92fee662007-07-25 12:31:35 -04002300{
2301 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2302 SLAB_MEM_SPREAD | extra_flags), ctor
2303#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2304 ,NULL
2305#endif
2306 );
2307}
2308
Chris Mason39279cc2007-06-12 06:35:45 -04002309int btrfs_init_cachep(void)
2310{
Chris Mason86479a02007-09-10 19:58:16 -04002311 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04002312 sizeof(struct btrfs_inode),
2313 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04002314 if (!btrfs_inode_cachep)
2315 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002316 btrfs_trans_handle_cachep =
2317 btrfs_cache_create("btrfs_trans_handle_cache",
2318 sizeof(struct btrfs_trans_handle),
2319 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002320 if (!btrfs_trans_handle_cachep)
2321 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002322 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04002323 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04002324 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002325 if (!btrfs_transaction_cachep)
2326 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002327 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04002328 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04002329 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002330 if (!btrfs_path_cachep)
2331 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002332 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04002333 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002334 if (!btrfs_bit_radix_cachep)
2335 goto fail;
2336 return 0;
2337fail:
2338 btrfs_destroy_cachep();
2339 return -ENOMEM;
2340}
2341
2342static int btrfs_getattr(struct vfsmount *mnt,
2343 struct dentry *dentry, struct kstat *stat)
2344{
2345 struct inode *inode = dentry->d_inode;
2346 generic_fillattr(inode, stat);
2347 stat->blksize = 256 * 1024;
2348 return 0;
2349}
2350
2351static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2352 struct inode * new_dir,struct dentry *new_dentry)
2353{
2354 struct btrfs_trans_handle *trans;
2355 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2356 struct inode *new_inode = new_dentry->d_inode;
2357 struct inode *old_inode = old_dentry->d_inode;
2358 struct timespec ctime = CURRENT_TIME;
2359 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002360 int ret;
2361
2362 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2363 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2364 return -ENOTEMPTY;
2365 }
Chris Mason5f39d392007-10-15 16:14:19 -04002366
Chris Mason39279cc2007-06-12 06:35:45 -04002367 mutex_lock(&root->fs_info->fs_mutex);
2368 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002369
Chris Mason39279cc2007-06-12 06:35:45 -04002370 btrfs_set_trans_block_group(trans, new_dir);
2371 path = btrfs_alloc_path();
2372 if (!path) {
2373 ret = -ENOMEM;
2374 goto out_fail;
2375 }
2376
2377 old_dentry->d_inode->i_nlink++;
2378 old_dir->i_ctime = old_dir->i_mtime = ctime;
2379 new_dir->i_ctime = new_dir->i_mtime = ctime;
2380 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04002381
Chris Mason39279cc2007-06-12 06:35:45 -04002382 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2383 if (ret)
2384 goto out_fail;
2385
2386 if (new_inode) {
2387 new_inode->i_ctime = CURRENT_TIME;
2388 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2389 if (ret)
2390 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002391 }
2392 ret = btrfs_add_link(trans, new_dentry, old_inode);
2393 if (ret)
2394 goto out_fail;
2395
2396out_fail:
2397 btrfs_free_path(path);
2398 btrfs_end_transaction(trans, root);
2399 mutex_unlock(&root->fs_info->fs_mutex);
2400 return ret;
2401}
2402
2403static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2404 const char *symname)
2405{
2406 struct btrfs_trans_handle *trans;
2407 struct btrfs_root *root = BTRFS_I(dir)->root;
2408 struct btrfs_path *path;
2409 struct btrfs_key key;
2410 struct inode *inode;
2411 int err;
2412 int drop_inode = 0;
2413 u64 objectid;
2414 int name_len;
2415 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04002416 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04002417 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04002418 struct extent_buffer *leaf;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002419 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002420
2421 name_len = strlen(symname) + 1;
2422 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2423 return -ENAMETOOLONG;
2424 mutex_lock(&root->fs_info->fs_mutex);
2425 trans = btrfs_start_transaction(root, 1);
2426 btrfs_set_trans_block_group(trans, dir);
2427
2428 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2429 if (err) {
2430 err = -ENOSPC;
2431 goto out_unlock;
2432 }
2433
2434 inode = btrfs_new_inode(trans, root, objectid,
2435 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2436 err = PTR_ERR(inode);
2437 if (IS_ERR(inode))
2438 goto out_unlock;
2439
2440 btrfs_set_trans_block_group(trans, inode);
2441 err = btrfs_add_nondir(trans, dentry, inode);
2442 if (err)
2443 drop_inode = 1;
2444 else {
2445 inode->i_mapping->a_ops = &btrfs_aops;
2446 inode->i_fop = &btrfs_file_operations;
2447 inode->i_op = &btrfs_file_inode_operations;
Chris Masona52d9a82007-08-27 16:49:44 -04002448 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
2449 inode->i_mapping, GFP_NOFS);
Chris Mason07157aa2007-08-30 08:50:51 -04002450 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002451 }
2452 dir->i_sb->s_dirt = 1;
2453 btrfs_update_inode_block_group(trans, inode);
2454 btrfs_update_inode_block_group(trans, dir);
2455 if (drop_inode)
2456 goto out_unlock;
2457
2458 path = btrfs_alloc_path();
2459 BUG_ON(!path);
2460 key.objectid = inode->i_ino;
2461 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002462 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2463 datasize = btrfs_file_extent_calc_inline_size(name_len);
2464 err = btrfs_insert_empty_item(trans, root, path, &key,
2465 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04002466 if (err) {
2467 drop_inode = 1;
2468 goto out_unlock;
2469 }
Chris Mason5f39d392007-10-15 16:14:19 -04002470 leaf = path->nodes[0];
2471 ei = btrfs_item_ptr(leaf, path->slots[0],
2472 struct btrfs_file_extent_item);
2473 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
2474 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04002475 BTRFS_FILE_EXTENT_INLINE);
2476 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04002477 write_extent_buffer(leaf, symname, ptr, name_len);
2478 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002479 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04002480
Chris Mason39279cc2007-06-12 06:35:45 -04002481 inode->i_op = &btrfs_symlink_inode_operations;
2482 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2483 inode->i_size = name_len - 1;
Chris Mason54aa1f42007-06-22 14:16:25 -04002484 err = btrfs_update_inode(trans, root, inode);
2485 if (err)
2486 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002487
2488out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002489 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04002490 btrfs_end_transaction(trans, root);
2491 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04002492 if (drop_inode) {
2493 inode_dec_link_count(inode);
2494 iput(inode);
2495 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002496 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002497 return err;
2498}
2499
2500static struct inode_operations btrfs_dir_inode_operations = {
2501 .lookup = btrfs_lookup,
2502 .create = btrfs_create,
2503 .unlink = btrfs_unlink,
2504 .link = btrfs_link,
2505 .mkdir = btrfs_mkdir,
2506 .rmdir = btrfs_rmdir,
2507 .rename = btrfs_rename,
2508 .symlink = btrfs_symlink,
2509 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04002510 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -05002511 .setxattr = generic_setxattr,
2512 .getxattr = generic_getxattr,
2513 .listxattr = btrfs_listxattr,
2514 .removexattr = generic_removexattr,
Chris Mason39279cc2007-06-12 06:35:45 -04002515};
2516
2517static struct inode_operations btrfs_dir_ro_inode_operations = {
2518 .lookup = btrfs_lookup,
2519};
2520
2521static struct file_operations btrfs_dir_file_operations = {
2522 .llseek = generic_file_llseek,
2523 .read = generic_read_dir,
2524 .readdir = btrfs_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002525 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002526#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002527 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002528#endif
2529};
2530
Chris Mason07157aa2007-08-30 08:50:51 -04002531static struct extent_map_ops btrfs_extent_map_ops = {
2532 .fill_delalloc = run_delalloc_range,
2533 .writepage_io_hook = btrfs_writepage_io_hook,
2534 .readpage_io_hook = btrfs_readpage_io_hook,
2535 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
2536};
2537
Chris Mason39279cc2007-06-12 06:35:45 -04002538static struct address_space_operations btrfs_aops = {
2539 .readpage = btrfs_readpage,
2540 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04002541 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05002542 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04002543 .sync_page = block_sync_page,
2544 .prepare_write = btrfs_prepare_write,
2545 .commit_write = btrfs_commit_write,
2546 .bmap = btrfs_bmap,
Chris Masona52d9a82007-08-27 16:49:44 -04002547 .invalidatepage = btrfs_invalidatepage,
2548 .releasepage = btrfs_releasepage,
2549 .set_page_dirty = __set_page_dirty_nobuffers,
Chris Mason39279cc2007-06-12 06:35:45 -04002550};
2551
2552static struct address_space_operations btrfs_symlink_aops = {
2553 .readpage = btrfs_readpage,
2554 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04002555 .invalidatepage = btrfs_invalidatepage,
2556 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04002557};
2558
2559static struct inode_operations btrfs_file_inode_operations = {
2560 .truncate = btrfs_truncate,
2561 .getattr = btrfs_getattr,
2562 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -05002563 .setxattr = generic_setxattr,
2564 .getxattr = generic_getxattr,
2565 .listxattr = btrfs_listxattr,
2566 .removexattr = generic_removexattr,
Chris Mason39279cc2007-06-12 06:35:45 -04002567};
2568
Josef Bacik618e21d2007-07-11 10:18:17 -04002569static struct inode_operations btrfs_special_inode_operations = {
2570 .getattr = btrfs_getattr,
2571 .setattr = btrfs_setattr,
2572};
2573
Chris Mason39279cc2007-06-12 06:35:45 -04002574static struct inode_operations btrfs_symlink_inode_operations = {
2575 .readlink = generic_readlink,
2576 .follow_link = page_follow_link_light,
2577 .put_link = page_put_link,
2578};