blob: 8fed2ed12f38b3ab468a042964ae800479f18d18 [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}
Tejun Heo08cbf542009-04-14 10:54:53 +090049EXPORT_SYMBOL_GPL(fuse_request_alloc);
Miklos Szeredi334f4852005-09-09 13:10:27 -070050
Miklos Szeredi3be5a522008-04-30 00:54:41 -070051struct fuse_req *fuse_request_alloc_nofs(void)
52{
53 struct fuse_req *req = kmem_cache_alloc(fuse_req_cachep, GFP_NOFS);
54 if (req)
55 fuse_request_init(req);
56 return req;
57}
58
Miklos Szeredi334f4852005-09-09 13:10:27 -070059void fuse_request_free(struct fuse_req *req)
60{
61 kmem_cache_free(fuse_req_cachep, req);
62}
63
Miklos Szeredi8bfc0162006-01-16 22:14:28 -080064static void block_sigs(sigset_t *oldset)
Miklos Szeredi334f4852005-09-09 13:10:27 -070065{
66 sigset_t mask;
67
68 siginitsetinv(&mask, sigmask(SIGKILL));
69 sigprocmask(SIG_BLOCK, &mask, oldset);
70}
71
Miklos Szeredi8bfc0162006-01-16 22:14:28 -080072static void restore_sigs(sigset_t *oldset)
Miklos Szeredi334f4852005-09-09 13:10:27 -070073{
74 sigprocmask(SIG_SETMASK, oldset, NULL);
75}
76
Miklos Szeredi334f4852005-09-09 13:10:27 -070077static void __fuse_get_request(struct fuse_req *req)
78{
79 atomic_inc(&req->count);
80}
81
82/* Must be called with > 1 refcount */
83static void __fuse_put_request(struct fuse_req *req)
84{
85 BUG_ON(atomic_read(&req->count) < 2);
86 atomic_dec(&req->count);
87}
88
Miklos Szeredi33649c92006-06-25 05:48:52 -070089static void fuse_req_init_context(struct fuse_req *req)
90{
David Howells2186a712008-11-14 10:38:53 +110091 req->in.h.uid = current_fsuid();
92 req->in.h.gid = current_fsgid();
Miklos Szeredi33649c92006-06-25 05:48:52 -070093 req->in.h.pid = current->pid;
94}
95
Miklos Szeredice1d5a42006-04-10 22:54:58 -070096struct fuse_req *fuse_get_req(struct fuse_conn *fc)
Miklos Szeredi334f4852005-09-09 13:10:27 -070097{
Miklos Szeredi08a53cd2006-04-10 22:54:59 -070098 struct fuse_req *req;
99 sigset_t oldset;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200100 int intr;
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700101 int err;
102
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200103 atomic_inc(&fc->num_waiting);
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700104 block_sigs(&oldset);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200105 intr = wait_event_interruptible(fc->blocked_waitq, !fc->blocked);
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700106 restore_sigs(&oldset);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200107 err = -EINTR;
108 if (intr)
109 goto out;
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700110
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700111 err = -ENOTCONN;
112 if (!fc->connected)
113 goto out;
114
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700115 req = fuse_request_alloc();
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200116 err = -ENOMEM;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700117 if (!req)
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200118 goto out;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700119
Miklos Szeredi33649c92006-06-25 05:48:52 -0700120 fuse_req_init_context(req);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200121 req->waiting = 1;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700122 return req;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200123
124 out:
125 atomic_dec(&fc->num_waiting);
126 return ERR_PTR(err);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700127}
Tejun Heo08cbf542009-04-14 10:54:53 +0900128EXPORT_SYMBOL_GPL(fuse_get_req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700129
Miklos Szeredi33649c92006-06-25 05:48:52 -0700130/*
131 * Return request in fuse_file->reserved_req. However that may
132 * currently be in use. If that is the case, wait for it to become
133 * available.
134 */
135static struct fuse_req *get_reserved_req(struct fuse_conn *fc,
136 struct file *file)
137{
138 struct fuse_req *req = NULL;
139 struct fuse_file *ff = file->private_data;
140
141 do {
Miklos Szeredide5e3de2007-10-16 23:31:00 -0700142 wait_event(fc->reserved_req_waitq, ff->reserved_req);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700143 spin_lock(&fc->lock);
144 if (ff->reserved_req) {
145 req = ff->reserved_req;
146 ff->reserved_req = NULL;
147 get_file(file);
148 req->stolen_file = file;
149 }
150 spin_unlock(&fc->lock);
151 } while (!req);
152
153 return req;
154}
155
156/*
157 * Put stolen request back into fuse_file->reserved_req
158 */
159static void put_reserved_req(struct fuse_conn *fc, struct fuse_req *req)
160{
161 struct file *file = req->stolen_file;
162 struct fuse_file *ff = file->private_data;
163
164 spin_lock(&fc->lock);
165 fuse_request_init(req);
166 BUG_ON(ff->reserved_req);
167 ff->reserved_req = req;
Miklos Szeredide5e3de2007-10-16 23:31:00 -0700168 wake_up_all(&fc->reserved_req_waitq);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700169 spin_unlock(&fc->lock);
170 fput(file);
171}
172
173/*
174 * Gets a requests for a file operation, always succeeds
175 *
176 * This is used for sending the FLUSH request, which must get to
177 * userspace, due to POSIX locks which may need to be unlocked.
178 *
179 * If allocation fails due to OOM, use the reserved request in
180 * fuse_file.
181 *
182 * This is very unlikely to deadlock accidentally, since the
183 * filesystem should not have it's own file open. If deadlock is
184 * intentional, it can still be broken by "aborting" the filesystem.
185 */
186struct fuse_req *fuse_get_req_nofail(struct fuse_conn *fc, struct file *file)
187{
188 struct fuse_req *req;
189
190 atomic_inc(&fc->num_waiting);
191 wait_event(fc->blocked_waitq, !fc->blocked);
192 req = fuse_request_alloc();
193 if (!req)
194 req = get_reserved_req(fc, file);
195
196 fuse_req_init_context(req);
197 req->waiting = 1;
198 return req;
199}
200
Miklos Szeredi334f4852005-09-09 13:10:27 -0700201void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req)
202{
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800203 if (atomic_dec_and_test(&req->count)) {
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200204 if (req->waiting)
205 atomic_dec(&fc->num_waiting);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700206
207 if (req->stolen_file)
208 put_reserved_req(fc, req);
209 else
210 fuse_request_free(req);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800211 }
212}
Tejun Heo08cbf542009-04-14 10:54:53 +0900213EXPORT_SYMBOL_GPL(fuse_put_request);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800214
Miklos Szeredid12def12008-02-06 01:38:39 -0800215static unsigned len_args(unsigned numargs, struct fuse_arg *args)
216{
217 unsigned nbytes = 0;
218 unsigned i;
219
220 for (i = 0; i < numargs; i++)
221 nbytes += args[i].size;
222
223 return nbytes;
224}
225
226static u64 fuse_get_unique(struct fuse_conn *fc)
227{
228 fc->reqctr++;
229 /* zero is special */
230 if (fc->reqctr == 0)
231 fc->reqctr = 1;
232
233 return fc->reqctr;
234}
235
236static void queue_request(struct fuse_conn *fc, struct fuse_req *req)
237{
238 req->in.h.unique = fuse_get_unique(fc);
239 req->in.h.len = sizeof(struct fuse_in_header) +
240 len_args(req->in.numargs, (struct fuse_arg *) req->in.args);
241 list_add_tail(&req->list, &fc->pending);
242 req->state = FUSE_REQ_PENDING;
243 if (!req->waiting) {
244 req->waiting = 1;
245 atomic_inc(&fc->num_waiting);
246 }
247 wake_up(&fc->waitq);
248 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
249}
250
251static void flush_bg_queue(struct fuse_conn *fc)
252{
253 while (fc->active_background < FUSE_MAX_BACKGROUND &&
254 !list_empty(&fc->bg_queue)) {
255 struct fuse_req *req;
256
257 req = list_entry(fc->bg_queue.next, struct fuse_req, list);
258 list_del(&req->list);
259 fc->active_background++;
260 queue_request(fc, req);
261 }
262}
263
Miklos Szeredi6dbbcb12006-04-26 10:49:06 +0200264/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700265 * This function is called when a request is finished. Either a reply
Miklos Szeredif9a28422006-06-25 05:48:53 -0700266 * has arrived or it was aborted (and not yet sent) or some error
Miklos Szeredif43b1552006-01-16 22:14:26 -0800267 * occurred during communication with userspace, or the device file
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700268 * was closed. The requester thread is woken up (if still waiting),
269 * the 'end' callback is called if given, else the reference to the
270 * request is released
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800271 *
Miklos Szeredid7133112006-04-10 22:54:55 -0700272 * Called with fc->lock, unlocks it
Miklos Szeredi334f4852005-09-09 13:10:27 -0700273 */
274static void request_end(struct fuse_conn *fc, struct fuse_req *req)
Harvey Harrison5d9ec852008-12-02 14:49:42 +0100275__releases(&fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700276{
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700277 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
278 req->end = NULL;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800279 list_del(&req->list);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700280 list_del(&req->intr_entry);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800281 req->state = FUSE_REQ_FINISHED;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700282 if (req->background) {
283 if (fc->num_background == FUSE_MAX_BACKGROUND) {
284 fc->blocked = 0;
285 wake_up_all(&fc->blocked_waitq);
286 }
Miklos Szeredi26c36792009-01-26 15:00:59 +0100287 if (fc->num_background == FUSE_CONGESTION_THRESHOLD &&
Tejun Heoa325f9b2009-04-14 10:54:52 +0900288 fc->connected && fc->bdi_initialized) {
Miklos Szeredif92b99b2007-10-16 23:30:59 -0700289 clear_bdi_congested(&fc->bdi, READ);
290 clear_bdi_congested(&fc->bdi, WRITE);
291 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700292 fc->num_background--;
Miklos Szeredid12def12008-02-06 01:38:39 -0800293 fc->active_background--;
294 flush_bg_queue(fc);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700295 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700296 spin_unlock(&fc->lock);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700297 wake_up(&req->waitq);
298 if (end)
299 end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100300 fuse_put_request(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700301}
302
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700303static void wait_answer_interruptible(struct fuse_conn *fc,
304 struct fuse_req *req)
Harvey Harrison5d9ec852008-12-02 14:49:42 +0100305__releases(&fc->lock)
306__acquires(&fc->lock)
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700307{
308 if (signal_pending(current))
309 return;
310
311 spin_unlock(&fc->lock);
312 wait_event_interruptible(req->waitq, req->state == FUSE_REQ_FINISHED);
313 spin_lock(&fc->lock);
314}
315
316static void queue_interrupt(struct fuse_conn *fc, struct fuse_req *req)
317{
318 list_add_tail(&req->intr_entry, &fc->interrupts);
319 wake_up(&fc->waitq);
320 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
321}
322
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700323static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
Harvey Harrison5d9ec852008-12-02 14:49:42 +0100324__releases(&fc->lock)
325__acquires(&fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700326{
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700327 if (!fc->no_interrupt) {
328 /* Any signal may interrupt this */
329 wait_answer_interruptible(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700330
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700331 if (req->aborted)
332 goto aborted;
333 if (req->state == FUSE_REQ_FINISHED)
334 return;
335
336 req->interrupted = 1;
337 if (req->state == FUSE_REQ_SENT)
338 queue_interrupt(fc, req);
339 }
340
Miklos Szeredia131de02007-10-16 23:31:04 -0700341 if (!req->force) {
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700342 sigset_t oldset;
343
344 /* Only fatal signals may interrupt this */
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700345 block_sigs(&oldset);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700346 wait_answer_interruptible(fc, req);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700347 restore_sigs(&oldset);
Miklos Szeredia131de02007-10-16 23:31:04 -0700348
349 if (req->aborted)
350 goto aborted;
351 if (req->state == FUSE_REQ_FINISHED)
352 return;
353
354 /* Request is not yet in userspace, bail out */
355 if (req->state == FUSE_REQ_PENDING) {
356 list_del(&req->list);
357 __fuse_put_request(req);
358 req->out.h.error = -EINTR;
359 return;
360 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700361 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700362
Miklos Szeredia131de02007-10-16 23:31:04 -0700363 /*
364 * Either request is already in userspace, or it was forced.
365 * Wait it out.
366 */
367 spin_unlock(&fc->lock);
368 wait_event(req->waitq, req->state == FUSE_REQ_FINISHED);
369 spin_lock(&fc->lock);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700370
Miklos Szeredia131de02007-10-16 23:31:04 -0700371 if (!req->aborted)
372 return;
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700373
374 aborted:
Miklos Szeredia131de02007-10-16 23:31:04 -0700375 BUG_ON(req->state != FUSE_REQ_FINISHED);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700376 if (req->locked) {
377 /* This is uninterruptible sleep, because data is
378 being copied to/from the buffers of req. During
379 locked state, there mustn't be any filesystem
380 operation (e.g. page fault), since that could lead
381 to deadlock */
Miklos Szeredid7133112006-04-10 22:54:55 -0700382 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700383 wait_event(req->waitq, !req->locked);
Miklos Szeredid7133112006-04-10 22:54:55 -0700384 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700385 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700386}
387
Tejun Heob93f8582008-11-26 12:03:55 +0100388void fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700389{
390 req->isreply = 1;
Miklos Szeredid7133112006-04-10 22:54:55 -0700391 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700392 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700393 req->out.h.error = -ENOTCONN;
394 else if (fc->conn_error)
395 req->out.h.error = -ECONNREFUSED;
396 else {
397 queue_request(fc, req);
398 /* acquire extra reference, since request is still needed
399 after request_end() */
400 __fuse_get_request(req);
401
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700402 request_wait_answer(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700403 }
Miklos Szeredid7133112006-04-10 22:54:55 -0700404 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700405}
Tejun Heo08cbf542009-04-14 10:54:53 +0900406EXPORT_SYMBOL_GPL(fuse_request_send);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700407
Tejun Heob93f8582008-11-26 12:03:55 +0100408static void fuse_request_send_nowait_locked(struct fuse_conn *fc,
409 struct fuse_req *req)
Miklos Szeredid12def12008-02-06 01:38:39 -0800410{
411 req->background = 1;
412 fc->num_background++;
413 if (fc->num_background == FUSE_MAX_BACKGROUND)
414 fc->blocked = 1;
Tejun Heoa325f9b2009-04-14 10:54:52 +0900415 if (fc->num_background == FUSE_CONGESTION_THRESHOLD &&
416 fc->bdi_initialized) {
Miklos Szeredid12def12008-02-06 01:38:39 -0800417 set_bdi_congested(&fc->bdi, READ);
418 set_bdi_congested(&fc->bdi, WRITE);
419 }
420 list_add_tail(&req->list, &fc->bg_queue);
421 flush_bg_queue(fc);
422}
423
Tejun Heob93f8582008-11-26 12:03:55 +0100424static void fuse_request_send_nowait(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700425{
Miklos Szeredid7133112006-04-10 22:54:55 -0700426 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700427 if (fc->connected) {
Tejun Heob93f8582008-11-26 12:03:55 +0100428 fuse_request_send_nowait_locked(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700429 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700430 } else {
431 req->out.h.error = -ENOTCONN;
432 request_end(fc, req);
433 }
434}
435
Tejun Heob93f8582008-11-26 12:03:55 +0100436void fuse_request_send_noreply(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700437{
438 req->isreply = 0;
Tejun Heob93f8582008-11-26 12:03:55 +0100439 fuse_request_send_nowait(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700440}
441
Tejun Heob93f8582008-11-26 12:03:55 +0100442void fuse_request_send_background(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700443{
444 req->isreply = 1;
Tejun Heob93f8582008-11-26 12:03:55 +0100445 fuse_request_send_nowait(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700446}
Tejun Heo08cbf542009-04-14 10:54:53 +0900447EXPORT_SYMBOL_GPL(fuse_request_send_background);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700448
Miklos Szeredi334f4852005-09-09 13:10:27 -0700449/*
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700450 * Called under fc->lock
451 *
452 * fc->connected must have been checked previously
453 */
Tejun Heob93f8582008-11-26 12:03:55 +0100454void fuse_request_send_background_locked(struct fuse_conn *fc,
455 struct fuse_req *req)
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700456{
457 req->isreply = 1;
Tejun Heob93f8582008-11-26 12:03:55 +0100458 fuse_request_send_nowait_locked(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700459}
460
461/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700462 * Lock the request. Up to the next unlock_request() there mustn't be
463 * anything that could cause a page-fault. If the request was already
Miklos Szeredif9a28422006-06-25 05:48:53 -0700464 * aborted bail out.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700465 */
Miklos Szeredid7133112006-04-10 22:54:55 -0700466static int lock_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700467{
468 int err = 0;
469 if (req) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700470 spin_lock(&fc->lock);
Miklos Szeredif9a28422006-06-25 05:48:53 -0700471 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700472 err = -ENOENT;
473 else
474 req->locked = 1;
Miklos Szeredid7133112006-04-10 22:54:55 -0700475 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700476 }
477 return err;
478}
479
480/*
Miklos Szeredif9a28422006-06-25 05:48:53 -0700481 * Unlock request. If it was aborted during being locked, the
Miklos Szeredi334f4852005-09-09 13:10:27 -0700482 * requester thread is currently waiting for it to be unlocked, so
483 * wake it up.
484 */
Miklos Szeredid7133112006-04-10 22:54:55 -0700485static void unlock_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700486{
487 if (req) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700488 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700489 req->locked = 0;
Miklos Szeredif9a28422006-06-25 05:48:53 -0700490 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700491 wake_up(&req->waitq);
Miklos Szeredid7133112006-04-10 22:54:55 -0700492 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700493 }
494}
495
496struct fuse_copy_state {
Miklos Szeredid7133112006-04-10 22:54:55 -0700497 struct fuse_conn *fc;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700498 int write;
499 struct fuse_req *req;
500 const struct iovec *iov;
501 unsigned long nr_segs;
502 unsigned long seglen;
503 unsigned long addr;
504 struct page *pg;
505 void *mapaddr;
506 void *buf;
507 unsigned len;
508};
509
Miklos Szeredid7133112006-04-10 22:54:55 -0700510static void fuse_copy_init(struct fuse_copy_state *cs, struct fuse_conn *fc,
511 int write, struct fuse_req *req,
512 const struct iovec *iov, unsigned long nr_segs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700513{
514 memset(cs, 0, sizeof(*cs));
Miklos Szeredid7133112006-04-10 22:54:55 -0700515 cs->fc = fc;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700516 cs->write = write;
517 cs->req = req;
518 cs->iov = iov;
519 cs->nr_segs = nr_segs;
520}
521
522/* Unmap and put previous page of userspace buffer */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800523static void fuse_copy_finish(struct fuse_copy_state *cs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700524{
525 if (cs->mapaddr) {
526 kunmap_atomic(cs->mapaddr, KM_USER0);
527 if (cs->write) {
528 flush_dcache_page(cs->pg);
529 set_page_dirty_lock(cs->pg);
530 }
531 put_page(cs->pg);
532 cs->mapaddr = NULL;
533 }
534}
535
536/*
537 * Get another pagefull of userspace buffer, and map it to kernel
538 * address space, and lock request
539 */
540static int fuse_copy_fill(struct fuse_copy_state *cs)
541{
542 unsigned long offset;
543 int err;
544
Miklos Szeredid7133112006-04-10 22:54:55 -0700545 unlock_request(cs->fc, cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700546 fuse_copy_finish(cs);
547 if (!cs->seglen) {
548 BUG_ON(!cs->nr_segs);
549 cs->seglen = cs->iov[0].iov_len;
550 cs->addr = (unsigned long) cs->iov[0].iov_base;
Miklos Szeredi1729a162008-11-26 12:03:54 +0100551 cs->iov++;
552 cs->nr_segs--;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700553 }
554 down_read(&current->mm->mmap_sem);
555 err = get_user_pages(current, current->mm, cs->addr, 1, cs->write, 0,
556 &cs->pg, NULL);
557 up_read(&current->mm->mmap_sem);
558 if (err < 0)
559 return err;
560 BUG_ON(err != 1);
561 offset = cs->addr % PAGE_SIZE;
562 cs->mapaddr = kmap_atomic(cs->pg, KM_USER0);
563 cs->buf = cs->mapaddr + offset;
564 cs->len = min(PAGE_SIZE - offset, cs->seglen);
565 cs->seglen -= cs->len;
566 cs->addr += cs->len;
567
Miklos Szeredid7133112006-04-10 22:54:55 -0700568 return lock_request(cs->fc, cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700569}
570
571/* Do as much copy to/from userspace buffer as we can */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800572static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700573{
574 unsigned ncpy = min(*size, cs->len);
575 if (val) {
576 if (cs->write)
577 memcpy(cs->buf, *val, ncpy);
578 else
579 memcpy(*val, cs->buf, ncpy);
580 *val += ncpy;
581 }
582 *size -= ncpy;
583 cs->len -= ncpy;
584 cs->buf += ncpy;
585 return ncpy;
586}
587
588/*
589 * Copy a page in the request to/from the userspace buffer. Must be
590 * done atomically
591 */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800592static int fuse_copy_page(struct fuse_copy_state *cs, struct page *page,
593 unsigned offset, unsigned count, int zeroing)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700594{
595 if (page && zeroing && count < PAGE_SIZE) {
596 void *mapaddr = kmap_atomic(page, KM_USER1);
597 memset(mapaddr, 0, PAGE_SIZE);
598 kunmap_atomic(mapaddr, KM_USER1);
599 }
600 while (count) {
Miklos Szeredi1729a162008-11-26 12:03:54 +0100601 if (!cs->len) {
602 int err = fuse_copy_fill(cs);
603 if (err)
604 return err;
605 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700606 if (page) {
607 void *mapaddr = kmap_atomic(page, KM_USER1);
608 void *buf = mapaddr + offset;
609 offset += fuse_copy_do(cs, &buf, &count);
610 kunmap_atomic(mapaddr, KM_USER1);
611 } else
612 offset += fuse_copy_do(cs, NULL, &count);
613 }
614 if (page && !cs->write)
615 flush_dcache_page(page);
616 return 0;
617}
618
619/* Copy pages in the request to/from userspace buffer */
620static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
621 int zeroing)
622{
623 unsigned i;
624 struct fuse_req *req = cs->req;
625 unsigned offset = req->page_offset;
626 unsigned count = min(nbytes, (unsigned) PAGE_SIZE - offset);
627
628 for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
629 struct page *page = req->pages[i];
630 int err = fuse_copy_page(cs, page, offset, count, zeroing);
631 if (err)
632 return err;
633
634 nbytes -= count;
635 count = min(nbytes, (unsigned) PAGE_SIZE);
636 offset = 0;
637 }
638 return 0;
639}
640
641/* Copy a single argument in the request to/from userspace buffer */
642static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
643{
644 while (size) {
Miklos Szeredi1729a162008-11-26 12:03:54 +0100645 if (!cs->len) {
646 int err = fuse_copy_fill(cs);
647 if (err)
648 return err;
649 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700650 fuse_copy_do(cs, &val, &size);
651 }
652 return 0;
653}
654
655/* Copy request arguments to/from userspace buffer */
656static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
657 unsigned argpages, struct fuse_arg *args,
658 int zeroing)
659{
660 int err = 0;
661 unsigned i;
662
663 for (i = 0; !err && i < numargs; i++) {
664 struct fuse_arg *arg = &args[i];
665 if (i == numargs - 1 && argpages)
666 err = fuse_copy_pages(cs, arg->size, zeroing);
667 else
668 err = fuse_copy_one(cs, arg->value, arg->size);
669 }
670 return err;
671}
672
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700673static int request_pending(struct fuse_conn *fc)
674{
675 return !list_empty(&fc->pending) || !list_empty(&fc->interrupts);
676}
677
Miklos Szeredi334f4852005-09-09 13:10:27 -0700678/* Wait until a request is available on the pending list */
679static void request_wait(struct fuse_conn *fc)
Harvey Harrison5d9ec852008-12-02 14:49:42 +0100680__releases(&fc->lock)
681__acquires(&fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700682{
683 DECLARE_WAITQUEUE(wait, current);
684
685 add_wait_queue_exclusive(&fc->waitq, &wait);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700686 while (fc->connected && !request_pending(fc)) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700687 set_current_state(TASK_INTERRUPTIBLE);
688 if (signal_pending(current))
689 break;
690
Miklos Szeredid7133112006-04-10 22:54:55 -0700691 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700692 schedule();
Miklos Szeredid7133112006-04-10 22:54:55 -0700693 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700694 }
695 set_current_state(TASK_RUNNING);
696 remove_wait_queue(&fc->waitq, &wait);
697}
698
699/*
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700700 * Transfer an interrupt request to userspace
701 *
702 * Unlike other requests this is assembled on demand, without a need
703 * to allocate a separate fuse_req structure.
704 *
705 * Called with fc->lock held, releases it
706 */
707static int fuse_read_interrupt(struct fuse_conn *fc, struct fuse_req *req,
708 const struct iovec *iov, unsigned long nr_segs)
Harvey Harrison5d9ec852008-12-02 14:49:42 +0100709__releases(&fc->lock)
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700710{
711 struct fuse_copy_state cs;
712 struct fuse_in_header ih;
713 struct fuse_interrupt_in arg;
714 unsigned reqsize = sizeof(ih) + sizeof(arg);
715 int err;
716
717 list_del_init(&req->intr_entry);
718 req->intr_unique = fuse_get_unique(fc);
719 memset(&ih, 0, sizeof(ih));
720 memset(&arg, 0, sizeof(arg));
721 ih.len = reqsize;
722 ih.opcode = FUSE_INTERRUPT;
723 ih.unique = req->intr_unique;
724 arg.unique = req->in.h.unique;
725
726 spin_unlock(&fc->lock);
727 if (iov_length(iov, nr_segs) < reqsize)
728 return -EINVAL;
729
730 fuse_copy_init(&cs, fc, 1, NULL, iov, nr_segs);
731 err = fuse_copy_one(&cs, &ih, sizeof(ih));
732 if (!err)
733 err = fuse_copy_one(&cs, &arg, sizeof(arg));
734 fuse_copy_finish(&cs);
735
736 return err ? err : reqsize;
737}
738
739/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700740 * Read a single request into the userspace filesystem's buffer. This
741 * function waits until a request is available, then removes it from
742 * the pending list and copies request data to userspace buffer. If
Miklos Szeredif9a28422006-06-25 05:48:53 -0700743 * no reply is needed (FORGET) or request has been aborted or there
744 * was an error during the copying then it's finished by calling
Miklos Szeredi334f4852005-09-09 13:10:27 -0700745 * request_end(). Otherwise add it to the processing list, and set
746 * the 'sent' flag.
747 */
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700748static ssize_t fuse_dev_read(struct kiocb *iocb, const struct iovec *iov,
749 unsigned long nr_segs, loff_t pos)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700750{
751 int err;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700752 struct fuse_req *req;
753 struct fuse_in *in;
754 struct fuse_copy_state cs;
755 unsigned reqsize;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700756 struct file *file = iocb->ki_filp;
Miklos Szeredi0720b312006-04-10 22:54:55 -0700757 struct fuse_conn *fc = fuse_get_conn(file);
758 if (!fc)
759 return -EPERM;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700760
Miklos Szeredi1d3d7522006-01-06 00:19:40 -0800761 restart:
Miklos Szeredid7133112006-04-10 22:54:55 -0700762 spin_lock(&fc->lock);
Jeff Dikee5ac1d12006-04-10 22:54:53 -0700763 err = -EAGAIN;
764 if ((file->f_flags & O_NONBLOCK) && fc->connected &&
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700765 !request_pending(fc))
Jeff Dikee5ac1d12006-04-10 22:54:53 -0700766 goto err_unlock;
767
Miklos Szeredi334f4852005-09-09 13:10:27 -0700768 request_wait(fc);
769 err = -ENODEV;
Miklos Szeredi9ba7cbb2006-01-16 22:14:34 -0800770 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700771 goto err_unlock;
772 err = -ERESTARTSYS;
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700773 if (!request_pending(fc))
Miklos Szeredi334f4852005-09-09 13:10:27 -0700774 goto err_unlock;
775
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700776 if (!list_empty(&fc->interrupts)) {
777 req = list_entry(fc->interrupts.next, struct fuse_req,
778 intr_entry);
779 return fuse_read_interrupt(fc, req, iov, nr_segs);
780 }
781
Miklos Szeredi334f4852005-09-09 13:10:27 -0700782 req = list_entry(fc->pending.next, struct fuse_req, list);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800783 req->state = FUSE_REQ_READING;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800784 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700785
786 in = &req->in;
Miklos Szeredi1d3d7522006-01-06 00:19:40 -0800787 reqsize = in->h.len;
788 /* If request is too large, reply with an error and restart the read */
789 if (iov_length(iov, nr_segs) < reqsize) {
790 req->out.h.error = -EIO;
791 /* SETXATTR is special, since it may contain too large data */
792 if (in->h.opcode == FUSE_SETXATTR)
793 req->out.h.error = -E2BIG;
794 request_end(fc, req);
795 goto restart;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700796 }
Miklos Szeredid7133112006-04-10 22:54:55 -0700797 spin_unlock(&fc->lock);
798 fuse_copy_init(&cs, fc, 1, req, iov, nr_segs);
Miklos Szeredi1d3d7522006-01-06 00:19:40 -0800799 err = fuse_copy_one(&cs, &in->h, sizeof(in->h));
800 if (!err)
801 err = fuse_copy_args(&cs, in->numargs, in->argpages,
802 (struct fuse_arg *) in->args, 0);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700803 fuse_copy_finish(&cs);
Miklos Szeredid7133112006-04-10 22:54:55 -0700804 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700805 req->locked = 0;
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -0700806 if (req->aborted) {
807 request_end(fc, req);
808 return -ENODEV;
809 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700810 if (err) {
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -0700811 req->out.h.error = -EIO;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700812 request_end(fc, req);
813 return err;
814 }
815 if (!req->isreply)
816 request_end(fc, req);
817 else {
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800818 req->state = FUSE_REQ_SENT;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800819 list_move_tail(&req->list, &fc->processing);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700820 if (req->interrupted)
821 queue_interrupt(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700822 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700823 }
824 return reqsize;
825
826 err_unlock:
Miklos Szeredid7133112006-04-10 22:54:55 -0700827 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700828 return err;
829}
830
Tejun Heo95668a62008-11-26 12:03:55 +0100831static int fuse_notify_poll(struct fuse_conn *fc, unsigned int size,
832 struct fuse_copy_state *cs)
833{
834 struct fuse_notify_poll_wakeup_out outarg;
Miklos Szeredif6d47a12009-01-26 15:00:59 +0100835 int err = -EINVAL;
Tejun Heo95668a62008-11-26 12:03:55 +0100836
837 if (size != sizeof(outarg))
Miklos Szeredif6d47a12009-01-26 15:00:59 +0100838 goto err;
Tejun Heo95668a62008-11-26 12:03:55 +0100839
840 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
841 if (err)
Miklos Szeredif6d47a12009-01-26 15:00:59 +0100842 goto err;
Tejun Heo95668a62008-11-26 12:03:55 +0100843
Miklos Szeredif6d47a12009-01-26 15:00:59 +0100844 fuse_copy_finish(cs);
Tejun Heo95668a62008-11-26 12:03:55 +0100845 return fuse_notify_poll_wakeup(fc, &outarg);
Miklos Szeredif6d47a12009-01-26 15:00:59 +0100846
847err:
848 fuse_copy_finish(cs);
849 return err;
Tejun Heo95668a62008-11-26 12:03:55 +0100850}
851
Tejun Heo85993962008-11-26 12:03:55 +0100852static int fuse_notify(struct fuse_conn *fc, enum fuse_notify_code code,
853 unsigned int size, struct fuse_copy_state *cs)
854{
855 switch (code) {
Tejun Heo95668a62008-11-26 12:03:55 +0100856 case FUSE_NOTIFY_POLL:
857 return fuse_notify_poll(fc, size, cs);
858
Tejun Heo85993962008-11-26 12:03:55 +0100859 default:
Miklos Szeredif6d47a12009-01-26 15:00:59 +0100860 fuse_copy_finish(cs);
Tejun Heo85993962008-11-26 12:03:55 +0100861 return -EINVAL;
862 }
863}
864
Miklos Szeredi334f4852005-09-09 13:10:27 -0700865/* Look up request on processing list by unique ID */
866static struct fuse_req *request_find(struct fuse_conn *fc, u64 unique)
867{
868 struct list_head *entry;
869
870 list_for_each(entry, &fc->processing) {
871 struct fuse_req *req;
872 req = list_entry(entry, struct fuse_req, list);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700873 if (req->in.h.unique == unique || req->intr_unique == unique)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700874 return req;
875 }
876 return NULL;
877}
878
879static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
880 unsigned nbytes)
881{
882 unsigned reqsize = sizeof(struct fuse_out_header);
883
884 if (out->h.error)
885 return nbytes != reqsize ? -EINVAL : 0;
886
887 reqsize += len_args(out->numargs, out->args);
888
889 if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
890 return -EINVAL;
891 else if (reqsize > nbytes) {
892 struct fuse_arg *lastarg = &out->args[out->numargs-1];
893 unsigned diffsize = reqsize - nbytes;
894 if (diffsize > lastarg->size)
895 return -EINVAL;
896 lastarg->size -= diffsize;
897 }
898 return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
899 out->page_zeroing);
900}
901
902/*
903 * Write a single reply to a request. First the header is copied from
904 * the write buffer. The request is then searched on the processing
905 * list by the unique ID found in the header. If found, then remove
906 * it from the list and copy the rest of the buffer to the request.
907 * The request is finished by calling request_end()
908 */
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700909static ssize_t fuse_dev_write(struct kiocb *iocb, const struct iovec *iov,
910 unsigned long nr_segs, loff_t pos)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700911{
912 int err;
913 unsigned nbytes = iov_length(iov, nr_segs);
914 struct fuse_req *req;
915 struct fuse_out_header oh;
916 struct fuse_copy_state cs;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700917 struct fuse_conn *fc = fuse_get_conn(iocb->ki_filp);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700918 if (!fc)
Miklos Szeredia87046d2006-04-10 22:54:56 -0700919 return -EPERM;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700920
Miklos Szeredid7133112006-04-10 22:54:55 -0700921 fuse_copy_init(&cs, fc, 0, NULL, iov, nr_segs);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700922 if (nbytes < sizeof(struct fuse_out_header))
923 return -EINVAL;
924
925 err = fuse_copy_one(&cs, &oh, sizeof(oh));
926 if (err)
927 goto err_finish;
Tejun Heo85993962008-11-26 12:03:55 +0100928
Miklos Szeredi334f4852005-09-09 13:10:27 -0700929 err = -EINVAL;
Tejun Heo85993962008-11-26 12:03:55 +0100930 if (oh.len != nbytes)
931 goto err_finish;
932
933 /*
934 * Zero oh.unique indicates unsolicited notification message
935 * and error contains notification code.
936 */
937 if (!oh.unique) {
938 err = fuse_notify(fc, oh.error, nbytes - sizeof(oh), &cs);
Tejun Heo85993962008-11-26 12:03:55 +0100939 return err ? err : nbytes;
940 }
941
942 err = -EINVAL;
943 if (oh.error <= -1000 || oh.error > 0)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700944 goto err_finish;
945
Miklos Szeredid7133112006-04-10 22:54:55 -0700946 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800947 err = -ENOENT;
948 if (!fc->connected)
949 goto err_unlock;
950
Miklos Szeredi334f4852005-09-09 13:10:27 -0700951 req = request_find(fc, oh.unique);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700952 if (!req)
953 goto err_unlock;
954
Miklos Szeredif9a28422006-06-25 05:48:53 -0700955 if (req->aborted) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700956 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700957 fuse_copy_finish(&cs);
Miklos Szeredid7133112006-04-10 22:54:55 -0700958 spin_lock(&fc->lock);
Miklos Szeredi222f1d62006-01-16 22:14:25 -0800959 request_end(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700960 return -ENOENT;
961 }
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700962 /* Is it an interrupt reply? */
963 if (req->intr_unique == oh.unique) {
964 err = -EINVAL;
965 if (nbytes != sizeof(struct fuse_out_header))
966 goto err_unlock;
967
968 if (oh.error == -ENOSYS)
969 fc->no_interrupt = 1;
970 else if (oh.error == -EAGAIN)
971 queue_interrupt(fc, req);
972
973 spin_unlock(&fc->lock);
974 fuse_copy_finish(&cs);
975 return nbytes;
976 }
977
978 req->state = FUSE_REQ_WRITING;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800979 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700980 req->out.h = oh;
981 req->locked = 1;
982 cs.req = req;
Miklos Szeredid7133112006-04-10 22:54:55 -0700983 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700984
985 err = copy_out_args(&cs, &req->out, nbytes);
986 fuse_copy_finish(&cs);
987
Miklos Szeredid7133112006-04-10 22:54:55 -0700988 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700989 req->locked = 0;
990 if (!err) {
Miklos Szeredif9a28422006-06-25 05:48:53 -0700991 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700992 err = -ENOENT;
Miklos Szeredif9a28422006-06-25 05:48:53 -0700993 } else if (!req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700994 req->out.h.error = -EIO;
995 request_end(fc, req);
996
997 return err ? err : nbytes;
998
999 err_unlock:
Miklos Szeredid7133112006-04-10 22:54:55 -07001000 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001001 err_finish:
1002 fuse_copy_finish(&cs);
1003 return err;
1004}
1005
Miklos Szeredi334f4852005-09-09 13:10:27 -07001006static unsigned fuse_dev_poll(struct file *file, poll_table *wait)
1007{
Miklos Szeredi334f4852005-09-09 13:10:27 -07001008 unsigned mask = POLLOUT | POLLWRNORM;
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07001009 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001010 if (!fc)
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07001011 return POLLERR;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001012
1013 poll_wait(file, &fc->waitq, wait);
1014
Miklos Szeredid7133112006-04-10 22:54:55 -07001015 spin_lock(&fc->lock);
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07001016 if (!fc->connected)
1017 mask = POLLERR;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001018 else if (request_pending(fc))
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07001019 mask |= POLLIN | POLLRDNORM;
Miklos Szeredid7133112006-04-10 22:54:55 -07001020 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001021
1022 return mask;
1023}
1024
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001025/*
1026 * Abort all requests on the given list (pending or processing)
1027 *
Miklos Szeredid7133112006-04-10 22:54:55 -07001028 * This function releases and reacquires fc->lock
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001029 */
Miklos Szeredi334f4852005-09-09 13:10:27 -07001030static void end_requests(struct fuse_conn *fc, struct list_head *head)
Harvey Harrison5d9ec852008-12-02 14:49:42 +01001031__releases(&fc->lock)
1032__acquires(&fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001033{
1034 while (!list_empty(head)) {
1035 struct fuse_req *req;
1036 req = list_entry(head->next, struct fuse_req, list);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001037 req->out.h.error = -ECONNABORTED;
1038 request_end(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -07001039 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001040 }
1041}
1042
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001043/*
1044 * Abort requests under I/O
1045 *
Miklos Szeredif9a28422006-06-25 05:48:53 -07001046 * The requests are set to aborted and finished, and the request
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001047 * waiter is woken up. This will make request_wait_answer() wait
1048 * until the request is unlocked and then return.
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08001049 *
1050 * If the request is asynchronous, then the end function needs to be
1051 * called after waiting for the request to be unlocked (if it was
1052 * locked).
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001053 */
1054static void end_io_requests(struct fuse_conn *fc)
Harvey Harrison5d9ec852008-12-02 14:49:42 +01001055__releases(&fc->lock)
1056__acquires(&fc->lock)
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001057{
1058 while (!list_empty(&fc->io)) {
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08001059 struct fuse_req *req =
1060 list_entry(fc->io.next, struct fuse_req, list);
1061 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
1062
Miklos Szeredif9a28422006-06-25 05:48:53 -07001063 req->aborted = 1;
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001064 req->out.h.error = -ECONNABORTED;
1065 req->state = FUSE_REQ_FINISHED;
1066 list_del_init(&req->list);
1067 wake_up(&req->waitq);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08001068 if (end) {
1069 req->end = NULL;
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08001070 __fuse_get_request(req);
Miklos Szeredid7133112006-04-10 22:54:55 -07001071 spin_unlock(&fc->lock);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08001072 wait_event(req->waitq, !req->locked);
1073 end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +01001074 fuse_put_request(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -07001075 spin_lock(&fc->lock);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08001076 }
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001077 }
1078}
1079
1080/*
1081 * Abort all requests.
1082 *
1083 * Emergency exit in case of a malicious or accidental deadlock, or
1084 * just a hung filesystem.
1085 *
1086 * The same effect is usually achievable through killing the
1087 * filesystem daemon and all users of the filesystem. The exception
1088 * is the combination of an asynchronous request and the tricky
1089 * deadlock (see Documentation/filesystems/fuse.txt).
1090 *
1091 * During the aborting, progression of requests from the pending and
1092 * processing lists onto the io list, and progression of new requests
1093 * onto the pending list is prevented by req->connected being false.
1094 *
1095 * Progression of requests under I/O to the processing list is
Miklos Szeredif9a28422006-06-25 05:48:53 -07001096 * prevented by the req->aborted flag being true for these requests.
1097 * For this reason requests on the io list must be aborted first.
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001098 */
1099void fuse_abort_conn(struct fuse_conn *fc)
1100{
Miklos Szeredid7133112006-04-10 22:54:55 -07001101 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001102 if (fc->connected) {
1103 fc->connected = 0;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -07001104 fc->blocked = 0;
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001105 end_io_requests(fc);
1106 end_requests(fc, &fc->pending);
1107 end_requests(fc, &fc->processing);
1108 wake_up_all(&fc->waitq);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -07001109 wake_up_all(&fc->blocked_waitq);
Jeff Dike385a17b2006-04-10 22:54:52 -07001110 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001111 }
Miklos Szeredid7133112006-04-10 22:54:55 -07001112 spin_unlock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001113}
Tejun Heo08cbf542009-04-14 10:54:53 +09001114EXPORT_SYMBOL_GPL(fuse_abort_conn);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001115
Tejun Heo08cbf542009-04-14 10:54:53 +09001116int fuse_dev_release(struct inode *inode, struct file *file)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001117{
Miklos Szeredi0720b312006-04-10 22:54:55 -07001118 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001119 if (fc) {
Miklos Szeredid7133112006-04-10 22:54:55 -07001120 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -07001121 fc->connected = 0;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001122 end_requests(fc, &fc->pending);
1123 end_requests(fc, &fc->processing);
Miklos Szeredid7133112006-04-10 22:54:55 -07001124 spin_unlock(&fc->lock);
Miklos Szeredibafa9652006-06-25 05:48:51 -07001125 fuse_conn_put(fc);
Jeff Dike385a17b2006-04-10 22:54:52 -07001126 }
Miklos Szeredif543f252006-01-16 22:14:35 -08001127
Miklos Szeredi334f4852005-09-09 13:10:27 -07001128 return 0;
1129}
Tejun Heo08cbf542009-04-14 10:54:53 +09001130EXPORT_SYMBOL_GPL(fuse_dev_release);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001131
Jeff Dike385a17b2006-04-10 22:54:52 -07001132static int fuse_dev_fasync(int fd, struct file *file, int on)
1133{
1134 struct fuse_conn *fc = fuse_get_conn(file);
1135 if (!fc)
Miklos Szeredia87046d2006-04-10 22:54:56 -07001136 return -EPERM;
Jeff Dike385a17b2006-04-10 22:54:52 -07001137
1138 /* No locking - fasync_helper does its own locking */
1139 return fasync_helper(fd, file, on, &fc->fasync);
1140}
1141
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001142const struct file_operations fuse_dev_operations = {
Miklos Szeredi334f4852005-09-09 13:10:27 -07001143 .owner = THIS_MODULE,
1144 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07001145 .read = do_sync_read,
1146 .aio_read = fuse_dev_read,
1147 .write = do_sync_write,
1148 .aio_write = fuse_dev_write,
Miklos Szeredi334f4852005-09-09 13:10:27 -07001149 .poll = fuse_dev_poll,
1150 .release = fuse_dev_release,
Jeff Dike385a17b2006-04-10 22:54:52 -07001151 .fasync = fuse_dev_fasync,
Miklos Szeredi334f4852005-09-09 13:10:27 -07001152};
Tejun Heo08cbf542009-04-14 10:54:53 +09001153EXPORT_SYMBOL_GPL(fuse_dev_operations);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001154
1155static struct miscdevice fuse_miscdevice = {
1156 .minor = FUSE_MINOR,
1157 .name = "fuse",
1158 .fops = &fuse_dev_operations,
1159};
1160
1161int __init fuse_dev_init(void)
1162{
1163 int err = -ENOMEM;
1164 fuse_req_cachep = kmem_cache_create("fuse_request",
1165 sizeof(struct fuse_req),
Paul Mundt20c2df82007-07-20 10:11:58 +09001166 0, 0, NULL);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001167 if (!fuse_req_cachep)
1168 goto out;
1169
1170 err = misc_register(&fuse_miscdevice);
1171 if (err)
1172 goto out_cache_clean;
1173
1174 return 0;
1175
1176 out_cache_clean:
1177 kmem_cache_destroy(fuse_req_cachep);
1178 out:
1179 return err;
1180}
1181
1182void fuse_dev_cleanup(void)
1183{
1184 misc_deregister(&fuse_miscdevice);
1185 kmem_cache_destroy(fuse_req_cachep);
1186}