blob: 37f8e0321af88e2760d2d6d21c33f01728dab64c [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 Masondb945352007-10-15 16:15:53 -040096 ret = btrfs_alloc_extent(trans, root, inode->i_ino, num_bytes, 0,
Chris Masonb888db22007-08-27 16:49:44 -040097 alloc_hint, (u64)-1, &ins, 1);
98 if (ret) {
99 WARN_ON(1);
100 goto out;
101 }
102 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
103 start, ins.objectid, ins.offset,
104 ins.offset);
105out:
106 btrfs_end_transaction(trans, root);
107 mutex_unlock(&root->fs_info->fs_mutex);
108 return ret;
109}
110
Chris Mason07157aa2007-08-30 08:50:51 -0400111int btrfs_writepage_io_hook(struct page *page, u64 start, u64 end)
112{
113 struct inode *inode = page->mapping->host;
114 struct btrfs_root *root = BTRFS_I(inode)->root;
115 struct btrfs_trans_handle *trans;
116 char *kaddr;
117 int ret;
Chris Mason35ebb932007-10-30 16:56:53 -0400118 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason07157aa2007-08-30 08:50:51 -0400119 size_t offset = start - page_start;
120
121 mutex_lock(&root->fs_info->fs_mutex);
122 trans = btrfs_start_transaction(root, 1);
123 btrfs_set_trans_block_group(trans, inode);
124 kaddr = kmap(page);
Chris Masonf578d4b2007-10-25 15:42:56 -0400125 btrfs_csum_file_block(trans, root, inode, inode->i_ino,
Chris Mason07157aa2007-08-30 08:50:51 -0400126 start, kaddr + offset, end - start + 1);
127 kunmap(page);
128 ret = btrfs_end_transaction(trans, root);
129 BUG_ON(ret);
130 mutex_unlock(&root->fs_info->fs_mutex);
131 return ret;
132}
133
134int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
135{
136 int ret = 0;
137 struct inode *inode = page->mapping->host;
138 struct btrfs_root *root = BTRFS_I(inode)->root;
139 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
140 struct btrfs_csum_item *item;
141 struct btrfs_path *path = NULL;
Chris Masonff79f812007-10-15 16:22:25 -0400142 u32 csum;
Chris Mason07157aa2007-08-30 08:50:51 -0400143
144 mutex_lock(&root->fs_info->fs_mutex);
145 path = btrfs_alloc_path();
146 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
147 if (IS_ERR(item)) {
148 ret = PTR_ERR(item);
149 /* a csum that isn't present is a preallocated region. */
150 if (ret == -ENOENT || ret == -EFBIG)
151 ret = 0;
Chris Masonff79f812007-10-15 16:22:25 -0400152 csum = 0;
Chris Mason07157aa2007-08-30 08:50:51 -0400153 goto out;
154 }
Chris Masonff79f812007-10-15 16:22:25 -0400155 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
156 BTRFS_CRC32_SIZE);
157 set_state_private(em_tree, start, csum);
Chris Mason07157aa2007-08-30 08:50:51 -0400158out:
159 if (path)
160 btrfs_free_path(path);
161 mutex_unlock(&root->fs_info->fs_mutex);
162 return ret;
163}
164
165int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end)
166{
Chris Mason35ebb932007-10-30 16:56:53 -0400167 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -0400168 struct inode *inode = page->mapping->host;
Chris Mason07157aa2007-08-30 08:50:51 -0400169 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
170 char *kaddr;
171 u64 private;
172 int ret;
Chris Masonff79f812007-10-15 16:22:25 -0400173 struct btrfs_root *root = BTRFS_I(inode)->root;
174 u32 csum = ~(u32)0;
Jens Axboebbf0d002007-10-19 09:23:07 -0400175 unsigned long flags;
Chris Mason07157aa2007-08-30 08:50:51 -0400176
177 ret = get_state_private(em_tree, start, &private);
Jens Axboebbf0d002007-10-19 09:23:07 -0400178 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400179 kaddr = kmap_atomic(page, KM_IRQ0);
180 if (ret) {
181 goto zeroit;
182 }
Chris Masonff79f812007-10-15 16:22:25 -0400183 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
184 btrfs_csum_final(csum, (char *)&csum);
185 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -0400186 goto zeroit;
187 }
188 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400189 local_irq_restore(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400190 return 0;
191
192zeroit:
193 printk("btrfs csum failed ino %lu off %llu\n",
194 page->mapping->host->i_ino, (unsigned long long)start);
Chris Masondb945352007-10-15 16:15:53 -0400195 memset(kaddr + offset, 1, end - start + 1);
196 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -0400197 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400198 local_irq_restore(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400199 return 0;
200}
Chris Masonb888db22007-08-27 16:49:44 -0400201
Chris Mason39279cc2007-06-12 06:35:45 -0400202void btrfs_read_locked_inode(struct inode *inode)
203{
204 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400205 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400206 struct btrfs_inode_item *inode_item;
Chris Mason5f39d392007-10-15 16:14:19 -0400207 struct btrfs_inode_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -0400208 struct btrfs_root *root = BTRFS_I(inode)->root;
209 struct btrfs_key location;
210 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -0400211 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -0400212 int ret;
213
214 path = btrfs_alloc_path();
215 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400216 mutex_lock(&root->fs_info->fs_mutex);
217
218 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
219 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400220 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -0400221 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -0400222
Chris Mason5f39d392007-10-15 16:14:19 -0400223 leaf = path->nodes[0];
224 inode_item = btrfs_item_ptr(leaf, path->slots[0],
225 struct btrfs_inode_item);
226
227 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
228 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
229 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
230 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
231 inode->i_size = btrfs_inode_size(leaf, inode_item);
232
233 tspec = btrfs_inode_atime(inode_item);
234 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
235 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
236
237 tspec = btrfs_inode_mtime(inode_item);
238 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
239 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
240
241 tspec = btrfs_inode_ctime(inode_item);
242 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
243 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
244
245 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
246 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik618e21d2007-07-11 10:18:17 -0400247 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400248 rdev = btrfs_inode_rdev(leaf, inode_item);
249
250 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -0400251 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
252 alloc_group_block);
253
254 btrfs_free_path(path);
255 inode_item = NULL;
256
257 mutex_unlock(&root->fs_info->fs_mutex);
258
259 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -0400260 case S_IFREG:
261 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason07157aa2007-08-30 08:50:51 -0400262 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -0400263 inode->i_fop = &btrfs_file_operations;
264 inode->i_op = &btrfs_file_inode_operations;
265 break;
266 case S_IFDIR:
267 inode->i_fop = &btrfs_dir_file_operations;
268 if (root == root->fs_info->tree_root)
269 inode->i_op = &btrfs_dir_ro_inode_operations;
270 else
271 inode->i_op = &btrfs_dir_inode_operations;
272 break;
273 case S_IFLNK:
274 inode->i_op = &btrfs_symlink_inode_operations;
275 inode->i_mapping->a_ops = &btrfs_symlink_aops;
276 break;
Josef Bacik618e21d2007-07-11 10:18:17 -0400277 default:
278 init_special_inode(inode, inode->i_mode, rdev);
279 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400280 }
281 return;
282
283make_bad:
284 btrfs_release_path(root, path);
285 btrfs_free_path(path);
286 mutex_unlock(&root->fs_info->fs_mutex);
287 make_bad_inode(inode);
288}
289
Chris Mason5f39d392007-10-15 16:14:19 -0400290static void fill_inode_item(struct extent_buffer *leaf,
291 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -0400292 struct inode *inode)
293{
Chris Mason5f39d392007-10-15 16:14:19 -0400294 btrfs_set_inode_uid(leaf, item, inode->i_uid);
295 btrfs_set_inode_gid(leaf, item, inode->i_gid);
296 btrfs_set_inode_size(leaf, item, inode->i_size);
297 btrfs_set_inode_mode(leaf, item, inode->i_mode);
298 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
299
300 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
301 inode->i_atime.tv_sec);
302 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
303 inode->i_atime.tv_nsec);
304
305 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
306 inode->i_mtime.tv_sec);
307 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
308 inode->i_mtime.tv_nsec);
309
310 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
311 inode->i_ctime.tv_sec);
312 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
313 inode->i_ctime.tv_nsec);
314
315 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
316 btrfs_set_inode_generation(leaf, item, inode->i_generation);
317 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
318 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -0400319 BTRFS_I(inode)->block_group->key.objectid);
320}
321
Chris Masona52d9a82007-08-27 16:49:44 -0400322int btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -0400323 struct btrfs_root *root,
324 struct inode *inode)
325{
326 struct btrfs_inode_item *inode_item;
327 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400328 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400329 int ret;
330
331 path = btrfs_alloc_path();
332 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400333 ret = btrfs_lookup_inode(trans, root, path,
334 &BTRFS_I(inode)->location, 1);
335 if (ret) {
336 if (ret > 0)
337 ret = -ENOENT;
338 goto failed;
339 }
340
Chris Mason5f39d392007-10-15 16:14:19 -0400341 leaf = path->nodes[0];
342 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400343 struct btrfs_inode_item);
344
Chris Mason5f39d392007-10-15 16:14:19 -0400345 fill_inode_item(leaf, inode_item, inode);
346 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400347 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400348 ret = 0;
349failed:
350 btrfs_release_path(root, path);
351 btrfs_free_path(path);
352 return ret;
353}
354
355
356static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
357 struct btrfs_root *root,
358 struct inode *dir,
359 struct dentry *dentry)
360{
361 struct btrfs_path *path;
362 const char *name = dentry->d_name.name;
363 int name_len = dentry->d_name.len;
364 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400365 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400366 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -0400367 struct btrfs_key key;
Chris Mason39279cc2007-06-12 06:35:45 -0400368
369 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400370 if (!path) {
371 ret = -ENOMEM;
372 goto err;
373 }
374
Chris Mason39279cc2007-06-12 06:35:45 -0400375 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
376 name, name_len, -1);
377 if (IS_ERR(di)) {
378 ret = PTR_ERR(di);
379 goto err;
380 }
381 if (!di) {
382 ret = -ENOENT;
383 goto err;
384 }
Chris Mason5f39d392007-10-15 16:14:19 -0400385 leaf = path->nodes[0];
386 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -0400387 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -0400388 if (ret)
389 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -0400390 btrfs_release_path(root, path);
391
392 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Chris Mason5f39d392007-10-15 16:14:19 -0400393 key.objectid, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -0400394 if (IS_ERR(di)) {
395 ret = PTR_ERR(di);
396 goto err;
397 }
398 if (!di) {
399 ret = -ENOENT;
400 goto err;
401 }
402 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason39279cc2007-06-12 06:35:45 -0400403
404 dentry->d_inode->i_ctime = dir->i_ctime;
405err:
406 btrfs_free_path(path);
407 if (!ret) {
408 dir->i_size -= name_len * 2;
Chris Mason79c44582007-06-25 10:09:33 -0400409 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -0400410 btrfs_update_inode(trans, root, dir);
411 drop_nlink(dentry->d_inode);
Chris Mason54aa1f42007-06-22 14:16:25 -0400412 ret = btrfs_update_inode(trans, root, dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400413 dir->i_sb->s_dirt = 1;
414 }
415 return ret;
416}
417
418static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
419{
420 struct btrfs_root *root;
421 struct btrfs_trans_handle *trans;
422 int ret;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400423 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -0400424
425 root = BTRFS_I(dir)->root;
426 mutex_lock(&root->fs_info->fs_mutex);
427 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400428
Chris Mason39279cc2007-06-12 06:35:45 -0400429 btrfs_set_trans_block_group(trans, dir);
430 ret = btrfs_unlink_trans(trans, root, dir, dentry);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400431 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -0400432
Chris Mason39279cc2007-06-12 06:35:45 -0400433 btrfs_end_transaction(trans, root);
434 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400435 btrfs_btree_balance_dirty(root, nr);
Chris Mason5f39d392007-10-15 16:14:19 -0400436
Chris Mason39279cc2007-06-12 06:35:45 -0400437 return ret;
438}
439
440static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
441{
442 struct inode *inode = dentry->d_inode;
443 int err;
444 int ret;
445 struct btrfs_root *root = BTRFS_I(dir)->root;
446 struct btrfs_path *path;
447 struct btrfs_key key;
448 struct btrfs_trans_handle *trans;
449 struct btrfs_key found_key;
450 int found_type;
Chris Mason5f39d392007-10-15 16:14:19 -0400451 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400452 char *goodnames = "..";
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400453 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -0400454
Yan134d4512007-10-25 15:49:25 -0400455 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
456 return -ENOTEMPTY;
457
Chris Mason39279cc2007-06-12 06:35:45 -0400458 path = btrfs_alloc_path();
459 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400460 mutex_lock(&root->fs_info->fs_mutex);
461 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400462
Chris Mason39279cc2007-06-12 06:35:45 -0400463 btrfs_set_trans_block_group(trans, dir);
464 key.objectid = inode->i_ino;
465 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -0400466 key.type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -0400467 while(1) {
468 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
469 if (ret < 0) {
470 err = ret;
471 goto out;
472 }
473 BUG_ON(ret == 0);
474 if (path->slots[0] == 0) {
475 err = -ENOENT;
476 goto out;
477 }
478 path->slots[0]--;
Chris Mason5f39d392007-10-15 16:14:19 -0400479 leaf = path->nodes[0];
480 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason39279cc2007-06-12 06:35:45 -0400481 found_type = btrfs_key_type(&found_key);
482 if (found_key.objectid != inode->i_ino) {
483 err = -ENOENT;
484 goto out;
485 }
486 if ((found_type != BTRFS_DIR_ITEM_KEY &&
487 found_type != BTRFS_DIR_INDEX_KEY) ||
488 (!btrfs_match_dir_item_name(root, path, goodnames, 2) &&
489 !btrfs_match_dir_item_name(root, path, goodnames, 1))) {
490 err = -ENOTEMPTY;
491 goto out;
492 }
493 ret = btrfs_del_item(trans, root, path);
494 BUG_ON(ret);
495
496 if (found_type == BTRFS_DIR_ITEM_KEY && found_key.offset == 1)
497 break;
498 btrfs_release_path(root, path);
499 }
500 ret = 0;
501 btrfs_release_path(root, path);
502
503 /* now the directory is empty */
504 err = btrfs_unlink_trans(trans, root, dir, dentry);
505 if (!err) {
506 inode->i_size = 0;
507 }
508out:
509 btrfs_release_path(root, path);
510 btrfs_free_path(path);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400511 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -0400512 ret = btrfs_end_transaction(trans, root);
Yan134d4512007-10-25 15:49:25 -0400513 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400514 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -0400515 if (ret && !err)
516 err = ret;
517 return err;
518}
519
520static int btrfs_free_inode(struct btrfs_trans_handle *trans,
521 struct btrfs_root *root,
522 struct inode *inode)
523{
524 struct btrfs_path *path;
525 int ret;
526
527 clear_inode(inode);
528
529 path = btrfs_alloc_path();
530 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400531 ret = btrfs_lookup_inode(trans, root, path,
532 &BTRFS_I(inode)->location, -1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400533 if (ret > 0)
534 ret = -ENOENT;
535 if (!ret)
536 ret = btrfs_del_item(trans, root, path);
Chris Mason39279cc2007-06-12 06:35:45 -0400537 btrfs_free_path(path);
538 return ret;
539}
540
541/*
Chris Mason39279cc2007-06-12 06:35:45 -0400542 * this can truncate away extent items, csum items and directory items.
543 * It starts at a high offset and removes keys until it can't find
544 * any higher than i_size.
545 *
546 * csum items that cross the new i_size are truncated to the new size
547 * as well.
548 */
549static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
550 struct btrfs_root *root,
551 struct inode *inode)
552{
553 int ret;
554 struct btrfs_path *path;
555 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400556 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -0400557 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -0400558 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400559 struct btrfs_file_extent_item *fi;
560 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -0400561 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400562 u64 item_end = 0;
563 int found_extent;
564 int del_item;
Chris Mason179e29e2007-11-01 11:28:41 -0400565 int extent_type = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400566
Chris Masona52d9a82007-08-27 16:49:44 -0400567 btrfs_drop_extent_cache(inode, inode->i_size, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -0400568 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -0400569 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400570 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -0400571
Chris Mason39279cc2007-06-12 06:35:45 -0400572 /* FIXME, add redo link to tree so we don't leak on crash */
573 key.objectid = inode->i_ino;
574 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -0400575 key.type = (u8)-1;
576
Chris Mason39279cc2007-06-12 06:35:45 -0400577 while(1) {
578 btrfs_init_path(path);
579 fi = NULL;
580 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
581 if (ret < 0) {
582 goto error;
583 }
584 if (ret > 0) {
585 BUG_ON(path->slots[0] == 0);
586 path->slots[0]--;
587 }
Chris Mason5f39d392007-10-15 16:14:19 -0400588 leaf = path->nodes[0];
589 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
590 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -0400591
Chris Mason5f39d392007-10-15 16:14:19 -0400592 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -0400593 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400594
Chris Mason39279cc2007-06-12 06:35:45 -0400595 if (found_type != BTRFS_CSUM_ITEM_KEY &&
596 found_type != BTRFS_DIR_ITEM_KEY &&
597 found_type != BTRFS_DIR_INDEX_KEY &&
598 found_type != BTRFS_EXTENT_DATA_KEY)
599 break;
600
Chris Mason5f39d392007-10-15 16:14:19 -0400601 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -0400602 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -0400603 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400604 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -0400605 extent_type = btrfs_file_extent_type(leaf, fi);
606 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -0400607 item_end +=
Chris Masondb945352007-10-15 16:15:53 -0400608 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -0400609 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
610 struct btrfs_item *item = btrfs_item_nr(leaf,
611 path->slots[0]);
612 item_end += btrfs_file_extent_inline_len(leaf,
613 item);
Chris Mason39279cc2007-06-12 06:35:45 -0400614 }
Yan008630c2007-11-07 13:31:09 -0500615 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -0400616 }
617 if (found_type == BTRFS_CSUM_ITEM_KEY) {
618 ret = btrfs_csum_truncate(trans, root, path,
619 inode->i_size);
620 BUG_ON(ret);
621 }
Yan008630c2007-11-07 13:31:09 -0500622 if (item_end < inode->i_size) {
Chris Masonb888db22007-08-27 16:49:44 -0400623 if (found_type == BTRFS_DIR_ITEM_KEY) {
624 found_type = BTRFS_INODE_ITEM_KEY;
625 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
626 found_type = BTRFS_CSUM_ITEM_KEY;
627 } else if (found_type) {
628 found_type--;
629 } else {
630 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400631 }
Yana61721d2007-09-17 11:08:38 -0400632 btrfs_set_key_type(&key, found_type);
Yan65555a02007-10-25 15:42:57 -0400633 btrfs_release_path(root, path);
Chris Masonb888db22007-08-27 16:49:44 -0400634 continue;
Chris Mason39279cc2007-06-12 06:35:45 -0400635 }
Chris Mason5f39d392007-10-15 16:14:19 -0400636 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -0400637 del_item = 1;
638 else
639 del_item = 0;
640 found_extent = 0;
641
642 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -0400643 if (found_type != BTRFS_EXTENT_DATA_KEY)
644 goto delete;
645
646 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -0400647 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -0400648 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400649 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -0400650 u64 orig_num_bytes =
651 btrfs_file_extent_num_bytes(leaf, fi);
652 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -0400653 found_key.offset + root->sectorsize - 1;
Chris Masondb945352007-10-15 16:15:53 -0400654 btrfs_set_file_extent_num_bytes(leaf, fi,
655 extent_num_bytes);
656 num_dec = (orig_num_bytes -
657 extent_num_bytes) >> 9;
Yanbab9fb02007-09-17 11:13:11 -0400658 if (extent_start != 0) {
659 inode->i_blocks -= num_dec;
660 }
Chris Mason5f39d392007-10-15 16:14:19 -0400661 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -0400662 } else {
Chris Masondb945352007-10-15 16:15:53 -0400663 extent_num_bytes =
664 btrfs_file_extent_disk_num_bytes(leaf,
665 fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400666 /* FIXME blocksize != 4096 */
Chris Masondb945352007-10-15 16:15:53 -0400667 num_dec = btrfs_file_extent_num_bytes(leaf,
668 fi) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -0400669 if (extent_start != 0) {
670 found_extent = 1;
671 inode->i_blocks -= num_dec;
672 }
673 }
Chris Mason179e29e2007-11-01 11:28:41 -0400674 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE &&
675 !del_item) {
676 u32 newsize = inode->i_size - found_key.offset;
677 newsize = btrfs_file_extent_calc_inline_size(newsize);
678 ret = btrfs_truncate_item(trans, root, path,
679 newsize, 1);
680 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -0400681 }
Chris Mason179e29e2007-11-01 11:28:41 -0400682delete:
Chris Mason39279cc2007-06-12 06:35:45 -0400683 if (del_item) {
684 ret = btrfs_del_item(trans, root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400685 if (ret)
686 goto error;
Chris Mason39279cc2007-06-12 06:35:45 -0400687 } else {
688 break;
689 }
690 btrfs_release_path(root, path);
691 if (found_extent) {
692 ret = btrfs_free_extent(trans, root, extent_start,
Chris Masondb945352007-10-15 16:15:53 -0400693 extent_num_bytes, 0);
Chris Mason39279cc2007-06-12 06:35:45 -0400694 BUG_ON(ret);
695 }
696 }
697 ret = 0;
698error:
699 btrfs_release_path(root, path);
700 btrfs_free_path(path);
701 inode->i_sb->s_dirt = 1;
702 return ret;
703}
704
Chris Masonb888db22007-08-27 16:49:44 -0400705static int btrfs_cow_one_page(struct inode *inode, struct page *page,
Chris Masona52d9a82007-08-27 16:49:44 -0400706 size_t zero_start)
Chris Mason39279cc2007-06-12 06:35:45 -0400707{
Chris Mason39279cc2007-06-12 06:35:45 -0400708 char *kaddr;
709 int ret = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400710 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason35ebb932007-10-30 16:56:53 -0400711 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masonb888db22007-08-27 16:49:44 -0400712 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Mason39279cc2007-06-12 06:35:45 -0400713
Christoph Hellwigb3cfa352007-09-17 11:25:58 -0400714 set_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400715
Chris Masonb888db22007-08-27 16:49:44 -0400716 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
717 set_extent_delalloc(&BTRFS_I(inode)->extent_tree, page_start,
718 page_end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -0400719 if (zero_start != PAGE_CACHE_SIZE) {
Chris Masonb888db22007-08-27 16:49:44 -0400720 kaddr = kmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400721 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
722 flush_dcache_page(page);
Chris Masonb888db22007-08-27 16:49:44 -0400723 kunmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400724 }
Chris Masonb888db22007-08-27 16:49:44 -0400725 set_page_dirty(page);
726 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -0400727
Chris Masona52d9a82007-08-27 16:49:44 -0400728 return ret;
729}
730
731/*
732 * taken from block_truncate_page, but does cow as it zeros out
733 * any bytes left in the last page in the file.
734 */
735static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
736{
737 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -0400738 struct btrfs_root *root = BTRFS_I(inode)->root;
739 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -0400740 pgoff_t index = from >> PAGE_CACHE_SHIFT;
741 unsigned offset = from & (PAGE_CACHE_SIZE-1);
742 struct page *page;
743 int ret = 0;
744 u64 page_start;
745
746 if ((offset & (blocksize - 1)) == 0)
747 goto out;
748
Chris Masondb945352007-10-15 16:15:53 -0400749 down_read(&root->snap_sem);
Chris Masona52d9a82007-08-27 16:49:44 -0400750 ret = -ENOMEM;
751 page = grab_cache_page(mapping, index);
752 if (!page)
753 goto out;
754 if (!PageUptodate(page)) {
755 ret = btrfs_readpage(NULL, page);
756 lock_page(page);
757 if (!PageUptodate(page)) {
758 ret = -EIO;
759 goto out;
760 }
761 }
Chris Mason35ebb932007-10-30 16:56:53 -0400762 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -0400763
Chris Masonb888db22007-08-27 16:49:44 -0400764 ret = btrfs_cow_one_page(inode, page, offset);
Chris Mason39279cc2007-06-12 06:35:45 -0400765
Chris Mason39279cc2007-06-12 06:35:45 -0400766 unlock_page(page);
767 page_cache_release(page);
Chris Mason011410b2007-09-10 19:58:36 -0400768 up_read(&BTRFS_I(inode)->root->snap_sem);
Chris Mason39279cc2007-06-12 06:35:45 -0400769out:
770 return ret;
771}
772
773static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
774{
775 struct inode *inode = dentry->d_inode;
776 int err;
777
778 err = inode_change_ok(inode, attr);
779 if (err)
780 return err;
781
782 if (S_ISREG(inode->i_mode) &&
783 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
784 struct btrfs_trans_handle *trans;
785 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason2bf5a722007-08-30 11:54:02 -0400786 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
787
Chris Mason5f39d392007-10-15 16:14:19 -0400788 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -0400789 u64 pos = (inode->i_size + mask) & ~mask;
Chris Mason2bf5a722007-08-30 11:54:02 -0400790 u64 block_end = attr->ia_size | mask;
Chris Mason39279cc2007-06-12 06:35:45 -0400791 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -0400792 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400793
794 if (attr->ia_size <= pos)
795 goto out;
796
797 btrfs_truncate_page(inode->i_mapping, inode->i_size);
798
Chris Mason2bf5a722007-08-30 11:54:02 -0400799 lock_extent(em_tree, pos, block_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -0400800 hole_size = (attr->ia_size - pos + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -0400801
802 mutex_lock(&root->fs_info->fs_mutex);
803 trans = btrfs_start_transaction(root, 1);
804 btrfs_set_trans_block_group(trans, inode);
Chris Mason2bf5a722007-08-30 11:54:02 -0400805 err = btrfs_drop_extents(trans, root, inode,
Chris Mason3326d1b2007-10-15 16:18:25 -0400806 pos, pos + hole_size, pos,
807 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -0400808
Chris Mason179e29e2007-11-01 11:28:41 -0400809 if (alloc_hint != EXTENT_MAP_INLINE) {
810 err = btrfs_insert_file_extent(trans, root,
811 inode->i_ino,
812 pos, 0, 0, hole_size);
813 }
Chris Mason39279cc2007-06-12 06:35:45 -0400814 btrfs_end_transaction(trans, root);
815 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason2bf5a722007-08-30 11:54:02 -0400816 unlock_extent(em_tree, pos, block_end, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -0400817 if (err)
818 return err;
Chris Mason39279cc2007-06-12 06:35:45 -0400819 }
820out:
821 err = inode_setattr(inode, attr);
822
823 return err;
824}
825void btrfs_delete_inode(struct inode *inode)
826{
827 struct btrfs_trans_handle *trans;
828 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400829 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -0400830 int ret;
831
832 truncate_inode_pages(&inode->i_data, 0);
833 if (is_bad_inode(inode)) {
834 goto no_delete;
835 }
Chris Mason5f39d392007-10-15 16:14:19 -0400836
Chris Mason39279cc2007-06-12 06:35:45 -0400837 inode->i_size = 0;
838 mutex_lock(&root->fs_info->fs_mutex);
839 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400840
Chris Mason39279cc2007-06-12 06:35:45 -0400841 btrfs_set_trans_block_group(trans, inode);
842 ret = btrfs_truncate_in_trans(trans, root, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -0400843 if (ret)
844 goto no_delete_lock;
Josef Bacik5103e942007-11-16 11:45:54 -0500845 ret = btrfs_delete_xattrs(trans, root, inode);
846 if (ret)
847 goto no_delete_lock;
Chris Mason54aa1f42007-06-22 14:16:25 -0400848 ret = btrfs_free_inode(trans, root, inode);
849 if (ret)
850 goto no_delete_lock;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400851 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -0400852
Chris Mason39279cc2007-06-12 06:35:45 -0400853 btrfs_end_transaction(trans, root);
854 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400855 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -0400856 return;
Chris Mason54aa1f42007-06-22 14:16:25 -0400857
858no_delete_lock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400859 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -0400860 btrfs_end_transaction(trans, root);
861 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400862 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -0400863no_delete:
864 clear_inode(inode);
865}
866
867/*
868 * this returns the key found in the dir entry in the location pointer.
869 * If no dir entries were found, location->objectid is 0.
870 */
871static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
872 struct btrfs_key *location)
873{
874 const char *name = dentry->d_name.name;
875 int namelen = dentry->d_name.len;
876 struct btrfs_dir_item *di;
877 struct btrfs_path *path;
878 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -0400879 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400880
881 path = btrfs_alloc_path();
882 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400883 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
884 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -0400885 if (IS_ERR(di))
886 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -0400887 if (!di || IS_ERR(di)) {
888 location->objectid = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400889 goto out;
890 }
Chris Mason5f39d392007-10-15 16:14:19 -0400891 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -0400892out:
893 btrfs_release_path(root, path);
894 btrfs_free_path(path);
895 return ret;
896}
897
898/*
899 * when we hit a tree root in a directory, the btrfs part of the inode
900 * needs to be changed to reflect the root directory of the tree root. This
901 * is kind of like crossing a mount point.
902 */
903static int fixup_tree_root_location(struct btrfs_root *root,
904 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -0400905 struct btrfs_root **sub_root,
906 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -0400907{
908 struct btrfs_path *path;
909 struct btrfs_root_item *ri;
910
911 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
912 return 0;
913 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
914 return 0;
915
916 path = btrfs_alloc_path();
917 BUG_ON(!path);
918 mutex_lock(&root->fs_info->fs_mutex);
919
Josef Bacik58176a92007-08-29 15:47:34 -0400920 *sub_root = btrfs_read_fs_root(root->fs_info, location,
921 dentry->d_name.name,
922 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -0400923 if (IS_ERR(*sub_root))
924 return PTR_ERR(*sub_root);
925
926 ri = &(*sub_root)->root_item;
927 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -0400928 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
929 location->offset = 0;
930
931 btrfs_free_path(path);
932 mutex_unlock(&root->fs_info->fs_mutex);
933 return 0;
934}
935
936static int btrfs_init_locked_inode(struct inode *inode, void *p)
937{
938 struct btrfs_iget_args *args = p;
939 inode->i_ino = args->ino;
940 BTRFS_I(inode)->root = args->root;
Chris Masonb888db22007-08-27 16:49:44 -0400941 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
942 inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -0400943 return 0;
944}
945
946static int btrfs_find_actor(struct inode *inode, void *opaque)
947{
948 struct btrfs_iget_args *args = opaque;
949 return (args->ino == inode->i_ino &&
950 args->root == BTRFS_I(inode)->root);
951}
952
953struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
954 struct btrfs_root *root)
955{
956 struct inode *inode;
957 struct btrfs_iget_args args;
958 args.ino = objectid;
959 args.root = root;
960
961 inode = iget5_locked(s, objectid, btrfs_find_actor,
962 btrfs_init_locked_inode,
963 (void *)&args);
964 return inode;
965}
966
967static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
968 struct nameidata *nd)
969{
970 struct inode * inode;
971 struct btrfs_inode *bi = BTRFS_I(dir);
972 struct btrfs_root *root = bi->root;
973 struct btrfs_root *sub_root = root;
974 struct btrfs_key location;
975 int ret;
976
977 if (dentry->d_name.len > BTRFS_NAME_LEN)
978 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -0400979
Chris Mason39279cc2007-06-12 06:35:45 -0400980 mutex_lock(&root->fs_info->fs_mutex);
981 ret = btrfs_inode_by_name(dir, dentry, &location);
982 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason5f39d392007-10-15 16:14:19 -0400983
Chris Mason39279cc2007-06-12 06:35:45 -0400984 if (ret < 0)
985 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -0400986
Chris Mason39279cc2007-06-12 06:35:45 -0400987 inode = NULL;
988 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -0400989 ret = fixup_tree_root_location(root, &location, &sub_root,
990 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -0400991 if (ret < 0)
992 return ERR_PTR(ret);
993 if (ret > 0)
994 return ERR_PTR(-ENOENT);
995 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
996 sub_root);
997 if (!inode)
998 return ERR_PTR(-EACCES);
999 if (inode->i_state & I_NEW) {
1000 /* the inode and parent dir are two different roots */
1001 if (sub_root != root) {
1002 igrab(inode);
1003 sub_root->inode = inode;
1004 }
1005 BTRFS_I(inode)->root = sub_root;
1006 memcpy(&BTRFS_I(inode)->location, &location,
1007 sizeof(location));
1008 btrfs_read_locked_inode(inode);
1009 unlock_new_inode(inode);
1010 }
1011 }
1012 return d_splice_alias(inode, dentry);
1013}
1014
Chris Mason39279cc2007-06-12 06:35:45 -04001015static unsigned char btrfs_filetype_table[] = {
1016 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1017};
1018
1019static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1020{
1021 struct inode *inode = filp->f_path.dentry->d_inode;
1022 struct btrfs_root *root = BTRFS_I(inode)->root;
1023 struct btrfs_item *item;
1024 struct btrfs_dir_item *di;
1025 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001026 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001027 struct btrfs_path *path;
1028 int ret;
1029 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001030 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001031 int slot;
1032 int advance;
1033 unsigned char d_type;
1034 int over = 0;
1035 u32 di_cur;
1036 u32 di_total;
1037 u32 di_len;
1038 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001039 char tmp_name[32];
1040 char *name_ptr;
1041 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001042
1043 /* FIXME, use a real flag for deciding about the key type */
1044 if (root->fs_info->tree_root == root)
1045 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001046
Chris Mason39279cc2007-06-12 06:35:45 -04001047 mutex_lock(&root->fs_info->fs_mutex);
1048 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04001049 btrfs_set_key_type(&key, key_type);
1050 key.offset = filp->f_pos;
Chris Mason5f39d392007-10-15 16:14:19 -04001051
Chris Mason39279cc2007-06-12 06:35:45 -04001052 path = btrfs_alloc_path();
Chris Mason2cc58cf2007-08-27 16:49:44 -04001053 path->reada = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04001054 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1055 if (ret < 0)
1056 goto err;
1057 advance = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001058 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001059 leaf = path->nodes[0];
1060 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001061 slot = path->slots[0];
1062 if (advance || slot >= nritems) {
1063 if (slot >= nritems -1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001064 ret = btrfs_next_leaf(root, path);
1065 if (ret)
1066 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001067 leaf = path->nodes[0];
1068 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001069 slot = path->slots[0];
1070 } else {
1071 slot++;
1072 path->slots[0]++;
1073 }
1074 }
1075 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001076 item = btrfs_item_nr(leaf, slot);
1077 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1078
1079 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04001080 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001081 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001082 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001083 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04001084 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001085
1086 filp->f_pos = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001087 advance = 1;
1088 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1089 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001090 di_total = btrfs_item_size(leaf, item);
Chris Mason39279cc2007-06-12 06:35:45 -04001091 while(di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04001092 struct btrfs_key location;
1093
1094 name_len = btrfs_dir_name_len(leaf, di);
1095 if (name_len < 32) {
1096 name_ptr = tmp_name;
1097 } else {
1098 name_ptr = kmalloc(name_len, GFP_NOFS);
1099 BUG_ON(!name_ptr);
1100 }
1101 read_extent_buffer(leaf, name_ptr,
1102 (unsigned long)(di + 1), name_len);
1103
1104 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1105 btrfs_dir_item_key_to_cpu(leaf, di, &location);
1106
1107 over = filldir(dirent, name_ptr, name_len,
1108 found_key.offset,
1109 location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001110 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04001111
1112 if (name_ptr != tmp_name)
1113 kfree(name_ptr);
1114
Chris Mason39279cc2007-06-12 06:35:45 -04001115 if (over)
1116 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05001117 di_len = btrfs_dir_name_len(leaf, di) +
1118 btrfs_dir_data_len(leaf, di) +sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04001119 di_cur += di_len;
1120 di = (struct btrfs_dir_item *)((char *)di + di_len);
1121 }
1122 }
1123 filp->f_pos++;
1124nopos:
1125 ret = 0;
1126err:
1127 btrfs_release_path(root, path);
1128 btrfs_free_path(path);
1129 mutex_unlock(&root->fs_info->fs_mutex);
1130 return ret;
1131}
1132
1133int btrfs_write_inode(struct inode *inode, int wait)
1134{
1135 struct btrfs_root *root = BTRFS_I(inode)->root;
1136 struct btrfs_trans_handle *trans;
1137 int ret = 0;
1138
1139 if (wait) {
1140 mutex_lock(&root->fs_info->fs_mutex);
1141 trans = btrfs_start_transaction(root, 1);
1142 btrfs_set_trans_block_group(trans, inode);
1143 ret = btrfs_commit_transaction(trans, root);
1144 mutex_unlock(&root->fs_info->fs_mutex);
1145 }
1146 return ret;
1147}
1148
1149/*
Chris Mason54aa1f42007-06-22 14:16:25 -04001150 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04001151 * inode changes. But, it is most likely to find the inode in cache.
1152 * FIXME, needs more benchmarking...there are no reasons other than performance
1153 * to keep or drop this code.
1154 */
1155void btrfs_dirty_inode(struct inode *inode)
1156{
1157 struct btrfs_root *root = BTRFS_I(inode)->root;
1158 struct btrfs_trans_handle *trans;
1159
1160 mutex_lock(&root->fs_info->fs_mutex);
1161 trans = btrfs_start_transaction(root, 1);
1162 btrfs_set_trans_block_group(trans, inode);
1163 btrfs_update_inode(trans, root, inode);
1164 btrfs_end_transaction(trans, root);
1165 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001166}
1167
1168static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1169 struct btrfs_root *root,
1170 u64 objectid,
1171 struct btrfs_block_group_cache *group,
1172 int mode)
1173{
1174 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001175 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04001176 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04001177 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04001178 int ret;
1179 int owner;
1180
Chris Mason5f39d392007-10-15 16:14:19 -04001181 path = btrfs_alloc_path();
1182 BUG_ON(!path);
1183
Chris Mason39279cc2007-06-12 06:35:45 -04001184 inode = new_inode(root->fs_info->sb);
1185 if (!inode)
1186 return ERR_PTR(-ENOMEM);
1187
Chris Masonb888db22007-08-27 16:49:44 -04001188 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1189 inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001190 BTRFS_I(inode)->root = root;
Chris Masonb888db22007-08-27 16:49:44 -04001191
Chris Mason39279cc2007-06-12 06:35:45 -04001192 if (mode & S_IFDIR)
1193 owner = 0;
1194 else
1195 owner = 1;
1196 group = btrfs_find_block_group(root, group, 0, 0, owner);
1197 BTRFS_I(inode)->block_group = group;
1198
Chris Mason5f39d392007-10-15 16:14:19 -04001199 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
1200 if (ret)
1201 goto fail;
1202
Chris Mason39279cc2007-06-12 06:35:45 -04001203 inode->i_uid = current->fsuid;
1204 inode->i_gid = current->fsgid;
1205 inode->i_mode = mode;
1206 inode->i_ino = objectid;
1207 inode->i_blocks = 0;
1208 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04001209 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1210 struct btrfs_inode_item);
1211 fill_inode_item(path->nodes[0], inode_item, inode);
1212 btrfs_mark_buffer_dirty(path->nodes[0]);
1213 btrfs_free_path(path);
1214
Chris Mason39279cc2007-06-12 06:35:45 -04001215 location = &BTRFS_I(inode)->location;
1216 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04001217 location->offset = 0;
1218 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1219
Chris Mason39279cc2007-06-12 06:35:45 -04001220 insert_inode_hash(inode);
1221 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001222fail:
1223 btrfs_free_path(path);
1224 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04001225}
1226
1227static inline u8 btrfs_inode_type(struct inode *inode)
1228{
1229 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1230}
1231
1232static int btrfs_add_link(struct btrfs_trans_handle *trans,
1233 struct dentry *dentry, struct inode *inode)
1234{
1235 int ret;
1236 struct btrfs_key key;
1237 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Chris Mason79c44582007-06-25 10:09:33 -04001238 struct inode *parent_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001239
Chris Mason39279cc2007-06-12 06:35:45 -04001240 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04001241 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1242 key.offset = 0;
1243
1244 ret = btrfs_insert_dir_item(trans, root,
1245 dentry->d_name.name, dentry->d_name.len,
1246 dentry->d_parent->d_inode->i_ino,
1247 &key, btrfs_inode_type(inode));
1248 if (ret == 0) {
Chris Mason79c44582007-06-25 10:09:33 -04001249 parent_inode = dentry->d_parent->d_inode;
1250 parent_inode->i_size += dentry->d_name.len * 2;
1251 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001252 ret = btrfs_update_inode(trans, root,
1253 dentry->d_parent->d_inode);
1254 }
1255 return ret;
1256}
1257
1258static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1259 struct dentry *dentry, struct inode *inode)
1260{
1261 int err = btrfs_add_link(trans, dentry, inode);
1262 if (!err) {
1263 d_instantiate(dentry, inode);
1264 return 0;
1265 }
1266 if (err > 0)
1267 err = -EEXIST;
1268 return err;
1269}
1270
Josef Bacik618e21d2007-07-11 10:18:17 -04001271static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1272 int mode, dev_t rdev)
1273{
1274 struct btrfs_trans_handle *trans;
1275 struct btrfs_root *root = BTRFS_I(dir)->root;
1276 struct inode *inode;
1277 int err;
1278 int drop_inode = 0;
1279 u64 objectid;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001280 unsigned long nr;
Josef Bacik618e21d2007-07-11 10:18:17 -04001281
1282 if (!new_valid_dev(rdev))
1283 return -EINVAL;
1284
1285 mutex_lock(&root->fs_info->fs_mutex);
1286 trans = btrfs_start_transaction(root, 1);
1287 btrfs_set_trans_block_group(trans, dir);
1288
1289 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1290 if (err) {
1291 err = -ENOSPC;
1292 goto out_unlock;
1293 }
1294
1295 inode = btrfs_new_inode(trans, root, objectid,
1296 BTRFS_I(dir)->block_group, mode);
1297 err = PTR_ERR(inode);
1298 if (IS_ERR(inode))
1299 goto out_unlock;
1300
1301 btrfs_set_trans_block_group(trans, inode);
1302 err = btrfs_add_nondir(trans, dentry, inode);
1303 if (err)
1304 drop_inode = 1;
1305 else {
1306 inode->i_op = &btrfs_special_inode_operations;
1307 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04001308 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04001309 }
1310 dir->i_sb->s_dirt = 1;
1311 btrfs_update_inode_block_group(trans, inode);
1312 btrfs_update_inode_block_group(trans, dir);
1313out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001314 nr = trans->blocks_used;
Josef Bacik618e21d2007-07-11 10:18:17 -04001315 btrfs_end_transaction(trans, root);
1316 mutex_unlock(&root->fs_info->fs_mutex);
1317
1318 if (drop_inode) {
1319 inode_dec_link_count(inode);
1320 iput(inode);
1321 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001322 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04001323 return err;
1324}
1325
Chris Mason39279cc2007-06-12 06:35:45 -04001326static int btrfs_create(struct inode *dir, struct dentry *dentry,
1327 int mode, struct nameidata *nd)
1328{
1329 struct btrfs_trans_handle *trans;
1330 struct btrfs_root *root = BTRFS_I(dir)->root;
1331 struct inode *inode;
1332 int err;
1333 int drop_inode = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001334 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001335 u64 objectid;
1336
1337 mutex_lock(&root->fs_info->fs_mutex);
1338 trans = btrfs_start_transaction(root, 1);
1339 btrfs_set_trans_block_group(trans, dir);
1340
1341 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1342 if (err) {
1343 err = -ENOSPC;
1344 goto out_unlock;
1345 }
1346
1347 inode = btrfs_new_inode(trans, root, objectid,
1348 BTRFS_I(dir)->block_group, mode);
1349 err = PTR_ERR(inode);
1350 if (IS_ERR(inode))
1351 goto out_unlock;
1352
1353 btrfs_set_trans_block_group(trans, inode);
1354 err = btrfs_add_nondir(trans, dentry, inode);
1355 if (err)
1356 drop_inode = 1;
1357 else {
1358 inode->i_mapping->a_ops = &btrfs_aops;
1359 inode->i_fop = &btrfs_file_operations;
1360 inode->i_op = &btrfs_file_inode_operations;
Chris Masona52d9a82007-08-27 16:49:44 -04001361 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1362 inode->i_mapping, GFP_NOFS);
Chris Mason07157aa2007-08-30 08:50:51 -04001363 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04001364 }
1365 dir->i_sb->s_dirt = 1;
1366 btrfs_update_inode_block_group(trans, inode);
1367 btrfs_update_inode_block_group(trans, dir);
1368out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001369 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001370 btrfs_end_transaction(trans, root);
1371 mutex_unlock(&root->fs_info->fs_mutex);
1372
1373 if (drop_inode) {
1374 inode_dec_link_count(inode);
1375 iput(inode);
1376 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001377 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001378 return err;
1379}
1380
1381static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1382 struct dentry *dentry)
1383{
1384 struct btrfs_trans_handle *trans;
1385 struct btrfs_root *root = BTRFS_I(dir)->root;
1386 struct inode *inode = old_dentry->d_inode;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001387 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001388 int err;
1389 int drop_inode = 0;
1390
1391 if (inode->i_nlink == 0)
1392 return -ENOENT;
1393
1394 inc_nlink(inode);
1395 mutex_lock(&root->fs_info->fs_mutex);
1396 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001397
Chris Mason39279cc2007-06-12 06:35:45 -04001398 btrfs_set_trans_block_group(trans, dir);
1399 atomic_inc(&inode->i_count);
1400 err = btrfs_add_nondir(trans, dentry, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001401
Chris Mason39279cc2007-06-12 06:35:45 -04001402 if (err)
1403 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001404
Chris Mason39279cc2007-06-12 06:35:45 -04001405 dir->i_sb->s_dirt = 1;
1406 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04001407 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001408
Chris Mason54aa1f42007-06-22 14:16:25 -04001409 if (err)
1410 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001411
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001412 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001413 btrfs_end_transaction(trans, root);
1414 mutex_unlock(&root->fs_info->fs_mutex);
1415
1416 if (drop_inode) {
1417 inode_dec_link_count(inode);
1418 iput(inode);
1419 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001420 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001421 return err;
1422}
1423
1424static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
1425 struct btrfs_root *root,
1426 u64 objectid, u64 dirid)
1427{
1428 int ret;
1429 char buf[2];
1430 struct btrfs_key key;
1431
1432 buf[0] = '.';
1433 buf[1] = '.';
1434
1435 key.objectid = objectid;
1436 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001437 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1438
1439 ret = btrfs_insert_dir_item(trans, root, buf, 1, objectid,
1440 &key, BTRFS_FT_DIR);
1441 if (ret)
1442 goto error;
Chris Mason5f39d392007-10-15 16:14:19 -04001443
Chris Mason39279cc2007-06-12 06:35:45 -04001444 key.objectid = dirid;
1445 ret = btrfs_insert_dir_item(trans, root, buf, 2, objectid,
1446 &key, BTRFS_FT_DIR);
1447 if (ret)
1448 goto error;
1449error:
1450 return ret;
1451}
1452
1453static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1454{
1455 struct inode *inode;
1456 struct btrfs_trans_handle *trans;
1457 struct btrfs_root *root = BTRFS_I(dir)->root;
1458 int err = 0;
1459 int drop_on_err = 0;
1460 u64 objectid;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001461 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001462
1463 mutex_lock(&root->fs_info->fs_mutex);
1464 trans = btrfs_start_transaction(root, 1);
1465 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04001466
Chris Mason39279cc2007-06-12 06:35:45 -04001467 if (IS_ERR(trans)) {
1468 err = PTR_ERR(trans);
1469 goto out_unlock;
1470 }
1471
1472 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1473 if (err) {
1474 err = -ENOSPC;
1475 goto out_unlock;
1476 }
1477
1478 inode = btrfs_new_inode(trans, root, objectid,
1479 BTRFS_I(dir)->block_group, S_IFDIR | mode);
1480 if (IS_ERR(inode)) {
1481 err = PTR_ERR(inode);
1482 goto out_fail;
1483 }
Chris Mason5f39d392007-10-15 16:14:19 -04001484
Chris Mason39279cc2007-06-12 06:35:45 -04001485 drop_on_err = 1;
1486 inode->i_op = &btrfs_dir_inode_operations;
1487 inode->i_fop = &btrfs_dir_file_operations;
1488 btrfs_set_trans_block_group(trans, inode);
1489
1490 err = btrfs_make_empty_dir(trans, root, inode->i_ino, dir->i_ino);
1491 if (err)
1492 goto out_fail;
1493
1494 inode->i_size = 6;
1495 err = btrfs_update_inode(trans, root, inode);
1496 if (err)
1497 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04001498
Chris Mason39279cc2007-06-12 06:35:45 -04001499 err = btrfs_add_link(trans, dentry, inode);
1500 if (err)
1501 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04001502
Chris Mason39279cc2007-06-12 06:35:45 -04001503 d_instantiate(dentry, inode);
1504 drop_on_err = 0;
1505 dir->i_sb->s_dirt = 1;
1506 btrfs_update_inode_block_group(trans, inode);
1507 btrfs_update_inode_block_group(trans, dir);
1508
1509out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001510 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001511 btrfs_end_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04001512
Chris Mason39279cc2007-06-12 06:35:45 -04001513out_unlock:
1514 mutex_unlock(&root->fs_info->fs_mutex);
1515 if (drop_on_err)
1516 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001517 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001518 return err;
1519}
1520
Chris Masona52d9a82007-08-27 16:49:44 -04001521struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
1522 size_t page_offset, u64 start, u64 end,
1523 int create)
1524{
1525 int ret;
1526 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04001527 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04001528 u64 extent_start = 0;
1529 u64 extent_end = 0;
1530 u64 objectid = inode->i_ino;
1531 u32 found_type;
1532 int failed_insert = 0;
1533 struct btrfs_path *path;
1534 struct btrfs_root *root = BTRFS_I(inode)->root;
1535 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04001536 struct extent_buffer *leaf;
1537 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04001538 struct extent_map *em = NULL;
1539 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1540 struct btrfs_trans_handle *trans = NULL;
1541
1542 path = btrfs_alloc_path();
1543 BUG_ON(!path);
1544 mutex_lock(&root->fs_info->fs_mutex);
1545
1546again:
1547 em = lookup_extent_mapping(em_tree, start, end);
1548 if (em) {
1549 goto out;
1550 }
1551 if (!em) {
1552 em = alloc_extent_map(GFP_NOFS);
1553 if (!em) {
1554 err = -ENOMEM;
1555 goto out;
1556 }
Chris Mason5f39d392007-10-15 16:14:19 -04001557 em->start = EXTENT_MAP_HOLE;
1558 em->end = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001559 }
1560 em->bdev = inode->i_sb->s_bdev;
Chris Mason179e29e2007-11-01 11:28:41 -04001561 ret = btrfs_lookup_file_extent(trans, root, path,
1562 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04001563 if (ret < 0) {
1564 err = ret;
1565 goto out;
1566 }
1567
1568 if (ret != 0) {
1569 if (path->slots[0] == 0)
1570 goto not_found;
1571 path->slots[0]--;
1572 }
1573
Chris Mason5f39d392007-10-15 16:14:19 -04001574 leaf = path->nodes[0];
1575 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04001576 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04001577 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04001578 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1579 found_type = btrfs_key_type(&found_key);
1580 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04001581 found_type != BTRFS_EXTENT_DATA_KEY) {
1582 goto not_found;
1583 }
1584
Chris Mason5f39d392007-10-15 16:14:19 -04001585 found_type = btrfs_file_extent_type(leaf, item);
1586 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04001587 if (found_type == BTRFS_FILE_EXTENT_REG) {
1588 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04001589 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04001590 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04001591 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04001592 em->start = start;
1593 if (start < extent_start) {
Chris Masonb888db22007-08-27 16:49:44 -04001594 if (end < extent_start)
1595 goto not_found;
Chris Masona52d9a82007-08-27 16:49:44 -04001596 em->end = extent_end - 1;
1597 } else {
1598 em->end = end;
1599 }
1600 goto not_found_em;
1601 }
Chris Masondb945352007-10-15 16:15:53 -04001602 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
1603 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04001604 em->start = extent_start;
1605 em->end = extent_end - 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001606 em->block_start = EXTENT_MAP_HOLE;
1607 em->block_end = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001608 goto insert;
1609 }
Chris Masondb945352007-10-15 16:15:53 -04001610 bytenr += btrfs_file_extent_offset(leaf, item);
1611 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04001612 em->block_end = em->block_start +
Chris Masondb945352007-10-15 16:15:53 -04001613 btrfs_file_extent_num_bytes(leaf, item) - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04001614 em->start = extent_start;
1615 em->end = extent_end - 1;
1616 goto insert;
1617 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04001618 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04001619 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04001620 size_t size;
1621 size_t extent_offset;
1622 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04001623
Chris Mason5f39d392007-10-15 16:14:19 -04001624 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
1625 path->slots[0]));
Yan689f9342007-10-29 11:41:07 -04001626 extent_end = (extent_start + size - 1) |
Chris Masondb945352007-10-15 16:15:53 -04001627 ((u64)root->sectorsize - 1);
Chris Masonb888db22007-08-27 16:49:44 -04001628 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04001629 em->start = start;
1630 if (start < extent_start) {
Chris Masonb888db22007-08-27 16:49:44 -04001631 if (end < extent_start)
1632 goto not_found;
Chris Mason50b78c22007-09-20 14:14:42 -04001633 em->end = extent_end;
Chris Masona52d9a82007-08-27 16:49:44 -04001634 } else {
1635 em->end = end;
1636 }
1637 goto not_found_em;
1638 }
1639 em->block_start = EXTENT_MAP_INLINE;
1640 em->block_end = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04001641
1642 if (!page) {
1643 em->start = extent_start;
1644 em->end = extent_start + size - 1;
1645 goto out;
1646 }
1647
Chris Mason35ebb932007-10-30 16:56:53 -04001648 extent_offset = ((u64)page->index << PAGE_CACHE_SHIFT) -
Yan689f9342007-10-29 11:41:07 -04001649 extent_start + page_offset;
1650 copy_size = min_t(u64, PAGE_CACHE_SIZE - page_offset,
1651 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04001652 em->start = extent_start + extent_offset;
1653 em->end = (em->start + copy_size -1) |
1654 ((u64)root->sectorsize -1);
Yan689f9342007-10-29 11:41:07 -04001655 map = kmap(page);
1656 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04001657 if (create == 0 && !PageUptodate(page)) {
1658 read_extent_buffer(leaf, map + page_offset, ptr,
1659 copy_size);
1660 flush_dcache_page(page);
1661 } else if (create && PageUptodate(page)) {
1662 if (!trans) {
1663 kunmap(page);
1664 free_extent_map(em);
1665 em = NULL;
1666 btrfs_release_path(root, path);
1667 trans = btrfs_start_transaction(root, 1);
1668 goto again;
1669 }
1670 write_extent_buffer(leaf, map + page_offset, ptr,
1671 copy_size);
1672 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04001673 }
Chris Masona52d9a82007-08-27 16:49:44 -04001674 kunmap(page);
Chris Mason3326d1b2007-10-15 16:18:25 -04001675 set_extent_uptodate(em_tree, em->start, em->end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04001676 goto insert;
1677 } else {
1678 printk("unkknown found_type %d\n", found_type);
1679 WARN_ON(1);
1680 }
1681not_found:
1682 em->start = start;
1683 em->end = end;
1684not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04001685 em->block_start = EXTENT_MAP_HOLE;
1686 em->block_end = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001687insert:
1688 btrfs_release_path(root, path);
1689 if (em->start > start || em->end < start) {
Chris Masonb888db22007-08-27 16:49:44 -04001690 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->end, start, end);
Chris Masona52d9a82007-08-27 16:49:44 -04001691 err = -EIO;
1692 goto out;
1693 }
1694 ret = add_extent_mapping(em_tree, em);
1695 if (ret == -EEXIST) {
1696 free_extent_map(em);
Chris Mason2bf5a722007-08-30 11:54:02 -04001697 em = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04001698 failed_insert++;
1699 if (failed_insert > 5) {
1700 printk("failing to insert %Lu %Lu\n", start, end);
1701 err = -EIO;
1702 goto out;
1703 }
Chris Masona52d9a82007-08-27 16:49:44 -04001704 goto again;
1705 }
1706 err = 0;
1707out:
1708 btrfs_free_path(path);
1709 if (trans) {
1710 ret = btrfs_end_transaction(trans, root);
1711 if (!err)
1712 err = ret;
1713 }
1714 mutex_unlock(&root->fs_info->fs_mutex);
1715 if (err) {
1716 free_extent_map(em);
1717 WARN_ON(1);
1718 return ERR_PTR(err);
1719 }
1720 return em;
1721}
1722
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04001723static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04001724{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04001725 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04001726}
1727
1728static int btrfs_prepare_write(struct file *file, struct page *page,
1729 unsigned from, unsigned to)
1730{
Chris Masona52d9a82007-08-27 16:49:44 -04001731 return extent_prepare_write(&BTRFS_I(page->mapping->host)->extent_tree,
1732 page->mapping->host, page, from, to,
1733 btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04001734}
1735
Chris Mason9ebefb182007-06-15 13:50:00 -04001736int btrfs_readpage(struct file *file, struct page *page)
1737{
Chris Masona52d9a82007-08-27 16:49:44 -04001738 struct extent_map_tree *tree;
1739 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1740 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04001741}
Chris Mason39279cc2007-06-12 06:35:45 -04001742static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1743{
Chris Masona52d9a82007-08-27 16:49:44 -04001744 struct extent_map_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04001745
1746
1747 if (current->flags & PF_MEMALLOC) {
1748 redirty_page_for_writepage(wbc, page);
1749 unlock_page(page);
1750 return 0;
1751 }
Chris Masona52d9a82007-08-27 16:49:44 -04001752 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1753 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
1754}
Chris Mason39279cc2007-06-12 06:35:45 -04001755
Chris Masonb293f02e2007-11-01 19:45:34 -04001756static int btrfs_writepages(struct address_space *mapping,
1757 struct writeback_control *wbc)
1758{
1759 struct extent_map_tree *tree;
1760 tree = &BTRFS_I(mapping->host)->extent_tree;
1761 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
1762}
1763
Chris Mason3ab2fb52007-11-08 10:59:22 -05001764static int
1765btrfs_readpages(struct file *file, struct address_space *mapping,
1766 struct list_head *pages, unsigned nr_pages)
1767{
1768 struct extent_map_tree *tree;
1769 tree = &BTRFS_I(mapping->host)->extent_tree;
1770 return extent_readpages(tree, mapping, pages, nr_pages,
1771 btrfs_get_extent);
1772}
1773
Chris Masona52d9a82007-08-27 16:49:44 -04001774static int btrfs_releasepage(struct page *page, gfp_t unused_gfp_flags)
1775{
1776 struct extent_map_tree *tree;
1777 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04001778
Chris Masona52d9a82007-08-27 16:49:44 -04001779 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1780 ret = try_release_extent_mapping(tree, page);
1781 if (ret == 1) {
1782 ClearPagePrivate(page);
1783 set_page_private(page, 0);
1784 page_cache_release(page);
1785 }
1786 return ret;
1787}
Chris Mason39279cc2007-06-12 06:35:45 -04001788
Chris Masona52d9a82007-08-27 16:49:44 -04001789static void btrfs_invalidatepage(struct page *page, unsigned long offset)
1790{
1791 struct extent_map_tree *tree;
1792
1793 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1794 extent_invalidatepage(tree, page, offset);
1795 btrfs_releasepage(page, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001796}
1797
Chris Mason9ebefb182007-06-15 13:50:00 -04001798/*
1799 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
1800 * called from a page fault handler when a page is first dirtied. Hence we must
1801 * be careful to check for EOF conditions here. We set the page up correctly
1802 * for a written page which means we get ENOSPC checking when writing into
1803 * holes and correct delalloc and unwritten extent mapping on filesystems that
1804 * support these features.
1805 *
1806 * We are not allowed to take the i_mutex here so we have to play games to
1807 * protect against truncate races as the page could now be beyond EOF. Because
1808 * vmtruncate() writes the inode size before removing pages, once we have the
1809 * page lock we can determine safely if the page is beyond EOF. If it is not
1810 * beyond EOF, then the page is guaranteed safe against truncation until we
1811 * unlock the page.
1812 */
1813int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
1814{
1815 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1816 unsigned long end;
1817 loff_t size;
1818 int ret = -EINVAL;
Chris Masona52d9a82007-08-27 16:49:44 -04001819 u64 page_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04001820
Chris Mason011410b2007-09-10 19:58:36 -04001821 down_read(&BTRFS_I(inode)->root->snap_sem);
Chris Mason9ebefb182007-06-15 13:50:00 -04001822 lock_page(page);
1823 wait_on_page_writeback(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04001824 size = i_size_read(inode);
Chris Mason35ebb932007-10-30 16:56:53 -04001825 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04001826
Chris Mason9ebefb182007-06-15 13:50:00 -04001827 if ((page->mapping != inode->i_mapping) ||
Chris Masona52d9a82007-08-27 16:49:44 -04001828 (page_start > size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04001829 /* page got truncated out from underneath us */
1830 goto out_unlock;
1831 }
1832
1833 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04001834 if (page_start + PAGE_CACHE_SIZE > size)
Chris Mason9ebefb182007-06-15 13:50:00 -04001835 end = size & ~PAGE_CACHE_MASK;
1836 else
1837 end = PAGE_CACHE_SIZE;
1838
Chris Masonb888db22007-08-27 16:49:44 -04001839 ret = btrfs_cow_one_page(inode, page, end);
Chris Mason9ebefb182007-06-15 13:50:00 -04001840
1841out_unlock:
Chris Mason011410b2007-09-10 19:58:36 -04001842 up_read(&BTRFS_I(inode)->root->snap_sem);
Chris Mason9ebefb182007-06-15 13:50:00 -04001843 unlock_page(page);
1844 return ret;
1845}
1846
Chris Mason39279cc2007-06-12 06:35:45 -04001847static void btrfs_truncate(struct inode *inode)
1848{
1849 struct btrfs_root *root = BTRFS_I(inode)->root;
1850 int ret;
1851 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001852 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001853
1854 if (!S_ISREG(inode->i_mode))
1855 return;
1856 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1857 return;
1858
1859 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1860
1861 mutex_lock(&root->fs_info->fs_mutex);
1862 trans = btrfs_start_transaction(root, 1);
1863 btrfs_set_trans_block_group(trans, inode);
1864
1865 /* FIXME, add redo link to tree so we don't leak on crash */
1866 ret = btrfs_truncate_in_trans(trans, root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001867 btrfs_update_inode(trans, root, inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001868 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04001869
Chris Mason39279cc2007-06-12 06:35:45 -04001870 ret = btrfs_end_transaction(trans, root);
1871 BUG_ON(ret);
1872 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001873 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001874}
1875
1876int btrfs_commit_write(struct file *file, struct page *page,
1877 unsigned from, unsigned to)
1878{
Chris Masona52d9a82007-08-27 16:49:44 -04001879 return extent_commit_write(&BTRFS_I(page->mapping->host)->extent_tree,
1880 page->mapping->host, page, from, to);
Chris Mason39279cc2007-06-12 06:35:45 -04001881}
1882
1883static int create_subvol(struct btrfs_root *root, char *name, int namelen)
1884{
1885 struct btrfs_trans_handle *trans;
1886 struct btrfs_key key;
1887 struct btrfs_root_item root_item;
1888 struct btrfs_inode_item *inode_item;
Chris Mason5f39d392007-10-15 16:14:19 -04001889 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001890 struct btrfs_root *new_root;
1891 struct inode *inode;
1892 struct inode *dir;
1893 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001894 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04001895 u64 objectid;
1896 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001897 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001898
1899 mutex_lock(&root->fs_info->fs_mutex);
1900 trans = btrfs_start_transaction(root, 1);
1901 BUG_ON(!trans);
1902
Chris Masondb945352007-10-15 16:15:53 -04001903 leaf = btrfs_alloc_free_block(trans, root, root->leafsize, 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001904 if (IS_ERR(leaf))
1905 return PTR_ERR(leaf);
1906
1907 btrfs_set_header_nritems(leaf, 0);
1908 btrfs_set_header_level(leaf, 0);
Chris Masondb945352007-10-15 16:15:53 -04001909 btrfs_set_header_bytenr(leaf, leaf->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001910 btrfs_set_header_generation(leaf, trans->transid);
1911 btrfs_set_header_owner(leaf, root->root_key.objectid);
1912 write_extent_buffer(leaf, root->fs_info->fsid,
1913 (unsigned long)btrfs_header_fsid(leaf),
1914 BTRFS_FSID_SIZE);
1915 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001916
1917 inode_item = &root_item.inode;
1918 memset(inode_item, 0, sizeof(*inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04001919 inode_item->generation = cpu_to_le64(1);
1920 inode_item->size = cpu_to_le64(3);
1921 inode_item->nlink = cpu_to_le32(1);
1922 inode_item->nblocks = cpu_to_le64(1);
1923 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
Chris Mason39279cc2007-06-12 06:35:45 -04001924
Chris Masondb945352007-10-15 16:15:53 -04001925 btrfs_set_root_bytenr(&root_item, leaf->start);
1926 btrfs_set_root_level(&root_item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001927 btrfs_set_root_refs(&root_item, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001928 btrfs_set_root_used(&root_item, 0);
1929
Chris Mason5eda7b52007-06-22 14:16:25 -04001930 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
1931 root_item.drop_level = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001932
1933 free_extent_buffer(leaf);
1934 leaf = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001935
1936 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
1937 0, &objectid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001938 if (ret)
1939 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001940
1941 btrfs_set_root_dirid(&root_item, new_dirid);
1942
1943 key.objectid = objectid;
1944 key.offset = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001945 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
1946 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
1947 &root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -04001948 if (ret)
1949 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001950
1951 /*
1952 * insert the directory item
1953 */
1954 key.offset = (u64)-1;
1955 dir = root->fs_info->sb->s_root->d_inode;
1956 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
1957 name, namelen, dir->i_ino, &key,
1958 BTRFS_FT_DIR);
Chris Mason54aa1f42007-06-22 14:16:25 -04001959 if (ret)
1960 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001961
1962 ret = btrfs_commit_transaction(trans, root);
Chris Mason54aa1f42007-06-22 14:16:25 -04001963 if (ret)
1964 goto fail_commit;
Chris Mason39279cc2007-06-12 06:35:45 -04001965
Josef Bacik58176a92007-08-29 15:47:34 -04001966 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
Chris Mason39279cc2007-06-12 06:35:45 -04001967 BUG_ON(!new_root);
1968
1969 trans = btrfs_start_transaction(new_root, 1);
1970 BUG_ON(!trans);
1971
1972 inode = btrfs_new_inode(trans, new_root, new_dirid,
1973 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
Chris Mason54aa1f42007-06-22 14:16:25 -04001974 if (IS_ERR(inode))
1975 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001976 inode->i_op = &btrfs_dir_inode_operations;
1977 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04001978 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001979
1980 ret = btrfs_make_empty_dir(trans, new_root, new_dirid, new_dirid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001981 if (ret)
1982 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001983
1984 inode->i_nlink = 1;
1985 inode->i_size = 6;
1986 ret = btrfs_update_inode(trans, new_root, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04001987 if (ret)
1988 goto fail;
1989fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001990 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04001991 err = btrfs_commit_transaction(trans, root);
1992 if (err && !ret)
1993 ret = err;
1994fail_commit:
Chris Mason39279cc2007-06-12 06:35:45 -04001995 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001996 btrfs_btree_balance_dirty(root, nr);
Chris Mason54aa1f42007-06-22 14:16:25 -04001997 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04001998}
1999
2000static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2001{
2002 struct btrfs_trans_handle *trans;
2003 struct btrfs_key key;
2004 struct btrfs_root_item new_root_item;
Chris Mason5f39d392007-10-15 16:14:19 -04002005 struct extent_buffer *tmp;
Chris Mason39279cc2007-06-12 06:35:45 -04002006 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002007 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04002008 u64 objectid;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002009 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002010
2011 if (!root->ref_cows)
2012 return -EINVAL;
2013
Chris Mason011410b2007-09-10 19:58:36 -04002014 down_write(&root->snap_sem);
2015 freeze_bdev(root->fs_info->sb->s_bdev);
2016 thaw_bdev(root->fs_info->sb->s_bdev, root->fs_info->sb);
2017
Chris Mason39279cc2007-06-12 06:35:45 -04002018 mutex_lock(&root->fs_info->fs_mutex);
2019 trans = btrfs_start_transaction(root, 1);
2020 BUG_ON(!trans);
2021
2022 ret = btrfs_update_inode(trans, root, root->inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002023 if (ret)
2024 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002025
2026 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2027 0, &objectid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002028 if (ret)
2029 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002030
2031 memcpy(&new_root_item, &root->root_item,
2032 sizeof(new_root_item));
2033
2034 key.objectid = objectid;
2035 key.offset = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002036 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
Chris Mason5f39d392007-10-15 16:14:19 -04002037
Chris Mason83df7c12007-08-27 16:49:44 -04002038 btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp);
Chris Masondb945352007-10-15 16:15:53 -04002039 btrfs_set_root_bytenr(&new_root_item, root->node->start);
2040 btrfs_set_root_level(&new_root_item, btrfs_header_level(root->node));
Chris Mason39279cc2007-06-12 06:35:45 -04002041
2042 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2043 &new_root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -04002044 if (ret)
2045 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002046
2047 /*
2048 * insert the directory item
2049 */
2050 key.offset = (u64)-1;
2051 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2052 name, namelen,
2053 root->fs_info->sb->s_root->d_inode->i_ino,
2054 &key, BTRFS_FT_DIR);
2055
Chris Mason54aa1f42007-06-22 14:16:25 -04002056 if (ret)
2057 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002058
2059 ret = btrfs_inc_root_ref(trans, root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002060 if (ret)
2061 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002062fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002063 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04002064 err = btrfs_commit_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002065
Chris Mason54aa1f42007-06-22 14:16:25 -04002066 if (err && !ret)
2067 ret = err;
Chris Mason5f39d392007-10-15 16:14:19 -04002068
Chris Mason39279cc2007-06-12 06:35:45 -04002069 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason011410b2007-09-10 19:58:36 -04002070 up_write(&root->snap_sem);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002071 btrfs_btree_balance_dirty(root, nr);
Chris Mason54aa1f42007-06-22 14:16:25 -04002072 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002073}
2074
Chris Mason86479a02007-09-10 19:58:16 -04002075static unsigned long force_ra(struct address_space *mapping,
2076 struct file_ra_state *ra, struct file *file,
2077 pgoff_t offset, pgoff_t last_index)
2078{
2079 pgoff_t req_size;
2080
2081#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2082 req_size = last_index - offset + 1;
2083 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2084 return offset;
2085#else
2086 req_size = min(last_index - offset + 1, (pgoff_t)128);
2087 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2088 return offset + req_size;
2089#endif
2090}
2091
2092int btrfs_defrag_file(struct file *file) {
2093 struct inode *inode = file->f_path.dentry->d_inode;
2094 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2095 struct page *page;
2096 unsigned long last_index;
2097 unsigned long ra_index = 0;
2098 u64 page_start;
2099 u64 page_end;
2100 unsigned long i;
2101
2102 mutex_lock(&inode->i_mutex);
2103 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2104 for (i = 0; i <= last_index; i++) {
2105 if (i == ra_index) {
2106 ra_index = force_ra(inode->i_mapping, &file->f_ra,
2107 file, ra_index, last_index);
2108 }
2109 page = grab_cache_page(inode->i_mapping, i);
2110 if (!page)
2111 goto out_unlock;
2112 if (!PageUptodate(page)) {
2113 btrfs_readpage(NULL, page);
2114 lock_page(page);
2115 if (!PageUptodate(page)) {
2116 unlock_page(page);
2117 page_cache_release(page);
2118 goto out_unlock;
2119 }
2120 }
Chris Mason35ebb932007-10-30 16:56:53 -04002121 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason86479a02007-09-10 19:58:16 -04002122 page_end = page_start + PAGE_CACHE_SIZE - 1;
2123
2124 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
2125 set_extent_delalloc(em_tree, page_start,
2126 page_end, GFP_NOFS);
2127 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
2128 set_page_dirty(page);
2129 unlock_page(page);
2130 page_cache_release(page);
2131 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2132 }
2133
2134out_unlock:
2135 mutex_unlock(&inode->i_mutex);
2136 return 0;
2137}
2138
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002139static int btrfs_ioctl_snap_create(struct btrfs_root *root, void __user *arg)
2140{
2141 struct btrfs_ioctl_vol_args vol_args;
2142 struct btrfs_dir_item *di;
2143 struct btrfs_path *path;
2144 int namelen;
2145 u64 root_dirid;
2146
2147 if (copy_from_user(&vol_args, arg, sizeof(vol_args)))
2148 return -EFAULT;
Chris Mason5f39d392007-10-15 16:14:19 -04002149
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002150 namelen = strlen(vol_args.name);
2151 if (namelen > BTRFS_VOL_NAME_MAX)
2152 return -EINVAL;
2153 if (strchr(vol_args.name, '/'))
2154 return -EINVAL;
2155
2156 path = btrfs_alloc_path();
2157 if (!path)
2158 return -ENOMEM;
2159
2160 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2161 mutex_lock(&root->fs_info->fs_mutex);
2162 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2163 path, root_dirid,
2164 vol_args.name, namelen, 0);
2165 mutex_unlock(&root->fs_info->fs_mutex);
2166 btrfs_free_path(path);
2167 if (di && !IS_ERR(di))
2168 return -EEXIST;
2169 if (IS_ERR(di))
2170 return PTR_ERR(di);
2171
2172 if (root == root->fs_info->tree_root)
2173 return create_subvol(root, vol_args.name, namelen);
2174 return create_snapshot(root, vol_args.name, namelen);
2175}
2176
2177static int btrfs_ioctl_defrag(struct file *file)
Chris Mason39279cc2007-06-12 06:35:45 -04002178{
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002179 struct inode *inode = file->f_path.dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002180 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002181
2182 switch (inode->i_mode & S_IFMT) {
2183 case S_IFDIR:
2184 mutex_lock(&root->fs_info->fs_mutex);
2185 btrfs_defrag_root(root, 0);
2186 btrfs_defrag_root(root->fs_info->extent_root, 0);
2187 mutex_unlock(&root->fs_info->fs_mutex);
2188 break;
2189 case S_IFREG:
2190 btrfs_defrag_file(file);
2191 break;
2192 }
2193
2194 return 0;
2195}
2196
2197long btrfs_ioctl(struct file *file, unsigned int
2198 cmd, unsigned long arg)
2199{
2200 struct btrfs_root *root = BTRFS_I(file->f_path.dentry->d_inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002201
2202 switch (cmd) {
2203 case BTRFS_IOC_SNAP_CREATE:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002204 return btrfs_ioctl_snap_create(root, (void __user *)arg);
Chris Mason6702ed42007-08-07 16:15:09 -04002205 case BTRFS_IOC_DEFRAG:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002206 return btrfs_ioctl_defrag(file);
Chris Mason39279cc2007-06-12 06:35:45 -04002207 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002208
2209 return -ENOTTY;
Chris Mason39279cc2007-06-12 06:35:45 -04002210}
2211
Chris Mason39279cc2007-06-12 06:35:45 -04002212/*
2213 * Called inside transaction, so use GFP_NOFS
2214 */
2215struct inode *btrfs_alloc_inode(struct super_block *sb)
2216{
2217 struct btrfs_inode *ei;
2218
2219 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2220 if (!ei)
2221 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002222 ei->last_trans = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002223 return &ei->vfs_inode;
2224}
2225
2226void btrfs_destroy_inode(struct inode *inode)
2227{
2228 WARN_ON(!list_empty(&inode->i_dentry));
2229 WARN_ON(inode->i_data.nrpages);
2230
2231 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2232}
2233
Chris Mason44ec0b72007-10-29 10:55:05 -04002234#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2235static void init_once(struct kmem_cache * cachep, void *foo)
2236#else
Chris Mason39279cc2007-06-12 06:35:45 -04002237static void init_once(void * foo, struct kmem_cache * cachep,
2238 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04002239#endif
Chris Mason39279cc2007-06-12 06:35:45 -04002240{
2241 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2242
2243 inode_init_once(&ei->vfs_inode);
2244}
2245
2246void btrfs_destroy_cachep(void)
2247{
2248 if (btrfs_inode_cachep)
2249 kmem_cache_destroy(btrfs_inode_cachep);
2250 if (btrfs_trans_handle_cachep)
2251 kmem_cache_destroy(btrfs_trans_handle_cachep);
2252 if (btrfs_transaction_cachep)
2253 kmem_cache_destroy(btrfs_transaction_cachep);
2254 if (btrfs_bit_radix_cachep)
2255 kmem_cache_destroy(btrfs_bit_radix_cachep);
2256 if (btrfs_path_cachep)
2257 kmem_cache_destroy(btrfs_path_cachep);
2258}
2259
Chris Mason86479a02007-09-10 19:58:16 -04002260struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04002261 unsigned long extra_flags,
Chris Mason44ec0b72007-10-29 10:55:05 -04002262#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2263 void (*ctor)(struct kmem_cache *, void *)
2264#else
Chris Mason92fee662007-07-25 12:31:35 -04002265 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04002266 unsigned long)
2267#endif
2268 )
Chris Mason92fee662007-07-25 12:31:35 -04002269{
2270 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2271 SLAB_MEM_SPREAD | extra_flags), ctor
2272#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2273 ,NULL
2274#endif
2275 );
2276}
2277
Chris Mason39279cc2007-06-12 06:35:45 -04002278int btrfs_init_cachep(void)
2279{
Chris Mason86479a02007-09-10 19:58:16 -04002280 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04002281 sizeof(struct btrfs_inode),
2282 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04002283 if (!btrfs_inode_cachep)
2284 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002285 btrfs_trans_handle_cachep =
2286 btrfs_cache_create("btrfs_trans_handle_cache",
2287 sizeof(struct btrfs_trans_handle),
2288 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002289 if (!btrfs_trans_handle_cachep)
2290 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002291 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04002292 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04002293 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002294 if (!btrfs_transaction_cachep)
2295 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002296 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04002297 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04002298 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002299 if (!btrfs_path_cachep)
2300 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002301 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04002302 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002303 if (!btrfs_bit_radix_cachep)
2304 goto fail;
2305 return 0;
2306fail:
2307 btrfs_destroy_cachep();
2308 return -ENOMEM;
2309}
2310
2311static int btrfs_getattr(struct vfsmount *mnt,
2312 struct dentry *dentry, struct kstat *stat)
2313{
2314 struct inode *inode = dentry->d_inode;
2315 generic_fillattr(inode, stat);
2316 stat->blksize = 256 * 1024;
2317 return 0;
2318}
2319
2320static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2321 struct inode * new_dir,struct dentry *new_dentry)
2322{
2323 struct btrfs_trans_handle *trans;
2324 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2325 struct inode *new_inode = new_dentry->d_inode;
2326 struct inode *old_inode = old_dentry->d_inode;
2327 struct timespec ctime = CURRENT_TIME;
2328 struct btrfs_path *path;
2329 struct btrfs_dir_item *di;
2330 int ret;
2331
2332 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2333 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2334 return -ENOTEMPTY;
2335 }
Chris Mason5f39d392007-10-15 16:14:19 -04002336
Chris Mason39279cc2007-06-12 06:35:45 -04002337 mutex_lock(&root->fs_info->fs_mutex);
2338 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002339
Chris Mason39279cc2007-06-12 06:35:45 -04002340 btrfs_set_trans_block_group(trans, new_dir);
2341 path = btrfs_alloc_path();
2342 if (!path) {
2343 ret = -ENOMEM;
2344 goto out_fail;
2345 }
2346
2347 old_dentry->d_inode->i_nlink++;
2348 old_dir->i_ctime = old_dir->i_mtime = ctime;
2349 new_dir->i_ctime = new_dir->i_mtime = ctime;
2350 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04002351
Chris Mason39279cc2007-06-12 06:35:45 -04002352 if (S_ISDIR(old_inode->i_mode) && old_dir != new_dir) {
2353 struct btrfs_key *location = &BTRFS_I(new_dir)->location;
Chris Mason5f39d392007-10-15 16:14:19 -04002354 struct btrfs_key old_parent_key;
Chris Mason39279cc2007-06-12 06:35:45 -04002355 di = btrfs_lookup_dir_item(trans, root, path, old_inode->i_ino,
2356 "..", 2, -1);
2357 if (IS_ERR(di)) {
2358 ret = PTR_ERR(di);
2359 goto out_fail;
2360 }
2361 if (!di) {
2362 ret = -ENOENT;
2363 goto out_fail;
2364 }
Chris Mason5f39d392007-10-15 16:14:19 -04002365 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &old_parent_key);
Chris Mason39279cc2007-06-12 06:35:45 -04002366 ret = btrfs_del_item(trans, root, path);
2367 if (ret) {
Chris Mason39279cc2007-06-12 06:35:45 -04002368 goto out_fail;
2369 }
2370 btrfs_release_path(root, path);
2371
2372 di = btrfs_lookup_dir_index_item(trans, root, path,
2373 old_inode->i_ino,
Chris Mason5f39d392007-10-15 16:14:19 -04002374 old_parent_key.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002375 "..", 2, -1);
2376 if (IS_ERR(di)) {
2377 ret = PTR_ERR(di);
2378 goto out_fail;
2379 }
2380 if (!di) {
2381 ret = -ENOENT;
2382 goto out_fail;
2383 }
2384 ret = btrfs_del_item(trans, root, path);
2385 if (ret) {
Chris Mason39279cc2007-06-12 06:35:45 -04002386 goto out_fail;
2387 }
2388 btrfs_release_path(root, path);
2389
2390 ret = btrfs_insert_dir_item(trans, root, "..", 2,
2391 old_inode->i_ino, location,
2392 BTRFS_FT_DIR);
2393 if (ret)
2394 goto out_fail;
2395 }
2396
2397
2398 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2399 if (ret)
2400 goto out_fail;
2401
2402 if (new_inode) {
2403 new_inode->i_ctime = CURRENT_TIME;
2404 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2405 if (ret)
2406 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002407 }
2408 ret = btrfs_add_link(trans, new_dentry, old_inode);
2409 if (ret)
2410 goto out_fail;
2411
2412out_fail:
2413 btrfs_free_path(path);
2414 btrfs_end_transaction(trans, root);
2415 mutex_unlock(&root->fs_info->fs_mutex);
2416 return ret;
2417}
2418
2419static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2420 const char *symname)
2421{
2422 struct btrfs_trans_handle *trans;
2423 struct btrfs_root *root = BTRFS_I(dir)->root;
2424 struct btrfs_path *path;
2425 struct btrfs_key key;
2426 struct inode *inode;
2427 int err;
2428 int drop_inode = 0;
2429 u64 objectid;
2430 int name_len;
2431 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04002432 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04002433 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04002434 struct extent_buffer *leaf;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002435 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002436
2437 name_len = strlen(symname) + 1;
2438 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2439 return -ENAMETOOLONG;
2440 mutex_lock(&root->fs_info->fs_mutex);
2441 trans = btrfs_start_transaction(root, 1);
2442 btrfs_set_trans_block_group(trans, dir);
2443
2444 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2445 if (err) {
2446 err = -ENOSPC;
2447 goto out_unlock;
2448 }
2449
2450 inode = btrfs_new_inode(trans, root, objectid,
2451 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2452 err = PTR_ERR(inode);
2453 if (IS_ERR(inode))
2454 goto out_unlock;
2455
2456 btrfs_set_trans_block_group(trans, inode);
2457 err = btrfs_add_nondir(trans, dentry, inode);
2458 if (err)
2459 drop_inode = 1;
2460 else {
2461 inode->i_mapping->a_ops = &btrfs_aops;
2462 inode->i_fop = &btrfs_file_operations;
2463 inode->i_op = &btrfs_file_inode_operations;
Chris Masona52d9a82007-08-27 16:49:44 -04002464 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
2465 inode->i_mapping, GFP_NOFS);
Chris Mason07157aa2007-08-30 08:50:51 -04002466 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002467 }
2468 dir->i_sb->s_dirt = 1;
2469 btrfs_update_inode_block_group(trans, inode);
2470 btrfs_update_inode_block_group(trans, dir);
2471 if (drop_inode)
2472 goto out_unlock;
2473
2474 path = btrfs_alloc_path();
2475 BUG_ON(!path);
2476 key.objectid = inode->i_ino;
2477 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002478 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2479 datasize = btrfs_file_extent_calc_inline_size(name_len);
2480 err = btrfs_insert_empty_item(trans, root, path, &key,
2481 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04002482 if (err) {
2483 drop_inode = 1;
2484 goto out_unlock;
2485 }
Chris Mason5f39d392007-10-15 16:14:19 -04002486 leaf = path->nodes[0];
2487 ei = btrfs_item_ptr(leaf, path->slots[0],
2488 struct btrfs_file_extent_item);
2489 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
2490 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04002491 BTRFS_FILE_EXTENT_INLINE);
2492 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04002493 write_extent_buffer(leaf, symname, ptr, name_len);
2494 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002495 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04002496
Chris Mason39279cc2007-06-12 06:35:45 -04002497 inode->i_op = &btrfs_symlink_inode_operations;
2498 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2499 inode->i_size = name_len - 1;
Chris Mason54aa1f42007-06-22 14:16:25 -04002500 err = btrfs_update_inode(trans, root, inode);
2501 if (err)
2502 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002503
2504out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002505 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04002506 btrfs_end_transaction(trans, root);
2507 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04002508 if (drop_inode) {
2509 inode_dec_link_count(inode);
2510 iput(inode);
2511 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002512 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002513 return err;
2514}
2515
2516static struct inode_operations btrfs_dir_inode_operations = {
2517 .lookup = btrfs_lookup,
2518 .create = btrfs_create,
2519 .unlink = btrfs_unlink,
2520 .link = btrfs_link,
2521 .mkdir = btrfs_mkdir,
2522 .rmdir = btrfs_rmdir,
2523 .rename = btrfs_rename,
2524 .symlink = btrfs_symlink,
2525 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04002526 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -05002527 .setxattr = generic_setxattr,
2528 .getxattr = generic_getxattr,
2529 .listxattr = btrfs_listxattr,
2530 .removexattr = generic_removexattr,
Chris Mason39279cc2007-06-12 06:35:45 -04002531};
2532
2533static struct inode_operations btrfs_dir_ro_inode_operations = {
2534 .lookup = btrfs_lookup,
2535};
2536
2537static struct file_operations btrfs_dir_file_operations = {
2538 .llseek = generic_file_llseek,
2539 .read = generic_read_dir,
2540 .readdir = btrfs_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002541 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002542#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002543 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002544#endif
2545};
2546
Chris Mason07157aa2007-08-30 08:50:51 -04002547static struct extent_map_ops btrfs_extent_map_ops = {
2548 .fill_delalloc = run_delalloc_range,
2549 .writepage_io_hook = btrfs_writepage_io_hook,
2550 .readpage_io_hook = btrfs_readpage_io_hook,
2551 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
2552};
2553
Chris Mason39279cc2007-06-12 06:35:45 -04002554static struct address_space_operations btrfs_aops = {
2555 .readpage = btrfs_readpage,
2556 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -04002557 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05002558 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04002559 .sync_page = block_sync_page,
2560 .prepare_write = btrfs_prepare_write,
2561 .commit_write = btrfs_commit_write,
2562 .bmap = btrfs_bmap,
Chris Masona52d9a82007-08-27 16:49:44 -04002563 .invalidatepage = btrfs_invalidatepage,
2564 .releasepage = btrfs_releasepage,
2565 .set_page_dirty = __set_page_dirty_nobuffers,
Chris Mason39279cc2007-06-12 06:35:45 -04002566};
2567
2568static struct address_space_operations btrfs_symlink_aops = {
2569 .readpage = btrfs_readpage,
2570 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04002571 .invalidatepage = btrfs_invalidatepage,
2572 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04002573};
2574
2575static struct inode_operations btrfs_file_inode_operations = {
2576 .truncate = btrfs_truncate,
2577 .getattr = btrfs_getattr,
2578 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -05002579 .setxattr = generic_setxattr,
2580 .getxattr = generic_getxattr,
2581 .listxattr = btrfs_listxattr,
2582 .removexattr = generic_removexattr,
Chris Mason39279cc2007-06-12 06:35:45 -04002583};
2584
Josef Bacik618e21d2007-07-11 10:18:17 -04002585static struct inode_operations btrfs_special_inode_operations = {
2586 .getattr = btrfs_getattr,
2587 .setattr = btrfs_setattr,
2588};
2589
Chris Mason39279cc2007-06-12 06:35:45 -04002590static struct inode_operations btrfs_symlink_inode_operations = {
2591 .readlink = generic_readlink,
2592 .follow_link = page_follow_link_light,
2593 .put_link = page_put_link,
2594};