1 #include <linux/module.h>
2 #include <linux/buffer_head.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>
14 #define BTRFS_SUPER_MAGIC 0x9123682E
16 static struct inode_operations btrfs_dir_inode_operations;
17 static struct super_operations btrfs_super_ops;
18 static struct file_operations btrfs_dir_file_operations;
21 /* some random number */
23 static struct super_operations ramfs_ops;
25 static struct backing_dev_info ramfs_backing_dev_info = {
26 .ra_pages = 0, /* No readahead */
27 .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK |
28 BDI_CAP_MAP_DIRECT | BDI_CAP_MAP_COPY |
29 BDI_CAP_READ_MAP | BDI_CAP_WRITE_MAP | BDI_CAP_EXEC_MAP,
32 struct inode *ramfs_get_inode(struct super_block *sb, int mode, dev_t dev)
34 struct inode * inode = new_inode(sb);
38 inode->i_uid = current->fsuid;
39 inode->i_gid = current->fsgid;
41 inode->i_mapping->a_ops = &ramfs_aops;
42 inode->i_mapping->backing_dev_info = &ramfs_backing_dev_info;
43 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
44 switch (mode & S_IFMT) {
46 init_special_inode(inode, mode, dev);
49 inode->i_op = &ramfs_file_inode_operations;
50 inode->i_fop = &ramfs_file_operations;
53 inode->i_op = &ramfs_dir_inode_operations;
54 inode->i_fop = &simple_dir_operations;
56 /* directory inodes start off with i_nlink == 2 (for "." entry) */
60 inode->i_op = &page_symlink_inode_operations;
68 * File creation. Allocate an inode, and we're done..
72 ramfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
74 struct inode * inode = ramfs_get_inode(dir->i_sb, mode, dev);
78 if (dir->i_mode & S_ISGID) {
79 inode->i_gid = dir->i_gid;
81 inode->i_mode |= S_ISGID;
83 d_instantiate(dentry, inode);
84 dget(dentry); /* Extra count - pin the dentry in core */
86 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
91 static int ramfs_mkdir(struct inode * dir, struct dentry * dentry, int mode)
93 int retval = ramfs_mknod(dir, dentry, mode | S_IFDIR, 0);
99 static int ramfs_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd)
101 return ramfs_mknod(dir, dentry, mode | S_IFREG, 0);
104 static int ramfs_symlink(struct inode * dir, struct dentry *dentry, const char * symname)
109 inode = ramfs_get_inode(dir->i_sb, S_IFLNK|S_IRWXUGO, 0);
111 int l = strlen(symname)+1;
112 error = page_symlink(inode, symname, l);
114 if (dir->i_mode & S_ISGID)
115 inode->i_gid = dir->i_gid;
116 d_instantiate(dentry, inode);
118 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
125 static struct inode_operations ramfs_dir_inode_operations = {
126 .create = ramfs_create,
127 .lookup = simple_lookup,
129 .unlink = simple_unlink,
130 .symlink = ramfs_symlink,
131 .mkdir = ramfs_mkdir,
132 .rmdir = simple_rmdir,
133 .mknod = ramfs_mknod,
134 .rename = simple_rename,
138 static void btrfs_read_locked_inode(struct inode *inode)
140 struct btrfs_path path;
141 struct btrfs_inode_item *inode_item;
142 struct btrfs_root *root = btrfs_sb(inode->i_sb);
144 btrfs_init_path(&path);
145 ret = btrfs_lookup_inode(NULL, root, &path, inode->i_ino, 0);
147 make_bad_inode(inode);
150 inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]),
152 struct btrfs_inode_item);
154 inode->i_mode = btrfs_inode_mode(inode_item);
155 inode->i_nlink = btrfs_inode_nlink(inode_item);
156 inode->i_uid = btrfs_inode_uid(inode_item);
157 inode->i_gid = btrfs_inode_gid(inode_item);
158 inode->i_size = btrfs_inode_size(inode_item);
159 inode->i_atime.tv_sec = btrfs_timespec_sec(&inode_item->atime);
160 inode->i_atime.tv_nsec = btrfs_timespec_nsec(&inode_item->atime);
161 inode->i_mtime.tv_sec = btrfs_timespec_sec(&inode_item->mtime);
162 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(&inode_item->mtime);
163 inode->i_ctime.tv_sec = btrfs_timespec_sec(&inode_item->ctime);
164 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(&inode_item->ctime);
165 inode->i_blocks = btrfs_inode_nblocks(inode_item);
166 inode->i_generation = btrfs_inode_generation(inode_item);
167 btrfs_release_path(root, &path);
168 switch (inode->i_mode & S_IFMT) {
171 init_special_inode(inode, inode->i_mode,
172 btrfs_inode_rdev(inode_item));
178 inode->i_op = &btrfs_dir_inode_operations;
179 inode->i_fop = &btrfs_dir_file_operations;
182 // inode->i_op = &page_symlink_inode_operations;
188 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
191 const char *name = dentry->d_name.name;
192 int namelen = dentry->d_name.len;
193 struct btrfs_dir_item *di;
194 struct btrfs_path path;
195 struct btrfs_root *root = btrfs_sb(dir->i_sb);
198 btrfs_init_path(&path);
199 ret = btrfs_lookup_dir_item(NULL, root, &path, dir->i_ino, name,
205 di = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]), path.slots[0],
206 struct btrfs_dir_item);
207 *ino = btrfs_dir_objectid(di);
209 btrfs_release_path(root, &path);
213 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
214 struct nameidata *nd)
216 struct inode * inode;
220 if (dentry->d_name.len > BTRFS_NAME_LEN)
221 return ERR_PTR(-ENAMETOOLONG);
223 ret = btrfs_inode_by_name(dir, dentry, &ino);
228 inode = iget(dir->i_sb, ino);
230 return ERR_PTR(-EACCES);
232 return d_splice_alias(inode, dentry);
235 static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
237 struct inode *inode = filp->f_path.dentry->d_inode;
238 struct btrfs_root *root = btrfs_sb(inode->i_sb);
239 struct btrfs_item *item;
240 struct btrfs_dir_item *di;
241 struct btrfs_key key;
242 struct btrfs_path path;
245 struct btrfs_leaf *leaf;
248 unsigned char d_type = DT_UNKNOWN;
251 key.objectid = inode->i_ino;
253 btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
254 key.offset = filp->f_pos;
255 btrfs_init_path(&path);
256 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
260 advance = filp->f_pos > 0 && ret != 0;
262 leaf = btrfs_buffer_leaf(path.nodes[0]);
263 nritems = btrfs_header_nritems(&leaf->header);
264 slot = path.slots[0];
266 if (slot == nritems -1) {
267 ret = btrfs_next_leaf(root, &path);
270 leaf = btrfs_buffer_leaf(path.nodes[0]);
271 nritems = btrfs_header_nritems(&leaf->header);
272 slot = path.slots[0];
279 item = leaf->items + slot;
280 if (btrfs_disk_key_objectid(&item->key) != key.objectid)
282 if (btrfs_disk_key_type(&item->key) != BTRFS_DIR_ITEM_KEY)
284 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
285 over = filldir(dirent, (const char *)(di + 1),
286 btrfs_dir_name_len(di),
287 btrfs_disk_key_offset(&item->key),
288 btrfs_dir_objectid(di), d_type);
291 filp->f_pos = btrfs_disk_key_offset(&item->key) + 1;
295 btrfs_release_path(root, &path);
299 static void btrfs_put_super (struct super_block * sb)
301 struct btrfs_root *root = btrfs_sb(sb);
304 ret = close_ctree(root);
306 printk("close ctree returns %d\n", ret);
308 sb->s_fs_info = NULL;
311 static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
313 struct inode * inode;
314 struct dentry * root_dentry;
315 struct btrfs_super_block *disk_super;
316 struct buffer_head *bh;
317 struct btrfs_root *root;
319 sb->s_maxbytes = MAX_LFS_FILESIZE;
320 sb->s_blocksize = PAGE_CACHE_SIZE;
321 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
322 sb->s_magic = BTRFS_SUPER_MAGIC;
323 sb->s_op = &btrfs_super_ops;
326 bh = sb_bread(sb, BTRFS_SUPER_INFO_OFFSET / sb->s_blocksize);
328 printk("btrfs: unable to read on disk super\n");
331 disk_super = (struct btrfs_super_block *)bh->b_data;
332 root = open_ctree(sb, bh, disk_super);
333 sb->s_fs_info = root;
335 printk("btrfs: open_ctree failed\n");
338 printk("read in super total blocks %Lu root %Lu\n",
339 btrfs_super_total_blocks(disk_super),
340 btrfs_super_root_dir(disk_super));
342 inode = iget_locked(sb, btrfs_super_root_dir(disk_super));
345 if (inode->i_state & I_NEW) {
346 btrfs_read_locked_inode(inode);
347 unlock_new_inode(inode);
350 root_dentry = d_alloc_root(inode);
355 sb->s_root = root_dentry;
360 static int btrfs_get_sb(struct file_system_type *fs_type,
361 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
363 return get_sb_bdev(fs_type, flags, dev_name, data,
364 btrfs_fill_super, mnt);
367 static struct file_system_type btrfs_fs_type = {
368 .owner = THIS_MODULE,
370 .get_sb = btrfs_get_sb,
371 .kill_sb = kill_block_super,
372 .fs_flags = FS_REQUIRES_DEV,
375 static struct super_operations btrfs_super_ops = {
376 .statfs = simple_statfs,
377 .drop_inode = generic_delete_inode,
378 .put_super = btrfs_put_super,
379 .read_inode = btrfs_read_locked_inode,
382 static struct inode_operations btrfs_dir_inode_operations = {
383 .lookup = btrfs_lookup,
386 static struct file_operations btrfs_dir_file_operations = {
387 .llseek = generic_file_llseek,
388 .read = generic_read_dir,
389 .readdir = btrfs_readdir,
393 static int __init init_btrfs_fs(void)
395 printk("btrfs loaded!\n");
396 return register_filesystem(&btrfs_fs_type);
399 static void __exit exit_btrfs_fs(void)
401 unregister_filesystem(&btrfs_fs_type);
402 printk("btrfs unloaded\n");
405 module_init(init_btrfs_fs)
406 module_exit(exit_btrfs_fs)
408 MODULE_LICENSE("GPL");