blob: 8538b298a6b0d1ab7020a3676af189c57b14da46 [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 fuse_request_init(req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700112 req->in.h.uid = current->fsuid;
113 req->in.h.gid = current->fsgid;
114 req->in.h.pid = current->pid;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200115 req->waiting = 1;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700116 return req;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200117
118 out:
119 atomic_dec(&fc->num_waiting);
120 return ERR_PTR(err);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700121}
122
Miklos Szeredi334f4852005-09-09 13:10:27 -0700123void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req)
124{
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800125 if (atomic_dec_and_test(&req->count)) {
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200126 if (req->waiting)
127 atomic_dec(&fc->num_waiting);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700128 fuse_request_free(req);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800129 }
130}
131
Miklos Szeredi73ce8352006-04-11 21:14:26 +0200132void fuse_remove_background(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700133{
Miklos Szeredi73ce8352006-04-11 21:14:26 +0200134 list_del_init(&req->bg_entry);
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700135 if (fc->num_background == FUSE_MAX_BACKGROUND) {
136 fc->blocked = 0;
137 wake_up_all(&fc->blocked_waitq);
138 }
139 fc->num_background--;
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700140}
141
Miklos Szeredi334f4852005-09-09 13:10:27 -0700142/*
143 * This function is called when a request is finished. Either a reply
144 * has arrived or it was interrupted (and not yet sent) or some error
Miklos Szeredif43b1552006-01-16 22:14:26 -0800145 * occurred during communication with userspace, or the device file
146 * was closed. In case of a background request the reference to the
147 * stored objects are released. The requester thread is woken up (if
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800148 * still waiting), the 'end' callback is called if given, else the
149 * reference to the request is released
Miklos Szeredi334f4852005-09-09 13:10:27 -0700150 *
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800151 * Releasing extra reference for foreground requests must be done
152 * within the same locked region as setting state to finished. This
153 * is because fuse_reset_request() may be called after request is
154 * finished and it must be the sole possessor. If request is
155 * interrupted and put in the background, it will return with an error
156 * and hence never be reset and reused.
157 *
Miklos Szeredid7133112006-04-10 22:54:55 -0700158 * Called with fc->lock, unlocks it
Miklos Szeredi334f4852005-09-09 13:10:27 -0700159 */
160static void request_end(struct fuse_conn *fc, struct fuse_req *req)
161{
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800162 list_del(&req->list);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800163 req->state = FUSE_REQ_FINISHED;
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800164 if (!req->background) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700165 spin_unlock(&fc->lock);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700166 wake_up(&req->waitq);
167 fuse_put_request(fc, req);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800168 } else {
Miklos Szeredi73ce8352006-04-11 21:14:26 +0200169 struct inode *inode = req->inode;
170 struct inode *inode2 = req->inode2;
171 struct file *file = req->file;
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800172 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
173 req->end = NULL;
Miklos Szeredi73ce8352006-04-11 21:14:26 +0200174 req->inode = NULL;
175 req->inode2 = NULL;
176 req->file = NULL;
177 if (!list_empty(&req->bg_entry))
178 fuse_remove_background(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700179 spin_unlock(&fc->lock);
Miklos Szeredi73ce8352006-04-11 21:14:26 +0200180
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800181 if (end)
182 end(fc, req);
183 else
184 fuse_put_request(fc, req);
Miklos Szeredi73ce8352006-04-11 21:14:26 +0200185
186 if (file)
187 fput(file);
188 iput(inode);
189 iput(inode2);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700190 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700191}
192
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700193/*
194 * Unfortunately request interruption not just solves the deadlock
195 * problem, it causes problems too. These stem from the fact, that an
196 * interrupted request is continued to be processed in userspace,
197 * while all the locks and object references (inode and file) held
198 * during the operation are released.
199 *
200 * To release the locks is exactly why there's a need to interrupt the
201 * request, so there's not a lot that can be done about this, except
202 * introduce additional locking in userspace.
203 *
204 * More important is to keep inode and file references until userspace
205 * has replied, otherwise FORGET and RELEASE could be sent while the
206 * inode/file is still used by the filesystem.
207 *
208 * For this reason the concept of "background" request is introduced.
209 * An interrupted request is backgrounded if it has been already sent
210 * to userspace. Backgrounding involves getting an extra reference to
211 * inode(s) or file used in the request, and adding the request to
212 * fc->background list. When a reply is received for a background
213 * request, the object references are released, and the request is
214 * removed from the list. If the filesystem is unmounted while there
215 * are still background requests, the list is walked and references
216 * are released as if a reply was received.
217 *
218 * There's one more use for a background request. The RELEASE message is
219 * always sent as background, since it doesn't return an error or
220 * data.
221 */
222static void background_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700223{
Miklos Szeredi334f4852005-09-09 13:10:27 -0700224 req->background = 1;
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700225 list_add(&req->bg_entry, &fc->background);
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700226 fc->num_background++;
227 if (fc->num_background == FUSE_MAX_BACKGROUND)
228 fc->blocked = 1;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700229 if (req->inode)
230 req->inode = igrab(req->inode);
231 if (req->inode2)
232 req->inode2 = igrab(req->inode2);
233 if (req->file)
234 get_file(req->file);
235}
236
Miklos Szeredid7133112006-04-10 22:54:55 -0700237/* Called with fc->lock held. Releases, and then reacquires it. */
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700238static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700239{
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700240 sigset_t oldset;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700241
Miklos Szeredid7133112006-04-10 22:54:55 -0700242 spin_unlock(&fc->lock);
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700243 block_sigs(&oldset);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800244 wait_event_interruptible(req->waitq, req->state == FUSE_REQ_FINISHED);
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700245 restore_sigs(&oldset);
Miklos Szeredid7133112006-04-10 22:54:55 -0700246 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800247 if (req->state == FUSE_REQ_FINISHED && !req->interrupted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700248 return;
249
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800250 if (!req->interrupted) {
251 req->out.h.error = -EINTR;
252 req->interrupted = 1;
253 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700254 if (req->locked) {
255 /* This is uninterruptible sleep, because data is
256 being copied to/from the buffers of req. During
257 locked state, there mustn't be any filesystem
258 operation (e.g. page fault), since that could lead
259 to deadlock */
Miklos Szeredid7133112006-04-10 22:54:55 -0700260 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700261 wait_event(req->waitq, !req->locked);
Miklos Szeredid7133112006-04-10 22:54:55 -0700262 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700263 }
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800264 if (req->state == FUSE_REQ_PENDING) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700265 list_del(&req->list);
266 __fuse_put_request(req);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800267 } else if (req->state == FUSE_REQ_SENT)
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700268 background_request(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700269}
270
271static unsigned len_args(unsigned numargs, struct fuse_arg *args)
272{
273 unsigned nbytes = 0;
274 unsigned i;
275
276 for (i = 0; i < numargs; i++)
277 nbytes += args[i].size;
278
279 return nbytes;
280}
281
282static void queue_request(struct fuse_conn *fc, struct fuse_req *req)
283{
284 fc->reqctr++;
285 /* zero is special */
286 if (fc->reqctr == 0)
287 fc->reqctr = 1;
288 req->in.h.unique = fc->reqctr;
289 req->in.h.len = sizeof(struct fuse_in_header) +
290 len_args(req->in.numargs, (struct fuse_arg *) req->in.args);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700291 list_add_tail(&req->list, &fc->pending);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800292 req->state = FUSE_REQ_PENDING;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200293 if (!req->waiting) {
294 req->waiting = 1;
295 atomic_inc(&fc->num_waiting);
296 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700297 wake_up(&fc->waitq);
Jeff Dike385a17b2006-04-10 22:54:52 -0700298 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700299}
300
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700301/*
302 * This can only be interrupted by a SIGKILL
303 */
304void request_send(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700305{
306 req->isreply = 1;
Miklos Szeredid7133112006-04-10 22:54:55 -0700307 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700308 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700309 req->out.h.error = -ENOTCONN;
310 else if (fc->conn_error)
311 req->out.h.error = -ECONNREFUSED;
312 else {
313 queue_request(fc, req);
314 /* acquire extra reference, since request is still needed
315 after request_end() */
316 __fuse_get_request(req);
317
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700318 request_wait_answer(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700319 }
Miklos Szeredid7133112006-04-10 22:54:55 -0700320 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700321}
322
Miklos Szeredi334f4852005-09-09 13:10:27 -0700323static void request_send_nowait(struct fuse_conn *fc, struct fuse_req *req)
324{
Miklos Szeredid7133112006-04-10 22:54:55 -0700325 spin_lock(&fc->lock);
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700326 background_request(fc, req);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700327 if (fc->connected) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700328 queue_request(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700329 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700330 } else {
331 req->out.h.error = -ENOTCONN;
332 request_end(fc, req);
333 }
334}
335
336void request_send_noreply(struct fuse_conn *fc, struct fuse_req *req)
337{
338 req->isreply = 0;
339 request_send_nowait(fc, req);
340}
341
342void request_send_background(struct fuse_conn *fc, struct fuse_req *req)
343{
344 req->isreply = 1;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700345 request_send_nowait(fc, req);
346}
347
Miklos Szeredi334f4852005-09-09 13:10:27 -0700348/*
349 * Lock the request. Up to the next unlock_request() there mustn't be
350 * anything that could cause a page-fault. If the request was already
351 * interrupted bail out.
352 */
Miklos Szeredid7133112006-04-10 22:54:55 -0700353static int lock_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700354{
355 int err = 0;
356 if (req) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700357 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700358 if (req->interrupted)
359 err = -ENOENT;
360 else
361 req->locked = 1;
Miklos Szeredid7133112006-04-10 22:54:55 -0700362 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700363 }
364 return err;
365}
366
367/*
368 * Unlock request. If it was interrupted during being locked, the
369 * requester thread is currently waiting for it to be unlocked, so
370 * wake it up.
371 */
Miklos Szeredid7133112006-04-10 22:54:55 -0700372static void unlock_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700373{
374 if (req) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700375 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700376 req->locked = 0;
377 if (req->interrupted)
378 wake_up(&req->waitq);
Miklos Szeredid7133112006-04-10 22:54:55 -0700379 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700380 }
381}
382
383struct fuse_copy_state {
Miklos Szeredid7133112006-04-10 22:54:55 -0700384 struct fuse_conn *fc;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700385 int write;
386 struct fuse_req *req;
387 const struct iovec *iov;
388 unsigned long nr_segs;
389 unsigned long seglen;
390 unsigned long addr;
391 struct page *pg;
392 void *mapaddr;
393 void *buf;
394 unsigned len;
395};
396
Miklos Szeredid7133112006-04-10 22:54:55 -0700397static void fuse_copy_init(struct fuse_copy_state *cs, struct fuse_conn *fc,
398 int write, struct fuse_req *req,
399 const struct iovec *iov, unsigned long nr_segs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700400{
401 memset(cs, 0, sizeof(*cs));
Miklos Szeredid7133112006-04-10 22:54:55 -0700402 cs->fc = fc;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700403 cs->write = write;
404 cs->req = req;
405 cs->iov = iov;
406 cs->nr_segs = nr_segs;
407}
408
409/* Unmap and put previous page of userspace buffer */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800410static void fuse_copy_finish(struct fuse_copy_state *cs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700411{
412 if (cs->mapaddr) {
413 kunmap_atomic(cs->mapaddr, KM_USER0);
414 if (cs->write) {
415 flush_dcache_page(cs->pg);
416 set_page_dirty_lock(cs->pg);
417 }
418 put_page(cs->pg);
419 cs->mapaddr = NULL;
420 }
421}
422
423/*
424 * Get another pagefull of userspace buffer, and map it to kernel
425 * address space, and lock request
426 */
427static int fuse_copy_fill(struct fuse_copy_state *cs)
428{
429 unsigned long offset;
430 int err;
431
Miklos Szeredid7133112006-04-10 22:54:55 -0700432 unlock_request(cs->fc, cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700433 fuse_copy_finish(cs);
434 if (!cs->seglen) {
435 BUG_ON(!cs->nr_segs);
436 cs->seglen = cs->iov[0].iov_len;
437 cs->addr = (unsigned long) cs->iov[0].iov_base;
438 cs->iov ++;
439 cs->nr_segs --;
440 }
441 down_read(&current->mm->mmap_sem);
442 err = get_user_pages(current, current->mm, cs->addr, 1, cs->write, 0,
443 &cs->pg, NULL);
444 up_read(&current->mm->mmap_sem);
445 if (err < 0)
446 return err;
447 BUG_ON(err != 1);
448 offset = cs->addr % PAGE_SIZE;
449 cs->mapaddr = kmap_atomic(cs->pg, KM_USER0);
450 cs->buf = cs->mapaddr + offset;
451 cs->len = min(PAGE_SIZE - offset, cs->seglen);
452 cs->seglen -= cs->len;
453 cs->addr += cs->len;
454
Miklos Szeredid7133112006-04-10 22:54:55 -0700455 return lock_request(cs->fc, cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700456}
457
458/* Do as much copy to/from userspace buffer as we can */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800459static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700460{
461 unsigned ncpy = min(*size, cs->len);
462 if (val) {
463 if (cs->write)
464 memcpy(cs->buf, *val, ncpy);
465 else
466 memcpy(*val, cs->buf, ncpy);
467 *val += ncpy;
468 }
469 *size -= ncpy;
470 cs->len -= ncpy;
471 cs->buf += ncpy;
472 return ncpy;
473}
474
475/*
476 * Copy a page in the request to/from the userspace buffer. Must be
477 * done atomically
478 */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800479static int fuse_copy_page(struct fuse_copy_state *cs, struct page *page,
480 unsigned offset, unsigned count, int zeroing)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700481{
482 if (page && zeroing && count < PAGE_SIZE) {
483 void *mapaddr = kmap_atomic(page, KM_USER1);
484 memset(mapaddr, 0, PAGE_SIZE);
485 kunmap_atomic(mapaddr, KM_USER1);
486 }
487 while (count) {
488 int err;
489 if (!cs->len && (err = fuse_copy_fill(cs)))
490 return err;
491 if (page) {
492 void *mapaddr = kmap_atomic(page, KM_USER1);
493 void *buf = mapaddr + offset;
494 offset += fuse_copy_do(cs, &buf, &count);
495 kunmap_atomic(mapaddr, KM_USER1);
496 } else
497 offset += fuse_copy_do(cs, NULL, &count);
498 }
499 if (page && !cs->write)
500 flush_dcache_page(page);
501 return 0;
502}
503
504/* Copy pages in the request to/from userspace buffer */
505static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
506 int zeroing)
507{
508 unsigned i;
509 struct fuse_req *req = cs->req;
510 unsigned offset = req->page_offset;
511 unsigned count = min(nbytes, (unsigned) PAGE_SIZE - offset);
512
513 for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
514 struct page *page = req->pages[i];
515 int err = fuse_copy_page(cs, page, offset, count, zeroing);
516 if (err)
517 return err;
518
519 nbytes -= count;
520 count = min(nbytes, (unsigned) PAGE_SIZE);
521 offset = 0;
522 }
523 return 0;
524}
525
526/* Copy a single argument in the request to/from userspace buffer */
527static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
528{
529 while (size) {
530 int err;
531 if (!cs->len && (err = fuse_copy_fill(cs)))
532 return err;
533 fuse_copy_do(cs, &val, &size);
534 }
535 return 0;
536}
537
538/* Copy request arguments to/from userspace buffer */
539static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
540 unsigned argpages, struct fuse_arg *args,
541 int zeroing)
542{
543 int err = 0;
544 unsigned i;
545
546 for (i = 0; !err && i < numargs; i++) {
547 struct fuse_arg *arg = &args[i];
548 if (i == numargs - 1 && argpages)
549 err = fuse_copy_pages(cs, arg->size, zeroing);
550 else
551 err = fuse_copy_one(cs, arg->value, arg->size);
552 }
553 return err;
554}
555
556/* Wait until a request is available on the pending list */
557static void request_wait(struct fuse_conn *fc)
558{
559 DECLARE_WAITQUEUE(wait, current);
560
561 add_wait_queue_exclusive(&fc->waitq, &wait);
Miklos Szeredi9ba7cbb2006-01-16 22:14:34 -0800562 while (fc->connected && list_empty(&fc->pending)) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700563 set_current_state(TASK_INTERRUPTIBLE);
564 if (signal_pending(current))
565 break;
566
Miklos Szeredid7133112006-04-10 22:54:55 -0700567 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700568 schedule();
Miklos Szeredid7133112006-04-10 22:54:55 -0700569 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700570 }
571 set_current_state(TASK_RUNNING);
572 remove_wait_queue(&fc->waitq, &wait);
573}
574
575/*
576 * Read a single request into the userspace filesystem's buffer. This
577 * function waits until a request is available, then removes it from
578 * the pending list and copies request data to userspace buffer. If
579 * no reply is needed (FORGET) or request has been interrupted or
580 * there was an error during the copying then it's finished by calling
581 * request_end(). Otherwise add it to the processing list, and set
582 * the 'sent' flag.
583 */
584static ssize_t fuse_dev_readv(struct file *file, const struct iovec *iov,
585 unsigned long nr_segs, loff_t *off)
586{
587 int err;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700588 struct fuse_req *req;
589 struct fuse_in *in;
590 struct fuse_copy_state cs;
591 unsigned reqsize;
Miklos Szeredi0720b312006-04-10 22:54:55 -0700592 struct fuse_conn *fc = fuse_get_conn(file);
593 if (!fc)
594 return -EPERM;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700595
Miklos Szeredi1d3d7522006-01-06 00:19:40 -0800596 restart:
Miklos Szeredid7133112006-04-10 22:54:55 -0700597 spin_lock(&fc->lock);
Jeff Dikee5ac1d12006-04-10 22:54:53 -0700598 err = -EAGAIN;
599 if ((file->f_flags & O_NONBLOCK) && fc->connected &&
600 list_empty(&fc->pending))
601 goto err_unlock;
602
Miklos Szeredi334f4852005-09-09 13:10:27 -0700603 request_wait(fc);
604 err = -ENODEV;
Miklos Szeredi9ba7cbb2006-01-16 22:14:34 -0800605 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700606 goto err_unlock;
607 err = -ERESTARTSYS;
608 if (list_empty(&fc->pending))
609 goto err_unlock;
610
611 req = list_entry(fc->pending.next, struct fuse_req, list);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800612 req->state = FUSE_REQ_READING;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800613 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700614
615 in = &req->in;
Miklos Szeredi1d3d7522006-01-06 00:19:40 -0800616 reqsize = in->h.len;
617 /* If request is too large, reply with an error and restart the read */
618 if (iov_length(iov, nr_segs) < reqsize) {
619 req->out.h.error = -EIO;
620 /* SETXATTR is special, since it may contain too large data */
621 if (in->h.opcode == FUSE_SETXATTR)
622 req->out.h.error = -E2BIG;
623 request_end(fc, req);
624 goto restart;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700625 }
Miklos Szeredid7133112006-04-10 22:54:55 -0700626 spin_unlock(&fc->lock);
627 fuse_copy_init(&cs, fc, 1, req, iov, nr_segs);
Miklos Szeredi1d3d7522006-01-06 00:19:40 -0800628 err = fuse_copy_one(&cs, &in->h, sizeof(in->h));
629 if (!err)
630 err = fuse_copy_args(&cs, in->numargs, in->argpages,
631 (struct fuse_arg *) in->args, 0);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700632 fuse_copy_finish(&cs);
Miklos Szeredid7133112006-04-10 22:54:55 -0700633 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700634 req->locked = 0;
635 if (!err && req->interrupted)
636 err = -ENOENT;
637 if (err) {
638 if (!req->interrupted)
639 req->out.h.error = -EIO;
640 request_end(fc, req);
641 return err;
642 }
643 if (!req->isreply)
644 request_end(fc, req);
645 else {
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800646 req->state = FUSE_REQ_SENT;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800647 list_move_tail(&req->list, &fc->processing);
Miklos Szeredid7133112006-04-10 22:54:55 -0700648 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700649 }
650 return reqsize;
651
652 err_unlock:
Miklos Szeredid7133112006-04-10 22:54:55 -0700653 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700654 return err;
655}
656
657static ssize_t fuse_dev_read(struct file *file, char __user *buf,
658 size_t nbytes, loff_t *off)
659{
660 struct iovec iov;
661 iov.iov_len = nbytes;
662 iov.iov_base = buf;
663 return fuse_dev_readv(file, &iov, 1, off);
664}
665
666/* Look up request on processing list by unique ID */
667static struct fuse_req *request_find(struct fuse_conn *fc, u64 unique)
668{
669 struct list_head *entry;
670
671 list_for_each(entry, &fc->processing) {
672 struct fuse_req *req;
673 req = list_entry(entry, struct fuse_req, list);
674 if (req->in.h.unique == unique)
675 return req;
676 }
677 return NULL;
678}
679
680static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
681 unsigned nbytes)
682{
683 unsigned reqsize = sizeof(struct fuse_out_header);
684
685 if (out->h.error)
686 return nbytes != reqsize ? -EINVAL : 0;
687
688 reqsize += len_args(out->numargs, out->args);
689
690 if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
691 return -EINVAL;
692 else if (reqsize > nbytes) {
693 struct fuse_arg *lastarg = &out->args[out->numargs-1];
694 unsigned diffsize = reqsize - nbytes;
695 if (diffsize > lastarg->size)
696 return -EINVAL;
697 lastarg->size -= diffsize;
698 }
699 return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
700 out->page_zeroing);
701}
702
703/*
704 * Write a single reply to a request. First the header is copied from
705 * the write buffer. The request is then searched on the processing
706 * list by the unique ID found in the header. If found, then remove
707 * it from the list and copy the rest of the buffer to the request.
708 * The request is finished by calling request_end()
709 */
710static ssize_t fuse_dev_writev(struct file *file, const struct iovec *iov,
711 unsigned long nr_segs, loff_t *off)
712{
713 int err;
714 unsigned nbytes = iov_length(iov, nr_segs);
715 struct fuse_req *req;
716 struct fuse_out_header oh;
717 struct fuse_copy_state cs;
718 struct fuse_conn *fc = fuse_get_conn(file);
719 if (!fc)
Miklos Szeredia87046d2006-04-10 22:54:56 -0700720 return -EPERM;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700721
Miklos Szeredid7133112006-04-10 22:54:55 -0700722 fuse_copy_init(&cs, fc, 0, NULL, iov, nr_segs);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700723 if (nbytes < sizeof(struct fuse_out_header))
724 return -EINVAL;
725
726 err = fuse_copy_one(&cs, &oh, sizeof(oh));
727 if (err)
728 goto err_finish;
729 err = -EINVAL;
730 if (!oh.unique || oh.error <= -1000 || oh.error > 0 ||
731 oh.len != nbytes)
732 goto err_finish;
733
Miklos Szeredid7133112006-04-10 22:54:55 -0700734 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800735 err = -ENOENT;
736 if (!fc->connected)
737 goto err_unlock;
738
Miklos Szeredi334f4852005-09-09 13:10:27 -0700739 req = request_find(fc, oh.unique);
740 err = -EINVAL;
741 if (!req)
742 goto err_unlock;
743
Miklos Szeredi334f4852005-09-09 13:10:27 -0700744 if (req->interrupted) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700745 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700746 fuse_copy_finish(&cs);
Miklos Szeredid7133112006-04-10 22:54:55 -0700747 spin_lock(&fc->lock);
Miklos Szeredi222f1d62006-01-16 22:14:25 -0800748 request_end(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700749 return -ENOENT;
750 }
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800751 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700752 req->out.h = oh;
753 req->locked = 1;
754 cs.req = req;
Miklos Szeredid7133112006-04-10 22:54:55 -0700755 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700756
757 err = copy_out_args(&cs, &req->out, nbytes);
758 fuse_copy_finish(&cs);
759
Miklos Szeredid7133112006-04-10 22:54:55 -0700760 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700761 req->locked = 0;
762 if (!err) {
763 if (req->interrupted)
764 err = -ENOENT;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700765 } else if (!req->interrupted)
766 req->out.h.error = -EIO;
767 request_end(fc, req);
768
769 return err ? err : nbytes;
770
771 err_unlock:
Miklos Szeredid7133112006-04-10 22:54:55 -0700772 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700773 err_finish:
774 fuse_copy_finish(&cs);
775 return err;
776}
777
778static ssize_t fuse_dev_write(struct file *file, const char __user *buf,
779 size_t nbytes, loff_t *off)
780{
781 struct iovec iov;
782 iov.iov_len = nbytes;
783 iov.iov_base = (char __user *) buf;
784 return fuse_dev_writev(file, &iov, 1, off);
785}
786
787static unsigned fuse_dev_poll(struct file *file, poll_table *wait)
788{
Miklos Szeredi334f4852005-09-09 13:10:27 -0700789 unsigned mask = POLLOUT | POLLWRNORM;
Miklos Szeredi7025d9a2006-04-10 22:54:50 -0700790 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700791 if (!fc)
Miklos Szeredi7025d9a2006-04-10 22:54:50 -0700792 return POLLERR;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700793
794 poll_wait(file, &fc->waitq, wait);
795
Miklos Szeredid7133112006-04-10 22:54:55 -0700796 spin_lock(&fc->lock);
Miklos Szeredi7025d9a2006-04-10 22:54:50 -0700797 if (!fc->connected)
798 mask = POLLERR;
799 else if (!list_empty(&fc->pending))
800 mask |= POLLIN | POLLRDNORM;
Miklos Szeredid7133112006-04-10 22:54:55 -0700801 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700802
803 return mask;
804}
805
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800806/*
807 * Abort all requests on the given list (pending or processing)
808 *
Miklos Szeredid7133112006-04-10 22:54:55 -0700809 * This function releases and reacquires fc->lock
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800810 */
Miklos Szeredi334f4852005-09-09 13:10:27 -0700811static void end_requests(struct fuse_conn *fc, struct list_head *head)
812{
813 while (!list_empty(head)) {
814 struct fuse_req *req;
815 req = list_entry(head->next, struct fuse_req, list);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700816 req->out.h.error = -ECONNABORTED;
817 request_end(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700818 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700819 }
820}
821
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800822/*
823 * Abort requests under I/O
824 *
825 * The requests are set to interrupted and finished, and the request
826 * waiter is woken up. This will make request_wait_answer() wait
827 * until the request is unlocked and then return.
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800828 *
829 * If the request is asynchronous, then the end function needs to be
830 * called after waiting for the request to be unlocked (if it was
831 * locked).
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800832 */
833static void end_io_requests(struct fuse_conn *fc)
834{
835 while (!list_empty(&fc->io)) {
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800836 struct fuse_req *req =
837 list_entry(fc->io.next, struct fuse_req, list);
838 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
839
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800840 req->interrupted = 1;
841 req->out.h.error = -ECONNABORTED;
842 req->state = FUSE_REQ_FINISHED;
843 list_del_init(&req->list);
844 wake_up(&req->waitq);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800845 if (end) {
846 req->end = NULL;
847 /* The end function will consume this reference */
848 __fuse_get_request(req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700849 spin_unlock(&fc->lock);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800850 wait_event(req->waitq, !req->locked);
851 end(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700852 spin_lock(&fc->lock);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800853 }
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800854 }
855}
856
857/*
858 * Abort all requests.
859 *
860 * Emergency exit in case of a malicious or accidental deadlock, or
861 * just a hung filesystem.
862 *
863 * The same effect is usually achievable through killing the
864 * filesystem daemon and all users of the filesystem. The exception
865 * is the combination of an asynchronous request and the tricky
866 * deadlock (see Documentation/filesystems/fuse.txt).
867 *
868 * During the aborting, progression of requests from the pending and
869 * processing lists onto the io list, and progression of new requests
870 * onto the pending list is prevented by req->connected being false.
871 *
872 * Progression of requests under I/O to the processing list is
873 * prevented by the req->interrupted flag being true for these
874 * requests. For this reason requests on the io list must be aborted
875 * first.
876 */
877void fuse_abort_conn(struct fuse_conn *fc)
878{
Miklos Szeredid7133112006-04-10 22:54:55 -0700879 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800880 if (fc->connected) {
881 fc->connected = 0;
882 end_io_requests(fc);
883 end_requests(fc, &fc->pending);
884 end_requests(fc, &fc->processing);
885 wake_up_all(&fc->waitq);
Jeff Dike385a17b2006-04-10 22:54:52 -0700886 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800887 }
Miklos Szeredid7133112006-04-10 22:54:55 -0700888 spin_unlock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800889}
890
Miklos Szeredi334f4852005-09-09 13:10:27 -0700891static int fuse_dev_release(struct inode *inode, struct file *file)
892{
Miklos Szeredi0720b312006-04-10 22:54:55 -0700893 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700894 if (fc) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700895 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700896 fc->connected = 0;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700897 end_requests(fc, &fc->pending);
898 end_requests(fc, &fc->processing);
Miklos Szeredid7133112006-04-10 22:54:55 -0700899 spin_unlock(&fc->lock);
Jeff Dike385a17b2006-04-10 22:54:52 -0700900 fasync_helper(-1, file, 0, &fc->fasync);
Miklos Szeredif543f252006-01-16 22:14:35 -0800901 kobject_put(&fc->kobj);
Jeff Dike385a17b2006-04-10 22:54:52 -0700902 }
Miklos Szeredif543f252006-01-16 22:14:35 -0800903
Miklos Szeredi334f4852005-09-09 13:10:27 -0700904 return 0;
905}
906
Jeff Dike385a17b2006-04-10 22:54:52 -0700907static int fuse_dev_fasync(int fd, struct file *file, int on)
908{
909 struct fuse_conn *fc = fuse_get_conn(file);
910 if (!fc)
Miklos Szeredia87046d2006-04-10 22:54:56 -0700911 return -EPERM;
Jeff Dike385a17b2006-04-10 22:54:52 -0700912
913 /* No locking - fasync_helper does its own locking */
914 return fasync_helper(fd, file, on, &fc->fasync);
915}
916
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800917const struct file_operations fuse_dev_operations = {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700918 .owner = THIS_MODULE,
919 .llseek = no_llseek,
920 .read = fuse_dev_read,
921 .readv = fuse_dev_readv,
922 .write = fuse_dev_write,
923 .writev = fuse_dev_writev,
924 .poll = fuse_dev_poll,
925 .release = fuse_dev_release,
Jeff Dike385a17b2006-04-10 22:54:52 -0700926 .fasync = fuse_dev_fasync,
Miklos Szeredi334f4852005-09-09 13:10:27 -0700927};
928
929static struct miscdevice fuse_miscdevice = {
930 .minor = FUSE_MINOR,
931 .name = "fuse",
932 .fops = &fuse_dev_operations,
933};
934
935int __init fuse_dev_init(void)
936{
937 int err = -ENOMEM;
938 fuse_req_cachep = kmem_cache_create("fuse_request",
939 sizeof(struct fuse_req),
940 0, 0, NULL, NULL);
941 if (!fuse_req_cachep)
942 goto out;
943
944 err = misc_register(&fuse_miscdevice);
945 if (err)
946 goto out_cache_clean;
947
948 return 0;
949
950 out_cache_clean:
951 kmem_cache_destroy(fuse_req_cachep);
952 out:
953 return err;
954}
955
956void fuse_dev_cleanup(void)
957{
958 misc_deregister(&fuse_miscdevice);
959 kmem_cache_destroy(fuse_req_cachep);
960}