blob: 4dc104c0e95d0d7db22c5f8703eb9f7922583537 [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 Szeredice1d5a42006-04-10 22:54:58 -070093 struct fuse_req *req = fuse_request_alloc();
94 if (!req)
95 return ERR_PTR(-ENOMEM);
Miklos Szeredi334f4852005-09-09 13:10:27 -070096
Miklos Szeredice1d5a42006-04-10 22:54:58 -070097 atomic_inc(&fc->num_waiting);
Miklos Szeredi334f4852005-09-09 13:10:27 -070098 fuse_request_init(req);
Miklos Szeredi334f4852005-09-09 13:10:27 -070099 req->in.h.uid = current->fsuid;
100 req->in.h.gid = current->fsgid;
101 req->in.h.pid = current->pid;
102 return req;
103}
104
Miklos Szeredi334f4852005-09-09 13:10:27 -0700105void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req)
106{
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800107 if (atomic_dec_and_test(&req->count)) {
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700108 atomic_dec(&fc->num_waiting);
109 fuse_request_free(req);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800110 }
111}
112
Miklos Szeredid7133112006-04-10 22:54:55 -0700113void fuse_release_background(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700114{
115 iput(req->inode);
116 iput(req->inode2);
117 if (req->file)
118 fput(req->file);
Miklos Szeredid7133112006-04-10 22:54:55 -0700119 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700120 list_del(&req->bg_entry);
Miklos Szeredid7133112006-04-10 22:54:55 -0700121 spin_unlock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700122}
123
Miklos Szeredi334f4852005-09-09 13:10:27 -0700124/*
125 * This function is called when a request is finished. Either a reply
126 * has arrived or it was interrupted (and not yet sent) or some error
Miklos Szeredif43b1552006-01-16 22:14:26 -0800127 * occurred during communication with userspace, or the device file
128 * was closed. In case of a background request the reference to the
129 * stored objects are released. The requester thread is woken up (if
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800130 * still waiting), the 'end' callback is called if given, else the
131 * reference to the request is released
Miklos Szeredi334f4852005-09-09 13:10:27 -0700132 *
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800133 * Releasing extra reference for foreground requests must be done
134 * within the same locked region as setting state to finished. This
135 * is because fuse_reset_request() may be called after request is
136 * finished and it must be the sole possessor. If request is
137 * interrupted and put in the background, it will return with an error
138 * and hence never be reset and reused.
139 *
Miklos Szeredid7133112006-04-10 22:54:55 -0700140 * Called with fc->lock, unlocks it
Miklos Szeredi334f4852005-09-09 13:10:27 -0700141 */
142static void request_end(struct fuse_conn *fc, struct fuse_req *req)
143{
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800144 list_del(&req->list);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800145 req->state = FUSE_REQ_FINISHED;
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800146 if (!req->background) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700147 spin_unlock(&fc->lock);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700148 wake_up(&req->waitq);
149 fuse_put_request(fc, req);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800150 } else {
151 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
152 req->end = NULL;
Miklos Szeredid7133112006-04-10 22:54:55 -0700153 spin_unlock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700154 down_read(&fc->sbput_sem);
155 if (fc->mounted)
Miklos Szeredid7133112006-04-10 22:54:55 -0700156 fuse_release_background(fc, req);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700157 up_read(&fc->sbput_sem);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800158 if (end)
159 end(fc, req);
160 else
161 fuse_put_request(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700162 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700163}
164
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700165/*
166 * Unfortunately request interruption not just solves the deadlock
167 * problem, it causes problems too. These stem from the fact, that an
168 * interrupted request is continued to be processed in userspace,
169 * while all the locks and object references (inode and file) held
170 * during the operation are released.
171 *
172 * To release the locks is exactly why there's a need to interrupt the
173 * request, so there's not a lot that can be done about this, except
174 * introduce additional locking in userspace.
175 *
176 * More important is to keep inode and file references until userspace
177 * has replied, otherwise FORGET and RELEASE could be sent while the
178 * inode/file is still used by the filesystem.
179 *
180 * For this reason the concept of "background" request is introduced.
181 * An interrupted request is backgrounded if it has been already sent
182 * to userspace. Backgrounding involves getting an extra reference to
183 * inode(s) or file used in the request, and adding the request to
184 * fc->background list. When a reply is received for a background
185 * request, the object references are released, and the request is
186 * removed from the list. If the filesystem is unmounted while there
187 * are still background requests, the list is walked and references
188 * are released as if a reply was received.
189 *
190 * There's one more use for a background request. The RELEASE message is
191 * always sent as background, since it doesn't return an error or
192 * data.
193 */
194static void background_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700195{
Miklos Szeredi334f4852005-09-09 13:10:27 -0700196 req->background = 1;
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700197 list_add(&req->bg_entry, &fc->background);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700198 if (req->inode)
199 req->inode = igrab(req->inode);
200 if (req->inode2)
201 req->inode2 = igrab(req->inode2);
202 if (req->file)
203 get_file(req->file);
204}
205
Miklos Szeredid7133112006-04-10 22:54:55 -0700206/* Called with fc->lock held. Releases, and then reacquires it. */
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700207static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700208{
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700209 sigset_t oldset;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700210
Miklos Szeredid7133112006-04-10 22:54:55 -0700211 spin_unlock(&fc->lock);
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700212 block_sigs(&oldset);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800213 wait_event_interruptible(req->waitq, req->state == FUSE_REQ_FINISHED);
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700214 restore_sigs(&oldset);
Miklos Szeredid7133112006-04-10 22:54:55 -0700215 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800216 if (req->state == FUSE_REQ_FINISHED && !req->interrupted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700217 return;
218
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800219 if (!req->interrupted) {
220 req->out.h.error = -EINTR;
221 req->interrupted = 1;
222 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700223 if (req->locked) {
224 /* This is uninterruptible sleep, because data is
225 being copied to/from the buffers of req. During
226 locked state, there mustn't be any filesystem
227 operation (e.g. page fault), since that could lead
228 to deadlock */
Miklos Szeredid7133112006-04-10 22:54:55 -0700229 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700230 wait_event(req->waitq, !req->locked);
Miklos Szeredid7133112006-04-10 22:54:55 -0700231 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700232 }
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800233 if (req->state == FUSE_REQ_PENDING) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700234 list_del(&req->list);
235 __fuse_put_request(req);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800236 } else if (req->state == FUSE_REQ_SENT)
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700237 background_request(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700238}
239
240static unsigned len_args(unsigned numargs, struct fuse_arg *args)
241{
242 unsigned nbytes = 0;
243 unsigned i;
244
245 for (i = 0; i < numargs; i++)
246 nbytes += args[i].size;
247
248 return nbytes;
249}
250
251static void queue_request(struct fuse_conn *fc, struct fuse_req *req)
252{
253 fc->reqctr++;
254 /* zero is special */
255 if (fc->reqctr == 0)
256 fc->reqctr = 1;
257 req->in.h.unique = fc->reqctr;
258 req->in.h.len = sizeof(struct fuse_in_header) +
259 len_args(req->in.numargs, (struct fuse_arg *) req->in.args);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700260 list_add_tail(&req->list, &fc->pending);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800261 req->state = FUSE_REQ_PENDING;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700262 wake_up(&fc->waitq);
Jeff Dike385a17b2006-04-10 22:54:52 -0700263 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700264}
265
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700266/*
267 * This can only be interrupted by a SIGKILL
268 */
269void request_send(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700270{
271 req->isreply = 1;
Miklos Szeredid7133112006-04-10 22:54:55 -0700272 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700273 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700274 req->out.h.error = -ENOTCONN;
275 else if (fc->conn_error)
276 req->out.h.error = -ECONNREFUSED;
277 else {
278 queue_request(fc, req);
279 /* acquire extra reference, since request is still needed
280 after request_end() */
281 __fuse_get_request(req);
282
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700283 request_wait_answer(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700284 }
Miklos Szeredid7133112006-04-10 22:54:55 -0700285 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700286}
287
Miklos Szeredi334f4852005-09-09 13:10:27 -0700288static void request_send_nowait(struct fuse_conn *fc, struct fuse_req *req)
289{
Miklos Szeredid7133112006-04-10 22:54:55 -0700290 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700291 if (fc->connected) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700292 queue_request(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700293 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700294 } else {
295 req->out.h.error = -ENOTCONN;
296 request_end(fc, req);
297 }
298}
299
300void request_send_noreply(struct fuse_conn *fc, struct fuse_req *req)
301{
302 req->isreply = 0;
303 request_send_nowait(fc, req);
304}
305
306void request_send_background(struct fuse_conn *fc, struct fuse_req *req)
307{
308 req->isreply = 1;
Miklos Szeredid7133112006-04-10 22:54:55 -0700309 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700310 background_request(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700311 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700312 request_send_nowait(fc, req);
313}
314
Miklos Szeredi334f4852005-09-09 13:10:27 -0700315/*
316 * Lock the request. Up to the next unlock_request() there mustn't be
317 * anything that could cause a page-fault. If the request was already
318 * interrupted bail out.
319 */
Miklos Szeredid7133112006-04-10 22:54:55 -0700320static int lock_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700321{
322 int err = 0;
323 if (req) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700324 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700325 if (req->interrupted)
326 err = -ENOENT;
327 else
328 req->locked = 1;
Miklos Szeredid7133112006-04-10 22:54:55 -0700329 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700330 }
331 return err;
332}
333
334/*
335 * Unlock request. If it was interrupted during being locked, the
336 * requester thread is currently waiting for it to be unlocked, so
337 * wake it up.
338 */
Miklos Szeredid7133112006-04-10 22:54:55 -0700339static void unlock_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700340{
341 if (req) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700342 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700343 req->locked = 0;
344 if (req->interrupted)
345 wake_up(&req->waitq);
Miklos Szeredid7133112006-04-10 22:54:55 -0700346 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700347 }
348}
349
350struct fuse_copy_state {
Miklos Szeredid7133112006-04-10 22:54:55 -0700351 struct fuse_conn *fc;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700352 int write;
353 struct fuse_req *req;
354 const struct iovec *iov;
355 unsigned long nr_segs;
356 unsigned long seglen;
357 unsigned long addr;
358 struct page *pg;
359 void *mapaddr;
360 void *buf;
361 unsigned len;
362};
363
Miklos Szeredid7133112006-04-10 22:54:55 -0700364static void fuse_copy_init(struct fuse_copy_state *cs, struct fuse_conn *fc,
365 int write, struct fuse_req *req,
366 const struct iovec *iov, unsigned long nr_segs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700367{
368 memset(cs, 0, sizeof(*cs));
Miklos Szeredid7133112006-04-10 22:54:55 -0700369 cs->fc = fc;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700370 cs->write = write;
371 cs->req = req;
372 cs->iov = iov;
373 cs->nr_segs = nr_segs;
374}
375
376/* Unmap and put previous page of userspace buffer */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800377static void fuse_copy_finish(struct fuse_copy_state *cs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700378{
379 if (cs->mapaddr) {
380 kunmap_atomic(cs->mapaddr, KM_USER0);
381 if (cs->write) {
382 flush_dcache_page(cs->pg);
383 set_page_dirty_lock(cs->pg);
384 }
385 put_page(cs->pg);
386 cs->mapaddr = NULL;
387 }
388}
389
390/*
391 * Get another pagefull of userspace buffer, and map it to kernel
392 * address space, and lock request
393 */
394static int fuse_copy_fill(struct fuse_copy_state *cs)
395{
396 unsigned long offset;
397 int err;
398
Miklos Szeredid7133112006-04-10 22:54:55 -0700399 unlock_request(cs->fc, cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700400 fuse_copy_finish(cs);
401 if (!cs->seglen) {
402 BUG_ON(!cs->nr_segs);
403 cs->seglen = cs->iov[0].iov_len;
404 cs->addr = (unsigned long) cs->iov[0].iov_base;
405 cs->iov ++;
406 cs->nr_segs --;
407 }
408 down_read(&current->mm->mmap_sem);
409 err = get_user_pages(current, current->mm, cs->addr, 1, cs->write, 0,
410 &cs->pg, NULL);
411 up_read(&current->mm->mmap_sem);
412 if (err < 0)
413 return err;
414 BUG_ON(err != 1);
415 offset = cs->addr % PAGE_SIZE;
416 cs->mapaddr = kmap_atomic(cs->pg, KM_USER0);
417 cs->buf = cs->mapaddr + offset;
418 cs->len = min(PAGE_SIZE - offset, cs->seglen);
419 cs->seglen -= cs->len;
420 cs->addr += cs->len;
421
Miklos Szeredid7133112006-04-10 22:54:55 -0700422 return lock_request(cs->fc, cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700423}
424
425/* Do as much copy to/from userspace buffer as we can */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800426static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700427{
428 unsigned ncpy = min(*size, cs->len);
429 if (val) {
430 if (cs->write)
431 memcpy(cs->buf, *val, ncpy);
432 else
433 memcpy(*val, cs->buf, ncpy);
434 *val += ncpy;
435 }
436 *size -= ncpy;
437 cs->len -= ncpy;
438 cs->buf += ncpy;
439 return ncpy;
440}
441
442/*
443 * Copy a page in the request to/from the userspace buffer. Must be
444 * done atomically
445 */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800446static int fuse_copy_page(struct fuse_copy_state *cs, struct page *page,
447 unsigned offset, unsigned count, int zeroing)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700448{
449 if (page && zeroing && count < PAGE_SIZE) {
450 void *mapaddr = kmap_atomic(page, KM_USER1);
451 memset(mapaddr, 0, PAGE_SIZE);
452 kunmap_atomic(mapaddr, KM_USER1);
453 }
454 while (count) {
455 int err;
456 if (!cs->len && (err = fuse_copy_fill(cs)))
457 return err;
458 if (page) {
459 void *mapaddr = kmap_atomic(page, KM_USER1);
460 void *buf = mapaddr + offset;
461 offset += fuse_copy_do(cs, &buf, &count);
462 kunmap_atomic(mapaddr, KM_USER1);
463 } else
464 offset += fuse_copy_do(cs, NULL, &count);
465 }
466 if (page && !cs->write)
467 flush_dcache_page(page);
468 return 0;
469}
470
471/* Copy pages in the request to/from userspace buffer */
472static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
473 int zeroing)
474{
475 unsigned i;
476 struct fuse_req *req = cs->req;
477 unsigned offset = req->page_offset;
478 unsigned count = min(nbytes, (unsigned) PAGE_SIZE - offset);
479
480 for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
481 struct page *page = req->pages[i];
482 int err = fuse_copy_page(cs, page, offset, count, zeroing);
483 if (err)
484 return err;
485
486 nbytes -= count;
487 count = min(nbytes, (unsigned) PAGE_SIZE);
488 offset = 0;
489 }
490 return 0;
491}
492
493/* Copy a single argument in the request to/from userspace buffer */
494static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
495{
496 while (size) {
497 int err;
498 if (!cs->len && (err = fuse_copy_fill(cs)))
499 return err;
500 fuse_copy_do(cs, &val, &size);
501 }
502 return 0;
503}
504
505/* Copy request arguments to/from userspace buffer */
506static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
507 unsigned argpages, struct fuse_arg *args,
508 int zeroing)
509{
510 int err = 0;
511 unsigned i;
512
513 for (i = 0; !err && i < numargs; i++) {
514 struct fuse_arg *arg = &args[i];
515 if (i == numargs - 1 && argpages)
516 err = fuse_copy_pages(cs, arg->size, zeroing);
517 else
518 err = fuse_copy_one(cs, arg->value, arg->size);
519 }
520 return err;
521}
522
523/* Wait until a request is available on the pending list */
524static void request_wait(struct fuse_conn *fc)
525{
526 DECLARE_WAITQUEUE(wait, current);
527
528 add_wait_queue_exclusive(&fc->waitq, &wait);
Miklos Szeredi9ba7cbb2006-01-16 22:14:34 -0800529 while (fc->connected && list_empty(&fc->pending)) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700530 set_current_state(TASK_INTERRUPTIBLE);
531 if (signal_pending(current))
532 break;
533
Miklos Szeredid7133112006-04-10 22:54:55 -0700534 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700535 schedule();
Miklos Szeredid7133112006-04-10 22:54:55 -0700536 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700537 }
538 set_current_state(TASK_RUNNING);
539 remove_wait_queue(&fc->waitq, &wait);
540}
541
542/*
543 * Read a single request into the userspace filesystem's buffer. This
544 * function waits until a request is available, then removes it from
545 * the pending list and copies request data to userspace buffer. If
546 * no reply is needed (FORGET) or request has been interrupted or
547 * there was an error during the copying then it's finished by calling
548 * request_end(). Otherwise add it to the processing list, and set
549 * the 'sent' flag.
550 */
551static ssize_t fuse_dev_readv(struct file *file, const struct iovec *iov,
552 unsigned long nr_segs, loff_t *off)
553{
554 int err;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700555 struct fuse_req *req;
556 struct fuse_in *in;
557 struct fuse_copy_state cs;
558 unsigned reqsize;
Miklos Szeredi0720b312006-04-10 22:54:55 -0700559 struct fuse_conn *fc = fuse_get_conn(file);
560 if (!fc)
561 return -EPERM;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700562
Miklos Szeredi1d3d7522006-01-06 00:19:40 -0800563 restart:
Miklos Szeredid7133112006-04-10 22:54:55 -0700564 spin_lock(&fc->lock);
Jeff Dikee5ac1d12006-04-10 22:54:53 -0700565 err = -EAGAIN;
566 if ((file->f_flags & O_NONBLOCK) && fc->connected &&
567 list_empty(&fc->pending))
568 goto err_unlock;
569
Miklos Szeredi334f4852005-09-09 13:10:27 -0700570 request_wait(fc);
571 err = -ENODEV;
Miklos Szeredi9ba7cbb2006-01-16 22:14:34 -0800572 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700573 goto err_unlock;
574 err = -ERESTARTSYS;
575 if (list_empty(&fc->pending))
576 goto err_unlock;
577
578 req = list_entry(fc->pending.next, struct fuse_req, list);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800579 req->state = FUSE_REQ_READING;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800580 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700581
582 in = &req->in;
Miklos Szeredi1d3d7522006-01-06 00:19:40 -0800583 reqsize = in->h.len;
584 /* If request is too large, reply with an error and restart the read */
585 if (iov_length(iov, nr_segs) < reqsize) {
586 req->out.h.error = -EIO;
587 /* SETXATTR is special, since it may contain too large data */
588 if (in->h.opcode == FUSE_SETXATTR)
589 req->out.h.error = -E2BIG;
590 request_end(fc, req);
591 goto restart;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700592 }
Miklos Szeredid7133112006-04-10 22:54:55 -0700593 spin_unlock(&fc->lock);
594 fuse_copy_init(&cs, fc, 1, req, iov, nr_segs);
Miklos Szeredi1d3d7522006-01-06 00:19:40 -0800595 err = fuse_copy_one(&cs, &in->h, sizeof(in->h));
596 if (!err)
597 err = fuse_copy_args(&cs, in->numargs, in->argpages,
598 (struct fuse_arg *) in->args, 0);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700599 fuse_copy_finish(&cs);
Miklos Szeredid7133112006-04-10 22:54:55 -0700600 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700601 req->locked = 0;
602 if (!err && req->interrupted)
603 err = -ENOENT;
604 if (err) {
605 if (!req->interrupted)
606 req->out.h.error = -EIO;
607 request_end(fc, req);
608 return err;
609 }
610 if (!req->isreply)
611 request_end(fc, req);
612 else {
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800613 req->state = FUSE_REQ_SENT;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800614 list_move_tail(&req->list, &fc->processing);
Miklos Szeredid7133112006-04-10 22:54:55 -0700615 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700616 }
617 return reqsize;
618
619 err_unlock:
Miklos Szeredid7133112006-04-10 22:54:55 -0700620 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700621 return err;
622}
623
624static ssize_t fuse_dev_read(struct file *file, char __user *buf,
625 size_t nbytes, loff_t *off)
626{
627 struct iovec iov;
628 iov.iov_len = nbytes;
629 iov.iov_base = buf;
630 return fuse_dev_readv(file, &iov, 1, off);
631}
632
633/* Look up request on processing list by unique ID */
634static struct fuse_req *request_find(struct fuse_conn *fc, u64 unique)
635{
636 struct list_head *entry;
637
638 list_for_each(entry, &fc->processing) {
639 struct fuse_req *req;
640 req = list_entry(entry, struct fuse_req, list);
641 if (req->in.h.unique == unique)
642 return req;
643 }
644 return NULL;
645}
646
647static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
648 unsigned nbytes)
649{
650 unsigned reqsize = sizeof(struct fuse_out_header);
651
652 if (out->h.error)
653 return nbytes != reqsize ? -EINVAL : 0;
654
655 reqsize += len_args(out->numargs, out->args);
656
657 if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
658 return -EINVAL;
659 else if (reqsize > nbytes) {
660 struct fuse_arg *lastarg = &out->args[out->numargs-1];
661 unsigned diffsize = reqsize - nbytes;
662 if (diffsize > lastarg->size)
663 return -EINVAL;
664 lastarg->size -= diffsize;
665 }
666 return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
667 out->page_zeroing);
668}
669
670/*
671 * Write a single reply to a request. First the header is copied from
672 * the write buffer. The request is then searched on the processing
673 * list by the unique ID found in the header. If found, then remove
674 * it from the list and copy the rest of the buffer to the request.
675 * The request is finished by calling request_end()
676 */
677static ssize_t fuse_dev_writev(struct file *file, const struct iovec *iov,
678 unsigned long nr_segs, loff_t *off)
679{
680 int err;
681 unsigned nbytes = iov_length(iov, nr_segs);
682 struct fuse_req *req;
683 struct fuse_out_header oh;
684 struct fuse_copy_state cs;
685 struct fuse_conn *fc = fuse_get_conn(file);
686 if (!fc)
Miklos Szeredia87046d2006-04-10 22:54:56 -0700687 return -EPERM;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700688
Miklos Szeredid7133112006-04-10 22:54:55 -0700689 fuse_copy_init(&cs, fc, 0, NULL, iov, nr_segs);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700690 if (nbytes < sizeof(struct fuse_out_header))
691 return -EINVAL;
692
693 err = fuse_copy_one(&cs, &oh, sizeof(oh));
694 if (err)
695 goto err_finish;
696 err = -EINVAL;
697 if (!oh.unique || oh.error <= -1000 || oh.error > 0 ||
698 oh.len != nbytes)
699 goto err_finish;
700
Miklos Szeredid7133112006-04-10 22:54:55 -0700701 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800702 err = -ENOENT;
703 if (!fc->connected)
704 goto err_unlock;
705
Miklos Szeredi334f4852005-09-09 13:10:27 -0700706 req = request_find(fc, oh.unique);
707 err = -EINVAL;
708 if (!req)
709 goto err_unlock;
710
Miklos Szeredi334f4852005-09-09 13:10:27 -0700711 if (req->interrupted) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700712 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700713 fuse_copy_finish(&cs);
Miklos Szeredid7133112006-04-10 22:54:55 -0700714 spin_lock(&fc->lock);
Miklos Szeredi222f1d692006-01-16 22:14:25 -0800715 request_end(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700716 return -ENOENT;
717 }
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800718 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700719 req->out.h = oh;
720 req->locked = 1;
721 cs.req = req;
Miklos Szeredid7133112006-04-10 22:54:55 -0700722 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700723
724 err = copy_out_args(&cs, &req->out, nbytes);
725 fuse_copy_finish(&cs);
726
Miklos Szeredid7133112006-04-10 22:54:55 -0700727 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700728 req->locked = 0;
729 if (!err) {
730 if (req->interrupted)
731 err = -ENOENT;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700732 } else if (!req->interrupted)
733 req->out.h.error = -EIO;
734 request_end(fc, req);
735
736 return err ? err : nbytes;
737
738 err_unlock:
Miklos Szeredid7133112006-04-10 22:54:55 -0700739 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700740 err_finish:
741 fuse_copy_finish(&cs);
742 return err;
743}
744
745static ssize_t fuse_dev_write(struct file *file, const char __user *buf,
746 size_t nbytes, loff_t *off)
747{
748 struct iovec iov;
749 iov.iov_len = nbytes;
750 iov.iov_base = (char __user *) buf;
751 return fuse_dev_writev(file, &iov, 1, off);
752}
753
754static unsigned fuse_dev_poll(struct file *file, poll_table *wait)
755{
Miklos Szeredi334f4852005-09-09 13:10:27 -0700756 unsigned mask = POLLOUT | POLLWRNORM;
Miklos Szeredi7025d9a2006-04-10 22:54:50 -0700757 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700758 if (!fc)
Miklos Szeredi7025d9a2006-04-10 22:54:50 -0700759 return POLLERR;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700760
761 poll_wait(file, &fc->waitq, wait);
762
Miklos Szeredid7133112006-04-10 22:54:55 -0700763 spin_lock(&fc->lock);
Miklos Szeredi7025d9a2006-04-10 22:54:50 -0700764 if (!fc->connected)
765 mask = POLLERR;
766 else if (!list_empty(&fc->pending))
767 mask |= POLLIN | POLLRDNORM;
Miklos Szeredid7133112006-04-10 22:54:55 -0700768 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700769
770 return mask;
771}
772
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800773/*
774 * Abort all requests on the given list (pending or processing)
775 *
Miklos Szeredid7133112006-04-10 22:54:55 -0700776 * This function releases and reacquires fc->lock
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800777 */
Miklos Szeredi334f4852005-09-09 13:10:27 -0700778static void end_requests(struct fuse_conn *fc, struct list_head *head)
779{
780 while (!list_empty(head)) {
781 struct fuse_req *req;
782 req = list_entry(head->next, struct fuse_req, list);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700783 req->out.h.error = -ECONNABORTED;
784 request_end(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700785 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700786 }
787}
788
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800789/*
790 * Abort requests under I/O
791 *
792 * The requests are set to interrupted and finished, and the request
793 * waiter is woken up. This will make request_wait_answer() wait
794 * until the request is unlocked and then return.
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800795 *
796 * If the request is asynchronous, then the end function needs to be
797 * called after waiting for the request to be unlocked (if it was
798 * locked).
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800799 */
800static void end_io_requests(struct fuse_conn *fc)
801{
802 while (!list_empty(&fc->io)) {
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800803 struct fuse_req *req =
804 list_entry(fc->io.next, struct fuse_req, list);
805 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
806
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800807 req->interrupted = 1;
808 req->out.h.error = -ECONNABORTED;
809 req->state = FUSE_REQ_FINISHED;
810 list_del_init(&req->list);
811 wake_up(&req->waitq);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800812 if (end) {
813 req->end = NULL;
814 /* The end function will consume this reference */
815 __fuse_get_request(req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700816 spin_unlock(&fc->lock);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800817 wait_event(req->waitq, !req->locked);
818 end(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700819 spin_lock(&fc->lock);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800820 }
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800821 }
822}
823
824/*
825 * Abort all requests.
826 *
827 * Emergency exit in case of a malicious or accidental deadlock, or
828 * just a hung filesystem.
829 *
830 * The same effect is usually achievable through killing the
831 * filesystem daemon and all users of the filesystem. The exception
832 * is the combination of an asynchronous request and the tricky
833 * deadlock (see Documentation/filesystems/fuse.txt).
834 *
835 * During the aborting, progression of requests from the pending and
836 * processing lists onto the io list, and progression of new requests
837 * onto the pending list is prevented by req->connected being false.
838 *
839 * Progression of requests under I/O to the processing list is
840 * prevented by the req->interrupted flag being true for these
841 * requests. For this reason requests on the io list must be aborted
842 * first.
843 */
844void fuse_abort_conn(struct fuse_conn *fc)
845{
Miklos Szeredid7133112006-04-10 22:54:55 -0700846 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800847 if (fc->connected) {
848 fc->connected = 0;
849 end_io_requests(fc);
850 end_requests(fc, &fc->pending);
851 end_requests(fc, &fc->processing);
852 wake_up_all(&fc->waitq);
Jeff Dike385a17b2006-04-10 22:54:52 -0700853 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800854 }
Miklos Szeredid7133112006-04-10 22:54:55 -0700855 spin_unlock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800856}
857
Miklos Szeredi334f4852005-09-09 13:10:27 -0700858static int fuse_dev_release(struct inode *inode, struct file *file)
859{
Miklos Szeredi0720b312006-04-10 22:54:55 -0700860 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700861 if (fc) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700862 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700863 fc->connected = 0;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700864 end_requests(fc, &fc->pending);
865 end_requests(fc, &fc->processing);
Miklos Szeredid7133112006-04-10 22:54:55 -0700866 spin_unlock(&fc->lock);
Jeff Dike385a17b2006-04-10 22:54:52 -0700867 fasync_helper(-1, file, 0, &fc->fasync);
Miklos Szeredif543f252006-01-16 22:14:35 -0800868 kobject_put(&fc->kobj);
Jeff Dike385a17b2006-04-10 22:54:52 -0700869 }
Miklos Szeredif543f252006-01-16 22:14:35 -0800870
Miklos Szeredi334f4852005-09-09 13:10:27 -0700871 return 0;
872}
873
Jeff Dike385a17b2006-04-10 22:54:52 -0700874static int fuse_dev_fasync(int fd, struct file *file, int on)
875{
876 struct fuse_conn *fc = fuse_get_conn(file);
877 if (!fc)
Miklos Szeredia87046d2006-04-10 22:54:56 -0700878 return -EPERM;
Jeff Dike385a17b2006-04-10 22:54:52 -0700879
880 /* No locking - fasync_helper does its own locking */
881 return fasync_helper(fd, file, on, &fc->fasync);
882}
883
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800884const struct file_operations fuse_dev_operations = {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700885 .owner = THIS_MODULE,
886 .llseek = no_llseek,
887 .read = fuse_dev_read,
888 .readv = fuse_dev_readv,
889 .write = fuse_dev_write,
890 .writev = fuse_dev_writev,
891 .poll = fuse_dev_poll,
892 .release = fuse_dev_release,
Jeff Dike385a17b2006-04-10 22:54:52 -0700893 .fasync = fuse_dev_fasync,
Miklos Szeredi334f4852005-09-09 13:10:27 -0700894};
895
896static struct miscdevice fuse_miscdevice = {
897 .minor = FUSE_MINOR,
898 .name = "fuse",
899 .fops = &fuse_dev_operations,
900};
901
902int __init fuse_dev_init(void)
903{
904 int err = -ENOMEM;
905 fuse_req_cachep = kmem_cache_create("fuse_request",
906 sizeof(struct fuse_req),
907 0, 0, NULL, NULL);
908 if (!fuse_req_cachep)
909 goto out;
910
911 err = misc_register(&fuse_miscdevice);
912 if (err)
913 goto out_cache_clean;
914
915 return 0;
916
917 out_cache_clean:
918 kmem_cache_destroy(fuse_req_cachep);
919 out:
920 return err;
921}
922
923void fuse_dev_cleanup(void)
924{
925 misc_deregister(&fuse_miscdevice);
926 kmem_cache_destroy(fuse_req_cachep);
927}