blob: d4efb6223e2c1e5cf64cc7111acf57e794cb8751 [file] [log] [blame]
Miklos Szeredi334f4852005-09-09 13:10:27 -07001/*
2 FUSE: Filesystem in Userspace
Miklos Szeredid7133112006-04-10 22:54:55 -07003 Copyright (C) 2001-2006 Miklos Szeredi <miklos@szeredi.hu>
Miklos Szeredi334f4852005-09-09 13:10:27 -07004
5 This program can be distributed under the terms of the GNU GPL.
6 See the file COPYING.
7*/
8
9#include "fuse_i.h"
10
11#include <linux/init.h>
12#include <linux/module.h>
13#include <linux/poll.h>
14#include <linux/uio.h>
15#include <linux/miscdevice.h>
16#include <linux/pagemap.h>
17#include <linux/file.h>
18#include <linux/slab.h>
19
20MODULE_ALIAS_MISCDEV(FUSE_MINOR);
21
22static kmem_cache_t *fuse_req_cachep;
23
Miklos Szeredi8bfc0162006-01-16 22:14:28 -080024static struct fuse_conn *fuse_get_conn(struct file *file)
Miklos Szeredi334f4852005-09-09 13:10:27 -070025{
Miklos Szeredi0720b312006-04-10 22:54:55 -070026 /*
27 * Lockless access is OK, because file->private data is set
28 * once during mount and is valid until the file is released.
29 */
30 return file->private_data;
Miklos Szeredi334f4852005-09-09 13:10:27 -070031}
32
Miklos Szeredi8bfc0162006-01-16 22:14:28 -080033static void fuse_request_init(struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -070034{
35 memset(req, 0, sizeof(*req));
36 INIT_LIST_HEAD(&req->list);
37 init_waitqueue_head(&req->waitq);
38 atomic_set(&req->count, 1);
39}
40
41struct fuse_req *fuse_request_alloc(void)
42{
43 struct fuse_req *req = kmem_cache_alloc(fuse_req_cachep, SLAB_KERNEL);
44 if (req)
45 fuse_request_init(req);
46 return req;
47}
48
49void fuse_request_free(struct fuse_req *req)
50{
51 kmem_cache_free(fuse_req_cachep, req);
52}
53
Miklos Szeredi8bfc0162006-01-16 22:14:28 -080054static void block_sigs(sigset_t *oldset)
Miklos Szeredi334f4852005-09-09 13:10:27 -070055{
56 sigset_t mask;
57
58 siginitsetinv(&mask, sigmask(SIGKILL));
59 sigprocmask(SIG_BLOCK, &mask, oldset);
60}
61
Miklos Szeredi8bfc0162006-01-16 22:14:28 -080062static void restore_sigs(sigset_t *oldset)
Miklos Szeredi334f4852005-09-09 13:10:27 -070063{
64 sigprocmask(SIG_SETMASK, oldset, NULL);
65}
66
Miklos Szeredi77e7f252006-02-17 13:52:52 -080067/*
68 * Reset request, so that it can be reused
69 *
70 * The caller must be _very_ careful to make sure, that it is holding
71 * the only reference to req
72 */
Miklos Szeredi334f4852005-09-09 13:10:27 -070073void fuse_reset_request(struct fuse_req *req)
74{
Miklos Szeredi334f4852005-09-09 13:10:27 -070075 BUG_ON(atomic_read(&req->count) != 1);
76 fuse_request_init(req);
Miklos Szeredi334f4852005-09-09 13:10:27 -070077}
78
79static void __fuse_get_request(struct fuse_req *req)
80{
81 atomic_inc(&req->count);
82}
83
84/* Must be called with > 1 refcount */
85static void __fuse_put_request(struct fuse_req *req)
86{
87 BUG_ON(atomic_read(&req->count) < 2);
88 atomic_dec(&req->count);
89}
90
Miklos Szeredice1d5a42006-04-10 22:54:58 -070091struct fuse_req *fuse_get_req(struct fuse_conn *fc)
Miklos Szeredi334f4852005-09-09 13:10:27 -070092{
Miklos Szeredi08a53cd2006-04-10 22:54:59 -070093 struct fuse_req *req;
94 sigset_t oldset;
95 int err;
96
97 block_sigs(&oldset);
98 err = wait_event_interruptible(fc->blocked_waitq, !fc->blocked);
99 restore_sigs(&oldset);
100 if (err)
101 return ERR_PTR(-EINTR);
102
103 req = fuse_request_alloc();
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700104 if (!req)
105 return ERR_PTR(-ENOMEM);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700106
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700107 atomic_inc(&fc->num_waiting);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700108 fuse_request_init(req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700109 req->in.h.uid = current->fsuid;
110 req->in.h.gid = current->fsgid;
111 req->in.h.pid = current->pid;
112 return req;
113}
114
Miklos Szeredi334f4852005-09-09 13:10:27 -0700115void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req)
116{
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800117 if (atomic_dec_and_test(&req->count)) {
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700118 atomic_dec(&fc->num_waiting);
119 fuse_request_free(req);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800120 }
121}
122
Miklos Szeredi73ce8352006-04-11 21:14:26 +0200123void fuse_remove_background(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700124{
Miklos Szeredi73ce8352006-04-11 21:14:26 +0200125 list_del_init(&req->bg_entry);
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700126 if (fc->num_background == FUSE_MAX_BACKGROUND) {
127 fc->blocked = 0;
128 wake_up_all(&fc->blocked_waitq);
129 }
130 fc->num_background--;
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700131}
132
Miklos Szeredi334f4852005-09-09 13:10:27 -0700133/*
134 * This function is called when a request is finished. Either a reply
135 * has arrived or it was interrupted (and not yet sent) or some error
Miklos Szeredif43b1552006-01-16 22:14:26 -0800136 * occurred during communication with userspace, or the device file
137 * was closed. In case of a background request the reference to the
138 * stored objects are released. The requester thread is woken up (if
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800139 * still waiting), the 'end' callback is called if given, else the
140 * reference to the request is released
Miklos Szeredi334f4852005-09-09 13:10:27 -0700141 *
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800142 * Releasing extra reference for foreground requests must be done
143 * within the same locked region as setting state to finished. This
144 * is because fuse_reset_request() may be called after request is
145 * finished and it must be the sole possessor. If request is
146 * interrupted and put in the background, it will return with an error
147 * and hence never be reset and reused.
148 *
Miklos Szeredid7133112006-04-10 22:54:55 -0700149 * Called with fc->lock, unlocks it
Miklos Szeredi334f4852005-09-09 13:10:27 -0700150 */
151static void request_end(struct fuse_conn *fc, struct fuse_req *req)
152{
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800153 list_del(&req->list);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800154 req->state = FUSE_REQ_FINISHED;
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800155 if (!req->background) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700156 spin_unlock(&fc->lock);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700157 wake_up(&req->waitq);
158 fuse_put_request(fc, req);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800159 } else {
Miklos Szeredi73ce8352006-04-11 21:14:26 +0200160 struct inode *inode = req->inode;
161 struct inode *inode2 = req->inode2;
162 struct file *file = req->file;
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800163 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
164 req->end = NULL;
Miklos Szeredi73ce8352006-04-11 21:14:26 +0200165 req->inode = NULL;
166 req->inode2 = NULL;
167 req->file = NULL;
168 if (!list_empty(&req->bg_entry))
169 fuse_remove_background(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700170 spin_unlock(&fc->lock);
Miklos Szeredi73ce8352006-04-11 21:14:26 +0200171
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800172 if (end)
173 end(fc, req);
174 else
175 fuse_put_request(fc, req);
Miklos Szeredi73ce8352006-04-11 21:14:26 +0200176
177 if (file)
178 fput(file);
179 iput(inode);
180 iput(inode2);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700181 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700182}
183
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700184/*
185 * Unfortunately request interruption not just solves the deadlock
186 * problem, it causes problems too. These stem from the fact, that an
187 * interrupted request is continued to be processed in userspace,
188 * while all the locks and object references (inode and file) held
189 * during the operation are released.
190 *
191 * To release the locks is exactly why there's a need to interrupt the
192 * request, so there's not a lot that can be done about this, except
193 * introduce additional locking in userspace.
194 *
195 * More important is to keep inode and file references until userspace
196 * has replied, otherwise FORGET and RELEASE could be sent while the
197 * inode/file is still used by the filesystem.
198 *
199 * For this reason the concept of "background" request is introduced.
200 * An interrupted request is backgrounded if it has been already sent
201 * to userspace. Backgrounding involves getting an extra reference to
202 * inode(s) or file used in the request, and adding the request to
203 * fc->background list. When a reply is received for a background
204 * request, the object references are released, and the request is
205 * removed from the list. If the filesystem is unmounted while there
206 * are still background requests, the list is walked and references
207 * are released as if a reply was received.
208 *
209 * There's one more use for a background request. The RELEASE message is
210 * always sent as background, since it doesn't return an error or
211 * data.
212 */
213static void background_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700214{
Miklos Szeredi334f4852005-09-09 13:10:27 -0700215 req->background = 1;
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700216 list_add(&req->bg_entry, &fc->background);
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700217 fc->num_background++;
218 if (fc->num_background == FUSE_MAX_BACKGROUND)
219 fc->blocked = 1;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700220 if (req->inode)
221 req->inode = igrab(req->inode);
222 if (req->inode2)
223 req->inode2 = igrab(req->inode2);
224 if (req->file)
225 get_file(req->file);
226}
227
Miklos Szeredid7133112006-04-10 22:54:55 -0700228/* Called with fc->lock held. Releases, and then reacquires it. */
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700229static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700230{
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700231 sigset_t oldset;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700232
Miklos Szeredid7133112006-04-10 22:54:55 -0700233 spin_unlock(&fc->lock);
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700234 block_sigs(&oldset);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800235 wait_event_interruptible(req->waitq, req->state == FUSE_REQ_FINISHED);
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700236 restore_sigs(&oldset);
Miklos Szeredid7133112006-04-10 22:54:55 -0700237 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800238 if (req->state == FUSE_REQ_FINISHED && !req->interrupted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700239 return;
240
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800241 if (!req->interrupted) {
242 req->out.h.error = -EINTR;
243 req->interrupted = 1;
244 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700245 if (req->locked) {
246 /* This is uninterruptible sleep, because data is
247 being copied to/from the buffers of req. During
248 locked state, there mustn't be any filesystem
249 operation (e.g. page fault), since that could lead
250 to deadlock */
Miklos Szeredid7133112006-04-10 22:54:55 -0700251 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700252 wait_event(req->waitq, !req->locked);
Miklos Szeredid7133112006-04-10 22:54:55 -0700253 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700254 }
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800255 if (req->state == FUSE_REQ_PENDING) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700256 list_del(&req->list);
257 __fuse_put_request(req);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800258 } else if (req->state == FUSE_REQ_SENT)
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700259 background_request(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700260}
261
262static unsigned len_args(unsigned numargs, struct fuse_arg *args)
263{
264 unsigned nbytes = 0;
265 unsigned i;
266
267 for (i = 0; i < numargs; i++)
268 nbytes += args[i].size;
269
270 return nbytes;
271}
272
273static void queue_request(struct fuse_conn *fc, struct fuse_req *req)
274{
275 fc->reqctr++;
276 /* zero is special */
277 if (fc->reqctr == 0)
278 fc->reqctr = 1;
279 req->in.h.unique = fc->reqctr;
280 req->in.h.len = sizeof(struct fuse_in_header) +
281 len_args(req->in.numargs, (struct fuse_arg *) req->in.args);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700282 list_add_tail(&req->list, &fc->pending);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800283 req->state = FUSE_REQ_PENDING;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700284 wake_up(&fc->waitq);
Jeff Dike385a17b2006-04-10 22:54:52 -0700285 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700286}
287
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700288/*
289 * This can only be interrupted by a SIGKILL
290 */
291void request_send(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700292{
293 req->isreply = 1;
Miklos Szeredid7133112006-04-10 22:54:55 -0700294 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700295 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700296 req->out.h.error = -ENOTCONN;
297 else if (fc->conn_error)
298 req->out.h.error = -ECONNREFUSED;
299 else {
300 queue_request(fc, req);
301 /* acquire extra reference, since request is still needed
302 after request_end() */
303 __fuse_get_request(req);
304
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700305 request_wait_answer(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700306 }
Miklos Szeredid7133112006-04-10 22:54:55 -0700307 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700308}
309
Miklos Szeredi334f4852005-09-09 13:10:27 -0700310static void request_send_nowait(struct fuse_conn *fc, struct fuse_req *req)
311{
Miklos Szeredid7133112006-04-10 22:54:55 -0700312 spin_lock(&fc->lock);
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700313 background_request(fc, req);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700314 if (fc->connected) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700315 queue_request(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700316 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700317 } else {
318 req->out.h.error = -ENOTCONN;
319 request_end(fc, req);
320 }
321}
322
323void request_send_noreply(struct fuse_conn *fc, struct fuse_req *req)
324{
325 req->isreply = 0;
326 request_send_nowait(fc, req);
327}
328
329void request_send_background(struct fuse_conn *fc, struct fuse_req *req)
330{
331 req->isreply = 1;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700332 request_send_nowait(fc, req);
333}
334
Miklos Szeredi334f4852005-09-09 13:10:27 -0700335/*
336 * Lock the request. Up to the next unlock_request() there mustn't be
337 * anything that could cause a page-fault. If the request was already
338 * interrupted bail out.
339 */
Miklos Szeredid7133112006-04-10 22:54:55 -0700340static int lock_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700341{
342 int err = 0;
343 if (req) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700344 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700345 if (req->interrupted)
346 err = -ENOENT;
347 else
348 req->locked = 1;
Miklos Szeredid7133112006-04-10 22:54:55 -0700349 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700350 }
351 return err;
352}
353
354/*
355 * Unlock request. If it was interrupted during being locked, the
356 * requester thread is currently waiting for it to be unlocked, so
357 * wake it up.
358 */
Miklos Szeredid7133112006-04-10 22:54:55 -0700359static void unlock_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700360{
361 if (req) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700362 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700363 req->locked = 0;
364 if (req->interrupted)
365 wake_up(&req->waitq);
Miklos Szeredid7133112006-04-10 22:54:55 -0700366 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700367 }
368}
369
370struct fuse_copy_state {
Miklos Szeredid7133112006-04-10 22:54:55 -0700371 struct fuse_conn *fc;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700372 int write;
373 struct fuse_req *req;
374 const struct iovec *iov;
375 unsigned long nr_segs;
376 unsigned long seglen;
377 unsigned long addr;
378 struct page *pg;
379 void *mapaddr;
380 void *buf;
381 unsigned len;
382};
383
Miklos Szeredid7133112006-04-10 22:54:55 -0700384static void fuse_copy_init(struct fuse_copy_state *cs, struct fuse_conn *fc,
385 int write, struct fuse_req *req,
386 const struct iovec *iov, unsigned long nr_segs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700387{
388 memset(cs, 0, sizeof(*cs));
Miklos Szeredid7133112006-04-10 22:54:55 -0700389 cs->fc = fc;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700390 cs->write = write;
391 cs->req = req;
392 cs->iov = iov;
393 cs->nr_segs = nr_segs;
394}
395
396/* Unmap and put previous page of userspace buffer */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800397static void fuse_copy_finish(struct fuse_copy_state *cs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700398{
399 if (cs->mapaddr) {
400 kunmap_atomic(cs->mapaddr, KM_USER0);
401 if (cs->write) {
402 flush_dcache_page(cs->pg);
403 set_page_dirty_lock(cs->pg);
404 }
405 put_page(cs->pg);
406 cs->mapaddr = NULL;
407 }
408}
409
410/*
411 * Get another pagefull of userspace buffer, and map it to kernel
412 * address space, and lock request
413 */
414static int fuse_copy_fill(struct fuse_copy_state *cs)
415{
416 unsigned long offset;
417 int err;
418
Miklos Szeredid7133112006-04-10 22:54:55 -0700419 unlock_request(cs->fc, cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700420 fuse_copy_finish(cs);
421 if (!cs->seglen) {
422 BUG_ON(!cs->nr_segs);
423 cs->seglen = cs->iov[0].iov_len;
424 cs->addr = (unsigned long) cs->iov[0].iov_base;
425 cs->iov ++;
426 cs->nr_segs --;
427 }
428 down_read(&current->mm->mmap_sem);
429 err = get_user_pages(current, current->mm, cs->addr, 1, cs->write, 0,
430 &cs->pg, NULL);
431 up_read(&current->mm->mmap_sem);
432 if (err < 0)
433 return err;
434 BUG_ON(err != 1);
435 offset = cs->addr % PAGE_SIZE;
436 cs->mapaddr = kmap_atomic(cs->pg, KM_USER0);
437 cs->buf = cs->mapaddr + offset;
438 cs->len = min(PAGE_SIZE - offset, cs->seglen);
439 cs->seglen -= cs->len;
440 cs->addr += cs->len;
441
Miklos Szeredid7133112006-04-10 22:54:55 -0700442 return lock_request(cs->fc, cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700443}
444
445/* Do as much copy to/from userspace buffer as we can */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800446static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700447{
448 unsigned ncpy = min(*size, cs->len);
449 if (val) {
450 if (cs->write)
451 memcpy(cs->buf, *val, ncpy);
452 else
453 memcpy(*val, cs->buf, ncpy);
454 *val += ncpy;
455 }
456 *size -= ncpy;
457 cs->len -= ncpy;
458 cs->buf += ncpy;
459 return ncpy;
460}
461
462/*
463 * Copy a page in the request to/from the userspace buffer. Must be
464 * done atomically
465 */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800466static int fuse_copy_page(struct fuse_copy_state *cs, struct page *page,
467 unsigned offset, unsigned count, int zeroing)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700468{
469 if (page && zeroing && count < PAGE_SIZE) {
470 void *mapaddr = kmap_atomic(page, KM_USER1);
471 memset(mapaddr, 0, PAGE_SIZE);
472 kunmap_atomic(mapaddr, KM_USER1);
473 }
474 while (count) {
475 int err;
476 if (!cs->len && (err = fuse_copy_fill(cs)))
477 return err;
478 if (page) {
479 void *mapaddr = kmap_atomic(page, KM_USER1);
480 void *buf = mapaddr + offset;
481 offset += fuse_copy_do(cs, &buf, &count);
482 kunmap_atomic(mapaddr, KM_USER1);
483 } else
484 offset += fuse_copy_do(cs, NULL, &count);
485 }
486 if (page && !cs->write)
487 flush_dcache_page(page);
488 return 0;
489}
490
491/* Copy pages in the request to/from userspace buffer */
492static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
493 int zeroing)
494{
495 unsigned i;
496 struct fuse_req *req = cs->req;
497 unsigned offset = req->page_offset;
498 unsigned count = min(nbytes, (unsigned) PAGE_SIZE - offset);
499
500 for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
501 struct page *page = req->pages[i];
502 int err = fuse_copy_page(cs, page, offset, count, zeroing);
503 if (err)
504 return err;
505
506 nbytes -= count;
507 count = min(nbytes, (unsigned) PAGE_SIZE);
508 offset = 0;
509 }
510 return 0;
511}
512
513/* Copy a single argument in the request to/from userspace buffer */
514static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
515{
516 while (size) {
517 int err;
518 if (!cs->len && (err = fuse_copy_fill(cs)))
519 return err;
520 fuse_copy_do(cs, &val, &size);
521 }
522 return 0;
523}
524
525/* Copy request arguments to/from userspace buffer */
526static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
527 unsigned argpages, struct fuse_arg *args,
528 int zeroing)
529{
530 int err = 0;
531 unsigned i;
532
533 for (i = 0; !err && i < numargs; i++) {
534 struct fuse_arg *arg = &args[i];
535 if (i == numargs - 1 && argpages)
536 err = fuse_copy_pages(cs, arg->size, zeroing);
537 else
538 err = fuse_copy_one(cs, arg->value, arg->size);
539 }
540 return err;
541}
542
543/* Wait until a request is available on the pending list */
544static void request_wait(struct fuse_conn *fc)
545{
546 DECLARE_WAITQUEUE(wait, current);
547
548 add_wait_queue_exclusive(&fc->waitq, &wait);
Miklos Szeredi9ba7cbb2006-01-16 22:14:34 -0800549 while (fc->connected && list_empty(&fc->pending)) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700550 set_current_state(TASK_INTERRUPTIBLE);
551 if (signal_pending(current))
552 break;
553
Miklos Szeredid7133112006-04-10 22:54:55 -0700554 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700555 schedule();
Miklos Szeredid7133112006-04-10 22:54:55 -0700556 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700557 }
558 set_current_state(TASK_RUNNING);
559 remove_wait_queue(&fc->waitq, &wait);
560}
561
562/*
563 * Read a single request into the userspace filesystem's buffer. This
564 * function waits until a request is available, then removes it from
565 * the pending list and copies request data to userspace buffer. If
566 * no reply is needed (FORGET) or request has been interrupted or
567 * there was an error during the copying then it's finished by calling
568 * request_end(). Otherwise add it to the processing list, and set
569 * the 'sent' flag.
570 */
571static ssize_t fuse_dev_readv(struct file *file, const struct iovec *iov,
572 unsigned long nr_segs, loff_t *off)
573{
574 int err;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700575 struct fuse_req *req;
576 struct fuse_in *in;
577 struct fuse_copy_state cs;
578 unsigned reqsize;
Miklos Szeredi0720b312006-04-10 22:54:55 -0700579 struct fuse_conn *fc = fuse_get_conn(file);
580 if (!fc)
581 return -EPERM;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700582
Miklos Szeredi1d3d7522006-01-06 00:19:40 -0800583 restart:
Miklos Szeredid7133112006-04-10 22:54:55 -0700584 spin_lock(&fc->lock);
Jeff Dikee5ac1d12006-04-10 22:54:53 -0700585 err = -EAGAIN;
586 if ((file->f_flags & O_NONBLOCK) && fc->connected &&
587 list_empty(&fc->pending))
588 goto err_unlock;
589
Miklos Szeredi334f4852005-09-09 13:10:27 -0700590 request_wait(fc);
591 err = -ENODEV;
Miklos Szeredi9ba7cbb2006-01-16 22:14:34 -0800592 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700593 goto err_unlock;
594 err = -ERESTARTSYS;
595 if (list_empty(&fc->pending))
596 goto err_unlock;
597
598 req = list_entry(fc->pending.next, struct fuse_req, list);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800599 req->state = FUSE_REQ_READING;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800600 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700601
602 in = &req->in;
Miklos Szeredi1d3d7522006-01-06 00:19:40 -0800603 reqsize = in->h.len;
604 /* If request is too large, reply with an error and restart the read */
605 if (iov_length(iov, nr_segs) < reqsize) {
606 req->out.h.error = -EIO;
607 /* SETXATTR is special, since it may contain too large data */
608 if (in->h.opcode == FUSE_SETXATTR)
609 req->out.h.error = -E2BIG;
610 request_end(fc, req);
611 goto restart;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700612 }
Miklos Szeredid7133112006-04-10 22:54:55 -0700613 spin_unlock(&fc->lock);
614 fuse_copy_init(&cs, fc, 1, req, iov, nr_segs);
Miklos Szeredi1d3d7522006-01-06 00:19:40 -0800615 err = fuse_copy_one(&cs, &in->h, sizeof(in->h));
616 if (!err)
617 err = fuse_copy_args(&cs, in->numargs, in->argpages,
618 (struct fuse_arg *) in->args, 0);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700619 fuse_copy_finish(&cs);
Miklos Szeredid7133112006-04-10 22:54:55 -0700620 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700621 req->locked = 0;
622 if (!err && req->interrupted)
623 err = -ENOENT;
624 if (err) {
625 if (!req->interrupted)
626 req->out.h.error = -EIO;
627 request_end(fc, req);
628 return err;
629 }
630 if (!req->isreply)
631 request_end(fc, req);
632 else {
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800633 req->state = FUSE_REQ_SENT;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800634 list_move_tail(&req->list, &fc->processing);
Miklos Szeredid7133112006-04-10 22:54:55 -0700635 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700636 }
637 return reqsize;
638
639 err_unlock:
Miklos Szeredid7133112006-04-10 22:54:55 -0700640 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700641 return err;
642}
643
644static ssize_t fuse_dev_read(struct file *file, char __user *buf,
645 size_t nbytes, loff_t *off)
646{
647 struct iovec iov;
648 iov.iov_len = nbytes;
649 iov.iov_base = buf;
650 return fuse_dev_readv(file, &iov, 1, off);
651}
652
653/* Look up request on processing list by unique ID */
654static struct fuse_req *request_find(struct fuse_conn *fc, u64 unique)
655{
656 struct list_head *entry;
657
658 list_for_each(entry, &fc->processing) {
659 struct fuse_req *req;
660 req = list_entry(entry, struct fuse_req, list);
661 if (req->in.h.unique == unique)
662 return req;
663 }
664 return NULL;
665}
666
667static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
668 unsigned nbytes)
669{
670 unsigned reqsize = sizeof(struct fuse_out_header);
671
672 if (out->h.error)
673 return nbytes != reqsize ? -EINVAL : 0;
674
675 reqsize += len_args(out->numargs, out->args);
676
677 if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
678 return -EINVAL;
679 else if (reqsize > nbytes) {
680 struct fuse_arg *lastarg = &out->args[out->numargs-1];
681 unsigned diffsize = reqsize - nbytes;
682 if (diffsize > lastarg->size)
683 return -EINVAL;
684 lastarg->size -= diffsize;
685 }
686 return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
687 out->page_zeroing);
688}
689
690/*
691 * Write a single reply to a request. First the header is copied from
692 * the write buffer. The request is then searched on the processing
693 * list by the unique ID found in the header. If found, then remove
694 * it from the list and copy the rest of the buffer to the request.
695 * The request is finished by calling request_end()
696 */
697static ssize_t fuse_dev_writev(struct file *file, const struct iovec *iov,
698 unsigned long nr_segs, loff_t *off)
699{
700 int err;
701 unsigned nbytes = iov_length(iov, nr_segs);
702 struct fuse_req *req;
703 struct fuse_out_header oh;
704 struct fuse_copy_state cs;
705 struct fuse_conn *fc = fuse_get_conn(file);
706 if (!fc)
Miklos Szeredia87046d2006-04-10 22:54:56 -0700707 return -EPERM;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700708
Miklos Szeredid7133112006-04-10 22:54:55 -0700709 fuse_copy_init(&cs, fc, 0, NULL, iov, nr_segs);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700710 if (nbytes < sizeof(struct fuse_out_header))
711 return -EINVAL;
712
713 err = fuse_copy_one(&cs, &oh, sizeof(oh));
714 if (err)
715 goto err_finish;
716 err = -EINVAL;
717 if (!oh.unique || oh.error <= -1000 || oh.error > 0 ||
718 oh.len != nbytes)
719 goto err_finish;
720
Miklos Szeredid7133112006-04-10 22:54:55 -0700721 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800722 err = -ENOENT;
723 if (!fc->connected)
724 goto err_unlock;
725
Miklos Szeredi334f4852005-09-09 13:10:27 -0700726 req = request_find(fc, oh.unique);
727 err = -EINVAL;
728 if (!req)
729 goto err_unlock;
730
Miklos Szeredi334f4852005-09-09 13:10:27 -0700731 if (req->interrupted) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700732 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700733 fuse_copy_finish(&cs);
Miklos Szeredid7133112006-04-10 22:54:55 -0700734 spin_lock(&fc->lock);
Miklos Szeredi222f1d62006-01-16 22:14:25 -0800735 request_end(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700736 return -ENOENT;
737 }
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800738 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700739 req->out.h = oh;
740 req->locked = 1;
741 cs.req = req;
Miklos Szeredid7133112006-04-10 22:54:55 -0700742 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700743
744 err = copy_out_args(&cs, &req->out, nbytes);
745 fuse_copy_finish(&cs);
746
Miklos Szeredid7133112006-04-10 22:54:55 -0700747 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700748 req->locked = 0;
749 if (!err) {
750 if (req->interrupted)
751 err = -ENOENT;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700752 } else if (!req->interrupted)
753 req->out.h.error = -EIO;
754 request_end(fc, req);
755
756 return err ? err : nbytes;
757
758 err_unlock:
Miklos Szeredid7133112006-04-10 22:54:55 -0700759 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700760 err_finish:
761 fuse_copy_finish(&cs);
762 return err;
763}
764
765static ssize_t fuse_dev_write(struct file *file, const char __user *buf,
766 size_t nbytes, loff_t *off)
767{
768 struct iovec iov;
769 iov.iov_len = nbytes;
770 iov.iov_base = (char __user *) buf;
771 return fuse_dev_writev(file, &iov, 1, off);
772}
773
774static unsigned fuse_dev_poll(struct file *file, poll_table *wait)
775{
Miklos Szeredi334f4852005-09-09 13:10:27 -0700776 unsigned mask = POLLOUT | POLLWRNORM;
Miklos Szeredi7025d9a2006-04-10 22:54:50 -0700777 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700778 if (!fc)
Miklos Szeredi7025d9a2006-04-10 22:54:50 -0700779 return POLLERR;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700780
781 poll_wait(file, &fc->waitq, wait);
782
Miklos Szeredid7133112006-04-10 22:54:55 -0700783 spin_lock(&fc->lock);
Miklos Szeredi7025d9a2006-04-10 22:54:50 -0700784 if (!fc->connected)
785 mask = POLLERR;
786 else if (!list_empty(&fc->pending))
787 mask |= POLLIN | POLLRDNORM;
Miklos Szeredid7133112006-04-10 22:54:55 -0700788 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700789
790 return mask;
791}
792
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800793/*
794 * Abort all requests on the given list (pending or processing)
795 *
Miklos Szeredid7133112006-04-10 22:54:55 -0700796 * This function releases and reacquires fc->lock
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800797 */
Miklos Szeredi334f4852005-09-09 13:10:27 -0700798static void end_requests(struct fuse_conn *fc, struct list_head *head)
799{
800 while (!list_empty(head)) {
801 struct fuse_req *req;
802 req = list_entry(head->next, struct fuse_req, list);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700803 req->out.h.error = -ECONNABORTED;
804 request_end(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700805 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700806 }
807}
808
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800809/*
810 * Abort requests under I/O
811 *
812 * The requests are set to interrupted and finished, and the request
813 * waiter is woken up. This will make request_wait_answer() wait
814 * until the request is unlocked and then return.
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800815 *
816 * If the request is asynchronous, then the end function needs to be
817 * called after waiting for the request to be unlocked (if it was
818 * locked).
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800819 */
820static void end_io_requests(struct fuse_conn *fc)
821{
822 while (!list_empty(&fc->io)) {
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800823 struct fuse_req *req =
824 list_entry(fc->io.next, struct fuse_req, list);
825 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
826
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800827 req->interrupted = 1;
828 req->out.h.error = -ECONNABORTED;
829 req->state = FUSE_REQ_FINISHED;
830 list_del_init(&req->list);
831 wake_up(&req->waitq);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800832 if (end) {
833 req->end = NULL;
834 /* The end function will consume this reference */
835 __fuse_get_request(req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700836 spin_unlock(&fc->lock);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800837 wait_event(req->waitq, !req->locked);
838 end(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700839 spin_lock(&fc->lock);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800840 }
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800841 }
842}
843
844/*
845 * Abort all requests.
846 *
847 * Emergency exit in case of a malicious or accidental deadlock, or
848 * just a hung filesystem.
849 *
850 * The same effect is usually achievable through killing the
851 * filesystem daemon and all users of the filesystem. The exception
852 * is the combination of an asynchronous request and the tricky
853 * deadlock (see Documentation/filesystems/fuse.txt).
854 *
855 * During the aborting, progression of requests from the pending and
856 * processing lists onto the io list, and progression of new requests
857 * onto the pending list is prevented by req->connected being false.
858 *
859 * Progression of requests under I/O to the processing list is
860 * prevented by the req->interrupted flag being true for these
861 * requests. For this reason requests on the io list must be aborted
862 * first.
863 */
864void fuse_abort_conn(struct fuse_conn *fc)
865{
Miklos Szeredid7133112006-04-10 22:54:55 -0700866 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800867 if (fc->connected) {
868 fc->connected = 0;
869 end_io_requests(fc);
870 end_requests(fc, &fc->pending);
871 end_requests(fc, &fc->processing);
872 wake_up_all(&fc->waitq);
Jeff Dike385a17b2006-04-10 22:54:52 -0700873 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800874 }
Miklos Szeredid7133112006-04-10 22:54:55 -0700875 spin_unlock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800876}
877
Miklos Szeredi334f4852005-09-09 13:10:27 -0700878static int fuse_dev_release(struct inode *inode, struct file *file)
879{
Miklos Szeredi0720b312006-04-10 22:54:55 -0700880 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700881 if (fc) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700882 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700883 fc->connected = 0;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700884 end_requests(fc, &fc->pending);
885 end_requests(fc, &fc->processing);
Miklos Szeredid7133112006-04-10 22:54:55 -0700886 spin_unlock(&fc->lock);
Jeff Dike385a17b2006-04-10 22:54:52 -0700887 fasync_helper(-1, file, 0, &fc->fasync);
Miklos Szeredif543f252006-01-16 22:14:35 -0800888 kobject_put(&fc->kobj);
Jeff Dike385a17b2006-04-10 22:54:52 -0700889 }
Miklos Szeredif543f252006-01-16 22:14:35 -0800890
Miklos Szeredi334f4852005-09-09 13:10:27 -0700891 return 0;
892}
893
Jeff Dike385a17b2006-04-10 22:54:52 -0700894static int fuse_dev_fasync(int fd, struct file *file, int on)
895{
896 struct fuse_conn *fc = fuse_get_conn(file);
897 if (!fc)
Miklos Szeredia87046d2006-04-10 22:54:56 -0700898 return -EPERM;
Jeff Dike385a17b2006-04-10 22:54:52 -0700899
900 /* No locking - fasync_helper does its own locking */
901 return fasync_helper(fd, file, on, &fc->fasync);
902}
903
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800904const struct file_operations fuse_dev_operations = {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700905 .owner = THIS_MODULE,
906 .llseek = no_llseek,
907 .read = fuse_dev_read,
908 .readv = fuse_dev_readv,
909 .write = fuse_dev_write,
910 .writev = fuse_dev_writev,
911 .poll = fuse_dev_poll,
912 .release = fuse_dev_release,
Jeff Dike385a17b2006-04-10 22:54:52 -0700913 .fasync = fuse_dev_fasync,
Miklos Szeredi334f4852005-09-09 13:10:27 -0700914};
915
916static struct miscdevice fuse_miscdevice = {
917 .minor = FUSE_MINOR,
918 .name = "fuse",
919 .fops = &fuse_dev_operations,
920};
921
922int __init fuse_dev_init(void)
923{
924 int err = -ENOMEM;
925 fuse_req_cachep = kmem_cache_create("fuse_request",
926 sizeof(struct fuse_req),
927 0, 0, NULL, NULL);
928 if (!fuse_req_cachep)
929 goto out;
930
931 err = misc_register(&fuse_miscdevice);
932 if (err)
933 goto out_cache_clean;
934
935 return 0;
936
937 out_cache_clean:
938 kmem_cache_destroy(fuse_req_cachep);
939 out:
940 return err;
941}
942
943void fuse_dev_cleanup(void)
944{
945 misc_deregister(&fuse_miscdevice);
946 kmem_cache_destroy(fuse_req_cachep);
947}