blob: bb70db0c9df440b06c952edee8db326d9599851f [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason39279cc2007-06-12 06:35:45 -040019#include <linux/buffer_head.h>
20#include <linux/fs.h>
21#include <linux/pagemap.h>
22#include <linux/highmem.h>
23#include <linux/time.h>
24#include <linux/init.h>
25#include <linux/string.h>
26#include <linux/smp_lock.h>
27#include <linux/backing-dev.h>
28#include <linux/mpage.h>
29#include <linux/swap.h>
30#include <linux/writeback.h>
31#include <linux/statfs.h>
32#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040033#include <linux/bit_spinlock.h>
Chris Mason92fee662007-07-25 12:31:35 -040034#include <linux/version.h>
Josef Bacik5103e942007-11-16 11:45:54 -050035#include <linux/xattr.h>
Chris Mason39279cc2007-06-12 06:35:45 -040036#include "ctree.h"
37#include "disk-io.h"
38#include "transaction.h"
39#include "btrfs_inode.h"
40#include "ioctl.h"
41#include "print-tree.h"
42
43struct btrfs_iget_args {
44 u64 ino;
45 struct btrfs_root *root;
46};
47
48static struct inode_operations btrfs_dir_inode_operations;
49static struct inode_operations btrfs_symlink_inode_operations;
50static struct inode_operations btrfs_dir_ro_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -040051static struct inode_operations btrfs_special_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -040052static struct inode_operations btrfs_file_inode_operations;
53static struct address_space_operations btrfs_aops;
54static struct address_space_operations btrfs_symlink_aops;
55static struct file_operations btrfs_dir_file_operations;
Chris Mason07157aa2007-08-30 08:50:51 -040056static struct extent_map_ops btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040057
58static struct kmem_cache *btrfs_inode_cachep;
59struct kmem_cache *btrfs_trans_handle_cachep;
60struct kmem_cache *btrfs_transaction_cachep;
61struct kmem_cache *btrfs_bit_radix_cachep;
62struct kmem_cache *btrfs_path_cachep;
63
64#define S_SHIFT 12
65static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
66 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
67 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
68 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
69 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
70 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
71 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
72 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
73};
74
Chris Masonb888db22007-08-27 16:49:44 -040075static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
76{
77 struct btrfs_root *root = BTRFS_I(inode)->root;
78 struct btrfs_trans_handle *trans;
79 struct btrfs_key ins;
80 u64 alloc_hint = 0;
Chris Masondb945352007-10-15 16:15:53 -040081 u64 num_bytes;
Chris Masonb888db22007-08-27 16:49:44 -040082 int ret;
Chris Masondb945352007-10-15 16:15:53 -040083 u64 blocksize = root->sectorsize;
Chris Masonb888db22007-08-27 16:49:44 -040084
85 mutex_lock(&root->fs_info->fs_mutex);
86 trans = btrfs_start_transaction(root, 1);
87 btrfs_set_trans_block_group(trans, inode);
88 BUG_ON(!trans);
Chris Masondb945352007-10-15 16:15:53 -040089 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonb888db22007-08-27 16:49:44 -040090 ret = btrfs_drop_extents(trans, root, inode,
Chris Mason3326d1b2007-10-15 16:18:25 -040091 start, start + num_bytes, start, &alloc_hint);
Chris Masondb945352007-10-15 16:15:53 -040092
Chris Mason179e29e2007-11-01 11:28:41 -040093 if (alloc_hint == EXTENT_MAP_INLINE)
94 goto out;
95
Chris Mason7bb86312007-12-11 09:25:06 -050096 ret = btrfs_alloc_extent(trans, root, num_bytes,
97 root->root_key.objectid, trans->transid,
98 inode->i_ino, start, 0,
Chris Masonb888db22007-08-27 16:49:44 -040099 alloc_hint, (u64)-1, &ins, 1);
100 if (ret) {
101 WARN_ON(1);
102 goto out;
103 }
104 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
105 start, ins.objectid, ins.offset,
106 ins.offset);
107out:
108 btrfs_end_transaction(trans, root);
109 mutex_unlock(&root->fs_info->fs_mutex);
110 return ret;
111}
112
Chris Mason07157aa2007-08-30 08:50:51 -0400113int btrfs_writepage_io_hook(struct page *page, u64 start, u64 end)
114{
115 struct inode *inode = page->mapping->host;
116 struct btrfs_root *root = BTRFS_I(inode)->root;
117 struct btrfs_trans_handle *trans;
118 char *kaddr;
119 int ret;
Chris Mason35ebb932007-10-30 16:56:53 -0400120 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason07157aa2007-08-30 08:50:51 -0400121 size_t offset = start - page_start;
122
123 mutex_lock(&root->fs_info->fs_mutex);
124 trans = btrfs_start_transaction(root, 1);
125 btrfs_set_trans_block_group(trans, inode);
126 kaddr = kmap(page);
Chris Masonf578d4b2007-10-25 15:42:56 -0400127 btrfs_csum_file_block(trans, root, inode, inode->i_ino,
Chris Mason07157aa2007-08-30 08:50:51 -0400128 start, kaddr + offset, end - start + 1);
129 kunmap(page);
130 ret = btrfs_end_transaction(trans, root);
131 BUG_ON(ret);
132 mutex_unlock(&root->fs_info->fs_mutex);
133 return ret;
134}
135
136int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
137{
138 int ret = 0;
139 struct inode *inode = page->mapping->host;
140 struct btrfs_root *root = BTRFS_I(inode)->root;
141 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
142 struct btrfs_csum_item *item;
143 struct btrfs_path *path = NULL;
Chris Masonff79f812007-10-15 16:22:25 -0400144 u32 csum;
Chris Mason07157aa2007-08-30 08:50:51 -0400145
146 mutex_lock(&root->fs_info->fs_mutex);
147 path = btrfs_alloc_path();
148 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
149 if (IS_ERR(item)) {
150 ret = PTR_ERR(item);
151 /* a csum that isn't present is a preallocated region. */
152 if (ret == -ENOENT || ret == -EFBIG)
153 ret = 0;
Chris Masonff79f812007-10-15 16:22:25 -0400154 csum = 0;
Chris Mason07157aa2007-08-30 08:50:51 -0400155 goto out;
156 }
Chris Masonff79f812007-10-15 16:22:25 -0400157 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
158 BTRFS_CRC32_SIZE);
159 set_state_private(em_tree, start, csum);
Chris Mason07157aa2007-08-30 08:50:51 -0400160out:
161 if (path)
162 btrfs_free_path(path);
163 mutex_unlock(&root->fs_info->fs_mutex);
164 return ret;
165}
166
167int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end)
168{
Chris Mason35ebb932007-10-30 16:56:53 -0400169 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -0400170 struct inode *inode = page->mapping->host;
Chris Mason07157aa2007-08-30 08:50:51 -0400171 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
172 char *kaddr;
173 u64 private;
174 int ret;
Chris Masonff79f812007-10-15 16:22:25 -0400175 struct btrfs_root *root = BTRFS_I(inode)->root;
176 u32 csum = ~(u32)0;
Jens Axboebbf0d002007-10-19 09:23:07 -0400177 unsigned long flags;
Chris Mason07157aa2007-08-30 08:50:51 -0400178
179 ret = get_state_private(em_tree, start, &private);
Jens Axboebbf0d002007-10-19 09:23:07 -0400180 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400181 kaddr = kmap_atomic(page, KM_IRQ0);
182 if (ret) {
183 goto zeroit;
184 }
Chris Masonff79f812007-10-15 16:22:25 -0400185 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
186 btrfs_csum_final(csum, (char *)&csum);
187 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -0400188 goto zeroit;
189 }
190 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400191 local_irq_restore(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400192 return 0;
193
194zeroit:
195 printk("btrfs csum failed ino %lu off %llu\n",
196 page->mapping->host->i_ino, (unsigned long long)start);
Chris Masondb945352007-10-15 16:15:53 -0400197 memset(kaddr + offset, 1, end - start + 1);
198 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -0400199 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400200 local_irq_restore(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400201 return 0;
202}
Chris Masonb888db22007-08-27 16:49:44 -0400203
Chris Mason39279cc2007-06-12 06:35:45 -0400204void btrfs_read_locked_inode(struct inode *inode)
205{
206 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400207 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400208 struct btrfs_inode_item *inode_item;
Chris Mason5f39d392007-10-15 16:14:19 -0400209 struct btrfs_inode_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -0400210 struct btrfs_root *root = BTRFS_I(inode)->root;
211 struct btrfs_key location;
212 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -0400213 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -0400214 int ret;
215
216 path = btrfs_alloc_path();
217 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400218 mutex_lock(&root->fs_info->fs_mutex);
219
220 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
221 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400222 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -0400223 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -0400224
Chris Mason5f39d392007-10-15 16:14:19 -0400225 leaf = path->nodes[0];
226 inode_item = btrfs_item_ptr(leaf, path->slots[0],
227 struct btrfs_inode_item);
228
229 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
230 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
231 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
232 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
233 inode->i_size = btrfs_inode_size(leaf, inode_item);
234
235 tspec = btrfs_inode_atime(inode_item);
236 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
237 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
238
239 tspec = btrfs_inode_mtime(inode_item);
240 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
241 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
242
243 tspec = btrfs_inode_ctime(inode_item);
244 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
245 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
246
247 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
248 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik618e21d2007-07-11 10:18:17 -0400249 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400250 rdev = btrfs_inode_rdev(leaf, inode_item);
251
252 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -0400253 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
254 alloc_group_block);
255
256 btrfs_free_path(path);
257 inode_item = NULL;
258
259 mutex_unlock(&root->fs_info->fs_mutex);
260
261 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -0400262 case S_IFREG:
263 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason07157aa2007-08-30 08:50:51 -0400264 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -0400265 inode->i_fop = &btrfs_file_operations;
266 inode->i_op = &btrfs_file_inode_operations;
267 break;
268 case S_IFDIR:
269 inode->i_fop = &btrfs_dir_file_operations;
270 if (root == root->fs_info->tree_root)
271 inode->i_op = &btrfs_dir_ro_inode_operations;
272 else
273 inode->i_op = &btrfs_dir_inode_operations;
274 break;
275 case S_IFLNK:
276 inode->i_op = &btrfs_symlink_inode_operations;
277 inode->i_mapping->a_ops = &btrfs_symlink_aops;
278 break;
Josef Bacik618e21d2007-07-11 10:18:17 -0400279 default:
280 init_special_inode(inode, inode->i_mode, rdev);
281 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400282 }
283 return;
284
285make_bad:
286 btrfs_release_path(root, path);
287 btrfs_free_path(path);
288 mutex_unlock(&root->fs_info->fs_mutex);
289 make_bad_inode(inode);
290}
291
Chris Mason5f39d392007-10-15 16:14:19 -0400292static void fill_inode_item(struct extent_buffer *leaf,
293 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -0400294 struct inode *inode)
295{
Chris Mason5f39d392007-10-15 16:14:19 -0400296 btrfs_set_inode_uid(leaf, item, inode->i_uid);
297 btrfs_set_inode_gid(leaf, item, inode->i_gid);
298 btrfs_set_inode_size(leaf, item, inode->i_size);
299 btrfs_set_inode_mode(leaf, item, inode->i_mode);
300 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
301
302 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
303 inode->i_atime.tv_sec);
304 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
305 inode->i_atime.tv_nsec);
306
307 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
308 inode->i_mtime.tv_sec);
309 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
310 inode->i_mtime.tv_nsec);
311
312 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
313 inode->i_ctime.tv_sec);
314 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
315 inode->i_ctime.tv_nsec);
316
317 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
318 btrfs_set_inode_generation(leaf, item, inode->i_generation);
319 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
320 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -0400321 BTRFS_I(inode)->block_group->key.objectid);
322}
323
Chris Masona52d9a82007-08-27 16:49:44 -0400324int btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -0400325 struct btrfs_root *root,
326 struct inode *inode)
327{
328 struct btrfs_inode_item *inode_item;
329 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400330 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400331 int ret;
332
333 path = btrfs_alloc_path();
334 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400335 ret = btrfs_lookup_inode(trans, root, path,
336 &BTRFS_I(inode)->location, 1);
337 if (ret) {
338 if (ret > 0)
339 ret = -ENOENT;
340 goto failed;
341 }
342
Chris Mason5f39d392007-10-15 16:14:19 -0400343 leaf = path->nodes[0];
344 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400345 struct btrfs_inode_item);
346
Chris Mason5f39d392007-10-15 16:14:19 -0400347 fill_inode_item(leaf, inode_item, inode);
348 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400349 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400350 ret = 0;
351failed:
352 btrfs_release_path(root, path);
353 btrfs_free_path(path);
354 return ret;
355}
356
357
358static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
359 struct btrfs_root *root,
360 struct inode *dir,
361 struct dentry *dentry)
362{
363 struct btrfs_path *path;
364 const char *name = dentry->d_name.name;
365 int name_len = dentry->d_name.len;
366 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400367 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400368 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -0400369 struct btrfs_key key;
Chris Mason39279cc2007-06-12 06:35:45 -0400370
371 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400372 if (!path) {
373 ret = -ENOMEM;
374 goto err;
375 }
376
Chris Mason39279cc2007-06-12 06:35:45 -0400377 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
378 name, name_len, -1);
379 if (IS_ERR(di)) {
380 ret = PTR_ERR(di);
381 goto err;
382 }
383 if (!di) {
384 ret = -ENOENT;
385 goto err;
386 }
Chris Mason5f39d392007-10-15 16:14:19 -0400387 leaf = path->nodes[0];
388 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -0400389 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -0400390 if (ret)
391 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -0400392 btrfs_release_path(root, path);
393
394 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Chris Mason5f39d392007-10-15 16:14:19 -0400395 key.objectid, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -0400396 if (IS_ERR(di)) {
397 ret = PTR_ERR(di);
398 goto err;
399 }
400 if (!di) {
401 ret = -ENOENT;
402 goto err;
403 }
404 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason39279cc2007-06-12 06:35:45 -0400405
406 dentry->d_inode->i_ctime = dir->i_ctime;
407err:
408 btrfs_free_path(path);
409 if (!ret) {
410 dir->i_size -= name_len * 2;
Chris Mason79c44582007-06-25 10:09:33 -0400411 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -0400412 btrfs_update_inode(trans, root, dir);
413 drop_nlink(dentry->d_inode);
Chris Mason54aa1f42007-06-22 14:16:25 -0400414 ret = btrfs_update_inode(trans, root, dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400415 dir->i_sb->s_dirt = 1;
416 }
417 return ret;
418}
419
420static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
421{
422 struct btrfs_root *root;
423 struct btrfs_trans_handle *trans;
424 int ret;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400425 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -0400426
427 root = BTRFS_I(dir)->root;
428 mutex_lock(&root->fs_info->fs_mutex);
429 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400430
Chris Mason39279cc2007-06-12 06:35:45 -0400431 btrfs_set_trans_block_group(trans, dir);
432 ret = btrfs_unlink_trans(trans, root, dir, dentry);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400433 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -0400434
Chris Mason39279cc2007-06-12 06:35:45 -0400435 btrfs_end_transaction(trans, root);
436 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400437 btrfs_btree_balance_dirty(root, nr);
Chris Mason5f39d392007-10-15 16:14:19 -0400438
Chris Mason39279cc2007-06-12 06:35:45 -0400439 return ret;
440}
441
442static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
443{
444 struct inode *inode = dentry->d_inode;
445 int err;
446 int ret;
447 struct btrfs_root *root = BTRFS_I(dir)->root;
448 struct btrfs_path *path;
449 struct btrfs_key key;
450 struct btrfs_trans_handle *trans;
451 struct btrfs_key found_key;
452 int found_type;
Chris Mason5f39d392007-10-15 16:14:19 -0400453 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400454 char *goodnames = "..";
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400455 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -0400456
Yan134d4512007-10-25 15:49:25 -0400457 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
458 return -ENOTEMPTY;
459
Chris Mason39279cc2007-06-12 06:35:45 -0400460 path = btrfs_alloc_path();
461 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400462 mutex_lock(&root->fs_info->fs_mutex);
463 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400464
Chris Mason39279cc2007-06-12 06:35:45 -0400465 btrfs_set_trans_block_group(trans, dir);
466 key.objectid = inode->i_ino;
467 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -0400468 key.type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -0400469 while(1) {
470 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
471 if (ret < 0) {
472 err = ret;
473 goto out;
474 }
475 BUG_ON(ret == 0);
476 if (path->slots[0] == 0) {
477 err = -ENOENT;
478 goto out;
479 }
480 path->slots[0]--;
Chris Mason5f39d392007-10-15 16:14:19 -0400481 leaf = path->nodes[0];
482 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason39279cc2007-06-12 06:35:45 -0400483 found_type = btrfs_key_type(&found_key);
484 if (found_key.objectid != inode->i_ino) {
485 err = -ENOENT;
486 goto out;
487 }
488 if ((found_type != BTRFS_DIR_ITEM_KEY &&
489 found_type != BTRFS_DIR_INDEX_KEY) ||
490 (!btrfs_match_dir_item_name(root, path, goodnames, 2) &&
491 !btrfs_match_dir_item_name(root, path, goodnames, 1))) {
492 err = -ENOTEMPTY;
493 goto out;
494 }
495 ret = btrfs_del_item(trans, root, path);
496 BUG_ON(ret);
497
498 if (found_type == BTRFS_DIR_ITEM_KEY && found_key.offset == 1)
499 break;
500 btrfs_release_path(root, path);
501 }
502 ret = 0;
503 btrfs_release_path(root, path);
504
505 /* now the directory is empty */
506 err = btrfs_unlink_trans(trans, root, dir, dentry);
507 if (!err) {
508 inode->i_size = 0;
509 }
510out:
511 btrfs_release_path(root, path);
512 btrfs_free_path(path);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400513 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -0400514 ret = btrfs_end_transaction(trans, root);
Yan134d4512007-10-25 15:49:25 -0400515 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400516 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -0400517 if (ret && !err)
518 err = ret;
519 return err;
520}
521
522static int btrfs_free_inode(struct btrfs_trans_handle *trans,
523 struct btrfs_root *root,
524 struct inode *inode)
525{
526 struct btrfs_path *path;
527 int ret;
528
529 clear_inode(inode);
530
531 path = btrfs_alloc_path();
532 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400533 ret = btrfs_lookup_inode(trans, root, path,
534 &BTRFS_I(inode)->location, -1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400535 if (ret > 0)
536 ret = -ENOENT;
537 if (!ret)
538 ret = btrfs_del_item(trans, root, path);
Chris Mason39279cc2007-06-12 06:35:45 -0400539 btrfs_free_path(path);
540 return ret;
541}
542
543/*
Chris Mason39279cc2007-06-12 06:35:45 -0400544 * this can truncate away extent items, csum items and directory items.
545 * It starts at a high offset and removes keys until it can't find
546 * any higher than i_size.
547 *
548 * csum items that cross the new i_size are truncated to the new size
549 * as well.
550 */
551static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
552 struct btrfs_root *root,
553 struct inode *inode)
554{
555 int ret;
556 struct btrfs_path *path;
557 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400558 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -0400559 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -0400560 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400561 struct btrfs_file_extent_item *fi;
562 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -0400563 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400564 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500565 u64 root_gen = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400566 int found_extent;
567 int del_item;
Chris Mason179e29e2007-11-01 11:28:41 -0400568 int extent_type = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400569
Chris Masona52d9a82007-08-27 16:49:44 -0400570 btrfs_drop_extent_cache(inode, inode->i_size, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -0400571 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -0400572 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400573 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -0400574
Chris Mason39279cc2007-06-12 06:35:45 -0400575 /* FIXME, add redo link to tree so we don't leak on crash */
576 key.objectid = inode->i_ino;
577 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -0400578 key.type = (u8)-1;
579
Chris Mason39279cc2007-06-12 06:35:45 -0400580 while(1) {
581 btrfs_init_path(path);
582 fi = NULL;
583 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
584 if (ret < 0) {
585 goto error;
586 }
587 if (ret > 0) {
588 BUG_ON(path->slots[0] == 0);
589 path->slots[0]--;
590 }
Chris Mason5f39d392007-10-15 16:14:19 -0400591 leaf = path->nodes[0];
592 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
593 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -0400594
Chris Mason5f39d392007-10-15 16:14:19 -0400595 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -0400596 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400597
Chris Mason39279cc2007-06-12 06:35:45 -0400598 if (found_type != BTRFS_CSUM_ITEM_KEY &&
599 found_type != BTRFS_DIR_ITEM_KEY &&
600 found_type != BTRFS_DIR_INDEX_KEY &&
601 found_type != BTRFS_EXTENT_DATA_KEY)
602 break;
603
Chris Mason5f39d392007-10-15 16:14:19 -0400604 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -0400605 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -0400606 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400607 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -0400608 extent_type = btrfs_file_extent_type(leaf, fi);
609 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -0400610 item_end +=
Chris Masondb945352007-10-15 16:15:53 -0400611 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -0400612 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
613 struct btrfs_item *item = btrfs_item_nr(leaf,
614 path->slots[0]);
615 item_end += btrfs_file_extent_inline_len(leaf,
616 item);
Chris Mason39279cc2007-06-12 06:35:45 -0400617 }
Yan008630c2007-11-07 13:31:09 -0500618 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -0400619 }
620 if (found_type == BTRFS_CSUM_ITEM_KEY) {
621 ret = btrfs_csum_truncate(trans, root, path,
622 inode->i_size);
623 BUG_ON(ret);
624 }
Yan008630c2007-11-07 13:31:09 -0500625 if (item_end < inode->i_size) {
Chris Masonb888db22007-08-27 16:49:44 -0400626 if (found_type == BTRFS_DIR_ITEM_KEY) {
627 found_type = BTRFS_INODE_ITEM_KEY;
628 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
629 found_type = BTRFS_CSUM_ITEM_KEY;
630 } else if (found_type) {
631 found_type--;
632 } else {
633 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400634 }
Yana61721d2007-09-17 11:08:38 -0400635 btrfs_set_key_type(&key, found_type);
Yan65555a02007-10-25 15:42:57 -0400636 btrfs_release_path(root, path);
Chris Masonb888db22007-08-27 16:49:44 -0400637 continue;
Chris Mason39279cc2007-06-12 06:35:45 -0400638 }
Chris Mason5f39d392007-10-15 16:14:19 -0400639 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -0400640 del_item = 1;
641 else
642 del_item = 0;
643 found_extent = 0;
644
645 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -0400646 if (found_type != BTRFS_EXTENT_DATA_KEY)
647 goto delete;
648
649 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -0400650 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -0400651 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400652 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -0400653 u64 orig_num_bytes =
654 btrfs_file_extent_num_bytes(leaf, fi);
655 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -0400656 found_key.offset + root->sectorsize - 1;
Chris Masondb945352007-10-15 16:15:53 -0400657 btrfs_set_file_extent_num_bytes(leaf, fi,
658 extent_num_bytes);
659 num_dec = (orig_num_bytes -
660 extent_num_bytes) >> 9;
Yanbab9fb02007-09-17 11:13:11 -0400661 if (extent_start != 0) {
662 inode->i_blocks -= num_dec;
663 }
Chris Mason5f39d392007-10-15 16:14:19 -0400664 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -0400665 } else {
Chris Masondb945352007-10-15 16:15:53 -0400666 extent_num_bytes =
667 btrfs_file_extent_disk_num_bytes(leaf,
668 fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400669 /* FIXME blocksize != 4096 */
Chris Masondb945352007-10-15 16:15:53 -0400670 num_dec = btrfs_file_extent_num_bytes(leaf,
671 fi) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -0400672 if (extent_start != 0) {
673 found_extent = 1;
674 inode->i_blocks -= num_dec;
675 }
Chris Mason7bb86312007-12-11 09:25:06 -0500676 if (leaf == root->node) {
677 root_gen =
678 btrfs_header_generation(leaf);
679 } else {
680 struct extent_buffer *parent;
681 parent = path->nodes[1];
682 root_gen =
683 btrfs_header_generation(parent);
684 }
Chris Mason39279cc2007-06-12 06:35:45 -0400685 }
Chris Mason179e29e2007-11-01 11:28:41 -0400686 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE &&
687 !del_item) {
688 u32 newsize = inode->i_size - found_key.offset;
689 newsize = btrfs_file_extent_calc_inline_size(newsize);
690 ret = btrfs_truncate_item(trans, root, path,
691 newsize, 1);
692 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -0400693 }
Chris Mason179e29e2007-11-01 11:28:41 -0400694delete:
Chris Mason39279cc2007-06-12 06:35:45 -0400695 if (del_item) {
696 ret = btrfs_del_item(trans, root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400697 if (ret)
698 goto error;
Chris Mason39279cc2007-06-12 06:35:45 -0400699 } else {
700 break;
701 }
702 btrfs_release_path(root, path);
703 if (found_extent) {
704 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -0500705 extent_num_bytes,
706 root->root_key.objectid,
707 root_gen, inode->i_ino,
708 found_key.offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -0400709 BUG_ON(ret);
710 }
711 }
712 ret = 0;
713error:
714 btrfs_release_path(root, path);
715 btrfs_free_path(path);
716 inode->i_sb->s_dirt = 1;
717 return ret;
718}
719
Chris Masonb888db22007-08-27 16:49:44 -0400720static int btrfs_cow_one_page(struct inode *inode, struct page *page,
Chris Masona52d9a82007-08-27 16:49:44 -0400721 size_t zero_start)
Chris Mason39279cc2007-06-12 06:35:45 -0400722{
Chris Mason39279cc2007-06-12 06:35:45 -0400723 char *kaddr;
724 int ret = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400725 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason35ebb932007-10-30 16:56:53 -0400726 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masonb888db22007-08-27 16:49:44 -0400727 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Mason39279cc2007-06-12 06:35:45 -0400728
Christoph Hellwigb3cfa352007-09-17 11:25:58 -0400729 set_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400730
Chris Masonb888db22007-08-27 16:49:44 -0400731 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
732 set_extent_delalloc(&BTRFS_I(inode)->extent_tree, page_start,
733 page_end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -0400734 if (zero_start != PAGE_CACHE_SIZE) {
Chris Masonb888db22007-08-27 16:49:44 -0400735 kaddr = kmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400736 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
737 flush_dcache_page(page);
Chris Masonb888db22007-08-27 16:49:44 -0400738 kunmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400739 }
Chris Masonb888db22007-08-27 16:49:44 -0400740 set_page_dirty(page);
741 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -0400742
Chris Masona52d9a82007-08-27 16:49:44 -0400743 return ret;
744}
745
746/*
747 * taken from block_truncate_page, but does cow as it zeros out
748 * any bytes left in the last page in the file.
749 */
750static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
751{
752 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -0400753 struct btrfs_root *root = BTRFS_I(inode)->root;
754 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -0400755 pgoff_t index = from >> PAGE_CACHE_SHIFT;
756 unsigned offset = from & (PAGE_CACHE_SIZE-1);
757 struct page *page;
758 int ret = 0;
759 u64 page_start;
760
761 if ((offset & (blocksize - 1)) == 0)
762 goto out;
763
Chris Masondb945352007-10-15 16:15:53 -0400764 down_read(&root->snap_sem);
Chris Masona52d9a82007-08-27 16:49:44 -0400765 ret = -ENOMEM;
766 page = grab_cache_page(mapping, index);
767 if (!page)
768 goto out;
769 if (!PageUptodate(page)) {
770 ret = btrfs_readpage(NULL, page);
771 lock_page(page);
772 if (!PageUptodate(page)) {
773 ret = -EIO;
774 goto out;
775 }
776 }
Chris Mason35ebb932007-10-30 16:56:53 -0400777 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -0400778
Chris Masonb888db22007-08-27 16:49:44 -0400779 ret = btrfs_cow_one_page(inode, page, offset);
Chris Mason39279cc2007-06-12 06:35:45 -0400780
Chris Mason39279cc2007-06-12 06:35:45 -0400781 unlock_page(page);
782 page_cache_release(page);
Chris Mason011410b2007-09-10 19:58:36 -0400783 up_read(&BTRFS_I(inode)->root->snap_sem);
Chris Mason39279cc2007-06-12 06:35:45 -0400784out:
785 return ret;
786}
787
788static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
789{
790 struct inode *inode = dentry->d_inode;
791 int err;
792
793 err = inode_change_ok(inode, attr);
794 if (err)
795 return err;
796
797 if (S_ISREG(inode->i_mode) &&
798 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
799 struct btrfs_trans_handle *trans;
800 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason2bf5a722007-08-30 11:54:02 -0400801 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
802
Chris Mason5f39d392007-10-15 16:14:19 -0400803 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -0400804 u64 pos = (inode->i_size + mask) & ~mask;
Chris Mason2bf5a722007-08-30 11:54:02 -0400805 u64 block_end = attr->ia_size | mask;
Chris Mason39279cc2007-06-12 06:35:45 -0400806 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -0400807 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400808
809 if (attr->ia_size <= pos)
810 goto out;
811
812 btrfs_truncate_page(inode->i_mapping, inode->i_size);
813
Chris Mason2bf5a722007-08-30 11:54:02 -0400814 lock_extent(em_tree, pos, block_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -0400815 hole_size = (attr->ia_size - pos + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -0400816
817 mutex_lock(&root->fs_info->fs_mutex);
818 trans = btrfs_start_transaction(root, 1);
819 btrfs_set_trans_block_group(trans, inode);
Chris Mason2bf5a722007-08-30 11:54:02 -0400820 err = btrfs_drop_extents(trans, root, inode,
Chris Mason3326d1b2007-10-15 16:18:25 -0400821 pos, pos + hole_size, pos,
822 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -0400823
Chris Mason179e29e2007-11-01 11:28:41 -0400824 if (alloc_hint != EXTENT_MAP_INLINE) {
825 err = btrfs_insert_file_extent(trans, root,
826 inode->i_ino,
827 pos, 0, 0, hole_size);
828 }
Chris Mason39279cc2007-06-12 06:35:45 -0400829 btrfs_end_transaction(trans, root);
830 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason2bf5a722007-08-30 11:54:02 -0400831 unlock_extent(em_tree, pos, block_end, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -0400832 if (err)
833 return err;
Chris Mason39279cc2007-06-12 06:35:45 -0400834 }
835out:
836 err = inode_setattr(inode, attr);
837
838 return err;
839}
840void btrfs_delete_inode(struct inode *inode)
841{
842 struct btrfs_trans_handle *trans;
843 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400844 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -0400845 int ret;
846
847 truncate_inode_pages(&inode->i_data, 0);
848 if (is_bad_inode(inode)) {
849 goto no_delete;
850 }
Chris Mason5f39d392007-10-15 16:14:19 -0400851
Chris Mason39279cc2007-06-12 06:35:45 -0400852 inode->i_size = 0;
853 mutex_lock(&root->fs_info->fs_mutex);
854 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400855
Chris Mason39279cc2007-06-12 06:35:45 -0400856 btrfs_set_trans_block_group(trans, inode);
857 ret = btrfs_truncate_in_trans(trans, root, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -0400858 if (ret)
859 goto no_delete_lock;
Josef Bacik5103e942007-11-16 11:45:54 -0500860 ret = btrfs_delete_xattrs(trans, root, inode);
861 if (ret)
862 goto no_delete_lock;
Chris Mason54aa1f42007-06-22 14:16:25 -0400863 ret = btrfs_free_inode(trans, root, inode);
864 if (ret)
865 goto no_delete_lock;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400866 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -0400867
Chris Mason39279cc2007-06-12 06:35:45 -0400868 btrfs_end_transaction(trans, root);
869 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400870 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -0400871 return;
Chris Mason54aa1f42007-06-22 14:16:25 -0400872
873no_delete_lock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400874 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -0400875 btrfs_end_transaction(trans, root);
876 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400877 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -0400878no_delete:
879 clear_inode(inode);
880}
881
882/*
883 * this returns the key found in the dir entry in the location pointer.
884 * If no dir entries were found, location->objectid is 0.
885 */
886static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
887 struct btrfs_key *location)
888{
889 const char *name = dentry->d_name.name;
890 int namelen = dentry->d_name.len;
891 struct btrfs_dir_item *di;
892 struct btrfs_path *path;
893 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -0400894 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400895
896 path = btrfs_alloc_path();
897 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400898 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
899 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -0400900 if (IS_ERR(di))
901 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -0400902 if (!di || IS_ERR(di)) {
903 location->objectid = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400904 goto out;
905 }
Chris Mason5f39d392007-10-15 16:14:19 -0400906 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -0400907out:
908 btrfs_release_path(root, path);
909 btrfs_free_path(path);
910 return ret;
911}
912
913/*
914 * when we hit a tree root in a directory, the btrfs part of the inode
915 * needs to be changed to reflect the root directory of the tree root. This
916 * is kind of like crossing a mount point.
917 */
918static int fixup_tree_root_location(struct btrfs_root *root,
919 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -0400920 struct btrfs_root **sub_root,
921 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -0400922{
923 struct btrfs_path *path;
924 struct btrfs_root_item *ri;
925
926 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
927 return 0;
928 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
929 return 0;
930
931 path = btrfs_alloc_path();
932 BUG_ON(!path);
933 mutex_lock(&root->fs_info->fs_mutex);
934
Josef Bacik58176a92007-08-29 15:47:34 -0400935 *sub_root = btrfs_read_fs_root(root->fs_info, location,
936 dentry->d_name.name,
937 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -0400938 if (IS_ERR(*sub_root))
939 return PTR_ERR(*sub_root);
940
941 ri = &(*sub_root)->root_item;
942 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -0400943 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
944 location->offset = 0;
945
946 btrfs_free_path(path);
947 mutex_unlock(&root->fs_info->fs_mutex);
948 return 0;
949}
950
951static int btrfs_init_locked_inode(struct inode *inode, void *p)
952{
953 struct btrfs_iget_args *args = p;
954 inode->i_ino = args->ino;
955 BTRFS_I(inode)->root = args->root;
Chris Masonb888db22007-08-27 16:49:44 -0400956 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
957 inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -0400958 return 0;
959}
960
961static int btrfs_find_actor(struct inode *inode, void *opaque)
962{
963 struct btrfs_iget_args *args = opaque;
964 return (args->ino == inode->i_ino &&
965 args->root == BTRFS_I(inode)->root);
966}
967
968struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
969 struct btrfs_root *root)
970{
971 struct inode *inode;
972 struct btrfs_iget_args args;
973 args.ino = objectid;
974 args.root = root;
975
976 inode = iget5_locked(s, objectid, btrfs_find_actor,
977 btrfs_init_locked_inode,
978 (void *)&args);
979 return inode;
980}
981
982static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
983 struct nameidata *nd)
984{
985 struct inode * inode;
986 struct btrfs_inode *bi = BTRFS_I(dir);
987 struct btrfs_root *root = bi->root;
988 struct btrfs_root *sub_root = root;
989 struct btrfs_key location;
990 int ret;
991
992 if (dentry->d_name.len > BTRFS_NAME_LEN)
993 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -0400994
Chris Mason39279cc2007-06-12 06:35:45 -0400995 mutex_lock(&root->fs_info->fs_mutex);
996 ret = btrfs_inode_by_name(dir, dentry, &location);
997 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason5f39d392007-10-15 16:14:19 -0400998
Chris Mason39279cc2007-06-12 06:35:45 -0400999 if (ret < 0)
1000 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001001
Chris Mason39279cc2007-06-12 06:35:45 -04001002 inode = NULL;
1003 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04001004 ret = fixup_tree_root_location(root, &location, &sub_root,
1005 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04001006 if (ret < 0)
1007 return ERR_PTR(ret);
1008 if (ret > 0)
1009 return ERR_PTR(-ENOENT);
1010 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1011 sub_root);
1012 if (!inode)
1013 return ERR_PTR(-EACCES);
1014 if (inode->i_state & I_NEW) {
1015 /* the inode and parent dir are two different roots */
1016 if (sub_root != root) {
1017 igrab(inode);
1018 sub_root->inode = inode;
1019 }
1020 BTRFS_I(inode)->root = sub_root;
1021 memcpy(&BTRFS_I(inode)->location, &location,
1022 sizeof(location));
1023 btrfs_read_locked_inode(inode);
1024 unlock_new_inode(inode);
1025 }
1026 }
1027 return d_splice_alias(inode, dentry);
1028}
1029
Chris Mason39279cc2007-06-12 06:35:45 -04001030static unsigned char btrfs_filetype_table[] = {
1031 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1032};
1033
1034static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1035{
1036 struct inode *inode = filp->f_path.dentry->d_inode;
1037 struct btrfs_root *root = BTRFS_I(inode)->root;
1038 struct btrfs_item *item;
1039 struct btrfs_dir_item *di;
1040 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001041 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001042 struct btrfs_path *path;
1043 int ret;
1044 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001045 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001046 int slot;
1047 int advance;
1048 unsigned char d_type;
1049 int over = 0;
1050 u32 di_cur;
1051 u32 di_total;
1052 u32 di_len;
1053 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001054 char tmp_name[32];
1055 char *name_ptr;
1056 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001057
1058 /* FIXME, use a real flag for deciding about the key type */
1059 if (root->fs_info->tree_root == root)
1060 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001061
Chris Mason39279cc2007-06-12 06:35:45 -04001062 mutex_lock(&root->fs_info->fs_mutex);
1063 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04001064 btrfs_set_key_type(&key, key_type);
1065 key.offset = filp->f_pos;
Chris Mason5f39d392007-10-15 16:14:19 -04001066
Chris Mason39279cc2007-06-12 06:35:45 -04001067 path = btrfs_alloc_path();
Chris Mason2cc58cf2007-08-27 16:49:44 -04001068 path->reada = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04001069 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1070 if (ret < 0)
1071 goto err;
1072 advance = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001073 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001074 leaf = path->nodes[0];
1075 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001076 slot = path->slots[0];
1077 if (advance || slot >= nritems) {
1078 if (slot >= nritems -1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001079 ret = btrfs_next_leaf(root, path);
1080 if (ret)
1081 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001082 leaf = path->nodes[0];
1083 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001084 slot = path->slots[0];
1085 } else {
1086 slot++;
1087 path->slots[0]++;
1088 }
1089 }
1090 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001091 item = btrfs_item_nr(leaf, slot);
1092 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1093
1094 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04001095 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001096 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001097 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001098 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04001099 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001100
1101 filp->f_pos = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001102 advance = 1;
1103 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1104 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001105 di_total = btrfs_item_size(leaf, item);
Chris Mason39279cc2007-06-12 06:35:45 -04001106 while(di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04001107 struct btrfs_key location;
1108
1109 name_len = btrfs_dir_name_len(leaf, di);
1110 if (name_len < 32) {
1111 name_ptr = tmp_name;
1112 } else {
1113 name_ptr = kmalloc(name_len, GFP_NOFS);
1114 BUG_ON(!name_ptr);
1115 }
1116 read_extent_buffer(leaf, name_ptr,
1117 (unsigned long)(di + 1), name_len);
1118
1119 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1120 btrfs_dir_item_key_to_cpu(leaf, di, &location);
1121
1122 over = filldir(dirent, name_ptr, name_len,
1123 found_key.offset,
1124 location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001125 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04001126
1127 if (name_ptr != tmp_name)
1128 kfree(name_ptr);
1129
Chris Mason39279cc2007-06-12 06:35:45 -04001130 if (over)
1131 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05001132 di_len = btrfs_dir_name_len(leaf, di) +
1133 btrfs_dir_data_len(leaf, di) +sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04001134 di_cur += di_len;
1135 di = (struct btrfs_dir_item *)((char *)di + di_len);
1136 }
1137 }
1138 filp->f_pos++;
1139nopos:
1140 ret = 0;
1141err:
1142 btrfs_release_path(root, path);
1143 btrfs_free_path(path);
1144 mutex_unlock(&root->fs_info->fs_mutex);
1145 return ret;
1146}
1147
1148int btrfs_write_inode(struct inode *inode, int wait)
1149{
1150 struct btrfs_root *root = BTRFS_I(inode)->root;
1151 struct btrfs_trans_handle *trans;
1152 int ret = 0;
1153
1154 if (wait) {
1155 mutex_lock(&root->fs_info->fs_mutex);
1156 trans = btrfs_start_transaction(root, 1);
1157 btrfs_set_trans_block_group(trans, inode);
1158 ret = btrfs_commit_transaction(trans, root);
1159 mutex_unlock(&root->fs_info->fs_mutex);
1160 }
1161 return ret;
1162}
1163
1164/*
Chris Mason54aa1f42007-06-22 14:16:25 -04001165 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04001166 * inode changes. But, it is most likely to find the inode in cache.
1167 * FIXME, needs more benchmarking...there are no reasons other than performance
1168 * to keep or drop this code.
1169 */
1170void btrfs_dirty_inode(struct inode *inode)
1171{
1172 struct btrfs_root *root = BTRFS_I(inode)->root;
1173 struct btrfs_trans_handle *trans;
1174
1175 mutex_lock(&root->fs_info->fs_mutex);
1176 trans = btrfs_start_transaction(root, 1);
1177 btrfs_set_trans_block_group(trans, inode);
1178 btrfs_update_inode(trans, root, inode);
1179 btrfs_end_transaction(trans, root);
1180 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001181}
1182
1183static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1184 struct btrfs_root *root,
1185 u64 objectid,
1186 struct btrfs_block_group_cache *group,
1187 int mode)
1188{
1189 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001190 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04001191 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04001192 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04001193 int ret;
1194 int owner;
1195
Chris Mason5f39d392007-10-15 16:14:19 -04001196 path = btrfs_alloc_path();
1197 BUG_ON(!path);
1198
Chris Mason39279cc2007-06-12 06:35:45 -04001199 inode = new_inode(root->fs_info->sb);
1200 if (!inode)
1201 return ERR_PTR(-ENOMEM);
1202
Chris Masonb888db22007-08-27 16:49:44 -04001203 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1204 inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001205 BTRFS_I(inode)->root = root;
Chris Masonb888db22007-08-27 16:49:44 -04001206
Chris Mason39279cc2007-06-12 06:35:45 -04001207 if (mode & S_IFDIR)
1208 owner = 0;
1209 else
1210 owner = 1;
1211 group = btrfs_find_block_group(root, group, 0, 0, owner);
1212 BTRFS_I(inode)->block_group = group;
1213
Chris Mason5f39d392007-10-15 16:14:19 -04001214 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
1215 if (ret)
1216 goto fail;
1217
Chris Mason39279cc2007-06-12 06:35:45 -04001218 inode->i_uid = current->fsuid;
1219 inode->i_gid = current->fsgid;
1220 inode->i_mode = mode;
1221 inode->i_ino = objectid;
1222 inode->i_blocks = 0;
1223 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04001224 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1225 struct btrfs_inode_item);
1226 fill_inode_item(path->nodes[0], inode_item, inode);
1227 btrfs_mark_buffer_dirty(path->nodes[0]);
1228 btrfs_free_path(path);
1229
Chris Mason39279cc2007-06-12 06:35:45 -04001230 location = &BTRFS_I(inode)->location;
1231 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04001232 location->offset = 0;
1233 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1234
Chris Mason39279cc2007-06-12 06:35:45 -04001235 insert_inode_hash(inode);
1236 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001237fail:
1238 btrfs_free_path(path);
1239 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04001240}
1241
1242static inline u8 btrfs_inode_type(struct inode *inode)
1243{
1244 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1245}
1246
1247static int btrfs_add_link(struct btrfs_trans_handle *trans,
1248 struct dentry *dentry, struct inode *inode)
1249{
1250 int ret;
1251 struct btrfs_key key;
1252 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Chris Mason79c44582007-06-25 10:09:33 -04001253 struct inode *parent_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001254
Chris Mason39279cc2007-06-12 06:35:45 -04001255 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04001256 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1257 key.offset = 0;
1258
1259 ret = btrfs_insert_dir_item(trans, root,
1260 dentry->d_name.name, dentry->d_name.len,
1261 dentry->d_parent->d_inode->i_ino,
1262 &key, btrfs_inode_type(inode));
1263 if (ret == 0) {
Chris Mason79c44582007-06-25 10:09:33 -04001264 parent_inode = dentry->d_parent->d_inode;
1265 parent_inode->i_size += dentry->d_name.len * 2;
1266 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001267 ret = btrfs_update_inode(trans, root,
1268 dentry->d_parent->d_inode);
1269 }
1270 return ret;
1271}
1272
1273static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1274 struct dentry *dentry, struct inode *inode)
1275{
1276 int err = btrfs_add_link(trans, dentry, inode);
1277 if (!err) {
1278 d_instantiate(dentry, inode);
1279 return 0;
1280 }
1281 if (err > 0)
1282 err = -EEXIST;
1283 return err;
1284}
1285
Josef Bacik618e21d2007-07-11 10:18:17 -04001286static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1287 int mode, dev_t rdev)
1288{
1289 struct btrfs_trans_handle *trans;
1290 struct btrfs_root *root = BTRFS_I(dir)->root;
1291 struct inode *inode;
1292 int err;
1293 int drop_inode = 0;
1294 u64 objectid;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001295 unsigned long nr;
Josef Bacik618e21d2007-07-11 10:18:17 -04001296
1297 if (!new_valid_dev(rdev))
1298 return -EINVAL;
1299
1300 mutex_lock(&root->fs_info->fs_mutex);
1301 trans = btrfs_start_transaction(root, 1);
1302 btrfs_set_trans_block_group(trans, dir);
1303
1304 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1305 if (err) {
1306 err = -ENOSPC;
1307 goto out_unlock;
1308 }
1309
1310 inode = btrfs_new_inode(trans, root, objectid,
1311 BTRFS_I(dir)->block_group, mode);
1312 err = PTR_ERR(inode);
1313 if (IS_ERR(inode))
1314 goto out_unlock;
1315
1316 btrfs_set_trans_block_group(trans, inode);
1317 err = btrfs_add_nondir(trans, dentry, inode);
1318 if (err)
1319 drop_inode = 1;
1320 else {
1321 inode->i_op = &btrfs_special_inode_operations;
1322 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04001323 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04001324 }
1325 dir->i_sb->s_dirt = 1;
1326 btrfs_update_inode_block_group(trans, inode);
1327 btrfs_update_inode_block_group(trans, dir);
1328out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001329 nr = trans->blocks_used;
Josef Bacik618e21d2007-07-11 10:18:17 -04001330 btrfs_end_transaction(trans, root);
1331 mutex_unlock(&root->fs_info->fs_mutex);
1332
1333 if (drop_inode) {
1334 inode_dec_link_count(inode);
1335 iput(inode);
1336 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001337 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04001338 return err;
1339}
1340
Chris Mason39279cc2007-06-12 06:35:45 -04001341static int btrfs_create(struct inode *dir, struct dentry *dentry,
1342 int mode, struct nameidata *nd)
1343{
1344 struct btrfs_trans_handle *trans;
1345 struct btrfs_root *root = BTRFS_I(dir)->root;
1346 struct inode *inode;
1347 int err;
1348 int drop_inode = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001349 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001350 u64 objectid;
1351
1352 mutex_lock(&root->fs_info->fs_mutex);
1353 trans = btrfs_start_transaction(root, 1);
1354 btrfs_set_trans_block_group(trans, dir);
1355
1356 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1357 if (err) {
1358 err = -ENOSPC;
1359 goto out_unlock;
1360 }
1361
1362 inode = btrfs_new_inode(trans, root, objectid,
1363 BTRFS_I(dir)->block_group, mode);
1364 err = PTR_ERR(inode);
1365 if (IS_ERR(inode))
1366 goto out_unlock;
1367
1368 btrfs_set_trans_block_group(trans, inode);
1369 err = btrfs_add_nondir(trans, dentry, inode);
1370 if (err)
1371 drop_inode = 1;
1372 else {
1373 inode->i_mapping->a_ops = &btrfs_aops;
1374 inode->i_fop = &btrfs_file_operations;
1375 inode->i_op = &btrfs_file_inode_operations;
Chris Masona52d9a82007-08-27 16:49:44 -04001376 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1377 inode->i_mapping, GFP_NOFS);
Chris Mason07157aa2007-08-30 08:50:51 -04001378 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04001379 }
1380 dir->i_sb->s_dirt = 1;
1381 btrfs_update_inode_block_group(trans, inode);
1382 btrfs_update_inode_block_group(trans, dir);
1383out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001384 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001385 btrfs_end_transaction(trans, root);
1386 mutex_unlock(&root->fs_info->fs_mutex);
1387
1388 if (drop_inode) {
1389 inode_dec_link_count(inode);
1390 iput(inode);
1391 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001392 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001393 return err;
1394}
1395
1396static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1397 struct dentry *dentry)
1398{
1399 struct btrfs_trans_handle *trans;
1400 struct btrfs_root *root = BTRFS_I(dir)->root;
1401 struct inode *inode = old_dentry->d_inode;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001402 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001403 int err;
1404 int drop_inode = 0;
1405
1406 if (inode->i_nlink == 0)
1407 return -ENOENT;
1408
1409 inc_nlink(inode);
1410 mutex_lock(&root->fs_info->fs_mutex);
1411 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001412
Chris Mason39279cc2007-06-12 06:35:45 -04001413 btrfs_set_trans_block_group(trans, dir);
1414 atomic_inc(&inode->i_count);
1415 err = btrfs_add_nondir(trans, dentry, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001416
Chris Mason39279cc2007-06-12 06:35:45 -04001417 if (err)
1418 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001419
Chris Mason39279cc2007-06-12 06:35:45 -04001420 dir->i_sb->s_dirt = 1;
1421 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04001422 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001423
Chris Mason54aa1f42007-06-22 14:16:25 -04001424 if (err)
1425 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001426
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001427 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001428 btrfs_end_transaction(trans, root);
1429 mutex_unlock(&root->fs_info->fs_mutex);
1430
1431 if (drop_inode) {
1432 inode_dec_link_count(inode);
1433 iput(inode);
1434 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001435 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001436 return err;
1437}
1438
1439static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
1440 struct btrfs_root *root,
1441 u64 objectid, u64 dirid)
1442{
1443 int ret;
1444 char buf[2];
1445 struct btrfs_key key;
1446
1447 buf[0] = '.';
1448 buf[1] = '.';
1449
1450 key.objectid = objectid;
1451 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001452 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1453
1454 ret = btrfs_insert_dir_item(trans, root, buf, 1, objectid,
1455 &key, BTRFS_FT_DIR);
1456 if (ret)
1457 goto error;
Chris Mason5f39d392007-10-15 16:14:19 -04001458
Chris Mason39279cc2007-06-12 06:35:45 -04001459 key.objectid = dirid;
1460 ret = btrfs_insert_dir_item(trans, root, buf, 2, objectid,
1461 &key, BTRFS_FT_DIR);
1462 if (ret)
1463 goto error;
1464error:
1465 return ret;
1466}
1467
1468static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1469{
1470 struct inode *inode;
1471 struct btrfs_trans_handle *trans;
1472 struct btrfs_root *root = BTRFS_I(dir)->root;
1473 int err = 0;
1474 int drop_on_err = 0;
1475 u64 objectid;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001476 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001477
1478 mutex_lock(&root->fs_info->fs_mutex);
1479 trans = btrfs_start_transaction(root, 1);
1480 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04001481
Chris Mason39279cc2007-06-12 06:35:45 -04001482 if (IS_ERR(trans)) {
1483 err = PTR_ERR(trans);
1484 goto out_unlock;
1485 }
1486
1487 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1488 if (err) {
1489 err = -ENOSPC;
1490 goto out_unlock;
1491 }
1492
1493 inode = btrfs_new_inode(trans, root, objectid,
1494 BTRFS_I(dir)->block_group, S_IFDIR | mode);
1495 if (IS_ERR(inode)) {
1496 err = PTR_ERR(inode);
1497 goto out_fail;
1498 }
Chris Mason5f39d392007-10-15 16:14:19 -04001499
Chris Mason39279cc2007-06-12 06:35:45 -04001500 drop_on_err = 1;
1501 inode->i_op = &btrfs_dir_inode_operations;
1502 inode->i_fop = &btrfs_dir_file_operations;
1503 btrfs_set_trans_block_group(trans, inode);
1504
1505 err = btrfs_make_empty_dir(trans, root, inode->i_ino, dir->i_ino);
1506 if (err)
1507 goto out_fail;
1508
1509 inode->i_size = 6;
1510 err = btrfs_update_inode(trans, root, inode);
1511 if (err)
1512 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04001513
Chris Mason39279cc2007-06-12 06:35:45 -04001514 err = btrfs_add_link(trans, dentry, inode);
1515 if (err)
1516 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04001517
Chris Mason39279cc2007-06-12 06:35:45 -04001518 d_instantiate(dentry, inode);
1519 drop_on_err = 0;
1520 dir->i_sb->s_dirt = 1;
1521 btrfs_update_inode_block_group(trans, inode);
1522 btrfs_update_inode_block_group(trans, dir);
1523
1524out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001525 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001526 btrfs_end_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04001527
Chris Mason39279cc2007-06-12 06:35:45 -04001528out_unlock:
1529 mutex_unlock(&root->fs_info->fs_mutex);
1530 if (drop_on_err)
1531 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001532 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001533 return err;
1534}
1535
Chris Masona52d9a82007-08-27 16:49:44 -04001536struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
1537 size_t page_offset, u64 start, u64 end,
1538 int create)
1539{
1540 int ret;
1541 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04001542 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04001543 u64 extent_start = 0;
1544 u64 extent_end = 0;
1545 u64 objectid = inode->i_ino;
1546 u32 found_type;
1547 int failed_insert = 0;
1548 struct btrfs_path *path;
1549 struct btrfs_root *root = BTRFS_I(inode)->root;
1550 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04001551 struct extent_buffer *leaf;
1552 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04001553 struct extent_map *em = NULL;
1554 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1555 struct btrfs_trans_handle *trans = NULL;
1556
1557 path = btrfs_alloc_path();
1558 BUG_ON(!path);
1559 mutex_lock(&root->fs_info->fs_mutex);
1560
1561again:
1562 em = lookup_extent_mapping(em_tree, start, end);
1563 if (em) {
1564 goto out;
1565 }
1566 if (!em) {
1567 em = alloc_extent_map(GFP_NOFS);
1568 if (!em) {
1569 err = -ENOMEM;
1570 goto out;
1571 }
Chris Mason5f39d392007-10-15 16:14:19 -04001572 em->start = EXTENT_MAP_HOLE;
1573 em->end = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001574 }
1575 em->bdev = inode->i_sb->s_bdev;
Chris Mason179e29e2007-11-01 11:28:41 -04001576 ret = btrfs_lookup_file_extent(trans, root, path,
1577 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04001578 if (ret < 0) {
1579 err = ret;
1580 goto out;
1581 }
1582
1583 if (ret != 0) {
1584 if (path->slots[0] == 0)
1585 goto not_found;
1586 path->slots[0]--;
1587 }
1588
Chris Mason5f39d392007-10-15 16:14:19 -04001589 leaf = path->nodes[0];
1590 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04001591 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04001592 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04001593 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1594 found_type = btrfs_key_type(&found_key);
1595 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04001596 found_type != BTRFS_EXTENT_DATA_KEY) {
1597 goto not_found;
1598 }
1599
Chris Mason5f39d392007-10-15 16:14:19 -04001600 found_type = btrfs_file_extent_type(leaf, item);
1601 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04001602 if (found_type == BTRFS_FILE_EXTENT_REG) {
1603 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04001604 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04001605 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04001606 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04001607 em->start = start;
1608 if (start < extent_start) {
Chris Masonb888db22007-08-27 16:49:44 -04001609 if (end < extent_start)
1610 goto not_found;
Chris Masona52d9a82007-08-27 16:49:44 -04001611 em->end = extent_end - 1;
1612 } else {
1613 em->end = end;
1614 }
1615 goto not_found_em;
1616 }
Chris Masondb945352007-10-15 16:15:53 -04001617 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
1618 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04001619 em->start = extent_start;
1620 em->end = extent_end - 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001621 em->block_start = EXTENT_MAP_HOLE;
1622 em->block_end = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001623 goto insert;
1624 }
Chris Masondb945352007-10-15 16:15:53 -04001625 bytenr += btrfs_file_extent_offset(leaf, item);
1626 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04001627 em->block_end = em->block_start +
Chris Masondb945352007-10-15 16:15:53 -04001628 btrfs_file_extent_num_bytes(leaf, item) - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04001629 em->start = extent_start;
1630 em->end = extent_end - 1;
1631 goto insert;
1632 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04001633 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04001634 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04001635 size_t size;
1636 size_t extent_offset;
1637 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04001638
Chris Mason5f39d392007-10-15 16:14:19 -04001639 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
1640 path->slots[0]));
Yan689f9342007-10-29 11:41:07 -04001641 extent_end = (extent_start + size - 1) |
Chris Masondb945352007-10-15 16:15:53 -04001642 ((u64)root->sectorsize - 1);
Chris Masonb888db22007-08-27 16:49:44 -04001643 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04001644 em->start = start;
1645 if (start < extent_start) {
Chris Masonb888db22007-08-27 16:49:44 -04001646 if (end < extent_start)
1647 goto not_found;
Chris Mason50b78c22007-09-20 14:14:42 -04001648 em->end = extent_end;
Chris Masona52d9a82007-08-27 16:49:44 -04001649 } else {
1650 em->end = end;
1651 }
1652 goto not_found_em;
1653 }
1654 em->block_start = EXTENT_MAP_INLINE;
1655 em->block_end = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04001656
1657 if (!page) {
1658 em->start = extent_start;
1659 em->end = extent_start + size - 1;
1660 goto out;
1661 }
1662
Chris Mason35ebb932007-10-30 16:56:53 -04001663 extent_offset = ((u64)page->index << PAGE_CACHE_SHIFT) -
Yan689f9342007-10-29 11:41:07 -04001664 extent_start + page_offset;
1665 copy_size = min_t(u64, PAGE_CACHE_SIZE - page_offset,
1666 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04001667 em->start = extent_start + extent_offset;
1668 em->end = (em->start + copy_size -1) |
1669 ((u64)root->sectorsize -1);
Yan689f9342007-10-29 11:41:07 -04001670 map = kmap(page);
1671 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04001672 if (create == 0 && !PageUptodate(page)) {
1673 read_extent_buffer(leaf, map + page_offset, ptr,
1674 copy_size);
1675 flush_dcache_page(page);
1676 } else if (create && PageUptodate(page)) {
1677 if (!trans) {
1678 kunmap(page);
1679 free_extent_map(em);
1680 em = NULL;
1681 btrfs_release_path(root, path);
1682 trans = btrfs_start_transaction(root, 1);
1683 goto again;
1684 }
1685 write_extent_buffer(leaf, map + page_offset, ptr,
1686 copy_size);
1687 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04001688 }
Chris Masona52d9a82007-08-27 16:49:44 -04001689 kunmap(page);
Chris Mason3326d1b2007-10-15 16:18:25 -04001690 set_extent_uptodate(em_tree, em->start, em->end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04001691 goto insert;
1692 } else {
1693 printk("unkknown found_type %d\n", found_type);
1694 WARN_ON(1);
1695 }
1696not_found:
1697 em->start = start;
1698 em->end = end;
1699not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04001700 em->block_start = EXTENT_MAP_HOLE;
1701 em->block_end = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001702insert:
1703 btrfs_release_path(root, path);
1704 if (em->start > start || em->end < start) {
Chris Masonb888db22007-08-27 16:49:44 -04001705 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->end, start, end);
Chris Masona52d9a82007-08-27 16:49:44 -04001706 err = -EIO;
1707 goto out;
1708 }
1709 ret = add_extent_mapping(em_tree, em);
1710 if (ret == -EEXIST) {
1711 free_extent_map(em);
Chris Mason2bf5a722007-08-30 11:54:02 -04001712 em = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04001713 failed_insert++;
1714 if (failed_insert > 5) {
1715 printk("failing to insert %Lu %Lu\n", start, end);
1716 err = -EIO;
1717 goto out;
1718 }
Chris Masona52d9a82007-08-27 16:49:44 -04001719 goto again;
1720 }
1721 err = 0;
1722out:
1723 btrfs_free_path(path);
1724 if (trans) {
1725 ret = btrfs_end_transaction(trans, root);
1726 if (!err)
1727 err = ret;
1728 }
1729 mutex_unlock(&root->fs_info->fs_mutex);
1730 if (err) {
1731 free_extent_map(em);
1732 WARN_ON(1);
1733 return ERR_PTR(err);
1734 }
1735 return em;
1736}
1737
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04001738static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04001739{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04001740 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04001741}
1742
1743static int btrfs_prepare_write(struct file *file, struct page *page,
1744 unsigned from, unsigned to)
1745{
Chris Masona52d9a82007-08-27 16:49:44 -04001746 return extent_prepare_write(&BTRFS_I(page->mapping->host)->extent_tree,
1747 page->mapping->host, page, from, to,
1748 btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04001749}
1750
Chris Mason9ebefb182007-06-15 13:50:00 -04001751int btrfs_readpage(struct file *file, struct page *page)
1752{
Chris Masona52d9a82007-08-27 16:49:44 -04001753 struct extent_map_tree *tree;
1754 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1755 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04001756}
Chris Mason39279cc2007-06-12 06:35:45 -04001757static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1758{
Chris Masona52d9a82007-08-27 16:49:44 -04001759 struct extent_map_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04001760
1761
1762 if (current->flags & PF_MEMALLOC) {
1763 redirty_page_for_writepage(wbc, page);
1764 unlock_page(page);
1765 return 0;
1766 }
Chris Masona52d9a82007-08-27 16:49:44 -04001767 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1768 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
1769}
Chris Mason39279cc2007-06-12 06:35:45 -04001770
Chris Masonb293f022007-11-01 19:45:34 -04001771static int btrfs_writepages(struct address_space *mapping,
1772 struct writeback_control *wbc)
1773{
1774 struct extent_map_tree *tree;
1775 tree = &BTRFS_I(mapping->host)->extent_tree;
1776 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
1777}
1778
Chris Mason3ab2fb52007-11-08 10:59:22 -05001779static int
1780btrfs_readpages(struct file *file, struct address_space *mapping,
1781 struct list_head *pages, unsigned nr_pages)
1782{
1783 struct extent_map_tree *tree;
1784 tree = &BTRFS_I(mapping->host)->extent_tree;
1785 return extent_readpages(tree, mapping, pages, nr_pages,
1786 btrfs_get_extent);
1787}
1788
Chris Masona52d9a82007-08-27 16:49:44 -04001789static int btrfs_releasepage(struct page *page, gfp_t unused_gfp_flags)
1790{
1791 struct extent_map_tree *tree;
1792 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04001793
Chris Masona52d9a82007-08-27 16:49:44 -04001794 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1795 ret = try_release_extent_mapping(tree, page);
1796 if (ret == 1) {
1797 ClearPagePrivate(page);
1798 set_page_private(page, 0);
1799 page_cache_release(page);
1800 }
1801 return ret;
1802}
Chris Mason39279cc2007-06-12 06:35:45 -04001803
Chris Masona52d9a82007-08-27 16:49:44 -04001804static void btrfs_invalidatepage(struct page *page, unsigned long offset)
1805{
1806 struct extent_map_tree *tree;
1807
1808 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1809 extent_invalidatepage(tree, page, offset);
1810 btrfs_releasepage(page, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001811}
1812
Chris Mason9ebefb182007-06-15 13:50:00 -04001813/*
1814 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
1815 * called from a page fault handler when a page is first dirtied. Hence we must
1816 * be careful to check for EOF conditions here. We set the page up correctly
1817 * for a written page which means we get ENOSPC checking when writing into
1818 * holes and correct delalloc and unwritten extent mapping on filesystems that
1819 * support these features.
1820 *
1821 * We are not allowed to take the i_mutex here so we have to play games to
1822 * protect against truncate races as the page could now be beyond EOF. Because
1823 * vmtruncate() writes the inode size before removing pages, once we have the
1824 * page lock we can determine safely if the page is beyond EOF. If it is not
1825 * beyond EOF, then the page is guaranteed safe against truncation until we
1826 * unlock the page.
1827 */
1828int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
1829{
1830 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1831 unsigned long end;
1832 loff_t size;
1833 int ret = -EINVAL;
Chris Masona52d9a82007-08-27 16:49:44 -04001834 u64 page_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04001835
Chris Mason011410b2007-09-10 19:58:36 -04001836 down_read(&BTRFS_I(inode)->root->snap_sem);
Chris Mason9ebefb182007-06-15 13:50:00 -04001837 lock_page(page);
1838 wait_on_page_writeback(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04001839 size = i_size_read(inode);
Chris Mason35ebb932007-10-30 16:56:53 -04001840 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04001841
Chris Mason9ebefb182007-06-15 13:50:00 -04001842 if ((page->mapping != inode->i_mapping) ||
Chris Masona52d9a82007-08-27 16:49:44 -04001843 (page_start > size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04001844 /* page got truncated out from underneath us */
1845 goto out_unlock;
1846 }
1847
1848 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04001849 if (page_start + PAGE_CACHE_SIZE > size)
Chris Mason9ebefb182007-06-15 13:50:00 -04001850 end = size & ~PAGE_CACHE_MASK;
1851 else
1852 end = PAGE_CACHE_SIZE;
1853
Chris Masonb888db22007-08-27 16:49:44 -04001854 ret = btrfs_cow_one_page(inode, page, end);
Chris Mason9ebefb182007-06-15 13:50:00 -04001855
1856out_unlock:
Chris Mason011410b2007-09-10 19:58:36 -04001857 up_read(&BTRFS_I(inode)->root->snap_sem);
Chris Mason9ebefb182007-06-15 13:50:00 -04001858 unlock_page(page);
1859 return ret;
1860}
1861
Chris Mason39279cc2007-06-12 06:35:45 -04001862static void btrfs_truncate(struct inode *inode)
1863{
1864 struct btrfs_root *root = BTRFS_I(inode)->root;
1865 int ret;
1866 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001867 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001868
1869 if (!S_ISREG(inode->i_mode))
1870 return;
1871 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1872 return;
1873
1874 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1875
1876 mutex_lock(&root->fs_info->fs_mutex);
1877 trans = btrfs_start_transaction(root, 1);
1878 btrfs_set_trans_block_group(trans, inode);
1879
1880 /* FIXME, add redo link to tree so we don't leak on crash */
1881 ret = btrfs_truncate_in_trans(trans, root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001882 btrfs_update_inode(trans, root, inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001883 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04001884
Chris Mason39279cc2007-06-12 06:35:45 -04001885 ret = btrfs_end_transaction(trans, root);
1886 BUG_ON(ret);
1887 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001888 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001889}
1890
1891int btrfs_commit_write(struct file *file, struct page *page,
1892 unsigned from, unsigned to)
1893{
Chris Masona52d9a82007-08-27 16:49:44 -04001894 return extent_commit_write(&BTRFS_I(page->mapping->host)->extent_tree,
1895 page->mapping->host, page, from, to);
Chris Mason39279cc2007-06-12 06:35:45 -04001896}
1897
1898static int create_subvol(struct btrfs_root *root, char *name, int namelen)
1899{
1900 struct btrfs_trans_handle *trans;
1901 struct btrfs_key key;
1902 struct btrfs_root_item root_item;
1903 struct btrfs_inode_item *inode_item;
Chris Mason5f39d392007-10-15 16:14:19 -04001904 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001905 struct btrfs_root *new_root;
1906 struct inode *inode;
1907 struct inode *dir;
1908 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001909 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04001910 u64 objectid;
1911 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001912 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001913
1914 mutex_lock(&root->fs_info->fs_mutex);
1915 trans = btrfs_start_transaction(root, 1);
1916 BUG_ON(!trans);
1917
Chris Mason7bb86312007-12-11 09:25:06 -05001918 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
1919 0, &objectid);
1920 if (ret)
1921 goto fail;
1922
1923 leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
1924 objectid, trans->transid, 0, 0,
1925 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001926 if (IS_ERR(leaf))
1927 return PTR_ERR(leaf);
1928
1929 btrfs_set_header_nritems(leaf, 0);
1930 btrfs_set_header_level(leaf, 0);
Chris Masondb945352007-10-15 16:15:53 -04001931 btrfs_set_header_bytenr(leaf, leaf->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001932 btrfs_set_header_generation(leaf, trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05001933 btrfs_set_header_owner(leaf, objectid);
1934
Chris Mason5f39d392007-10-15 16:14:19 -04001935 write_extent_buffer(leaf, root->fs_info->fsid,
1936 (unsigned long)btrfs_header_fsid(leaf),
1937 BTRFS_FSID_SIZE);
1938 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001939
1940 inode_item = &root_item.inode;
1941 memset(inode_item, 0, sizeof(*inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04001942 inode_item->generation = cpu_to_le64(1);
1943 inode_item->size = cpu_to_le64(3);
1944 inode_item->nlink = cpu_to_le32(1);
1945 inode_item->nblocks = cpu_to_le64(1);
1946 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
Chris Mason39279cc2007-06-12 06:35:45 -04001947
Chris Masondb945352007-10-15 16:15:53 -04001948 btrfs_set_root_bytenr(&root_item, leaf->start);
1949 btrfs_set_root_level(&root_item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001950 btrfs_set_root_refs(&root_item, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001951 btrfs_set_root_used(&root_item, 0);
1952
Chris Mason5eda7b52007-06-22 14:16:25 -04001953 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
1954 root_item.drop_level = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001955
1956 free_extent_buffer(leaf);
1957 leaf = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001958
Chris Mason39279cc2007-06-12 06:35:45 -04001959 btrfs_set_root_dirid(&root_item, new_dirid);
1960
1961 key.objectid = objectid;
1962 key.offset = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001963 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
1964 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
1965 &root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -04001966 if (ret)
1967 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001968
1969 /*
1970 * insert the directory item
1971 */
1972 key.offset = (u64)-1;
1973 dir = root->fs_info->sb->s_root->d_inode;
1974 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
1975 name, namelen, dir->i_ino, &key,
1976 BTRFS_FT_DIR);
Chris Mason54aa1f42007-06-22 14:16:25 -04001977 if (ret)
1978 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001979
1980 ret = btrfs_commit_transaction(trans, root);
Chris Mason54aa1f42007-06-22 14:16:25 -04001981 if (ret)
1982 goto fail_commit;
Chris Mason39279cc2007-06-12 06:35:45 -04001983
Josef Bacik58176a92007-08-29 15:47:34 -04001984 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
Chris Mason39279cc2007-06-12 06:35:45 -04001985 BUG_ON(!new_root);
1986
1987 trans = btrfs_start_transaction(new_root, 1);
1988 BUG_ON(!trans);
1989
1990 inode = btrfs_new_inode(trans, new_root, new_dirid,
1991 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
Chris Mason54aa1f42007-06-22 14:16:25 -04001992 if (IS_ERR(inode))
1993 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001994 inode->i_op = &btrfs_dir_inode_operations;
1995 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04001996 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001997
1998 ret = btrfs_make_empty_dir(trans, new_root, new_dirid, new_dirid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001999 if (ret)
2000 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002001
2002 inode->i_nlink = 1;
2003 inode->i_size = 6;
2004 ret = btrfs_update_inode(trans, new_root, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002005 if (ret)
2006 goto fail;
2007fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002008 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04002009 err = btrfs_commit_transaction(trans, root);
2010 if (err && !ret)
2011 ret = err;
2012fail_commit:
Chris Mason39279cc2007-06-12 06:35:45 -04002013 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002014 btrfs_btree_balance_dirty(root, nr);
Chris Mason54aa1f42007-06-22 14:16:25 -04002015 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002016}
2017
2018static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2019{
2020 struct btrfs_trans_handle *trans;
2021 struct btrfs_key key;
2022 struct btrfs_root_item new_root_item;
Chris Mason5f39d392007-10-15 16:14:19 -04002023 struct extent_buffer *tmp;
Chris Mason39279cc2007-06-12 06:35:45 -04002024 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002025 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04002026 u64 objectid;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002027 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002028
2029 if (!root->ref_cows)
2030 return -EINVAL;
2031
Chris Mason011410b2007-09-10 19:58:36 -04002032 down_write(&root->snap_sem);
2033 freeze_bdev(root->fs_info->sb->s_bdev);
2034 thaw_bdev(root->fs_info->sb->s_bdev, root->fs_info->sb);
2035
Chris Mason39279cc2007-06-12 06:35:45 -04002036 mutex_lock(&root->fs_info->fs_mutex);
2037 trans = btrfs_start_transaction(root, 1);
2038 BUG_ON(!trans);
2039
2040 ret = btrfs_update_inode(trans, root, root->inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002041 if (ret)
2042 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002043
2044 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2045 0, &objectid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002046 if (ret)
2047 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002048
2049 memcpy(&new_root_item, &root->root_item,
2050 sizeof(new_root_item));
2051
2052 key.objectid = objectid;
2053 key.offset = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002054 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
Yan96919752007-12-04 13:20:20 -05002055 extent_buffer_get(root->node);
Chris Mason83df7c12007-08-27 16:49:44 -04002056 btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp);
Yan96919752007-12-04 13:20:20 -05002057 free_extent_buffer(tmp);
Chris Masondb945352007-10-15 16:15:53 -04002058 btrfs_set_root_bytenr(&new_root_item, root->node->start);
2059 btrfs_set_root_level(&new_root_item, btrfs_header_level(root->node));
Chris Mason39279cc2007-06-12 06:35:45 -04002060 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2061 &new_root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -04002062 if (ret)
2063 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002064
2065 /*
2066 * insert the directory item
2067 */
2068 key.offset = (u64)-1;
2069 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2070 name, namelen,
2071 root->fs_info->sb->s_root->d_inode->i_ino,
2072 &key, BTRFS_FT_DIR);
2073
Chris Mason54aa1f42007-06-22 14:16:25 -04002074 if (ret)
2075 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002076
Chris Mason7bb86312007-12-11 09:25:06 -05002077 ret = btrfs_inc_root_ref(trans, root, objectid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002078 if (ret)
2079 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002080fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002081 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04002082 err = btrfs_commit_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002083
Chris Mason54aa1f42007-06-22 14:16:25 -04002084 if (err && !ret)
2085 ret = err;
Chris Mason5f39d392007-10-15 16:14:19 -04002086
Chris Mason39279cc2007-06-12 06:35:45 -04002087 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason011410b2007-09-10 19:58:36 -04002088 up_write(&root->snap_sem);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002089 btrfs_btree_balance_dirty(root, nr);
Chris Mason54aa1f42007-06-22 14:16:25 -04002090 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002091}
2092
Chris Mason86479a02007-09-10 19:58:16 -04002093static unsigned long force_ra(struct address_space *mapping,
2094 struct file_ra_state *ra, struct file *file,
2095 pgoff_t offset, pgoff_t last_index)
2096{
2097 pgoff_t req_size;
2098
2099#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2100 req_size = last_index - offset + 1;
2101 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2102 return offset;
2103#else
2104 req_size = min(last_index - offset + 1, (pgoff_t)128);
2105 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2106 return offset + req_size;
2107#endif
2108}
2109
2110int btrfs_defrag_file(struct file *file) {
2111 struct inode *inode = file->f_path.dentry->d_inode;
2112 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2113 struct page *page;
2114 unsigned long last_index;
2115 unsigned long ra_index = 0;
2116 u64 page_start;
2117 u64 page_end;
2118 unsigned long i;
2119
2120 mutex_lock(&inode->i_mutex);
2121 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2122 for (i = 0; i <= last_index; i++) {
2123 if (i == ra_index) {
2124 ra_index = force_ra(inode->i_mapping, &file->f_ra,
2125 file, ra_index, last_index);
2126 }
2127 page = grab_cache_page(inode->i_mapping, i);
2128 if (!page)
2129 goto out_unlock;
2130 if (!PageUptodate(page)) {
2131 btrfs_readpage(NULL, page);
2132 lock_page(page);
2133 if (!PageUptodate(page)) {
2134 unlock_page(page);
2135 page_cache_release(page);
2136 goto out_unlock;
2137 }
2138 }
Chris Mason35ebb932007-10-30 16:56:53 -04002139 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason86479a02007-09-10 19:58:16 -04002140 page_end = page_start + PAGE_CACHE_SIZE - 1;
2141
2142 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
2143 set_extent_delalloc(em_tree, page_start,
2144 page_end, GFP_NOFS);
2145 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
2146 set_page_dirty(page);
2147 unlock_page(page);
2148 page_cache_release(page);
2149 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2150 }
2151
2152out_unlock:
2153 mutex_unlock(&inode->i_mutex);
2154 return 0;
2155}
2156
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002157static int btrfs_ioctl_snap_create(struct btrfs_root *root, void __user *arg)
2158{
2159 struct btrfs_ioctl_vol_args vol_args;
2160 struct btrfs_dir_item *di;
2161 struct btrfs_path *path;
2162 int namelen;
2163 u64 root_dirid;
2164
2165 if (copy_from_user(&vol_args, arg, sizeof(vol_args)))
2166 return -EFAULT;
Chris Mason5f39d392007-10-15 16:14:19 -04002167
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002168 namelen = strlen(vol_args.name);
2169 if (namelen > BTRFS_VOL_NAME_MAX)
2170 return -EINVAL;
2171 if (strchr(vol_args.name, '/'))
2172 return -EINVAL;
2173
2174 path = btrfs_alloc_path();
2175 if (!path)
2176 return -ENOMEM;
2177
2178 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2179 mutex_lock(&root->fs_info->fs_mutex);
2180 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2181 path, root_dirid,
2182 vol_args.name, namelen, 0);
2183 mutex_unlock(&root->fs_info->fs_mutex);
2184 btrfs_free_path(path);
2185 if (di && !IS_ERR(di))
2186 return -EEXIST;
2187 if (IS_ERR(di))
2188 return PTR_ERR(di);
2189
2190 if (root == root->fs_info->tree_root)
2191 return create_subvol(root, vol_args.name, namelen);
2192 return create_snapshot(root, vol_args.name, namelen);
2193}
2194
2195static int btrfs_ioctl_defrag(struct file *file)
Chris Mason39279cc2007-06-12 06:35:45 -04002196{
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002197 struct inode *inode = file->f_path.dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002198 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002199
2200 switch (inode->i_mode & S_IFMT) {
2201 case S_IFDIR:
2202 mutex_lock(&root->fs_info->fs_mutex);
2203 btrfs_defrag_root(root, 0);
2204 btrfs_defrag_root(root->fs_info->extent_root, 0);
2205 mutex_unlock(&root->fs_info->fs_mutex);
2206 break;
2207 case S_IFREG:
2208 btrfs_defrag_file(file);
2209 break;
2210 }
2211
2212 return 0;
2213}
2214
2215long btrfs_ioctl(struct file *file, unsigned int
2216 cmd, unsigned long arg)
2217{
2218 struct btrfs_root *root = BTRFS_I(file->f_path.dentry->d_inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002219
2220 switch (cmd) {
2221 case BTRFS_IOC_SNAP_CREATE:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002222 return btrfs_ioctl_snap_create(root, (void __user *)arg);
Chris Mason6702ed42007-08-07 16:15:09 -04002223 case BTRFS_IOC_DEFRAG:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002224 return btrfs_ioctl_defrag(file);
Chris Mason39279cc2007-06-12 06:35:45 -04002225 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002226
2227 return -ENOTTY;
Chris Mason39279cc2007-06-12 06:35:45 -04002228}
2229
Chris Mason39279cc2007-06-12 06:35:45 -04002230/*
2231 * Called inside transaction, so use GFP_NOFS
2232 */
2233struct inode *btrfs_alloc_inode(struct super_block *sb)
2234{
2235 struct btrfs_inode *ei;
2236
2237 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2238 if (!ei)
2239 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002240 ei->last_trans = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002241 return &ei->vfs_inode;
2242}
2243
2244void btrfs_destroy_inode(struct inode *inode)
2245{
2246 WARN_ON(!list_empty(&inode->i_dentry));
2247 WARN_ON(inode->i_data.nrpages);
2248
2249 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2250}
2251
Chris Mason44ec0b72007-10-29 10:55:05 -04002252#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2253static void init_once(struct kmem_cache * cachep, void *foo)
2254#else
Chris Mason39279cc2007-06-12 06:35:45 -04002255static void init_once(void * foo, struct kmem_cache * cachep,
2256 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04002257#endif
Chris Mason39279cc2007-06-12 06:35:45 -04002258{
2259 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2260
2261 inode_init_once(&ei->vfs_inode);
2262}
2263
2264void btrfs_destroy_cachep(void)
2265{
2266 if (btrfs_inode_cachep)
2267 kmem_cache_destroy(btrfs_inode_cachep);
2268 if (btrfs_trans_handle_cachep)
2269 kmem_cache_destroy(btrfs_trans_handle_cachep);
2270 if (btrfs_transaction_cachep)
2271 kmem_cache_destroy(btrfs_transaction_cachep);
2272 if (btrfs_bit_radix_cachep)
2273 kmem_cache_destroy(btrfs_bit_radix_cachep);
2274 if (btrfs_path_cachep)
2275 kmem_cache_destroy(btrfs_path_cachep);
2276}
2277
Chris Mason86479a02007-09-10 19:58:16 -04002278struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04002279 unsigned long extra_flags,
Chris Mason44ec0b72007-10-29 10:55:05 -04002280#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2281 void (*ctor)(struct kmem_cache *, void *)
2282#else
Chris Mason92fee662007-07-25 12:31:35 -04002283 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04002284 unsigned long)
2285#endif
2286 )
Chris Mason92fee662007-07-25 12:31:35 -04002287{
2288 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2289 SLAB_MEM_SPREAD | extra_flags), ctor
2290#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2291 ,NULL
2292#endif
2293 );
2294}
2295
Chris Mason39279cc2007-06-12 06:35:45 -04002296int btrfs_init_cachep(void)
2297{
Chris Mason86479a02007-09-10 19:58:16 -04002298 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04002299 sizeof(struct btrfs_inode),
2300 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04002301 if (!btrfs_inode_cachep)
2302 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002303 btrfs_trans_handle_cachep =
2304 btrfs_cache_create("btrfs_trans_handle_cache",
2305 sizeof(struct btrfs_trans_handle),
2306 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002307 if (!btrfs_trans_handle_cachep)
2308 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002309 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04002310 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04002311 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002312 if (!btrfs_transaction_cachep)
2313 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002314 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04002315 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04002316 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002317 if (!btrfs_path_cachep)
2318 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002319 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04002320 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002321 if (!btrfs_bit_radix_cachep)
2322 goto fail;
2323 return 0;
2324fail:
2325 btrfs_destroy_cachep();
2326 return -ENOMEM;
2327}
2328
2329static int btrfs_getattr(struct vfsmount *mnt,
2330 struct dentry *dentry, struct kstat *stat)
2331{
2332 struct inode *inode = dentry->d_inode;
2333 generic_fillattr(inode, stat);
2334 stat->blksize = 256 * 1024;
2335 return 0;
2336}
2337
2338static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2339 struct inode * new_dir,struct dentry *new_dentry)
2340{
2341 struct btrfs_trans_handle *trans;
2342 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2343 struct inode *new_inode = new_dentry->d_inode;
2344 struct inode *old_inode = old_dentry->d_inode;
2345 struct timespec ctime = CURRENT_TIME;
2346 struct btrfs_path *path;
2347 struct btrfs_dir_item *di;
2348 int ret;
2349
2350 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2351 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2352 return -ENOTEMPTY;
2353 }
Chris Mason5f39d392007-10-15 16:14:19 -04002354
Chris Mason39279cc2007-06-12 06:35:45 -04002355 mutex_lock(&root->fs_info->fs_mutex);
2356 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002357
Chris Mason39279cc2007-06-12 06:35:45 -04002358 btrfs_set_trans_block_group(trans, new_dir);
2359 path = btrfs_alloc_path();
2360 if (!path) {
2361 ret = -ENOMEM;
2362 goto out_fail;
2363 }
2364
2365 old_dentry->d_inode->i_nlink++;
2366 old_dir->i_ctime = old_dir->i_mtime = ctime;
2367 new_dir->i_ctime = new_dir->i_mtime = ctime;
2368 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04002369
Chris Mason39279cc2007-06-12 06:35:45 -04002370 if (S_ISDIR(old_inode->i_mode) && old_dir != new_dir) {
2371 struct btrfs_key *location = &BTRFS_I(new_dir)->location;
Chris Mason5f39d392007-10-15 16:14:19 -04002372 struct btrfs_key old_parent_key;
Chris Mason39279cc2007-06-12 06:35:45 -04002373 di = btrfs_lookup_dir_item(trans, root, path, old_inode->i_ino,
2374 "..", 2, -1);
2375 if (IS_ERR(di)) {
2376 ret = PTR_ERR(di);
2377 goto out_fail;
2378 }
2379 if (!di) {
2380 ret = -ENOENT;
2381 goto out_fail;
2382 }
Chris Mason5f39d392007-10-15 16:14:19 -04002383 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &old_parent_key);
Chris Mason39279cc2007-06-12 06:35:45 -04002384 ret = btrfs_del_item(trans, root, path);
2385 if (ret) {
Chris Mason39279cc2007-06-12 06:35:45 -04002386 goto out_fail;
2387 }
2388 btrfs_release_path(root, path);
2389
2390 di = btrfs_lookup_dir_index_item(trans, root, path,
2391 old_inode->i_ino,
Chris Mason5f39d392007-10-15 16:14:19 -04002392 old_parent_key.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002393 "..", 2, -1);
2394 if (IS_ERR(di)) {
2395 ret = PTR_ERR(di);
2396 goto out_fail;
2397 }
2398 if (!di) {
2399 ret = -ENOENT;
2400 goto out_fail;
2401 }
2402 ret = btrfs_del_item(trans, root, path);
2403 if (ret) {
Chris Mason39279cc2007-06-12 06:35:45 -04002404 goto out_fail;
2405 }
2406 btrfs_release_path(root, path);
2407
2408 ret = btrfs_insert_dir_item(trans, root, "..", 2,
2409 old_inode->i_ino, location,
2410 BTRFS_FT_DIR);
2411 if (ret)
2412 goto out_fail;
2413 }
2414
2415
2416 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2417 if (ret)
2418 goto out_fail;
2419
2420 if (new_inode) {
2421 new_inode->i_ctime = CURRENT_TIME;
2422 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2423 if (ret)
2424 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002425 }
2426 ret = btrfs_add_link(trans, new_dentry, old_inode);
2427 if (ret)
2428 goto out_fail;
2429
2430out_fail:
2431 btrfs_free_path(path);
2432 btrfs_end_transaction(trans, root);
2433 mutex_unlock(&root->fs_info->fs_mutex);
2434 return ret;
2435}
2436
2437static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2438 const char *symname)
2439{
2440 struct btrfs_trans_handle *trans;
2441 struct btrfs_root *root = BTRFS_I(dir)->root;
2442 struct btrfs_path *path;
2443 struct btrfs_key key;
2444 struct inode *inode;
2445 int err;
2446 int drop_inode = 0;
2447 u64 objectid;
2448 int name_len;
2449 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04002450 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04002451 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04002452 struct extent_buffer *leaf;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002453 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002454
2455 name_len = strlen(symname) + 1;
2456 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2457 return -ENAMETOOLONG;
2458 mutex_lock(&root->fs_info->fs_mutex);
2459 trans = btrfs_start_transaction(root, 1);
2460 btrfs_set_trans_block_group(trans, dir);
2461
2462 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2463 if (err) {
2464 err = -ENOSPC;
2465 goto out_unlock;
2466 }
2467
2468 inode = btrfs_new_inode(trans, root, objectid,
2469 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2470 err = PTR_ERR(inode);
2471 if (IS_ERR(inode))
2472 goto out_unlock;
2473
2474 btrfs_set_trans_block_group(trans, inode);
2475 err = btrfs_add_nondir(trans, dentry, inode);
2476 if (err)
2477 drop_inode = 1;
2478 else {
2479 inode->i_mapping->a_ops = &btrfs_aops;
2480 inode->i_fop = &btrfs_file_operations;
2481 inode->i_op = &btrfs_file_inode_operations;
Chris Masona52d9a82007-08-27 16:49:44 -04002482 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
2483 inode->i_mapping, GFP_NOFS);
Chris Mason07157aa2007-08-30 08:50:51 -04002484 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002485 }
2486 dir->i_sb->s_dirt = 1;
2487 btrfs_update_inode_block_group(trans, inode);
2488 btrfs_update_inode_block_group(trans, dir);
2489 if (drop_inode)
2490 goto out_unlock;
2491
2492 path = btrfs_alloc_path();
2493 BUG_ON(!path);
2494 key.objectid = inode->i_ino;
2495 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002496 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2497 datasize = btrfs_file_extent_calc_inline_size(name_len);
2498 err = btrfs_insert_empty_item(trans, root, path, &key,
2499 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04002500 if (err) {
2501 drop_inode = 1;
2502 goto out_unlock;
2503 }
Chris Mason5f39d392007-10-15 16:14:19 -04002504 leaf = path->nodes[0];
2505 ei = btrfs_item_ptr(leaf, path->slots[0],
2506 struct btrfs_file_extent_item);
2507 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
2508 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04002509 BTRFS_FILE_EXTENT_INLINE);
2510 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04002511 write_extent_buffer(leaf, symname, ptr, name_len);
2512 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002513 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04002514
Chris Mason39279cc2007-06-12 06:35:45 -04002515 inode->i_op = &btrfs_symlink_inode_operations;
2516 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2517 inode->i_size = name_len - 1;
Chris Mason54aa1f42007-06-22 14:16:25 -04002518 err = btrfs_update_inode(trans, root, inode);
2519 if (err)
2520 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002521
2522out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002523 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04002524 btrfs_end_transaction(trans, root);
2525 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04002526 if (drop_inode) {
2527 inode_dec_link_count(inode);
2528 iput(inode);
2529 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002530 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002531 return err;
2532}
2533
2534static struct inode_operations btrfs_dir_inode_operations = {
2535 .lookup = btrfs_lookup,
2536 .create = btrfs_create,
2537 .unlink = btrfs_unlink,
2538 .link = btrfs_link,
2539 .mkdir = btrfs_mkdir,
2540 .rmdir = btrfs_rmdir,
2541 .rename = btrfs_rename,
2542 .symlink = btrfs_symlink,
2543 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04002544 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -05002545 .setxattr = generic_setxattr,
2546 .getxattr = generic_getxattr,
2547 .listxattr = btrfs_listxattr,
2548 .removexattr = generic_removexattr,
Chris Mason39279cc2007-06-12 06:35:45 -04002549};
2550
2551static struct inode_operations btrfs_dir_ro_inode_operations = {
2552 .lookup = btrfs_lookup,
2553};
2554
2555static struct file_operations btrfs_dir_file_operations = {
2556 .llseek = generic_file_llseek,
2557 .read = generic_read_dir,
2558 .readdir = btrfs_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002559 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002560#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002561 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002562#endif
2563};
2564
Chris Mason07157aa2007-08-30 08:50:51 -04002565static struct extent_map_ops btrfs_extent_map_ops = {
2566 .fill_delalloc = run_delalloc_range,
2567 .writepage_io_hook = btrfs_writepage_io_hook,
2568 .readpage_io_hook = btrfs_readpage_io_hook,
2569 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
2570};
2571
Chris Mason39279cc2007-06-12 06:35:45 -04002572static struct address_space_operations btrfs_aops = {
2573 .readpage = btrfs_readpage,
2574 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04002575 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05002576 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04002577 .sync_page = block_sync_page,
2578 .prepare_write = btrfs_prepare_write,
2579 .commit_write = btrfs_commit_write,
2580 .bmap = btrfs_bmap,
Chris Masona52d9a82007-08-27 16:49:44 -04002581 .invalidatepage = btrfs_invalidatepage,
2582 .releasepage = btrfs_releasepage,
2583 .set_page_dirty = __set_page_dirty_nobuffers,
Chris Mason39279cc2007-06-12 06:35:45 -04002584};
2585
2586static struct address_space_operations btrfs_symlink_aops = {
2587 .readpage = btrfs_readpage,
2588 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04002589 .invalidatepage = btrfs_invalidatepage,
2590 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04002591};
2592
2593static struct inode_operations btrfs_file_inode_operations = {
2594 .truncate = btrfs_truncate,
2595 .getattr = btrfs_getattr,
2596 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -05002597 .setxattr = generic_setxattr,
2598 .getxattr = generic_getxattr,
2599 .listxattr = btrfs_listxattr,
2600 .removexattr = generic_removexattr,
Chris Mason39279cc2007-06-12 06:35:45 -04002601};
2602
Josef Bacik618e21d2007-07-11 10:18:17 -04002603static struct inode_operations btrfs_special_inode_operations = {
2604 .getattr = btrfs_getattr,
2605 .setattr = btrfs_setattr,
2606};
2607
Chris Mason39279cc2007-06-12 06:35:45 -04002608static struct inode_operations btrfs_symlink_inode_operations = {
2609 .readlink = generic_readlink,
2610 .follow_link = page_follow_link_light,
2611 .put_link = page_put_link,
2612};