blob: 9aa6f46d0c32c5c9bd13e966c3c95b39ea6f4d81 [file] [log] [blame]
Miklos Szeredid8a5ba42005-09-09 13:10:26 -07001/*
2 FUSE: Filesystem in Userspace
Miklos Szeredi1729a162008-11-26 12:03:54 +01003 Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu>
Miklos Szeredid8a5ba42005-09-09 13:10:26 -07004
5 This program can be distributed under the terms of the GNU GPL.
6 See the file COPYING.
7*/
8
9#include "fuse_i.h"
10
11#include <linux/pagemap.h>
12#include <linux/slab.h>
13#include <linux/file.h>
Miklos Szeredid8a5ba42005-09-09 13:10:26 -070014#include <linux/seq_file.h>
15#include <linux/init.h>
16#include <linux/module.h>
Miklos Szeredid8a5ba42005-09-09 13:10:26 -070017#include <linux/parser.h>
18#include <linux/statfs.h>
Miklos Szeredi9c8ef562006-06-25 05:48:55 -070019#include <linux/random.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040020#include <linux/sched.h>
Miklos Szeredidbd561d2008-07-25 01:49:00 -070021#include <linux/exportfs.h>
Miklos Szeredid8a5ba42005-09-09 13:10:26 -070022
23MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
24MODULE_DESCRIPTION("Filesystem in Userspace");
25MODULE_LICENSE("GPL");
26
Christoph Lametere18b8902006-12-06 20:33:20 -080027static struct kmem_cache *fuse_inode_cachep;
Miklos Szeredibafa9652006-06-25 05:48:51 -070028struct list_head fuse_conn_list;
29DEFINE_MUTEX(fuse_mutex);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -070030
31#define FUSE_SUPER_MAGIC 0x65735546
32
Miklos Szeredid1875db2008-02-08 04:21:43 -080033#define FUSE_DEFAULT_BLKSIZE 512
34
Csaba Henk7a6d3c82009-07-01 17:28:41 -070035/** Maximum number of outstanding background requests */
36#define FUSE_DEFAULT_MAX_BACKGROUND 12
37
38/** Congestion starts at 75% of maximum */
39#define FUSE_DEFAULT_CONGESTION_THRESHOLD (FUSE_DEFAULT_MAX_BACKGROUND * 3 / 4)
40
Miklos Szeredid8a5ba42005-09-09 13:10:26 -070041struct fuse_mount_data {
42 int fd;
43 unsigned rootmode;
44 unsigned user_id;
Miklos Szeredi87729a52005-09-09 13:10:34 -070045 unsigned group_id;
Miklos Szeredi1729a162008-11-26 12:03:54 +010046 unsigned fd_present:1;
47 unsigned rootmode_present:1;
48 unsigned user_id_present:1;
49 unsigned group_id_present:1;
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -070050 unsigned flags;
Miklos Szeredidb50b962005-09-09 13:10:33 -070051 unsigned max_read;
Miklos Szeredid8091612006-12-06 20:35:48 -080052 unsigned blksize;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -070053};
54
55static struct inode *fuse_alloc_inode(struct super_block *sb)
56{
57 struct inode *inode;
58 struct fuse_inode *fi;
59
Christoph Lametere94b1762006-12-06 20:33:17 -080060 inode = kmem_cache_alloc(fuse_inode_cachep, GFP_KERNEL);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -070061 if (!inode)
62 return NULL;
63
64 fi = get_fuse_inode(inode);
Miklos Szeredi0a0898c2006-07-30 03:04:10 -070065 fi->i_time = 0;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -070066 fi->nodeid = 0;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -070067 fi->nlookup = 0;
John Muirfbee36b2007-11-28 16:22:02 -080068 fi->attr_version = 0;
Miklos Szeredi3be5a522008-04-30 00:54:41 -070069 fi->writectr = 0;
Miklos Szeredi93a8c3c2007-10-18 03:07:03 -070070 INIT_LIST_HEAD(&fi->write_files);
Miklos Szeredi3be5a522008-04-30 00:54:41 -070071 INIT_LIST_HEAD(&fi->queued_writes);
72 INIT_LIST_HEAD(&fi->writepages);
73 init_waitqueue_head(&fi->page_waitq);
Miklos Szeredie5e55582005-09-09 13:10:28 -070074 fi->forget_req = fuse_request_alloc();
75 if (!fi->forget_req) {
76 kmem_cache_free(fuse_inode_cachep, inode);
77 return NULL;
78 }
Miklos Szeredid8a5ba42005-09-09 13:10:26 -070079
80 return inode;
81}
82
83static void fuse_destroy_inode(struct inode *inode)
84{
Miklos Szeredie5e55582005-09-09 13:10:28 -070085 struct fuse_inode *fi = get_fuse_inode(inode);
Miklos Szeredi93a8c3c2007-10-18 03:07:03 -070086 BUG_ON(!list_empty(&fi->write_files));
Miklos Szeredi3be5a522008-04-30 00:54:41 -070087 BUG_ON(!list_empty(&fi->queued_writes));
Miklos Szeredie5e55582005-09-09 13:10:28 -070088 if (fi->forget_req)
89 fuse_request_free(fi->forget_req);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -070090 kmem_cache_free(fuse_inode_cachep, inode);
91}
92
Miklos Szeredie5e55582005-09-09 13:10:28 -070093void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req,
Miklos Szeredib48badf2008-04-30 00:54:44 -070094 u64 nodeid, u64 nlookup)
Miklos Szeredie5e55582005-09-09 13:10:28 -070095{
96 struct fuse_forget_in *inarg = &req->misc.forget_in;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -070097 inarg->nlookup = nlookup;
Miklos Szeredie5e55582005-09-09 13:10:28 -070098 req->in.h.opcode = FUSE_FORGET;
99 req->in.h.nodeid = nodeid;
100 req->in.numargs = 1;
101 req->in.args[0].size = sizeof(struct fuse_forget_in);
102 req->in.args[0].value = inarg;
Tejun Heob93f8582008-11-26 12:03:55 +0100103 fuse_request_send_noreply(fc, req);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700104}
105
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700106static void fuse_clear_inode(struct inode *inode)
107{
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700108 if (inode->i_sb->s_flags & MS_ACTIVE) {
109 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700110 struct fuse_inode *fi = get_fuse_inode(inode);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700111 fuse_send_forget(fc, fi->forget_req, fi->nodeid, fi->nlookup);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700112 fi->forget_req = NULL;
113 }
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700114}
115
Miklos Szeredi71421252006-06-25 05:48:52 -0700116static int fuse_remount_fs(struct super_block *sb, int *flags, char *data)
117{
118 if (*flags & MS_MANDLOCK)
119 return -EINVAL;
120
121 return 0;
122}
123
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700124void fuse_truncate(struct address_space *mapping, loff_t offset)
Miklos Szeredie00d2c22007-10-16 23:31:01 -0700125{
126 /* See vmtruncate() */
127 unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
128 truncate_inode_pages(mapping, offset);
129 unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
130}
131
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700132void fuse_change_attributes_common(struct inode *inode, struct fuse_attr *attr,
133 u64 attr_valid)
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700134{
Miklos Szeredi9ffbb912006-10-17 00:10:06 -0700135 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szerediebc14c42007-10-16 23:31:03 -0700136 struct fuse_inode *fi = get_fuse_inode(inode);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700137
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700138 fi->attr_version = ++fc->attr_version;
139 fi->i_time = attr_valid;
140
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700141 inode->i_ino = attr->ino;
Miklos Szerediebc14c42007-10-16 23:31:03 -0700142 inode->i_mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700143 inode->i_nlink = attr->nlink;
144 inode->i_uid = attr->uid;
145 inode->i_gid = attr->gid;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700146 inode->i_blocks = attr->blocks;
147 inode->i_atime.tv_sec = attr->atime;
148 inode->i_atime.tv_nsec = attr->atimensec;
149 inode->i_mtime.tv_sec = attr->mtime;
150 inode->i_mtime.tv_nsec = attr->mtimensec;
151 inode->i_ctime.tv_sec = attr->ctime;
152 inode->i_ctime.tv_nsec = attr->ctimensec;
Miklos Szeredie00d2c22007-10-16 23:31:01 -0700153
Miklos Szeredi0e9663e2007-10-18 03:07:05 -0700154 if (attr->blksize != 0)
155 inode->i_blkbits = ilog2(attr->blksize);
156 else
157 inode->i_blkbits = inode->i_sb->s_blocksize_bits;
158
Miklos Szerediebc14c42007-10-16 23:31:03 -0700159 /*
160 * Don't set the sticky bit in i_mode, unless we want the VFS
161 * to check permissions. This prevents failures due to the
162 * check in may_delete().
163 */
164 fi->orig_i_mode = inode->i_mode;
165 if (!(fc->flags & FUSE_DEFAULT_PERMISSIONS))
166 inode->i_mode &= ~S_ISVTX;
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700167}
168
169void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr,
170 u64 attr_valid, u64 attr_version)
171{
172 struct fuse_conn *fc = get_fuse_conn(inode);
173 struct fuse_inode *fi = get_fuse_inode(inode);
174 loff_t oldsize;
175
176 spin_lock(&fc->lock);
177 if (attr_version != 0 && fi->attr_version > attr_version) {
178 spin_unlock(&fc->lock);
179 return;
180 }
181
182 fuse_change_attributes_common(inode, attr, attr_valid);
Miklos Szerediebc14c42007-10-16 23:31:03 -0700183
Miklos Szeredie00d2c22007-10-16 23:31:01 -0700184 oldsize = inode->i_size;
185 i_size_write(inode, attr->size);
186 spin_unlock(&fc->lock);
187
188 if (S_ISREG(inode->i_mode) && oldsize != attr->size) {
189 if (attr->size < oldsize)
190 fuse_truncate(inode->i_mapping, attr->size);
Miklos Szeredib1009972007-10-16 23:31:01 -0700191 invalidate_inode_pages2(inode->i_mapping);
Miklos Szeredie00d2c22007-10-16 23:31:01 -0700192 }
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700193}
194
195static void fuse_init_inode(struct inode *inode, struct fuse_attr *attr)
196{
197 inode->i_mode = attr->mode & S_IFMT;
Miklos Szeredi9ffbb912006-10-17 00:10:06 -0700198 inode->i_size = attr->size;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700199 if (S_ISREG(inode->i_mode)) {
200 fuse_init_common(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700201 fuse_init_file_inode(inode);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700202 } else if (S_ISDIR(inode->i_mode))
203 fuse_init_dir(inode);
204 else if (S_ISLNK(inode->i_mode))
205 fuse_init_symlink(inode);
206 else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
207 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
208 fuse_init_common(inode);
209 init_special_inode(inode, inode->i_mode,
210 new_decode_dev(attr->rdev));
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800211 } else
212 BUG();
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700213}
214
John Muir3b463ae2009-05-31 11:13:57 -0400215int fuse_inode_eq(struct inode *inode, void *_nodeidp)
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700216{
Miklos Szeredib48badf2008-04-30 00:54:44 -0700217 u64 nodeid = *(u64 *) _nodeidp;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700218 if (get_node_id(inode) == nodeid)
219 return 1;
220 else
221 return 0;
222}
223
224static int fuse_inode_set(struct inode *inode, void *_nodeidp)
225{
Miklos Szeredib48badf2008-04-30 00:54:44 -0700226 u64 nodeid = *(u64 *) _nodeidp;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700227 get_fuse_inode(inode)->nodeid = nodeid;
228 return 0;
229}
230
Miklos Szeredib48badf2008-04-30 00:54:44 -0700231struct inode *fuse_iget(struct super_block *sb, u64 nodeid,
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700232 int generation, struct fuse_attr *attr,
233 u64 attr_valid, u64 attr_version)
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700234{
235 struct inode *inode;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700236 struct fuse_inode *fi;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700237 struct fuse_conn *fc = get_fuse_conn_super(sb);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700238
239 retry:
240 inode = iget5_locked(sb, nodeid, fuse_inode_eq, fuse_inode_set, &nodeid);
241 if (!inode)
242 return NULL;
243
244 if ((inode->i_state & I_NEW)) {
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700245 inode->i_flags |= S_NOATIME|S_NOCMTIME;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700246 inode->i_generation = generation;
247 inode->i_data.backing_dev_info = &fc->bdi;
248 fuse_init_inode(inode, attr);
249 unlock_new_inode(inode);
250 } else if ((inode->i_mode ^ attr->mode) & S_IFMT) {
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700251 /* Inode has changed type, any I/O on the old should fail */
252 make_bad_inode(inode);
253 iput(inode);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700254 goto retry;
255 }
256
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700257 fi = get_fuse_inode(inode);
Miklos Szeredi8da5ff22006-10-17 00:10:08 -0700258 spin_lock(&fc->lock);
Miklos Szeredi1729a162008-11-26 12:03:54 +0100259 fi->nlookup++;
Miklos Szeredi8da5ff22006-10-17 00:10:08 -0700260 spin_unlock(&fc->lock);
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700261 fuse_change_attributes(inode, attr, attr_valid, attr_version);
262
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700263 return inode;
264}
265
John Muir3b463ae2009-05-31 11:13:57 -0400266int fuse_reverse_inval_inode(struct super_block *sb, u64 nodeid,
267 loff_t offset, loff_t len)
268{
269 struct inode *inode;
270 pgoff_t pg_start;
271 pgoff_t pg_end;
272
273 inode = ilookup5(sb, nodeid, fuse_inode_eq, &nodeid);
274 if (!inode)
275 return -ENOENT;
276
277 fuse_invalidate_attr(inode);
278 if (offset >= 0) {
279 pg_start = offset >> PAGE_CACHE_SHIFT;
280 if (len <= 0)
281 pg_end = -1;
282 else
283 pg_end = (offset + len - 1) >> PAGE_CACHE_SHIFT;
284 invalidate_inode_pages2_range(inode->i_mapping,
285 pg_start, pg_end);
286 }
287 iput(inode);
288 return 0;
289}
290
Al Viro42faad92008-04-24 07:21:56 -0400291static void fuse_umount_begin(struct super_block *sb)
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800292{
Al Viro42faad92008-04-24 07:21:56 -0400293 fuse_abort_conn(get_fuse_conn_super(sb));
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800294}
295
Miklos Szeredi0ec7ca42006-12-06 20:35:52 -0800296static void fuse_send_destroy(struct fuse_conn *fc)
297{
298 struct fuse_req *req = fc->destroy_req;
299 if (req && fc->conn_init) {
300 fc->destroy_req = NULL;
301 req->in.h.opcode = FUSE_DESTROY;
302 req->force = 1;
Tejun Heob93f8582008-11-26 12:03:55 +0100303 fuse_request_send(fc, req);
Miklos Szeredi0ec7ca42006-12-06 20:35:52 -0800304 fuse_put_request(fc, req);
305 }
306}
307
Tejun Heoa325f9b2009-04-14 10:54:52 +0900308static void fuse_bdi_destroy(struct fuse_conn *fc)
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700309{
Tejun Heoa325f9b2009-04-14 10:54:52 +0900310 if (fc->bdi_initialized)
311 bdi_destroy(&fc->bdi);
312}
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700313
Tejun Heo08cbf542009-04-14 10:54:53 +0900314void fuse_conn_kill(struct fuse_conn *fc)
Tejun Heoa325f9b2009-04-14 10:54:52 +0900315{
Miklos Szeredid7133112006-04-10 22:54:55 -0700316 spin_lock(&fc->lock);
Miklos Szeredi9ba7cbb2006-01-16 22:14:34 -0800317 fc->connected = 0;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700318 fc->blocked = 0;
Miklos Szeredid7133112006-04-10 22:54:55 -0700319 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700320 /* Flush all readers on this fs */
Jeff Dike385a17b2006-04-10 22:54:52 -0700321 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700322 wake_up_all(&fc->waitq);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700323 wake_up_all(&fc->blocked_waitq);
Miklos Szeredide5e3de2007-10-16 23:31:00 -0700324 wake_up_all(&fc->reserved_req_waitq);
Miklos Szeredibafa9652006-06-25 05:48:51 -0700325 mutex_lock(&fuse_mutex);
326 list_del(&fc->entry);
327 fuse_ctl_remove_conn(fc);
328 mutex_unlock(&fuse_mutex);
Tejun Heoa325f9b2009-04-14 10:54:52 +0900329 fuse_bdi_destroy(fc);
330}
Tejun Heo08cbf542009-04-14 10:54:53 +0900331EXPORT_SYMBOL_GPL(fuse_conn_kill);
Tejun Heoa325f9b2009-04-14 10:54:52 +0900332
333static void fuse_put_super(struct super_block *sb)
334{
335 struct fuse_conn *fc = get_fuse_conn_super(sb);
336
337 fuse_send_destroy(fc);
338 fuse_conn_kill(fc);
Miklos Szeredibafa9652006-06-25 05:48:51 -0700339 fuse_conn_put(fc);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700340}
341
Miklos Szeredie5e55582005-09-09 13:10:28 -0700342static void convert_fuse_statfs(struct kstatfs *stbuf, struct fuse_kstatfs *attr)
343{
344 stbuf->f_type = FUSE_SUPER_MAGIC;
345 stbuf->f_bsize = attr->bsize;
Miklos Szeredide5f1202006-01-06 00:19:37 -0800346 stbuf->f_frsize = attr->frsize;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700347 stbuf->f_blocks = attr->blocks;
348 stbuf->f_bfree = attr->bfree;
349 stbuf->f_bavail = attr->bavail;
350 stbuf->f_files = attr->files;
351 stbuf->f_ffree = attr->ffree;
352 stbuf->f_namelen = attr->namelen;
353 /* fsid is left zero */
354}
355
David Howells726c3342006-06-23 02:02:58 -0700356static int fuse_statfs(struct dentry *dentry, struct kstatfs *buf)
Miklos Szeredie5e55582005-09-09 13:10:28 -0700357{
David Howells726c3342006-06-23 02:02:58 -0700358 struct super_block *sb = dentry->d_sb;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700359 struct fuse_conn *fc = get_fuse_conn_super(sb);
360 struct fuse_req *req;
361 struct fuse_statfs_out outarg;
362 int err;
363
Miklos Szeredie57ac682007-10-18 03:06:58 -0700364 if (!fuse_allow_task(fc, current)) {
365 buf->f_type = FUSE_SUPER_MAGIC;
366 return 0;
367 }
368
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700369 req = fuse_get_req(fc);
370 if (IS_ERR(req))
371 return PTR_ERR(req);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700372
Miklos Szeredide5f1202006-01-06 00:19:37 -0800373 memset(&outarg, 0, sizeof(outarg));
Miklos Szeredie5e55582005-09-09 13:10:28 -0700374 req->in.numargs = 0;
375 req->in.h.opcode = FUSE_STATFS;
Miklos Szeredi5b35e8e2006-09-29 01:59:34 -0700376 req->in.h.nodeid = get_node_id(dentry->d_inode);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700377 req->out.numargs = 1;
Miklos Szeredide5f1202006-01-06 00:19:37 -0800378 req->out.args[0].size =
379 fc->minor < 4 ? FUSE_COMPAT_STATFS_SIZE : sizeof(outarg);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700380 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +0100381 fuse_request_send(fc, req);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700382 err = req->out.h.error;
383 if (!err)
384 convert_fuse_statfs(buf, &outarg.st);
385 fuse_put_request(fc, req);
386 return err;
387}
388
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700389enum {
390 OPT_FD,
391 OPT_ROOTMODE,
392 OPT_USER_ID,
Miklos Szeredi87729a52005-09-09 13:10:34 -0700393 OPT_GROUP_ID,
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700394 OPT_DEFAULT_PERMISSIONS,
395 OPT_ALLOW_OTHER,
Miklos Szeredidb50b962005-09-09 13:10:33 -0700396 OPT_MAX_READ,
Miklos Szeredid8091612006-12-06 20:35:48 -0800397 OPT_BLKSIZE,
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700398 OPT_ERR
399};
400
Steven Whitehousea447c092008-10-13 10:46:57 +0100401static const match_table_t tokens = {
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700402 {OPT_FD, "fd=%u"},
403 {OPT_ROOTMODE, "rootmode=%o"},
404 {OPT_USER_ID, "user_id=%u"},
Miklos Szeredi87729a52005-09-09 13:10:34 -0700405 {OPT_GROUP_ID, "group_id=%u"},
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700406 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
407 {OPT_ALLOW_OTHER, "allow_other"},
Miklos Szeredidb50b962005-09-09 13:10:33 -0700408 {OPT_MAX_READ, "max_read=%u"},
Miklos Szeredid8091612006-12-06 20:35:48 -0800409 {OPT_BLKSIZE, "blksize=%u"},
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700410 {OPT_ERR, NULL}
411};
412
Miklos Szeredid8091612006-12-06 20:35:48 -0800413static int parse_fuse_opt(char *opt, struct fuse_mount_data *d, int is_bdev)
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700414{
415 char *p;
416 memset(d, 0, sizeof(struct fuse_mount_data));
Miklos Szeredidb50b962005-09-09 13:10:33 -0700417 d->max_read = ~0;
Miklos Szeredid1875db2008-02-08 04:21:43 -0800418 d->blksize = FUSE_DEFAULT_BLKSIZE;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700419
420 while ((p = strsep(&opt, ",")) != NULL) {
421 int token;
422 int value;
423 substring_t args[MAX_OPT_ARGS];
424 if (!*p)
425 continue;
426
427 token = match_token(p, tokens, args);
428 switch (token) {
429 case OPT_FD:
430 if (match_int(&args[0], &value))
431 return 0;
432 d->fd = value;
Miklos Szeredi5a533682005-09-09 13:10:34 -0700433 d->fd_present = 1;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700434 break;
435
436 case OPT_ROOTMODE:
437 if (match_octal(&args[0], &value))
438 return 0;
Timo Savolaa5bfffac2007-04-08 16:04:00 -0700439 if (!fuse_valid_type(value))
440 return 0;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700441 d->rootmode = value;
Miklos Szeredi5a533682005-09-09 13:10:34 -0700442 d->rootmode_present = 1;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700443 break;
444
445 case OPT_USER_ID:
446 if (match_int(&args[0], &value))
447 return 0;
448 d->user_id = value;
Miklos Szeredi5a533682005-09-09 13:10:34 -0700449 d->user_id_present = 1;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700450 break;
451
Miklos Szeredi87729a52005-09-09 13:10:34 -0700452 case OPT_GROUP_ID:
453 if (match_int(&args[0], &value))
454 return 0;
455 d->group_id = value;
Miklos Szeredi5a533682005-09-09 13:10:34 -0700456 d->group_id_present = 1;
Miklos Szeredi87729a52005-09-09 13:10:34 -0700457 break;
458
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700459 case OPT_DEFAULT_PERMISSIONS:
460 d->flags |= FUSE_DEFAULT_PERMISSIONS;
461 break;
462
463 case OPT_ALLOW_OTHER:
464 d->flags |= FUSE_ALLOW_OTHER;
465 break;
466
Miklos Szeredidb50b962005-09-09 13:10:33 -0700467 case OPT_MAX_READ:
468 if (match_int(&args[0], &value))
469 return 0;
470 d->max_read = value;
471 break;
472
Miklos Szeredid8091612006-12-06 20:35:48 -0800473 case OPT_BLKSIZE:
474 if (!is_bdev || match_int(&args[0], &value))
475 return 0;
476 d->blksize = value;
477 break;
478
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700479 default:
480 return 0;
481 }
482 }
Miklos Szeredi5a533682005-09-09 13:10:34 -0700483
484 if (!d->fd_present || !d->rootmode_present ||
485 !d->user_id_present || !d->group_id_present)
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700486 return 0;
487
488 return 1;
489}
490
491static int fuse_show_options(struct seq_file *m, struct vfsmount *mnt)
492{
493 struct fuse_conn *fc = get_fuse_conn_super(mnt->mnt_sb);
494
495 seq_printf(m, ",user_id=%u", fc->user_id);
Miklos Szeredi87729a52005-09-09 13:10:34 -0700496 seq_printf(m, ",group_id=%u", fc->group_id);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700497 if (fc->flags & FUSE_DEFAULT_PERMISSIONS)
498 seq_puts(m, ",default_permissions");
499 if (fc->flags & FUSE_ALLOW_OTHER)
500 seq_puts(m, ",allow_other");
Miklos Szeredidb50b962005-09-09 13:10:33 -0700501 if (fc->max_read != ~0)
502 seq_printf(m, ",max_read=%u", fc->max_read);
Miklos Szeredid1875db2008-02-08 04:21:43 -0800503 if (mnt->mnt_sb->s_bdev &&
504 mnt->mnt_sb->s_blocksize != FUSE_DEFAULT_BLKSIZE)
505 seq_printf(m, ",blksize=%lu", mnt->mnt_sb->s_blocksize);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700506 return 0;
507}
508
Tejun Heoa325f9b2009-04-14 10:54:52 +0900509void fuse_conn_init(struct fuse_conn *fc)
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700510{
Tejun Heo0d179aa2008-11-26 12:03:55 +0100511 memset(fc, 0, sizeof(*fc));
512 spin_lock_init(&fc->lock);
513 mutex_init(&fc->inst_mutex);
John Muir3b463ae2009-05-31 11:13:57 -0400514 init_rwsem(&fc->killsb);
Tejun Heo0d179aa2008-11-26 12:03:55 +0100515 atomic_set(&fc->count, 1);
516 init_waitqueue_head(&fc->waitq);
517 init_waitqueue_head(&fc->blocked_waitq);
518 init_waitqueue_head(&fc->reserved_req_waitq);
519 INIT_LIST_HEAD(&fc->pending);
520 INIT_LIST_HEAD(&fc->processing);
521 INIT_LIST_HEAD(&fc->io);
522 INIT_LIST_HEAD(&fc->interrupts);
523 INIT_LIST_HEAD(&fc->bg_queue);
524 INIT_LIST_HEAD(&fc->entry);
525 atomic_set(&fc->num_waiting, 0);
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700526 fc->max_background = FUSE_DEFAULT_MAX_BACKGROUND;
527 fc->congestion_threshold = FUSE_DEFAULT_CONGESTION_THRESHOLD;
Tejun Heo0d179aa2008-11-26 12:03:55 +0100528 fc->khctr = 0;
529 fc->polled_files = RB_ROOT;
Tejun Heo0d179aa2008-11-26 12:03:55 +0100530 fc->reqctr = 0;
531 fc->blocked = 1;
532 fc->attr_version = 1;
533 get_random_bytes(&fc->scramble_key, sizeof(fc->scramble_key));
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700534}
Tejun Heo0d179aa2008-11-26 12:03:55 +0100535EXPORT_SYMBOL_GPL(fuse_conn_init);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700536
Miklos Szeredibafa9652006-06-25 05:48:51 -0700537void fuse_conn_put(struct fuse_conn *fc)
538{
Miklos Szeredid2a85162006-10-17 00:10:11 -0700539 if (atomic_dec_and_test(&fc->count)) {
Miklos Szeredi0ec7ca42006-12-06 20:35:52 -0800540 if (fc->destroy_req)
541 fuse_request_free(fc->destroy_req);
Miklos Szeredid2a85162006-10-17 00:10:11 -0700542 mutex_destroy(&fc->inst_mutex);
Tejun Heo43901aa2008-11-26 12:03:56 +0100543 fc->release(fc);
Miklos Szeredid2a85162006-10-17 00:10:11 -0700544 }
Miklos Szeredibafa9652006-06-25 05:48:51 -0700545}
Tejun Heo08cbf542009-04-14 10:54:53 +0900546EXPORT_SYMBOL_GPL(fuse_conn_put);
Miklos Szeredibafa9652006-06-25 05:48:51 -0700547
548struct fuse_conn *fuse_conn_get(struct fuse_conn *fc)
549{
550 atomic_inc(&fc->count);
551 return fc;
552}
Tejun Heo08cbf542009-04-14 10:54:53 +0900553EXPORT_SYMBOL_GPL(fuse_conn_get);
Miklos Szeredibafa9652006-06-25 05:48:51 -0700554
Tejun Heob93f8582008-11-26 12:03:55 +0100555static struct inode *fuse_get_root_inode(struct super_block *sb, unsigned mode)
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700556{
557 struct fuse_attr attr;
558 memset(&attr, 0, sizeof(attr));
559
560 attr.mode = mode;
561 attr.ino = FUSE_ROOT_ID;
Miklos Szeredi074406f2007-10-16 23:31:02 -0700562 attr.nlink = 1;
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700563 return fuse_iget(sb, 1, 0, &attr, 0, 0);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700564}
565
Miklos Szeredi1729a162008-11-26 12:03:54 +0100566struct fuse_inode_handle {
Miklos Szeredidbd561d2008-07-25 01:49:00 -0700567 u64 nodeid;
568 u32 generation;
569};
570
571static struct dentry *fuse_get_dentry(struct super_block *sb,
572 struct fuse_inode_handle *handle)
573{
Miklos Szeredi33670fa2008-07-25 01:49:02 -0700574 struct fuse_conn *fc = get_fuse_conn_super(sb);
Miklos Szeredidbd561d2008-07-25 01:49:00 -0700575 struct inode *inode;
576 struct dentry *entry;
577 int err = -ESTALE;
578
579 if (handle->nodeid == 0)
580 goto out_err;
581
582 inode = ilookup5(sb, handle->nodeid, fuse_inode_eq, &handle->nodeid);
Miklos Szeredi33670fa2008-07-25 01:49:02 -0700583 if (!inode) {
584 struct fuse_entry_out outarg;
585 struct qstr name;
586
587 if (!fc->export_support)
588 goto out_err;
589
590 name.len = 1;
591 name.name = ".";
592 err = fuse_lookup_name(sb, handle->nodeid, &name, &outarg,
593 &inode);
594 if (err && err != -ENOENT)
595 goto out_err;
596 if (err || !inode) {
597 err = -ESTALE;
598 goto out_err;
599 }
600 err = -EIO;
601 if (get_node_id(inode) != handle->nodeid)
602 goto out_iput;
603 }
Miklos Szeredidbd561d2008-07-25 01:49:00 -0700604 err = -ESTALE;
605 if (inode->i_generation != handle->generation)
606 goto out_iput;
607
Christoph Hellwig44003722008-08-11 15:49:04 +0200608 entry = d_obtain_alias(inode);
609 if (!IS_ERR(entry) && get_node_id(inode) != FUSE_ROOT_ID) {
Miklos Szeredidbd561d2008-07-25 01:49:00 -0700610 entry->d_op = &fuse_dentry_operations;
611 fuse_invalidate_entry_cache(entry);
612 }
613
614 return entry;
615
616 out_iput:
617 iput(inode);
618 out_err:
619 return ERR_PTR(err);
620}
621
622static int fuse_encode_fh(struct dentry *dentry, u32 *fh, int *max_len,
623 int connectable)
624{
625 struct inode *inode = dentry->d_inode;
626 bool encode_parent = connectable && !S_ISDIR(inode->i_mode);
627 int len = encode_parent ? 6 : 3;
628 u64 nodeid;
629 u32 generation;
630
631 if (*max_len < len)
632 return 255;
633
634 nodeid = get_fuse_inode(inode)->nodeid;
635 generation = inode->i_generation;
636
637 fh[0] = (u32)(nodeid >> 32);
638 fh[1] = (u32)(nodeid & 0xffffffff);
639 fh[2] = generation;
640
641 if (encode_parent) {
642 struct inode *parent;
643
644 spin_lock(&dentry->d_lock);
645 parent = dentry->d_parent->d_inode;
646 nodeid = get_fuse_inode(parent)->nodeid;
647 generation = parent->i_generation;
648 spin_unlock(&dentry->d_lock);
649
650 fh[3] = (u32)(nodeid >> 32);
651 fh[4] = (u32)(nodeid & 0xffffffff);
652 fh[5] = generation;
653 }
654
655 *max_len = len;
656 return encode_parent ? 0x82 : 0x81;
657}
658
659static struct dentry *fuse_fh_to_dentry(struct super_block *sb,
660 struct fid *fid, int fh_len, int fh_type)
661{
662 struct fuse_inode_handle handle;
663
664 if ((fh_type != 0x81 && fh_type != 0x82) || fh_len < 3)
665 return NULL;
666
667 handle.nodeid = (u64) fid->raw[0] << 32;
668 handle.nodeid |= (u64) fid->raw[1];
669 handle.generation = fid->raw[2];
670 return fuse_get_dentry(sb, &handle);
671}
672
673static struct dentry *fuse_fh_to_parent(struct super_block *sb,
674 struct fid *fid, int fh_len, int fh_type)
675{
676 struct fuse_inode_handle parent;
677
678 if (fh_type != 0x82 || fh_len < 6)
679 return NULL;
680
681 parent.nodeid = (u64) fid->raw[3] << 32;
682 parent.nodeid |= (u64) fid->raw[4];
683 parent.generation = fid->raw[5];
684 return fuse_get_dentry(sb, &parent);
685}
686
Miklos Szeredi33670fa2008-07-25 01:49:02 -0700687static struct dentry *fuse_get_parent(struct dentry *child)
688{
689 struct inode *child_inode = child->d_inode;
690 struct fuse_conn *fc = get_fuse_conn(child_inode);
691 struct inode *inode;
692 struct dentry *parent;
693 struct fuse_entry_out outarg;
694 struct qstr name;
695 int err;
696
697 if (!fc->export_support)
698 return ERR_PTR(-ESTALE);
699
700 name.len = 2;
701 name.name = "..";
702 err = fuse_lookup_name(child_inode->i_sb, get_node_id(child_inode),
703 &name, &outarg, &inode);
Christoph Hellwig44003722008-08-11 15:49:04 +0200704 if (err) {
705 if (err == -ENOENT)
706 return ERR_PTR(-ESTALE);
Miklos Szeredi33670fa2008-07-25 01:49:02 -0700707 return ERR_PTR(err);
Miklos Szeredi33670fa2008-07-25 01:49:02 -0700708 }
Christoph Hellwig44003722008-08-11 15:49:04 +0200709
710 parent = d_obtain_alias(inode);
711 if (!IS_ERR(parent) && get_node_id(inode) != FUSE_ROOT_ID) {
Miklos Szeredi33670fa2008-07-25 01:49:02 -0700712 parent->d_op = &fuse_dentry_operations;
713 fuse_invalidate_entry_cache(parent);
714 }
715
716 return parent;
717}
Miklos Szeredidbd561d2008-07-25 01:49:00 -0700718
719static const struct export_operations fuse_export_operations = {
720 .fh_to_dentry = fuse_fh_to_dentry,
721 .fh_to_parent = fuse_fh_to_parent,
722 .encode_fh = fuse_encode_fh,
Miklos Szeredi33670fa2008-07-25 01:49:02 -0700723 .get_parent = fuse_get_parent,
Miklos Szeredidbd561d2008-07-25 01:49:00 -0700724};
725
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800726static const struct super_operations fuse_super_operations = {
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700727 .alloc_inode = fuse_alloc_inode,
728 .destroy_inode = fuse_destroy_inode,
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700729 .clear_inode = fuse_clear_inode,
Miklos Szerediead5f0b2007-05-23 13:57:54 -0700730 .drop_inode = generic_delete_inode,
Miklos Szeredi71421252006-06-25 05:48:52 -0700731 .remount_fs = fuse_remount_fs,
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700732 .put_super = fuse_put_super,
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800733 .umount_begin = fuse_umount_begin,
Miklos Szeredie5e55582005-09-09 13:10:28 -0700734 .statfs = fuse_statfs,
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700735 .show_options = fuse_show_options,
736};
737
Miklos Szeredi9b9a0462006-01-16 22:14:44 -0800738static void process_init_reply(struct fuse_conn *fc, struct fuse_req *req)
739{
Miklos Szeredi9b9a0462006-01-16 22:14:44 -0800740 struct fuse_init_out *arg = &req->misc.init_out;
741
742 if (req->out.h.error || arg->major != FUSE_KERNEL_VERSION)
743 fc->conn_error = 1;
744 else {
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800745 unsigned long ra_pages;
746
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700747 if (arg->minor >= 13) {
748 if (arg->max_background)
749 fc->max_background = arg->max_background;
750 if (arg->congestion_threshold)
751 fc->congestion_threshold = arg->congestion_threshold;
752 }
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800753 if (arg->minor >= 6) {
754 ra_pages = arg->max_readahead / PAGE_CACHE_SIZE;
755 if (arg->flags & FUSE_ASYNC_READ)
756 fc->async_read = 1;
Miklos Szeredi71421252006-06-25 05:48:52 -0700757 if (!(arg->flags & FUSE_POSIX_LOCKS))
758 fc->no_lock = 1;
Miklos Szeredi6ff958e2007-10-18 03:07:02 -0700759 if (arg->flags & FUSE_ATOMIC_O_TRUNC)
760 fc->atomic_o_trunc = 1;
Miklos Szeredi33670fa2008-07-25 01:49:02 -0700761 if (arg->minor >= 9) {
762 /* LOOKUP has dependency on proto version */
763 if (arg->flags & FUSE_EXPORT_SUPPORT)
764 fc->export_support = 1;
765 }
Miklos Szeredi78bb6cb2008-05-12 14:02:32 -0700766 if (arg->flags & FUSE_BIG_WRITES)
767 fc->big_writes = 1;
Miklos Szeredie0a43dd2009-06-30 20:12:23 +0200768 if (arg->flags & FUSE_DONT_MASK)
769 fc->dont_mask = 1;
Miklos Szeredi71421252006-06-25 05:48:52 -0700770 } else {
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800771 ra_pages = fc->max_read / PAGE_CACHE_SIZE;
Miklos Szeredi71421252006-06-25 05:48:52 -0700772 fc->no_lock = 1;
773 }
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800774
775 fc->bdi.ra_pages = min(fc->bdi.ra_pages, ra_pages);
Miklos Szeredi9b9a0462006-01-16 22:14:44 -0800776 fc->minor = arg->minor;
777 fc->max_write = arg->minor < 5 ? 4096 : arg->max_write;
Miklos Szeredif948d562008-06-17 18:05:40 +0200778 fc->max_write = max_t(unsigned, 4096, fc->max_write);
Miklos Szeredi0ec7ca42006-12-06 20:35:52 -0800779 fc->conn_init = 1;
Miklos Szeredi9b9a0462006-01-16 22:14:44 -0800780 }
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700781 fc->blocked = 0;
782 wake_up_all(&fc->blocked_waitq);
Miklos Szeredi9b9a0462006-01-16 22:14:44 -0800783}
784
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700785static void fuse_send_init(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi9b9a0462006-01-16 22:14:44 -0800786{
Miklos Szeredi9b9a0462006-01-16 22:14:44 -0800787 struct fuse_init_in *arg = &req->misc.init_in;
Miklos Szeredi095da6c2006-01-16 22:14:52 -0800788
Miklos Szeredi9b9a0462006-01-16 22:14:44 -0800789 arg->major = FUSE_KERNEL_VERSION;
790 arg->minor = FUSE_KERNEL_MINOR_VERSION;
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800791 arg->max_readahead = fc->bdi.ra_pages * PAGE_CACHE_SIZE;
Miklos Szeredi78bb6cb2008-05-12 14:02:32 -0700792 arg->flags |= FUSE_ASYNC_READ | FUSE_POSIX_LOCKS | FUSE_ATOMIC_O_TRUNC |
Miklos Szeredie0a43dd2009-06-30 20:12:23 +0200793 FUSE_EXPORT_SUPPORT | FUSE_BIG_WRITES | FUSE_DONT_MASK;
Miklos Szeredi9b9a0462006-01-16 22:14:44 -0800794 req->in.h.opcode = FUSE_INIT;
795 req->in.numargs = 1;
796 req->in.args[0].size = sizeof(*arg);
797 req->in.args[0].value = arg;
798 req->out.numargs = 1;
799 /* Variable length arguement used for backward compatibility
800 with interface version < 7.5. Rest of init_out is zeroed
801 by do_get_request(), so a short reply is not a problem */
802 req->out.argvar = 1;
803 req->out.args[0].size = sizeof(struct fuse_init_out);
804 req->out.args[0].value = &req->misc.init_out;
805 req->end = process_init_reply;
Tejun Heob93f8582008-11-26 12:03:55 +0100806 fuse_request_send_background(fc, req);
Miklos Szeredi9b9a0462006-01-16 22:14:44 -0800807}
808
Tejun Heo43901aa2008-11-26 12:03:56 +0100809static void fuse_free_conn(struct fuse_conn *fc)
810{
811 kfree(fc);
812}
813
Tejun Heoa325f9b2009-04-14 10:54:52 +0900814static int fuse_bdi_init(struct fuse_conn *fc, struct super_block *sb)
815{
816 int err;
817
818 fc->bdi.ra_pages = (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
819 fc->bdi.unplug_io_fn = default_unplug_io_fn;
820 /* fuse does it's own writeback accounting */
821 fc->bdi.capabilities = BDI_CAP_NO_ACCT_WB;
822
823 err = bdi_init(&fc->bdi);
824 if (err)
825 return err;
826
827 fc->bdi_initialized = 1;
828
829 if (sb->s_bdev) {
830 err = bdi_register(&fc->bdi, NULL, "%u:%u-fuseblk",
831 MAJOR(fc->dev), MINOR(fc->dev));
832 } else {
833 err = bdi_register_dev(&fc->bdi, fc->dev);
834 }
835
836 if (err)
837 return err;
838
839 /*
840 * For a single fuse filesystem use max 1% of dirty +
841 * writeback threshold.
842 *
843 * This gives about 1M of write buffer for memory maps on a
844 * machine with 1G and 10% dirty_ratio, which should be more
845 * than enough.
846 *
847 * Privileged users can raise it by writing to
848 *
849 * /sys/class/bdi/<bdi>/max_ratio
850 */
851 bdi_set_max_ratio(&fc->bdi, 1);
852
853 return 0;
854}
855
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700856static int fuse_fill_super(struct super_block *sb, void *data, int silent)
857{
858 struct fuse_conn *fc;
859 struct inode *root;
860 struct fuse_mount_data d;
861 struct file *file;
Miklos Szeredif543f252006-01-16 22:14:35 -0800862 struct dentry *root_dentry;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700863 struct fuse_req *init_req;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700864 int err;
Miklos Szeredid8091612006-12-06 20:35:48 -0800865 int is_bdev = sb->s_bdev != NULL;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700866
Miklos Szeredic2b8f002009-01-26 15:00:58 +0100867 err = -EINVAL;
Miklos Szeredi71421252006-06-25 05:48:52 -0700868 if (sb->s_flags & MS_MANDLOCK)
Miklos Szeredic2b8f002009-01-26 15:00:58 +0100869 goto err;
Miklos Szeredi71421252006-06-25 05:48:52 -0700870
Miklos Szeredid8091612006-12-06 20:35:48 -0800871 if (!parse_fuse_opt((char *) data, &d, is_bdev))
Miklos Szeredic2b8f002009-01-26 15:00:58 +0100872 goto err;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700873
Miklos Szeredid8091612006-12-06 20:35:48 -0800874 if (is_bdev) {
Miklos Szeredi875d95e2006-12-06 20:35:54 -0800875#ifdef CONFIG_BLOCK
Miklos Szeredic2b8f002009-01-26 15:00:58 +0100876 err = -EINVAL;
Miklos Szeredid8091612006-12-06 20:35:48 -0800877 if (!sb_set_blocksize(sb, d.blksize))
Miklos Szeredic2b8f002009-01-26 15:00:58 +0100878 goto err;
Miklos Szeredi875d95e2006-12-06 20:35:54 -0800879#endif
Miklos Szeredid8091612006-12-06 20:35:48 -0800880 } else {
881 sb->s_blocksize = PAGE_CACHE_SIZE;
882 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
883 }
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700884 sb->s_magic = FUSE_SUPER_MAGIC;
885 sb->s_op = &fuse_super_operations;
886 sb->s_maxbytes = MAX_LFS_FILESIZE;
Miklos Szeredidbd561d2008-07-25 01:49:00 -0700887 sb->s_export_op = &fuse_export_operations;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700888
889 file = fget(d.fd);
Miklos Szeredic2b8f002009-01-26 15:00:58 +0100890 err = -EINVAL;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700891 if (!file)
Miklos Szeredic2b8f002009-01-26 15:00:58 +0100892 goto err;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700893
Miklos Szeredic2b8f002009-01-26 15:00:58 +0100894 if (file->f_op != &fuse_dev_operations)
895 goto err_fput;
Miklos Szeredi0720b312006-04-10 22:54:55 -0700896
Tejun Heo0d179aa2008-11-26 12:03:55 +0100897 fc = kmalloc(sizeof(*fc), GFP_KERNEL);
Miklos Szeredic2b8f002009-01-26 15:00:58 +0100898 err = -ENOMEM;
899 if (!fc)
900 goto err_fput;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700901
Tejun Heoa325f9b2009-04-14 10:54:52 +0900902 fuse_conn_init(fc);
903
904 fc->dev = sb->s_dev;
John Muir3b463ae2009-05-31 11:13:57 -0400905 fc->sb = sb;
Tejun Heoa325f9b2009-04-14 10:54:52 +0900906 err = fuse_bdi_init(fc, sb);
907 if (err)
908 goto err_put_conn;
Tejun Heo0d179aa2008-11-26 12:03:55 +0100909
Miklos Szeredie0a43dd2009-06-30 20:12:23 +0200910 /* Handle umasking inside the fuse code */
911 if (sb->s_flags & MS_POSIXACL)
912 fc->dont_mask = 1;
913 sb->s_flags |= MS_POSIXACL;
914
Tejun Heo43901aa2008-11-26 12:03:56 +0100915 fc->release = fuse_free_conn;
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700916 fc->flags = d.flags;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700917 fc->user_id = d.user_id;
Miklos Szeredi87729a52005-09-09 13:10:34 -0700918 fc->group_id = d.group_id;
Miklos Szeredif948d562008-06-17 18:05:40 +0200919 fc->max_read = max_t(unsigned, 4096, d.max_read);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700920
Miklos Szeredif543f252006-01-16 22:14:35 -0800921 /* Used by get_root_inode() */
922 sb->s_fs_info = fc;
923
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700924 err = -ENOMEM;
Tejun Heob93f8582008-11-26 12:03:55 +0100925 root = fuse_get_root_inode(sb, d.rootmode);
Miklos Szeredif543f252006-01-16 22:14:35 -0800926 if (!root)
Miklos Szeredic2b8f002009-01-26 15:00:58 +0100927 goto err_put_conn;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700928
Miklos Szeredif543f252006-01-16 22:14:35 -0800929 root_dentry = d_alloc_root(root);
930 if (!root_dentry) {
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700931 iput(root);
Miklos Szeredic2b8f002009-01-26 15:00:58 +0100932 goto err_put_conn;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700933 }
Miklos Szeredif543f252006-01-16 22:14:35 -0800934
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700935 init_req = fuse_request_alloc();
936 if (!init_req)
937 goto err_put_root;
938
Miklos Szeredi0ec7ca42006-12-06 20:35:52 -0800939 if (is_bdev) {
940 fc->destroy_req = fuse_request_alloc();
941 if (!fc->destroy_req)
Julia Lawall17e18ab2008-10-16 16:08:56 +0200942 goto err_free_init_req;
Miklos Szeredi0ec7ca42006-12-06 20:35:52 -0800943 }
944
Miklos Szeredibafa9652006-06-25 05:48:51 -0700945 mutex_lock(&fuse_mutex);
Miklos Szeredi8aa09a52006-04-26 10:49:16 +0200946 err = -EINVAL;
947 if (file->private_data)
Miklos Szeredibafa9652006-06-25 05:48:51 -0700948 goto err_unlock;
Miklos Szeredi8aa09a52006-04-26 10:49:16 +0200949
Miklos Szeredibafa9652006-06-25 05:48:51 -0700950 err = fuse_ctl_add_conn(fc);
951 if (err)
952 goto err_unlock;
953
954 list_add_tail(&fc->entry, &fuse_conn_list);
Miklos Szeredif543f252006-01-16 22:14:35 -0800955 sb->s_root = root_dentry;
Miklos Szeredif543f252006-01-16 22:14:35 -0800956 fc->connected = 1;
Miklos Szeredibafa9652006-06-25 05:48:51 -0700957 file->private_data = fuse_conn_get(fc);
958 mutex_unlock(&fuse_mutex);
Miklos Szeredi0720b312006-04-10 22:54:55 -0700959 /*
960 * atomic_dec_and_test() in fput() provides the necessary
961 * memory barrier for file->private_data to be visible on all
962 * CPUs after this
963 */
964 fput(file);
Miklos Szeredif543f252006-01-16 22:14:35 -0800965
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700966 fuse_send_init(fc, init_req);
Miklos Szeredif543f252006-01-16 22:14:35 -0800967
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700968 return 0;
969
Miklos Szeredibafa9652006-06-25 05:48:51 -0700970 err_unlock:
971 mutex_unlock(&fuse_mutex);
Julia Lawall17e18ab2008-10-16 16:08:56 +0200972 err_free_init_req:
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700973 fuse_request_free(init_req);
Miklos Szeredif543f252006-01-16 22:14:35 -0800974 err_put_root:
975 dput(root_dentry);
Miklos Szeredic2b8f002009-01-26 15:00:58 +0100976 err_put_conn:
Tejun Heoa325f9b2009-04-14 10:54:52 +0900977 fuse_bdi_destroy(fc);
Miklos Szeredibafa9652006-06-25 05:48:51 -0700978 fuse_conn_put(fc);
Miklos Szeredic2b8f002009-01-26 15:00:58 +0100979 err_fput:
980 fput(file);
981 err:
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700982 return err;
983}
984
David Howells454e2392006-06-23 02:02:57 -0700985static int fuse_get_sb(struct file_system_type *fs_type,
986 int flags, const char *dev_name,
987 void *raw_data, struct vfsmount *mnt)
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700988{
David Howells454e2392006-06-23 02:02:57 -0700989 return get_sb_nodev(fs_type, flags, raw_data, fuse_fill_super, mnt);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700990}
991
John Muir3b463ae2009-05-31 11:13:57 -0400992static void fuse_kill_sb_anon(struct super_block *sb)
993{
994 struct fuse_conn *fc = get_fuse_conn_super(sb);
995
996 if (fc) {
997 down_write(&fc->killsb);
998 fc->sb = NULL;
999 up_write(&fc->killsb);
1000 }
1001
1002 kill_anon_super(sb);
1003}
1004
Miklos Szeredi875d95e2006-12-06 20:35:54 -08001005static struct file_system_type fuse_fs_type = {
1006 .owner = THIS_MODULE,
1007 .name = "fuse",
Miklos Szeredi79c0b2d2007-05-08 00:25:43 -07001008 .fs_flags = FS_HAS_SUBTYPE,
Miklos Szeredi875d95e2006-12-06 20:35:54 -08001009 .get_sb = fuse_get_sb,
John Muir3b463ae2009-05-31 11:13:57 -04001010 .kill_sb = fuse_kill_sb_anon,
Miklos Szeredi875d95e2006-12-06 20:35:54 -08001011};
1012
1013#ifdef CONFIG_BLOCK
Miklos Szeredid6392f82006-12-06 20:35:44 -08001014static int fuse_get_sb_blk(struct file_system_type *fs_type,
1015 int flags, const char *dev_name,
1016 void *raw_data, struct vfsmount *mnt)
1017{
1018 return get_sb_bdev(fs_type, flags, dev_name, raw_data, fuse_fill_super,
1019 mnt);
1020}
1021
John Muir3b463ae2009-05-31 11:13:57 -04001022static void fuse_kill_sb_blk(struct super_block *sb)
1023{
1024 struct fuse_conn *fc = get_fuse_conn_super(sb);
1025
1026 if (fc) {
1027 down_write(&fc->killsb);
1028 fc->sb = NULL;
1029 up_write(&fc->killsb);
1030 }
1031
1032 kill_block_super(sb);
1033}
1034
Miklos Szeredid6392f82006-12-06 20:35:44 -08001035static struct file_system_type fuseblk_fs_type = {
1036 .owner = THIS_MODULE,
1037 .name = "fuseblk",
1038 .get_sb = fuse_get_sb_blk,
John Muir3b463ae2009-05-31 11:13:57 -04001039 .kill_sb = fuse_kill_sb_blk,
Alexey Dobriyanedad01e2007-06-16 10:16:05 -07001040 .fs_flags = FS_REQUIRES_DEV | FS_HAS_SUBTYPE,
Miklos Szeredid6392f82006-12-06 20:35:44 -08001041};
1042
Miklos Szeredi875d95e2006-12-06 20:35:54 -08001043static inline int register_fuseblk(void)
1044{
1045 return register_filesystem(&fuseblk_fs_type);
1046}
1047
1048static inline void unregister_fuseblk(void)
1049{
1050 unregister_filesystem(&fuseblk_fs_type);
1051}
1052#else
1053static inline int register_fuseblk(void)
1054{
1055 return 0;
1056}
1057
1058static inline void unregister_fuseblk(void)
1059{
1060}
1061#endif
1062
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001063static void fuse_inode_init_once(void *foo)
Miklos Szeredid8a5ba42005-09-09 13:10:26 -07001064{
Miklos Szeredi1729a162008-11-26 12:03:54 +01001065 struct inode *inode = foo;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -07001066
Christoph Lametera35afb82007-05-16 22:10:57 -07001067 inode_init_once(inode);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -07001068}
1069
1070static int __init fuse_fs_init(void)
1071{
1072 int err;
1073
1074 err = register_filesystem(&fuse_fs_type);
1075 if (err)
Miklos Szeredid6392f82006-12-06 20:35:44 -08001076 goto out;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -07001077
Miklos Szeredi875d95e2006-12-06 20:35:54 -08001078 err = register_fuseblk();
Miklos Szeredid6392f82006-12-06 20:35:44 -08001079 if (err)
1080 goto out_unreg;
1081
1082 fuse_inode_cachep = kmem_cache_create("fuse_inode",
1083 sizeof(struct fuse_inode),
1084 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +09001085 fuse_inode_init_once);
Miklos Szeredid6392f82006-12-06 20:35:44 -08001086 err = -ENOMEM;
1087 if (!fuse_inode_cachep)
1088 goto out_unreg2;
1089
1090 return 0;
1091
1092 out_unreg2:
Miklos Szeredi875d95e2006-12-06 20:35:54 -08001093 unregister_fuseblk();
Miklos Szeredid6392f82006-12-06 20:35:44 -08001094 out_unreg:
1095 unregister_filesystem(&fuse_fs_type);
1096 out:
Miklos Szeredid8a5ba42005-09-09 13:10:26 -07001097 return err;
1098}
1099
1100static void fuse_fs_cleanup(void)
1101{
1102 unregister_filesystem(&fuse_fs_type);
Miklos Szeredi875d95e2006-12-06 20:35:54 -08001103 unregister_fuseblk();
Miklos Szeredid8a5ba42005-09-09 13:10:26 -07001104 kmem_cache_destroy(fuse_inode_cachep);
1105}
1106
Greg Kroah-Hartman5c89e172007-10-29 20:13:17 +01001107static struct kobject *fuse_kobj;
1108static struct kobject *connections_kobj;
1109
Miklos Szeredif543f252006-01-16 22:14:35 -08001110static int fuse_sysfs_init(void)
1111{
1112 int err;
1113
Greg Kroah-Hartman00d26662007-10-29 14:17:23 -06001114 fuse_kobj = kobject_create_and_add("fuse", fs_kobj);
Greg Kroah-Hartman5c89e172007-10-29 20:13:17 +01001115 if (!fuse_kobj) {
1116 err = -ENOMEM;
Miklos Szeredif543f252006-01-16 22:14:35 -08001117 goto out_err;
Greg Kroah-Hartman5c89e172007-10-29 20:13:17 +01001118 }
Miklos Szeredif543f252006-01-16 22:14:35 -08001119
Greg Kroah-Hartman5c89e172007-10-29 20:13:17 +01001120 connections_kobj = kobject_create_and_add("connections", fuse_kobj);
1121 if (!connections_kobj) {
1122 err = -ENOMEM;
Miklos Szeredif543f252006-01-16 22:14:35 -08001123 goto out_fuse_unregister;
Greg Kroah-Hartman5c89e172007-10-29 20:13:17 +01001124 }
Miklos Szeredif543f252006-01-16 22:14:35 -08001125
1126 return 0;
1127
1128 out_fuse_unregister:
Greg Kroah-Hartman197b12d2007-12-20 08:13:05 -08001129 kobject_put(fuse_kobj);
Miklos Szeredif543f252006-01-16 22:14:35 -08001130 out_err:
1131 return err;
1132}
1133
1134static void fuse_sysfs_cleanup(void)
1135{
Greg Kroah-Hartman197b12d2007-12-20 08:13:05 -08001136 kobject_put(connections_kobj);
1137 kobject_put(fuse_kobj);
Miklos Szeredif543f252006-01-16 22:14:35 -08001138}
1139
Miklos Szeredid8a5ba42005-09-09 13:10:26 -07001140static int __init fuse_init(void)
1141{
1142 int res;
1143
Miklos Szeredi1729a162008-11-26 12:03:54 +01001144 printk(KERN_INFO "fuse init (API version %i.%i)\n",
Miklos Szeredid8a5ba42005-09-09 13:10:26 -07001145 FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
1146
Miklos Szeredibafa9652006-06-25 05:48:51 -07001147 INIT_LIST_HEAD(&fuse_conn_list);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -07001148 res = fuse_fs_init();
1149 if (res)
1150 goto err;
1151
Miklos Szeredi334f4852005-09-09 13:10:27 -07001152 res = fuse_dev_init();
1153 if (res)
1154 goto err_fs_cleanup;
1155
Miklos Szeredif543f252006-01-16 22:14:35 -08001156 res = fuse_sysfs_init();
1157 if (res)
1158 goto err_dev_cleanup;
1159
Miklos Szeredibafa9652006-06-25 05:48:51 -07001160 res = fuse_ctl_init();
1161 if (res)
1162 goto err_sysfs_cleanup;
1163
Miklos Szeredid8a5ba42005-09-09 13:10:26 -07001164 return 0;
1165
Miklos Szeredibafa9652006-06-25 05:48:51 -07001166 err_sysfs_cleanup:
1167 fuse_sysfs_cleanup();
Miklos Szeredif543f252006-01-16 22:14:35 -08001168 err_dev_cleanup:
1169 fuse_dev_cleanup();
Miklos Szeredi334f4852005-09-09 13:10:27 -07001170 err_fs_cleanup:
1171 fuse_fs_cleanup();
Miklos Szeredid8a5ba42005-09-09 13:10:26 -07001172 err:
1173 return res;
1174}
1175
1176static void __exit fuse_exit(void)
1177{
1178 printk(KERN_DEBUG "fuse exit\n");
1179
Miklos Szeredibafa9652006-06-25 05:48:51 -07001180 fuse_ctl_cleanup();
Miklos Szeredif543f252006-01-16 22:14:35 -08001181 fuse_sysfs_cleanup();
Miklos Szeredid8a5ba42005-09-09 13:10:26 -07001182 fuse_fs_cleanup();
Miklos Szeredi334f4852005-09-09 13:10:27 -07001183 fuse_dev_cleanup();
Miklos Szeredid8a5ba42005-09-09 13:10:26 -07001184}
1185
1186module_init(fuse_init);
1187module_exit(fuse_exit);