blob: 4967bd40b953c7e00b9366f1376875b01d3912d0 [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;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +020095 int intr;
Miklos Szeredi08a53cd2006-04-10 22:54:59 -070096 int err;
97
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +020098 atomic_inc(&fc->num_waiting);
Miklos Szeredi08a53cd2006-04-10 22:54:59 -070099 block_sigs(&oldset);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200100 intr = wait_event_interruptible(fc->blocked_waitq, !fc->blocked);
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700101 restore_sigs(&oldset);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200102 err = -EINTR;
103 if (intr)
104 goto out;
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700105
106 req = fuse_request_alloc();
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200107 err = -ENOMEM;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700108 if (!req)
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200109 goto out;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700110
Miklos Szeredi334f4852005-09-09 13:10:27 -0700111 req->in.h.uid = current->fsuid;
112 req->in.h.gid = current->fsgid;
113 req->in.h.pid = current->pid;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200114 req->waiting = 1;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700115 return req;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200116
117 out:
118 atomic_dec(&fc->num_waiting);
119 return ERR_PTR(err);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700120}
121
Miklos Szeredi334f4852005-09-09 13:10:27 -0700122void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req)
123{
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800124 if (atomic_dec_and_test(&req->count)) {
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200125 if (req->waiting)
126 atomic_dec(&fc->num_waiting);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700127 fuse_request_free(req);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800128 }
129}
130
Miklos Szeredi5a5fb1e2006-04-26 10:48:55 +0200131void fuse_release_background(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700132{
Miklos Szeredi5a5fb1e2006-04-26 10:48:55 +0200133 iput(req->inode);
134 iput(req->inode2);
135 if (req->file)
136 fput(req->file);
137 spin_lock(&fc->lock);
138 list_del(&req->bg_entry);
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700139 if (fc->num_background == FUSE_MAX_BACKGROUND) {
140 fc->blocked = 0;
141 wake_up_all(&fc->blocked_waitq);
142 }
143 fc->num_background--;
Miklos Szeredi5a5fb1e2006-04-26 10:48:55 +0200144 spin_unlock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700145}
146
Miklos Szeredi334f4852005-09-09 13:10:27 -0700147/*
148 * This function is called when a request is finished. Either a reply
149 * has arrived or it was interrupted (and not yet sent) or some error
Miklos Szeredif43b1552006-01-16 22:14:26 -0800150 * occurred during communication with userspace, or the device file
151 * was closed. In case of a background request the reference to the
152 * stored objects are released. The requester thread is woken up (if
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800153 * still waiting), the 'end' callback is called if given, else the
154 * reference to the request is released
Miklos Szeredi334f4852005-09-09 13:10:27 -0700155 *
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800156 * Releasing extra reference for foreground requests must be done
157 * within the same locked region as setting state to finished. This
158 * is because fuse_reset_request() may be called after request is
159 * finished and it must be the sole possessor. If request is
160 * interrupted and put in the background, it will return with an error
161 * and hence never be reset and reused.
162 *
Miklos Szeredid7133112006-04-10 22:54:55 -0700163 * Called with fc->lock, unlocks it
Miklos Szeredi334f4852005-09-09 13:10:27 -0700164 */
165static void request_end(struct fuse_conn *fc, struct fuse_req *req)
166{
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800167 list_del(&req->list);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800168 req->state = FUSE_REQ_FINISHED;
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800169 if (!req->background) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700170 spin_unlock(&fc->lock);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700171 wake_up(&req->waitq);
172 fuse_put_request(fc, req);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800173 } else {
174 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
175 req->end = NULL;
Miklos Szeredid7133112006-04-10 22:54:55 -0700176 spin_unlock(&fc->lock);
Miklos Szeredi5a5fb1e2006-04-26 10:48:55 +0200177 down_read(&fc->sbput_sem);
178 if (fc->mounted)
179 fuse_release_background(fc, req);
180 up_read(&fc->sbput_sem);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800181 if (end)
182 end(fc, req);
183 else
184 fuse_put_request(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700185 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700186}
187
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700188/*
189 * Unfortunately request interruption not just solves the deadlock
190 * problem, it causes problems too. These stem from the fact, that an
191 * interrupted request is continued to be processed in userspace,
192 * while all the locks and object references (inode and file) held
193 * during the operation are released.
194 *
195 * To release the locks is exactly why there's a need to interrupt the
196 * request, so there's not a lot that can be done about this, except
197 * introduce additional locking in userspace.
198 *
199 * More important is to keep inode and file references until userspace
200 * has replied, otherwise FORGET and RELEASE could be sent while the
201 * inode/file is still used by the filesystem.
202 *
203 * For this reason the concept of "background" request is introduced.
204 * An interrupted request is backgrounded if it has been already sent
205 * to userspace. Backgrounding involves getting an extra reference to
206 * inode(s) or file used in the request, and adding the request to
207 * fc->background list. When a reply is received for a background
208 * request, the object references are released, and the request is
209 * removed from the list. If the filesystem is unmounted while there
210 * are still background requests, the list is walked and references
211 * are released as if a reply was received.
212 *
213 * There's one more use for a background request. The RELEASE message is
214 * always sent as background, since it doesn't return an error or
215 * data.
216 */
217static void background_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700218{
Miklos Szeredi334f4852005-09-09 13:10:27 -0700219 req->background = 1;
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700220 list_add(&req->bg_entry, &fc->background);
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700221 fc->num_background++;
222 if (fc->num_background == FUSE_MAX_BACKGROUND)
223 fc->blocked = 1;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700224 if (req->inode)
225 req->inode = igrab(req->inode);
226 if (req->inode2)
227 req->inode2 = igrab(req->inode2);
228 if (req->file)
229 get_file(req->file);
230}
231
Miklos Szeredid7133112006-04-10 22:54:55 -0700232/* Called with fc->lock held. Releases, and then reacquires it. */
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700233static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700234{
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700235 sigset_t oldset;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700236
Miklos Szeredid7133112006-04-10 22:54:55 -0700237 spin_unlock(&fc->lock);
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700238 block_sigs(&oldset);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800239 wait_event_interruptible(req->waitq, req->state == FUSE_REQ_FINISHED);
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700240 restore_sigs(&oldset);
Miklos Szeredid7133112006-04-10 22:54:55 -0700241 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800242 if (req->state == FUSE_REQ_FINISHED && !req->interrupted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700243 return;
244
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800245 if (!req->interrupted) {
246 req->out.h.error = -EINTR;
247 req->interrupted = 1;
248 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700249 if (req->locked) {
250 /* This is uninterruptible sleep, because data is
251 being copied to/from the buffers of req. During
252 locked state, there mustn't be any filesystem
253 operation (e.g. page fault), since that could lead
254 to deadlock */
Miklos Szeredid7133112006-04-10 22:54:55 -0700255 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700256 wait_event(req->waitq, !req->locked);
Miklos Szeredid7133112006-04-10 22:54:55 -0700257 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700258 }
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800259 if (req->state == FUSE_REQ_PENDING) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700260 list_del(&req->list);
261 __fuse_put_request(req);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800262 } else if (req->state == FUSE_REQ_SENT)
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700263 background_request(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700264}
265
266static unsigned len_args(unsigned numargs, struct fuse_arg *args)
267{
268 unsigned nbytes = 0;
269 unsigned i;
270
271 for (i = 0; i < numargs; i++)
272 nbytes += args[i].size;
273
274 return nbytes;
275}
276
277static void queue_request(struct fuse_conn *fc, struct fuse_req *req)
278{
279 fc->reqctr++;
280 /* zero is special */
281 if (fc->reqctr == 0)
282 fc->reqctr = 1;
283 req->in.h.unique = fc->reqctr;
284 req->in.h.len = sizeof(struct fuse_in_header) +
285 len_args(req->in.numargs, (struct fuse_arg *) req->in.args);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700286 list_add_tail(&req->list, &fc->pending);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800287 req->state = FUSE_REQ_PENDING;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200288 if (!req->waiting) {
289 req->waiting = 1;
290 atomic_inc(&fc->num_waiting);
291 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700292 wake_up(&fc->waitq);
Jeff Dike385a17b2006-04-10 22:54:52 -0700293 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700294}
295
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700296/*
297 * This can only be interrupted by a SIGKILL
298 */
299void request_send(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700300{
301 req->isreply = 1;
Miklos Szeredid7133112006-04-10 22:54:55 -0700302 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700303 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700304 req->out.h.error = -ENOTCONN;
305 else if (fc->conn_error)
306 req->out.h.error = -ECONNREFUSED;
307 else {
308 queue_request(fc, req);
309 /* acquire extra reference, since request is still needed
310 after request_end() */
311 __fuse_get_request(req);
312
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700313 request_wait_answer(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700314 }
Miklos Szeredid7133112006-04-10 22:54:55 -0700315 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700316}
317
Miklos Szeredi334f4852005-09-09 13:10:27 -0700318static void request_send_nowait(struct fuse_conn *fc, struct fuse_req *req)
319{
Miklos Szeredid7133112006-04-10 22:54:55 -0700320 spin_lock(&fc->lock);
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700321 background_request(fc, req);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700322 if (fc->connected) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700323 queue_request(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700324 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700325 } else {
326 req->out.h.error = -ENOTCONN;
327 request_end(fc, req);
328 }
329}
330
331void request_send_noreply(struct fuse_conn *fc, struct fuse_req *req)
332{
333 req->isreply = 0;
334 request_send_nowait(fc, req);
335}
336
337void request_send_background(struct fuse_conn *fc, struct fuse_req *req)
338{
339 req->isreply = 1;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700340 request_send_nowait(fc, req);
341}
342
Miklos Szeredi334f4852005-09-09 13:10:27 -0700343/*
344 * Lock the request. Up to the next unlock_request() there mustn't be
345 * anything that could cause a page-fault. If the request was already
346 * interrupted bail out.
347 */
Miklos Szeredid7133112006-04-10 22:54:55 -0700348static int lock_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700349{
350 int err = 0;
351 if (req) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700352 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700353 if (req->interrupted)
354 err = -ENOENT;
355 else
356 req->locked = 1;
Miklos Szeredid7133112006-04-10 22:54:55 -0700357 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700358 }
359 return err;
360}
361
362/*
363 * Unlock request. If it was interrupted during being locked, the
364 * requester thread is currently waiting for it to be unlocked, so
365 * wake it up.
366 */
Miklos Szeredid7133112006-04-10 22:54:55 -0700367static void unlock_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700368{
369 if (req) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700370 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700371 req->locked = 0;
372 if (req->interrupted)
373 wake_up(&req->waitq);
Miklos Szeredid7133112006-04-10 22:54:55 -0700374 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700375 }
376}
377
378struct fuse_copy_state {
Miklos Szeredid7133112006-04-10 22:54:55 -0700379 struct fuse_conn *fc;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700380 int write;
381 struct fuse_req *req;
382 const struct iovec *iov;
383 unsigned long nr_segs;
384 unsigned long seglen;
385 unsigned long addr;
386 struct page *pg;
387 void *mapaddr;
388 void *buf;
389 unsigned len;
390};
391
Miklos Szeredid7133112006-04-10 22:54:55 -0700392static void fuse_copy_init(struct fuse_copy_state *cs, struct fuse_conn *fc,
393 int write, struct fuse_req *req,
394 const struct iovec *iov, unsigned long nr_segs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700395{
396 memset(cs, 0, sizeof(*cs));
Miklos Szeredid7133112006-04-10 22:54:55 -0700397 cs->fc = fc;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700398 cs->write = write;
399 cs->req = req;
400 cs->iov = iov;
401 cs->nr_segs = nr_segs;
402}
403
404/* Unmap and put previous page of userspace buffer */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800405static void fuse_copy_finish(struct fuse_copy_state *cs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700406{
407 if (cs->mapaddr) {
408 kunmap_atomic(cs->mapaddr, KM_USER0);
409 if (cs->write) {
410 flush_dcache_page(cs->pg);
411 set_page_dirty_lock(cs->pg);
412 }
413 put_page(cs->pg);
414 cs->mapaddr = NULL;
415 }
416}
417
418/*
419 * Get another pagefull of userspace buffer, and map it to kernel
420 * address space, and lock request
421 */
422static int fuse_copy_fill(struct fuse_copy_state *cs)
423{
424 unsigned long offset;
425 int err;
426
Miklos Szeredid7133112006-04-10 22:54:55 -0700427 unlock_request(cs->fc, cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700428 fuse_copy_finish(cs);
429 if (!cs->seglen) {
430 BUG_ON(!cs->nr_segs);
431 cs->seglen = cs->iov[0].iov_len;
432 cs->addr = (unsigned long) cs->iov[0].iov_base;
433 cs->iov ++;
434 cs->nr_segs --;
435 }
436 down_read(&current->mm->mmap_sem);
437 err = get_user_pages(current, current->mm, cs->addr, 1, cs->write, 0,
438 &cs->pg, NULL);
439 up_read(&current->mm->mmap_sem);
440 if (err < 0)
441 return err;
442 BUG_ON(err != 1);
443 offset = cs->addr % PAGE_SIZE;
444 cs->mapaddr = kmap_atomic(cs->pg, KM_USER0);
445 cs->buf = cs->mapaddr + offset;
446 cs->len = min(PAGE_SIZE - offset, cs->seglen);
447 cs->seglen -= cs->len;
448 cs->addr += cs->len;
449
Miklos Szeredid7133112006-04-10 22:54:55 -0700450 return lock_request(cs->fc, cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700451}
452
453/* Do as much copy to/from userspace buffer as we can */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800454static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700455{
456 unsigned ncpy = min(*size, cs->len);
457 if (val) {
458 if (cs->write)
459 memcpy(cs->buf, *val, ncpy);
460 else
461 memcpy(*val, cs->buf, ncpy);
462 *val += ncpy;
463 }
464 *size -= ncpy;
465 cs->len -= ncpy;
466 cs->buf += ncpy;
467 return ncpy;
468}
469
470/*
471 * Copy a page in the request to/from the userspace buffer. Must be
472 * done atomically
473 */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800474static int fuse_copy_page(struct fuse_copy_state *cs, struct page *page,
475 unsigned offset, unsigned count, int zeroing)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700476{
477 if (page && zeroing && count < PAGE_SIZE) {
478 void *mapaddr = kmap_atomic(page, KM_USER1);
479 memset(mapaddr, 0, PAGE_SIZE);
480 kunmap_atomic(mapaddr, KM_USER1);
481 }
482 while (count) {
483 int err;
484 if (!cs->len && (err = fuse_copy_fill(cs)))
485 return err;
486 if (page) {
487 void *mapaddr = kmap_atomic(page, KM_USER1);
488 void *buf = mapaddr + offset;
489 offset += fuse_copy_do(cs, &buf, &count);
490 kunmap_atomic(mapaddr, KM_USER1);
491 } else
492 offset += fuse_copy_do(cs, NULL, &count);
493 }
494 if (page && !cs->write)
495 flush_dcache_page(page);
496 return 0;
497}
498
499/* Copy pages in the request to/from userspace buffer */
500static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
501 int zeroing)
502{
503 unsigned i;
504 struct fuse_req *req = cs->req;
505 unsigned offset = req->page_offset;
506 unsigned count = min(nbytes, (unsigned) PAGE_SIZE - offset);
507
508 for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
509 struct page *page = req->pages[i];
510 int err = fuse_copy_page(cs, page, offset, count, zeroing);
511 if (err)
512 return err;
513
514 nbytes -= count;
515 count = min(nbytes, (unsigned) PAGE_SIZE);
516 offset = 0;
517 }
518 return 0;
519}
520
521/* Copy a single argument in the request to/from userspace buffer */
522static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
523{
524 while (size) {
525 int err;
526 if (!cs->len && (err = fuse_copy_fill(cs)))
527 return err;
528 fuse_copy_do(cs, &val, &size);
529 }
530 return 0;
531}
532
533/* Copy request arguments to/from userspace buffer */
534static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
535 unsigned argpages, struct fuse_arg *args,
536 int zeroing)
537{
538 int err = 0;
539 unsigned i;
540
541 for (i = 0; !err && i < numargs; i++) {
542 struct fuse_arg *arg = &args[i];
543 if (i == numargs - 1 && argpages)
544 err = fuse_copy_pages(cs, arg->size, zeroing);
545 else
546 err = fuse_copy_one(cs, arg->value, arg->size);
547 }
548 return err;
549}
550
551/* Wait until a request is available on the pending list */
552static void request_wait(struct fuse_conn *fc)
553{
554 DECLARE_WAITQUEUE(wait, current);
555
556 add_wait_queue_exclusive(&fc->waitq, &wait);
Miklos Szeredi9ba7cbb2006-01-16 22:14:34 -0800557 while (fc->connected && list_empty(&fc->pending)) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700558 set_current_state(TASK_INTERRUPTIBLE);
559 if (signal_pending(current))
560 break;
561
Miklos Szeredid7133112006-04-10 22:54:55 -0700562 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700563 schedule();
Miklos Szeredid7133112006-04-10 22:54:55 -0700564 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700565 }
566 set_current_state(TASK_RUNNING);
567 remove_wait_queue(&fc->waitq, &wait);
568}
569
570/*
571 * Read a single request into the userspace filesystem's buffer. This
572 * function waits until a request is available, then removes it from
573 * the pending list and copies request data to userspace buffer. If
574 * no reply is needed (FORGET) or request has been interrupted or
575 * there was an error during the copying then it's finished by calling
576 * request_end(). Otherwise add it to the processing list, and set
577 * the 'sent' flag.
578 */
579static ssize_t fuse_dev_readv(struct file *file, const struct iovec *iov,
580 unsigned long nr_segs, loff_t *off)
581{
582 int err;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700583 struct fuse_req *req;
584 struct fuse_in *in;
585 struct fuse_copy_state cs;
586 unsigned reqsize;
Miklos Szeredi0720b312006-04-10 22:54:55 -0700587 struct fuse_conn *fc = fuse_get_conn(file);
588 if (!fc)
589 return -EPERM;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700590
Miklos Szeredi1d3d7522006-01-06 00:19:40 -0800591 restart:
Miklos Szeredid7133112006-04-10 22:54:55 -0700592 spin_lock(&fc->lock);
Jeff Dikee5ac1d12006-04-10 22:54:53 -0700593 err = -EAGAIN;
594 if ((file->f_flags & O_NONBLOCK) && fc->connected &&
595 list_empty(&fc->pending))
596 goto err_unlock;
597
Miklos Szeredi334f4852005-09-09 13:10:27 -0700598 request_wait(fc);
599 err = -ENODEV;
Miklos Szeredi9ba7cbb2006-01-16 22:14:34 -0800600 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700601 goto err_unlock;
602 err = -ERESTARTSYS;
603 if (list_empty(&fc->pending))
604 goto err_unlock;
605
606 req = list_entry(fc->pending.next, struct fuse_req, list);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800607 req->state = FUSE_REQ_READING;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800608 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700609
610 in = &req->in;
Miklos Szeredi1d3d7522006-01-06 00:19:40 -0800611 reqsize = in->h.len;
612 /* If request is too large, reply with an error and restart the read */
613 if (iov_length(iov, nr_segs) < reqsize) {
614 req->out.h.error = -EIO;
615 /* SETXATTR is special, since it may contain too large data */
616 if (in->h.opcode == FUSE_SETXATTR)
617 req->out.h.error = -E2BIG;
618 request_end(fc, req);
619 goto restart;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700620 }
Miklos Szeredid7133112006-04-10 22:54:55 -0700621 spin_unlock(&fc->lock);
622 fuse_copy_init(&cs, fc, 1, req, iov, nr_segs);
Miklos Szeredi1d3d7522006-01-06 00:19:40 -0800623 err = fuse_copy_one(&cs, &in->h, sizeof(in->h));
624 if (!err)
625 err = fuse_copy_args(&cs, in->numargs, in->argpages,
626 (struct fuse_arg *) in->args, 0);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700627 fuse_copy_finish(&cs);
Miklos Szeredid7133112006-04-10 22:54:55 -0700628 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700629 req->locked = 0;
630 if (!err && req->interrupted)
631 err = -ENOENT;
632 if (err) {
633 if (!req->interrupted)
634 req->out.h.error = -EIO;
635 request_end(fc, req);
636 return err;
637 }
638 if (!req->isreply)
639 request_end(fc, req);
640 else {
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800641 req->state = FUSE_REQ_SENT;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800642 list_move_tail(&req->list, &fc->processing);
Miklos Szeredid7133112006-04-10 22:54:55 -0700643 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700644 }
645 return reqsize;
646
647 err_unlock:
Miklos Szeredid7133112006-04-10 22:54:55 -0700648 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700649 return err;
650}
651
652static ssize_t fuse_dev_read(struct file *file, char __user *buf,
653 size_t nbytes, loff_t *off)
654{
655 struct iovec iov;
656 iov.iov_len = nbytes;
657 iov.iov_base = buf;
658 return fuse_dev_readv(file, &iov, 1, off);
659}
660
661/* Look up request on processing list by unique ID */
662static struct fuse_req *request_find(struct fuse_conn *fc, u64 unique)
663{
664 struct list_head *entry;
665
666 list_for_each(entry, &fc->processing) {
667 struct fuse_req *req;
668 req = list_entry(entry, struct fuse_req, list);
669 if (req->in.h.unique == unique)
670 return req;
671 }
672 return NULL;
673}
674
675static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
676 unsigned nbytes)
677{
678 unsigned reqsize = sizeof(struct fuse_out_header);
679
680 if (out->h.error)
681 return nbytes != reqsize ? -EINVAL : 0;
682
683 reqsize += len_args(out->numargs, out->args);
684
685 if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
686 return -EINVAL;
687 else if (reqsize > nbytes) {
688 struct fuse_arg *lastarg = &out->args[out->numargs-1];
689 unsigned diffsize = reqsize - nbytes;
690 if (diffsize > lastarg->size)
691 return -EINVAL;
692 lastarg->size -= diffsize;
693 }
694 return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
695 out->page_zeroing);
696}
697
698/*
699 * Write a single reply to a request. First the header is copied from
700 * the write buffer. The request is then searched on the processing
701 * list by the unique ID found in the header. If found, then remove
702 * it from the list and copy the rest of the buffer to the request.
703 * The request is finished by calling request_end()
704 */
705static ssize_t fuse_dev_writev(struct file *file, const struct iovec *iov,
706 unsigned long nr_segs, loff_t *off)
707{
708 int err;
709 unsigned nbytes = iov_length(iov, nr_segs);
710 struct fuse_req *req;
711 struct fuse_out_header oh;
712 struct fuse_copy_state cs;
713 struct fuse_conn *fc = fuse_get_conn(file);
714 if (!fc)
Miklos Szeredia87046d2006-04-10 22:54:56 -0700715 return -EPERM;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700716
Miklos Szeredid7133112006-04-10 22:54:55 -0700717 fuse_copy_init(&cs, fc, 0, NULL, iov, nr_segs);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700718 if (nbytes < sizeof(struct fuse_out_header))
719 return -EINVAL;
720
721 err = fuse_copy_one(&cs, &oh, sizeof(oh));
722 if (err)
723 goto err_finish;
724 err = -EINVAL;
725 if (!oh.unique || oh.error <= -1000 || oh.error > 0 ||
726 oh.len != nbytes)
727 goto err_finish;
728
Miklos Szeredid7133112006-04-10 22:54:55 -0700729 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800730 err = -ENOENT;
731 if (!fc->connected)
732 goto err_unlock;
733
Miklos Szeredi334f4852005-09-09 13:10:27 -0700734 req = request_find(fc, oh.unique);
735 err = -EINVAL;
736 if (!req)
737 goto err_unlock;
738
Miklos Szeredi334f4852005-09-09 13:10:27 -0700739 if (req->interrupted) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700740 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700741 fuse_copy_finish(&cs);
Miklos Szeredid7133112006-04-10 22:54:55 -0700742 spin_lock(&fc->lock);
Miklos Szeredi222f1d62006-01-16 22:14:25 -0800743 request_end(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700744 return -ENOENT;
745 }
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800746 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700747 req->out.h = oh;
748 req->locked = 1;
749 cs.req = req;
Miklos Szeredid7133112006-04-10 22:54:55 -0700750 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700751
752 err = copy_out_args(&cs, &req->out, nbytes);
753 fuse_copy_finish(&cs);
754
Miklos Szeredid7133112006-04-10 22:54:55 -0700755 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700756 req->locked = 0;
757 if (!err) {
758 if (req->interrupted)
759 err = -ENOENT;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700760 } else if (!req->interrupted)
761 req->out.h.error = -EIO;
762 request_end(fc, req);
763
764 return err ? err : nbytes;
765
766 err_unlock:
Miklos Szeredid7133112006-04-10 22:54:55 -0700767 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700768 err_finish:
769 fuse_copy_finish(&cs);
770 return err;
771}
772
773static ssize_t fuse_dev_write(struct file *file, const char __user *buf,
774 size_t nbytes, loff_t *off)
775{
776 struct iovec iov;
777 iov.iov_len = nbytes;
778 iov.iov_base = (char __user *) buf;
779 return fuse_dev_writev(file, &iov, 1, off);
780}
781
782static unsigned fuse_dev_poll(struct file *file, poll_table *wait)
783{
Miklos Szeredi334f4852005-09-09 13:10:27 -0700784 unsigned mask = POLLOUT | POLLWRNORM;
Miklos Szeredi7025d9a2006-04-10 22:54:50 -0700785 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700786 if (!fc)
Miklos Szeredi7025d9a2006-04-10 22:54:50 -0700787 return POLLERR;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700788
789 poll_wait(file, &fc->waitq, wait);
790
Miklos Szeredid7133112006-04-10 22:54:55 -0700791 spin_lock(&fc->lock);
Miklos Szeredi7025d9a2006-04-10 22:54:50 -0700792 if (!fc->connected)
793 mask = POLLERR;
794 else if (!list_empty(&fc->pending))
795 mask |= POLLIN | POLLRDNORM;
Miklos Szeredid7133112006-04-10 22:54:55 -0700796 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700797
798 return mask;
799}
800
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800801/*
802 * Abort all requests on the given list (pending or processing)
803 *
Miklos Szeredid7133112006-04-10 22:54:55 -0700804 * This function releases and reacquires fc->lock
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800805 */
Miklos Szeredi334f4852005-09-09 13:10:27 -0700806static void end_requests(struct fuse_conn *fc, struct list_head *head)
807{
808 while (!list_empty(head)) {
809 struct fuse_req *req;
810 req = list_entry(head->next, struct fuse_req, list);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700811 req->out.h.error = -ECONNABORTED;
812 request_end(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700813 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700814 }
815}
816
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800817/*
818 * Abort requests under I/O
819 *
820 * The requests are set to interrupted and finished, and the request
821 * waiter is woken up. This will make request_wait_answer() wait
822 * until the request is unlocked and then return.
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800823 *
824 * If the request is asynchronous, then the end function needs to be
825 * called after waiting for the request to be unlocked (if it was
826 * locked).
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800827 */
828static void end_io_requests(struct fuse_conn *fc)
829{
830 while (!list_empty(&fc->io)) {
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800831 struct fuse_req *req =
832 list_entry(fc->io.next, struct fuse_req, list);
833 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
834
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800835 req->interrupted = 1;
836 req->out.h.error = -ECONNABORTED;
837 req->state = FUSE_REQ_FINISHED;
838 list_del_init(&req->list);
839 wake_up(&req->waitq);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800840 if (end) {
841 req->end = NULL;
842 /* The end function will consume this reference */
843 __fuse_get_request(req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700844 spin_unlock(&fc->lock);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800845 wait_event(req->waitq, !req->locked);
846 end(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700847 spin_lock(&fc->lock);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800848 }
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800849 }
850}
851
852/*
853 * Abort all requests.
854 *
855 * Emergency exit in case of a malicious or accidental deadlock, or
856 * just a hung filesystem.
857 *
858 * The same effect is usually achievable through killing the
859 * filesystem daemon and all users of the filesystem. The exception
860 * is the combination of an asynchronous request and the tricky
861 * deadlock (see Documentation/filesystems/fuse.txt).
862 *
863 * During the aborting, progression of requests from the pending and
864 * processing lists onto the io list, and progression of new requests
865 * onto the pending list is prevented by req->connected being false.
866 *
867 * Progression of requests under I/O to the processing list is
868 * prevented by the req->interrupted flag being true for these
869 * requests. For this reason requests on the io list must be aborted
870 * first.
871 */
872void fuse_abort_conn(struct fuse_conn *fc)
873{
Miklos Szeredid7133112006-04-10 22:54:55 -0700874 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800875 if (fc->connected) {
876 fc->connected = 0;
877 end_io_requests(fc);
878 end_requests(fc, &fc->pending);
879 end_requests(fc, &fc->processing);
880 wake_up_all(&fc->waitq);
Jeff Dike385a17b2006-04-10 22:54:52 -0700881 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800882 }
Miklos Szeredid7133112006-04-10 22:54:55 -0700883 spin_unlock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800884}
885
Miklos Szeredi334f4852005-09-09 13:10:27 -0700886static int fuse_dev_release(struct inode *inode, struct file *file)
887{
Miklos Szeredi0720b312006-04-10 22:54:55 -0700888 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700889 if (fc) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700890 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700891 fc->connected = 0;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700892 end_requests(fc, &fc->pending);
893 end_requests(fc, &fc->processing);
Miklos Szeredid7133112006-04-10 22:54:55 -0700894 spin_unlock(&fc->lock);
Jeff Dike385a17b2006-04-10 22:54:52 -0700895 fasync_helper(-1, file, 0, &fc->fasync);
Miklos Szeredif543f252006-01-16 22:14:35 -0800896 kobject_put(&fc->kobj);
Jeff Dike385a17b2006-04-10 22:54:52 -0700897 }
Miklos Szeredif543f252006-01-16 22:14:35 -0800898
Miklos Szeredi334f4852005-09-09 13:10:27 -0700899 return 0;
900}
901
Jeff Dike385a17b2006-04-10 22:54:52 -0700902static int fuse_dev_fasync(int fd, struct file *file, int on)
903{
904 struct fuse_conn *fc = fuse_get_conn(file);
905 if (!fc)
Miklos Szeredia87046d2006-04-10 22:54:56 -0700906 return -EPERM;
Jeff Dike385a17b2006-04-10 22:54:52 -0700907
908 /* No locking - fasync_helper does its own locking */
909 return fasync_helper(fd, file, on, &fc->fasync);
910}
911
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800912const struct file_operations fuse_dev_operations = {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700913 .owner = THIS_MODULE,
914 .llseek = no_llseek,
915 .read = fuse_dev_read,
916 .readv = fuse_dev_readv,
917 .write = fuse_dev_write,
918 .writev = fuse_dev_writev,
919 .poll = fuse_dev_poll,
920 .release = fuse_dev_release,
Jeff Dike385a17b2006-04-10 22:54:52 -0700921 .fasync = fuse_dev_fasync,
Miklos Szeredi334f4852005-09-09 13:10:27 -0700922};
923
924static struct miscdevice fuse_miscdevice = {
925 .minor = FUSE_MINOR,
926 .name = "fuse",
927 .fops = &fuse_dev_operations,
928};
929
930int __init fuse_dev_init(void)
931{
932 int err = -ENOMEM;
933 fuse_req_cachep = kmem_cache_create("fuse_request",
934 sizeof(struct fuse_req),
935 0, 0, NULL, NULL);
936 if (!fuse_req_cachep)
937 goto out;
938
939 err = misc_register(&fuse_miscdevice);
940 if (err)
941 goto out_cache_clean;
942
943 return 0;
944
945 out_cache_clean:
946 kmem_cache_destroy(fuse_req_cachep);
947 out:
948 return err;
949}
950
951void fuse_dev_cleanup(void)
952{
953 misc_deregister(&fuse_miscdevice);
954 kmem_cache_destroy(fuse_req_cachep);
955}