blob: f96bd92155cc2d183a2323c3f115f8f113991ef4 [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>
11#include "ctree.h"
Chris Masone20d96d2007-03-22 12:13:20 -040012#include "disk-io.h"
Chris Masond5719762007-03-23 10:01:08 -040013#include "transaction.h"
Chris Mason2e635a22007-03-21 11:12:56 -040014
15#define BTRFS_SUPER_MAGIC 0x9123682E
Chris Masone20d96d2007-03-22 12:13:20 -040016
17static struct inode_operations btrfs_dir_inode_operations;
18static struct super_operations btrfs_super_ops;
19static struct file_operations btrfs_dir_file_operations;
20
Chris Mason2e635a22007-03-21 11:12:56 -040021#if 0
22/* some random number */
23
24static struct super_operations ramfs_ops;
Chris Mason2e635a22007-03-21 11:12:56 -040025
26static struct backing_dev_info ramfs_backing_dev_info = {
27 .ra_pages = 0, /* No readahead */
28 .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK |
29 BDI_CAP_MAP_DIRECT | BDI_CAP_MAP_COPY |
30 BDI_CAP_READ_MAP | BDI_CAP_WRITE_MAP | BDI_CAP_EXEC_MAP,
31};
32
33struct inode *ramfs_get_inode(struct super_block *sb, int mode, dev_t dev)
34{
35 struct inode * inode = new_inode(sb);
36
37 if (inode) {
38 inode->i_mode = mode;
39 inode->i_uid = current->fsuid;
40 inode->i_gid = current->fsgid;
41 inode->i_blocks = 0;
42 inode->i_mapping->a_ops = &ramfs_aops;
43 inode->i_mapping->backing_dev_info = &ramfs_backing_dev_info;
44 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
45 switch (mode & S_IFMT) {
46 default:
47 init_special_inode(inode, mode, dev);
48 break;
49 case S_IFREG:
50 inode->i_op = &ramfs_file_inode_operations;
51 inode->i_fop = &ramfs_file_operations;
52 break;
53 case S_IFDIR:
54 inode->i_op = &ramfs_dir_inode_operations;
55 inode->i_fop = &simple_dir_operations;
56
57 /* directory inodes start off with i_nlink == 2 (for "." entry) */
58 inc_nlink(inode);
59 break;
60 case S_IFLNK:
61 inode->i_op = &page_symlink_inode_operations;
62 break;
63 }
64 }
65 return inode;
66}
67
68/*
69 * File creation. Allocate an inode, and we're done..
70 */
71/* SMP-safe */
72static int
73ramfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
74{
75 struct inode * inode = ramfs_get_inode(dir->i_sb, mode, dev);
76 int error = -ENOSPC;
77
78 if (inode) {
79 if (dir->i_mode & S_ISGID) {
80 inode->i_gid = dir->i_gid;
81 if (S_ISDIR(mode))
82 inode->i_mode |= S_ISGID;
83 }
84 d_instantiate(dentry, inode);
85 dget(dentry); /* Extra count - pin the dentry in core */
86 error = 0;
87 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
88 }
89 return error;
90}
91
92static int ramfs_mkdir(struct inode * dir, struct dentry * dentry, int mode)
93{
94 int retval = ramfs_mknod(dir, dentry, mode | S_IFDIR, 0);
95 if (!retval)
96 inc_nlink(dir);
97 return retval;
98}
99
100static int ramfs_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd)
101{
102 return ramfs_mknod(dir, dentry, mode | S_IFREG, 0);
103}
104
105static int ramfs_symlink(struct inode * dir, struct dentry *dentry, const char * symname)
106{
107 struct inode *inode;
108 int error = -ENOSPC;
109
110 inode = ramfs_get_inode(dir->i_sb, S_IFLNK|S_IRWXUGO, 0);
111 if (inode) {
112 int l = strlen(symname)+1;
113 error = page_symlink(inode, symname, l);
114 if (!error) {
115 if (dir->i_mode & S_ISGID)
116 inode->i_gid = dir->i_gid;
117 d_instantiate(dentry, inode);
118 dget(dentry);
119 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
120 } else
121 iput(inode);
122 }
123 return error;
124}
125
126static struct inode_operations ramfs_dir_inode_operations = {
127 .create = ramfs_create,
128 .lookup = simple_lookup,
129 .link = simple_link,
130 .unlink = simple_unlink,
131 .symlink = ramfs_symlink,
132 .mkdir = ramfs_mkdir,
133 .rmdir = simple_rmdir,
134 .mknod = ramfs_mknod,
135 .rename = simple_rename,
136};
137#endif
138
Chris Masone20d96d2007-03-22 12:13:20 -0400139static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason2e635a22007-03-21 11:12:56 -0400140{
Chris Masone20d96d2007-03-22 12:13:20 -0400141 struct btrfs_path path;
142 struct btrfs_inode_item *inode_item;
143 struct btrfs_root *root = btrfs_sb(inode->i_sb);
144 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -0400145 btrfs_init_path(&path);
146 ret = btrfs_lookup_inode(NULL, root, &path, inode->i_ino, 0);
147 if (ret) {
148 make_bad_inode(inode);
Chris Mason78fae272007-03-25 11:35:08 -0400149 btrfs_release_path(root, &path);
Chris Masone20d96d2007-03-22 12:13:20 -0400150 return;
Chris Mason2e635a22007-03-21 11:12:56 -0400151 }
Chris Masone20d96d2007-03-22 12:13:20 -0400152 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]),
153 path.slots[0],
154 struct btrfs_inode_item);
155
Chris Masone20d96d2007-03-22 12:13:20 -0400156 inode->i_mode = btrfs_inode_mode(inode_item);
157 inode->i_nlink = btrfs_inode_nlink(inode_item);
158 inode->i_uid = btrfs_inode_uid(inode_item);
159 inode->i_gid = btrfs_inode_gid(inode_item);
160 inode->i_size = btrfs_inode_size(inode_item);
161 inode->i_atime.tv_sec = btrfs_timespec_sec(&inode_item->atime);
162 inode->i_atime.tv_nsec = btrfs_timespec_nsec(&inode_item->atime);
163 inode->i_mtime.tv_sec = btrfs_timespec_sec(&inode_item->mtime);
164 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(&inode_item->mtime);
165 inode->i_ctime.tv_sec = btrfs_timespec_sec(&inode_item->ctime);
166 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(&inode_item->ctime);
167 inode->i_blocks = btrfs_inode_nblocks(inode_item);
168 inode->i_generation = btrfs_inode_generation(inode_item);
Chris Masone20d96d2007-03-22 12:13:20 -0400169 btrfs_release_path(root, &path);
170 switch (inode->i_mode & S_IFMT) {
171#if 0
172 default:
173 init_special_inode(inode, inode->i_mode,
174 btrfs_inode_rdev(inode_item));
175 break;
176#endif
177 case S_IFREG:
Chris Masone20d96d2007-03-22 12:13:20 -0400178 break;
179 case S_IFDIR:
Chris Masone20d96d2007-03-22 12:13:20 -0400180 inode->i_op = &btrfs_dir_inode_operations;
181 inode->i_fop = &btrfs_dir_file_operations;
182 break;
183 case S_IFLNK:
Chris Masone20d96d2007-03-22 12:13:20 -0400184 // inode->i_op = &page_symlink_inode_operations;
185 break;
186 }
Chris Masone20d96d2007-03-22 12:13:20 -0400187 return;
Chris Mason2e635a22007-03-21 11:12:56 -0400188}
189
Chris Mason134e9732007-03-25 13:44:56 -0400190static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
191{
192 struct btrfs_path path;
193 struct btrfs_root *root;
194 struct btrfs_trans_handle *trans;
195 const char *name = dentry->d_name.name;
196 int name_len = dentry->d_name.len;
197 int ret;
198 u64 objectid;
199 struct btrfs_dir_item *di;
200
201 btrfs_init_path(&path);
202 root = btrfs_sb(dir->i_sb);
203 mutex_lock(&root->fs_info->fs_mutex);
204 trans = btrfs_start_transaction(root, 1);
205
206 ret = btrfs_lookup_dir_item(trans, root, &path, dir->i_ino,
207 name, name_len, -1);
208 if (ret < 0)
209 goto err;
210 if (ret > 0) {
211 ret = -ENOENT;
212 goto err;
213 }
214 di = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]), path.slots[0],
215 struct btrfs_dir_item);
216 objectid = btrfs_dir_objectid(di);
217
218 ret = btrfs_del_item(trans, root, &path);
219 BUG_ON(ret);
220 dentry->d_inode->i_ctime = dir->i_ctime;
221err:
222 btrfs_release_path(root, &path);
223 btrfs_end_transaction(trans, root);
224 mutex_unlock(&root->fs_info->fs_mutex);
225 if (ret == 0)
226 inode_dec_link_count(dentry->d_inode);
227 return ret;
228}
229
230static int btrfs_free_inode(struct btrfs_trans_handle *trans,
231 struct btrfs_root *root,
232 struct inode *inode)
233{
234 u64 objectid = inode->i_ino;
235 struct btrfs_path path;
236 struct btrfs_inode_map_item *map;
237 struct btrfs_key stat_data_key;
238 int ret;
239 clear_inode(inode);
240 btrfs_init_path(&path);
241 ret = btrfs_lookup_inode_map(trans, root, &path, objectid, -1);
242 if (ret) {
243 if (ret > 0)
244 ret = -ENOENT;
245 btrfs_release_path(root, &path);
246 goto error;
247 }
248 map = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]), path.slots[0],
249 struct btrfs_inode_map_item);
250 btrfs_disk_key_to_cpu(&stat_data_key, &map->key);
251 ret = btrfs_del_item(trans, root->fs_info->inode_root, &path);
252 BUG_ON(ret);
253 btrfs_release_path(root, &path);
254 btrfs_init_path(&path);
255
256 ret = btrfs_lookup_inode(trans, root, &path, objectid, -1);
257 BUG_ON(ret);
258 ret = btrfs_del_item(trans, root, &path);
259 BUG_ON(ret);
260 btrfs_release_path(root, &path);
261error:
262 return ret;
263}
264
265static void btrfs_delete_inode(struct inode *inode)
266{
267 struct btrfs_trans_handle *trans;
268 struct btrfs_root *root = btrfs_sb(inode->i_sb);
269 truncate_inode_pages(&inode->i_data, 0);
270 if (is_bad_inode(inode)) {
271 goto no_delete;
272 }
273 inode->i_size = 0;
274 if (inode->i_blocks)
275 WARN_ON(1);
276
277 mutex_lock(&root->fs_info->fs_mutex);
278 trans = btrfs_start_transaction(root, 1);
279 btrfs_free_inode(trans, root, inode);
280 btrfs_end_transaction(trans, root);
281 mutex_unlock(&root->fs_info->fs_mutex);
282 return;
283no_delete:
284 clear_inode(inode);
285}
286
287
Chris Masone20d96d2007-03-22 12:13:20 -0400288static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
289 ino_t *ino)
290{
291 const char *name = dentry->d_name.name;
292 int namelen = dentry->d_name.len;
293 struct btrfs_dir_item *di;
294 struct btrfs_path path;
295 struct btrfs_root *root = btrfs_sb(dir->i_sb);
296 int ret;
297
298 btrfs_init_path(&path);
299 ret = btrfs_lookup_dir_item(NULL, root, &path, dir->i_ino, name,
300 namelen, 0);
Chris Mason7f5c1512007-03-23 15:56:19 -0400301 if (ret || !btrfs_match_dir_item_name(root, &path, name, namelen)) {
Chris Masone20d96d2007-03-22 12:13:20 -0400302 *ino = 0;
303 goto out;
304 }
305 di = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]), path.slots[0],
306 struct btrfs_dir_item);
307 *ino = btrfs_dir_objectid(di);
308out:
309 btrfs_release_path(root, &path);
310 return ret;
311}
312
313static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
314 struct nameidata *nd)
315{
316 struct inode * inode;
317 ino_t ino;
318 int ret;
319
320 if (dentry->d_name.len > BTRFS_NAME_LEN)
321 return ERR_PTR(-ENAMETOOLONG);
322
323 ret = btrfs_inode_by_name(dir, dentry, &ino);
324 if (ret < 0)
325 return ERR_PTR(ret);
326 inode = NULL;
327 if (ino) {
Chris Masone20d96d2007-03-22 12:13:20 -0400328 inode = iget(dir->i_sb, ino);
329 if (!inode)
330 return ERR_PTR(-EACCES);
331 }
332 return d_splice_alias(inode, dentry);
333}
334
335static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
336{
337 struct inode *inode = filp->f_path.dentry->d_inode;
338 struct btrfs_root *root = btrfs_sb(inode->i_sb);
339 struct btrfs_item *item;
340 struct btrfs_dir_item *di;
341 struct btrfs_key key;
342 struct btrfs_path path;
343 int ret;
344 u32 nritems;
345 struct btrfs_leaf *leaf;
346 int slot;
347 int advance;
348 unsigned char d_type = DT_UNKNOWN;
Chris Mason7f5c1512007-03-23 15:56:19 -0400349 int over = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400350
351 key.objectid = inode->i_ino;
Chris Masone20d96d2007-03-22 12:13:20 -0400352 key.flags = 0;
353 btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
354 key.offset = filp->f_pos;
355 btrfs_init_path(&path);
356 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
357 if (ret < 0) {
358 goto err;
359 }
Chris Mason7f5c1512007-03-23 15:56:19 -0400360 advance = 0;
Chris Masone20d96d2007-03-22 12:13:20 -0400361 while(1) {
362 leaf = btrfs_buffer_leaf(path.nodes[0]);
363 nritems = btrfs_header_nritems(&leaf->header);
364 slot = path.slots[0];
Chris Masone20d96d2007-03-22 12:13:20 -0400365 if (advance) {
Chris Masone20d96d2007-03-22 12:13:20 -0400366 if (slot == nritems -1) {
367 ret = btrfs_next_leaf(root, &path);
368 if (ret)
369 break;
370 leaf = btrfs_buffer_leaf(path.nodes[0]);
371 nritems = btrfs_header_nritems(&leaf->header);
372 slot = path.slots[0];
Chris Mason134e9732007-03-25 13:44:56 -0400373#if 0
374 page_cache_readahead(
375 inode->i_sb->s_bdev->bd_inode->i_mapping,
376 &filp->f_ra, filp,
377 path.nodes[0]->b_blocknr >>
378 (PAGE_CACHE_SHIFT - inode->i_blkbits), 1);
379#endif
Chris Masone20d96d2007-03-22 12:13:20 -0400380 } else {
381 slot++;
382 path.slots[0]++;
383 }
384 }
385 advance = 1;
386 item = leaf->items + slot;
Chris Masone20d96d2007-03-22 12:13:20 -0400387 if (btrfs_disk_key_objectid(&item->key) != key.objectid)
388 break;
389 if (btrfs_disk_key_type(&item->key) != BTRFS_DIR_ITEM_KEY)
390 continue;
Chris Mason7f5c1512007-03-23 15:56:19 -0400391 if (btrfs_disk_key_offset(&item->key) < filp->f_pos)
392 continue;
Chris Masone20d96d2007-03-22 12:13:20 -0400393 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Chris Masone20d96d2007-03-22 12:13:20 -0400394 over = filldir(dirent, (const char *)(di + 1),
395 btrfs_dir_name_len(di),
396 btrfs_disk_key_offset(&item->key),
397 btrfs_dir_objectid(di), d_type);
Chris Mason7f5c1512007-03-23 15:56:19 -0400398 if (over) {
399 filp->f_pos = btrfs_disk_key_offset(&item->key);
Chris Masone20d96d2007-03-22 12:13:20 -0400400 break;
Chris Mason7f5c1512007-03-23 15:56:19 -0400401 }
Chris Masone20d96d2007-03-22 12:13:20 -0400402 filp->f_pos = btrfs_disk_key_offset(&item->key) + 1;
403 }
Chris Masone20d96d2007-03-22 12:13:20 -0400404 ret = 0;
405err:
406 btrfs_release_path(root, &path);
407 return ret;
408}
409
410static void btrfs_put_super (struct super_block * sb)
411{
412 struct btrfs_root *root = btrfs_sb(sb);
413 int ret;
414
415 ret = close_ctree(root);
416 if (ret) {
417 printk("close ctree returns %d\n", ret);
418 }
419 sb->s_fs_info = NULL;
420}
Chris Mason2e635a22007-03-21 11:12:56 -0400421
422static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
423{
424 struct inode * inode;
Chris Masone20d96d2007-03-22 12:13:20 -0400425 struct dentry * root_dentry;
426 struct btrfs_super_block *disk_super;
427 struct buffer_head *bh;
428 struct btrfs_root *root;
Chris Mason2e635a22007-03-21 11:12:56 -0400429
430 sb->s_maxbytes = MAX_LFS_FILESIZE;
431 sb->s_blocksize = PAGE_CACHE_SIZE;
432 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
433 sb->s_magic = BTRFS_SUPER_MAGIC;
Chris Masone20d96d2007-03-22 12:13:20 -0400434 sb->s_op = &btrfs_super_ops;
Chris Mason2e635a22007-03-21 11:12:56 -0400435 sb->s_time_gran = 1;
Chris Masone20d96d2007-03-22 12:13:20 -0400436
437 bh = sb_bread(sb, BTRFS_SUPER_INFO_OFFSET / sb->s_blocksize);
438 if (!bh) {
439 printk("btrfs: unable to read on disk super\n");
440 return -EIO;
441 }
442 disk_super = (struct btrfs_super_block *)bh->b_data;
443 root = open_ctree(sb, bh, disk_super);
444 sb->s_fs_info = root;
445 if (!root) {
446 printk("btrfs: open_ctree failed\n");
447 return -EIO;
448 }
449 printk("read in super total blocks %Lu root %Lu\n",
450 btrfs_super_total_blocks(disk_super),
451 btrfs_super_root_dir(disk_super));
452
453 inode = iget_locked(sb, btrfs_super_root_dir(disk_super));
Chris Mason2e635a22007-03-21 11:12:56 -0400454 if (!inode)
455 return -ENOMEM;
Chris Masone20d96d2007-03-22 12:13:20 -0400456 if (inode->i_state & I_NEW) {
457 btrfs_read_locked_inode(inode);
458 unlock_new_inode(inode);
459 }
Chris Mason2e635a22007-03-21 11:12:56 -0400460
Chris Masone20d96d2007-03-22 12:13:20 -0400461 root_dentry = d_alloc_root(inode);
462 if (!root_dentry) {
Chris Mason2e635a22007-03-21 11:12:56 -0400463 iput(inode);
464 return -ENOMEM;
465 }
Chris Masone20d96d2007-03-22 12:13:20 -0400466 sb->s_root = root_dentry;
467
Chris Mason2e635a22007-03-21 11:12:56 -0400468 return 0;
469}
470
Chris Masond5719762007-03-23 10:01:08 -0400471static void fill_inode_item(struct btrfs_inode_item *item,
472 struct inode *inode)
473{
474 btrfs_set_inode_uid(item, inode->i_uid);
475 btrfs_set_inode_gid(item, inode->i_gid);
476 btrfs_set_inode_size(item, inode->i_size);
477 btrfs_set_inode_mode(item, inode->i_mode);
478 btrfs_set_inode_nlink(item, inode->i_nlink);
479 btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
480 btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec);
481 btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec);
482 btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec);
483 btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec);
484 btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
485 btrfs_set_inode_nblocks(item, inode->i_blocks);
486 btrfs_set_inode_generation(item, inode->i_generation);
487}
488
489static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
490 struct inode *dir, int mode)
491{
492 struct inode *inode;
493 struct btrfs_inode_item inode_item;
494 struct btrfs_root *root = btrfs_sb(dir->i_sb);
495 struct btrfs_key key;
496 int ret;
497 u64 objectid;
498
499 inode = new_inode(dir->i_sb);
500 if (!inode)
501 return ERR_PTR(-ENOMEM);
502
503 ret = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
504 BUG_ON(ret);
505
506 inode->i_uid = current->fsuid;
507 inode->i_gid = current->fsgid;
508 inode->i_mode = mode;
509 inode->i_ino = objectid;
510 inode->i_blocks = 0;
511 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
512 fill_inode_item(&inode_item, inode);
513
514
515 key.objectid = objectid;
516 key.flags = 0;
517 key.offset = 0;
518 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
519 ret = btrfs_insert_inode_map(trans, root, objectid, &key);
520 BUG_ON(ret);
521
522 ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
523 BUG_ON(ret);
524
525 insert_inode_hash(inode);
526 // FIXME mark_inode_dirty(inode)
527 return inode;
528}
529
530static int btrfs_add_link(struct btrfs_trans_handle *trans,
531 struct dentry *dentry, struct inode *inode)
532{
533 int ret;
534 ret = btrfs_insert_dir_item(trans, btrfs_sb(inode->i_sb),
535 dentry->d_name.name, dentry->d_name.len,
536 dentry->d_parent->d_inode->i_ino,
537 inode->i_ino, 0);
Chris Masond5719762007-03-23 10:01:08 -0400538 return ret;
539}
540
541static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
542 struct dentry *dentry, struct inode *inode)
543{
544 int err = btrfs_add_link(trans, dentry, inode);
545 if (!err) {
546 d_instantiate(dentry, inode);
547 return 0;
548 }
Chris Masond5719762007-03-23 10:01:08 -0400549 return err;
550}
551
552static int btrfs_create(struct inode *dir, struct dentry *dentry,
553 int mode, struct nameidata *nd)
554{
555 struct btrfs_trans_handle *trans;
556 struct btrfs_root *root = btrfs_sb(dir->i_sb);
557 struct inode *inode;
558 int err;
Chris Mason134e9732007-03-25 13:44:56 -0400559 int drop_inode = 0;
Chris Masond5719762007-03-23 10:01:08 -0400560
Chris Masond561c022007-03-23 19:47:49 -0400561 mutex_lock(&root->fs_info->fs_mutex);
Chris Masond5719762007-03-23 10:01:08 -0400562 trans = btrfs_start_transaction(root, 1);
563 inode = btrfs_new_inode(trans, dir, mode);
564 err = PTR_ERR(inode);
565 if (IS_ERR(inode))
Chris Masond561c022007-03-23 19:47:49 -0400566 goto out_unlock;
Chris Masond5719762007-03-23 10:01:08 -0400567 // FIXME mark the inode dirty
568 err = btrfs_add_nondir(trans, dentry, inode);
Chris Mason134e9732007-03-25 13:44:56 -0400569 if (err)
570 drop_inode = 1;
Chris Masond5719762007-03-23 10:01:08 -0400571 dir->i_sb->s_dirt = 1;
572 btrfs_end_transaction(trans, root);
Chris Masond561c022007-03-23 19:47:49 -0400573out_unlock:
574 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason134e9732007-03-25 13:44:56 -0400575 if (drop_inode) {
576 inode_dec_link_count(inode);
577 iput(inode);
578 }
Chris Masond5719762007-03-23 10:01:08 -0400579 return err;
580}
581
Chris Masond5719762007-03-23 10:01:08 -0400582static int btrfs_sync_fs(struct super_block *sb, int wait)
583{
584 struct btrfs_trans_handle *trans;
585 struct btrfs_root *root;
586 int ret;
Chris Masondf2ce342007-03-23 11:00:45 -0400587
Chris Masond5719762007-03-23 10:01:08 -0400588 sb->s_dirt = 0;
Chris Masond561c022007-03-23 19:47:49 -0400589 if (!wait) {
590 filemap_flush(sb->s_bdev->bd_inode->i_mapping);
591 return 0;
592 }
593 filemap_write_and_wait(sb->s_bdev->bd_inode->i_mapping);
Chris Masondf2ce342007-03-23 11:00:45 -0400594
Chris Masond5719762007-03-23 10:01:08 -0400595 root = btrfs_sb(sb);
Chris Masond561c022007-03-23 19:47:49 -0400596 mutex_lock(&root->fs_info->fs_mutex);
Chris Masond5719762007-03-23 10:01:08 -0400597 trans = btrfs_start_transaction(root, 1);
598 ret = btrfs_commit_transaction(trans, root);
599 sb->s_dirt = 0;
600 BUG_ON(ret);
601printk("btrfs sync_fs\n");
Chris Masond561c022007-03-23 19:47:49 -0400602 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond5719762007-03-23 10:01:08 -0400603 return 0;
604}
605
Chris Masond561c022007-03-23 19:47:49 -0400606static void btrfs_write_super(struct super_block *sb)
607{
608 btrfs_sync_fs(sb, 1);
609}
610
611
Chris Mason2e635a22007-03-21 11:12:56 -0400612static int btrfs_get_sb(struct file_system_type *fs_type,
613 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
614{
615 return get_sb_bdev(fs_type, flags, dev_name, data,
616 btrfs_fill_super, mnt);
617}
618
619static struct file_system_type btrfs_fs_type = {
620 .owner = THIS_MODULE,
621 .name = "btrfs",
622 .get_sb = btrfs_get_sb,
623 .kill_sb = kill_block_super,
624 .fs_flags = FS_REQUIRES_DEV,
625};
626
Chris Masone20d96d2007-03-22 12:13:20 -0400627static struct super_operations btrfs_super_ops = {
628 .statfs = simple_statfs,
Chris Mason134e9732007-03-25 13:44:56 -0400629 .delete_inode = btrfs_delete_inode,
Chris Masone20d96d2007-03-22 12:13:20 -0400630 .put_super = btrfs_put_super,
631 .read_inode = btrfs_read_locked_inode,
Chris Masond5719762007-03-23 10:01:08 -0400632 .write_super = btrfs_write_super,
633 .sync_fs = btrfs_sync_fs,
Chris Masone20d96d2007-03-22 12:13:20 -0400634};
635
636static struct inode_operations btrfs_dir_inode_operations = {
637 .lookup = btrfs_lookup,
Chris Masond5719762007-03-23 10:01:08 -0400638 .create = btrfs_create,
Chris Mason134e9732007-03-25 13:44:56 -0400639 .unlink = btrfs_unlink,
Chris Masone20d96d2007-03-22 12:13:20 -0400640};
641
642static struct file_operations btrfs_dir_file_operations = {
643 .llseek = generic_file_llseek,
644 .read = generic_read_dir,
645 .readdir = btrfs_readdir,
646};
647
648
Chris Mason2e635a22007-03-21 11:12:56 -0400649static int __init init_btrfs_fs(void)
650{
651 printk("btrfs loaded!\n");
652 return register_filesystem(&btrfs_fs_type);
653}
654
655static void __exit exit_btrfs_fs(void)
656{
657 unregister_filesystem(&btrfs_fs_type);
658 printk("btrfs unloaded\n");
659}
660
661module_init(init_btrfs_fs)
662module_exit(exit_btrfs_fs)
663
664MODULE_LICENSE("GPL");