blob: a868edb8eb1d72cf2e51b4b45b845890e972759c [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 super_operations fuse_super_operations = {
Miklos Szeredie8663f32004-01-13 15:33:12 +0000109 .read_inode = fuse_read_inode,
110 .clear_inode = fuse_clear_inode,
111 .put_super = fuse_put_super,
112 .statfs = fuse_statfs,
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000113};
114
115
116static struct fuse_conn *get_conn(struct fuse_mount_data *d)
117{
118 struct fuse_conn *fc = NULL;
119 struct file *file;
120 struct inode *ino;
121
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000122 if(d == NULL) {
123 printk("fuse_read_super: Bad mount data\n");
124 return NULL;
125 }
126
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000127 if(d->version != FUSE_KERNEL_VERSION) {
128 printk("fuse_read_super: Bad version: %i\n", d->version);
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000129 return NULL;
130 }
131
132 file = fget(d->fd);
133 ino = NULL;
134 if(file)
135 ino = file->f_dentry->d_inode;
136
Miklos Szeredi60c23522002-10-24 09:19:43 +0000137 if(!ino || !proc_fuse_dev || proc_fuse_dev->low_ino != ino->i_ino) {
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000138 printk("fuse_read_super: Bad file: %i\n", d->fd);
139 goto out;
140 }
141
142 fc = file->private_data;
143
144 out:
145 fput(file);
146 return fc;
147
148}
149
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000150static struct inode *get_root_inode(struct super_block *sb, unsigned int mode)
Miklos Szeredi79b52f62001-10-24 14:37:13 +0000151{
Miklos Szeredi5e183482001-10-31 14:52:35 +0000152 struct fuse_attr attr;
153 memset(&attr, 0, sizeof(attr));
Miklos Szeredi79b52f62001-10-24 14:37:13 +0000154
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000155 attr.mode = mode;
Miklos Szeredia181e612001-11-06 12:03:23 +0000156 return fuse_iget(sb, 1, &attr, 0);
Miklos Szeredi79b52f62001-10-24 14:37:13 +0000157}
158
Miklos Szeredif85ab242004-01-07 12:16:45 +0000159static int fuse_read_super(struct super_block *sb, void *data, int silent)
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000160{
161 struct fuse_conn *fc;
162 struct inode *root;
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000163 struct fuse_mount_data *d = data;
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000164
Miklos Szeredi05033042001-11-13 16:11:35 +0000165 sb->s_blocksize = PAGE_CACHE_SIZE;
166 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000167 sb->s_magic = FUSE_SUPER_MAGIC;
168 sb->s_op = &fuse_super_operations;
169
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000170 root = get_root_inode(sb, d->rootmode);
Miklos Szeredi79b52f62001-10-24 14:37:13 +0000171 if(root == NULL) {
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000172 printk("fuse_read_super: failed to get root inode\n");
Miklos Szeredif85ab242004-01-07 12:16:45 +0000173 return -EINVAL;
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000174 }
175
Miklos Szeredi79b52f62001-10-24 14:37:13 +0000176 spin_lock(&fuse_lock);
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000177 fc = get_conn(d);
Miklos Szeredi79b52f62001-10-24 14:37:13 +0000178 if(fc == NULL)
179 goto err;
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000180
Miklos Szeredi79b52f62001-10-24 14:37:13 +0000181 if(fc->sb != NULL) {
Miklos Szeredia181e612001-11-06 12:03:23 +0000182 printk("fuse_read_super: connection already mounted\n");
Miklos Szeredi79b52f62001-10-24 14:37:13 +0000183 goto err;
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000184 }
185
Miklos Szeredif85ab242004-01-07 12:16:45 +0000186 SB_FC(sb) = fc;
Miklos Szeredi79b52f62001-10-24 14:37:13 +0000187 sb->s_root = d_alloc_root(root);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000188 if(!sb->s_root)
189 goto err;
190
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000191 fc->sb = sb;
Miklos Szeredi8cffdb92001-11-09 14:49:18 +0000192 fc->flags = d->flags;
193 fc->uid = d->uid;
Miklos Szeredi79b52f62001-10-24 14:37:13 +0000194 spin_unlock(&fuse_lock);
195
Miklos Szeredif85ab242004-01-07 12:16:45 +0000196 return 0;
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000197
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000198 err:
Miklos Szeredi79b52f62001-10-24 14:37:13 +0000199 spin_unlock(&fuse_lock);
200 iput(root);
Miklos Szeredif85ab242004-01-07 12:16:45 +0000201 return -EINVAL;
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000202}
203
Miklos Szeredif85ab242004-01-07 12:16:45 +0000204#ifdef KERNEL_2_6
205static struct super_block *fuse_get_sb(struct file_system_type *fs_type,
Miklos Szeredie8663f32004-01-13 15:33:12 +0000206 int flags, const char *dev_name,
207 void *raw_data)
Miklos Szeredif85ab242004-01-07 12:16:45 +0000208{
209 return get_sb_nodev(fs_type, flags, raw_data, fuse_read_super);
210}
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000211
Miklos Szeredif85ab242004-01-07 12:16:45 +0000212static struct file_system_type fuse_fs_type = {
213 .owner = THIS_MODULE,
214 .name = "fuse",
215 .get_sb = fuse_get_sb,
216 .kill_sb = kill_anon_super,
217 .fs_flags = 0
218};
219#else
220static struct super_block *fuse_read_super_compat(struct super_block *sb,
221 void *data, int silent)
222{
223 int err = fuse_read_super(sb, data, silent);
224 if(err)
225 return NULL;
226 else
227 return sb;
228}
229
230static DECLARE_FSTYPE(fuse_fs_type, "fuse", fuse_read_super_compat, 0);
231#endif
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000232
233int fuse_fs_init()
234{
235 int res;
236
237 res = register_filesystem(&fuse_fs_type);
238 if(res)
239 printk("fuse: failed to register filesystem\n");
240
241 return res;
242}
243
244void fuse_fs_cleanup()
245{
246 unregister_filesystem(&fuse_fs_type);
247}
248
249/*
250 * Local Variables:
251 * indent-tabs-mode: t
252 * c-basic-offset: 8
Miklos Szeredi63a63bc2001-12-02 20:37:53 +0000253 * End:
254 */