blob: 66d9fb2288c302511fbbfdf6529611a2fd53ea09 [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;
439 int ret;
440
441 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
442 return 0;
443 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
444 return 0;
445
446 path = btrfs_alloc_path();
447 BUG_ON(!path);
448 mutex_lock(&root->fs_info->fs_mutex);
449
450 ret = btrfs_lookup_inode(NULL, root, path, location, 0);
451 if (ret)
452 goto out;
453 ri = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
454 path->slots[0],
455 struct btrfs_root_item);
456 location->objectid = btrfs_root_dirid(ri);
457 location->flags = 0;
458 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
459 location->offset = 0;
460 /* FIXME properly select the root */
461 *sub_root = root->fs_info->fs_root;
462out:
463 btrfs_free_path(path);
464 mutex_unlock(&root->fs_info->fs_mutex);
465 return ret;
466}
467
468
Chris Masone20d96d2007-03-22 12:13:20 -0400469static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
470 struct nameidata *nd)
471{
472 struct inode * inode;
Chris Masond6e4a422007-04-06 15:37:36 -0400473 struct btrfs_inode *bi = BTRFS_I(dir);
474 struct btrfs_root *root = bi->root;
475 struct btrfs_root *sub_root = root;
476 struct btrfs_key location;
Chris Masone20d96d2007-03-22 12:13:20 -0400477 int ret;
478
479 if (dentry->d_name.len > BTRFS_NAME_LEN)
480 return ERR_PTR(-ENAMETOOLONG);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400481 mutex_lock(&root->fs_info->fs_mutex);
Chris Masond6e4a422007-04-06 15:37:36 -0400482 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400483 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masone20d96d2007-03-22 12:13:20 -0400484 if (ret < 0)
485 return ERR_PTR(ret);
486 inode = NULL;
Chris Masond6e4a422007-04-06 15:37:36 -0400487 if (location.objectid) {
488 ret = fixup_tree_root_location(root, &location, &sub_root);
489 if (ret < 0)
490 return ERR_PTR(ret);
491 if (ret > 0)
492 return ERR_PTR(-ENOENT);
493 inode = iget_locked(dir->i_sb, location.objectid);
Chris Masone20d96d2007-03-22 12:13:20 -0400494 if (!inode)
495 return ERR_PTR(-EACCES);
Chris Masond6e4a422007-04-06 15:37:36 -0400496 if (inode->i_state & I_NEW) {
497 BTRFS_I(inode)->root = sub_root;
498 memcpy(&BTRFS_I(inode)->location, &location,
499 sizeof(location));
500 btrfs_read_locked_inode(inode);
501 unlock_new_inode(inode);
502 }
Chris Mason2c90e5d2007-04-02 10:50:19 -0400503 check_inode(inode);
Chris Masone20d96d2007-03-22 12:13:20 -0400504 }
Chris Mason2c90e5d2007-04-02 10:50:19 -0400505 check_inode(dir);
Chris Masone20d96d2007-03-22 12:13:20 -0400506 return d_splice_alias(inode, dentry);
507}
508
509static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
510{
511 struct inode *inode = filp->f_path.dentry->d_inode;
Chris Masond6e4a422007-04-06 15:37:36 -0400512 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone20d96d2007-03-22 12:13:20 -0400513 struct btrfs_item *item;
514 struct btrfs_dir_item *di;
515 struct btrfs_key key;
Chris Mason5caf2a02007-04-02 11:20:42 -0400516 struct btrfs_path *path;
Chris Masone20d96d2007-03-22 12:13:20 -0400517 int ret;
518 u32 nritems;
519 struct btrfs_leaf *leaf;
520 int slot;
521 int advance;
522 unsigned char d_type = DT_UNKNOWN;
Chris Mason7f5c1512007-03-23 15:56:19 -0400523 int over = 0;
Chris Masond6e4a422007-04-06 15:37:36 -0400524 int key_type = BTRFS_DIR_INDEX_KEY;
525
526 /* FIXME, use a real flag for deciding about the key type */
527 if (root->fs_info->tree_root == root)
528 key_type = BTRFS_DIR_ITEM_KEY;
Chris Masone20d96d2007-03-22 12:13:20 -0400529
Chris Mason22b0ebd2007-03-30 08:47:31 -0400530 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone20d96d2007-03-22 12:13:20 -0400531 key.objectid = inode->i_ino;
Chris Masone20d96d2007-03-22 12:13:20 -0400532 key.flags = 0;
Chris Masond6e4a422007-04-06 15:37:36 -0400533 btrfs_set_key_type(&key, key_type);
Chris Masone20d96d2007-03-22 12:13:20 -0400534 key.offset = filp->f_pos;
Chris Mason5caf2a02007-04-02 11:20:42 -0400535 path = btrfs_alloc_path();
536 btrfs_init_path(path);
537 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Masone20d96d2007-03-22 12:13:20 -0400538 if (ret < 0) {
539 goto err;
540 }
Chris Mason7f5c1512007-03-23 15:56:19 -0400541 advance = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400542 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400543 leaf = btrfs_buffer_leaf(path->nodes[0]);
Chris Masone20d96d2007-03-22 12:13:20 -0400544 nritems = btrfs_header_nritems(&leaf->header);
Chris Mason5caf2a02007-04-02 11:20:42 -0400545 slot = path->slots[0];
Chris Masondee26a92007-03-26 16:00:06 -0400546 if (advance || slot >= nritems) {
547 if (slot >= nritems -1) {
Chris Mason5caf2a02007-04-02 11:20:42 -0400548 ret = btrfs_next_leaf(root, path);
Chris Masone20d96d2007-03-22 12:13:20 -0400549 if (ret)
550 break;
Chris Mason5caf2a02007-04-02 11:20:42 -0400551 leaf = btrfs_buffer_leaf(path->nodes[0]);
Chris Masone20d96d2007-03-22 12:13:20 -0400552 nritems = btrfs_header_nritems(&leaf->header);
Chris Mason5caf2a02007-04-02 11:20:42 -0400553 slot = path->slots[0];
Chris Masone20d96d2007-03-22 12:13:20 -0400554 } else {
555 slot++;
Chris Mason5caf2a02007-04-02 11:20:42 -0400556 path->slots[0]++;
Chris Masone20d96d2007-03-22 12:13:20 -0400557 }
558 }
559 advance = 1;
560 item = leaf->items + slot;
Chris Masone20d96d2007-03-22 12:13:20 -0400561 if (btrfs_disk_key_objectid(&item->key) != key.objectid)
562 break;
Chris Masond6e4a422007-04-06 15:37:36 -0400563 if (key_type == BTRFS_DIR_INDEX_KEY &&
564 btrfs_disk_key_offset(&item->key) >
Chris Mason3eb03142007-04-05 14:28:50 -0400565 root->fs_info->highest_inode)
Chris Mason5be6f7f2007-04-05 13:35:25 -0400566 break;
Chris Masond6e4a422007-04-06 15:37:36 -0400567 if (btrfs_disk_key_type(&item->key) != key_type)
Chris Masone20d96d2007-03-22 12:13:20 -0400568 continue;
Chris Mason7f5c1512007-03-23 15:56:19 -0400569 if (btrfs_disk_key_offset(&item->key) < filp->f_pos)
570 continue;
Chris Mason7fcde0e2007-04-05 12:13:21 -0400571 filp->f_pos = btrfs_disk_key_offset(&item->key);
Chris Masondee26a92007-03-26 16:00:06 -0400572 advance = 1;
Chris Masone20d96d2007-03-22 12:13:20 -0400573 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Chris Masone20d96d2007-03-22 12:13:20 -0400574 over = filldir(dirent, (const char *)(di + 1),
575 btrfs_dir_name_len(di),
576 btrfs_disk_key_offset(&item->key),
Chris Masond6e4a422007-04-06 15:37:36 -0400577 btrfs_disk_key_objectid(&di->location), d_type);
Chris Mason7fcde0e2007-04-05 12:13:21 -0400578 if (over)
579 goto nopos;
Chris Masone20d96d2007-03-22 12:13:20 -0400580 }
Chris Mason7fcde0e2007-04-05 12:13:21 -0400581 filp->f_pos++;
582nopos:
Chris Masone20d96d2007-03-22 12:13:20 -0400583 ret = 0;
584err:
Chris Mason5caf2a02007-04-02 11:20:42 -0400585 btrfs_release_path(root, path);
586 btrfs_free_path(path);
Chris Mason22b0ebd2007-03-30 08:47:31 -0400587 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masone20d96d2007-03-22 12:13:20 -0400588 return ret;
589}
590
591static void btrfs_put_super (struct super_block * sb)
592{
593 struct btrfs_root *root = btrfs_sb(sb);
594 int ret;
595
596 ret = close_ctree(root);
597 if (ret) {
598 printk("close ctree returns %d\n", ret);
599 }
600 sb->s_fs_info = NULL;
601}
Chris Mason2e635a22007-03-21 11:12:56 -0400602
603static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
604{
605 struct inode * inode;
Chris Masone20d96d2007-03-22 12:13:20 -0400606 struct dentry * root_dentry;
607 struct btrfs_super_block *disk_super;
Chris Masone20d96d2007-03-22 12:13:20 -0400608 struct btrfs_root *root;
Chris Masond6e4a422007-04-06 15:37:36 -0400609 struct btrfs_inode *bi;
Chris Mason2e635a22007-03-21 11:12:56 -0400610
611 sb->s_maxbytes = MAX_LFS_FILESIZE;
Chris Mason2e635a22007-03-21 11:12:56 -0400612 sb->s_magic = BTRFS_SUPER_MAGIC;
Chris Masone20d96d2007-03-22 12:13:20 -0400613 sb->s_op = &btrfs_super_ops;
Chris Mason2e635a22007-03-21 11:12:56 -0400614 sb->s_time_gran = 1;
Chris Masone20d96d2007-03-22 12:13:20 -0400615
Chris Mason2c90e5d2007-04-02 10:50:19 -0400616 root = open_ctree(sb);
Chris Masond98237b2007-03-28 13:57:48 -0400617
Chris Masone20d96d2007-03-22 12:13:20 -0400618 if (!root) {
619 printk("btrfs: open_ctree failed\n");
620 return -EIO;
621 }
Chris Mason87cbda52007-03-28 19:44:27 -0400622 sb->s_fs_info = root;
623 disk_super = root->fs_info->disk_super;
Chris Masone20d96d2007-03-22 12:13:20 -0400624 printk("read in super total blocks %Lu root %Lu\n",
625 btrfs_super_total_blocks(disk_super),
626 btrfs_super_root_dir(disk_super));
627
628 inode = iget_locked(sb, btrfs_super_root_dir(disk_super));
Chris Masond6e4a422007-04-06 15:37:36 -0400629 bi = BTRFS_I(inode);
630 bi->location.objectid = inode->i_ino;
631 bi->location.offset = 0;
632 bi->location.flags = 0;
633 bi->root = root->fs_info->tree_root;
634 btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY);
635
Chris Mason2e635a22007-03-21 11:12:56 -0400636 if (!inode)
637 return -ENOMEM;
Chris Masone20d96d2007-03-22 12:13:20 -0400638 if (inode->i_state & I_NEW) {
639 btrfs_read_locked_inode(inode);
640 unlock_new_inode(inode);
641 }
Chris Mason2e635a22007-03-21 11:12:56 -0400642
Chris Masone20d96d2007-03-22 12:13:20 -0400643 root_dentry = d_alloc_root(inode);
644 if (!root_dentry) {
Chris Mason2e635a22007-03-21 11:12:56 -0400645 iput(inode);
646 return -ENOMEM;
647 }
Chris Masone20d96d2007-03-22 12:13:20 -0400648 sb->s_root = root_dentry;
649
Chris Mason2e635a22007-03-21 11:12:56 -0400650 return 0;
651}
652
Chris Masond5719762007-03-23 10:01:08 -0400653static void fill_inode_item(struct btrfs_inode_item *item,
654 struct inode *inode)
655{
656 btrfs_set_inode_uid(item, inode->i_uid);
657 btrfs_set_inode_gid(item, inode->i_gid);
658 btrfs_set_inode_size(item, inode->i_size);
659 btrfs_set_inode_mode(item, inode->i_mode);
660 btrfs_set_inode_nlink(item, inode->i_nlink);
661 btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
662 btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec);
663 btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec);
664 btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec);
665 btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec);
666 btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
667 btrfs_set_inode_nblocks(item, inode->i_blocks);
668 btrfs_set_inode_generation(item, inode->i_generation);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400669 check_inode(inode);
Chris Masond5719762007-03-23 10:01:08 -0400670}
671
Chris Mason4730a4b2007-03-26 12:00:39 -0400672static int btrfs_update_inode(struct btrfs_trans_handle *trans,
673 struct btrfs_root *root,
674 struct inode *inode)
675{
676 struct btrfs_inode_item *inode_item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400677 struct btrfs_path *path;
Chris Mason4730a4b2007-03-26 12:00:39 -0400678 int ret;
679
Chris Mason5caf2a02007-04-02 11:20:42 -0400680 path = btrfs_alloc_path();
681 BUG_ON(!path);
682 btrfs_init_path(path);
Chris Mason4730a4b2007-03-26 12:00:39 -0400683
Chris Masond6e4a422007-04-06 15:37:36 -0400684 ret = btrfs_lookup_inode(trans, root, path,
685 &BTRFS_I(inode)->location, 1);
Chris Mason4730a4b2007-03-26 12:00:39 -0400686 if (ret) {
687 if (ret > 0)
688 ret = -ENOENT;
689 goto failed;
690 }
691
Chris Mason5caf2a02007-04-02 11:20:42 -0400692 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
693 path->slots[0],
Chris Mason4730a4b2007-03-26 12:00:39 -0400694 struct btrfs_inode_item);
695
696 fill_inode_item(inode_item, inode);
Chris Mason5caf2a02007-04-02 11:20:42 -0400697 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason4730a4b2007-03-26 12:00:39 -0400698failed:
Chris Mason5caf2a02007-04-02 11:20:42 -0400699 btrfs_release_path(root, path);
700 btrfs_free_path(path);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400701 check_inode(inode);
Chris Mason4730a4b2007-03-26 12:00:39 -0400702 return 0;
703}
704
705static int btrfs_write_inode(struct inode *inode, int wait)
706{
Chris Masond6e4a422007-04-06 15:37:36 -0400707 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason4730a4b2007-03-26 12:00:39 -0400708 struct btrfs_trans_handle *trans;
709 int ret;
710
711 mutex_lock(&root->fs_info->fs_mutex);
712 trans = btrfs_start_transaction(root, 1);
713 ret = btrfs_update_inode(trans, root, inode);
714 if (wait)
715 btrfs_commit_transaction(trans, root);
716 else
717 btrfs_end_transaction(trans, root);
718 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400719 check_inode(inode);
Chris Mason4730a4b2007-03-26 12:00:39 -0400720 return ret;
721}
722
Chris Masond5719762007-03-23 10:01:08 -0400723static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
724 struct inode *dir, int mode)
725{
726 struct inode *inode;
727 struct btrfs_inode_item inode_item;
Chris Masond6e4a422007-04-06 15:37:36 -0400728 struct btrfs_root *root = BTRFS_I(dir)->root;
729 struct btrfs_key *key;
Chris Masond5719762007-03-23 10:01:08 -0400730 int ret;
731 u64 objectid;
732
733 inode = new_inode(dir->i_sb);
734 if (!inode)
735 return ERR_PTR(-ENOMEM);
736
Chris Masond6e4a422007-04-06 15:37:36 -0400737 BTRFS_I(inode)->root = BTRFS_I(dir)->root;
738 key = &BTRFS_I(inode)->location;
Chris Mason2c90e5d2007-04-02 10:50:19 -0400739 check_inode(inode);
Chris Masond5719762007-03-23 10:01:08 -0400740 ret = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
741 BUG_ON(ret);
742
743 inode->i_uid = current->fsuid;
744 inode->i_gid = current->fsgid;
745 inode->i_mode = mode;
746 inode->i_ino = objectid;
747 inode->i_blocks = 0;
748 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
749 fill_inode_item(&inode_item, inode);
750
Chris Masond6e4a422007-04-06 15:37:36 -0400751 key->objectid = objectid;
752 key->flags = 0;
753 key->offset = 0;
754 btrfs_set_key_type(key, BTRFS_INODE_ITEM_KEY);
755 ret = btrfs_insert_inode_map(trans, root, objectid, key);
Chris Masond5719762007-03-23 10:01:08 -0400756 BUG_ON(ret);
757
758 ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
759 BUG_ON(ret);
760
761 insert_inode_hash(inode);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400762 check_inode(inode);
763 check_inode(dir);
Chris Masond5719762007-03-23 10:01:08 -0400764 return inode;
765}
766
767static int btrfs_add_link(struct btrfs_trans_handle *trans,
768 struct dentry *dentry, struct inode *inode)
769{
770 int ret;
Chris Masond6e4a422007-04-06 15:37:36 -0400771 struct btrfs_key key;
772 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
773 key.objectid = inode->i_ino;
774 key.flags = 0;
775 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
776 key.offset = 0;
777
778 ret = btrfs_insert_dir_item(trans, root,
Chris Masond5719762007-03-23 10:01:08 -0400779 dentry->d_name.name, dentry->d_name.len,
780 dentry->d_parent->d_inode->i_ino,
Chris Masond6e4a422007-04-06 15:37:36 -0400781 &key, 0);
Chris Mason4730a4b2007-03-26 12:00:39 -0400782 if (ret == 0) {
Chris Mason5f26f772007-04-05 10:38:44 -0400783 dentry->d_parent->d_inode->i_size += dentry->d_name.len * 2;
Chris Masond6e4a422007-04-06 15:37:36 -0400784 ret = btrfs_update_inode(trans, root,
Chris Mason4730a4b2007-03-26 12:00:39 -0400785 dentry->d_parent->d_inode);
786 }
Chris Mason2c90e5d2007-04-02 10:50:19 -0400787 check_inode(inode);
788 check_inode(dentry->d_parent->d_inode);
Chris Masond5719762007-03-23 10:01:08 -0400789 return ret;
790}
791
792static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
793 struct dentry *dentry, struct inode *inode)
794{
795 int err = btrfs_add_link(trans, dentry, inode);
796 if (!err) {
797 d_instantiate(dentry, inode);
798 return 0;
799 }
Chris Mason2c90e5d2007-04-02 10:50:19 -0400800 if (err > 0)
801 err = -EEXIST;
802 check_inode(inode);
Chris Masond5719762007-03-23 10:01:08 -0400803 return err;
804}
805
806static int btrfs_create(struct inode *dir, struct dentry *dentry,
807 int mode, struct nameidata *nd)
808{
809 struct btrfs_trans_handle *trans;
Chris Masond6e4a422007-04-06 15:37:36 -0400810 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Masond5719762007-03-23 10:01:08 -0400811 struct inode *inode;
812 int err;
Chris Mason134e9732007-03-25 13:44:56 -0400813 int drop_inode = 0;
Chris Masond5719762007-03-23 10:01:08 -0400814
Chris Masond561c022007-03-23 19:47:49 -0400815 mutex_lock(&root->fs_info->fs_mutex);
Chris Masond5719762007-03-23 10:01:08 -0400816 trans = btrfs_start_transaction(root, 1);
817 inode = btrfs_new_inode(trans, dir, mode);
818 err = PTR_ERR(inode);
819 if (IS_ERR(inode))
Chris Masond561c022007-03-23 19:47:49 -0400820 goto out_unlock;
Chris Masond5719762007-03-23 10:01:08 -0400821 // FIXME mark the inode dirty
822 err = btrfs_add_nondir(trans, dentry, inode);
Chris Mason134e9732007-03-25 13:44:56 -0400823 if (err)
824 drop_inode = 1;
Chris Masondee26a92007-03-26 16:00:06 -0400825 else {
826 inode->i_mapping->a_ops = &btrfs_aops;
827 inode->i_fop = &btrfs_file_operations;
828 inode->i_op = &btrfs_file_inode_operations;
829 }
Chris Masond5719762007-03-23 10:01:08 -0400830 dir->i_sb->s_dirt = 1;
Chris Masond561c022007-03-23 19:47:49 -0400831out_unlock:
Chris Mason22b0ebd2007-03-30 08:47:31 -0400832 btrfs_end_transaction(trans, root);
Chris Masond561c022007-03-23 19:47:49 -0400833 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400834 check_inode(inode);
835 check_inode(dir);
836
Chris Mason134e9732007-03-25 13:44:56 -0400837 if (drop_inode) {
838 inode_dec_link_count(inode);
839 iput(inode);
840 }
Chris Masond5719762007-03-23 10:01:08 -0400841 return err;
842}
843
Chris Masonf7922032007-03-25 20:17:36 -0400844static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans,
845 struct inode *inode, struct inode *dir)
846{
Chris Masond6e4a422007-04-06 15:37:36 -0400847 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Masonf7922032007-03-25 20:17:36 -0400848 int ret;
849 char buf[2];
Chris Masond6e4a422007-04-06 15:37:36 -0400850 struct btrfs_key key;
851
Chris Masonf7922032007-03-25 20:17:36 -0400852 buf[0] = '.';
853 buf[1] = '.';
854
Chris Masond6e4a422007-04-06 15:37:36 -0400855 key.objectid = inode->i_ino;
856 key.offset = 0;
857 key.flags = 0;
858 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
859
Chris Masonf7922032007-03-25 20:17:36 -0400860 ret = btrfs_insert_dir_item(trans, root, buf, 1, inode->i_ino,
Chris Masond6e4a422007-04-06 15:37:36 -0400861 &key, 1);
Chris Masonf7922032007-03-25 20:17:36 -0400862 if (ret)
863 goto error;
Chris Masond6e4a422007-04-06 15:37:36 -0400864 key.objectid = dir->i_ino;
Chris Masonf7922032007-03-25 20:17:36 -0400865 ret = btrfs_insert_dir_item(trans, root, buf, 2, inode->i_ino,
Chris Masond6e4a422007-04-06 15:37:36 -0400866 &key, 1);
Chris Mason4730a4b2007-03-26 12:00:39 -0400867 if (ret)
868 goto error;
Chris Mason5f26f772007-04-05 10:38:44 -0400869 inode->i_size = 6;
Chris Mason4730a4b2007-03-26 12:00:39 -0400870 ret = btrfs_update_inode(trans, root, inode);
Chris Masonf7922032007-03-25 20:17:36 -0400871error:
872 return ret;
873}
874
875static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
876{
877 struct inode *inode;
878 struct btrfs_trans_handle *trans;
Chris Masond6e4a422007-04-06 15:37:36 -0400879 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Masonf7922032007-03-25 20:17:36 -0400880 int err = 0;
881 int drop_on_err = 0;
882
883 mutex_lock(&root->fs_info->fs_mutex);
884 trans = btrfs_start_transaction(root, 1);
885 if (IS_ERR(trans)) {
886 err = PTR_ERR(trans);
887 goto out_unlock;
888 }
889 inode = btrfs_new_inode(trans, dir, S_IFDIR | mode);
890 if (IS_ERR(inode)) {
891 err = PTR_ERR(inode);
892 goto out_fail;
893 }
894 drop_on_err = 1;
895 inode->i_op = &btrfs_dir_inode_operations;
896 inode->i_fop = &btrfs_dir_file_operations;
897
898 err = btrfs_make_empty_dir(trans, inode, dir);
899 if (err)
900 goto out_fail;
901 err = btrfs_add_link(trans, dentry, inode);
902 if (err)
903 goto out_fail;
904 d_instantiate(dentry, inode);
Chris Masonf7922032007-03-25 20:17:36 -0400905 drop_on_err = 0;
906
907out_fail:
908 btrfs_end_transaction(trans, root);
909out_unlock:
910 mutex_unlock(&root->fs_info->fs_mutex);
911 if (drop_on_err)
912 iput(inode);
913 return err;
914}
915
Chris Masond5719762007-03-23 10:01:08 -0400916static int btrfs_sync_fs(struct super_block *sb, int wait)
917{
918 struct btrfs_trans_handle *trans;
919 struct btrfs_root *root;
920 int ret;
Chris Masond98237b2007-03-28 13:57:48 -0400921 root = btrfs_sb(sb);
Chris Masondf2ce342007-03-23 11:00:45 -0400922
Chris Masond5719762007-03-23 10:01:08 -0400923 sb->s_dirt = 0;
Chris Masond561c022007-03-23 19:47:49 -0400924 if (!wait) {
Chris Mason7cfcc172007-04-02 14:53:59 -0400925 filemap_flush(root->fs_info->btree_inode->i_mapping);
Chris Masond561c022007-03-23 19:47:49 -0400926 return 0;
927 }
Chris Mason7cfcc172007-04-02 14:53:59 -0400928 filemap_write_and_wait(root->fs_info->btree_inode->i_mapping);
Chris Masond561c022007-03-23 19:47:49 -0400929 mutex_lock(&root->fs_info->fs_mutex);
Chris Masond5719762007-03-23 10:01:08 -0400930 trans = btrfs_start_transaction(root, 1);
931 ret = btrfs_commit_transaction(trans, root);
932 sb->s_dirt = 0;
933 BUG_ON(ret);
934printk("btrfs sync_fs\n");
Chris Masond561c022007-03-23 19:47:49 -0400935 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond5719762007-03-23 10:01:08 -0400936 return 0;
937}
938
Chris Masone8f05c42007-04-04 14:30:09 -0400939#if 0
Chris Masondf24a2b2007-04-04 09:36:31 -0400940static int btrfs_get_block_inline(struct inode *inode, sector_t iblock,
941 struct buffer_head *result, int create)
942{
943 struct btrfs_root *root = btrfs_sb(inode->i_sb);
944 struct btrfs_path *path;
945 struct btrfs_key key;
946 struct btrfs_leaf *leaf;
947 int num_bytes = result->b_size;
948 int item_size;
949 int ret;
950 u64 pos;
951 char *ptr;
952 int copy_size;
953 int err = 0;
954 char *safe_ptr;
955 char *data_ptr;
956
957 path = btrfs_alloc_path();
958 BUG_ON(!path);
959
960 WARN_ON(create);
961 if (create) {
962 return 0;
963 }
964 pos = iblock << inode->i_blkbits;
965 key.objectid = inode->i_ino;
966 key.flags = 0;
967 btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
968 ptr = kmap(result->b_page);
969 safe_ptr = ptr;
970 ptr += (pos & (PAGE_CACHE_SIZE -1));
971again:
972 key.offset = pos;
973 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
974 if (ret) {
975 if (ret < 0)
976 err = ret;
977 else
978 err = 0;
979 goto out;
980 }
981 leaf = btrfs_buffer_leaf(path->nodes[0]);
982 item_size = btrfs_item_size(leaf->items + path->slots[0]);
983 copy_size = min(num_bytes, item_size);
984 data_ptr = btrfs_item_ptr(leaf, path->slots[0], char);
985 WARN_ON(safe_ptr + PAGE_CACHE_SIZE < ptr + copy_size);
986 memcpy(ptr, data_ptr, copy_size);
987 pos += copy_size;
988 num_bytes -= copy_size;
989 WARN_ON(num_bytes < 0);
990 ptr += copy_size;
991 btrfs_release_path(root, path);
992 if (num_bytes != 0) {
993 if (pos >= i_size_read(inode))
994 memset(ptr, 0, num_bytes);
995 else
996 goto again;
997 }
998 set_buffer_uptodate(result);
999 map_bh(result, inode->i_sb, 0);
1000 err = 0;
1001out:
1002 btrfs_free_path(path);
1003 kunmap(result->b_page);
1004 return err;
1005}
Chris Masone8f05c42007-04-04 14:30:09 -04001006#endif
Chris Masondf24a2b2007-04-04 09:36:31 -04001007
Chris Mason75dfe392007-03-29 11:56:46 -04001008static int btrfs_get_block_lock(struct inode *inode, sector_t iblock,
Chris Masondee26a92007-03-26 16:00:06 -04001009 struct buffer_head *result, int create)
1010{
1011 int ret;
1012 int err = 0;
1013 u64 blocknr;
1014 u64 extent_start = 0;
1015 u64 extent_end = 0;
1016 u64 objectid = inode->i_ino;
Chris Mason5caf2a02007-04-02 11:20:42 -04001017 struct btrfs_path *path;
Chris Masond6e4a422007-04-06 15:37:36 -04001018 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondee26a92007-03-26 16:00:06 -04001019 struct btrfs_trans_handle *trans = NULL;
1020 struct btrfs_file_extent_item *item;
1021 struct btrfs_leaf *leaf;
1022 struct btrfs_disk_key *found_key;
1023
Chris Mason5caf2a02007-04-02 11:20:42 -04001024 path = btrfs_alloc_path();
1025 BUG_ON(!path);
1026 btrfs_init_path(path);
Chris Masondee26a92007-03-26 16:00:06 -04001027 if (create)
1028 trans = btrfs_start_transaction(root, 1);
1029
Chris Mason5caf2a02007-04-02 11:20:42 -04001030 ret = btrfs_lookup_file_extent(trans, root, path,
Chris Mason9773a782007-03-27 11:26:26 -04001031 inode->i_ino,
1032 iblock << inode->i_blkbits, 0);
Chris Masondee26a92007-03-26 16:00:06 -04001033 if (ret < 0) {
Chris Masondee26a92007-03-26 16:00:06 -04001034 err = ret;
1035 goto out;
1036 }
1037
1038 if (ret != 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04001039 if (path->slots[0] == 0) {
1040 btrfs_release_path(root, path);
Chris Masondee26a92007-03-26 16:00:06 -04001041 goto allocate;
1042 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001043 path->slots[0]--;
Chris Masondee26a92007-03-26 16:00:06 -04001044 }
1045
Chris Mason5caf2a02007-04-02 11:20:42 -04001046 item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Masondee26a92007-03-26 16:00:06 -04001047 struct btrfs_file_extent_item);
Chris Mason5caf2a02007-04-02 11:20:42 -04001048 leaf = btrfs_buffer_leaf(path->nodes[0]);
Chris Masondee26a92007-03-26 16:00:06 -04001049 blocknr = btrfs_file_extent_disk_blocknr(item);
1050 blocknr += btrfs_file_extent_offset(item);
1051
1052 /* exact match found, use it */
1053 if (ret == 0) {
1054 err = 0;
1055 map_bh(result, inode->i_sb, blocknr);
Chris Masondee26a92007-03-26 16:00:06 -04001056 goto out;
1057 }
1058
1059 /* are we inside the extent that was found? */
Chris Mason5caf2a02007-04-02 11:20:42 -04001060 found_key = &leaf->items[path->slots[0]].key;
Chris Masondee26a92007-03-26 16:00:06 -04001061 if (btrfs_disk_key_objectid(found_key) != objectid ||
1062 btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY) {
1063 extent_end = 0;
1064 extent_start = 0;
Chris Mason5caf2a02007-04-02 11:20:42 -04001065 btrfs_release_path(root, path);
Chris Masondee26a92007-03-26 16:00:06 -04001066 goto allocate;
1067 }
1068
Chris Mason5caf2a02007-04-02 11:20:42 -04001069 extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key);
Chris Mason9773a782007-03-27 11:26:26 -04001070 extent_start = extent_start >> inode->i_blkbits;
Chris Masondee26a92007-03-26 16:00:06 -04001071 extent_start += btrfs_file_extent_offset(item);
1072 extent_end = extent_start + btrfs_file_extent_num_blocks(item);
Chris Masondee26a92007-03-26 16:00:06 -04001073 if (iblock >= extent_start && iblock < extent_end) {
1074 err = 0;
1075 map_bh(result, inode->i_sb, blocknr + iblock - extent_start);
1076 goto out;
1077 }
1078allocate:
1079 /* ok, create a new extent */
1080 if (!create) {
1081 err = 0;
1082 goto out;
1083 }
Chris Mason9773a782007-03-27 11:26:26 -04001084 ret = btrfs_alloc_file_extent(trans, root, objectid,
1085 iblock << inode->i_blkbits,
Chris Masondee26a92007-03-26 16:00:06 -04001086 1, extent_end, &blocknr);
1087 if (ret) {
1088 err = ret;
1089 goto out;
1090 }
Chris Masonf4b9aa82007-03-27 11:05:53 -04001091 inode->i_blocks += inode->i_sb->s_blocksize >> 9;
1092 set_buffer_new(result);
Chris Masondee26a92007-03-26 16:00:06 -04001093 map_bh(result, inode->i_sb, blocknr);
1094
1095out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001096 btrfs_release_path(root, path);
1097 btrfs_free_path(path);
Chris Masondee26a92007-03-26 16:00:06 -04001098 if (trans)
1099 btrfs_end_transaction(trans, root);
Chris Mason75dfe392007-03-29 11:56:46 -04001100 return err;
1101}
1102
1103static int btrfs_get_block(struct inode *inode, sector_t iblock,
1104 struct buffer_head *result, int create)
1105{
1106 int err;
Chris Masond6e4a422007-04-06 15:37:36 -04001107 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason75dfe392007-03-29 11:56:46 -04001108 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone8f05c42007-04-04 14:30:09 -04001109 err = btrfs_get_block_lock(inode, iblock, result, create);
1110 // err = btrfs_get_block_inline(inode, iblock, result, create);
Chris Masondee26a92007-03-26 16:00:06 -04001111 mutex_unlock(&root->fs_info->fs_mutex);
1112 return err;
1113}
1114
1115static int btrfs_prepare_write(struct file *file, struct page *page,
1116 unsigned from, unsigned to)
1117{
Chris Mason6407bf62007-03-27 06:33:00 -04001118 return nobh_prepare_write(page, from, to, btrfs_get_block);
Chris Masondee26a92007-03-26 16:00:06 -04001119}
Chris Mason75dfe392007-03-29 11:56:46 -04001120static int btrfs_commit_write(struct file *file, struct page *page,
1121 unsigned from, unsigned to)
1122{
Chris Mason75dfe392007-03-29 11:56:46 -04001123 return nobh_commit_write(file, page, from, to);
1124}
Chris Masondee26a92007-03-26 16:00:06 -04001125
Chris Masond561c022007-03-23 19:47:49 -04001126static void btrfs_write_super(struct super_block *sb)
1127{
1128 btrfs_sync_fs(sb, 1);
1129}
1130
Chris Masondee26a92007-03-26 16:00:06 -04001131static int btrfs_readpage(struct file *file, struct page *page)
1132{
1133 return mpage_readpage(page, btrfs_get_block);
1134}
1135
1136static int btrfs_readpages(struct file *file, struct address_space *mapping,
1137 struct list_head *pages, unsigned nr_pages)
1138{
1139 return mpage_readpages(mapping, pages, nr_pages, btrfs_get_block);
1140}
1141
1142static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1143{
Chris Mason6407bf62007-03-27 06:33:00 -04001144 return nobh_writepage(page, btrfs_get_block, wbc);
Chris Masondee26a92007-03-26 16:00:06 -04001145}
Chris Masond561c022007-03-23 19:47:49 -04001146
Chris Masonf4b9aa82007-03-27 11:05:53 -04001147static void btrfs_truncate(struct inode *inode)
1148{
Chris Masond6e4a422007-04-06 15:37:36 -04001149 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonf4b9aa82007-03-27 11:05:53 -04001150 int ret;
1151 struct btrfs_trans_handle *trans;
1152
1153 if (!S_ISREG(inode->i_mode))
1154 return;
1155 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1156 return;
1157
Chris Masone8f05c42007-04-04 14:30:09 -04001158 nobh_truncate_page(inode->i_mapping, inode->i_size);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001159
1160 /* FIXME, add redo link to tree so we don't leak on crash */
1161 mutex_lock(&root->fs_info->fs_mutex);
1162 trans = btrfs_start_transaction(root, 1);
1163 ret = btrfs_truncate_in_trans(trans, root, inode);
1164 BUG_ON(ret);
1165 ret = btrfs_end_transaction(trans, root);
1166 BUG_ON(ret);
1167 mutex_unlock(&root->fs_info->fs_mutex);
1168 mark_inode_dirty(inode);
1169}
1170
Chris Mason75dfe392007-03-29 11:56:46 -04001171static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes,
1172 struct page **prepared_pages,
1173 const char __user * buf)
1174{
1175 long page_fault = 0;
1176 int i;
1177 int offset = pos & (PAGE_CACHE_SIZE - 1);
1178
1179 for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
1180 size_t count = min_t(size_t,
1181 PAGE_CACHE_SIZE - offset, write_bytes);
1182 struct page *page = prepared_pages[i];
1183 fault_in_pages_readable(buf, count);
1184
1185 /* Copy data from userspace to the current page */
1186 kmap(page);
1187 page_fault = __copy_from_user(page_address(page) + offset,
1188 buf, count);
1189 /* Flush processor's dcache for this page */
1190 flush_dcache_page(page);
1191 kunmap(page);
1192 buf += count;
1193 write_bytes -= count;
1194
1195 if (page_fault)
1196 break;
1197 }
1198 return page_fault ? -EFAULT : 0;
1199}
1200
1201static void btrfs_drop_pages(struct page **pages, size_t num_pages)
1202{
1203 size_t i;
1204 for (i = 0; i < num_pages; i++) {
1205 if (!pages[i])
1206 break;
1207 unlock_page(pages[i]);
1208 mark_page_accessed(pages[i]);
1209 page_cache_release(pages[i]);
1210 }
1211}
1212static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
1213 struct btrfs_root *root,
1214 struct file *file,
1215 struct page **pages,
1216 size_t num_pages,
1217 loff_t pos,
1218 size_t write_bytes)
1219{
1220 int i;
1221 int offset;
1222 int err = 0;
1223 int ret;
1224 int this_write;
Chris Masonf254e522007-03-29 15:15:27 -04001225 struct inode *inode = file->f_path.dentry->d_inode;
Chris Mason75dfe392007-03-29 11:56:46 -04001226
1227 for (i = 0; i < num_pages; i++) {
1228 offset = pos & (PAGE_CACHE_SIZE -1);
1229 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
Chris Masonf254e522007-03-29 15:15:27 -04001230 /* FIXME, one block at a time */
1231
1232 mutex_lock(&root->fs_info->fs_mutex);
1233 trans = btrfs_start_transaction(root, 1);
1234 btrfs_csum_file_block(trans, root, inode->i_ino,
1235 pages[i]->index << PAGE_CACHE_SHIFT,
1236 kmap(pages[i]), PAGE_CACHE_SIZE);
1237 kunmap(pages[i]);
1238 SetPageChecked(pages[i]);
1239 ret = btrfs_end_transaction(trans, root);
1240 BUG_ON(ret);
1241 mutex_unlock(&root->fs_info->fs_mutex);
1242
Chris Mason75dfe392007-03-29 11:56:46 -04001243 ret = nobh_commit_write(file, pages[i], offset,
1244 offset + this_write);
1245 pos += this_write;
1246 if (ret) {
1247 err = ret;
1248 goto failed;
1249 }
1250 WARN_ON(this_write > write_bytes);
1251 write_bytes -= this_write;
1252 }
1253failed:
1254 return err;
1255}
1256
1257static int prepare_pages(struct btrfs_trans_handle *trans,
1258 struct btrfs_root *root,
1259 struct file *file,
1260 struct page **pages,
1261 size_t num_pages,
1262 loff_t pos,
1263 size_t write_bytes)
1264{
1265 int i;
1266 unsigned long index = pos >> PAGE_CACHE_SHIFT;
1267 struct inode *inode = file->f_path.dentry->d_inode;
1268 int offset;
1269 int err = 0;
1270 int ret;
1271 int this_write;
1272 loff_t isize = i_size_read(inode);
1273
1274 memset(pages, 0, num_pages * sizeof(struct page *));
1275
1276 for (i = 0; i < num_pages; i++) {
1277 pages[i] = grab_cache_page(inode->i_mapping, index + i);
1278 if (!pages[i]) {
1279 err = -ENOMEM;
1280 goto failed_release;
1281 }
1282 offset = pos & (PAGE_CACHE_SIZE -1);
1283 this_write = min(PAGE_CACHE_SIZE - offset, write_bytes);
1284 ret = nobh_prepare_write(pages[i], offset,
1285 offset + this_write,
Chris Masonf254e522007-03-29 15:15:27 -04001286 btrfs_get_block);
Chris Mason75dfe392007-03-29 11:56:46 -04001287 pos += this_write;
1288 if (ret) {
1289 err = ret;
1290 goto failed_truncate;
1291 }
1292 WARN_ON(this_write > write_bytes);
1293 write_bytes -= this_write;
1294 }
1295 return 0;
1296
1297failed_release:
1298 btrfs_drop_pages(pages, num_pages);
1299 return err;
1300
1301failed_truncate:
1302 btrfs_drop_pages(pages, num_pages);
1303 if (pos > isize)
1304 vmtruncate(inode, isize);
1305 return err;
1306}
1307
1308static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
1309 size_t count, loff_t *ppos)
1310{
1311 loff_t pos;
1312 size_t num_written = 0;
1313 int err = 0;
1314 int ret = 0;
Chris Mason75dfe392007-03-29 11:56:46 -04001315 struct inode *inode = file->f_path.dentry->d_inode;
Chris Masond6e4a422007-04-06 15:37:36 -04001316 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason75dfe392007-03-29 11:56:46 -04001317 struct page *pages[1];
1318
1319 if (file->f_flags & O_DIRECT)
1320 return -EINVAL;
1321 pos = *ppos;
1322
1323 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
1324 current->backing_dev_info = inode->i_mapping->backing_dev_info;
1325 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1326 if (err)
1327 goto out;
1328 if (count == 0)
1329 goto out;
1330 err = remove_suid(file->f_path.dentry);
1331 if (err)
1332 goto out;
1333 file_update_time(file);
1334 mutex_lock(&inode->i_mutex);
1335 while(count > 0) {
1336 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
1337 size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
1338 size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
1339 PAGE_CACHE_SHIFT;
Chris Masonf254e522007-03-29 15:15:27 -04001340 ret = prepare_pages(NULL, root, file, pages, num_pages,
Chris Mason75dfe392007-03-29 11:56:46 -04001341 pos, write_bytes);
1342 BUG_ON(ret);
1343 ret = btrfs_copy_from_user(pos, num_pages,
1344 write_bytes, pages, buf);
1345 BUG_ON(ret);
1346
Chris Masonf254e522007-03-29 15:15:27 -04001347 ret = dirty_and_release_pages(NULL, root, file, pages,
Chris Mason75dfe392007-03-29 11:56:46 -04001348 num_pages, pos, write_bytes);
1349 BUG_ON(ret);
1350 btrfs_drop_pages(pages, num_pages);
1351
Chris Mason75dfe392007-03-29 11:56:46 -04001352 buf += write_bytes;
1353 count -= write_bytes;
1354 pos += write_bytes;
1355 num_written += write_bytes;
1356
1357 balance_dirty_pages_ratelimited(inode->i_mapping);
1358 cond_resched();
1359 }
1360 mutex_unlock(&inode->i_mutex);
1361out:
1362 *ppos = pos;
1363 current->backing_dev_info = NULL;
1364 return num_written ? num_written : err;
1365}
1366
Chris Masone8f05c42007-04-04 14:30:09 -04001367#if 0
Chris Masondf24a2b2007-04-04 09:36:31 -04001368static ssize_t inline_one_page(struct btrfs_root *root, struct inode *inode,
1369 struct page *page, loff_t pos,
1370 size_t offset, size_t write_bytes)
1371{
1372 struct btrfs_path *path;
1373 struct btrfs_trans_handle *trans;
1374 struct btrfs_key key;
1375 struct btrfs_leaf *leaf;
1376 struct btrfs_key found_key;
1377 int ret;
1378 size_t copy_size = 0;
1379 char *dst = NULL;
1380 int err = 0;
1381 size_t num_written = 0;
1382
1383 path = btrfs_alloc_path();
1384 BUG_ON(!path);
1385 mutex_lock(&root->fs_info->fs_mutex);
1386 trans = btrfs_start_transaction(root, 1);
1387 key.objectid = inode->i_ino;
1388 key.flags = 0;
1389 btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY);
1390
1391again:
1392 key.offset = pos;
1393 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1394 if (ret < 0) {
1395 err = ret;
1396 goto out;
1397 }
1398 if (ret == 0) {
1399 leaf = btrfs_buffer_leaf(path->nodes[0]);
1400 btrfs_disk_key_to_cpu(&found_key,
1401 &leaf->items[path->slots[0]].key);
1402 copy_size = btrfs_item_size(leaf->items + path->slots[0]);
1403 dst = btrfs_item_ptr(leaf, path->slots[0], char);
1404 copy_size = min(write_bytes, copy_size);
1405 goto copyit;
1406 } else {
1407 int slot = path->slots[0];
1408 if (slot > 0) {
1409 slot--;
1410 }
1411 // FIXME find max key
1412 leaf = btrfs_buffer_leaf(path->nodes[0]);
1413 btrfs_disk_key_to_cpu(&found_key,
1414 &leaf->items[slot].key);
1415 if (found_key.objectid != inode->i_ino)
1416 goto insert;
1417 if (btrfs_key_type(&found_key) != BTRFS_INLINE_DATA_KEY)
1418 goto insert;
1419 copy_size = btrfs_item_size(leaf->items + slot);
1420 if (found_key.offset + copy_size <= pos)
1421 goto insert;
1422 dst = btrfs_item_ptr(leaf, path->slots[0], char);
1423 dst += pos - found_key.offset;
1424 copy_size = copy_size - (pos - found_key.offset);
1425 BUG_ON(copy_size < 0);
1426 copy_size = min(write_bytes, copy_size);
1427 WARN_ON(copy_size == 0);
1428 goto copyit;
1429 }
1430insert:
1431 btrfs_release_path(root, path);
Chris Masond4dbff92007-04-04 14:08:15 -04001432 copy_size = min(write_bytes,
1433 (size_t)BTRFS_LEAF_DATA_SIZE(root) -
1434 sizeof(struct btrfs_item) * 4);
Chris Masondf24a2b2007-04-04 09:36:31 -04001435 ret = btrfs_insert_empty_item(trans, root, path, &key, copy_size);
1436 BUG_ON(ret);
1437 dst = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1438 path->slots[0], char);
1439copyit:
1440 WARN_ON(copy_size == 0);
1441 WARN_ON(dst + copy_size >
1442 btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
1443 path->slots[0], char) +
1444 btrfs_item_size(btrfs_buffer_leaf(path->nodes[0])->items +
1445 path->slots[0]));
1446 btrfs_memcpy(root, path->nodes[0]->b_data, dst,
1447 page_address(page) + offset, copy_size);
1448 mark_buffer_dirty(path->nodes[0]);
1449 btrfs_release_path(root, path);
1450 pos += copy_size;
1451 offset += copy_size;
1452 num_written += copy_size;
1453 write_bytes -= copy_size;
1454 if (write_bytes)
1455 goto again;
1456out:
1457 btrfs_free_path(path);
1458 ret = btrfs_end_transaction(trans, root);
1459 BUG_ON(ret);
1460 mutex_unlock(&root->fs_info->fs_mutex);
1461 return num_written ? num_written : err;
1462}
1463
1464static ssize_t btrfs_file_inline_write(struct file *file,
1465 const char __user *buf,
1466 size_t count, loff_t *ppos)
1467{
1468 loff_t pos;
1469 size_t num_written = 0;
1470 int err = 0;
1471 int ret = 0;
1472 struct inode *inode = file->f_path.dentry->d_inode;
Chris Masond6e4a422007-04-06 15:37:36 -04001473 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masondf24a2b2007-04-04 09:36:31 -04001474 unsigned long page_index;
1475
1476 if (file->f_flags & O_DIRECT)
1477 return -EINVAL;
1478 pos = *ppos;
1479
1480 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
1481 current->backing_dev_info = inode->i_mapping->backing_dev_info;
1482 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1483 if (err)
1484 goto out;
1485 if (count == 0)
1486 goto out;
1487 err = remove_suid(file->f_path.dentry);
1488 if (err)
1489 goto out;
1490 file_update_time(file);
1491 mutex_lock(&inode->i_mutex);
1492 while(count > 0) {
1493 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
1494 size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset);
1495 struct page *page;
1496
1497 page_index = pos >> PAGE_CACHE_SHIFT;
1498 page = grab_cache_page(inode->i_mapping, page_index);
1499 if (!PageUptodate(page)) {
1500 ret = mpage_readpage(page, btrfs_get_block);
1501 BUG_ON(ret);
1502 lock_page(page);
1503 }
1504 ret = btrfs_copy_from_user(pos, 1,
1505 write_bytes, &page, buf);
1506 BUG_ON(ret);
1507 write_bytes = inline_one_page(root, inode, page, pos,
1508 offset, write_bytes);
1509 SetPageUptodate(page);
1510 if (write_bytes > 0 && pos + write_bytes > inode->i_size) {
1511 i_size_write(inode, pos + write_bytes);
1512 mark_inode_dirty(inode);
1513 }
1514 page_cache_release(page);
1515 unlock_page(page);
1516 if (write_bytes < 0)
1517 goto out_unlock;
1518 buf += write_bytes;
1519 count -= write_bytes;
1520 pos += write_bytes;
1521 num_written += write_bytes;
1522
1523 balance_dirty_pages_ratelimited(inode->i_mapping);
1524 cond_resched();
1525 }
1526out_unlock:
1527 mutex_unlock(&inode->i_mutex);
1528out:
1529 *ppos = pos;
1530 current->backing_dev_info = NULL;
1531 return num_written ? num_written : err;
1532}
Chris Masone8f05c42007-04-04 14:30:09 -04001533#endif
Chris Masondf24a2b2007-04-04 09:36:31 -04001534
Chris Masonf254e522007-03-29 15:15:27 -04001535static int btrfs_read_actor(read_descriptor_t *desc, struct page *page,
1536 unsigned long offset, unsigned long size)
1537{
1538 char *kaddr;
1539 unsigned long left, count = desc->count;
Chris Masond6e4a422007-04-06 15:37:36 -04001540 struct inode *inode = page->mapping->host;
Chris Masonf254e522007-03-29 15:15:27 -04001541
1542 if (size > count)
1543 size = count;
1544
1545 if (!PageChecked(page)) {
1546 /* FIXME, do it per block */
Chris Masond6e4a422007-04-06 15:37:36 -04001547 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonf254e522007-03-29 15:15:27 -04001548 int ret = btrfs_csum_verify_file_block(root,
1549 page->mapping->host->i_ino,
1550 page->index << PAGE_CACHE_SHIFT,
1551 kmap(page), PAGE_CACHE_SIZE);
1552 if (ret) {
1553 printk("failed to verify ino %lu page %lu\n",
1554 page->mapping->host->i_ino,
1555 page->index);
1556 memset(page_address(page), 0, PAGE_CACHE_SIZE);
1557 }
1558 SetPageChecked(page);
1559 kunmap(page);
1560 }
1561 /*
1562 * Faults on the destination of a read are common, so do it before
1563 * taking the kmap.
1564 */
1565 if (!fault_in_pages_writeable(desc->arg.buf, size)) {
1566 kaddr = kmap_atomic(page, KM_USER0);
1567 left = __copy_to_user_inatomic(desc->arg.buf,
1568 kaddr + offset, size);
1569 kunmap_atomic(kaddr, KM_USER0);
1570 if (left == 0)
1571 goto success;
1572 }
1573
1574 /* Do it the slow way */
1575 kaddr = kmap(page);
1576 left = __copy_to_user(desc->arg.buf, kaddr + offset, size);
1577 kunmap(page);
1578
1579 if (left) {
1580 size -= left;
1581 desc->error = -EFAULT;
1582 }
1583success:
1584 desc->count = count - size;
1585 desc->written += size;
1586 desc->arg.buf += size;
1587 return size;
1588}
1589
1590/**
1591 * btrfs_file_aio_read - filesystem read routine
1592 * @iocb: kernel I/O control block
1593 * @iov: io vector request
1594 * @nr_segs: number of segments in the iovec
1595 * @pos: current file position
1596 */
1597static ssize_t btrfs_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
1598 unsigned long nr_segs, loff_t pos)
1599{
1600 struct file *filp = iocb->ki_filp;
1601 ssize_t retval;
1602 unsigned long seg;
1603 size_t count;
1604 loff_t *ppos = &iocb->ki_pos;
1605
1606 count = 0;
1607 for (seg = 0; seg < nr_segs; seg++) {
1608 const struct iovec *iv = &iov[seg];
1609
1610 /*
1611 * If any segment has a negative length, or the cumulative
1612 * length ever wraps negative then return -EINVAL.
1613 */
1614 count += iv->iov_len;
1615 if (unlikely((ssize_t)(count|iv->iov_len) < 0))
1616 return -EINVAL;
1617 if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
1618 continue;
1619 if (seg == 0)
1620 return -EFAULT;
1621 nr_segs = seg;
1622 count -= iv->iov_len; /* This segment is no good */
1623 break;
1624 }
1625 retval = 0;
1626 if (count) {
1627 for (seg = 0; seg < nr_segs; seg++) {
1628 read_descriptor_t desc;
1629
1630 desc.written = 0;
1631 desc.arg.buf = iov[seg].iov_base;
1632 desc.count = iov[seg].iov_len;
1633 if (desc.count == 0)
1634 continue;
1635 desc.error = 0;
1636 do_generic_file_read(filp, ppos, &desc,
1637 btrfs_read_actor);
1638 retval += desc.written;
1639 if (desc.error) {
1640 retval = retval ?: desc.error;
1641 break;
1642 }
1643 }
1644 }
1645 return retval;
1646}
1647
Chris Mason2c90e5d2007-04-02 10:50:19 -04001648static struct kmem_cache *btrfs_inode_cachep;
1649struct kmem_cache *btrfs_trans_handle_cachep;
1650struct kmem_cache *btrfs_transaction_cachep;
1651struct kmem_cache *btrfs_bit_radix_cachep;
1652struct kmem_cache *btrfs_path_cachep;
1653
1654/*
1655 * Called inside transaction, so use GFP_NOFS
1656 */
1657static struct inode *btrfs_alloc_inode(struct super_block *sb)
1658{
1659 struct btrfs_inode *ei;
1660
1661 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
1662 if (!ei)
1663 return NULL;
1664 ei->magic = 0xDEADBEEF;
1665 ei->magic2 = 0xDEADBEAF;
1666 return &ei->vfs_inode;
1667}
1668
1669static void btrfs_destroy_inode(struct inode *inode)
1670{
1671 struct btrfs_inode *ei = BTRFS_I(inode);
1672 WARN_ON(ei->magic != 0xDEADBEEF);
1673 WARN_ON(ei->magic2 != 0xDEADBEAF);
1674 WARN_ON(!list_empty(&inode->i_dentry));
Chris Mason2c90e5d2007-04-02 10:50:19 -04001675 WARN_ON(inode->i_data.nrpages);
1676
1677 ei->magic = 0;
1678 ei->magic2 = 0;
1679 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
1680}
1681
1682static void init_once(void * foo, struct kmem_cache * cachep,
1683 unsigned long flags)
1684{
1685 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
1686
1687 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
1688 SLAB_CTOR_CONSTRUCTOR) {
1689 inode_init_once(&ei->vfs_inode);
1690 }
1691}
1692
1693static int init_inodecache(void)
1694{
1695 btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
1696 sizeof(struct btrfs_inode),
1697 0, (SLAB_RECLAIM_ACCOUNT|
1698 SLAB_MEM_SPREAD),
1699 init_once, NULL);
1700 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
1701 sizeof(struct btrfs_trans_handle),
1702 0, (SLAB_RECLAIM_ACCOUNT|
1703 SLAB_MEM_SPREAD),
1704 NULL, NULL);
1705 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
1706 sizeof(struct btrfs_transaction),
1707 0, (SLAB_RECLAIM_ACCOUNT|
1708 SLAB_MEM_SPREAD),
1709 NULL, NULL);
1710 btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
1711 sizeof(struct btrfs_transaction),
1712 0, (SLAB_RECLAIM_ACCOUNT|
1713 SLAB_MEM_SPREAD),
1714 NULL, NULL);
1715 btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix",
1716 256,
1717 0, (SLAB_RECLAIM_ACCOUNT|
1718 SLAB_MEM_SPREAD |
1719 SLAB_DESTROY_BY_RCU),
1720 NULL, NULL);
1721 if (btrfs_inode_cachep == NULL || btrfs_trans_handle_cachep == NULL ||
1722 btrfs_transaction_cachep == NULL || btrfs_bit_radix_cachep == NULL)
1723 return -ENOMEM;
1724 return 0;
1725}
1726
1727static void destroy_inodecache(void)
1728{
1729 kmem_cache_destroy(btrfs_inode_cachep);
1730 kmem_cache_destroy(btrfs_trans_handle_cachep);
1731 kmem_cache_destroy(btrfs_transaction_cachep);
1732 kmem_cache_destroy(btrfs_bit_radix_cachep);
1733 kmem_cache_destroy(btrfs_path_cachep);
1734}
1735
Chris Mason2e635a22007-03-21 11:12:56 -04001736static int btrfs_get_sb(struct file_system_type *fs_type,
1737 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
1738{
1739 return get_sb_bdev(fs_type, flags, dev_name, data,
1740 btrfs_fill_super, mnt);
1741}
1742
1743static struct file_system_type btrfs_fs_type = {
1744 .owner = THIS_MODULE,
1745 .name = "btrfs",
1746 .get_sb = btrfs_get_sb,
1747 .kill_sb = kill_block_super,
1748 .fs_flags = FS_REQUIRES_DEV,
1749};
1750
Chris Masone20d96d2007-03-22 12:13:20 -04001751static struct super_operations btrfs_super_ops = {
1752 .statfs = simple_statfs,
Chris Mason134e9732007-03-25 13:44:56 -04001753 .delete_inode = btrfs_delete_inode,
Chris Masone20d96d2007-03-22 12:13:20 -04001754 .put_super = btrfs_put_super,
1755 .read_inode = btrfs_read_locked_inode,
Chris Masond5719762007-03-23 10:01:08 -04001756 .write_super = btrfs_write_super,
1757 .sync_fs = btrfs_sync_fs,
Chris Mason4730a4b2007-03-26 12:00:39 -04001758 .write_inode = btrfs_write_inode,
Chris Mason2c90e5d2007-04-02 10:50:19 -04001759 .alloc_inode = btrfs_alloc_inode,
1760 .destroy_inode = btrfs_destroy_inode,
Chris Masone20d96d2007-03-22 12:13:20 -04001761};
1762
1763static struct inode_operations btrfs_dir_inode_operations = {
1764 .lookup = btrfs_lookup,
Chris Masond5719762007-03-23 10:01:08 -04001765 .create = btrfs_create,
Chris Mason134e9732007-03-25 13:44:56 -04001766 .unlink = btrfs_unlink,
Chris Masonf7922032007-03-25 20:17:36 -04001767 .mkdir = btrfs_mkdir,
Chris Mason5f443fd2007-03-27 13:42:32 -04001768 .rmdir = btrfs_rmdir,
Chris Masone20d96d2007-03-22 12:13:20 -04001769};
1770
Chris Masond6e4a422007-04-06 15:37:36 -04001771static struct inode_operations btrfs_dir_ro_inode_operations = {
1772 .lookup = btrfs_lookup,
1773};
1774
Chris Masone20d96d2007-03-22 12:13:20 -04001775static struct file_operations btrfs_dir_file_operations = {
1776 .llseek = generic_file_llseek,
1777 .read = generic_read_dir,
1778 .readdir = btrfs_readdir,
1779};
1780
Chris Masondee26a92007-03-26 16:00:06 -04001781static struct address_space_operations btrfs_aops = {
1782 .readpage = btrfs_readpage,
Chris Masone8f05c42007-04-04 14:30:09 -04001783 .readpages = btrfs_readpages,
Chris Masondee26a92007-03-26 16:00:06 -04001784 .writepage = btrfs_writepage,
1785 .sync_page = block_sync_page,
1786 .prepare_write = btrfs_prepare_write,
Chris Mason75dfe392007-03-29 11:56:46 -04001787 .commit_write = btrfs_commit_write,
Chris Masondee26a92007-03-26 16:00:06 -04001788};
1789
1790static struct inode_operations btrfs_file_inode_operations = {
Chris Masonf4b9aa82007-03-27 11:05:53 -04001791 .truncate = btrfs_truncate,
Chris Masondee26a92007-03-26 16:00:06 -04001792};
1793
1794static struct file_operations btrfs_file_operations = {
1795 .llseek = generic_file_llseek,
1796 .read = do_sync_read,
Chris Masone8f05c42007-04-04 14:30:09 -04001797 .aio_read = btrfs_file_aio_read,
1798 .write = btrfs_file_write,
Chris Masondee26a92007-03-26 16:00:06 -04001799 .mmap = generic_file_mmap,
1800 .open = generic_file_open,
Chris Masondee26a92007-03-26 16:00:06 -04001801};
Chris Masone20d96d2007-03-22 12:13:20 -04001802
Chris Mason2e635a22007-03-21 11:12:56 -04001803static int __init init_btrfs_fs(void)
1804{
Chris Mason2c90e5d2007-04-02 10:50:19 -04001805 int err;
Chris Mason2e635a22007-03-21 11:12:56 -04001806 printk("btrfs loaded!\n");
Chris Mason2c90e5d2007-04-02 10:50:19 -04001807 err = init_inodecache();
1808 if (err)
1809 return err;
Chris Masond6e4a422007-04-06 15:37:36 -04001810 kset_set_kset_s(&btrfs_subsys, fs_subsys);
1811 err = subsystem_register(&btrfs_subsys);
1812 if (err)
1813 goto out;
Chris Mason2e635a22007-03-21 11:12:56 -04001814 return register_filesystem(&btrfs_fs_type);
Chris Masond6e4a422007-04-06 15:37:36 -04001815out:
1816 destroy_inodecache();
1817 return err;
Chris Mason2e635a22007-03-21 11:12:56 -04001818}
1819
1820static void __exit exit_btrfs_fs(void)
1821{
Chris Mason2c90e5d2007-04-02 10:50:19 -04001822 destroy_inodecache();
Chris Mason2e635a22007-03-21 11:12:56 -04001823 unregister_filesystem(&btrfs_fs_type);
Chris Masond6e4a422007-04-06 15:37:36 -04001824 subsystem_unregister(&btrfs_subsys);
Chris Mason2e635a22007-03-21 11:12:56 -04001825 printk("btrfs unloaded\n");
1826}
1827
1828module_init(init_btrfs_fs)
1829module_exit(exit_btrfs_fs)
1830
1831MODULE_LICENSE("GPL");