blob: 0c65141b99930425c94e90dc9c8e264b9909aa90 [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
Chris Mason179e29e2007-11-01 11:28:41 -040092 if (alloc_hint == EXTENT_MAP_INLINE)
93 goto out;
94
Chris Masondb945352007-10-15 16:15:53 -040095 ret = btrfs_alloc_extent(trans, root, inode->i_ino, num_bytes, 0,
Chris Masonb888db22007-08-27 16:49:44 -040096 alloc_hint, (u64)-1, &ins, 1);
97 if (ret) {
98 WARN_ON(1);
99 goto out;
100 }
101 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
102 start, ins.objectid, ins.offset,
103 ins.offset);
104out:
105 btrfs_end_transaction(trans, root);
106 mutex_unlock(&root->fs_info->fs_mutex);
107 return ret;
108}
109
Chris Mason07157aa2007-08-30 08:50:51 -0400110int btrfs_writepage_io_hook(struct page *page, u64 start, u64 end)
111{
112 struct inode *inode = page->mapping->host;
113 struct btrfs_root *root = BTRFS_I(inode)->root;
114 struct btrfs_trans_handle *trans;
115 char *kaddr;
116 int ret;
Chris Mason35ebb932007-10-30 16:56:53 -0400117 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason07157aa2007-08-30 08:50:51 -0400118 size_t offset = start - page_start;
119
120 mutex_lock(&root->fs_info->fs_mutex);
121 trans = btrfs_start_transaction(root, 1);
122 btrfs_set_trans_block_group(trans, inode);
123 kaddr = kmap(page);
Chris Masonf578d4b2007-10-25 15:42:56 -0400124 btrfs_csum_file_block(trans, root, inode, inode->i_ino,
Chris Mason07157aa2007-08-30 08:50:51 -0400125 start, kaddr + offset, end - start + 1);
126 kunmap(page);
127 ret = btrfs_end_transaction(trans, root);
128 BUG_ON(ret);
129 mutex_unlock(&root->fs_info->fs_mutex);
130 return ret;
131}
132
133int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
134{
135 int ret = 0;
136 struct inode *inode = page->mapping->host;
137 struct btrfs_root *root = BTRFS_I(inode)->root;
138 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
139 struct btrfs_csum_item *item;
140 struct btrfs_path *path = NULL;
Chris Masonff79f812007-10-15 16:22:25 -0400141 u32 csum;
Chris Mason07157aa2007-08-30 08:50:51 -0400142
143 mutex_lock(&root->fs_info->fs_mutex);
144 path = btrfs_alloc_path();
145 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
146 if (IS_ERR(item)) {
147 ret = PTR_ERR(item);
148 /* a csum that isn't present is a preallocated region. */
149 if (ret == -ENOENT || ret == -EFBIG)
150 ret = 0;
Chris Masonff79f812007-10-15 16:22:25 -0400151 csum = 0;
Chris Mason07157aa2007-08-30 08:50:51 -0400152 goto out;
153 }
Chris Masonff79f812007-10-15 16:22:25 -0400154 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
155 BTRFS_CRC32_SIZE);
156 set_state_private(em_tree, start, csum);
Chris Mason07157aa2007-08-30 08:50:51 -0400157out:
158 if (path)
159 btrfs_free_path(path);
160 mutex_unlock(&root->fs_info->fs_mutex);
161 return ret;
162}
163
164int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end)
165{
Chris Mason35ebb932007-10-30 16:56:53 -0400166 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -0400167 struct inode *inode = page->mapping->host;
Chris Mason07157aa2007-08-30 08:50:51 -0400168 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
169 char *kaddr;
170 u64 private;
171 int ret;
Chris Masonff79f812007-10-15 16:22:25 -0400172 struct btrfs_root *root = BTRFS_I(inode)->root;
173 u32 csum = ~(u32)0;
Jens Axboebbf0d002007-10-19 09:23:07 -0400174 unsigned long flags;
Chris Mason07157aa2007-08-30 08:50:51 -0400175
176 ret = get_state_private(em_tree, start, &private);
Jens Axboebbf0d002007-10-19 09:23:07 -0400177 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400178 kaddr = kmap_atomic(page, KM_IRQ0);
179 if (ret) {
180 goto zeroit;
181 }
Chris Masonff79f812007-10-15 16:22:25 -0400182 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
183 btrfs_csum_final(csum, (char *)&csum);
184 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -0400185 goto zeroit;
186 }
187 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400188 local_irq_restore(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400189 return 0;
190
191zeroit:
192 printk("btrfs csum failed ino %lu off %llu\n",
193 page->mapping->host->i_ino, (unsigned long long)start);
Chris Masondb945352007-10-15 16:15:53 -0400194 memset(kaddr + offset, 1, end - start + 1);
195 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -0400196 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400197 local_irq_restore(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400198 return 0;
199}
Chris Masonb888db22007-08-27 16:49:44 -0400200
Chris Mason39279cc2007-06-12 06:35:45 -0400201void btrfs_read_locked_inode(struct inode *inode)
202{
203 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400204 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400205 struct btrfs_inode_item *inode_item;
Chris Mason5f39d392007-10-15 16:14:19 -0400206 struct btrfs_inode_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -0400207 struct btrfs_root *root = BTRFS_I(inode)->root;
208 struct btrfs_key location;
209 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -0400210 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -0400211 int ret;
212
213 path = btrfs_alloc_path();
214 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400215 mutex_lock(&root->fs_info->fs_mutex);
216
217 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
218 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400219 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -0400220 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -0400221
Chris Mason5f39d392007-10-15 16:14:19 -0400222 leaf = path->nodes[0];
223 inode_item = btrfs_item_ptr(leaf, path->slots[0],
224 struct btrfs_inode_item);
225
226 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
227 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
228 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
229 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
230 inode->i_size = btrfs_inode_size(leaf, inode_item);
231
232 tspec = btrfs_inode_atime(inode_item);
233 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
234 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
235
236 tspec = btrfs_inode_mtime(inode_item);
237 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
238 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
239
240 tspec = btrfs_inode_ctime(inode_item);
241 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
242 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
243
244 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
245 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik618e21d2007-07-11 10:18:17 -0400246 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400247 rdev = btrfs_inode_rdev(leaf, inode_item);
248
249 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -0400250 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
251 alloc_group_block);
252
253 btrfs_free_path(path);
254 inode_item = NULL;
255
256 mutex_unlock(&root->fs_info->fs_mutex);
257
258 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -0400259 case S_IFREG:
260 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason07157aa2007-08-30 08:50:51 -0400261 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -0400262 inode->i_fop = &btrfs_file_operations;
263 inode->i_op = &btrfs_file_inode_operations;
264 break;
265 case S_IFDIR:
266 inode->i_fop = &btrfs_dir_file_operations;
267 if (root == root->fs_info->tree_root)
268 inode->i_op = &btrfs_dir_ro_inode_operations;
269 else
270 inode->i_op = &btrfs_dir_inode_operations;
271 break;
272 case S_IFLNK:
273 inode->i_op = &btrfs_symlink_inode_operations;
274 inode->i_mapping->a_ops = &btrfs_symlink_aops;
275 break;
Josef Bacik618e21d2007-07-11 10:18:17 -0400276 default:
277 init_special_inode(inode, inode->i_mode, rdev);
278 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400279 }
280 return;
281
282make_bad:
283 btrfs_release_path(root, path);
284 btrfs_free_path(path);
285 mutex_unlock(&root->fs_info->fs_mutex);
286 make_bad_inode(inode);
287}
288
Chris Mason5f39d392007-10-15 16:14:19 -0400289static void fill_inode_item(struct extent_buffer *leaf,
290 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -0400291 struct inode *inode)
292{
Chris Mason5f39d392007-10-15 16:14:19 -0400293 btrfs_set_inode_uid(leaf, item, inode->i_uid);
294 btrfs_set_inode_gid(leaf, item, inode->i_gid);
295 btrfs_set_inode_size(leaf, item, inode->i_size);
296 btrfs_set_inode_mode(leaf, item, inode->i_mode);
297 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
298
299 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
300 inode->i_atime.tv_sec);
301 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
302 inode->i_atime.tv_nsec);
303
304 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
305 inode->i_mtime.tv_sec);
306 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
307 inode->i_mtime.tv_nsec);
308
309 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
310 inode->i_ctime.tv_sec);
311 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
312 inode->i_ctime.tv_nsec);
313
314 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
315 btrfs_set_inode_generation(leaf, item, inode->i_generation);
316 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
317 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -0400318 BTRFS_I(inode)->block_group->key.objectid);
319}
320
Chris Masona52d9a82007-08-27 16:49:44 -0400321int btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -0400322 struct btrfs_root *root,
323 struct inode *inode)
324{
325 struct btrfs_inode_item *inode_item;
326 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400327 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400328 int ret;
329
330 path = btrfs_alloc_path();
331 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400332 ret = btrfs_lookup_inode(trans, root, path,
333 &BTRFS_I(inode)->location, 1);
334 if (ret) {
335 if (ret > 0)
336 ret = -ENOENT;
337 goto failed;
338 }
339
Chris Mason5f39d392007-10-15 16:14:19 -0400340 leaf = path->nodes[0];
341 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400342 struct btrfs_inode_item);
343
Chris Mason5f39d392007-10-15 16:14:19 -0400344 fill_inode_item(leaf, inode_item, inode);
345 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400346 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400347 ret = 0;
348failed:
349 btrfs_release_path(root, path);
350 btrfs_free_path(path);
351 return ret;
352}
353
354
355static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
356 struct btrfs_root *root,
357 struct inode *dir,
358 struct dentry *dentry)
359{
360 struct btrfs_path *path;
361 const char *name = dentry->d_name.name;
362 int name_len = dentry->d_name.len;
363 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400364 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400365 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -0400366 struct btrfs_key key;
Chris Mason39279cc2007-06-12 06:35:45 -0400367
368 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400369 if (!path) {
370 ret = -ENOMEM;
371 goto err;
372 }
373
Chris Mason39279cc2007-06-12 06:35:45 -0400374 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
375 name, name_len, -1);
376 if (IS_ERR(di)) {
377 ret = PTR_ERR(di);
378 goto err;
379 }
380 if (!di) {
381 ret = -ENOENT;
382 goto err;
383 }
Chris Mason5f39d392007-10-15 16:14:19 -0400384 leaf = path->nodes[0];
385 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -0400386 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -0400387 if (ret)
388 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -0400389 btrfs_release_path(root, path);
390
391 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Chris Mason5f39d392007-10-15 16:14:19 -0400392 key.objectid, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -0400393 if (IS_ERR(di)) {
394 ret = PTR_ERR(di);
395 goto err;
396 }
397 if (!di) {
398 ret = -ENOENT;
399 goto err;
400 }
401 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason39279cc2007-06-12 06:35:45 -0400402
403 dentry->d_inode->i_ctime = dir->i_ctime;
404err:
405 btrfs_free_path(path);
406 if (!ret) {
407 dir->i_size -= name_len * 2;
Chris Mason79c44582007-06-25 10:09:33 -0400408 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -0400409 btrfs_update_inode(trans, root, dir);
410 drop_nlink(dentry->d_inode);
Chris Mason54aa1f42007-06-22 14:16:25 -0400411 ret = btrfs_update_inode(trans, root, dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400412 dir->i_sb->s_dirt = 1;
413 }
414 return ret;
415}
416
417static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
418{
419 struct btrfs_root *root;
420 struct btrfs_trans_handle *trans;
421 int ret;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400422 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -0400423
424 root = BTRFS_I(dir)->root;
425 mutex_lock(&root->fs_info->fs_mutex);
426 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400427
Chris Mason39279cc2007-06-12 06:35:45 -0400428 btrfs_set_trans_block_group(trans, dir);
429 ret = btrfs_unlink_trans(trans, root, dir, dentry);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400430 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -0400431
Chris Mason39279cc2007-06-12 06:35:45 -0400432 btrfs_end_transaction(trans, root);
433 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400434 btrfs_btree_balance_dirty(root, nr);
Chris Mason5f39d392007-10-15 16:14:19 -0400435
Chris Mason39279cc2007-06-12 06:35:45 -0400436 return ret;
437}
438
439static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
440{
441 struct inode *inode = dentry->d_inode;
442 int err;
443 int ret;
444 struct btrfs_root *root = BTRFS_I(dir)->root;
445 struct btrfs_path *path;
446 struct btrfs_key key;
447 struct btrfs_trans_handle *trans;
448 struct btrfs_key found_key;
449 int found_type;
Chris Mason5f39d392007-10-15 16:14:19 -0400450 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400451 char *goodnames = "..";
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400452 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -0400453
Yan134d4512007-10-25 15:49:25 -0400454 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
455 return -ENOTEMPTY;
456
Chris Mason39279cc2007-06-12 06:35:45 -0400457 path = btrfs_alloc_path();
458 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400459 mutex_lock(&root->fs_info->fs_mutex);
460 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400461
Chris Mason39279cc2007-06-12 06:35:45 -0400462 btrfs_set_trans_block_group(trans, dir);
463 key.objectid = inode->i_ino;
464 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -0400465 key.type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -0400466 while(1) {
467 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
468 if (ret < 0) {
469 err = ret;
470 goto out;
471 }
472 BUG_ON(ret == 0);
473 if (path->slots[0] == 0) {
474 err = -ENOENT;
475 goto out;
476 }
477 path->slots[0]--;
Chris Mason5f39d392007-10-15 16:14:19 -0400478 leaf = path->nodes[0];
479 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason39279cc2007-06-12 06:35:45 -0400480 found_type = btrfs_key_type(&found_key);
481 if (found_key.objectid != inode->i_ino) {
482 err = -ENOENT;
483 goto out;
484 }
485 if ((found_type != BTRFS_DIR_ITEM_KEY &&
486 found_type != BTRFS_DIR_INDEX_KEY) ||
487 (!btrfs_match_dir_item_name(root, path, goodnames, 2) &&
488 !btrfs_match_dir_item_name(root, path, goodnames, 1))) {
489 err = -ENOTEMPTY;
490 goto out;
491 }
492 ret = btrfs_del_item(trans, root, path);
493 BUG_ON(ret);
494
495 if (found_type == BTRFS_DIR_ITEM_KEY && found_key.offset == 1)
496 break;
497 btrfs_release_path(root, path);
498 }
499 ret = 0;
500 btrfs_release_path(root, path);
501
502 /* now the directory is empty */
503 err = btrfs_unlink_trans(trans, root, dir, dentry);
504 if (!err) {
505 inode->i_size = 0;
506 }
507out:
508 btrfs_release_path(root, path);
509 btrfs_free_path(path);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400510 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -0400511 ret = btrfs_end_transaction(trans, root);
Yan134d4512007-10-25 15:49:25 -0400512 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400513 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -0400514 if (ret && !err)
515 err = ret;
516 return err;
517}
518
519static int btrfs_free_inode(struct btrfs_trans_handle *trans,
520 struct btrfs_root *root,
521 struct inode *inode)
522{
523 struct btrfs_path *path;
524 int ret;
525
526 clear_inode(inode);
527
528 path = btrfs_alloc_path();
529 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400530 ret = btrfs_lookup_inode(trans, root, path,
531 &BTRFS_I(inode)->location, -1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400532 if (ret > 0)
533 ret = -ENOENT;
534 if (!ret)
535 ret = btrfs_del_item(trans, root, path);
Chris Mason39279cc2007-06-12 06:35:45 -0400536 btrfs_free_path(path);
537 return ret;
538}
539
540/*
Chris Mason39279cc2007-06-12 06:35:45 -0400541 * this can truncate away extent items, csum items and directory items.
542 * It starts at a high offset and removes keys until it can't find
543 * any higher than i_size.
544 *
545 * csum items that cross the new i_size are truncated to the new size
546 * as well.
547 */
548static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
549 struct btrfs_root *root,
550 struct inode *inode)
551{
552 int ret;
553 struct btrfs_path *path;
554 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400555 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -0400556 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -0400557 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400558 struct btrfs_file_extent_item *fi;
559 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -0400560 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400561 u64 item_end = 0;
562 int found_extent;
563 int del_item;
Chris Mason179e29e2007-11-01 11:28:41 -0400564 int extent_type = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400565
Chris Masona52d9a82007-08-27 16:49:44 -0400566 btrfs_drop_extent_cache(inode, inode->i_size, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -0400567 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -0400568 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400569 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -0400570
Chris Mason39279cc2007-06-12 06:35:45 -0400571 /* FIXME, add redo link to tree so we don't leak on crash */
572 key.objectid = inode->i_ino;
573 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -0400574 key.type = (u8)-1;
575
Chris Mason39279cc2007-06-12 06:35:45 -0400576 while(1) {
577 btrfs_init_path(path);
578 fi = NULL;
579 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
580 if (ret < 0) {
581 goto error;
582 }
583 if (ret > 0) {
584 BUG_ON(path->slots[0] == 0);
585 path->slots[0]--;
586 }
Chris Mason5f39d392007-10-15 16:14:19 -0400587 leaf = path->nodes[0];
588 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
589 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -0400590
Chris Mason5f39d392007-10-15 16:14:19 -0400591 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -0400592 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400593
Chris Mason39279cc2007-06-12 06:35:45 -0400594 if (found_type != BTRFS_CSUM_ITEM_KEY &&
595 found_type != BTRFS_DIR_ITEM_KEY &&
596 found_type != BTRFS_DIR_INDEX_KEY &&
597 found_type != BTRFS_EXTENT_DATA_KEY)
598 break;
599
Chris Mason5f39d392007-10-15 16:14:19 -0400600 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -0400601 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -0400602 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400603 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -0400604 extent_type = btrfs_file_extent_type(leaf, fi);
605 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -0400606 item_end +=
Chris Masondb945352007-10-15 16:15:53 -0400607 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -0400608 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
609 struct btrfs_item *item = btrfs_item_nr(leaf,
610 path->slots[0]);
611 item_end += btrfs_file_extent_inline_len(leaf,
612 item);
Chris Mason39279cc2007-06-12 06:35:45 -0400613 }
614 }
615 if (found_type == BTRFS_CSUM_ITEM_KEY) {
616 ret = btrfs_csum_truncate(trans, root, path,
617 inode->i_size);
618 BUG_ON(ret);
619 }
Chris Mason179e29e2007-11-01 11:28:41 -0400620 if (item_end <= inode->i_size) {
Chris Masonb888db22007-08-27 16:49:44 -0400621 if (found_type == BTRFS_DIR_ITEM_KEY) {
622 found_type = BTRFS_INODE_ITEM_KEY;
623 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
624 found_type = BTRFS_CSUM_ITEM_KEY;
625 } else if (found_type) {
626 found_type--;
627 } else {
628 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400629 }
Yana61721d2007-09-17 11:08:38 -0400630 btrfs_set_key_type(&key, found_type);
Yan65555a02007-10-25 15:42:57 -0400631 btrfs_release_path(root, path);
Chris Masonb888db22007-08-27 16:49:44 -0400632 continue;
Chris Mason39279cc2007-06-12 06:35:45 -0400633 }
Chris Mason5f39d392007-10-15 16:14:19 -0400634 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -0400635 del_item = 1;
636 else
637 del_item = 0;
638 found_extent = 0;
639
640 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -0400641 if (found_type != BTRFS_EXTENT_DATA_KEY)
642 goto delete;
643
644 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -0400645 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -0400646 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400647 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -0400648 u64 orig_num_bytes =
649 btrfs_file_extent_num_bytes(leaf, fi);
650 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -0400651 found_key.offset + root->sectorsize - 1;
Chris Masondb945352007-10-15 16:15:53 -0400652 btrfs_set_file_extent_num_bytes(leaf, fi,
653 extent_num_bytes);
654 num_dec = (orig_num_bytes -
655 extent_num_bytes) >> 9;
Yanbab9fb02007-09-17 11:13:11 -0400656 if (extent_start != 0) {
657 inode->i_blocks -= num_dec;
658 }
Chris Mason5f39d392007-10-15 16:14:19 -0400659 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -0400660 } else {
Chris Masondb945352007-10-15 16:15:53 -0400661 extent_num_bytes =
662 btrfs_file_extent_disk_num_bytes(leaf,
663 fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400664 /* FIXME blocksize != 4096 */
Chris Masondb945352007-10-15 16:15:53 -0400665 num_dec = btrfs_file_extent_num_bytes(leaf,
666 fi) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -0400667 if (extent_start != 0) {
668 found_extent = 1;
669 inode->i_blocks -= num_dec;
670 }
671 }
Chris Mason179e29e2007-11-01 11:28:41 -0400672 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE &&
673 !del_item) {
674 u32 newsize = inode->i_size - found_key.offset;
675 newsize = btrfs_file_extent_calc_inline_size(newsize);
676 ret = btrfs_truncate_item(trans, root, path,
677 newsize, 1);
678 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -0400679 }
Chris Mason179e29e2007-11-01 11:28:41 -0400680delete:
Chris Mason39279cc2007-06-12 06:35:45 -0400681 if (del_item) {
682 ret = btrfs_del_item(trans, root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400683 if (ret)
684 goto error;
Chris Mason39279cc2007-06-12 06:35:45 -0400685 } else {
686 break;
687 }
688 btrfs_release_path(root, path);
689 if (found_extent) {
690 ret = btrfs_free_extent(trans, root, extent_start,
Chris Masondb945352007-10-15 16:15:53 -0400691 extent_num_bytes, 0);
Chris Mason39279cc2007-06-12 06:35:45 -0400692 BUG_ON(ret);
693 }
694 }
695 ret = 0;
696error:
697 btrfs_release_path(root, path);
698 btrfs_free_path(path);
699 inode->i_sb->s_dirt = 1;
700 return ret;
701}
702
Chris Masonb888db22007-08-27 16:49:44 -0400703static int btrfs_cow_one_page(struct inode *inode, struct page *page,
Chris Masona52d9a82007-08-27 16:49:44 -0400704 size_t zero_start)
Chris Mason39279cc2007-06-12 06:35:45 -0400705{
Chris Mason39279cc2007-06-12 06:35:45 -0400706 char *kaddr;
707 int ret = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400708 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason35ebb932007-10-30 16:56:53 -0400709 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masonb888db22007-08-27 16:49:44 -0400710 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Mason39279cc2007-06-12 06:35:45 -0400711
Christoph Hellwigb3cfa352007-09-17 11:25:58 -0400712 set_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400713
Chris Masonb888db22007-08-27 16:49:44 -0400714 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
715 set_extent_delalloc(&BTRFS_I(inode)->extent_tree, page_start,
716 page_end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -0400717 if (zero_start != PAGE_CACHE_SIZE) {
Chris Masonb888db22007-08-27 16:49:44 -0400718 kaddr = kmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400719 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
720 flush_dcache_page(page);
Chris Masonb888db22007-08-27 16:49:44 -0400721 kunmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400722 }
Chris Masonb888db22007-08-27 16:49:44 -0400723 set_page_dirty(page);
724 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -0400725
Chris Masona52d9a82007-08-27 16:49:44 -0400726 return ret;
727}
728
729/*
730 * taken from block_truncate_page, but does cow as it zeros out
731 * any bytes left in the last page in the file.
732 */
733static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
734{
735 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -0400736 struct btrfs_root *root = BTRFS_I(inode)->root;
737 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -0400738 pgoff_t index = from >> PAGE_CACHE_SHIFT;
739 unsigned offset = from & (PAGE_CACHE_SIZE-1);
740 struct page *page;
741 int ret = 0;
742 u64 page_start;
743
744 if ((offset & (blocksize - 1)) == 0)
745 goto out;
746
Chris Masondb945352007-10-15 16:15:53 -0400747 down_read(&root->snap_sem);
Chris Masona52d9a82007-08-27 16:49:44 -0400748 ret = -ENOMEM;
749 page = grab_cache_page(mapping, index);
750 if (!page)
751 goto out;
752 if (!PageUptodate(page)) {
753 ret = btrfs_readpage(NULL, page);
754 lock_page(page);
755 if (!PageUptodate(page)) {
756 ret = -EIO;
757 goto out;
758 }
759 }
Chris Mason35ebb932007-10-30 16:56:53 -0400760 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -0400761
Chris Masonb888db22007-08-27 16:49:44 -0400762 ret = btrfs_cow_one_page(inode, page, offset);
Chris Mason39279cc2007-06-12 06:35:45 -0400763
Chris Mason39279cc2007-06-12 06:35:45 -0400764 unlock_page(page);
765 page_cache_release(page);
Chris Mason011410b2007-09-10 19:58:36 -0400766 up_read(&BTRFS_I(inode)->root->snap_sem);
Chris Mason39279cc2007-06-12 06:35:45 -0400767out:
768 return ret;
769}
770
771static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
772{
773 struct inode *inode = dentry->d_inode;
774 int err;
775
776 err = inode_change_ok(inode, attr);
777 if (err)
778 return err;
779
780 if (S_ISREG(inode->i_mode) &&
781 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
782 struct btrfs_trans_handle *trans;
783 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason2bf5a722007-08-30 11:54:02 -0400784 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
785
Chris Mason5f39d392007-10-15 16:14:19 -0400786 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -0400787 u64 pos = (inode->i_size + mask) & ~mask;
Chris Mason2bf5a722007-08-30 11:54:02 -0400788 u64 block_end = attr->ia_size | mask;
Chris Mason39279cc2007-06-12 06:35:45 -0400789 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -0400790 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400791
792 if (attr->ia_size <= pos)
793 goto out;
794
795 btrfs_truncate_page(inode->i_mapping, inode->i_size);
796
Chris Mason2bf5a722007-08-30 11:54:02 -0400797 lock_extent(em_tree, pos, block_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -0400798 hole_size = (attr->ia_size - pos + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -0400799
800 mutex_lock(&root->fs_info->fs_mutex);
801 trans = btrfs_start_transaction(root, 1);
802 btrfs_set_trans_block_group(trans, inode);
Chris Mason2bf5a722007-08-30 11:54:02 -0400803 err = btrfs_drop_extents(trans, root, inode,
Chris Mason3326d1b2007-10-15 16:18:25 -0400804 pos, pos + hole_size, pos,
805 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -0400806
Chris Mason179e29e2007-11-01 11:28:41 -0400807 if (alloc_hint != EXTENT_MAP_INLINE) {
808 err = btrfs_insert_file_extent(trans, root,
809 inode->i_ino,
810 pos, 0, 0, hole_size);
811 }
Chris Mason39279cc2007-06-12 06:35:45 -0400812 btrfs_end_transaction(trans, root);
813 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason2bf5a722007-08-30 11:54:02 -0400814 unlock_extent(em_tree, pos, block_end, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -0400815 if (err)
816 return err;
Chris Mason39279cc2007-06-12 06:35:45 -0400817 }
818out:
819 err = inode_setattr(inode, attr);
820
821 return err;
822}
823void btrfs_delete_inode(struct inode *inode)
824{
825 struct btrfs_trans_handle *trans;
826 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400827 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -0400828 int ret;
829
830 truncate_inode_pages(&inode->i_data, 0);
831 if (is_bad_inode(inode)) {
832 goto no_delete;
833 }
Chris Mason5f39d392007-10-15 16:14:19 -0400834
Chris Mason39279cc2007-06-12 06:35:45 -0400835 inode->i_size = 0;
836 mutex_lock(&root->fs_info->fs_mutex);
837 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400838
Chris Mason39279cc2007-06-12 06:35:45 -0400839 btrfs_set_trans_block_group(trans, inode);
840 ret = btrfs_truncate_in_trans(trans, root, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -0400841 if (ret)
842 goto no_delete_lock;
843 ret = btrfs_free_inode(trans, root, inode);
844 if (ret)
845 goto no_delete_lock;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400846 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -0400847
Chris Mason39279cc2007-06-12 06:35:45 -0400848 btrfs_end_transaction(trans, root);
849 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400850 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -0400851 return;
Chris Mason54aa1f42007-06-22 14:16:25 -0400852
853no_delete_lock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400854 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -0400855 btrfs_end_transaction(trans, root);
856 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400857 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -0400858no_delete:
859 clear_inode(inode);
860}
861
862/*
863 * this returns the key found in the dir entry in the location pointer.
864 * If no dir entries were found, location->objectid is 0.
865 */
866static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
867 struct btrfs_key *location)
868{
869 const char *name = dentry->d_name.name;
870 int namelen = dentry->d_name.len;
871 struct btrfs_dir_item *di;
872 struct btrfs_path *path;
873 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -0400874 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400875
876 path = btrfs_alloc_path();
877 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400878 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
879 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -0400880 if (IS_ERR(di))
881 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -0400882 if (!di || IS_ERR(di)) {
883 location->objectid = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400884 goto out;
885 }
Chris Mason5f39d392007-10-15 16:14:19 -0400886 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -0400887out:
888 btrfs_release_path(root, path);
889 btrfs_free_path(path);
890 return ret;
891}
892
893/*
894 * when we hit a tree root in a directory, the btrfs part of the inode
895 * needs to be changed to reflect the root directory of the tree root. This
896 * is kind of like crossing a mount point.
897 */
898static int fixup_tree_root_location(struct btrfs_root *root,
899 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -0400900 struct btrfs_root **sub_root,
901 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -0400902{
903 struct btrfs_path *path;
904 struct btrfs_root_item *ri;
905
906 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
907 return 0;
908 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
909 return 0;
910
911 path = btrfs_alloc_path();
912 BUG_ON(!path);
913 mutex_lock(&root->fs_info->fs_mutex);
914
Josef Bacik58176a92007-08-29 15:47:34 -0400915 *sub_root = btrfs_read_fs_root(root->fs_info, location,
916 dentry->d_name.name,
917 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -0400918 if (IS_ERR(*sub_root))
919 return PTR_ERR(*sub_root);
920
921 ri = &(*sub_root)->root_item;
922 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -0400923 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
924 location->offset = 0;
925
926 btrfs_free_path(path);
927 mutex_unlock(&root->fs_info->fs_mutex);
928 return 0;
929}
930
931static int btrfs_init_locked_inode(struct inode *inode, void *p)
932{
933 struct btrfs_iget_args *args = p;
934 inode->i_ino = args->ino;
935 BTRFS_I(inode)->root = args->root;
Chris Masonb888db22007-08-27 16:49:44 -0400936 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
937 inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -0400938 return 0;
939}
940
941static int btrfs_find_actor(struct inode *inode, void *opaque)
942{
943 struct btrfs_iget_args *args = opaque;
944 return (args->ino == inode->i_ino &&
945 args->root == BTRFS_I(inode)->root);
946}
947
948struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
949 struct btrfs_root *root)
950{
951 struct inode *inode;
952 struct btrfs_iget_args args;
953 args.ino = objectid;
954 args.root = root;
955
956 inode = iget5_locked(s, objectid, btrfs_find_actor,
957 btrfs_init_locked_inode,
958 (void *)&args);
959 return inode;
960}
961
962static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
963 struct nameidata *nd)
964{
965 struct inode * inode;
966 struct btrfs_inode *bi = BTRFS_I(dir);
967 struct btrfs_root *root = bi->root;
968 struct btrfs_root *sub_root = root;
969 struct btrfs_key location;
970 int ret;
971
972 if (dentry->d_name.len > BTRFS_NAME_LEN)
973 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -0400974
Chris Mason39279cc2007-06-12 06:35:45 -0400975 mutex_lock(&root->fs_info->fs_mutex);
976 ret = btrfs_inode_by_name(dir, dentry, &location);
977 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason5f39d392007-10-15 16:14:19 -0400978
Chris Mason39279cc2007-06-12 06:35:45 -0400979 if (ret < 0)
980 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -0400981
Chris Mason39279cc2007-06-12 06:35:45 -0400982 inode = NULL;
983 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -0400984 ret = fixup_tree_root_location(root, &location, &sub_root,
985 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -0400986 if (ret < 0)
987 return ERR_PTR(ret);
988 if (ret > 0)
989 return ERR_PTR(-ENOENT);
990 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
991 sub_root);
992 if (!inode)
993 return ERR_PTR(-EACCES);
994 if (inode->i_state & I_NEW) {
995 /* the inode and parent dir are two different roots */
996 if (sub_root != root) {
997 igrab(inode);
998 sub_root->inode = inode;
999 }
1000 BTRFS_I(inode)->root = sub_root;
1001 memcpy(&BTRFS_I(inode)->location, &location,
1002 sizeof(location));
1003 btrfs_read_locked_inode(inode);
1004 unlock_new_inode(inode);
1005 }
1006 }
1007 return d_splice_alias(inode, dentry);
1008}
1009
Chris Mason39279cc2007-06-12 06:35:45 -04001010static unsigned char btrfs_filetype_table[] = {
1011 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1012};
1013
1014static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1015{
1016 struct inode *inode = filp->f_path.dentry->d_inode;
1017 struct btrfs_root *root = BTRFS_I(inode)->root;
1018 struct btrfs_item *item;
1019 struct btrfs_dir_item *di;
1020 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001021 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001022 struct btrfs_path *path;
1023 int ret;
1024 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001025 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001026 int slot;
1027 int advance;
1028 unsigned char d_type;
1029 int over = 0;
1030 u32 di_cur;
1031 u32 di_total;
1032 u32 di_len;
1033 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001034 char tmp_name[32];
1035 char *name_ptr;
1036 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001037
1038 /* FIXME, use a real flag for deciding about the key type */
1039 if (root->fs_info->tree_root == root)
1040 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001041
Chris Mason39279cc2007-06-12 06:35:45 -04001042 mutex_lock(&root->fs_info->fs_mutex);
1043 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04001044 btrfs_set_key_type(&key, key_type);
1045 key.offset = filp->f_pos;
Chris Mason5f39d392007-10-15 16:14:19 -04001046
Chris Mason39279cc2007-06-12 06:35:45 -04001047 path = btrfs_alloc_path();
Chris Mason2cc58cf2007-08-27 16:49:44 -04001048 path->reada = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04001049 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1050 if (ret < 0)
1051 goto err;
1052 advance = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001053 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001054 leaf = path->nodes[0];
1055 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001056 slot = path->slots[0];
1057 if (advance || slot >= nritems) {
1058 if (slot >= nritems -1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001059 ret = btrfs_next_leaf(root, path);
1060 if (ret)
1061 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001062 leaf = path->nodes[0];
1063 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001064 slot = path->slots[0];
1065 } else {
1066 slot++;
1067 path->slots[0]++;
1068 }
1069 }
1070 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001071 item = btrfs_item_nr(leaf, slot);
1072 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1073
1074 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04001075 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001076 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001077 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001078 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04001079 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001080
1081 filp->f_pos = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001082 advance = 1;
1083 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1084 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001085 di_total = btrfs_item_size(leaf, item);
Chris Mason39279cc2007-06-12 06:35:45 -04001086 while(di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04001087 struct btrfs_key location;
1088
1089 name_len = btrfs_dir_name_len(leaf, di);
1090 if (name_len < 32) {
1091 name_ptr = tmp_name;
1092 } else {
1093 name_ptr = kmalloc(name_len, GFP_NOFS);
1094 BUG_ON(!name_ptr);
1095 }
1096 read_extent_buffer(leaf, name_ptr,
1097 (unsigned long)(di + 1), name_len);
1098
1099 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1100 btrfs_dir_item_key_to_cpu(leaf, di, &location);
1101
1102 over = filldir(dirent, name_ptr, name_len,
1103 found_key.offset,
1104 location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001105 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04001106
1107 if (name_ptr != tmp_name)
1108 kfree(name_ptr);
1109
Chris Mason39279cc2007-06-12 06:35:45 -04001110 if (over)
1111 goto nopos;
Chris Mason5f39d392007-10-15 16:14:19 -04001112 di_len = btrfs_dir_name_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04001113 di_cur += di_len;
1114 di = (struct btrfs_dir_item *)((char *)di + di_len);
1115 }
1116 }
1117 filp->f_pos++;
1118nopos:
1119 ret = 0;
1120err:
1121 btrfs_release_path(root, path);
1122 btrfs_free_path(path);
1123 mutex_unlock(&root->fs_info->fs_mutex);
1124 return ret;
1125}
1126
1127int btrfs_write_inode(struct inode *inode, int wait)
1128{
1129 struct btrfs_root *root = BTRFS_I(inode)->root;
1130 struct btrfs_trans_handle *trans;
1131 int ret = 0;
1132
1133 if (wait) {
1134 mutex_lock(&root->fs_info->fs_mutex);
1135 trans = btrfs_start_transaction(root, 1);
1136 btrfs_set_trans_block_group(trans, inode);
1137 ret = btrfs_commit_transaction(trans, root);
1138 mutex_unlock(&root->fs_info->fs_mutex);
1139 }
1140 return ret;
1141}
1142
1143/*
Chris Mason54aa1f42007-06-22 14:16:25 -04001144 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04001145 * inode changes. But, it is most likely to find the inode in cache.
1146 * FIXME, needs more benchmarking...there are no reasons other than performance
1147 * to keep or drop this code.
1148 */
1149void btrfs_dirty_inode(struct inode *inode)
1150{
1151 struct btrfs_root *root = BTRFS_I(inode)->root;
1152 struct btrfs_trans_handle *trans;
1153
1154 mutex_lock(&root->fs_info->fs_mutex);
1155 trans = btrfs_start_transaction(root, 1);
1156 btrfs_set_trans_block_group(trans, inode);
1157 btrfs_update_inode(trans, root, inode);
1158 btrfs_end_transaction(trans, root);
1159 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001160}
1161
1162static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1163 struct btrfs_root *root,
1164 u64 objectid,
1165 struct btrfs_block_group_cache *group,
1166 int mode)
1167{
1168 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001169 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04001170 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04001171 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04001172 int ret;
1173 int owner;
1174
Chris Mason5f39d392007-10-15 16:14:19 -04001175 path = btrfs_alloc_path();
1176 BUG_ON(!path);
1177
Chris Mason39279cc2007-06-12 06:35:45 -04001178 inode = new_inode(root->fs_info->sb);
1179 if (!inode)
1180 return ERR_PTR(-ENOMEM);
1181
Chris Masonb888db22007-08-27 16:49:44 -04001182 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1183 inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001184 BTRFS_I(inode)->root = root;
Chris Masonb888db22007-08-27 16:49:44 -04001185
Chris Mason39279cc2007-06-12 06:35:45 -04001186 if (mode & S_IFDIR)
1187 owner = 0;
1188 else
1189 owner = 1;
1190 group = btrfs_find_block_group(root, group, 0, 0, owner);
1191 BTRFS_I(inode)->block_group = group;
1192
Chris Mason5f39d392007-10-15 16:14:19 -04001193 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
1194 if (ret)
1195 goto fail;
1196
Chris Mason39279cc2007-06-12 06:35:45 -04001197 inode->i_uid = current->fsuid;
1198 inode->i_gid = current->fsgid;
1199 inode->i_mode = mode;
1200 inode->i_ino = objectid;
1201 inode->i_blocks = 0;
1202 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04001203 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1204 struct btrfs_inode_item);
1205 fill_inode_item(path->nodes[0], inode_item, inode);
1206 btrfs_mark_buffer_dirty(path->nodes[0]);
1207 btrfs_free_path(path);
1208
Chris Mason39279cc2007-06-12 06:35:45 -04001209 location = &BTRFS_I(inode)->location;
1210 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04001211 location->offset = 0;
1212 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1213
Chris Mason39279cc2007-06-12 06:35:45 -04001214 insert_inode_hash(inode);
1215 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001216fail:
1217 btrfs_free_path(path);
1218 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04001219}
1220
1221static inline u8 btrfs_inode_type(struct inode *inode)
1222{
1223 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1224}
1225
1226static int btrfs_add_link(struct btrfs_trans_handle *trans,
1227 struct dentry *dentry, struct inode *inode)
1228{
1229 int ret;
1230 struct btrfs_key key;
1231 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Chris Mason79c44582007-06-25 10:09:33 -04001232 struct inode *parent_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001233
Chris Mason39279cc2007-06-12 06:35:45 -04001234 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04001235 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1236 key.offset = 0;
1237
1238 ret = btrfs_insert_dir_item(trans, root,
1239 dentry->d_name.name, dentry->d_name.len,
1240 dentry->d_parent->d_inode->i_ino,
1241 &key, btrfs_inode_type(inode));
1242 if (ret == 0) {
Chris Mason79c44582007-06-25 10:09:33 -04001243 parent_inode = dentry->d_parent->d_inode;
1244 parent_inode->i_size += dentry->d_name.len * 2;
1245 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001246 ret = btrfs_update_inode(trans, root,
1247 dentry->d_parent->d_inode);
1248 }
1249 return ret;
1250}
1251
1252static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1253 struct dentry *dentry, struct inode *inode)
1254{
1255 int err = btrfs_add_link(trans, dentry, inode);
1256 if (!err) {
1257 d_instantiate(dentry, inode);
1258 return 0;
1259 }
1260 if (err > 0)
1261 err = -EEXIST;
1262 return err;
1263}
1264
Josef Bacik618e21d2007-07-11 10:18:17 -04001265static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1266 int mode, dev_t rdev)
1267{
1268 struct btrfs_trans_handle *trans;
1269 struct btrfs_root *root = BTRFS_I(dir)->root;
1270 struct inode *inode;
1271 int err;
1272 int drop_inode = 0;
1273 u64 objectid;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001274 unsigned long nr;
Josef Bacik618e21d2007-07-11 10:18:17 -04001275
1276 if (!new_valid_dev(rdev))
1277 return -EINVAL;
1278
1279 mutex_lock(&root->fs_info->fs_mutex);
1280 trans = btrfs_start_transaction(root, 1);
1281 btrfs_set_trans_block_group(trans, dir);
1282
1283 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1284 if (err) {
1285 err = -ENOSPC;
1286 goto out_unlock;
1287 }
1288
1289 inode = btrfs_new_inode(trans, root, objectid,
1290 BTRFS_I(dir)->block_group, mode);
1291 err = PTR_ERR(inode);
1292 if (IS_ERR(inode))
1293 goto out_unlock;
1294
1295 btrfs_set_trans_block_group(trans, inode);
1296 err = btrfs_add_nondir(trans, dentry, inode);
1297 if (err)
1298 drop_inode = 1;
1299 else {
1300 inode->i_op = &btrfs_special_inode_operations;
1301 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04001302 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04001303 }
1304 dir->i_sb->s_dirt = 1;
1305 btrfs_update_inode_block_group(trans, inode);
1306 btrfs_update_inode_block_group(trans, dir);
1307out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001308 nr = trans->blocks_used;
Josef Bacik618e21d2007-07-11 10:18:17 -04001309 btrfs_end_transaction(trans, root);
1310 mutex_unlock(&root->fs_info->fs_mutex);
1311
1312 if (drop_inode) {
1313 inode_dec_link_count(inode);
1314 iput(inode);
1315 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001316 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04001317 return err;
1318}
1319
Chris Mason39279cc2007-06-12 06:35:45 -04001320static int btrfs_create(struct inode *dir, struct dentry *dentry,
1321 int mode, struct nameidata *nd)
1322{
1323 struct btrfs_trans_handle *trans;
1324 struct btrfs_root *root = BTRFS_I(dir)->root;
1325 struct inode *inode;
1326 int err;
1327 int drop_inode = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001328 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001329 u64 objectid;
1330
1331 mutex_lock(&root->fs_info->fs_mutex);
1332 trans = btrfs_start_transaction(root, 1);
1333 btrfs_set_trans_block_group(trans, dir);
1334
1335 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1336 if (err) {
1337 err = -ENOSPC;
1338 goto out_unlock;
1339 }
1340
1341 inode = btrfs_new_inode(trans, root, objectid,
1342 BTRFS_I(dir)->block_group, mode);
1343 err = PTR_ERR(inode);
1344 if (IS_ERR(inode))
1345 goto out_unlock;
1346
1347 btrfs_set_trans_block_group(trans, inode);
1348 err = btrfs_add_nondir(trans, dentry, inode);
1349 if (err)
1350 drop_inode = 1;
1351 else {
1352 inode->i_mapping->a_ops = &btrfs_aops;
1353 inode->i_fop = &btrfs_file_operations;
1354 inode->i_op = &btrfs_file_inode_operations;
Chris Masona52d9a82007-08-27 16:49:44 -04001355 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1356 inode->i_mapping, GFP_NOFS);
Chris Mason07157aa2007-08-30 08:50:51 -04001357 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04001358 }
1359 dir->i_sb->s_dirt = 1;
1360 btrfs_update_inode_block_group(trans, inode);
1361 btrfs_update_inode_block_group(trans, dir);
1362out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001363 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001364 btrfs_end_transaction(trans, root);
1365 mutex_unlock(&root->fs_info->fs_mutex);
1366
1367 if (drop_inode) {
1368 inode_dec_link_count(inode);
1369 iput(inode);
1370 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001371 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001372 return err;
1373}
1374
1375static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1376 struct dentry *dentry)
1377{
1378 struct btrfs_trans_handle *trans;
1379 struct btrfs_root *root = BTRFS_I(dir)->root;
1380 struct inode *inode = old_dentry->d_inode;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001381 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001382 int err;
1383 int drop_inode = 0;
1384
1385 if (inode->i_nlink == 0)
1386 return -ENOENT;
1387
1388 inc_nlink(inode);
1389 mutex_lock(&root->fs_info->fs_mutex);
1390 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001391
Chris Mason39279cc2007-06-12 06:35:45 -04001392 btrfs_set_trans_block_group(trans, dir);
1393 atomic_inc(&inode->i_count);
1394 err = btrfs_add_nondir(trans, dentry, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001395
Chris Mason39279cc2007-06-12 06:35:45 -04001396 if (err)
1397 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001398
Chris Mason39279cc2007-06-12 06:35:45 -04001399 dir->i_sb->s_dirt = 1;
1400 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04001401 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001402
Chris Mason54aa1f42007-06-22 14:16:25 -04001403 if (err)
1404 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001405
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001406 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001407 btrfs_end_transaction(trans, root);
1408 mutex_unlock(&root->fs_info->fs_mutex);
1409
1410 if (drop_inode) {
1411 inode_dec_link_count(inode);
1412 iput(inode);
1413 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001414 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001415 return err;
1416}
1417
1418static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
1419 struct btrfs_root *root,
1420 u64 objectid, u64 dirid)
1421{
1422 int ret;
1423 char buf[2];
1424 struct btrfs_key key;
1425
1426 buf[0] = '.';
1427 buf[1] = '.';
1428
1429 key.objectid = objectid;
1430 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001431 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1432
1433 ret = btrfs_insert_dir_item(trans, root, buf, 1, objectid,
1434 &key, BTRFS_FT_DIR);
1435 if (ret)
1436 goto error;
Chris Mason5f39d392007-10-15 16:14:19 -04001437
Chris Mason39279cc2007-06-12 06:35:45 -04001438 key.objectid = dirid;
1439 ret = btrfs_insert_dir_item(trans, root, buf, 2, objectid,
1440 &key, BTRFS_FT_DIR);
1441 if (ret)
1442 goto error;
1443error:
1444 return ret;
1445}
1446
1447static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1448{
1449 struct inode *inode;
1450 struct btrfs_trans_handle *trans;
1451 struct btrfs_root *root = BTRFS_I(dir)->root;
1452 int err = 0;
1453 int drop_on_err = 0;
1454 u64 objectid;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001455 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001456
1457 mutex_lock(&root->fs_info->fs_mutex);
1458 trans = btrfs_start_transaction(root, 1);
1459 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04001460
Chris Mason39279cc2007-06-12 06:35:45 -04001461 if (IS_ERR(trans)) {
1462 err = PTR_ERR(trans);
1463 goto out_unlock;
1464 }
1465
1466 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1467 if (err) {
1468 err = -ENOSPC;
1469 goto out_unlock;
1470 }
1471
1472 inode = btrfs_new_inode(trans, root, objectid,
1473 BTRFS_I(dir)->block_group, S_IFDIR | mode);
1474 if (IS_ERR(inode)) {
1475 err = PTR_ERR(inode);
1476 goto out_fail;
1477 }
Chris Mason5f39d392007-10-15 16:14:19 -04001478
Chris Mason39279cc2007-06-12 06:35:45 -04001479 drop_on_err = 1;
1480 inode->i_op = &btrfs_dir_inode_operations;
1481 inode->i_fop = &btrfs_dir_file_operations;
1482 btrfs_set_trans_block_group(trans, inode);
1483
1484 err = btrfs_make_empty_dir(trans, root, inode->i_ino, dir->i_ino);
1485 if (err)
1486 goto out_fail;
1487
1488 inode->i_size = 6;
1489 err = btrfs_update_inode(trans, root, inode);
1490 if (err)
1491 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04001492
Chris Mason39279cc2007-06-12 06:35:45 -04001493 err = btrfs_add_link(trans, dentry, inode);
1494 if (err)
1495 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04001496
Chris Mason39279cc2007-06-12 06:35:45 -04001497 d_instantiate(dentry, inode);
1498 drop_on_err = 0;
1499 dir->i_sb->s_dirt = 1;
1500 btrfs_update_inode_block_group(trans, inode);
1501 btrfs_update_inode_block_group(trans, dir);
1502
1503out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001504 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001505 btrfs_end_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04001506
Chris Mason39279cc2007-06-12 06:35:45 -04001507out_unlock:
1508 mutex_unlock(&root->fs_info->fs_mutex);
1509 if (drop_on_err)
1510 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001511 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001512 return err;
1513}
1514
Chris Masona52d9a82007-08-27 16:49:44 -04001515struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
1516 size_t page_offset, u64 start, u64 end,
1517 int create)
1518{
1519 int ret;
1520 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04001521 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04001522 u64 extent_start = 0;
1523 u64 extent_end = 0;
1524 u64 objectid = inode->i_ino;
1525 u32 found_type;
1526 int failed_insert = 0;
1527 struct btrfs_path *path;
1528 struct btrfs_root *root = BTRFS_I(inode)->root;
1529 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04001530 struct extent_buffer *leaf;
1531 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04001532 struct extent_map *em = NULL;
1533 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1534 struct btrfs_trans_handle *trans = NULL;
1535
1536 path = btrfs_alloc_path();
1537 BUG_ON(!path);
1538 mutex_lock(&root->fs_info->fs_mutex);
1539
1540again:
1541 em = lookup_extent_mapping(em_tree, start, end);
1542 if (em) {
1543 goto out;
1544 }
1545 if (!em) {
1546 em = alloc_extent_map(GFP_NOFS);
1547 if (!em) {
1548 err = -ENOMEM;
1549 goto out;
1550 }
Chris Mason5f39d392007-10-15 16:14:19 -04001551 em->start = EXTENT_MAP_HOLE;
1552 em->end = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001553 }
1554 em->bdev = inode->i_sb->s_bdev;
Chris Mason179e29e2007-11-01 11:28:41 -04001555 ret = btrfs_lookup_file_extent(trans, root, path,
1556 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04001557 if (ret < 0) {
1558 err = ret;
1559 goto out;
1560 }
1561
1562 if (ret != 0) {
1563 if (path->slots[0] == 0)
1564 goto not_found;
1565 path->slots[0]--;
1566 }
1567
Chris Mason5f39d392007-10-15 16:14:19 -04001568 leaf = path->nodes[0];
1569 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04001570 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04001571 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04001572 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1573 found_type = btrfs_key_type(&found_key);
1574 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04001575 found_type != BTRFS_EXTENT_DATA_KEY) {
1576 goto not_found;
1577 }
1578
Chris Mason5f39d392007-10-15 16:14:19 -04001579 found_type = btrfs_file_extent_type(leaf, item);
1580 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04001581 if (found_type == BTRFS_FILE_EXTENT_REG) {
1582 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04001583 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04001584 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04001585 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04001586 em->start = start;
1587 if (start < extent_start) {
Chris Masonb888db22007-08-27 16:49:44 -04001588 if (end < extent_start)
1589 goto not_found;
Chris Masona52d9a82007-08-27 16:49:44 -04001590 em->end = extent_end - 1;
1591 } else {
1592 em->end = end;
1593 }
1594 goto not_found_em;
1595 }
Chris Masondb945352007-10-15 16:15:53 -04001596 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
1597 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04001598 em->start = extent_start;
1599 em->end = extent_end - 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001600 em->block_start = EXTENT_MAP_HOLE;
1601 em->block_end = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001602 goto insert;
1603 }
Chris Masondb945352007-10-15 16:15:53 -04001604 bytenr += btrfs_file_extent_offset(leaf, item);
1605 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04001606 em->block_end = em->block_start +
Chris Masondb945352007-10-15 16:15:53 -04001607 btrfs_file_extent_num_bytes(leaf, item) - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04001608 em->start = extent_start;
1609 em->end = extent_end - 1;
1610 goto insert;
1611 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04001612 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04001613 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04001614 size_t size;
1615 size_t extent_offset;
1616 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04001617
Chris Mason5f39d392007-10-15 16:14:19 -04001618 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
1619 path->slots[0]));
Yan689f9342007-10-29 11:41:07 -04001620 extent_end = (extent_start + size - 1) |
Chris Masondb945352007-10-15 16:15:53 -04001621 ((u64)root->sectorsize - 1);
Chris Masonb888db22007-08-27 16:49:44 -04001622 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04001623 em->start = start;
1624 if (start < extent_start) {
Chris Masonb888db22007-08-27 16:49:44 -04001625 if (end < extent_start)
1626 goto not_found;
Chris Mason50b78c22007-09-20 14:14:42 -04001627 em->end = extent_end;
Chris Masona52d9a82007-08-27 16:49:44 -04001628 } else {
1629 em->end = end;
1630 }
1631 goto not_found_em;
1632 }
1633 em->block_start = EXTENT_MAP_INLINE;
1634 em->block_end = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04001635
1636 if (!page) {
1637 em->start = extent_start;
1638 em->end = extent_start + size - 1;
1639 goto out;
1640 }
1641
Chris Mason35ebb932007-10-30 16:56:53 -04001642 extent_offset = ((u64)page->index << PAGE_CACHE_SHIFT) -
Yan689f9342007-10-29 11:41:07 -04001643 extent_start + page_offset;
1644 copy_size = min_t(u64, PAGE_CACHE_SIZE - page_offset,
1645 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04001646 em->start = extent_start + extent_offset;
1647 em->end = (em->start + copy_size -1) |
1648 ((u64)root->sectorsize -1);
Yan689f9342007-10-29 11:41:07 -04001649 map = kmap(page);
1650 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04001651 if (create == 0 && !PageUptodate(page)) {
1652 read_extent_buffer(leaf, map + page_offset, ptr,
1653 copy_size);
1654 flush_dcache_page(page);
1655 } else if (create && PageUptodate(page)) {
1656 if (!trans) {
1657 kunmap(page);
1658 free_extent_map(em);
1659 em = NULL;
1660 btrfs_release_path(root, path);
1661 trans = btrfs_start_transaction(root, 1);
1662 goto again;
1663 }
1664 write_extent_buffer(leaf, map + page_offset, ptr,
1665 copy_size);
1666 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04001667 }
Chris Masona52d9a82007-08-27 16:49:44 -04001668 kunmap(page);
Chris Mason3326d1b2007-10-15 16:18:25 -04001669 set_extent_uptodate(em_tree, em->start, em->end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04001670 goto insert;
1671 } else {
1672 printk("unkknown found_type %d\n", found_type);
1673 WARN_ON(1);
1674 }
1675not_found:
1676 em->start = start;
1677 em->end = end;
1678not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04001679 em->block_start = EXTENT_MAP_HOLE;
1680 em->block_end = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001681insert:
1682 btrfs_release_path(root, path);
1683 if (em->start > start || em->end < start) {
Chris Masonb888db22007-08-27 16:49:44 -04001684 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->end, start, end);
Chris Masona52d9a82007-08-27 16:49:44 -04001685 err = -EIO;
1686 goto out;
1687 }
1688 ret = add_extent_mapping(em_tree, em);
1689 if (ret == -EEXIST) {
1690 free_extent_map(em);
Chris Mason2bf5a722007-08-30 11:54:02 -04001691 em = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04001692 failed_insert++;
1693 if (failed_insert > 5) {
1694 printk("failing to insert %Lu %Lu\n", start, end);
1695 err = -EIO;
1696 goto out;
1697 }
Chris Masona52d9a82007-08-27 16:49:44 -04001698 goto again;
1699 }
1700 err = 0;
1701out:
1702 btrfs_free_path(path);
1703 if (trans) {
1704 ret = btrfs_end_transaction(trans, root);
1705 if (!err)
1706 err = ret;
1707 }
1708 mutex_unlock(&root->fs_info->fs_mutex);
1709 if (err) {
1710 free_extent_map(em);
1711 WARN_ON(1);
1712 return ERR_PTR(err);
1713 }
1714 return em;
1715}
1716
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04001717static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04001718{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04001719 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04001720}
1721
1722static int btrfs_prepare_write(struct file *file, struct page *page,
1723 unsigned from, unsigned to)
1724{
Chris Masona52d9a82007-08-27 16:49:44 -04001725 return extent_prepare_write(&BTRFS_I(page->mapping->host)->extent_tree,
1726 page->mapping->host, page, from, to,
1727 btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04001728}
1729
Chris Mason9ebefb182007-06-15 13:50:00 -04001730int btrfs_readpage(struct file *file, struct page *page)
1731{
Chris Masona52d9a82007-08-27 16:49:44 -04001732 struct extent_map_tree *tree;
1733 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1734 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04001735}
Chris Mason39279cc2007-06-12 06:35:45 -04001736static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1737{
Chris Masona52d9a82007-08-27 16:49:44 -04001738 struct extent_map_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04001739
1740
1741 if (current->flags & PF_MEMALLOC) {
1742 redirty_page_for_writepage(wbc, page);
1743 unlock_page(page);
1744 return 0;
1745 }
Chris Masona52d9a82007-08-27 16:49:44 -04001746 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1747 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
1748}
Chris Mason39279cc2007-06-12 06:35:45 -04001749
Chris Masona52d9a82007-08-27 16:49:44 -04001750static int btrfs_releasepage(struct page *page, gfp_t unused_gfp_flags)
1751{
1752 struct extent_map_tree *tree;
1753 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04001754
Chris Masona52d9a82007-08-27 16:49:44 -04001755 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1756 ret = try_release_extent_mapping(tree, page);
1757 if (ret == 1) {
1758 ClearPagePrivate(page);
1759 set_page_private(page, 0);
1760 page_cache_release(page);
1761 }
1762 return ret;
1763}
Chris Mason39279cc2007-06-12 06:35:45 -04001764
Chris Masona52d9a82007-08-27 16:49:44 -04001765static void btrfs_invalidatepage(struct page *page, unsigned long offset)
1766{
1767 struct extent_map_tree *tree;
1768
1769 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1770 extent_invalidatepage(tree, page, offset);
1771 btrfs_releasepage(page, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001772}
1773
Chris Mason9ebefb182007-06-15 13:50:00 -04001774/*
1775 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
1776 * called from a page fault handler when a page is first dirtied. Hence we must
1777 * be careful to check for EOF conditions here. We set the page up correctly
1778 * for a written page which means we get ENOSPC checking when writing into
1779 * holes and correct delalloc and unwritten extent mapping on filesystems that
1780 * support these features.
1781 *
1782 * We are not allowed to take the i_mutex here so we have to play games to
1783 * protect against truncate races as the page could now be beyond EOF. Because
1784 * vmtruncate() writes the inode size before removing pages, once we have the
1785 * page lock we can determine safely if the page is beyond EOF. If it is not
1786 * beyond EOF, then the page is guaranteed safe against truncation until we
1787 * unlock the page.
1788 */
1789int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
1790{
1791 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1792 unsigned long end;
1793 loff_t size;
1794 int ret = -EINVAL;
Chris Masona52d9a82007-08-27 16:49:44 -04001795 u64 page_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04001796
Chris Mason011410b2007-09-10 19:58:36 -04001797 down_read(&BTRFS_I(inode)->root->snap_sem);
Chris Mason9ebefb182007-06-15 13:50:00 -04001798 lock_page(page);
1799 wait_on_page_writeback(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04001800 size = i_size_read(inode);
Chris Mason35ebb932007-10-30 16:56:53 -04001801 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04001802
Chris Mason9ebefb182007-06-15 13:50:00 -04001803 if ((page->mapping != inode->i_mapping) ||
Chris Masona52d9a82007-08-27 16:49:44 -04001804 (page_start > size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04001805 /* page got truncated out from underneath us */
1806 goto out_unlock;
1807 }
1808
1809 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04001810 if (page_start + PAGE_CACHE_SIZE > size)
Chris Mason9ebefb182007-06-15 13:50:00 -04001811 end = size & ~PAGE_CACHE_MASK;
1812 else
1813 end = PAGE_CACHE_SIZE;
1814
Chris Masonb888db22007-08-27 16:49:44 -04001815 ret = btrfs_cow_one_page(inode, page, end);
Chris Mason9ebefb182007-06-15 13:50:00 -04001816
1817out_unlock:
Chris Mason011410b2007-09-10 19:58:36 -04001818 up_read(&BTRFS_I(inode)->root->snap_sem);
Chris Mason9ebefb182007-06-15 13:50:00 -04001819 unlock_page(page);
1820 return ret;
1821}
1822
Chris Mason39279cc2007-06-12 06:35:45 -04001823static void btrfs_truncate(struct inode *inode)
1824{
1825 struct btrfs_root *root = BTRFS_I(inode)->root;
1826 int ret;
1827 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001828 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001829
1830 if (!S_ISREG(inode->i_mode))
1831 return;
1832 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1833 return;
1834
1835 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1836
1837 mutex_lock(&root->fs_info->fs_mutex);
1838 trans = btrfs_start_transaction(root, 1);
1839 btrfs_set_trans_block_group(trans, inode);
1840
1841 /* FIXME, add redo link to tree so we don't leak on crash */
1842 ret = btrfs_truncate_in_trans(trans, root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001843 btrfs_update_inode(trans, root, inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001844 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04001845
Chris Mason39279cc2007-06-12 06:35:45 -04001846 ret = btrfs_end_transaction(trans, root);
1847 BUG_ON(ret);
1848 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001849 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001850}
1851
1852int btrfs_commit_write(struct file *file, struct page *page,
1853 unsigned from, unsigned to)
1854{
Chris Masona52d9a82007-08-27 16:49:44 -04001855 return extent_commit_write(&BTRFS_I(page->mapping->host)->extent_tree,
1856 page->mapping->host, page, from, to);
Chris Mason39279cc2007-06-12 06:35:45 -04001857}
1858
1859static int create_subvol(struct btrfs_root *root, char *name, int namelen)
1860{
1861 struct btrfs_trans_handle *trans;
1862 struct btrfs_key key;
1863 struct btrfs_root_item root_item;
1864 struct btrfs_inode_item *inode_item;
Chris Mason5f39d392007-10-15 16:14:19 -04001865 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001866 struct btrfs_root *new_root;
1867 struct inode *inode;
1868 struct inode *dir;
1869 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001870 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04001871 u64 objectid;
1872 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001873 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001874
1875 mutex_lock(&root->fs_info->fs_mutex);
1876 trans = btrfs_start_transaction(root, 1);
1877 BUG_ON(!trans);
1878
Chris Masondb945352007-10-15 16:15:53 -04001879 leaf = btrfs_alloc_free_block(trans, root, root->leafsize, 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001880 if (IS_ERR(leaf))
1881 return PTR_ERR(leaf);
1882
1883 btrfs_set_header_nritems(leaf, 0);
1884 btrfs_set_header_level(leaf, 0);
Chris Masondb945352007-10-15 16:15:53 -04001885 btrfs_set_header_bytenr(leaf, leaf->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001886 btrfs_set_header_generation(leaf, trans->transid);
1887 btrfs_set_header_owner(leaf, root->root_key.objectid);
1888 write_extent_buffer(leaf, root->fs_info->fsid,
1889 (unsigned long)btrfs_header_fsid(leaf),
1890 BTRFS_FSID_SIZE);
1891 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001892
1893 inode_item = &root_item.inode;
1894 memset(inode_item, 0, sizeof(*inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04001895 inode_item->generation = cpu_to_le64(1);
1896 inode_item->size = cpu_to_le64(3);
1897 inode_item->nlink = cpu_to_le32(1);
1898 inode_item->nblocks = cpu_to_le64(1);
1899 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
Chris Mason39279cc2007-06-12 06:35:45 -04001900
Chris Masondb945352007-10-15 16:15:53 -04001901 btrfs_set_root_bytenr(&root_item, leaf->start);
1902 btrfs_set_root_level(&root_item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001903 btrfs_set_root_refs(&root_item, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001904 btrfs_set_root_used(&root_item, 0);
1905
Chris Mason5eda7b52007-06-22 14:16:25 -04001906 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
1907 root_item.drop_level = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001908
1909 free_extent_buffer(leaf);
1910 leaf = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001911
1912 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
1913 0, &objectid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001914 if (ret)
1915 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001916
1917 btrfs_set_root_dirid(&root_item, new_dirid);
1918
1919 key.objectid = objectid;
1920 key.offset = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001921 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
1922 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
1923 &root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -04001924 if (ret)
1925 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001926
1927 /*
1928 * insert the directory item
1929 */
1930 key.offset = (u64)-1;
1931 dir = root->fs_info->sb->s_root->d_inode;
1932 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
1933 name, namelen, dir->i_ino, &key,
1934 BTRFS_FT_DIR);
Chris Mason54aa1f42007-06-22 14:16:25 -04001935 if (ret)
1936 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001937
1938 ret = btrfs_commit_transaction(trans, root);
Chris Mason54aa1f42007-06-22 14:16:25 -04001939 if (ret)
1940 goto fail_commit;
Chris Mason39279cc2007-06-12 06:35:45 -04001941
Josef Bacik58176a92007-08-29 15:47:34 -04001942 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
Chris Mason39279cc2007-06-12 06:35:45 -04001943 BUG_ON(!new_root);
1944
1945 trans = btrfs_start_transaction(new_root, 1);
1946 BUG_ON(!trans);
1947
1948 inode = btrfs_new_inode(trans, new_root, new_dirid,
1949 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
Chris Mason54aa1f42007-06-22 14:16:25 -04001950 if (IS_ERR(inode))
1951 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001952 inode->i_op = &btrfs_dir_inode_operations;
1953 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04001954 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001955
1956 ret = btrfs_make_empty_dir(trans, new_root, new_dirid, new_dirid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001957 if (ret)
1958 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001959
1960 inode->i_nlink = 1;
1961 inode->i_size = 6;
1962 ret = btrfs_update_inode(trans, new_root, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04001963 if (ret)
1964 goto fail;
1965fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001966 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04001967 err = btrfs_commit_transaction(trans, root);
1968 if (err && !ret)
1969 ret = err;
1970fail_commit:
Chris Mason39279cc2007-06-12 06:35:45 -04001971 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001972 btrfs_btree_balance_dirty(root, nr);
Chris Mason54aa1f42007-06-22 14:16:25 -04001973 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04001974}
1975
1976static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
1977{
1978 struct btrfs_trans_handle *trans;
1979 struct btrfs_key key;
1980 struct btrfs_root_item new_root_item;
Chris Mason5f39d392007-10-15 16:14:19 -04001981 struct extent_buffer *tmp;
Chris Mason39279cc2007-06-12 06:35:45 -04001982 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001983 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04001984 u64 objectid;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001985 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001986
1987 if (!root->ref_cows)
1988 return -EINVAL;
1989
Chris Mason011410b2007-09-10 19:58:36 -04001990 down_write(&root->snap_sem);
1991 freeze_bdev(root->fs_info->sb->s_bdev);
1992 thaw_bdev(root->fs_info->sb->s_bdev, root->fs_info->sb);
1993
Chris Mason39279cc2007-06-12 06:35:45 -04001994 mutex_lock(&root->fs_info->fs_mutex);
1995 trans = btrfs_start_transaction(root, 1);
1996 BUG_ON(!trans);
1997
1998 ret = btrfs_update_inode(trans, root, root->inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04001999 if (ret)
2000 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002001
2002 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2003 0, &objectid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002004 if (ret)
2005 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002006
2007 memcpy(&new_root_item, &root->root_item,
2008 sizeof(new_root_item));
2009
2010 key.objectid = objectid;
2011 key.offset = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002012 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
Chris Mason5f39d392007-10-15 16:14:19 -04002013
Chris Mason83df7c12007-08-27 16:49:44 -04002014 btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp);
Chris Masondb945352007-10-15 16:15:53 -04002015 btrfs_set_root_bytenr(&new_root_item, root->node->start);
2016 btrfs_set_root_level(&new_root_item, btrfs_header_level(root->node));
Chris Mason39279cc2007-06-12 06:35:45 -04002017
2018 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2019 &new_root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -04002020 if (ret)
2021 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002022
2023 /*
2024 * insert the directory item
2025 */
2026 key.offset = (u64)-1;
2027 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2028 name, namelen,
2029 root->fs_info->sb->s_root->d_inode->i_ino,
2030 &key, BTRFS_FT_DIR);
2031
Chris Mason54aa1f42007-06-22 14:16:25 -04002032 if (ret)
2033 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002034
2035 ret = btrfs_inc_root_ref(trans, root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002036 if (ret)
2037 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002038fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002039 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04002040 err = btrfs_commit_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002041
Chris Mason54aa1f42007-06-22 14:16:25 -04002042 if (err && !ret)
2043 ret = err;
Chris Mason5f39d392007-10-15 16:14:19 -04002044
Chris Mason39279cc2007-06-12 06:35:45 -04002045 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason011410b2007-09-10 19:58:36 -04002046 up_write(&root->snap_sem);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002047 btrfs_btree_balance_dirty(root, nr);
Chris Mason54aa1f42007-06-22 14:16:25 -04002048 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002049}
2050
Chris Mason86479a02007-09-10 19:58:16 -04002051static unsigned long force_ra(struct address_space *mapping,
2052 struct file_ra_state *ra, struct file *file,
2053 pgoff_t offset, pgoff_t last_index)
2054{
2055 pgoff_t req_size;
2056
2057#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2058 req_size = last_index - offset + 1;
2059 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2060 return offset;
2061#else
2062 req_size = min(last_index - offset + 1, (pgoff_t)128);
2063 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2064 return offset + req_size;
2065#endif
2066}
2067
2068int btrfs_defrag_file(struct file *file) {
2069 struct inode *inode = file->f_path.dentry->d_inode;
2070 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2071 struct page *page;
2072 unsigned long last_index;
2073 unsigned long ra_index = 0;
2074 u64 page_start;
2075 u64 page_end;
2076 unsigned long i;
2077
2078 mutex_lock(&inode->i_mutex);
2079 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2080 for (i = 0; i <= last_index; i++) {
2081 if (i == ra_index) {
2082 ra_index = force_ra(inode->i_mapping, &file->f_ra,
2083 file, ra_index, last_index);
2084 }
2085 page = grab_cache_page(inode->i_mapping, i);
2086 if (!page)
2087 goto out_unlock;
2088 if (!PageUptodate(page)) {
2089 btrfs_readpage(NULL, page);
2090 lock_page(page);
2091 if (!PageUptodate(page)) {
2092 unlock_page(page);
2093 page_cache_release(page);
2094 goto out_unlock;
2095 }
2096 }
Chris Mason35ebb932007-10-30 16:56:53 -04002097 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason86479a02007-09-10 19:58:16 -04002098 page_end = page_start + PAGE_CACHE_SIZE - 1;
2099
2100 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
2101 set_extent_delalloc(em_tree, page_start,
2102 page_end, GFP_NOFS);
2103 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
2104 set_page_dirty(page);
2105 unlock_page(page);
2106 page_cache_release(page);
2107 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2108 }
2109
2110out_unlock:
2111 mutex_unlock(&inode->i_mutex);
2112 return 0;
2113}
2114
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002115static int btrfs_ioctl_snap_create(struct btrfs_root *root, void __user *arg)
2116{
2117 struct btrfs_ioctl_vol_args vol_args;
2118 struct btrfs_dir_item *di;
2119 struct btrfs_path *path;
2120 int namelen;
2121 u64 root_dirid;
2122
2123 if (copy_from_user(&vol_args, arg, sizeof(vol_args)))
2124 return -EFAULT;
Chris Mason5f39d392007-10-15 16:14:19 -04002125
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002126 namelen = strlen(vol_args.name);
2127 if (namelen > BTRFS_VOL_NAME_MAX)
2128 return -EINVAL;
2129 if (strchr(vol_args.name, '/'))
2130 return -EINVAL;
2131
2132 path = btrfs_alloc_path();
2133 if (!path)
2134 return -ENOMEM;
2135
2136 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2137 mutex_lock(&root->fs_info->fs_mutex);
2138 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2139 path, root_dirid,
2140 vol_args.name, namelen, 0);
2141 mutex_unlock(&root->fs_info->fs_mutex);
2142 btrfs_free_path(path);
2143 if (di && !IS_ERR(di))
2144 return -EEXIST;
2145 if (IS_ERR(di))
2146 return PTR_ERR(di);
2147
2148 if (root == root->fs_info->tree_root)
2149 return create_subvol(root, vol_args.name, namelen);
2150 return create_snapshot(root, vol_args.name, namelen);
2151}
2152
2153static int btrfs_ioctl_defrag(struct file *file)
Chris Mason39279cc2007-06-12 06:35:45 -04002154{
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002155 struct inode *inode = file->f_path.dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002156 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002157
2158 switch (inode->i_mode & S_IFMT) {
2159 case S_IFDIR:
2160 mutex_lock(&root->fs_info->fs_mutex);
2161 btrfs_defrag_root(root, 0);
2162 btrfs_defrag_root(root->fs_info->extent_root, 0);
2163 mutex_unlock(&root->fs_info->fs_mutex);
2164 break;
2165 case S_IFREG:
2166 btrfs_defrag_file(file);
2167 break;
2168 }
2169
2170 return 0;
2171}
2172
2173long btrfs_ioctl(struct file *file, unsigned int
2174 cmd, unsigned long arg)
2175{
2176 struct btrfs_root *root = BTRFS_I(file->f_path.dentry->d_inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002177
2178 switch (cmd) {
2179 case BTRFS_IOC_SNAP_CREATE:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002180 return btrfs_ioctl_snap_create(root, (void __user *)arg);
Chris Mason6702ed42007-08-07 16:15:09 -04002181 case BTRFS_IOC_DEFRAG:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002182 return btrfs_ioctl_defrag(file);
Chris Mason39279cc2007-06-12 06:35:45 -04002183 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002184
2185 return -ENOTTY;
Chris Mason39279cc2007-06-12 06:35:45 -04002186}
2187
Chris Mason39279cc2007-06-12 06:35:45 -04002188/*
2189 * Called inside transaction, so use GFP_NOFS
2190 */
2191struct inode *btrfs_alloc_inode(struct super_block *sb)
2192{
2193 struct btrfs_inode *ei;
2194
2195 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2196 if (!ei)
2197 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002198 ei->last_trans = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002199 return &ei->vfs_inode;
2200}
2201
2202void btrfs_destroy_inode(struct inode *inode)
2203{
2204 WARN_ON(!list_empty(&inode->i_dentry));
2205 WARN_ON(inode->i_data.nrpages);
2206
2207 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2208}
2209
Chris Mason44ec0b72007-10-29 10:55:05 -04002210#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2211static void init_once(struct kmem_cache * cachep, void *foo)
2212#else
Chris Mason39279cc2007-06-12 06:35:45 -04002213static void init_once(void * foo, struct kmem_cache * cachep,
2214 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04002215#endif
Chris Mason39279cc2007-06-12 06:35:45 -04002216{
2217 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2218
2219 inode_init_once(&ei->vfs_inode);
2220}
2221
2222void btrfs_destroy_cachep(void)
2223{
2224 if (btrfs_inode_cachep)
2225 kmem_cache_destroy(btrfs_inode_cachep);
2226 if (btrfs_trans_handle_cachep)
2227 kmem_cache_destroy(btrfs_trans_handle_cachep);
2228 if (btrfs_transaction_cachep)
2229 kmem_cache_destroy(btrfs_transaction_cachep);
2230 if (btrfs_bit_radix_cachep)
2231 kmem_cache_destroy(btrfs_bit_radix_cachep);
2232 if (btrfs_path_cachep)
2233 kmem_cache_destroy(btrfs_path_cachep);
2234}
2235
Chris Mason86479a02007-09-10 19:58:16 -04002236struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04002237 unsigned long extra_flags,
Chris Mason44ec0b72007-10-29 10:55:05 -04002238#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2239 void (*ctor)(struct kmem_cache *, void *)
2240#else
Chris Mason92fee662007-07-25 12:31:35 -04002241 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04002242 unsigned long)
2243#endif
2244 )
Chris Mason92fee662007-07-25 12:31:35 -04002245{
2246 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2247 SLAB_MEM_SPREAD | extra_flags), ctor
2248#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2249 ,NULL
2250#endif
2251 );
2252}
2253
Chris Mason39279cc2007-06-12 06:35:45 -04002254int btrfs_init_cachep(void)
2255{
Chris Mason86479a02007-09-10 19:58:16 -04002256 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04002257 sizeof(struct btrfs_inode),
2258 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04002259 if (!btrfs_inode_cachep)
2260 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002261 btrfs_trans_handle_cachep =
2262 btrfs_cache_create("btrfs_trans_handle_cache",
2263 sizeof(struct btrfs_trans_handle),
2264 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002265 if (!btrfs_trans_handle_cachep)
2266 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002267 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04002268 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04002269 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002270 if (!btrfs_transaction_cachep)
2271 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002272 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04002273 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04002274 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002275 if (!btrfs_path_cachep)
2276 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002277 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04002278 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002279 if (!btrfs_bit_radix_cachep)
2280 goto fail;
2281 return 0;
2282fail:
2283 btrfs_destroy_cachep();
2284 return -ENOMEM;
2285}
2286
2287static int btrfs_getattr(struct vfsmount *mnt,
2288 struct dentry *dentry, struct kstat *stat)
2289{
2290 struct inode *inode = dentry->d_inode;
2291 generic_fillattr(inode, stat);
2292 stat->blksize = 256 * 1024;
2293 return 0;
2294}
2295
2296static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2297 struct inode * new_dir,struct dentry *new_dentry)
2298{
2299 struct btrfs_trans_handle *trans;
2300 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2301 struct inode *new_inode = new_dentry->d_inode;
2302 struct inode *old_inode = old_dentry->d_inode;
2303 struct timespec ctime = CURRENT_TIME;
2304 struct btrfs_path *path;
2305 struct btrfs_dir_item *di;
2306 int ret;
2307
2308 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2309 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2310 return -ENOTEMPTY;
2311 }
Chris Mason5f39d392007-10-15 16:14:19 -04002312
Chris Mason39279cc2007-06-12 06:35:45 -04002313 mutex_lock(&root->fs_info->fs_mutex);
2314 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002315
Chris Mason39279cc2007-06-12 06:35:45 -04002316 btrfs_set_trans_block_group(trans, new_dir);
2317 path = btrfs_alloc_path();
2318 if (!path) {
2319 ret = -ENOMEM;
2320 goto out_fail;
2321 }
2322
2323 old_dentry->d_inode->i_nlink++;
2324 old_dir->i_ctime = old_dir->i_mtime = ctime;
2325 new_dir->i_ctime = new_dir->i_mtime = ctime;
2326 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04002327
Chris Mason39279cc2007-06-12 06:35:45 -04002328 if (S_ISDIR(old_inode->i_mode) && old_dir != new_dir) {
2329 struct btrfs_key *location = &BTRFS_I(new_dir)->location;
Chris Mason5f39d392007-10-15 16:14:19 -04002330 struct btrfs_key old_parent_key;
Chris Mason39279cc2007-06-12 06:35:45 -04002331 di = btrfs_lookup_dir_item(trans, root, path, old_inode->i_ino,
2332 "..", 2, -1);
2333 if (IS_ERR(di)) {
2334 ret = PTR_ERR(di);
2335 goto out_fail;
2336 }
2337 if (!di) {
2338 ret = -ENOENT;
2339 goto out_fail;
2340 }
Chris Mason5f39d392007-10-15 16:14:19 -04002341 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &old_parent_key);
Chris Mason39279cc2007-06-12 06:35:45 -04002342 ret = btrfs_del_item(trans, root, path);
2343 if (ret) {
Chris Mason39279cc2007-06-12 06:35:45 -04002344 goto out_fail;
2345 }
2346 btrfs_release_path(root, path);
2347
2348 di = btrfs_lookup_dir_index_item(trans, root, path,
2349 old_inode->i_ino,
Chris Mason5f39d392007-10-15 16:14:19 -04002350 old_parent_key.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002351 "..", 2, -1);
2352 if (IS_ERR(di)) {
2353 ret = PTR_ERR(di);
2354 goto out_fail;
2355 }
2356 if (!di) {
2357 ret = -ENOENT;
2358 goto out_fail;
2359 }
2360 ret = btrfs_del_item(trans, root, path);
2361 if (ret) {
Chris Mason39279cc2007-06-12 06:35:45 -04002362 goto out_fail;
2363 }
2364 btrfs_release_path(root, path);
2365
2366 ret = btrfs_insert_dir_item(trans, root, "..", 2,
2367 old_inode->i_ino, location,
2368 BTRFS_FT_DIR);
2369 if (ret)
2370 goto out_fail;
2371 }
2372
2373
2374 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2375 if (ret)
2376 goto out_fail;
2377
2378 if (new_inode) {
2379 new_inode->i_ctime = CURRENT_TIME;
2380 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2381 if (ret)
2382 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002383 }
2384 ret = btrfs_add_link(trans, new_dentry, old_inode);
2385 if (ret)
2386 goto out_fail;
2387
2388out_fail:
2389 btrfs_free_path(path);
2390 btrfs_end_transaction(trans, root);
2391 mutex_unlock(&root->fs_info->fs_mutex);
2392 return ret;
2393}
2394
2395static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2396 const char *symname)
2397{
2398 struct btrfs_trans_handle *trans;
2399 struct btrfs_root *root = BTRFS_I(dir)->root;
2400 struct btrfs_path *path;
2401 struct btrfs_key key;
2402 struct inode *inode;
2403 int err;
2404 int drop_inode = 0;
2405 u64 objectid;
2406 int name_len;
2407 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04002408 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04002409 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04002410 struct extent_buffer *leaf;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002411 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002412
2413 name_len = strlen(symname) + 1;
2414 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2415 return -ENAMETOOLONG;
2416 mutex_lock(&root->fs_info->fs_mutex);
2417 trans = btrfs_start_transaction(root, 1);
2418 btrfs_set_trans_block_group(trans, dir);
2419
2420 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2421 if (err) {
2422 err = -ENOSPC;
2423 goto out_unlock;
2424 }
2425
2426 inode = btrfs_new_inode(trans, root, objectid,
2427 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2428 err = PTR_ERR(inode);
2429 if (IS_ERR(inode))
2430 goto out_unlock;
2431
2432 btrfs_set_trans_block_group(trans, inode);
2433 err = btrfs_add_nondir(trans, dentry, inode);
2434 if (err)
2435 drop_inode = 1;
2436 else {
2437 inode->i_mapping->a_ops = &btrfs_aops;
2438 inode->i_fop = &btrfs_file_operations;
2439 inode->i_op = &btrfs_file_inode_operations;
Chris Masona52d9a82007-08-27 16:49:44 -04002440 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
2441 inode->i_mapping, GFP_NOFS);
Chris Mason07157aa2007-08-30 08:50:51 -04002442 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002443 }
2444 dir->i_sb->s_dirt = 1;
2445 btrfs_update_inode_block_group(trans, inode);
2446 btrfs_update_inode_block_group(trans, dir);
2447 if (drop_inode)
2448 goto out_unlock;
2449
2450 path = btrfs_alloc_path();
2451 BUG_ON(!path);
2452 key.objectid = inode->i_ino;
2453 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002454 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2455 datasize = btrfs_file_extent_calc_inline_size(name_len);
2456 err = btrfs_insert_empty_item(trans, root, path, &key,
2457 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04002458 if (err) {
2459 drop_inode = 1;
2460 goto out_unlock;
2461 }
Chris Mason5f39d392007-10-15 16:14:19 -04002462 leaf = path->nodes[0];
2463 ei = btrfs_item_ptr(leaf, path->slots[0],
2464 struct btrfs_file_extent_item);
2465 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
2466 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04002467 BTRFS_FILE_EXTENT_INLINE);
2468 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04002469 write_extent_buffer(leaf, symname, ptr, name_len);
2470 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002471 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04002472
Chris Mason39279cc2007-06-12 06:35:45 -04002473 inode->i_op = &btrfs_symlink_inode_operations;
2474 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2475 inode->i_size = name_len - 1;
Chris Mason54aa1f42007-06-22 14:16:25 -04002476 err = btrfs_update_inode(trans, root, inode);
2477 if (err)
2478 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002479
2480out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002481 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04002482 btrfs_end_transaction(trans, root);
2483 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04002484 if (drop_inode) {
2485 inode_dec_link_count(inode);
2486 iput(inode);
2487 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002488 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002489 return err;
2490}
2491
2492static struct inode_operations btrfs_dir_inode_operations = {
2493 .lookup = btrfs_lookup,
2494 .create = btrfs_create,
2495 .unlink = btrfs_unlink,
2496 .link = btrfs_link,
2497 .mkdir = btrfs_mkdir,
2498 .rmdir = btrfs_rmdir,
2499 .rename = btrfs_rename,
2500 .symlink = btrfs_symlink,
2501 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04002502 .mknod = btrfs_mknod,
Chris Mason39279cc2007-06-12 06:35:45 -04002503};
2504
2505static struct inode_operations btrfs_dir_ro_inode_operations = {
2506 .lookup = btrfs_lookup,
2507};
2508
2509static struct file_operations btrfs_dir_file_operations = {
2510 .llseek = generic_file_llseek,
2511 .read = generic_read_dir,
2512 .readdir = btrfs_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002513 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002514#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002515 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002516#endif
2517};
2518
Chris Mason07157aa2007-08-30 08:50:51 -04002519static struct extent_map_ops btrfs_extent_map_ops = {
2520 .fill_delalloc = run_delalloc_range,
2521 .writepage_io_hook = btrfs_writepage_io_hook,
2522 .readpage_io_hook = btrfs_readpage_io_hook,
2523 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
2524};
2525
Chris Mason39279cc2007-06-12 06:35:45 -04002526static struct address_space_operations btrfs_aops = {
2527 .readpage = btrfs_readpage,
2528 .writepage = btrfs_writepage,
2529 .sync_page = block_sync_page,
2530 .prepare_write = btrfs_prepare_write,
2531 .commit_write = btrfs_commit_write,
2532 .bmap = btrfs_bmap,
Chris Masona52d9a82007-08-27 16:49:44 -04002533 .invalidatepage = btrfs_invalidatepage,
2534 .releasepage = btrfs_releasepage,
2535 .set_page_dirty = __set_page_dirty_nobuffers,
Chris Mason39279cc2007-06-12 06:35:45 -04002536};
2537
2538static struct address_space_operations btrfs_symlink_aops = {
2539 .readpage = btrfs_readpage,
2540 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04002541 .invalidatepage = btrfs_invalidatepage,
2542 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04002543};
2544
2545static struct inode_operations btrfs_file_inode_operations = {
2546 .truncate = btrfs_truncate,
2547 .getattr = btrfs_getattr,
2548 .setattr = btrfs_setattr,
2549};
2550
Josef Bacik618e21d2007-07-11 10:18:17 -04002551static struct inode_operations btrfs_special_inode_operations = {
2552 .getattr = btrfs_getattr,
2553 .setattr = btrfs_setattr,
2554};
2555
Chris Mason39279cc2007-06-12 06:35:45 -04002556static struct inode_operations btrfs_symlink_inode_operations = {
2557 .readlink = generic_readlink,
2558 .follow_link = page_follow_link_light,
2559 .put_link = page_put_link,
2560};