blob: 3c9236ca889c0aca801dc0213696b78fd5aba2bf [file] [log] [blame]
Chris Mason2e635a22007-03-21 11:12:56 -04001#include <linux/module.h>
Chris Masone20d96d2007-03-22 12:13:20 -04002#include <linux/buffer_head.h>
Chris Mason2e635a22007-03-21 11:12:56 -04003#include <linux/fs.h>
4#include <linux/pagemap.h>
5#include <linux/highmem.h>
6#include <linux/time.h>
7#include <linux/init.h>
8#include <linux/string.h>
9#include <linux/smp_lock.h>
10#include <linux/backing-dev.h>
Chris Masondee26a92007-03-26 16:00:06 -040011#include <linux/mpage.h>
Chris Mason75dfe392007-03-29 11:56:46 -040012#include <linux/swap.h>
13#include <linux/writeback.h>
Chris Mason2e635a22007-03-21 11:12:56 -040014#include "ctree.h"
Chris Masone20d96d2007-03-22 12:13:20 -040015#include "disk-io.h"
Chris Masond5719762007-03-23 10:01:08 -040016#include "transaction.h"
Chris Mason2c90e5d2007-04-02 10:50:19 -040017#include "btrfs_inode.h"
Chris Mason2e635a22007-03-21 11:12:56 -040018
Chris Masond6e4a422007-04-06 15:37:36 -040019void btrfs_fsinfo_release(struct kobject *obj)
20{
21 struct btrfs_fs_info *fsinfo = container_of(obj,
22 struct btrfs_fs_info, kobj);
23 kfree(fsinfo);
24}
25
26struct kobj_type btrfs_fsinfo_ktype = {
27 .release = btrfs_fsinfo_release,
28};
29
30decl_subsys(btrfs, &btrfs_fsinfo_ktype, NULL);
31
Chris Mason2e635a22007-03-21 11:12:56 -040032#define BTRFS_SUPER_MAGIC 0x9123682E
Chris Masone20d96d2007-03-22 12:13:20 -040033
34static struct inode_operations btrfs_dir_inode_operations;
Chris Masond6e4a422007-04-06 15:37:36 -040035static struct inode_operations btrfs_dir_ro_inode_operations;
Chris Masone20d96d2007-03-22 12:13:20 -040036static struct super_operations btrfs_super_ops;
37static struct file_operations btrfs_dir_file_operations;
Chris Masondee26a92007-03-26 16:00:06 -040038static struct inode_operations btrfs_file_inode_operations;
39static struct address_space_operations btrfs_aops;
40static struct file_operations btrfs_file_operations;
Chris Masone20d96d2007-03-22 12:13:20 -040041
Chris Mason2c90e5d2007-04-02 10:50:19 -040042static int check_inode(struct inode *inode)
43{
44 struct btrfs_inode *ei = BTRFS_I(inode);
45 WARN_ON(ei->magic != 0xDEADBEEF);
46 WARN_ON(ei->magic2 != 0xDEADBEAF);
47 return 0;
48}
49
Chris Masone20d96d2007-03-22 12:13:20 -040050static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason2e635a22007-03-21 11:12:56 -040051{
Chris Mason5caf2a02007-04-02 11:20:42 -040052 struct btrfs_path *path;
Chris Masone20d96d2007-03-22 12:13:20 -040053 struct btrfs_inode_item *inode_item;
Chris Masond6e4a422007-04-06 15:37:36 -040054 struct btrfs_root *root = BTRFS_I(inode)->root;
55 struct btrfs_key location;
Chris Masone20d96d2007-03-22 12:13:20 -040056 int ret;
Chris Masonf4b9aa82007-03-27 11:05:53 -040057
Chris Mason5caf2a02007-04-02 11:20:42 -040058 path = btrfs_alloc_path();
59 BUG_ON(!path);
60 btrfs_init_path(path);
Chris Masonf4b9aa82007-03-27 11:05:53 -040061 mutex_lock(&root->fs_info->fs_mutex);
62
Chris Mason2c90e5d2007-04-02 10:50:19 -040063 check_inode(inode);
Chris Masond6e4a422007-04-06 15:37:36 -040064
65 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
66 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Masone20d96d2007-03-22 12:13:20 -040067 if (ret) {
Chris Mason7cfcc172007-04-02 14:53:59 -040068 btrfs_free_path(path);
Chris Masond6e4a422007-04-06 15:37:36 -040069 goto make_bad;
Chris Mason2e635a22007-03-21 11:12:56 -040070 }
Chris Mason2c90e5d2007-04-02 10:50:19 -040071 check_inode(inode);
Chris Mason5caf2a02007-04-02 11:20:42 -040072 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
73 path->slots[0],
Chris Masone20d96d2007-03-22 12:13:20 -040074 struct btrfs_inode_item);
75
Chris Masone20d96d2007-03-22 12:13:20 -040076 inode->i_mode = btrfs_inode_mode(inode_item);
77 inode->i_nlink = btrfs_inode_nlink(inode_item);
78 inode->i_uid = btrfs_inode_uid(inode_item);
79 inode->i_gid = btrfs_inode_gid(inode_item);
80 inode->i_size = btrfs_inode_size(inode_item);
81 inode->i_atime.tv_sec = btrfs_timespec_sec(&inode_item->atime);
82 inode->i_atime.tv_nsec = btrfs_timespec_nsec(&inode_item->atime);
83 inode->i_mtime.tv_sec = btrfs_timespec_sec(&inode_item->mtime);
84 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(&inode_item->mtime);
85 inode->i_ctime.tv_sec = btrfs_timespec_sec(&inode_item->ctime);
86 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(&inode_item->ctime);
87 inode->i_blocks = btrfs_inode_nblocks(inode_item);
88 inode->i_generation = btrfs_inode_generation(inode_item);
Chris Mason5caf2a02007-04-02 11:20:42 -040089
Chris Mason5caf2a02007-04-02 11:20:42 -040090 btrfs_free_path(path);
91 inode_item = NULL;
92
Chris Masonf4b9aa82007-03-27 11:05:53 -040093 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -040094 check_inode(inode);
Chris Masone20d96d2007-03-22 12:13:20 -040095 switch (inode->i_mode & S_IFMT) {
96#if 0
97 default:
98 init_special_inode(inode, inode->i_mode,
99 btrfs_inode_rdev(inode_item));
100 break;
101#endif
102 case S_IFREG:
Chris Masondee26a92007-03-26 16:00:06 -0400103 inode->i_mapping->a_ops = &btrfs_aops;
104 inode->i_fop = &btrfs_file_operations;
105 inode->i_op = &btrfs_file_inode_operations;
Chris Masone20d96d2007-03-22 12:13:20 -0400106 break;
107 case S_IFDIR:
Chris Masone20d96d2007-03-22 12:13:20 -0400108 inode->i_fop = &btrfs_dir_file_operations;
Chris Masond6e4a422007-04-06 15:37:36 -0400109 if (root == root->fs_info->tree_root)
110 inode->i_op = &btrfs_dir_ro_inode_operations;
111 else
112 inode->i_op = &btrfs_dir_inode_operations;
Chris Masone20d96d2007-03-22 12:13:20 -0400113 break;
114 case S_IFLNK:
Chris Masone20d96d2007-03-22 12:13:20 -0400115 // inode->i_op = &page_symlink_inode_operations;
116 break;
117 }
Chris Mason2c90e5d2007-04-02 10:50:19 -0400118 check_inode(inode);
Chris Masone20d96d2007-03-22 12:13:20 -0400119 return;
Chris Masond6e4a422007-04-06 15:37:36 -0400120
121make_bad:
122 btrfs_release_path(root, path);
123 btrfs_free_path(path);
124 mutex_unlock(&root->fs_info->fs_mutex);
125 make_bad_inode(inode);
Chris Mason2e635a22007-03-21 11:12:56 -0400126}
127
Chris Mason5f443fd2007-03-27 13:42:32 -0400128static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
129 struct btrfs_root *root,
130 struct inode *dir,
131 struct dentry *dentry)
Chris Mason134e9732007-03-25 13:44:56 -0400132{
Chris Mason5caf2a02007-04-02 11:20:42 -0400133 struct btrfs_path *path;
Chris Mason134e9732007-03-25 13:44:56 -0400134 const char *name = dentry->d_name.name;
135 int name_len = dentry->d_name.len;
136 int ret;
137 u64 objectid;
138 struct btrfs_dir_item *di;
139
Chris Mason5caf2a02007-04-02 11:20:42 -0400140 path = btrfs_alloc_path();
141 BUG_ON(!path);
142 btrfs_init_path(path);
143 ret = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
Chris Mason134e9732007-03-25 13:44:56 -0400144 name, name_len, -1);
145 if (ret < 0)
146 goto err;
147 if (ret > 0) {
148 ret = -ENOENT;
149 goto err;
150 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400151 di = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Mason134e9732007-03-25 13:44:56 -0400152 struct btrfs_dir_item);
Chris Masond6e4a422007-04-06 15:37:36 -0400153 objectid = btrfs_disk_key_objectid(&di->location);
Chris Mason134e9732007-03-25 13:44:56 -0400154
Chris Mason5caf2a02007-04-02 11:20:42 -0400155 ret = btrfs_del_item(trans, root, path);
Chris Mason134e9732007-03-25 13:44:56 -0400156 BUG_ON(ret);
Chris Mason5f26f772007-04-05 10:38:44 -0400157
158 btrfs_release_path(root, path);
159 ret = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
160 objectid, -1);
161 BUG_ON(ret);
162 ret = btrfs_del_item(trans, root, path);
163 BUG_ON(ret);
Chris Mason134e9732007-03-25 13:44:56 -0400164 dentry->d_inode->i_ctime = dir->i_ctime;
165err:
Chris Mason5caf2a02007-04-02 11:20:42 -0400166 btrfs_release_path(root, path);
167 btrfs_free_path(path);
Chris Masond4dbff92007-04-04 14:08:15 -0400168 if (ret == 0) {
Chris Mason134e9732007-03-25 13:44:56 -0400169 inode_dec_link_count(dentry->d_inode);
Chris Mason5f26f772007-04-05 10:38:44 -0400170 dir->i_size -= name_len * 2;
Chris Masond4dbff92007-04-04 14:08:15 -0400171 mark_inode_dirty(dir);
172 }
Chris Mason134e9732007-03-25 13:44:56 -0400173 return ret;
174}
175
Chris Mason5f443fd2007-03-27 13:42:32 -0400176static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
177{
178 struct btrfs_root *root;
179 struct btrfs_trans_handle *trans;
180 int ret;
181
Chris Masond6e4a422007-04-06 15:37:36 -0400182 root = BTRFS_I(dir)->root;
Chris Mason5f443fd2007-03-27 13:42:32 -0400183 mutex_lock(&root->fs_info->fs_mutex);
184 trans = btrfs_start_transaction(root, 1);
185 ret = btrfs_unlink_trans(trans, root, dir, dentry);
186 btrfs_end_transaction(trans, root);
187 mutex_unlock(&root->fs_info->fs_mutex);
188 return ret;
189}
190
191static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
192{
193 struct inode *inode = dentry->d_inode;
194 int err;
195 int ret;
Chris Masond6e4a422007-04-06 15:37:36 -0400196 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason5caf2a02007-04-02 11:20:42 -0400197 struct btrfs_path *path;
Chris Mason5f443fd2007-03-27 13:42:32 -0400198 struct btrfs_key key;
199 struct btrfs_trans_handle *trans;
Chris Mason5f26f772007-04-05 10:38:44 -0400200 struct btrfs_key found_key;
201 int found_type;
Chris Mason5f443fd2007-03-27 13:42:32 -0400202 struct btrfs_leaf *leaf;
Chris Mason5f26f772007-04-05 10:38:44 -0400203 char *goodnames = "..";
Chris Mason5f443fd2007-03-27 13:42:32 -0400204
Chris Mason5caf2a02007-04-02 11:20:42 -0400205 path = btrfs_alloc_path();
206 BUG_ON(!path);
207 btrfs_init_path(path);
Chris Mason5f443fd2007-03-27 13:42:32 -0400208 mutex_lock(&root->fs_info->fs_mutex);
209 trans = btrfs_start_transaction(root, 1);
210 key.objectid = inode->i_ino;
211 key.offset = (u64)-1;
Chris Mason5f26f772007-04-05 10:38:44 -0400212 key.flags = (u32)-1;
213 while(1) {
214 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
215 if (ret < 0) {
216 err = ret;
217 goto out;
218 }
219 BUG_ON(ret == 0);
220 if (path->slots[0] == 0) {
221 err = -ENOENT;
222 goto out;
223 }
224 path->slots[0]--;
225 leaf = btrfs_buffer_leaf(path->nodes[0]);
226 btrfs_disk_key_to_cpu(&found_key,
227 &leaf->items[path->slots[0]].key);
228 found_type = btrfs_key_type(&found_key);
229 if (found_key.objectid != inode->i_ino) {
230 err = -ENOENT;
231 goto out;
232 }
233 if ((found_type != BTRFS_DIR_ITEM_KEY &&
234 found_type != BTRFS_DIR_INDEX_KEY) ||
235 (!btrfs_match_dir_item_name(root, path, goodnames, 2) &&
236 !btrfs_match_dir_item_name(root, path, goodnames, 1))) {
237 err = -ENOTEMPTY;
238 goto out;
239 }
240 ret = btrfs_del_item(trans, root, path);
241 BUG_ON(ret);
Chris Mason5f443fd2007-03-27 13:42:32 -0400242
Chris Mason5f26f772007-04-05 10:38:44 -0400243 if (found_type == BTRFS_DIR_ITEM_KEY && found_key.offset == 1)
244 break;
245 btrfs_release_path(root, path);
Chris Mason5f443fd2007-03-27 13:42:32 -0400246 }
Chris Mason5f26f772007-04-05 10:38:44 -0400247 ret = 0;
Chris Mason5caf2a02007-04-02 11:20:42 -0400248 btrfs_release_path(root, path);
Chris Mason5f443fd2007-03-27 13:42:32 -0400249
250 /* now the directory is empty */
251 err = btrfs_unlink_trans(trans, root, dir, dentry);
252 if (!err) {
253 inode->i_size = 0;
254 }
255out:
Chris Mason7cfcc172007-04-02 14:53:59 -0400256 btrfs_release_path(root, path);
257 btrfs_free_path(path);
Chris Mason5f443fd2007-03-27 13:42:32 -0400258 mutex_unlock(&root->fs_info->fs_mutex);
259 ret = btrfs_end_transaction(trans, root);
260 if (ret && !err)
261 err = ret;
262 return err;
263}
264
Chris Mason134e9732007-03-25 13:44:56 -0400265static int btrfs_free_inode(struct btrfs_trans_handle *trans,
266 struct btrfs_root *root,
267 struct inode *inode)
268{
269 u64 objectid = inode->i_ino;
Chris Mason5caf2a02007-04-02 11:20:42 -0400270 struct btrfs_path *path;
Chris Mason134e9732007-03-25 13:44:56 -0400271 struct btrfs_inode_map_item *map;
272 struct btrfs_key stat_data_key;
273 int ret;
Chris Mason5caf2a02007-04-02 11:20:42 -0400274
Chris Mason134e9732007-03-25 13:44:56 -0400275 clear_inode(inode);
Chris Mason5caf2a02007-04-02 11:20:42 -0400276
277 path = btrfs_alloc_path();
278 BUG_ON(!path);
279 btrfs_init_path(path);
280 ret = btrfs_lookup_inode_map(trans, root, path, objectid, -1);
Chris Mason134e9732007-03-25 13:44:56 -0400281 if (ret) {
282 if (ret > 0)
283 ret = -ENOENT;
Chris Mason134e9732007-03-25 13:44:56 -0400284 goto error;
285 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400286 map = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Mason134e9732007-03-25 13:44:56 -0400287 struct btrfs_inode_map_item);
288 btrfs_disk_key_to_cpu(&stat_data_key, &map->key);
Chris Mason5caf2a02007-04-02 11:20:42 -0400289 ret = btrfs_del_item(trans, root->fs_info->inode_root, path);
Chris Mason134e9732007-03-25 13:44:56 -0400290 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -0400291 btrfs_release_path(root, path);
Chris Mason134e9732007-03-25 13:44:56 -0400292
Chris Masond6e4a422007-04-06 15:37:36 -0400293 ret = btrfs_lookup_inode(trans, root, path,
294 &BTRFS_I(inode)->location, -1);
Chris Mason134e9732007-03-25 13:44:56 -0400295 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -0400296 ret = btrfs_del_item(trans, root, path);
Chris Mason134e9732007-03-25 13:44:56 -0400297 BUG_ON(ret);
Chris Mason134e9732007-03-25 13:44:56 -0400298error:
Chris Mason5caf2a02007-04-02 11:20:42 -0400299 btrfs_release_path(root, path);
300 btrfs_free_path(path);
Chris Mason134e9732007-03-25 13:44:56 -0400301 return ret;
302}
303
Chris Masonf4b9aa82007-03-27 11:05:53 -0400304static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
305 struct btrfs_root *root,
306 struct inode *inode)
307{
308 int ret;
Chris Mason5caf2a02007-04-02 11:20:42 -0400309 struct btrfs_path *path;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400310 struct btrfs_key key;
311 struct btrfs_disk_key *found_key;
312 struct btrfs_leaf *leaf;
Chris Masonf254e522007-03-29 15:15:27 -0400313 struct btrfs_file_extent_item *fi = NULL;
314 u64 extent_start = 0;
315 u64 extent_num_blocks = 0;
316 int found_extent;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400317
Chris Mason5caf2a02007-04-02 11:20:42 -0400318 path = btrfs_alloc_path();
319 BUG_ON(!path);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400320 /* FIXME, add redo link to tree so we don't leak on crash */
321 key.objectid = inode->i_ino;
322 key.offset = (u64)-1;
323 key.flags = 0;
Chris Masond4dbff92007-04-04 14:08:15 -0400324 /*
325 * use BTRFS_CSUM_ITEM_KEY because it is larger than inline keys
326 * or extent data
327 */
Chris Masonf254e522007-03-29 15:15:27 -0400328 btrfs_set_key_type(&key, BTRFS_CSUM_ITEM_KEY);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400329 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400330 btrfs_init_path(path);
331 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400332 if (ret < 0) {
Chris Masonf4b9aa82007-03-27 11:05:53 -0400333 goto error;
334 }
335 if (ret > 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400336 BUG_ON(path->slots[0] == 0);
337 path->slots[0]--;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400338 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400339 leaf = btrfs_buffer_leaf(path->nodes[0]);
340 found_key = &leaf->items[path->slots[0]].key;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400341 if (btrfs_disk_key_objectid(found_key) != inode->i_ino)
342 break;
Chris Masonf254e522007-03-29 15:15:27 -0400343 if (btrfs_disk_key_type(found_key) != BTRFS_CSUM_ITEM_KEY &&
Chris Masond4dbff92007-04-04 14:08:15 -0400344 btrfs_disk_key_type(found_key) != BTRFS_INLINE_DATA_KEY &&
Chris Masonf254e522007-03-29 15:15:27 -0400345 btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY)
Chris Masonf4b9aa82007-03-27 11:05:53 -0400346 break;
347 if (btrfs_disk_key_offset(found_key) < inode->i_size)
348 break;
Chris Masonf254e522007-03-29 15:15:27 -0400349 if (btrfs_disk_key_type(found_key) == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400350 fi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
351 path->slots[0],
Chris Masonf254e522007-03-29 15:15:27 -0400352 struct btrfs_file_extent_item);
353 extent_start = btrfs_file_extent_disk_blocknr(fi);
354 extent_num_blocks =
355 btrfs_file_extent_disk_num_blocks(fi);
356 inode->i_blocks -=
357 btrfs_file_extent_num_blocks(fi) >> 9;
358 found_extent = 1;
359 } else {
360 found_extent = 0;
361 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400362 ret = btrfs_del_item(trans, root, path);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400363 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -0400364 btrfs_release_path(root, path);
Chris Masonf254e522007-03-29 15:15:27 -0400365 if (found_extent) {
366 ret = btrfs_free_extent(trans, root, extent_start,
367 extent_num_blocks, 0);
368 BUG_ON(ret);
369 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400370 }
Chris Masonf4b9aa82007-03-27 11:05:53 -0400371 ret = 0;
372error:
Chris Mason5caf2a02007-04-02 11:20:42 -0400373 btrfs_release_path(root, path);
374 btrfs_free_path(path);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400375 return ret;
376}
377
Chris Mason134e9732007-03-25 13:44:56 -0400378static void btrfs_delete_inode(struct inode *inode)
379{
380 struct btrfs_trans_handle *trans;
Chris Masond6e4a422007-04-06 15:37:36 -0400381 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonf4b9aa82007-03-27 11:05:53 -0400382 int ret;
383
Chris Mason134e9732007-03-25 13:44:56 -0400384 truncate_inode_pages(&inode->i_data, 0);
385 if (is_bad_inode(inode)) {
386 goto no_delete;
387 }
388 inode->i_size = 0;
Chris Mason134e9732007-03-25 13:44:56 -0400389 mutex_lock(&root->fs_info->fs_mutex);
390 trans = btrfs_start_transaction(root, 1);
Chris Masonf4b9aa82007-03-27 11:05:53 -0400391 if (S_ISREG(inode->i_mode)) {
392 ret = btrfs_truncate_in_trans(trans, root, inode);
393 BUG_ON(ret);
394 }
Chris Mason134e9732007-03-25 13:44:56 -0400395 btrfs_free_inode(trans, root, inode);
396 btrfs_end_transaction(trans, root);
397 mutex_unlock(&root->fs_info->fs_mutex);
398 return;
399no_delete:
400 clear_inode(inode);
401}
402
Chris Masone20d96d2007-03-22 12:13:20 -0400403static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
Chris Masond6e4a422007-04-06 15:37:36 -0400404 struct btrfs_key *location)
Chris Masone20d96d2007-03-22 12:13:20 -0400405{
406 const char *name = dentry->d_name.name;
407 int namelen = dentry->d_name.len;
408 struct btrfs_dir_item *di;
Chris Mason5caf2a02007-04-02 11:20:42 -0400409 struct btrfs_path *path;
Chris Masond6e4a422007-04-06 15:37:36 -0400410 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Masone20d96d2007-03-22 12:13:20 -0400411 int ret;
412
Chris Mason5caf2a02007-04-02 11:20:42 -0400413 path = btrfs_alloc_path();
414 BUG_ON(!path);
415 btrfs_init_path(path);
416 ret = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
Chris Masone20d96d2007-03-22 12:13:20 -0400417 namelen, 0);
Chris Mason5caf2a02007-04-02 11:20:42 -0400418 if (ret || !btrfs_match_dir_item_name(root, path, name, namelen)) {
Chris Masond6e4a422007-04-06 15:37:36 -0400419 location->objectid = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400420 ret = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400421 goto out;
422 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400423 di = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Masone20d96d2007-03-22 12:13:20 -0400424 struct btrfs_dir_item);
Chris Masond6e4a422007-04-06 15:37:36 -0400425 btrfs_disk_key_to_cpu(location, &di->location);
Chris Masone20d96d2007-03-22 12:13:20 -0400426out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400427 btrfs_release_path(root, path);
428 btrfs_free_path(path);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400429 check_inode(dir);
Chris Masone20d96d2007-03-22 12:13:20 -0400430 return ret;
431}
432
Chris Masond6e4a422007-04-06 15:37:36 -0400433int fixup_tree_root_location(struct btrfs_root *root,
434 struct btrfs_key *location,
435 struct btrfs_root **sub_root)
436{
437 struct btrfs_path *path;
438 struct btrfs_root_item *ri;
Chris Masond6e4a422007-04-06 15:37:36 -0400439
440 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
441 return 0;
442 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
443 return 0;
444
445 path = btrfs_alloc_path();
446 BUG_ON(!path);
447 mutex_lock(&root->fs_info->fs_mutex);
448
Chris Mason0f7d52f2007-04-09 10:42:37 -0400449 *sub_root = btrfs_read_fs_root(root->fs_info, location);
450 if (IS_ERR(*sub_root))
451 return PTR_ERR(*sub_root);
452
453 ri = &(*sub_root)->root_item;
Chris Masond6e4a422007-04-06 15:37:36 -0400454 location->objectid = btrfs_root_dirid(ri);
455 location->flags = 0;
456 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
457 location->offset = 0;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400458
Chris Masond6e4a422007-04-06 15:37:36 -0400459 btrfs_free_path(path);
460 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason0f7d52f2007-04-09 10:42:37 -0400461 return 0;
Chris Masond6e4a422007-04-06 15:37:36 -0400462}
463
464
Chris Masone20d96d2007-03-22 12:13:20 -0400465static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
466 struct nameidata *nd)
467{
468 struct inode * inode;
Chris Masond6e4a422007-04-06 15:37:36 -0400469 struct btrfs_inode *bi = BTRFS_I(dir);
470 struct btrfs_root *root = bi->root;
471 struct btrfs_root *sub_root = root;
472 struct btrfs_key location;
Chris Masone20d96d2007-03-22 12:13:20 -0400473 int ret;
474
475 if (dentry->d_name.len > BTRFS_NAME_LEN)
476 return ERR_PTR(-ENAMETOOLONG);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400477 mutex_lock(&root->fs_info->fs_mutex);
Chris Masond6e4a422007-04-06 15:37:36 -0400478 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400479 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masone20d96d2007-03-22 12:13:20 -0400480 if (ret < 0)
481 return ERR_PTR(ret);
482 inode = NULL;
Chris Masond6e4a422007-04-06 15:37:36 -0400483 if (location.objectid) {
484 ret = fixup_tree_root_location(root, &location, &sub_root);
485 if (ret < 0)
486 return ERR_PTR(ret);
487 if (ret > 0)
488 return ERR_PTR(-ENOENT);
489 inode = iget_locked(dir->i_sb, location.objectid);
Chris Masone20d96d2007-03-22 12:13:20 -0400490 if (!inode)
491 return ERR_PTR(-EACCES);
Chris Masond6e4a422007-04-06 15:37:36 -0400492 if (inode->i_state & I_NEW) {
Chris Mason0f7d52f2007-04-09 10:42:37 -0400493 if (sub_root != root) {
494 ret = radix_tree_insert(
495 &root->fs_info->fs_roots_radix,
496 (unsigned long)sub_root,
497 sub_root);
498printk("adding new root for inode %lu\n", inode->i_ino);
499 igrab(inode);
500 sub_root->inode = inode;
501 }
Chris Masond6e4a422007-04-06 15:37:36 -0400502 BTRFS_I(inode)->root = sub_root;
503 memcpy(&BTRFS_I(inode)->location, &location,
504 sizeof(location));
505 btrfs_read_locked_inode(inode);
506 unlock_new_inode(inode);
507 }
Chris Mason2c90e5d2007-04-02 10:50:19 -0400508 check_inode(inode);
Chris Masone20d96d2007-03-22 12:13:20 -0400509 }
Chris Mason2c90e5d2007-04-02 10:50:19 -0400510 check_inode(dir);
Chris Masone20d96d2007-03-22 12:13:20 -0400511 return d_splice_alias(inode, dentry);
512}
513
514static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
515{
516 struct inode *inode = filp->f_path.dentry->d_inode;
Chris Masond6e4a422007-04-06 15:37:36 -0400517 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone20d96d2007-03-22 12:13:20 -0400518 struct btrfs_item *item;
519 struct btrfs_dir_item *di;
520 struct btrfs_key key;
Chris Mason5caf2a02007-04-02 11:20:42 -0400521 struct btrfs_path *path;
Chris Masone20d96d2007-03-22 12:13:20 -0400522 int ret;
523 u32 nritems;
524 struct btrfs_leaf *leaf;
525 int slot;
526 int advance;
527 unsigned char d_type = DT_UNKNOWN;
Chris Mason7f5c1512007-03-23 15:56:19 -0400528 int over = 0;
Chris Masond6e4a422007-04-06 15:37:36 -0400529 int key_type = BTRFS_DIR_INDEX_KEY;
530
531 /* FIXME, use a real flag for deciding about the key type */
532 if (root->fs_info->tree_root == root)
533 key_type = BTRFS_DIR_ITEM_KEY;
Chris Masone20d96d2007-03-22 12:13:20 -0400534
Chris Mason22b0ebd2007-03-30 08:47:31 -0400535 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone20d96d2007-03-22 12:13:20 -0400536 key.objectid = inode->i_ino;
Chris Masone20d96d2007-03-22 12:13:20 -0400537 key.flags = 0;
Chris Masond6e4a422007-04-06 15:37:36 -0400538 btrfs_set_key_type(&key, key_type);
Chris Masone20d96d2007-03-22 12:13:20 -0400539 key.offset = filp->f_pos;
Chris Mason5caf2a02007-04-02 11:20:42 -0400540 path = btrfs_alloc_path();
541 btrfs_init_path(path);
542 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400543 if (ret < 0) {
544 goto err;
545 }
Chris Mason7f5c1512007-03-23 15:56:19 -0400546 advance = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400547 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400548 leaf = btrfs_buffer_leaf(path->nodes[0]);
Chris Masone20d96d2007-03-22 12:13:20 -0400549 nritems = btrfs_header_nritems(&leaf->header);
Chris Mason5caf2a02007-04-02 11:20:42 -0400550 slot = path->slots[0];
Chris Masondee26a92007-03-26 16:00:06 -0400551 if (advance || slot >= nritems) {
552 if (slot >= nritems -1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400553 ret = btrfs_next_leaf(root, path);
Chris Masone20d96d2007-03-22 12:13:20 -0400554 if (ret)
555 break;
Chris Mason5caf2a02007-04-02 11:20:42 -0400556 leaf = btrfs_buffer_leaf(path->nodes[0]);
Chris Masone20d96d2007-03-22 12:13:20 -0400557 nritems = btrfs_header_nritems(&leaf->header);
Chris Mason5caf2a02007-04-02 11:20:42 -0400558 slot = path->slots[0];
Chris Masone20d96d2007-03-22 12:13:20 -0400559 } else {
560 slot++;
Chris Mason5caf2a02007-04-02 11:20:42 -0400561 path->slots[0]++;
Chris Masone20d96d2007-03-22 12:13:20 -0400562 }
563 }
564 advance = 1;
565 item = leaf->items + slot;
Chris Masone20d96d2007-03-22 12:13:20 -0400566 if (btrfs_disk_key_objectid(&item->key) != key.objectid)
567 break;
Chris Masond6e4a422007-04-06 15:37:36 -0400568 if (key_type == BTRFS_DIR_INDEX_KEY &&
569 btrfs_disk_key_offset(&item->key) >
Chris Mason3eb03142007-04-05 14:28:50 -0400570 root->fs_info->highest_inode)
Chris Mason5be6f7f2007-04-05 13:35:25 -0400571 break;
Chris Masond6e4a422007-04-06 15:37:36 -0400572 if (btrfs_disk_key_type(&item->key) != key_type)
Chris Masone20d96d2007-03-22 12:13:20 -0400573 continue;
Chris Mason7f5c1512007-03-23 15:56:19 -0400574 if (btrfs_disk_key_offset(&item->key) < filp->f_pos)
575 continue;
Chris Mason7fcde0e2007-04-05 12:13:21 -0400576 filp->f_pos = btrfs_disk_key_offset(&item->key);
Chris Masondee26a92007-03-26 16:00:06 -0400577 advance = 1;
Chris Masone20d96d2007-03-22 12:13:20 -0400578 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Chris Masone20d96d2007-03-22 12:13:20 -0400579 over = filldir(dirent, (const char *)(di + 1),
580 btrfs_dir_name_len(di),
581 btrfs_disk_key_offset(&item->key),
Chris Masond6e4a422007-04-06 15:37:36 -0400582 btrfs_disk_key_objectid(&di->location), d_type);
Chris Mason7fcde0e2007-04-05 12:13:21 -0400583 if (over)
584 goto nopos;
Chris Masone20d96d2007-03-22 12:13:20 -0400585 }
Chris Mason7fcde0e2007-04-05 12:13:21 -0400586 filp->f_pos++;
587nopos:
Chris Masone20d96d2007-03-22 12:13:20 -0400588 ret = 0;
589err:
Chris Mason5caf2a02007-04-02 11:20:42 -0400590 btrfs_release_path(root, path);
591 btrfs_free_path(path);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400592 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masone20d96d2007-03-22 12:13:20 -0400593 return ret;
594}
595
596static void btrfs_put_super (struct super_block * sb)
597{
598 struct btrfs_root *root = btrfs_sb(sb);
599 int ret;
600
601 ret = close_ctree(root);
602 if (ret) {
603 printk("close ctree returns %d\n", ret);
604 }
605 sb->s_fs_info = NULL;
606}
Chris Mason2e635a22007-03-21 11:12:56 -0400607
608static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
609{
610 struct inode * inode;
Chris Masone20d96d2007-03-22 12:13:20 -0400611 struct dentry * root_dentry;
612 struct btrfs_super_block *disk_super;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400613 struct btrfs_root *tree_root;
Chris Masond6e4a422007-04-06 15:37:36 -0400614 struct btrfs_inode *bi;
Chris Mason2e635a22007-03-21 11:12:56 -0400615
616 sb->s_maxbytes = MAX_LFS_FILESIZE;
Chris Mason2e635a22007-03-21 11:12:56 -0400617 sb->s_magic = BTRFS_SUPER_MAGIC;
Chris Masone20d96d2007-03-22 12:13:20 -0400618 sb->s_op = &btrfs_super_ops;
Chris Mason2e635a22007-03-21 11:12:56 -0400619 sb->s_time_gran = 1;
Chris Masone20d96d2007-03-22 12:13:20 -0400620
Chris Mason0f7d52f2007-04-09 10:42:37 -0400621 tree_root = open_ctree(sb);
Chris Masond98237b2007-03-28 13:57:48 -0400622
Chris Mason0f7d52f2007-04-09 10:42:37 -0400623 if (!tree_root) {
Chris Masone20d96d2007-03-22 12:13:20 -0400624 printk("btrfs: open_ctree failed\n");
625 return -EIO;
626 }
Chris Mason0f7d52f2007-04-09 10:42:37 -0400627 sb->s_fs_info = tree_root;
628 disk_super = tree_root->fs_info->disk_super;
Chris Masone20d96d2007-03-22 12:13:20 -0400629 printk("read in super total blocks %Lu root %Lu\n",
630 btrfs_super_total_blocks(disk_super),
631 btrfs_super_root_dir(disk_super));
632
633 inode = iget_locked(sb, btrfs_super_root_dir(disk_super));
Chris Masond6e4a422007-04-06 15:37:36 -0400634 bi = BTRFS_I(inode);
635 bi->location.objectid = inode->i_ino;
636 bi->location.offset = 0;
637 bi->location.flags = 0;
Chris Mason0f7d52f2007-04-09 10:42:37 -0400638 bi->root = tree_root;
Chris Masond6e4a422007-04-06 15:37:36 -0400639 btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY);
640
Chris Mason2e635a22007-03-21 11:12:56 -0400641 if (!inode)
642 return -ENOMEM;
Chris Masone20d96d2007-03-22 12:13:20 -0400643 if (inode->i_state & I_NEW) {
644 btrfs_read_locked_inode(inode);
645 unlock_new_inode(inode);
646 }
Chris Mason2e635a22007-03-21 11:12:56 -0400647
Chris Masone20d96d2007-03-22 12:13:20 -0400648 root_dentry = d_alloc_root(inode);
649 if (!root_dentry) {
Chris Mason2e635a22007-03-21 11:12:56 -0400650 iput(inode);
651 return -ENOMEM;
652 }
Chris Masone20d96d2007-03-22 12:13:20 -0400653 sb->s_root = root_dentry;
654
Chris Mason2e635a22007-03-21 11:12:56 -0400655 return 0;
656}
657
Chris Masond5719762007-03-23 10:01:08 -0400658static void fill_inode_item(struct btrfs_inode_item *item,
659 struct inode *inode)
660{
661 btrfs_set_inode_uid(item, inode->i_uid);
662 btrfs_set_inode_gid(item, inode->i_gid);
663 btrfs_set_inode_size(item, inode->i_size);
664 btrfs_set_inode_mode(item, inode->i_mode);
665 btrfs_set_inode_nlink(item, inode->i_nlink);
666 btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
667 btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec);
668 btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec);
669 btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec);
670 btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec);
671 btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
672 btrfs_set_inode_nblocks(item, inode->i_blocks);
673 btrfs_set_inode_generation(item, inode->i_generation);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400674 check_inode(inode);
Chris Masond5719762007-03-23 10:01:08 -0400675}
676
Chris Mason4730a4b2007-03-26 12:00:39 -0400677static int btrfs_update_inode(struct btrfs_trans_handle *trans,
678 struct btrfs_root *root,
679 struct inode *inode)
680{
681 struct btrfs_inode_item *inode_item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400682 struct btrfs_path *path;
Chris Mason4730a4b2007-03-26 12:00:39 -0400683 int ret;
684
Chris Mason5caf2a02007-04-02 11:20:42 -0400685 path = btrfs_alloc_path();
686 BUG_ON(!path);
687 btrfs_init_path(path);
Chris Mason4730a4b2007-03-26 12:00:39 -0400688
Chris Masond6e4a422007-04-06 15:37:36 -0400689 ret = btrfs_lookup_inode(trans, root, path,
690 &BTRFS_I(inode)->location, 1);
Chris Mason4730a4b2007-03-26 12:00:39 -0400691 if (ret) {
692 if (ret > 0)
693 ret = -ENOENT;
694 goto failed;
695 }
696
Chris Mason5caf2a02007-04-02 11:20:42 -0400697 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
698 path->slots[0],
Chris Mason4730a4b2007-03-26 12:00:39 -0400699 struct btrfs_inode_item);
700
701 fill_inode_item(inode_item, inode);
Chris Mason5caf2a02007-04-02 11:20:42 -0400702 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason4730a4b2007-03-26 12:00:39 -0400703failed:
Chris Mason5caf2a02007-04-02 11:20:42 -0400704 btrfs_release_path(root, path);
705 btrfs_free_path(path);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400706 check_inode(inode);
Chris Mason4730a4b2007-03-26 12:00:39 -0400707 return 0;
708}
709
710static int btrfs_write_inode(struct inode *inode, int wait)
711{
Chris Masond6e4a422007-04-06 15:37:36 -0400712 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason4730a4b2007-03-26 12:00:39 -0400713 struct btrfs_trans_handle *trans;
714 int ret;
715
716 mutex_lock(&root->fs_info->fs_mutex);
717 trans = btrfs_start_transaction(root, 1);
718 ret = btrfs_update_inode(trans, root, inode);
719 if (wait)
720 btrfs_commit_transaction(trans, root);
721 else
722 btrfs_end_transaction(trans, root);
723 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400724 check_inode(inode);
Chris Mason4730a4b2007-03-26 12:00:39 -0400725 return ret;
726}
727
Chris Masond5719762007-03-23 10:01:08 -0400728static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
729 struct inode *dir, int mode)
730{
731 struct inode *inode;
732 struct btrfs_inode_item inode_item;
Chris Masond6e4a422007-04-06 15:37:36 -0400733 struct btrfs_root *root = BTRFS_I(dir)->root;
734 struct btrfs_key *key;
Chris Masond5719762007-03-23 10:01:08 -0400735 int ret;
736 u64 objectid;
737
738 inode = new_inode(dir->i_sb);
739 if (!inode)
740 return ERR_PTR(-ENOMEM);
741
Chris Masond6e4a422007-04-06 15:37:36 -0400742 BTRFS_I(inode)->root = BTRFS_I(dir)->root;
743 key = &BTRFS_I(inode)->location;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400744 check_inode(inode);
Chris Masond5719762007-03-23 10:01:08 -0400745 ret = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
746 BUG_ON(ret);
747
748 inode->i_uid = current->fsuid;
749 inode->i_gid = current->fsgid;
750 inode->i_mode = mode;
751 inode->i_ino = objectid;
752 inode->i_blocks = 0;
753 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
754 fill_inode_item(&inode_item, inode);
755
Chris Masond6e4a422007-04-06 15:37:36 -0400756 key->objectid = objectid;
757 key->flags = 0;
758 key->offset = 0;
759 btrfs_set_key_type(key, BTRFS_INODE_ITEM_KEY);
760 ret = btrfs_insert_inode_map(trans, root, objectid, key);
Chris Masond5719762007-03-23 10:01:08 -0400761 BUG_ON(ret);
762
763 ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
764 BUG_ON(ret);
765
766 insert_inode_hash(inode);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400767 check_inode(inode);
768 check_inode(dir);
Chris Masond5719762007-03-23 10:01:08 -0400769 return inode;
770}
771
772static int btrfs_add_link(struct btrfs_trans_handle *trans,
773 struct dentry *dentry, struct inode *inode)
774{
775 int ret;
Chris Masond6e4a422007-04-06 15:37:36 -0400776 struct btrfs_key key;
777 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
778 key.objectid = inode->i_ino;
779 key.flags = 0;
780 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
781 key.offset = 0;
782
783 ret = btrfs_insert_dir_item(trans, root,
Chris Masond5719762007-03-23 10:01:08 -0400784 dentry->d_name.name, dentry->d_name.len,
785 dentry->d_parent->d_inode->i_ino,
Chris Masond6e4a422007-04-06 15:37:36 -0400786 &key, 0);
Chris Mason4730a4b2007-03-26 12:00:39 -0400787 if (ret == 0) {
Chris Mason5f26f772007-04-05 10:38:44 -0400788 dentry->d_parent->d_inode->i_size += dentry->d_name.len * 2;
Chris Masond6e4a422007-04-06 15:37:36 -0400789 ret = btrfs_update_inode(trans, root,
Chris Mason4730a4b2007-03-26 12:00:39 -0400790 dentry->d_parent->d_inode);
791 }
Chris Mason2c90e5d2007-04-02 10:50:19 -0400792 check_inode(inode);
793 check_inode(dentry->d_parent->d_inode);
Chris Masond5719762007-03-23 10:01:08 -0400794 return ret;
795}
796
797static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
798 struct dentry *dentry, struct inode *inode)
799{
800 int err = btrfs_add_link(trans, dentry, inode);
801 if (!err) {
802 d_instantiate(dentry, inode);
803 return 0;
804 }
Chris Mason2c90e5d2007-04-02 10:50:19 -0400805 if (err > 0)
806 err = -EEXIST;
807 check_inode(inode);
Chris Masond5719762007-03-23 10:01:08 -0400808 return err;
809}
810
811static int btrfs_create(struct inode *dir, struct dentry *dentry,
812 int mode, struct nameidata *nd)
813{
814 struct btrfs_trans_handle *trans;
Chris Masond6e4a422007-04-06 15:37:36 -0400815 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Masond5719762007-03-23 10:01:08 -0400816 struct inode *inode;
817 int err;
Chris Mason134e9732007-03-25 13:44:56 -0400818 int drop_inode = 0;
Chris Masond5719762007-03-23 10:01:08 -0400819
Chris Masond561c022007-03-23 19:47:49 -0400820 mutex_lock(&root->fs_info->fs_mutex);
Chris Masond5719762007-03-23 10:01:08 -0400821 trans = btrfs_start_transaction(root, 1);
822 inode = btrfs_new_inode(trans, dir, mode);
823 err = PTR_ERR(inode);
824 if (IS_ERR(inode))
Chris Masond561c022007-03-23 19:47:49 -0400825 goto out_unlock;
Chris Masond5719762007-03-23 10:01:08 -0400826 // FIXME mark the inode dirty
827 err = btrfs_add_nondir(trans, dentry, inode);
Chris Mason134e9732007-03-25 13:44:56 -0400828 if (err)
829 drop_inode = 1;
Chris Masondee26a92007-03-26 16:00:06 -0400830 else {
831 inode->i_mapping->a_ops = &btrfs_aops;
832 inode->i_fop = &btrfs_file_operations;
833 inode->i_op = &btrfs_file_inode_operations;
834 }
Chris Masond5719762007-03-23 10:01:08 -0400835 dir->i_sb->s_dirt = 1;
Chris Masond561c022007-03-23 19:47:49 -0400836out_unlock:
Chris Mason22b0ebd2007-03-30 08:47:31 -0400837 btrfs_end_transaction(trans, root);
Chris Masond561c022007-03-23 19:47:49 -0400838 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400839 check_inode(inode);
840 check_inode(dir);
841
Chris Mason134e9732007-03-25 13:44:56 -0400842 if (drop_inode) {
843 inode_dec_link_count(inode);
844 iput(inode);
845 }
Chris Masond5719762007-03-23 10:01:08 -0400846 return err;
847}
848
Chris Masonf7922032007-03-25 20:17:36 -0400849static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
850 struct inode *inode, struct inode *dir)
851{
Chris Masond6e4a422007-04-06 15:37:36 -0400852 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Masonf7922032007-03-25 20:17:36 -0400853 int ret;
854 char buf[2];
Chris Masond6e4a422007-04-06 15:37:36 -0400855 struct btrfs_key key;
856
Chris Masonf7922032007-03-25 20:17:36 -0400857 buf[0] = '.';
858 buf[1] = '.';
859
Chris Masond6e4a422007-04-06 15:37:36 -0400860 key.objectid = inode->i_ino;
861 key.offset = 0;
862 key.flags = 0;
863 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
864
Chris Masonf7922032007-03-25 20:17:36 -0400865 ret = btrfs_insert_dir_item(trans, root, buf, 1, inode->i_ino,
Chris Masond6e4a422007-04-06 15:37:36 -0400866 &key, 1);
Chris Masonf7922032007-03-25 20:17:36 -0400867 if (ret)
868 goto error;
Chris Masond6e4a422007-04-06 15:37:36 -0400869 key.objectid = dir->i_ino;
Chris Masonf7922032007-03-25 20:17:36 -0400870 ret = btrfs_insert_dir_item(trans, root, buf, 2, inode->i_ino,
Chris Masond6e4a422007-04-06 15:37:36 -0400871 &key, 1);
Chris Mason4730a4b2007-03-26 12:00:39 -0400872 if (ret)
873 goto error;
Chris Mason5f26f772007-04-05 10:38:44 -0400874 inode->i_size = 6;
Chris Mason4730a4b2007-03-26 12:00:39 -0400875 ret = btrfs_update_inode(trans, root, inode);
Chris Masonf7922032007-03-25 20:17:36 -0400876error:
877 return ret;
878}
879
880static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
881{
882 struct inode *inode;
883 struct btrfs_trans_handle *trans;
Chris Masond6e4a422007-04-06 15:37:36 -0400884 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Masonf7922032007-03-25 20:17:36 -0400885 int err = 0;
886 int drop_on_err = 0;
887
888 mutex_lock(&root->fs_info->fs_mutex);
889 trans = btrfs_start_transaction(root, 1);
890 if (IS_ERR(trans)) {
891 err = PTR_ERR(trans);
892 goto out_unlock;
893 }
894 inode = btrfs_new_inode(trans, dir, S_IFDIR | mode);
895 if (IS_ERR(inode)) {
896 err = PTR_ERR(inode);
897 goto out_fail;
898 }
899 drop_on_err = 1;
900 inode->i_op = &btrfs_dir_inode_operations;
901 inode->i_fop = &btrfs_dir_file_operations;
902
903 err = btrfs_make_empty_dir(trans, inode, dir);
904 if (err)
905 goto out_fail;
906 err = btrfs_add_link(trans, dentry, inode);
907 if (err)
908 goto out_fail;
909 d_instantiate(dentry, inode);
Chris Masonf7922032007-03-25 20:17:36 -0400910 drop_on_err = 0;
911
912out_fail:
913 btrfs_end_transaction(trans, root);
914out_unlock:
915 mutex_unlock(&root->fs_info->fs_mutex);
916 if (drop_on_err)
917 iput(inode);
918 return err;
919}
920
Chris Masond5719762007-03-23 10:01:08 -0400921static int btrfs_sync_fs(struct super_block *sb, int wait)
922{
923 struct btrfs_trans_handle *trans;
924 struct btrfs_root *root;
925 int ret;
Chris Masond98237b2007-03-28 13:57:48 -0400926 root = btrfs_sb(sb);
Chris Masondf2ce342007-03-23 11:00:45 -0400927
Chris Masond5719762007-03-23 10:01:08 -0400928 sb->s_dirt = 0;
Chris Masond561c022007-03-23 19:47:49 -0400929 if (!wait) {
Chris Mason7cfcc172007-04-02 14:53:59 -0400930 filemap_flush(root->fs_info->btree_inode->i_mapping);
Chris Masond561c022007-03-23 19:47:49 -0400931 return 0;
932 }
Chris Mason7cfcc172007-04-02 14:53:59 -0400933 filemap_write_and_wait(root->fs_info->btree_inode->i_mapping);
Chris Masond561c022007-03-23 19:47:49 -0400934 mutex_lock(&root->fs_info->fs_mutex);
Chris Masond5719762007-03-23 10:01:08 -0400935 trans = btrfs_start_transaction(root, 1);
936 ret = btrfs_commit_transaction(trans, root);
937 sb->s_dirt = 0;
938 BUG_ON(ret);
939printk("btrfs sync_fs\n");
Chris Masond561c022007-03-23 19:47:49 -0400940 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond5719762007-03-23 10:01:08 -0400941 return 0;
942}
943
Chris Masone8f05c42007-04-04 14:30:09 -0400944#if 0
Chris Masondf24a2b2007-04-04 09:36:31 -0400945static int btrfs_get_block_inline(struct inode *inode, sector_t iblock,
946 struct buffer_head *result, int create)
947{
948 struct btrfs_root *root = btrfs_sb(inode->i_sb);
949 struct btrfs_path *path;
950 struct btrfs_key key;
951 struct btrfs_leaf *leaf;
952 int num_bytes = result->b_size;
953 int item_size;
954 int ret;
955 u64 pos;
956 char *ptr;
957 int copy_size;
958 int err = 0;
959 char *safe_ptr;
960 char *data_ptr;
961
962 path = btrfs_alloc_path();
963 BUG_ON(!path);
964
965 WARN_ON(create);
966 if (create) {
967 return 0;
968 }
969 pos = iblock << inode->i_blkbits;
970 key.objectid = inode->i_ino;
971 key.flags = 0;
972 btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
973 ptr = kmap(result->b_page);
974 safe_ptr = ptr;
975 ptr += (pos & (PAGE_CACHE_SIZE -1));
976again:
977 key.offset = pos;
978 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
979 if (ret) {
980 if (ret < 0)
981 err = ret;
982 else
983 err = 0;
984 goto out;
985 }
986 leaf = btrfs_buffer_leaf(path->nodes[0]);
987 item_size = btrfs_item_size(leaf->items + path->slots[0]);
988 copy_size = min(num_bytes, item_size);
989 data_ptr = btrfs_item_ptr(leaf, path->slots[0], char);
990 WARN_ON(safe_ptr + PAGE_CACHE_SIZE < ptr + copy_size);
991 memcpy(ptr, data_ptr, copy_size);
992 pos += copy_size;
993 num_bytes -= copy_size;
994 WARN_ON(num_bytes < 0);
995 ptr += copy_size;
996 btrfs_release_path(root, path);
997 if (num_bytes != 0) {
998 if (pos >= i_size_read(inode))
999 memset(ptr, 0, num_bytes);
1000 else
1001 goto again;
1002 }
1003 set_buffer_uptodate(result);
1004 map_bh(result, inode->i_sb, 0);
1005 err = 0;
1006out:
1007 btrfs_free_path(path);
1008 kunmap(result->b_page);
1009 return err;
1010}
Chris Masone8f05c42007-04-04 14:30:09 -04001011#endif
Chris Masondf24a2b2007-04-04 09:36:31 -04001012
Chris Mason75dfe392007-03-29 11:56:46 -04001013static int btrfs_get_block_lock(struct inode *inode, sector_t iblock,
Chris Masondee26a92007-03-26 16:00:06 -04001014 struct buffer_head *result, int create)
1015{
1016 int ret;
1017 int err = 0;
1018 u64 blocknr;
1019 u64 extent_start = 0;
1020 u64 extent_end = 0;
1021 u64 objectid = inode->i_ino;
Chris Mason5caf2a02007-04-02 11:20:42 -04001022 struct btrfs_path *path;
Chris Masond6e4a422007-04-06 15:37:36 -04001023 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondee26a92007-03-26 16:00:06 -04001024 struct btrfs_trans_handle *trans = NULL;
1025 struct btrfs_file_extent_item *item;
1026 struct btrfs_leaf *leaf;
1027 struct btrfs_disk_key *found_key;
1028
Chris Mason5caf2a02007-04-02 11:20:42 -04001029 path = btrfs_alloc_path();
1030 BUG_ON(!path);
1031 btrfs_init_path(path);
Chris Masondee26a92007-03-26 16:00:06 -04001032 if (create)
1033 trans = btrfs_start_transaction(root, 1);
1034
Chris Mason5caf2a02007-04-02 11:20:42 -04001035 ret = btrfs_lookup_file_extent(trans, root, path,
Chris Mason9773a782007-03-27 11:26:26 -04001036 inode->i_ino,
1037 iblock << inode->i_blkbits, 0);
Chris Masondee26a92007-03-26 16:00:06 -04001038 if (ret < 0) {
Chris Masondee26a92007-03-26 16:00:06 -04001039 err = ret;
1040 goto out;
1041 }
1042
1043 if (ret != 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001044 if (path->slots[0] == 0) {
1045 btrfs_release_path(root, path);
Chris Masondee26a92007-03-26 16:00:06 -04001046 goto allocate;
1047 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001048 path->slots[0]--;
Chris Masondee26a92007-03-26 16:00:06 -04001049 }
1050
Chris Mason5caf2a02007-04-02 11:20:42 -04001051 item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Masondee26a92007-03-26 16:00:06 -04001052 struct btrfs_file_extent_item);
Chris Mason5caf2a02007-04-02 11:20:42 -04001053 leaf = btrfs_buffer_leaf(path->nodes[0]);
Chris Masondee26a92007-03-26 16:00:06 -04001054 blocknr = btrfs_file_extent_disk_blocknr(item);
1055 blocknr += btrfs_file_extent_offset(item);
1056
1057 /* exact match found, use it */
1058 if (ret == 0) {
1059 err = 0;
1060 map_bh(result, inode->i_sb, blocknr);
Chris Masondee26a92007-03-26 16:00:06 -04001061 goto out;
1062 }
1063
1064 /* are we inside the extent that was found? */
Chris Mason5caf2a02007-04-02 11:20:42 -04001065 found_key = &leaf->items[path->slots[0]].key;
Chris Masondee26a92007-03-26 16:00:06 -04001066 if (btrfs_disk_key_objectid(found_key) != objectid ||
1067 btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY) {
1068 extent_end = 0;
1069 extent_start = 0;
Chris Mason5caf2a02007-04-02 11:20:42 -04001070 btrfs_release_path(root, path);
Chris Masondee26a92007-03-26 16:00:06 -04001071 goto allocate;
1072 }
1073
Chris Mason5caf2a02007-04-02 11:20:42 -04001074 extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key);
Chris Mason9773a782007-03-27 11:26:26 -04001075 extent_start = extent_start >> inode->i_blkbits;
Chris Masondee26a92007-03-26 16:00:06 -04001076 extent_start += btrfs_file_extent_offset(item);
1077 extent_end = extent_start + btrfs_file_extent_num_blocks(item);
Chris Masondee26a92007-03-26 16:00:06 -04001078 if (iblock >= extent_start && iblock < extent_end) {
1079 err = 0;
1080 map_bh(result, inode->i_sb, blocknr + iblock - extent_start);
1081 goto out;
1082 }
1083allocate:
1084 /* ok, create a new extent */
1085 if (!create) {
1086 err = 0;
1087 goto out;
1088 }
Chris Mason9773a782007-03-27 11:26:26 -04001089 ret = btrfs_alloc_file_extent(trans, root, objectid,
1090 iblock << inode->i_blkbits,
Chris Masondee26a92007-03-26 16:00:06 -04001091 1, extent_end, &blocknr);
1092 if (ret) {
1093 err = ret;
1094 goto out;
1095 }
Chris Masonf4b9aa82007-03-27 11:05:53 -04001096 inode->i_blocks += inode->i_sb->s_blocksize >> 9;
1097 set_buffer_new(result);
Chris Masondee26a92007-03-26 16:00:06 -04001098 map_bh(result, inode->i_sb, blocknr);
1099
1100out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001101 btrfs_release_path(root, path);
1102 btrfs_free_path(path);
Chris Masondee26a92007-03-26 16:00:06 -04001103 if (trans)
1104 btrfs_end_transaction(trans, root);
Chris Mason75dfe392007-03-29 11:56:46 -04001105 return err;
1106}
1107
1108static int btrfs_get_block(struct inode *inode, sector_t iblock,
1109 struct buffer_head *result, int create)
1110{
1111 int err;
Chris Masond6e4a422007-04-06 15:37:36 -04001112 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason75dfe392007-03-29 11:56:46 -04001113 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone8f05c42007-04-04 14:30:09 -04001114 err = btrfs_get_block_lock(inode, iblock, result, create);
1115 // err = btrfs_get_block_inline(inode, iblock, result, create);
Chris Masondee26a92007-03-26 16:00:06 -04001116 mutex_unlock(&root->fs_info->fs_mutex);
1117 return err;
1118}
1119
1120static int btrfs_prepare_write(struct file *file, struct page *page,
1121 unsigned from, unsigned to)
1122{
Chris Mason6407bf62007-03-27 06:33:00 -04001123 return nobh_prepare_write(page, from, to, btrfs_get_block);
Chris Masondee26a92007-03-26 16:00:06 -04001124}
Chris Mason75dfe392007-03-29 11:56:46 -04001125static int btrfs_commit_write(struct file *file, struct page *page,
1126 unsigned from, unsigned to)
1127{
Chris Mason75dfe392007-03-29 11:56:46 -04001128 return nobh_commit_write(file, page, from, to);
1129}
Chris Masondee26a92007-03-26 16:00:06 -04001130
Chris Masond561c022007-03-23 19:47:49 -04001131static void btrfs_write_super(struct super_block *sb)
1132{
1133 btrfs_sync_fs(sb, 1);
1134}
1135
Chris Masondee26a92007-03-26 16:00:06 -04001136static int btrfs_readpage(struct file *file, struct page *page)
1137{
1138 return mpage_readpage(page, btrfs_get_block);
1139}
1140
1141static int btrfs_readpages(struct file *file, struct address_space *mapping,
1142 struct list_head *pages, unsigned nr_pages)
1143{
1144 return mpage_readpages(mapping, pages, nr_pages, btrfs_get_block);
1145}
1146
1147static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1148{
Chris Mason6407bf62007-03-27 06:33:00 -04001149 return nobh_writepage(page, btrfs_get_block, wbc);
Chris Masondee26a92007-03-26 16:00:06 -04001150}
Chris Masond561c022007-03-23 19:47:49 -04001151
Chris Masonf4b9aa82007-03-27 11:05:53 -04001152static void btrfs_truncate(struct inode *inode)
1153{
Chris Masond6e4a422007-04-06 15:37:36 -04001154 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonf4b9aa82007-03-27 11:05:53 -04001155 int ret;
1156 struct btrfs_trans_handle *trans;
1157
1158 if (!S_ISREG(inode->i_mode))
1159 return;
1160 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1161 return;
1162
Chris Masone8f05c42007-04-04 14:30:09 -04001163 nobh_truncate_page(inode->i_mapping, inode->i_size);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001164
1165 /* FIXME, add redo link to tree so we don't leak on crash */
1166 mutex_lock(&root->fs_info->fs_mutex);
1167 trans = btrfs_start_transaction(root, 1);
1168 ret = btrfs_truncate_in_trans(trans, root, inode);
1169 BUG_ON(ret);
1170 ret = btrfs_end_transaction(trans, root);
1171 BUG_ON(ret);
1172 mutex_unlock(&root->fs_info->fs_mutex);
1173 mark_inode_dirty(inode);
1174}
1175
Chris Mason75dfe392007-03-29 11:56:46 -04001176static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes,
1177 struct page **prepared_pages,
1178 const char __user * buf)
1179{
1180 long page_fault = 0;
1181 int i;
1182 int offset = pos & (PAGE_CACHE_SIZE - 1);
1183
1184 for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
1185 size_t count = min_t(size_t,
1186 PAGE_CACHE_SIZE - offset, write_bytes);
1187 struct page *page = prepared_pages[i];
1188 fault_in_pages_readable(buf, count);
1189
1190 /* Copy data from userspace to the current page */
1191 kmap(page);
1192 page_fault = __copy_from_user(page_address(page) + offset,
1193 buf, count);
1194 /* Flush processor's dcache for this page */
1195 flush_dcache_page(page);
1196 kunmap(page);
1197 buf += count;
1198 write_bytes -= count;
1199
1200 if (page_fault)
1201 break;
1202 }
1203 return page_fault ? -EFAULT : 0;
1204}
1205
1206static void btrfs_drop_pages(struct page **pages, size_t num_pages)
1207{
1208 size_t i;
1209 for (i = 0; i < num_pages; i++) {
1210 if (!pages[i])
1211 break;
1212 unlock_page(pages[i]);
1213 mark_page_accessed(pages[i]);
1214 page_cache_release(pages[i]);
1215 }
1216}
1217static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
1218 struct btrfs_root *root,
1219 struct file *file,
1220 struct page **pages,
1221 size_t num_pages,
1222 loff_t pos,
1223 size_t write_bytes)
1224{
1225 int i;
1226 int offset;
1227 int err = 0;
1228 int ret;
1229 int this_write;
Chris Masonf254e522007-03-29 15:15:27 -04001230 struct inode *inode = file->f_path.dentry->d_inode;
Chris Mason75dfe392007-03-29 11:56:46 -04001231
1232 for (i = 0; i < num_pages; i++) {
1233 offset = pos & (PAGE_CACHE_SIZE -1);
1234 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
Chris Masonf254e522007-03-29 15:15:27 -04001235 /* FIXME, one block at a time */
1236
1237 mutex_lock(&root->fs_info->fs_mutex);
1238 trans = btrfs_start_transaction(root, 1);
1239 btrfs_csum_file_block(trans, root, inode->i_ino,
1240 pages[i]->index << PAGE_CACHE_SHIFT,
1241 kmap(pages[i]), PAGE_CACHE_SIZE);
1242 kunmap(pages[i]);
1243 SetPageChecked(pages[i]);
1244 ret = btrfs_end_transaction(trans, root);
1245 BUG_ON(ret);
1246 mutex_unlock(&root->fs_info->fs_mutex);
1247
Chris Mason75dfe392007-03-29 11:56:46 -04001248 ret = nobh_commit_write(file, pages[i], offset,
1249 offset + this_write);
1250 pos += this_write;
1251 if (ret) {
1252 err = ret;
1253 goto failed;
1254 }
1255 WARN_ON(this_write > write_bytes);
1256 write_bytes -= this_write;
1257 }
1258failed:
1259 return err;
1260}
1261
1262static int prepare_pages(struct btrfs_trans_handle *trans,
1263 struct btrfs_root *root,
1264 struct file *file,
1265 struct page **pages,
1266 size_t num_pages,
1267 loff_t pos,
1268 size_t write_bytes)
1269{
1270 int i;
1271 unsigned long index = pos >> PAGE_CACHE_SHIFT;
1272 struct inode *inode = file->f_path.dentry->d_inode;
1273 int offset;
1274 int err = 0;
1275 int ret;
1276 int this_write;
1277 loff_t isize = i_size_read(inode);
1278
1279 memset(pages, 0, num_pages * sizeof(struct page *));
1280
1281 for (i = 0; i < num_pages; i++) {
1282 pages[i] = grab_cache_page(inode->i_mapping, index + i);
1283 if (!pages[i]) {
1284 err = -ENOMEM;
1285 goto failed_release;
1286 }
1287 offset = pos & (PAGE_CACHE_SIZE -1);
1288 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
1289 ret = nobh_prepare_write(pages[i], offset,
1290 offset + this_write,
Chris Masonf254e522007-03-29 15:15:27 -04001291 btrfs_get_block);
Chris Mason75dfe392007-03-29 11:56:46 -04001292 pos += this_write;
1293 if (ret) {
1294 err = ret;
1295 goto failed_truncate;
1296 }
1297 WARN_ON(this_write > write_bytes);
1298 write_bytes -= this_write;
1299 }
1300 return 0;
1301
1302failed_release:
1303 btrfs_drop_pages(pages, num_pages);
1304 return err;
1305
1306failed_truncate:
1307 btrfs_drop_pages(pages, num_pages);
1308 if (pos > isize)
1309 vmtruncate(inode, isize);
1310 return err;
1311}
1312
1313static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
1314 size_t count, loff_t *ppos)
1315{
1316 loff_t pos;
1317 size_t num_written = 0;
1318 int err = 0;
1319 int ret = 0;
Chris Mason75dfe392007-03-29 11:56:46 -04001320 struct inode *inode = file->f_path.dentry->d_inode;
Chris Masond6e4a422007-04-06 15:37:36 -04001321 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason75dfe392007-03-29 11:56:46 -04001322 struct page *pages[1];
1323
1324 if (file->f_flags & O_DIRECT)
1325 return -EINVAL;
1326 pos = *ppos;
1327
1328 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
1329 current->backing_dev_info = inode->i_mapping->backing_dev_info;
1330 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1331 if (err)
1332 goto out;
1333 if (count == 0)
1334 goto out;
1335 err = remove_suid(file->f_path.dentry);
1336 if (err)
1337 goto out;
1338 file_update_time(file);
1339 mutex_lock(&inode->i_mutex);
1340 while(count > 0) {
1341 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
1342 size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
1343 size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
1344 PAGE_CACHE_SHIFT;
Chris Masonf254e522007-03-29 15:15:27 -04001345 ret = prepare_pages(NULL, root, file, pages, num_pages,
Chris Mason75dfe392007-03-29 11:56:46 -04001346 pos, write_bytes);
1347 BUG_ON(ret);
1348 ret = btrfs_copy_from_user(pos, num_pages,
1349 write_bytes, pages, buf);
1350 BUG_ON(ret);
1351
Chris Masonf254e522007-03-29 15:15:27 -04001352 ret = dirty_and_release_pages(NULL, root, file, pages,
Chris Mason75dfe392007-03-29 11:56:46 -04001353 num_pages, pos, write_bytes);
1354 BUG_ON(ret);
1355 btrfs_drop_pages(pages, num_pages);
1356
Chris Mason75dfe392007-03-29 11:56:46 -04001357 buf += write_bytes;
1358 count -= write_bytes;
1359 pos += write_bytes;
1360 num_written += write_bytes;
1361
1362 balance_dirty_pages_ratelimited(inode->i_mapping);
1363 cond_resched();
1364 }
1365 mutex_unlock(&inode->i_mutex);
1366out:
1367 *ppos = pos;
1368 current->backing_dev_info = NULL;
1369 return num_written ? num_written : err;
1370}
1371
Chris Masone8f05c42007-04-04 14:30:09 -04001372#if 0
Chris Masondf24a2b2007-04-04 09:36:31 -04001373static ssize_t inline_one_page(struct btrfs_root *root, struct inode *inode,
1374 struct page *page, loff_t pos,
1375 size_t offset, size_t write_bytes)
1376{
1377 struct btrfs_path *path;
1378 struct btrfs_trans_handle *trans;
1379 struct btrfs_key key;
1380 struct btrfs_leaf *leaf;
1381 struct btrfs_key found_key;
1382 int ret;
1383 size_t copy_size = 0;
1384 char *dst = NULL;
1385 int err = 0;
1386 size_t num_written = 0;
1387
1388 path = btrfs_alloc_path();
1389 BUG_ON(!path);
1390 mutex_lock(&root->fs_info->fs_mutex);
1391 trans = btrfs_start_transaction(root, 1);
1392 key.objectid = inode->i_ino;
1393 key.flags = 0;
1394 btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
1395
1396again:
1397 key.offset = pos;
1398 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1399 if (ret < 0) {
1400 err = ret;
1401 goto out;
1402 }
1403 if (ret == 0) {
1404 leaf = btrfs_buffer_leaf(path->nodes[0]);
1405 btrfs_disk_key_to_cpu(&found_key,
1406 &leaf->items[path->slots[0]].key);
1407 copy_size = btrfs_item_size(leaf->items + path->slots[0]);
1408 dst = btrfs_item_ptr(leaf, path->slots[0], char);
1409 copy_size = min(write_bytes, copy_size);
1410 goto copyit;
1411 } else {
1412 int slot = path->slots[0];
1413 if (slot > 0) {
1414 slot--;
1415 }
1416 // FIXME find max key
1417 leaf = btrfs_buffer_leaf(path->nodes[0]);
1418 btrfs_disk_key_to_cpu(&found_key,
1419 &leaf->items[slot].key);
1420 if (found_key.objectid != inode->i_ino)
1421 goto insert;
1422 if (btrfs_key_type(&found_key) != BTRFS_INLINE_DATA_KEY)
1423 goto insert;
1424 copy_size = btrfs_item_size(leaf->items + slot);
1425 if (found_key.offset + copy_size <= pos)
1426 goto insert;
1427 dst = btrfs_item_ptr(leaf, path->slots[0], char);
1428 dst += pos - found_key.offset;
1429 copy_size = copy_size - (pos - found_key.offset);
1430 BUG_ON(copy_size < 0);
1431 copy_size = min(write_bytes, copy_size);
1432 WARN_ON(copy_size == 0);
1433 goto copyit;
1434 }
1435insert:
1436 btrfs_release_path(root, path);
Chris Masond4dbff92007-04-04 14:08:15 -04001437 copy_size = min(write_bytes,
1438 (size_t)BTRFS_LEAF_DATA_SIZE(root) -
1439 sizeof(struct btrfs_item) * 4);
Chris Masondf24a2b2007-04-04 09:36:31 -04001440 ret = btrfs_insert_empty_item(trans, root, path, &key, copy_size);
1441 BUG_ON(ret);
1442 dst = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1443 path->slots[0], char);
1444copyit:
1445 WARN_ON(copy_size == 0);
1446 WARN_ON(dst + copy_size >
1447 btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1448 path->slots[0], char) +
1449 btrfs_item_size(btrfs_buffer_leaf(path->nodes[0])->items +
1450 path->slots[0]));
1451 btrfs_memcpy(root, path->nodes[0]->b_data, dst,
1452 page_address(page) + offset, copy_size);
1453 mark_buffer_dirty(path->nodes[0]);
1454 btrfs_release_path(root, path);
1455 pos += copy_size;
1456 offset += copy_size;
1457 num_written += copy_size;
1458 write_bytes -= copy_size;
1459 if (write_bytes)
1460 goto again;
1461out:
1462 btrfs_free_path(path);
1463 ret = btrfs_end_transaction(trans, root);
1464 BUG_ON(ret);
1465 mutex_unlock(&root->fs_info->fs_mutex);
1466 return num_written ? num_written : err;
1467}
1468
1469static ssize_t btrfs_file_inline_write(struct file *file,
1470 const char __user *buf,
1471 size_t count, loff_t *ppos)
1472{
1473 loff_t pos;
1474 size_t num_written = 0;
1475 int err = 0;
1476 int ret = 0;
1477 struct inode *inode = file->f_path.dentry->d_inode;
Chris Masond6e4a422007-04-06 15:37:36 -04001478 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondf24a2b2007-04-04 09:36:31 -04001479 unsigned long page_index;
1480
1481 if (file->f_flags & O_DIRECT)
1482 return -EINVAL;
1483 pos = *ppos;
1484
1485 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
1486 current->backing_dev_info = inode->i_mapping->backing_dev_info;
1487 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1488 if (err)
1489 goto out;
1490 if (count == 0)
1491 goto out;
1492 err = remove_suid(file->f_path.dentry);
1493 if (err)
1494 goto out;
1495 file_update_time(file);
1496 mutex_lock(&inode->i_mutex);
1497 while(count > 0) {
1498 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
1499 size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
1500 struct page *page;
1501
1502 page_index = pos >> PAGE_CACHE_SHIFT;
1503 page = grab_cache_page(inode->i_mapping, page_index);
1504 if (!PageUptodate(page)) {
1505 ret = mpage_readpage(page, btrfs_get_block);
1506 BUG_ON(ret);
1507 lock_page(page);
1508 }
1509 ret = btrfs_copy_from_user(pos, 1,
1510 write_bytes, &page, buf);
1511 BUG_ON(ret);
1512 write_bytes = inline_one_page(root, inode, page, pos,
1513 offset, write_bytes);
1514 SetPageUptodate(page);
1515 if (write_bytes > 0 && pos + write_bytes > inode->i_size) {
1516 i_size_write(inode, pos + write_bytes);
1517 mark_inode_dirty(inode);
1518 }
1519 page_cache_release(page);
1520 unlock_page(page);
1521 if (write_bytes < 0)
1522 goto out_unlock;
1523 buf += write_bytes;
1524 count -= write_bytes;
1525 pos += write_bytes;
1526 num_written += write_bytes;
1527
1528 balance_dirty_pages_ratelimited(inode->i_mapping);
1529 cond_resched();
1530 }
1531out_unlock:
1532 mutex_unlock(&inode->i_mutex);
1533out:
1534 *ppos = pos;
1535 current->backing_dev_info = NULL;
1536 return num_written ? num_written : err;
1537}
Chris Masone8f05c42007-04-04 14:30:09 -04001538#endif
Chris Masondf24a2b2007-04-04 09:36:31 -04001539
Chris Masonf254e522007-03-29 15:15:27 -04001540static int btrfs_read_actor(read_descriptor_t *desc, struct page *page,
1541 unsigned long offset, unsigned long size)
1542{
1543 char *kaddr;
1544 unsigned long left, count = desc->count;
Chris Masond6e4a422007-04-06 15:37:36 -04001545 struct inode *inode = page->mapping->host;
Chris Masonf254e522007-03-29 15:15:27 -04001546
1547 if (size > count)
1548 size = count;
1549
1550 if (!PageChecked(page)) {
1551 /* FIXME, do it per block */
Chris Masond6e4a422007-04-06 15:37:36 -04001552 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonf254e522007-03-29 15:15:27 -04001553 int ret = btrfs_csum_verify_file_block(root,
1554 page->mapping->host->i_ino,
1555 page->index << PAGE_CACHE_SHIFT,
1556 kmap(page), PAGE_CACHE_SIZE);
1557 if (ret) {
1558 printk("failed to verify ino %lu page %lu\n",
1559 page->mapping->host->i_ino,
1560 page->index);
1561 memset(page_address(page), 0, PAGE_CACHE_SIZE);
1562 }
1563 SetPageChecked(page);
1564 kunmap(page);
1565 }
1566 /*
1567 * Faults on the destination of a read are common, so do it before
1568 * taking the kmap.
1569 */
1570 if (!fault_in_pages_writeable(desc->arg.buf, size)) {
1571 kaddr = kmap_atomic(page, KM_USER0);
1572 left = __copy_to_user_inatomic(desc->arg.buf,
1573 kaddr + offset, size);
1574 kunmap_atomic(kaddr, KM_USER0);
1575 if (left == 0)
1576 goto success;
1577 }
1578
1579 /* Do it the slow way */
1580 kaddr = kmap(page);
1581 left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
1582 kunmap(page);
1583
1584 if (left) {
1585 size -= left;
1586 desc->error = -EFAULT;
1587 }
1588success:
1589 desc->count = count - size;
1590 desc->written += size;
1591 desc->arg.buf += size;
1592 return size;
1593}
1594
1595/**
1596 * btrfs_file_aio_read - filesystem read routine
1597 * @iocb: kernel I/O control block
1598 * @iov: io vector request
1599 * @nr_segs: number of segments in the iovec
1600 * @pos: current file position
1601 */
1602static ssize_t btrfs_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
1603 unsigned long nr_segs, loff_t pos)
1604{
1605 struct file *filp = iocb->ki_filp;
1606 ssize_t retval;
1607 unsigned long seg;
1608 size_t count;
1609 loff_t *ppos = &iocb->ki_pos;
1610
1611 count = 0;
1612 for (seg = 0; seg < nr_segs; seg++) {
1613 const struct iovec *iv = &iov[seg];
1614
1615 /*
1616 * If any segment has a negative length, or the cumulative
1617 * length ever wraps negative then return -EINVAL.
1618 */
1619 count += iv->iov_len;
1620 if (unlikely((ssize_t)(count|iv->iov_len) < 0))
1621 return -EINVAL;
1622 if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
1623 continue;
1624 if (seg == 0)
1625 return -EFAULT;
1626 nr_segs = seg;
1627 count -= iv->iov_len; /* This segment is no good */
1628 break;
1629 }
1630 retval = 0;
1631 if (count) {
1632 for (seg = 0; seg < nr_segs; seg++) {
1633 read_descriptor_t desc;
1634
1635 desc.written = 0;
1636 desc.arg.buf = iov[seg].iov_base;
1637 desc.count = iov[seg].iov_len;
1638 if (desc.count == 0)
1639 continue;
1640 desc.error = 0;
1641 do_generic_file_read(filp, ppos, &desc,
1642 btrfs_read_actor);
1643 retval += desc.written;
1644 if (desc.error) {
1645 retval = retval ?: desc.error;
1646 break;
1647 }
1648 }
1649 }
1650 return retval;
1651}
1652
Chris Mason2c90e5d2007-04-02 10:50:19 -04001653static struct kmem_cache *btrfs_inode_cachep;
1654struct kmem_cache *btrfs_trans_handle_cachep;
1655struct kmem_cache *btrfs_transaction_cachep;
1656struct kmem_cache *btrfs_bit_radix_cachep;
1657struct kmem_cache *btrfs_path_cachep;
1658
1659/*
1660 * Called inside transaction, so use GFP_NOFS
1661 */
1662static struct inode *btrfs_alloc_inode(struct super_block *sb)
1663{
1664 struct btrfs_inode *ei;
1665
1666 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
1667 if (!ei)
1668 return NULL;
1669 ei->magic = 0xDEADBEEF;
1670 ei->magic2 = 0xDEADBEAF;
1671 return &ei->vfs_inode;
1672}
1673
1674static void btrfs_destroy_inode(struct inode *inode)
1675{
1676 struct btrfs_inode *ei = BTRFS_I(inode);
1677 WARN_ON(ei->magic != 0xDEADBEEF);
1678 WARN_ON(ei->magic2 != 0xDEADBEAF);
1679 WARN_ON(!list_empty(&inode->i_dentry));
Chris Mason2c90e5d2007-04-02 10:50:19 -04001680 WARN_ON(inode->i_data.nrpages);
1681
1682 ei->magic = 0;
1683 ei->magic2 = 0;
1684 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
1685}
1686
1687static void init_once(void * foo, struct kmem_cache * cachep,
1688 unsigned long flags)
1689{
1690 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
1691
1692 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
1693 SLAB_CTOR_CONSTRUCTOR) {
1694 inode_init_once(&ei->vfs_inode);
1695 }
1696}
1697
1698static int init_inodecache(void)
1699{
1700 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
1701 sizeof(struct btrfs_inode),
1702 0, (SLAB_RECLAIM_ACCOUNT|
1703 SLAB_MEM_SPREAD),
1704 init_once, NULL);
1705 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
1706 sizeof(struct btrfs_trans_handle),
1707 0, (SLAB_RECLAIM_ACCOUNT|
1708 SLAB_MEM_SPREAD),
1709 NULL, NULL);
1710 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
1711 sizeof(struct btrfs_transaction),
1712 0, (SLAB_RECLAIM_ACCOUNT|
1713 SLAB_MEM_SPREAD),
1714 NULL, NULL);
1715 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
1716 sizeof(struct btrfs_transaction),
1717 0, (SLAB_RECLAIM_ACCOUNT|
1718 SLAB_MEM_SPREAD),
1719 NULL, NULL);
1720 btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix",
1721 256,
1722 0, (SLAB_RECLAIM_ACCOUNT|
1723 SLAB_MEM_SPREAD |
1724 SLAB_DESTROY_BY_RCU),
1725 NULL, NULL);
1726 if (btrfs_inode_cachep == NULL || btrfs_trans_handle_cachep == NULL ||
1727 btrfs_transaction_cachep == NULL || btrfs_bit_radix_cachep == NULL)
1728 return -ENOMEM;
1729 return 0;
1730}
1731
1732static void destroy_inodecache(void)
1733{
1734 kmem_cache_destroy(btrfs_inode_cachep);
1735 kmem_cache_destroy(btrfs_trans_handle_cachep);
1736 kmem_cache_destroy(btrfs_transaction_cachep);
1737 kmem_cache_destroy(btrfs_bit_radix_cachep);
1738 kmem_cache_destroy(btrfs_path_cachep);
1739}
1740
Chris Mason2e635a22007-03-21 11:12:56 -04001741static int btrfs_get_sb(struct file_system_type *fs_type,
1742 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
1743{
1744 return get_sb_bdev(fs_type, flags, dev_name, data,
1745 btrfs_fill_super, mnt);
1746}
1747
1748static struct file_system_type btrfs_fs_type = {
1749 .owner = THIS_MODULE,
1750 .name = "btrfs",
1751 .get_sb = btrfs_get_sb,
1752 .kill_sb = kill_block_super,
1753 .fs_flags = FS_REQUIRES_DEV,
1754};
1755
Chris Masone20d96d2007-03-22 12:13:20 -04001756static struct super_operations btrfs_super_ops = {
1757 .statfs = simple_statfs,
Chris Mason134e9732007-03-25 13:44:56 -04001758 .delete_inode = btrfs_delete_inode,
Chris Masone20d96d2007-03-22 12:13:20 -04001759 .put_super = btrfs_put_super,
1760 .read_inode = btrfs_read_locked_inode,
Chris Masond5719762007-03-23 10:01:08 -04001761 .write_super = btrfs_write_super,
1762 .sync_fs = btrfs_sync_fs,
Chris Mason4730a4b2007-03-26 12:00:39 -04001763 .write_inode = btrfs_write_inode,
Chris Mason2c90e5d2007-04-02 10:50:19 -04001764 .alloc_inode = btrfs_alloc_inode,
1765 .destroy_inode = btrfs_destroy_inode,
Chris Masone20d96d2007-03-22 12:13:20 -04001766};
1767
1768static struct inode_operations btrfs_dir_inode_operations = {
1769 .lookup = btrfs_lookup,
Chris Masond5719762007-03-23 10:01:08 -04001770 .create = btrfs_create,
Chris Mason134e9732007-03-25 13:44:56 -04001771 .unlink = btrfs_unlink,
Chris Masonf7922032007-03-25 20:17:36 -04001772 .mkdir = btrfs_mkdir,
Chris Mason5f443fd2007-03-27 13:42:32 -04001773 .rmdir = btrfs_rmdir,
Chris Masone20d96d2007-03-22 12:13:20 -04001774};
1775
Chris Masond6e4a422007-04-06 15:37:36 -04001776static struct inode_operations btrfs_dir_ro_inode_operations = {
1777 .lookup = btrfs_lookup,
1778};
1779
Chris Masone20d96d2007-03-22 12:13:20 -04001780static struct file_operations btrfs_dir_file_operations = {
1781 .llseek = generic_file_llseek,
1782 .read = generic_read_dir,
1783 .readdir = btrfs_readdir,
1784};
1785
Chris Masondee26a92007-03-26 16:00:06 -04001786static struct address_space_operations btrfs_aops = {
1787 .readpage = btrfs_readpage,
Chris Masone8f05c42007-04-04 14:30:09 -04001788 .readpages = btrfs_readpages,
Chris Masondee26a92007-03-26 16:00:06 -04001789 .writepage = btrfs_writepage,
1790 .sync_page = block_sync_page,
1791 .prepare_write = btrfs_prepare_write,
Chris Mason75dfe392007-03-29 11:56:46 -04001792 .commit_write = btrfs_commit_write,
Chris Masondee26a92007-03-26 16:00:06 -04001793};
1794
1795static struct inode_operations btrfs_file_inode_operations = {
Chris Masonf4b9aa82007-03-27 11:05:53 -04001796 .truncate = btrfs_truncate,
Chris Masondee26a92007-03-26 16:00:06 -04001797};
1798
1799static struct file_operations btrfs_file_operations = {
1800 .llseek = generic_file_llseek,
1801 .read = do_sync_read,
Chris Masone8f05c42007-04-04 14:30:09 -04001802 .aio_read = btrfs_file_aio_read,
1803 .write = btrfs_file_write,
Chris Masondee26a92007-03-26 16:00:06 -04001804 .mmap = generic_file_mmap,
1805 .open = generic_file_open,
Chris Masondee26a92007-03-26 16:00:06 -04001806};
Chris Masone20d96d2007-03-22 12:13:20 -04001807
Chris Mason2e635a22007-03-21 11:12:56 -04001808static int __init init_btrfs_fs(void)
1809{
Chris Mason2c90e5d2007-04-02 10:50:19 -04001810 int err;
Chris Mason2e635a22007-03-21 11:12:56 -04001811 printk("btrfs loaded!\n");
Chris Mason2c90e5d2007-04-02 10:50:19 -04001812 err = init_inodecache();
1813 if (err)
1814 return err;
Chris Masond6e4a422007-04-06 15:37:36 -04001815 kset_set_kset_s(&btrfs_subsys, fs_subsys);
1816 err = subsystem_register(&btrfs_subsys);
1817 if (err)
1818 goto out;
Chris Mason2e635a22007-03-21 11:12:56 -04001819 return register_filesystem(&btrfs_fs_type);
Chris Masond6e4a422007-04-06 15:37:36 -04001820out:
1821 destroy_inodecache();
1822 return err;
Chris Mason2e635a22007-03-21 11:12:56 -04001823}
1824
1825static void __exit exit_btrfs_fs(void)
1826{
Chris Mason2c90e5d2007-04-02 10:50:19 -04001827 destroy_inodecache();
Chris Mason2e635a22007-03-21 11:12:56 -04001828 unregister_filesystem(&btrfs_fs_type);
Chris Masond6e4a422007-04-06 15:37:36 -04001829 subsystem_unregister(&btrfs_subsys);
Chris Mason2e635a22007-03-21 11:12:56 -04001830 printk("btrfs unloaded\n");
1831}
1832
1833module_init(init_btrfs_fs)
1834module_exit(exit_btrfs_fs)
1835
1836MODULE_LICENSE("GPL");