blob: 4a160512c4f574d1d7da31d6e69fbf4efd40bf85 [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
Miklos Szeredi05033042001-11-13 16:11:35 +000011#include <linux/pagemap.h>
Miklos Szeredibc22e7b2001-10-23 19:26:04 +000012#include <linux/sched.h>
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000013#include <linux/slab.h>
Miklos Szeredibc22e7b2001-10-23 19:26:04 +000014#include <linux/file.h>
Miklos Szeredi60c23522002-10-24 09:19:43 +000015#include <linux/proc_fs.h>
Miklos Szeredif85ab242004-01-07 12:16:45 +000016#ifdef KERNEL_2_6
17#include <linux/statfs.h>
18#endif
Miklos Szeredibc22e7b2001-10-23 19:26:04 +000019
20#define FUSE_SUPER_MAGIC 0x65735546
21
Miklos Szeredif85ab242004-01-07 12:16:45 +000022#ifndef KERNEL_2_6
23#define kstatfs statfs
24#endif
25
Miklos Szeredibc22e7b2001-10-23 19:26:04 +000026static void fuse_read_inode(struct inode *inode)
27{
Miklos Szeredi90d8bef2001-10-26 14:55:42 +000028 /* No op */
Miklos Szeredibc22e7b2001-10-23 19:26:04 +000029}
30
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000031static void fuse_clear_inode(struct inode *inode)
32{
Miklos Szeredi5e183482001-10-31 14:52:35 +000033 struct fuse_conn *fc = INO_FC(inode);
Miklos Szeredi43696432001-11-18 19:15:05 +000034 struct fuse_in *in = NULL;
35 struct fuse_forget_in *inarg = NULL;
Miklos Szeredi7c35cf92004-01-14 16:56:49 +000036 unsigned int s = sizeof(struct fuse_in) + sizeof(struct fuse_forget_in);
Miklos Szeredia181e612001-11-06 12:03:23 +000037
Miklos Szeredi43696432001-11-18 19:15:05 +000038 if(fc == NULL)
39 return;
40
Miklos Szeredi7c35cf92004-01-14 16:56:49 +000041 in = kmalloc(s, GFP_NOFS);
Miklos Szeredi43696432001-11-18 19:15:05 +000042 if(!in)
43 return;
Miklos Szeredi7c35cf92004-01-14 16:56:49 +000044 memset(in, 0, s);
45 inarg = (struct fuse_forget_in *) (in + 1);
Miklos Szeredi43696432001-11-18 19:15:05 +000046 inarg->version = inode->i_version;
47
48 in->h.opcode = FUSE_FORGET;
49 in->h.ino = inode->i_ino;
50 in->numargs = 1;
51 in->args[0].size = sizeof(struct fuse_forget_in);
52 in->args[0].value = inarg;
53
54 if(!request_send_noreply(fc, in))
55 return;
56
Miklos Szeredi43696432001-11-18 19:15:05 +000057 kfree(in);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000058}
59
Miklos Szeredibc22e7b2001-10-23 19:26:04 +000060static void fuse_put_super(struct super_block *sb)
61{
Miklos Szeredif85ab242004-01-07 12:16:45 +000062 struct fuse_conn *fc = SB_FC(sb);
Miklos Szeredibc22e7b2001-10-23 19:26:04 +000063
Miklos Szeredi79b52f62001-10-24 14:37:13 +000064 spin_lock(&fuse_lock);
Miklos Szeredibc22e7b2001-10-23 19:26:04 +000065 fc->sb = NULL;
Miklos Szeredi8cffdb92001-11-09 14:49:18 +000066 fc->uid = 0;
67 fc->flags = 0;
Miklos Szeredi96249982001-11-21 12:21:19 +000068 /* Flush all readers on this fs */
69 wake_up_all(&fc->waitq);
Miklos Szeredi79b461a2003-03-10 09:35:34 +000070 fuse_release_conn(fc);
Miklos Szeredif85ab242004-01-07 12:16:45 +000071 SB_FC(sb) = NULL;
Miklos Szeredi79b52f62001-10-24 14:37:13 +000072 spin_unlock(&fuse_lock);
Miklos Szeredibc22e7b2001-10-23 19:26:04 +000073}
74
Miklos Szeredif85ab242004-01-07 12:16:45 +000075static void convert_fuse_statfs(struct kstatfs *stbuf, struct fuse_kstatfs *attr)
Mark Glinesd84b39a2002-01-07 16:32:02 +000076{
77 stbuf->f_type = FUSE_SUPER_MAGIC;
78 stbuf->f_bsize = attr->block_size;
79 stbuf->f_blocks = attr->blocks;
80 stbuf->f_bfree = stbuf->f_bavail = attr->blocks_free;
81 stbuf->f_files = attr->files;
82 stbuf->f_ffree = attr->files_free;
83 /* Is this field necessary? Most filesystems ignore it...
84 stbuf->f_fsid.val[0] = (FUSE_SUPER_MAGIC>>16)&0xffff;
85 stbuf->f_fsid.val[1] = FUSE_SUPER_MAGIC &0xffff; */
86 stbuf->f_namelen = attr->namelen;
87}
88
Miklos Szeredif85ab242004-01-07 12:16:45 +000089static int fuse_statfs(struct super_block *sb, struct kstatfs *buf)
Mark Glinesd84b39a2002-01-07 16:32:02 +000090{
Miklos Szeredif85ab242004-01-07 12:16:45 +000091 struct fuse_conn *fc = SB_FC(sb);
Mark Glinesd84b39a2002-01-07 16:32:02 +000092 struct fuse_in in = FUSE_IN_INIT;
93 struct fuse_out out = FUSE_OUT_INIT;
94 struct fuse_statfs_out outarg;
95
96 in.numargs = 0;
97 in.h.opcode = FUSE_STATFS;
98 out.numargs = 1;
99 out.args[0].size = sizeof(outarg);
100 out.args[0].value = &outarg;
101 request_send(fc, &in, &out);
102 if(!out.h.error)
Miklos Szeredif85ab242004-01-07 12:16:45 +0000103 convert_fuse_statfs(buf, &outarg.st);
Mark Glinesd84b39a2002-01-07 16:32:02 +0000104
105 return out.h.error;
106}
107
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000108static struct fuse_conn *get_conn(struct fuse_mount_data *d)
109{
110 struct fuse_conn *fc = NULL;
111 struct file *file;
112 struct inode *ino;
113
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000114 if(d == NULL) {
115 printk("fuse_read_super: Bad mount data\n");
116 return NULL;
117 }
118
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000119 if(d->version != FUSE_KERNEL_VERSION) {
120 printk("fuse_read_super: Bad version: %i\n", d->version);
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000121 return NULL;
122 }
123
124 file = fget(d->fd);
125 ino = NULL;
126 if(file)
127 ino = file->f_dentry->d_inode;
128
Miklos Szeredi60c23522002-10-24 09:19:43 +0000129 if(!ino || !proc_fuse_dev || proc_fuse_dev->low_ino != ino->i_ino) {
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000130 printk("fuse_read_super: Bad file: %i\n", d->fd);
131 goto out;
132 }
133
134 fc = file->private_data;
135
136 out:
137 fput(file);
138 return fc;
139
140}
141
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000142static struct inode *get_root_inode(struct super_block *sb, unsigned int mode)
Miklos Szeredi79b52f62001-10-24 14:37:13 +0000143{
Miklos Szeredi5e183482001-10-31 14:52:35 +0000144 struct fuse_attr attr;
145 memset(&attr, 0, sizeof(attr));
Miklos Szeredi79b52f62001-10-24 14:37:13 +0000146
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000147 attr.mode = mode;
Miklos Szeredia181e612001-11-06 12:03:23 +0000148 return fuse_iget(sb, 1, &attr, 0);
Miklos Szeredi79b52f62001-10-24 14:37:13 +0000149}
150
Miklos Szeredie815c032004-01-19 18:20:49 +0000151
152#ifdef KERNEL_2_6
153
154static struct dentry *fuse_get_dentry(struct super_block *sb, void *vobjp)
155{
156 __u32 *objp = vobjp;
157 unsigned long ino = objp[0];
158 /* __u32 generation = objp[1]; */
159 struct inode *inode;
160 struct dentry *entry;
161
162 if(ino == 0)
163 return ERR_PTR(-ESTALE);
164
165 inode = ilookup(sb, ino);
166 if(!inode)
167 return ERR_PTR(-ESTALE);
168
169 entry = d_alloc_anon(inode);
170 if(!entry) {
171 iput(inode);
172 return ERR_PTR(-ENOMEM);
173 }
174
175 return entry;
176}
177
178static struct export_operations fuse_export_operations = {
179 .get_dentry = fuse_get_dentry,
180};
181#endif
182
183static struct super_operations fuse_super_operations = {
184 .read_inode = fuse_read_inode,
185 .clear_inode = fuse_clear_inode,
186 .put_super = fuse_put_super,
187 .statfs = fuse_statfs,
188};
189
Miklos Szeredif85ab242004-01-07 12:16:45 +0000190static int fuse_read_super(struct super_block *sb, void *data, int silent)
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000191{
192 struct fuse_conn *fc;
193 struct inode *root;
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000194 struct fuse_mount_data *d = data;
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000195
Miklos Szeredi05033042001-11-13 16:11:35 +0000196 sb->s_blocksize = PAGE_CACHE_SIZE;
197 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000198 sb->s_magic = FUSE_SUPER_MAGIC;
199 sb->s_op = &fuse_super_operations;
Miklos Szeredie815c032004-01-19 18:20:49 +0000200#ifdef KERNEL_2_6
201 sb->s_export_op = &fuse_export_operations;
202#endif
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000203
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000204 root = get_root_inode(sb, d->rootmode);
Miklos Szeredi79b52f62001-10-24 14:37:13 +0000205 if(root == NULL) {
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000206 printk("fuse_read_super: failed to get root inode\n");
Miklos Szeredif85ab242004-01-07 12:16:45 +0000207 return -EINVAL;
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000208 }
209
Miklos Szeredi79b52f62001-10-24 14:37:13 +0000210 spin_lock(&fuse_lock);
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000211 fc = get_conn(d);
Miklos Szeredi79b52f62001-10-24 14:37:13 +0000212 if(fc == NULL)
213 goto err;
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000214
Miklos Szeredi79b52f62001-10-24 14:37:13 +0000215 if(fc->sb != NULL) {
Miklos Szeredia181e612001-11-06 12:03:23 +0000216 printk("fuse_read_super: connection already mounted\n");
Miklos Szeredi79b52f62001-10-24 14:37:13 +0000217 goto err;
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000218 }
219
Miklos Szeredif85ab242004-01-07 12:16:45 +0000220 SB_FC(sb) = fc;
Miklos Szeredi79b52f62001-10-24 14:37:13 +0000221 sb->s_root = d_alloc_root(root);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000222 if(!sb->s_root)
223 goto err;
224
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000225 fc->sb = sb;
Miklos Szeredi8cffdb92001-11-09 14:49:18 +0000226 fc->flags = d->flags;
227 fc->uid = d->uid;
Miklos Szeredi79b52f62001-10-24 14:37:13 +0000228 spin_unlock(&fuse_lock);
229
Miklos Szeredif85ab242004-01-07 12:16:45 +0000230 return 0;
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000231
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000232 err:
Miklos Szeredi79b52f62001-10-24 14:37:13 +0000233 spin_unlock(&fuse_lock);
234 iput(root);
Miklos Szeredif85ab242004-01-07 12:16:45 +0000235 return -EINVAL;
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000236}
237
Miklos Szeredif85ab242004-01-07 12:16:45 +0000238#ifdef KERNEL_2_6
239static struct super_block *fuse_get_sb(struct file_system_type *fs_type,
Miklos Szeredie8663f32004-01-13 15:33:12 +0000240 int flags, const char *dev_name,
241 void *raw_data)
Miklos Szeredif85ab242004-01-07 12:16:45 +0000242{
243 return get_sb_nodev(fs_type, flags, raw_data, fuse_read_super);
244}
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000245
Miklos Szeredif85ab242004-01-07 12:16:45 +0000246static struct file_system_type fuse_fs_type = {
247 .owner = THIS_MODULE,
248 .name = "fuse",
249 .get_sb = fuse_get_sb,
250 .kill_sb = kill_anon_super,
251 .fs_flags = 0
252};
253#else
254static struct super_block *fuse_read_super_compat(struct super_block *sb,
255 void *data, int silent)
256{
257 int err = fuse_read_super(sb, data, silent);
258 if(err)
259 return NULL;
260 else
261 return sb;
262}
263
264static DECLARE_FSTYPE(fuse_fs_type, "fuse", fuse_read_super_compat, 0);
265#endif
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000266
267int fuse_fs_init()
268{
269 int res;
270
271 res = register_filesystem(&fuse_fs_type);
272 if(res)
273 printk("fuse: failed to register filesystem\n");
274
275 return res;
276}
277
278void fuse_fs_cleanup()
279{
280 unregister_filesystem(&fuse_fs_type);
281}
282
283/*
284 * Local Variables:
285 * indent-tabs-mode: t
286 * c-basic-offset: 8
Miklos Szeredi63a63bc2001-12-02 20:37:53 +0000287 * End:
288 */