blob: 85a23bb524f71119a1057de65653c2d7e9155d03 [file] [log] [blame]
Miklos Szeredi334f4852005-09-09 13:10:27 -07001/*
2 FUSE: Filesystem in Userspace
Miklos Szeredi1729a162008-11-26 12:03:54 +01003 Copyright (C) 2001-2008 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
Christoph Lametere18b8902006-12-06 20:33:20 -080022static struct kmem_cache *fuse_req_cachep;
Miklos Szeredi334f4852005-09-09 13:10:27 -070023
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{
Christoph Lametere94b1762006-12-06 20:33:17 -080044 struct fuse_req *req = kmem_cache_alloc(fuse_req_cachep, GFP_KERNEL);
Miklos Szeredi334f4852005-09-09 13:10:27 -070045 if (req)
46 fuse_request_init(req);
47 return req;
48}
49
Miklos Szeredi3be5a522008-04-30 00:54:41 -070050struct fuse_req *fuse_request_alloc_nofs(void)
51{
52 struct fuse_req *req = kmem_cache_alloc(fuse_req_cachep, GFP_NOFS);
53 if (req)
54 fuse_request_init(req);
55 return req;
56}
57
Miklos Szeredi334f4852005-09-09 13:10:27 -070058void fuse_request_free(struct fuse_req *req)
59{
60 kmem_cache_free(fuse_req_cachep, req);
61}
62
Miklos Szeredi8bfc0162006-01-16 22:14:28 -080063static void block_sigs(sigset_t *oldset)
Miklos Szeredi334f4852005-09-09 13:10:27 -070064{
65 sigset_t mask;
66
67 siginitsetinv(&mask, sigmask(SIGKILL));
68 sigprocmask(SIG_BLOCK, &mask, oldset);
69}
70
Miklos Szeredi8bfc0162006-01-16 22:14:28 -080071static void restore_sigs(sigset_t *oldset)
Miklos Szeredi334f4852005-09-09 13:10:27 -070072{
73 sigprocmask(SIG_SETMASK, oldset, NULL);
74}
75
Miklos Szeredi334f4852005-09-09 13:10:27 -070076static void __fuse_get_request(struct fuse_req *req)
77{
78 atomic_inc(&req->count);
79}
80
81/* Must be called with > 1 refcount */
82static void __fuse_put_request(struct fuse_req *req)
83{
84 BUG_ON(atomic_read(&req->count) < 2);
85 atomic_dec(&req->count);
86}
87
Miklos Szeredi33649c92006-06-25 05:48:52 -070088static void fuse_req_init_context(struct fuse_req *req)
89{
90 req->in.h.uid = current->fsuid;
91 req->in.h.gid = current->fsgid;
92 req->in.h.pid = current->pid;
93}
94
Miklos Szeredice1d5a42006-04-10 22:54:58 -070095struct fuse_req *fuse_get_req(struct fuse_conn *fc)
Miklos Szeredi334f4852005-09-09 13:10:27 -070096{
Miklos Szeredi08a53cd2006-04-10 22:54:59 -070097 struct fuse_req *req;
98 sigset_t oldset;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +020099 int intr;
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700100 int err;
101
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200102 atomic_inc(&fc->num_waiting);
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700103 block_sigs(&oldset);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200104 intr = wait_event_interruptible(fc->blocked_waitq, !fc->blocked);
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700105 restore_sigs(&oldset);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200106 err = -EINTR;
107 if (intr)
108 goto out;
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700109
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700110 err = -ENOTCONN;
111 if (!fc->connected)
112 goto out;
113
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700114 req = fuse_request_alloc();
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200115 err = -ENOMEM;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700116 if (!req)
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200117 goto out;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700118
Miklos Szeredi33649c92006-06-25 05:48:52 -0700119 fuse_req_init_context(req);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200120 req->waiting = 1;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700121 return req;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200122
123 out:
124 atomic_dec(&fc->num_waiting);
125 return ERR_PTR(err);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700126}
127
Miklos Szeredi33649c92006-06-25 05:48:52 -0700128/*
129 * Return request in fuse_file->reserved_req. However that may
130 * currently be in use. If that is the case, wait for it to become
131 * available.
132 */
133static struct fuse_req *get_reserved_req(struct fuse_conn *fc,
134 struct file *file)
135{
136 struct fuse_req *req = NULL;
137 struct fuse_file *ff = file->private_data;
138
139 do {
Miklos Szeredide5e3de2007-10-16 23:31:00 -0700140 wait_event(fc->reserved_req_waitq, ff->reserved_req);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700141 spin_lock(&fc->lock);
142 if (ff->reserved_req) {
143 req = ff->reserved_req;
144 ff->reserved_req = NULL;
145 get_file(file);
146 req->stolen_file = file;
147 }
148 spin_unlock(&fc->lock);
149 } while (!req);
150
151 return req;
152}
153
154/*
155 * Put stolen request back into fuse_file->reserved_req
156 */
157static void put_reserved_req(struct fuse_conn *fc, struct fuse_req *req)
158{
159 struct file *file = req->stolen_file;
160 struct fuse_file *ff = file->private_data;
161
162 spin_lock(&fc->lock);
163 fuse_request_init(req);
164 BUG_ON(ff->reserved_req);
165 ff->reserved_req = req;
Miklos Szeredide5e3de2007-10-16 23:31:00 -0700166 wake_up_all(&fc->reserved_req_waitq);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700167 spin_unlock(&fc->lock);
168 fput(file);
169}
170
171/*
172 * Gets a requests for a file operation, always succeeds
173 *
174 * This is used for sending the FLUSH request, which must get to
175 * userspace, due to POSIX locks which may need to be unlocked.
176 *
177 * If allocation fails due to OOM, use the reserved request in
178 * fuse_file.
179 *
180 * This is very unlikely to deadlock accidentally, since the
181 * filesystem should not have it's own file open. If deadlock is
182 * intentional, it can still be broken by "aborting" the filesystem.
183 */
184struct fuse_req *fuse_get_req_nofail(struct fuse_conn *fc, struct file *file)
185{
186 struct fuse_req *req;
187
188 atomic_inc(&fc->num_waiting);
189 wait_event(fc->blocked_waitq, !fc->blocked);
190 req = fuse_request_alloc();
191 if (!req)
192 req = get_reserved_req(fc, file);
193
194 fuse_req_init_context(req);
195 req->waiting = 1;
196 return req;
197}
198
Miklos Szeredi334f4852005-09-09 13:10:27 -0700199void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req)
200{
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800201 if (atomic_dec_and_test(&req->count)) {
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200202 if (req->waiting)
203 atomic_dec(&fc->num_waiting);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700204
205 if (req->stolen_file)
206 put_reserved_req(fc, req);
207 else
208 fuse_request_free(req);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800209 }
210}
211
Miklos Szeredid12def12008-02-06 01:38:39 -0800212static unsigned len_args(unsigned numargs, struct fuse_arg *args)
213{
214 unsigned nbytes = 0;
215 unsigned i;
216
217 for (i = 0; i < numargs; i++)
218 nbytes += args[i].size;
219
220 return nbytes;
221}
222
223static u64 fuse_get_unique(struct fuse_conn *fc)
224{
225 fc->reqctr++;
226 /* zero is special */
227 if (fc->reqctr == 0)
228 fc->reqctr = 1;
229
230 return fc->reqctr;
231}
232
233static void queue_request(struct fuse_conn *fc, struct fuse_req *req)
234{
235 req->in.h.unique = fuse_get_unique(fc);
236 req->in.h.len = sizeof(struct fuse_in_header) +
237 len_args(req->in.numargs, (struct fuse_arg *) req->in.args);
238 list_add_tail(&req->list, &fc->pending);
239 req->state = FUSE_REQ_PENDING;
240 if (!req->waiting) {
241 req->waiting = 1;
242 atomic_inc(&fc->num_waiting);
243 }
244 wake_up(&fc->waitq);
245 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
246}
247
248static void flush_bg_queue(struct fuse_conn *fc)
249{
250 while (fc->active_background < FUSE_MAX_BACKGROUND &&
251 !list_empty(&fc->bg_queue)) {
252 struct fuse_req *req;
253
254 req = list_entry(fc->bg_queue.next, struct fuse_req, list);
255 list_del(&req->list);
256 fc->active_background++;
257 queue_request(fc, req);
258 }
259}
260
Miklos Szeredi6dbbcb12006-04-26 10:49:06 +0200261/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700262 * This function is called when a request is finished. Either a reply
Miklos Szeredif9a28422006-06-25 05:48:53 -0700263 * has arrived or it was aborted (and not yet sent) or some error
Miklos Szeredif43b1552006-01-16 22:14:26 -0800264 * occurred during communication with userspace, or the device file
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700265 * was closed. The requester thread is woken up (if still waiting),
266 * the 'end' callback is called if given, else the reference to the
267 * request is released
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800268 *
Miklos Szeredid7133112006-04-10 22:54:55 -0700269 * Called with fc->lock, unlocks it
Miklos Szeredi334f4852005-09-09 13:10:27 -0700270 */
271static void request_end(struct fuse_conn *fc, struct fuse_req *req)
Josh Triplett105f4d72006-09-29 01:59:25 -0700272 __releases(fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700273{
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700274 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
275 req->end = NULL;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800276 list_del(&req->list);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700277 list_del(&req->intr_entry);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800278 req->state = FUSE_REQ_FINISHED;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700279 if (req->background) {
280 if (fc->num_background == FUSE_MAX_BACKGROUND) {
281 fc->blocked = 0;
282 wake_up_all(&fc->blocked_waitq);
283 }
Miklos Szeredif92b99b2007-10-16 23:30:59 -0700284 if (fc->num_background == FUSE_CONGESTION_THRESHOLD) {
285 clear_bdi_congested(&fc->bdi, READ);
286 clear_bdi_congested(&fc->bdi, WRITE);
287 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700288 fc->num_background--;
Miklos Szeredid12def12008-02-06 01:38:39 -0800289 fc->active_background--;
290 flush_bg_queue(fc);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700291 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700292 spin_unlock(&fc->lock);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700293 wake_up(&req->waitq);
294 if (end)
295 end(fc, req);
296 else
297 fuse_put_request(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700298}
299
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700300static void wait_answer_interruptible(struct fuse_conn *fc,
301 struct fuse_req *req)
Miklos Szeredi4dbf9302008-04-30 00:54:45 -0700302 __releases(fc->lock) __acquires(fc->lock)
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700303{
304 if (signal_pending(current))
305 return;
306
307 spin_unlock(&fc->lock);
308 wait_event_interruptible(req->waitq, req->state == FUSE_REQ_FINISHED);
309 spin_lock(&fc->lock);
310}
311
312static void queue_interrupt(struct fuse_conn *fc, struct fuse_req *req)
313{
314 list_add_tail(&req->intr_entry, &fc->interrupts);
315 wake_up(&fc->waitq);
316 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
317}
318
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700319static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi4dbf9302008-04-30 00:54:45 -0700320 __releases(fc->lock) __acquires(fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700321{
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700322 if (!fc->no_interrupt) {
323 /* Any signal may interrupt this */
324 wait_answer_interruptible(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700325
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700326 if (req->aborted)
327 goto aborted;
328 if (req->state == FUSE_REQ_FINISHED)
329 return;
330
331 req->interrupted = 1;
332 if (req->state == FUSE_REQ_SENT)
333 queue_interrupt(fc, req);
334 }
335
Miklos Szeredia131de02007-10-16 23:31:04 -0700336 if (!req->force) {
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700337 sigset_t oldset;
338
339 /* Only fatal signals may interrupt this */
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700340 block_sigs(&oldset);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700341 wait_answer_interruptible(fc, req);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700342 restore_sigs(&oldset);
Miklos Szeredia131de02007-10-16 23:31:04 -0700343
344 if (req->aborted)
345 goto aborted;
346 if (req->state == FUSE_REQ_FINISHED)
347 return;
348
349 /* Request is not yet in userspace, bail out */
350 if (req->state == FUSE_REQ_PENDING) {
351 list_del(&req->list);
352 __fuse_put_request(req);
353 req->out.h.error = -EINTR;
354 return;
355 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700356 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700357
Miklos Szeredia131de02007-10-16 23:31:04 -0700358 /*
359 * Either request is already in userspace, or it was forced.
360 * Wait it out.
361 */
362 spin_unlock(&fc->lock);
363 wait_event(req->waitq, req->state == FUSE_REQ_FINISHED);
364 spin_lock(&fc->lock);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700365
Miklos Szeredia131de02007-10-16 23:31:04 -0700366 if (!req->aborted)
367 return;
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700368
369 aborted:
Miklos Szeredia131de02007-10-16 23:31:04 -0700370 BUG_ON(req->state != FUSE_REQ_FINISHED);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700371 if (req->locked) {
372 /* This is uninterruptible sleep, because data is
373 being copied to/from the buffers of req. During
374 locked state, there mustn't be any filesystem
375 operation (e.g. page fault), since that could lead
376 to deadlock */
Miklos Szeredid7133112006-04-10 22:54:55 -0700377 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700378 wait_event(req->waitq, !req->locked);
Miklos Szeredid7133112006-04-10 22:54:55 -0700379 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700380 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700381}
382
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700383void request_send(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700384{
385 req->isreply = 1;
Miklos Szeredid7133112006-04-10 22:54:55 -0700386 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700387 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700388 req->out.h.error = -ENOTCONN;
389 else if (fc->conn_error)
390 req->out.h.error = -ECONNREFUSED;
391 else {
392 queue_request(fc, req);
393 /* acquire extra reference, since request is still needed
394 after request_end() */
395 __fuse_get_request(req);
396
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700397 request_wait_answer(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700398 }
Miklos Szeredid7133112006-04-10 22:54:55 -0700399 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700400}
401
Miklos Szeredid12def12008-02-06 01:38:39 -0800402static void request_send_nowait_locked(struct fuse_conn *fc,
403 struct fuse_req *req)
404{
405 req->background = 1;
406 fc->num_background++;
407 if (fc->num_background == FUSE_MAX_BACKGROUND)
408 fc->blocked = 1;
409 if (fc->num_background == FUSE_CONGESTION_THRESHOLD) {
410 set_bdi_congested(&fc->bdi, READ);
411 set_bdi_congested(&fc->bdi, WRITE);
412 }
413 list_add_tail(&req->list, &fc->bg_queue);
414 flush_bg_queue(fc);
415}
416
Miklos Szeredi334f4852005-09-09 13:10:27 -0700417static void request_send_nowait(struct fuse_conn *fc, struct fuse_req *req)
418{
Miklos Szeredid7133112006-04-10 22:54:55 -0700419 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700420 if (fc->connected) {
Miklos Szeredid12def12008-02-06 01:38:39 -0800421 request_send_nowait_locked(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700422 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700423 } else {
424 req->out.h.error = -ENOTCONN;
425 request_end(fc, req);
426 }
427}
428
429void request_send_noreply(struct fuse_conn *fc, struct fuse_req *req)
430{
431 req->isreply = 0;
432 request_send_nowait(fc, req);
433}
434
435void request_send_background(struct fuse_conn *fc, struct fuse_req *req)
436{
437 req->isreply = 1;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700438 request_send_nowait(fc, req);
439}
440
Miklos Szeredi334f4852005-09-09 13:10:27 -0700441/*
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700442 * Called under fc->lock
443 *
444 * fc->connected must have been checked previously
445 */
446void request_send_background_locked(struct fuse_conn *fc, struct fuse_req *req)
447{
448 req->isreply = 1;
449 request_send_nowait_locked(fc, req);
450}
451
452/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700453 * Lock the request. Up to the next unlock_request() there mustn't be
454 * anything that could cause a page-fault. If the request was already
Miklos Szeredif9a28422006-06-25 05:48:53 -0700455 * aborted bail out.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700456 */
Miklos Szeredid7133112006-04-10 22:54:55 -0700457static int lock_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700458{
459 int err = 0;
460 if (req) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700461 spin_lock(&fc->lock);
Miklos Szeredif9a28422006-06-25 05:48:53 -0700462 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700463 err = -ENOENT;
464 else
465 req->locked = 1;
Miklos Szeredid7133112006-04-10 22:54:55 -0700466 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700467 }
468 return err;
469}
470
471/*
Miklos Szeredif9a28422006-06-25 05:48:53 -0700472 * Unlock request. If it was aborted during being locked, the
Miklos Szeredi334f4852005-09-09 13:10:27 -0700473 * requester thread is currently waiting for it to be unlocked, so
474 * wake it up.
475 */
Miklos Szeredid7133112006-04-10 22:54:55 -0700476static void unlock_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700477{
478 if (req) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700479 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700480 req->locked = 0;
Miklos Szeredif9a28422006-06-25 05:48:53 -0700481 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700482 wake_up(&req->waitq);
Miklos Szeredid7133112006-04-10 22:54:55 -0700483 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700484 }
485}
486
487struct fuse_copy_state {
Miklos Szeredid7133112006-04-10 22:54:55 -0700488 struct fuse_conn *fc;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700489 int write;
490 struct fuse_req *req;
491 const struct iovec *iov;
492 unsigned long nr_segs;
493 unsigned long seglen;
494 unsigned long addr;
495 struct page *pg;
496 void *mapaddr;
497 void *buf;
498 unsigned len;
499};
500
Miklos Szeredid7133112006-04-10 22:54:55 -0700501static void fuse_copy_init(struct fuse_copy_state *cs, struct fuse_conn *fc,
502 int write, struct fuse_req *req,
503 const struct iovec *iov, unsigned long nr_segs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700504{
505 memset(cs, 0, sizeof(*cs));
Miklos Szeredid7133112006-04-10 22:54:55 -0700506 cs->fc = fc;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700507 cs->write = write;
508 cs->req = req;
509 cs->iov = iov;
510 cs->nr_segs = nr_segs;
511}
512
513/* Unmap and put previous page of userspace buffer */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800514static void fuse_copy_finish(struct fuse_copy_state *cs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700515{
516 if (cs->mapaddr) {
517 kunmap_atomic(cs->mapaddr, KM_USER0);
518 if (cs->write) {
519 flush_dcache_page(cs->pg);
520 set_page_dirty_lock(cs->pg);
521 }
522 put_page(cs->pg);
523 cs->mapaddr = NULL;
524 }
525}
526
527/*
528 * Get another pagefull of userspace buffer, and map it to kernel
529 * address space, and lock request
530 */
531static int fuse_copy_fill(struct fuse_copy_state *cs)
532{
533 unsigned long offset;
534 int err;
535
Miklos Szeredid7133112006-04-10 22:54:55 -0700536 unlock_request(cs->fc, cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700537 fuse_copy_finish(cs);
538 if (!cs->seglen) {
539 BUG_ON(!cs->nr_segs);
540 cs->seglen = cs->iov[0].iov_len;
541 cs->addr = (unsigned long) cs->iov[0].iov_base;
Miklos Szeredi1729a162008-11-26 12:03:54 +0100542 cs->iov++;
543 cs->nr_segs--;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700544 }
545 down_read(&current->mm->mmap_sem);
546 err = get_user_pages(current, current->mm, cs->addr, 1, cs->write, 0,
547 &cs->pg, NULL);
548 up_read(&current->mm->mmap_sem);
549 if (err < 0)
550 return err;
551 BUG_ON(err != 1);
552 offset = cs->addr % PAGE_SIZE;
553 cs->mapaddr = kmap_atomic(cs->pg, KM_USER0);
554 cs->buf = cs->mapaddr + offset;
555 cs->len = min(PAGE_SIZE - offset, cs->seglen);
556 cs->seglen -= cs->len;
557 cs->addr += cs->len;
558
Miklos Szeredid7133112006-04-10 22:54:55 -0700559 return lock_request(cs->fc, cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700560}
561
562/* Do as much copy to/from userspace buffer as we can */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800563static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700564{
565 unsigned ncpy = min(*size, cs->len);
566 if (val) {
567 if (cs->write)
568 memcpy(cs->buf, *val, ncpy);
569 else
570 memcpy(*val, cs->buf, ncpy);
571 *val += ncpy;
572 }
573 *size -= ncpy;
574 cs->len -= ncpy;
575 cs->buf += ncpy;
576 return ncpy;
577}
578
579/*
580 * Copy a page in the request to/from the userspace buffer. Must be
581 * done atomically
582 */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800583static int fuse_copy_page(struct fuse_copy_state *cs, struct page *page,
584 unsigned offset, unsigned count, int zeroing)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700585{
586 if (page && zeroing && count < PAGE_SIZE) {
587 void *mapaddr = kmap_atomic(page, KM_USER1);
588 memset(mapaddr, 0, PAGE_SIZE);
589 kunmap_atomic(mapaddr, KM_USER1);
590 }
591 while (count) {
Miklos Szeredi1729a162008-11-26 12:03:54 +0100592 if (!cs->len) {
593 int err = fuse_copy_fill(cs);
594 if (err)
595 return err;
596 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700597 if (page) {
598 void *mapaddr = kmap_atomic(page, KM_USER1);
599 void *buf = mapaddr + offset;
600 offset += fuse_copy_do(cs, &buf, &count);
601 kunmap_atomic(mapaddr, KM_USER1);
602 } else
603 offset += fuse_copy_do(cs, NULL, &count);
604 }
605 if (page && !cs->write)
606 flush_dcache_page(page);
607 return 0;
608}
609
610/* Copy pages in the request to/from userspace buffer */
611static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
612 int zeroing)
613{
614 unsigned i;
615 struct fuse_req *req = cs->req;
616 unsigned offset = req->page_offset;
617 unsigned count = min(nbytes, (unsigned) PAGE_SIZE - offset);
618
619 for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
620 struct page *page = req->pages[i];
621 int err = fuse_copy_page(cs, page, offset, count, zeroing);
622 if (err)
623 return err;
624
625 nbytes -= count;
626 count = min(nbytes, (unsigned) PAGE_SIZE);
627 offset = 0;
628 }
629 return 0;
630}
631
632/* Copy a single argument in the request to/from userspace buffer */
633static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
634{
635 while (size) {
Miklos Szeredi1729a162008-11-26 12:03:54 +0100636 if (!cs->len) {
637 int err = fuse_copy_fill(cs);
638 if (err)
639 return err;
640 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700641 fuse_copy_do(cs, &val, &size);
642 }
643 return 0;
644}
645
646/* Copy request arguments to/from userspace buffer */
647static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
648 unsigned argpages, struct fuse_arg *args,
649 int zeroing)
650{
651 int err = 0;
652 unsigned i;
653
654 for (i = 0; !err && i < numargs; i++) {
655 struct fuse_arg *arg = &args[i];
656 if (i == numargs - 1 && argpages)
657 err = fuse_copy_pages(cs, arg->size, zeroing);
658 else
659 err = fuse_copy_one(cs, arg->value, arg->size);
660 }
661 return err;
662}
663
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700664static int request_pending(struct fuse_conn *fc)
665{
666 return !list_empty(&fc->pending) || !list_empty(&fc->interrupts);
667}
668
Miklos Szeredi334f4852005-09-09 13:10:27 -0700669/* Wait until a request is available on the pending list */
670static void request_wait(struct fuse_conn *fc)
671{
672 DECLARE_WAITQUEUE(wait, current);
673
674 add_wait_queue_exclusive(&fc->waitq, &wait);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700675 while (fc->connected && !request_pending(fc)) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700676 set_current_state(TASK_INTERRUPTIBLE);
677 if (signal_pending(current))
678 break;
679
Miklos Szeredid7133112006-04-10 22:54:55 -0700680 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700681 schedule();
Miklos Szeredid7133112006-04-10 22:54:55 -0700682 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700683 }
684 set_current_state(TASK_RUNNING);
685 remove_wait_queue(&fc->waitq, &wait);
686}
687
688/*
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700689 * Transfer an interrupt request to userspace
690 *
691 * Unlike other requests this is assembled on demand, without a need
692 * to allocate a separate fuse_req structure.
693 *
694 * Called with fc->lock held, releases it
695 */
696static int fuse_read_interrupt(struct fuse_conn *fc, struct fuse_req *req,
697 const struct iovec *iov, unsigned long nr_segs)
Josh Triplett105f4d72006-09-29 01:59:25 -0700698 __releases(fc->lock)
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700699{
700 struct fuse_copy_state cs;
701 struct fuse_in_header ih;
702 struct fuse_interrupt_in arg;
703 unsigned reqsize = sizeof(ih) + sizeof(arg);
704 int err;
705
706 list_del_init(&req->intr_entry);
707 req->intr_unique = fuse_get_unique(fc);
708 memset(&ih, 0, sizeof(ih));
709 memset(&arg, 0, sizeof(arg));
710 ih.len = reqsize;
711 ih.opcode = FUSE_INTERRUPT;
712 ih.unique = req->intr_unique;
713 arg.unique = req->in.h.unique;
714
715 spin_unlock(&fc->lock);
716 if (iov_length(iov, nr_segs) < reqsize)
717 return -EINVAL;
718
719 fuse_copy_init(&cs, fc, 1, NULL, iov, nr_segs);
720 err = fuse_copy_one(&cs, &ih, sizeof(ih));
721 if (!err)
722 err = fuse_copy_one(&cs, &arg, sizeof(arg));
723 fuse_copy_finish(&cs);
724
725 return err ? err : reqsize;
726}
727
728/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700729 * Read a single request into the userspace filesystem's buffer. This
730 * function waits until a request is available, then removes it from
731 * the pending list and copies request data to userspace buffer. If
Miklos Szeredif9a28422006-06-25 05:48:53 -0700732 * no reply is needed (FORGET) or request has been aborted or there
733 * was an error during the copying then it's finished by calling
Miklos Szeredi334f4852005-09-09 13:10:27 -0700734 * request_end(). Otherwise add it to the processing list, and set
735 * the 'sent' flag.
736 */
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700737static ssize_t fuse_dev_read(struct kiocb *iocb, const struct iovec *iov,
738 unsigned long nr_segs, loff_t pos)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700739{
740 int err;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700741 struct fuse_req *req;
742 struct fuse_in *in;
743 struct fuse_copy_state cs;
744 unsigned reqsize;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700745 struct file *file = iocb->ki_filp;
Miklos Szeredi0720b312006-04-10 22:54:55 -0700746 struct fuse_conn *fc = fuse_get_conn(file);
747 if (!fc)
748 return -EPERM;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700749
Miklos Szeredi1d3d7522006-01-06 00:19:40 -0800750 restart:
Miklos Szeredid7133112006-04-10 22:54:55 -0700751 spin_lock(&fc->lock);
Jeff Dikee5ac1d12006-04-10 22:54:53 -0700752 err = -EAGAIN;
753 if ((file->f_flags & O_NONBLOCK) && fc->connected &&
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700754 !request_pending(fc))
Jeff Dikee5ac1d12006-04-10 22:54:53 -0700755 goto err_unlock;
756
Miklos Szeredi334f4852005-09-09 13:10:27 -0700757 request_wait(fc);
758 err = -ENODEV;
Miklos Szeredi9ba7cbb2006-01-16 22:14:34 -0800759 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700760 goto err_unlock;
761 err = -ERESTARTSYS;
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700762 if (!request_pending(fc))
Miklos Szeredi334f4852005-09-09 13:10:27 -0700763 goto err_unlock;
764
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700765 if (!list_empty(&fc->interrupts)) {
766 req = list_entry(fc->interrupts.next, struct fuse_req,
767 intr_entry);
768 return fuse_read_interrupt(fc, req, iov, nr_segs);
769 }
770
Miklos Szeredi334f4852005-09-09 13:10:27 -0700771 req = list_entry(fc->pending.next, struct fuse_req, list);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800772 req->state = FUSE_REQ_READING;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800773 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700774
775 in = &req->in;
Miklos Szeredi1d3d7522006-01-06 00:19:40 -0800776 reqsize = in->h.len;
777 /* If request is too large, reply with an error and restart the read */
778 if (iov_length(iov, nr_segs) < reqsize) {
779 req->out.h.error = -EIO;
780 /* SETXATTR is special, since it may contain too large data */
781 if (in->h.opcode == FUSE_SETXATTR)
782 req->out.h.error = -E2BIG;
783 request_end(fc, req);
784 goto restart;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700785 }
Miklos Szeredid7133112006-04-10 22:54:55 -0700786 spin_unlock(&fc->lock);
787 fuse_copy_init(&cs, fc, 1, req, iov, nr_segs);
Miklos Szeredi1d3d7522006-01-06 00:19:40 -0800788 err = fuse_copy_one(&cs, &in->h, sizeof(in->h));
789 if (!err)
790 err = fuse_copy_args(&cs, in->numargs, in->argpages,
791 (struct fuse_arg *) in->args, 0);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700792 fuse_copy_finish(&cs);
Miklos Szeredid7133112006-04-10 22:54:55 -0700793 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700794 req->locked = 0;
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -0700795 if (req->aborted) {
796 request_end(fc, req);
797 return -ENODEV;
798 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700799 if (err) {
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -0700800 req->out.h.error = -EIO;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700801 request_end(fc, req);
802 return err;
803 }
804 if (!req->isreply)
805 request_end(fc, req);
806 else {
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800807 req->state = FUSE_REQ_SENT;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800808 list_move_tail(&req->list, &fc->processing);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700809 if (req->interrupted)
810 queue_interrupt(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700811 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700812 }
813 return reqsize;
814
815 err_unlock:
Miklos Szeredid7133112006-04-10 22:54:55 -0700816 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700817 return err;
818}
819
Miklos Szeredi334f4852005-09-09 13:10:27 -0700820/* Look up request on processing list by unique ID */
821static struct fuse_req *request_find(struct fuse_conn *fc, u64 unique)
822{
823 struct list_head *entry;
824
825 list_for_each(entry, &fc->processing) {
826 struct fuse_req *req;
827 req = list_entry(entry, struct fuse_req, list);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700828 if (req->in.h.unique == unique || req->intr_unique == unique)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700829 return req;
830 }
831 return NULL;
832}
833
834static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
835 unsigned nbytes)
836{
837 unsigned reqsize = sizeof(struct fuse_out_header);
838
839 if (out->h.error)
840 return nbytes != reqsize ? -EINVAL : 0;
841
842 reqsize += len_args(out->numargs, out->args);
843
844 if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
845 return -EINVAL;
846 else if (reqsize > nbytes) {
847 struct fuse_arg *lastarg = &out->args[out->numargs-1];
848 unsigned diffsize = reqsize - nbytes;
849 if (diffsize > lastarg->size)
850 return -EINVAL;
851 lastarg->size -= diffsize;
852 }
853 return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
854 out->page_zeroing);
855}
856
857/*
858 * Write a single reply to a request. First the header is copied from
859 * the write buffer. The request is then searched on the processing
860 * list by the unique ID found in the header. If found, then remove
861 * it from the list and copy the rest of the buffer to the request.
862 * The request is finished by calling request_end()
863 */
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700864static ssize_t fuse_dev_write(struct kiocb *iocb, const struct iovec *iov,
865 unsigned long nr_segs, loff_t pos)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700866{
867 int err;
868 unsigned nbytes = iov_length(iov, nr_segs);
869 struct fuse_req *req;
870 struct fuse_out_header oh;
871 struct fuse_copy_state cs;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700872 struct fuse_conn *fc = fuse_get_conn(iocb->ki_filp);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700873 if (!fc)
Miklos Szeredia87046d2006-04-10 22:54:56 -0700874 return -EPERM;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700875
Miklos Szeredid7133112006-04-10 22:54:55 -0700876 fuse_copy_init(&cs, fc, 0, NULL, iov, nr_segs);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700877 if (nbytes < sizeof(struct fuse_out_header))
878 return -EINVAL;
879
880 err = fuse_copy_one(&cs, &oh, sizeof(oh));
881 if (err)
882 goto err_finish;
883 err = -EINVAL;
884 if (!oh.unique || oh.error <= -1000 || oh.error > 0 ||
885 oh.len != nbytes)
886 goto err_finish;
887
Miklos Szeredid7133112006-04-10 22:54:55 -0700888 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800889 err = -ENOENT;
890 if (!fc->connected)
891 goto err_unlock;
892
Miklos Szeredi334f4852005-09-09 13:10:27 -0700893 req = request_find(fc, oh.unique);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700894 if (!req)
895 goto err_unlock;
896
Miklos Szeredif9a28422006-06-25 05:48:53 -0700897 if (req->aborted) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700898 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700899 fuse_copy_finish(&cs);
Miklos Szeredid7133112006-04-10 22:54:55 -0700900 spin_lock(&fc->lock);
Miklos Szeredi222f1d62006-01-16 22:14:25 -0800901 request_end(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700902 return -ENOENT;
903 }
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700904 /* Is it an interrupt reply? */
905 if (req->intr_unique == oh.unique) {
906 err = -EINVAL;
907 if (nbytes != sizeof(struct fuse_out_header))
908 goto err_unlock;
909
910 if (oh.error == -ENOSYS)
911 fc->no_interrupt = 1;
912 else if (oh.error == -EAGAIN)
913 queue_interrupt(fc, req);
914
915 spin_unlock(&fc->lock);
916 fuse_copy_finish(&cs);
917 return nbytes;
918 }
919
920 req->state = FUSE_REQ_WRITING;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800921 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700922 req->out.h = oh;
923 req->locked = 1;
924 cs.req = req;
Miklos Szeredid7133112006-04-10 22:54:55 -0700925 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700926
927 err = copy_out_args(&cs, &req->out, nbytes);
928 fuse_copy_finish(&cs);
929
Miklos Szeredid7133112006-04-10 22:54:55 -0700930 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700931 req->locked = 0;
932 if (!err) {
Miklos Szeredif9a28422006-06-25 05:48:53 -0700933 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700934 err = -ENOENT;
Miklos Szeredif9a28422006-06-25 05:48:53 -0700935 } else if (!req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700936 req->out.h.error = -EIO;
937 request_end(fc, req);
938
939 return err ? err : nbytes;
940
941 err_unlock:
Miklos Szeredid7133112006-04-10 22:54:55 -0700942 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700943 err_finish:
944 fuse_copy_finish(&cs);
945 return err;
946}
947
Miklos Szeredi334f4852005-09-09 13:10:27 -0700948static unsigned fuse_dev_poll(struct file *file, poll_table *wait)
949{
Miklos Szeredi334f4852005-09-09 13:10:27 -0700950 unsigned mask = POLLOUT | POLLWRNORM;
Miklos Szeredi7025d9a2006-04-10 22:54:50 -0700951 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700952 if (!fc)
Miklos Szeredi7025d9a2006-04-10 22:54:50 -0700953 return POLLERR;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700954
955 poll_wait(file, &fc->waitq, wait);
956
Miklos Szeredid7133112006-04-10 22:54:55 -0700957 spin_lock(&fc->lock);
Miklos Szeredi7025d9a2006-04-10 22:54:50 -0700958 if (!fc->connected)
959 mask = POLLERR;
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700960 else if (request_pending(fc))
Miklos Szeredi7025d9a2006-04-10 22:54:50 -0700961 mask |= POLLIN | POLLRDNORM;
Miklos Szeredid7133112006-04-10 22:54:55 -0700962 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700963
964 return mask;
965}
966
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800967/*
968 * Abort all requests on the given list (pending or processing)
969 *
Miklos Szeredid7133112006-04-10 22:54:55 -0700970 * This function releases and reacquires fc->lock
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800971 */
Miklos Szeredi334f4852005-09-09 13:10:27 -0700972static void end_requests(struct fuse_conn *fc, struct list_head *head)
973{
974 while (!list_empty(head)) {
975 struct fuse_req *req;
976 req = list_entry(head->next, struct fuse_req, list);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700977 req->out.h.error = -ECONNABORTED;
978 request_end(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700979 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700980 }
981}
982
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800983/*
984 * Abort requests under I/O
985 *
Miklos Szeredif9a28422006-06-25 05:48:53 -0700986 * The requests are set to aborted and finished, and the request
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800987 * waiter is woken up. This will make request_wait_answer() wait
988 * until the request is unlocked and then return.
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800989 *
990 * If the request is asynchronous, then the end function needs to be
991 * called after waiting for the request to be unlocked (if it was
992 * locked).
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800993 */
994static void end_io_requests(struct fuse_conn *fc)
Miklos Szeredi4dbf9302008-04-30 00:54:45 -0700995 __releases(fc->lock) __acquires(fc->lock)
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800996{
997 while (!list_empty(&fc->io)) {
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800998 struct fuse_req *req =
999 list_entry(fc->io.next, struct fuse_req, list);
1000 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
1001
Miklos Szeredif9a28422006-06-25 05:48:53 -07001002 req->aborted = 1;
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001003 req->out.h.error = -ECONNABORTED;
1004 req->state = FUSE_REQ_FINISHED;
1005 list_del_init(&req->list);
1006 wake_up(&req->waitq);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08001007 if (end) {
1008 req->end = NULL;
1009 /* The end function will consume this reference */
1010 __fuse_get_request(req);
Miklos Szeredid7133112006-04-10 22:54:55 -07001011 spin_unlock(&fc->lock);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08001012 wait_event(req->waitq, !req->locked);
1013 end(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -07001014 spin_lock(&fc->lock);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08001015 }
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001016 }
1017}
1018
1019/*
1020 * Abort all requests.
1021 *
1022 * Emergency exit in case of a malicious or accidental deadlock, or
1023 * just a hung filesystem.
1024 *
1025 * The same effect is usually achievable through killing the
1026 * filesystem daemon and all users of the filesystem. The exception
1027 * is the combination of an asynchronous request and the tricky
1028 * deadlock (see Documentation/filesystems/fuse.txt).
1029 *
1030 * During the aborting, progression of requests from the pending and
1031 * processing lists onto the io list, and progression of new requests
1032 * onto the pending list is prevented by req->connected being false.
1033 *
1034 * Progression of requests under I/O to the processing list is
Miklos Szeredif9a28422006-06-25 05:48:53 -07001035 * prevented by the req->aborted flag being true for these requests.
1036 * For this reason requests on the io list must be aborted first.
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001037 */
1038void fuse_abort_conn(struct fuse_conn *fc)
1039{
Miklos Szeredid7133112006-04-10 22:54:55 -07001040 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001041 if (fc->connected) {
1042 fc->connected = 0;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -07001043 fc->blocked = 0;
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001044 end_io_requests(fc);
1045 end_requests(fc, &fc->pending);
1046 end_requests(fc, &fc->processing);
1047 wake_up_all(&fc->waitq);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -07001048 wake_up_all(&fc->blocked_waitq);
Jeff Dike385a17b2006-04-10 22:54:52 -07001049 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001050 }
Miklos Szeredid7133112006-04-10 22:54:55 -07001051 spin_unlock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001052}
1053
Miklos Szeredi334f4852005-09-09 13:10:27 -07001054static int fuse_dev_release(struct inode *inode, struct file *file)
1055{
Miklos Szeredi0720b312006-04-10 22:54:55 -07001056 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001057 if (fc) {
Miklos Szeredid7133112006-04-10 22:54:55 -07001058 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -07001059 fc->connected = 0;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001060 end_requests(fc, &fc->pending);
1061 end_requests(fc, &fc->processing);
Miklos Szeredid7133112006-04-10 22:54:55 -07001062 spin_unlock(&fc->lock);
Miklos Szeredibafa9652006-06-25 05:48:51 -07001063 fuse_conn_put(fc);
Jeff Dike385a17b2006-04-10 22:54:52 -07001064 }
Miklos Szeredif543f252006-01-16 22:14:35 -08001065
Miklos Szeredi334f4852005-09-09 13:10:27 -07001066 return 0;
1067}
1068
Jeff Dike385a17b2006-04-10 22:54:52 -07001069static int fuse_dev_fasync(int fd, struct file *file, int on)
1070{
1071 struct fuse_conn *fc = fuse_get_conn(file);
1072 if (!fc)
Miklos Szeredia87046d2006-04-10 22:54:56 -07001073 return -EPERM;
Jeff Dike385a17b2006-04-10 22:54:52 -07001074
1075 /* No locking - fasync_helper does its own locking */
1076 return fasync_helper(fd, file, on, &fc->fasync);
1077}
1078
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001079const struct file_operations fuse_dev_operations = {
Miklos Szeredi334f4852005-09-09 13:10:27 -07001080 .owner = THIS_MODULE,
1081 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07001082 .read = do_sync_read,
1083 .aio_read = fuse_dev_read,
1084 .write = do_sync_write,
1085 .aio_write = fuse_dev_write,
Miklos Szeredi334f4852005-09-09 13:10:27 -07001086 .poll = fuse_dev_poll,
1087 .release = fuse_dev_release,
Jeff Dike385a17b2006-04-10 22:54:52 -07001088 .fasync = fuse_dev_fasync,
Miklos Szeredi334f4852005-09-09 13:10:27 -07001089};
1090
1091static struct miscdevice fuse_miscdevice = {
1092 .minor = FUSE_MINOR,
1093 .name = "fuse",
1094 .fops = &fuse_dev_operations,
1095};
1096
1097int __init fuse_dev_init(void)
1098{
1099 int err = -ENOMEM;
1100 fuse_req_cachep = kmem_cache_create("fuse_request",
1101 sizeof(struct fuse_req),
Paul Mundt20c2df82007-07-20 10:11:58 +09001102 0, 0, NULL);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001103 if (!fuse_req_cachep)
1104 goto out;
1105
1106 err = misc_register(&fuse_miscdevice);
1107 if (err)
1108 goto out_cache_clean;
1109
1110 return 0;
1111
1112 out_cache_clean:
1113 kmem_cache_destroy(fuse_req_cachep);
1114 out:
1115 return err;
1116}
1117
1118void fuse_dev_cleanup(void)
1119{
1120 misc_deregister(&fuse_miscdevice);
1121 kmem_cache_destroy(fuse_req_cachep);
1122}