blob: f702a4a1f8514d83d7661a66833b83c333ef3a3b [file] [log] [blame]
Miklos Szeredibc22e7b2001-10-23 19:26:04 +00001/*
Miklos Szeredie56818b2004-12-12 11:45:24 +00002 FUSE: Filesystem in Userspace
Miklos Szeredi149f6072005-01-10 12:29:28 +00003 Copyright (C) 2001-2005 Miklos Szeredi <miklos@szeredi.hu>
Miklos Szeredibc22e7b2001-10-23 19:26:04 +00004
Miklos Szeredie56818b2004-12-12 11:45:24 +00005 This program can be distributed under the terms of the GNU GPL.
6 See the file COPYING.
Miklos Szeredibc22e7b2001-10-23 19:26:04 +00007*/
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 Szeredi85c74fc2001-10-28 19:44:14 +000012#include <linux/slab.h>
Miklos Szeredibc22e7b2001-10-23 19:26:04 +000013#include <linux/file.h>
Miklos Szeredi7f6bc892004-04-13 10:49:14 +000014#include <linux/mount.h>
Miklos Szeredi7f6bc892004-04-13 10:49:14 +000015#include <linux/seq_file.h>
Miklos Szeredic6ee9fd2005-01-10 09:53:04 +000016#include <linux/init.h>
Miklos Szeredi13ed4822004-11-20 11:12:21 +000017#include <linux/module.h>
Miklos Szeredif85ab242004-01-07 12:16:45 +000018#ifdef KERNEL_2_6
Miklos Szeredi8ec48ec2004-04-19 10:24:41 +000019#include <linux/parser.h>
Miklos Szeredif85ab242004-01-07 12:16:45 +000020#include <linux/statfs.h>
Miklos Szeredi8ec48ec2004-04-19 10:24:41 +000021#else
22#include "compat/parser.h"
Miklos Szeredif85ab242004-01-07 12:16:45 +000023#endif
Miklos Szeredibc22e7b2001-10-23 19:26:04 +000024
Miklos Szeredic6ee9fd2005-01-10 09:53:04 +000025MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
26MODULE_DESCRIPTION("Filesystem in Userspace");
27#ifdef MODULE_LICENSE
28MODULE_LICENSE("GPL");
29#endif
30
31spinlock_t fuse_lock;
Miklos Szeredi069c9502004-07-16 16:17:02 +000032static kmem_cache_t *fuse_inode_cachep;
Miklos Szerediacb4d362004-07-02 16:20:45 +000033
Miklos Szeredibc22e7b2001-10-23 19:26:04 +000034#define FUSE_SUPER_MAGIC 0x65735546
35
Miklos Szeredif85ab242004-01-07 12:16:45 +000036#ifndef KERNEL_2_6
37#define kstatfs statfs
38#endif
Miklos Szeredib1ab8b52004-11-01 10:57:41 +000039#ifndef MAX_LFS_FILESIZE
Miklos Szerediaa63b6b2004-12-03 13:24:35 +000040#define MAX_LFS_FILESIZE (((u64)PAGE_CACHE_SIZE << (BITS_PER_LONG-1))-1)
Miklos Szeredib1ab8b52004-11-01 10:57:41 +000041#endif
Miklos Szeredi7f6bc892004-04-13 10:49:14 +000042struct fuse_mount_data {
43 int fd;
Miklos Szeredi83a07442004-11-30 18:25:20 +000044 unsigned rootmode;
Miklos Szeredi3c7d41b2005-01-09 20:05:27 +000045 unsigned user_id;
Miklos Szeredi83a07442004-11-30 18:25:20 +000046 unsigned flags;
47 unsigned max_read;
Miklos Szeredi7f6bc892004-04-13 10:49:14 +000048};
Miklos Szeredi69cc79a2004-02-17 08:57:29 +000049
Miklos Szeredia13d9002004-11-02 17:32:03 +000050static struct inode *fuse_alloc_inode(struct super_block *sb)
Miklos Szeredi069c9502004-07-16 16:17:02 +000051{
Miklos Szeredia13d9002004-11-02 17:32:03 +000052 struct inode *inode;
Miklos Szeredi069c9502004-07-16 16:17:02 +000053 struct fuse_inode *fi;
54
Miklos Szeredia13d9002004-11-02 17:32:03 +000055 inode = kmem_cache_alloc(fuse_inode_cachep, SLAB_KERNEL);
56 if (!inode)
57 return NULL;
Miklos Szeredi069c9502004-07-16 16:17:02 +000058
Miklos Szeredia13d9002004-11-02 17:32:03 +000059#ifndef KERNEL_2_6
60 inode->u.generic_ip = NULL;
61#endif
Miklos Szeredi039322d2004-12-01 18:39:12 +000062 fi = get_fuse_inode(inode);
Miklos Szeredi81394522005-01-11 14:24:18 +000063 fi->i_time = jiffies - 1;
64 fi->nodeid = 0;
Miklos Szeredia13d9002004-11-02 17:32:03 +000065 fi->forget_req = fuse_request_alloc();
66 if (!fi->forget_req) {
67 kmem_cache_free(fuse_inode_cachep, inode);
68 return NULL;
69 }
Miklos Szeredia13d9002004-11-02 17:32:03 +000070
71 return inode;
Miklos Szeredi069c9502004-07-16 16:17:02 +000072}
73
Miklos Szeredia13d9002004-11-02 17:32:03 +000074static void fuse_destroy_inode(struct inode *inode)
Miklos Szeredi069c9502004-07-16 16:17:02 +000075{
Miklos Szeredi039322d2004-12-01 18:39:12 +000076 struct fuse_inode *fi = get_fuse_inode(inode);
Miklos Szeredi209f5d02004-07-24 19:56:16 +000077 if (fi->forget_req)
78 fuse_request_free(fi->forget_req);
Miklos Szeredia13d9002004-11-02 17:32:03 +000079 kmem_cache_free(fuse_inode_cachep, inode);
Miklos Szeredi069c9502004-07-16 16:17:02 +000080}
81
Miklos Szeredibc22e7b2001-10-23 19:26:04 +000082static void fuse_read_inode(struct inode *inode)
83{
Miklos Szeredi90d8bef2001-10-26 14:55:42 +000084 /* No op */
Miklos Szeredibc22e7b2001-10-23 19:26:04 +000085}
86
Miklos Szeredia13d9002004-11-02 17:32:03 +000087void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req,
88 unsigned long nodeid, int version)
Miklos Szeredi2778f6c2004-06-21 09:45:30 +000089{
90 struct fuse_forget_in *inarg = &req->misc.forget_in;
91 inarg->version = version;
92 req->in.h.opcode = FUSE_FORGET;
Miklos Szeredia13d9002004-11-02 17:32:03 +000093 req->in.h.nodeid = nodeid;
Miklos Szeredi2778f6c2004-06-21 09:45:30 +000094 req->in.numargs = 1;
95 req->in.args[0].size = sizeof(struct fuse_forget_in);
96 req->in.args[0].value = inarg;
97 request_send_noreply(fc, req);
98}
99
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000100static void fuse_clear_inode(struct inode *inode)
101{
Miklos Szeredi039322d2004-12-01 18:39:12 +0000102 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000103 if (fc) {
Miklos Szeredi039322d2004-12-01 18:39:12 +0000104 struct fuse_inode *fi = get_fuse_inode(inode);
Miklos Szeredia13d9002004-11-02 17:32:03 +0000105 fuse_send_forget(fc, fi->forget_req, fi->nodeid, inode->i_version);
106 fi->forget_req = NULL;
Miklos Szeredid3dd2d52004-06-22 18:46:02 +0000107 }
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000108}
109
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000110void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr)
111{
112 if (S_ISREG(inode->i_mode) && i_size_read(inode) != attr->size)
113#ifdef KERNEL_2_6
114 invalidate_inode_pages(inode->i_mapping);
115#else
116 invalidate_inode_pages(inode);
117#endif
118
119 inode->i_ino = attr->ino;
120 inode->i_mode = (inode->i_mode & S_IFMT) + (attr->mode & 07777);
121 inode->i_nlink = attr->nlink;
122 inode->i_uid = attr->uid;
123 inode->i_gid = attr->gid;
124 i_size_write(inode, attr->size);
125 inode->i_blksize = PAGE_CACHE_SIZE;
126 inode->i_blocks = attr->blocks;
127#ifdef KERNEL_2_6
128 inode->i_atime.tv_sec = attr->atime;
129 inode->i_atime.tv_nsec = attr->atimensec;
130 inode->i_mtime.tv_sec = attr->mtime;
131 inode->i_mtime.tv_nsec = attr->mtimensec;
132 inode->i_ctime.tv_sec = attr->ctime;
133 inode->i_ctime.tv_nsec = attr->ctimensec;
134#else
135 inode->i_atime = attr->atime;
136 inode->i_mtime = attr->mtime;
137 inode->i_ctime = attr->ctime;
138#endif
139}
140
141static void fuse_init_inode(struct inode *inode, struct fuse_attr *attr)
142{
143 inode->i_mode = attr->mode & S_IFMT;
144 i_size_write(inode, attr->size);
145 if (S_ISREG(inode->i_mode)) {
146 fuse_init_common(inode);
147 fuse_init_file_inode(inode);
148 } else if (S_ISDIR(inode->i_mode))
149 fuse_init_dir(inode);
150 else if (S_ISLNK(inode->i_mode))
151 fuse_init_symlink(inode);
152 else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
153 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
154 fuse_init_common(inode);
155 init_special_inode(inode, inode->i_mode,
156 new_decode_dev(attr->rdev));
157 } else {
158 /* Don't let user create weird files */
159 inode->i_mode = S_IFREG;
160 fuse_init_common(inode);
161 fuse_init_file_inode(inode);
162 }
163}
164
165#ifdef KERNEL_2_6
166static int fuse_inode_eq(struct inode *inode, void *_nodeidp)
167{
168 unsigned long nodeid = *(unsigned long *) _nodeidp;
169 if (get_node_id(inode) == nodeid)
170 return 1;
171 else
172 return 0;
173}
174
175static int fuse_inode_set(struct inode *inode, void *_nodeidp)
176{
177 unsigned long nodeid = *(unsigned long *) _nodeidp;
178 get_fuse_inode(inode)->nodeid = nodeid;
179 return 0;
180}
181
182struct inode *fuse_iget(struct super_block *sb, unsigned long nodeid,
183 int generation, struct fuse_attr *attr, int version)
184{
185 struct inode *inode;
186 struct fuse_conn *fc = get_fuse_conn_super(sb);
187 int retried = 0;
188
189 retry:
190 inode = iget5_locked(sb, nodeid, fuse_inode_eq, fuse_inode_set, &nodeid);
191 if (!inode)
192 return NULL;
193
194 if ((inode->i_state & I_NEW)) {
195 inode->i_generation = generation;
196 inode->i_data.backing_dev_info = &fc->bdi;
197 fuse_init_inode(inode, attr);
198 unlock_new_inode(inode);
199 } else if ((inode->i_mode ^ attr->mode) & S_IFMT) {
200 BUG_ON(retried);
201 /* Inode has changed type, any I/O on the old should fail */
202 make_bad_inode(inode);
203 iput(inode);
204 retried = 1;
205 goto retry;
206 }
207
208 fuse_change_attributes(inode, attr);
209 inode->i_version = version;
210 return inode;
211}
212#else
213static int fuse_inode_eq(struct inode *inode, unsigned long ino, void *_nodeidp){
214 unsigned long nodeid = *(unsigned long *) _nodeidp;
215 if (inode->u.generic_ip && get_node_id(inode) == nodeid)
216 return 1;
217 else
218 return 0;
219}
220
221struct inode *fuse_iget(struct super_block *sb, unsigned long nodeid,
222 int generation, struct fuse_attr *attr, int version)
223{
224 struct inode *inode;
225 int retried = 0;
226
227 retry:
228 inode = iget4(sb, attr->ino, fuse_inode_eq, &nodeid);
229 if (!inode)
230 return NULL;
231
232 if (!inode->u.generic_ip) {
233 get_fuse_inode(inode)->nodeid = nodeid;
234 inode->u.generic_ip = inode;
235 inode->i_generation = generation;
236 fuse_init_inode(inode, attr);
237 } else if ((inode->i_mode ^ attr->mode) & S_IFMT) {
238 BUG_ON(retried);
239 /* Inode has changed type, any I/O on the old should fail */
240 remove_inode_hash(inode);
241 make_bad_inode(inode);
242 iput(inode);
243 retried = 1;
244 goto retry;
245 }
246
247 fuse_change_attributes(inode, attr);
248 inode->i_version = version;
249 return inode;
250}
251#endif
252
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000253static void fuse_put_super(struct super_block *sb)
254{
Miklos Szeredi039322d2004-12-01 18:39:12 +0000255 struct fuse_conn *fc = get_fuse_conn_super(sb);
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000256
Miklos Szeredi79b52f62001-10-24 14:37:13 +0000257 spin_lock(&fuse_lock);
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000258 fc->sb = NULL;
Miklos Szeredi3c7d41b2005-01-09 20:05:27 +0000259 fc->user_id = 0;
Miklos Szeredi8cffdb92001-11-09 14:49:18 +0000260 fc->flags = 0;
Miklos Szeredi96249982001-11-21 12:21:19 +0000261 /* Flush all readers on this fs */
262 wake_up_all(&fc->waitq);
Miklos Szeredi79b461a2003-03-10 09:35:34 +0000263 fuse_release_conn(fc);
Miklos Szeredi039322d2004-12-01 18:39:12 +0000264 *get_fuse_conn_super_p(sb) = NULL;
Miklos Szeredi79b52f62001-10-24 14:37:13 +0000265 spin_unlock(&fuse_lock);
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000266}
267
Miklos Szeredif85ab242004-01-07 12:16:45 +0000268static void convert_fuse_statfs(struct kstatfs *stbuf, struct fuse_kstatfs *attr)
Mark Glinesd84b39a2002-01-07 16:32:02 +0000269{
270 stbuf->f_type = FUSE_SUPER_MAGIC;
Miklos Szeredi18e75e42004-02-19 14:23:27 +0000271 stbuf->f_bsize = attr->bsize;
Mark Glinesd84b39a2002-01-07 16:32:02 +0000272 stbuf->f_blocks = attr->blocks;
Miklos Szeredi18e75e42004-02-19 14:23:27 +0000273 stbuf->f_bfree = attr->bfree;
274 stbuf->f_bavail = attr->bavail;
Mark Glinesd84b39a2002-01-07 16:32:02 +0000275 stbuf->f_files = attr->files;
Miklos Szeredi18e75e42004-02-19 14:23:27 +0000276 stbuf->f_ffree = attr->ffree;
Mark Glinesd84b39a2002-01-07 16:32:02 +0000277 stbuf->f_namelen = attr->namelen;
Miklos Szeredi18e75e42004-02-19 14:23:27 +0000278 /* fsid is left zero */
Mark Glinesd84b39a2002-01-07 16:32:02 +0000279}
280
Miklos Szeredif85ab242004-01-07 12:16:45 +0000281static int fuse_statfs(struct super_block *sb, struct kstatfs *buf)
Mark Glinesd84b39a2002-01-07 16:32:02 +0000282{
Miklos Szeredi039322d2004-12-01 18:39:12 +0000283 struct fuse_conn *fc = get_fuse_conn_super(sb);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000284 struct fuse_req *req;
Mark Glinesd84b39a2002-01-07 16:32:02 +0000285 struct fuse_statfs_out outarg;
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000286 int err;
287
288 req = fuse_get_request(fc);
289 if (!req)
290 return -ERESTARTSYS;
291
292 req->in.numargs = 0;
293 req->in.h.opcode = FUSE_STATFS;
294 req->out.numargs = 1;
295 req->out.args[0].size = sizeof(outarg);
296 req->out.args[0].value = &outarg;
297 request_send(fc, req);
298 err = req->out.h.error;
299 if (!err)
Miklos Szeredif85ab242004-01-07 12:16:45 +0000300 convert_fuse_statfs(buf, &outarg.st);
Miklos Szeredi7eafcce2004-06-19 22:42:38 +0000301 fuse_put_request(fc, req);
302 return err;
Mark Glinesd84b39a2002-01-07 16:32:02 +0000303}
304
Miklos Szeredi13ed4822004-11-20 11:12:21 +0000305enum {
306 OPT_FD,
307 OPT_ROOTMODE,
Miklos Szeredi3c7d41b2005-01-09 20:05:27 +0000308 OPT_USER_ID,
Miklos Szerediaa63b6b2004-12-03 13:24:35 +0000309 OPT_DEFAULT_PERMISSIONS,
Miklos Szeredi13ed4822004-11-20 11:12:21 +0000310 OPT_ALLOW_OTHER,
311 OPT_ALLOW_ROOT,
312 OPT_KERNEL_CACHE,
Miklos Szeredie56818b2004-12-12 11:45:24 +0000313#ifndef KERNEL_2_6
Miklos Szeredi13ed4822004-11-20 11:12:21 +0000314 OPT_LARGE_READ,
Miklos Szeredia25d4c22004-11-23 22:32:16 +0000315#endif
Miklos Szeredi13ed4822004-11-20 11:12:21 +0000316 OPT_DIRECT_IO,
317 OPT_MAX_READ,
Miklos Szerediaa63b6b2004-12-03 13:24:35 +0000318 OPT_ERR
Miklos Szeredi13ed4822004-11-20 11:12:21 +0000319};
Miklos Szeredi7f6bc892004-04-13 10:49:14 +0000320
321static match_table_t tokens = {
Miklos Szeredi13ed4822004-11-20 11:12:21 +0000322 {OPT_FD, "fd=%u"},
323 {OPT_ROOTMODE, "rootmode=%o"},
Miklos Szeredi3c7d41b2005-01-09 20:05:27 +0000324 {OPT_USER_ID, "user_id=%u"},
Miklos Szeredi13ed4822004-11-20 11:12:21 +0000325 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
326 {OPT_ALLOW_OTHER, "allow_other"},
327 {OPT_ALLOW_ROOT, "allow_root"},
328 {OPT_KERNEL_CACHE, "kernel_cache"},
Miklos Szeredie56818b2004-12-12 11:45:24 +0000329#ifndef KERNEL_2_6
Miklos Szeredi13ed4822004-11-20 11:12:21 +0000330 {OPT_LARGE_READ, "large_read"},
Miklos Szeredia25d4c22004-11-23 22:32:16 +0000331#endif
Miklos Szeredi13ed4822004-11-20 11:12:21 +0000332 {OPT_DIRECT_IO, "direct_io"},
333 {OPT_MAX_READ, "max_read=%u"},
334 {OPT_ERR, NULL}
Miklos Szeredi7f6bc892004-04-13 10:49:14 +0000335};
336
337static int parse_fuse_opt(char *opt, struct fuse_mount_data *d)
338{
339 char *p;
340 memset(d, 0, sizeof(struct fuse_mount_data));
341 d->fd = -1;
Miklos Szerediad051c32004-07-02 09:22:50 +0000342 d->max_read = ~0;
Miklos Szeredi7f6bc892004-04-13 10:49:14 +0000343
Miklos Szeredi7f6bc892004-04-13 10:49:14 +0000344 while ((p = strsep(&opt, ",")) != NULL) {
345 int token;
346 int value;
347 substring_t args[MAX_OPT_ARGS];
348 if (!*p)
349 continue;
Miklos Szerediaa63b6b2004-12-03 13:24:35 +0000350
Miklos Szeredi7f6bc892004-04-13 10:49:14 +0000351 token = match_token(p, tokens, args);
352 switch (token) {
Miklos Szeredi13ed4822004-11-20 11:12:21 +0000353 case OPT_FD:
Miklos Szeredi7f6bc892004-04-13 10:49:14 +0000354 if (match_int(&args[0], &value))
355 return 0;
356 d->fd = value;
357 break;
358
Miklos Szeredi13ed4822004-11-20 11:12:21 +0000359 case OPT_ROOTMODE:
Miklos Szeredi7f6bc892004-04-13 10:49:14 +0000360 if (match_octal(&args[0], &value))
361 return 0;
362 d->rootmode = value;
363 break;
Miklos Szerediaa63b6b2004-12-03 13:24:35 +0000364
Miklos Szeredi3c7d41b2005-01-09 20:05:27 +0000365 case OPT_USER_ID:
Miklos Szeredi7f6bc892004-04-13 10:49:14 +0000366 if (match_int(&args[0], &value))
367 return 0;
Miklos Szeredi3c7d41b2005-01-09 20:05:27 +0000368 d->user_id = value;
Miklos Szeredi7f6bc892004-04-13 10:49:14 +0000369 break;
Miklos Szerediaa63b6b2004-12-03 13:24:35 +0000370
Miklos Szeredi13ed4822004-11-20 11:12:21 +0000371 case OPT_DEFAULT_PERMISSIONS:
Miklos Szeredi7f6bc892004-04-13 10:49:14 +0000372 d->flags |= FUSE_DEFAULT_PERMISSIONS;
373 break;
374
Miklos Szeredi13ed4822004-11-20 11:12:21 +0000375 case OPT_ALLOW_OTHER:
Miklos Szeredi7f6bc892004-04-13 10:49:14 +0000376 d->flags |= FUSE_ALLOW_OTHER;
377 break;
378
Miklos Szeredi13ed4822004-11-20 11:12:21 +0000379 case OPT_ALLOW_ROOT:
Miklos Szeredi180ff692004-11-01 16:01:05 +0000380 d->flags |= FUSE_ALLOW_ROOT;
381 break;
382
Miklos Szeredi13ed4822004-11-20 11:12:21 +0000383 case OPT_KERNEL_CACHE:
Miklos Szeredi7f6bc892004-04-13 10:49:14 +0000384 d->flags |= FUSE_KERNEL_CACHE;
385 break;
Miklos Szeredia25d4c22004-11-23 22:32:16 +0000386
Miklos Szeredi1bf53ee2004-09-13 11:47:59 +0000387#ifndef KERNEL_2_6
Miklos Szeredie56818b2004-12-12 11:45:24 +0000388 case OPT_LARGE_READ:
Miklos Szeredi7f6bc892004-04-13 10:49:14 +0000389 d->flags |= FUSE_LARGE_READ;
Miklos Szeredi1bf53ee2004-09-13 11:47:59 +0000390 break;
Miklos Szerediaa63b6b2004-12-03 13:24:35 +0000391#endif
Miklos Szeredi13ed4822004-11-20 11:12:21 +0000392 case OPT_DIRECT_IO:
Miklos Szerediad051c32004-07-02 09:22:50 +0000393 d->flags |= FUSE_DIRECT_IO;
394 break;
395
Miklos Szeredi13ed4822004-11-20 11:12:21 +0000396 case OPT_MAX_READ:
Miklos Szerediad051c32004-07-02 09:22:50 +0000397 if (match_int(&args[0], &value))
398 return 0;
399 d->max_read = value;
400 break;
401
Miklos Szeredi7f6bc892004-04-13 10:49:14 +0000402 default:
403 return 0;
404 }
405 }
406 if (d->fd == -1)
407 return 0;
408
409 return 1;
410}
411
412static int fuse_show_options(struct seq_file *m, struct vfsmount *mnt)
413{
Miklos Szeredi039322d2004-12-01 18:39:12 +0000414 struct fuse_conn *fc = get_fuse_conn_super(mnt->mnt_sb);
Miklos Szeredi7f6bc892004-04-13 10:49:14 +0000415
Miklos Szeredi3c7d41b2005-01-09 20:05:27 +0000416 seq_printf(m, ",user_id=%u", fc->user_id);
Miklos Szeredi7f6bc892004-04-13 10:49:14 +0000417 if (fc->flags & FUSE_DEFAULT_PERMISSIONS)
418 seq_puts(m, ",default_permissions");
419 if (fc->flags & FUSE_ALLOW_OTHER)
420 seq_puts(m, ",allow_other");
Miklos Szeredi180ff692004-11-01 16:01:05 +0000421 if (fc->flags & FUSE_ALLOW_ROOT)
422 seq_puts(m, ",allow_root");
Miklos Szeredi7f6bc892004-04-13 10:49:14 +0000423 if (fc->flags & FUSE_KERNEL_CACHE)
424 seq_puts(m, ",kernel_cache");
Miklos Szeredi209f5d02004-07-24 19:56:16 +0000425#ifndef KERNEL_2_6
Miklos Szeredi7f6bc892004-04-13 10:49:14 +0000426 if (fc->flags & FUSE_LARGE_READ)
427 seq_puts(m, ",large_read");
Miklos Szeredi209f5d02004-07-24 19:56:16 +0000428#endif
Miklos Szerediad051c32004-07-02 09:22:50 +0000429 if (fc->flags & FUSE_DIRECT_IO)
430 seq_puts(m, ",direct_io");
431 if (fc->max_read != ~0)
432 seq_printf(m, ",max_read=%u", fc->max_read);
Miklos Szeredi7f6bc892004-04-13 10:49:14 +0000433 return 0;
434}
435
Miklos Szeredi3d60e762004-11-11 14:44:04 +0000436static void free_conn(struct fuse_conn *fc)
437{
438 while (!list_empty(&fc->unused_list)) {
439 struct fuse_req *req;
440 req = list_entry(fc->unused_list.next, struct fuse_req, list);
441 list_del(&req->list);
442 fuse_request_free(req);
443 }
444 kfree(fc);
445}
446
447/* Must be called with the fuse lock held */
448void fuse_release_conn(struct fuse_conn *fc)
449{
450 if (!fc->sb && !fc->file)
451 free_conn(fc);
452}
453
Miklos Szeredi3d60e762004-11-11 14:44:04 +0000454static struct fuse_conn *new_conn(void)
455{
456 struct fuse_conn *fc;
457
458 fc = kmalloc(sizeof(*fc), GFP_KERNEL);
459 if (fc != NULL) {
460 int i;
461 memset(fc, 0, sizeof(*fc));
462 fc->sb = NULL;
463 fc->file = NULL;
464 fc->flags = 0;
Miklos Szeredi3c7d41b2005-01-09 20:05:27 +0000465 fc->user_id = 0;
Miklos Szeredi3d60e762004-11-11 14:44:04 +0000466 init_waitqueue_head(&fc->waitq);
467 INIT_LIST_HEAD(&fc->pending);
468 INIT_LIST_HEAD(&fc->processing);
469 INIT_LIST_HEAD(&fc->unused_list);
Miklos Szeredi3f0005f2005-01-04 19:24:31 +0000470 sema_init(&fc->outstanding_sem, 0);
Miklos Szeredi3d60e762004-11-11 14:44:04 +0000471 for (i = 0; i < FUSE_MAX_OUTSTANDING; i++) {
472 struct fuse_req *req = fuse_request_alloc();
473 if (!req) {
474 free_conn(fc);
475 return NULL;
476 }
Miklos Szeredi3d60e762004-11-11 14:44:04 +0000477 list_add(&req->list, &fc->unused_list);
478 }
Miklos Szeredib6220192005-01-05 16:19:10 +0000479#ifdef KERNEL_2_6_6_PLUS
Miklos Szeredia25d4c22004-11-23 22:32:16 +0000480 fc->bdi.ra_pages = (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
481 fc->bdi.unplug_io_fn = default_unplug_io_fn;
482#endif
Miklos Szeredi3d60e762004-11-11 14:44:04 +0000483 fc->reqctr = 1;
484 }
485 return fc;
486}
487
Miklos Szeredied62d862004-06-20 08:57:39 +0000488static struct fuse_conn *get_conn(struct file *file, struct super_block *sb)
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000489{
Miklos Szeredied62d862004-06-20 08:57:39 +0000490 struct fuse_conn *fc;
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000491
Miklos Szeredie56818b2004-12-12 11:45:24 +0000492 if (file->f_op != &fuse_dev_operations)
Miklos Szerediee808382005-01-21 22:05:37 +0000493 return ERR_PTR(-EINVAL);
Miklos Szeredi3d60e762004-11-11 14:44:04 +0000494 fc = new_conn();
Miklos Szeredie56818b2004-12-12 11:45:24 +0000495 if (fc == NULL)
Miklos Szerediee808382005-01-21 22:05:37 +0000496 return ERR_PTR(-ENOMEM);
Miklos Szeredi3d60e762004-11-11 14:44:04 +0000497 spin_lock(&fuse_lock);
498 if (file->private_data) {
Miklos Szeredi3d60e762004-11-11 14:44:04 +0000499 free_conn(fc);
Miklos Szerediee808382005-01-21 22:05:37 +0000500 fc = ERR_PTR(-EINVAL);
Miklos Szeredi3d60e762004-11-11 14:44:04 +0000501 } else {
502 file->private_data = fc;
503 fc->sb = sb;
504 fc->file = file;
505 }
506 spin_unlock(&fuse_lock);
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000507 return fc;
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000508}
509
Miklos Szeredi83a07442004-11-30 18:25:20 +0000510static struct inode *get_root_inode(struct super_block *sb, unsigned mode)
Miklos Szeredi79b52f62001-10-24 14:37:13 +0000511{
Miklos Szeredi5e183482001-10-31 14:52:35 +0000512 struct fuse_attr attr;
513 memset(&attr, 0, sizeof(attr));
Miklos Szeredi79b52f62001-10-24 14:37:13 +0000514
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000515 attr.mode = mode;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000516 attr.ino = FUSE_ROOT_ID;
Miklos Szeredi76f65782004-02-19 16:55:40 +0000517 return fuse_iget(sb, 1, 0, &attr, 0);
Miklos Szeredi79b52f62001-10-24 14:37:13 +0000518}
519
Miklos Szeredie815c032004-01-19 18:20:49 +0000520#ifdef KERNEL_2_6
Miklos Szeredie815c032004-01-19 18:20:49 +0000521static struct dentry *fuse_get_dentry(struct super_block *sb, void *vobjp)
522{
Miklos Szeredif43f0632005-02-28 11:46:56 +0000523 int err;
Miklos Szeredie815c032004-01-19 18:20:49 +0000524 __u32 *objp = vobjp;
Miklos Szeredi13ed4822004-11-20 11:12:21 +0000525 unsigned long nodeid = objp[0];
Miklos Szeredi76f65782004-02-19 16:55:40 +0000526 __u32 generation = objp[1];
Miklos Szeredie815c032004-01-19 18:20:49 +0000527 struct inode *inode;
528 struct dentry *entry;
529
Miklos Szeredif43f0632005-02-28 11:46:56 +0000530 err = -ESTALE;
Miklos Szeredi13ed4822004-11-20 11:12:21 +0000531 if (nodeid == 0)
Miklos Szeredif43f0632005-02-28 11:46:56 +0000532 goto error;
Miklos Szeredie815c032004-01-19 18:20:49 +0000533
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000534 inode = ilookup5(sb, nodeid, fuse_inode_eq, &nodeid);
Miklos Szeredif43f0632005-02-28 11:46:56 +0000535 if (!inode)
536 goto error;
537 if (inode->i_generation != generation)
538 goto error_iput;
Miklos Szeredie815c032004-01-19 18:20:49 +0000539
Miklos Szeredif43f0632005-02-28 11:46:56 +0000540 err = -ENOMEM;
Miklos Szeredie815c032004-01-19 18:20:49 +0000541 entry = d_alloc_anon(inode);
Miklos Szeredif43f0632005-02-28 11:46:56 +0000542 if (!entry)
543 goto error_iput;
Miklos Szeredie815c032004-01-19 18:20:49 +0000544
545 return entry;
Miklos Szeredif43f0632005-02-28 11:46:56 +0000546
547 error_iput:
548 iput(inode);
549 error:
550 return ERR_PTR(err);
Miklos Szeredie815c032004-01-19 18:20:49 +0000551}
552
Miklos Szeredi13ed4822004-11-20 11:12:21 +0000553static int fuse_encode_fh(struct dentry *dentry, __u32 *fh, int *max_len,
554 int connectable)
555{
556 struct inode *inode = dentry->d_inode;
Miklos Szeredi13ed4822004-11-20 11:12:21 +0000557 int len = *max_len;
558 int type = 1;
Miklos Szerediaa63b6b2004-12-03 13:24:35 +0000559
Miklos Szeredi13ed4822004-11-20 11:12:21 +0000560 if (len < 2 || (connectable && len < 4))
561 return 255;
562
563 len = 2;
Miklos Szeredi039322d2004-12-01 18:39:12 +0000564 fh[0] = get_fuse_inode(inode)->nodeid;
Miklos Szeredi13ed4822004-11-20 11:12:21 +0000565 fh[1] = inode->i_generation;
566 if (connectable && !S_ISDIR(inode->i_mode)) {
567 struct inode *parent;
Miklos Szeredi13ed4822004-11-20 11:12:21 +0000568
569 spin_lock(&dentry->d_lock);
570 parent = dentry->d_parent->d_inode;
Miklos Szeredi039322d2004-12-01 18:39:12 +0000571 fh[2] = get_fuse_inode(parent)->nodeid;
Miklos Szeredi13ed4822004-11-20 11:12:21 +0000572 fh[3] = parent->i_generation;
573 spin_unlock(&dentry->d_lock);
574 len = 4;
575 type = 2;
576 }
577 *max_len = len;
578 return type;
579}
580
Miklos Szeredie815c032004-01-19 18:20:49 +0000581static struct export_operations fuse_export_operations = {
582 .get_dentry = fuse_get_dentry,
Miklos Szeredi13ed4822004-11-20 11:12:21 +0000583 .encode_fh = fuse_encode_fh,
Miklos Szeredie815c032004-01-19 18:20:49 +0000584};
585#endif
586
587static struct super_operations fuse_super_operations = {
Miklos Szeredia13d9002004-11-02 17:32:03 +0000588 .alloc_inode = fuse_alloc_inode,
589 .destroy_inode = fuse_destroy_inode,
Miklos Szeredie815c032004-01-19 18:20:49 +0000590 .read_inode = fuse_read_inode,
591 .clear_inode = fuse_clear_inode,
592 .put_super = fuse_put_super,
593 .statfs = fuse_statfs,
Miklos Szeredi7f6bc892004-04-13 10:49:14 +0000594 .show_options = fuse_show_options,
Miklos Szeredie815c032004-01-19 18:20:49 +0000595};
596
Miklos Szeredi81394522005-01-11 14:24:18 +0000597static int fuse_fill_super(struct super_block *sb, void *data, int silent)
Miklos Szerediaa63b6b2004-12-03 13:24:35 +0000598{
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000599 struct fuse_conn *fc;
600 struct inode *root;
Miklos Szeredi7f6bc892004-04-13 10:49:14 +0000601 struct fuse_mount_data d;
Miklos Szeredied62d862004-06-20 08:57:39 +0000602 struct file *file;
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000603 int err;
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000604
Miklos Szeredi7f6bc892004-04-13 10:49:14 +0000605 if (!parse_fuse_opt((char *) data, &d))
606 return -EINVAL;
607
Miklos Szeredi377b2ea2004-02-06 09:48:51 +0000608 sb->s_blocksize = PAGE_CACHE_SIZE;
609 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
610 sb->s_magic = FUSE_SUPER_MAGIC;
611 sb->s_op = &fuse_super_operations;
612 sb->s_maxbytes = MAX_LFS_FILESIZE;
Miklos Szeredie815c032004-01-19 18:20:49 +0000613#ifdef KERNEL_2_6
614 sb->s_export_op = &fuse_export_operations;
615#endif
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000616
Miklos Szeredied62d862004-06-20 08:57:39 +0000617 file = fget(d.fd);
618 if (!file)
Miklos Szeredi307242f2004-01-26 11:28:44 +0000619 return -EINVAL;
Miklos Szeredi7f6bc892004-04-13 10:49:14 +0000620
Miklos Szeredied62d862004-06-20 08:57:39 +0000621 fc = get_conn(file, sb);
Miklos Szeredied62d862004-06-20 08:57:39 +0000622 fput(file);
Miklos Szerediee808382005-01-21 22:05:37 +0000623 if (IS_ERR(fc))
624 return PTR_ERR(fc);
Miklos Szeredied62d862004-06-20 08:57:39 +0000625
Miklos Szeredi7f6bc892004-04-13 10:49:14 +0000626 fc->flags = d.flags;
Miklos Szeredi3c7d41b2005-01-09 20:05:27 +0000627 fc->user_id = d.user_id;
Miklos Szerediad051c32004-07-02 09:22:50 +0000628 fc->max_read = d.max_read;
Miklos Szeredia25d4c22004-11-23 22:32:16 +0000629#ifdef KERNEL_2_6
630 if (fc->max_read / PAGE_CACHE_SIZE < fc->bdi.ra_pages)
631 fc->bdi.ra_pages = fc->max_read / PAGE_CACHE_SIZE;
632#endif
Miklos Szerediad051c32004-07-02 09:22:50 +0000633 fc->max_write = FUSE_MAX_IN / 2;
Miklos Szerediaa63b6b2004-12-03 13:24:35 +0000634
Miklos Szeredi039322d2004-12-01 18:39:12 +0000635 *get_fuse_conn_super_p(sb) = fc;
Miklos Szeredi307242f2004-01-26 11:28:44 +0000636
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000637 err = -ENOMEM;
Miklos Szeredi7f6bc892004-04-13 10:49:14 +0000638 root = get_root_inode(sb, d.rootmode);
Miklos Szeredie56818b2004-12-12 11:45:24 +0000639 if (root == NULL)
Miklos Szeredied62d862004-06-20 08:57:39 +0000640 goto err;
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000641
Miklos Szeredi79b52f62001-10-24 14:37:13 +0000642 sb->s_root = d_alloc_root(root);
Miklos Szeredied62d862004-06-20 08:57:39 +0000643 if (!sb->s_root) {
644 iput(root);
645 goto err;
646 }
Miklos Szeredi3f0005f2005-01-04 19:24:31 +0000647 fuse_send_init(fc);
Miklos Szeredif85ab242004-01-07 12:16:45 +0000648 return 0;
Miklos Szeredied62d862004-06-20 08:57:39 +0000649
650 err:
651 spin_lock(&fuse_lock);
652 fc->sb = NULL;
653 fuse_release_conn(fc);
654 spin_unlock(&fuse_lock);
Miklos Szeredi039322d2004-12-01 18:39:12 +0000655 *get_fuse_conn_super_p(sb) = NULL;
Miklos Szeredi0f62d722005-01-04 12:45:54 +0000656 return err;
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000657}
658
Miklos Szeredif85ab242004-01-07 12:16:45 +0000659#ifdef KERNEL_2_6
660static struct super_block *fuse_get_sb(struct file_system_type *fs_type,
Miklos Szeredie8663f32004-01-13 15:33:12 +0000661 int flags, const char *dev_name,
662 void *raw_data)
Miklos Szeredif85ab242004-01-07 12:16:45 +0000663{
Miklos Szeredi81394522005-01-11 14:24:18 +0000664 return get_sb_nodev(fs_type, flags, raw_data, fuse_fill_super);
Miklos Szeredif85ab242004-01-07 12:16:45 +0000665}
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000666
Miklos Szeredif85ab242004-01-07 12:16:45 +0000667static struct file_system_type fuse_fs_type = {
Miklos Szeredi377b2ea2004-02-06 09:48:51 +0000668 .owner = THIS_MODULE,
669 .name = "fuse",
670 .get_sb = fuse_get_sb,
671 .kill_sb = kill_anon_super,
Miklos Szeredif85ab242004-01-07 12:16:45 +0000672};
673#else
674static struct super_block *fuse_read_super_compat(struct super_block *sb,
675 void *data, int silent)
676{
Miklos Szeredi81394522005-01-11 14:24:18 +0000677 int err = fuse_fill_super(sb, data, silent);
Miklos Szeredic26c14d2004-04-09 17:48:32 +0000678 if (err)
Miklos Szeredif85ab242004-01-07 12:16:45 +0000679 return NULL;
680 else
681 return sb;
682}
683
684static DECLARE_FSTYPE(fuse_fs_type, "fuse", fuse_read_super_compat, 0);
685#endif
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000686
Miklos Szeredi81394522005-01-11 14:24:18 +0000687static void fuse_inode_init_once(void *foo, kmem_cache_t *cachep,
Miklos Szeredia13d9002004-11-02 17:32:03 +0000688 unsigned long flags)
689{
Miklos Szeredi13ed4822004-11-20 11:12:21 +0000690 struct inode * inode = foo;
Miklos Szeredia13d9002004-11-02 17:32:03 +0000691
692 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
693 SLAB_CTOR_CONSTRUCTOR)
694 inode_init_once(inode);
695}
696
Miklos Szeredi9080c792005-01-08 11:50:08 +0000697static int __init fuse_fs_init(void)
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000698{
Miklos Szeredi069c9502004-07-16 16:17:02 +0000699 int err;
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000700
Miklos Szeredi069c9502004-07-16 16:17:02 +0000701 err = register_filesystem(&fuse_fs_type);
702 if (err)
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000703 printk("fuse: failed to register filesystem\n");
Miklos Szeredi069c9502004-07-16 16:17:02 +0000704 else {
705 fuse_inode_cachep = kmem_cache_create("fuse_inode",
Miklos Szeredi81394522005-01-11 14:24:18 +0000706 sizeof(struct fuse_inode),
Miklos Szeredia13d9002004-11-02 17:32:03 +0000707 0, SLAB_HWCACHE_ALIGN,
708 fuse_inode_init_once, NULL);
Miklos Szeredi069c9502004-07-16 16:17:02 +0000709 if (!fuse_inode_cachep) {
710 unregister_filesystem(&fuse_fs_type);
711 err = -ENOMEM;
712 }
713 }
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000714
Miklos Szeredi069c9502004-07-16 16:17:02 +0000715 return err;
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000716}
717
Miklos Szeredi9080c792005-01-08 11:50:08 +0000718static void fuse_fs_cleanup(void)
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000719{
720 unregister_filesystem(&fuse_fs_type);
Miklos Szeredi069c9502004-07-16 16:17:02 +0000721 kmem_cache_destroy(fuse_inode_cachep);
Miklos Szeredibc22e7b2001-10-23 19:26:04 +0000722}
Miklos Szeredi9080c792005-01-08 11:50:08 +0000723
Miklos Szeredi81394522005-01-11 14:24:18 +0000724static int __init fuse_init(void)
Miklos Szeredi9080c792005-01-08 11:50:08 +0000725{
726 int res;
727
728 printk("fuse init (API version %i.%i)\n",
729 FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
730#ifndef FUSE_MAINLINE
731 printk("fuse distribution version: %s\n", FUSE_VERSION);
732#endif
733
734 spin_lock_init(&fuse_lock);
735 res = fuse_fs_init();
736 if (res)
737 goto err;
738
739 res = fuse_dev_init();
740 if (res)
741 goto err_fs_cleanup;
742
743 return 0;
744
745 err_fs_cleanup:
746 fuse_fs_cleanup();
747 err:
748 return res;
749}
750
Miklos Szeredi81394522005-01-11 14:24:18 +0000751static void __exit fuse_exit(void)
Miklos Szeredi9080c792005-01-08 11:50:08 +0000752{
753 printk(KERN_DEBUG "fuse exit\n");
754
755 fuse_fs_cleanup();
756 fuse_dev_cleanup();
757}
758
759module_init(fuse_init);
760module_exit(fuse_exit);