blob: 1e2006caf15842b2c2beffe11968f3cc6311f16f [file] [log] [blame]
Miklos Szeredi334f4852005-09-09 13:10:27 -07001/*
2 FUSE: Filesystem in Userspace
Miklos Szeredid7133112006-04-10 22:54:55 -07003 Copyright (C) 2001-2006 Miklos Szeredi <miklos@szeredi.hu>
Miklos Szeredi334f4852005-09-09 13:10:27 -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/init.h>
12#include <linux/module.h>
13#include <linux/poll.h>
14#include <linux/uio.h>
15#include <linux/miscdevice.h>
16#include <linux/pagemap.h>
17#include <linux/file.h>
18#include <linux/slab.h>
19
20MODULE_ALIAS_MISCDEV(FUSE_MINOR);
21
22static kmem_cache_t *fuse_req_cachep;
23
Miklos Szeredi8bfc0162006-01-16 22:14:28 -080024static struct fuse_conn *fuse_get_conn(struct file *file)
Miklos Szeredi334f4852005-09-09 13:10:27 -070025{
Miklos Szeredi0720b312006-04-10 22:54:55 -070026 /*
27 * Lockless access is OK, because file->private data is set
28 * once during mount and is valid until the file is released.
29 */
30 return file->private_data;
Miklos Szeredi334f4852005-09-09 13:10:27 -070031}
32
Miklos Szeredi8bfc0162006-01-16 22:14:28 -080033static void fuse_request_init(struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -070034{
35 memset(req, 0, sizeof(*req));
36 INIT_LIST_HEAD(&req->list);
Miklos Szeredia4d27e72006-06-25 05:48:54 -070037 INIT_LIST_HEAD(&req->intr_entry);
Miklos Szeredi334f4852005-09-09 13:10:27 -070038 init_waitqueue_head(&req->waitq);
39 atomic_set(&req->count, 1);
40}
41
42struct fuse_req *fuse_request_alloc(void)
43{
44 struct fuse_req *req = kmem_cache_alloc(fuse_req_cachep, SLAB_KERNEL);
45 if (req)
46 fuse_request_init(req);
47 return req;
48}
49
50void fuse_request_free(struct fuse_req *req)
51{
52 kmem_cache_free(fuse_req_cachep, req);
53}
54
Miklos Szeredi8bfc0162006-01-16 22:14:28 -080055static void block_sigs(sigset_t *oldset)
Miklos Szeredi334f4852005-09-09 13:10:27 -070056{
57 sigset_t mask;
58
59 siginitsetinv(&mask, sigmask(SIGKILL));
60 sigprocmask(SIG_BLOCK, &mask, oldset);
61}
62
Miklos Szeredi8bfc0162006-01-16 22:14:28 -080063static void restore_sigs(sigset_t *oldset)
Miklos Szeredi334f4852005-09-09 13:10:27 -070064{
65 sigprocmask(SIG_SETMASK, oldset, NULL);
66}
67
Miklos Szeredi334f4852005-09-09 13:10:27 -070068static void __fuse_get_request(struct fuse_req *req)
69{
70 atomic_inc(&req->count);
71}
72
73/* Must be called with > 1 refcount */
74static void __fuse_put_request(struct fuse_req *req)
75{
76 BUG_ON(atomic_read(&req->count) < 2);
77 atomic_dec(&req->count);
78}
79
Miklos Szeredi33649c92006-06-25 05:48:52 -070080static void fuse_req_init_context(struct fuse_req *req)
81{
82 req->in.h.uid = current->fsuid;
83 req->in.h.gid = current->fsgid;
84 req->in.h.pid = current->pid;
85}
86
Miklos Szeredice1d5a42006-04-10 22:54:58 -070087struct fuse_req *fuse_get_req(struct fuse_conn *fc)
Miklos Szeredi334f4852005-09-09 13:10:27 -070088{
Miklos Szeredi08a53cd2006-04-10 22:54:59 -070089 struct fuse_req *req;
90 sigset_t oldset;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +020091 int intr;
Miklos Szeredi08a53cd2006-04-10 22:54:59 -070092 int err;
93
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +020094 atomic_inc(&fc->num_waiting);
Miklos Szeredi08a53cd2006-04-10 22:54:59 -070095 block_sigs(&oldset);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +020096 intr = wait_event_interruptible(fc->blocked_waitq, !fc->blocked);
Miklos Szeredi08a53cd2006-04-10 22:54:59 -070097 restore_sigs(&oldset);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +020098 err = -EINTR;
99 if (intr)
100 goto out;
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700101
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700102 err = -ENOTCONN;
103 if (!fc->connected)
104 goto out;
105
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700106 req = fuse_request_alloc();
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200107 err = -ENOMEM;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700108 if (!req)
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200109 goto out;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700110
Miklos Szeredi33649c92006-06-25 05:48:52 -0700111 fuse_req_init_context(req);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200112 req->waiting = 1;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700113 return req;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200114
115 out:
116 atomic_dec(&fc->num_waiting);
117 return ERR_PTR(err);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700118}
119
Miklos Szeredi33649c92006-06-25 05:48:52 -0700120/*
121 * Return request in fuse_file->reserved_req. However that may
122 * currently be in use. If that is the case, wait for it to become
123 * available.
124 */
125static struct fuse_req *get_reserved_req(struct fuse_conn *fc,
126 struct file *file)
127{
128 struct fuse_req *req = NULL;
129 struct fuse_file *ff = file->private_data;
130
131 do {
132 wait_event(fc->blocked_waitq, ff->reserved_req);
133 spin_lock(&fc->lock);
134 if (ff->reserved_req) {
135 req = ff->reserved_req;
136 ff->reserved_req = NULL;
137 get_file(file);
138 req->stolen_file = file;
139 }
140 spin_unlock(&fc->lock);
141 } while (!req);
142
143 return req;
144}
145
146/*
147 * Put stolen request back into fuse_file->reserved_req
148 */
149static void put_reserved_req(struct fuse_conn *fc, struct fuse_req *req)
150{
151 struct file *file = req->stolen_file;
152 struct fuse_file *ff = file->private_data;
153
154 spin_lock(&fc->lock);
155 fuse_request_init(req);
156 BUG_ON(ff->reserved_req);
157 ff->reserved_req = req;
158 wake_up(&fc->blocked_waitq);
159 spin_unlock(&fc->lock);
160 fput(file);
161}
162
163/*
164 * Gets a requests for a file operation, always succeeds
165 *
166 * This is used for sending the FLUSH request, which must get to
167 * userspace, due to POSIX locks which may need to be unlocked.
168 *
169 * If allocation fails due to OOM, use the reserved request in
170 * fuse_file.
171 *
172 * This is very unlikely to deadlock accidentally, since the
173 * filesystem should not have it's own file open. If deadlock is
174 * intentional, it can still be broken by "aborting" the filesystem.
175 */
176struct fuse_req *fuse_get_req_nofail(struct fuse_conn *fc, struct file *file)
177{
178 struct fuse_req *req;
179
180 atomic_inc(&fc->num_waiting);
181 wait_event(fc->blocked_waitq, !fc->blocked);
182 req = fuse_request_alloc();
183 if (!req)
184 req = get_reserved_req(fc, file);
185
186 fuse_req_init_context(req);
187 req->waiting = 1;
188 return req;
189}
190
Miklos Szeredi334f4852005-09-09 13:10:27 -0700191void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req)
192{
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800193 if (atomic_dec_and_test(&req->count)) {
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200194 if (req->waiting)
195 atomic_dec(&fc->num_waiting);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700196
197 if (req->stolen_file)
198 put_reserved_req(fc, req);
199 else
200 fuse_request_free(req);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800201 }
202}
203
Miklos Szeredi6dbbcb12006-04-26 10:49:06 +0200204/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700205 * This function is called when a request is finished. Either a reply
Miklos Szeredif9a28422006-06-25 05:48:53 -0700206 * has arrived or it was aborted (and not yet sent) or some error
Miklos Szeredif43b1552006-01-16 22:14:26 -0800207 * occurred during communication with userspace, or the device file
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700208 * was closed. The requester thread is woken up (if still waiting),
209 * the 'end' callback is called if given, else the reference to the
210 * request is released
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800211 *
Miklos Szeredid7133112006-04-10 22:54:55 -0700212 * Called with fc->lock, unlocks it
Miklos Szeredi334f4852005-09-09 13:10:27 -0700213 */
214static void request_end(struct fuse_conn *fc, struct fuse_req *req)
215{
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700216 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
217 req->end = NULL;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800218 list_del(&req->list);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700219 list_del(&req->intr_entry);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800220 req->state = FUSE_REQ_FINISHED;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700221 if (req->background) {
222 if (fc->num_background == FUSE_MAX_BACKGROUND) {
223 fc->blocked = 0;
224 wake_up_all(&fc->blocked_waitq);
225 }
226 fc->num_background--;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700227 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700228 spin_unlock(&fc->lock);
229 dput(req->dentry);
230 mntput(req->vfsmount);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700231 if (req->file)
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700232 fput(req->file);
233 wake_up(&req->waitq);
234 if (end)
235 end(fc, req);
236 else
237 fuse_put_request(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700238}
239
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700240static void wait_answer_interruptible(struct fuse_conn *fc,
241 struct fuse_req *req)
242{
243 if (signal_pending(current))
244 return;
245
246 spin_unlock(&fc->lock);
247 wait_event_interruptible(req->waitq, req->state == FUSE_REQ_FINISHED);
248 spin_lock(&fc->lock);
249}
250
251static void queue_interrupt(struct fuse_conn *fc, struct fuse_req *req)
252{
253 list_add_tail(&req->intr_entry, &fc->interrupts);
254 wake_up(&fc->waitq);
255 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
256}
257
Miklos Szeredid7133112006-04-10 22:54:55 -0700258/* Called with fc->lock held. Releases, and then reacquires it. */
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700259static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700260{
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700261 if (!fc->no_interrupt) {
262 /* Any signal may interrupt this */
263 wait_answer_interruptible(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700264
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700265 if (req->aborted)
266 goto aborted;
267 if (req->state == FUSE_REQ_FINISHED)
268 return;
269
270 req->interrupted = 1;
271 if (req->state == FUSE_REQ_SENT)
272 queue_interrupt(fc, req);
273 }
274
275 if (req->force) {
276 spin_unlock(&fc->lock);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700277 wait_event(req->waitq, req->state == FUSE_REQ_FINISHED);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700278 spin_lock(&fc->lock);
279 } else {
280 sigset_t oldset;
281
282 /* Only fatal signals may interrupt this */
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700283 block_sigs(&oldset);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700284 wait_answer_interruptible(fc, req);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700285 restore_sigs(&oldset);
286 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700287
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700288 if (req->aborted)
289 goto aborted;
290 if (req->state == FUSE_REQ_FINISHED)
291 return;
292
293 req->out.h.error = -EINTR;
294 req->aborted = 1;
295
296 aborted:
Miklos Szeredi334f4852005-09-09 13:10:27 -0700297 if (req->locked) {
298 /* This is uninterruptible sleep, because data is
299 being copied to/from the buffers of req. During
300 locked state, there mustn't be any filesystem
301 operation (e.g. page fault), since that could lead
302 to deadlock */
Miklos Szeredid7133112006-04-10 22:54:55 -0700303 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700304 wait_event(req->waitq, !req->locked);
Miklos Szeredid7133112006-04-10 22:54:55 -0700305 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700306 }
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800307 if (req->state == FUSE_REQ_PENDING) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700308 list_del(&req->list);
309 __fuse_put_request(req);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700310 } else if (req->state == FUSE_REQ_SENT) {
311 spin_unlock(&fc->lock);
312 wait_event(req->waitq, req->state == FUSE_REQ_FINISHED);
313 spin_lock(&fc->lock);
314 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700315}
316
317static unsigned len_args(unsigned numargs, struct fuse_arg *args)
318{
319 unsigned nbytes = 0;
320 unsigned i;
321
322 for (i = 0; i < numargs; i++)
323 nbytes += args[i].size;
324
325 return nbytes;
326}
327
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700328static u64 fuse_get_unique(struct fuse_conn *fc)
329 {
330 fc->reqctr++;
331 /* zero is special */
332 if (fc->reqctr == 0)
333 fc->reqctr = 1;
334
335 return fc->reqctr;
336}
337
Miklos Szeredi334f4852005-09-09 13:10:27 -0700338static void queue_request(struct fuse_conn *fc, struct fuse_req *req)
339{
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700340 req->in.h.unique = fuse_get_unique(fc);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700341 req->in.h.len = sizeof(struct fuse_in_header) +
342 len_args(req->in.numargs, (struct fuse_arg *) req->in.args);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700343 list_add_tail(&req->list, &fc->pending);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800344 req->state = FUSE_REQ_PENDING;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200345 if (!req->waiting) {
346 req->waiting = 1;
347 atomic_inc(&fc->num_waiting);
348 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700349 wake_up(&fc->waitq);
Jeff Dike385a17b2006-04-10 22:54:52 -0700350 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700351}
352
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700353void request_send(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700354{
355 req->isreply = 1;
Miklos Szeredid7133112006-04-10 22:54:55 -0700356 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700357 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700358 req->out.h.error = -ENOTCONN;
359 else if (fc->conn_error)
360 req->out.h.error = -ECONNREFUSED;
361 else {
362 queue_request(fc, req);
363 /* acquire extra reference, since request is still needed
364 after request_end() */
365 __fuse_get_request(req);
366
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700367 request_wait_answer(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700368 }
Miklos Szeredid7133112006-04-10 22:54:55 -0700369 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700370}
371
Miklos Szeredi334f4852005-09-09 13:10:27 -0700372static void request_send_nowait(struct fuse_conn *fc, struct fuse_req *req)
373{
Miklos Szeredid7133112006-04-10 22:54:55 -0700374 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700375 if (fc->connected) {
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700376 req->background = 1;
377 fc->num_background++;
378 if (fc->num_background == FUSE_MAX_BACKGROUND)
379 fc->blocked = 1;
380
Miklos Szeredi334f4852005-09-09 13:10:27 -0700381 queue_request(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700382 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700383 } else {
384 req->out.h.error = -ENOTCONN;
385 request_end(fc, req);
386 }
387}
388
389void request_send_noreply(struct fuse_conn *fc, struct fuse_req *req)
390{
391 req->isreply = 0;
392 request_send_nowait(fc, req);
393}
394
395void request_send_background(struct fuse_conn *fc, struct fuse_req *req)
396{
397 req->isreply = 1;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700398 request_send_nowait(fc, req);
399}
400
Miklos Szeredi334f4852005-09-09 13:10:27 -0700401/*
402 * Lock the request. Up to the next unlock_request() there mustn't be
403 * anything that could cause a page-fault. If the request was already
Miklos Szeredif9a28422006-06-25 05:48:53 -0700404 * aborted bail out.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700405 */
Miklos Szeredid7133112006-04-10 22:54:55 -0700406static int lock_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700407{
408 int err = 0;
409 if (req) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700410 spin_lock(&fc->lock);
Miklos Szeredif9a28422006-06-25 05:48:53 -0700411 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700412 err = -ENOENT;
413 else
414 req->locked = 1;
Miklos Szeredid7133112006-04-10 22:54:55 -0700415 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700416 }
417 return err;
418}
419
420/*
Miklos Szeredif9a28422006-06-25 05:48:53 -0700421 * Unlock request. If it was aborted during being locked, the
Miklos Szeredi334f4852005-09-09 13:10:27 -0700422 * requester thread is currently waiting for it to be unlocked, so
423 * wake it up.
424 */
Miklos Szeredid7133112006-04-10 22:54:55 -0700425static void unlock_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700426{
427 if (req) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700428 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700429 req->locked = 0;
Miklos Szeredif9a28422006-06-25 05:48:53 -0700430 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700431 wake_up(&req->waitq);
Miklos Szeredid7133112006-04-10 22:54:55 -0700432 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700433 }
434}
435
436struct fuse_copy_state {
Miklos Szeredid7133112006-04-10 22:54:55 -0700437 struct fuse_conn *fc;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700438 int write;
439 struct fuse_req *req;
440 const struct iovec *iov;
441 unsigned long nr_segs;
442 unsigned long seglen;
443 unsigned long addr;
444 struct page *pg;
445 void *mapaddr;
446 void *buf;
447 unsigned len;
448};
449
Miklos Szeredid7133112006-04-10 22:54:55 -0700450static void fuse_copy_init(struct fuse_copy_state *cs, struct fuse_conn *fc,
451 int write, struct fuse_req *req,
452 const struct iovec *iov, unsigned long nr_segs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700453{
454 memset(cs, 0, sizeof(*cs));
Miklos Szeredid7133112006-04-10 22:54:55 -0700455 cs->fc = fc;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700456 cs->write = write;
457 cs->req = req;
458 cs->iov = iov;
459 cs->nr_segs = nr_segs;
460}
461
462/* Unmap and put previous page of userspace buffer */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800463static void fuse_copy_finish(struct fuse_copy_state *cs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700464{
465 if (cs->mapaddr) {
466 kunmap_atomic(cs->mapaddr, KM_USER0);
467 if (cs->write) {
468 flush_dcache_page(cs->pg);
469 set_page_dirty_lock(cs->pg);
470 }
471 put_page(cs->pg);
472 cs->mapaddr = NULL;
473 }
474}
475
476/*
477 * Get another pagefull of userspace buffer, and map it to kernel
478 * address space, and lock request
479 */
480static int fuse_copy_fill(struct fuse_copy_state *cs)
481{
482 unsigned long offset;
483 int err;
484
Miklos Szeredid7133112006-04-10 22:54:55 -0700485 unlock_request(cs->fc, cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700486 fuse_copy_finish(cs);
487 if (!cs->seglen) {
488 BUG_ON(!cs->nr_segs);
489 cs->seglen = cs->iov[0].iov_len;
490 cs->addr = (unsigned long) cs->iov[0].iov_base;
491 cs->iov ++;
492 cs->nr_segs --;
493 }
494 down_read(&current->mm->mmap_sem);
495 err = get_user_pages(current, current->mm, cs->addr, 1, cs->write, 0,
496 &cs->pg, NULL);
497 up_read(&current->mm->mmap_sem);
498 if (err < 0)
499 return err;
500 BUG_ON(err != 1);
501 offset = cs->addr % PAGE_SIZE;
502 cs->mapaddr = kmap_atomic(cs->pg, KM_USER0);
503 cs->buf = cs->mapaddr + offset;
504 cs->len = min(PAGE_SIZE - offset, cs->seglen);
505 cs->seglen -= cs->len;
506 cs->addr += cs->len;
507
Miklos Szeredid7133112006-04-10 22:54:55 -0700508 return lock_request(cs->fc, cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700509}
510
511/* Do as much copy to/from userspace buffer as we can */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800512static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700513{
514 unsigned ncpy = min(*size, cs->len);
515 if (val) {
516 if (cs->write)
517 memcpy(cs->buf, *val, ncpy);
518 else
519 memcpy(*val, cs->buf, ncpy);
520 *val += ncpy;
521 }
522 *size -= ncpy;
523 cs->len -= ncpy;
524 cs->buf += ncpy;
525 return ncpy;
526}
527
528/*
529 * Copy a page in the request to/from the userspace buffer. Must be
530 * done atomically
531 */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800532static int fuse_copy_page(struct fuse_copy_state *cs, struct page *page,
533 unsigned offset, unsigned count, int zeroing)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700534{
535 if (page && zeroing && count < PAGE_SIZE) {
536 void *mapaddr = kmap_atomic(page, KM_USER1);
537 memset(mapaddr, 0, PAGE_SIZE);
538 kunmap_atomic(mapaddr, KM_USER1);
539 }
540 while (count) {
541 int err;
542 if (!cs->len && (err = fuse_copy_fill(cs)))
543 return err;
544 if (page) {
545 void *mapaddr = kmap_atomic(page, KM_USER1);
546 void *buf = mapaddr + offset;
547 offset += fuse_copy_do(cs, &buf, &count);
548 kunmap_atomic(mapaddr, KM_USER1);
549 } else
550 offset += fuse_copy_do(cs, NULL, &count);
551 }
552 if (page && !cs->write)
553 flush_dcache_page(page);
554 return 0;
555}
556
557/* Copy pages in the request to/from userspace buffer */
558static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
559 int zeroing)
560{
561 unsigned i;
562 struct fuse_req *req = cs->req;
563 unsigned offset = req->page_offset;
564 unsigned count = min(nbytes, (unsigned) PAGE_SIZE - offset);
565
566 for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
567 struct page *page = req->pages[i];
568 int err = fuse_copy_page(cs, page, offset, count, zeroing);
569 if (err)
570 return err;
571
572 nbytes -= count;
573 count = min(nbytes, (unsigned) PAGE_SIZE);
574 offset = 0;
575 }
576 return 0;
577}
578
579/* Copy a single argument in the request to/from userspace buffer */
580static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
581{
582 while (size) {
583 int err;
584 if (!cs->len && (err = fuse_copy_fill(cs)))
585 return err;
586 fuse_copy_do(cs, &val, &size);
587 }
588 return 0;
589}
590
591/* Copy request arguments to/from userspace buffer */
592static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
593 unsigned argpages, struct fuse_arg *args,
594 int zeroing)
595{
596 int err = 0;
597 unsigned i;
598
599 for (i = 0; !err && i < numargs; i++) {
600 struct fuse_arg *arg = &args[i];
601 if (i == numargs - 1 && argpages)
602 err = fuse_copy_pages(cs, arg->size, zeroing);
603 else
604 err = fuse_copy_one(cs, arg->value, arg->size);
605 }
606 return err;
607}
608
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700609static int request_pending(struct fuse_conn *fc)
610{
611 return !list_empty(&fc->pending) || !list_empty(&fc->interrupts);
612}
613
Miklos Szeredi334f4852005-09-09 13:10:27 -0700614/* Wait until a request is available on the pending list */
615static void request_wait(struct fuse_conn *fc)
616{
617 DECLARE_WAITQUEUE(wait, current);
618
619 add_wait_queue_exclusive(&fc->waitq, &wait);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700620 while (fc->connected && !request_pending(fc)) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700621 set_current_state(TASK_INTERRUPTIBLE);
622 if (signal_pending(current))
623 break;
624
Miklos Szeredid7133112006-04-10 22:54:55 -0700625 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700626 schedule();
Miklos Szeredid7133112006-04-10 22:54:55 -0700627 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700628 }
629 set_current_state(TASK_RUNNING);
630 remove_wait_queue(&fc->waitq, &wait);
631}
632
633/*
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700634 * Transfer an interrupt request to userspace
635 *
636 * Unlike other requests this is assembled on demand, without a need
637 * to allocate a separate fuse_req structure.
638 *
639 * Called with fc->lock held, releases it
640 */
641static int fuse_read_interrupt(struct fuse_conn *fc, struct fuse_req *req,
642 const struct iovec *iov, unsigned long nr_segs)
643{
644 struct fuse_copy_state cs;
645 struct fuse_in_header ih;
646 struct fuse_interrupt_in arg;
647 unsigned reqsize = sizeof(ih) + sizeof(arg);
648 int err;
649
650 list_del_init(&req->intr_entry);
651 req->intr_unique = fuse_get_unique(fc);
652 memset(&ih, 0, sizeof(ih));
653 memset(&arg, 0, sizeof(arg));
654 ih.len = reqsize;
655 ih.opcode = FUSE_INTERRUPT;
656 ih.unique = req->intr_unique;
657 arg.unique = req->in.h.unique;
658
659 spin_unlock(&fc->lock);
660 if (iov_length(iov, nr_segs) < reqsize)
661 return -EINVAL;
662
663 fuse_copy_init(&cs, fc, 1, NULL, iov, nr_segs);
664 err = fuse_copy_one(&cs, &ih, sizeof(ih));
665 if (!err)
666 err = fuse_copy_one(&cs, &arg, sizeof(arg));
667 fuse_copy_finish(&cs);
668
669 return err ? err : reqsize;
670}
671
672/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700673 * Read a single request into the userspace filesystem's buffer. This
674 * function waits until a request is available, then removes it from
675 * the pending list and copies request data to userspace buffer. If
Miklos Szeredif9a28422006-06-25 05:48:53 -0700676 * no reply is needed (FORGET) or request has been aborted or there
677 * was an error during the copying then it's finished by calling
Miklos Szeredi334f4852005-09-09 13:10:27 -0700678 * request_end(). Otherwise add it to the processing list, and set
679 * the 'sent' flag.
680 */
681static ssize_t fuse_dev_readv(struct file *file, const struct iovec *iov,
682 unsigned long nr_segs, loff_t *off)
683{
684 int err;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700685 struct fuse_req *req;
686 struct fuse_in *in;
687 struct fuse_copy_state cs;
688 unsigned reqsize;
Miklos Szeredi0720b312006-04-10 22:54:55 -0700689 struct fuse_conn *fc = fuse_get_conn(file);
690 if (!fc)
691 return -EPERM;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700692
Miklos Szeredi1d3d7522006-01-06 00:19:40 -0800693 restart:
Miklos Szeredid7133112006-04-10 22:54:55 -0700694 spin_lock(&fc->lock);
Jeff Dikee5ac1d12006-04-10 22:54:53 -0700695 err = -EAGAIN;
696 if ((file->f_flags & O_NONBLOCK) && fc->connected &&
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700697 !request_pending(fc))
Jeff Dikee5ac1d12006-04-10 22:54:53 -0700698 goto err_unlock;
699
Miklos Szeredi334f4852005-09-09 13:10:27 -0700700 request_wait(fc);
701 err = -ENODEV;
Miklos Szeredi9ba7cbb2006-01-16 22:14:34 -0800702 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700703 goto err_unlock;
704 err = -ERESTARTSYS;
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700705 if (!request_pending(fc))
Miklos Szeredi334f4852005-09-09 13:10:27 -0700706 goto err_unlock;
707
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700708 if (!list_empty(&fc->interrupts)) {
709 req = list_entry(fc->interrupts.next, struct fuse_req,
710 intr_entry);
711 return fuse_read_interrupt(fc, req, iov, nr_segs);
712 }
713
Miklos Szeredi334f4852005-09-09 13:10:27 -0700714 req = list_entry(fc->pending.next, struct fuse_req, list);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800715 req->state = FUSE_REQ_READING;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800716 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700717
718 in = &req->in;
Miklos Szeredi1d3d7522006-01-06 00:19:40 -0800719 reqsize = in->h.len;
720 /* If request is too large, reply with an error and restart the read */
721 if (iov_length(iov, nr_segs) < reqsize) {
722 req->out.h.error = -EIO;
723 /* SETXATTR is special, since it may contain too large data */
724 if (in->h.opcode == FUSE_SETXATTR)
725 req->out.h.error = -E2BIG;
726 request_end(fc, req);
727 goto restart;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700728 }
Miklos Szeredid7133112006-04-10 22:54:55 -0700729 spin_unlock(&fc->lock);
730 fuse_copy_init(&cs, fc, 1, req, iov, nr_segs);
Miklos Szeredi1d3d7522006-01-06 00:19:40 -0800731 err = fuse_copy_one(&cs, &in->h, sizeof(in->h));
732 if (!err)
733 err = fuse_copy_args(&cs, in->numargs, in->argpages,
734 (struct fuse_arg *) in->args, 0);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700735 fuse_copy_finish(&cs);
Miklos Szeredid7133112006-04-10 22:54:55 -0700736 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700737 req->locked = 0;
Miklos Szeredif9a28422006-06-25 05:48:53 -0700738 if (!err && req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700739 err = -ENOENT;
740 if (err) {
Miklos Szeredif9a28422006-06-25 05:48:53 -0700741 if (!req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700742 req->out.h.error = -EIO;
743 request_end(fc, req);
744 return err;
745 }
746 if (!req->isreply)
747 request_end(fc, req);
748 else {
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800749 req->state = FUSE_REQ_SENT;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800750 list_move_tail(&req->list, &fc->processing);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700751 if (req->interrupted)
752 queue_interrupt(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700753 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700754 }
755 return reqsize;
756
757 err_unlock:
Miklos Szeredid7133112006-04-10 22:54:55 -0700758 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700759 return err;
760}
761
762static ssize_t fuse_dev_read(struct file *file, char __user *buf,
763 size_t nbytes, loff_t *off)
764{
765 struct iovec iov;
766 iov.iov_len = nbytes;
767 iov.iov_base = buf;
768 return fuse_dev_readv(file, &iov, 1, off);
769}
770
771/* Look up request on processing list by unique ID */
772static struct fuse_req *request_find(struct fuse_conn *fc, u64 unique)
773{
774 struct list_head *entry;
775
776 list_for_each(entry, &fc->processing) {
777 struct fuse_req *req;
778 req = list_entry(entry, struct fuse_req, list);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700779 if (req->in.h.unique == unique || req->intr_unique == unique)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700780 return req;
781 }
782 return NULL;
783}
784
785static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
786 unsigned nbytes)
787{
788 unsigned reqsize = sizeof(struct fuse_out_header);
789
790 if (out->h.error)
791 return nbytes != reqsize ? -EINVAL : 0;
792
793 reqsize += len_args(out->numargs, out->args);
794
795 if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
796 return -EINVAL;
797 else if (reqsize > nbytes) {
798 struct fuse_arg *lastarg = &out->args[out->numargs-1];
799 unsigned diffsize = reqsize - nbytes;
800 if (diffsize > lastarg->size)
801 return -EINVAL;
802 lastarg->size -= diffsize;
803 }
804 return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
805 out->page_zeroing);
806}
807
808/*
809 * Write a single reply to a request. First the header is copied from
810 * the write buffer. The request is then searched on the processing
811 * list by the unique ID found in the header. If found, then remove
812 * it from the list and copy the rest of the buffer to the request.
813 * The request is finished by calling request_end()
814 */
815static ssize_t fuse_dev_writev(struct file *file, const struct iovec *iov,
816 unsigned long nr_segs, loff_t *off)
817{
818 int err;
819 unsigned nbytes = iov_length(iov, nr_segs);
820 struct fuse_req *req;
821 struct fuse_out_header oh;
822 struct fuse_copy_state cs;
823 struct fuse_conn *fc = fuse_get_conn(file);
824 if (!fc)
Miklos Szeredia87046d2006-04-10 22:54:56 -0700825 return -EPERM;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700826
Miklos Szeredid7133112006-04-10 22:54:55 -0700827 fuse_copy_init(&cs, fc, 0, NULL, iov, nr_segs);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700828 if (nbytes < sizeof(struct fuse_out_header))
829 return -EINVAL;
830
831 err = fuse_copy_one(&cs, &oh, sizeof(oh));
832 if (err)
833 goto err_finish;
834 err = -EINVAL;
835 if (!oh.unique || oh.error <= -1000 || oh.error > 0 ||
836 oh.len != nbytes)
837 goto err_finish;
838
Miklos Szeredid7133112006-04-10 22:54:55 -0700839 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800840 err = -ENOENT;
841 if (!fc->connected)
842 goto err_unlock;
843
Miklos Szeredi334f4852005-09-09 13:10:27 -0700844 req = request_find(fc, oh.unique);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700845 if (!req)
846 goto err_unlock;
847
Miklos Szeredif9a28422006-06-25 05:48:53 -0700848 if (req->aborted) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700849 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700850 fuse_copy_finish(&cs);
Miklos Szeredid7133112006-04-10 22:54:55 -0700851 spin_lock(&fc->lock);
Miklos Szeredi222f1d62006-01-16 22:14:25 -0800852 request_end(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700853 return -ENOENT;
854 }
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700855 /* Is it an interrupt reply? */
856 if (req->intr_unique == oh.unique) {
857 err = -EINVAL;
858 if (nbytes != sizeof(struct fuse_out_header))
859 goto err_unlock;
860
861 if (oh.error == -ENOSYS)
862 fc->no_interrupt = 1;
863 else if (oh.error == -EAGAIN)
864 queue_interrupt(fc, req);
865
866 spin_unlock(&fc->lock);
867 fuse_copy_finish(&cs);
868 return nbytes;
869 }
870
871 req->state = FUSE_REQ_WRITING;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800872 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700873 req->out.h = oh;
874 req->locked = 1;
875 cs.req = req;
Miklos Szeredid7133112006-04-10 22:54:55 -0700876 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700877
878 err = copy_out_args(&cs, &req->out, nbytes);
879 fuse_copy_finish(&cs);
880
Miklos Szeredid7133112006-04-10 22:54:55 -0700881 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700882 req->locked = 0;
883 if (!err) {
Miklos Szeredif9a28422006-06-25 05:48:53 -0700884 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700885 err = -ENOENT;
Miklos Szeredif9a28422006-06-25 05:48:53 -0700886 } else if (!req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700887 req->out.h.error = -EIO;
888 request_end(fc, req);
889
890 return err ? err : nbytes;
891
892 err_unlock:
Miklos Szeredid7133112006-04-10 22:54:55 -0700893 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700894 err_finish:
895 fuse_copy_finish(&cs);
896 return err;
897}
898
899static ssize_t fuse_dev_write(struct file *file, const char __user *buf,
900 size_t nbytes, loff_t *off)
901{
902 struct iovec iov;
903 iov.iov_len = nbytes;
904 iov.iov_base = (char __user *) buf;
905 return fuse_dev_writev(file, &iov, 1, off);
906}
907
908static unsigned fuse_dev_poll(struct file *file, poll_table *wait)
909{
Miklos Szeredi334f4852005-09-09 13:10:27 -0700910 unsigned mask = POLLOUT | POLLWRNORM;
Miklos Szeredi7025d9a2006-04-10 22:54:50 -0700911 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700912 if (!fc)
Miklos Szeredi7025d9a2006-04-10 22:54:50 -0700913 return POLLERR;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700914
915 poll_wait(file, &fc->waitq, wait);
916
Miklos Szeredid7133112006-04-10 22:54:55 -0700917 spin_lock(&fc->lock);
Miklos Szeredi7025d9a2006-04-10 22:54:50 -0700918 if (!fc->connected)
919 mask = POLLERR;
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700920 else if (request_pending(fc))
Miklos Szeredi7025d9a2006-04-10 22:54:50 -0700921 mask |= POLLIN | POLLRDNORM;
Miklos Szeredid7133112006-04-10 22:54:55 -0700922 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700923
924 return mask;
925}
926
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800927/*
928 * Abort all requests on the given list (pending or processing)
929 *
Miklos Szeredid7133112006-04-10 22:54:55 -0700930 * This function releases and reacquires fc->lock
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800931 */
Miklos Szeredi334f4852005-09-09 13:10:27 -0700932static void end_requests(struct fuse_conn *fc, struct list_head *head)
933{
934 while (!list_empty(head)) {
935 struct fuse_req *req;
936 req = list_entry(head->next, struct fuse_req, list);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700937 req->out.h.error = -ECONNABORTED;
938 request_end(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700939 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700940 }
941}
942
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800943/*
944 * Abort requests under I/O
945 *
Miklos Szeredif9a28422006-06-25 05:48:53 -0700946 * The requests are set to aborted and finished, and the request
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800947 * waiter is woken up. This will make request_wait_answer() wait
948 * until the request is unlocked and then return.
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800949 *
950 * If the request is asynchronous, then the end function needs to be
951 * called after waiting for the request to be unlocked (if it was
952 * locked).
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800953 */
954static void end_io_requests(struct fuse_conn *fc)
955{
956 while (!list_empty(&fc->io)) {
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800957 struct fuse_req *req =
958 list_entry(fc->io.next, struct fuse_req, list);
959 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
960
Miklos Szeredif9a28422006-06-25 05:48:53 -0700961 req->aborted = 1;
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800962 req->out.h.error = -ECONNABORTED;
963 req->state = FUSE_REQ_FINISHED;
964 list_del_init(&req->list);
965 wake_up(&req->waitq);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800966 if (end) {
967 req->end = NULL;
968 /* The end function will consume this reference */
969 __fuse_get_request(req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700970 spin_unlock(&fc->lock);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800971 wait_event(req->waitq, !req->locked);
972 end(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700973 spin_lock(&fc->lock);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800974 }
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800975 }
976}
977
978/*
979 * Abort all requests.
980 *
981 * Emergency exit in case of a malicious or accidental deadlock, or
982 * just a hung filesystem.
983 *
984 * The same effect is usually achievable through killing the
985 * filesystem daemon and all users of the filesystem. The exception
986 * is the combination of an asynchronous request and the tricky
987 * deadlock (see Documentation/filesystems/fuse.txt).
988 *
989 * During the aborting, progression of requests from the pending and
990 * processing lists onto the io list, and progression of new requests
991 * onto the pending list is prevented by req->connected being false.
992 *
993 * Progression of requests under I/O to the processing list is
Miklos Szeredif9a28422006-06-25 05:48:53 -0700994 * prevented by the req->aborted flag being true for these requests.
995 * For this reason requests on the io list must be aborted first.
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800996 */
997void fuse_abort_conn(struct fuse_conn *fc)
998{
Miklos Szeredid7133112006-04-10 22:54:55 -0700999 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001000 if (fc->connected) {
1001 fc->connected = 0;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -07001002 fc->blocked = 0;
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001003 end_io_requests(fc);
1004 end_requests(fc, &fc->pending);
1005 end_requests(fc, &fc->processing);
1006 wake_up_all(&fc->waitq);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -07001007 wake_up_all(&fc->blocked_waitq);
Jeff Dike385a17b2006-04-10 22:54:52 -07001008 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001009 }
Miklos Szeredid7133112006-04-10 22:54:55 -07001010 spin_unlock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001011}
1012
Miklos Szeredi334f4852005-09-09 13:10:27 -07001013static int fuse_dev_release(struct inode *inode, struct file *file)
1014{
Miklos Szeredi0720b312006-04-10 22:54:55 -07001015 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001016 if (fc) {
Miklos Szeredid7133112006-04-10 22:54:55 -07001017 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -07001018 fc->connected = 0;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001019 end_requests(fc, &fc->pending);
1020 end_requests(fc, &fc->processing);
Miklos Szeredid7133112006-04-10 22:54:55 -07001021 spin_unlock(&fc->lock);
Jeff Dike385a17b2006-04-10 22:54:52 -07001022 fasync_helper(-1, file, 0, &fc->fasync);
Miklos Szeredibafa9652006-06-25 05:48:51 -07001023 fuse_conn_put(fc);
Jeff Dike385a17b2006-04-10 22:54:52 -07001024 }
Miklos Szeredif543f252006-01-16 22:14:35 -08001025
Miklos Szeredi334f4852005-09-09 13:10:27 -07001026 return 0;
1027}
1028
Jeff Dike385a17b2006-04-10 22:54:52 -07001029static int fuse_dev_fasync(int fd, struct file *file, int on)
1030{
1031 struct fuse_conn *fc = fuse_get_conn(file);
1032 if (!fc)
Miklos Szeredia87046d2006-04-10 22:54:56 -07001033 return -EPERM;
Jeff Dike385a17b2006-04-10 22:54:52 -07001034
1035 /* No locking - fasync_helper does its own locking */
1036 return fasync_helper(fd, file, on, &fc->fasync);
1037}
1038
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001039const struct file_operations fuse_dev_operations = {
Miklos Szeredi334f4852005-09-09 13:10:27 -07001040 .owner = THIS_MODULE,
1041 .llseek = no_llseek,
1042 .read = fuse_dev_read,
1043 .readv = fuse_dev_readv,
1044 .write = fuse_dev_write,
1045 .writev = fuse_dev_writev,
1046 .poll = fuse_dev_poll,
1047 .release = fuse_dev_release,
Jeff Dike385a17b2006-04-10 22:54:52 -07001048 .fasync = fuse_dev_fasync,
Miklos Szeredi334f4852005-09-09 13:10:27 -07001049};
1050
1051static struct miscdevice fuse_miscdevice = {
1052 .minor = FUSE_MINOR,
1053 .name = "fuse",
1054 .fops = &fuse_dev_operations,
1055};
1056
1057int __init fuse_dev_init(void)
1058{
1059 int err = -ENOMEM;
1060 fuse_req_cachep = kmem_cache_create("fuse_request",
1061 sizeof(struct fuse_req),
1062 0, 0, NULL, NULL);
1063 if (!fuse_req_cachep)
1064 goto out;
1065
1066 err = misc_register(&fuse_miscdevice);
1067 if (err)
1068 goto out_cache_clean;
1069
1070 return 0;
1071
1072 out_cache_clean:
1073 kmem_cache_destroy(fuse_req_cachep);
1074 out:
1075 return err;
1076}
1077
1078void fuse_dev_cleanup(void)
1079{
1080 misc_deregister(&fuse_miscdevice);
1081 kmem_cache_destroy(fuse_req_cachep);
1082}