blob: 03fea037667e4d61924240a9f853cb5e4c7a22ab [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 Masond8d5f3e2007-12-11 12:42:00 -0500566 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400567 int found_extent;
568 int del_item;
Chris Mason179e29e2007-11-01 11:28:41 -0400569 int extent_type = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400570
Chris Masona52d9a82007-08-27 16:49:44 -0400571 btrfs_drop_extent_cache(inode, inode->i_size, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -0400572 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -0400573 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400574 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -0400575
Chris Mason39279cc2007-06-12 06:35:45 -0400576 /* FIXME, add redo link to tree so we don't leak on crash */
577 key.objectid = inode->i_ino;
578 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -0400579 key.type = (u8)-1;
580
Chris Mason39279cc2007-06-12 06:35:45 -0400581 while(1) {
582 btrfs_init_path(path);
583 fi = NULL;
584 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
585 if (ret < 0) {
586 goto error;
587 }
588 if (ret > 0) {
589 BUG_ON(path->slots[0] == 0);
590 path->slots[0]--;
591 }
Chris Mason5f39d392007-10-15 16:14:19 -0400592 leaf = path->nodes[0];
593 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
594 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -0400595
Chris Mason5f39d392007-10-15 16:14:19 -0400596 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -0400597 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400598
Chris Mason39279cc2007-06-12 06:35:45 -0400599 if (found_type != BTRFS_CSUM_ITEM_KEY &&
600 found_type != BTRFS_DIR_ITEM_KEY &&
601 found_type != BTRFS_DIR_INDEX_KEY &&
602 found_type != BTRFS_EXTENT_DATA_KEY)
603 break;
604
Chris Mason5f39d392007-10-15 16:14:19 -0400605 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -0400606 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -0400607 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400608 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -0400609 extent_type = btrfs_file_extent_type(leaf, fi);
610 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -0400611 item_end +=
Chris Masondb945352007-10-15 16:15:53 -0400612 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -0400613 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
614 struct btrfs_item *item = btrfs_item_nr(leaf,
615 path->slots[0]);
616 item_end += btrfs_file_extent_inline_len(leaf,
617 item);
Chris Mason39279cc2007-06-12 06:35:45 -0400618 }
Yan008630c2007-11-07 13:31:09 -0500619 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -0400620 }
621 if (found_type == BTRFS_CSUM_ITEM_KEY) {
622 ret = btrfs_csum_truncate(trans, root, path,
623 inode->i_size);
624 BUG_ON(ret);
625 }
Yan008630c2007-11-07 13:31:09 -0500626 if (item_end < inode->i_size) {
Chris Masonb888db22007-08-27 16:49:44 -0400627 if (found_type == BTRFS_DIR_ITEM_KEY) {
628 found_type = BTRFS_INODE_ITEM_KEY;
629 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
630 found_type = BTRFS_CSUM_ITEM_KEY;
631 } else if (found_type) {
632 found_type--;
633 } else {
634 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400635 }
Yana61721d2007-09-17 11:08:38 -0400636 btrfs_set_key_type(&key, found_type);
Yan65555a02007-10-25 15:42:57 -0400637 btrfs_release_path(root, path);
Chris Masonb888db22007-08-27 16:49:44 -0400638 continue;
Chris Mason39279cc2007-06-12 06:35:45 -0400639 }
Chris Mason5f39d392007-10-15 16:14:19 -0400640 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -0400641 del_item = 1;
642 else
643 del_item = 0;
644 found_extent = 0;
645
646 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -0400647 if (found_type != BTRFS_EXTENT_DATA_KEY)
648 goto delete;
649
650 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -0400651 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -0400652 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400653 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -0400654 u64 orig_num_bytes =
655 btrfs_file_extent_num_bytes(leaf, fi);
656 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -0400657 found_key.offset + root->sectorsize - 1;
Chris Masondb945352007-10-15 16:15:53 -0400658 btrfs_set_file_extent_num_bytes(leaf, fi,
659 extent_num_bytes);
660 num_dec = (orig_num_bytes -
661 extent_num_bytes) >> 9;
Yanbab9fb02007-09-17 11:13:11 -0400662 if (extent_start != 0) {
663 inode->i_blocks -= num_dec;
664 }
Chris Mason5f39d392007-10-15 16:14:19 -0400665 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -0400666 } else {
Chris Masondb945352007-10-15 16:15:53 -0400667 extent_num_bytes =
668 btrfs_file_extent_disk_num_bytes(leaf,
669 fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400670 /* FIXME blocksize != 4096 */
Chris Masondb945352007-10-15 16:15:53 -0400671 num_dec = btrfs_file_extent_num_bytes(leaf,
672 fi) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -0400673 if (extent_start != 0) {
674 found_extent = 1;
675 inode->i_blocks -= num_dec;
676 }
Chris Masond8d5f3e2007-12-11 12:42:00 -0500677 root_gen = btrfs_header_generation(leaf);
678 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -0400679 }
Chris Mason179e29e2007-11-01 11:28:41 -0400680 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE &&
681 !del_item) {
682 u32 newsize = inode->i_size - found_key.offset;
683 newsize = btrfs_file_extent_calc_inline_size(newsize);
684 ret = btrfs_truncate_item(trans, root, path,
685 newsize, 1);
686 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -0400687 }
Chris Mason179e29e2007-11-01 11:28:41 -0400688delete:
Chris Mason39279cc2007-06-12 06:35:45 -0400689 if (del_item) {
690 ret = btrfs_del_item(trans, root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400691 if (ret)
692 goto error;
Chris Mason39279cc2007-06-12 06:35:45 -0400693 } else {
694 break;
695 }
696 btrfs_release_path(root, path);
697 if (found_extent) {
698 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -0500699 extent_num_bytes,
Chris Masond8d5f3e2007-12-11 12:42:00 -0500700 root_owner,
Chris Mason7bb86312007-12-11 09:25:06 -0500701 root_gen, inode->i_ino,
702 found_key.offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -0400703 BUG_ON(ret);
704 }
705 }
706 ret = 0;
707error:
708 btrfs_release_path(root, path);
709 btrfs_free_path(path);
710 inode->i_sb->s_dirt = 1;
711 return ret;
712}
713
Chris Masonb888db22007-08-27 16:49:44 -0400714static int btrfs_cow_one_page(struct inode *inode, struct page *page,
Chris Masona52d9a82007-08-27 16:49:44 -0400715 size_t zero_start)
Chris Mason39279cc2007-06-12 06:35:45 -0400716{
Chris Mason39279cc2007-06-12 06:35:45 -0400717 char *kaddr;
718 int ret = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400719 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason35ebb932007-10-30 16:56:53 -0400720 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masonb888db22007-08-27 16:49:44 -0400721 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Mason39279cc2007-06-12 06:35:45 -0400722
Christoph Hellwigb3cfa352007-09-17 11:25:58 -0400723 set_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400724
Chris Masonb888db22007-08-27 16:49:44 -0400725 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
726 set_extent_delalloc(&BTRFS_I(inode)->extent_tree, page_start,
727 page_end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -0400728 if (zero_start != PAGE_CACHE_SIZE) {
Chris Masonb888db22007-08-27 16:49:44 -0400729 kaddr = kmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400730 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
731 flush_dcache_page(page);
Chris Masonb888db22007-08-27 16:49:44 -0400732 kunmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400733 }
Chris Masonb888db22007-08-27 16:49:44 -0400734 set_page_dirty(page);
735 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -0400736
Chris Masona52d9a82007-08-27 16:49:44 -0400737 return ret;
738}
739
740/*
741 * taken from block_truncate_page, but does cow as it zeros out
742 * any bytes left in the last page in the file.
743 */
744static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
745{
746 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -0400747 struct btrfs_root *root = BTRFS_I(inode)->root;
748 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -0400749 pgoff_t index = from >> PAGE_CACHE_SHIFT;
750 unsigned offset = from & (PAGE_CACHE_SIZE-1);
751 struct page *page;
752 int ret = 0;
753 u64 page_start;
754
755 if ((offset & (blocksize - 1)) == 0)
756 goto out;
757
Chris Masondb945352007-10-15 16:15:53 -0400758 down_read(&root->snap_sem);
Chris Masona52d9a82007-08-27 16:49:44 -0400759 ret = -ENOMEM;
760 page = grab_cache_page(mapping, index);
761 if (!page)
762 goto out;
763 if (!PageUptodate(page)) {
764 ret = btrfs_readpage(NULL, page);
765 lock_page(page);
766 if (!PageUptodate(page)) {
767 ret = -EIO;
768 goto out;
769 }
770 }
Chris Mason35ebb932007-10-30 16:56:53 -0400771 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -0400772
Chris Masonb888db22007-08-27 16:49:44 -0400773 ret = btrfs_cow_one_page(inode, page, offset);
Chris Mason39279cc2007-06-12 06:35:45 -0400774
Chris Mason39279cc2007-06-12 06:35:45 -0400775 unlock_page(page);
776 page_cache_release(page);
Chris Mason011410b2007-09-10 19:58:36 -0400777 up_read(&BTRFS_I(inode)->root->snap_sem);
Chris Mason39279cc2007-06-12 06:35:45 -0400778out:
779 return ret;
780}
781
782static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
783{
784 struct inode *inode = dentry->d_inode;
785 int err;
786
787 err = inode_change_ok(inode, attr);
788 if (err)
789 return err;
790
791 if (S_ISREG(inode->i_mode) &&
792 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
793 struct btrfs_trans_handle *trans;
794 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason2bf5a722007-08-30 11:54:02 -0400795 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
796
Chris Mason5f39d392007-10-15 16:14:19 -0400797 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -0400798 u64 pos = (inode->i_size + mask) & ~mask;
Chris Mason2bf5a722007-08-30 11:54:02 -0400799 u64 block_end = attr->ia_size | mask;
Chris Mason39279cc2007-06-12 06:35:45 -0400800 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -0400801 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400802
803 if (attr->ia_size <= pos)
804 goto out;
805
806 btrfs_truncate_page(inode->i_mapping, inode->i_size);
807
Chris Mason2bf5a722007-08-30 11:54:02 -0400808 lock_extent(em_tree, pos, block_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -0400809 hole_size = (attr->ia_size - pos + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -0400810
811 mutex_lock(&root->fs_info->fs_mutex);
812 trans = btrfs_start_transaction(root, 1);
813 btrfs_set_trans_block_group(trans, inode);
Chris Mason2bf5a722007-08-30 11:54:02 -0400814 err = btrfs_drop_extents(trans, root, inode,
Chris Mason3326d1b2007-10-15 16:18:25 -0400815 pos, pos + hole_size, pos,
816 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -0400817
Chris Mason179e29e2007-11-01 11:28:41 -0400818 if (alloc_hint != EXTENT_MAP_INLINE) {
819 err = btrfs_insert_file_extent(trans, root,
820 inode->i_ino,
821 pos, 0, 0, hole_size);
822 }
Chris Mason39279cc2007-06-12 06:35:45 -0400823 btrfs_end_transaction(trans, root);
824 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason2bf5a722007-08-30 11:54:02 -0400825 unlock_extent(em_tree, pos, block_end, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -0400826 if (err)
827 return err;
Chris Mason39279cc2007-06-12 06:35:45 -0400828 }
829out:
830 err = inode_setattr(inode, attr);
831
832 return err;
833}
834void btrfs_delete_inode(struct inode *inode)
835{
836 struct btrfs_trans_handle *trans;
837 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400838 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -0400839 int ret;
840
841 truncate_inode_pages(&inode->i_data, 0);
842 if (is_bad_inode(inode)) {
843 goto no_delete;
844 }
Chris Mason5f39d392007-10-15 16:14:19 -0400845
Chris Mason39279cc2007-06-12 06:35:45 -0400846 inode->i_size = 0;
847 mutex_lock(&root->fs_info->fs_mutex);
848 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400849
Chris Mason39279cc2007-06-12 06:35:45 -0400850 btrfs_set_trans_block_group(trans, inode);
851 ret = btrfs_truncate_in_trans(trans, root, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -0400852 if (ret)
853 goto no_delete_lock;
Josef Bacik5103e942007-11-16 11:45:54 -0500854 ret = btrfs_delete_xattrs(trans, root, inode);
855 if (ret)
856 goto no_delete_lock;
Chris Mason54aa1f42007-06-22 14:16:25 -0400857 ret = btrfs_free_inode(trans, root, inode);
858 if (ret)
859 goto no_delete_lock;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400860 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -0400861
Chris Mason39279cc2007-06-12 06:35:45 -0400862 btrfs_end_transaction(trans, root);
863 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400864 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -0400865 return;
Chris Mason54aa1f42007-06-22 14:16:25 -0400866
867no_delete_lock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400868 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -0400869 btrfs_end_transaction(trans, root);
870 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400871 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -0400872no_delete:
873 clear_inode(inode);
874}
875
876/*
877 * this returns the key found in the dir entry in the location pointer.
878 * If no dir entries were found, location->objectid is 0.
879 */
880static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
881 struct btrfs_key *location)
882{
883 const char *name = dentry->d_name.name;
884 int namelen = dentry->d_name.len;
885 struct btrfs_dir_item *di;
886 struct btrfs_path *path;
887 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -0400888 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400889
890 path = btrfs_alloc_path();
891 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400892 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
893 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -0400894 if (IS_ERR(di))
895 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -0400896 if (!di || IS_ERR(di)) {
897 location->objectid = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400898 goto out;
899 }
Chris Mason5f39d392007-10-15 16:14:19 -0400900 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -0400901out:
902 btrfs_release_path(root, path);
903 btrfs_free_path(path);
904 return ret;
905}
906
907/*
908 * when we hit a tree root in a directory, the btrfs part of the inode
909 * needs to be changed to reflect the root directory of the tree root. This
910 * is kind of like crossing a mount point.
911 */
912static int fixup_tree_root_location(struct btrfs_root *root,
913 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -0400914 struct btrfs_root **sub_root,
915 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -0400916{
917 struct btrfs_path *path;
918 struct btrfs_root_item *ri;
919
920 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
921 return 0;
922 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
923 return 0;
924
925 path = btrfs_alloc_path();
926 BUG_ON(!path);
927 mutex_lock(&root->fs_info->fs_mutex);
928
Josef Bacik58176a92007-08-29 15:47:34 -0400929 *sub_root = btrfs_read_fs_root(root->fs_info, location,
930 dentry->d_name.name,
931 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -0400932 if (IS_ERR(*sub_root))
933 return PTR_ERR(*sub_root);
934
935 ri = &(*sub_root)->root_item;
936 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -0400937 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
938 location->offset = 0;
939
940 btrfs_free_path(path);
941 mutex_unlock(&root->fs_info->fs_mutex);
942 return 0;
943}
944
945static int btrfs_init_locked_inode(struct inode *inode, void *p)
946{
947 struct btrfs_iget_args *args = p;
948 inode->i_ino = args->ino;
949 BTRFS_I(inode)->root = args->root;
Chris Masonb888db22007-08-27 16:49:44 -0400950 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
951 inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -0400952 return 0;
953}
954
955static int btrfs_find_actor(struct inode *inode, void *opaque)
956{
957 struct btrfs_iget_args *args = opaque;
958 return (args->ino == inode->i_ino &&
959 args->root == BTRFS_I(inode)->root);
960}
961
962struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
963 struct btrfs_root *root)
964{
965 struct inode *inode;
966 struct btrfs_iget_args args;
967 args.ino = objectid;
968 args.root = root;
969
970 inode = iget5_locked(s, objectid, btrfs_find_actor,
971 btrfs_init_locked_inode,
972 (void *)&args);
973 return inode;
974}
975
976static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
977 struct nameidata *nd)
978{
979 struct inode * inode;
980 struct btrfs_inode *bi = BTRFS_I(dir);
981 struct btrfs_root *root = bi->root;
982 struct btrfs_root *sub_root = root;
983 struct btrfs_key location;
984 int ret;
985
986 if (dentry->d_name.len > BTRFS_NAME_LEN)
987 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -0400988
Chris Mason39279cc2007-06-12 06:35:45 -0400989 mutex_lock(&root->fs_info->fs_mutex);
990 ret = btrfs_inode_by_name(dir, dentry, &location);
991 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason5f39d392007-10-15 16:14:19 -0400992
Chris Mason39279cc2007-06-12 06:35:45 -0400993 if (ret < 0)
994 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -0400995
Chris Mason39279cc2007-06-12 06:35:45 -0400996 inode = NULL;
997 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -0400998 ret = fixup_tree_root_location(root, &location, &sub_root,
999 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04001000 if (ret < 0)
1001 return ERR_PTR(ret);
1002 if (ret > 0)
1003 return ERR_PTR(-ENOENT);
1004 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1005 sub_root);
1006 if (!inode)
1007 return ERR_PTR(-EACCES);
1008 if (inode->i_state & I_NEW) {
1009 /* the inode and parent dir are two different roots */
1010 if (sub_root != root) {
1011 igrab(inode);
1012 sub_root->inode = inode;
1013 }
1014 BTRFS_I(inode)->root = sub_root;
1015 memcpy(&BTRFS_I(inode)->location, &location,
1016 sizeof(location));
1017 btrfs_read_locked_inode(inode);
1018 unlock_new_inode(inode);
1019 }
1020 }
1021 return d_splice_alias(inode, dentry);
1022}
1023
Chris Mason39279cc2007-06-12 06:35:45 -04001024static unsigned char btrfs_filetype_table[] = {
1025 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1026};
1027
1028static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1029{
1030 struct inode *inode = filp->f_path.dentry->d_inode;
1031 struct btrfs_root *root = BTRFS_I(inode)->root;
1032 struct btrfs_item *item;
1033 struct btrfs_dir_item *di;
1034 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001035 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001036 struct btrfs_path *path;
1037 int ret;
1038 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001039 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001040 int slot;
1041 int advance;
1042 unsigned char d_type;
1043 int over = 0;
1044 u32 di_cur;
1045 u32 di_total;
1046 u32 di_len;
1047 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001048 char tmp_name[32];
1049 char *name_ptr;
1050 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001051
1052 /* FIXME, use a real flag for deciding about the key type */
1053 if (root->fs_info->tree_root == root)
1054 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001055
Chris Mason39279cc2007-06-12 06:35:45 -04001056 mutex_lock(&root->fs_info->fs_mutex);
1057 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04001058 btrfs_set_key_type(&key, key_type);
1059 key.offset = filp->f_pos;
Chris Mason5f39d392007-10-15 16:14:19 -04001060
Chris Mason39279cc2007-06-12 06:35:45 -04001061 path = btrfs_alloc_path();
Chris Mason2cc58cf2007-08-27 16:49:44 -04001062 path->reada = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04001063 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1064 if (ret < 0)
1065 goto err;
1066 advance = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001067 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001068 leaf = path->nodes[0];
1069 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001070 slot = path->slots[0];
1071 if (advance || slot >= nritems) {
1072 if (slot >= nritems -1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001073 ret = btrfs_next_leaf(root, path);
1074 if (ret)
1075 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001076 leaf = path->nodes[0];
1077 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001078 slot = path->slots[0];
1079 } else {
1080 slot++;
1081 path->slots[0]++;
1082 }
1083 }
1084 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001085 item = btrfs_item_nr(leaf, slot);
1086 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1087
1088 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04001089 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001090 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001091 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001092 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04001093 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001094
1095 filp->f_pos = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001096 advance = 1;
1097 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1098 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001099 di_total = btrfs_item_size(leaf, item);
Chris Mason39279cc2007-06-12 06:35:45 -04001100 while(di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04001101 struct btrfs_key location;
1102
1103 name_len = btrfs_dir_name_len(leaf, di);
1104 if (name_len < 32) {
1105 name_ptr = tmp_name;
1106 } else {
1107 name_ptr = kmalloc(name_len, GFP_NOFS);
1108 BUG_ON(!name_ptr);
1109 }
1110 read_extent_buffer(leaf, name_ptr,
1111 (unsigned long)(di + 1), name_len);
1112
1113 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1114 btrfs_dir_item_key_to_cpu(leaf, di, &location);
1115
1116 over = filldir(dirent, name_ptr, name_len,
1117 found_key.offset,
1118 location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001119 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04001120
1121 if (name_ptr != tmp_name)
1122 kfree(name_ptr);
1123
Chris Mason39279cc2007-06-12 06:35:45 -04001124 if (over)
1125 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05001126 di_len = btrfs_dir_name_len(leaf, di) +
1127 btrfs_dir_data_len(leaf, di) +sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04001128 di_cur += di_len;
1129 di = (struct btrfs_dir_item *)((char *)di + di_len);
1130 }
1131 }
1132 filp->f_pos++;
1133nopos:
1134 ret = 0;
1135err:
1136 btrfs_release_path(root, path);
1137 btrfs_free_path(path);
1138 mutex_unlock(&root->fs_info->fs_mutex);
1139 return ret;
1140}
1141
1142int btrfs_write_inode(struct inode *inode, int wait)
1143{
1144 struct btrfs_root *root = BTRFS_I(inode)->root;
1145 struct btrfs_trans_handle *trans;
1146 int ret = 0;
1147
1148 if (wait) {
1149 mutex_lock(&root->fs_info->fs_mutex);
1150 trans = btrfs_start_transaction(root, 1);
1151 btrfs_set_trans_block_group(trans, inode);
1152 ret = btrfs_commit_transaction(trans, root);
1153 mutex_unlock(&root->fs_info->fs_mutex);
1154 }
1155 return ret;
1156}
1157
1158/*
Chris Mason54aa1f42007-06-22 14:16:25 -04001159 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04001160 * inode changes. But, it is most likely to find the inode in cache.
1161 * FIXME, needs more benchmarking...there are no reasons other than performance
1162 * to keep or drop this code.
1163 */
1164void btrfs_dirty_inode(struct inode *inode)
1165{
1166 struct btrfs_root *root = BTRFS_I(inode)->root;
1167 struct btrfs_trans_handle *trans;
1168
1169 mutex_lock(&root->fs_info->fs_mutex);
1170 trans = btrfs_start_transaction(root, 1);
1171 btrfs_set_trans_block_group(trans, inode);
1172 btrfs_update_inode(trans, root, inode);
1173 btrfs_end_transaction(trans, root);
1174 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001175}
1176
1177static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1178 struct btrfs_root *root,
1179 u64 objectid,
1180 struct btrfs_block_group_cache *group,
1181 int mode)
1182{
1183 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001184 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04001185 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04001186 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04001187 int ret;
1188 int owner;
1189
Chris Mason5f39d392007-10-15 16:14:19 -04001190 path = btrfs_alloc_path();
1191 BUG_ON(!path);
1192
Chris Mason39279cc2007-06-12 06:35:45 -04001193 inode = new_inode(root->fs_info->sb);
1194 if (!inode)
1195 return ERR_PTR(-ENOMEM);
1196
Chris Masonb888db22007-08-27 16:49:44 -04001197 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1198 inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001199 BTRFS_I(inode)->root = root;
Chris Masonb888db22007-08-27 16:49:44 -04001200
Chris Mason39279cc2007-06-12 06:35:45 -04001201 if (mode & S_IFDIR)
1202 owner = 0;
1203 else
1204 owner = 1;
1205 group = btrfs_find_block_group(root, group, 0, 0, owner);
1206 BTRFS_I(inode)->block_group = group;
1207
Chris Mason5f39d392007-10-15 16:14:19 -04001208 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
1209 if (ret)
1210 goto fail;
1211
Chris Mason39279cc2007-06-12 06:35:45 -04001212 inode->i_uid = current->fsuid;
1213 inode->i_gid = current->fsgid;
1214 inode->i_mode = mode;
1215 inode->i_ino = objectid;
1216 inode->i_blocks = 0;
1217 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04001218 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1219 struct btrfs_inode_item);
1220 fill_inode_item(path->nodes[0], inode_item, inode);
1221 btrfs_mark_buffer_dirty(path->nodes[0]);
1222 btrfs_free_path(path);
1223
Chris Mason39279cc2007-06-12 06:35:45 -04001224 location = &BTRFS_I(inode)->location;
1225 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04001226 location->offset = 0;
1227 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1228
Chris Mason39279cc2007-06-12 06:35:45 -04001229 insert_inode_hash(inode);
1230 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001231fail:
1232 btrfs_free_path(path);
1233 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04001234}
1235
1236static inline u8 btrfs_inode_type(struct inode *inode)
1237{
1238 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1239}
1240
1241static int btrfs_add_link(struct btrfs_trans_handle *trans,
1242 struct dentry *dentry, struct inode *inode)
1243{
1244 int ret;
1245 struct btrfs_key key;
1246 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Chris Mason79c44582007-06-25 10:09:33 -04001247 struct inode *parent_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001248
Chris Mason39279cc2007-06-12 06:35:45 -04001249 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04001250 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1251 key.offset = 0;
1252
1253 ret = btrfs_insert_dir_item(trans, root,
1254 dentry->d_name.name, dentry->d_name.len,
1255 dentry->d_parent->d_inode->i_ino,
1256 &key, btrfs_inode_type(inode));
1257 if (ret == 0) {
Chris Mason79c44582007-06-25 10:09:33 -04001258 parent_inode = dentry->d_parent->d_inode;
1259 parent_inode->i_size += dentry->d_name.len * 2;
1260 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001261 ret = btrfs_update_inode(trans, root,
1262 dentry->d_parent->d_inode);
1263 }
1264 return ret;
1265}
1266
1267static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1268 struct dentry *dentry, struct inode *inode)
1269{
1270 int err = btrfs_add_link(trans, dentry, inode);
1271 if (!err) {
1272 d_instantiate(dentry, inode);
1273 return 0;
1274 }
1275 if (err > 0)
1276 err = -EEXIST;
1277 return err;
1278}
1279
Josef Bacik618e21d2007-07-11 10:18:17 -04001280static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1281 int mode, dev_t rdev)
1282{
1283 struct btrfs_trans_handle *trans;
1284 struct btrfs_root *root = BTRFS_I(dir)->root;
1285 struct inode *inode;
1286 int err;
1287 int drop_inode = 0;
1288 u64 objectid;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001289 unsigned long nr;
Josef Bacik618e21d2007-07-11 10:18:17 -04001290
1291 if (!new_valid_dev(rdev))
1292 return -EINVAL;
1293
1294 mutex_lock(&root->fs_info->fs_mutex);
1295 trans = btrfs_start_transaction(root, 1);
1296 btrfs_set_trans_block_group(trans, dir);
1297
1298 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1299 if (err) {
1300 err = -ENOSPC;
1301 goto out_unlock;
1302 }
1303
1304 inode = btrfs_new_inode(trans, root, objectid,
1305 BTRFS_I(dir)->block_group, mode);
1306 err = PTR_ERR(inode);
1307 if (IS_ERR(inode))
1308 goto out_unlock;
1309
1310 btrfs_set_trans_block_group(trans, inode);
1311 err = btrfs_add_nondir(trans, dentry, inode);
1312 if (err)
1313 drop_inode = 1;
1314 else {
1315 inode->i_op = &btrfs_special_inode_operations;
1316 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04001317 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04001318 }
1319 dir->i_sb->s_dirt = 1;
1320 btrfs_update_inode_block_group(trans, inode);
1321 btrfs_update_inode_block_group(trans, dir);
1322out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001323 nr = trans->blocks_used;
Josef Bacik618e21d2007-07-11 10:18:17 -04001324 btrfs_end_transaction(trans, root);
1325 mutex_unlock(&root->fs_info->fs_mutex);
1326
1327 if (drop_inode) {
1328 inode_dec_link_count(inode);
1329 iput(inode);
1330 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001331 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04001332 return err;
1333}
1334
Chris Mason39279cc2007-06-12 06:35:45 -04001335static int btrfs_create(struct inode *dir, struct dentry *dentry,
1336 int mode, struct nameidata *nd)
1337{
1338 struct btrfs_trans_handle *trans;
1339 struct btrfs_root *root = BTRFS_I(dir)->root;
1340 struct inode *inode;
1341 int err;
1342 int drop_inode = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001343 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001344 u64 objectid;
1345
1346 mutex_lock(&root->fs_info->fs_mutex);
1347 trans = btrfs_start_transaction(root, 1);
1348 btrfs_set_trans_block_group(trans, dir);
1349
1350 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1351 if (err) {
1352 err = -ENOSPC;
1353 goto out_unlock;
1354 }
1355
1356 inode = btrfs_new_inode(trans, root, objectid,
1357 BTRFS_I(dir)->block_group, mode);
1358 err = PTR_ERR(inode);
1359 if (IS_ERR(inode))
1360 goto out_unlock;
1361
1362 btrfs_set_trans_block_group(trans, inode);
1363 err = btrfs_add_nondir(trans, dentry, inode);
1364 if (err)
1365 drop_inode = 1;
1366 else {
1367 inode->i_mapping->a_ops = &btrfs_aops;
1368 inode->i_fop = &btrfs_file_operations;
1369 inode->i_op = &btrfs_file_inode_operations;
Chris Masona52d9a82007-08-27 16:49:44 -04001370 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1371 inode->i_mapping, GFP_NOFS);
Chris Mason07157aa2007-08-30 08:50:51 -04001372 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04001373 }
1374 dir->i_sb->s_dirt = 1;
1375 btrfs_update_inode_block_group(trans, inode);
1376 btrfs_update_inode_block_group(trans, dir);
1377out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001378 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001379 btrfs_end_transaction(trans, root);
1380 mutex_unlock(&root->fs_info->fs_mutex);
1381
1382 if (drop_inode) {
1383 inode_dec_link_count(inode);
1384 iput(inode);
1385 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001386 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001387 return err;
1388}
1389
1390static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1391 struct dentry *dentry)
1392{
1393 struct btrfs_trans_handle *trans;
1394 struct btrfs_root *root = BTRFS_I(dir)->root;
1395 struct inode *inode = old_dentry->d_inode;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001396 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001397 int err;
1398 int drop_inode = 0;
1399
1400 if (inode->i_nlink == 0)
1401 return -ENOENT;
1402
1403 inc_nlink(inode);
1404 mutex_lock(&root->fs_info->fs_mutex);
1405 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001406
Chris Mason39279cc2007-06-12 06:35:45 -04001407 btrfs_set_trans_block_group(trans, dir);
1408 atomic_inc(&inode->i_count);
1409 err = btrfs_add_nondir(trans, dentry, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001410
Chris Mason39279cc2007-06-12 06:35:45 -04001411 if (err)
1412 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001413
Chris Mason39279cc2007-06-12 06:35:45 -04001414 dir->i_sb->s_dirt = 1;
1415 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04001416 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001417
Chris Mason54aa1f42007-06-22 14:16:25 -04001418 if (err)
1419 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001420
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001421 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001422 btrfs_end_transaction(trans, root);
1423 mutex_unlock(&root->fs_info->fs_mutex);
1424
1425 if (drop_inode) {
1426 inode_dec_link_count(inode);
1427 iput(inode);
1428 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001429 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001430 return err;
1431}
1432
1433static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
1434 struct btrfs_root *root,
1435 u64 objectid, u64 dirid)
1436{
1437 int ret;
1438 char buf[2];
1439 struct btrfs_key key;
1440
1441 buf[0] = '.';
1442 buf[1] = '.';
1443
1444 key.objectid = objectid;
1445 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001446 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1447
1448 ret = btrfs_insert_dir_item(trans, root, buf, 1, objectid,
1449 &key, BTRFS_FT_DIR);
1450 if (ret)
1451 goto error;
Chris Mason5f39d392007-10-15 16:14:19 -04001452
Chris Mason39279cc2007-06-12 06:35:45 -04001453 key.objectid = dirid;
1454 ret = btrfs_insert_dir_item(trans, root, buf, 2, objectid,
1455 &key, BTRFS_FT_DIR);
1456 if (ret)
1457 goto error;
1458error:
1459 return ret;
1460}
1461
1462static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1463{
1464 struct inode *inode;
1465 struct btrfs_trans_handle *trans;
1466 struct btrfs_root *root = BTRFS_I(dir)->root;
1467 int err = 0;
1468 int drop_on_err = 0;
1469 u64 objectid;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001470 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001471
1472 mutex_lock(&root->fs_info->fs_mutex);
1473 trans = btrfs_start_transaction(root, 1);
1474 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04001475
Chris Mason39279cc2007-06-12 06:35:45 -04001476 if (IS_ERR(trans)) {
1477 err = PTR_ERR(trans);
1478 goto out_unlock;
1479 }
1480
1481 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1482 if (err) {
1483 err = -ENOSPC;
1484 goto out_unlock;
1485 }
1486
1487 inode = btrfs_new_inode(trans, root, objectid,
1488 BTRFS_I(dir)->block_group, S_IFDIR | mode);
1489 if (IS_ERR(inode)) {
1490 err = PTR_ERR(inode);
1491 goto out_fail;
1492 }
Chris Mason5f39d392007-10-15 16:14:19 -04001493
Chris Mason39279cc2007-06-12 06:35:45 -04001494 drop_on_err = 1;
1495 inode->i_op = &btrfs_dir_inode_operations;
1496 inode->i_fop = &btrfs_dir_file_operations;
1497 btrfs_set_trans_block_group(trans, inode);
1498
1499 err = btrfs_make_empty_dir(trans, root, inode->i_ino, dir->i_ino);
1500 if (err)
1501 goto out_fail;
1502
1503 inode->i_size = 6;
1504 err = btrfs_update_inode(trans, root, inode);
1505 if (err)
1506 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04001507
Chris Mason39279cc2007-06-12 06:35:45 -04001508 err = btrfs_add_link(trans, dentry, inode);
1509 if (err)
1510 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04001511
Chris Mason39279cc2007-06-12 06:35:45 -04001512 d_instantiate(dentry, inode);
1513 drop_on_err = 0;
1514 dir->i_sb->s_dirt = 1;
1515 btrfs_update_inode_block_group(trans, inode);
1516 btrfs_update_inode_block_group(trans, dir);
1517
1518out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001519 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001520 btrfs_end_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04001521
Chris Mason39279cc2007-06-12 06:35:45 -04001522out_unlock:
1523 mutex_unlock(&root->fs_info->fs_mutex);
1524 if (drop_on_err)
1525 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001526 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001527 return err;
1528}
1529
Chris Masona52d9a82007-08-27 16:49:44 -04001530struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
1531 size_t page_offset, u64 start, u64 end,
1532 int create)
1533{
1534 int ret;
1535 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04001536 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04001537 u64 extent_start = 0;
1538 u64 extent_end = 0;
1539 u64 objectid = inode->i_ino;
1540 u32 found_type;
1541 int failed_insert = 0;
1542 struct btrfs_path *path;
1543 struct btrfs_root *root = BTRFS_I(inode)->root;
1544 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04001545 struct extent_buffer *leaf;
1546 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04001547 struct extent_map *em = NULL;
1548 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1549 struct btrfs_trans_handle *trans = NULL;
1550
1551 path = btrfs_alloc_path();
1552 BUG_ON(!path);
1553 mutex_lock(&root->fs_info->fs_mutex);
1554
1555again:
1556 em = lookup_extent_mapping(em_tree, start, end);
1557 if (em) {
1558 goto out;
1559 }
1560 if (!em) {
1561 em = alloc_extent_map(GFP_NOFS);
1562 if (!em) {
1563 err = -ENOMEM;
1564 goto out;
1565 }
Chris Mason5f39d392007-10-15 16:14:19 -04001566 em->start = EXTENT_MAP_HOLE;
1567 em->end = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001568 }
1569 em->bdev = inode->i_sb->s_bdev;
Chris Mason179e29e2007-11-01 11:28:41 -04001570 ret = btrfs_lookup_file_extent(trans, root, path,
1571 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04001572 if (ret < 0) {
1573 err = ret;
1574 goto out;
1575 }
1576
1577 if (ret != 0) {
1578 if (path->slots[0] == 0)
1579 goto not_found;
1580 path->slots[0]--;
1581 }
1582
Chris Mason5f39d392007-10-15 16:14:19 -04001583 leaf = path->nodes[0];
1584 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04001585 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04001586 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04001587 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1588 found_type = btrfs_key_type(&found_key);
1589 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04001590 found_type != BTRFS_EXTENT_DATA_KEY) {
1591 goto not_found;
1592 }
1593
Chris Mason5f39d392007-10-15 16:14:19 -04001594 found_type = btrfs_file_extent_type(leaf, item);
1595 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04001596 if (found_type == BTRFS_FILE_EXTENT_REG) {
1597 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04001598 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04001599 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04001600 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04001601 em->start = start;
1602 if (start < extent_start) {
Chris Masonb888db22007-08-27 16:49:44 -04001603 if (end < extent_start)
1604 goto not_found;
Chris Masona52d9a82007-08-27 16:49:44 -04001605 em->end = extent_end - 1;
1606 } else {
1607 em->end = end;
1608 }
1609 goto not_found_em;
1610 }
Chris Masondb945352007-10-15 16:15:53 -04001611 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
1612 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04001613 em->start = extent_start;
1614 em->end = extent_end - 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001615 em->block_start = EXTENT_MAP_HOLE;
1616 em->block_end = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001617 goto insert;
1618 }
Chris Masondb945352007-10-15 16:15:53 -04001619 bytenr += btrfs_file_extent_offset(leaf, item);
1620 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04001621 em->block_end = em->block_start +
Chris Masondb945352007-10-15 16:15:53 -04001622 btrfs_file_extent_num_bytes(leaf, item) - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04001623 em->start = extent_start;
1624 em->end = extent_end - 1;
1625 goto insert;
1626 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04001627 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04001628 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04001629 size_t size;
1630 size_t extent_offset;
1631 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04001632
Chris Mason5f39d392007-10-15 16:14:19 -04001633 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
1634 path->slots[0]));
Yan689f9342007-10-29 11:41:07 -04001635 extent_end = (extent_start + size - 1) |
Chris Masondb945352007-10-15 16:15:53 -04001636 ((u64)root->sectorsize - 1);
Chris Masonb888db22007-08-27 16:49:44 -04001637 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04001638 em->start = start;
1639 if (start < extent_start) {
Chris Masonb888db22007-08-27 16:49:44 -04001640 if (end < extent_start)
1641 goto not_found;
Chris Mason50b78c22007-09-20 14:14:42 -04001642 em->end = extent_end;
Chris Masona52d9a82007-08-27 16:49:44 -04001643 } else {
1644 em->end = end;
1645 }
1646 goto not_found_em;
1647 }
1648 em->block_start = EXTENT_MAP_INLINE;
1649 em->block_end = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04001650
1651 if (!page) {
1652 em->start = extent_start;
1653 em->end = extent_start + size - 1;
1654 goto out;
1655 }
1656
Chris Mason35ebb932007-10-30 16:56:53 -04001657 extent_offset = ((u64)page->index << PAGE_CACHE_SHIFT) -
Yan689f9342007-10-29 11:41:07 -04001658 extent_start + page_offset;
1659 copy_size = min_t(u64, PAGE_CACHE_SIZE - page_offset,
1660 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04001661 em->start = extent_start + extent_offset;
1662 em->end = (em->start + copy_size -1) |
1663 ((u64)root->sectorsize -1);
Yan689f9342007-10-29 11:41:07 -04001664 map = kmap(page);
1665 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04001666 if (create == 0 && !PageUptodate(page)) {
1667 read_extent_buffer(leaf, map + page_offset, ptr,
1668 copy_size);
1669 flush_dcache_page(page);
1670 } else if (create && PageUptodate(page)) {
1671 if (!trans) {
1672 kunmap(page);
1673 free_extent_map(em);
1674 em = NULL;
1675 btrfs_release_path(root, path);
1676 trans = btrfs_start_transaction(root, 1);
1677 goto again;
1678 }
1679 write_extent_buffer(leaf, map + page_offset, ptr,
1680 copy_size);
1681 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04001682 }
Chris Masona52d9a82007-08-27 16:49:44 -04001683 kunmap(page);
Chris Mason3326d1b2007-10-15 16:18:25 -04001684 set_extent_uptodate(em_tree, em->start, em->end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04001685 goto insert;
1686 } else {
1687 printk("unkknown found_type %d\n", found_type);
1688 WARN_ON(1);
1689 }
1690not_found:
1691 em->start = start;
1692 em->end = end;
1693not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04001694 em->block_start = EXTENT_MAP_HOLE;
1695 em->block_end = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001696insert:
1697 btrfs_release_path(root, path);
1698 if (em->start > start || em->end < start) {
Chris Masonb888db22007-08-27 16:49:44 -04001699 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->end, start, end);
Chris Masona52d9a82007-08-27 16:49:44 -04001700 err = -EIO;
1701 goto out;
1702 }
1703 ret = add_extent_mapping(em_tree, em);
1704 if (ret == -EEXIST) {
1705 free_extent_map(em);
Chris Mason2bf5a722007-08-30 11:54:02 -04001706 em = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04001707 failed_insert++;
1708 if (failed_insert > 5) {
1709 printk("failing to insert %Lu %Lu\n", start, end);
1710 err = -EIO;
1711 goto out;
1712 }
Chris Masona52d9a82007-08-27 16:49:44 -04001713 goto again;
1714 }
1715 err = 0;
1716out:
1717 btrfs_free_path(path);
1718 if (trans) {
1719 ret = btrfs_end_transaction(trans, root);
1720 if (!err)
1721 err = ret;
1722 }
1723 mutex_unlock(&root->fs_info->fs_mutex);
1724 if (err) {
1725 free_extent_map(em);
1726 WARN_ON(1);
1727 return ERR_PTR(err);
1728 }
1729 return em;
1730}
1731
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04001732static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04001733{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04001734 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04001735}
1736
1737static int btrfs_prepare_write(struct file *file, struct page *page,
1738 unsigned from, unsigned to)
1739{
Chris Masona52d9a82007-08-27 16:49:44 -04001740 return extent_prepare_write(&BTRFS_I(page->mapping->host)->extent_tree,
1741 page->mapping->host, page, from, to,
1742 btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04001743}
1744
Chris Mason9ebefb182007-06-15 13:50:00 -04001745int btrfs_readpage(struct file *file, struct page *page)
1746{
Chris Masona52d9a82007-08-27 16:49:44 -04001747 struct extent_map_tree *tree;
1748 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1749 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04001750}
Chris Mason39279cc2007-06-12 06:35:45 -04001751static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1752{
Chris Masona52d9a82007-08-27 16:49:44 -04001753 struct extent_map_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04001754
1755
1756 if (current->flags & PF_MEMALLOC) {
1757 redirty_page_for_writepage(wbc, page);
1758 unlock_page(page);
1759 return 0;
1760 }
Chris Masona52d9a82007-08-27 16:49:44 -04001761 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1762 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
1763}
Chris Mason39279cc2007-06-12 06:35:45 -04001764
Chris Masonb293f022007-11-01 19:45:34 -04001765static int btrfs_writepages(struct address_space *mapping,
1766 struct writeback_control *wbc)
1767{
1768 struct extent_map_tree *tree;
1769 tree = &BTRFS_I(mapping->host)->extent_tree;
1770 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
1771}
1772
Chris Mason3ab2fb52007-11-08 10:59:22 -05001773static int
1774btrfs_readpages(struct file *file, struct address_space *mapping,
1775 struct list_head *pages, unsigned nr_pages)
1776{
1777 struct extent_map_tree *tree;
1778 tree = &BTRFS_I(mapping->host)->extent_tree;
1779 return extent_readpages(tree, mapping, pages, nr_pages,
1780 btrfs_get_extent);
1781}
1782
Chris Masona52d9a82007-08-27 16:49:44 -04001783static int btrfs_releasepage(struct page *page, gfp_t unused_gfp_flags)
1784{
1785 struct extent_map_tree *tree;
1786 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04001787
Chris Masona52d9a82007-08-27 16:49:44 -04001788 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1789 ret = try_release_extent_mapping(tree, page);
1790 if (ret == 1) {
1791 ClearPagePrivate(page);
1792 set_page_private(page, 0);
1793 page_cache_release(page);
1794 }
1795 return ret;
1796}
Chris Mason39279cc2007-06-12 06:35:45 -04001797
Chris Masona52d9a82007-08-27 16:49:44 -04001798static void btrfs_invalidatepage(struct page *page, unsigned long offset)
1799{
1800 struct extent_map_tree *tree;
1801
1802 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1803 extent_invalidatepage(tree, page, offset);
1804 btrfs_releasepage(page, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001805}
1806
Chris Mason9ebefb182007-06-15 13:50:00 -04001807/*
1808 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
1809 * called from a page fault handler when a page is first dirtied. Hence we must
1810 * be careful to check for EOF conditions here. We set the page up correctly
1811 * for a written page which means we get ENOSPC checking when writing into
1812 * holes and correct delalloc and unwritten extent mapping on filesystems that
1813 * support these features.
1814 *
1815 * We are not allowed to take the i_mutex here so we have to play games to
1816 * protect against truncate races as the page could now be beyond EOF. Because
1817 * vmtruncate() writes the inode size before removing pages, once we have the
1818 * page lock we can determine safely if the page is beyond EOF. If it is not
1819 * beyond EOF, then the page is guaranteed safe against truncation until we
1820 * unlock the page.
1821 */
1822int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
1823{
1824 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1825 unsigned long end;
1826 loff_t size;
1827 int ret = -EINVAL;
Chris Masona52d9a82007-08-27 16:49:44 -04001828 u64 page_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04001829
Chris Mason011410b2007-09-10 19:58:36 -04001830 down_read(&BTRFS_I(inode)->root->snap_sem);
Chris Mason9ebefb182007-06-15 13:50:00 -04001831 lock_page(page);
1832 wait_on_page_writeback(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04001833 size = i_size_read(inode);
Chris Mason35ebb932007-10-30 16:56:53 -04001834 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04001835
Chris Mason9ebefb182007-06-15 13:50:00 -04001836 if ((page->mapping != inode->i_mapping) ||
Chris Masona52d9a82007-08-27 16:49:44 -04001837 (page_start > size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04001838 /* page got truncated out from underneath us */
1839 goto out_unlock;
1840 }
1841
1842 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04001843 if (page_start + PAGE_CACHE_SIZE > size)
Chris Mason9ebefb182007-06-15 13:50:00 -04001844 end = size & ~PAGE_CACHE_MASK;
1845 else
1846 end = PAGE_CACHE_SIZE;
1847
Chris Masonb888db22007-08-27 16:49:44 -04001848 ret = btrfs_cow_one_page(inode, page, end);
Chris Mason9ebefb182007-06-15 13:50:00 -04001849
1850out_unlock:
Chris Mason011410b2007-09-10 19:58:36 -04001851 up_read(&BTRFS_I(inode)->root->snap_sem);
Chris Mason9ebefb182007-06-15 13:50:00 -04001852 unlock_page(page);
1853 return ret;
1854}
1855
Chris Mason39279cc2007-06-12 06:35:45 -04001856static void btrfs_truncate(struct inode *inode)
1857{
1858 struct btrfs_root *root = BTRFS_I(inode)->root;
1859 int ret;
1860 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001861 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001862
1863 if (!S_ISREG(inode->i_mode))
1864 return;
1865 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1866 return;
1867
1868 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1869
1870 mutex_lock(&root->fs_info->fs_mutex);
1871 trans = btrfs_start_transaction(root, 1);
1872 btrfs_set_trans_block_group(trans, inode);
1873
1874 /* FIXME, add redo link to tree so we don't leak on crash */
1875 ret = btrfs_truncate_in_trans(trans, root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001876 btrfs_update_inode(trans, root, inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001877 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04001878
Chris Mason39279cc2007-06-12 06:35:45 -04001879 ret = btrfs_end_transaction(trans, root);
1880 BUG_ON(ret);
1881 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001882 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001883}
1884
1885int btrfs_commit_write(struct file *file, struct page *page,
1886 unsigned from, unsigned to)
1887{
Chris Masona52d9a82007-08-27 16:49:44 -04001888 return extent_commit_write(&BTRFS_I(page->mapping->host)->extent_tree,
1889 page->mapping->host, page, from, to);
Chris Mason39279cc2007-06-12 06:35:45 -04001890}
1891
1892static int create_subvol(struct btrfs_root *root, char *name, int namelen)
1893{
1894 struct btrfs_trans_handle *trans;
1895 struct btrfs_key key;
1896 struct btrfs_root_item root_item;
1897 struct btrfs_inode_item *inode_item;
Chris Mason5f39d392007-10-15 16:14:19 -04001898 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001899 struct btrfs_root *new_root;
1900 struct inode *inode;
1901 struct inode *dir;
1902 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001903 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04001904 u64 objectid;
1905 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001906 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001907
1908 mutex_lock(&root->fs_info->fs_mutex);
1909 trans = btrfs_start_transaction(root, 1);
1910 BUG_ON(!trans);
1911
Chris Mason7bb86312007-12-11 09:25:06 -05001912 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
1913 0, &objectid);
1914 if (ret)
1915 goto fail;
1916
1917 leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
1918 objectid, trans->transid, 0, 0,
1919 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001920 if (IS_ERR(leaf))
1921 return PTR_ERR(leaf);
1922
1923 btrfs_set_header_nritems(leaf, 0);
1924 btrfs_set_header_level(leaf, 0);
Chris Masondb945352007-10-15 16:15:53 -04001925 btrfs_set_header_bytenr(leaf, leaf->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001926 btrfs_set_header_generation(leaf, trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05001927 btrfs_set_header_owner(leaf, objectid);
1928
Chris Mason5f39d392007-10-15 16:14:19 -04001929 write_extent_buffer(leaf, root->fs_info->fsid,
1930 (unsigned long)btrfs_header_fsid(leaf),
1931 BTRFS_FSID_SIZE);
1932 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001933
1934 inode_item = &root_item.inode;
1935 memset(inode_item, 0, sizeof(*inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04001936 inode_item->generation = cpu_to_le64(1);
1937 inode_item->size = cpu_to_le64(3);
1938 inode_item->nlink = cpu_to_le32(1);
1939 inode_item->nblocks = cpu_to_le64(1);
1940 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
Chris Mason39279cc2007-06-12 06:35:45 -04001941
Chris Masondb945352007-10-15 16:15:53 -04001942 btrfs_set_root_bytenr(&root_item, leaf->start);
1943 btrfs_set_root_level(&root_item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001944 btrfs_set_root_refs(&root_item, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001945 btrfs_set_root_used(&root_item, 0);
1946
Chris Mason5eda7b52007-06-22 14:16:25 -04001947 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
1948 root_item.drop_level = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001949
1950 free_extent_buffer(leaf);
1951 leaf = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001952
Chris Mason39279cc2007-06-12 06:35:45 -04001953 btrfs_set_root_dirid(&root_item, new_dirid);
1954
1955 key.objectid = objectid;
1956 key.offset = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001957 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
1958 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
1959 &root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -04001960 if (ret)
1961 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001962
1963 /*
1964 * insert the directory item
1965 */
1966 key.offset = (u64)-1;
1967 dir = root->fs_info->sb->s_root->d_inode;
1968 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
1969 name, namelen, dir->i_ino, &key,
1970 BTRFS_FT_DIR);
Chris Mason54aa1f42007-06-22 14:16:25 -04001971 if (ret)
1972 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001973
1974 ret = btrfs_commit_transaction(trans, root);
Chris Mason54aa1f42007-06-22 14:16:25 -04001975 if (ret)
1976 goto fail_commit;
Chris Mason39279cc2007-06-12 06:35:45 -04001977
Josef Bacik58176a92007-08-29 15:47:34 -04001978 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
Chris Mason39279cc2007-06-12 06:35:45 -04001979 BUG_ON(!new_root);
1980
1981 trans = btrfs_start_transaction(new_root, 1);
1982 BUG_ON(!trans);
1983
1984 inode = btrfs_new_inode(trans, new_root, new_dirid,
1985 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
Chris Mason54aa1f42007-06-22 14:16:25 -04001986 if (IS_ERR(inode))
1987 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001988 inode->i_op = &btrfs_dir_inode_operations;
1989 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04001990 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001991
1992 ret = btrfs_make_empty_dir(trans, new_root, new_dirid, new_dirid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001993 if (ret)
1994 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001995
1996 inode->i_nlink = 1;
1997 inode->i_size = 6;
1998 ret = btrfs_update_inode(trans, new_root, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04001999 if (ret)
2000 goto fail;
2001fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002002 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04002003 err = btrfs_commit_transaction(trans, root);
2004 if (err && !ret)
2005 ret = err;
2006fail_commit:
Chris Mason39279cc2007-06-12 06:35:45 -04002007 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002008 btrfs_btree_balance_dirty(root, nr);
Chris Mason54aa1f42007-06-22 14:16:25 -04002009 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002010}
2011
2012static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2013{
2014 struct btrfs_trans_handle *trans;
2015 struct btrfs_key key;
2016 struct btrfs_root_item new_root_item;
Chris Mason5f39d392007-10-15 16:14:19 -04002017 struct extent_buffer *tmp;
Chris Mason39279cc2007-06-12 06:35:45 -04002018 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002019 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04002020 u64 objectid;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002021 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002022
2023 if (!root->ref_cows)
2024 return -EINVAL;
2025
Chris Mason011410b2007-09-10 19:58:36 -04002026 down_write(&root->snap_sem);
2027 freeze_bdev(root->fs_info->sb->s_bdev);
2028 thaw_bdev(root->fs_info->sb->s_bdev, root->fs_info->sb);
2029
Chris Mason39279cc2007-06-12 06:35:45 -04002030 mutex_lock(&root->fs_info->fs_mutex);
2031 trans = btrfs_start_transaction(root, 1);
2032 BUG_ON(!trans);
2033
2034 ret = btrfs_update_inode(trans, root, root->inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002035 if (ret)
2036 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002037
2038 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2039 0, &objectid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002040 if (ret)
2041 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002042
2043 memcpy(&new_root_item, &root->root_item,
2044 sizeof(new_root_item));
2045
2046 key.objectid = objectid;
2047 key.offset = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002048 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
Yan96919752007-12-04 13:20:20 -05002049 extent_buffer_get(root->node);
Chris Mason83df7c12007-08-27 16:49:44 -04002050 btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp);
Yan96919752007-12-04 13:20:20 -05002051 free_extent_buffer(tmp);
Chris Masondb945352007-10-15 16:15:53 -04002052 btrfs_set_root_bytenr(&new_root_item, root->node->start);
2053 btrfs_set_root_level(&new_root_item, btrfs_header_level(root->node));
Chris Mason39279cc2007-06-12 06:35:45 -04002054 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2055 &new_root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -04002056 if (ret)
2057 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002058
2059 /*
2060 * insert the directory item
2061 */
2062 key.offset = (u64)-1;
2063 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2064 name, namelen,
2065 root->fs_info->sb->s_root->d_inode->i_ino,
2066 &key, BTRFS_FT_DIR);
2067
Chris Mason54aa1f42007-06-22 14:16:25 -04002068 if (ret)
2069 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002070
Chris Mason7bb86312007-12-11 09:25:06 -05002071 ret = btrfs_inc_root_ref(trans, root, objectid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002072 if (ret)
2073 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002074fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002075 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04002076 err = btrfs_commit_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002077
Chris Mason54aa1f42007-06-22 14:16:25 -04002078 if (err && !ret)
2079 ret = err;
Chris Mason5f39d392007-10-15 16:14:19 -04002080
Chris Mason39279cc2007-06-12 06:35:45 -04002081 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason011410b2007-09-10 19:58:36 -04002082 up_write(&root->snap_sem);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002083 btrfs_btree_balance_dirty(root, nr);
Chris Mason54aa1f42007-06-22 14:16:25 -04002084 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002085}
2086
Chris Mason86479a02007-09-10 19:58:16 -04002087static unsigned long force_ra(struct address_space *mapping,
2088 struct file_ra_state *ra, struct file *file,
2089 pgoff_t offset, pgoff_t last_index)
2090{
2091 pgoff_t req_size;
2092
2093#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2094 req_size = last_index - offset + 1;
2095 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2096 return offset;
2097#else
2098 req_size = min(last_index - offset + 1, (pgoff_t)128);
2099 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2100 return offset + req_size;
2101#endif
2102}
2103
2104int btrfs_defrag_file(struct file *file) {
2105 struct inode *inode = file->f_path.dentry->d_inode;
2106 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2107 struct page *page;
2108 unsigned long last_index;
2109 unsigned long ra_index = 0;
2110 u64 page_start;
2111 u64 page_end;
2112 unsigned long i;
2113
2114 mutex_lock(&inode->i_mutex);
2115 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2116 for (i = 0; i <= last_index; i++) {
2117 if (i == ra_index) {
2118 ra_index = force_ra(inode->i_mapping, &file->f_ra,
2119 file, ra_index, last_index);
2120 }
2121 page = grab_cache_page(inode->i_mapping, i);
2122 if (!page)
2123 goto out_unlock;
2124 if (!PageUptodate(page)) {
2125 btrfs_readpage(NULL, page);
2126 lock_page(page);
2127 if (!PageUptodate(page)) {
2128 unlock_page(page);
2129 page_cache_release(page);
2130 goto out_unlock;
2131 }
2132 }
Chris Mason35ebb932007-10-30 16:56:53 -04002133 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason86479a02007-09-10 19:58:16 -04002134 page_end = page_start + PAGE_CACHE_SIZE - 1;
2135
2136 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
2137 set_extent_delalloc(em_tree, page_start,
2138 page_end, GFP_NOFS);
2139 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
2140 set_page_dirty(page);
2141 unlock_page(page);
2142 page_cache_release(page);
2143 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2144 }
2145
2146out_unlock:
2147 mutex_unlock(&inode->i_mutex);
2148 return 0;
2149}
2150
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002151static int btrfs_ioctl_snap_create(struct btrfs_root *root, void __user *arg)
2152{
2153 struct btrfs_ioctl_vol_args vol_args;
2154 struct btrfs_dir_item *di;
2155 struct btrfs_path *path;
2156 int namelen;
2157 u64 root_dirid;
2158
2159 if (copy_from_user(&vol_args, arg, sizeof(vol_args)))
2160 return -EFAULT;
Chris Mason5f39d392007-10-15 16:14:19 -04002161
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002162 namelen = strlen(vol_args.name);
2163 if (namelen > BTRFS_VOL_NAME_MAX)
2164 return -EINVAL;
2165 if (strchr(vol_args.name, '/'))
2166 return -EINVAL;
2167
2168 path = btrfs_alloc_path();
2169 if (!path)
2170 return -ENOMEM;
2171
2172 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2173 mutex_lock(&root->fs_info->fs_mutex);
2174 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2175 path, root_dirid,
2176 vol_args.name, namelen, 0);
2177 mutex_unlock(&root->fs_info->fs_mutex);
2178 btrfs_free_path(path);
2179 if (di && !IS_ERR(di))
2180 return -EEXIST;
2181 if (IS_ERR(di))
2182 return PTR_ERR(di);
2183
2184 if (root == root->fs_info->tree_root)
2185 return create_subvol(root, vol_args.name, namelen);
2186 return create_snapshot(root, vol_args.name, namelen);
2187}
2188
2189static int btrfs_ioctl_defrag(struct file *file)
Chris Mason39279cc2007-06-12 06:35:45 -04002190{
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002191 struct inode *inode = file->f_path.dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002192 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002193
2194 switch (inode->i_mode & S_IFMT) {
2195 case S_IFDIR:
2196 mutex_lock(&root->fs_info->fs_mutex);
2197 btrfs_defrag_root(root, 0);
2198 btrfs_defrag_root(root->fs_info->extent_root, 0);
2199 mutex_unlock(&root->fs_info->fs_mutex);
2200 break;
2201 case S_IFREG:
2202 btrfs_defrag_file(file);
2203 break;
2204 }
2205
2206 return 0;
2207}
2208
2209long btrfs_ioctl(struct file *file, unsigned int
2210 cmd, unsigned long arg)
2211{
2212 struct btrfs_root *root = BTRFS_I(file->f_path.dentry->d_inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002213
2214 switch (cmd) {
2215 case BTRFS_IOC_SNAP_CREATE:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002216 return btrfs_ioctl_snap_create(root, (void __user *)arg);
Chris Mason6702ed42007-08-07 16:15:09 -04002217 case BTRFS_IOC_DEFRAG:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002218 return btrfs_ioctl_defrag(file);
Chris Mason39279cc2007-06-12 06:35:45 -04002219 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002220
2221 return -ENOTTY;
Chris Mason39279cc2007-06-12 06:35:45 -04002222}
2223
Chris Mason39279cc2007-06-12 06:35:45 -04002224/*
2225 * Called inside transaction, so use GFP_NOFS
2226 */
2227struct inode *btrfs_alloc_inode(struct super_block *sb)
2228{
2229 struct btrfs_inode *ei;
2230
2231 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2232 if (!ei)
2233 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002234 ei->last_trans = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002235 return &ei->vfs_inode;
2236}
2237
2238void btrfs_destroy_inode(struct inode *inode)
2239{
2240 WARN_ON(!list_empty(&inode->i_dentry));
2241 WARN_ON(inode->i_data.nrpages);
2242
2243 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2244}
2245
Chris Mason44ec0b72007-10-29 10:55:05 -04002246#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2247static void init_once(struct kmem_cache * cachep, void *foo)
2248#else
Chris Mason39279cc2007-06-12 06:35:45 -04002249static void init_once(void * foo, struct kmem_cache * cachep,
2250 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04002251#endif
Chris Mason39279cc2007-06-12 06:35:45 -04002252{
2253 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2254
2255 inode_init_once(&ei->vfs_inode);
2256}
2257
2258void btrfs_destroy_cachep(void)
2259{
2260 if (btrfs_inode_cachep)
2261 kmem_cache_destroy(btrfs_inode_cachep);
2262 if (btrfs_trans_handle_cachep)
2263 kmem_cache_destroy(btrfs_trans_handle_cachep);
2264 if (btrfs_transaction_cachep)
2265 kmem_cache_destroy(btrfs_transaction_cachep);
2266 if (btrfs_bit_radix_cachep)
2267 kmem_cache_destroy(btrfs_bit_radix_cachep);
2268 if (btrfs_path_cachep)
2269 kmem_cache_destroy(btrfs_path_cachep);
2270}
2271
Chris Mason86479a02007-09-10 19:58:16 -04002272struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04002273 unsigned long extra_flags,
Chris Mason44ec0b72007-10-29 10:55:05 -04002274#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2275 void (*ctor)(struct kmem_cache *, void *)
2276#else
Chris Mason92fee662007-07-25 12:31:35 -04002277 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04002278 unsigned long)
2279#endif
2280 )
Chris Mason92fee662007-07-25 12:31:35 -04002281{
2282 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2283 SLAB_MEM_SPREAD | extra_flags), ctor
2284#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2285 ,NULL
2286#endif
2287 );
2288}
2289
Chris Mason39279cc2007-06-12 06:35:45 -04002290int btrfs_init_cachep(void)
2291{
Chris Mason86479a02007-09-10 19:58:16 -04002292 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04002293 sizeof(struct btrfs_inode),
2294 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04002295 if (!btrfs_inode_cachep)
2296 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002297 btrfs_trans_handle_cachep =
2298 btrfs_cache_create("btrfs_trans_handle_cache",
2299 sizeof(struct btrfs_trans_handle),
2300 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002301 if (!btrfs_trans_handle_cachep)
2302 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002303 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04002304 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04002305 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002306 if (!btrfs_transaction_cachep)
2307 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002308 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04002309 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04002310 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002311 if (!btrfs_path_cachep)
2312 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002313 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04002314 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002315 if (!btrfs_bit_radix_cachep)
2316 goto fail;
2317 return 0;
2318fail:
2319 btrfs_destroy_cachep();
2320 return -ENOMEM;
2321}
2322
2323static int btrfs_getattr(struct vfsmount *mnt,
2324 struct dentry *dentry, struct kstat *stat)
2325{
2326 struct inode *inode = dentry->d_inode;
2327 generic_fillattr(inode, stat);
2328 stat->blksize = 256 * 1024;
2329 return 0;
2330}
2331
2332static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2333 struct inode * new_dir,struct dentry *new_dentry)
2334{
2335 struct btrfs_trans_handle *trans;
2336 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2337 struct inode *new_inode = new_dentry->d_inode;
2338 struct inode *old_inode = old_dentry->d_inode;
2339 struct timespec ctime = CURRENT_TIME;
2340 struct btrfs_path *path;
2341 struct btrfs_dir_item *di;
2342 int ret;
2343
2344 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2345 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2346 return -ENOTEMPTY;
2347 }
Chris Mason5f39d392007-10-15 16:14:19 -04002348
Chris Mason39279cc2007-06-12 06:35:45 -04002349 mutex_lock(&root->fs_info->fs_mutex);
2350 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002351
Chris Mason39279cc2007-06-12 06:35:45 -04002352 btrfs_set_trans_block_group(trans, new_dir);
2353 path = btrfs_alloc_path();
2354 if (!path) {
2355 ret = -ENOMEM;
2356 goto out_fail;
2357 }
2358
2359 old_dentry->d_inode->i_nlink++;
2360 old_dir->i_ctime = old_dir->i_mtime = ctime;
2361 new_dir->i_ctime = new_dir->i_mtime = ctime;
2362 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04002363
Chris Mason39279cc2007-06-12 06:35:45 -04002364 if (S_ISDIR(old_inode->i_mode) && old_dir != new_dir) {
2365 struct btrfs_key *location = &BTRFS_I(new_dir)->location;
Chris Mason5f39d392007-10-15 16:14:19 -04002366 struct btrfs_key old_parent_key;
Chris Mason39279cc2007-06-12 06:35:45 -04002367 di = btrfs_lookup_dir_item(trans, root, path, old_inode->i_ino,
2368 "..", 2, -1);
2369 if (IS_ERR(di)) {
2370 ret = PTR_ERR(di);
2371 goto out_fail;
2372 }
2373 if (!di) {
2374 ret = -ENOENT;
2375 goto out_fail;
2376 }
Chris Mason5f39d392007-10-15 16:14:19 -04002377 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &old_parent_key);
Chris Mason39279cc2007-06-12 06:35:45 -04002378 ret = btrfs_del_item(trans, root, path);
2379 if (ret) {
Chris Mason39279cc2007-06-12 06:35:45 -04002380 goto out_fail;
2381 }
2382 btrfs_release_path(root, path);
2383
2384 di = btrfs_lookup_dir_index_item(trans, root, path,
2385 old_inode->i_ino,
Chris Mason5f39d392007-10-15 16:14:19 -04002386 old_parent_key.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002387 "..", 2, -1);
2388 if (IS_ERR(di)) {
2389 ret = PTR_ERR(di);
2390 goto out_fail;
2391 }
2392 if (!di) {
2393 ret = -ENOENT;
2394 goto out_fail;
2395 }
2396 ret = btrfs_del_item(trans, root, path);
2397 if (ret) {
Chris Mason39279cc2007-06-12 06:35:45 -04002398 goto out_fail;
2399 }
2400 btrfs_release_path(root, path);
2401
2402 ret = btrfs_insert_dir_item(trans, root, "..", 2,
2403 old_inode->i_ino, location,
2404 BTRFS_FT_DIR);
2405 if (ret)
2406 goto out_fail;
2407 }
2408
2409
2410 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2411 if (ret)
2412 goto out_fail;
2413
2414 if (new_inode) {
2415 new_inode->i_ctime = CURRENT_TIME;
2416 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2417 if (ret)
2418 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002419 }
2420 ret = btrfs_add_link(trans, new_dentry, old_inode);
2421 if (ret)
2422 goto out_fail;
2423
2424out_fail:
2425 btrfs_free_path(path);
2426 btrfs_end_transaction(trans, root);
2427 mutex_unlock(&root->fs_info->fs_mutex);
2428 return ret;
2429}
2430
2431static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2432 const char *symname)
2433{
2434 struct btrfs_trans_handle *trans;
2435 struct btrfs_root *root = BTRFS_I(dir)->root;
2436 struct btrfs_path *path;
2437 struct btrfs_key key;
2438 struct inode *inode;
2439 int err;
2440 int drop_inode = 0;
2441 u64 objectid;
2442 int name_len;
2443 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04002444 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04002445 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04002446 struct extent_buffer *leaf;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002447 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002448
2449 name_len = strlen(symname) + 1;
2450 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2451 return -ENAMETOOLONG;
2452 mutex_lock(&root->fs_info->fs_mutex);
2453 trans = btrfs_start_transaction(root, 1);
2454 btrfs_set_trans_block_group(trans, dir);
2455
2456 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2457 if (err) {
2458 err = -ENOSPC;
2459 goto out_unlock;
2460 }
2461
2462 inode = btrfs_new_inode(trans, root, objectid,
2463 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2464 err = PTR_ERR(inode);
2465 if (IS_ERR(inode))
2466 goto out_unlock;
2467
2468 btrfs_set_trans_block_group(trans, inode);
2469 err = btrfs_add_nondir(trans, dentry, inode);
2470 if (err)
2471 drop_inode = 1;
2472 else {
2473 inode->i_mapping->a_ops = &btrfs_aops;
2474 inode->i_fop = &btrfs_file_operations;
2475 inode->i_op = &btrfs_file_inode_operations;
Chris Masona52d9a82007-08-27 16:49:44 -04002476 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
2477 inode->i_mapping, GFP_NOFS);
Chris Mason07157aa2007-08-30 08:50:51 -04002478 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002479 }
2480 dir->i_sb->s_dirt = 1;
2481 btrfs_update_inode_block_group(trans, inode);
2482 btrfs_update_inode_block_group(trans, dir);
2483 if (drop_inode)
2484 goto out_unlock;
2485
2486 path = btrfs_alloc_path();
2487 BUG_ON(!path);
2488 key.objectid = inode->i_ino;
2489 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002490 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2491 datasize = btrfs_file_extent_calc_inline_size(name_len);
2492 err = btrfs_insert_empty_item(trans, root, path, &key,
2493 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04002494 if (err) {
2495 drop_inode = 1;
2496 goto out_unlock;
2497 }
Chris Mason5f39d392007-10-15 16:14:19 -04002498 leaf = path->nodes[0];
2499 ei = btrfs_item_ptr(leaf, path->slots[0],
2500 struct btrfs_file_extent_item);
2501 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
2502 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04002503 BTRFS_FILE_EXTENT_INLINE);
2504 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04002505 write_extent_buffer(leaf, symname, ptr, name_len);
2506 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002507 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04002508
Chris Mason39279cc2007-06-12 06:35:45 -04002509 inode->i_op = &btrfs_symlink_inode_operations;
2510 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2511 inode->i_size = name_len - 1;
Chris Mason54aa1f42007-06-22 14:16:25 -04002512 err = btrfs_update_inode(trans, root, inode);
2513 if (err)
2514 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002515
2516out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002517 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04002518 btrfs_end_transaction(trans, root);
2519 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04002520 if (drop_inode) {
2521 inode_dec_link_count(inode);
2522 iput(inode);
2523 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002524 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002525 return err;
2526}
2527
2528static struct inode_operations btrfs_dir_inode_operations = {
2529 .lookup = btrfs_lookup,
2530 .create = btrfs_create,
2531 .unlink = btrfs_unlink,
2532 .link = btrfs_link,
2533 .mkdir = btrfs_mkdir,
2534 .rmdir = btrfs_rmdir,
2535 .rename = btrfs_rename,
2536 .symlink = btrfs_symlink,
2537 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04002538 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -05002539 .setxattr = generic_setxattr,
2540 .getxattr = generic_getxattr,
2541 .listxattr = btrfs_listxattr,
2542 .removexattr = generic_removexattr,
Chris Mason39279cc2007-06-12 06:35:45 -04002543};
2544
2545static struct inode_operations btrfs_dir_ro_inode_operations = {
2546 .lookup = btrfs_lookup,
2547};
2548
2549static struct file_operations btrfs_dir_file_operations = {
2550 .llseek = generic_file_llseek,
2551 .read = generic_read_dir,
2552 .readdir = btrfs_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002553 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002554#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002555 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002556#endif
2557};
2558
Chris Mason07157aa2007-08-30 08:50:51 -04002559static struct extent_map_ops btrfs_extent_map_ops = {
2560 .fill_delalloc = run_delalloc_range,
2561 .writepage_io_hook = btrfs_writepage_io_hook,
2562 .readpage_io_hook = btrfs_readpage_io_hook,
2563 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
2564};
2565
Chris Mason39279cc2007-06-12 06:35:45 -04002566static struct address_space_operations btrfs_aops = {
2567 .readpage = btrfs_readpage,
2568 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04002569 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05002570 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04002571 .sync_page = block_sync_page,
2572 .prepare_write = btrfs_prepare_write,
2573 .commit_write = btrfs_commit_write,
2574 .bmap = btrfs_bmap,
Chris Masona52d9a82007-08-27 16:49:44 -04002575 .invalidatepage = btrfs_invalidatepage,
2576 .releasepage = btrfs_releasepage,
2577 .set_page_dirty = __set_page_dirty_nobuffers,
Chris Mason39279cc2007-06-12 06:35:45 -04002578};
2579
2580static struct address_space_operations btrfs_symlink_aops = {
2581 .readpage = btrfs_readpage,
2582 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04002583 .invalidatepage = btrfs_invalidatepage,
2584 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04002585};
2586
2587static struct inode_operations btrfs_file_inode_operations = {
2588 .truncate = btrfs_truncate,
2589 .getattr = btrfs_getattr,
2590 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -05002591 .setxattr = generic_setxattr,
2592 .getxattr = generic_getxattr,
2593 .listxattr = btrfs_listxattr,
2594 .removexattr = generic_removexattr,
Chris Mason39279cc2007-06-12 06:35:45 -04002595};
2596
Josef Bacik618e21d2007-07-11 10:18:17 -04002597static struct inode_operations btrfs_special_inode_operations = {
2598 .getattr = btrfs_getattr,
2599 .setattr = btrfs_setattr,
2600};
2601
Chris Mason39279cc2007-06-12 06:35:45 -04002602static struct inode_operations btrfs_symlink_inode_operations = {
2603 .readlink = generic_readlink,
2604 .follow_link = page_follow_link_light,
2605 .put_link = page_put_link,
2606};