blob: 6080a8133d7105e5a78a077d29f0ed8586f49a2a [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);
149 return;
Chris Mason2e635a22007-03-21 11:12:56 -0400150 }
Chris Masone20d96d2007-03-22 12:13:20 -0400151 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]),
152 path.slots[0],
153 struct btrfs_inode_item);
154
Chris Masone20d96d2007-03-22 12:13:20 -0400155 inode->i_mode = btrfs_inode_mode(inode_item);
156 inode->i_nlink = btrfs_inode_nlink(inode_item);
157 inode->i_uid = btrfs_inode_uid(inode_item);
158 inode->i_gid = btrfs_inode_gid(inode_item);
159 inode->i_size = btrfs_inode_size(inode_item);
160 inode->i_atime.tv_sec = btrfs_timespec_sec(&inode_item->atime);
161 inode->i_atime.tv_nsec = btrfs_timespec_nsec(&inode_item->atime);
162 inode->i_mtime.tv_sec = btrfs_timespec_sec(&inode_item->mtime);
163 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(&inode_item->mtime);
164 inode->i_ctime.tv_sec = btrfs_timespec_sec(&inode_item->ctime);
165 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(&inode_item->ctime);
166 inode->i_blocks = btrfs_inode_nblocks(inode_item);
167 inode->i_generation = btrfs_inode_generation(inode_item);
Chris Masone20d96d2007-03-22 12:13:20 -0400168 btrfs_release_path(root, &path);
169 switch (inode->i_mode & S_IFMT) {
170#if 0
171 default:
172 init_special_inode(inode, inode->i_mode,
173 btrfs_inode_rdev(inode_item));
174 break;
175#endif
176 case S_IFREG:
Chris Masone20d96d2007-03-22 12:13:20 -0400177 break;
178 case S_IFDIR:
Chris Masone20d96d2007-03-22 12:13:20 -0400179 inode->i_op = &btrfs_dir_inode_operations;
180 inode->i_fop = &btrfs_dir_file_operations;
181 break;
182 case S_IFLNK:
Chris Masone20d96d2007-03-22 12:13:20 -0400183 // inode->i_op = &page_symlink_inode_operations;
184 break;
185 }
Chris Masone20d96d2007-03-22 12:13:20 -0400186 return;
Chris Mason2e635a22007-03-21 11:12:56 -0400187}
188
Chris Masone20d96d2007-03-22 12:13:20 -0400189static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
190 ino_t *ino)
191{
192 const char *name = dentry->d_name.name;
193 int namelen = dentry->d_name.len;
194 struct btrfs_dir_item *di;
195 struct btrfs_path path;
196 struct btrfs_root *root = btrfs_sb(dir->i_sb);
197 int ret;
198
199 btrfs_init_path(&path);
200 ret = btrfs_lookup_dir_item(NULL, root, &path, dir->i_ino, name,
201 namelen, 0);
202 if (ret) {
203 *ino = 0;
204 goto out;
205 }
206 di = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]), path.slots[0],
207 struct btrfs_dir_item);
208 *ino = btrfs_dir_objectid(di);
209out:
210 btrfs_release_path(root, &path);
211 return ret;
212}
213
214static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
215 struct nameidata *nd)
216{
217 struct inode * inode;
218 ino_t ino;
219 int ret;
220
221 if (dentry->d_name.len > BTRFS_NAME_LEN)
222 return ERR_PTR(-ENAMETOOLONG);
223
224 ret = btrfs_inode_by_name(dir, dentry, &ino);
225 if (ret < 0)
226 return ERR_PTR(ret);
227 inode = NULL;
228 if (ino) {
Chris Masone20d96d2007-03-22 12:13:20 -0400229 inode = iget(dir->i_sb, ino);
230 if (!inode)
231 return ERR_PTR(-EACCES);
232 }
233 return d_splice_alias(inode, dentry);
234}
235
236static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
237{
238 struct inode *inode = filp->f_path.dentry->d_inode;
239 struct btrfs_root *root = btrfs_sb(inode->i_sb);
240 struct btrfs_item *item;
241 struct btrfs_dir_item *di;
242 struct btrfs_key key;
243 struct btrfs_path path;
244 int ret;
245 u32 nritems;
246 struct btrfs_leaf *leaf;
247 int slot;
248 int advance;
249 unsigned char d_type = DT_UNKNOWN;
250 int over;
251
252 key.objectid = inode->i_ino;
Chris Masone20d96d2007-03-22 12:13:20 -0400253 key.flags = 0;
254 btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
255 key.offset = filp->f_pos;
256 btrfs_init_path(&path);
257 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
258 if (ret < 0) {
259 goto err;
260 }
Chris Masone20d96d2007-03-22 12:13:20 -0400261 advance = filp->f_pos > 0 && ret != 0;
262 while(1) {
263 leaf = btrfs_buffer_leaf(path.nodes[0]);
264 nritems = btrfs_header_nritems(&leaf->header);
265 slot = path.slots[0];
Chris Masone20d96d2007-03-22 12:13:20 -0400266 if (advance) {
Chris Masone20d96d2007-03-22 12:13:20 -0400267 if (slot == nritems -1) {
268 ret = btrfs_next_leaf(root, &path);
269 if (ret)
270 break;
271 leaf = btrfs_buffer_leaf(path.nodes[0]);
272 nritems = btrfs_header_nritems(&leaf->header);
273 slot = path.slots[0];
Chris Masone20d96d2007-03-22 12:13:20 -0400274 } else {
275 slot++;
276 path.slots[0]++;
277 }
278 }
279 advance = 1;
280 item = leaf->items + slot;
Chris Masone20d96d2007-03-22 12:13:20 -0400281 if (btrfs_disk_key_objectid(&item->key) != key.objectid)
282 break;
283 if (btrfs_disk_key_type(&item->key) != BTRFS_DIR_ITEM_KEY)
284 continue;
285 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Chris Masone20d96d2007-03-22 12:13:20 -0400286 over = filldir(dirent, (const char *)(di + 1),
287 btrfs_dir_name_len(di),
288 btrfs_disk_key_offset(&item->key),
289 btrfs_dir_objectid(di), d_type);
290 if (over)
291 break;
292 filp->f_pos = btrfs_disk_key_offset(&item->key) + 1;
293 }
Chris Masone20d96d2007-03-22 12:13:20 -0400294 ret = 0;
295err:
296 btrfs_release_path(root, &path);
297 return ret;
298}
299
300static void btrfs_put_super (struct super_block * sb)
301{
302 struct btrfs_root *root = btrfs_sb(sb);
303 int ret;
304
305 ret = close_ctree(root);
306 if (ret) {
307 printk("close ctree returns %d\n", ret);
308 }
309 sb->s_fs_info = NULL;
310}
Chris Mason2e635a22007-03-21 11:12:56 -0400311
312static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
313{
314 struct inode * inode;
Chris Masone20d96d2007-03-22 12:13:20 -0400315 struct dentry * root_dentry;
316 struct btrfs_super_block *disk_super;
317 struct buffer_head *bh;
318 struct btrfs_root *root;
Chris Mason2e635a22007-03-21 11:12:56 -0400319
320 sb->s_maxbytes = MAX_LFS_FILESIZE;
321 sb->s_blocksize = PAGE_CACHE_SIZE;
322 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
323 sb->s_magic = BTRFS_SUPER_MAGIC;
Chris Masone20d96d2007-03-22 12:13:20 -0400324 sb->s_op = &btrfs_super_ops;
Chris Mason2e635a22007-03-21 11:12:56 -0400325 sb->s_time_gran = 1;
Chris Masone20d96d2007-03-22 12:13:20 -0400326
327 bh = sb_bread(sb, BTRFS_SUPER_INFO_OFFSET / sb->s_blocksize);
328 if (!bh) {
329 printk("btrfs: unable to read on disk super\n");
330 return -EIO;
331 }
332 disk_super = (struct btrfs_super_block *)bh->b_data;
333 root = open_ctree(sb, bh, disk_super);
334 sb->s_fs_info = root;
335 if (!root) {
336 printk("btrfs: open_ctree failed\n");
337 return -EIO;
338 }
339 printk("read in super total blocks %Lu root %Lu\n",
340 btrfs_super_total_blocks(disk_super),
341 btrfs_super_root_dir(disk_super));
342
343 inode = iget_locked(sb, btrfs_super_root_dir(disk_super));
Chris Mason2e635a22007-03-21 11:12:56 -0400344 if (!inode)
345 return -ENOMEM;
Chris Masone20d96d2007-03-22 12:13:20 -0400346 if (inode->i_state & I_NEW) {
347 btrfs_read_locked_inode(inode);
348 unlock_new_inode(inode);
349 }
Chris Mason2e635a22007-03-21 11:12:56 -0400350
Chris Masone20d96d2007-03-22 12:13:20 -0400351 root_dentry = d_alloc_root(inode);
352 if (!root_dentry) {
Chris Mason2e635a22007-03-21 11:12:56 -0400353 iput(inode);
354 return -ENOMEM;
355 }
Chris Masone20d96d2007-03-22 12:13:20 -0400356 sb->s_root = root_dentry;
357
Chris Mason2e635a22007-03-21 11:12:56 -0400358 return 0;
359}
360
Chris Masond5719762007-03-23 10:01:08 -0400361static void fill_inode_item(struct btrfs_inode_item *item,
362 struct inode *inode)
363{
364 btrfs_set_inode_uid(item, inode->i_uid);
365 btrfs_set_inode_gid(item, inode->i_gid);
366 btrfs_set_inode_size(item, inode->i_size);
367 btrfs_set_inode_mode(item, inode->i_mode);
368 btrfs_set_inode_nlink(item, inode->i_nlink);
369 btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec);
370 btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec);
371 btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec);
372 btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec);
373 btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec);
374 btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec);
375 btrfs_set_inode_nblocks(item, inode->i_blocks);
376 btrfs_set_inode_generation(item, inode->i_generation);
377}
378
379static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
380 struct inode *dir, int mode)
381{
382 struct inode *inode;
383 struct btrfs_inode_item inode_item;
384 struct btrfs_root *root = btrfs_sb(dir->i_sb);
385 struct btrfs_key key;
386 int ret;
387 u64 objectid;
388
389 inode = new_inode(dir->i_sb);
390 if (!inode)
391 return ERR_PTR(-ENOMEM);
392
393 ret = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
394 BUG_ON(ret);
395
396 inode->i_uid = current->fsuid;
397 inode->i_gid = current->fsgid;
398 inode->i_mode = mode;
399 inode->i_ino = objectid;
400 inode->i_blocks = 0;
401 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
402 fill_inode_item(&inode_item, inode);
403
404
405 key.objectid = objectid;
406 key.flags = 0;
407 key.offset = 0;
408 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
409 ret = btrfs_insert_inode_map(trans, root, objectid, &key);
410 BUG_ON(ret);
411
412 ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
413 BUG_ON(ret);
414
415 insert_inode_hash(inode);
416 // FIXME mark_inode_dirty(inode)
417 return inode;
418}
419
420static int btrfs_add_link(struct btrfs_trans_handle *trans,
421 struct dentry *dentry, struct inode *inode)
422{
423 int ret;
424 ret = btrfs_insert_dir_item(trans, btrfs_sb(inode->i_sb),
425 dentry->d_name.name, dentry->d_name.len,
426 dentry->d_parent->d_inode->i_ino,
427 inode->i_ino, 0);
428 BUG_ON(ret);
429 return ret;
430}
431
432static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
433 struct dentry *dentry, struct inode *inode)
434{
435 int err = btrfs_add_link(trans, dentry, inode);
436 if (!err) {
437 d_instantiate(dentry, inode);
438 return 0;
439 }
440 inode_dec_link_count(inode);
441 iput(inode);
442 return err;
443}
444
445static int btrfs_create(struct inode *dir, struct dentry *dentry,
446 int mode, struct nameidata *nd)
447{
448 struct btrfs_trans_handle *trans;
449 struct btrfs_root *root = btrfs_sb(dir->i_sb);
450 struct inode *inode;
451 int err;
452
453 trans = btrfs_start_transaction(root, 1);
454 inode = btrfs_new_inode(trans, dir, mode);
455 err = PTR_ERR(inode);
456 if (IS_ERR(inode))
457 return err;
458 // FIXME mark the inode dirty
459 err = btrfs_add_nondir(trans, dentry, inode);
460 dir->i_sb->s_dirt = 1;
461 btrfs_end_transaction(trans, root);
462 return err;
463}
464
465static void btrfs_write_super(struct super_block *sb)
466{
467 sb->s_dirt = 0;
468printk("btrfs write_super!\n");
469}
470
471static int btrfs_sync_fs(struct super_block *sb, int wait)
472{
473 struct btrfs_trans_handle *trans;
474 struct btrfs_root *root;
475 int ret;
476 sb->s_dirt = 0;
477 root = btrfs_sb(sb);
478 trans = btrfs_start_transaction(root, 1);
479 ret = btrfs_commit_transaction(trans, root);
480 sb->s_dirt = 0;
481 BUG_ON(ret);
482printk("btrfs sync_fs\n");
483 return 0;
484}
485
Chris Mason2e635a22007-03-21 11:12:56 -0400486static int btrfs_get_sb(struct file_system_type *fs_type,
487 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
488{
489 return get_sb_bdev(fs_type, flags, dev_name, data,
490 btrfs_fill_super, mnt);
491}
492
493static struct file_system_type btrfs_fs_type = {
494 .owner = THIS_MODULE,
495 .name = "btrfs",
496 .get_sb = btrfs_get_sb,
497 .kill_sb = kill_block_super,
498 .fs_flags = FS_REQUIRES_DEV,
499};
500
Chris Masone20d96d2007-03-22 12:13:20 -0400501static struct super_operations btrfs_super_ops = {
502 .statfs = simple_statfs,
503 .drop_inode = generic_delete_inode,
504 .put_super = btrfs_put_super,
505 .read_inode = btrfs_read_locked_inode,
Chris Masond5719762007-03-23 10:01:08 -0400506 .write_super = btrfs_write_super,
507 .sync_fs = btrfs_sync_fs,
Chris Masone20d96d2007-03-22 12:13:20 -0400508};
509
510static struct inode_operations btrfs_dir_inode_operations = {
511 .lookup = btrfs_lookup,
Chris Masond5719762007-03-23 10:01:08 -0400512 .create = btrfs_create,
Chris Masone20d96d2007-03-22 12:13:20 -0400513};
514
515static struct file_operations btrfs_dir_file_operations = {
516 .llseek = generic_file_llseek,
517 .read = generic_read_dir,
518 .readdir = btrfs_readdir,
519};
520
521
Chris Mason2e635a22007-03-21 11:12:56 -0400522static int __init init_btrfs_fs(void)
523{
524 printk("btrfs loaded!\n");
525 return register_filesystem(&btrfs_fs_type);
526}
527
528static void __exit exit_btrfs_fs(void)
529{
530 unregister_filesystem(&btrfs_fs_type);
531 printk("btrfs unloaded\n");
532}
533
534module_init(init_btrfs_fs)
535module_exit(exit_btrfs_fs)
536
537MODULE_LICENSE("GPL");