blob: ac2d6be6a99e873918394a7337bbed5e2e8a3fe6 [file] [log] [blame]
Miklos Szeredibc22e7b2001-10-23 19:26:04 +00001/*
2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001 Miklos Szeredi (mszeredi@inf.bme.hu)
4
5 This program can be distributed under the terms of the GNU GPL.
6 See the file COPYING.
7*/
8
Miklos Szeredibc22e7b2001-10-23 19:26:04 +00009#include "fuse_i.h"
10
11#include <linux/module.h>
12#include <linux/kernel.h>
Miklos Szeredibc22e7b2001-10-23 19:26:04 +000013#include <linux/sched.h>
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000014#include <linux/slab.h>
Miklos Szeredibc22e7b2001-10-23 19:26:04 +000015#include <linux/file.h>
16
17#define FUSE_SUPER_MAGIC 0x65735546
18
19static void fuse_read_inode(struct inode *inode)
20{
Miklos Szeredi90d8bef2001-10-26 14:55:42 +000021 /* No op */
Miklos Szeredibc22e7b2001-10-23 19:26:04 +000022}
23
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000024static void send_forget(struct fuse_conn *fc, unsigned long *forget,
25 unsigned int numforget)
26{
27 struct fuse_in in = FUSE_IN_INIT;
28
29 in.h.opcode = FUSE_FORGET;
30 in.h.ino = 0;
31 in.argsize = numforget * sizeof(unsigned long);
32 in.arg = forget;
33
34 request_send(fc, &in, NULL);
35}
36
37static int alloc_cleared(struct fuse_conn *fc)
38{
39 unsigned long *tmp;
40
41 spin_unlock(&fuse_lock);
42 tmp = kmalloc(sizeof(unsigned long) * MAX_CLEARED, GFP_KERNEL);
43 spin_lock(&fuse_lock);
44
45 if(!fc->file || fc->cleared != NULL)
46 kfree(tmp);
47 else if(!tmp)
48 printk("fuse_clear_inode: Cannot allocate memory\n");
49 else
50 fc->cleared = tmp;
51
52 return fc->cleared != NULL;
53}
54
55static unsigned long *add_cleared(struct fuse_conn *fc, unsigned long ino)
56{
57 if(!fc->file || (!fc->cleared && !alloc_cleared(fc)))
58 return NULL;
59
60 fc->cleared[fc->numcleared] = ino;
61 fc->numcleared ++;
62
63 if(fc->numcleared == MAX_CLEARED) {
64 unsigned long *tmp = fc->cleared;
65 fc->cleared = NULL;
66 fc->numcleared = 0;
67 return tmp;
68 }
69
70 return NULL;
71}
72
73static void fuse_clear_inode(struct inode *inode)
74{
75 struct fuse_conn *fc = inode->i_sb->u.generic_sbp;
76 unsigned long *forget;
77
78 spin_lock(&fuse_lock);
79 forget = add_cleared(fc, inode->i_ino);
80 spin_unlock(&fuse_lock);
81
82 if(forget) {
83 send_forget(fc, forget, MAX_CLEARED);
84 kfree(forget);
85 }
86}
87
Miklos Szeredibc22e7b2001-10-23 19:26:04 +000088static void fuse_put_super(struct super_block *sb)
89{
90 struct fuse_conn *fc = sb->u.generic_sbp;
91
Miklos Szeredi79b52f62001-10-24 14:37:13 +000092 spin_lock(&fuse_lock);
Miklos Szeredibc22e7b2001-10-23 19:26:04 +000093 fc->sb = NULL;
94 fuse_release_conn(fc);
Miklos Szeredi79b52f62001-10-24 14:37:13 +000095 spin_unlock(&fuse_lock);
96
Miklos Szeredibc22e7b2001-10-23 19:26:04 +000097}
98
99static struct super_operations fuse_super_operations = {
100 read_inode: fuse_read_inode,
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000101 clear_inode: fuse_clear_inode,
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000102 put_super: fuse_put_super,
103};
104
105
106static struct fuse_conn *get_conn(struct fuse_mount_data *d)
107{
108 struct fuse_conn *fc = NULL;
109 struct file *file;
110 struct inode *ino;
111
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000112 if(d == NULL) {
113 printk("fuse_read_super: Bad mount data\n");
114 return NULL;
115 }
116
117 if(d->version != FUSE_MOUNT_VERSION) {
118 printk("fuse_read_super: Bad mount version: %i\n", d->version);
119 return NULL;
120 }
121
122 file = fget(d->fd);
123 ino = NULL;
124 if(file)
125 ino = file->f_dentry->d_inode;
126
127 if(!ino || ino->u.generic_ip != proc_fuse_dev) {
128 printk("fuse_read_super: Bad file: %i\n", d->fd);
129 goto out;
130 }
131
132 fc = file->private_data;
133
134 out:
135 fput(file);
136 return fc;
137
138}
139
Miklos Szeredi79b52f62001-10-24 14:37:13 +0000140static struct inode *get_root_inode(struct super_block *sb)
141{
142 struct inode *root ;
143
144 root = iget(sb, 1);
145 if(root) {
146 root->i_mode = S_IFDIR;
147 root->i_uid = 0;
148 root->i_gid = 0;
149 root->i_nlink = 2;
150 root->i_size = 0;
151 root->i_blksize = 1024;
152 root->i_blocks = 0;
153 root->i_atime = CURRENT_TIME;
154 root->i_mtime = CURRENT_TIME;
155 root->i_ctime = CURRENT_TIME;
156 fuse_dir_init(root);
157 }
158
159 return root;
160}
161
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000162static struct super_block *fuse_read_super(struct super_block *sb,
163 void *data, int silent)
164{
165 struct fuse_conn *fc;
166 struct inode *root;
167
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000168 sb->s_blocksize = 1024;
169 sb->s_blocksize_bits = 10;
170 sb->s_magic = FUSE_SUPER_MAGIC;
171 sb->s_op = &fuse_super_operations;
172
Miklos Szeredi79b52f62001-10-24 14:37:13 +0000173 root = get_root_inode(sb);
174 if(root == NULL) {
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000175 printk("fuse_read_super: failed to get root inode\n");
Miklos Szeredi79b52f62001-10-24 14:37:13 +0000176 return NULL;
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000177 }
178
Miklos Szeredi79b52f62001-10-24 14:37:13 +0000179 spin_lock(&fuse_lock);
180 fc = get_conn(data);
181 if(fc == NULL)
182 goto err;
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000183
Miklos Szeredi79b52f62001-10-24 14:37:13 +0000184 if(fc->sb != NULL) {
185 printk("fuse_read_super: connection %i already mounted\n",
186 fc->id);
187 goto err;
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000188 }
189
Miklos Szeredi79b52f62001-10-24 14:37:13 +0000190 sb->u.generic_sbp = fc;
191 sb->s_root = d_alloc_root(root);
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000192 fc->sb = sb;
Miklos Szeredi79b52f62001-10-24 14:37:13 +0000193 spin_unlock(&fuse_lock);
194
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000195 return sb;
196
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000197 err:
Miklos Szeredi79b52f62001-10-24 14:37:13 +0000198 spin_unlock(&fuse_lock);
199 iput(root);
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000200 return NULL;
201}
202
203
204static DECLARE_FSTYPE(fuse_fs_type, "fuse", fuse_read_super, 0);
205
206int fuse_fs_init()
207{
208 int res;
209
210 res = register_filesystem(&fuse_fs_type);
211 if(res)
212 printk("fuse: failed to register filesystem\n");
213
214 return res;
215}
216
217void fuse_fs_cleanup()
218{
219 unregister_filesystem(&fuse_fs_type);
220}
221
222/*
223 * Local Variables:
224 * indent-tabs-mode: t
225 * c-basic-offset: 8
226 * End:
227 */
228