blob: c88f13a885555d57baebb6785163ad556528e31b [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>
Chris Mason39279cc2007-06-12 06:35:45 -040035#include "ctree.h"
36#include "disk-io.h"
37#include "transaction.h"
38#include "btrfs_inode.h"
39#include "ioctl.h"
40#include "print-tree.h"
41
42struct btrfs_iget_args {
43 u64 ino;
44 struct btrfs_root *root;
45};
46
47static struct inode_operations btrfs_dir_inode_operations;
48static struct inode_operations btrfs_symlink_inode_operations;
49static struct inode_operations btrfs_dir_ro_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -040050static struct inode_operations btrfs_special_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -040051static struct inode_operations btrfs_file_inode_operations;
52static struct address_space_operations btrfs_aops;
53static struct address_space_operations btrfs_symlink_aops;
54static struct file_operations btrfs_dir_file_operations;
Chris Mason07157aa2007-08-30 08:50:51 -040055static struct extent_map_ops btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040056
57static struct kmem_cache *btrfs_inode_cachep;
58struct kmem_cache *btrfs_trans_handle_cachep;
59struct kmem_cache *btrfs_transaction_cachep;
60struct kmem_cache *btrfs_bit_radix_cachep;
61struct kmem_cache *btrfs_path_cachep;
62
63#define S_SHIFT 12
64static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
65 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
66 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
67 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
68 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
69 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
70 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
71 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
72};
73
Chris Masonb888db22007-08-27 16:49:44 -040074static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
75{
76 struct btrfs_root *root = BTRFS_I(inode)->root;
77 struct btrfs_trans_handle *trans;
78 struct btrfs_key ins;
79 u64 alloc_hint = 0;
Chris Masondb945352007-10-15 16:15:53 -040080 u64 num_bytes;
Chris Masonb888db22007-08-27 16:49:44 -040081 int ret;
Chris Masondb945352007-10-15 16:15:53 -040082 u64 blocksize = root->sectorsize;
Chris Masonb888db22007-08-27 16:49:44 -040083
84 mutex_lock(&root->fs_info->fs_mutex);
85 trans = btrfs_start_transaction(root, 1);
86 btrfs_set_trans_block_group(trans, inode);
87 BUG_ON(!trans);
Chris Masondb945352007-10-15 16:15:53 -040088 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonb888db22007-08-27 16:49:44 -040089 ret = btrfs_drop_extents(trans, root, inode,
Chris Mason3326d1b2007-10-15 16:18:25 -040090 start, start + num_bytes, start, &alloc_hint);
Chris Masondb945352007-10-15 16:15:53 -040091
92 ret = btrfs_alloc_extent(trans, root, inode->i_ino, num_bytes, 0,
Chris Masonb888db22007-08-27 16:49:44 -040093 alloc_hint, (u64)-1, &ins, 1);
94 if (ret) {
95 WARN_ON(1);
96 goto out;
97 }
98 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
99 start, ins.objectid, ins.offset,
100 ins.offset);
101out:
102 btrfs_end_transaction(trans, root);
103 mutex_unlock(&root->fs_info->fs_mutex);
104 return ret;
105}
106
Chris Mason07157aa2007-08-30 08:50:51 -0400107int btrfs_writepage_io_hook(struct page *page, u64 start, u64 end)
108{
109 struct inode *inode = page->mapping->host;
110 struct btrfs_root *root = BTRFS_I(inode)->root;
111 struct btrfs_trans_handle *trans;
112 char *kaddr;
113 int ret;
114 u64 page_start = page->index << PAGE_CACHE_SHIFT;
115 size_t offset = start - page_start;
116
117 mutex_lock(&root->fs_info->fs_mutex);
118 trans = btrfs_start_transaction(root, 1);
119 btrfs_set_trans_block_group(trans, inode);
120 kaddr = kmap(page);
Chris Masonf578d4b2007-10-25 15:42:56 -0400121 btrfs_csum_file_block(trans, root, inode, inode->i_ino,
Chris Mason07157aa2007-08-30 08:50:51 -0400122 start, kaddr + offset, end - start + 1);
123 kunmap(page);
124 ret = btrfs_end_transaction(trans, root);
125 BUG_ON(ret);
126 mutex_unlock(&root->fs_info->fs_mutex);
127 return ret;
128}
129
130int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
131{
132 int ret = 0;
133 struct inode *inode = page->mapping->host;
134 struct btrfs_root *root = BTRFS_I(inode)->root;
135 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
136 struct btrfs_csum_item *item;
137 struct btrfs_path *path = NULL;
Chris Masonff79f812007-10-15 16:22:25 -0400138 u32 csum;
Chris Mason07157aa2007-08-30 08:50:51 -0400139
140 mutex_lock(&root->fs_info->fs_mutex);
141 path = btrfs_alloc_path();
142 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
143 if (IS_ERR(item)) {
144 ret = PTR_ERR(item);
145 /* a csum that isn't present is a preallocated region. */
146 if (ret == -ENOENT || ret == -EFBIG)
147 ret = 0;
Chris Masonff79f812007-10-15 16:22:25 -0400148 csum = 0;
Chris Mason07157aa2007-08-30 08:50:51 -0400149 goto out;
150 }
Chris Masonff79f812007-10-15 16:22:25 -0400151 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
152 BTRFS_CRC32_SIZE);
153 set_state_private(em_tree, start, csum);
Chris Mason07157aa2007-08-30 08:50:51 -0400154out:
155 if (path)
156 btrfs_free_path(path);
157 mutex_unlock(&root->fs_info->fs_mutex);
158 return ret;
159}
160
161int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end)
162{
Chris Mason07157aa2007-08-30 08:50:51 -0400163 size_t offset = start - (page->index << PAGE_CACHE_SHIFT);
164 struct inode *inode = page->mapping->host;
Chris Mason07157aa2007-08-30 08:50:51 -0400165 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
166 char *kaddr;
167 u64 private;
168 int ret;
Chris Masonff79f812007-10-15 16:22:25 -0400169 struct btrfs_root *root = BTRFS_I(inode)->root;
170 u32 csum = ~(u32)0;
Jens Axboebbf0d002007-10-19 09:23:07 -0400171 unsigned long flags;
Chris Mason07157aa2007-08-30 08:50:51 -0400172
173 ret = get_state_private(em_tree, start, &private);
Jens Axboebbf0d002007-10-19 09:23:07 -0400174 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400175 kaddr = kmap_atomic(page, KM_IRQ0);
176 if (ret) {
177 goto zeroit;
178 }
Chris Masonff79f812007-10-15 16:22:25 -0400179 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
180 btrfs_csum_final(csum, (char *)&csum);
181 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -0400182 goto zeroit;
183 }
184 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400185 local_irq_restore(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400186 return 0;
187
188zeroit:
189 printk("btrfs csum failed ino %lu off %llu\n",
190 page->mapping->host->i_ino, (unsigned long long)start);
Chris Masondb945352007-10-15 16:15:53 -0400191 memset(kaddr + offset, 1, end - start + 1);
192 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -0400193 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400194 local_irq_restore(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400195 return 0;
196}
Chris Masonb888db22007-08-27 16:49:44 -0400197
Chris Mason39279cc2007-06-12 06:35:45 -0400198void btrfs_read_locked_inode(struct inode *inode)
199{
200 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400201 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400202 struct btrfs_inode_item *inode_item;
Chris Mason5f39d392007-10-15 16:14:19 -0400203 struct btrfs_inode_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -0400204 struct btrfs_root *root = BTRFS_I(inode)->root;
205 struct btrfs_key location;
206 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -0400207 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -0400208 int ret;
209
210 path = btrfs_alloc_path();
211 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400212 mutex_lock(&root->fs_info->fs_mutex);
213
214 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
215 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400216 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -0400217 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -0400218
Chris Mason5f39d392007-10-15 16:14:19 -0400219 leaf = path->nodes[0];
220 inode_item = btrfs_item_ptr(leaf, path->slots[0],
221 struct btrfs_inode_item);
222
223 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
224 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
225 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
226 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
227 inode->i_size = btrfs_inode_size(leaf, inode_item);
228
229 tspec = btrfs_inode_atime(inode_item);
230 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
231 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
232
233 tspec = btrfs_inode_mtime(inode_item);
234 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
235 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
236
237 tspec = btrfs_inode_ctime(inode_item);
238 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
239 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
240
241 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
242 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik618e21d2007-07-11 10:18:17 -0400243 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400244 rdev = btrfs_inode_rdev(leaf, inode_item);
245
246 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -0400247 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
248 alloc_group_block);
249
250 btrfs_free_path(path);
251 inode_item = NULL;
252
253 mutex_unlock(&root->fs_info->fs_mutex);
254
255 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -0400256 case S_IFREG:
257 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason07157aa2007-08-30 08:50:51 -0400258 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -0400259 inode->i_fop = &btrfs_file_operations;
260 inode->i_op = &btrfs_file_inode_operations;
261 break;
262 case S_IFDIR:
263 inode->i_fop = &btrfs_dir_file_operations;
264 if (root == root->fs_info->tree_root)
265 inode->i_op = &btrfs_dir_ro_inode_operations;
266 else
267 inode->i_op = &btrfs_dir_inode_operations;
268 break;
269 case S_IFLNK:
270 inode->i_op = &btrfs_symlink_inode_operations;
271 inode->i_mapping->a_ops = &btrfs_symlink_aops;
272 break;
Josef Bacik618e21d2007-07-11 10:18:17 -0400273 default:
274 init_special_inode(inode, inode->i_mode, rdev);
275 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400276 }
277 return;
278
279make_bad:
280 btrfs_release_path(root, path);
281 btrfs_free_path(path);
282 mutex_unlock(&root->fs_info->fs_mutex);
283 make_bad_inode(inode);
284}
285
Chris Mason5f39d392007-10-15 16:14:19 -0400286static void fill_inode_item(struct extent_buffer *leaf,
287 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -0400288 struct inode *inode)
289{
Chris Mason5f39d392007-10-15 16:14:19 -0400290 btrfs_set_inode_uid(leaf, item, inode->i_uid);
291 btrfs_set_inode_gid(leaf, item, inode->i_gid);
292 btrfs_set_inode_size(leaf, item, inode->i_size);
293 btrfs_set_inode_mode(leaf, item, inode->i_mode);
294 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
295
296 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
297 inode->i_atime.tv_sec);
298 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
299 inode->i_atime.tv_nsec);
300
301 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
302 inode->i_mtime.tv_sec);
303 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
304 inode->i_mtime.tv_nsec);
305
306 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
307 inode->i_ctime.tv_sec);
308 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
309 inode->i_ctime.tv_nsec);
310
311 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
312 btrfs_set_inode_generation(leaf, item, inode->i_generation);
313 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
314 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -0400315 BTRFS_I(inode)->block_group->key.objectid);
316}
317
Chris Masona52d9a82007-08-27 16:49:44 -0400318int btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -0400319 struct btrfs_root *root,
320 struct inode *inode)
321{
322 struct btrfs_inode_item *inode_item;
323 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400324 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400325 int ret;
326
327 path = btrfs_alloc_path();
328 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400329 ret = btrfs_lookup_inode(trans, root, path,
330 &BTRFS_I(inode)->location, 1);
331 if (ret) {
332 if (ret > 0)
333 ret = -ENOENT;
334 goto failed;
335 }
336
Chris Mason5f39d392007-10-15 16:14:19 -0400337 leaf = path->nodes[0];
338 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400339 struct btrfs_inode_item);
340
Chris Mason5f39d392007-10-15 16:14:19 -0400341 fill_inode_item(leaf, inode_item, inode);
342 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400343 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400344 ret = 0;
345failed:
346 btrfs_release_path(root, path);
347 btrfs_free_path(path);
348 return ret;
349}
350
351
352static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
353 struct btrfs_root *root,
354 struct inode *dir,
355 struct dentry *dentry)
356{
357 struct btrfs_path *path;
358 const char *name = dentry->d_name.name;
359 int name_len = dentry->d_name.len;
360 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400361 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400362 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -0400363 struct btrfs_key key;
Chris Mason39279cc2007-06-12 06:35:45 -0400364
365 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400366 if (!path) {
367 ret = -ENOMEM;
368 goto err;
369 }
370
Chris Mason39279cc2007-06-12 06:35:45 -0400371 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
372 name, name_len, -1);
373 if (IS_ERR(di)) {
374 ret = PTR_ERR(di);
375 goto err;
376 }
377 if (!di) {
378 ret = -ENOENT;
379 goto err;
380 }
Chris Mason5f39d392007-10-15 16:14:19 -0400381 leaf = path->nodes[0];
382 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -0400383 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -0400384 if (ret)
385 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -0400386 btrfs_release_path(root, path);
387
388 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Chris Mason5f39d392007-10-15 16:14:19 -0400389 key.objectid, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -0400390 if (IS_ERR(di)) {
391 ret = PTR_ERR(di);
392 goto err;
393 }
394 if (!di) {
395 ret = -ENOENT;
396 goto err;
397 }
398 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason39279cc2007-06-12 06:35:45 -0400399
400 dentry->d_inode->i_ctime = dir->i_ctime;
401err:
402 btrfs_free_path(path);
403 if (!ret) {
404 dir->i_size -= name_len * 2;
Chris Mason79c44582007-06-25 10:09:33 -0400405 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -0400406 btrfs_update_inode(trans, root, dir);
407 drop_nlink(dentry->d_inode);
Chris Mason54aa1f42007-06-22 14:16:25 -0400408 ret = btrfs_update_inode(trans, root, dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400409 dir->i_sb->s_dirt = 1;
410 }
411 return ret;
412}
413
414static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
415{
416 struct btrfs_root *root;
417 struct btrfs_trans_handle *trans;
418 int ret;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400419 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -0400420
421 root = BTRFS_I(dir)->root;
422 mutex_lock(&root->fs_info->fs_mutex);
423 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400424
Chris Mason39279cc2007-06-12 06:35:45 -0400425 btrfs_set_trans_block_group(trans, dir);
426 ret = btrfs_unlink_trans(trans, root, dir, dentry);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400427 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -0400428
Chris Mason39279cc2007-06-12 06:35:45 -0400429 btrfs_end_transaction(trans, root);
430 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400431 btrfs_btree_balance_dirty(root, nr);
Chris Mason5f39d392007-10-15 16:14:19 -0400432
Chris Mason39279cc2007-06-12 06:35:45 -0400433 return ret;
434}
435
436static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
437{
438 struct inode *inode = dentry->d_inode;
439 int err;
440 int ret;
441 struct btrfs_root *root = BTRFS_I(dir)->root;
442 struct btrfs_path *path;
443 struct btrfs_key key;
444 struct btrfs_trans_handle *trans;
445 struct btrfs_key found_key;
446 int found_type;
Chris Mason5f39d392007-10-15 16:14:19 -0400447 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400448 char *goodnames = "..";
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400449 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -0400450
Yan134d4512007-10-25 15:49:25 -0400451 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
452 return -ENOTEMPTY;
453
Chris Mason39279cc2007-06-12 06:35:45 -0400454 path = btrfs_alloc_path();
455 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400456 mutex_lock(&root->fs_info->fs_mutex);
457 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400458
Chris Mason39279cc2007-06-12 06:35:45 -0400459 btrfs_set_trans_block_group(trans, dir);
460 key.objectid = inode->i_ino;
461 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -0400462 key.type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -0400463 while(1) {
464 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
465 if (ret < 0) {
466 err = ret;
467 goto out;
468 }
469 BUG_ON(ret == 0);
470 if (path->slots[0] == 0) {
471 err = -ENOENT;
472 goto out;
473 }
474 path->slots[0]--;
Chris Mason5f39d392007-10-15 16:14:19 -0400475 leaf = path->nodes[0];
476 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason39279cc2007-06-12 06:35:45 -0400477 found_type = btrfs_key_type(&found_key);
478 if (found_key.objectid != inode->i_ino) {
479 err = -ENOENT;
480 goto out;
481 }
482 if ((found_type != BTRFS_DIR_ITEM_KEY &&
483 found_type != BTRFS_DIR_INDEX_KEY) ||
484 (!btrfs_match_dir_item_name(root, path, goodnames, 2) &&
485 !btrfs_match_dir_item_name(root, path, goodnames, 1))) {
486 err = -ENOTEMPTY;
487 goto out;
488 }
489 ret = btrfs_del_item(trans, root, path);
490 BUG_ON(ret);
491
492 if (found_type == BTRFS_DIR_ITEM_KEY && found_key.offset == 1)
493 break;
494 btrfs_release_path(root, path);
495 }
496 ret = 0;
497 btrfs_release_path(root, path);
498
499 /* now the directory is empty */
500 err = btrfs_unlink_trans(trans, root, dir, dentry);
501 if (!err) {
502 inode->i_size = 0;
503 }
504out:
505 btrfs_release_path(root, path);
506 btrfs_free_path(path);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400507 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -0400508 ret = btrfs_end_transaction(trans, root);
Yan134d4512007-10-25 15:49:25 -0400509 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400510 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -0400511 if (ret && !err)
512 err = ret;
513 return err;
514}
515
516static int btrfs_free_inode(struct btrfs_trans_handle *trans,
517 struct btrfs_root *root,
518 struct inode *inode)
519{
520 struct btrfs_path *path;
521 int ret;
522
523 clear_inode(inode);
524
525 path = btrfs_alloc_path();
526 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400527 ret = btrfs_lookup_inode(trans, root, path,
528 &BTRFS_I(inode)->location, -1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400529 if (ret > 0)
530 ret = -ENOENT;
531 if (!ret)
532 ret = btrfs_del_item(trans, root, path);
Chris Mason39279cc2007-06-12 06:35:45 -0400533 btrfs_free_path(path);
534 return ret;
535}
536
537/*
Chris Mason39279cc2007-06-12 06:35:45 -0400538 * this can truncate away extent items, csum items and directory items.
539 * It starts at a high offset and removes keys until it can't find
540 * any higher than i_size.
541 *
542 * csum items that cross the new i_size are truncated to the new size
543 * as well.
544 */
545static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
546 struct btrfs_root *root,
547 struct inode *inode)
548{
549 int ret;
550 struct btrfs_path *path;
551 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400552 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -0400553 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -0400554 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400555 struct btrfs_file_extent_item *fi;
556 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -0400557 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400558 u64 item_end = 0;
559 int found_extent;
560 int del_item;
561
Chris Masona52d9a82007-08-27 16:49:44 -0400562 btrfs_drop_extent_cache(inode, inode->i_size, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -0400563 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -0400564 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400565 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -0400566
Chris Mason39279cc2007-06-12 06:35:45 -0400567 /* FIXME, add redo link to tree so we don't leak on crash */
568 key.objectid = inode->i_ino;
569 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -0400570 key.type = (u8)-1;
571
Chris Mason39279cc2007-06-12 06:35:45 -0400572 while(1) {
573 btrfs_init_path(path);
574 fi = NULL;
575 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
576 if (ret < 0) {
577 goto error;
578 }
579 if (ret > 0) {
580 BUG_ON(path->slots[0] == 0);
581 path->slots[0]--;
582 }
Chris Mason5f39d392007-10-15 16:14:19 -0400583 leaf = path->nodes[0];
584 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
585 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -0400586
Chris Mason5f39d392007-10-15 16:14:19 -0400587 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -0400588 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400589
Chris Mason39279cc2007-06-12 06:35:45 -0400590 if (found_type != BTRFS_CSUM_ITEM_KEY &&
591 found_type != BTRFS_DIR_ITEM_KEY &&
592 found_type != BTRFS_DIR_INDEX_KEY &&
593 found_type != BTRFS_EXTENT_DATA_KEY)
594 break;
595
Chris Mason5f39d392007-10-15 16:14:19 -0400596 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -0400597 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -0400598 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400599 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400600 if (btrfs_file_extent_type(leaf, fi) !=
Chris Mason39279cc2007-06-12 06:35:45 -0400601 BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -0400602 item_end +=
Chris Masondb945352007-10-15 16:15:53 -0400603 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400604 }
605 }
606 if (found_type == BTRFS_CSUM_ITEM_KEY) {
607 ret = btrfs_csum_truncate(trans, root, path,
608 inode->i_size);
609 BUG_ON(ret);
610 }
611 if (item_end < inode->i_size) {
Chris Masonb888db22007-08-27 16:49:44 -0400612 if (found_type == BTRFS_DIR_ITEM_KEY) {
613 found_type = BTRFS_INODE_ITEM_KEY;
614 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
615 found_type = BTRFS_CSUM_ITEM_KEY;
616 } else if (found_type) {
617 found_type--;
618 } else {
619 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400620 }
Yana61721d2007-09-17 11:08:38 -0400621 btrfs_set_key_type(&key, found_type);
Yan65555a02007-10-25 15:42:57 -0400622 btrfs_release_path(root, path);
Chris Masonb888db22007-08-27 16:49:44 -0400623 continue;
Chris Mason39279cc2007-06-12 06:35:45 -0400624 }
Chris Mason5f39d392007-10-15 16:14:19 -0400625 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -0400626 del_item = 1;
627 else
628 del_item = 0;
629 found_extent = 0;
630
631 /* FIXME, shrink the extent if the ref count is only 1 */
632 if (found_type == BTRFS_EXTENT_DATA_KEY &&
Chris Mason5f39d392007-10-15 16:14:19 -0400633 btrfs_file_extent_type(leaf, fi) !=
Chris Mason39279cc2007-06-12 06:35:45 -0400634 BTRFS_FILE_EXTENT_INLINE) {
635 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -0400636 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400637 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -0400638 u64 orig_num_bytes =
639 btrfs_file_extent_num_bytes(leaf, fi);
640 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -0400641 found_key.offset + root->sectorsize - 1;
Chris Masondb945352007-10-15 16:15:53 -0400642 btrfs_set_file_extent_num_bytes(leaf, fi,
643 extent_num_bytes);
644 num_dec = (orig_num_bytes -
645 extent_num_bytes) >> 9;
Yanbab9fb02007-09-17 11:13:11 -0400646 if (extent_start != 0) {
647 inode->i_blocks -= num_dec;
648 }
Chris Mason5f39d392007-10-15 16:14:19 -0400649 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -0400650 } else {
Chris Masondb945352007-10-15 16:15:53 -0400651 extent_num_bytes =
652 btrfs_file_extent_disk_num_bytes(leaf,
653 fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400654 /* FIXME blocksize != 4096 */
Chris Masondb945352007-10-15 16:15:53 -0400655 num_dec = btrfs_file_extent_num_bytes(leaf,
656 fi) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -0400657 if (extent_start != 0) {
658 found_extent = 1;
659 inode->i_blocks -= num_dec;
660 }
661 }
662 }
663 if (del_item) {
664 ret = btrfs_del_item(trans, root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400665 if (ret)
666 goto error;
Chris Mason39279cc2007-06-12 06:35:45 -0400667 } else {
668 break;
669 }
670 btrfs_release_path(root, path);
671 if (found_extent) {
672 ret = btrfs_free_extent(trans, root, extent_start,
Chris Masondb945352007-10-15 16:15:53 -0400673 extent_num_bytes, 0);
Chris Mason39279cc2007-06-12 06:35:45 -0400674 BUG_ON(ret);
675 }
676 }
677 ret = 0;
678error:
679 btrfs_release_path(root, path);
680 btrfs_free_path(path);
681 inode->i_sb->s_dirt = 1;
682 return ret;
683}
684
Chris Masonb888db22007-08-27 16:49:44 -0400685static int btrfs_cow_one_page(struct inode *inode, struct page *page,
Chris Masona52d9a82007-08-27 16:49:44 -0400686 size_t zero_start)
Chris Mason39279cc2007-06-12 06:35:45 -0400687{
Chris Mason39279cc2007-06-12 06:35:45 -0400688 char *kaddr;
689 int ret = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400690 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masona52d9a82007-08-27 16:49:44 -0400691 u64 page_start = page->index << PAGE_CACHE_SHIFT;
Chris Masonb888db22007-08-27 16:49:44 -0400692 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Mason39279cc2007-06-12 06:35:45 -0400693
Christoph Hellwigb3cfa352007-09-17 11:25:58 -0400694 set_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400695
Chris Masonb888db22007-08-27 16:49:44 -0400696 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
697 set_extent_delalloc(&BTRFS_I(inode)->extent_tree, page_start,
698 page_end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -0400699 if (zero_start != PAGE_CACHE_SIZE) {
Chris Masonb888db22007-08-27 16:49:44 -0400700 kaddr = kmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400701 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
702 flush_dcache_page(page);
Chris Masonb888db22007-08-27 16:49:44 -0400703 kunmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400704 }
Chris Masonb888db22007-08-27 16:49:44 -0400705 set_page_dirty(page);
706 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -0400707
Chris Masona52d9a82007-08-27 16:49:44 -0400708 return ret;
709}
710
711/*
712 * taken from block_truncate_page, but does cow as it zeros out
713 * any bytes left in the last page in the file.
714 */
715static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
716{
717 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -0400718 struct btrfs_root *root = BTRFS_I(inode)->root;
719 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -0400720 pgoff_t index = from >> PAGE_CACHE_SHIFT;
721 unsigned offset = from & (PAGE_CACHE_SIZE-1);
722 struct page *page;
723 int ret = 0;
724 u64 page_start;
725
726 if ((offset & (blocksize - 1)) == 0)
727 goto out;
728
Chris Masondb945352007-10-15 16:15:53 -0400729 down_read(&root->snap_sem);
Chris Masona52d9a82007-08-27 16:49:44 -0400730 ret = -ENOMEM;
731 page = grab_cache_page(mapping, index);
732 if (!page)
733 goto out;
734 if (!PageUptodate(page)) {
735 ret = btrfs_readpage(NULL, page);
736 lock_page(page);
737 if (!PageUptodate(page)) {
738 ret = -EIO;
739 goto out;
740 }
741 }
742 page_start = page->index << PAGE_CACHE_SHIFT;
743
Chris Masonb888db22007-08-27 16:49:44 -0400744 ret = btrfs_cow_one_page(inode, page, offset);
Chris Mason39279cc2007-06-12 06:35:45 -0400745
Chris Mason39279cc2007-06-12 06:35:45 -0400746 unlock_page(page);
747 page_cache_release(page);
Chris Mason011410b2007-09-10 19:58:36 -0400748 up_read(&BTRFS_I(inode)->root->snap_sem);
Chris Mason39279cc2007-06-12 06:35:45 -0400749out:
750 return ret;
751}
752
753static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
754{
755 struct inode *inode = dentry->d_inode;
756 int err;
757
758 err = inode_change_ok(inode, attr);
759 if (err)
760 return err;
761
762 if (S_ISREG(inode->i_mode) &&
763 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
764 struct btrfs_trans_handle *trans;
765 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason2bf5a722007-08-30 11:54:02 -0400766 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
767
Chris Mason5f39d392007-10-15 16:14:19 -0400768 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -0400769 u64 pos = (inode->i_size + mask) & ~mask;
Chris Mason2bf5a722007-08-30 11:54:02 -0400770 u64 block_end = attr->ia_size | mask;
Chris Mason39279cc2007-06-12 06:35:45 -0400771 u64 hole_size;
Chris Mason2bf5a722007-08-30 11:54:02 -0400772 u64 alloc_hint;
Chris Mason39279cc2007-06-12 06:35:45 -0400773
774 if (attr->ia_size <= pos)
775 goto out;
776
777 btrfs_truncate_page(inode->i_mapping, inode->i_size);
778
Chris Mason2bf5a722007-08-30 11:54:02 -0400779 lock_extent(em_tree, pos, block_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -0400780 hole_size = (attr->ia_size - pos + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -0400781
782 mutex_lock(&root->fs_info->fs_mutex);
783 trans = btrfs_start_transaction(root, 1);
784 btrfs_set_trans_block_group(trans, inode);
Chris Mason2bf5a722007-08-30 11:54:02 -0400785 err = btrfs_drop_extents(trans, root, inode,
Chris Mason3326d1b2007-10-15 16:18:25 -0400786 pos, pos + hole_size, pos,
787 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -0400788
Chris Mason39279cc2007-06-12 06:35:45 -0400789 err = btrfs_insert_file_extent(trans, root, inode->i_ino,
790 pos, 0, 0, hole_size);
Chris Mason39279cc2007-06-12 06:35:45 -0400791 btrfs_end_transaction(trans, root);
792 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason2bf5a722007-08-30 11:54:02 -0400793 unlock_extent(em_tree, pos, block_end, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -0400794 if (err)
795 return err;
Chris Mason39279cc2007-06-12 06:35:45 -0400796 }
797out:
798 err = inode_setattr(inode, attr);
799
800 return err;
801}
802void btrfs_delete_inode(struct inode *inode)
803{
804 struct btrfs_trans_handle *trans;
805 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400806 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -0400807 int ret;
808
809 truncate_inode_pages(&inode->i_data, 0);
810 if (is_bad_inode(inode)) {
811 goto no_delete;
812 }
Chris Mason5f39d392007-10-15 16:14:19 -0400813
Chris Mason39279cc2007-06-12 06:35:45 -0400814 inode->i_size = 0;
815 mutex_lock(&root->fs_info->fs_mutex);
816 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400817
Chris Mason39279cc2007-06-12 06:35:45 -0400818 btrfs_set_trans_block_group(trans, inode);
819 ret = btrfs_truncate_in_trans(trans, root, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -0400820 if (ret)
821 goto no_delete_lock;
822 ret = btrfs_free_inode(trans, root, inode);
823 if (ret)
824 goto no_delete_lock;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400825 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -0400826
Chris Mason39279cc2007-06-12 06:35:45 -0400827 btrfs_end_transaction(trans, root);
828 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400829 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -0400830 return;
Chris Mason54aa1f42007-06-22 14:16:25 -0400831
832no_delete_lock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400833 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -0400834 btrfs_end_transaction(trans, root);
835 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400836 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -0400837no_delete:
838 clear_inode(inode);
839}
840
841/*
842 * this returns the key found in the dir entry in the location pointer.
843 * If no dir entries were found, location->objectid is 0.
844 */
845static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
846 struct btrfs_key *location)
847{
848 const char *name = dentry->d_name.name;
849 int namelen = dentry->d_name.len;
850 struct btrfs_dir_item *di;
851 struct btrfs_path *path;
852 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -0400853 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400854
855 path = btrfs_alloc_path();
856 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400857 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
858 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -0400859 if (IS_ERR(di))
860 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -0400861 if (!di || IS_ERR(di)) {
862 location->objectid = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400863 goto out;
864 }
Chris Mason5f39d392007-10-15 16:14:19 -0400865 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -0400866out:
867 btrfs_release_path(root, path);
868 btrfs_free_path(path);
869 return ret;
870}
871
872/*
873 * when we hit a tree root in a directory, the btrfs part of the inode
874 * needs to be changed to reflect the root directory of the tree root. This
875 * is kind of like crossing a mount point.
876 */
877static int fixup_tree_root_location(struct btrfs_root *root,
878 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -0400879 struct btrfs_root **sub_root,
880 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -0400881{
882 struct btrfs_path *path;
883 struct btrfs_root_item *ri;
884
885 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
886 return 0;
887 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
888 return 0;
889
890 path = btrfs_alloc_path();
891 BUG_ON(!path);
892 mutex_lock(&root->fs_info->fs_mutex);
893
Josef Bacik58176a92007-08-29 15:47:34 -0400894 *sub_root = btrfs_read_fs_root(root->fs_info, location,
895 dentry->d_name.name,
896 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -0400897 if (IS_ERR(*sub_root))
898 return PTR_ERR(*sub_root);
899
900 ri = &(*sub_root)->root_item;
901 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -0400902 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
903 location->offset = 0;
904
905 btrfs_free_path(path);
906 mutex_unlock(&root->fs_info->fs_mutex);
907 return 0;
908}
909
910static int btrfs_init_locked_inode(struct inode *inode, void *p)
911{
912 struct btrfs_iget_args *args = p;
913 inode->i_ino = args->ino;
914 BTRFS_I(inode)->root = args->root;
Chris Masonb888db22007-08-27 16:49:44 -0400915 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
916 inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -0400917 return 0;
918}
919
920static int btrfs_find_actor(struct inode *inode, void *opaque)
921{
922 struct btrfs_iget_args *args = opaque;
923 return (args->ino == inode->i_ino &&
924 args->root == BTRFS_I(inode)->root);
925}
926
927struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
928 struct btrfs_root *root)
929{
930 struct inode *inode;
931 struct btrfs_iget_args args;
932 args.ino = objectid;
933 args.root = root;
934
935 inode = iget5_locked(s, objectid, btrfs_find_actor,
936 btrfs_init_locked_inode,
937 (void *)&args);
938 return inode;
939}
940
941static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
942 struct nameidata *nd)
943{
944 struct inode * inode;
945 struct btrfs_inode *bi = BTRFS_I(dir);
946 struct btrfs_root *root = bi->root;
947 struct btrfs_root *sub_root = root;
948 struct btrfs_key location;
949 int ret;
950
951 if (dentry->d_name.len > BTRFS_NAME_LEN)
952 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -0400953
Chris Mason39279cc2007-06-12 06:35:45 -0400954 mutex_lock(&root->fs_info->fs_mutex);
955 ret = btrfs_inode_by_name(dir, dentry, &location);
956 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason5f39d392007-10-15 16:14:19 -0400957
Chris Mason39279cc2007-06-12 06:35:45 -0400958 if (ret < 0)
959 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -0400960
Chris Mason39279cc2007-06-12 06:35:45 -0400961 inode = NULL;
962 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -0400963 ret = fixup_tree_root_location(root, &location, &sub_root,
964 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -0400965 if (ret < 0)
966 return ERR_PTR(ret);
967 if (ret > 0)
968 return ERR_PTR(-ENOENT);
969 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
970 sub_root);
971 if (!inode)
972 return ERR_PTR(-EACCES);
973 if (inode->i_state & I_NEW) {
974 /* the inode and parent dir are two different roots */
975 if (sub_root != root) {
976 igrab(inode);
977 sub_root->inode = inode;
978 }
979 BTRFS_I(inode)->root = sub_root;
980 memcpy(&BTRFS_I(inode)->location, &location,
981 sizeof(location));
982 btrfs_read_locked_inode(inode);
983 unlock_new_inode(inode);
984 }
985 }
986 return d_splice_alias(inode, dentry);
987}
988
Chris Mason39279cc2007-06-12 06:35:45 -0400989static unsigned char btrfs_filetype_table[] = {
990 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
991};
992
993static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
994{
995 struct inode *inode = filp->f_path.dentry->d_inode;
996 struct btrfs_root *root = BTRFS_I(inode)->root;
997 struct btrfs_item *item;
998 struct btrfs_dir_item *di;
999 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001000 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001001 struct btrfs_path *path;
1002 int ret;
1003 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001004 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001005 int slot;
1006 int advance;
1007 unsigned char d_type;
1008 int over = 0;
1009 u32 di_cur;
1010 u32 di_total;
1011 u32 di_len;
1012 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001013 char tmp_name[32];
1014 char *name_ptr;
1015 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001016
1017 /* FIXME, use a real flag for deciding about the key type */
1018 if (root->fs_info->tree_root == root)
1019 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001020
Chris Mason39279cc2007-06-12 06:35:45 -04001021 mutex_lock(&root->fs_info->fs_mutex);
1022 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04001023 btrfs_set_key_type(&key, key_type);
1024 key.offset = filp->f_pos;
Chris Mason5f39d392007-10-15 16:14:19 -04001025
Chris Mason39279cc2007-06-12 06:35:45 -04001026 path = btrfs_alloc_path();
Chris Mason2cc58cf2007-08-27 16:49:44 -04001027 path->reada = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04001028 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1029 if (ret < 0)
1030 goto err;
1031 advance = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001032 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001033 leaf = path->nodes[0];
1034 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001035 slot = path->slots[0];
1036 if (advance || slot >= nritems) {
1037 if (slot >= nritems -1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001038 ret = btrfs_next_leaf(root, path);
1039 if (ret)
1040 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001041 leaf = path->nodes[0];
1042 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001043 slot = path->slots[0];
1044 } else {
1045 slot++;
1046 path->slots[0]++;
1047 }
1048 }
1049 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001050 item = btrfs_item_nr(leaf, slot);
1051 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1052
1053 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04001054 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001055 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001056 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001057 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04001058 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001059
1060 filp->f_pos = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001061 advance = 1;
1062 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1063 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001064 di_total = btrfs_item_size(leaf, item);
Chris Mason39279cc2007-06-12 06:35:45 -04001065 while(di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04001066 struct btrfs_key location;
1067
1068 name_len = btrfs_dir_name_len(leaf, di);
1069 if (name_len < 32) {
1070 name_ptr = tmp_name;
1071 } else {
1072 name_ptr = kmalloc(name_len, GFP_NOFS);
1073 BUG_ON(!name_ptr);
1074 }
1075 read_extent_buffer(leaf, name_ptr,
1076 (unsigned long)(di + 1), name_len);
1077
1078 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1079 btrfs_dir_item_key_to_cpu(leaf, di, &location);
1080
1081 over = filldir(dirent, name_ptr, name_len,
1082 found_key.offset,
1083 location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001084 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04001085
1086 if (name_ptr != tmp_name)
1087 kfree(name_ptr);
1088
Chris Mason39279cc2007-06-12 06:35:45 -04001089 if (over)
1090 goto nopos;
Chris Mason5f39d392007-10-15 16:14:19 -04001091 di_len = btrfs_dir_name_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04001092 di_cur += di_len;
1093 di = (struct btrfs_dir_item *)((char *)di + di_len);
1094 }
1095 }
1096 filp->f_pos++;
1097nopos:
1098 ret = 0;
1099err:
1100 btrfs_release_path(root, path);
1101 btrfs_free_path(path);
1102 mutex_unlock(&root->fs_info->fs_mutex);
1103 return ret;
1104}
1105
1106int btrfs_write_inode(struct inode *inode, int wait)
1107{
1108 struct btrfs_root *root = BTRFS_I(inode)->root;
1109 struct btrfs_trans_handle *trans;
1110 int ret = 0;
1111
1112 if (wait) {
1113 mutex_lock(&root->fs_info->fs_mutex);
1114 trans = btrfs_start_transaction(root, 1);
1115 btrfs_set_trans_block_group(trans, inode);
1116 ret = btrfs_commit_transaction(trans, root);
1117 mutex_unlock(&root->fs_info->fs_mutex);
1118 }
1119 return ret;
1120}
1121
1122/*
Chris Mason54aa1f42007-06-22 14:16:25 -04001123 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04001124 * inode changes. But, it is most likely to find the inode in cache.
1125 * FIXME, needs more benchmarking...there are no reasons other than performance
1126 * to keep or drop this code.
1127 */
1128void btrfs_dirty_inode(struct inode *inode)
1129{
1130 struct btrfs_root *root = BTRFS_I(inode)->root;
1131 struct btrfs_trans_handle *trans;
1132
1133 mutex_lock(&root->fs_info->fs_mutex);
1134 trans = btrfs_start_transaction(root, 1);
1135 btrfs_set_trans_block_group(trans, inode);
1136 btrfs_update_inode(trans, root, inode);
1137 btrfs_end_transaction(trans, root);
1138 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001139}
1140
1141static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1142 struct btrfs_root *root,
1143 u64 objectid,
1144 struct btrfs_block_group_cache *group,
1145 int mode)
1146{
1147 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001148 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04001149 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04001150 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04001151 int ret;
1152 int owner;
1153
Chris Mason5f39d392007-10-15 16:14:19 -04001154 path = btrfs_alloc_path();
1155 BUG_ON(!path);
1156
Chris Mason39279cc2007-06-12 06:35:45 -04001157 inode = new_inode(root->fs_info->sb);
1158 if (!inode)
1159 return ERR_PTR(-ENOMEM);
1160
Chris Masonb888db22007-08-27 16:49:44 -04001161 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1162 inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001163 BTRFS_I(inode)->root = root;
Chris Masonb888db22007-08-27 16:49:44 -04001164
Chris Mason39279cc2007-06-12 06:35:45 -04001165 if (mode & S_IFDIR)
1166 owner = 0;
1167 else
1168 owner = 1;
1169 group = btrfs_find_block_group(root, group, 0, 0, owner);
1170 BTRFS_I(inode)->block_group = group;
1171
Chris Mason5f39d392007-10-15 16:14:19 -04001172 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
1173 if (ret)
1174 goto fail;
1175
Chris Mason39279cc2007-06-12 06:35:45 -04001176 inode->i_uid = current->fsuid;
1177 inode->i_gid = current->fsgid;
1178 inode->i_mode = mode;
1179 inode->i_ino = objectid;
1180 inode->i_blocks = 0;
1181 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04001182 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1183 struct btrfs_inode_item);
1184 fill_inode_item(path->nodes[0], inode_item, inode);
1185 btrfs_mark_buffer_dirty(path->nodes[0]);
1186 btrfs_free_path(path);
1187
Chris Mason39279cc2007-06-12 06:35:45 -04001188 location = &BTRFS_I(inode)->location;
1189 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04001190 location->offset = 0;
1191 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1192
Chris Mason39279cc2007-06-12 06:35:45 -04001193 insert_inode_hash(inode);
1194 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001195fail:
1196 btrfs_free_path(path);
1197 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04001198}
1199
1200static inline u8 btrfs_inode_type(struct inode *inode)
1201{
1202 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1203}
1204
1205static int btrfs_add_link(struct btrfs_trans_handle *trans,
1206 struct dentry *dentry, struct inode *inode)
1207{
1208 int ret;
1209 struct btrfs_key key;
1210 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Chris Mason79c44582007-06-25 10:09:33 -04001211 struct inode *parent_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001212
Chris Mason39279cc2007-06-12 06:35:45 -04001213 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04001214 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1215 key.offset = 0;
1216
1217 ret = btrfs_insert_dir_item(trans, root,
1218 dentry->d_name.name, dentry->d_name.len,
1219 dentry->d_parent->d_inode->i_ino,
1220 &key, btrfs_inode_type(inode));
1221 if (ret == 0) {
Chris Mason79c44582007-06-25 10:09:33 -04001222 parent_inode = dentry->d_parent->d_inode;
1223 parent_inode->i_size += dentry->d_name.len * 2;
1224 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001225 ret = btrfs_update_inode(trans, root,
1226 dentry->d_parent->d_inode);
1227 }
1228 return ret;
1229}
1230
1231static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1232 struct dentry *dentry, struct inode *inode)
1233{
1234 int err = btrfs_add_link(trans, dentry, inode);
1235 if (!err) {
1236 d_instantiate(dentry, inode);
1237 return 0;
1238 }
1239 if (err > 0)
1240 err = -EEXIST;
1241 return err;
1242}
1243
Josef Bacik618e21d2007-07-11 10:18:17 -04001244static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1245 int mode, dev_t rdev)
1246{
1247 struct btrfs_trans_handle *trans;
1248 struct btrfs_root *root = BTRFS_I(dir)->root;
1249 struct inode *inode;
1250 int err;
1251 int drop_inode = 0;
1252 u64 objectid;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001253 unsigned long nr;
Josef Bacik618e21d2007-07-11 10:18:17 -04001254
1255 if (!new_valid_dev(rdev))
1256 return -EINVAL;
1257
1258 mutex_lock(&root->fs_info->fs_mutex);
1259 trans = btrfs_start_transaction(root, 1);
1260 btrfs_set_trans_block_group(trans, dir);
1261
1262 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1263 if (err) {
1264 err = -ENOSPC;
1265 goto out_unlock;
1266 }
1267
1268 inode = btrfs_new_inode(trans, root, objectid,
1269 BTRFS_I(dir)->block_group, mode);
1270 err = PTR_ERR(inode);
1271 if (IS_ERR(inode))
1272 goto out_unlock;
1273
1274 btrfs_set_trans_block_group(trans, inode);
1275 err = btrfs_add_nondir(trans, dentry, inode);
1276 if (err)
1277 drop_inode = 1;
1278 else {
1279 inode->i_op = &btrfs_special_inode_operations;
1280 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04001281 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04001282 }
1283 dir->i_sb->s_dirt = 1;
1284 btrfs_update_inode_block_group(trans, inode);
1285 btrfs_update_inode_block_group(trans, dir);
1286out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001287 nr = trans->blocks_used;
Josef Bacik618e21d2007-07-11 10:18:17 -04001288 btrfs_end_transaction(trans, root);
1289 mutex_unlock(&root->fs_info->fs_mutex);
1290
1291 if (drop_inode) {
1292 inode_dec_link_count(inode);
1293 iput(inode);
1294 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001295 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04001296 return err;
1297}
1298
Chris Mason39279cc2007-06-12 06:35:45 -04001299static int btrfs_create(struct inode *dir, struct dentry *dentry,
1300 int mode, struct nameidata *nd)
1301{
1302 struct btrfs_trans_handle *trans;
1303 struct btrfs_root *root = BTRFS_I(dir)->root;
1304 struct inode *inode;
1305 int err;
1306 int drop_inode = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001307 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001308 u64 objectid;
1309
1310 mutex_lock(&root->fs_info->fs_mutex);
1311 trans = btrfs_start_transaction(root, 1);
1312 btrfs_set_trans_block_group(trans, dir);
1313
1314 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1315 if (err) {
1316 err = -ENOSPC;
1317 goto out_unlock;
1318 }
1319
1320 inode = btrfs_new_inode(trans, root, objectid,
1321 BTRFS_I(dir)->block_group, mode);
1322 err = PTR_ERR(inode);
1323 if (IS_ERR(inode))
1324 goto out_unlock;
1325
1326 btrfs_set_trans_block_group(trans, inode);
1327 err = btrfs_add_nondir(trans, dentry, inode);
1328 if (err)
1329 drop_inode = 1;
1330 else {
1331 inode->i_mapping->a_ops = &btrfs_aops;
1332 inode->i_fop = &btrfs_file_operations;
1333 inode->i_op = &btrfs_file_inode_operations;
Chris Masona52d9a82007-08-27 16:49:44 -04001334 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1335 inode->i_mapping, GFP_NOFS);
Chris Mason07157aa2007-08-30 08:50:51 -04001336 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04001337 }
1338 dir->i_sb->s_dirt = 1;
1339 btrfs_update_inode_block_group(trans, inode);
1340 btrfs_update_inode_block_group(trans, dir);
1341out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001342 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001343 btrfs_end_transaction(trans, root);
1344 mutex_unlock(&root->fs_info->fs_mutex);
1345
1346 if (drop_inode) {
1347 inode_dec_link_count(inode);
1348 iput(inode);
1349 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001350 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001351 return err;
1352}
1353
1354static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1355 struct dentry *dentry)
1356{
1357 struct btrfs_trans_handle *trans;
1358 struct btrfs_root *root = BTRFS_I(dir)->root;
1359 struct inode *inode = old_dentry->d_inode;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001360 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001361 int err;
1362 int drop_inode = 0;
1363
1364 if (inode->i_nlink == 0)
1365 return -ENOENT;
1366
1367 inc_nlink(inode);
1368 mutex_lock(&root->fs_info->fs_mutex);
1369 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001370
Chris Mason39279cc2007-06-12 06:35:45 -04001371 btrfs_set_trans_block_group(trans, dir);
1372 atomic_inc(&inode->i_count);
1373 err = btrfs_add_nondir(trans, dentry, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001374
Chris Mason39279cc2007-06-12 06:35:45 -04001375 if (err)
1376 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001377
Chris Mason39279cc2007-06-12 06:35:45 -04001378 dir->i_sb->s_dirt = 1;
1379 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04001380 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001381
Chris Mason54aa1f42007-06-22 14:16:25 -04001382 if (err)
1383 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001384
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001385 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001386 btrfs_end_transaction(trans, root);
1387 mutex_unlock(&root->fs_info->fs_mutex);
1388
1389 if (drop_inode) {
1390 inode_dec_link_count(inode);
1391 iput(inode);
1392 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001393 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001394 return err;
1395}
1396
1397static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
1398 struct btrfs_root *root,
1399 u64 objectid, u64 dirid)
1400{
1401 int ret;
1402 char buf[2];
1403 struct btrfs_key key;
1404
1405 buf[0] = '.';
1406 buf[1] = '.';
1407
1408 key.objectid = objectid;
1409 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001410 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1411
1412 ret = btrfs_insert_dir_item(trans, root, buf, 1, objectid,
1413 &key, BTRFS_FT_DIR);
1414 if (ret)
1415 goto error;
Chris Mason5f39d392007-10-15 16:14:19 -04001416
Chris Mason39279cc2007-06-12 06:35:45 -04001417 key.objectid = dirid;
1418 ret = btrfs_insert_dir_item(trans, root, buf, 2, objectid,
1419 &key, BTRFS_FT_DIR);
1420 if (ret)
1421 goto error;
1422error:
1423 return ret;
1424}
1425
1426static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1427{
1428 struct inode *inode;
1429 struct btrfs_trans_handle *trans;
1430 struct btrfs_root *root = BTRFS_I(dir)->root;
1431 int err = 0;
1432 int drop_on_err = 0;
1433 u64 objectid;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001434 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001435
1436 mutex_lock(&root->fs_info->fs_mutex);
1437 trans = btrfs_start_transaction(root, 1);
1438 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04001439
Chris Mason39279cc2007-06-12 06:35:45 -04001440 if (IS_ERR(trans)) {
1441 err = PTR_ERR(trans);
1442 goto out_unlock;
1443 }
1444
1445 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1446 if (err) {
1447 err = -ENOSPC;
1448 goto out_unlock;
1449 }
1450
1451 inode = btrfs_new_inode(trans, root, objectid,
1452 BTRFS_I(dir)->block_group, S_IFDIR | mode);
1453 if (IS_ERR(inode)) {
1454 err = PTR_ERR(inode);
1455 goto out_fail;
1456 }
Chris Mason5f39d392007-10-15 16:14:19 -04001457
Chris Mason39279cc2007-06-12 06:35:45 -04001458 drop_on_err = 1;
1459 inode->i_op = &btrfs_dir_inode_operations;
1460 inode->i_fop = &btrfs_dir_file_operations;
1461 btrfs_set_trans_block_group(trans, inode);
1462
1463 err = btrfs_make_empty_dir(trans, root, inode->i_ino, dir->i_ino);
1464 if (err)
1465 goto out_fail;
1466
1467 inode->i_size = 6;
1468 err = btrfs_update_inode(trans, root, inode);
1469 if (err)
1470 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04001471
Chris Mason39279cc2007-06-12 06:35:45 -04001472 err = btrfs_add_link(trans, dentry, inode);
1473 if (err)
1474 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04001475
Chris Mason39279cc2007-06-12 06:35:45 -04001476 d_instantiate(dentry, inode);
1477 drop_on_err = 0;
1478 dir->i_sb->s_dirt = 1;
1479 btrfs_update_inode_block_group(trans, inode);
1480 btrfs_update_inode_block_group(trans, dir);
1481
1482out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001483 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001484 btrfs_end_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04001485
Chris Mason39279cc2007-06-12 06:35:45 -04001486out_unlock:
1487 mutex_unlock(&root->fs_info->fs_mutex);
1488 if (drop_on_err)
1489 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001490 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001491 return err;
1492}
1493
Chris Masona52d9a82007-08-27 16:49:44 -04001494struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
1495 size_t page_offset, u64 start, u64 end,
1496 int create)
1497{
1498 int ret;
1499 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04001500 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04001501 u64 extent_start = 0;
1502 u64 extent_end = 0;
1503 u64 objectid = inode->i_ino;
1504 u32 found_type;
1505 int failed_insert = 0;
1506 struct btrfs_path *path;
1507 struct btrfs_root *root = BTRFS_I(inode)->root;
1508 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04001509 struct extent_buffer *leaf;
1510 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04001511 struct extent_map *em = NULL;
1512 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1513 struct btrfs_trans_handle *trans = NULL;
1514
1515 path = btrfs_alloc_path();
1516 BUG_ON(!path);
1517 mutex_lock(&root->fs_info->fs_mutex);
1518
1519again:
1520 em = lookup_extent_mapping(em_tree, start, end);
1521 if (em) {
1522 goto out;
1523 }
1524 if (!em) {
1525 em = alloc_extent_map(GFP_NOFS);
1526 if (!em) {
1527 err = -ENOMEM;
1528 goto out;
1529 }
Chris Mason5f39d392007-10-15 16:14:19 -04001530 em->start = EXTENT_MAP_HOLE;
1531 em->end = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001532 }
1533 em->bdev = inode->i_sb->s_bdev;
1534 ret = btrfs_lookup_file_extent(NULL, root, path,
1535 objectid, start, 0);
1536 if (ret < 0) {
1537 err = ret;
1538 goto out;
1539 }
1540
1541 if (ret != 0) {
1542 if (path->slots[0] == 0)
1543 goto not_found;
1544 path->slots[0]--;
1545 }
1546
Chris Mason5f39d392007-10-15 16:14:19 -04001547 leaf = path->nodes[0];
1548 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04001549 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04001550 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04001551 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1552 found_type = btrfs_key_type(&found_key);
1553 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04001554 found_type != BTRFS_EXTENT_DATA_KEY) {
1555 goto not_found;
1556 }
1557
Chris Mason5f39d392007-10-15 16:14:19 -04001558 found_type = btrfs_file_extent_type(leaf, item);
1559 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04001560 if (found_type == BTRFS_FILE_EXTENT_REG) {
1561 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04001562 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04001563 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04001564 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04001565 em->start = start;
1566 if (start < extent_start) {
Chris Masonb888db22007-08-27 16:49:44 -04001567 if (end < extent_start)
1568 goto not_found;
Chris Masona52d9a82007-08-27 16:49:44 -04001569 em->end = extent_end - 1;
1570 } else {
1571 em->end = end;
1572 }
1573 goto not_found_em;
1574 }
Chris Masondb945352007-10-15 16:15:53 -04001575 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
1576 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04001577 em->start = extent_start;
1578 em->end = extent_end - 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001579 em->block_start = EXTENT_MAP_HOLE;
1580 em->block_end = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001581 goto insert;
1582 }
Chris Masondb945352007-10-15 16:15:53 -04001583 bytenr += btrfs_file_extent_offset(leaf, item);
1584 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04001585 em->block_end = em->block_start +
Chris Masondb945352007-10-15 16:15:53 -04001586 btrfs_file_extent_num_bytes(leaf, item) - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04001587 em->start = extent_start;
1588 em->end = extent_end - 1;
1589 goto insert;
1590 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04001591 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04001592 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04001593 size_t size;
1594 size_t extent_offset;
1595 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04001596
Chris Mason5f39d392007-10-15 16:14:19 -04001597 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
1598 path->slots[0]));
1599
Chris Masondb945352007-10-15 16:15:53 -04001600 extent_end = (extent_start + size) |
1601 ((u64)root->sectorsize - 1);
Chris Masonb888db22007-08-27 16:49:44 -04001602 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04001603 em->start = start;
1604 if (start < extent_start) {
Chris Masonb888db22007-08-27 16:49:44 -04001605 if (end < extent_start)
1606 goto not_found;
Chris Mason50b78c22007-09-20 14:14:42 -04001607 em->end = extent_end;
Chris Masona52d9a82007-08-27 16:49:44 -04001608 } else {
1609 em->end = end;
1610 }
1611 goto not_found_em;
1612 }
Chris Mason5f39d392007-10-15 16:14:19 -04001613
Chris Mason3326d1b2007-10-15 16:18:25 -04001614 extent_offset = (page->index << PAGE_CACHE_SHIFT) -
1615 extent_start;
1616 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
1617 map = kmap(page);
Jens Axboeae2f5412007-10-19 09:22:59 -04001618 copy_size = min_t(u64, PAGE_CACHE_SIZE - page_offset,
Chris Mason3326d1b2007-10-15 16:18:25 -04001619 size - extent_offset);
1620
Chris Masona52d9a82007-08-27 16:49:44 -04001621 em->block_start = EXTENT_MAP_INLINE;
1622 em->block_end = EXTENT_MAP_INLINE;
Chris Mason3326d1b2007-10-15 16:18:25 -04001623 em->start = extent_start + extent_offset;
1624 em->end = (em->start + copy_size -1) |
1625 ((u64)root->sectorsize -1);
Chris Mason5f39d392007-10-15 16:14:19 -04001626
Chris Masona52d9a82007-08-27 16:49:44 -04001627 if (!page) {
1628 goto insert;
1629 }
Chris Mason5f39d392007-10-15 16:14:19 -04001630
Chris Mason3326d1b2007-10-15 16:18:25 -04001631 read_extent_buffer(leaf, map + page_offset, ptr, copy_size);
Chris Masondb945352007-10-15 16:15:53 -04001632 /*
Chris Mason3326d1b2007-10-15 16:18:25 -04001633 memset(map + page_offset + copy_size, 0,
1634 PAGE_CACHE_SIZE - copy_size - page_offset);
Chris Masondb945352007-10-15 16:15:53 -04001635 */
Chris Mason50b78c22007-09-20 14:14:42 -04001636 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04001637 kunmap(page);
Chris Mason3326d1b2007-10-15 16:18:25 -04001638 set_extent_uptodate(em_tree, em->start, em->end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04001639 goto insert;
1640 } else {
1641 printk("unkknown found_type %d\n", found_type);
1642 WARN_ON(1);
1643 }
1644not_found:
1645 em->start = start;
1646 em->end = end;
1647not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04001648 em->block_start = EXTENT_MAP_HOLE;
1649 em->block_end = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001650insert:
1651 btrfs_release_path(root, path);
1652 if (em->start > start || em->end < start) {
Chris Masonb888db22007-08-27 16:49:44 -04001653 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->end, start, end);
Chris Masona52d9a82007-08-27 16:49:44 -04001654 err = -EIO;
1655 goto out;
1656 }
1657 ret = add_extent_mapping(em_tree, em);
1658 if (ret == -EEXIST) {
1659 free_extent_map(em);
Chris Mason2bf5a722007-08-30 11:54:02 -04001660 em = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04001661 failed_insert++;
1662 if (failed_insert > 5) {
1663 printk("failing to insert %Lu %Lu\n", start, end);
1664 err = -EIO;
1665 goto out;
1666 }
Chris Masona52d9a82007-08-27 16:49:44 -04001667 goto again;
1668 }
1669 err = 0;
1670out:
1671 btrfs_free_path(path);
1672 if (trans) {
1673 ret = btrfs_end_transaction(trans, root);
1674 if (!err)
1675 err = ret;
1676 }
1677 mutex_unlock(&root->fs_info->fs_mutex);
1678 if (err) {
1679 free_extent_map(em);
1680 WARN_ON(1);
1681 return ERR_PTR(err);
1682 }
1683 return em;
1684}
1685
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04001686static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04001687{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04001688 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04001689}
1690
1691static int btrfs_prepare_write(struct file *file, struct page *page,
1692 unsigned from, unsigned to)
1693{
Chris Masona52d9a82007-08-27 16:49:44 -04001694 return extent_prepare_write(&BTRFS_I(page->mapping->host)->extent_tree,
1695 page->mapping->host, page, from, to,
1696 btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04001697}
1698
Chris Mason9ebefb182007-06-15 13:50:00 -04001699int btrfs_readpage(struct file *file, struct page *page)
1700{
Chris Masona52d9a82007-08-27 16:49:44 -04001701 struct extent_map_tree *tree;
1702 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1703 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04001704}
Chris Mason39279cc2007-06-12 06:35:45 -04001705static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1706{
Chris Masona52d9a82007-08-27 16:49:44 -04001707 struct extent_map_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04001708
1709
1710 if (current->flags & PF_MEMALLOC) {
1711 redirty_page_for_writepage(wbc, page);
1712 unlock_page(page);
1713 return 0;
1714 }
Chris Masona52d9a82007-08-27 16:49:44 -04001715 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1716 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
1717}
Chris Mason39279cc2007-06-12 06:35:45 -04001718
Chris Masona52d9a82007-08-27 16:49:44 -04001719static int btrfs_releasepage(struct page *page, gfp_t unused_gfp_flags)
1720{
1721 struct extent_map_tree *tree;
1722 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04001723
Chris Masona52d9a82007-08-27 16:49:44 -04001724 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1725 ret = try_release_extent_mapping(tree, page);
1726 if (ret == 1) {
1727 ClearPagePrivate(page);
1728 set_page_private(page, 0);
1729 page_cache_release(page);
1730 }
1731 return ret;
1732}
Chris Mason39279cc2007-06-12 06:35:45 -04001733
Chris Masona52d9a82007-08-27 16:49:44 -04001734static void btrfs_invalidatepage(struct page *page, unsigned long offset)
1735{
1736 struct extent_map_tree *tree;
1737
1738 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1739 extent_invalidatepage(tree, page, offset);
1740 btrfs_releasepage(page, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001741}
1742
Chris Mason9ebefb182007-06-15 13:50:00 -04001743/*
1744 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
1745 * called from a page fault handler when a page is first dirtied. Hence we must
1746 * be careful to check for EOF conditions here. We set the page up correctly
1747 * for a written page which means we get ENOSPC checking when writing into
1748 * holes and correct delalloc and unwritten extent mapping on filesystems that
1749 * support these features.
1750 *
1751 * We are not allowed to take the i_mutex here so we have to play games to
1752 * protect against truncate races as the page could now be beyond EOF. Because
1753 * vmtruncate() writes the inode size before removing pages, once we have the
1754 * page lock we can determine safely if the page is beyond EOF. If it is not
1755 * beyond EOF, then the page is guaranteed safe against truncation until we
1756 * unlock the page.
1757 */
1758int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
1759{
1760 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1761 unsigned long end;
1762 loff_t size;
1763 int ret = -EINVAL;
Chris Masona52d9a82007-08-27 16:49:44 -04001764 u64 page_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04001765
Chris Mason011410b2007-09-10 19:58:36 -04001766 down_read(&BTRFS_I(inode)->root->snap_sem);
Chris Mason9ebefb182007-06-15 13:50:00 -04001767 lock_page(page);
1768 wait_on_page_writeback(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04001769 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04001770 page_start = page->index << PAGE_CACHE_SHIFT;
1771
Chris Mason9ebefb182007-06-15 13:50:00 -04001772 if ((page->mapping != inode->i_mapping) ||
Chris Masona52d9a82007-08-27 16:49:44 -04001773 (page_start > size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04001774 /* page got truncated out from underneath us */
1775 goto out_unlock;
1776 }
1777
1778 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04001779 if (page_start + PAGE_CACHE_SIZE > size)
Chris Mason9ebefb182007-06-15 13:50:00 -04001780 end = size & ~PAGE_CACHE_MASK;
1781 else
1782 end = PAGE_CACHE_SIZE;
1783
Chris Masonb888db22007-08-27 16:49:44 -04001784 ret = btrfs_cow_one_page(inode, page, end);
Chris Mason9ebefb182007-06-15 13:50:00 -04001785
1786out_unlock:
Chris Mason011410b2007-09-10 19:58:36 -04001787 up_read(&BTRFS_I(inode)->root->snap_sem);
Chris Mason9ebefb182007-06-15 13:50:00 -04001788 unlock_page(page);
1789 return ret;
1790}
1791
Chris Mason39279cc2007-06-12 06:35:45 -04001792static void btrfs_truncate(struct inode *inode)
1793{
1794 struct btrfs_root *root = BTRFS_I(inode)->root;
1795 int ret;
1796 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001797 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001798
1799 if (!S_ISREG(inode->i_mode))
1800 return;
1801 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1802 return;
1803
1804 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1805
1806 mutex_lock(&root->fs_info->fs_mutex);
1807 trans = btrfs_start_transaction(root, 1);
1808 btrfs_set_trans_block_group(trans, inode);
1809
1810 /* FIXME, add redo link to tree so we don't leak on crash */
1811 ret = btrfs_truncate_in_trans(trans, root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001812 btrfs_update_inode(trans, root, inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001813 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04001814
Chris Mason39279cc2007-06-12 06:35:45 -04001815 ret = btrfs_end_transaction(trans, root);
1816 BUG_ON(ret);
1817 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001818 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001819}
1820
1821int btrfs_commit_write(struct file *file, struct page *page,
1822 unsigned from, unsigned to)
1823{
Chris Masona52d9a82007-08-27 16:49:44 -04001824 return extent_commit_write(&BTRFS_I(page->mapping->host)->extent_tree,
1825 page->mapping->host, page, from, to);
Chris Mason39279cc2007-06-12 06:35:45 -04001826}
1827
1828static int create_subvol(struct btrfs_root *root, char *name, int namelen)
1829{
1830 struct btrfs_trans_handle *trans;
1831 struct btrfs_key key;
1832 struct btrfs_root_item root_item;
1833 struct btrfs_inode_item *inode_item;
Chris Mason5f39d392007-10-15 16:14:19 -04001834 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001835 struct btrfs_root *new_root;
1836 struct inode *inode;
1837 struct inode *dir;
1838 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001839 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04001840 u64 objectid;
1841 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001842 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001843
1844 mutex_lock(&root->fs_info->fs_mutex);
1845 trans = btrfs_start_transaction(root, 1);
1846 BUG_ON(!trans);
1847
Chris Masondb945352007-10-15 16:15:53 -04001848 leaf = btrfs_alloc_free_block(trans, root, root->leafsize, 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001849 if (IS_ERR(leaf))
1850 return PTR_ERR(leaf);
1851
1852 btrfs_set_header_nritems(leaf, 0);
1853 btrfs_set_header_level(leaf, 0);
Chris Masondb945352007-10-15 16:15:53 -04001854 btrfs_set_header_bytenr(leaf, leaf->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001855 btrfs_set_header_generation(leaf, trans->transid);
1856 btrfs_set_header_owner(leaf, root->root_key.objectid);
1857 write_extent_buffer(leaf, root->fs_info->fsid,
1858 (unsigned long)btrfs_header_fsid(leaf),
1859 BTRFS_FSID_SIZE);
1860 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001861
1862 inode_item = &root_item.inode;
1863 memset(inode_item, 0, sizeof(*inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04001864 inode_item->generation = cpu_to_le64(1);
1865 inode_item->size = cpu_to_le64(3);
1866 inode_item->nlink = cpu_to_le32(1);
1867 inode_item->nblocks = cpu_to_le64(1);
1868 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
Chris Mason39279cc2007-06-12 06:35:45 -04001869
Chris Masondb945352007-10-15 16:15:53 -04001870 btrfs_set_root_bytenr(&root_item, leaf->start);
1871 btrfs_set_root_level(&root_item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001872 btrfs_set_root_refs(&root_item, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001873 btrfs_set_root_used(&root_item, 0);
1874
Chris Mason5eda7b52007-06-22 14:16:25 -04001875 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
1876 root_item.drop_level = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001877
1878 free_extent_buffer(leaf);
1879 leaf = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001880
1881 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
1882 0, &objectid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001883 if (ret)
1884 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001885
1886 btrfs_set_root_dirid(&root_item, new_dirid);
1887
1888 key.objectid = objectid;
1889 key.offset = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001890 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
1891 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
1892 &root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -04001893 if (ret)
1894 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001895
1896 /*
1897 * insert the directory item
1898 */
1899 key.offset = (u64)-1;
1900 dir = root->fs_info->sb->s_root->d_inode;
1901 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
1902 name, namelen, dir->i_ino, &key,
1903 BTRFS_FT_DIR);
Chris Mason54aa1f42007-06-22 14:16:25 -04001904 if (ret)
1905 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001906
1907 ret = btrfs_commit_transaction(trans, root);
Chris Mason54aa1f42007-06-22 14:16:25 -04001908 if (ret)
1909 goto fail_commit;
Chris Mason39279cc2007-06-12 06:35:45 -04001910
Josef Bacik58176a92007-08-29 15:47:34 -04001911 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
Chris Mason39279cc2007-06-12 06:35:45 -04001912 BUG_ON(!new_root);
1913
1914 trans = btrfs_start_transaction(new_root, 1);
1915 BUG_ON(!trans);
1916
1917 inode = btrfs_new_inode(trans, new_root, new_dirid,
1918 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
Chris Mason54aa1f42007-06-22 14:16:25 -04001919 if (IS_ERR(inode))
1920 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001921 inode->i_op = &btrfs_dir_inode_operations;
1922 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04001923 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001924
1925 ret = btrfs_make_empty_dir(trans, new_root, new_dirid, new_dirid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001926 if (ret)
1927 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001928
1929 inode->i_nlink = 1;
1930 inode->i_size = 6;
1931 ret = btrfs_update_inode(trans, new_root, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04001932 if (ret)
1933 goto fail;
1934fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001935 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04001936 err = btrfs_commit_transaction(trans, root);
1937 if (err && !ret)
1938 ret = err;
1939fail_commit:
Chris Mason39279cc2007-06-12 06:35:45 -04001940 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001941 btrfs_btree_balance_dirty(root, nr);
Chris Mason54aa1f42007-06-22 14:16:25 -04001942 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04001943}
1944
1945static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
1946{
1947 struct btrfs_trans_handle *trans;
1948 struct btrfs_key key;
1949 struct btrfs_root_item new_root_item;
Chris Mason5f39d392007-10-15 16:14:19 -04001950 struct extent_buffer *tmp;
Chris Mason39279cc2007-06-12 06:35:45 -04001951 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001952 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04001953 u64 objectid;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001954 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001955
1956 if (!root->ref_cows)
1957 return -EINVAL;
1958
Chris Mason011410b2007-09-10 19:58:36 -04001959 down_write(&root->snap_sem);
1960 freeze_bdev(root->fs_info->sb->s_bdev);
1961 thaw_bdev(root->fs_info->sb->s_bdev, root->fs_info->sb);
1962
Chris Mason39279cc2007-06-12 06:35:45 -04001963 mutex_lock(&root->fs_info->fs_mutex);
1964 trans = btrfs_start_transaction(root, 1);
1965 BUG_ON(!trans);
1966
1967 ret = btrfs_update_inode(trans, root, root->inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04001968 if (ret)
1969 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001970
1971 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
1972 0, &objectid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001973 if (ret)
1974 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001975
1976 memcpy(&new_root_item, &root->root_item,
1977 sizeof(new_root_item));
1978
1979 key.objectid = objectid;
1980 key.offset = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001981 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
Chris Mason5f39d392007-10-15 16:14:19 -04001982
Chris Mason83df7c12007-08-27 16:49:44 -04001983 btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp);
Chris Masondb945352007-10-15 16:15:53 -04001984 btrfs_set_root_bytenr(&new_root_item, root->node->start);
1985 btrfs_set_root_level(&new_root_item, btrfs_header_level(root->node));
Chris Mason39279cc2007-06-12 06:35:45 -04001986
1987 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
1988 &new_root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -04001989 if (ret)
1990 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001991
1992 /*
1993 * insert the directory item
1994 */
1995 key.offset = (u64)-1;
1996 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
1997 name, namelen,
1998 root->fs_info->sb->s_root->d_inode->i_ino,
1999 &key, BTRFS_FT_DIR);
2000
Chris Mason54aa1f42007-06-22 14:16:25 -04002001 if (ret)
2002 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002003
2004 ret = btrfs_inc_root_ref(trans, root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002005 if (ret)
2006 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002007fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002008 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04002009 err = btrfs_commit_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002010
Chris Mason54aa1f42007-06-22 14:16:25 -04002011 if (err && !ret)
2012 ret = err;
Chris Mason5f39d392007-10-15 16:14:19 -04002013
Chris Mason39279cc2007-06-12 06:35:45 -04002014 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason011410b2007-09-10 19:58:36 -04002015 up_write(&root->snap_sem);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002016 btrfs_btree_balance_dirty(root, nr);
Chris Mason54aa1f42007-06-22 14:16:25 -04002017 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002018}
2019
Chris Mason86479a02007-09-10 19:58:16 -04002020static unsigned long force_ra(struct address_space *mapping,
2021 struct file_ra_state *ra, struct file *file,
2022 pgoff_t offset, pgoff_t last_index)
2023{
2024 pgoff_t req_size;
2025
2026#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2027 req_size = last_index - offset + 1;
2028 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2029 return offset;
2030#else
2031 req_size = min(last_index - offset + 1, (pgoff_t)128);
2032 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2033 return offset + req_size;
2034#endif
2035}
2036
2037int btrfs_defrag_file(struct file *file) {
2038 struct inode *inode = file->f_path.dentry->d_inode;
2039 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2040 struct page *page;
2041 unsigned long last_index;
2042 unsigned long ra_index = 0;
2043 u64 page_start;
2044 u64 page_end;
2045 unsigned long i;
2046
2047 mutex_lock(&inode->i_mutex);
2048 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2049 for (i = 0; i <= last_index; i++) {
2050 if (i == ra_index) {
2051 ra_index = force_ra(inode->i_mapping, &file->f_ra,
2052 file, ra_index, last_index);
2053 }
2054 page = grab_cache_page(inode->i_mapping, i);
2055 if (!page)
2056 goto out_unlock;
2057 if (!PageUptodate(page)) {
2058 btrfs_readpage(NULL, page);
2059 lock_page(page);
2060 if (!PageUptodate(page)) {
2061 unlock_page(page);
2062 page_cache_release(page);
2063 goto out_unlock;
2064 }
2065 }
2066 page_start = page->index << PAGE_CACHE_SHIFT;
2067 page_end = page_start + PAGE_CACHE_SIZE - 1;
2068
2069 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
2070 set_extent_delalloc(em_tree, page_start,
2071 page_end, GFP_NOFS);
2072 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
2073 set_page_dirty(page);
2074 unlock_page(page);
2075 page_cache_release(page);
2076 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2077 }
2078
2079out_unlock:
2080 mutex_unlock(&inode->i_mutex);
2081 return 0;
2082}
2083
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002084static int btrfs_ioctl_snap_create(struct btrfs_root *root, void __user *arg)
2085{
2086 struct btrfs_ioctl_vol_args vol_args;
2087 struct btrfs_dir_item *di;
2088 struct btrfs_path *path;
2089 int namelen;
2090 u64 root_dirid;
2091
2092 if (copy_from_user(&vol_args, arg, sizeof(vol_args)))
2093 return -EFAULT;
Chris Mason5f39d392007-10-15 16:14:19 -04002094
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002095 namelen = strlen(vol_args.name);
2096 if (namelen > BTRFS_VOL_NAME_MAX)
2097 return -EINVAL;
2098 if (strchr(vol_args.name, '/'))
2099 return -EINVAL;
2100
2101 path = btrfs_alloc_path();
2102 if (!path)
2103 return -ENOMEM;
2104
2105 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2106 mutex_lock(&root->fs_info->fs_mutex);
2107 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2108 path, root_dirid,
2109 vol_args.name, namelen, 0);
2110 mutex_unlock(&root->fs_info->fs_mutex);
2111 btrfs_free_path(path);
2112 if (di && !IS_ERR(di))
2113 return -EEXIST;
2114 if (IS_ERR(di))
2115 return PTR_ERR(di);
2116
2117 if (root == root->fs_info->tree_root)
2118 return create_subvol(root, vol_args.name, namelen);
2119 return create_snapshot(root, vol_args.name, namelen);
2120}
2121
2122static int btrfs_ioctl_defrag(struct file *file)
Chris Mason39279cc2007-06-12 06:35:45 -04002123{
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002124 struct inode *inode = file->f_path.dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002125 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002126
2127 switch (inode->i_mode & S_IFMT) {
2128 case S_IFDIR:
2129 mutex_lock(&root->fs_info->fs_mutex);
2130 btrfs_defrag_root(root, 0);
2131 btrfs_defrag_root(root->fs_info->extent_root, 0);
2132 mutex_unlock(&root->fs_info->fs_mutex);
2133 break;
2134 case S_IFREG:
2135 btrfs_defrag_file(file);
2136 break;
2137 }
2138
2139 return 0;
2140}
2141
2142long btrfs_ioctl(struct file *file, unsigned int
2143 cmd, unsigned long arg)
2144{
2145 struct btrfs_root *root = BTRFS_I(file->f_path.dentry->d_inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002146
2147 switch (cmd) {
2148 case BTRFS_IOC_SNAP_CREATE:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002149 return btrfs_ioctl_snap_create(root, (void __user *)arg);
Chris Mason6702ed42007-08-07 16:15:09 -04002150 case BTRFS_IOC_DEFRAG:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002151 return btrfs_ioctl_defrag(file);
Chris Mason39279cc2007-06-12 06:35:45 -04002152 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002153
2154 return -ENOTTY;
Chris Mason39279cc2007-06-12 06:35:45 -04002155}
2156
Chris Mason39279cc2007-06-12 06:35:45 -04002157/*
2158 * Called inside transaction, so use GFP_NOFS
2159 */
2160struct inode *btrfs_alloc_inode(struct super_block *sb)
2161{
2162 struct btrfs_inode *ei;
2163
2164 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2165 if (!ei)
2166 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002167 ei->last_trans = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002168 return &ei->vfs_inode;
2169}
2170
2171void btrfs_destroy_inode(struct inode *inode)
2172{
2173 WARN_ON(!list_empty(&inode->i_dentry));
2174 WARN_ON(inode->i_data.nrpages);
2175
2176 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2177}
2178
Chris Mason44ec0b72007-10-29 10:55:05 -04002179#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2180static void init_once(struct kmem_cache * cachep, void *foo)
2181#else
Chris Mason39279cc2007-06-12 06:35:45 -04002182static void init_once(void * foo, struct kmem_cache * cachep,
2183 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04002184#endif
Chris Mason39279cc2007-06-12 06:35:45 -04002185{
2186 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2187
2188 inode_init_once(&ei->vfs_inode);
2189}
2190
2191void btrfs_destroy_cachep(void)
2192{
2193 if (btrfs_inode_cachep)
2194 kmem_cache_destroy(btrfs_inode_cachep);
2195 if (btrfs_trans_handle_cachep)
2196 kmem_cache_destroy(btrfs_trans_handle_cachep);
2197 if (btrfs_transaction_cachep)
2198 kmem_cache_destroy(btrfs_transaction_cachep);
2199 if (btrfs_bit_radix_cachep)
2200 kmem_cache_destroy(btrfs_bit_radix_cachep);
2201 if (btrfs_path_cachep)
2202 kmem_cache_destroy(btrfs_path_cachep);
2203}
2204
Chris Mason86479a02007-09-10 19:58:16 -04002205struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04002206 unsigned long extra_flags,
Chris Mason44ec0b72007-10-29 10:55:05 -04002207#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2208 void (*ctor)(struct kmem_cache *, void *)
2209#else
Chris Mason92fee662007-07-25 12:31:35 -04002210 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04002211 unsigned long)
2212#endif
2213 )
Chris Mason92fee662007-07-25 12:31:35 -04002214{
2215 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2216 SLAB_MEM_SPREAD | extra_flags), ctor
2217#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2218 ,NULL
2219#endif
2220 );
2221}
2222
Chris Mason39279cc2007-06-12 06:35:45 -04002223int btrfs_init_cachep(void)
2224{
Chris Mason86479a02007-09-10 19:58:16 -04002225 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04002226 sizeof(struct btrfs_inode),
2227 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04002228 if (!btrfs_inode_cachep)
2229 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002230 btrfs_trans_handle_cachep =
2231 btrfs_cache_create("btrfs_trans_handle_cache",
2232 sizeof(struct btrfs_trans_handle),
2233 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002234 if (!btrfs_trans_handle_cachep)
2235 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002236 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04002237 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04002238 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002239 if (!btrfs_transaction_cachep)
2240 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002241 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04002242 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04002243 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002244 if (!btrfs_path_cachep)
2245 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002246 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04002247 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002248 if (!btrfs_bit_radix_cachep)
2249 goto fail;
2250 return 0;
2251fail:
2252 btrfs_destroy_cachep();
2253 return -ENOMEM;
2254}
2255
2256static int btrfs_getattr(struct vfsmount *mnt,
2257 struct dentry *dentry, struct kstat *stat)
2258{
2259 struct inode *inode = dentry->d_inode;
2260 generic_fillattr(inode, stat);
2261 stat->blksize = 256 * 1024;
2262 return 0;
2263}
2264
2265static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2266 struct inode * new_dir,struct dentry *new_dentry)
2267{
2268 struct btrfs_trans_handle *trans;
2269 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2270 struct inode *new_inode = new_dentry->d_inode;
2271 struct inode *old_inode = old_dentry->d_inode;
2272 struct timespec ctime = CURRENT_TIME;
2273 struct btrfs_path *path;
2274 struct btrfs_dir_item *di;
2275 int ret;
2276
2277 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2278 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2279 return -ENOTEMPTY;
2280 }
Chris Mason5f39d392007-10-15 16:14:19 -04002281
Chris Mason39279cc2007-06-12 06:35:45 -04002282 mutex_lock(&root->fs_info->fs_mutex);
2283 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002284
Chris Mason39279cc2007-06-12 06:35:45 -04002285 btrfs_set_trans_block_group(trans, new_dir);
2286 path = btrfs_alloc_path();
2287 if (!path) {
2288 ret = -ENOMEM;
2289 goto out_fail;
2290 }
2291
2292 old_dentry->d_inode->i_nlink++;
2293 old_dir->i_ctime = old_dir->i_mtime = ctime;
2294 new_dir->i_ctime = new_dir->i_mtime = ctime;
2295 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04002296
Chris Mason39279cc2007-06-12 06:35:45 -04002297 if (S_ISDIR(old_inode->i_mode) && old_dir != new_dir) {
2298 struct btrfs_key *location = &BTRFS_I(new_dir)->location;
Chris Mason5f39d392007-10-15 16:14:19 -04002299 struct btrfs_key old_parent_key;
Chris Mason39279cc2007-06-12 06:35:45 -04002300 di = btrfs_lookup_dir_item(trans, root, path, old_inode->i_ino,
2301 "..", 2, -1);
2302 if (IS_ERR(di)) {
2303 ret = PTR_ERR(di);
2304 goto out_fail;
2305 }
2306 if (!di) {
2307 ret = -ENOENT;
2308 goto out_fail;
2309 }
Chris Mason5f39d392007-10-15 16:14:19 -04002310 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &old_parent_key);
Chris Mason39279cc2007-06-12 06:35:45 -04002311 ret = btrfs_del_item(trans, root, path);
2312 if (ret) {
Chris Mason39279cc2007-06-12 06:35:45 -04002313 goto out_fail;
2314 }
2315 btrfs_release_path(root, path);
2316
2317 di = btrfs_lookup_dir_index_item(trans, root, path,
2318 old_inode->i_ino,
Chris Mason5f39d392007-10-15 16:14:19 -04002319 old_parent_key.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002320 "..", 2, -1);
2321 if (IS_ERR(di)) {
2322 ret = PTR_ERR(di);
2323 goto out_fail;
2324 }
2325 if (!di) {
2326 ret = -ENOENT;
2327 goto out_fail;
2328 }
2329 ret = btrfs_del_item(trans, root, path);
2330 if (ret) {
Chris Mason39279cc2007-06-12 06:35:45 -04002331 goto out_fail;
2332 }
2333 btrfs_release_path(root, path);
2334
2335 ret = btrfs_insert_dir_item(trans, root, "..", 2,
2336 old_inode->i_ino, location,
2337 BTRFS_FT_DIR);
2338 if (ret)
2339 goto out_fail;
2340 }
2341
2342
2343 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2344 if (ret)
2345 goto out_fail;
2346
2347 if (new_inode) {
2348 new_inode->i_ctime = CURRENT_TIME;
2349 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2350 if (ret)
2351 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002352 }
2353 ret = btrfs_add_link(trans, new_dentry, old_inode);
2354 if (ret)
2355 goto out_fail;
2356
2357out_fail:
2358 btrfs_free_path(path);
2359 btrfs_end_transaction(trans, root);
2360 mutex_unlock(&root->fs_info->fs_mutex);
2361 return ret;
2362}
2363
2364static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2365 const char *symname)
2366{
2367 struct btrfs_trans_handle *trans;
2368 struct btrfs_root *root = BTRFS_I(dir)->root;
2369 struct btrfs_path *path;
2370 struct btrfs_key key;
2371 struct inode *inode;
2372 int err;
2373 int drop_inode = 0;
2374 u64 objectid;
2375 int name_len;
2376 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04002377 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04002378 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04002379 struct extent_buffer *leaf;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002380 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002381
2382 name_len = strlen(symname) + 1;
2383 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2384 return -ENAMETOOLONG;
2385 mutex_lock(&root->fs_info->fs_mutex);
2386 trans = btrfs_start_transaction(root, 1);
2387 btrfs_set_trans_block_group(trans, dir);
2388
2389 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2390 if (err) {
2391 err = -ENOSPC;
2392 goto out_unlock;
2393 }
2394
2395 inode = btrfs_new_inode(trans, root, objectid,
2396 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2397 err = PTR_ERR(inode);
2398 if (IS_ERR(inode))
2399 goto out_unlock;
2400
2401 btrfs_set_trans_block_group(trans, inode);
2402 err = btrfs_add_nondir(trans, dentry, inode);
2403 if (err)
2404 drop_inode = 1;
2405 else {
2406 inode->i_mapping->a_ops = &btrfs_aops;
2407 inode->i_fop = &btrfs_file_operations;
2408 inode->i_op = &btrfs_file_inode_operations;
Chris Masona52d9a82007-08-27 16:49:44 -04002409 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
2410 inode->i_mapping, GFP_NOFS);
Chris Mason07157aa2007-08-30 08:50:51 -04002411 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002412 }
2413 dir->i_sb->s_dirt = 1;
2414 btrfs_update_inode_block_group(trans, inode);
2415 btrfs_update_inode_block_group(trans, dir);
2416 if (drop_inode)
2417 goto out_unlock;
2418
2419 path = btrfs_alloc_path();
2420 BUG_ON(!path);
2421 key.objectid = inode->i_ino;
2422 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002423 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2424 datasize = btrfs_file_extent_calc_inline_size(name_len);
2425 err = btrfs_insert_empty_item(trans, root, path, &key,
2426 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04002427 if (err) {
2428 drop_inode = 1;
2429 goto out_unlock;
2430 }
Chris Mason5f39d392007-10-15 16:14:19 -04002431 leaf = path->nodes[0];
2432 ei = btrfs_item_ptr(leaf, path->slots[0],
2433 struct btrfs_file_extent_item);
2434 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
2435 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04002436 BTRFS_FILE_EXTENT_INLINE);
2437 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04002438 write_extent_buffer(leaf, symname, ptr, name_len);
2439 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002440 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04002441
Chris Mason39279cc2007-06-12 06:35:45 -04002442 inode->i_op = &btrfs_symlink_inode_operations;
2443 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2444 inode->i_size = name_len - 1;
Chris Mason54aa1f42007-06-22 14:16:25 -04002445 err = btrfs_update_inode(trans, root, inode);
2446 if (err)
2447 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002448
2449out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002450 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04002451 btrfs_end_transaction(trans, root);
2452 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04002453 if (drop_inode) {
2454 inode_dec_link_count(inode);
2455 iput(inode);
2456 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002457 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002458 return err;
2459}
2460
2461static struct inode_operations btrfs_dir_inode_operations = {
2462 .lookup = btrfs_lookup,
2463 .create = btrfs_create,
2464 .unlink = btrfs_unlink,
2465 .link = btrfs_link,
2466 .mkdir = btrfs_mkdir,
2467 .rmdir = btrfs_rmdir,
2468 .rename = btrfs_rename,
2469 .symlink = btrfs_symlink,
2470 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04002471 .mknod = btrfs_mknod,
Chris Mason39279cc2007-06-12 06:35:45 -04002472};
2473
2474static struct inode_operations btrfs_dir_ro_inode_operations = {
2475 .lookup = btrfs_lookup,
2476};
2477
2478static struct file_operations btrfs_dir_file_operations = {
2479 .llseek = generic_file_llseek,
2480 .read = generic_read_dir,
2481 .readdir = btrfs_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002482 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002483#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002484 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002485#endif
2486};
2487
Chris Mason07157aa2007-08-30 08:50:51 -04002488static struct extent_map_ops btrfs_extent_map_ops = {
2489 .fill_delalloc = run_delalloc_range,
2490 .writepage_io_hook = btrfs_writepage_io_hook,
2491 .readpage_io_hook = btrfs_readpage_io_hook,
2492 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
2493};
2494
Chris Mason39279cc2007-06-12 06:35:45 -04002495static struct address_space_operations btrfs_aops = {
2496 .readpage = btrfs_readpage,
2497 .writepage = btrfs_writepage,
2498 .sync_page = block_sync_page,
2499 .prepare_write = btrfs_prepare_write,
2500 .commit_write = btrfs_commit_write,
2501 .bmap = btrfs_bmap,
Chris Masona52d9a82007-08-27 16:49:44 -04002502 .invalidatepage = btrfs_invalidatepage,
2503 .releasepage = btrfs_releasepage,
2504 .set_page_dirty = __set_page_dirty_nobuffers,
Chris Mason39279cc2007-06-12 06:35:45 -04002505};
2506
2507static struct address_space_operations btrfs_symlink_aops = {
2508 .readpage = btrfs_readpage,
2509 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04002510 .invalidatepage = btrfs_invalidatepage,
2511 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04002512};
2513
2514static struct inode_operations btrfs_file_inode_operations = {
2515 .truncate = btrfs_truncate,
2516 .getattr = btrfs_getattr,
2517 .setattr = btrfs_setattr,
2518};
2519
Josef Bacik618e21d2007-07-11 10:18:17 -04002520static struct inode_operations btrfs_special_inode_operations = {
2521 .getattr = btrfs_getattr,
2522 .setattr = btrfs_setattr,
2523};
2524
Chris Mason39279cc2007-06-12 06:35:45 -04002525static struct inode_operations btrfs_symlink_inode_operations = {
2526 .readlink = generic_readlink,
2527 .follow_link = page_follow_link_light,
2528 .put_link = page_put_link,
2529};