blob: b070d3adf9b07b1819390b4735e519fe527c0a10 [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>
Miklos Szeredidd3bb142010-05-25 15:06:06 +020019#include <linux/pipe_fs_i.h>
Miklos Szeredice534fb2010-05-25 15:06:07 +020020#include <linux/swap.h>
21#include <linux/splice.h>
Miklos Szeredi334f4852005-09-09 13:10:27 -070022
23MODULE_ALIAS_MISCDEV(FUSE_MINOR);
24
Christoph Lametere18b8902006-12-06 20:33:20 -080025static struct kmem_cache *fuse_req_cachep;
Miklos Szeredi334f4852005-09-09 13:10:27 -070026
Miklos Szeredi8bfc0162006-01-16 22:14:28 -080027static struct fuse_conn *fuse_get_conn(struct file *file)
Miklos Szeredi334f4852005-09-09 13:10:27 -070028{
Miklos Szeredi0720b312006-04-10 22:54:55 -070029 /*
30 * Lockless access is OK, because file->private data is set
31 * once during mount and is valid until the file is released.
32 */
33 return file->private_data;
Miklos Szeredi334f4852005-09-09 13:10:27 -070034}
35
Miklos Szeredi8bfc0162006-01-16 22:14:28 -080036static void fuse_request_init(struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -070037{
38 memset(req, 0, sizeof(*req));
39 INIT_LIST_HEAD(&req->list);
Miklos Szeredia4d27e72006-06-25 05:48:54 -070040 INIT_LIST_HEAD(&req->intr_entry);
Miklos Szeredi334f4852005-09-09 13:10:27 -070041 init_waitqueue_head(&req->waitq);
42 atomic_set(&req->count, 1);
43}
44
45struct fuse_req *fuse_request_alloc(void)
46{
Christoph Lametere94b1762006-12-06 20:33:17 -080047 struct fuse_req *req = kmem_cache_alloc(fuse_req_cachep, GFP_KERNEL);
Miklos Szeredi334f4852005-09-09 13:10:27 -070048 if (req)
49 fuse_request_init(req);
50 return req;
51}
Tejun Heo08cbf542009-04-14 10:54:53 +090052EXPORT_SYMBOL_GPL(fuse_request_alloc);
Miklos Szeredi334f4852005-09-09 13:10:27 -070053
Miklos Szeredi3be5a522008-04-30 00:54:41 -070054struct fuse_req *fuse_request_alloc_nofs(void)
55{
56 struct fuse_req *req = kmem_cache_alloc(fuse_req_cachep, GFP_NOFS);
57 if (req)
58 fuse_request_init(req);
59 return req;
60}
61
Miklos Szeredi334f4852005-09-09 13:10:27 -070062void fuse_request_free(struct fuse_req *req)
63{
64 kmem_cache_free(fuse_req_cachep, req);
65}
66
Miklos Szeredi8bfc0162006-01-16 22:14:28 -080067static void block_sigs(sigset_t *oldset)
Miklos Szeredi334f4852005-09-09 13:10:27 -070068{
69 sigset_t mask;
70
71 siginitsetinv(&mask, sigmask(SIGKILL));
72 sigprocmask(SIG_BLOCK, &mask, oldset);
73}
74
Miklos Szeredi8bfc0162006-01-16 22:14:28 -080075static void restore_sigs(sigset_t *oldset)
Miklos Szeredi334f4852005-09-09 13:10:27 -070076{
77 sigprocmask(SIG_SETMASK, oldset, NULL);
78}
79
Miklos Szeredi334f4852005-09-09 13:10:27 -070080static void __fuse_get_request(struct fuse_req *req)
81{
82 atomic_inc(&req->count);
83}
84
85/* Must be called with > 1 refcount */
86static void __fuse_put_request(struct fuse_req *req)
87{
88 BUG_ON(atomic_read(&req->count) < 2);
89 atomic_dec(&req->count);
90}
91
Miklos Szeredi33649c92006-06-25 05:48:52 -070092static void fuse_req_init_context(struct fuse_req *req)
93{
David Howells2186a712008-11-14 10:38:53 +110094 req->in.h.uid = current_fsuid();
95 req->in.h.gid = current_fsgid();
Miklos Szeredi33649c92006-06-25 05:48:52 -070096 req->in.h.pid = current->pid;
97}
98
Miklos Szeredice1d5a42006-04-10 22:54:58 -070099struct fuse_req *fuse_get_req(struct fuse_conn *fc)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700100{
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700101 struct fuse_req *req;
102 sigset_t oldset;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200103 int intr;
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700104 int err;
105
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200106 atomic_inc(&fc->num_waiting);
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700107 block_sigs(&oldset);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200108 intr = wait_event_interruptible(fc->blocked_waitq, !fc->blocked);
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700109 restore_sigs(&oldset);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200110 err = -EINTR;
111 if (intr)
112 goto out;
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700113
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700114 err = -ENOTCONN;
115 if (!fc->connected)
116 goto out;
117
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700118 req = fuse_request_alloc();
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200119 err = -ENOMEM;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700120 if (!req)
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200121 goto out;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700122
Miklos Szeredi33649c92006-06-25 05:48:52 -0700123 fuse_req_init_context(req);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200124 req->waiting = 1;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700125 return req;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200126
127 out:
128 atomic_dec(&fc->num_waiting);
129 return ERR_PTR(err);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700130}
Tejun Heo08cbf542009-04-14 10:54:53 +0900131EXPORT_SYMBOL_GPL(fuse_get_req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700132
Miklos Szeredi33649c92006-06-25 05:48:52 -0700133/*
134 * Return request in fuse_file->reserved_req. However that may
135 * currently be in use. If that is the case, wait for it to become
136 * available.
137 */
138static struct fuse_req *get_reserved_req(struct fuse_conn *fc,
139 struct file *file)
140{
141 struct fuse_req *req = NULL;
142 struct fuse_file *ff = file->private_data;
143
144 do {
Miklos Szeredide5e3de2007-10-16 23:31:00 -0700145 wait_event(fc->reserved_req_waitq, ff->reserved_req);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700146 spin_lock(&fc->lock);
147 if (ff->reserved_req) {
148 req = ff->reserved_req;
149 ff->reserved_req = NULL;
150 get_file(file);
151 req->stolen_file = file;
152 }
153 spin_unlock(&fc->lock);
154 } while (!req);
155
156 return req;
157}
158
159/*
160 * Put stolen request back into fuse_file->reserved_req
161 */
162static void put_reserved_req(struct fuse_conn *fc, struct fuse_req *req)
163{
164 struct file *file = req->stolen_file;
165 struct fuse_file *ff = file->private_data;
166
167 spin_lock(&fc->lock);
168 fuse_request_init(req);
169 BUG_ON(ff->reserved_req);
170 ff->reserved_req = req;
Miklos Szeredide5e3de2007-10-16 23:31:00 -0700171 wake_up_all(&fc->reserved_req_waitq);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700172 spin_unlock(&fc->lock);
173 fput(file);
174}
175
176/*
177 * Gets a requests for a file operation, always succeeds
178 *
179 * This is used for sending the FLUSH request, which must get to
180 * userspace, due to POSIX locks which may need to be unlocked.
181 *
182 * If allocation fails due to OOM, use the reserved request in
183 * fuse_file.
184 *
185 * This is very unlikely to deadlock accidentally, since the
186 * filesystem should not have it's own file open. If deadlock is
187 * intentional, it can still be broken by "aborting" the filesystem.
188 */
189struct fuse_req *fuse_get_req_nofail(struct fuse_conn *fc, struct file *file)
190{
191 struct fuse_req *req;
192
193 atomic_inc(&fc->num_waiting);
194 wait_event(fc->blocked_waitq, !fc->blocked);
195 req = fuse_request_alloc();
196 if (!req)
197 req = get_reserved_req(fc, file);
198
199 fuse_req_init_context(req);
200 req->waiting = 1;
201 return req;
202}
203
Miklos Szeredi334f4852005-09-09 13:10:27 -0700204void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req)
205{
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800206 if (atomic_dec_and_test(&req->count)) {
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200207 if (req->waiting)
208 atomic_dec(&fc->num_waiting);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700209
210 if (req->stolen_file)
211 put_reserved_req(fc, req);
212 else
213 fuse_request_free(req);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800214 }
215}
Tejun Heo08cbf542009-04-14 10:54:53 +0900216EXPORT_SYMBOL_GPL(fuse_put_request);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800217
Miklos Szeredid12def12008-02-06 01:38:39 -0800218static unsigned len_args(unsigned numargs, struct fuse_arg *args)
219{
220 unsigned nbytes = 0;
221 unsigned i;
222
223 for (i = 0; i < numargs; i++)
224 nbytes += args[i].size;
225
226 return nbytes;
227}
228
229static u64 fuse_get_unique(struct fuse_conn *fc)
230{
231 fc->reqctr++;
232 /* zero is special */
233 if (fc->reqctr == 0)
234 fc->reqctr = 1;
235
236 return fc->reqctr;
237}
238
239static void queue_request(struct fuse_conn *fc, struct fuse_req *req)
240{
241 req->in.h.unique = fuse_get_unique(fc);
242 req->in.h.len = sizeof(struct fuse_in_header) +
243 len_args(req->in.numargs, (struct fuse_arg *) req->in.args);
244 list_add_tail(&req->list, &fc->pending);
245 req->state = FUSE_REQ_PENDING;
246 if (!req->waiting) {
247 req->waiting = 1;
248 atomic_inc(&fc->num_waiting);
249 }
250 wake_up(&fc->waitq);
251 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
252}
253
254static void flush_bg_queue(struct fuse_conn *fc)
255{
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700256 while (fc->active_background < fc->max_background &&
Miklos Szeredid12def12008-02-06 01:38:39 -0800257 !list_empty(&fc->bg_queue)) {
258 struct fuse_req *req;
259
260 req = list_entry(fc->bg_queue.next, struct fuse_req, list);
261 list_del(&req->list);
262 fc->active_background++;
263 queue_request(fc, req);
264 }
265}
266
Miklos Szeredi6dbbcb12006-04-26 10:49:06 +0200267/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700268 * This function is called when a request is finished. Either a reply
Miklos Szeredif9a28422006-06-25 05:48:53 -0700269 * has arrived or it was aborted (and not yet sent) or some error
Miklos Szeredif43b1552006-01-16 22:14:26 -0800270 * occurred during communication with userspace, or the device file
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700271 * was closed. The requester thread is woken up (if still waiting),
272 * the 'end' callback is called if given, else the reference to the
273 * request is released
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800274 *
Miklos Szeredid7133112006-04-10 22:54:55 -0700275 * Called with fc->lock, unlocks it
Miklos Szeredi334f4852005-09-09 13:10:27 -0700276 */
277static void request_end(struct fuse_conn *fc, struct fuse_req *req)
Harvey Harrison5d9ec852008-12-02 14:49:42 +0100278__releases(&fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700279{
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700280 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
281 req->end = NULL;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800282 list_del(&req->list);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700283 list_del(&req->intr_entry);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800284 req->state = FUSE_REQ_FINISHED;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700285 if (req->background) {
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700286 if (fc->num_background == fc->max_background) {
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700287 fc->blocked = 0;
288 wake_up_all(&fc->blocked_waitq);
289 }
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700290 if (fc->num_background == fc->congestion_threshold &&
Tejun Heoa325f9b2009-04-14 10:54:52 +0900291 fc->connected && fc->bdi_initialized) {
Jens Axboe8aa7e842009-07-09 14:52:32 +0200292 clear_bdi_congested(&fc->bdi, BLK_RW_SYNC);
293 clear_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
Miklos Szeredif92b99b2007-10-16 23:30:59 -0700294 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700295 fc->num_background--;
Miklos Szeredid12def12008-02-06 01:38:39 -0800296 fc->active_background--;
297 flush_bg_queue(fc);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700298 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700299 spin_unlock(&fc->lock);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700300 wake_up(&req->waitq);
301 if (end)
302 end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100303 fuse_put_request(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700304}
305
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700306static void wait_answer_interruptible(struct fuse_conn *fc,
307 struct fuse_req *req)
Harvey Harrison5d9ec852008-12-02 14:49:42 +0100308__releases(&fc->lock)
309__acquires(&fc->lock)
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700310{
311 if (signal_pending(current))
312 return;
313
314 spin_unlock(&fc->lock);
315 wait_event_interruptible(req->waitq, req->state == FUSE_REQ_FINISHED);
316 spin_lock(&fc->lock);
317}
318
319static void queue_interrupt(struct fuse_conn *fc, struct fuse_req *req)
320{
321 list_add_tail(&req->intr_entry, &fc->interrupts);
322 wake_up(&fc->waitq);
323 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
324}
325
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700326static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
Harvey Harrison5d9ec852008-12-02 14:49:42 +0100327__releases(&fc->lock)
328__acquires(&fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700329{
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700330 if (!fc->no_interrupt) {
331 /* Any signal may interrupt this */
332 wait_answer_interruptible(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700333
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700334 if (req->aborted)
335 goto aborted;
336 if (req->state == FUSE_REQ_FINISHED)
337 return;
338
339 req->interrupted = 1;
340 if (req->state == FUSE_REQ_SENT)
341 queue_interrupt(fc, req);
342 }
343
Miklos Szeredia131de02007-10-16 23:31:04 -0700344 if (!req->force) {
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700345 sigset_t oldset;
346
347 /* Only fatal signals may interrupt this */
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700348 block_sigs(&oldset);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700349 wait_answer_interruptible(fc, req);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700350 restore_sigs(&oldset);
Miklos Szeredia131de02007-10-16 23:31:04 -0700351
352 if (req->aborted)
353 goto aborted;
354 if (req->state == FUSE_REQ_FINISHED)
355 return;
356
357 /* Request is not yet in userspace, bail out */
358 if (req->state == FUSE_REQ_PENDING) {
359 list_del(&req->list);
360 __fuse_put_request(req);
361 req->out.h.error = -EINTR;
362 return;
363 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700364 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700365
Miklos Szeredia131de02007-10-16 23:31:04 -0700366 /*
367 * Either request is already in userspace, or it was forced.
368 * Wait it out.
369 */
370 spin_unlock(&fc->lock);
371 wait_event(req->waitq, req->state == FUSE_REQ_FINISHED);
372 spin_lock(&fc->lock);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700373
Miklos Szeredia131de02007-10-16 23:31:04 -0700374 if (!req->aborted)
375 return;
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700376
377 aborted:
Miklos Szeredia131de02007-10-16 23:31:04 -0700378 BUG_ON(req->state != FUSE_REQ_FINISHED);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700379 if (req->locked) {
380 /* This is uninterruptible sleep, because data is
381 being copied to/from the buffers of req. During
382 locked state, there mustn't be any filesystem
383 operation (e.g. page fault), since that could lead
384 to deadlock */
Miklos Szeredid7133112006-04-10 22:54:55 -0700385 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700386 wait_event(req->waitq, !req->locked);
Miklos Szeredid7133112006-04-10 22:54:55 -0700387 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700388 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700389}
390
Tejun Heob93f8582008-11-26 12:03:55 +0100391void fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700392{
393 req->isreply = 1;
Miklos Szeredid7133112006-04-10 22:54:55 -0700394 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700395 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700396 req->out.h.error = -ENOTCONN;
397 else if (fc->conn_error)
398 req->out.h.error = -ECONNREFUSED;
399 else {
400 queue_request(fc, req);
401 /* acquire extra reference, since request is still needed
402 after request_end() */
403 __fuse_get_request(req);
404
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700405 request_wait_answer(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700406 }
Miklos Szeredid7133112006-04-10 22:54:55 -0700407 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700408}
Tejun Heo08cbf542009-04-14 10:54:53 +0900409EXPORT_SYMBOL_GPL(fuse_request_send);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700410
Tejun Heob93f8582008-11-26 12:03:55 +0100411static void fuse_request_send_nowait_locked(struct fuse_conn *fc,
412 struct fuse_req *req)
Miklos Szeredid12def12008-02-06 01:38:39 -0800413{
414 req->background = 1;
415 fc->num_background++;
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700416 if (fc->num_background == fc->max_background)
Miklos Szeredid12def12008-02-06 01:38:39 -0800417 fc->blocked = 1;
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700418 if (fc->num_background == fc->congestion_threshold &&
Tejun Heoa325f9b2009-04-14 10:54:52 +0900419 fc->bdi_initialized) {
Jens Axboe8aa7e842009-07-09 14:52:32 +0200420 set_bdi_congested(&fc->bdi, BLK_RW_SYNC);
421 set_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
Miklos Szeredid12def12008-02-06 01:38:39 -0800422 }
423 list_add_tail(&req->list, &fc->bg_queue);
424 flush_bg_queue(fc);
425}
426
Tejun Heob93f8582008-11-26 12:03:55 +0100427static void fuse_request_send_nowait(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700428{
Miklos Szeredid7133112006-04-10 22:54:55 -0700429 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700430 if (fc->connected) {
Tejun Heob93f8582008-11-26 12:03:55 +0100431 fuse_request_send_nowait_locked(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700432 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700433 } else {
434 req->out.h.error = -ENOTCONN;
435 request_end(fc, req);
436 }
437}
438
Tejun Heob93f8582008-11-26 12:03:55 +0100439void fuse_request_send_noreply(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700440{
441 req->isreply = 0;
Tejun Heob93f8582008-11-26 12:03:55 +0100442 fuse_request_send_nowait(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700443}
444
Tejun Heob93f8582008-11-26 12:03:55 +0100445void fuse_request_send_background(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700446{
447 req->isreply = 1;
Tejun Heob93f8582008-11-26 12:03:55 +0100448 fuse_request_send_nowait(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700449}
Tejun Heo08cbf542009-04-14 10:54:53 +0900450EXPORT_SYMBOL_GPL(fuse_request_send_background);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700451
Miklos Szeredi334f4852005-09-09 13:10:27 -0700452/*
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700453 * Called under fc->lock
454 *
455 * fc->connected must have been checked previously
456 */
Tejun Heob93f8582008-11-26 12:03:55 +0100457void fuse_request_send_background_locked(struct fuse_conn *fc,
458 struct fuse_req *req)
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700459{
460 req->isreply = 1;
Tejun Heob93f8582008-11-26 12:03:55 +0100461 fuse_request_send_nowait_locked(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700462}
463
464/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700465 * Lock the request. Up to the next unlock_request() there mustn't be
466 * anything that could cause a page-fault. If the request was already
Miklos Szeredif9a28422006-06-25 05:48:53 -0700467 * aborted bail out.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700468 */
Miklos Szeredid7133112006-04-10 22:54:55 -0700469static int lock_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700470{
471 int err = 0;
472 if (req) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700473 spin_lock(&fc->lock);
Miklos Szeredif9a28422006-06-25 05:48:53 -0700474 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700475 err = -ENOENT;
476 else
477 req->locked = 1;
Miklos Szeredid7133112006-04-10 22:54:55 -0700478 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700479 }
480 return err;
481}
482
483/*
Miklos Szeredif9a28422006-06-25 05:48:53 -0700484 * Unlock request. If it was aborted during being locked, the
Miklos Szeredi334f4852005-09-09 13:10:27 -0700485 * requester thread is currently waiting for it to be unlocked, so
486 * wake it up.
487 */
Miklos Szeredid7133112006-04-10 22:54:55 -0700488static void unlock_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700489{
490 if (req) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700491 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700492 req->locked = 0;
Miklos Szeredif9a28422006-06-25 05:48:53 -0700493 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700494 wake_up(&req->waitq);
Miklos Szeredid7133112006-04-10 22:54:55 -0700495 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700496 }
497}
498
499struct fuse_copy_state {
Miklos Szeredid7133112006-04-10 22:54:55 -0700500 struct fuse_conn *fc;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700501 int write;
502 struct fuse_req *req;
503 const struct iovec *iov;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200504 struct pipe_buffer *pipebufs;
505 struct pipe_buffer *currbuf;
506 struct pipe_inode_info *pipe;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700507 unsigned long nr_segs;
508 unsigned long seglen;
509 unsigned long addr;
510 struct page *pg;
511 void *mapaddr;
512 void *buf;
513 unsigned len;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200514 unsigned move_pages:1;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700515};
516
Miklos Szeredid7133112006-04-10 22:54:55 -0700517static void fuse_copy_init(struct fuse_copy_state *cs, struct fuse_conn *fc,
518 int write, struct fuse_req *req,
519 const struct iovec *iov, unsigned long nr_segs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700520{
521 memset(cs, 0, sizeof(*cs));
Miklos Szeredid7133112006-04-10 22:54:55 -0700522 cs->fc = fc;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700523 cs->write = write;
524 cs->req = req;
525 cs->iov = iov;
526 cs->nr_segs = nr_segs;
527}
528
529/* Unmap and put previous page of userspace buffer */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800530static void fuse_copy_finish(struct fuse_copy_state *cs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700531{
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200532 if (cs->currbuf) {
533 struct pipe_buffer *buf = cs->currbuf;
534
535 buf->ops->unmap(cs->pipe, buf, cs->mapaddr);
536
537 cs->currbuf = NULL;
538 cs->mapaddr = NULL;
539 } else if (cs->mapaddr) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700540 kunmap_atomic(cs->mapaddr, KM_USER0);
541 if (cs->write) {
542 flush_dcache_page(cs->pg);
543 set_page_dirty_lock(cs->pg);
544 }
545 put_page(cs->pg);
546 cs->mapaddr = NULL;
547 }
548}
549
550/*
551 * Get another pagefull of userspace buffer, and map it to kernel
552 * address space, and lock request
553 */
554static int fuse_copy_fill(struct fuse_copy_state *cs)
555{
556 unsigned long offset;
557 int err;
558
Miklos Szeredid7133112006-04-10 22:54:55 -0700559 unlock_request(cs->fc, cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700560 fuse_copy_finish(cs);
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200561 if (cs->pipebufs) {
562 struct pipe_buffer *buf = cs->pipebufs;
563
564 err = buf->ops->confirm(cs->pipe, buf);
565 if (err)
566 return err;
567
Miklos Szeredi334f4852005-09-09 13:10:27 -0700568 BUG_ON(!cs->nr_segs);
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200569 cs->currbuf = buf;
570 cs->mapaddr = buf->ops->map(cs->pipe, buf, 1);
571 cs->len = buf->len;
572 cs->buf = cs->mapaddr + buf->offset;
573 cs->pipebufs++;
Miklos Szeredi1729a162008-11-26 12:03:54 +0100574 cs->nr_segs--;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200575 } else {
576 if (!cs->seglen) {
577 BUG_ON(!cs->nr_segs);
578 cs->seglen = cs->iov[0].iov_len;
579 cs->addr = (unsigned long) cs->iov[0].iov_base;
580 cs->iov++;
581 cs->nr_segs--;
582 }
583 err = get_user_pages_fast(cs->addr, 1, cs->write, &cs->pg);
584 if (err < 0)
585 return err;
586 BUG_ON(err != 1);
587 offset = cs->addr % PAGE_SIZE;
588 cs->mapaddr = kmap_atomic(cs->pg, KM_USER0);
589 cs->buf = cs->mapaddr + offset;
590 cs->len = min(PAGE_SIZE - offset, cs->seglen);
591 cs->seglen -= cs->len;
592 cs->addr += cs->len;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700593 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700594
Miklos Szeredid7133112006-04-10 22:54:55 -0700595 return lock_request(cs->fc, cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700596}
597
598/* Do as much copy to/from userspace buffer as we can */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800599static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700600{
601 unsigned ncpy = min(*size, cs->len);
602 if (val) {
603 if (cs->write)
604 memcpy(cs->buf, *val, ncpy);
605 else
606 memcpy(*val, cs->buf, ncpy);
607 *val += ncpy;
608 }
609 *size -= ncpy;
610 cs->len -= ncpy;
611 cs->buf += ncpy;
612 return ncpy;
613}
614
Miklos Szeredice534fb2010-05-25 15:06:07 +0200615static int fuse_check_page(struct page *page)
616{
617 if (page_mapcount(page) ||
618 page->mapping != NULL ||
619 page_count(page) != 1 ||
620 (page->flags & PAGE_FLAGS_CHECK_AT_PREP &
621 ~(1 << PG_locked |
622 1 << PG_referenced |
623 1 << PG_uptodate |
624 1 << PG_lru |
625 1 << PG_active |
626 1 << PG_reclaim))) {
627 printk(KERN_WARNING "fuse: trying to steal weird page\n");
628 printk(KERN_WARNING " page=%p index=%li flags=%08lx, count=%i, mapcount=%i, mapping=%p\n", page, page->index, page->flags, page_count(page), page_mapcount(page), page->mapping);
629 return 1;
630 }
631 return 0;
632}
633
634static int fuse_try_move_page(struct fuse_copy_state *cs, struct page **pagep)
635{
636 int err;
637 struct page *oldpage = *pagep;
638 struct page *newpage;
639 struct pipe_buffer *buf = cs->pipebufs;
640 struct address_space *mapping;
641 pgoff_t index;
642
643 unlock_request(cs->fc, cs->req);
644 fuse_copy_finish(cs);
645
646 err = buf->ops->confirm(cs->pipe, buf);
647 if (err)
648 return err;
649
650 BUG_ON(!cs->nr_segs);
651 cs->currbuf = buf;
652 cs->len = buf->len;
653 cs->pipebufs++;
654 cs->nr_segs--;
655
656 if (cs->len != PAGE_SIZE)
657 goto out_fallback;
658
659 if (buf->ops->steal(cs->pipe, buf) != 0)
660 goto out_fallback;
661
662 newpage = buf->page;
663
664 if (WARN_ON(!PageUptodate(newpage)))
665 return -EIO;
666
667 ClearPageMappedToDisk(newpage);
668
669 if (fuse_check_page(newpage) != 0)
670 goto out_fallback_unlock;
671
672 mapping = oldpage->mapping;
673 index = oldpage->index;
674
675 /*
676 * This is a new and locked page, it shouldn't be mapped or
677 * have any special flags on it
678 */
679 if (WARN_ON(page_mapped(oldpage)))
680 goto out_fallback_unlock;
681 if (WARN_ON(page_has_private(oldpage)))
682 goto out_fallback_unlock;
683 if (WARN_ON(PageDirty(oldpage) || PageWriteback(oldpage)))
684 goto out_fallback_unlock;
685 if (WARN_ON(PageMlocked(oldpage)))
686 goto out_fallback_unlock;
687
688 remove_from_page_cache(oldpage);
689 page_cache_release(oldpage);
690
691 err = add_to_page_cache_locked(newpage, mapping, index, GFP_KERNEL);
692 if (err) {
693 printk(KERN_WARNING "fuse_try_move_page: failed to add page");
694 goto out_fallback_unlock;
695 }
696 page_cache_get(newpage);
697
698 if (!(buf->flags & PIPE_BUF_FLAG_LRU))
699 lru_cache_add_file(newpage);
700
701 err = 0;
702 spin_lock(&cs->fc->lock);
703 if (cs->req->aborted)
704 err = -ENOENT;
705 else
706 *pagep = newpage;
707 spin_unlock(&cs->fc->lock);
708
709 if (err) {
710 unlock_page(newpage);
711 page_cache_release(newpage);
712 return err;
713 }
714
715 unlock_page(oldpage);
716 page_cache_release(oldpage);
717 cs->len = 0;
718
719 return 0;
720
721out_fallback_unlock:
722 unlock_page(newpage);
723out_fallback:
724 cs->mapaddr = buf->ops->map(cs->pipe, buf, 1);
725 cs->buf = cs->mapaddr + buf->offset;
726
727 err = lock_request(cs->fc, cs->req);
728 if (err)
729 return err;
730
731 return 1;
732}
733
Miklos Szeredi334f4852005-09-09 13:10:27 -0700734/*
735 * Copy a page in the request to/from the userspace buffer. Must be
736 * done atomically
737 */
Miklos Szeredice534fb2010-05-25 15:06:07 +0200738static int fuse_copy_page(struct fuse_copy_state *cs, struct page **pagep,
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800739 unsigned offset, unsigned count, int zeroing)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700740{
Miklos Szeredice534fb2010-05-25 15:06:07 +0200741 int err;
742 struct page *page = *pagep;
743
Miklos Szeredi334f4852005-09-09 13:10:27 -0700744 if (page && zeroing && count < PAGE_SIZE) {
745 void *mapaddr = kmap_atomic(page, KM_USER1);
746 memset(mapaddr, 0, PAGE_SIZE);
747 kunmap_atomic(mapaddr, KM_USER1);
748 }
749 while (count) {
Miklos Szeredi1729a162008-11-26 12:03:54 +0100750 if (!cs->len) {
Miklos Szeredice534fb2010-05-25 15:06:07 +0200751 if (cs->move_pages && page &&
752 offset == 0 && count == PAGE_SIZE) {
753 err = fuse_try_move_page(cs, pagep);
754 if (err <= 0)
755 return err;
756 } else {
757 err = fuse_copy_fill(cs);
758 if (err)
759 return err;
760 }
Miklos Szeredi1729a162008-11-26 12:03:54 +0100761 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700762 if (page) {
763 void *mapaddr = kmap_atomic(page, KM_USER1);
764 void *buf = mapaddr + offset;
765 offset += fuse_copy_do(cs, &buf, &count);
766 kunmap_atomic(mapaddr, KM_USER1);
767 } else
768 offset += fuse_copy_do(cs, NULL, &count);
769 }
770 if (page && !cs->write)
771 flush_dcache_page(page);
772 return 0;
773}
774
775/* Copy pages in the request to/from userspace buffer */
776static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
777 int zeroing)
778{
779 unsigned i;
780 struct fuse_req *req = cs->req;
781 unsigned offset = req->page_offset;
782 unsigned count = min(nbytes, (unsigned) PAGE_SIZE - offset);
783
784 for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
Miklos Szeredice534fb2010-05-25 15:06:07 +0200785 int err;
786
787 err = fuse_copy_page(cs, &req->pages[i], offset, count,
788 zeroing);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700789 if (err)
790 return err;
791
792 nbytes -= count;
793 count = min(nbytes, (unsigned) PAGE_SIZE);
794 offset = 0;
795 }
796 return 0;
797}
798
799/* Copy a single argument in the request to/from userspace buffer */
800static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
801{
802 while (size) {
Miklos Szeredi1729a162008-11-26 12:03:54 +0100803 if (!cs->len) {
804 int err = fuse_copy_fill(cs);
805 if (err)
806 return err;
807 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700808 fuse_copy_do(cs, &val, &size);
809 }
810 return 0;
811}
812
813/* Copy request arguments to/from userspace buffer */
814static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
815 unsigned argpages, struct fuse_arg *args,
816 int zeroing)
817{
818 int err = 0;
819 unsigned i;
820
821 for (i = 0; !err && i < numargs; i++) {
822 struct fuse_arg *arg = &args[i];
823 if (i == numargs - 1 && argpages)
824 err = fuse_copy_pages(cs, arg->size, zeroing);
825 else
826 err = fuse_copy_one(cs, arg->value, arg->size);
827 }
828 return err;
829}
830
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700831static int request_pending(struct fuse_conn *fc)
832{
833 return !list_empty(&fc->pending) || !list_empty(&fc->interrupts);
834}
835
Miklos Szeredi334f4852005-09-09 13:10:27 -0700836/* Wait until a request is available on the pending list */
837static void request_wait(struct fuse_conn *fc)
Harvey Harrison5d9ec852008-12-02 14:49:42 +0100838__releases(&fc->lock)
839__acquires(&fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700840{
841 DECLARE_WAITQUEUE(wait, current);
842
843 add_wait_queue_exclusive(&fc->waitq, &wait);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700844 while (fc->connected && !request_pending(fc)) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700845 set_current_state(TASK_INTERRUPTIBLE);
846 if (signal_pending(current))
847 break;
848
Miklos Szeredid7133112006-04-10 22:54:55 -0700849 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700850 schedule();
Miklos Szeredid7133112006-04-10 22:54:55 -0700851 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700852 }
853 set_current_state(TASK_RUNNING);
854 remove_wait_queue(&fc->waitq, &wait);
855}
856
857/*
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700858 * Transfer an interrupt request to userspace
859 *
860 * Unlike other requests this is assembled on demand, without a need
861 * to allocate a separate fuse_req structure.
862 *
863 * Called with fc->lock held, releases it
864 */
865static int fuse_read_interrupt(struct fuse_conn *fc, struct fuse_req *req,
866 const struct iovec *iov, unsigned long nr_segs)
Harvey Harrison5d9ec852008-12-02 14:49:42 +0100867__releases(&fc->lock)
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700868{
869 struct fuse_copy_state cs;
870 struct fuse_in_header ih;
871 struct fuse_interrupt_in arg;
872 unsigned reqsize = sizeof(ih) + sizeof(arg);
873 int err;
874
875 list_del_init(&req->intr_entry);
876 req->intr_unique = fuse_get_unique(fc);
877 memset(&ih, 0, sizeof(ih));
878 memset(&arg, 0, sizeof(arg));
879 ih.len = reqsize;
880 ih.opcode = FUSE_INTERRUPT;
881 ih.unique = req->intr_unique;
882 arg.unique = req->in.h.unique;
883
884 spin_unlock(&fc->lock);
885 if (iov_length(iov, nr_segs) < reqsize)
886 return -EINVAL;
887
888 fuse_copy_init(&cs, fc, 1, NULL, iov, nr_segs);
889 err = fuse_copy_one(&cs, &ih, sizeof(ih));
890 if (!err)
891 err = fuse_copy_one(&cs, &arg, sizeof(arg));
892 fuse_copy_finish(&cs);
893
894 return err ? err : reqsize;
895}
896
897/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700898 * Read a single request into the userspace filesystem's buffer. This
899 * function waits until a request is available, then removes it from
900 * the pending list and copies request data to userspace buffer. If
Miklos Szeredif9a28422006-06-25 05:48:53 -0700901 * no reply is needed (FORGET) or request has been aborted or there
902 * was an error during the copying then it's finished by calling
Miklos Szeredi334f4852005-09-09 13:10:27 -0700903 * request_end(). Otherwise add it to the processing list, and set
904 * the 'sent' flag.
905 */
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700906static ssize_t fuse_dev_read(struct kiocb *iocb, const struct iovec *iov,
907 unsigned long nr_segs, loff_t pos)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700908{
909 int err;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700910 struct fuse_req *req;
911 struct fuse_in *in;
912 struct fuse_copy_state cs;
913 unsigned reqsize;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700914 struct file *file = iocb->ki_filp;
Miklos Szeredi0720b312006-04-10 22:54:55 -0700915 struct fuse_conn *fc = fuse_get_conn(file);
916 if (!fc)
917 return -EPERM;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700918
Miklos Szeredi1d3d7522006-01-06 00:19:40 -0800919 restart:
Miklos Szeredid7133112006-04-10 22:54:55 -0700920 spin_lock(&fc->lock);
Jeff Dikee5ac1d12006-04-10 22:54:53 -0700921 err = -EAGAIN;
922 if ((file->f_flags & O_NONBLOCK) && fc->connected &&
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700923 !request_pending(fc))
Jeff Dikee5ac1d12006-04-10 22:54:53 -0700924 goto err_unlock;
925
Miklos Szeredi334f4852005-09-09 13:10:27 -0700926 request_wait(fc);
927 err = -ENODEV;
Miklos Szeredi9ba7cbb2006-01-16 22:14:34 -0800928 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700929 goto err_unlock;
930 err = -ERESTARTSYS;
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700931 if (!request_pending(fc))
Miklos Szeredi334f4852005-09-09 13:10:27 -0700932 goto err_unlock;
933
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700934 if (!list_empty(&fc->interrupts)) {
935 req = list_entry(fc->interrupts.next, struct fuse_req,
936 intr_entry);
937 return fuse_read_interrupt(fc, req, iov, nr_segs);
938 }
939
Miklos Szeredi334f4852005-09-09 13:10:27 -0700940 req = list_entry(fc->pending.next, struct fuse_req, list);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800941 req->state = FUSE_REQ_READING;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800942 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700943
944 in = &req->in;
Miklos Szeredi1d3d7522006-01-06 00:19:40 -0800945 reqsize = in->h.len;
946 /* If request is too large, reply with an error and restart the read */
947 if (iov_length(iov, nr_segs) < reqsize) {
948 req->out.h.error = -EIO;
949 /* SETXATTR is special, since it may contain too large data */
950 if (in->h.opcode == FUSE_SETXATTR)
951 req->out.h.error = -E2BIG;
952 request_end(fc, req);
953 goto restart;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700954 }
Miklos Szeredid7133112006-04-10 22:54:55 -0700955 spin_unlock(&fc->lock);
956 fuse_copy_init(&cs, fc, 1, req, iov, nr_segs);
Miklos Szeredi1d3d7522006-01-06 00:19:40 -0800957 err = fuse_copy_one(&cs, &in->h, sizeof(in->h));
958 if (!err)
959 err = fuse_copy_args(&cs, in->numargs, in->argpages,
960 (struct fuse_arg *) in->args, 0);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700961 fuse_copy_finish(&cs);
Miklos Szeredid7133112006-04-10 22:54:55 -0700962 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700963 req->locked = 0;
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -0700964 if (req->aborted) {
965 request_end(fc, req);
966 return -ENODEV;
967 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700968 if (err) {
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -0700969 req->out.h.error = -EIO;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700970 request_end(fc, req);
971 return err;
972 }
973 if (!req->isreply)
974 request_end(fc, req);
975 else {
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800976 req->state = FUSE_REQ_SENT;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800977 list_move_tail(&req->list, &fc->processing);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700978 if (req->interrupted)
979 queue_interrupt(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700980 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700981 }
982 return reqsize;
983
984 err_unlock:
Miklos Szeredid7133112006-04-10 22:54:55 -0700985 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700986 return err;
987}
988
Tejun Heo95668a62008-11-26 12:03:55 +0100989static int fuse_notify_poll(struct fuse_conn *fc, unsigned int size,
990 struct fuse_copy_state *cs)
991{
992 struct fuse_notify_poll_wakeup_out outarg;
Miklos Szeredif6d47a12009-01-26 15:00:59 +0100993 int err = -EINVAL;
Tejun Heo95668a62008-11-26 12:03:55 +0100994
995 if (size != sizeof(outarg))
Miklos Szeredif6d47a12009-01-26 15:00:59 +0100996 goto err;
Tejun Heo95668a62008-11-26 12:03:55 +0100997
998 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
999 if (err)
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001000 goto err;
Tejun Heo95668a62008-11-26 12:03:55 +01001001
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001002 fuse_copy_finish(cs);
Tejun Heo95668a62008-11-26 12:03:55 +01001003 return fuse_notify_poll_wakeup(fc, &outarg);
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001004
1005err:
1006 fuse_copy_finish(cs);
1007 return err;
Tejun Heo95668a62008-11-26 12:03:55 +01001008}
1009
John Muir3b463ae2009-05-31 11:13:57 -04001010static int fuse_notify_inval_inode(struct fuse_conn *fc, unsigned int size,
1011 struct fuse_copy_state *cs)
1012{
1013 struct fuse_notify_inval_inode_out outarg;
1014 int err = -EINVAL;
1015
1016 if (size != sizeof(outarg))
1017 goto err;
1018
1019 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1020 if (err)
1021 goto err;
1022 fuse_copy_finish(cs);
1023
1024 down_read(&fc->killsb);
1025 err = -ENOENT;
Miklos Szeredib21dda42010-02-05 12:08:31 +01001026 if (fc->sb) {
1027 err = fuse_reverse_inval_inode(fc->sb, outarg.ino,
1028 outarg.off, outarg.len);
1029 }
John Muir3b463ae2009-05-31 11:13:57 -04001030 up_read(&fc->killsb);
1031 return err;
1032
1033err:
1034 fuse_copy_finish(cs);
1035 return err;
1036}
1037
1038static int fuse_notify_inval_entry(struct fuse_conn *fc, unsigned int size,
1039 struct fuse_copy_state *cs)
1040{
1041 struct fuse_notify_inval_entry_out outarg;
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001042 int err = -ENOMEM;
1043 char *buf;
John Muir3b463ae2009-05-31 11:13:57 -04001044 struct qstr name;
1045
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001046 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1047 if (!buf)
1048 goto err;
1049
1050 err = -EINVAL;
John Muir3b463ae2009-05-31 11:13:57 -04001051 if (size < sizeof(outarg))
1052 goto err;
1053
1054 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1055 if (err)
1056 goto err;
1057
1058 err = -ENAMETOOLONG;
1059 if (outarg.namelen > FUSE_NAME_MAX)
1060 goto err;
1061
1062 name.name = buf;
1063 name.len = outarg.namelen;
1064 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1065 if (err)
1066 goto err;
1067 fuse_copy_finish(cs);
1068 buf[outarg.namelen] = 0;
1069 name.hash = full_name_hash(name.name, name.len);
1070
1071 down_read(&fc->killsb);
1072 err = -ENOENT;
Miklos Szeredib21dda42010-02-05 12:08:31 +01001073 if (fc->sb)
1074 err = fuse_reverse_inval_entry(fc->sb, outarg.parent, &name);
John Muir3b463ae2009-05-31 11:13:57 -04001075 up_read(&fc->killsb);
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001076 kfree(buf);
John Muir3b463ae2009-05-31 11:13:57 -04001077 return err;
1078
1079err:
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001080 kfree(buf);
John Muir3b463ae2009-05-31 11:13:57 -04001081 fuse_copy_finish(cs);
1082 return err;
1083}
1084
Tejun Heo85993962008-11-26 12:03:55 +01001085static int fuse_notify(struct fuse_conn *fc, enum fuse_notify_code code,
1086 unsigned int size, struct fuse_copy_state *cs)
1087{
1088 switch (code) {
Tejun Heo95668a62008-11-26 12:03:55 +01001089 case FUSE_NOTIFY_POLL:
1090 return fuse_notify_poll(fc, size, cs);
1091
John Muir3b463ae2009-05-31 11:13:57 -04001092 case FUSE_NOTIFY_INVAL_INODE:
1093 return fuse_notify_inval_inode(fc, size, cs);
1094
1095 case FUSE_NOTIFY_INVAL_ENTRY:
1096 return fuse_notify_inval_entry(fc, size, cs);
1097
Tejun Heo85993962008-11-26 12:03:55 +01001098 default:
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001099 fuse_copy_finish(cs);
Tejun Heo85993962008-11-26 12:03:55 +01001100 return -EINVAL;
1101 }
1102}
1103
Miklos Szeredi334f4852005-09-09 13:10:27 -07001104/* Look up request on processing list by unique ID */
1105static struct fuse_req *request_find(struct fuse_conn *fc, u64 unique)
1106{
1107 struct list_head *entry;
1108
1109 list_for_each(entry, &fc->processing) {
1110 struct fuse_req *req;
1111 req = list_entry(entry, struct fuse_req, list);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001112 if (req->in.h.unique == unique || req->intr_unique == unique)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001113 return req;
1114 }
1115 return NULL;
1116}
1117
1118static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
1119 unsigned nbytes)
1120{
1121 unsigned reqsize = sizeof(struct fuse_out_header);
1122
1123 if (out->h.error)
1124 return nbytes != reqsize ? -EINVAL : 0;
1125
1126 reqsize += len_args(out->numargs, out->args);
1127
1128 if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
1129 return -EINVAL;
1130 else if (reqsize > nbytes) {
1131 struct fuse_arg *lastarg = &out->args[out->numargs-1];
1132 unsigned diffsize = reqsize - nbytes;
1133 if (diffsize > lastarg->size)
1134 return -EINVAL;
1135 lastarg->size -= diffsize;
1136 }
1137 return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
1138 out->page_zeroing);
1139}
1140
1141/*
1142 * Write a single reply to a request. First the header is copied from
1143 * the write buffer. The request is then searched on the processing
1144 * list by the unique ID found in the header. If found, then remove
1145 * it from the list and copy the rest of the buffer to the request.
1146 * The request is finished by calling request_end()
1147 */
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001148static ssize_t fuse_dev_do_write(struct fuse_conn *fc,
1149 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001150{
1151 int err;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001152 struct fuse_req *req;
1153 struct fuse_out_header oh;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001154
Miklos Szeredi334f4852005-09-09 13:10:27 -07001155 if (nbytes < sizeof(struct fuse_out_header))
1156 return -EINVAL;
1157
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001158 err = fuse_copy_one(cs, &oh, sizeof(oh));
Miklos Szeredi334f4852005-09-09 13:10:27 -07001159 if (err)
1160 goto err_finish;
Tejun Heo85993962008-11-26 12:03:55 +01001161
Miklos Szeredi334f4852005-09-09 13:10:27 -07001162 err = -EINVAL;
Tejun Heo85993962008-11-26 12:03:55 +01001163 if (oh.len != nbytes)
1164 goto err_finish;
1165
1166 /*
1167 * Zero oh.unique indicates unsolicited notification message
1168 * and error contains notification code.
1169 */
1170 if (!oh.unique) {
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001171 err = fuse_notify(fc, oh.error, nbytes - sizeof(oh), cs);
Tejun Heo85993962008-11-26 12:03:55 +01001172 return err ? err : nbytes;
1173 }
1174
1175 err = -EINVAL;
1176 if (oh.error <= -1000 || oh.error > 0)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001177 goto err_finish;
1178
Miklos Szeredid7133112006-04-10 22:54:55 -07001179 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001180 err = -ENOENT;
1181 if (!fc->connected)
1182 goto err_unlock;
1183
Miklos Szeredi334f4852005-09-09 13:10:27 -07001184 req = request_find(fc, oh.unique);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001185 if (!req)
1186 goto err_unlock;
1187
Miklos Szeredif9a28422006-06-25 05:48:53 -07001188 if (req->aborted) {
Miklos Szeredid7133112006-04-10 22:54:55 -07001189 spin_unlock(&fc->lock);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001190 fuse_copy_finish(cs);
Miklos Szeredid7133112006-04-10 22:54:55 -07001191 spin_lock(&fc->lock);
Miklos Szeredi222f1d62006-01-16 22:14:25 -08001192 request_end(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001193 return -ENOENT;
1194 }
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001195 /* Is it an interrupt reply? */
1196 if (req->intr_unique == oh.unique) {
1197 err = -EINVAL;
1198 if (nbytes != sizeof(struct fuse_out_header))
1199 goto err_unlock;
1200
1201 if (oh.error == -ENOSYS)
1202 fc->no_interrupt = 1;
1203 else if (oh.error == -EAGAIN)
1204 queue_interrupt(fc, req);
1205
1206 spin_unlock(&fc->lock);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001207 fuse_copy_finish(cs);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001208 return nbytes;
1209 }
1210
1211 req->state = FUSE_REQ_WRITING;
Miklos Szeredid77a1d52006-01-16 22:14:31 -08001212 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001213 req->out.h = oh;
1214 req->locked = 1;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001215 cs->req = req;
Miklos Szeredice534fb2010-05-25 15:06:07 +02001216 if (!req->out.page_replace)
1217 cs->move_pages = 0;
Miklos Szeredid7133112006-04-10 22:54:55 -07001218 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001219
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001220 err = copy_out_args(cs, &req->out, nbytes);
1221 fuse_copy_finish(cs);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001222
Miklos Szeredid7133112006-04-10 22:54:55 -07001223 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001224 req->locked = 0;
1225 if (!err) {
Miklos Szeredif9a28422006-06-25 05:48:53 -07001226 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001227 err = -ENOENT;
Miklos Szeredif9a28422006-06-25 05:48:53 -07001228 } else if (!req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001229 req->out.h.error = -EIO;
1230 request_end(fc, req);
1231
1232 return err ? err : nbytes;
1233
1234 err_unlock:
Miklos Szeredid7133112006-04-10 22:54:55 -07001235 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001236 err_finish:
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001237 fuse_copy_finish(cs);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001238 return err;
1239}
1240
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001241static ssize_t fuse_dev_write(struct kiocb *iocb, const struct iovec *iov,
1242 unsigned long nr_segs, loff_t pos)
1243{
1244 struct fuse_copy_state cs;
1245 struct fuse_conn *fc = fuse_get_conn(iocb->ki_filp);
1246 if (!fc)
1247 return -EPERM;
1248
1249 fuse_copy_init(&cs, fc, 0, NULL, iov, nr_segs);
1250
1251 return fuse_dev_do_write(fc, &cs, iov_length(iov, nr_segs));
1252}
1253
1254static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe,
1255 struct file *out, loff_t *ppos,
1256 size_t len, unsigned int flags)
1257{
1258 unsigned nbuf;
1259 unsigned idx;
1260 struct pipe_buffer *bufs;
1261 struct fuse_copy_state cs;
1262 struct fuse_conn *fc;
1263 size_t rem;
1264 ssize_t ret;
1265
1266 fc = fuse_get_conn(out);
1267 if (!fc)
1268 return -EPERM;
1269
1270 bufs = kmalloc(pipe->buffers * sizeof (struct pipe_buffer), GFP_KERNEL);
1271 if (!bufs)
1272 return -ENOMEM;
1273
1274 pipe_lock(pipe);
1275 nbuf = 0;
1276 rem = 0;
1277 for (idx = 0; idx < pipe->nrbufs && rem < len; idx++)
1278 rem += pipe->bufs[(pipe->curbuf + idx) & (pipe->buffers - 1)].len;
1279
1280 ret = -EINVAL;
1281 if (rem < len) {
1282 pipe_unlock(pipe);
1283 goto out;
1284 }
1285
1286 rem = len;
1287 while (rem) {
1288 struct pipe_buffer *ibuf;
1289 struct pipe_buffer *obuf;
1290
1291 BUG_ON(nbuf >= pipe->buffers);
1292 BUG_ON(!pipe->nrbufs);
1293 ibuf = &pipe->bufs[pipe->curbuf];
1294 obuf = &bufs[nbuf];
1295
1296 if (rem >= ibuf->len) {
1297 *obuf = *ibuf;
1298 ibuf->ops = NULL;
1299 pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
1300 pipe->nrbufs--;
1301 } else {
1302 ibuf->ops->get(pipe, ibuf);
1303 *obuf = *ibuf;
1304 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1305 obuf->len = rem;
1306 ibuf->offset += obuf->len;
1307 ibuf->len -= obuf->len;
1308 }
1309 nbuf++;
1310 rem -= obuf->len;
1311 }
1312 pipe_unlock(pipe);
1313
1314 memset(&cs, 0, sizeof(struct fuse_copy_state));
1315 cs.fc = fc;
1316 cs.write = 0;
1317 cs.pipebufs = bufs;
1318 cs.nr_segs = nbuf;
1319 cs.pipe = pipe;
1320
Miklos Szeredice534fb2010-05-25 15:06:07 +02001321 if (flags & SPLICE_F_MOVE)
1322 cs.move_pages = 1;
1323
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001324 ret = fuse_dev_do_write(fc, &cs, len);
1325
1326 for (idx = 0; idx < nbuf; idx++) {
1327 struct pipe_buffer *buf = &bufs[idx];
1328 buf->ops->release(pipe, buf);
1329 }
1330out:
1331 kfree(bufs);
1332 return ret;
1333}
1334
Miklos Szeredi334f4852005-09-09 13:10:27 -07001335static unsigned fuse_dev_poll(struct file *file, poll_table *wait)
1336{
Miklos Szeredi334f4852005-09-09 13:10:27 -07001337 unsigned mask = POLLOUT | POLLWRNORM;
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07001338 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001339 if (!fc)
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07001340 return POLLERR;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001341
1342 poll_wait(file, &fc->waitq, wait);
1343
Miklos Szeredid7133112006-04-10 22:54:55 -07001344 spin_lock(&fc->lock);
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07001345 if (!fc->connected)
1346 mask = POLLERR;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001347 else if (request_pending(fc))
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07001348 mask |= POLLIN | POLLRDNORM;
Miklos Szeredid7133112006-04-10 22:54:55 -07001349 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001350
1351 return mask;
1352}
1353
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001354/*
1355 * Abort all requests on the given list (pending or processing)
1356 *
Miklos Szeredid7133112006-04-10 22:54:55 -07001357 * This function releases and reacquires fc->lock
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001358 */
Miklos Szeredi334f4852005-09-09 13:10:27 -07001359static void end_requests(struct fuse_conn *fc, struct list_head *head)
Harvey Harrison5d9ec852008-12-02 14:49:42 +01001360__releases(&fc->lock)
1361__acquires(&fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001362{
1363 while (!list_empty(head)) {
1364 struct fuse_req *req;
1365 req = list_entry(head->next, struct fuse_req, list);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001366 req->out.h.error = -ECONNABORTED;
1367 request_end(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -07001368 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001369 }
1370}
1371
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001372/*
1373 * Abort requests under I/O
1374 *
Miklos Szeredif9a28422006-06-25 05:48:53 -07001375 * The requests are set to aborted and finished, and the request
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001376 * waiter is woken up. This will make request_wait_answer() wait
1377 * until the request is unlocked and then return.
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08001378 *
1379 * If the request is asynchronous, then the end function needs to be
1380 * called after waiting for the request to be unlocked (if it was
1381 * locked).
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001382 */
1383static void end_io_requests(struct fuse_conn *fc)
Harvey Harrison5d9ec852008-12-02 14:49:42 +01001384__releases(&fc->lock)
1385__acquires(&fc->lock)
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001386{
1387 while (!list_empty(&fc->io)) {
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08001388 struct fuse_req *req =
1389 list_entry(fc->io.next, struct fuse_req, list);
1390 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
1391
Miklos Szeredif9a28422006-06-25 05:48:53 -07001392 req->aborted = 1;
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001393 req->out.h.error = -ECONNABORTED;
1394 req->state = FUSE_REQ_FINISHED;
1395 list_del_init(&req->list);
1396 wake_up(&req->waitq);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08001397 if (end) {
1398 req->end = NULL;
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08001399 __fuse_get_request(req);
Miklos Szeredid7133112006-04-10 22:54:55 -07001400 spin_unlock(&fc->lock);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08001401 wait_event(req->waitq, !req->locked);
1402 end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +01001403 fuse_put_request(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -07001404 spin_lock(&fc->lock);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08001405 }
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001406 }
1407}
1408
1409/*
1410 * Abort all requests.
1411 *
1412 * Emergency exit in case of a malicious or accidental deadlock, or
1413 * just a hung filesystem.
1414 *
1415 * The same effect is usually achievable through killing the
1416 * filesystem daemon and all users of the filesystem. The exception
1417 * is the combination of an asynchronous request and the tricky
1418 * deadlock (see Documentation/filesystems/fuse.txt).
1419 *
1420 * During the aborting, progression of requests from the pending and
1421 * processing lists onto the io list, and progression of new requests
1422 * onto the pending list is prevented by req->connected being false.
1423 *
1424 * Progression of requests under I/O to the processing list is
Miklos Szeredif9a28422006-06-25 05:48:53 -07001425 * prevented by the req->aborted flag being true for these requests.
1426 * For this reason requests on the io list must be aborted first.
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001427 */
1428void fuse_abort_conn(struct fuse_conn *fc)
1429{
Miklos Szeredid7133112006-04-10 22:54:55 -07001430 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001431 if (fc->connected) {
1432 fc->connected = 0;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -07001433 fc->blocked = 0;
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001434 end_io_requests(fc);
1435 end_requests(fc, &fc->pending);
1436 end_requests(fc, &fc->processing);
1437 wake_up_all(&fc->waitq);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -07001438 wake_up_all(&fc->blocked_waitq);
Jeff Dike385a17b2006-04-10 22:54:52 -07001439 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001440 }
Miklos Szeredid7133112006-04-10 22:54:55 -07001441 spin_unlock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001442}
Tejun Heo08cbf542009-04-14 10:54:53 +09001443EXPORT_SYMBOL_GPL(fuse_abort_conn);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001444
Tejun Heo08cbf542009-04-14 10:54:53 +09001445int fuse_dev_release(struct inode *inode, struct file *file)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001446{
Miklos Szeredi0720b312006-04-10 22:54:55 -07001447 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001448 if (fc) {
Miklos Szeredid7133112006-04-10 22:54:55 -07001449 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -07001450 fc->connected = 0;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001451 end_requests(fc, &fc->pending);
1452 end_requests(fc, &fc->processing);
Miklos Szeredid7133112006-04-10 22:54:55 -07001453 spin_unlock(&fc->lock);
Miklos Szeredibafa9652006-06-25 05:48:51 -07001454 fuse_conn_put(fc);
Jeff Dike385a17b2006-04-10 22:54:52 -07001455 }
Miklos Szeredif543f252006-01-16 22:14:35 -08001456
Miklos Szeredi334f4852005-09-09 13:10:27 -07001457 return 0;
1458}
Tejun Heo08cbf542009-04-14 10:54:53 +09001459EXPORT_SYMBOL_GPL(fuse_dev_release);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001460
Jeff Dike385a17b2006-04-10 22:54:52 -07001461static int fuse_dev_fasync(int fd, struct file *file, int on)
1462{
1463 struct fuse_conn *fc = fuse_get_conn(file);
1464 if (!fc)
Miklos Szeredia87046d2006-04-10 22:54:56 -07001465 return -EPERM;
Jeff Dike385a17b2006-04-10 22:54:52 -07001466
1467 /* No locking - fasync_helper does its own locking */
1468 return fasync_helper(fd, file, on, &fc->fasync);
1469}
1470
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001471const struct file_operations fuse_dev_operations = {
Miklos Szeredi334f4852005-09-09 13:10:27 -07001472 .owner = THIS_MODULE,
1473 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07001474 .read = do_sync_read,
1475 .aio_read = fuse_dev_read,
1476 .write = do_sync_write,
1477 .aio_write = fuse_dev_write,
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001478 .splice_write = fuse_dev_splice_write,
Miklos Szeredi334f4852005-09-09 13:10:27 -07001479 .poll = fuse_dev_poll,
1480 .release = fuse_dev_release,
Jeff Dike385a17b2006-04-10 22:54:52 -07001481 .fasync = fuse_dev_fasync,
Miklos Szeredi334f4852005-09-09 13:10:27 -07001482};
Tejun Heo08cbf542009-04-14 10:54:53 +09001483EXPORT_SYMBOL_GPL(fuse_dev_operations);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001484
1485static struct miscdevice fuse_miscdevice = {
1486 .minor = FUSE_MINOR,
1487 .name = "fuse",
1488 .fops = &fuse_dev_operations,
1489};
1490
1491int __init fuse_dev_init(void)
1492{
1493 int err = -ENOMEM;
1494 fuse_req_cachep = kmem_cache_create("fuse_request",
1495 sizeof(struct fuse_req),
Paul Mundt20c2df82007-07-20 10:11:58 +09001496 0, 0, NULL);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001497 if (!fuse_req_cachep)
1498 goto out;
1499
1500 err = misc_register(&fuse_miscdevice);
1501 if (err)
1502 goto out_cache_clean;
1503
1504 return 0;
1505
1506 out_cache_clean:
1507 kmem_cache_destroy(fuse_req_cachep);
1508 out:
1509 return err;
1510}
1511
1512void fuse_dev_cleanup(void)
1513{
1514 misc_deregister(&fuse_miscdevice);
1515 kmem_cache_destroy(fuse_req_cachep);
1516}