blob: c4a3d9bbdaa877f2a817045abddb3b94317124eb [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{
David Howells2186a712008-11-14 10:38:53 +110090 req->in.h.uid = current_fsuid();
91 req->in.h.gid = current_fsgid();
Miklos Szeredi33649c92006-06-25 05:48:52 -070092 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)
Harvey Harrison5d9ec852008-12-02 14:49:42 +0100272__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 Szeredi26c36792009-01-26 15:00:59 +0100284 if (fc->num_background == FUSE_CONGESTION_THRESHOLD &&
285 fc->connected) {
Miklos Szeredif92b99b2007-10-16 23:30:59 -0700286 clear_bdi_congested(&fc->bdi, READ);
287 clear_bdi_congested(&fc->bdi, WRITE);
288 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700289 fc->num_background--;
Miklos Szeredid12def12008-02-06 01:38:39 -0800290 fc->active_background--;
291 flush_bg_queue(fc);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700292 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700293 spin_unlock(&fc->lock);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700294 wake_up(&req->waitq);
295 if (end)
296 end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100297 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)
Harvey Harrison5d9ec852008-12-02 14:49:42 +0100302__releases(&fc->lock)
303__acquires(&fc->lock)
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700304{
305 if (signal_pending(current))
306 return;
307
308 spin_unlock(&fc->lock);
309 wait_event_interruptible(req->waitq, req->state == FUSE_REQ_FINISHED);
310 spin_lock(&fc->lock);
311}
312
313static void queue_interrupt(struct fuse_conn *fc, struct fuse_req *req)
314{
315 list_add_tail(&req->intr_entry, &fc->interrupts);
316 wake_up(&fc->waitq);
317 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
318}
319
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700320static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
Harvey Harrison5d9ec852008-12-02 14:49:42 +0100321__releases(&fc->lock)
322__acquires(&fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700323{
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700324 if (!fc->no_interrupt) {
325 /* Any signal may interrupt this */
326 wait_answer_interruptible(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700327
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700328 if (req->aborted)
329 goto aborted;
330 if (req->state == FUSE_REQ_FINISHED)
331 return;
332
333 req->interrupted = 1;
334 if (req->state == FUSE_REQ_SENT)
335 queue_interrupt(fc, req);
336 }
337
Miklos Szeredia131de02007-10-16 23:31:04 -0700338 if (!req->force) {
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700339 sigset_t oldset;
340
341 /* Only fatal signals may interrupt this */
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700342 block_sigs(&oldset);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700343 wait_answer_interruptible(fc, req);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700344 restore_sigs(&oldset);
Miklos Szeredia131de02007-10-16 23:31:04 -0700345
346 if (req->aborted)
347 goto aborted;
348 if (req->state == FUSE_REQ_FINISHED)
349 return;
350
351 /* Request is not yet in userspace, bail out */
352 if (req->state == FUSE_REQ_PENDING) {
353 list_del(&req->list);
354 __fuse_put_request(req);
355 req->out.h.error = -EINTR;
356 return;
357 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700358 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700359
Miklos Szeredia131de02007-10-16 23:31:04 -0700360 /*
361 * Either request is already in userspace, or it was forced.
362 * Wait it out.
363 */
364 spin_unlock(&fc->lock);
365 wait_event(req->waitq, req->state == FUSE_REQ_FINISHED);
366 spin_lock(&fc->lock);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700367
Miklos Szeredia131de02007-10-16 23:31:04 -0700368 if (!req->aborted)
369 return;
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700370
371 aborted:
Miklos Szeredia131de02007-10-16 23:31:04 -0700372 BUG_ON(req->state != FUSE_REQ_FINISHED);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700373 if (req->locked) {
374 /* This is uninterruptible sleep, because data is
375 being copied to/from the buffers of req. During
376 locked state, there mustn't be any filesystem
377 operation (e.g. page fault), since that could lead
378 to deadlock */
Miklos Szeredid7133112006-04-10 22:54:55 -0700379 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700380 wait_event(req->waitq, !req->locked);
Miklos Szeredid7133112006-04-10 22:54:55 -0700381 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700382 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700383}
384
Tejun Heob93f8582008-11-26 12:03:55 +0100385void fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700386{
387 req->isreply = 1;
Miklos Szeredid7133112006-04-10 22:54:55 -0700388 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700389 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700390 req->out.h.error = -ENOTCONN;
391 else if (fc->conn_error)
392 req->out.h.error = -ECONNREFUSED;
393 else {
394 queue_request(fc, req);
395 /* acquire extra reference, since request is still needed
396 after request_end() */
397 __fuse_get_request(req);
398
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700399 request_wait_answer(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700400 }
Miklos Szeredid7133112006-04-10 22:54:55 -0700401 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700402}
403
Tejun Heob93f8582008-11-26 12:03:55 +0100404static void fuse_request_send_nowait_locked(struct fuse_conn *fc,
405 struct fuse_req *req)
Miklos Szeredid12def12008-02-06 01:38:39 -0800406{
407 req->background = 1;
408 fc->num_background++;
409 if (fc->num_background == FUSE_MAX_BACKGROUND)
410 fc->blocked = 1;
411 if (fc->num_background == FUSE_CONGESTION_THRESHOLD) {
412 set_bdi_congested(&fc->bdi, READ);
413 set_bdi_congested(&fc->bdi, WRITE);
414 }
415 list_add_tail(&req->list, &fc->bg_queue);
416 flush_bg_queue(fc);
417}
418
Tejun Heob93f8582008-11-26 12:03:55 +0100419static void fuse_request_send_nowait(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700420{
Miklos Szeredid7133112006-04-10 22:54:55 -0700421 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700422 if (fc->connected) {
Tejun Heob93f8582008-11-26 12:03:55 +0100423 fuse_request_send_nowait_locked(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700424 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700425 } else {
426 req->out.h.error = -ENOTCONN;
427 request_end(fc, req);
428 }
429}
430
Tejun Heob93f8582008-11-26 12:03:55 +0100431void fuse_request_send_noreply(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700432{
433 req->isreply = 0;
Tejun Heob93f8582008-11-26 12:03:55 +0100434 fuse_request_send_nowait(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700435}
436
Tejun Heob93f8582008-11-26 12:03:55 +0100437void fuse_request_send_background(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700438{
439 req->isreply = 1;
Tejun Heob93f8582008-11-26 12:03:55 +0100440 fuse_request_send_nowait(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700441}
442
Miklos Szeredi334f4852005-09-09 13:10:27 -0700443/*
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700444 * Called under fc->lock
445 *
446 * fc->connected must have been checked previously
447 */
Tejun Heob93f8582008-11-26 12:03:55 +0100448void fuse_request_send_background_locked(struct fuse_conn *fc,
449 struct fuse_req *req)
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700450{
451 req->isreply = 1;
Tejun Heob93f8582008-11-26 12:03:55 +0100452 fuse_request_send_nowait_locked(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700453}
454
455/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700456 * Lock the request. Up to the next unlock_request() there mustn't be
457 * anything that could cause a page-fault. If the request was already
Miklos Szeredif9a28422006-06-25 05:48:53 -0700458 * aborted bail out.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700459 */
Miklos Szeredid7133112006-04-10 22:54:55 -0700460static int lock_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700461{
462 int err = 0;
463 if (req) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700464 spin_lock(&fc->lock);
Miklos Szeredif9a28422006-06-25 05:48:53 -0700465 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700466 err = -ENOENT;
467 else
468 req->locked = 1;
Miklos Szeredid7133112006-04-10 22:54:55 -0700469 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700470 }
471 return err;
472}
473
474/*
Miklos Szeredif9a28422006-06-25 05:48:53 -0700475 * Unlock request. If it was aborted during being locked, the
Miklos Szeredi334f4852005-09-09 13:10:27 -0700476 * requester thread is currently waiting for it to be unlocked, so
477 * wake it up.
478 */
Miklos Szeredid7133112006-04-10 22:54:55 -0700479static void unlock_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700480{
481 if (req) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700482 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700483 req->locked = 0;
Miklos Szeredif9a28422006-06-25 05:48:53 -0700484 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700485 wake_up(&req->waitq);
Miklos Szeredid7133112006-04-10 22:54:55 -0700486 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700487 }
488}
489
490struct fuse_copy_state {
Miklos Szeredid7133112006-04-10 22:54:55 -0700491 struct fuse_conn *fc;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700492 int write;
493 struct fuse_req *req;
494 const struct iovec *iov;
495 unsigned long nr_segs;
496 unsigned long seglen;
497 unsigned long addr;
498 struct page *pg;
499 void *mapaddr;
500 void *buf;
501 unsigned len;
502};
503
Miklos Szeredid7133112006-04-10 22:54:55 -0700504static void fuse_copy_init(struct fuse_copy_state *cs, struct fuse_conn *fc,
505 int write, struct fuse_req *req,
506 const struct iovec *iov, unsigned long nr_segs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700507{
508 memset(cs, 0, sizeof(*cs));
Miklos Szeredid7133112006-04-10 22:54:55 -0700509 cs->fc = fc;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700510 cs->write = write;
511 cs->req = req;
512 cs->iov = iov;
513 cs->nr_segs = nr_segs;
514}
515
516/* Unmap and put previous page of userspace buffer */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800517static void fuse_copy_finish(struct fuse_copy_state *cs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700518{
519 if (cs->mapaddr) {
520 kunmap_atomic(cs->mapaddr, KM_USER0);
521 if (cs->write) {
522 flush_dcache_page(cs->pg);
523 set_page_dirty_lock(cs->pg);
524 }
525 put_page(cs->pg);
526 cs->mapaddr = NULL;
527 }
528}
529
530/*
531 * Get another pagefull of userspace buffer, and map it to kernel
532 * address space, and lock request
533 */
534static int fuse_copy_fill(struct fuse_copy_state *cs)
535{
536 unsigned long offset;
537 int err;
538
Miklos Szeredid7133112006-04-10 22:54:55 -0700539 unlock_request(cs->fc, cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700540 fuse_copy_finish(cs);
541 if (!cs->seglen) {
542 BUG_ON(!cs->nr_segs);
543 cs->seglen = cs->iov[0].iov_len;
544 cs->addr = (unsigned long) cs->iov[0].iov_base;
Miklos Szeredi1729a162008-11-26 12:03:54 +0100545 cs->iov++;
546 cs->nr_segs--;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700547 }
548 down_read(&current->mm->mmap_sem);
549 err = get_user_pages(current, current->mm, cs->addr, 1, cs->write, 0,
550 &cs->pg, NULL);
551 up_read(&current->mm->mmap_sem);
552 if (err < 0)
553 return err;
554 BUG_ON(err != 1);
555 offset = cs->addr % PAGE_SIZE;
556 cs->mapaddr = kmap_atomic(cs->pg, KM_USER0);
557 cs->buf = cs->mapaddr + offset;
558 cs->len = min(PAGE_SIZE - offset, cs->seglen);
559 cs->seglen -= cs->len;
560 cs->addr += cs->len;
561
Miklos Szeredid7133112006-04-10 22:54:55 -0700562 return lock_request(cs->fc, cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700563}
564
565/* Do as much copy to/from userspace buffer as we can */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800566static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700567{
568 unsigned ncpy = min(*size, cs->len);
569 if (val) {
570 if (cs->write)
571 memcpy(cs->buf, *val, ncpy);
572 else
573 memcpy(*val, cs->buf, ncpy);
574 *val += ncpy;
575 }
576 *size -= ncpy;
577 cs->len -= ncpy;
578 cs->buf += ncpy;
579 return ncpy;
580}
581
582/*
583 * Copy a page in the request to/from the userspace buffer. Must be
584 * done atomically
585 */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800586static int fuse_copy_page(struct fuse_copy_state *cs, struct page *page,
587 unsigned offset, unsigned count, int zeroing)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700588{
589 if (page && zeroing && count < PAGE_SIZE) {
590 void *mapaddr = kmap_atomic(page, KM_USER1);
591 memset(mapaddr, 0, PAGE_SIZE);
592 kunmap_atomic(mapaddr, KM_USER1);
593 }
594 while (count) {
Miklos Szeredi1729a162008-11-26 12:03:54 +0100595 if (!cs->len) {
596 int err = fuse_copy_fill(cs);
597 if (err)
598 return err;
599 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700600 if (page) {
601 void *mapaddr = kmap_atomic(page, KM_USER1);
602 void *buf = mapaddr + offset;
603 offset += fuse_copy_do(cs, &buf, &count);
604 kunmap_atomic(mapaddr, KM_USER1);
605 } else
606 offset += fuse_copy_do(cs, NULL, &count);
607 }
608 if (page && !cs->write)
609 flush_dcache_page(page);
610 return 0;
611}
612
613/* Copy pages in the request to/from userspace buffer */
614static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
615 int zeroing)
616{
617 unsigned i;
618 struct fuse_req *req = cs->req;
619 unsigned offset = req->page_offset;
620 unsigned count = min(nbytes, (unsigned) PAGE_SIZE - offset);
621
622 for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
623 struct page *page = req->pages[i];
624 int err = fuse_copy_page(cs, page, offset, count, zeroing);
625 if (err)
626 return err;
627
628 nbytes -= count;
629 count = min(nbytes, (unsigned) PAGE_SIZE);
630 offset = 0;
631 }
632 return 0;
633}
634
635/* Copy a single argument in the request to/from userspace buffer */
636static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
637{
638 while (size) {
Miklos Szeredi1729a162008-11-26 12:03:54 +0100639 if (!cs->len) {
640 int err = fuse_copy_fill(cs);
641 if (err)
642 return err;
643 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700644 fuse_copy_do(cs, &val, &size);
645 }
646 return 0;
647}
648
649/* Copy request arguments to/from userspace buffer */
650static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
651 unsigned argpages, struct fuse_arg *args,
652 int zeroing)
653{
654 int err = 0;
655 unsigned i;
656
657 for (i = 0; !err && i < numargs; i++) {
658 struct fuse_arg *arg = &args[i];
659 if (i == numargs - 1 && argpages)
660 err = fuse_copy_pages(cs, arg->size, zeroing);
661 else
662 err = fuse_copy_one(cs, arg->value, arg->size);
663 }
664 return err;
665}
666
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700667static int request_pending(struct fuse_conn *fc)
668{
669 return !list_empty(&fc->pending) || !list_empty(&fc->interrupts);
670}
671
Miklos Szeredi334f4852005-09-09 13:10:27 -0700672/* Wait until a request is available on the pending list */
673static void request_wait(struct fuse_conn *fc)
Harvey Harrison5d9ec852008-12-02 14:49:42 +0100674__releases(&fc->lock)
675__acquires(&fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700676{
677 DECLARE_WAITQUEUE(wait, current);
678
679 add_wait_queue_exclusive(&fc->waitq, &wait);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700680 while (fc->connected && !request_pending(fc)) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700681 set_current_state(TASK_INTERRUPTIBLE);
682 if (signal_pending(current))
683 break;
684
Miklos Szeredid7133112006-04-10 22:54:55 -0700685 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700686 schedule();
Miklos Szeredid7133112006-04-10 22:54:55 -0700687 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700688 }
689 set_current_state(TASK_RUNNING);
690 remove_wait_queue(&fc->waitq, &wait);
691}
692
693/*
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700694 * Transfer an interrupt request to userspace
695 *
696 * Unlike other requests this is assembled on demand, without a need
697 * to allocate a separate fuse_req structure.
698 *
699 * Called with fc->lock held, releases it
700 */
701static int fuse_read_interrupt(struct fuse_conn *fc, struct fuse_req *req,
702 const struct iovec *iov, unsigned long nr_segs)
Harvey Harrison5d9ec852008-12-02 14:49:42 +0100703__releases(&fc->lock)
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700704{
705 struct fuse_copy_state cs;
706 struct fuse_in_header ih;
707 struct fuse_interrupt_in arg;
708 unsigned reqsize = sizeof(ih) + sizeof(arg);
709 int err;
710
711 list_del_init(&req->intr_entry);
712 req->intr_unique = fuse_get_unique(fc);
713 memset(&ih, 0, sizeof(ih));
714 memset(&arg, 0, sizeof(arg));
715 ih.len = reqsize;
716 ih.opcode = FUSE_INTERRUPT;
717 ih.unique = req->intr_unique;
718 arg.unique = req->in.h.unique;
719
720 spin_unlock(&fc->lock);
721 if (iov_length(iov, nr_segs) < reqsize)
722 return -EINVAL;
723
724 fuse_copy_init(&cs, fc, 1, NULL, iov, nr_segs);
725 err = fuse_copy_one(&cs, &ih, sizeof(ih));
726 if (!err)
727 err = fuse_copy_one(&cs, &arg, sizeof(arg));
728 fuse_copy_finish(&cs);
729
730 return err ? err : reqsize;
731}
732
733/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700734 * Read a single request into the userspace filesystem's buffer. This
735 * function waits until a request is available, then removes it from
736 * the pending list and copies request data to userspace buffer. If
Miklos Szeredif9a28422006-06-25 05:48:53 -0700737 * no reply is needed (FORGET) or request has been aborted or there
738 * was an error during the copying then it's finished by calling
Miklos Szeredi334f4852005-09-09 13:10:27 -0700739 * request_end(). Otherwise add it to the processing list, and set
740 * the 'sent' flag.
741 */
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700742static ssize_t fuse_dev_read(struct kiocb *iocb, const struct iovec *iov,
743 unsigned long nr_segs, loff_t pos)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700744{
745 int err;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700746 struct fuse_req *req;
747 struct fuse_in *in;
748 struct fuse_copy_state cs;
749 unsigned reqsize;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700750 struct file *file = iocb->ki_filp;
Miklos Szeredi0720b312006-04-10 22:54:55 -0700751 struct fuse_conn *fc = fuse_get_conn(file);
752 if (!fc)
753 return -EPERM;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700754
Miklos Szeredi1d3d7522006-01-06 00:19:40 -0800755 restart:
Miklos Szeredid7133112006-04-10 22:54:55 -0700756 spin_lock(&fc->lock);
Jeff Dikee5ac1d12006-04-10 22:54:53 -0700757 err = -EAGAIN;
758 if ((file->f_flags & O_NONBLOCK) && fc->connected &&
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700759 !request_pending(fc))
Jeff Dikee5ac1d12006-04-10 22:54:53 -0700760 goto err_unlock;
761
Miklos Szeredi334f4852005-09-09 13:10:27 -0700762 request_wait(fc);
763 err = -ENODEV;
Miklos Szeredi9ba7cbb2006-01-16 22:14:34 -0800764 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700765 goto err_unlock;
766 err = -ERESTARTSYS;
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700767 if (!request_pending(fc))
Miklos Szeredi334f4852005-09-09 13:10:27 -0700768 goto err_unlock;
769
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700770 if (!list_empty(&fc->interrupts)) {
771 req = list_entry(fc->interrupts.next, struct fuse_req,
772 intr_entry);
773 return fuse_read_interrupt(fc, req, iov, nr_segs);
774 }
775
Miklos Szeredi334f4852005-09-09 13:10:27 -0700776 req = list_entry(fc->pending.next, struct fuse_req, list);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800777 req->state = FUSE_REQ_READING;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800778 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700779
780 in = &req->in;
Miklos Szeredi1d3d7522006-01-06 00:19:40 -0800781 reqsize = in->h.len;
782 /* If request is too large, reply with an error and restart the read */
783 if (iov_length(iov, nr_segs) < reqsize) {
784 req->out.h.error = -EIO;
785 /* SETXATTR is special, since it may contain too large data */
786 if (in->h.opcode == FUSE_SETXATTR)
787 req->out.h.error = -E2BIG;
788 request_end(fc, req);
789 goto restart;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700790 }
Miklos Szeredid7133112006-04-10 22:54:55 -0700791 spin_unlock(&fc->lock);
792 fuse_copy_init(&cs, fc, 1, req, iov, nr_segs);
Miklos Szeredi1d3d7522006-01-06 00:19:40 -0800793 err = fuse_copy_one(&cs, &in->h, sizeof(in->h));
794 if (!err)
795 err = fuse_copy_args(&cs, in->numargs, in->argpages,
796 (struct fuse_arg *) in->args, 0);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700797 fuse_copy_finish(&cs);
Miklos Szeredid7133112006-04-10 22:54:55 -0700798 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700799 req->locked = 0;
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -0700800 if (req->aborted) {
801 request_end(fc, req);
802 return -ENODEV;
803 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700804 if (err) {
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -0700805 req->out.h.error = -EIO;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700806 request_end(fc, req);
807 return err;
808 }
809 if (!req->isreply)
810 request_end(fc, req);
811 else {
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800812 req->state = FUSE_REQ_SENT;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800813 list_move_tail(&req->list, &fc->processing);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700814 if (req->interrupted)
815 queue_interrupt(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700816 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700817 }
818 return reqsize;
819
820 err_unlock:
Miklos Szeredid7133112006-04-10 22:54:55 -0700821 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700822 return err;
823}
824
Tejun Heo95668a62008-11-26 12:03:55 +0100825static int fuse_notify_poll(struct fuse_conn *fc, unsigned int size,
826 struct fuse_copy_state *cs)
827{
828 struct fuse_notify_poll_wakeup_out outarg;
829 int err;
830
831 if (size != sizeof(outarg))
832 return -EINVAL;
833
834 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
835 if (err)
836 return err;
837
838 return fuse_notify_poll_wakeup(fc, &outarg);
839}
840
Tejun Heo85993962008-11-26 12:03:55 +0100841static int fuse_notify(struct fuse_conn *fc, enum fuse_notify_code code,
842 unsigned int size, struct fuse_copy_state *cs)
843{
844 switch (code) {
Tejun Heo95668a62008-11-26 12:03:55 +0100845 case FUSE_NOTIFY_POLL:
846 return fuse_notify_poll(fc, size, cs);
847
Tejun Heo85993962008-11-26 12:03:55 +0100848 default:
849 return -EINVAL;
850 }
851}
852
Miklos Szeredi334f4852005-09-09 13:10:27 -0700853/* Look up request on processing list by unique ID */
854static struct fuse_req *request_find(struct fuse_conn *fc, u64 unique)
855{
856 struct list_head *entry;
857
858 list_for_each(entry, &fc->processing) {
859 struct fuse_req *req;
860 req = list_entry(entry, struct fuse_req, list);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700861 if (req->in.h.unique == unique || req->intr_unique == unique)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700862 return req;
863 }
864 return NULL;
865}
866
867static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
868 unsigned nbytes)
869{
870 unsigned reqsize = sizeof(struct fuse_out_header);
871
872 if (out->h.error)
873 return nbytes != reqsize ? -EINVAL : 0;
874
875 reqsize += len_args(out->numargs, out->args);
876
877 if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
878 return -EINVAL;
879 else if (reqsize > nbytes) {
880 struct fuse_arg *lastarg = &out->args[out->numargs-1];
881 unsigned diffsize = reqsize - nbytes;
882 if (diffsize > lastarg->size)
883 return -EINVAL;
884 lastarg->size -= diffsize;
885 }
886 return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
887 out->page_zeroing);
888}
889
890/*
891 * Write a single reply to a request. First the header is copied from
892 * the write buffer. The request is then searched on the processing
893 * list by the unique ID found in the header. If found, then remove
894 * it from the list and copy the rest of the buffer to the request.
895 * The request is finished by calling request_end()
896 */
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700897static ssize_t fuse_dev_write(struct kiocb *iocb, const struct iovec *iov,
898 unsigned long nr_segs, loff_t pos)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700899{
900 int err;
901 unsigned nbytes = iov_length(iov, nr_segs);
902 struct fuse_req *req;
903 struct fuse_out_header oh;
904 struct fuse_copy_state cs;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700905 struct fuse_conn *fc = fuse_get_conn(iocb->ki_filp);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700906 if (!fc)
Miklos Szeredia87046d2006-04-10 22:54:56 -0700907 return -EPERM;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700908
Miklos Szeredid7133112006-04-10 22:54:55 -0700909 fuse_copy_init(&cs, fc, 0, NULL, iov, nr_segs);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700910 if (nbytes < sizeof(struct fuse_out_header))
911 return -EINVAL;
912
913 err = fuse_copy_one(&cs, &oh, sizeof(oh));
914 if (err)
915 goto err_finish;
Tejun Heo85993962008-11-26 12:03:55 +0100916
Miklos Szeredi334f4852005-09-09 13:10:27 -0700917 err = -EINVAL;
Tejun Heo85993962008-11-26 12:03:55 +0100918 if (oh.len != nbytes)
919 goto err_finish;
920
921 /*
922 * Zero oh.unique indicates unsolicited notification message
923 * and error contains notification code.
924 */
925 if (!oh.unique) {
926 err = fuse_notify(fc, oh.error, nbytes - sizeof(oh), &cs);
927 fuse_copy_finish(&cs);
928 return err ? err : nbytes;
929 }
930
931 err = -EINVAL;
932 if (oh.error <= -1000 || oh.error > 0)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700933 goto err_finish;
934
Miklos Szeredid7133112006-04-10 22:54:55 -0700935 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800936 err = -ENOENT;
937 if (!fc->connected)
938 goto err_unlock;
939
Miklos Szeredi334f4852005-09-09 13:10:27 -0700940 req = request_find(fc, oh.unique);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700941 if (!req)
942 goto err_unlock;
943
Miklos Szeredif9a28422006-06-25 05:48:53 -0700944 if (req->aborted) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700945 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700946 fuse_copy_finish(&cs);
Miklos Szeredid7133112006-04-10 22:54:55 -0700947 spin_lock(&fc->lock);
Miklos Szeredi222f1d62006-01-16 22:14:25 -0800948 request_end(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700949 return -ENOENT;
950 }
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700951 /* Is it an interrupt reply? */
952 if (req->intr_unique == oh.unique) {
953 err = -EINVAL;
954 if (nbytes != sizeof(struct fuse_out_header))
955 goto err_unlock;
956
957 if (oh.error == -ENOSYS)
958 fc->no_interrupt = 1;
959 else if (oh.error == -EAGAIN)
960 queue_interrupt(fc, req);
961
962 spin_unlock(&fc->lock);
963 fuse_copy_finish(&cs);
964 return nbytes;
965 }
966
967 req->state = FUSE_REQ_WRITING;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800968 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700969 req->out.h = oh;
970 req->locked = 1;
971 cs.req = req;
Miklos Szeredid7133112006-04-10 22:54:55 -0700972 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700973
974 err = copy_out_args(&cs, &req->out, nbytes);
975 fuse_copy_finish(&cs);
976
Miklos Szeredid7133112006-04-10 22:54:55 -0700977 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700978 req->locked = 0;
979 if (!err) {
Miklos Szeredif9a28422006-06-25 05:48:53 -0700980 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700981 err = -ENOENT;
Miklos Szeredif9a28422006-06-25 05:48:53 -0700982 } else if (!req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700983 req->out.h.error = -EIO;
984 request_end(fc, req);
985
986 return err ? err : nbytes;
987
988 err_unlock:
Miklos Szeredid7133112006-04-10 22:54:55 -0700989 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700990 err_finish:
991 fuse_copy_finish(&cs);
992 return err;
993}
994
Miklos Szeredi334f4852005-09-09 13:10:27 -0700995static unsigned fuse_dev_poll(struct file *file, poll_table *wait)
996{
Miklos Szeredi334f4852005-09-09 13:10:27 -0700997 unsigned mask = POLLOUT | POLLWRNORM;
Miklos Szeredi7025d9a2006-04-10 22:54:50 -0700998 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700999 if (!fc)
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07001000 return POLLERR;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001001
1002 poll_wait(file, &fc->waitq, wait);
1003
Miklos Szeredid7133112006-04-10 22:54:55 -07001004 spin_lock(&fc->lock);
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07001005 if (!fc->connected)
1006 mask = POLLERR;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001007 else if (request_pending(fc))
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07001008 mask |= POLLIN | POLLRDNORM;
Miklos Szeredid7133112006-04-10 22:54:55 -07001009 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001010
1011 return mask;
1012}
1013
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001014/*
1015 * Abort all requests on the given list (pending or processing)
1016 *
Miklos Szeredid7133112006-04-10 22:54:55 -07001017 * This function releases and reacquires fc->lock
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001018 */
Miklos Szeredi334f4852005-09-09 13:10:27 -07001019static void end_requests(struct fuse_conn *fc, struct list_head *head)
Harvey Harrison5d9ec852008-12-02 14:49:42 +01001020__releases(&fc->lock)
1021__acquires(&fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001022{
1023 while (!list_empty(head)) {
1024 struct fuse_req *req;
1025 req = list_entry(head->next, struct fuse_req, list);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001026 req->out.h.error = -ECONNABORTED;
1027 request_end(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -07001028 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001029 }
1030}
1031
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001032/*
1033 * Abort requests under I/O
1034 *
Miklos Szeredif9a28422006-06-25 05:48:53 -07001035 * The requests are set to aborted and finished, and the request
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001036 * waiter is woken up. This will make request_wait_answer() wait
1037 * until the request is unlocked and then return.
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08001038 *
1039 * If the request is asynchronous, then the end function needs to be
1040 * called after waiting for the request to be unlocked (if it was
1041 * locked).
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001042 */
1043static void end_io_requests(struct fuse_conn *fc)
Harvey Harrison5d9ec852008-12-02 14:49:42 +01001044__releases(&fc->lock)
1045__acquires(&fc->lock)
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001046{
1047 while (!list_empty(&fc->io)) {
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08001048 struct fuse_req *req =
1049 list_entry(fc->io.next, struct fuse_req, list);
1050 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
1051
Miklos Szeredif9a28422006-06-25 05:48:53 -07001052 req->aborted = 1;
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001053 req->out.h.error = -ECONNABORTED;
1054 req->state = FUSE_REQ_FINISHED;
1055 list_del_init(&req->list);
1056 wake_up(&req->waitq);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08001057 if (end) {
1058 req->end = NULL;
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08001059 __fuse_get_request(req);
Miklos Szeredid7133112006-04-10 22:54:55 -07001060 spin_unlock(&fc->lock);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08001061 wait_event(req->waitq, !req->locked);
1062 end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +01001063 fuse_put_request(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -07001064 spin_lock(&fc->lock);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08001065 }
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001066 }
1067}
1068
1069/*
1070 * Abort all requests.
1071 *
1072 * Emergency exit in case of a malicious or accidental deadlock, or
1073 * just a hung filesystem.
1074 *
1075 * The same effect is usually achievable through killing the
1076 * filesystem daemon and all users of the filesystem. The exception
1077 * is the combination of an asynchronous request and the tricky
1078 * deadlock (see Documentation/filesystems/fuse.txt).
1079 *
1080 * During the aborting, progression of requests from the pending and
1081 * processing lists onto the io list, and progression of new requests
1082 * onto the pending list is prevented by req->connected being false.
1083 *
1084 * Progression of requests under I/O to the processing list is
Miklos Szeredif9a28422006-06-25 05:48:53 -07001085 * prevented by the req->aborted flag being true for these requests.
1086 * For this reason requests on the io list must be aborted first.
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001087 */
1088void fuse_abort_conn(struct fuse_conn *fc)
1089{
Miklos Szeredid7133112006-04-10 22:54:55 -07001090 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001091 if (fc->connected) {
1092 fc->connected = 0;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -07001093 fc->blocked = 0;
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001094 end_io_requests(fc);
1095 end_requests(fc, &fc->pending);
1096 end_requests(fc, &fc->processing);
1097 wake_up_all(&fc->waitq);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -07001098 wake_up_all(&fc->blocked_waitq);
Jeff Dike385a17b2006-04-10 22:54:52 -07001099 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001100 }
Miklos Szeredid7133112006-04-10 22:54:55 -07001101 spin_unlock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001102}
1103
Miklos Szeredi334f4852005-09-09 13:10:27 -07001104static int fuse_dev_release(struct inode *inode, struct file *file)
1105{
Miklos Szeredi0720b312006-04-10 22:54:55 -07001106 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001107 if (fc) {
Miklos Szeredid7133112006-04-10 22:54:55 -07001108 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -07001109 fc->connected = 0;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001110 end_requests(fc, &fc->pending);
1111 end_requests(fc, &fc->processing);
Miklos Szeredid7133112006-04-10 22:54:55 -07001112 spin_unlock(&fc->lock);
Miklos Szeredibafa9652006-06-25 05:48:51 -07001113 fuse_conn_put(fc);
Jeff Dike385a17b2006-04-10 22:54:52 -07001114 }
Miklos Szeredif543f252006-01-16 22:14:35 -08001115
Miklos Szeredi334f4852005-09-09 13:10:27 -07001116 return 0;
1117}
1118
Jeff Dike385a17b2006-04-10 22:54:52 -07001119static int fuse_dev_fasync(int fd, struct file *file, int on)
1120{
1121 struct fuse_conn *fc = fuse_get_conn(file);
1122 if (!fc)
Miklos Szeredia87046d2006-04-10 22:54:56 -07001123 return -EPERM;
Jeff Dike385a17b2006-04-10 22:54:52 -07001124
1125 /* No locking - fasync_helper does its own locking */
1126 return fasync_helper(fd, file, on, &fc->fasync);
1127}
1128
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001129const struct file_operations fuse_dev_operations = {
Miklos Szeredi334f4852005-09-09 13:10:27 -07001130 .owner = THIS_MODULE,
1131 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07001132 .read = do_sync_read,
1133 .aio_read = fuse_dev_read,
1134 .write = do_sync_write,
1135 .aio_write = fuse_dev_write,
Miklos Szeredi334f4852005-09-09 13:10:27 -07001136 .poll = fuse_dev_poll,
1137 .release = fuse_dev_release,
Jeff Dike385a17b2006-04-10 22:54:52 -07001138 .fasync = fuse_dev_fasync,
Miklos Szeredi334f4852005-09-09 13:10:27 -07001139};
1140
1141static struct miscdevice fuse_miscdevice = {
1142 .minor = FUSE_MINOR,
1143 .name = "fuse",
1144 .fops = &fuse_dev_operations,
1145};
1146
1147int __init fuse_dev_init(void)
1148{
1149 int err = -ENOMEM;
1150 fuse_req_cachep = kmem_cache_create("fuse_request",
1151 sizeof(struct fuse_req),
Paul Mundt20c2df82007-07-20 10:11:58 +09001152 0, 0, NULL);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001153 if (!fuse_req_cachep)
1154 goto out;
1155
1156 err = misc_register(&fuse_miscdevice);
1157 if (err)
1158 goto out_cache_clean;
1159
1160 return 0;
1161
1162 out_cache_clean:
1163 kmem_cache_destroy(fuse_req_cachep);
1164 out:
1165 return err;
1166}
1167
1168void fuse_dev_cleanup(void)
1169{
1170 misc_deregister(&fuse_miscdevice);
1171 kmem_cache_destroy(fuse_req_cachep);
1172}