blob: cefe740b6c7933e36196893632fcb07a0c434315 [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;
Chris Mason39544012007-12-12 14:38:19 -0500407 if (!S_ISLNK(dentry->d_inode->i_mode)) {
408 ret = btrfs_del_inode_ref(trans, root, name, name_len,
409 dentry->d_inode->i_ino,
410 dentry->d_parent->d_inode->i_ino);
411 if (ret) {
412 printk("failed to delete reference to %.*s, "
413 "inode %lu parent %lu\n", name_len, name,
414 dentry->d_inode->i_ino,
415 dentry->d_parent->d_inode->i_ino);
416 }
417 }
Chris Mason39279cc2007-06-12 06:35:45 -0400418err:
419 btrfs_free_path(path);
420 if (!ret) {
421 dir->i_size -= name_len * 2;
Chris Mason79c44582007-06-25 10:09:33 -0400422 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -0400423 btrfs_update_inode(trans, root, dir);
424 drop_nlink(dentry->d_inode);
Chris Mason54aa1f42007-06-22 14:16:25 -0400425 ret = btrfs_update_inode(trans, root, dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400426 dir->i_sb->s_dirt = 1;
427 }
428 return ret;
429}
430
431static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
432{
433 struct btrfs_root *root;
434 struct btrfs_trans_handle *trans;
435 int ret;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400436 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -0400437
438 root = BTRFS_I(dir)->root;
439 mutex_lock(&root->fs_info->fs_mutex);
440 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400441
Chris Mason39279cc2007-06-12 06:35:45 -0400442 btrfs_set_trans_block_group(trans, dir);
443 ret = btrfs_unlink_trans(trans, root, dir, dentry);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400444 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -0400445
Chris Mason39279cc2007-06-12 06:35:45 -0400446 btrfs_end_transaction(trans, root);
447 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400448 btrfs_btree_balance_dirty(root, nr);
Chris Mason5f39d392007-10-15 16:14:19 -0400449
Chris Mason39279cc2007-06-12 06:35:45 -0400450 return ret;
451}
452
453static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
454{
455 struct inode *inode = dentry->d_inode;
456 int err;
457 int ret;
458 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -0400459 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400460 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -0400461
Yan134d4512007-10-25 15:49:25 -0400462 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
463 return -ENOTEMPTY;
464
Chris Mason39279cc2007-06-12 06:35:45 -0400465 mutex_lock(&root->fs_info->fs_mutex);
466 trans = btrfs_start_transaction(root, 1);
467 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -0400468
469 /* now the directory is empty */
470 err = btrfs_unlink_trans(trans, root, dir, dentry);
471 if (!err) {
472 inode->i_size = 0;
473 }
Chris Mason39544012007-12-12 14:38:19 -0500474
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400475 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -0400476 ret = btrfs_end_transaction(trans, root);
Yan134d4512007-10-25 15:49:25 -0400477 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400478 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -0500479
Chris Mason39279cc2007-06-12 06:35:45 -0400480 if (ret && !err)
481 err = ret;
482 return err;
483}
484
485static int btrfs_free_inode(struct btrfs_trans_handle *trans,
486 struct btrfs_root *root,
487 struct inode *inode)
488{
489 struct btrfs_path *path;
490 int ret;
491
492 clear_inode(inode);
493
494 path = btrfs_alloc_path();
495 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400496 ret = btrfs_lookup_inode(trans, root, path,
497 &BTRFS_I(inode)->location, -1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400498 if (ret > 0)
499 ret = -ENOENT;
500 if (!ret)
501 ret = btrfs_del_item(trans, root, path);
Chris Mason39279cc2007-06-12 06:35:45 -0400502 btrfs_free_path(path);
503 return ret;
504}
505
506/*
Chris Mason39279cc2007-06-12 06:35:45 -0400507 * this can truncate away extent items, csum items and directory items.
508 * It starts at a high offset and removes keys until it can't find
509 * any higher than i_size.
510 *
511 * csum items that cross the new i_size are truncated to the new size
512 * as well.
513 */
514static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
515 struct btrfs_root *root,
516 struct inode *inode)
517{
518 int ret;
519 struct btrfs_path *path;
520 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400521 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -0400522 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -0400523 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400524 struct btrfs_file_extent_item *fi;
525 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -0400526 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400527 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500528 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -0500529 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400530 int found_extent;
531 int del_item;
Chris Mason179e29e2007-11-01 11:28:41 -0400532 int extent_type = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400533
Chris Masona52d9a82007-08-27 16:49:44 -0400534 btrfs_drop_extent_cache(inode, inode->i_size, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -0400535 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -0400536 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400537 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -0400538
Chris Mason39279cc2007-06-12 06:35:45 -0400539 /* FIXME, add redo link to tree so we don't leak on crash */
540 key.objectid = inode->i_ino;
541 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -0400542 key.type = (u8)-1;
543
Chris Mason39279cc2007-06-12 06:35:45 -0400544 while(1) {
545 btrfs_init_path(path);
546 fi = NULL;
547 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
548 if (ret < 0) {
549 goto error;
550 }
551 if (ret > 0) {
552 BUG_ON(path->slots[0] == 0);
553 path->slots[0]--;
554 }
Chris Mason5f39d392007-10-15 16:14:19 -0400555 leaf = path->nodes[0];
556 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
557 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -0400558
Chris Mason5f39d392007-10-15 16:14:19 -0400559 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -0400560 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400561
Chris Mason39279cc2007-06-12 06:35:45 -0400562 if (found_type != BTRFS_CSUM_ITEM_KEY &&
563 found_type != BTRFS_DIR_ITEM_KEY &&
564 found_type != BTRFS_DIR_INDEX_KEY &&
565 found_type != BTRFS_EXTENT_DATA_KEY)
566 break;
567
Chris Mason5f39d392007-10-15 16:14:19 -0400568 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -0400569 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -0400570 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400571 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -0400572 extent_type = btrfs_file_extent_type(leaf, fi);
573 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -0400574 item_end +=
Chris Masondb945352007-10-15 16:15:53 -0400575 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -0400576 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
577 struct btrfs_item *item = btrfs_item_nr(leaf,
578 path->slots[0]);
579 item_end += btrfs_file_extent_inline_len(leaf,
580 item);
Chris Mason39279cc2007-06-12 06:35:45 -0400581 }
Yan008630c2007-11-07 13:31:09 -0500582 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -0400583 }
584 if (found_type == BTRFS_CSUM_ITEM_KEY) {
585 ret = btrfs_csum_truncate(trans, root, path,
586 inode->i_size);
587 BUG_ON(ret);
588 }
Yan008630c2007-11-07 13:31:09 -0500589 if (item_end < inode->i_size) {
Chris Masonb888db22007-08-27 16:49:44 -0400590 if (found_type == BTRFS_DIR_ITEM_KEY) {
591 found_type = BTRFS_INODE_ITEM_KEY;
592 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
593 found_type = BTRFS_CSUM_ITEM_KEY;
594 } else if (found_type) {
595 found_type--;
596 } else {
597 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400598 }
Yana61721d2007-09-17 11:08:38 -0400599 btrfs_set_key_type(&key, found_type);
Yan65555a02007-10-25 15:42:57 -0400600 btrfs_release_path(root, path);
Chris Masonb888db22007-08-27 16:49:44 -0400601 continue;
Chris Mason39279cc2007-06-12 06:35:45 -0400602 }
Chris Mason5f39d392007-10-15 16:14:19 -0400603 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -0400604 del_item = 1;
605 else
606 del_item = 0;
607 found_extent = 0;
608
609 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -0400610 if (found_type != BTRFS_EXTENT_DATA_KEY)
611 goto delete;
612
613 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -0400614 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -0400615 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400616 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -0400617 u64 orig_num_bytes =
618 btrfs_file_extent_num_bytes(leaf, fi);
619 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -0400620 found_key.offset + root->sectorsize - 1;
Chris Masondb945352007-10-15 16:15:53 -0400621 btrfs_set_file_extent_num_bytes(leaf, fi,
622 extent_num_bytes);
623 num_dec = (orig_num_bytes -
624 extent_num_bytes) >> 9;
Yanbab9fb02007-09-17 11:13:11 -0400625 if (extent_start != 0) {
626 inode->i_blocks -= num_dec;
627 }
Chris Mason5f39d392007-10-15 16:14:19 -0400628 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -0400629 } else {
Chris Masondb945352007-10-15 16:15:53 -0400630 extent_num_bytes =
631 btrfs_file_extent_disk_num_bytes(leaf,
632 fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400633 /* FIXME blocksize != 4096 */
Chris Masondb945352007-10-15 16:15:53 -0400634 num_dec = btrfs_file_extent_num_bytes(leaf,
635 fi) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -0400636 if (extent_start != 0) {
637 found_extent = 1;
638 inode->i_blocks -= num_dec;
639 }
Chris Masond8d5f3e2007-12-11 12:42:00 -0500640 root_gen = btrfs_header_generation(leaf);
641 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -0400642 }
Chris Mason179e29e2007-11-01 11:28:41 -0400643 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE &&
644 !del_item) {
645 u32 newsize = inode->i_size - found_key.offset;
646 newsize = btrfs_file_extent_calc_inline_size(newsize);
647 ret = btrfs_truncate_item(trans, root, path,
648 newsize, 1);
649 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -0400650 }
Chris Mason179e29e2007-11-01 11:28:41 -0400651delete:
Chris Mason39279cc2007-06-12 06:35:45 -0400652 if (del_item) {
653 ret = btrfs_del_item(trans, root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400654 if (ret)
655 goto error;
Chris Mason39279cc2007-06-12 06:35:45 -0400656 } else {
657 break;
658 }
659 btrfs_release_path(root, path);
660 if (found_extent) {
661 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -0500662 extent_num_bytes,
Chris Masond8d5f3e2007-12-11 12:42:00 -0500663 root_owner,
Chris Mason7bb86312007-12-11 09:25:06 -0500664 root_gen, inode->i_ino,
665 found_key.offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -0400666 BUG_ON(ret);
667 }
668 }
669 ret = 0;
670error:
671 btrfs_release_path(root, path);
672 btrfs_free_path(path);
673 inode->i_sb->s_dirt = 1;
674 return ret;
675}
676
Chris Masonb888db22007-08-27 16:49:44 -0400677static int btrfs_cow_one_page(struct inode *inode, struct page *page,
Chris Masona52d9a82007-08-27 16:49:44 -0400678 size_t zero_start)
Chris Mason39279cc2007-06-12 06:35:45 -0400679{
Chris Mason39279cc2007-06-12 06:35:45 -0400680 char *kaddr;
681 int ret = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400682 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason35ebb932007-10-30 16:56:53 -0400683 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masonb888db22007-08-27 16:49:44 -0400684 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Mason39279cc2007-06-12 06:35:45 -0400685
Christoph Hellwigb3cfa352007-09-17 11:25:58 -0400686 set_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400687
Chris Masonb888db22007-08-27 16:49:44 -0400688 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
689 set_extent_delalloc(&BTRFS_I(inode)->extent_tree, page_start,
690 page_end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -0400691 if (zero_start != PAGE_CACHE_SIZE) {
Chris Masonb888db22007-08-27 16:49:44 -0400692 kaddr = kmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400693 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
694 flush_dcache_page(page);
Chris Masonb888db22007-08-27 16:49:44 -0400695 kunmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400696 }
Chris Masonb888db22007-08-27 16:49:44 -0400697 set_page_dirty(page);
698 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -0400699
Chris Masona52d9a82007-08-27 16:49:44 -0400700 return ret;
701}
702
703/*
704 * taken from block_truncate_page, but does cow as it zeros out
705 * any bytes left in the last page in the file.
706 */
707static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
708{
709 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -0400710 struct btrfs_root *root = BTRFS_I(inode)->root;
711 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -0400712 pgoff_t index = from >> PAGE_CACHE_SHIFT;
713 unsigned offset = from & (PAGE_CACHE_SIZE-1);
714 struct page *page;
715 int ret = 0;
716 u64 page_start;
717
718 if ((offset & (blocksize - 1)) == 0)
719 goto out;
720
Chris Masondb945352007-10-15 16:15:53 -0400721 down_read(&root->snap_sem);
Chris Masona52d9a82007-08-27 16:49:44 -0400722 ret = -ENOMEM;
723 page = grab_cache_page(mapping, index);
724 if (!page)
725 goto out;
726 if (!PageUptodate(page)) {
727 ret = btrfs_readpage(NULL, page);
728 lock_page(page);
729 if (!PageUptodate(page)) {
730 ret = -EIO;
731 goto out;
732 }
733 }
Chris Mason35ebb932007-10-30 16:56:53 -0400734 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -0400735
Chris Masonb888db22007-08-27 16:49:44 -0400736 ret = btrfs_cow_one_page(inode, page, offset);
Chris Mason39279cc2007-06-12 06:35:45 -0400737
Chris Mason39279cc2007-06-12 06:35:45 -0400738 unlock_page(page);
739 page_cache_release(page);
Chris Mason011410b2007-09-10 19:58:36 -0400740 up_read(&BTRFS_I(inode)->root->snap_sem);
Chris Mason39279cc2007-06-12 06:35:45 -0400741out:
742 return ret;
743}
744
745static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
746{
747 struct inode *inode = dentry->d_inode;
748 int err;
749
750 err = inode_change_ok(inode, attr);
751 if (err)
752 return err;
753
754 if (S_ISREG(inode->i_mode) &&
755 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
756 struct btrfs_trans_handle *trans;
757 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason2bf5a722007-08-30 11:54:02 -0400758 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
759
Chris Mason5f39d392007-10-15 16:14:19 -0400760 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -0400761 u64 pos = (inode->i_size + mask) & ~mask;
Chris Mason2bf5a722007-08-30 11:54:02 -0400762 u64 block_end = attr->ia_size | mask;
Chris Mason39279cc2007-06-12 06:35:45 -0400763 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -0400764 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400765
766 if (attr->ia_size <= pos)
767 goto out;
768
769 btrfs_truncate_page(inode->i_mapping, inode->i_size);
770
Chris Mason2bf5a722007-08-30 11:54:02 -0400771 lock_extent(em_tree, pos, block_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -0400772 hole_size = (attr->ia_size - pos + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -0400773
774 mutex_lock(&root->fs_info->fs_mutex);
775 trans = btrfs_start_transaction(root, 1);
776 btrfs_set_trans_block_group(trans, inode);
Chris Mason2bf5a722007-08-30 11:54:02 -0400777 err = btrfs_drop_extents(trans, root, inode,
Chris Mason3326d1b2007-10-15 16:18:25 -0400778 pos, pos + hole_size, pos,
779 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -0400780
Chris Mason179e29e2007-11-01 11:28:41 -0400781 if (alloc_hint != EXTENT_MAP_INLINE) {
782 err = btrfs_insert_file_extent(trans, root,
783 inode->i_ino,
784 pos, 0, 0, hole_size);
785 }
Chris Mason39279cc2007-06-12 06:35:45 -0400786 btrfs_end_transaction(trans, root);
787 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason2bf5a722007-08-30 11:54:02 -0400788 unlock_extent(em_tree, pos, block_end, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -0400789 if (err)
790 return err;
Chris Mason39279cc2007-06-12 06:35:45 -0400791 }
792out:
793 err = inode_setattr(inode, attr);
794
795 return err;
796}
797void btrfs_delete_inode(struct inode *inode)
798{
799 struct btrfs_trans_handle *trans;
800 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400801 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -0400802 int ret;
803
804 truncate_inode_pages(&inode->i_data, 0);
805 if (is_bad_inode(inode)) {
806 goto no_delete;
807 }
Chris Mason5f39d392007-10-15 16:14:19 -0400808
Chris Mason39279cc2007-06-12 06:35:45 -0400809 inode->i_size = 0;
810 mutex_lock(&root->fs_info->fs_mutex);
811 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400812
Chris Mason39279cc2007-06-12 06:35:45 -0400813 btrfs_set_trans_block_group(trans, inode);
814 ret = btrfs_truncate_in_trans(trans, root, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -0400815 if (ret)
816 goto no_delete_lock;
Josef Bacik5103e942007-11-16 11:45:54 -0500817 ret = btrfs_delete_xattrs(trans, root, inode);
818 if (ret)
819 goto no_delete_lock;
Chris Mason54aa1f42007-06-22 14:16:25 -0400820 ret = btrfs_free_inode(trans, root, inode);
821 if (ret)
822 goto no_delete_lock;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400823 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -0400824
Chris Mason39279cc2007-06-12 06:35:45 -0400825 btrfs_end_transaction(trans, root);
826 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400827 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -0400828 return;
Chris Mason54aa1f42007-06-22 14:16:25 -0400829
830no_delete_lock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400831 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -0400832 btrfs_end_transaction(trans, root);
833 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400834 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -0400835no_delete:
836 clear_inode(inode);
837}
838
839/*
840 * this returns the key found in the dir entry in the location pointer.
841 * If no dir entries were found, location->objectid is 0.
842 */
843static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
844 struct btrfs_key *location)
845{
846 const char *name = dentry->d_name.name;
847 int namelen = dentry->d_name.len;
848 struct btrfs_dir_item *di;
849 struct btrfs_path *path;
850 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -0400851 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400852
Chris Mason39544012007-12-12 14:38:19 -0500853 if (namelen == 1 && strcmp(name, ".") == 0) {
854 location->objectid = dir->i_ino;
855 location->type = BTRFS_INODE_ITEM_KEY;
856 location->offset = 0;
857 return 0;
858 }
Chris Mason39279cc2007-06-12 06:35:45 -0400859 path = btrfs_alloc_path();
860 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -0500861
862 if (namelen == 1 && strcmp(name, "..") == 0) {
863 struct btrfs_key key;
864 struct extent_buffer *leaf;
865 u32 nritems;
866 int slot;
867
868 key.objectid = dir->i_ino;
869 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
870 key.offset = 0;
871 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
872 BUG_ON(ret == 0);
873 ret = 0;
874
875 leaf = path->nodes[0];
876 slot = path->slots[0];
877 nritems = btrfs_header_nritems(leaf);
878 if (slot >= nritems)
879 goto out_err;
880
881 btrfs_item_key_to_cpu(leaf, &key, slot);
882 if (key.objectid != dir->i_ino ||
883 key.type != BTRFS_INODE_REF_KEY) {
884 goto out_err;
885 }
886 location->objectid = key.offset;
887 location->type = BTRFS_INODE_ITEM_KEY;
888 location->offset = 0;
889 goto out;
890 }
891
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)) {
Chris Mason39544012007-12-12 14:38:19 -0500897 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -0400898 }
Chris Mason5f39d392007-10-15 16:14:19 -0400899 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -0400900out:
Chris Mason39279cc2007-06-12 06:35:45 -0400901 btrfs_free_path(path);
902 return ret;
Chris Mason39544012007-12-12 14:38:19 -0500903out_err:
904 location->objectid = 0;
905 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -0400906}
907
908/*
909 * when we hit a tree root in a directory, the btrfs part of the inode
910 * needs to be changed to reflect the root directory of the tree root. This
911 * is kind of like crossing a mount point.
912 */
913static int fixup_tree_root_location(struct btrfs_root *root,
914 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -0400915 struct btrfs_root **sub_root,
916 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -0400917{
918 struct btrfs_path *path;
919 struct btrfs_root_item *ri;
920
921 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
922 return 0;
923 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
924 return 0;
925
926 path = btrfs_alloc_path();
927 BUG_ON(!path);
928 mutex_lock(&root->fs_info->fs_mutex);
929
Josef Bacik58176a92007-08-29 15:47:34 -0400930 *sub_root = btrfs_read_fs_root(root->fs_info, location,
931 dentry->d_name.name,
932 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -0400933 if (IS_ERR(*sub_root))
934 return PTR_ERR(*sub_root);
935
936 ri = &(*sub_root)->root_item;
937 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -0400938 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
939 location->offset = 0;
940
941 btrfs_free_path(path);
942 mutex_unlock(&root->fs_info->fs_mutex);
943 return 0;
944}
945
946static int btrfs_init_locked_inode(struct inode *inode, void *p)
947{
948 struct btrfs_iget_args *args = p;
949 inode->i_ino = args->ino;
950 BTRFS_I(inode)->root = args->root;
Chris Masonb888db22007-08-27 16:49:44 -0400951 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
952 inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -0400953 return 0;
954}
955
956static int btrfs_find_actor(struct inode *inode, void *opaque)
957{
958 struct btrfs_iget_args *args = opaque;
959 return (args->ino == inode->i_ino &&
960 args->root == BTRFS_I(inode)->root);
961}
962
963struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
964 struct btrfs_root *root)
965{
966 struct inode *inode;
967 struct btrfs_iget_args args;
968 args.ino = objectid;
969 args.root = root;
970
971 inode = iget5_locked(s, objectid, btrfs_find_actor,
972 btrfs_init_locked_inode,
973 (void *)&args);
974 return inode;
975}
976
977static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
978 struct nameidata *nd)
979{
980 struct inode * inode;
981 struct btrfs_inode *bi = BTRFS_I(dir);
982 struct btrfs_root *root = bi->root;
983 struct btrfs_root *sub_root = root;
984 struct btrfs_key location;
985 int ret;
986
987 if (dentry->d_name.len > BTRFS_NAME_LEN)
988 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -0400989
Chris Mason39279cc2007-06-12 06:35:45 -0400990 mutex_lock(&root->fs_info->fs_mutex);
991 ret = btrfs_inode_by_name(dir, dentry, &location);
992 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason5f39d392007-10-15 16:14:19 -0400993
Chris Mason39279cc2007-06-12 06:35:45 -0400994 if (ret < 0)
995 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -0400996
Chris Mason39279cc2007-06-12 06:35:45 -0400997 inode = NULL;
998 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -0400999 ret = fixup_tree_root_location(root, &location, &sub_root,
1000 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04001001 if (ret < 0)
1002 return ERR_PTR(ret);
1003 if (ret > 0)
1004 return ERR_PTR(-ENOENT);
1005 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1006 sub_root);
1007 if (!inode)
1008 return ERR_PTR(-EACCES);
1009 if (inode->i_state & I_NEW) {
1010 /* the inode and parent dir are two different roots */
1011 if (sub_root != root) {
1012 igrab(inode);
1013 sub_root->inode = inode;
1014 }
1015 BTRFS_I(inode)->root = sub_root;
1016 memcpy(&BTRFS_I(inode)->location, &location,
1017 sizeof(location));
1018 btrfs_read_locked_inode(inode);
1019 unlock_new_inode(inode);
1020 }
1021 }
1022 return d_splice_alias(inode, dentry);
1023}
1024
Chris Mason39279cc2007-06-12 06:35:45 -04001025static unsigned char btrfs_filetype_table[] = {
1026 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1027};
1028
1029static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1030{
1031 struct inode *inode = filp->f_path.dentry->d_inode;
1032 struct btrfs_root *root = BTRFS_I(inode)->root;
1033 struct btrfs_item *item;
1034 struct btrfs_dir_item *di;
1035 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001036 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001037 struct btrfs_path *path;
1038 int ret;
1039 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001040 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001041 int slot;
1042 int advance;
1043 unsigned char d_type;
1044 int over = 0;
1045 u32 di_cur;
1046 u32 di_total;
1047 u32 di_len;
1048 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001049 char tmp_name[32];
1050 char *name_ptr;
1051 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001052
1053 /* FIXME, use a real flag for deciding about the key type */
1054 if (root->fs_info->tree_root == root)
1055 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001056
Chris Mason39544012007-12-12 14:38:19 -05001057 /* special case for "." */
1058 if (filp->f_pos == 0) {
1059 over = filldir(dirent, ".", 1,
1060 1, inode->i_ino,
1061 DT_DIR);
1062 if (over)
1063 return 0;
1064 filp->f_pos = 1;
1065 }
1066
Chris Mason39279cc2007-06-12 06:35:45 -04001067 mutex_lock(&root->fs_info->fs_mutex);
1068 key.objectid = inode->i_ino;
Chris Mason39544012007-12-12 14:38:19 -05001069 path = btrfs_alloc_path();
1070 path->reada = 2;
1071
1072 /* special case for .., just use the back ref */
1073 if (filp->f_pos == 1) {
1074 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1075 key.offset = 0;
1076 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1077 BUG_ON(ret == 0);
1078 leaf = path->nodes[0];
1079 slot = path->slots[0];
1080 nritems = btrfs_header_nritems(leaf);
1081 if (slot >= nritems) {
1082 btrfs_release_path(root, path);
1083 goto read_dir_items;
1084 }
1085 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1086 btrfs_release_path(root, path);
1087 if (found_key.objectid != key.objectid ||
1088 found_key.type != BTRFS_INODE_REF_KEY)
1089 goto read_dir_items;
1090 over = filldir(dirent, "..", 2,
1091 2, found_key.offset, DT_DIR);
1092 if (over)
1093 goto nopos;
1094 filp->f_pos = 2;
1095 }
1096
1097read_dir_items:
Chris Mason39279cc2007-06-12 06:35:45 -04001098 btrfs_set_key_type(&key, key_type);
1099 key.offset = filp->f_pos;
Chris Mason5f39d392007-10-15 16:14:19 -04001100
Chris Mason39279cc2007-06-12 06:35:45 -04001101 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1102 if (ret < 0)
1103 goto err;
1104 advance = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001105 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001106 leaf = path->nodes[0];
1107 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001108 slot = path->slots[0];
1109 if (advance || slot >= nritems) {
1110 if (slot >= nritems -1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001111 ret = btrfs_next_leaf(root, path);
1112 if (ret)
1113 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001114 leaf = path->nodes[0];
1115 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001116 slot = path->slots[0];
1117 } else {
1118 slot++;
1119 path->slots[0]++;
1120 }
1121 }
1122 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001123 item = btrfs_item_nr(leaf, slot);
1124 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1125
1126 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04001127 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001128 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001129 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001130 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04001131 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001132
1133 filp->f_pos = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001134 advance = 1;
1135 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1136 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001137 di_total = btrfs_item_size(leaf, item);
Chris Mason39279cc2007-06-12 06:35:45 -04001138 while(di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04001139 struct btrfs_key location;
1140
1141 name_len = btrfs_dir_name_len(leaf, di);
1142 if (name_len < 32) {
1143 name_ptr = tmp_name;
1144 } else {
1145 name_ptr = kmalloc(name_len, GFP_NOFS);
1146 BUG_ON(!name_ptr);
1147 }
1148 read_extent_buffer(leaf, name_ptr,
1149 (unsigned long)(di + 1), name_len);
1150
1151 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1152 btrfs_dir_item_key_to_cpu(leaf, di, &location);
1153
1154 over = filldir(dirent, name_ptr, name_len,
1155 found_key.offset,
1156 location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001157 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04001158
1159 if (name_ptr != tmp_name)
1160 kfree(name_ptr);
1161
Chris Mason39279cc2007-06-12 06:35:45 -04001162 if (over)
1163 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05001164 di_len = btrfs_dir_name_len(leaf, di) +
1165 btrfs_dir_data_len(leaf, di) +sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04001166 di_cur += di_len;
1167 di = (struct btrfs_dir_item *)((char *)di + di_len);
1168 }
1169 }
1170 filp->f_pos++;
1171nopos:
1172 ret = 0;
1173err:
1174 btrfs_release_path(root, path);
1175 btrfs_free_path(path);
1176 mutex_unlock(&root->fs_info->fs_mutex);
1177 return ret;
1178}
1179
1180int btrfs_write_inode(struct inode *inode, int wait)
1181{
1182 struct btrfs_root *root = BTRFS_I(inode)->root;
1183 struct btrfs_trans_handle *trans;
1184 int ret = 0;
1185
1186 if (wait) {
1187 mutex_lock(&root->fs_info->fs_mutex);
1188 trans = btrfs_start_transaction(root, 1);
1189 btrfs_set_trans_block_group(trans, inode);
1190 ret = btrfs_commit_transaction(trans, root);
1191 mutex_unlock(&root->fs_info->fs_mutex);
1192 }
1193 return ret;
1194}
1195
1196/*
Chris Mason54aa1f42007-06-22 14:16:25 -04001197 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04001198 * inode changes. But, it is most likely to find the inode in cache.
1199 * FIXME, needs more benchmarking...there are no reasons other than performance
1200 * to keep or drop this code.
1201 */
1202void btrfs_dirty_inode(struct inode *inode)
1203{
1204 struct btrfs_root *root = BTRFS_I(inode)->root;
1205 struct btrfs_trans_handle *trans;
1206
1207 mutex_lock(&root->fs_info->fs_mutex);
1208 trans = btrfs_start_transaction(root, 1);
1209 btrfs_set_trans_block_group(trans, inode);
1210 btrfs_update_inode(trans, root, inode);
1211 btrfs_end_transaction(trans, root);
1212 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001213}
1214
1215static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1216 struct btrfs_root *root,
1217 u64 objectid,
1218 struct btrfs_block_group_cache *group,
1219 int mode)
1220{
1221 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001222 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04001223 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04001224 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04001225 int ret;
1226 int owner;
1227
Chris Mason5f39d392007-10-15 16:14:19 -04001228 path = btrfs_alloc_path();
1229 BUG_ON(!path);
1230
Chris Mason39279cc2007-06-12 06:35:45 -04001231 inode = new_inode(root->fs_info->sb);
1232 if (!inode)
1233 return ERR_PTR(-ENOMEM);
1234
Chris Masonb888db22007-08-27 16:49:44 -04001235 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1236 inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001237 BTRFS_I(inode)->root = root;
Chris Masonb888db22007-08-27 16:49:44 -04001238
Chris Mason39279cc2007-06-12 06:35:45 -04001239 if (mode & S_IFDIR)
1240 owner = 0;
1241 else
1242 owner = 1;
1243 group = btrfs_find_block_group(root, group, 0, 0, owner);
1244 BTRFS_I(inode)->block_group = group;
1245
Chris Mason5f39d392007-10-15 16:14:19 -04001246 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
1247 if (ret)
1248 goto fail;
1249
Chris Mason39279cc2007-06-12 06:35:45 -04001250 inode->i_uid = current->fsuid;
1251 inode->i_gid = current->fsgid;
1252 inode->i_mode = mode;
1253 inode->i_ino = objectid;
1254 inode->i_blocks = 0;
1255 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04001256 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1257 struct btrfs_inode_item);
1258 fill_inode_item(path->nodes[0], inode_item, inode);
1259 btrfs_mark_buffer_dirty(path->nodes[0]);
1260 btrfs_free_path(path);
1261
Chris Mason39279cc2007-06-12 06:35:45 -04001262 location = &BTRFS_I(inode)->location;
1263 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04001264 location->offset = 0;
1265 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1266
Chris Mason39279cc2007-06-12 06:35:45 -04001267 insert_inode_hash(inode);
1268 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001269fail:
1270 btrfs_free_path(path);
1271 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04001272}
1273
1274static inline u8 btrfs_inode_type(struct inode *inode)
1275{
1276 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1277}
1278
1279static int btrfs_add_link(struct btrfs_trans_handle *trans,
1280 struct dentry *dentry, struct inode *inode)
1281{
1282 int ret;
1283 struct btrfs_key key;
1284 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Chris Mason79c44582007-06-25 10:09:33 -04001285 struct inode *parent_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001286
Chris Mason39279cc2007-06-12 06:35:45 -04001287 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04001288 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1289 key.offset = 0;
1290
1291 ret = btrfs_insert_dir_item(trans, root,
1292 dentry->d_name.name, dentry->d_name.len,
1293 dentry->d_parent->d_inode->i_ino,
1294 &key, btrfs_inode_type(inode));
1295 if (ret == 0) {
Chris Mason39544012007-12-12 14:38:19 -05001296 if (!S_ISLNK(inode->i_mode)) {
1297 ret = btrfs_insert_inode_ref(trans, root,
1298 dentry->d_name.name,
1299 dentry->d_name.len,
1300 inode->i_ino,
1301 dentry->d_parent->d_inode->i_ino);
1302 }
Chris Mason79c44582007-06-25 10:09:33 -04001303 parent_inode = dentry->d_parent->d_inode;
1304 parent_inode->i_size += dentry->d_name.len * 2;
1305 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001306 ret = btrfs_update_inode(trans, root,
1307 dentry->d_parent->d_inode);
1308 }
1309 return ret;
1310}
1311
1312static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1313 struct dentry *dentry, struct inode *inode)
1314{
1315 int err = btrfs_add_link(trans, dentry, inode);
1316 if (!err) {
1317 d_instantiate(dentry, inode);
1318 return 0;
1319 }
1320 if (err > 0)
1321 err = -EEXIST;
1322 return err;
1323}
1324
Josef Bacik618e21d2007-07-11 10:18:17 -04001325static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1326 int mode, dev_t rdev)
1327{
1328 struct btrfs_trans_handle *trans;
1329 struct btrfs_root *root = BTRFS_I(dir)->root;
1330 struct inode *inode;
1331 int err;
1332 int drop_inode = 0;
1333 u64 objectid;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001334 unsigned long nr;
Josef Bacik618e21d2007-07-11 10:18:17 -04001335
1336 if (!new_valid_dev(rdev))
1337 return -EINVAL;
1338
1339 mutex_lock(&root->fs_info->fs_mutex);
1340 trans = btrfs_start_transaction(root, 1);
1341 btrfs_set_trans_block_group(trans, dir);
1342
1343 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1344 if (err) {
1345 err = -ENOSPC;
1346 goto out_unlock;
1347 }
1348
1349 inode = btrfs_new_inode(trans, root, objectid,
1350 BTRFS_I(dir)->block_group, mode);
1351 err = PTR_ERR(inode);
1352 if (IS_ERR(inode))
1353 goto out_unlock;
1354
1355 btrfs_set_trans_block_group(trans, inode);
1356 err = btrfs_add_nondir(trans, dentry, inode);
1357 if (err)
1358 drop_inode = 1;
1359 else {
1360 inode->i_op = &btrfs_special_inode_operations;
1361 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04001362 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04001363 }
1364 dir->i_sb->s_dirt = 1;
1365 btrfs_update_inode_block_group(trans, inode);
1366 btrfs_update_inode_block_group(trans, dir);
1367out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001368 nr = trans->blocks_used;
Josef Bacik618e21d2007-07-11 10:18:17 -04001369 btrfs_end_transaction(trans, root);
1370 mutex_unlock(&root->fs_info->fs_mutex);
1371
1372 if (drop_inode) {
1373 inode_dec_link_count(inode);
1374 iput(inode);
1375 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001376 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04001377 return err;
1378}
1379
Chris Mason39279cc2007-06-12 06:35:45 -04001380static int btrfs_create(struct inode *dir, struct dentry *dentry,
1381 int mode, struct nameidata *nd)
1382{
1383 struct btrfs_trans_handle *trans;
1384 struct btrfs_root *root = BTRFS_I(dir)->root;
1385 struct inode *inode;
1386 int err;
1387 int drop_inode = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001388 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001389 u64 objectid;
1390
1391 mutex_lock(&root->fs_info->fs_mutex);
1392 trans = btrfs_start_transaction(root, 1);
1393 btrfs_set_trans_block_group(trans, dir);
1394
1395 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1396 if (err) {
1397 err = -ENOSPC;
1398 goto out_unlock;
1399 }
1400
1401 inode = btrfs_new_inode(trans, root, objectid,
1402 BTRFS_I(dir)->block_group, mode);
1403 err = PTR_ERR(inode);
1404 if (IS_ERR(inode))
1405 goto out_unlock;
1406
1407 btrfs_set_trans_block_group(trans, inode);
1408 err = btrfs_add_nondir(trans, dentry, inode);
1409 if (err)
1410 drop_inode = 1;
1411 else {
1412 inode->i_mapping->a_ops = &btrfs_aops;
1413 inode->i_fop = &btrfs_file_operations;
1414 inode->i_op = &btrfs_file_inode_operations;
Chris Masona52d9a82007-08-27 16:49:44 -04001415 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1416 inode->i_mapping, GFP_NOFS);
Chris Mason07157aa2007-08-30 08:50:51 -04001417 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04001418 }
1419 dir->i_sb->s_dirt = 1;
1420 btrfs_update_inode_block_group(trans, inode);
1421 btrfs_update_inode_block_group(trans, dir);
1422out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001423 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001424 btrfs_end_transaction(trans, root);
1425 mutex_unlock(&root->fs_info->fs_mutex);
1426
1427 if (drop_inode) {
1428 inode_dec_link_count(inode);
1429 iput(inode);
1430 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001431 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001432 return err;
1433}
1434
1435static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1436 struct dentry *dentry)
1437{
1438 struct btrfs_trans_handle *trans;
1439 struct btrfs_root *root = BTRFS_I(dir)->root;
1440 struct inode *inode = old_dentry->d_inode;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001441 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001442 int err;
1443 int drop_inode = 0;
1444
1445 if (inode->i_nlink == 0)
1446 return -ENOENT;
1447
1448 inc_nlink(inode);
1449 mutex_lock(&root->fs_info->fs_mutex);
1450 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001451
Chris Mason39279cc2007-06-12 06:35:45 -04001452 btrfs_set_trans_block_group(trans, dir);
1453 atomic_inc(&inode->i_count);
1454 err = btrfs_add_nondir(trans, dentry, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001455
Chris Mason39279cc2007-06-12 06:35:45 -04001456 if (err)
1457 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001458
Chris Mason39279cc2007-06-12 06:35:45 -04001459 dir->i_sb->s_dirt = 1;
1460 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04001461 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001462
Chris Mason54aa1f42007-06-22 14:16:25 -04001463 if (err)
1464 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001465
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001466 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001467 btrfs_end_transaction(trans, root);
1468 mutex_unlock(&root->fs_info->fs_mutex);
1469
1470 if (drop_inode) {
1471 inode_dec_link_count(inode);
1472 iput(inode);
1473 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001474 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001475 return err;
1476}
1477
Chris Mason39279cc2007-06-12 06:35:45 -04001478static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1479{
1480 struct inode *inode;
1481 struct btrfs_trans_handle *trans;
1482 struct btrfs_root *root = BTRFS_I(dir)->root;
1483 int err = 0;
1484 int drop_on_err = 0;
1485 u64 objectid;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001486 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001487
1488 mutex_lock(&root->fs_info->fs_mutex);
1489 trans = btrfs_start_transaction(root, 1);
1490 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04001491
Chris Mason39279cc2007-06-12 06:35:45 -04001492 if (IS_ERR(trans)) {
1493 err = PTR_ERR(trans);
1494 goto out_unlock;
1495 }
1496
1497 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1498 if (err) {
1499 err = -ENOSPC;
1500 goto out_unlock;
1501 }
1502
1503 inode = btrfs_new_inode(trans, root, objectid,
1504 BTRFS_I(dir)->block_group, S_IFDIR | mode);
1505 if (IS_ERR(inode)) {
1506 err = PTR_ERR(inode);
1507 goto out_fail;
1508 }
Chris Mason5f39d392007-10-15 16:14:19 -04001509
Chris Mason39279cc2007-06-12 06:35:45 -04001510 drop_on_err = 1;
1511 inode->i_op = &btrfs_dir_inode_operations;
1512 inode->i_fop = &btrfs_dir_file_operations;
1513 btrfs_set_trans_block_group(trans, inode);
1514
Chris Mason39544012007-12-12 14:38:19 -05001515 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001516 err = btrfs_update_inode(trans, root, inode);
1517 if (err)
1518 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04001519
Chris Mason39279cc2007-06-12 06:35:45 -04001520 err = btrfs_add_link(trans, dentry, inode);
1521 if (err)
1522 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04001523
Chris Mason39279cc2007-06-12 06:35:45 -04001524 d_instantiate(dentry, inode);
1525 drop_on_err = 0;
1526 dir->i_sb->s_dirt = 1;
1527 btrfs_update_inode_block_group(trans, inode);
1528 btrfs_update_inode_block_group(trans, dir);
1529
1530out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001531 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001532 btrfs_end_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04001533
Chris Mason39279cc2007-06-12 06:35:45 -04001534out_unlock:
1535 mutex_unlock(&root->fs_info->fs_mutex);
1536 if (drop_on_err)
1537 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001538 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001539 return err;
1540}
1541
Chris Masona52d9a82007-08-27 16:49:44 -04001542struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
1543 size_t page_offset, u64 start, u64 end,
1544 int create)
1545{
1546 int ret;
1547 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04001548 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04001549 u64 extent_start = 0;
1550 u64 extent_end = 0;
1551 u64 objectid = inode->i_ino;
1552 u32 found_type;
1553 int failed_insert = 0;
1554 struct btrfs_path *path;
1555 struct btrfs_root *root = BTRFS_I(inode)->root;
1556 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04001557 struct extent_buffer *leaf;
1558 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04001559 struct extent_map *em = NULL;
1560 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1561 struct btrfs_trans_handle *trans = NULL;
1562
1563 path = btrfs_alloc_path();
1564 BUG_ON(!path);
1565 mutex_lock(&root->fs_info->fs_mutex);
1566
1567again:
1568 em = lookup_extent_mapping(em_tree, start, end);
1569 if (em) {
1570 goto out;
1571 }
1572 if (!em) {
1573 em = alloc_extent_map(GFP_NOFS);
1574 if (!em) {
1575 err = -ENOMEM;
1576 goto out;
1577 }
Chris Mason5f39d392007-10-15 16:14:19 -04001578 em->start = EXTENT_MAP_HOLE;
1579 em->end = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001580 }
1581 em->bdev = inode->i_sb->s_bdev;
Chris Mason179e29e2007-11-01 11:28:41 -04001582 ret = btrfs_lookup_file_extent(trans, root, path,
1583 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04001584 if (ret < 0) {
1585 err = ret;
1586 goto out;
1587 }
1588
1589 if (ret != 0) {
1590 if (path->slots[0] == 0)
1591 goto not_found;
1592 path->slots[0]--;
1593 }
1594
Chris Mason5f39d392007-10-15 16:14:19 -04001595 leaf = path->nodes[0];
1596 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04001597 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04001598 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04001599 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1600 found_type = btrfs_key_type(&found_key);
1601 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04001602 found_type != BTRFS_EXTENT_DATA_KEY) {
1603 goto not_found;
1604 }
1605
Chris Mason5f39d392007-10-15 16:14:19 -04001606 found_type = btrfs_file_extent_type(leaf, item);
1607 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04001608 if (found_type == BTRFS_FILE_EXTENT_REG) {
1609 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04001610 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04001611 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04001612 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04001613 em->start = start;
1614 if (start < extent_start) {
Chris Masonb888db22007-08-27 16:49:44 -04001615 if (end < extent_start)
1616 goto not_found;
Chris Masona52d9a82007-08-27 16:49:44 -04001617 em->end = extent_end - 1;
1618 } else {
1619 em->end = end;
1620 }
1621 goto not_found_em;
1622 }
Chris Masondb945352007-10-15 16:15:53 -04001623 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
1624 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04001625 em->start = extent_start;
1626 em->end = extent_end - 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001627 em->block_start = EXTENT_MAP_HOLE;
1628 em->block_end = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001629 goto insert;
1630 }
Chris Masondb945352007-10-15 16:15:53 -04001631 bytenr += btrfs_file_extent_offset(leaf, item);
1632 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04001633 em->block_end = em->block_start +
Chris Masondb945352007-10-15 16:15:53 -04001634 btrfs_file_extent_num_bytes(leaf, item) - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04001635 em->start = extent_start;
1636 em->end = extent_end - 1;
1637 goto insert;
1638 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04001639 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04001640 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04001641 size_t size;
1642 size_t extent_offset;
1643 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04001644
Chris Mason5f39d392007-10-15 16:14:19 -04001645 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
1646 path->slots[0]));
Yan689f9342007-10-29 11:41:07 -04001647 extent_end = (extent_start + size - 1) |
Chris Masondb945352007-10-15 16:15:53 -04001648 ((u64)root->sectorsize - 1);
Chris Masonb888db22007-08-27 16:49:44 -04001649 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04001650 em->start = start;
1651 if (start < extent_start) {
Chris Masonb888db22007-08-27 16:49:44 -04001652 if (end < extent_start)
1653 goto not_found;
Chris Mason50b78c22007-09-20 14:14:42 -04001654 em->end = extent_end;
Chris Masona52d9a82007-08-27 16:49:44 -04001655 } else {
1656 em->end = end;
1657 }
1658 goto not_found_em;
1659 }
1660 em->block_start = EXTENT_MAP_INLINE;
1661 em->block_end = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04001662
1663 if (!page) {
1664 em->start = extent_start;
1665 em->end = extent_start + size - 1;
1666 goto out;
1667 }
1668
Chris Mason35ebb932007-10-30 16:56:53 -04001669 extent_offset = ((u64)page->index << PAGE_CACHE_SHIFT) -
Yan689f9342007-10-29 11:41:07 -04001670 extent_start + page_offset;
1671 copy_size = min_t(u64, PAGE_CACHE_SIZE - page_offset,
1672 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04001673 em->start = extent_start + extent_offset;
1674 em->end = (em->start + copy_size -1) |
1675 ((u64)root->sectorsize -1);
Yan689f9342007-10-29 11:41:07 -04001676 map = kmap(page);
1677 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04001678 if (create == 0 && !PageUptodate(page)) {
1679 read_extent_buffer(leaf, map + page_offset, ptr,
1680 copy_size);
1681 flush_dcache_page(page);
1682 } else if (create && PageUptodate(page)) {
1683 if (!trans) {
1684 kunmap(page);
1685 free_extent_map(em);
1686 em = NULL;
1687 btrfs_release_path(root, path);
1688 trans = btrfs_start_transaction(root, 1);
1689 goto again;
1690 }
1691 write_extent_buffer(leaf, map + page_offset, ptr,
1692 copy_size);
1693 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04001694 }
Chris Masona52d9a82007-08-27 16:49:44 -04001695 kunmap(page);
Chris Mason3326d1b2007-10-15 16:18:25 -04001696 set_extent_uptodate(em_tree, em->start, em->end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04001697 goto insert;
1698 } else {
1699 printk("unkknown found_type %d\n", found_type);
1700 WARN_ON(1);
1701 }
1702not_found:
1703 em->start = start;
1704 em->end = end;
1705not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04001706 em->block_start = EXTENT_MAP_HOLE;
1707 em->block_end = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001708insert:
1709 btrfs_release_path(root, path);
1710 if (em->start > start || em->end < start) {
Chris Masonb888db22007-08-27 16:49:44 -04001711 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->end, start, end);
Chris Masona52d9a82007-08-27 16:49:44 -04001712 err = -EIO;
1713 goto out;
1714 }
1715 ret = add_extent_mapping(em_tree, em);
1716 if (ret == -EEXIST) {
1717 free_extent_map(em);
Chris Mason2bf5a722007-08-30 11:54:02 -04001718 em = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04001719 failed_insert++;
1720 if (failed_insert > 5) {
1721 printk("failing to insert %Lu %Lu\n", start, end);
1722 err = -EIO;
1723 goto out;
1724 }
Chris Masona52d9a82007-08-27 16:49:44 -04001725 goto again;
1726 }
1727 err = 0;
1728out:
1729 btrfs_free_path(path);
1730 if (trans) {
1731 ret = btrfs_end_transaction(trans, root);
1732 if (!err)
1733 err = ret;
1734 }
1735 mutex_unlock(&root->fs_info->fs_mutex);
1736 if (err) {
1737 free_extent_map(em);
1738 WARN_ON(1);
1739 return ERR_PTR(err);
1740 }
1741 return em;
1742}
1743
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04001744static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04001745{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04001746 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04001747}
1748
1749static int btrfs_prepare_write(struct file *file, struct page *page,
1750 unsigned from, unsigned to)
1751{
Chris Masona52d9a82007-08-27 16:49:44 -04001752 return extent_prepare_write(&BTRFS_I(page->mapping->host)->extent_tree,
1753 page->mapping->host, page, from, to,
1754 btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04001755}
1756
Chris Mason9ebefb182007-06-15 13:50:00 -04001757int btrfs_readpage(struct file *file, struct page *page)
1758{
Chris Masona52d9a82007-08-27 16:49:44 -04001759 struct extent_map_tree *tree;
1760 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1761 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04001762}
Chris Mason39279cc2007-06-12 06:35:45 -04001763static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1764{
Chris Masona52d9a82007-08-27 16:49:44 -04001765 struct extent_map_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04001766
1767
1768 if (current->flags & PF_MEMALLOC) {
1769 redirty_page_for_writepage(wbc, page);
1770 unlock_page(page);
1771 return 0;
1772 }
Chris Masona52d9a82007-08-27 16:49:44 -04001773 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1774 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
1775}
Chris Mason39279cc2007-06-12 06:35:45 -04001776
Chris Masonb293f022007-11-01 19:45:34 -04001777static int btrfs_writepages(struct address_space *mapping,
1778 struct writeback_control *wbc)
1779{
1780 struct extent_map_tree *tree;
1781 tree = &BTRFS_I(mapping->host)->extent_tree;
1782 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
1783}
1784
Chris Mason3ab2fb52007-11-08 10:59:22 -05001785static int
1786btrfs_readpages(struct file *file, struct address_space *mapping,
1787 struct list_head *pages, unsigned nr_pages)
1788{
1789 struct extent_map_tree *tree;
1790 tree = &BTRFS_I(mapping->host)->extent_tree;
1791 return extent_readpages(tree, mapping, pages, nr_pages,
1792 btrfs_get_extent);
1793}
1794
Chris Masona52d9a82007-08-27 16:49:44 -04001795static int btrfs_releasepage(struct page *page, gfp_t unused_gfp_flags)
1796{
1797 struct extent_map_tree *tree;
1798 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04001799
Chris Masona52d9a82007-08-27 16:49:44 -04001800 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1801 ret = try_release_extent_mapping(tree, page);
1802 if (ret == 1) {
1803 ClearPagePrivate(page);
1804 set_page_private(page, 0);
1805 page_cache_release(page);
1806 }
1807 return ret;
1808}
Chris Mason39279cc2007-06-12 06:35:45 -04001809
Chris Masona52d9a82007-08-27 16:49:44 -04001810static void btrfs_invalidatepage(struct page *page, unsigned long offset)
1811{
1812 struct extent_map_tree *tree;
1813
1814 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1815 extent_invalidatepage(tree, page, offset);
1816 btrfs_releasepage(page, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001817}
1818
Chris Mason9ebefb182007-06-15 13:50:00 -04001819/*
1820 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
1821 * called from a page fault handler when a page is first dirtied. Hence we must
1822 * be careful to check for EOF conditions here. We set the page up correctly
1823 * for a written page which means we get ENOSPC checking when writing into
1824 * holes and correct delalloc and unwritten extent mapping on filesystems that
1825 * support these features.
1826 *
1827 * We are not allowed to take the i_mutex here so we have to play games to
1828 * protect against truncate races as the page could now be beyond EOF. Because
1829 * vmtruncate() writes the inode size before removing pages, once we have the
1830 * page lock we can determine safely if the page is beyond EOF. If it is not
1831 * beyond EOF, then the page is guaranteed safe against truncation until we
1832 * unlock the page.
1833 */
1834int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
1835{
1836 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1837 unsigned long end;
1838 loff_t size;
1839 int ret = -EINVAL;
Chris Masona52d9a82007-08-27 16:49:44 -04001840 u64 page_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04001841
Chris Mason011410b2007-09-10 19:58:36 -04001842 down_read(&BTRFS_I(inode)->root->snap_sem);
Chris Mason9ebefb182007-06-15 13:50:00 -04001843 lock_page(page);
1844 wait_on_page_writeback(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04001845 size = i_size_read(inode);
Chris Mason35ebb932007-10-30 16:56:53 -04001846 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04001847
Chris Mason9ebefb182007-06-15 13:50:00 -04001848 if ((page->mapping != inode->i_mapping) ||
Chris Masona52d9a82007-08-27 16:49:44 -04001849 (page_start > size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04001850 /* page got truncated out from underneath us */
1851 goto out_unlock;
1852 }
1853
1854 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04001855 if (page_start + PAGE_CACHE_SIZE > size)
Chris Mason9ebefb182007-06-15 13:50:00 -04001856 end = size & ~PAGE_CACHE_MASK;
1857 else
1858 end = PAGE_CACHE_SIZE;
1859
Chris Masonb888db22007-08-27 16:49:44 -04001860 ret = btrfs_cow_one_page(inode, page, end);
Chris Mason9ebefb182007-06-15 13:50:00 -04001861
1862out_unlock:
Chris Mason011410b2007-09-10 19:58:36 -04001863 up_read(&BTRFS_I(inode)->root->snap_sem);
Chris Mason9ebefb182007-06-15 13:50:00 -04001864 unlock_page(page);
1865 return ret;
1866}
1867
Chris Mason39279cc2007-06-12 06:35:45 -04001868static void btrfs_truncate(struct inode *inode)
1869{
1870 struct btrfs_root *root = BTRFS_I(inode)->root;
1871 int ret;
1872 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001873 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001874
1875 if (!S_ISREG(inode->i_mode))
1876 return;
1877 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1878 return;
1879
1880 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1881
1882 mutex_lock(&root->fs_info->fs_mutex);
1883 trans = btrfs_start_transaction(root, 1);
1884 btrfs_set_trans_block_group(trans, inode);
1885
1886 /* FIXME, add redo link to tree so we don't leak on crash */
1887 ret = btrfs_truncate_in_trans(trans, root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001888 btrfs_update_inode(trans, root, inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001889 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04001890
Chris Mason39279cc2007-06-12 06:35:45 -04001891 ret = btrfs_end_transaction(trans, root);
1892 BUG_ON(ret);
1893 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001894 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001895}
1896
1897int btrfs_commit_write(struct file *file, struct page *page,
1898 unsigned from, unsigned to)
1899{
Chris Masona52d9a82007-08-27 16:49:44 -04001900 return extent_commit_write(&BTRFS_I(page->mapping->host)->extent_tree,
1901 page->mapping->host, page, from, to);
Chris Mason39279cc2007-06-12 06:35:45 -04001902}
1903
1904static int create_subvol(struct btrfs_root *root, char *name, int namelen)
1905{
1906 struct btrfs_trans_handle *trans;
1907 struct btrfs_key key;
1908 struct btrfs_root_item root_item;
1909 struct btrfs_inode_item *inode_item;
Chris Mason5f39d392007-10-15 16:14:19 -04001910 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001911 struct btrfs_root *new_root;
1912 struct inode *inode;
1913 struct inode *dir;
1914 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001915 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04001916 u64 objectid;
1917 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001918 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001919
1920 mutex_lock(&root->fs_info->fs_mutex);
1921 trans = btrfs_start_transaction(root, 1);
1922 BUG_ON(!trans);
1923
Chris Mason7bb86312007-12-11 09:25:06 -05001924 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
1925 0, &objectid);
1926 if (ret)
1927 goto fail;
1928
1929 leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
1930 objectid, trans->transid, 0, 0,
1931 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001932 if (IS_ERR(leaf))
1933 return PTR_ERR(leaf);
1934
1935 btrfs_set_header_nritems(leaf, 0);
1936 btrfs_set_header_level(leaf, 0);
Chris Masondb945352007-10-15 16:15:53 -04001937 btrfs_set_header_bytenr(leaf, leaf->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001938 btrfs_set_header_generation(leaf, trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05001939 btrfs_set_header_owner(leaf, objectid);
1940
Chris Mason5f39d392007-10-15 16:14:19 -04001941 write_extent_buffer(leaf, root->fs_info->fsid,
1942 (unsigned long)btrfs_header_fsid(leaf),
1943 BTRFS_FSID_SIZE);
1944 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001945
1946 inode_item = &root_item.inode;
1947 memset(inode_item, 0, sizeof(*inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04001948 inode_item->generation = cpu_to_le64(1);
1949 inode_item->size = cpu_to_le64(3);
1950 inode_item->nlink = cpu_to_le32(1);
1951 inode_item->nblocks = cpu_to_le64(1);
1952 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
Chris Mason39279cc2007-06-12 06:35:45 -04001953
Chris Masondb945352007-10-15 16:15:53 -04001954 btrfs_set_root_bytenr(&root_item, leaf->start);
1955 btrfs_set_root_level(&root_item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001956 btrfs_set_root_refs(&root_item, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001957 btrfs_set_root_used(&root_item, 0);
1958
Chris Mason5eda7b52007-06-22 14:16:25 -04001959 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
1960 root_item.drop_level = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001961
1962 free_extent_buffer(leaf);
1963 leaf = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001964
Chris Mason39279cc2007-06-12 06:35:45 -04001965 btrfs_set_root_dirid(&root_item, new_dirid);
1966
1967 key.objectid = objectid;
1968 key.offset = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001969 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
1970 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
1971 &root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -04001972 if (ret)
1973 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001974
1975 /*
1976 * insert the directory item
1977 */
1978 key.offset = (u64)-1;
1979 dir = root->fs_info->sb->s_root->d_inode;
1980 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
1981 name, namelen, dir->i_ino, &key,
1982 BTRFS_FT_DIR);
Chris Mason54aa1f42007-06-22 14:16:25 -04001983 if (ret)
1984 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001985
Chris Mason39544012007-12-12 14:38:19 -05001986 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
1987 name, namelen, objectid,
1988 root->fs_info->sb->s_root->d_inode->i_ino);
1989 if (ret)
1990 goto fail;
1991
Chris Mason39279cc2007-06-12 06:35:45 -04001992 ret = btrfs_commit_transaction(trans, root);
Chris Mason54aa1f42007-06-22 14:16:25 -04001993 if (ret)
1994 goto fail_commit;
Chris Mason39279cc2007-06-12 06:35:45 -04001995
Josef Bacik58176a92007-08-29 15:47:34 -04001996 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
Chris Mason39279cc2007-06-12 06:35:45 -04001997 BUG_ON(!new_root);
1998
1999 trans = btrfs_start_transaction(new_root, 1);
2000 BUG_ON(!trans);
2001
2002 inode = btrfs_new_inode(trans, new_root, new_dirid,
2003 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
Chris Mason54aa1f42007-06-22 14:16:25 -04002004 if (IS_ERR(inode))
2005 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002006 inode->i_op = &btrfs_dir_inode_operations;
2007 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04002008 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002009
Chris Mason39544012007-12-12 14:38:19 -05002010 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2011 new_dirid);
Chris Mason39279cc2007-06-12 06:35:45 -04002012 inode->i_nlink = 1;
Chris Mason39544012007-12-12 14:38:19 -05002013 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002014 ret = btrfs_update_inode(trans, new_root, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002015 if (ret)
2016 goto fail;
2017fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002018 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04002019 err = btrfs_commit_transaction(trans, root);
2020 if (err && !ret)
2021 ret = err;
2022fail_commit:
Chris Mason39279cc2007-06-12 06:35:45 -04002023 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002024 btrfs_btree_balance_dirty(root, nr);
Chris Mason54aa1f42007-06-22 14:16:25 -04002025 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002026}
2027
2028static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2029{
2030 struct btrfs_trans_handle *trans;
2031 struct btrfs_key key;
2032 struct btrfs_root_item new_root_item;
Chris Mason5f39d392007-10-15 16:14:19 -04002033 struct extent_buffer *tmp;
Chris Mason39279cc2007-06-12 06:35:45 -04002034 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002035 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04002036 u64 objectid;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002037 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002038
2039 if (!root->ref_cows)
2040 return -EINVAL;
2041
Chris Mason011410b2007-09-10 19:58:36 -04002042 down_write(&root->snap_sem);
2043 freeze_bdev(root->fs_info->sb->s_bdev);
2044 thaw_bdev(root->fs_info->sb->s_bdev, root->fs_info->sb);
2045
Chris Mason39279cc2007-06-12 06:35:45 -04002046 mutex_lock(&root->fs_info->fs_mutex);
2047 trans = btrfs_start_transaction(root, 1);
2048 BUG_ON(!trans);
2049
2050 ret = btrfs_update_inode(trans, root, root->inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002051 if (ret)
2052 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002053
2054 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2055 0, &objectid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002056 if (ret)
2057 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002058
2059 memcpy(&new_root_item, &root->root_item,
2060 sizeof(new_root_item));
2061
2062 key.objectid = objectid;
2063 key.offset = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002064 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
Yan96919752007-12-04 13:20:20 -05002065 extent_buffer_get(root->node);
Chris Mason83df7c12007-08-27 16:49:44 -04002066 btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp);
Yan96919752007-12-04 13:20:20 -05002067 free_extent_buffer(tmp);
Chris Masondb945352007-10-15 16:15:53 -04002068 btrfs_set_root_bytenr(&new_root_item, root->node->start);
2069 btrfs_set_root_level(&new_root_item, btrfs_header_level(root->node));
Chris Mason39279cc2007-06-12 06:35:45 -04002070 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2071 &new_root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -04002072 if (ret)
2073 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002074
2075 /*
2076 * insert the directory item
2077 */
2078 key.offset = (u64)-1;
2079 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2080 name, namelen,
2081 root->fs_info->sb->s_root->d_inode->i_ino,
2082 &key, BTRFS_FT_DIR);
2083
Chris Mason54aa1f42007-06-22 14:16:25 -04002084 if (ret)
2085 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002086
Chris Mason39544012007-12-12 14:38:19 -05002087 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2088 name, namelen, objectid,
2089 root->fs_info->sb->s_root->d_inode->i_ino);
2090
2091 if (ret)
2092 goto fail;
2093
Chris Mason7bb86312007-12-11 09:25:06 -05002094 ret = btrfs_inc_root_ref(trans, root, objectid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002095 if (ret)
2096 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002097fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002098 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04002099 err = btrfs_commit_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002100
Chris Mason54aa1f42007-06-22 14:16:25 -04002101 if (err && !ret)
2102 ret = err;
Chris Mason5f39d392007-10-15 16:14:19 -04002103
Chris Mason39279cc2007-06-12 06:35:45 -04002104 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason011410b2007-09-10 19:58:36 -04002105 up_write(&root->snap_sem);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002106 btrfs_btree_balance_dirty(root, nr);
Chris Mason54aa1f42007-06-22 14:16:25 -04002107 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002108}
2109
Chris Mason86479a02007-09-10 19:58:16 -04002110static unsigned long force_ra(struct address_space *mapping,
2111 struct file_ra_state *ra, struct file *file,
2112 pgoff_t offset, pgoff_t last_index)
2113{
2114 pgoff_t req_size;
2115
2116#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2117 req_size = last_index - offset + 1;
2118 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2119 return offset;
2120#else
2121 req_size = min(last_index - offset + 1, (pgoff_t)128);
2122 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2123 return offset + req_size;
2124#endif
2125}
2126
2127int btrfs_defrag_file(struct file *file) {
2128 struct inode *inode = file->f_path.dentry->d_inode;
2129 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2130 struct page *page;
2131 unsigned long last_index;
2132 unsigned long ra_index = 0;
2133 u64 page_start;
2134 u64 page_end;
2135 unsigned long i;
2136
2137 mutex_lock(&inode->i_mutex);
2138 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2139 for (i = 0; i <= last_index; i++) {
2140 if (i == ra_index) {
2141 ra_index = force_ra(inode->i_mapping, &file->f_ra,
2142 file, ra_index, last_index);
2143 }
2144 page = grab_cache_page(inode->i_mapping, i);
2145 if (!page)
2146 goto out_unlock;
2147 if (!PageUptodate(page)) {
2148 btrfs_readpage(NULL, page);
2149 lock_page(page);
2150 if (!PageUptodate(page)) {
2151 unlock_page(page);
2152 page_cache_release(page);
2153 goto out_unlock;
2154 }
2155 }
Chris Mason35ebb932007-10-30 16:56:53 -04002156 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason86479a02007-09-10 19:58:16 -04002157 page_end = page_start + PAGE_CACHE_SIZE - 1;
2158
2159 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
2160 set_extent_delalloc(em_tree, page_start,
2161 page_end, GFP_NOFS);
2162 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
2163 set_page_dirty(page);
2164 unlock_page(page);
2165 page_cache_release(page);
2166 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2167 }
2168
2169out_unlock:
2170 mutex_unlock(&inode->i_mutex);
2171 return 0;
2172}
2173
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002174static int btrfs_ioctl_snap_create(struct btrfs_root *root, void __user *arg)
2175{
2176 struct btrfs_ioctl_vol_args vol_args;
2177 struct btrfs_dir_item *di;
2178 struct btrfs_path *path;
2179 int namelen;
2180 u64 root_dirid;
2181
2182 if (copy_from_user(&vol_args, arg, sizeof(vol_args)))
2183 return -EFAULT;
Chris Mason5f39d392007-10-15 16:14:19 -04002184
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002185 namelen = strlen(vol_args.name);
2186 if (namelen > BTRFS_VOL_NAME_MAX)
2187 return -EINVAL;
2188 if (strchr(vol_args.name, '/'))
2189 return -EINVAL;
2190
2191 path = btrfs_alloc_path();
2192 if (!path)
2193 return -ENOMEM;
2194
2195 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2196 mutex_lock(&root->fs_info->fs_mutex);
2197 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2198 path, root_dirid,
2199 vol_args.name, namelen, 0);
2200 mutex_unlock(&root->fs_info->fs_mutex);
2201 btrfs_free_path(path);
2202 if (di && !IS_ERR(di))
2203 return -EEXIST;
2204 if (IS_ERR(di))
2205 return PTR_ERR(di);
2206
2207 if (root == root->fs_info->tree_root)
2208 return create_subvol(root, vol_args.name, namelen);
2209 return create_snapshot(root, vol_args.name, namelen);
2210}
2211
2212static int btrfs_ioctl_defrag(struct file *file)
Chris Mason39279cc2007-06-12 06:35:45 -04002213{
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002214 struct inode *inode = file->f_path.dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002215 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002216
2217 switch (inode->i_mode & S_IFMT) {
2218 case S_IFDIR:
2219 mutex_lock(&root->fs_info->fs_mutex);
2220 btrfs_defrag_root(root, 0);
2221 btrfs_defrag_root(root->fs_info->extent_root, 0);
2222 mutex_unlock(&root->fs_info->fs_mutex);
2223 break;
2224 case S_IFREG:
2225 btrfs_defrag_file(file);
2226 break;
2227 }
2228
2229 return 0;
2230}
2231
2232long btrfs_ioctl(struct file *file, unsigned int
2233 cmd, unsigned long arg)
2234{
2235 struct btrfs_root *root = BTRFS_I(file->f_path.dentry->d_inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002236
2237 switch (cmd) {
2238 case BTRFS_IOC_SNAP_CREATE:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002239 return btrfs_ioctl_snap_create(root, (void __user *)arg);
Chris Mason6702ed42007-08-07 16:15:09 -04002240 case BTRFS_IOC_DEFRAG:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002241 return btrfs_ioctl_defrag(file);
Chris Mason39279cc2007-06-12 06:35:45 -04002242 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002243
2244 return -ENOTTY;
Chris Mason39279cc2007-06-12 06:35:45 -04002245}
2246
Chris Mason39279cc2007-06-12 06:35:45 -04002247/*
2248 * Called inside transaction, so use GFP_NOFS
2249 */
2250struct inode *btrfs_alloc_inode(struct super_block *sb)
2251{
2252 struct btrfs_inode *ei;
2253
2254 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2255 if (!ei)
2256 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002257 ei->last_trans = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002258 return &ei->vfs_inode;
2259}
2260
2261void btrfs_destroy_inode(struct inode *inode)
2262{
2263 WARN_ON(!list_empty(&inode->i_dentry));
2264 WARN_ON(inode->i_data.nrpages);
2265
2266 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2267}
2268
Chris Mason44ec0b72007-10-29 10:55:05 -04002269#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2270static void init_once(struct kmem_cache * cachep, void *foo)
2271#else
Chris Mason39279cc2007-06-12 06:35:45 -04002272static void init_once(void * foo, struct kmem_cache * cachep,
2273 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04002274#endif
Chris Mason39279cc2007-06-12 06:35:45 -04002275{
2276 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2277
2278 inode_init_once(&ei->vfs_inode);
2279}
2280
2281void btrfs_destroy_cachep(void)
2282{
2283 if (btrfs_inode_cachep)
2284 kmem_cache_destroy(btrfs_inode_cachep);
2285 if (btrfs_trans_handle_cachep)
2286 kmem_cache_destroy(btrfs_trans_handle_cachep);
2287 if (btrfs_transaction_cachep)
2288 kmem_cache_destroy(btrfs_transaction_cachep);
2289 if (btrfs_bit_radix_cachep)
2290 kmem_cache_destroy(btrfs_bit_radix_cachep);
2291 if (btrfs_path_cachep)
2292 kmem_cache_destroy(btrfs_path_cachep);
2293}
2294
Chris Mason86479a02007-09-10 19:58:16 -04002295struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04002296 unsigned long extra_flags,
Chris Mason44ec0b72007-10-29 10:55:05 -04002297#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2298 void (*ctor)(struct kmem_cache *, void *)
2299#else
Chris Mason92fee662007-07-25 12:31:35 -04002300 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04002301 unsigned long)
2302#endif
2303 )
Chris Mason92fee662007-07-25 12:31:35 -04002304{
2305 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2306 SLAB_MEM_SPREAD | extra_flags), ctor
2307#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2308 ,NULL
2309#endif
2310 );
2311}
2312
Chris Mason39279cc2007-06-12 06:35:45 -04002313int btrfs_init_cachep(void)
2314{
Chris Mason86479a02007-09-10 19:58:16 -04002315 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04002316 sizeof(struct btrfs_inode),
2317 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04002318 if (!btrfs_inode_cachep)
2319 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002320 btrfs_trans_handle_cachep =
2321 btrfs_cache_create("btrfs_trans_handle_cache",
2322 sizeof(struct btrfs_trans_handle),
2323 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002324 if (!btrfs_trans_handle_cachep)
2325 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002326 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04002327 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04002328 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002329 if (!btrfs_transaction_cachep)
2330 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002331 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04002332 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04002333 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002334 if (!btrfs_path_cachep)
2335 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002336 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04002337 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002338 if (!btrfs_bit_radix_cachep)
2339 goto fail;
2340 return 0;
2341fail:
2342 btrfs_destroy_cachep();
2343 return -ENOMEM;
2344}
2345
2346static int btrfs_getattr(struct vfsmount *mnt,
2347 struct dentry *dentry, struct kstat *stat)
2348{
2349 struct inode *inode = dentry->d_inode;
2350 generic_fillattr(inode, stat);
2351 stat->blksize = 256 * 1024;
2352 return 0;
2353}
2354
2355static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2356 struct inode * new_dir,struct dentry *new_dentry)
2357{
2358 struct btrfs_trans_handle *trans;
2359 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2360 struct inode *new_inode = new_dentry->d_inode;
2361 struct inode *old_inode = old_dentry->d_inode;
2362 struct timespec ctime = CURRENT_TIME;
2363 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002364 int ret;
2365
2366 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2367 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2368 return -ENOTEMPTY;
2369 }
Chris Mason5f39d392007-10-15 16:14:19 -04002370
Chris Mason39279cc2007-06-12 06:35:45 -04002371 mutex_lock(&root->fs_info->fs_mutex);
2372 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002373
Chris Mason39279cc2007-06-12 06:35:45 -04002374 btrfs_set_trans_block_group(trans, new_dir);
2375 path = btrfs_alloc_path();
2376 if (!path) {
2377 ret = -ENOMEM;
2378 goto out_fail;
2379 }
2380
2381 old_dentry->d_inode->i_nlink++;
2382 old_dir->i_ctime = old_dir->i_mtime = ctime;
2383 new_dir->i_ctime = new_dir->i_mtime = ctime;
2384 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04002385
Chris Mason39279cc2007-06-12 06:35:45 -04002386 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2387 if (ret)
2388 goto out_fail;
2389
2390 if (new_inode) {
2391 new_inode->i_ctime = CURRENT_TIME;
2392 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2393 if (ret)
2394 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002395 }
2396 ret = btrfs_add_link(trans, new_dentry, old_inode);
2397 if (ret)
2398 goto out_fail;
2399
2400out_fail:
2401 btrfs_free_path(path);
2402 btrfs_end_transaction(trans, root);
2403 mutex_unlock(&root->fs_info->fs_mutex);
2404 return ret;
2405}
2406
2407static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2408 const char *symname)
2409{
2410 struct btrfs_trans_handle *trans;
2411 struct btrfs_root *root = BTRFS_I(dir)->root;
2412 struct btrfs_path *path;
2413 struct btrfs_key key;
2414 struct inode *inode;
2415 int err;
2416 int drop_inode = 0;
2417 u64 objectid;
2418 int name_len;
2419 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04002420 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04002421 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04002422 struct extent_buffer *leaf;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002423 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002424
2425 name_len = strlen(symname) + 1;
2426 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2427 return -ENAMETOOLONG;
2428 mutex_lock(&root->fs_info->fs_mutex);
2429 trans = btrfs_start_transaction(root, 1);
2430 btrfs_set_trans_block_group(trans, dir);
2431
2432 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2433 if (err) {
2434 err = -ENOSPC;
2435 goto out_unlock;
2436 }
2437
2438 inode = btrfs_new_inode(trans, root, objectid,
2439 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2440 err = PTR_ERR(inode);
2441 if (IS_ERR(inode))
2442 goto out_unlock;
2443
2444 btrfs_set_trans_block_group(trans, inode);
2445 err = btrfs_add_nondir(trans, dentry, inode);
2446 if (err)
2447 drop_inode = 1;
2448 else {
2449 inode->i_mapping->a_ops = &btrfs_aops;
2450 inode->i_fop = &btrfs_file_operations;
2451 inode->i_op = &btrfs_file_inode_operations;
Chris Masona52d9a82007-08-27 16:49:44 -04002452 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
2453 inode->i_mapping, GFP_NOFS);
Chris Mason07157aa2007-08-30 08:50:51 -04002454 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002455 }
2456 dir->i_sb->s_dirt = 1;
2457 btrfs_update_inode_block_group(trans, inode);
2458 btrfs_update_inode_block_group(trans, dir);
2459 if (drop_inode)
2460 goto out_unlock;
2461
2462 path = btrfs_alloc_path();
2463 BUG_ON(!path);
2464 key.objectid = inode->i_ino;
2465 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002466 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2467 datasize = btrfs_file_extent_calc_inline_size(name_len);
2468 err = btrfs_insert_empty_item(trans, root, path, &key,
2469 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04002470 if (err) {
2471 drop_inode = 1;
2472 goto out_unlock;
2473 }
Chris Mason5f39d392007-10-15 16:14:19 -04002474 leaf = path->nodes[0];
2475 ei = btrfs_item_ptr(leaf, path->slots[0],
2476 struct btrfs_file_extent_item);
2477 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
2478 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04002479 BTRFS_FILE_EXTENT_INLINE);
2480 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04002481 write_extent_buffer(leaf, symname, ptr, name_len);
2482 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002483 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04002484
Chris Mason39279cc2007-06-12 06:35:45 -04002485 inode->i_op = &btrfs_symlink_inode_operations;
2486 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2487 inode->i_size = name_len - 1;
Chris Mason54aa1f42007-06-22 14:16:25 -04002488 err = btrfs_update_inode(trans, root, inode);
2489 if (err)
2490 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002491
2492out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002493 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04002494 btrfs_end_transaction(trans, root);
2495 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04002496 if (drop_inode) {
2497 inode_dec_link_count(inode);
2498 iput(inode);
2499 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002500 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002501 return err;
2502}
2503
2504static struct inode_operations btrfs_dir_inode_operations = {
2505 .lookup = btrfs_lookup,
2506 .create = btrfs_create,
2507 .unlink = btrfs_unlink,
2508 .link = btrfs_link,
2509 .mkdir = btrfs_mkdir,
2510 .rmdir = btrfs_rmdir,
2511 .rename = btrfs_rename,
2512 .symlink = btrfs_symlink,
2513 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04002514 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -05002515 .setxattr = generic_setxattr,
2516 .getxattr = generic_getxattr,
2517 .listxattr = btrfs_listxattr,
2518 .removexattr = generic_removexattr,
Chris Mason39279cc2007-06-12 06:35:45 -04002519};
2520
2521static struct inode_operations btrfs_dir_ro_inode_operations = {
2522 .lookup = btrfs_lookup,
2523};
2524
2525static struct file_operations btrfs_dir_file_operations = {
2526 .llseek = generic_file_llseek,
2527 .read = generic_read_dir,
2528 .readdir = btrfs_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002529 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002530#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002531 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002532#endif
2533};
2534
Chris Mason07157aa2007-08-30 08:50:51 -04002535static struct extent_map_ops btrfs_extent_map_ops = {
2536 .fill_delalloc = run_delalloc_range,
2537 .writepage_io_hook = btrfs_writepage_io_hook,
2538 .readpage_io_hook = btrfs_readpage_io_hook,
2539 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
2540};
2541
Chris Mason39279cc2007-06-12 06:35:45 -04002542static struct address_space_operations btrfs_aops = {
2543 .readpage = btrfs_readpage,
2544 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04002545 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05002546 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04002547 .sync_page = block_sync_page,
2548 .prepare_write = btrfs_prepare_write,
2549 .commit_write = btrfs_commit_write,
2550 .bmap = btrfs_bmap,
Chris Masona52d9a82007-08-27 16:49:44 -04002551 .invalidatepage = btrfs_invalidatepage,
2552 .releasepage = btrfs_releasepage,
2553 .set_page_dirty = __set_page_dirty_nobuffers,
Chris Mason39279cc2007-06-12 06:35:45 -04002554};
2555
2556static struct address_space_operations btrfs_symlink_aops = {
2557 .readpage = btrfs_readpage,
2558 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04002559 .invalidatepage = btrfs_invalidatepage,
2560 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04002561};
2562
2563static struct inode_operations btrfs_file_inode_operations = {
2564 .truncate = btrfs_truncate,
2565 .getattr = btrfs_getattr,
2566 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -05002567 .setxattr = generic_setxattr,
2568 .getxattr = generic_getxattr,
2569 .listxattr = btrfs_listxattr,
2570 .removexattr = generic_removexattr,
Chris Mason39279cc2007-06-12 06:35:45 -04002571};
2572
Josef Bacik618e21d2007-07-11 10:18:17 -04002573static struct inode_operations btrfs_special_inode_operations = {
2574 .getattr = btrfs_getattr,
2575 .setattr = btrfs_setattr,
2576};
2577
Chris Mason39279cc2007-06-12 06:35:45 -04002578static struct inode_operations btrfs_symlink_inode_operations = {
2579 .readlink = generic_readlink,
2580 .follow_link = page_follow_link_light,
2581 .put_link = page_put_link,
2582};