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