blob: ccc056aad692c47859e745f58434a2b8ff570e1f [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 Mason2e635a22007-03-21 11:12:56 -040013
14#define BTRFS_SUPER_MAGIC 0x9123682E
Chris Masone20d96d2007-03-22 12:13:20 -040015
16static struct inode_operations btrfs_dir_inode_operations;
17static struct super_operations btrfs_super_ops;
18static struct file_operations btrfs_dir_file_operations;
19
Chris Mason2e635a22007-03-21 11:12:56 -040020#if 0
21/* some random number */
22
23static struct super_operations ramfs_ops;
Chris Mason2e635a22007-03-21 11:12:56 -040024
25static 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,
30};
31
32struct inode *ramfs_get_inode(struct super_block *sb, int mode, dev_t dev)
33{
34 struct inode * inode = new_inode(sb);
35
36 if (inode) {
37 inode->i_mode = mode;
38 inode->i_uid = current->fsuid;
39 inode->i_gid = current->fsgid;
40 inode->i_blocks = 0;
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) {
45 default:
46 init_special_inode(inode, mode, dev);
47 break;
48 case S_IFREG:
49 inode->i_op = &ramfs_file_inode_operations;
50 inode->i_fop = &ramfs_file_operations;
51 break;
52 case S_IFDIR:
53 inode->i_op = &ramfs_dir_inode_operations;
54 inode->i_fop = &simple_dir_operations;
55
56 /* directory inodes start off with i_nlink == 2 (for "." entry) */
57 inc_nlink(inode);
58 break;
59 case S_IFLNK:
60 inode->i_op = &page_symlink_inode_operations;
61 break;
62 }
63 }
64 return inode;
65}
66
67/*
68 * File creation. Allocate an inode, and we're done..
69 */
70/* SMP-safe */
71static int
72ramfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
73{
74 struct inode * inode = ramfs_get_inode(dir->i_sb, mode, dev);
75 int error = -ENOSPC;
76
77 if (inode) {
78 if (dir->i_mode & S_ISGID) {
79 inode->i_gid = dir->i_gid;
80 if (S_ISDIR(mode))
81 inode->i_mode |= S_ISGID;
82 }
83 d_instantiate(dentry, inode);
84 dget(dentry); /* Extra count - pin the dentry in core */
85 error = 0;
86 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
87 }
88 return error;
89}
90
91static int ramfs_mkdir(struct inode * dir, struct dentry * dentry, int mode)
92{
93 int retval = ramfs_mknod(dir, dentry, mode | S_IFDIR, 0);
94 if (!retval)
95 inc_nlink(dir);
96 return retval;
97}
98
99static int ramfs_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd)
100{
101 return ramfs_mknod(dir, dentry, mode | S_IFREG, 0);
102}
103
104static int ramfs_symlink(struct inode * dir, struct dentry *dentry, const char * symname)
105{
106 struct inode *inode;
107 int error = -ENOSPC;
108
109 inode = ramfs_get_inode(dir->i_sb, S_IFLNK|S_IRWXUGO, 0);
110 if (inode) {
111 int l = strlen(symname)+1;
112 error = page_symlink(inode, symname, l);
113 if (!error) {
114 if (dir->i_mode & S_ISGID)
115 inode->i_gid = dir->i_gid;
116 d_instantiate(dentry, inode);
117 dget(dentry);
118 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
119 } else
120 iput(inode);
121 }
122 return error;
123}
124
125static struct inode_operations ramfs_dir_inode_operations = {
126 .create = ramfs_create,
127 .lookup = simple_lookup,
128 .link = simple_link,
129 .unlink = simple_unlink,
130 .symlink = ramfs_symlink,
131 .mkdir = ramfs_mkdir,
132 .rmdir = simple_rmdir,
133 .mknod = ramfs_mknod,
134 .rename = simple_rename,
135};
136#endif
137
Chris Masone20d96d2007-03-22 12:13:20 -0400138static void btrfs_read_locked_inode(struct inode *inode)
Chris Mason2e635a22007-03-21 11:12:56 -0400139{
Chris Masone20d96d2007-03-22 12:13:20 -0400140 struct btrfs_path path;
141 struct btrfs_inode_item *inode_item;
142 struct btrfs_root *root = btrfs_sb(inode->i_sb);
143 int ret;
144printk("read locked inode %lu\n", inode->i_ino);
145 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
155printk("found locked inode %lu\n", inode->i_ino);
156 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);
169printk("about to release\n");
170 btrfs_release_path(root, &path);
171 switch (inode->i_mode & S_IFMT) {
172#if 0
173 default:
174 init_special_inode(inode, inode->i_mode,
175 btrfs_inode_rdev(inode_item));
176 break;
177#endif
178 case S_IFREG:
179printk("inode %lu now a file\n", inode->i_ino);
180 break;
181 case S_IFDIR:
182printk("inode %lu now a directory\n", inode->i_ino);
183 inode->i_op = &btrfs_dir_inode_operations;
184 inode->i_fop = &btrfs_dir_file_operations;
185 break;
186 case S_IFLNK:
187printk("inode %lu now a link\n", inode->i_ino);
188 // inode->i_op = &page_symlink_inode_operations;
189 break;
190 }
191printk("returning!\n");
192 return;
Chris Mason2e635a22007-03-21 11:12:56 -0400193}
194
Chris Masone20d96d2007-03-22 12:13:20 -0400195static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
196 ino_t *ino)
197{
198 const char *name = dentry->d_name.name;
199 int namelen = dentry->d_name.len;
200 struct btrfs_dir_item *di;
201 struct btrfs_path path;
202 struct btrfs_root *root = btrfs_sb(dir->i_sb);
203 int ret;
204
205 btrfs_init_path(&path);
206 ret = btrfs_lookup_dir_item(NULL, root, &path, dir->i_ino, name,
207 namelen, 0);
208 if (ret) {
209 *ino = 0;
210 goto out;
211 }
212 di = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]), path.slots[0],
213 struct btrfs_dir_item);
214 *ino = btrfs_dir_objectid(di);
215out:
216 btrfs_release_path(root, &path);
217 return ret;
218}
219
220static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
221 struct nameidata *nd)
222{
223 struct inode * inode;
224 ino_t ino;
225 int ret;
226
227 if (dentry->d_name.len > BTRFS_NAME_LEN)
228 return ERR_PTR(-ENAMETOOLONG);
229
230 ret = btrfs_inode_by_name(dir, dentry, &ino);
231 if (ret < 0)
232 return ERR_PTR(ret);
233 inode = NULL;
234 if (ino) {
235printk("lookup on %.*s returns %lu\n", dentry->d_name.len, dentry->d_name.name, ino);
236 inode = iget(dir->i_sb, ino);
237 if (!inode)
238 return ERR_PTR(-EACCES);
239 }
240 return d_splice_alias(inode, dentry);
241}
242
243static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
244{
245 struct inode *inode = filp->f_path.dentry->d_inode;
246 struct btrfs_root *root = btrfs_sb(inode->i_sb);
247 struct btrfs_item *item;
248 struct btrfs_dir_item *di;
249 struct btrfs_key key;
250 struct btrfs_path path;
251 int ret;
252 u32 nritems;
253 struct btrfs_leaf *leaf;
254 int slot;
255 int advance;
256 unsigned char d_type = DT_UNKNOWN;
257 int over;
258
259 key.objectid = inode->i_ino;
260printk("readdir on dir %Lu pos %Lu\n", key.objectid, filp->f_pos);
261 key.flags = 0;
262 btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
263 key.offset = filp->f_pos;
264 btrfs_init_path(&path);
265 ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
266 if (ret < 0) {
267 goto err;
268 }
269printk("first ret %d\n", ret);
270 advance = filp->f_pos > 0 && ret != 0;
271 while(1) {
272 leaf = btrfs_buffer_leaf(path.nodes[0]);
273 nritems = btrfs_header_nritems(&leaf->header);
274 slot = path.slots[0];
275printk("leaf %Lu nritems %lu slot %d\n", path.nodes[0]->b_blocknr, nritems, slot);
276 if (advance) {
277printk("advancing!\n");
278 if (slot == nritems -1) {
279 ret = btrfs_next_leaf(root, &path);
280 if (ret)
281 break;
282 leaf = btrfs_buffer_leaf(path.nodes[0]);
283 nritems = btrfs_header_nritems(&leaf->header);
284 slot = path.slots[0];
285printk("2leaf %Lu nritems %lu slot %d\n", path.nodes[0]->b_blocknr, nritems, slot);
286 } else {
287 slot++;
288 path.slots[0]++;
289 }
290 }
291 advance = 1;
292 item = leaf->items + slot;
293printk("item key %Lu %u %Lu\n", btrfs_disk_key_objectid(&item->key),
294 btrfs_disk_key_flags(&item->key), btrfs_disk_key_offset(&item->key));
295 if (btrfs_disk_key_objectid(&item->key) != key.objectid)
296 break;
297 if (btrfs_disk_key_type(&item->key) != BTRFS_DIR_ITEM_KEY)
298 continue;
299 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
300printk("filldir name %.*s, objectid %Lu\n", btrfs_dir_name_len(di),
301 (const char *)(di + 1), btrfs_dir_objectid(di));
302 over = filldir(dirent, (const char *)(di + 1),
303 btrfs_dir_name_len(di),
304 btrfs_disk_key_offset(&item->key),
305 btrfs_dir_objectid(di), d_type);
306 if (over)
307 break;
308 filp->f_pos = btrfs_disk_key_offset(&item->key) + 1;
309 }
310printk("filldir all done\n");
311 ret = 0;
312err:
313 btrfs_release_path(root, &path);
314 return ret;
315}
316
317static void btrfs_put_super (struct super_block * sb)
318{
319 struct btrfs_root *root = btrfs_sb(sb);
320 int ret;
321
322 ret = close_ctree(root);
323 if (ret) {
324 printk("close ctree returns %d\n", ret);
325 }
326 sb->s_fs_info = NULL;
327}
Chris Mason2e635a22007-03-21 11:12:56 -0400328
329static int btrfs_fill_super(struct super_block * sb, void * data, int silent)
330{
331 struct inode * inode;
Chris Masone20d96d2007-03-22 12:13:20 -0400332 struct dentry * root_dentry;
333 struct btrfs_super_block *disk_super;
334 struct buffer_head *bh;
335 struct btrfs_root *root;
Chris Mason2e635a22007-03-21 11:12:56 -0400336
337 sb->s_maxbytes = MAX_LFS_FILESIZE;
338 sb->s_blocksize = PAGE_CACHE_SIZE;
339 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
340 sb->s_magic = BTRFS_SUPER_MAGIC;
Chris Masone20d96d2007-03-22 12:13:20 -0400341 sb->s_op = &btrfs_super_ops;
Chris Mason2e635a22007-03-21 11:12:56 -0400342 sb->s_time_gran = 1;
Chris Masone20d96d2007-03-22 12:13:20 -0400343
344 bh = sb_bread(sb, BTRFS_SUPER_INFO_OFFSET / sb->s_blocksize);
345 if (!bh) {
346 printk("btrfs: unable to read on disk super\n");
347 return -EIO;
348 }
349 disk_super = (struct btrfs_super_block *)bh->b_data;
350 root = open_ctree(sb, bh, disk_super);
351 sb->s_fs_info = root;
352 if (!root) {
353 printk("btrfs: open_ctree failed\n");
354 return -EIO;
355 }
356 printk("read in super total blocks %Lu root %Lu\n",
357 btrfs_super_total_blocks(disk_super),
358 btrfs_super_root_dir(disk_super));
359
360 inode = iget_locked(sb, btrfs_super_root_dir(disk_super));
Chris Mason2e635a22007-03-21 11:12:56 -0400361 if (!inode)
362 return -ENOMEM;
Chris Masone20d96d2007-03-22 12:13:20 -0400363 if (inode->i_state & I_NEW) {
364 btrfs_read_locked_inode(inode);
365 unlock_new_inode(inode);
366 }
Chris Mason2e635a22007-03-21 11:12:56 -0400367
Chris Masone20d96d2007-03-22 12:13:20 -0400368 root_dentry = d_alloc_root(inode);
369 if (!root_dentry) {
Chris Mason2e635a22007-03-21 11:12:56 -0400370 iput(inode);
371 return -ENOMEM;
372 }
Chris Masone20d96d2007-03-22 12:13:20 -0400373 sb->s_root = root_dentry;
374
Chris Mason2e635a22007-03-21 11:12:56 -0400375 return 0;
376}
377
378static int btrfs_get_sb(struct file_system_type *fs_type,
379 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
380{
381 return get_sb_bdev(fs_type, flags, dev_name, data,
382 btrfs_fill_super, mnt);
383}
384
385static struct file_system_type btrfs_fs_type = {
386 .owner = THIS_MODULE,
387 .name = "btrfs",
388 .get_sb = btrfs_get_sb,
389 .kill_sb = kill_block_super,
390 .fs_flags = FS_REQUIRES_DEV,
391};
392
Chris Masone20d96d2007-03-22 12:13:20 -0400393static struct super_operations btrfs_super_ops = {
394 .statfs = simple_statfs,
395 .drop_inode = generic_delete_inode,
396 .put_super = btrfs_put_super,
397 .read_inode = btrfs_read_locked_inode,
398};
399
400static struct inode_operations btrfs_dir_inode_operations = {
401 .lookup = btrfs_lookup,
402};
403
404static struct file_operations btrfs_dir_file_operations = {
405 .llseek = generic_file_llseek,
406 .read = generic_read_dir,
407 .readdir = btrfs_readdir,
408};
409
410
Chris Mason2e635a22007-03-21 11:12:56 -0400411static int __init init_btrfs_fs(void)
412{
413 printk("btrfs loaded!\n");
414 return register_filesystem(&btrfs_fs_type);
415}
416
417static void __exit exit_btrfs_fs(void)
418{
419 unregister_filesystem(&btrfs_fs_type);
420 printk("btrfs unloaded\n");
421}
422
423module_init(init_btrfs_fs)
424module_exit(exit_btrfs_fs)
425
426MODULE_LICENSE("GPL");