Commit | Line | Data |
---|---|---|
d8a5ba45 MS |
1 | /* |
2 | FUSE: Filesystem in Userspace | |
d7133114 | 3 | Copyright (C) 2001-2006 Miklos Szeredi <miklos@szeredi.hu> |
d8a5ba45 MS |
4 | |
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> | |
d8a5ba45 MS |
14 | #include <linux/seq_file.h> |
15 | #include <linux/init.h> | |
16 | #include <linux/module.h> | |
d8a5ba45 MS |
17 | #include <linux/parser.h> |
18 | #include <linux/statfs.h> | |
19 | ||
20 | MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>"); | |
21 | MODULE_DESCRIPTION("Filesystem in Userspace"); | |
22 | MODULE_LICENSE("GPL"); | |
23 | ||
d8a5ba45 | 24 | static kmem_cache_t *fuse_inode_cachep; |
f543f253 MS |
25 | static struct subsystem connections_subsys; |
26 | ||
27 | struct fuse_conn_attr { | |
28 | struct attribute attr; | |
29 | ssize_t (*show)(struct fuse_conn *, char *); | |
30 | ssize_t (*store)(struct fuse_conn *, const char *, size_t); | |
31 | }; | |
d8a5ba45 MS |
32 | |
33 | #define FUSE_SUPER_MAGIC 0x65735546 | |
34 | ||
35 | struct fuse_mount_data { | |
36 | int fd; | |
37 | unsigned rootmode; | |
38 | unsigned user_id; | |
87729a55 | 39 | unsigned group_id; |
5a533682 MS |
40 | unsigned fd_present : 1; |
41 | unsigned rootmode_present : 1; | |
42 | unsigned user_id_present : 1; | |
43 | unsigned group_id_present : 1; | |
1e9a4ed9 | 44 | unsigned flags; |
db50b96c | 45 | unsigned max_read; |
d8a5ba45 MS |
46 | }; |
47 | ||
48 | static struct inode *fuse_alloc_inode(struct super_block *sb) | |
49 | { | |
50 | struct inode *inode; | |
51 | struct fuse_inode *fi; | |
52 | ||
53 | inode = kmem_cache_alloc(fuse_inode_cachep, SLAB_KERNEL); | |
54 | if (!inode) | |
55 | return NULL; | |
56 | ||
57 | fi = get_fuse_inode(inode); | |
58 | fi->i_time = jiffies - 1; | |
59 | fi->nodeid = 0; | |
9e6268db | 60 | fi->nlookup = 0; |
e5e5558e MS |
61 | fi->forget_req = fuse_request_alloc(); |
62 | if (!fi->forget_req) { | |
63 | kmem_cache_free(fuse_inode_cachep, inode); | |
64 | return NULL; | |
65 | } | |
d8a5ba45 MS |
66 | |
67 | return inode; | |
68 | } | |
69 | ||
70 | static void fuse_destroy_inode(struct inode *inode) | |
71 | { | |
e5e5558e MS |
72 | struct fuse_inode *fi = get_fuse_inode(inode); |
73 | if (fi->forget_req) | |
74 | fuse_request_free(fi->forget_req); | |
d8a5ba45 MS |
75 | kmem_cache_free(fuse_inode_cachep, inode); |
76 | } | |
77 | ||
78 | static void fuse_read_inode(struct inode *inode) | |
79 | { | |
80 | /* No op */ | |
81 | } | |
82 | ||
e5e5558e | 83 | void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req, |
9e6268db | 84 | unsigned long nodeid, u64 nlookup) |
e5e5558e MS |
85 | { |
86 | struct fuse_forget_in *inarg = &req->misc.forget_in; | |
9e6268db | 87 | inarg->nlookup = nlookup; |
e5e5558e MS |
88 | req->in.h.opcode = FUSE_FORGET; |
89 | req->in.h.nodeid = nodeid; | |
90 | req->in.numargs = 1; | |
91 | req->in.args[0].size = sizeof(struct fuse_forget_in); | |
92 | req->in.args[0].value = inarg; | |
93 | request_send_noreply(fc, req); | |
94 | } | |
95 | ||
d8a5ba45 MS |
96 | static void fuse_clear_inode(struct inode *inode) |
97 | { | |
1e9a4ed9 MS |
98 | if (inode->i_sb->s_flags & MS_ACTIVE) { |
99 | struct fuse_conn *fc = get_fuse_conn(inode); | |
e5e5558e | 100 | struct fuse_inode *fi = get_fuse_inode(inode); |
9e6268db | 101 | fuse_send_forget(fc, fi->forget_req, fi->nodeid, fi->nlookup); |
e5e5558e MS |
102 | fi->forget_req = NULL; |
103 | } | |
d8a5ba45 MS |
104 | } |
105 | ||
106 | void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr) | |
107 | { | |
108 | if (S_ISREG(inode->i_mode) && i_size_read(inode) != attr->size) | |
109 | invalidate_inode_pages(inode->i_mapping); | |
110 | ||
111 | inode->i_ino = attr->ino; | |
112 | inode->i_mode = (inode->i_mode & S_IFMT) + (attr->mode & 07777); | |
113 | inode->i_nlink = attr->nlink; | |
114 | inode->i_uid = attr->uid; | |
115 | inode->i_gid = attr->gid; | |
116 | i_size_write(inode, attr->size); | |
117 | inode->i_blksize = PAGE_CACHE_SIZE; | |
118 | inode->i_blocks = attr->blocks; | |
119 | inode->i_atime.tv_sec = attr->atime; | |
120 | inode->i_atime.tv_nsec = attr->atimensec; | |
121 | inode->i_mtime.tv_sec = attr->mtime; | |
122 | inode->i_mtime.tv_nsec = attr->mtimensec; | |
123 | inode->i_ctime.tv_sec = attr->ctime; | |
124 | inode->i_ctime.tv_nsec = attr->ctimensec; | |
125 | } | |
126 | ||
127 | static void fuse_init_inode(struct inode *inode, struct fuse_attr *attr) | |
128 | { | |
129 | inode->i_mode = attr->mode & S_IFMT; | |
130 | i_size_write(inode, attr->size); | |
e5e5558e MS |
131 | if (S_ISREG(inode->i_mode)) { |
132 | fuse_init_common(inode); | |
b6aeaded | 133 | fuse_init_file_inode(inode); |
e5e5558e MS |
134 | } else if (S_ISDIR(inode->i_mode)) |
135 | fuse_init_dir(inode); | |
136 | else if (S_ISLNK(inode->i_mode)) | |
137 | fuse_init_symlink(inode); | |
138 | else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || | |
139 | S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { | |
140 | fuse_init_common(inode); | |
141 | init_special_inode(inode, inode->i_mode, | |
142 | new_decode_dev(attr->rdev)); | |
39ee059a MS |
143 | } else |
144 | BUG(); | |
d8a5ba45 MS |
145 | } |
146 | ||
147 | static int fuse_inode_eq(struct inode *inode, void *_nodeidp) | |
148 | { | |
149 | unsigned long nodeid = *(unsigned long *) _nodeidp; | |
150 | if (get_node_id(inode) == nodeid) | |
151 | return 1; | |
152 | else | |
153 | return 0; | |
154 | } | |
155 | ||
156 | static int fuse_inode_set(struct inode *inode, void *_nodeidp) | |
157 | { | |
158 | unsigned long nodeid = *(unsigned long *) _nodeidp; | |
159 | get_fuse_inode(inode)->nodeid = nodeid; | |
160 | return 0; | |
161 | } | |
162 | ||
163 | struct inode *fuse_iget(struct super_block *sb, unsigned long nodeid, | |
9e6268db | 164 | int generation, struct fuse_attr *attr) |
d8a5ba45 MS |
165 | { |
166 | struct inode *inode; | |
9e6268db | 167 | struct fuse_inode *fi; |
d8a5ba45 MS |
168 | struct fuse_conn *fc = get_fuse_conn_super(sb); |
169 | int retried = 0; | |
170 | ||
171 | retry: | |
172 | inode = iget5_locked(sb, nodeid, fuse_inode_eq, fuse_inode_set, &nodeid); | |
173 | if (!inode) | |
174 | return NULL; | |
175 | ||
176 | if ((inode->i_state & I_NEW)) { | |
b36c31ba | 177 | inode->i_flags |= S_NOATIME|S_NOCMTIME; |
d8a5ba45 MS |
178 | inode->i_generation = generation; |
179 | inode->i_data.backing_dev_info = &fc->bdi; | |
180 | fuse_init_inode(inode, attr); | |
181 | unlock_new_inode(inode); | |
182 | } else if ((inode->i_mode ^ attr->mode) & S_IFMT) { | |
183 | BUG_ON(retried); | |
184 | /* Inode has changed type, any I/O on the old should fail */ | |
185 | make_bad_inode(inode); | |
186 | iput(inode); | |
187 | retried = 1; | |
188 | goto retry; | |
189 | } | |
190 | ||
9e6268db MS |
191 | fi = get_fuse_inode(inode); |
192 | fi->nlookup ++; | |
d8a5ba45 | 193 | fuse_change_attributes(inode, attr); |
d8a5ba45 MS |
194 | return inode; |
195 | } | |
196 | ||
69a53bf2 MS |
197 | static void fuse_umount_begin(struct super_block *sb) |
198 | { | |
199 | fuse_abort_conn(get_fuse_conn_super(sb)); | |
200 | } | |
201 | ||
d8a5ba45 MS |
202 | static void fuse_put_super(struct super_block *sb) |
203 | { | |
204 | struct fuse_conn *fc = get_fuse_conn_super(sb); | |
205 | ||
d7133114 | 206 | spin_lock(&fc->lock); |
9ba7cbba | 207 | fc->connected = 0; |
51eb01e7 | 208 | fc->blocked = 0; |
d7133114 | 209 | spin_unlock(&fc->lock); |
334f485d | 210 | /* Flush all readers on this fs */ |
385a17bf | 211 | kill_fasync(&fc->fasync, SIGIO, POLL_IN); |
334f485d | 212 | wake_up_all(&fc->waitq); |
51eb01e7 | 213 | wake_up_all(&fc->blocked_waitq); |
f543f253 MS |
214 | kobject_del(&fc->kobj); |
215 | kobject_put(&fc->kobj); | |
d8a5ba45 MS |
216 | } |
217 | ||
e5e5558e MS |
218 | static void convert_fuse_statfs(struct kstatfs *stbuf, struct fuse_kstatfs *attr) |
219 | { | |
220 | stbuf->f_type = FUSE_SUPER_MAGIC; | |
221 | stbuf->f_bsize = attr->bsize; | |
de5f1202 | 222 | stbuf->f_frsize = attr->frsize; |
e5e5558e MS |
223 | stbuf->f_blocks = attr->blocks; |
224 | stbuf->f_bfree = attr->bfree; | |
225 | stbuf->f_bavail = attr->bavail; | |
226 | stbuf->f_files = attr->files; | |
227 | stbuf->f_ffree = attr->ffree; | |
228 | stbuf->f_namelen = attr->namelen; | |
229 | /* fsid is left zero */ | |
230 | } | |
231 | ||
726c3342 | 232 | static int fuse_statfs(struct dentry *dentry, struct kstatfs *buf) |
e5e5558e | 233 | { |
726c3342 | 234 | struct super_block *sb = dentry->d_sb; |
e5e5558e MS |
235 | struct fuse_conn *fc = get_fuse_conn_super(sb); |
236 | struct fuse_req *req; | |
237 | struct fuse_statfs_out outarg; | |
238 | int err; | |
239 | ||
ce1d5a49 MS |
240 | req = fuse_get_req(fc); |
241 | if (IS_ERR(req)) | |
242 | return PTR_ERR(req); | |
e5e5558e | 243 | |
de5f1202 | 244 | memset(&outarg, 0, sizeof(outarg)); |
e5e5558e MS |
245 | req->in.numargs = 0; |
246 | req->in.h.opcode = FUSE_STATFS; | |
247 | req->out.numargs = 1; | |
de5f1202 MS |
248 | req->out.args[0].size = |
249 | fc->minor < 4 ? FUSE_COMPAT_STATFS_SIZE : sizeof(outarg); | |
e5e5558e MS |
250 | req->out.args[0].value = &outarg; |
251 | request_send(fc, req); | |
252 | err = req->out.h.error; | |
253 | if (!err) | |
254 | convert_fuse_statfs(buf, &outarg.st); | |
255 | fuse_put_request(fc, req); | |
256 | return err; | |
257 | } | |
258 | ||
d8a5ba45 MS |
259 | enum { |
260 | OPT_FD, | |
261 | OPT_ROOTMODE, | |
262 | OPT_USER_ID, | |
87729a55 | 263 | OPT_GROUP_ID, |
d8a5ba45 MS |
264 | OPT_DEFAULT_PERMISSIONS, |
265 | OPT_ALLOW_OTHER, | |
db50b96c | 266 | OPT_MAX_READ, |
d8a5ba45 MS |
267 | OPT_ERR |
268 | }; | |
269 | ||
270 | static match_table_t tokens = { | |
271 | {OPT_FD, "fd=%u"}, | |
272 | {OPT_ROOTMODE, "rootmode=%o"}, | |
273 | {OPT_USER_ID, "user_id=%u"}, | |
87729a55 | 274 | {OPT_GROUP_ID, "group_id=%u"}, |
d8a5ba45 MS |
275 | {OPT_DEFAULT_PERMISSIONS, "default_permissions"}, |
276 | {OPT_ALLOW_OTHER, "allow_other"}, | |
db50b96c | 277 | {OPT_MAX_READ, "max_read=%u"}, |
d8a5ba45 MS |
278 | {OPT_ERR, NULL} |
279 | }; | |
280 | ||
281 | static int parse_fuse_opt(char *opt, struct fuse_mount_data *d) | |
282 | { | |
283 | char *p; | |
284 | memset(d, 0, sizeof(struct fuse_mount_data)); | |
db50b96c | 285 | d->max_read = ~0; |
d8a5ba45 MS |
286 | |
287 | while ((p = strsep(&opt, ",")) != NULL) { | |
288 | int token; | |
289 | int value; | |
290 | substring_t args[MAX_OPT_ARGS]; | |
291 | if (!*p) | |
292 | continue; | |
293 | ||
294 | token = match_token(p, tokens, args); | |
295 | switch (token) { | |
296 | case OPT_FD: | |
297 | if (match_int(&args[0], &value)) | |
298 | return 0; | |
299 | d->fd = value; | |
5a533682 | 300 | d->fd_present = 1; |
d8a5ba45 MS |
301 | break; |
302 | ||
303 | case OPT_ROOTMODE: | |
304 | if (match_octal(&args[0], &value)) | |
305 | return 0; | |
306 | d->rootmode = value; | |
5a533682 | 307 | d->rootmode_present = 1; |
d8a5ba45 MS |
308 | break; |
309 | ||
310 | case OPT_USER_ID: | |
311 | if (match_int(&args[0], &value)) | |
312 | return 0; | |
313 | d->user_id = value; | |
5a533682 | 314 | d->user_id_present = 1; |
d8a5ba45 MS |
315 | break; |
316 | ||
87729a55 MS |
317 | case OPT_GROUP_ID: |
318 | if (match_int(&args[0], &value)) | |
319 | return 0; | |
320 | d->group_id = value; | |
5a533682 | 321 | d->group_id_present = 1; |
87729a55 MS |
322 | break; |
323 | ||
1e9a4ed9 MS |
324 | case OPT_DEFAULT_PERMISSIONS: |
325 | d->flags |= FUSE_DEFAULT_PERMISSIONS; | |
326 | break; | |
327 | ||
328 | case OPT_ALLOW_OTHER: | |
329 | d->flags |= FUSE_ALLOW_OTHER; | |
330 | break; | |
331 | ||
db50b96c MS |
332 | case OPT_MAX_READ: |
333 | if (match_int(&args[0], &value)) | |
334 | return 0; | |
335 | d->max_read = value; | |
336 | break; | |
337 | ||
d8a5ba45 MS |
338 | default: |
339 | return 0; | |
340 | } | |
341 | } | |
5a533682 MS |
342 | |
343 | if (!d->fd_present || !d->rootmode_present || | |
344 | !d->user_id_present || !d->group_id_present) | |
d8a5ba45 MS |
345 | return 0; |
346 | ||
347 | return 1; | |
348 | } | |
349 | ||
350 | static int fuse_show_options(struct seq_file *m, struct vfsmount *mnt) | |
351 | { | |
352 | struct fuse_conn *fc = get_fuse_conn_super(mnt->mnt_sb); | |
353 | ||
354 | seq_printf(m, ",user_id=%u", fc->user_id); | |
87729a55 | 355 | seq_printf(m, ",group_id=%u", fc->group_id); |
1e9a4ed9 MS |
356 | if (fc->flags & FUSE_DEFAULT_PERMISSIONS) |
357 | seq_puts(m, ",default_permissions"); | |
358 | if (fc->flags & FUSE_ALLOW_OTHER) | |
359 | seq_puts(m, ",allow_other"); | |
db50b96c MS |
360 | if (fc->max_read != ~0) |
361 | seq_printf(m, ",max_read=%u", fc->max_read); | |
d8a5ba45 MS |
362 | return 0; |
363 | } | |
364 | ||
f543f253 | 365 | static void fuse_conn_release(struct kobject *kobj) |
d8a5ba45 | 366 | { |
ce1d5a49 | 367 | kfree(get_fuse_conn_kobj(kobj)); |
d8a5ba45 MS |
368 | } |
369 | ||
370 | static struct fuse_conn *new_conn(void) | |
371 | { | |
372 | struct fuse_conn *fc; | |
373 | ||
6383bdaa | 374 | fc = kzalloc(sizeof(*fc), GFP_KERNEL); |
f543f253 | 375 | if (fc) { |
d7133114 | 376 | spin_lock_init(&fc->lock); |
334f485d | 377 | init_waitqueue_head(&fc->waitq); |
08a53cdc | 378 | init_waitqueue_head(&fc->blocked_waitq); |
334f485d MS |
379 | INIT_LIST_HEAD(&fc->pending); |
380 | INIT_LIST_HEAD(&fc->processing); | |
d77a1d5b | 381 | INIT_LIST_HEAD(&fc->io); |
f543f253 MS |
382 | kobj_set_kset_s(fc, connections_subsys); |
383 | kobject_init(&fc->kobj); | |
095da6cb | 384 | atomic_set(&fc->num_waiting, 0); |
d8a5ba45 MS |
385 | fc->bdi.ra_pages = (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE; |
386 | fc->bdi.unplug_io_fn = default_unplug_io_fn; | |
334f485d | 387 | fc->reqctr = 0; |
08a53cdc | 388 | fc->blocked = 1; |
d8a5ba45 MS |
389 | } |
390 | return fc; | |
391 | } | |
392 | ||
d8a5ba45 MS |
393 | static struct inode *get_root_inode(struct super_block *sb, unsigned mode) |
394 | { | |
395 | struct fuse_attr attr; | |
396 | memset(&attr, 0, sizeof(attr)); | |
397 | ||
398 | attr.mode = mode; | |
399 | attr.ino = FUSE_ROOT_ID; | |
9e6268db | 400 | return fuse_iget(sb, 1, 0, &attr); |
d8a5ba45 MS |
401 | } |
402 | ||
403 | static struct super_operations fuse_super_operations = { | |
404 | .alloc_inode = fuse_alloc_inode, | |
405 | .destroy_inode = fuse_destroy_inode, | |
406 | .read_inode = fuse_read_inode, | |
407 | .clear_inode = fuse_clear_inode, | |
408 | .put_super = fuse_put_super, | |
69a53bf2 | 409 | .umount_begin = fuse_umount_begin, |
e5e5558e | 410 | .statfs = fuse_statfs, |
d8a5ba45 MS |
411 | .show_options = fuse_show_options, |
412 | }; | |
413 | ||
9b9a0469 MS |
414 | static void process_init_reply(struct fuse_conn *fc, struct fuse_req *req) |
415 | { | |
9b9a0469 MS |
416 | struct fuse_init_out *arg = &req->misc.init_out; |
417 | ||
418 | if (req->out.h.error || arg->major != FUSE_KERNEL_VERSION) | |
419 | fc->conn_error = 1; | |
420 | else { | |
9cd68455 MS |
421 | unsigned long ra_pages; |
422 | ||
423 | if (arg->minor >= 6) { | |
424 | ra_pages = arg->max_readahead / PAGE_CACHE_SIZE; | |
425 | if (arg->flags & FUSE_ASYNC_READ) | |
426 | fc->async_read = 1; | |
427 | } else | |
428 | ra_pages = fc->max_read / PAGE_CACHE_SIZE; | |
429 | ||
430 | fc->bdi.ra_pages = min(fc->bdi.ra_pages, ra_pages); | |
9b9a0469 MS |
431 | fc->minor = arg->minor; |
432 | fc->max_write = arg->minor < 5 ? 4096 : arg->max_write; | |
433 | } | |
9b9a0469 | 434 | fuse_put_request(fc, req); |
08a53cdc MS |
435 | fc->blocked = 0; |
436 | wake_up_all(&fc->blocked_waitq); | |
9b9a0469 MS |
437 | } |
438 | ||
ce1d5a49 | 439 | static void fuse_send_init(struct fuse_conn *fc, struct fuse_req *req) |
9b9a0469 | 440 | { |
9b9a0469 | 441 | struct fuse_init_in *arg = &req->misc.init_in; |
095da6cb | 442 | |
9b9a0469 MS |
443 | arg->major = FUSE_KERNEL_VERSION; |
444 | arg->minor = FUSE_KERNEL_MINOR_VERSION; | |
9cd68455 MS |
445 | arg->max_readahead = fc->bdi.ra_pages * PAGE_CACHE_SIZE; |
446 | arg->flags |= FUSE_ASYNC_READ; | |
9b9a0469 MS |
447 | req->in.h.opcode = FUSE_INIT; |
448 | req->in.numargs = 1; | |
449 | req->in.args[0].size = sizeof(*arg); | |
450 | req->in.args[0].value = arg; | |
451 | req->out.numargs = 1; | |
452 | /* Variable length arguement used for backward compatibility | |
453 | with interface version < 7.5. Rest of init_out is zeroed | |
454 | by do_get_request(), so a short reply is not a problem */ | |
455 | req->out.argvar = 1; | |
456 | req->out.args[0].size = sizeof(struct fuse_init_out); | |
457 | req->out.args[0].value = &req->misc.init_out; | |
458 | req->end = process_init_reply; | |
459 | request_send_background(fc, req); | |
460 | } | |
461 | ||
f543f253 MS |
462 | static unsigned long long conn_id(void) |
463 | { | |
0720b315 | 464 | /* BKL is held for ->get_sb() */ |
f543f253 | 465 | static unsigned long long ctr = 1; |
0720b315 | 466 | return ctr++; |
f543f253 MS |
467 | } |
468 | ||
d8a5ba45 MS |
469 | static int fuse_fill_super(struct super_block *sb, void *data, int silent) |
470 | { | |
471 | struct fuse_conn *fc; | |
472 | struct inode *root; | |
473 | struct fuse_mount_data d; | |
474 | struct file *file; | |
f543f253 | 475 | struct dentry *root_dentry; |
ce1d5a49 | 476 | struct fuse_req *init_req; |
d8a5ba45 MS |
477 | int err; |
478 | ||
479 | if (!parse_fuse_opt((char *) data, &d)) | |
480 | return -EINVAL; | |
481 | ||
482 | sb->s_blocksize = PAGE_CACHE_SIZE; | |
483 | sb->s_blocksize_bits = PAGE_CACHE_SHIFT; | |
484 | sb->s_magic = FUSE_SUPER_MAGIC; | |
485 | sb->s_op = &fuse_super_operations; | |
486 | sb->s_maxbytes = MAX_LFS_FILESIZE; | |
487 | ||
488 | file = fget(d.fd); | |
489 | if (!file) | |
490 | return -EINVAL; | |
491 | ||
0720b315 MS |
492 | if (file->f_op != &fuse_dev_operations) |
493 | return -EINVAL; | |
494 | ||
0720b315 MS |
495 | fc = new_conn(); |
496 | if (!fc) | |
497 | return -ENOMEM; | |
d8a5ba45 | 498 | |
1e9a4ed9 | 499 | fc->flags = d.flags; |
d8a5ba45 | 500 | fc->user_id = d.user_id; |
87729a55 | 501 | fc->group_id = d.group_id; |
db50b96c | 502 | fc->max_read = d.max_read; |
d8a5ba45 | 503 | |
f543f253 MS |
504 | /* Used by get_root_inode() */ |
505 | sb->s_fs_info = fc; | |
506 | ||
d8a5ba45 MS |
507 | err = -ENOMEM; |
508 | root = get_root_inode(sb, d.rootmode); | |
f543f253 | 509 | if (!root) |
d8a5ba45 MS |
510 | goto err; |
511 | ||
f543f253 MS |
512 | root_dentry = d_alloc_root(root); |
513 | if (!root_dentry) { | |
d8a5ba45 MS |
514 | iput(root); |
515 | goto err; | |
516 | } | |
f543f253 | 517 | |
ce1d5a49 MS |
518 | init_req = fuse_request_alloc(); |
519 | if (!init_req) | |
520 | goto err_put_root; | |
521 | ||
f543f253 MS |
522 | err = kobject_set_name(&fc->kobj, "%llu", conn_id()); |
523 | if (err) | |
ce1d5a49 | 524 | goto err_free_req; |
f543f253 MS |
525 | |
526 | err = kobject_add(&fc->kobj); | |
527 | if (err) | |
ce1d5a49 | 528 | goto err_free_req; |
f543f253 | 529 | |
8aa09a50 MS |
530 | /* Setting file->private_data can't race with other mount() |
531 | instances, since BKL is held for ->get_sb() */ | |
532 | err = -EINVAL; | |
533 | if (file->private_data) | |
534 | goto err_kobject_del; | |
535 | ||
f543f253 | 536 | sb->s_root = root_dentry; |
f543f253 | 537 | fc->connected = 1; |
0720b315 MS |
538 | kobject_get(&fc->kobj); |
539 | file->private_data = fc; | |
540 | /* | |
541 | * atomic_dec_and_test() in fput() provides the necessary | |
542 | * memory barrier for file->private_data to be visible on all | |
543 | * CPUs after this | |
544 | */ | |
545 | fput(file); | |
f543f253 | 546 | |
ce1d5a49 | 547 | fuse_send_init(fc, init_req); |
f543f253 | 548 | |
d8a5ba45 MS |
549 | return 0; |
550 | ||
8aa09a50 MS |
551 | err_kobject_del: |
552 | kobject_del(&fc->kobj); | |
ce1d5a49 MS |
553 | err_free_req: |
554 | fuse_request_free(init_req); | |
f543f253 MS |
555 | err_put_root: |
556 | dput(root_dentry); | |
d8a5ba45 | 557 | err: |
0720b315 | 558 | fput(file); |
f543f253 | 559 | kobject_put(&fc->kobj); |
d8a5ba45 MS |
560 | return err; |
561 | } | |
562 | ||
454e2398 DH |
563 | static int fuse_get_sb(struct file_system_type *fs_type, |
564 | int flags, const char *dev_name, | |
565 | void *raw_data, struct vfsmount *mnt) | |
d8a5ba45 | 566 | { |
454e2398 | 567 | return get_sb_nodev(fs_type, flags, raw_data, fuse_fill_super, mnt); |
d8a5ba45 MS |
568 | } |
569 | ||
570 | static struct file_system_type fuse_fs_type = { | |
571 | .owner = THIS_MODULE, | |
572 | .name = "fuse", | |
573 | .get_sb = fuse_get_sb, | |
574 | .kill_sb = kill_anon_super, | |
575 | }; | |
576 | ||
0cd5b885 MS |
577 | static ssize_t fuse_conn_waiting_show(struct fuse_conn *fc, char *page) |
578 | { | |
579 | return sprintf(page, "%i\n", atomic_read(&fc->num_waiting)); | |
580 | } | |
581 | ||
69a53bf2 MS |
582 | static ssize_t fuse_conn_abort_store(struct fuse_conn *fc, const char *page, |
583 | size_t count) | |
584 | { | |
585 | fuse_abort_conn(fc); | |
586 | return count; | |
587 | } | |
588 | ||
0cd5b885 MS |
589 | static struct fuse_conn_attr fuse_conn_waiting = |
590 | __ATTR(waiting, 0400, fuse_conn_waiting_show, NULL); | |
69a53bf2 MS |
591 | static struct fuse_conn_attr fuse_conn_abort = |
592 | __ATTR(abort, 0600, NULL, fuse_conn_abort_store); | |
0cd5b885 | 593 | |
f543f253 | 594 | static struct attribute *fuse_conn_attrs[] = { |
0cd5b885 | 595 | &fuse_conn_waiting.attr, |
69a53bf2 | 596 | &fuse_conn_abort.attr, |
f543f253 MS |
597 | NULL, |
598 | }; | |
599 | ||
600 | static ssize_t fuse_conn_attr_show(struct kobject *kobj, | |
601 | struct attribute *attr, | |
602 | char *page) | |
603 | { | |
604 | struct fuse_conn_attr *fca = | |
605 | container_of(attr, struct fuse_conn_attr, attr); | |
606 | ||
607 | if (fca->show) | |
608 | return fca->show(get_fuse_conn_kobj(kobj), page); | |
609 | else | |
610 | return -EACCES; | |
611 | } | |
612 | ||
613 | static ssize_t fuse_conn_attr_store(struct kobject *kobj, | |
614 | struct attribute *attr, | |
615 | const char *page, size_t count) | |
616 | { | |
617 | struct fuse_conn_attr *fca = | |
618 | container_of(attr, struct fuse_conn_attr, attr); | |
619 | ||
620 | if (fca->store) | |
621 | return fca->store(get_fuse_conn_kobj(kobj), page, count); | |
622 | else | |
623 | return -EACCES; | |
624 | } | |
625 | ||
626 | static struct sysfs_ops fuse_conn_sysfs_ops = { | |
627 | .show = &fuse_conn_attr_show, | |
628 | .store = &fuse_conn_attr_store, | |
629 | }; | |
630 | ||
631 | static struct kobj_type ktype_fuse_conn = { | |
632 | .release = fuse_conn_release, | |
633 | .sysfs_ops = &fuse_conn_sysfs_ops, | |
634 | .default_attrs = fuse_conn_attrs, | |
635 | }; | |
636 | ||
637 | static decl_subsys(fuse, NULL, NULL); | |
638 | static decl_subsys(connections, &ktype_fuse_conn, NULL); | |
639 | ||
d8a5ba45 MS |
640 | static void fuse_inode_init_once(void *foo, kmem_cache_t *cachep, |
641 | unsigned long flags) | |
642 | { | |
643 | struct inode * inode = foo; | |
644 | ||
645 | if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) == | |
646 | SLAB_CTOR_CONSTRUCTOR) | |
647 | inode_init_once(inode); | |
648 | } | |
649 | ||
650 | static int __init fuse_fs_init(void) | |
651 | { | |
652 | int err; | |
653 | ||
654 | err = register_filesystem(&fuse_fs_type); | |
655 | if (err) | |
656 | printk("fuse: failed to register filesystem\n"); | |
657 | else { | |
658 | fuse_inode_cachep = kmem_cache_create("fuse_inode", | |
659 | sizeof(struct fuse_inode), | |
660 | 0, SLAB_HWCACHE_ALIGN, | |
661 | fuse_inode_init_once, NULL); | |
662 | if (!fuse_inode_cachep) { | |
663 | unregister_filesystem(&fuse_fs_type); | |
664 | err = -ENOMEM; | |
665 | } | |
666 | } | |
667 | ||
668 | return err; | |
669 | } | |
670 | ||
671 | static void fuse_fs_cleanup(void) | |
672 | { | |
673 | unregister_filesystem(&fuse_fs_type); | |
674 | kmem_cache_destroy(fuse_inode_cachep); | |
675 | } | |
676 | ||
f543f253 MS |
677 | static int fuse_sysfs_init(void) |
678 | { | |
679 | int err; | |
680 | ||
681 | kset_set_kset_s(&fuse_subsys, fs_subsys); | |
682 | err = subsystem_register(&fuse_subsys); | |
683 | if (err) | |
684 | goto out_err; | |
685 | ||
686 | kset_set_kset_s(&connections_subsys, fuse_subsys); | |
687 | err = subsystem_register(&connections_subsys); | |
688 | if (err) | |
689 | goto out_fuse_unregister; | |
690 | ||
691 | return 0; | |
692 | ||
693 | out_fuse_unregister: | |
694 | subsystem_unregister(&fuse_subsys); | |
695 | out_err: | |
696 | return err; | |
697 | } | |
698 | ||
699 | static void fuse_sysfs_cleanup(void) | |
700 | { | |
701 | subsystem_unregister(&connections_subsys); | |
702 | subsystem_unregister(&fuse_subsys); | |
703 | } | |
704 | ||
d8a5ba45 MS |
705 | static int __init fuse_init(void) |
706 | { | |
707 | int res; | |
708 | ||
709 | printk("fuse init (API version %i.%i)\n", | |
710 | FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION); | |
711 | ||
d8a5ba45 MS |
712 | res = fuse_fs_init(); |
713 | if (res) | |
714 | goto err; | |
715 | ||
334f485d MS |
716 | res = fuse_dev_init(); |
717 | if (res) | |
718 | goto err_fs_cleanup; | |
719 | ||
f543f253 MS |
720 | res = fuse_sysfs_init(); |
721 | if (res) | |
722 | goto err_dev_cleanup; | |
723 | ||
d8a5ba45 MS |
724 | return 0; |
725 | ||
f543f253 MS |
726 | err_dev_cleanup: |
727 | fuse_dev_cleanup(); | |
334f485d MS |
728 | err_fs_cleanup: |
729 | fuse_fs_cleanup(); | |
d8a5ba45 MS |
730 | err: |
731 | return res; | |
732 | } | |
733 | ||
734 | static void __exit fuse_exit(void) | |
735 | { | |
736 | printk(KERN_DEBUG "fuse exit\n"); | |
737 | ||
f543f253 | 738 | fuse_sysfs_cleanup(); |
d8a5ba45 | 739 | fuse_fs_cleanup(); |
334f485d | 740 | fuse_dev_cleanup(); |
d8a5ba45 MS |
741 | } |
742 | ||
743 | module_init(fuse_init); | |
744 | module_exit(fuse_exit); |