blob: e53df5ebb2b89904046e8a06808f68cd25fd6b5d [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);
Kay Sievers578454f2010-05-20 18:07:20 +020021MODULE_ALIAS("devname:fuse");
Miklos Szeredi334f4852005-09-09 13:10:27 -070022
Christoph Lametere18b8902006-12-06 20:33:20 -080023static struct kmem_cache *fuse_req_cachep;
Miklos Szeredi334f4852005-09-09 13:10:27 -070024
Miklos Szeredi8bfc0162006-01-16 22:14:28 -080025static struct fuse_conn *fuse_get_conn(struct file *file)
Miklos Szeredi334f4852005-09-09 13:10:27 -070026{
Miklos Szeredi0720b312006-04-10 22:54:55 -070027 /*
28 * Lockless access is OK, because file->private data is set
29 * once during mount and is valid until the file is released.
30 */
31 return file->private_data;
Miklos Szeredi334f4852005-09-09 13:10:27 -070032}
33
Miklos Szeredi8bfc0162006-01-16 22:14:28 -080034static void fuse_request_init(struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -070035{
36 memset(req, 0, sizeof(*req));
37 INIT_LIST_HEAD(&req->list);
Miklos Szeredia4d27e72006-06-25 05:48:54 -070038 INIT_LIST_HEAD(&req->intr_entry);
Miklos Szeredi334f4852005-09-09 13:10:27 -070039 init_waitqueue_head(&req->waitq);
40 atomic_set(&req->count, 1);
41}
42
43struct fuse_req *fuse_request_alloc(void)
44{
Christoph Lametere94b1762006-12-06 20:33:17 -080045 struct fuse_req *req = kmem_cache_alloc(fuse_req_cachep, GFP_KERNEL);
Miklos Szeredi334f4852005-09-09 13:10:27 -070046 if (req)
47 fuse_request_init(req);
48 return req;
49}
Tejun Heo08cbf542009-04-14 10:54:53 +090050EXPORT_SYMBOL_GPL(fuse_request_alloc);
Miklos Szeredi334f4852005-09-09 13:10:27 -070051
Miklos Szeredi3be5a522008-04-30 00:54:41 -070052struct fuse_req *fuse_request_alloc_nofs(void)
53{
54 struct fuse_req *req = kmem_cache_alloc(fuse_req_cachep, GFP_NOFS);
55 if (req)
56 fuse_request_init(req);
57 return req;
58}
59
Miklos Szeredi334f4852005-09-09 13:10:27 -070060void fuse_request_free(struct fuse_req *req)
61{
62 kmem_cache_free(fuse_req_cachep, req);
63}
64
Miklos Szeredi8bfc0162006-01-16 22:14:28 -080065static void block_sigs(sigset_t *oldset)
Miklos Szeredi334f4852005-09-09 13:10:27 -070066{
67 sigset_t mask;
68
69 siginitsetinv(&mask, sigmask(SIGKILL));
70 sigprocmask(SIG_BLOCK, &mask, oldset);
71}
72
Miklos Szeredi8bfc0162006-01-16 22:14:28 -080073static void restore_sigs(sigset_t *oldset)
Miklos Szeredi334f4852005-09-09 13:10:27 -070074{
75 sigprocmask(SIG_SETMASK, oldset, NULL);
76}
77
Miklos Szeredi334f4852005-09-09 13:10:27 -070078static void __fuse_get_request(struct fuse_req *req)
79{
80 atomic_inc(&req->count);
81}
82
83/* Must be called with > 1 refcount */
84static void __fuse_put_request(struct fuse_req *req)
85{
86 BUG_ON(atomic_read(&req->count) < 2);
87 atomic_dec(&req->count);
88}
89
Miklos Szeredi33649c92006-06-25 05:48:52 -070090static void fuse_req_init_context(struct fuse_req *req)
91{
David Howells2186a712008-11-14 10:38:53 +110092 req->in.h.uid = current_fsuid();
93 req->in.h.gid = current_fsgid();
Miklos Szeredi33649c92006-06-25 05:48:52 -070094 req->in.h.pid = current->pid;
95}
96
Miklos Szeredice1d5a42006-04-10 22:54:58 -070097struct fuse_req *fuse_get_req(struct fuse_conn *fc)
Miklos Szeredi334f4852005-09-09 13:10:27 -070098{
Miklos Szeredi08a53cd2006-04-10 22:54:59 -070099 struct fuse_req *req;
100 sigset_t oldset;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200101 int intr;
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700102 int err;
103
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200104 atomic_inc(&fc->num_waiting);
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700105 block_sigs(&oldset);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200106 intr = wait_event_interruptible(fc->blocked_waitq, !fc->blocked);
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700107 restore_sigs(&oldset);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200108 err = -EINTR;
109 if (intr)
110 goto out;
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700111
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700112 err = -ENOTCONN;
113 if (!fc->connected)
114 goto out;
115
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700116 req = fuse_request_alloc();
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200117 err = -ENOMEM;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700118 if (!req)
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200119 goto out;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700120
Miklos Szeredi33649c92006-06-25 05:48:52 -0700121 fuse_req_init_context(req);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200122 req->waiting = 1;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700123 return req;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200124
125 out:
126 atomic_dec(&fc->num_waiting);
127 return ERR_PTR(err);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700128}
Tejun Heo08cbf542009-04-14 10:54:53 +0900129EXPORT_SYMBOL_GPL(fuse_get_req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700130
Miklos Szeredi33649c92006-06-25 05:48:52 -0700131/*
132 * Return request in fuse_file->reserved_req. However that may
133 * currently be in use. If that is the case, wait for it to become
134 * available.
135 */
136static struct fuse_req *get_reserved_req(struct fuse_conn *fc,
137 struct file *file)
138{
139 struct fuse_req *req = NULL;
140 struct fuse_file *ff = file->private_data;
141
142 do {
Miklos Szeredide5e3de2007-10-16 23:31:00 -0700143 wait_event(fc->reserved_req_waitq, ff->reserved_req);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700144 spin_lock(&fc->lock);
145 if (ff->reserved_req) {
146 req = ff->reserved_req;
147 ff->reserved_req = NULL;
148 get_file(file);
149 req->stolen_file = file;
150 }
151 spin_unlock(&fc->lock);
152 } while (!req);
153
154 return req;
155}
156
157/*
158 * Put stolen request back into fuse_file->reserved_req
159 */
160static void put_reserved_req(struct fuse_conn *fc, struct fuse_req *req)
161{
162 struct file *file = req->stolen_file;
163 struct fuse_file *ff = file->private_data;
164
165 spin_lock(&fc->lock);
166 fuse_request_init(req);
167 BUG_ON(ff->reserved_req);
168 ff->reserved_req = req;
Miklos Szeredide5e3de2007-10-16 23:31:00 -0700169 wake_up_all(&fc->reserved_req_waitq);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700170 spin_unlock(&fc->lock);
171 fput(file);
172}
173
174/*
175 * Gets a requests for a file operation, always succeeds
176 *
177 * This is used for sending the FLUSH request, which must get to
178 * userspace, due to POSIX locks which may need to be unlocked.
179 *
180 * If allocation fails due to OOM, use the reserved request in
181 * fuse_file.
182 *
183 * This is very unlikely to deadlock accidentally, since the
184 * filesystem should not have it's own file open. If deadlock is
185 * intentional, it can still be broken by "aborting" the filesystem.
186 */
187struct fuse_req *fuse_get_req_nofail(struct fuse_conn *fc, struct file *file)
188{
189 struct fuse_req *req;
190
191 atomic_inc(&fc->num_waiting);
192 wait_event(fc->blocked_waitq, !fc->blocked);
193 req = fuse_request_alloc();
194 if (!req)
195 req = get_reserved_req(fc, file);
196
197 fuse_req_init_context(req);
198 req->waiting = 1;
199 return req;
200}
201
Miklos Szeredi334f4852005-09-09 13:10:27 -0700202void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req)
203{
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800204 if (atomic_dec_and_test(&req->count)) {
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200205 if (req->waiting)
206 atomic_dec(&fc->num_waiting);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700207
208 if (req->stolen_file)
209 put_reserved_req(fc, req);
210 else
211 fuse_request_free(req);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800212 }
213}
Tejun Heo08cbf542009-04-14 10:54:53 +0900214EXPORT_SYMBOL_GPL(fuse_put_request);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800215
Miklos Szeredid12def12008-02-06 01:38:39 -0800216static unsigned len_args(unsigned numargs, struct fuse_arg *args)
217{
218 unsigned nbytes = 0;
219 unsigned i;
220
221 for (i = 0; i < numargs; i++)
222 nbytes += args[i].size;
223
224 return nbytes;
225}
226
227static u64 fuse_get_unique(struct fuse_conn *fc)
228{
229 fc->reqctr++;
230 /* zero is special */
231 if (fc->reqctr == 0)
232 fc->reqctr = 1;
233
234 return fc->reqctr;
235}
236
237static void queue_request(struct fuse_conn *fc, struct fuse_req *req)
238{
239 req->in.h.unique = fuse_get_unique(fc);
240 req->in.h.len = sizeof(struct fuse_in_header) +
241 len_args(req->in.numargs, (struct fuse_arg *) req->in.args);
242 list_add_tail(&req->list, &fc->pending);
243 req->state = FUSE_REQ_PENDING;
244 if (!req->waiting) {
245 req->waiting = 1;
246 atomic_inc(&fc->num_waiting);
247 }
248 wake_up(&fc->waitq);
249 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
250}
251
252static void flush_bg_queue(struct fuse_conn *fc)
253{
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700254 while (fc->active_background < fc->max_background &&
Miklos Szeredid12def12008-02-06 01:38:39 -0800255 !list_empty(&fc->bg_queue)) {
256 struct fuse_req *req;
257
258 req = list_entry(fc->bg_queue.next, struct fuse_req, list);
259 list_del(&req->list);
260 fc->active_background++;
261 queue_request(fc, req);
262 }
263}
264
Miklos Szeredi6dbbcb12006-04-26 10:49:06 +0200265/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700266 * This function is called when a request is finished. Either a reply
Miklos Szeredif9a28422006-06-25 05:48:53 -0700267 * has arrived or it was aborted (and not yet sent) or some error
Miklos Szeredif43b1552006-01-16 22:14:26 -0800268 * occurred during communication with userspace, or the device file
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700269 * was closed. The requester thread is woken up (if still waiting),
270 * the 'end' callback is called if given, else the reference to the
271 * request is released
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800272 *
Miklos Szeredid7133112006-04-10 22:54:55 -0700273 * Called with fc->lock, unlocks it
Miklos Szeredi334f4852005-09-09 13:10:27 -0700274 */
275static void request_end(struct fuse_conn *fc, struct fuse_req *req)
Harvey Harrison5d9ec852008-12-02 14:49:42 +0100276__releases(&fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700277{
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700278 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
279 req->end = NULL;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800280 list_del(&req->list);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700281 list_del(&req->intr_entry);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800282 req->state = FUSE_REQ_FINISHED;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700283 if (req->background) {
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700284 if (fc->num_background == fc->max_background) {
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700285 fc->blocked = 0;
286 wake_up_all(&fc->blocked_waitq);
287 }
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700288 if (fc->num_background == fc->congestion_threshold &&
Tejun Heoa325f9b2009-04-14 10:54:52 +0900289 fc->connected && fc->bdi_initialized) {
Jens Axboe8aa7e842009-07-09 14:52:32 +0200290 clear_bdi_congested(&fc->bdi, BLK_RW_SYNC);
291 clear_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
Miklos Szeredif92b99b2007-10-16 23:30:59 -0700292 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700293 fc->num_background--;
Miklos Szeredid12def12008-02-06 01:38:39 -0800294 fc->active_background--;
295 flush_bg_queue(fc);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700296 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700297 spin_unlock(&fc->lock);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700298 wake_up(&req->waitq);
299 if (end)
300 end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100301 fuse_put_request(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700302}
303
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700304static void wait_answer_interruptible(struct fuse_conn *fc,
305 struct fuse_req *req)
Harvey Harrison5d9ec852008-12-02 14:49:42 +0100306__releases(&fc->lock)
307__acquires(&fc->lock)
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700308{
309 if (signal_pending(current))
310 return;
311
312 spin_unlock(&fc->lock);
313 wait_event_interruptible(req->waitq, req->state == FUSE_REQ_FINISHED);
314 spin_lock(&fc->lock);
315}
316
317static void queue_interrupt(struct fuse_conn *fc, struct fuse_req *req)
318{
319 list_add_tail(&req->intr_entry, &fc->interrupts);
320 wake_up(&fc->waitq);
321 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
322}
323
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700324static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
Harvey Harrison5d9ec852008-12-02 14:49:42 +0100325__releases(&fc->lock)
326__acquires(&fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700327{
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700328 if (!fc->no_interrupt) {
329 /* Any signal may interrupt this */
330 wait_answer_interruptible(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700331
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700332 if (req->aborted)
333 goto aborted;
334 if (req->state == FUSE_REQ_FINISHED)
335 return;
336
337 req->interrupted = 1;
338 if (req->state == FUSE_REQ_SENT)
339 queue_interrupt(fc, req);
340 }
341
Miklos Szeredia131de02007-10-16 23:31:04 -0700342 if (!req->force) {
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700343 sigset_t oldset;
344
345 /* Only fatal signals may interrupt this */
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700346 block_sigs(&oldset);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700347 wait_answer_interruptible(fc, req);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700348 restore_sigs(&oldset);
Miklos Szeredia131de02007-10-16 23:31:04 -0700349
350 if (req->aborted)
351 goto aborted;
352 if (req->state == FUSE_REQ_FINISHED)
353 return;
354
355 /* Request is not yet in userspace, bail out */
356 if (req->state == FUSE_REQ_PENDING) {
357 list_del(&req->list);
358 __fuse_put_request(req);
359 req->out.h.error = -EINTR;
360 return;
361 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700362 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700363
Miklos Szeredia131de02007-10-16 23:31:04 -0700364 /*
365 * Either request is already in userspace, or it was forced.
366 * Wait it out.
367 */
368 spin_unlock(&fc->lock);
369 wait_event(req->waitq, req->state == FUSE_REQ_FINISHED);
370 spin_lock(&fc->lock);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700371
Miklos Szeredia131de02007-10-16 23:31:04 -0700372 if (!req->aborted)
373 return;
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700374
375 aborted:
Miklos Szeredia131de02007-10-16 23:31:04 -0700376 BUG_ON(req->state != FUSE_REQ_FINISHED);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700377 if (req->locked) {
378 /* This is uninterruptible sleep, because data is
379 being copied to/from the buffers of req. During
380 locked state, there mustn't be any filesystem
381 operation (e.g. page fault), since that could lead
382 to deadlock */
Miklos Szeredid7133112006-04-10 22:54:55 -0700383 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700384 wait_event(req->waitq, !req->locked);
Miklos Szeredid7133112006-04-10 22:54:55 -0700385 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700386 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700387}
388
Tejun Heob93f8582008-11-26 12:03:55 +0100389void fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700390{
391 req->isreply = 1;
Miklos Szeredid7133112006-04-10 22:54:55 -0700392 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700393 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700394 req->out.h.error = -ENOTCONN;
395 else if (fc->conn_error)
396 req->out.h.error = -ECONNREFUSED;
397 else {
398 queue_request(fc, req);
399 /* acquire extra reference, since request is still needed
400 after request_end() */
401 __fuse_get_request(req);
402
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700403 request_wait_answer(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700404 }
Miklos Szeredid7133112006-04-10 22:54:55 -0700405 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700406}
Tejun Heo08cbf542009-04-14 10:54:53 +0900407EXPORT_SYMBOL_GPL(fuse_request_send);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700408
Tejun Heob93f8582008-11-26 12:03:55 +0100409static void fuse_request_send_nowait_locked(struct fuse_conn *fc,
410 struct fuse_req *req)
Miklos Szeredid12def12008-02-06 01:38:39 -0800411{
412 req->background = 1;
413 fc->num_background++;
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700414 if (fc->num_background == fc->max_background)
Miklos Szeredid12def12008-02-06 01:38:39 -0800415 fc->blocked = 1;
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700416 if (fc->num_background == fc->congestion_threshold &&
Tejun Heoa325f9b2009-04-14 10:54:52 +0900417 fc->bdi_initialized) {
Jens Axboe8aa7e842009-07-09 14:52:32 +0200418 set_bdi_congested(&fc->bdi, BLK_RW_SYNC);
419 set_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
Miklos Szeredid12def12008-02-06 01:38:39 -0800420 }
421 list_add_tail(&req->list, &fc->bg_queue);
422 flush_bg_queue(fc);
423}
424
Tejun Heob93f8582008-11-26 12:03:55 +0100425static void fuse_request_send_nowait(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700426{
Miklos Szeredid7133112006-04-10 22:54:55 -0700427 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700428 if (fc->connected) {
Tejun Heob93f8582008-11-26 12:03:55 +0100429 fuse_request_send_nowait_locked(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700430 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700431 } else {
432 req->out.h.error = -ENOTCONN;
433 request_end(fc, req);
434 }
435}
436
Tejun Heob93f8582008-11-26 12:03:55 +0100437void fuse_request_send_noreply(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700438{
439 req->isreply = 0;
Tejun Heob93f8582008-11-26 12:03:55 +0100440 fuse_request_send_nowait(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700441}
442
Tejun Heob93f8582008-11-26 12:03:55 +0100443void fuse_request_send_background(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700444{
445 req->isreply = 1;
Tejun Heob93f8582008-11-26 12:03:55 +0100446 fuse_request_send_nowait(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700447}
Tejun Heo08cbf542009-04-14 10:54:53 +0900448EXPORT_SYMBOL_GPL(fuse_request_send_background);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700449
Miklos Szeredi334f4852005-09-09 13:10:27 -0700450/*
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700451 * Called under fc->lock
452 *
453 * fc->connected must have been checked previously
454 */
Tejun Heob93f8582008-11-26 12:03:55 +0100455void fuse_request_send_background_locked(struct fuse_conn *fc,
456 struct fuse_req *req)
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700457{
458 req->isreply = 1;
Tejun Heob93f8582008-11-26 12:03:55 +0100459 fuse_request_send_nowait_locked(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700460}
461
462/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700463 * Lock the request. Up to the next unlock_request() there mustn't be
464 * anything that could cause a page-fault. If the request was already
Miklos Szeredif9a28422006-06-25 05:48:53 -0700465 * aborted bail out.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700466 */
Miklos Szeredid7133112006-04-10 22:54:55 -0700467static int lock_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700468{
469 int err = 0;
470 if (req) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700471 spin_lock(&fc->lock);
Miklos Szeredif9a28422006-06-25 05:48:53 -0700472 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700473 err = -ENOENT;
474 else
475 req->locked = 1;
Miklos Szeredid7133112006-04-10 22:54:55 -0700476 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700477 }
478 return err;
479}
480
481/*
Miklos Szeredif9a28422006-06-25 05:48:53 -0700482 * Unlock request. If it was aborted during being locked, the
Miklos Szeredi334f4852005-09-09 13:10:27 -0700483 * requester thread is currently waiting for it to be unlocked, so
484 * wake it up.
485 */
Miklos Szeredid7133112006-04-10 22:54:55 -0700486static void unlock_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700487{
488 if (req) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700489 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700490 req->locked = 0;
Miklos Szeredif9a28422006-06-25 05:48:53 -0700491 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700492 wake_up(&req->waitq);
Miklos Szeredid7133112006-04-10 22:54:55 -0700493 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700494 }
495}
496
497struct fuse_copy_state {
Miklos Szeredid7133112006-04-10 22:54:55 -0700498 struct fuse_conn *fc;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700499 int write;
500 struct fuse_req *req;
501 const struct iovec *iov;
502 unsigned long nr_segs;
503 unsigned long seglen;
504 unsigned long addr;
505 struct page *pg;
506 void *mapaddr;
507 void *buf;
508 unsigned len;
509};
510
Miklos Szeredid7133112006-04-10 22:54:55 -0700511static void fuse_copy_init(struct fuse_copy_state *cs, struct fuse_conn *fc,
512 int write, struct fuse_req *req,
513 const struct iovec *iov, unsigned long nr_segs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700514{
515 memset(cs, 0, sizeof(*cs));
Miklos Szeredid7133112006-04-10 22:54:55 -0700516 cs->fc = fc;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700517 cs->write = write;
518 cs->req = req;
519 cs->iov = iov;
520 cs->nr_segs = nr_segs;
521}
522
523/* Unmap and put previous page of userspace buffer */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800524static void fuse_copy_finish(struct fuse_copy_state *cs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700525{
526 if (cs->mapaddr) {
527 kunmap_atomic(cs->mapaddr, KM_USER0);
528 if (cs->write) {
529 flush_dcache_page(cs->pg);
530 set_page_dirty_lock(cs->pg);
531 }
532 put_page(cs->pg);
533 cs->mapaddr = NULL;
534 }
535}
536
537/*
538 * Get another pagefull of userspace buffer, and map it to kernel
539 * address space, and lock request
540 */
541static int fuse_copy_fill(struct fuse_copy_state *cs)
542{
543 unsigned long offset;
544 int err;
545
Miklos Szeredid7133112006-04-10 22:54:55 -0700546 unlock_request(cs->fc, cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700547 fuse_copy_finish(cs);
548 if (!cs->seglen) {
549 BUG_ON(!cs->nr_segs);
550 cs->seglen = cs->iov[0].iov_len;
551 cs->addr = (unsigned long) cs->iov[0].iov_base;
Miklos Szeredi1729a162008-11-26 12:03:54 +0100552 cs->iov++;
553 cs->nr_segs--;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700554 }
555 down_read(&current->mm->mmap_sem);
556 err = get_user_pages(current, current->mm, cs->addr, 1, cs->write, 0,
557 &cs->pg, NULL);
558 up_read(&current->mm->mmap_sem);
559 if (err < 0)
560 return err;
561 BUG_ON(err != 1);
562 offset = cs->addr % PAGE_SIZE;
563 cs->mapaddr = kmap_atomic(cs->pg, KM_USER0);
564 cs->buf = cs->mapaddr + offset;
565 cs->len = min(PAGE_SIZE - offset, cs->seglen);
566 cs->seglen -= cs->len;
567 cs->addr += cs->len;
568
Miklos Szeredid7133112006-04-10 22:54:55 -0700569 return lock_request(cs->fc, cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700570}
571
572/* Do as much copy to/from userspace buffer as we can */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800573static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700574{
575 unsigned ncpy = min(*size, cs->len);
576 if (val) {
577 if (cs->write)
578 memcpy(cs->buf, *val, ncpy);
579 else
580 memcpy(*val, cs->buf, ncpy);
581 *val += ncpy;
582 }
583 *size -= ncpy;
584 cs->len -= ncpy;
585 cs->buf += ncpy;
586 return ncpy;
587}
588
589/*
590 * Copy a page in the request to/from the userspace buffer. Must be
591 * done atomically
592 */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800593static int fuse_copy_page(struct fuse_copy_state *cs, struct page *page,
594 unsigned offset, unsigned count, int zeroing)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700595{
596 if (page && zeroing && count < PAGE_SIZE) {
597 void *mapaddr = kmap_atomic(page, KM_USER1);
598 memset(mapaddr, 0, PAGE_SIZE);
599 kunmap_atomic(mapaddr, KM_USER1);
600 }
601 while (count) {
Miklos Szeredi1729a162008-11-26 12:03:54 +0100602 if (!cs->len) {
603 int err = fuse_copy_fill(cs);
604 if (err)
605 return err;
606 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700607 if (page) {
608 void *mapaddr = kmap_atomic(page, KM_USER1);
609 void *buf = mapaddr + offset;
610 offset += fuse_copy_do(cs, &buf, &count);
611 kunmap_atomic(mapaddr, KM_USER1);
612 } else
613 offset += fuse_copy_do(cs, NULL, &count);
614 }
615 if (page && !cs->write)
616 flush_dcache_page(page);
617 return 0;
618}
619
620/* Copy pages in the request to/from userspace buffer */
621static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
622 int zeroing)
623{
624 unsigned i;
625 struct fuse_req *req = cs->req;
626 unsigned offset = req->page_offset;
627 unsigned count = min(nbytes, (unsigned) PAGE_SIZE - offset);
628
629 for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
630 struct page *page = req->pages[i];
631 int err = fuse_copy_page(cs, page, offset, count, zeroing);
632 if (err)
633 return err;
634
635 nbytes -= count;
636 count = min(nbytes, (unsigned) PAGE_SIZE);
637 offset = 0;
638 }
639 return 0;
640}
641
642/* Copy a single argument in the request to/from userspace buffer */
643static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
644{
645 while (size) {
Miklos Szeredi1729a162008-11-26 12:03:54 +0100646 if (!cs->len) {
647 int err = fuse_copy_fill(cs);
648 if (err)
649 return err;
650 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700651 fuse_copy_do(cs, &val, &size);
652 }
653 return 0;
654}
655
656/* Copy request arguments to/from userspace buffer */
657static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
658 unsigned argpages, struct fuse_arg *args,
659 int zeroing)
660{
661 int err = 0;
662 unsigned i;
663
664 for (i = 0; !err && i < numargs; i++) {
665 struct fuse_arg *arg = &args[i];
666 if (i == numargs - 1 && argpages)
667 err = fuse_copy_pages(cs, arg->size, zeroing);
668 else
669 err = fuse_copy_one(cs, arg->value, arg->size);
670 }
671 return err;
672}
673
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700674static int request_pending(struct fuse_conn *fc)
675{
676 return !list_empty(&fc->pending) || !list_empty(&fc->interrupts);
677}
678
Miklos Szeredi334f4852005-09-09 13:10:27 -0700679/* Wait until a request is available on the pending list */
680static void request_wait(struct fuse_conn *fc)
Harvey Harrison5d9ec852008-12-02 14:49:42 +0100681__releases(&fc->lock)
682__acquires(&fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700683{
684 DECLARE_WAITQUEUE(wait, current);
685
686 add_wait_queue_exclusive(&fc->waitq, &wait);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700687 while (fc->connected && !request_pending(fc)) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700688 set_current_state(TASK_INTERRUPTIBLE);
689 if (signal_pending(current))
690 break;
691
Miklos Szeredid7133112006-04-10 22:54:55 -0700692 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700693 schedule();
Miklos Szeredid7133112006-04-10 22:54:55 -0700694 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700695 }
696 set_current_state(TASK_RUNNING);
697 remove_wait_queue(&fc->waitq, &wait);
698}
699
700/*
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700701 * Transfer an interrupt request to userspace
702 *
703 * Unlike other requests this is assembled on demand, without a need
704 * to allocate a separate fuse_req structure.
705 *
706 * Called with fc->lock held, releases it
707 */
708static int fuse_read_interrupt(struct fuse_conn *fc, struct fuse_req *req,
709 const struct iovec *iov, unsigned long nr_segs)
Harvey Harrison5d9ec852008-12-02 14:49:42 +0100710__releases(&fc->lock)
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700711{
712 struct fuse_copy_state cs;
713 struct fuse_in_header ih;
714 struct fuse_interrupt_in arg;
715 unsigned reqsize = sizeof(ih) + sizeof(arg);
716 int err;
717
718 list_del_init(&req->intr_entry);
719 req->intr_unique = fuse_get_unique(fc);
720 memset(&ih, 0, sizeof(ih));
721 memset(&arg, 0, sizeof(arg));
722 ih.len = reqsize;
723 ih.opcode = FUSE_INTERRUPT;
724 ih.unique = req->intr_unique;
725 arg.unique = req->in.h.unique;
726
727 spin_unlock(&fc->lock);
728 if (iov_length(iov, nr_segs) < reqsize)
729 return -EINVAL;
730
731 fuse_copy_init(&cs, fc, 1, NULL, iov, nr_segs);
732 err = fuse_copy_one(&cs, &ih, sizeof(ih));
733 if (!err)
734 err = fuse_copy_one(&cs, &arg, sizeof(arg));
735 fuse_copy_finish(&cs);
736
737 return err ? err : reqsize;
738}
739
740/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700741 * Read a single request into the userspace filesystem's buffer. This
742 * function waits until a request is available, then removes it from
743 * the pending list and copies request data to userspace buffer. If
Miklos Szeredif9a28422006-06-25 05:48:53 -0700744 * no reply is needed (FORGET) or request has been aborted or there
745 * was an error during the copying then it's finished by calling
Miklos Szeredi334f4852005-09-09 13:10:27 -0700746 * request_end(). Otherwise add it to the processing list, and set
747 * the 'sent' flag.
748 */
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700749static ssize_t fuse_dev_read(struct kiocb *iocb, const struct iovec *iov,
750 unsigned long nr_segs, loff_t pos)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700751{
752 int err;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700753 struct fuse_req *req;
754 struct fuse_in *in;
755 struct fuse_copy_state cs;
756 unsigned reqsize;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700757 struct file *file = iocb->ki_filp;
Miklos Szeredi0720b312006-04-10 22:54:55 -0700758 struct fuse_conn *fc = fuse_get_conn(file);
759 if (!fc)
760 return -EPERM;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700761
Miklos Szeredi1d3d7522006-01-06 00:19:40 -0800762 restart:
Miklos Szeredid7133112006-04-10 22:54:55 -0700763 spin_lock(&fc->lock);
Jeff Dikee5ac1d12006-04-10 22:54:53 -0700764 err = -EAGAIN;
765 if ((file->f_flags & O_NONBLOCK) && fc->connected &&
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700766 !request_pending(fc))
Jeff Dikee5ac1d12006-04-10 22:54:53 -0700767 goto err_unlock;
768
Miklos Szeredi334f4852005-09-09 13:10:27 -0700769 request_wait(fc);
770 err = -ENODEV;
Miklos Szeredi9ba7cbb2006-01-16 22:14:34 -0800771 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700772 goto err_unlock;
773 err = -ERESTARTSYS;
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700774 if (!request_pending(fc))
Miklos Szeredi334f4852005-09-09 13:10:27 -0700775 goto err_unlock;
776
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700777 if (!list_empty(&fc->interrupts)) {
778 req = list_entry(fc->interrupts.next, struct fuse_req,
779 intr_entry);
780 return fuse_read_interrupt(fc, req, iov, nr_segs);
781 }
782
Miklos Szeredi334f4852005-09-09 13:10:27 -0700783 req = list_entry(fc->pending.next, struct fuse_req, list);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800784 req->state = FUSE_REQ_READING;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800785 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700786
787 in = &req->in;
Miklos Szeredi1d3d7522006-01-06 00:19:40 -0800788 reqsize = in->h.len;
789 /* If request is too large, reply with an error and restart the read */
790 if (iov_length(iov, nr_segs) < reqsize) {
791 req->out.h.error = -EIO;
792 /* SETXATTR is special, since it may contain too large data */
793 if (in->h.opcode == FUSE_SETXATTR)
794 req->out.h.error = -E2BIG;
795 request_end(fc, req);
796 goto restart;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700797 }
Miklos Szeredid7133112006-04-10 22:54:55 -0700798 spin_unlock(&fc->lock);
799 fuse_copy_init(&cs, fc, 1, req, iov, nr_segs);
Miklos Szeredi1d3d7522006-01-06 00:19:40 -0800800 err = fuse_copy_one(&cs, &in->h, sizeof(in->h));
801 if (!err)
802 err = fuse_copy_args(&cs, in->numargs, in->argpages,
803 (struct fuse_arg *) in->args, 0);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700804 fuse_copy_finish(&cs);
Miklos Szeredid7133112006-04-10 22:54:55 -0700805 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700806 req->locked = 0;
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -0700807 if (req->aborted) {
808 request_end(fc, req);
809 return -ENODEV;
810 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700811 if (err) {
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -0700812 req->out.h.error = -EIO;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700813 request_end(fc, req);
814 return err;
815 }
816 if (!req->isreply)
817 request_end(fc, req);
818 else {
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800819 req->state = FUSE_REQ_SENT;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800820 list_move_tail(&req->list, &fc->processing);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700821 if (req->interrupted)
822 queue_interrupt(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700823 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700824 }
825 return reqsize;
826
827 err_unlock:
Miklos Szeredid7133112006-04-10 22:54:55 -0700828 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700829 return err;
830}
831
Tejun Heo95668a62008-11-26 12:03:55 +0100832static int fuse_notify_poll(struct fuse_conn *fc, unsigned int size,
833 struct fuse_copy_state *cs)
834{
835 struct fuse_notify_poll_wakeup_out outarg;
Miklos Szeredif6d47a12009-01-26 15:00:59 +0100836 int err = -EINVAL;
Tejun Heo95668a62008-11-26 12:03:55 +0100837
838 if (size != sizeof(outarg))
Miklos Szeredif6d47a12009-01-26 15:00:59 +0100839 goto err;
Tejun Heo95668a62008-11-26 12:03:55 +0100840
841 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
842 if (err)
Miklos Szeredif6d47a12009-01-26 15:00:59 +0100843 goto err;
Tejun Heo95668a62008-11-26 12:03:55 +0100844
Miklos Szeredif6d47a12009-01-26 15:00:59 +0100845 fuse_copy_finish(cs);
Tejun Heo95668a62008-11-26 12:03:55 +0100846 return fuse_notify_poll_wakeup(fc, &outarg);
Miklos Szeredif6d47a12009-01-26 15:00:59 +0100847
848err:
849 fuse_copy_finish(cs);
850 return err;
Tejun Heo95668a62008-11-26 12:03:55 +0100851}
852
John Muir3b463ae2009-05-31 11:13:57 -0400853static int fuse_notify_inval_inode(struct fuse_conn *fc, unsigned int size,
854 struct fuse_copy_state *cs)
855{
856 struct fuse_notify_inval_inode_out outarg;
857 int err = -EINVAL;
858
859 if (size != sizeof(outarg))
860 goto err;
861
862 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
863 if (err)
864 goto err;
865 fuse_copy_finish(cs);
866
867 down_read(&fc->killsb);
868 err = -ENOENT;
Miklos Szeredib21dda42010-02-05 12:08:31 +0100869 if (fc->sb) {
870 err = fuse_reverse_inval_inode(fc->sb, outarg.ino,
871 outarg.off, outarg.len);
872 }
John Muir3b463ae2009-05-31 11:13:57 -0400873 up_read(&fc->killsb);
874 return err;
875
876err:
877 fuse_copy_finish(cs);
878 return err;
879}
880
881static int fuse_notify_inval_entry(struct fuse_conn *fc, unsigned int size,
882 struct fuse_copy_state *cs)
883{
884 struct fuse_notify_inval_entry_out outarg;
Fang Wenqib2d82ee2009-12-30 18:37:13 +0800885 int err = -ENOMEM;
886 char *buf;
John Muir3b463ae2009-05-31 11:13:57 -0400887 struct qstr name;
888
Fang Wenqib2d82ee2009-12-30 18:37:13 +0800889 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
890 if (!buf)
891 goto err;
892
893 err = -EINVAL;
John Muir3b463ae2009-05-31 11:13:57 -0400894 if (size < sizeof(outarg))
895 goto err;
896
897 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
898 if (err)
899 goto err;
900
901 err = -ENAMETOOLONG;
902 if (outarg.namelen > FUSE_NAME_MAX)
903 goto err;
904
905 name.name = buf;
906 name.len = outarg.namelen;
907 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
908 if (err)
909 goto err;
910 fuse_copy_finish(cs);
911 buf[outarg.namelen] = 0;
912 name.hash = full_name_hash(name.name, name.len);
913
914 down_read(&fc->killsb);
915 err = -ENOENT;
Miklos Szeredib21dda42010-02-05 12:08:31 +0100916 if (fc->sb)
917 err = fuse_reverse_inval_entry(fc->sb, outarg.parent, &name);
John Muir3b463ae2009-05-31 11:13:57 -0400918 up_read(&fc->killsb);
Fang Wenqib2d82ee2009-12-30 18:37:13 +0800919 kfree(buf);
John Muir3b463ae2009-05-31 11:13:57 -0400920 return err;
921
922err:
Fang Wenqib2d82ee2009-12-30 18:37:13 +0800923 kfree(buf);
John Muir3b463ae2009-05-31 11:13:57 -0400924 fuse_copy_finish(cs);
925 return err;
926}
927
Tejun Heo85993962008-11-26 12:03:55 +0100928static int fuse_notify(struct fuse_conn *fc, enum fuse_notify_code code,
929 unsigned int size, struct fuse_copy_state *cs)
930{
931 switch (code) {
Tejun Heo95668a62008-11-26 12:03:55 +0100932 case FUSE_NOTIFY_POLL:
933 return fuse_notify_poll(fc, size, cs);
934
John Muir3b463ae2009-05-31 11:13:57 -0400935 case FUSE_NOTIFY_INVAL_INODE:
936 return fuse_notify_inval_inode(fc, size, cs);
937
938 case FUSE_NOTIFY_INVAL_ENTRY:
939 return fuse_notify_inval_entry(fc, size, cs);
940
Tejun Heo85993962008-11-26 12:03:55 +0100941 default:
Miklos Szeredif6d47a12009-01-26 15:00:59 +0100942 fuse_copy_finish(cs);
Tejun Heo85993962008-11-26 12:03:55 +0100943 return -EINVAL;
944 }
945}
946
Miklos Szeredi334f4852005-09-09 13:10:27 -0700947/* Look up request on processing list by unique ID */
948static struct fuse_req *request_find(struct fuse_conn *fc, u64 unique)
949{
950 struct list_head *entry;
951
952 list_for_each(entry, &fc->processing) {
953 struct fuse_req *req;
954 req = list_entry(entry, struct fuse_req, list);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700955 if (req->in.h.unique == unique || req->intr_unique == unique)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700956 return req;
957 }
958 return NULL;
959}
960
961static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
962 unsigned nbytes)
963{
964 unsigned reqsize = sizeof(struct fuse_out_header);
965
966 if (out->h.error)
967 return nbytes != reqsize ? -EINVAL : 0;
968
969 reqsize += len_args(out->numargs, out->args);
970
971 if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
972 return -EINVAL;
973 else if (reqsize > nbytes) {
974 struct fuse_arg *lastarg = &out->args[out->numargs-1];
975 unsigned diffsize = reqsize - nbytes;
976 if (diffsize > lastarg->size)
977 return -EINVAL;
978 lastarg->size -= diffsize;
979 }
980 return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
981 out->page_zeroing);
982}
983
984/*
985 * Write a single reply to a request. First the header is copied from
986 * the write buffer. The request is then searched on the processing
987 * list by the unique ID found in the header. If found, then remove
988 * it from the list and copy the rest of the buffer to the request.
989 * The request is finished by calling request_end()
990 */
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700991static ssize_t fuse_dev_write(struct kiocb *iocb, const struct iovec *iov,
992 unsigned long nr_segs, loff_t pos)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700993{
994 int err;
Csaba Henkb4c458b2009-06-29 03:26:53 +0200995 size_t nbytes = iov_length(iov, nr_segs);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700996 struct fuse_req *req;
997 struct fuse_out_header oh;
998 struct fuse_copy_state cs;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700999 struct fuse_conn *fc = fuse_get_conn(iocb->ki_filp);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001000 if (!fc)
Miklos Szeredia87046d2006-04-10 22:54:56 -07001001 return -EPERM;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001002
Miklos Szeredid7133112006-04-10 22:54:55 -07001003 fuse_copy_init(&cs, fc, 0, NULL, iov, nr_segs);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001004 if (nbytes < sizeof(struct fuse_out_header))
1005 return -EINVAL;
1006
1007 err = fuse_copy_one(&cs, &oh, sizeof(oh));
1008 if (err)
1009 goto err_finish;
Tejun Heo85993962008-11-26 12:03:55 +01001010
Miklos Szeredi334f4852005-09-09 13:10:27 -07001011 err = -EINVAL;
Tejun Heo85993962008-11-26 12:03:55 +01001012 if (oh.len != nbytes)
1013 goto err_finish;
1014
1015 /*
1016 * Zero oh.unique indicates unsolicited notification message
1017 * and error contains notification code.
1018 */
1019 if (!oh.unique) {
1020 err = fuse_notify(fc, oh.error, nbytes - sizeof(oh), &cs);
Tejun Heo85993962008-11-26 12:03:55 +01001021 return err ? err : nbytes;
1022 }
1023
1024 err = -EINVAL;
1025 if (oh.error <= -1000 || oh.error > 0)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001026 goto err_finish;
1027
Miklos Szeredid7133112006-04-10 22:54:55 -07001028 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001029 err = -ENOENT;
1030 if (!fc->connected)
1031 goto err_unlock;
1032
Miklos Szeredi334f4852005-09-09 13:10:27 -07001033 req = request_find(fc, oh.unique);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001034 if (!req)
1035 goto err_unlock;
1036
Miklos Szeredif9a28422006-06-25 05:48:53 -07001037 if (req->aborted) {
Miklos Szeredid7133112006-04-10 22:54:55 -07001038 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001039 fuse_copy_finish(&cs);
Miklos Szeredid7133112006-04-10 22:54:55 -07001040 spin_lock(&fc->lock);
Miklos Szeredi222f1d62006-01-16 22:14:25 -08001041 request_end(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001042 return -ENOENT;
1043 }
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001044 /* Is it an interrupt reply? */
1045 if (req->intr_unique == oh.unique) {
1046 err = -EINVAL;
1047 if (nbytes != sizeof(struct fuse_out_header))
1048 goto err_unlock;
1049
1050 if (oh.error == -ENOSYS)
1051 fc->no_interrupt = 1;
1052 else if (oh.error == -EAGAIN)
1053 queue_interrupt(fc, req);
1054
1055 spin_unlock(&fc->lock);
1056 fuse_copy_finish(&cs);
1057 return nbytes;
1058 }
1059
1060 req->state = FUSE_REQ_WRITING;
Miklos Szeredid77a1d52006-01-16 22:14:31 -08001061 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001062 req->out.h = oh;
1063 req->locked = 1;
1064 cs.req = req;
Miklos Szeredid7133112006-04-10 22:54:55 -07001065 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001066
1067 err = copy_out_args(&cs, &req->out, nbytes);
1068 fuse_copy_finish(&cs);
1069
Miklos Szeredid7133112006-04-10 22:54:55 -07001070 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001071 req->locked = 0;
1072 if (!err) {
Miklos Szeredif9a28422006-06-25 05:48:53 -07001073 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001074 err = -ENOENT;
Miklos Szeredif9a28422006-06-25 05:48:53 -07001075 } else if (!req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001076 req->out.h.error = -EIO;
1077 request_end(fc, req);
1078
1079 return err ? err : nbytes;
1080
1081 err_unlock:
Miklos Szeredid7133112006-04-10 22:54:55 -07001082 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001083 err_finish:
1084 fuse_copy_finish(&cs);
1085 return err;
1086}
1087
Miklos Szeredi334f4852005-09-09 13:10:27 -07001088static unsigned fuse_dev_poll(struct file *file, poll_table *wait)
1089{
Miklos Szeredi334f4852005-09-09 13:10:27 -07001090 unsigned mask = POLLOUT | POLLWRNORM;
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07001091 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001092 if (!fc)
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07001093 return POLLERR;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001094
1095 poll_wait(file, &fc->waitq, wait);
1096
Miklos Szeredid7133112006-04-10 22:54:55 -07001097 spin_lock(&fc->lock);
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07001098 if (!fc->connected)
1099 mask = POLLERR;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001100 else if (request_pending(fc))
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07001101 mask |= POLLIN | POLLRDNORM;
Miklos Szeredid7133112006-04-10 22:54:55 -07001102 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001103
1104 return mask;
1105}
1106
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001107/*
1108 * Abort all requests on the given list (pending or processing)
1109 *
Miklos Szeredid7133112006-04-10 22:54:55 -07001110 * This function releases and reacquires fc->lock
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001111 */
Miklos Szeredi334f4852005-09-09 13:10:27 -07001112static void end_requests(struct fuse_conn *fc, struct list_head *head)
Harvey Harrison5d9ec852008-12-02 14:49:42 +01001113__releases(&fc->lock)
1114__acquires(&fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001115{
1116 while (!list_empty(head)) {
1117 struct fuse_req *req;
1118 req = list_entry(head->next, struct fuse_req, list);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001119 req->out.h.error = -ECONNABORTED;
1120 request_end(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -07001121 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001122 }
1123}
1124
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001125/*
1126 * Abort requests under I/O
1127 *
Miklos Szeredif9a28422006-06-25 05:48:53 -07001128 * The requests are set to aborted and finished, and the request
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001129 * waiter is woken up. This will make request_wait_answer() wait
1130 * until the request is unlocked and then return.
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08001131 *
1132 * If the request is asynchronous, then the end function needs to be
1133 * called after waiting for the request to be unlocked (if it was
1134 * locked).
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001135 */
1136static void end_io_requests(struct fuse_conn *fc)
Harvey Harrison5d9ec852008-12-02 14:49:42 +01001137__releases(&fc->lock)
1138__acquires(&fc->lock)
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001139{
1140 while (!list_empty(&fc->io)) {
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08001141 struct fuse_req *req =
1142 list_entry(fc->io.next, struct fuse_req, list);
1143 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
1144
Miklos Szeredif9a28422006-06-25 05:48:53 -07001145 req->aborted = 1;
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001146 req->out.h.error = -ECONNABORTED;
1147 req->state = FUSE_REQ_FINISHED;
1148 list_del_init(&req->list);
1149 wake_up(&req->waitq);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08001150 if (end) {
1151 req->end = NULL;
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08001152 __fuse_get_request(req);
Miklos Szeredid7133112006-04-10 22:54:55 -07001153 spin_unlock(&fc->lock);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08001154 wait_event(req->waitq, !req->locked);
1155 end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +01001156 fuse_put_request(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -07001157 spin_lock(&fc->lock);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08001158 }
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001159 }
1160}
1161
1162/*
1163 * Abort all requests.
1164 *
1165 * Emergency exit in case of a malicious or accidental deadlock, or
1166 * just a hung filesystem.
1167 *
1168 * The same effect is usually achievable through killing the
1169 * filesystem daemon and all users of the filesystem. The exception
1170 * is the combination of an asynchronous request and the tricky
1171 * deadlock (see Documentation/filesystems/fuse.txt).
1172 *
1173 * During the aborting, progression of requests from the pending and
1174 * processing lists onto the io list, and progression of new requests
1175 * onto the pending list is prevented by req->connected being false.
1176 *
1177 * Progression of requests under I/O to the processing list is
Miklos Szeredif9a28422006-06-25 05:48:53 -07001178 * prevented by the req->aborted flag being true for these requests.
1179 * For this reason requests on the io list must be aborted first.
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001180 */
1181void fuse_abort_conn(struct fuse_conn *fc)
1182{
Miklos Szeredid7133112006-04-10 22:54:55 -07001183 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001184 if (fc->connected) {
1185 fc->connected = 0;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -07001186 fc->blocked = 0;
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001187 end_io_requests(fc);
1188 end_requests(fc, &fc->pending);
1189 end_requests(fc, &fc->processing);
1190 wake_up_all(&fc->waitq);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -07001191 wake_up_all(&fc->blocked_waitq);
Jeff Dike385a17b2006-04-10 22:54:52 -07001192 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001193 }
Miklos Szeredid7133112006-04-10 22:54:55 -07001194 spin_unlock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001195}
Tejun Heo08cbf542009-04-14 10:54:53 +09001196EXPORT_SYMBOL_GPL(fuse_abort_conn);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001197
Tejun Heo08cbf542009-04-14 10:54:53 +09001198int fuse_dev_release(struct inode *inode, struct file *file)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001199{
Miklos Szeredi0720b312006-04-10 22:54:55 -07001200 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001201 if (fc) {
Miklos Szeredid7133112006-04-10 22:54:55 -07001202 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -07001203 fc->connected = 0;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001204 end_requests(fc, &fc->pending);
1205 end_requests(fc, &fc->processing);
Miklos Szeredid7133112006-04-10 22:54:55 -07001206 spin_unlock(&fc->lock);
Miklos Szeredibafa9652006-06-25 05:48:51 -07001207 fuse_conn_put(fc);
Jeff Dike385a17b2006-04-10 22:54:52 -07001208 }
Miklos Szeredif543f252006-01-16 22:14:35 -08001209
Miklos Szeredi334f4852005-09-09 13:10:27 -07001210 return 0;
1211}
Tejun Heo08cbf542009-04-14 10:54:53 +09001212EXPORT_SYMBOL_GPL(fuse_dev_release);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001213
Jeff Dike385a17b2006-04-10 22:54:52 -07001214static int fuse_dev_fasync(int fd, struct file *file, int on)
1215{
1216 struct fuse_conn *fc = fuse_get_conn(file);
1217 if (!fc)
Miklos Szeredia87046d2006-04-10 22:54:56 -07001218 return -EPERM;
Jeff Dike385a17b2006-04-10 22:54:52 -07001219
1220 /* No locking - fasync_helper does its own locking */
1221 return fasync_helper(fd, file, on, &fc->fasync);
1222}
1223
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001224const struct file_operations fuse_dev_operations = {
Miklos Szeredi334f4852005-09-09 13:10:27 -07001225 .owner = THIS_MODULE,
1226 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07001227 .read = do_sync_read,
1228 .aio_read = fuse_dev_read,
1229 .write = do_sync_write,
1230 .aio_write = fuse_dev_write,
Miklos Szeredi334f4852005-09-09 13:10:27 -07001231 .poll = fuse_dev_poll,
1232 .release = fuse_dev_release,
Jeff Dike385a17b2006-04-10 22:54:52 -07001233 .fasync = fuse_dev_fasync,
Miklos Szeredi334f4852005-09-09 13:10:27 -07001234};
Tejun Heo08cbf542009-04-14 10:54:53 +09001235EXPORT_SYMBOL_GPL(fuse_dev_operations);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001236
1237static struct miscdevice fuse_miscdevice = {
1238 .minor = FUSE_MINOR,
1239 .name = "fuse",
1240 .fops = &fuse_dev_operations,
1241};
1242
1243int __init fuse_dev_init(void)
1244{
1245 int err = -ENOMEM;
1246 fuse_req_cachep = kmem_cache_create("fuse_request",
1247 sizeof(struct fuse_req),
Paul Mundt20c2df82007-07-20 10:11:58 +09001248 0, 0, NULL);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001249 if (!fuse_req_cachep)
1250 goto out;
1251
1252 err = misc_register(&fuse_miscdevice);
1253 if (err)
1254 goto out_cache_clean;
1255
1256 return 0;
1257
1258 out_cache_clean:
1259 kmem_cache_destroy(fuse_req_cachep);
1260 out:
1261 return err;
1262}
1263
1264void fuse_dev_cleanup(void)
1265{
1266 misc_deregister(&fuse_miscdevice);
1267 kmem_cache_destroy(fuse_req_cachep);
1268}