blob: ff5e8bed5d88f8de73f36782221cc6aa3223f249 [file] [log] [blame]
Miklos Szeredi334f4852005-09-09 13:10:27 -07001/*
2 FUSE: Filesystem in Userspace
Miklos Szeredi1729a162008-11-26 12:03:54 +01003 Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu>
Miklos Szeredi334f4852005-09-09 13:10:27 -07004
5 This program can be distributed under the terms of the GNU GPL.
6 See the file COPYING.
7*/
8
9#include "fuse_i.h"
10
11#include <linux/init.h>
12#include <linux/module.h>
13#include <linux/poll.h>
14#include <linux/uio.h>
15#include <linux/miscdevice.h>
16#include <linux/pagemap.h>
17#include <linux/file.h>
18#include <linux/slab.h>
Miklos Szeredidd3bb142010-05-25 15:06:06 +020019#include <linux/pipe_fs_i.h>
Miklos Szeredice534fb2010-05-25 15:06:07 +020020#include <linux/swap.h>
21#include <linux/splice.h>
Miklos Szeredi334f4852005-09-09 13:10:27 -070022
23MODULE_ALIAS_MISCDEV(FUSE_MINOR);
Kay Sievers578454f2010-05-20 18:07:20 +020024MODULE_ALIAS("devname:fuse");
Miklos Szeredi334f4852005-09-09 13:10:27 -070025
Christoph Lametere18b8902006-12-06 20:33:20 -080026static struct kmem_cache *fuse_req_cachep;
Miklos Szeredi334f4852005-09-09 13:10:27 -070027
Miklos Szeredi8bfc0162006-01-16 22:14:28 -080028static struct fuse_conn *fuse_get_conn(struct file *file)
Miklos Szeredi334f4852005-09-09 13:10:27 -070029{
Miklos Szeredi0720b312006-04-10 22:54:55 -070030 /*
31 * Lockless access is OK, because file->private data is set
32 * once during mount and is valid until the file is released.
33 */
34 return file->private_data;
Miklos Szeredi334f4852005-09-09 13:10:27 -070035}
36
Maxim Patlasov4250c062012-10-26 19:48:07 +040037static void fuse_request_init(struct fuse_req *req, struct page **pages,
38 unsigned npages)
Miklos Szeredi334f4852005-09-09 13:10:27 -070039{
40 memset(req, 0, sizeof(*req));
Maxim Patlasov4250c062012-10-26 19:48:07 +040041 memset(pages, 0, sizeof(*pages) * npages);
Miklos Szeredi334f4852005-09-09 13:10:27 -070042 INIT_LIST_HEAD(&req->list);
Miklos Szeredia4d27e72006-06-25 05:48:54 -070043 INIT_LIST_HEAD(&req->intr_entry);
Miklos Szeredi334f4852005-09-09 13:10:27 -070044 init_waitqueue_head(&req->waitq);
45 atomic_set(&req->count, 1);
Maxim Patlasov4250c062012-10-26 19:48:07 +040046 req->pages = pages;
47 req->max_pages = npages;
Miklos Szeredi334f4852005-09-09 13:10:27 -070048}
49
Maxim Patlasov4250c062012-10-26 19:48:07 +040050static struct fuse_req *__fuse_request_alloc(unsigned npages, gfp_t flags)
Miklos Szeredi334f4852005-09-09 13:10:27 -070051{
Maxim Patlasov4250c062012-10-26 19:48:07 +040052 struct fuse_req *req = kmem_cache_alloc(fuse_req_cachep, flags);
53 if (req) {
54 struct page **pages;
55
56 if (npages <= FUSE_REQ_INLINE_PAGES)
57 pages = req->inline_pages;
58 else
59 pages = kmalloc(sizeof(struct page *) * npages, flags);
60
61 if (!pages) {
62 kmem_cache_free(fuse_req_cachep, req);
63 return NULL;
64 }
65
66 fuse_request_init(req, pages, npages);
67 }
Miklos Szeredi334f4852005-09-09 13:10:27 -070068 return req;
69}
Maxim Patlasov4250c062012-10-26 19:48:07 +040070
71struct fuse_req *fuse_request_alloc(unsigned npages)
72{
73 return __fuse_request_alloc(npages, GFP_KERNEL);
74}
Tejun Heo08cbf542009-04-14 10:54:53 +090075EXPORT_SYMBOL_GPL(fuse_request_alloc);
Miklos Szeredi334f4852005-09-09 13:10:27 -070076
Maxim Patlasov4250c062012-10-26 19:48:07 +040077struct fuse_req *fuse_request_alloc_nofs(unsigned npages)
Miklos Szeredi3be5a522008-04-30 00:54:41 -070078{
Maxim Patlasov4250c062012-10-26 19:48:07 +040079 return __fuse_request_alloc(npages, GFP_NOFS);
Miklos Szeredi3be5a522008-04-30 00:54:41 -070080}
81
Miklos Szeredi334f4852005-09-09 13:10:27 -070082void fuse_request_free(struct fuse_req *req)
83{
Maxim Patlasov4250c062012-10-26 19:48:07 +040084 if (req->pages != req->inline_pages)
85 kfree(req->pages);
Miklos Szeredi334f4852005-09-09 13:10:27 -070086 kmem_cache_free(fuse_req_cachep, req);
87}
88
Miklos Szeredi8bfc0162006-01-16 22:14:28 -080089static void block_sigs(sigset_t *oldset)
Miklos Szeredi334f4852005-09-09 13:10:27 -070090{
91 sigset_t mask;
92
93 siginitsetinv(&mask, sigmask(SIGKILL));
94 sigprocmask(SIG_BLOCK, &mask, oldset);
95}
96
Miklos Szeredi8bfc0162006-01-16 22:14:28 -080097static void restore_sigs(sigset_t *oldset)
Miklos Szeredi334f4852005-09-09 13:10:27 -070098{
99 sigprocmask(SIG_SETMASK, oldset, NULL);
100}
101
Miklos Szeredi334f4852005-09-09 13:10:27 -0700102static void __fuse_get_request(struct fuse_req *req)
103{
104 atomic_inc(&req->count);
105}
106
107/* Must be called with > 1 refcount */
108static void __fuse_put_request(struct fuse_req *req)
109{
110 BUG_ON(atomic_read(&req->count) < 2);
111 atomic_dec(&req->count);
112}
113
Miklos Szeredi33649c92006-06-25 05:48:52 -0700114static void fuse_req_init_context(struct fuse_req *req)
115{
Eric W. Biederman499dcf22012-02-07 16:26:03 -0800116 req->in.h.uid = from_kuid_munged(&init_user_ns, current_fsuid());
117 req->in.h.gid = from_kgid_munged(&init_user_ns, current_fsgid());
Miklos Szeredi33649c92006-06-25 05:48:52 -0700118 req->in.h.pid = current->pid;
119}
120
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400121struct fuse_req *fuse_get_req(struct fuse_conn *fc, unsigned npages)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700122{
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700123 struct fuse_req *req;
124 sigset_t oldset;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200125 int intr;
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700126 int err;
127
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200128 atomic_inc(&fc->num_waiting);
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700129 block_sigs(&oldset);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200130 intr = wait_event_interruptible(fc->blocked_waitq, !fc->blocked);
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700131 restore_sigs(&oldset);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200132 err = -EINTR;
133 if (intr)
134 goto out;
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700135
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700136 err = -ENOTCONN;
137 if (!fc->connected)
138 goto out;
139
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400140 req = fuse_request_alloc(npages);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200141 err = -ENOMEM;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700142 if (!req)
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200143 goto out;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700144
Miklos Szeredi33649c92006-06-25 05:48:52 -0700145 fuse_req_init_context(req);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200146 req->waiting = 1;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700147 return req;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200148
149 out:
150 atomic_dec(&fc->num_waiting);
151 return ERR_PTR(err);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700152}
Tejun Heo08cbf542009-04-14 10:54:53 +0900153EXPORT_SYMBOL_GPL(fuse_get_req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700154
Miklos Szeredi33649c92006-06-25 05:48:52 -0700155/*
156 * Return request in fuse_file->reserved_req. However that may
157 * currently be in use. If that is the case, wait for it to become
158 * available.
159 */
160static struct fuse_req *get_reserved_req(struct fuse_conn *fc,
161 struct file *file)
162{
163 struct fuse_req *req = NULL;
164 struct fuse_file *ff = file->private_data;
165
166 do {
Miklos Szeredide5e3de2007-10-16 23:31:00 -0700167 wait_event(fc->reserved_req_waitq, ff->reserved_req);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700168 spin_lock(&fc->lock);
169 if (ff->reserved_req) {
170 req = ff->reserved_req;
171 ff->reserved_req = NULL;
Al Virocb0942b2012-08-27 14:48:26 -0400172 req->stolen_file = get_file(file);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700173 }
174 spin_unlock(&fc->lock);
175 } while (!req);
176
177 return req;
178}
179
180/*
181 * Put stolen request back into fuse_file->reserved_req
182 */
183static void put_reserved_req(struct fuse_conn *fc, struct fuse_req *req)
184{
185 struct file *file = req->stolen_file;
186 struct fuse_file *ff = file->private_data;
187
188 spin_lock(&fc->lock);
Maxim Patlasov4250c062012-10-26 19:48:07 +0400189 fuse_request_init(req, req->pages, req->max_pages);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700190 BUG_ON(ff->reserved_req);
191 ff->reserved_req = req;
Miklos Szeredide5e3de2007-10-16 23:31:00 -0700192 wake_up_all(&fc->reserved_req_waitq);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700193 spin_unlock(&fc->lock);
194 fput(file);
195}
196
197/*
198 * Gets a requests for a file operation, always succeeds
199 *
200 * This is used for sending the FLUSH request, which must get to
201 * userspace, due to POSIX locks which may need to be unlocked.
202 *
203 * If allocation fails due to OOM, use the reserved request in
204 * fuse_file.
205 *
206 * This is very unlikely to deadlock accidentally, since the
207 * filesystem should not have it's own file open. If deadlock is
208 * intentional, it can still be broken by "aborting" the filesystem.
209 */
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400210struct fuse_req *fuse_get_req_nofail_nopages(struct fuse_conn *fc,
211 struct file *file)
Miklos Szeredi33649c92006-06-25 05:48:52 -0700212{
213 struct fuse_req *req;
214
215 atomic_inc(&fc->num_waiting);
216 wait_event(fc->blocked_waitq, !fc->blocked);
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400217 req = fuse_request_alloc(0);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700218 if (!req)
219 req = get_reserved_req(fc, file);
220
221 fuse_req_init_context(req);
222 req->waiting = 1;
223 return req;
224}
225
Miklos Szeredi334f4852005-09-09 13:10:27 -0700226void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req)
227{
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800228 if (atomic_dec_and_test(&req->count)) {
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200229 if (req->waiting)
230 atomic_dec(&fc->num_waiting);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700231
232 if (req->stolen_file)
233 put_reserved_req(fc, req);
234 else
235 fuse_request_free(req);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800236 }
237}
Tejun Heo08cbf542009-04-14 10:54:53 +0900238EXPORT_SYMBOL_GPL(fuse_put_request);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800239
Miklos Szeredid12def12008-02-06 01:38:39 -0800240static 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 u64 fuse_get_unique(struct fuse_conn *fc)
252{
253 fc->reqctr++;
254 /* zero is special */
255 if (fc->reqctr == 0)
256 fc->reqctr = 1;
257
258 return fc->reqctr;
259}
260
261static void queue_request(struct fuse_conn *fc, struct fuse_req *req)
262{
Miklos Szeredid12def12008-02-06 01:38:39 -0800263 req->in.h.len = sizeof(struct fuse_in_header) +
264 len_args(req->in.numargs, (struct fuse_arg *) req->in.args);
265 list_add_tail(&req->list, &fc->pending);
266 req->state = FUSE_REQ_PENDING;
267 if (!req->waiting) {
268 req->waiting = 1;
269 atomic_inc(&fc->num_waiting);
270 }
271 wake_up(&fc->waitq);
272 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
273}
274
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100275void fuse_queue_forget(struct fuse_conn *fc, struct fuse_forget_link *forget,
276 u64 nodeid, u64 nlookup)
277{
Miklos Szeredi02c048b2010-12-07 20:16:56 +0100278 forget->forget_one.nodeid = nodeid;
279 forget->forget_one.nlookup = nlookup;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100280
281 spin_lock(&fc->lock);
Miklos Szeredi5dfcc872011-09-12 09:38:03 +0200282 if (fc->connected) {
283 fc->forget_list_tail->next = forget;
284 fc->forget_list_tail = forget;
285 wake_up(&fc->waitq);
286 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
287 } else {
288 kfree(forget);
289 }
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100290 spin_unlock(&fc->lock);
291}
292
Miklos Szeredid12def12008-02-06 01:38:39 -0800293static void flush_bg_queue(struct fuse_conn *fc)
294{
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700295 while (fc->active_background < fc->max_background &&
Miklos Szeredid12def12008-02-06 01:38:39 -0800296 !list_empty(&fc->bg_queue)) {
297 struct fuse_req *req;
298
299 req = list_entry(fc->bg_queue.next, struct fuse_req, list);
300 list_del(&req->list);
301 fc->active_background++;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200302 req->in.h.unique = fuse_get_unique(fc);
Miklos Szeredid12def12008-02-06 01:38:39 -0800303 queue_request(fc, req);
304 }
305}
306
Miklos Szeredi6dbbcb12006-04-26 10:49:06 +0200307/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700308 * This function is called when a request is finished. Either a reply
Miklos Szeredif9a28422006-06-25 05:48:53 -0700309 * has arrived or it was aborted (and not yet sent) or some error
Miklos Szeredif43b1552006-01-16 22:14:26 -0800310 * occurred during communication with userspace, or the device file
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700311 * was closed. The requester thread is woken up (if still waiting),
312 * the 'end' callback is called if given, else the reference to the
313 * request is released
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800314 *
Miklos Szeredid7133112006-04-10 22:54:55 -0700315 * Called with fc->lock, unlocks it
Miklos Szeredi334f4852005-09-09 13:10:27 -0700316 */
317static void request_end(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +0200318__releases(fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700319{
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700320 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
321 req->end = NULL;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800322 list_del(&req->list);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700323 list_del(&req->intr_entry);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800324 req->state = FUSE_REQ_FINISHED;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700325 if (req->background) {
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700326 if (fc->num_background == fc->max_background) {
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700327 fc->blocked = 0;
328 wake_up_all(&fc->blocked_waitq);
329 }
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700330 if (fc->num_background == fc->congestion_threshold &&
Tejun Heoa325f9b2009-04-14 10:54:52 +0900331 fc->connected && fc->bdi_initialized) {
Jens Axboe8aa7e842009-07-09 14:52:32 +0200332 clear_bdi_congested(&fc->bdi, BLK_RW_SYNC);
333 clear_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
Miklos Szeredif92b99b2007-10-16 23:30:59 -0700334 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700335 fc->num_background--;
Miklos Szeredid12def12008-02-06 01:38:39 -0800336 fc->active_background--;
337 flush_bg_queue(fc);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700338 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700339 spin_unlock(&fc->lock);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700340 wake_up(&req->waitq);
341 if (end)
342 end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100343 fuse_put_request(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700344}
345
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700346static void wait_answer_interruptible(struct fuse_conn *fc,
347 struct fuse_req *req)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +0200348__releases(fc->lock)
349__acquires(fc->lock)
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700350{
351 if (signal_pending(current))
352 return;
353
354 spin_unlock(&fc->lock);
355 wait_event_interruptible(req->waitq, req->state == FUSE_REQ_FINISHED);
356 spin_lock(&fc->lock);
357}
358
359static void queue_interrupt(struct fuse_conn *fc, struct fuse_req *req)
360{
361 list_add_tail(&req->intr_entry, &fc->interrupts);
362 wake_up(&fc->waitq);
363 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
364}
365
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700366static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +0200367__releases(fc->lock)
368__acquires(fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700369{
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700370 if (!fc->no_interrupt) {
371 /* Any signal may interrupt this */
372 wait_answer_interruptible(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700373
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700374 if (req->aborted)
375 goto aborted;
376 if (req->state == FUSE_REQ_FINISHED)
377 return;
378
379 req->interrupted = 1;
380 if (req->state == FUSE_REQ_SENT)
381 queue_interrupt(fc, req);
382 }
383
Miklos Szeredia131de02007-10-16 23:31:04 -0700384 if (!req->force) {
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700385 sigset_t oldset;
386
387 /* Only fatal signals may interrupt this */
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700388 block_sigs(&oldset);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700389 wait_answer_interruptible(fc, req);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700390 restore_sigs(&oldset);
Miklos Szeredia131de02007-10-16 23:31:04 -0700391
392 if (req->aborted)
393 goto aborted;
394 if (req->state == FUSE_REQ_FINISHED)
395 return;
396
397 /* Request is not yet in userspace, bail out */
398 if (req->state == FUSE_REQ_PENDING) {
399 list_del(&req->list);
400 __fuse_put_request(req);
401 req->out.h.error = -EINTR;
402 return;
403 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700404 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700405
Miklos Szeredia131de02007-10-16 23:31:04 -0700406 /*
407 * Either request is already in userspace, or it was forced.
408 * Wait it out.
409 */
410 spin_unlock(&fc->lock);
411 wait_event(req->waitq, req->state == FUSE_REQ_FINISHED);
412 spin_lock(&fc->lock);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700413
Miklos Szeredia131de02007-10-16 23:31:04 -0700414 if (!req->aborted)
415 return;
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700416
417 aborted:
Miklos Szeredia131de02007-10-16 23:31:04 -0700418 BUG_ON(req->state != FUSE_REQ_FINISHED);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700419 if (req->locked) {
420 /* This is uninterruptible sleep, because data is
421 being copied to/from the buffers of req. During
422 locked state, there mustn't be any filesystem
423 operation (e.g. page fault), since that could lead
424 to deadlock */
Miklos Szeredid7133112006-04-10 22:54:55 -0700425 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700426 wait_event(req->waitq, !req->locked);
Miklos Szeredid7133112006-04-10 22:54:55 -0700427 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700428 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700429}
430
Tejun Heob93f8582008-11-26 12:03:55 +0100431void fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700432{
433 req->isreply = 1;
Miklos Szeredid7133112006-04-10 22:54:55 -0700434 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700435 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700436 req->out.h.error = -ENOTCONN;
437 else if (fc->conn_error)
438 req->out.h.error = -ECONNREFUSED;
439 else {
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200440 req->in.h.unique = fuse_get_unique(fc);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700441 queue_request(fc, req);
442 /* acquire extra reference, since request is still needed
443 after request_end() */
444 __fuse_get_request(req);
445
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700446 request_wait_answer(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700447 }
Miklos Szeredid7133112006-04-10 22:54:55 -0700448 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700449}
Tejun Heo08cbf542009-04-14 10:54:53 +0900450EXPORT_SYMBOL_GPL(fuse_request_send);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700451
Tejun Heob93f8582008-11-26 12:03:55 +0100452static void fuse_request_send_nowait_locked(struct fuse_conn *fc,
453 struct fuse_req *req)
Miklos Szeredid12def12008-02-06 01:38:39 -0800454{
455 req->background = 1;
456 fc->num_background++;
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700457 if (fc->num_background == fc->max_background)
Miklos Szeredid12def12008-02-06 01:38:39 -0800458 fc->blocked = 1;
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700459 if (fc->num_background == fc->congestion_threshold &&
Tejun Heoa325f9b2009-04-14 10:54:52 +0900460 fc->bdi_initialized) {
Jens Axboe8aa7e842009-07-09 14:52:32 +0200461 set_bdi_congested(&fc->bdi, BLK_RW_SYNC);
462 set_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
Miklos Szeredid12def12008-02-06 01:38:39 -0800463 }
464 list_add_tail(&req->list, &fc->bg_queue);
465 flush_bg_queue(fc);
466}
467
Tejun Heob93f8582008-11-26 12:03:55 +0100468static void fuse_request_send_nowait(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700469{
Miklos Szeredid7133112006-04-10 22:54:55 -0700470 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700471 if (fc->connected) {
Tejun Heob93f8582008-11-26 12:03:55 +0100472 fuse_request_send_nowait_locked(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700473 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700474 } else {
475 req->out.h.error = -ENOTCONN;
476 request_end(fc, req);
477 }
478}
479
Tejun Heob93f8582008-11-26 12:03:55 +0100480void fuse_request_send_background(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700481{
482 req->isreply = 1;
Tejun Heob93f8582008-11-26 12:03:55 +0100483 fuse_request_send_nowait(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700484}
Tejun Heo08cbf542009-04-14 10:54:53 +0900485EXPORT_SYMBOL_GPL(fuse_request_send_background);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700486
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200487static int fuse_request_send_notify_reply(struct fuse_conn *fc,
488 struct fuse_req *req, u64 unique)
489{
490 int err = -ENODEV;
491
492 req->isreply = 0;
493 req->in.h.unique = unique;
494 spin_lock(&fc->lock);
495 if (fc->connected) {
496 queue_request(fc, req);
497 err = 0;
498 }
499 spin_unlock(&fc->lock);
500
501 return err;
502}
503
Miklos Szeredi334f4852005-09-09 13:10:27 -0700504/*
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700505 * Called under fc->lock
506 *
507 * fc->connected must have been checked previously
508 */
Tejun Heob93f8582008-11-26 12:03:55 +0100509void fuse_request_send_background_locked(struct fuse_conn *fc,
510 struct fuse_req *req)
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700511{
512 req->isreply = 1;
Tejun Heob93f8582008-11-26 12:03:55 +0100513 fuse_request_send_nowait_locked(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700514}
515
Anand V. Avati0b05b182012-08-19 08:53:23 -0400516void fuse_force_forget(struct file *file, u64 nodeid)
517{
518 struct inode *inode = file->f_path.dentry->d_inode;
519 struct fuse_conn *fc = get_fuse_conn(inode);
520 struct fuse_req *req;
521 struct fuse_forget_in inarg;
522
523 memset(&inarg, 0, sizeof(inarg));
524 inarg.nlookup = 1;
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400525 req = fuse_get_req_nofail_nopages(fc, file);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400526 req->in.h.opcode = FUSE_FORGET;
527 req->in.h.nodeid = nodeid;
528 req->in.numargs = 1;
529 req->in.args[0].size = sizeof(inarg);
530 req->in.args[0].value = &inarg;
531 req->isreply = 0;
532 fuse_request_send_nowait(fc, req);
533}
534
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700535/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700536 * Lock the request. Up to the next unlock_request() there mustn't be
537 * anything that could cause a page-fault. If the request was already
Miklos Szeredif9a28422006-06-25 05:48:53 -0700538 * aborted bail out.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700539 */
Miklos Szeredid7133112006-04-10 22:54:55 -0700540static int lock_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700541{
542 int err = 0;
543 if (req) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700544 spin_lock(&fc->lock);
Miklos Szeredif9a28422006-06-25 05:48:53 -0700545 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700546 err = -ENOENT;
547 else
548 req->locked = 1;
Miklos Szeredid7133112006-04-10 22:54:55 -0700549 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700550 }
551 return err;
552}
553
554/*
Miklos Szeredif9a28422006-06-25 05:48:53 -0700555 * Unlock request. If it was aborted during being locked, the
Miklos Szeredi334f4852005-09-09 13:10:27 -0700556 * requester thread is currently waiting for it to be unlocked, so
557 * wake it up.
558 */
Miklos Szeredid7133112006-04-10 22:54:55 -0700559static void unlock_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700560{
561 if (req) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700562 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700563 req->locked = 0;
Miklos Szeredif9a28422006-06-25 05:48:53 -0700564 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700565 wake_up(&req->waitq);
Miklos Szeredid7133112006-04-10 22:54:55 -0700566 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700567 }
568}
569
570struct fuse_copy_state {
Miklos Szeredid7133112006-04-10 22:54:55 -0700571 struct fuse_conn *fc;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700572 int write;
573 struct fuse_req *req;
574 const struct iovec *iov;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200575 struct pipe_buffer *pipebufs;
576 struct pipe_buffer *currbuf;
577 struct pipe_inode_info *pipe;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700578 unsigned long nr_segs;
579 unsigned long seglen;
580 unsigned long addr;
581 struct page *pg;
582 void *mapaddr;
583 void *buf;
584 unsigned len;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200585 unsigned move_pages:1;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700586};
587
Miklos Szeredid7133112006-04-10 22:54:55 -0700588static void fuse_copy_init(struct fuse_copy_state *cs, struct fuse_conn *fc,
Miklos Szeredic3021622010-05-25 15:06:07 +0200589 int write,
Miklos Szeredid7133112006-04-10 22:54:55 -0700590 const struct iovec *iov, unsigned long nr_segs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700591{
592 memset(cs, 0, sizeof(*cs));
Miklos Szeredid7133112006-04-10 22:54:55 -0700593 cs->fc = fc;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700594 cs->write = write;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700595 cs->iov = iov;
596 cs->nr_segs = nr_segs;
597}
598
599/* Unmap and put previous page of userspace buffer */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800600static void fuse_copy_finish(struct fuse_copy_state *cs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700601{
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200602 if (cs->currbuf) {
603 struct pipe_buffer *buf = cs->currbuf;
604
Miklos Szeredic3021622010-05-25 15:06:07 +0200605 if (!cs->write) {
606 buf->ops->unmap(cs->pipe, buf, cs->mapaddr);
607 } else {
Miklos Szeredi7909b1c2010-07-12 14:41:40 +0200608 kunmap(buf->page);
Miklos Szeredic3021622010-05-25 15:06:07 +0200609 buf->len = PAGE_SIZE - cs->len;
610 }
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200611 cs->currbuf = NULL;
612 cs->mapaddr = NULL;
613 } else if (cs->mapaddr) {
Miklos Szeredi7909b1c2010-07-12 14:41:40 +0200614 kunmap(cs->pg);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700615 if (cs->write) {
616 flush_dcache_page(cs->pg);
617 set_page_dirty_lock(cs->pg);
618 }
619 put_page(cs->pg);
620 cs->mapaddr = NULL;
621 }
622}
623
624/*
625 * Get another pagefull of userspace buffer, and map it to kernel
626 * address space, and lock request
627 */
628static int fuse_copy_fill(struct fuse_copy_state *cs)
629{
630 unsigned long offset;
631 int err;
632
Miklos Szeredid7133112006-04-10 22:54:55 -0700633 unlock_request(cs->fc, cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700634 fuse_copy_finish(cs);
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200635 if (cs->pipebufs) {
636 struct pipe_buffer *buf = cs->pipebufs;
637
Miklos Szeredic3021622010-05-25 15:06:07 +0200638 if (!cs->write) {
639 err = buf->ops->confirm(cs->pipe, buf);
640 if (err)
641 return err;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200642
Miklos Szeredic3021622010-05-25 15:06:07 +0200643 BUG_ON(!cs->nr_segs);
644 cs->currbuf = buf;
Miklos Szeredi7909b1c2010-07-12 14:41:40 +0200645 cs->mapaddr = buf->ops->map(cs->pipe, buf, 0);
Miklos Szeredic3021622010-05-25 15:06:07 +0200646 cs->len = buf->len;
647 cs->buf = cs->mapaddr + buf->offset;
648 cs->pipebufs++;
649 cs->nr_segs--;
650 } else {
651 struct page *page;
652
653 if (cs->nr_segs == cs->pipe->buffers)
654 return -EIO;
655
656 page = alloc_page(GFP_HIGHUSER);
657 if (!page)
658 return -ENOMEM;
659
660 buf->page = page;
661 buf->offset = 0;
662 buf->len = 0;
663
664 cs->currbuf = buf;
Miklos Szeredi7909b1c2010-07-12 14:41:40 +0200665 cs->mapaddr = kmap(page);
Miklos Szeredic3021622010-05-25 15:06:07 +0200666 cs->buf = cs->mapaddr;
667 cs->len = PAGE_SIZE;
668 cs->pipebufs++;
669 cs->nr_segs++;
670 }
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200671 } else {
672 if (!cs->seglen) {
673 BUG_ON(!cs->nr_segs);
674 cs->seglen = cs->iov[0].iov_len;
675 cs->addr = (unsigned long) cs->iov[0].iov_base;
676 cs->iov++;
677 cs->nr_segs--;
678 }
679 err = get_user_pages_fast(cs->addr, 1, cs->write, &cs->pg);
680 if (err < 0)
681 return err;
682 BUG_ON(err != 1);
683 offset = cs->addr % PAGE_SIZE;
Miklos Szeredi7909b1c2010-07-12 14:41:40 +0200684 cs->mapaddr = kmap(cs->pg);
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200685 cs->buf = cs->mapaddr + offset;
686 cs->len = min(PAGE_SIZE - offset, cs->seglen);
687 cs->seglen -= cs->len;
688 cs->addr += cs->len;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700689 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700690
Miklos Szeredid7133112006-04-10 22:54:55 -0700691 return lock_request(cs->fc, cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700692}
693
694/* Do as much copy to/from userspace buffer as we can */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800695static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700696{
697 unsigned ncpy = min(*size, cs->len);
698 if (val) {
699 if (cs->write)
700 memcpy(cs->buf, *val, ncpy);
701 else
702 memcpy(*val, cs->buf, ncpy);
703 *val += ncpy;
704 }
705 *size -= ncpy;
706 cs->len -= ncpy;
707 cs->buf += ncpy;
708 return ncpy;
709}
710
Miklos Szeredice534fb2010-05-25 15:06:07 +0200711static int fuse_check_page(struct page *page)
712{
713 if (page_mapcount(page) ||
714 page->mapping != NULL ||
715 page_count(page) != 1 ||
716 (page->flags & PAGE_FLAGS_CHECK_AT_PREP &
717 ~(1 << PG_locked |
718 1 << PG_referenced |
719 1 << PG_uptodate |
720 1 << PG_lru |
721 1 << PG_active |
722 1 << PG_reclaim))) {
723 printk(KERN_WARNING "fuse: trying to steal weird page\n");
724 printk(KERN_WARNING " page=%p index=%li flags=%08lx, count=%i, mapcount=%i, mapping=%p\n", page, page->index, page->flags, page_count(page), page_mapcount(page), page->mapping);
725 return 1;
726 }
727 return 0;
728}
729
730static int fuse_try_move_page(struct fuse_copy_state *cs, struct page **pagep)
731{
732 int err;
733 struct page *oldpage = *pagep;
734 struct page *newpage;
735 struct pipe_buffer *buf = cs->pipebufs;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200736
737 unlock_request(cs->fc, cs->req);
738 fuse_copy_finish(cs);
739
740 err = buf->ops->confirm(cs->pipe, buf);
741 if (err)
742 return err;
743
744 BUG_ON(!cs->nr_segs);
745 cs->currbuf = buf;
746 cs->len = buf->len;
747 cs->pipebufs++;
748 cs->nr_segs--;
749
750 if (cs->len != PAGE_SIZE)
751 goto out_fallback;
752
753 if (buf->ops->steal(cs->pipe, buf) != 0)
754 goto out_fallback;
755
756 newpage = buf->page;
757
758 if (WARN_ON(!PageUptodate(newpage)))
759 return -EIO;
760
761 ClearPageMappedToDisk(newpage);
762
763 if (fuse_check_page(newpage) != 0)
764 goto out_fallback_unlock;
765
Miklos Szeredice534fb2010-05-25 15:06:07 +0200766 /*
767 * This is a new and locked page, it shouldn't be mapped or
768 * have any special flags on it
769 */
770 if (WARN_ON(page_mapped(oldpage)))
771 goto out_fallback_unlock;
772 if (WARN_ON(page_has_private(oldpage)))
773 goto out_fallback_unlock;
774 if (WARN_ON(PageDirty(oldpage) || PageWriteback(oldpage)))
775 goto out_fallback_unlock;
776 if (WARN_ON(PageMlocked(oldpage)))
777 goto out_fallback_unlock;
778
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700779 err = replace_page_cache_page(oldpage, newpage, GFP_KERNEL);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200780 if (err) {
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700781 unlock_page(newpage);
782 return err;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200783 }
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700784
Miklos Szeredice534fb2010-05-25 15:06:07 +0200785 page_cache_get(newpage);
786
787 if (!(buf->flags & PIPE_BUF_FLAG_LRU))
788 lru_cache_add_file(newpage);
789
790 err = 0;
791 spin_lock(&cs->fc->lock);
792 if (cs->req->aborted)
793 err = -ENOENT;
794 else
795 *pagep = newpage;
796 spin_unlock(&cs->fc->lock);
797
798 if (err) {
799 unlock_page(newpage);
800 page_cache_release(newpage);
801 return err;
802 }
803
804 unlock_page(oldpage);
805 page_cache_release(oldpage);
806 cs->len = 0;
807
808 return 0;
809
810out_fallback_unlock:
811 unlock_page(newpage);
812out_fallback:
813 cs->mapaddr = buf->ops->map(cs->pipe, buf, 1);
814 cs->buf = cs->mapaddr + buf->offset;
815
816 err = lock_request(cs->fc, cs->req);
817 if (err)
818 return err;
819
820 return 1;
821}
822
Miklos Szeredic3021622010-05-25 15:06:07 +0200823static int fuse_ref_page(struct fuse_copy_state *cs, struct page *page,
824 unsigned offset, unsigned count)
825{
826 struct pipe_buffer *buf;
827
828 if (cs->nr_segs == cs->pipe->buffers)
829 return -EIO;
830
831 unlock_request(cs->fc, cs->req);
832 fuse_copy_finish(cs);
833
834 buf = cs->pipebufs;
835 page_cache_get(page);
836 buf->page = page;
837 buf->offset = offset;
838 buf->len = count;
839
840 cs->pipebufs++;
841 cs->nr_segs++;
842 cs->len = 0;
843
844 return 0;
845}
846
Miklos Szeredi334f4852005-09-09 13:10:27 -0700847/*
848 * Copy a page in the request to/from the userspace buffer. Must be
849 * done atomically
850 */
Miklos Szeredice534fb2010-05-25 15:06:07 +0200851static int fuse_copy_page(struct fuse_copy_state *cs, struct page **pagep,
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800852 unsigned offset, unsigned count, int zeroing)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700853{
Miklos Szeredice534fb2010-05-25 15:06:07 +0200854 int err;
855 struct page *page = *pagep;
856
Miklos Szeredib6777c42010-10-26 14:22:27 -0700857 if (page && zeroing && count < PAGE_SIZE)
858 clear_highpage(page);
859
Miklos Szeredi334f4852005-09-09 13:10:27 -0700860 while (count) {
Miklos Szeredic3021622010-05-25 15:06:07 +0200861 if (cs->write && cs->pipebufs && page) {
862 return fuse_ref_page(cs, page, offset, count);
863 } else if (!cs->len) {
Miklos Szeredice534fb2010-05-25 15:06:07 +0200864 if (cs->move_pages && page &&
865 offset == 0 && count == PAGE_SIZE) {
866 err = fuse_try_move_page(cs, pagep);
867 if (err <= 0)
868 return err;
869 } else {
870 err = fuse_copy_fill(cs);
871 if (err)
872 return err;
873 }
Miklos Szeredi1729a162008-11-26 12:03:54 +0100874 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700875 if (page) {
Cong Wang2408f6e2011-11-25 23:14:30 +0800876 void *mapaddr = kmap_atomic(page);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700877 void *buf = mapaddr + offset;
878 offset += fuse_copy_do(cs, &buf, &count);
Cong Wang2408f6e2011-11-25 23:14:30 +0800879 kunmap_atomic(mapaddr);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700880 } else
881 offset += fuse_copy_do(cs, NULL, &count);
882 }
883 if (page && !cs->write)
884 flush_dcache_page(page);
885 return 0;
886}
887
888/* Copy pages in the request to/from userspace buffer */
889static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
890 int zeroing)
891{
892 unsigned i;
893 struct fuse_req *req = cs->req;
894 unsigned offset = req->page_offset;
895 unsigned count = min(nbytes, (unsigned) PAGE_SIZE - offset);
896
897 for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
Miklos Szeredice534fb2010-05-25 15:06:07 +0200898 int err;
899
900 err = fuse_copy_page(cs, &req->pages[i], offset, count,
901 zeroing);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700902 if (err)
903 return err;
904
905 nbytes -= count;
906 count = min(nbytes, (unsigned) PAGE_SIZE);
907 offset = 0;
908 }
909 return 0;
910}
911
912/* Copy a single argument in the request to/from userspace buffer */
913static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
914{
915 while (size) {
Miklos Szeredi1729a162008-11-26 12:03:54 +0100916 if (!cs->len) {
917 int err = fuse_copy_fill(cs);
918 if (err)
919 return err;
920 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700921 fuse_copy_do(cs, &val, &size);
922 }
923 return 0;
924}
925
926/* Copy request arguments to/from userspace buffer */
927static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
928 unsigned argpages, struct fuse_arg *args,
929 int zeroing)
930{
931 int err = 0;
932 unsigned i;
933
934 for (i = 0; !err && i < numargs; i++) {
935 struct fuse_arg *arg = &args[i];
936 if (i == numargs - 1 && argpages)
937 err = fuse_copy_pages(cs, arg->size, zeroing);
938 else
939 err = fuse_copy_one(cs, arg->value, arg->size);
940 }
941 return err;
942}
943
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100944static int forget_pending(struct fuse_conn *fc)
945{
946 return fc->forget_list_head.next != NULL;
947}
948
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700949static int request_pending(struct fuse_conn *fc)
950{
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100951 return !list_empty(&fc->pending) || !list_empty(&fc->interrupts) ||
952 forget_pending(fc);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700953}
954
Miklos Szeredi334f4852005-09-09 13:10:27 -0700955/* Wait until a request is available on the pending list */
956static void request_wait(struct fuse_conn *fc)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +0200957__releases(fc->lock)
958__acquires(fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700959{
960 DECLARE_WAITQUEUE(wait, current);
961
962 add_wait_queue_exclusive(&fc->waitq, &wait);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700963 while (fc->connected && !request_pending(fc)) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700964 set_current_state(TASK_INTERRUPTIBLE);
965 if (signal_pending(current))
966 break;
967
Miklos Szeredid7133112006-04-10 22:54:55 -0700968 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700969 schedule();
Miklos Szeredid7133112006-04-10 22:54:55 -0700970 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700971 }
972 set_current_state(TASK_RUNNING);
973 remove_wait_queue(&fc->waitq, &wait);
974}
975
976/*
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700977 * Transfer an interrupt request to userspace
978 *
979 * Unlike other requests this is assembled on demand, without a need
980 * to allocate a separate fuse_req structure.
981 *
982 * Called with fc->lock held, releases it
983 */
Miklos Szeredic3021622010-05-25 15:06:07 +0200984static int fuse_read_interrupt(struct fuse_conn *fc, struct fuse_copy_state *cs,
985 size_t nbytes, struct fuse_req *req)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +0200986__releases(fc->lock)
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700987{
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700988 struct fuse_in_header ih;
989 struct fuse_interrupt_in arg;
990 unsigned reqsize = sizeof(ih) + sizeof(arg);
991 int err;
992
993 list_del_init(&req->intr_entry);
994 req->intr_unique = fuse_get_unique(fc);
995 memset(&ih, 0, sizeof(ih));
996 memset(&arg, 0, sizeof(arg));
997 ih.len = reqsize;
998 ih.opcode = FUSE_INTERRUPT;
999 ih.unique = req->intr_unique;
1000 arg.unique = req->in.h.unique;
1001
1002 spin_unlock(&fc->lock);
Miklos Szeredic3021622010-05-25 15:06:07 +02001003 if (nbytes < reqsize)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001004 return -EINVAL;
1005
Miklos Szeredic3021622010-05-25 15:06:07 +02001006 err = fuse_copy_one(cs, &ih, sizeof(ih));
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001007 if (!err)
Miklos Szeredic3021622010-05-25 15:06:07 +02001008 err = fuse_copy_one(cs, &arg, sizeof(arg));
1009 fuse_copy_finish(cs);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001010
1011 return err ? err : reqsize;
1012}
1013
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001014static struct fuse_forget_link *dequeue_forget(struct fuse_conn *fc,
1015 unsigned max,
1016 unsigned *countp)
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001017{
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001018 struct fuse_forget_link *head = fc->forget_list_head.next;
1019 struct fuse_forget_link **newhead = &head;
1020 unsigned count;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001021
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001022 for (count = 0; *newhead != NULL && count < max; count++)
1023 newhead = &(*newhead)->next;
1024
1025 fc->forget_list_head.next = *newhead;
1026 *newhead = NULL;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001027 if (fc->forget_list_head.next == NULL)
1028 fc->forget_list_tail = &fc->forget_list_head;
1029
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001030 if (countp != NULL)
1031 *countp = count;
1032
1033 return head;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001034}
1035
1036static int fuse_read_single_forget(struct fuse_conn *fc,
1037 struct fuse_copy_state *cs,
1038 size_t nbytes)
1039__releases(fc->lock)
1040{
1041 int err;
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001042 struct fuse_forget_link *forget = dequeue_forget(fc, 1, NULL);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001043 struct fuse_forget_in arg = {
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001044 .nlookup = forget->forget_one.nlookup,
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001045 };
1046 struct fuse_in_header ih = {
1047 .opcode = FUSE_FORGET,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001048 .nodeid = forget->forget_one.nodeid,
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001049 .unique = fuse_get_unique(fc),
1050 .len = sizeof(ih) + sizeof(arg),
1051 };
1052
1053 spin_unlock(&fc->lock);
1054 kfree(forget);
1055 if (nbytes < ih.len)
1056 return -EINVAL;
1057
1058 err = fuse_copy_one(cs, &ih, sizeof(ih));
1059 if (!err)
1060 err = fuse_copy_one(cs, &arg, sizeof(arg));
1061 fuse_copy_finish(cs);
1062
1063 if (err)
1064 return err;
1065
1066 return ih.len;
1067}
1068
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001069static int fuse_read_batch_forget(struct fuse_conn *fc,
1070 struct fuse_copy_state *cs, size_t nbytes)
1071__releases(fc->lock)
1072{
1073 int err;
1074 unsigned max_forgets;
1075 unsigned count;
1076 struct fuse_forget_link *head;
1077 struct fuse_batch_forget_in arg = { .count = 0 };
1078 struct fuse_in_header ih = {
1079 .opcode = FUSE_BATCH_FORGET,
1080 .unique = fuse_get_unique(fc),
1081 .len = sizeof(ih) + sizeof(arg),
1082 };
1083
1084 if (nbytes < ih.len) {
1085 spin_unlock(&fc->lock);
1086 return -EINVAL;
1087 }
1088
1089 max_forgets = (nbytes - ih.len) / sizeof(struct fuse_forget_one);
1090 head = dequeue_forget(fc, max_forgets, &count);
1091 spin_unlock(&fc->lock);
1092
1093 arg.count = count;
1094 ih.len += count * sizeof(struct fuse_forget_one);
1095 err = fuse_copy_one(cs, &ih, sizeof(ih));
1096 if (!err)
1097 err = fuse_copy_one(cs, &arg, sizeof(arg));
1098
1099 while (head) {
1100 struct fuse_forget_link *forget = head;
1101
1102 if (!err) {
1103 err = fuse_copy_one(cs, &forget->forget_one,
1104 sizeof(forget->forget_one));
1105 }
1106 head = forget->next;
1107 kfree(forget);
1108 }
1109
1110 fuse_copy_finish(cs);
1111
1112 if (err)
1113 return err;
1114
1115 return ih.len;
1116}
1117
1118static int fuse_read_forget(struct fuse_conn *fc, struct fuse_copy_state *cs,
1119 size_t nbytes)
1120__releases(fc->lock)
1121{
1122 if (fc->minor < 16 || fc->forget_list_head.next->next == NULL)
1123 return fuse_read_single_forget(fc, cs, nbytes);
1124 else
1125 return fuse_read_batch_forget(fc, cs, nbytes);
1126}
1127
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001128/*
Miklos Szeredi334f4852005-09-09 13:10:27 -07001129 * Read a single request into the userspace filesystem's buffer. This
1130 * function waits until a request is available, then removes it from
1131 * the pending list and copies request data to userspace buffer. If
Miklos Szeredif9a28422006-06-25 05:48:53 -07001132 * no reply is needed (FORGET) or request has been aborted or there
1133 * was an error during the copying then it's finished by calling
Miklos Szeredi334f4852005-09-09 13:10:27 -07001134 * request_end(). Otherwise add it to the processing list, and set
1135 * the 'sent' flag.
1136 */
Miklos Szeredic3021622010-05-25 15:06:07 +02001137static ssize_t fuse_dev_do_read(struct fuse_conn *fc, struct file *file,
1138 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001139{
1140 int err;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001141 struct fuse_req *req;
1142 struct fuse_in *in;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001143 unsigned reqsize;
1144
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001145 restart:
Miklos Szeredid7133112006-04-10 22:54:55 -07001146 spin_lock(&fc->lock);
Jeff Dikee5ac1d12006-04-10 22:54:53 -07001147 err = -EAGAIN;
1148 if ((file->f_flags & O_NONBLOCK) && fc->connected &&
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001149 !request_pending(fc))
Jeff Dikee5ac1d12006-04-10 22:54:53 -07001150 goto err_unlock;
1151
Miklos Szeredi334f4852005-09-09 13:10:27 -07001152 request_wait(fc);
1153 err = -ENODEV;
Miklos Szeredi9ba7cbb2006-01-16 22:14:34 -08001154 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001155 goto err_unlock;
1156 err = -ERESTARTSYS;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001157 if (!request_pending(fc))
Miklos Szeredi334f4852005-09-09 13:10:27 -07001158 goto err_unlock;
1159
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001160 if (!list_empty(&fc->interrupts)) {
1161 req = list_entry(fc->interrupts.next, struct fuse_req,
1162 intr_entry);
Miklos Szeredic3021622010-05-25 15:06:07 +02001163 return fuse_read_interrupt(fc, cs, nbytes, req);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001164 }
1165
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001166 if (forget_pending(fc)) {
1167 if (list_empty(&fc->pending) || fc->forget_batch-- > 0)
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001168 return fuse_read_forget(fc, cs, nbytes);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001169
1170 if (fc->forget_batch <= -8)
1171 fc->forget_batch = 16;
1172 }
1173
Miklos Szeredi334f4852005-09-09 13:10:27 -07001174 req = list_entry(fc->pending.next, struct fuse_req, list);
Miklos Szeredi83cfd492006-01-16 22:14:31 -08001175 req->state = FUSE_REQ_READING;
Miklos Szeredid77a1d52006-01-16 22:14:31 -08001176 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001177
1178 in = &req->in;
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001179 reqsize = in->h.len;
1180 /* If request is too large, reply with an error and restart the read */
Miklos Szeredic3021622010-05-25 15:06:07 +02001181 if (nbytes < reqsize) {
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001182 req->out.h.error = -EIO;
1183 /* SETXATTR is special, since it may contain too large data */
1184 if (in->h.opcode == FUSE_SETXATTR)
1185 req->out.h.error = -E2BIG;
1186 request_end(fc, req);
1187 goto restart;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001188 }
Miklos Szeredid7133112006-04-10 22:54:55 -07001189 spin_unlock(&fc->lock);
Miklos Szeredic3021622010-05-25 15:06:07 +02001190 cs->req = req;
1191 err = fuse_copy_one(cs, &in->h, sizeof(in->h));
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001192 if (!err)
Miklos Szeredic3021622010-05-25 15:06:07 +02001193 err = fuse_copy_args(cs, in->numargs, in->argpages,
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001194 (struct fuse_arg *) in->args, 0);
Miklos Szeredic3021622010-05-25 15:06:07 +02001195 fuse_copy_finish(cs);
Miklos Szeredid7133112006-04-10 22:54:55 -07001196 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001197 req->locked = 0;
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -07001198 if (req->aborted) {
1199 request_end(fc, req);
1200 return -ENODEV;
1201 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001202 if (err) {
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -07001203 req->out.h.error = -EIO;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001204 request_end(fc, req);
1205 return err;
1206 }
1207 if (!req->isreply)
1208 request_end(fc, req);
1209 else {
Miklos Szeredi83cfd492006-01-16 22:14:31 -08001210 req->state = FUSE_REQ_SENT;
Miklos Szeredid77a1d52006-01-16 22:14:31 -08001211 list_move_tail(&req->list, &fc->processing);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001212 if (req->interrupted)
1213 queue_interrupt(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -07001214 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001215 }
1216 return reqsize;
1217
1218 err_unlock:
Miklos Szeredid7133112006-04-10 22:54:55 -07001219 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001220 return err;
1221}
1222
Miklos Szeredic3021622010-05-25 15:06:07 +02001223static ssize_t fuse_dev_read(struct kiocb *iocb, const struct iovec *iov,
1224 unsigned long nr_segs, loff_t pos)
1225{
1226 struct fuse_copy_state cs;
1227 struct file *file = iocb->ki_filp;
1228 struct fuse_conn *fc = fuse_get_conn(file);
1229 if (!fc)
1230 return -EPERM;
1231
1232 fuse_copy_init(&cs, fc, 1, iov, nr_segs);
1233
1234 return fuse_dev_do_read(fc, file, &cs, iov_length(iov, nr_segs));
1235}
1236
1237static int fuse_dev_pipe_buf_steal(struct pipe_inode_info *pipe,
1238 struct pipe_buffer *buf)
1239{
1240 return 1;
1241}
1242
1243static const struct pipe_buf_operations fuse_dev_pipe_buf_ops = {
1244 .can_merge = 0,
1245 .map = generic_pipe_buf_map,
1246 .unmap = generic_pipe_buf_unmap,
1247 .confirm = generic_pipe_buf_confirm,
1248 .release = generic_pipe_buf_release,
1249 .steal = fuse_dev_pipe_buf_steal,
1250 .get = generic_pipe_buf_get,
1251};
1252
1253static ssize_t fuse_dev_splice_read(struct file *in, loff_t *ppos,
1254 struct pipe_inode_info *pipe,
1255 size_t len, unsigned int flags)
1256{
1257 int ret;
1258 int page_nr = 0;
1259 int do_wakeup = 0;
1260 struct pipe_buffer *bufs;
1261 struct fuse_copy_state cs;
1262 struct fuse_conn *fc = fuse_get_conn(in);
1263 if (!fc)
1264 return -EPERM;
1265
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001266 bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), GFP_KERNEL);
Miklos Szeredic3021622010-05-25 15:06:07 +02001267 if (!bufs)
1268 return -ENOMEM;
1269
1270 fuse_copy_init(&cs, fc, 1, NULL, 0);
1271 cs.pipebufs = bufs;
1272 cs.pipe = pipe;
1273 ret = fuse_dev_do_read(fc, in, &cs, len);
1274 if (ret < 0)
1275 goto out;
1276
1277 ret = 0;
1278 pipe_lock(pipe);
1279
1280 if (!pipe->readers) {
1281 send_sig(SIGPIPE, current, 0);
1282 if (!ret)
1283 ret = -EPIPE;
1284 goto out_unlock;
1285 }
1286
1287 if (pipe->nrbufs + cs.nr_segs > pipe->buffers) {
1288 ret = -EIO;
1289 goto out_unlock;
1290 }
1291
1292 while (page_nr < cs.nr_segs) {
1293 int newbuf = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
1294 struct pipe_buffer *buf = pipe->bufs + newbuf;
1295
1296 buf->page = bufs[page_nr].page;
1297 buf->offset = bufs[page_nr].offset;
1298 buf->len = bufs[page_nr].len;
1299 buf->ops = &fuse_dev_pipe_buf_ops;
1300
1301 pipe->nrbufs++;
1302 page_nr++;
1303 ret += buf->len;
1304
1305 if (pipe->inode)
1306 do_wakeup = 1;
1307 }
1308
1309out_unlock:
1310 pipe_unlock(pipe);
1311
1312 if (do_wakeup) {
1313 smp_mb();
1314 if (waitqueue_active(&pipe->wait))
1315 wake_up_interruptible(&pipe->wait);
1316 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
1317 }
1318
1319out:
1320 for (; page_nr < cs.nr_segs; page_nr++)
1321 page_cache_release(bufs[page_nr].page);
1322
1323 kfree(bufs);
1324 return ret;
1325}
1326
Tejun Heo95668a62008-11-26 12:03:55 +01001327static int fuse_notify_poll(struct fuse_conn *fc, unsigned int size,
1328 struct fuse_copy_state *cs)
1329{
1330 struct fuse_notify_poll_wakeup_out outarg;
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001331 int err = -EINVAL;
Tejun Heo95668a62008-11-26 12:03:55 +01001332
1333 if (size != sizeof(outarg))
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001334 goto err;
Tejun Heo95668a62008-11-26 12:03:55 +01001335
1336 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1337 if (err)
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001338 goto err;
Tejun Heo95668a62008-11-26 12:03:55 +01001339
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001340 fuse_copy_finish(cs);
Tejun Heo95668a62008-11-26 12:03:55 +01001341 return fuse_notify_poll_wakeup(fc, &outarg);
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001342
1343err:
1344 fuse_copy_finish(cs);
1345 return err;
Tejun Heo95668a62008-11-26 12:03:55 +01001346}
1347
John Muir3b463ae2009-05-31 11:13:57 -04001348static int fuse_notify_inval_inode(struct fuse_conn *fc, unsigned int size,
1349 struct fuse_copy_state *cs)
1350{
1351 struct fuse_notify_inval_inode_out outarg;
1352 int err = -EINVAL;
1353
1354 if (size != sizeof(outarg))
1355 goto err;
1356
1357 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1358 if (err)
1359 goto err;
1360 fuse_copy_finish(cs);
1361
1362 down_read(&fc->killsb);
1363 err = -ENOENT;
Miklos Szeredib21dda42010-02-05 12:08:31 +01001364 if (fc->sb) {
1365 err = fuse_reverse_inval_inode(fc->sb, outarg.ino,
1366 outarg.off, outarg.len);
1367 }
John Muir3b463ae2009-05-31 11:13:57 -04001368 up_read(&fc->killsb);
1369 return err;
1370
1371err:
1372 fuse_copy_finish(cs);
1373 return err;
1374}
1375
1376static int fuse_notify_inval_entry(struct fuse_conn *fc, unsigned int size,
1377 struct fuse_copy_state *cs)
1378{
1379 struct fuse_notify_inval_entry_out outarg;
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001380 int err = -ENOMEM;
1381 char *buf;
John Muir3b463ae2009-05-31 11:13:57 -04001382 struct qstr name;
1383
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001384 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1385 if (!buf)
1386 goto err;
1387
1388 err = -EINVAL;
John Muir3b463ae2009-05-31 11:13:57 -04001389 if (size < sizeof(outarg))
1390 goto err;
1391
1392 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1393 if (err)
1394 goto err;
1395
1396 err = -ENAMETOOLONG;
1397 if (outarg.namelen > FUSE_NAME_MAX)
1398 goto err;
1399
Miklos Szeredic2183d12011-08-24 10:20:17 +02001400 err = -EINVAL;
1401 if (size != sizeof(outarg) + outarg.namelen + 1)
1402 goto err;
1403
John Muir3b463ae2009-05-31 11:13:57 -04001404 name.name = buf;
1405 name.len = outarg.namelen;
1406 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1407 if (err)
1408 goto err;
1409 fuse_copy_finish(cs);
1410 buf[outarg.namelen] = 0;
1411 name.hash = full_name_hash(name.name, name.len);
1412
1413 down_read(&fc->killsb);
1414 err = -ENOENT;
Miklos Szeredib21dda42010-02-05 12:08:31 +01001415 if (fc->sb)
John Muir451d0f52011-12-06 21:50:06 +01001416 err = fuse_reverse_inval_entry(fc->sb, outarg.parent, 0, &name);
1417 up_read(&fc->killsb);
1418 kfree(buf);
1419 return err;
1420
1421err:
1422 kfree(buf);
1423 fuse_copy_finish(cs);
1424 return err;
1425}
1426
1427static int fuse_notify_delete(struct fuse_conn *fc, unsigned int size,
1428 struct fuse_copy_state *cs)
1429{
1430 struct fuse_notify_delete_out outarg;
1431 int err = -ENOMEM;
1432 char *buf;
1433 struct qstr name;
1434
1435 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1436 if (!buf)
1437 goto err;
1438
1439 err = -EINVAL;
1440 if (size < sizeof(outarg))
1441 goto err;
1442
1443 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1444 if (err)
1445 goto err;
1446
1447 err = -ENAMETOOLONG;
1448 if (outarg.namelen > FUSE_NAME_MAX)
1449 goto err;
1450
1451 err = -EINVAL;
1452 if (size != sizeof(outarg) + outarg.namelen + 1)
1453 goto err;
1454
1455 name.name = buf;
1456 name.len = outarg.namelen;
1457 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1458 if (err)
1459 goto err;
1460 fuse_copy_finish(cs);
1461 buf[outarg.namelen] = 0;
1462 name.hash = full_name_hash(name.name, name.len);
1463
1464 down_read(&fc->killsb);
1465 err = -ENOENT;
1466 if (fc->sb)
1467 err = fuse_reverse_inval_entry(fc->sb, outarg.parent,
1468 outarg.child, &name);
John Muir3b463ae2009-05-31 11:13:57 -04001469 up_read(&fc->killsb);
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001470 kfree(buf);
John Muir3b463ae2009-05-31 11:13:57 -04001471 return err;
1472
1473err:
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001474 kfree(buf);
John Muir3b463ae2009-05-31 11:13:57 -04001475 fuse_copy_finish(cs);
1476 return err;
1477}
1478
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001479static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
1480 struct fuse_copy_state *cs)
1481{
1482 struct fuse_notify_store_out outarg;
1483 struct inode *inode;
1484 struct address_space *mapping;
1485 u64 nodeid;
1486 int err;
1487 pgoff_t index;
1488 unsigned int offset;
1489 unsigned int num;
1490 loff_t file_size;
1491 loff_t end;
1492
1493 err = -EINVAL;
1494 if (size < sizeof(outarg))
1495 goto out_finish;
1496
1497 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1498 if (err)
1499 goto out_finish;
1500
1501 err = -EINVAL;
1502 if (size - sizeof(outarg) != outarg.size)
1503 goto out_finish;
1504
1505 nodeid = outarg.nodeid;
1506
1507 down_read(&fc->killsb);
1508
1509 err = -ENOENT;
1510 if (!fc->sb)
1511 goto out_up_killsb;
1512
1513 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1514 if (!inode)
1515 goto out_up_killsb;
1516
1517 mapping = inode->i_mapping;
1518 index = outarg.offset >> PAGE_CACHE_SHIFT;
1519 offset = outarg.offset & ~PAGE_CACHE_MASK;
1520 file_size = i_size_read(inode);
1521 end = outarg.offset + outarg.size;
1522 if (end > file_size) {
1523 file_size = end;
1524 fuse_write_update_size(inode, file_size);
1525 }
1526
1527 num = outarg.size;
1528 while (num) {
1529 struct page *page;
1530 unsigned int this_num;
1531
1532 err = -ENOMEM;
1533 page = find_or_create_page(mapping, index,
1534 mapping_gfp_mask(mapping));
1535 if (!page)
1536 goto out_iput;
1537
1538 this_num = min_t(unsigned, num, PAGE_CACHE_SIZE - offset);
1539 err = fuse_copy_page(cs, &page, offset, this_num, 0);
1540 if (!err && offset == 0 && (num != 0 || file_size == end))
1541 SetPageUptodate(page);
1542 unlock_page(page);
1543 page_cache_release(page);
1544
1545 if (err)
1546 goto out_iput;
1547
1548 num -= this_num;
1549 offset = 0;
1550 index++;
1551 }
1552
1553 err = 0;
1554
1555out_iput:
1556 iput(inode);
1557out_up_killsb:
1558 up_read(&fc->killsb);
1559out_finish:
1560 fuse_copy_finish(cs);
1561 return err;
1562}
1563
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001564static void fuse_retrieve_end(struct fuse_conn *fc, struct fuse_req *req)
1565{
Miklos Szeredi0be85572010-10-27 15:34:46 -07001566 release_pages(req->pages, req->num_pages, 0);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001567}
1568
1569static int fuse_retrieve(struct fuse_conn *fc, struct inode *inode,
1570 struct fuse_notify_retrieve_out *outarg)
1571{
1572 int err;
1573 struct address_space *mapping = inode->i_mapping;
1574 struct fuse_req *req;
1575 pgoff_t index;
1576 loff_t file_size;
1577 unsigned int num;
1578 unsigned int offset;
Geert Uytterhoeven01574432010-09-30 22:06:21 +02001579 size_t total_len = 0;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001580
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04001581 req = fuse_get_req(fc, FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001582 if (IS_ERR(req))
1583 return PTR_ERR(req);
1584
1585 offset = outarg->offset & ~PAGE_CACHE_MASK;
1586
1587 req->in.h.opcode = FUSE_NOTIFY_REPLY;
1588 req->in.h.nodeid = outarg->nodeid;
1589 req->in.numargs = 2;
1590 req->in.argpages = 1;
1591 req->page_offset = offset;
1592 req->end = fuse_retrieve_end;
1593
1594 index = outarg->offset >> PAGE_CACHE_SHIFT;
1595 file_size = i_size_read(inode);
1596 num = outarg->size;
1597 if (outarg->offset > file_size)
1598 num = 0;
1599 else if (outarg->offset + num > file_size)
1600 num = file_size - outarg->offset;
1601
Miklos Szeredi48706d02011-12-13 10:36:59 +01001602 while (num && req->num_pages < FUSE_MAX_PAGES_PER_REQ) {
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001603 struct page *page;
1604 unsigned int this_num;
1605
1606 page = find_get_page(mapping, index);
1607 if (!page)
1608 break;
1609
1610 this_num = min_t(unsigned, num, PAGE_CACHE_SIZE - offset);
1611 req->pages[req->num_pages] = page;
1612 req->num_pages++;
1613
Miklos Szeredic9e67d42012-09-04 18:45:54 +02001614 offset = 0;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001615 num -= this_num;
1616 total_len += this_num;
Miklos Szeredi48706d02011-12-13 10:36:59 +01001617 index++;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001618 }
1619 req->misc.retrieve_in.offset = outarg->offset;
1620 req->misc.retrieve_in.size = total_len;
1621 req->in.args[0].size = sizeof(req->misc.retrieve_in);
1622 req->in.args[0].value = &req->misc.retrieve_in;
1623 req->in.args[1].size = total_len;
1624
1625 err = fuse_request_send_notify_reply(fc, req, outarg->notify_unique);
1626 if (err)
1627 fuse_retrieve_end(fc, req);
1628
1629 return err;
1630}
1631
1632static int fuse_notify_retrieve(struct fuse_conn *fc, unsigned int size,
1633 struct fuse_copy_state *cs)
1634{
1635 struct fuse_notify_retrieve_out outarg;
1636 struct inode *inode;
1637 int err;
1638
1639 err = -EINVAL;
1640 if (size != sizeof(outarg))
1641 goto copy_finish;
1642
1643 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1644 if (err)
1645 goto copy_finish;
1646
1647 fuse_copy_finish(cs);
1648
1649 down_read(&fc->killsb);
1650 err = -ENOENT;
1651 if (fc->sb) {
1652 u64 nodeid = outarg.nodeid;
1653
1654 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1655 if (inode) {
1656 err = fuse_retrieve(fc, inode, &outarg);
1657 iput(inode);
1658 }
1659 }
1660 up_read(&fc->killsb);
1661
1662 return err;
1663
1664copy_finish:
1665 fuse_copy_finish(cs);
1666 return err;
1667}
1668
Tejun Heo85993962008-11-26 12:03:55 +01001669static int fuse_notify(struct fuse_conn *fc, enum fuse_notify_code code,
1670 unsigned int size, struct fuse_copy_state *cs)
1671{
1672 switch (code) {
Tejun Heo95668a62008-11-26 12:03:55 +01001673 case FUSE_NOTIFY_POLL:
1674 return fuse_notify_poll(fc, size, cs);
1675
John Muir3b463ae2009-05-31 11:13:57 -04001676 case FUSE_NOTIFY_INVAL_INODE:
1677 return fuse_notify_inval_inode(fc, size, cs);
1678
1679 case FUSE_NOTIFY_INVAL_ENTRY:
1680 return fuse_notify_inval_entry(fc, size, cs);
1681
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001682 case FUSE_NOTIFY_STORE:
1683 return fuse_notify_store(fc, size, cs);
1684
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001685 case FUSE_NOTIFY_RETRIEVE:
1686 return fuse_notify_retrieve(fc, size, cs);
1687
John Muir451d0f52011-12-06 21:50:06 +01001688 case FUSE_NOTIFY_DELETE:
1689 return fuse_notify_delete(fc, size, cs);
1690
Tejun Heo85993962008-11-26 12:03:55 +01001691 default:
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001692 fuse_copy_finish(cs);
Tejun Heo85993962008-11-26 12:03:55 +01001693 return -EINVAL;
1694 }
1695}
1696
Miklos Szeredi334f4852005-09-09 13:10:27 -07001697/* Look up request on processing list by unique ID */
1698static struct fuse_req *request_find(struct fuse_conn *fc, u64 unique)
1699{
1700 struct list_head *entry;
1701
1702 list_for_each(entry, &fc->processing) {
1703 struct fuse_req *req;
1704 req = list_entry(entry, struct fuse_req, list);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001705 if (req->in.h.unique == unique || req->intr_unique == unique)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001706 return req;
1707 }
1708 return NULL;
1709}
1710
1711static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
1712 unsigned nbytes)
1713{
1714 unsigned reqsize = sizeof(struct fuse_out_header);
1715
1716 if (out->h.error)
1717 return nbytes != reqsize ? -EINVAL : 0;
1718
1719 reqsize += len_args(out->numargs, out->args);
1720
1721 if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
1722 return -EINVAL;
1723 else if (reqsize > nbytes) {
1724 struct fuse_arg *lastarg = &out->args[out->numargs-1];
1725 unsigned diffsize = reqsize - nbytes;
1726 if (diffsize > lastarg->size)
1727 return -EINVAL;
1728 lastarg->size -= diffsize;
1729 }
1730 return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
1731 out->page_zeroing);
1732}
1733
1734/*
1735 * Write a single reply to a request. First the header is copied from
1736 * the write buffer. The request is then searched on the processing
1737 * list by the unique ID found in the header. If found, then remove
1738 * it from the list and copy the rest of the buffer to the request.
1739 * The request is finished by calling request_end()
1740 */
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001741static ssize_t fuse_dev_do_write(struct fuse_conn *fc,
1742 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001743{
1744 int err;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001745 struct fuse_req *req;
1746 struct fuse_out_header oh;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001747
Miklos Szeredi334f4852005-09-09 13:10:27 -07001748 if (nbytes < sizeof(struct fuse_out_header))
1749 return -EINVAL;
1750
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001751 err = fuse_copy_one(cs, &oh, sizeof(oh));
Miklos Szeredi334f4852005-09-09 13:10:27 -07001752 if (err)
1753 goto err_finish;
Tejun Heo85993962008-11-26 12:03:55 +01001754
Miklos Szeredi334f4852005-09-09 13:10:27 -07001755 err = -EINVAL;
Tejun Heo85993962008-11-26 12:03:55 +01001756 if (oh.len != nbytes)
1757 goto err_finish;
1758
1759 /*
1760 * Zero oh.unique indicates unsolicited notification message
1761 * and error contains notification code.
1762 */
1763 if (!oh.unique) {
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001764 err = fuse_notify(fc, oh.error, nbytes - sizeof(oh), cs);
Tejun Heo85993962008-11-26 12:03:55 +01001765 return err ? err : nbytes;
1766 }
1767
1768 err = -EINVAL;
1769 if (oh.error <= -1000 || oh.error > 0)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001770 goto err_finish;
1771
Miklos Szeredid7133112006-04-10 22:54:55 -07001772 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001773 err = -ENOENT;
1774 if (!fc->connected)
1775 goto err_unlock;
1776
Miklos Szeredi334f4852005-09-09 13:10:27 -07001777 req = request_find(fc, oh.unique);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001778 if (!req)
1779 goto err_unlock;
1780
Miklos Szeredif9a28422006-06-25 05:48:53 -07001781 if (req->aborted) {
Miklos Szeredid7133112006-04-10 22:54:55 -07001782 spin_unlock(&fc->lock);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001783 fuse_copy_finish(cs);
Miklos Szeredid7133112006-04-10 22:54:55 -07001784 spin_lock(&fc->lock);
Miklos Szeredi222f1d62006-01-16 22:14:25 -08001785 request_end(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001786 return -ENOENT;
1787 }
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001788 /* Is it an interrupt reply? */
1789 if (req->intr_unique == oh.unique) {
1790 err = -EINVAL;
1791 if (nbytes != sizeof(struct fuse_out_header))
1792 goto err_unlock;
1793
1794 if (oh.error == -ENOSYS)
1795 fc->no_interrupt = 1;
1796 else if (oh.error == -EAGAIN)
1797 queue_interrupt(fc, req);
1798
1799 spin_unlock(&fc->lock);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001800 fuse_copy_finish(cs);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001801 return nbytes;
1802 }
1803
1804 req->state = FUSE_REQ_WRITING;
Miklos Szeredid77a1d52006-01-16 22:14:31 -08001805 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001806 req->out.h = oh;
1807 req->locked = 1;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001808 cs->req = req;
Miklos Szeredice534fb2010-05-25 15:06:07 +02001809 if (!req->out.page_replace)
1810 cs->move_pages = 0;
Miklos Szeredid7133112006-04-10 22:54:55 -07001811 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001812
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001813 err = copy_out_args(cs, &req->out, nbytes);
1814 fuse_copy_finish(cs);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001815
Miklos Szeredid7133112006-04-10 22:54:55 -07001816 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001817 req->locked = 0;
1818 if (!err) {
Miklos Szeredif9a28422006-06-25 05:48:53 -07001819 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001820 err = -ENOENT;
Miklos Szeredif9a28422006-06-25 05:48:53 -07001821 } else if (!req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001822 req->out.h.error = -EIO;
1823 request_end(fc, req);
1824
1825 return err ? err : nbytes;
1826
1827 err_unlock:
Miklos Szeredid7133112006-04-10 22:54:55 -07001828 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001829 err_finish:
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001830 fuse_copy_finish(cs);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001831 return err;
1832}
1833
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001834static ssize_t fuse_dev_write(struct kiocb *iocb, const struct iovec *iov,
1835 unsigned long nr_segs, loff_t pos)
1836{
1837 struct fuse_copy_state cs;
1838 struct fuse_conn *fc = fuse_get_conn(iocb->ki_filp);
1839 if (!fc)
1840 return -EPERM;
1841
Miklos Szeredic3021622010-05-25 15:06:07 +02001842 fuse_copy_init(&cs, fc, 0, iov, nr_segs);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001843
1844 return fuse_dev_do_write(fc, &cs, iov_length(iov, nr_segs));
1845}
1846
1847static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe,
1848 struct file *out, loff_t *ppos,
1849 size_t len, unsigned int flags)
1850{
1851 unsigned nbuf;
1852 unsigned idx;
1853 struct pipe_buffer *bufs;
1854 struct fuse_copy_state cs;
1855 struct fuse_conn *fc;
1856 size_t rem;
1857 ssize_t ret;
1858
1859 fc = fuse_get_conn(out);
1860 if (!fc)
1861 return -EPERM;
1862
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001863 bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), GFP_KERNEL);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001864 if (!bufs)
1865 return -ENOMEM;
1866
1867 pipe_lock(pipe);
1868 nbuf = 0;
1869 rem = 0;
1870 for (idx = 0; idx < pipe->nrbufs && rem < len; idx++)
1871 rem += pipe->bufs[(pipe->curbuf + idx) & (pipe->buffers - 1)].len;
1872
1873 ret = -EINVAL;
1874 if (rem < len) {
1875 pipe_unlock(pipe);
1876 goto out;
1877 }
1878
1879 rem = len;
1880 while (rem) {
1881 struct pipe_buffer *ibuf;
1882 struct pipe_buffer *obuf;
1883
1884 BUG_ON(nbuf >= pipe->buffers);
1885 BUG_ON(!pipe->nrbufs);
1886 ibuf = &pipe->bufs[pipe->curbuf];
1887 obuf = &bufs[nbuf];
1888
1889 if (rem >= ibuf->len) {
1890 *obuf = *ibuf;
1891 ibuf->ops = NULL;
1892 pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
1893 pipe->nrbufs--;
1894 } else {
1895 ibuf->ops->get(pipe, ibuf);
1896 *obuf = *ibuf;
1897 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1898 obuf->len = rem;
1899 ibuf->offset += obuf->len;
1900 ibuf->len -= obuf->len;
1901 }
1902 nbuf++;
1903 rem -= obuf->len;
1904 }
1905 pipe_unlock(pipe);
1906
Miklos Szeredic3021622010-05-25 15:06:07 +02001907 fuse_copy_init(&cs, fc, 0, NULL, nbuf);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001908 cs.pipebufs = bufs;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001909 cs.pipe = pipe;
1910
Miklos Szeredice534fb2010-05-25 15:06:07 +02001911 if (flags & SPLICE_F_MOVE)
1912 cs.move_pages = 1;
1913
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001914 ret = fuse_dev_do_write(fc, &cs, len);
1915
1916 for (idx = 0; idx < nbuf; idx++) {
1917 struct pipe_buffer *buf = &bufs[idx];
1918 buf->ops->release(pipe, buf);
1919 }
1920out:
1921 kfree(bufs);
1922 return ret;
1923}
1924
Miklos Szeredi334f4852005-09-09 13:10:27 -07001925static unsigned fuse_dev_poll(struct file *file, poll_table *wait)
1926{
Miklos Szeredi334f4852005-09-09 13:10:27 -07001927 unsigned mask = POLLOUT | POLLWRNORM;
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07001928 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001929 if (!fc)
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07001930 return POLLERR;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001931
1932 poll_wait(file, &fc->waitq, wait);
1933
Miklos Szeredid7133112006-04-10 22:54:55 -07001934 spin_lock(&fc->lock);
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07001935 if (!fc->connected)
1936 mask = POLLERR;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001937 else if (request_pending(fc))
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07001938 mask |= POLLIN | POLLRDNORM;
Miklos Szeredid7133112006-04-10 22:54:55 -07001939 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001940
1941 return mask;
1942}
1943
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001944/*
1945 * Abort all requests on the given list (pending or processing)
1946 *
Miklos Szeredid7133112006-04-10 22:54:55 -07001947 * This function releases and reacquires fc->lock
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001948 */
Miklos Szeredi334f4852005-09-09 13:10:27 -07001949static void end_requests(struct fuse_conn *fc, struct list_head *head)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001950__releases(fc->lock)
1951__acquires(fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001952{
1953 while (!list_empty(head)) {
1954 struct fuse_req *req;
1955 req = list_entry(head->next, struct fuse_req, list);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001956 req->out.h.error = -ECONNABORTED;
1957 request_end(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -07001958 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001959 }
1960}
1961
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001962/*
1963 * Abort requests under I/O
1964 *
Miklos Szeredif9a28422006-06-25 05:48:53 -07001965 * The requests are set to aborted and finished, and the request
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001966 * waiter is woken up. This will make request_wait_answer() wait
1967 * until the request is unlocked and then return.
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08001968 *
1969 * If the request is asynchronous, then the end function needs to be
1970 * called after waiting for the request to be unlocked (if it was
1971 * locked).
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001972 */
1973static void end_io_requests(struct fuse_conn *fc)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001974__releases(fc->lock)
1975__acquires(fc->lock)
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001976{
1977 while (!list_empty(&fc->io)) {
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08001978 struct fuse_req *req =
1979 list_entry(fc->io.next, struct fuse_req, list);
1980 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
1981
Miklos Szeredif9a28422006-06-25 05:48:53 -07001982 req->aborted = 1;
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001983 req->out.h.error = -ECONNABORTED;
1984 req->state = FUSE_REQ_FINISHED;
1985 list_del_init(&req->list);
1986 wake_up(&req->waitq);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08001987 if (end) {
1988 req->end = NULL;
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08001989 __fuse_get_request(req);
Miklos Szeredid7133112006-04-10 22:54:55 -07001990 spin_unlock(&fc->lock);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08001991 wait_event(req->waitq, !req->locked);
1992 end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +01001993 fuse_put_request(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -07001994 spin_lock(&fc->lock);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08001995 }
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001996 }
1997}
1998
Miklos Szeredi595afaf2010-09-07 13:42:41 +02001999static void end_queued_requests(struct fuse_conn *fc)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02002000__releases(fc->lock)
2001__acquires(fc->lock)
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002002{
2003 fc->max_background = UINT_MAX;
2004 flush_bg_queue(fc);
2005 end_requests(fc, &fc->pending);
2006 end_requests(fc, &fc->processing);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01002007 while (forget_pending(fc))
Miklos Szeredi02c048b2010-12-07 20:16:56 +01002008 kfree(dequeue_forget(fc, 1, NULL));
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002009}
2010
Bryan Green357ccf22011-03-01 16:43:52 -08002011static void end_polls(struct fuse_conn *fc)
2012{
2013 struct rb_node *p;
2014
2015 p = rb_first(&fc->polled_files);
2016
2017 while (p) {
2018 struct fuse_file *ff;
2019 ff = rb_entry(p, struct fuse_file, polled_node);
2020 wake_up_interruptible_all(&ff->poll_wait);
2021
2022 p = rb_next(p);
2023 }
2024}
2025
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002026/*
2027 * Abort all requests.
2028 *
2029 * Emergency exit in case of a malicious or accidental deadlock, or
2030 * just a hung filesystem.
2031 *
2032 * The same effect is usually achievable through killing the
2033 * filesystem daemon and all users of the filesystem. The exception
2034 * is the combination of an asynchronous request and the tricky
2035 * deadlock (see Documentation/filesystems/fuse.txt).
2036 *
2037 * During the aborting, progression of requests from the pending and
2038 * processing lists onto the io list, and progression of new requests
2039 * onto the pending list is prevented by req->connected being false.
2040 *
2041 * Progression of requests under I/O to the processing list is
Miklos Szeredif9a28422006-06-25 05:48:53 -07002042 * prevented by the req->aborted flag being true for these requests.
2043 * For this reason requests on the io list must be aborted first.
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002044 */
2045void fuse_abort_conn(struct fuse_conn *fc)
2046{
Miklos Szeredid7133112006-04-10 22:54:55 -07002047 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002048 if (fc->connected) {
2049 fc->connected = 0;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -07002050 fc->blocked = 0;
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002051 end_io_requests(fc);
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002052 end_queued_requests(fc);
Bryan Green357ccf22011-03-01 16:43:52 -08002053 end_polls(fc);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002054 wake_up_all(&fc->waitq);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -07002055 wake_up_all(&fc->blocked_waitq);
Jeff Dike385a17b2006-04-10 22:54:52 -07002056 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002057 }
Miklos Szeredid7133112006-04-10 22:54:55 -07002058 spin_unlock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002059}
Tejun Heo08cbf542009-04-14 10:54:53 +09002060EXPORT_SYMBOL_GPL(fuse_abort_conn);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002061
Tejun Heo08cbf542009-04-14 10:54:53 +09002062int fuse_dev_release(struct inode *inode, struct file *file)
Miklos Szeredi334f4852005-09-09 13:10:27 -07002063{
Miklos Szeredi0720b312006-04-10 22:54:55 -07002064 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002065 if (fc) {
Miklos Szeredid7133112006-04-10 22:54:55 -07002066 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -07002067 fc->connected = 0;
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002068 fc->blocked = 0;
2069 end_queued_requests(fc);
Bryan Green357ccf22011-03-01 16:43:52 -08002070 end_polls(fc);
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002071 wake_up_all(&fc->blocked_waitq);
Miklos Szeredid7133112006-04-10 22:54:55 -07002072 spin_unlock(&fc->lock);
Miklos Szeredibafa9652006-06-25 05:48:51 -07002073 fuse_conn_put(fc);
Jeff Dike385a17b2006-04-10 22:54:52 -07002074 }
Miklos Szeredif543f252006-01-16 22:14:35 -08002075
Miklos Szeredi334f4852005-09-09 13:10:27 -07002076 return 0;
2077}
Tejun Heo08cbf542009-04-14 10:54:53 +09002078EXPORT_SYMBOL_GPL(fuse_dev_release);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002079
Jeff Dike385a17b2006-04-10 22:54:52 -07002080static int fuse_dev_fasync(int fd, struct file *file, int on)
2081{
2082 struct fuse_conn *fc = fuse_get_conn(file);
2083 if (!fc)
Miklos Szeredia87046d2006-04-10 22:54:56 -07002084 return -EPERM;
Jeff Dike385a17b2006-04-10 22:54:52 -07002085
2086 /* No locking - fasync_helper does its own locking */
2087 return fasync_helper(fd, file, on, &fc->fasync);
2088}
2089
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002090const struct file_operations fuse_dev_operations = {
Miklos Szeredi334f4852005-09-09 13:10:27 -07002091 .owner = THIS_MODULE,
2092 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002093 .read = do_sync_read,
2094 .aio_read = fuse_dev_read,
Miklos Szeredic3021622010-05-25 15:06:07 +02002095 .splice_read = fuse_dev_splice_read,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002096 .write = do_sync_write,
2097 .aio_write = fuse_dev_write,
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002098 .splice_write = fuse_dev_splice_write,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002099 .poll = fuse_dev_poll,
2100 .release = fuse_dev_release,
Jeff Dike385a17b2006-04-10 22:54:52 -07002101 .fasync = fuse_dev_fasync,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002102};
Tejun Heo08cbf542009-04-14 10:54:53 +09002103EXPORT_SYMBOL_GPL(fuse_dev_operations);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002104
2105static struct miscdevice fuse_miscdevice = {
2106 .minor = FUSE_MINOR,
2107 .name = "fuse",
2108 .fops = &fuse_dev_operations,
2109};
2110
2111int __init fuse_dev_init(void)
2112{
2113 int err = -ENOMEM;
2114 fuse_req_cachep = kmem_cache_create("fuse_request",
2115 sizeof(struct fuse_req),
Paul Mundt20c2df82007-07-20 10:11:58 +09002116 0, 0, NULL);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002117 if (!fuse_req_cachep)
2118 goto out;
2119
2120 err = misc_register(&fuse_miscdevice);
2121 if (err)
2122 goto out_cache_clean;
2123
2124 return 0;
2125
2126 out_cache_clean:
2127 kmem_cache_destroy(fuse_req_cachep);
2128 out:
2129 return err;
2130}
2131
2132void fuse_dev_cleanup(void)
2133{
2134 misc_deregister(&fuse_miscdevice);
2135 kmem_cache_destroy(fuse_req_cachep);
2136}