blob: ef74ad5fd362b193d858fdd2cfe3335d951d2a99 [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>
Kent Overstreeta27bb332013-05-07 16:19:08 -070022#include <linux/aio.h>
Miklos Szeredi334f4852005-09-09 13:10:27 -070023
24MODULE_ALIAS_MISCDEV(FUSE_MINOR);
Kay Sievers578454f2010-05-20 18:07:20 +020025MODULE_ALIAS("devname:fuse");
Miklos Szeredi334f4852005-09-09 13:10:27 -070026
Christoph Lametere18b8902006-12-06 20:33:20 -080027static struct kmem_cache *fuse_req_cachep;
Miklos Szeredi334f4852005-09-09 13:10:27 -070028
Miklos Szeredi8bfc0162006-01-16 22:14:28 -080029static struct fuse_conn *fuse_get_conn(struct file *file)
Miklos Szeredi334f4852005-09-09 13:10:27 -070030{
Miklos Szeredi0720b312006-04-10 22:54:55 -070031 /*
32 * Lockless access is OK, because file->private data is set
33 * once during mount and is valid until the file is released.
34 */
35 return file->private_data;
Miklos Szeredi334f4852005-09-09 13:10:27 -070036}
37
Maxim Patlasov4250c062012-10-26 19:48:07 +040038static void fuse_request_init(struct fuse_req *req, struct page **pages,
Maxim Patlasovb2430d72012-10-26 19:49:24 +040039 struct fuse_page_desc *page_descs,
Maxim Patlasov4250c062012-10-26 19:48:07 +040040 unsigned npages)
Miklos Szeredi334f4852005-09-09 13:10:27 -070041{
42 memset(req, 0, sizeof(*req));
Maxim Patlasov4250c062012-10-26 19:48:07 +040043 memset(pages, 0, sizeof(*pages) * npages);
Maxim Patlasovb2430d72012-10-26 19:49:24 +040044 memset(page_descs, 0, sizeof(*page_descs) * npages);
Miklos Szeredi334f4852005-09-09 13:10:27 -070045 INIT_LIST_HEAD(&req->list);
Miklos Szeredia4d27e72006-06-25 05:48:54 -070046 INIT_LIST_HEAD(&req->intr_entry);
Miklos Szeredi334f4852005-09-09 13:10:27 -070047 init_waitqueue_head(&req->waitq);
48 atomic_set(&req->count, 1);
Maxim Patlasov4250c062012-10-26 19:48:07 +040049 req->pages = pages;
Maxim Patlasovb2430d72012-10-26 19:49:24 +040050 req->page_descs = page_descs;
Maxim Patlasov4250c062012-10-26 19:48:07 +040051 req->max_pages = npages;
Miklos Szeredi334f4852005-09-09 13:10:27 -070052}
53
Maxim Patlasov4250c062012-10-26 19:48:07 +040054static struct fuse_req *__fuse_request_alloc(unsigned npages, gfp_t flags)
Miklos Szeredi334f4852005-09-09 13:10:27 -070055{
Maxim Patlasov4250c062012-10-26 19:48:07 +040056 struct fuse_req *req = kmem_cache_alloc(fuse_req_cachep, flags);
57 if (req) {
58 struct page **pages;
Maxim Patlasovb2430d72012-10-26 19:49:24 +040059 struct fuse_page_desc *page_descs;
Maxim Patlasov4250c062012-10-26 19:48:07 +040060
Maxim Patlasovb2430d72012-10-26 19:49:24 +040061 if (npages <= FUSE_REQ_INLINE_PAGES) {
Maxim Patlasov4250c062012-10-26 19:48:07 +040062 pages = req->inline_pages;
Maxim Patlasovb2430d72012-10-26 19:49:24 +040063 page_descs = req->inline_page_descs;
64 } else {
Maxim Patlasov4250c062012-10-26 19:48:07 +040065 pages = kmalloc(sizeof(struct page *) * npages, flags);
Maxim Patlasovb2430d72012-10-26 19:49:24 +040066 page_descs = kmalloc(sizeof(struct fuse_page_desc) *
67 npages, flags);
68 }
Maxim Patlasov4250c062012-10-26 19:48:07 +040069
Maxim Patlasovb2430d72012-10-26 19:49:24 +040070 if (!pages || !page_descs) {
71 kfree(pages);
72 kfree(page_descs);
Maxim Patlasov4250c062012-10-26 19:48:07 +040073 kmem_cache_free(fuse_req_cachep, req);
74 return NULL;
75 }
76
Maxim Patlasovb2430d72012-10-26 19:49:24 +040077 fuse_request_init(req, pages, page_descs, npages);
Maxim Patlasov4250c062012-10-26 19:48:07 +040078 }
Miklos Szeredi334f4852005-09-09 13:10:27 -070079 return req;
80}
Maxim Patlasov4250c062012-10-26 19:48:07 +040081
82struct fuse_req *fuse_request_alloc(unsigned npages)
83{
84 return __fuse_request_alloc(npages, GFP_KERNEL);
85}
Tejun Heo08cbf542009-04-14 10:54:53 +090086EXPORT_SYMBOL_GPL(fuse_request_alloc);
Miklos Szeredi334f4852005-09-09 13:10:27 -070087
Maxim Patlasov4250c062012-10-26 19:48:07 +040088struct fuse_req *fuse_request_alloc_nofs(unsigned npages)
Miklos Szeredi3be5a522008-04-30 00:54:41 -070089{
Maxim Patlasov4250c062012-10-26 19:48:07 +040090 return __fuse_request_alloc(npages, GFP_NOFS);
Miklos Szeredi3be5a522008-04-30 00:54:41 -070091}
92
Miklos Szeredi334f4852005-09-09 13:10:27 -070093void fuse_request_free(struct fuse_req *req)
94{
Maxim Patlasovb2430d72012-10-26 19:49:24 +040095 if (req->pages != req->inline_pages) {
Maxim Patlasov4250c062012-10-26 19:48:07 +040096 kfree(req->pages);
Maxim Patlasovb2430d72012-10-26 19:49:24 +040097 kfree(req->page_descs);
98 }
Miklos Szeredi334f4852005-09-09 13:10:27 -070099 kmem_cache_free(fuse_req_cachep, req);
100}
101
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800102static void block_sigs(sigset_t *oldset)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700103{
104 sigset_t mask;
105
106 siginitsetinv(&mask, sigmask(SIGKILL));
107 sigprocmask(SIG_BLOCK, &mask, oldset);
108}
109
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800110static void restore_sigs(sigset_t *oldset)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700111{
112 sigprocmask(SIG_SETMASK, oldset, NULL);
113}
114
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400115void __fuse_get_request(struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700116{
117 atomic_inc(&req->count);
118}
119
120/* Must be called with > 1 refcount */
121static void __fuse_put_request(struct fuse_req *req)
122{
123 BUG_ON(atomic_read(&req->count) < 2);
124 atomic_dec(&req->count);
125}
126
Miklos Szeredi33649c92006-06-25 05:48:52 -0700127static void fuse_req_init_context(struct fuse_req *req)
128{
Eric W. Biederman499dcf22012-02-07 16:26:03 -0800129 req->in.h.uid = from_kuid_munged(&init_user_ns, current_fsuid());
130 req->in.h.gid = from_kgid_munged(&init_user_ns, current_fsgid());
Miklos Szeredi33649c92006-06-25 05:48:52 -0700131 req->in.h.pid = current->pid;
132}
133
Maxim Patlasov0aada882013-03-21 18:02:28 +0400134static bool fuse_block_alloc(struct fuse_conn *fc, bool for_background)
135{
136 return !fc->initialized || (for_background && fc->blocked);
137}
138
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400139static struct fuse_req *__fuse_get_req(struct fuse_conn *fc, unsigned npages,
140 bool for_background)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700141{
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700142 struct fuse_req *req;
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700143 int err;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200144 atomic_inc(&fc->num_waiting);
Maxim Patlasov0aada882013-03-21 18:02:28 +0400145
146 if (fuse_block_alloc(fc, for_background)) {
147 sigset_t oldset;
148 int intr;
149
150 block_sigs(&oldset);
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400151 intr = wait_event_interruptible_exclusive(fc->blocked_waitq,
Maxim Patlasov0aada882013-03-21 18:02:28 +0400152 !fuse_block_alloc(fc, for_background));
153 restore_sigs(&oldset);
154 err = -EINTR;
155 if (intr)
156 goto out;
157 }
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700158
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700159 err = -ENOTCONN;
160 if (!fc->connected)
161 goto out;
162
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400163 req = fuse_request_alloc(npages);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200164 err = -ENOMEM;
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400165 if (!req) {
166 if (for_background)
167 wake_up(&fc->blocked_waitq);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200168 goto out;
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400169 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700170
Miklos Szeredi33649c92006-06-25 05:48:52 -0700171 fuse_req_init_context(req);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200172 req->waiting = 1;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400173 req->background = for_background;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700174 return req;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200175
176 out:
177 atomic_dec(&fc->num_waiting);
178 return ERR_PTR(err);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700179}
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400180
181struct fuse_req *fuse_get_req(struct fuse_conn *fc, unsigned npages)
182{
183 return __fuse_get_req(fc, npages, false);
184}
Tejun Heo08cbf542009-04-14 10:54:53 +0900185EXPORT_SYMBOL_GPL(fuse_get_req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700186
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400187struct fuse_req *fuse_get_req_for_background(struct fuse_conn *fc,
188 unsigned npages)
189{
190 return __fuse_get_req(fc, npages, true);
191}
192EXPORT_SYMBOL_GPL(fuse_get_req_for_background);
193
Miklos Szeredi33649c92006-06-25 05:48:52 -0700194/*
195 * Return request in fuse_file->reserved_req. However that may
196 * currently be in use. If that is the case, wait for it to become
197 * available.
198 */
199static struct fuse_req *get_reserved_req(struct fuse_conn *fc,
200 struct file *file)
201{
202 struct fuse_req *req = NULL;
203 struct fuse_file *ff = file->private_data;
204
205 do {
Miklos Szeredide5e3de2007-10-16 23:31:00 -0700206 wait_event(fc->reserved_req_waitq, ff->reserved_req);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700207 spin_lock(&fc->lock);
208 if (ff->reserved_req) {
209 req = ff->reserved_req;
210 ff->reserved_req = NULL;
Al Virocb0942b2012-08-27 14:48:26 -0400211 req->stolen_file = get_file(file);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700212 }
213 spin_unlock(&fc->lock);
214 } while (!req);
215
216 return req;
217}
218
219/*
220 * Put stolen request back into fuse_file->reserved_req
221 */
222static void put_reserved_req(struct fuse_conn *fc, struct fuse_req *req)
223{
224 struct file *file = req->stolen_file;
225 struct fuse_file *ff = file->private_data;
226
227 spin_lock(&fc->lock);
Maxim Patlasovb2430d72012-10-26 19:49:24 +0400228 fuse_request_init(req, req->pages, req->page_descs, req->max_pages);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700229 BUG_ON(ff->reserved_req);
230 ff->reserved_req = req;
Miklos Szeredide5e3de2007-10-16 23:31:00 -0700231 wake_up_all(&fc->reserved_req_waitq);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700232 spin_unlock(&fc->lock);
233 fput(file);
234}
235
236/*
237 * Gets a requests for a file operation, always succeeds
238 *
239 * This is used for sending the FLUSH request, which must get to
240 * userspace, due to POSIX locks which may need to be unlocked.
241 *
242 * If allocation fails due to OOM, use the reserved request in
243 * fuse_file.
244 *
245 * This is very unlikely to deadlock accidentally, since the
246 * filesystem should not have it's own file open. If deadlock is
247 * intentional, it can still be broken by "aborting" the filesystem.
248 */
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400249struct fuse_req *fuse_get_req_nofail_nopages(struct fuse_conn *fc,
250 struct file *file)
Miklos Szeredi33649c92006-06-25 05:48:52 -0700251{
252 struct fuse_req *req;
253
254 atomic_inc(&fc->num_waiting);
Maxim Patlasov0aada882013-03-21 18:02:28 +0400255 wait_event(fc->blocked_waitq, fc->initialized);
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400256 req = fuse_request_alloc(0);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700257 if (!req)
258 req = get_reserved_req(fc, file);
259
260 fuse_req_init_context(req);
261 req->waiting = 1;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400262 req->background = 0;
Miklos Szeredi33649c92006-06-25 05:48:52 -0700263 return req;
264}
265
Miklos Szeredi334f4852005-09-09 13:10:27 -0700266void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req)
267{
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800268 if (atomic_dec_and_test(&req->count)) {
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400269 if (unlikely(req->background)) {
270 /*
271 * We get here in the unlikely case that a background
272 * request was allocated but not sent
273 */
274 spin_lock(&fc->lock);
275 if (!fc->blocked)
276 wake_up(&fc->blocked_waitq);
277 spin_unlock(&fc->lock);
278 }
279
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200280 if (req->waiting)
281 atomic_dec(&fc->num_waiting);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700282
283 if (req->stolen_file)
284 put_reserved_req(fc, req);
285 else
286 fuse_request_free(req);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800287 }
288}
Tejun Heo08cbf542009-04-14 10:54:53 +0900289EXPORT_SYMBOL_GPL(fuse_put_request);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800290
Miklos Szeredid12def12008-02-06 01:38:39 -0800291static unsigned len_args(unsigned numargs, struct fuse_arg *args)
292{
293 unsigned nbytes = 0;
294 unsigned i;
295
296 for (i = 0; i < numargs; i++)
297 nbytes += args[i].size;
298
299 return nbytes;
300}
301
302static u64 fuse_get_unique(struct fuse_conn *fc)
303{
304 fc->reqctr++;
305 /* zero is special */
306 if (fc->reqctr == 0)
307 fc->reqctr = 1;
308
309 return fc->reqctr;
310}
311
312static void queue_request(struct fuse_conn *fc, struct fuse_req *req)
313{
Miklos Szeredid12def12008-02-06 01:38:39 -0800314 req->in.h.len = sizeof(struct fuse_in_header) +
315 len_args(req->in.numargs, (struct fuse_arg *) req->in.args);
316 list_add_tail(&req->list, &fc->pending);
317 req->state = FUSE_REQ_PENDING;
318 if (!req->waiting) {
319 req->waiting = 1;
320 atomic_inc(&fc->num_waiting);
321 }
322 wake_up(&fc->waitq);
323 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
324}
325
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100326void fuse_queue_forget(struct fuse_conn *fc, struct fuse_forget_link *forget,
327 u64 nodeid, u64 nlookup)
328{
Miklos Szeredi02c048b2010-12-07 20:16:56 +0100329 forget->forget_one.nodeid = nodeid;
330 forget->forget_one.nlookup = nlookup;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100331
332 spin_lock(&fc->lock);
Miklos Szeredi5dfcc872011-09-12 09:38:03 +0200333 if (fc->connected) {
334 fc->forget_list_tail->next = forget;
335 fc->forget_list_tail = forget;
336 wake_up(&fc->waitq);
337 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
338 } else {
339 kfree(forget);
340 }
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100341 spin_unlock(&fc->lock);
342}
343
Miklos Szeredid12def12008-02-06 01:38:39 -0800344static void flush_bg_queue(struct fuse_conn *fc)
345{
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700346 while (fc->active_background < fc->max_background &&
Miklos Szeredid12def12008-02-06 01:38:39 -0800347 !list_empty(&fc->bg_queue)) {
348 struct fuse_req *req;
349
350 req = list_entry(fc->bg_queue.next, struct fuse_req, list);
351 list_del(&req->list);
352 fc->active_background++;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200353 req->in.h.unique = fuse_get_unique(fc);
Miklos Szeredid12def12008-02-06 01:38:39 -0800354 queue_request(fc, req);
355 }
356}
357
Miklos Szeredi6dbbcb12006-04-26 10:49:06 +0200358/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700359 * This function is called when a request is finished. Either a reply
Miklos Szeredif9a28422006-06-25 05:48:53 -0700360 * has arrived or it was aborted (and not yet sent) or some error
Miklos Szeredif43b1552006-01-16 22:14:26 -0800361 * occurred during communication with userspace, or the device file
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700362 * was closed. The requester thread is woken up (if still waiting),
363 * the 'end' callback is called if given, else the reference to the
364 * request is released
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800365 *
Miklos Szeredid7133112006-04-10 22:54:55 -0700366 * Called with fc->lock, unlocks it
Miklos Szeredi334f4852005-09-09 13:10:27 -0700367 */
368static void request_end(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +0200369__releases(fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700370{
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700371 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
372 req->end = NULL;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800373 list_del(&req->list);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700374 list_del(&req->intr_entry);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800375 req->state = FUSE_REQ_FINISHED;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700376 if (req->background) {
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400377 req->background = 0;
378
379 if (fc->num_background == fc->max_background)
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700380 fc->blocked = 0;
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400381
382 /* Wake up next waiter, if any */
Miklos Szeredi3c18ef82013-04-17 21:50:58 +0200383 if (!fc->blocked && waitqueue_active(&fc->blocked_waitq))
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400384 wake_up(&fc->blocked_waitq);
385
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700386 if (fc->num_background == fc->congestion_threshold &&
Tejun Heoa325f9b2009-04-14 10:54:52 +0900387 fc->connected && fc->bdi_initialized) {
Jens Axboe8aa7e842009-07-09 14:52:32 +0200388 clear_bdi_congested(&fc->bdi, BLK_RW_SYNC);
389 clear_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
Miklos Szeredif92b99b2007-10-16 23:30:59 -0700390 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700391 fc->num_background--;
Miklos Szeredid12def12008-02-06 01:38:39 -0800392 fc->active_background--;
393 flush_bg_queue(fc);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700394 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700395 spin_unlock(&fc->lock);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700396 wake_up(&req->waitq);
397 if (end)
398 end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100399 fuse_put_request(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700400}
401
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700402static void wait_answer_interruptible(struct fuse_conn *fc,
403 struct fuse_req *req)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +0200404__releases(fc->lock)
405__acquires(fc->lock)
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700406{
407 if (signal_pending(current))
408 return;
409
410 spin_unlock(&fc->lock);
411 wait_event_interruptible(req->waitq, req->state == FUSE_REQ_FINISHED);
412 spin_lock(&fc->lock);
413}
414
415static void queue_interrupt(struct fuse_conn *fc, struct fuse_req *req)
416{
417 list_add_tail(&req->intr_entry, &fc->interrupts);
418 wake_up(&fc->waitq);
419 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
420}
421
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700422static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +0200423__releases(fc->lock)
424__acquires(fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700425{
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700426 if (!fc->no_interrupt) {
427 /* Any signal may interrupt this */
428 wait_answer_interruptible(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700429
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700430 if (req->aborted)
431 goto aborted;
432 if (req->state == FUSE_REQ_FINISHED)
433 return;
434
435 req->interrupted = 1;
436 if (req->state == FUSE_REQ_SENT)
437 queue_interrupt(fc, req);
438 }
439
Miklos Szeredia131de02007-10-16 23:31:04 -0700440 if (!req->force) {
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700441 sigset_t oldset;
442
443 /* Only fatal signals may interrupt this */
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700444 block_sigs(&oldset);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700445 wait_answer_interruptible(fc, req);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700446 restore_sigs(&oldset);
Miklos Szeredia131de02007-10-16 23:31:04 -0700447
448 if (req->aborted)
449 goto aborted;
450 if (req->state == FUSE_REQ_FINISHED)
451 return;
452
453 /* Request is not yet in userspace, bail out */
454 if (req->state == FUSE_REQ_PENDING) {
455 list_del(&req->list);
456 __fuse_put_request(req);
457 req->out.h.error = -EINTR;
458 return;
459 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700460 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700461
Miklos Szeredia131de02007-10-16 23:31:04 -0700462 /*
463 * Either request is already in userspace, or it was forced.
464 * Wait it out.
465 */
466 spin_unlock(&fc->lock);
467 wait_event(req->waitq, req->state == FUSE_REQ_FINISHED);
468 spin_lock(&fc->lock);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700469
Miklos Szeredia131de02007-10-16 23:31:04 -0700470 if (!req->aborted)
471 return;
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700472
473 aborted:
Miklos Szeredia131de02007-10-16 23:31:04 -0700474 BUG_ON(req->state != FUSE_REQ_FINISHED);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700475 if (req->locked) {
476 /* This is uninterruptible sleep, because data is
477 being copied to/from the buffers of req. During
478 locked state, there mustn't be any filesystem
479 operation (e.g. page fault), since that could lead
480 to deadlock */
Miklos Szeredid7133112006-04-10 22:54:55 -0700481 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700482 wait_event(req->waitq, !req->locked);
Miklos Szeredid7133112006-04-10 22:54:55 -0700483 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700484 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700485}
486
Eric Wong6a4e9222013-02-04 13:04:44 +0000487static void __fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700488{
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400489 BUG_ON(req->background);
Miklos Szeredid7133112006-04-10 22:54:55 -0700490 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700491 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700492 req->out.h.error = -ENOTCONN;
493 else if (fc->conn_error)
494 req->out.h.error = -ECONNREFUSED;
495 else {
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200496 req->in.h.unique = fuse_get_unique(fc);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700497 queue_request(fc, req);
498 /* acquire extra reference, since request is still needed
499 after request_end() */
500 __fuse_get_request(req);
501
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700502 request_wait_answer(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700503 }
Miklos Szeredid7133112006-04-10 22:54:55 -0700504 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700505}
Eric Wong6a4e9222013-02-04 13:04:44 +0000506
507void fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
508{
509 req->isreply = 1;
510 __fuse_request_send(fc, req);
511}
Tejun Heo08cbf542009-04-14 10:54:53 +0900512EXPORT_SYMBOL_GPL(fuse_request_send);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700513
Tejun Heob93f8582008-11-26 12:03:55 +0100514static void fuse_request_send_nowait_locked(struct fuse_conn *fc,
515 struct fuse_req *req)
Miklos Szeredid12def12008-02-06 01:38:39 -0800516{
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400517 BUG_ON(!req->background);
Miklos Szeredid12def12008-02-06 01:38:39 -0800518 fc->num_background++;
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700519 if (fc->num_background == fc->max_background)
Miklos Szeredid12def12008-02-06 01:38:39 -0800520 fc->blocked = 1;
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700521 if (fc->num_background == fc->congestion_threshold &&
Tejun Heoa325f9b2009-04-14 10:54:52 +0900522 fc->bdi_initialized) {
Jens Axboe8aa7e842009-07-09 14:52:32 +0200523 set_bdi_congested(&fc->bdi, BLK_RW_SYNC);
524 set_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
Miklos Szeredid12def12008-02-06 01:38:39 -0800525 }
526 list_add_tail(&req->list, &fc->bg_queue);
527 flush_bg_queue(fc);
528}
529
Tejun Heob93f8582008-11-26 12:03:55 +0100530static void fuse_request_send_nowait(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700531{
Miklos Szeredid7133112006-04-10 22:54:55 -0700532 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700533 if (fc->connected) {
Tejun Heob93f8582008-11-26 12:03:55 +0100534 fuse_request_send_nowait_locked(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700535 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700536 } else {
537 req->out.h.error = -ENOTCONN;
538 request_end(fc, req);
539 }
540}
541
Tejun Heob93f8582008-11-26 12:03:55 +0100542void fuse_request_send_background(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700543{
544 req->isreply = 1;
Tejun Heob93f8582008-11-26 12:03:55 +0100545 fuse_request_send_nowait(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700546}
Tejun Heo08cbf542009-04-14 10:54:53 +0900547EXPORT_SYMBOL_GPL(fuse_request_send_background);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700548
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200549static int fuse_request_send_notify_reply(struct fuse_conn *fc,
550 struct fuse_req *req, u64 unique)
551{
552 int err = -ENODEV;
553
554 req->isreply = 0;
555 req->in.h.unique = unique;
556 spin_lock(&fc->lock);
557 if (fc->connected) {
558 queue_request(fc, req);
559 err = 0;
560 }
561 spin_unlock(&fc->lock);
562
563 return err;
564}
565
Miklos Szeredi334f4852005-09-09 13:10:27 -0700566/*
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700567 * Called under fc->lock
568 *
569 * fc->connected must have been checked previously
570 */
Tejun Heob93f8582008-11-26 12:03:55 +0100571void fuse_request_send_background_locked(struct fuse_conn *fc,
572 struct fuse_req *req)
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700573{
574 req->isreply = 1;
Tejun Heob93f8582008-11-26 12:03:55 +0100575 fuse_request_send_nowait_locked(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700576}
577
Anand V. Avati0b05b182012-08-19 08:53:23 -0400578void fuse_force_forget(struct file *file, u64 nodeid)
579{
Al Viro6131ffa2013-02-27 16:59:05 -0500580 struct inode *inode = file_inode(file);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400581 struct fuse_conn *fc = get_fuse_conn(inode);
582 struct fuse_req *req;
583 struct fuse_forget_in inarg;
584
585 memset(&inarg, 0, sizeof(inarg));
586 inarg.nlookup = 1;
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400587 req = fuse_get_req_nofail_nopages(fc, file);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400588 req->in.h.opcode = FUSE_FORGET;
589 req->in.h.nodeid = nodeid;
590 req->in.numargs = 1;
591 req->in.args[0].size = sizeof(inarg);
592 req->in.args[0].value = &inarg;
593 req->isreply = 0;
Eric Wong6a4e9222013-02-04 13:04:44 +0000594 __fuse_request_send(fc, req);
595 /* ignore errors */
596 fuse_put_request(fc, req);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400597}
598
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700599/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700600 * Lock the request. Up to the next unlock_request() there mustn't be
601 * anything that could cause a page-fault. If the request was already
Miklos Szeredif9a28422006-06-25 05:48:53 -0700602 * aborted bail out.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700603 */
Miklos Szeredid7133112006-04-10 22:54:55 -0700604static int lock_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700605{
606 int err = 0;
607 if (req) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700608 spin_lock(&fc->lock);
Miklos Szeredif9a28422006-06-25 05:48:53 -0700609 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700610 err = -ENOENT;
611 else
612 req->locked = 1;
Miklos Szeredid7133112006-04-10 22:54:55 -0700613 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700614 }
615 return err;
616}
617
618/*
Miklos Szeredif9a28422006-06-25 05:48:53 -0700619 * Unlock request. If it was aborted during being locked, the
Miklos Szeredi334f4852005-09-09 13:10:27 -0700620 * requester thread is currently waiting for it to be unlocked, so
621 * wake it up.
622 */
Miklos Szeredid7133112006-04-10 22:54:55 -0700623static void unlock_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700624{
625 if (req) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700626 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700627 req->locked = 0;
Miklos Szeredif9a28422006-06-25 05:48:53 -0700628 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700629 wake_up(&req->waitq);
Miklos Szeredid7133112006-04-10 22:54:55 -0700630 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700631 }
632}
633
634struct fuse_copy_state {
Miklos Szeredid7133112006-04-10 22:54:55 -0700635 struct fuse_conn *fc;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700636 int write;
637 struct fuse_req *req;
638 const struct iovec *iov;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200639 struct pipe_buffer *pipebufs;
640 struct pipe_buffer *currbuf;
641 struct pipe_inode_info *pipe;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700642 unsigned long nr_segs;
643 unsigned long seglen;
644 unsigned long addr;
645 struct page *pg;
646 void *mapaddr;
647 void *buf;
648 unsigned len;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200649 unsigned move_pages:1;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700650};
651
Miklos Szeredid7133112006-04-10 22:54:55 -0700652static void fuse_copy_init(struct fuse_copy_state *cs, struct fuse_conn *fc,
Miklos Szeredic3021622010-05-25 15:06:07 +0200653 int write,
Miklos Szeredid7133112006-04-10 22:54:55 -0700654 const struct iovec *iov, unsigned long nr_segs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700655{
656 memset(cs, 0, sizeof(*cs));
Miklos Szeredid7133112006-04-10 22:54:55 -0700657 cs->fc = fc;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700658 cs->write = write;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700659 cs->iov = iov;
660 cs->nr_segs = nr_segs;
661}
662
663/* Unmap and put previous page of userspace buffer */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800664static void fuse_copy_finish(struct fuse_copy_state *cs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700665{
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200666 if (cs->currbuf) {
667 struct pipe_buffer *buf = cs->currbuf;
668
Miklos Szeredic3021622010-05-25 15:06:07 +0200669 if (!cs->write) {
670 buf->ops->unmap(cs->pipe, buf, cs->mapaddr);
671 } else {
Miklos Szeredi7909b1c2010-07-12 14:41:40 +0200672 kunmap(buf->page);
Miklos Szeredic3021622010-05-25 15:06:07 +0200673 buf->len = PAGE_SIZE - cs->len;
674 }
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200675 cs->currbuf = NULL;
676 cs->mapaddr = NULL;
677 } else if (cs->mapaddr) {
Miklos Szeredi7909b1c2010-07-12 14:41:40 +0200678 kunmap(cs->pg);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700679 if (cs->write) {
680 flush_dcache_page(cs->pg);
681 set_page_dirty_lock(cs->pg);
682 }
683 put_page(cs->pg);
684 cs->mapaddr = NULL;
685 }
686}
687
688/*
689 * Get another pagefull of userspace buffer, and map it to kernel
690 * address space, and lock request
691 */
692static int fuse_copy_fill(struct fuse_copy_state *cs)
693{
694 unsigned long offset;
695 int err;
696
Miklos Szeredid7133112006-04-10 22:54:55 -0700697 unlock_request(cs->fc, cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700698 fuse_copy_finish(cs);
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200699 if (cs->pipebufs) {
700 struct pipe_buffer *buf = cs->pipebufs;
701
Miklos Szeredic3021622010-05-25 15:06:07 +0200702 if (!cs->write) {
703 err = buf->ops->confirm(cs->pipe, buf);
704 if (err)
705 return err;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200706
Miklos Szeredic3021622010-05-25 15:06:07 +0200707 BUG_ON(!cs->nr_segs);
708 cs->currbuf = buf;
Miklos Szeredi7909b1c2010-07-12 14:41:40 +0200709 cs->mapaddr = buf->ops->map(cs->pipe, buf, 0);
Miklos Szeredic3021622010-05-25 15:06:07 +0200710 cs->len = buf->len;
711 cs->buf = cs->mapaddr + buf->offset;
712 cs->pipebufs++;
713 cs->nr_segs--;
714 } else {
715 struct page *page;
716
717 if (cs->nr_segs == cs->pipe->buffers)
718 return -EIO;
719
720 page = alloc_page(GFP_HIGHUSER);
721 if (!page)
722 return -ENOMEM;
723
724 buf->page = page;
725 buf->offset = 0;
726 buf->len = 0;
727
728 cs->currbuf = buf;
Miklos Szeredi7909b1c2010-07-12 14:41:40 +0200729 cs->mapaddr = kmap(page);
Miklos Szeredic3021622010-05-25 15:06:07 +0200730 cs->buf = cs->mapaddr;
731 cs->len = PAGE_SIZE;
732 cs->pipebufs++;
733 cs->nr_segs++;
734 }
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200735 } else {
736 if (!cs->seglen) {
737 BUG_ON(!cs->nr_segs);
738 cs->seglen = cs->iov[0].iov_len;
739 cs->addr = (unsigned long) cs->iov[0].iov_base;
740 cs->iov++;
741 cs->nr_segs--;
742 }
743 err = get_user_pages_fast(cs->addr, 1, cs->write, &cs->pg);
744 if (err < 0)
745 return err;
746 BUG_ON(err != 1);
747 offset = cs->addr % PAGE_SIZE;
Miklos Szeredi7909b1c2010-07-12 14:41:40 +0200748 cs->mapaddr = kmap(cs->pg);
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200749 cs->buf = cs->mapaddr + offset;
750 cs->len = min(PAGE_SIZE - offset, cs->seglen);
751 cs->seglen -= cs->len;
752 cs->addr += cs->len;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700753 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700754
Miklos Szeredid7133112006-04-10 22:54:55 -0700755 return lock_request(cs->fc, cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700756}
757
758/* Do as much copy to/from userspace buffer as we can */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800759static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700760{
761 unsigned ncpy = min(*size, cs->len);
762 if (val) {
763 if (cs->write)
764 memcpy(cs->buf, *val, ncpy);
765 else
766 memcpy(*val, cs->buf, ncpy);
767 *val += ncpy;
768 }
769 *size -= ncpy;
770 cs->len -= ncpy;
771 cs->buf += ncpy;
772 return ncpy;
773}
774
Miklos Szeredice534fb2010-05-25 15:06:07 +0200775static int fuse_check_page(struct page *page)
776{
777 if (page_mapcount(page) ||
778 page->mapping != NULL ||
779 page_count(page) != 1 ||
780 (page->flags & PAGE_FLAGS_CHECK_AT_PREP &
781 ~(1 << PG_locked |
782 1 << PG_referenced |
783 1 << PG_uptodate |
784 1 << PG_lru |
785 1 << PG_active |
786 1 << PG_reclaim))) {
787 printk(KERN_WARNING "fuse: trying to steal weird page\n");
788 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);
789 return 1;
790 }
791 return 0;
792}
793
794static int fuse_try_move_page(struct fuse_copy_state *cs, struct page **pagep)
795{
796 int err;
797 struct page *oldpage = *pagep;
798 struct page *newpage;
799 struct pipe_buffer *buf = cs->pipebufs;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200800
801 unlock_request(cs->fc, cs->req);
802 fuse_copy_finish(cs);
803
804 err = buf->ops->confirm(cs->pipe, buf);
805 if (err)
806 return err;
807
808 BUG_ON(!cs->nr_segs);
809 cs->currbuf = buf;
810 cs->len = buf->len;
811 cs->pipebufs++;
812 cs->nr_segs--;
813
814 if (cs->len != PAGE_SIZE)
815 goto out_fallback;
816
817 if (buf->ops->steal(cs->pipe, buf) != 0)
818 goto out_fallback;
819
820 newpage = buf->page;
821
822 if (WARN_ON(!PageUptodate(newpage)))
823 return -EIO;
824
825 ClearPageMappedToDisk(newpage);
826
827 if (fuse_check_page(newpage) != 0)
828 goto out_fallback_unlock;
829
Miklos Szeredice534fb2010-05-25 15:06:07 +0200830 /*
831 * This is a new and locked page, it shouldn't be mapped or
832 * have any special flags on it
833 */
834 if (WARN_ON(page_mapped(oldpage)))
835 goto out_fallback_unlock;
836 if (WARN_ON(page_has_private(oldpage)))
837 goto out_fallback_unlock;
838 if (WARN_ON(PageDirty(oldpage) || PageWriteback(oldpage)))
839 goto out_fallback_unlock;
840 if (WARN_ON(PageMlocked(oldpage)))
841 goto out_fallback_unlock;
842
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700843 err = replace_page_cache_page(oldpage, newpage, GFP_KERNEL);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200844 if (err) {
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700845 unlock_page(newpage);
846 return err;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200847 }
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700848
Miklos Szeredice534fb2010-05-25 15:06:07 +0200849 page_cache_get(newpage);
850
851 if (!(buf->flags & PIPE_BUF_FLAG_LRU))
852 lru_cache_add_file(newpage);
853
854 err = 0;
855 spin_lock(&cs->fc->lock);
856 if (cs->req->aborted)
857 err = -ENOENT;
858 else
859 *pagep = newpage;
860 spin_unlock(&cs->fc->lock);
861
862 if (err) {
863 unlock_page(newpage);
864 page_cache_release(newpage);
865 return err;
866 }
867
868 unlock_page(oldpage);
869 page_cache_release(oldpage);
870 cs->len = 0;
871
872 return 0;
873
874out_fallback_unlock:
875 unlock_page(newpage);
876out_fallback:
877 cs->mapaddr = buf->ops->map(cs->pipe, buf, 1);
878 cs->buf = cs->mapaddr + buf->offset;
879
880 err = lock_request(cs->fc, cs->req);
881 if (err)
882 return err;
883
884 return 1;
885}
886
Miklos Szeredic3021622010-05-25 15:06:07 +0200887static int fuse_ref_page(struct fuse_copy_state *cs, struct page *page,
888 unsigned offset, unsigned count)
889{
890 struct pipe_buffer *buf;
891
892 if (cs->nr_segs == cs->pipe->buffers)
893 return -EIO;
894
895 unlock_request(cs->fc, cs->req);
896 fuse_copy_finish(cs);
897
898 buf = cs->pipebufs;
899 page_cache_get(page);
900 buf->page = page;
901 buf->offset = offset;
902 buf->len = count;
903
904 cs->pipebufs++;
905 cs->nr_segs++;
906 cs->len = 0;
907
908 return 0;
909}
910
Miklos Szeredi334f4852005-09-09 13:10:27 -0700911/*
912 * Copy a page in the request to/from the userspace buffer. Must be
913 * done atomically
914 */
Miklos Szeredice534fb2010-05-25 15:06:07 +0200915static int fuse_copy_page(struct fuse_copy_state *cs, struct page **pagep,
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800916 unsigned offset, unsigned count, int zeroing)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700917{
Miklos Szeredice534fb2010-05-25 15:06:07 +0200918 int err;
919 struct page *page = *pagep;
920
Miklos Szeredib6777c42010-10-26 14:22:27 -0700921 if (page && zeroing && count < PAGE_SIZE)
922 clear_highpage(page);
923
Miklos Szeredi334f4852005-09-09 13:10:27 -0700924 while (count) {
Miklos Szeredic3021622010-05-25 15:06:07 +0200925 if (cs->write && cs->pipebufs && page) {
926 return fuse_ref_page(cs, page, offset, count);
927 } else if (!cs->len) {
Miklos Szeredice534fb2010-05-25 15:06:07 +0200928 if (cs->move_pages && page &&
929 offset == 0 && count == PAGE_SIZE) {
930 err = fuse_try_move_page(cs, pagep);
931 if (err <= 0)
932 return err;
933 } else {
934 err = fuse_copy_fill(cs);
935 if (err)
936 return err;
937 }
Miklos Szeredi1729a162008-11-26 12:03:54 +0100938 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700939 if (page) {
Cong Wang2408f6e2011-11-25 23:14:30 +0800940 void *mapaddr = kmap_atomic(page);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700941 void *buf = mapaddr + offset;
942 offset += fuse_copy_do(cs, &buf, &count);
Cong Wang2408f6e2011-11-25 23:14:30 +0800943 kunmap_atomic(mapaddr);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700944 } else
945 offset += fuse_copy_do(cs, NULL, &count);
946 }
947 if (page && !cs->write)
948 flush_dcache_page(page);
949 return 0;
950}
951
952/* Copy pages in the request to/from userspace buffer */
953static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
954 int zeroing)
955{
956 unsigned i;
957 struct fuse_req *req = cs->req;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700958
959 for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
Miklos Szeredice534fb2010-05-25 15:06:07 +0200960 int err;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400961 unsigned offset = req->page_descs[i].offset;
962 unsigned count = min(nbytes, req->page_descs[i].length);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200963
964 err = fuse_copy_page(cs, &req->pages[i], offset, count,
965 zeroing);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700966 if (err)
967 return err;
968
969 nbytes -= count;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700970 }
971 return 0;
972}
973
974/* Copy a single argument in the request to/from userspace buffer */
975static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
976{
977 while (size) {
Miklos Szeredi1729a162008-11-26 12:03:54 +0100978 if (!cs->len) {
979 int err = fuse_copy_fill(cs);
980 if (err)
981 return err;
982 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700983 fuse_copy_do(cs, &val, &size);
984 }
985 return 0;
986}
987
988/* Copy request arguments to/from userspace buffer */
989static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
990 unsigned argpages, struct fuse_arg *args,
991 int zeroing)
992{
993 int err = 0;
994 unsigned i;
995
996 for (i = 0; !err && i < numargs; i++) {
997 struct fuse_arg *arg = &args[i];
998 if (i == numargs - 1 && argpages)
999 err = fuse_copy_pages(cs, arg->size, zeroing);
1000 else
1001 err = fuse_copy_one(cs, arg->value, arg->size);
1002 }
1003 return err;
1004}
1005
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001006static int forget_pending(struct fuse_conn *fc)
1007{
1008 return fc->forget_list_head.next != NULL;
1009}
1010
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001011static int request_pending(struct fuse_conn *fc)
1012{
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001013 return !list_empty(&fc->pending) || !list_empty(&fc->interrupts) ||
1014 forget_pending(fc);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001015}
1016
Miklos Szeredi334f4852005-09-09 13:10:27 -07001017/* Wait until a request is available on the pending list */
1018static void request_wait(struct fuse_conn *fc)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001019__releases(fc->lock)
1020__acquires(fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001021{
1022 DECLARE_WAITQUEUE(wait, current);
1023
1024 add_wait_queue_exclusive(&fc->waitq, &wait);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001025 while (fc->connected && !request_pending(fc)) {
Miklos Szeredi334f4852005-09-09 13:10:27 -07001026 set_current_state(TASK_INTERRUPTIBLE);
1027 if (signal_pending(current))
1028 break;
1029
Miklos Szeredid7133112006-04-10 22:54:55 -07001030 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001031 schedule();
Miklos Szeredid7133112006-04-10 22:54:55 -07001032 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001033 }
1034 set_current_state(TASK_RUNNING);
1035 remove_wait_queue(&fc->waitq, &wait);
1036}
1037
1038/*
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001039 * Transfer an interrupt request to userspace
1040 *
1041 * Unlike other requests this is assembled on demand, without a need
1042 * to allocate a separate fuse_req structure.
1043 *
1044 * Called with fc->lock held, releases it
1045 */
Miklos Szeredic3021622010-05-25 15:06:07 +02001046static int fuse_read_interrupt(struct fuse_conn *fc, struct fuse_copy_state *cs,
1047 size_t nbytes, struct fuse_req *req)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001048__releases(fc->lock)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001049{
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001050 struct fuse_in_header ih;
1051 struct fuse_interrupt_in arg;
1052 unsigned reqsize = sizeof(ih) + sizeof(arg);
1053 int err;
1054
1055 list_del_init(&req->intr_entry);
1056 req->intr_unique = fuse_get_unique(fc);
1057 memset(&ih, 0, sizeof(ih));
1058 memset(&arg, 0, sizeof(arg));
1059 ih.len = reqsize;
1060 ih.opcode = FUSE_INTERRUPT;
1061 ih.unique = req->intr_unique;
1062 arg.unique = req->in.h.unique;
1063
1064 spin_unlock(&fc->lock);
Miklos Szeredic3021622010-05-25 15:06:07 +02001065 if (nbytes < reqsize)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001066 return -EINVAL;
1067
Miklos Szeredic3021622010-05-25 15:06:07 +02001068 err = fuse_copy_one(cs, &ih, sizeof(ih));
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001069 if (!err)
Miklos Szeredic3021622010-05-25 15:06:07 +02001070 err = fuse_copy_one(cs, &arg, sizeof(arg));
1071 fuse_copy_finish(cs);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001072
1073 return err ? err : reqsize;
1074}
1075
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001076static struct fuse_forget_link *dequeue_forget(struct fuse_conn *fc,
1077 unsigned max,
1078 unsigned *countp)
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001079{
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001080 struct fuse_forget_link *head = fc->forget_list_head.next;
1081 struct fuse_forget_link **newhead = &head;
1082 unsigned count;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001083
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001084 for (count = 0; *newhead != NULL && count < max; count++)
1085 newhead = &(*newhead)->next;
1086
1087 fc->forget_list_head.next = *newhead;
1088 *newhead = NULL;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001089 if (fc->forget_list_head.next == NULL)
1090 fc->forget_list_tail = &fc->forget_list_head;
1091
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001092 if (countp != NULL)
1093 *countp = count;
1094
1095 return head;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001096}
1097
1098static int fuse_read_single_forget(struct fuse_conn *fc,
1099 struct fuse_copy_state *cs,
1100 size_t nbytes)
1101__releases(fc->lock)
1102{
1103 int err;
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001104 struct fuse_forget_link *forget = dequeue_forget(fc, 1, NULL);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001105 struct fuse_forget_in arg = {
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001106 .nlookup = forget->forget_one.nlookup,
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001107 };
1108 struct fuse_in_header ih = {
1109 .opcode = FUSE_FORGET,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001110 .nodeid = forget->forget_one.nodeid,
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001111 .unique = fuse_get_unique(fc),
1112 .len = sizeof(ih) + sizeof(arg),
1113 };
1114
1115 spin_unlock(&fc->lock);
1116 kfree(forget);
1117 if (nbytes < ih.len)
1118 return -EINVAL;
1119
1120 err = fuse_copy_one(cs, &ih, sizeof(ih));
1121 if (!err)
1122 err = fuse_copy_one(cs, &arg, sizeof(arg));
1123 fuse_copy_finish(cs);
1124
1125 if (err)
1126 return err;
1127
1128 return ih.len;
1129}
1130
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001131static int fuse_read_batch_forget(struct fuse_conn *fc,
1132 struct fuse_copy_state *cs, size_t nbytes)
1133__releases(fc->lock)
1134{
1135 int err;
1136 unsigned max_forgets;
1137 unsigned count;
1138 struct fuse_forget_link *head;
1139 struct fuse_batch_forget_in arg = { .count = 0 };
1140 struct fuse_in_header ih = {
1141 .opcode = FUSE_BATCH_FORGET,
1142 .unique = fuse_get_unique(fc),
1143 .len = sizeof(ih) + sizeof(arg),
1144 };
1145
1146 if (nbytes < ih.len) {
1147 spin_unlock(&fc->lock);
1148 return -EINVAL;
1149 }
1150
1151 max_forgets = (nbytes - ih.len) / sizeof(struct fuse_forget_one);
1152 head = dequeue_forget(fc, max_forgets, &count);
1153 spin_unlock(&fc->lock);
1154
1155 arg.count = count;
1156 ih.len += count * sizeof(struct fuse_forget_one);
1157 err = fuse_copy_one(cs, &ih, sizeof(ih));
1158 if (!err)
1159 err = fuse_copy_one(cs, &arg, sizeof(arg));
1160
1161 while (head) {
1162 struct fuse_forget_link *forget = head;
1163
1164 if (!err) {
1165 err = fuse_copy_one(cs, &forget->forget_one,
1166 sizeof(forget->forget_one));
1167 }
1168 head = forget->next;
1169 kfree(forget);
1170 }
1171
1172 fuse_copy_finish(cs);
1173
1174 if (err)
1175 return err;
1176
1177 return ih.len;
1178}
1179
1180static int fuse_read_forget(struct fuse_conn *fc, struct fuse_copy_state *cs,
1181 size_t nbytes)
1182__releases(fc->lock)
1183{
1184 if (fc->minor < 16 || fc->forget_list_head.next->next == NULL)
1185 return fuse_read_single_forget(fc, cs, nbytes);
1186 else
1187 return fuse_read_batch_forget(fc, cs, nbytes);
1188}
1189
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001190/*
Miklos Szeredi334f4852005-09-09 13:10:27 -07001191 * Read a single request into the userspace filesystem's buffer. This
1192 * function waits until a request is available, then removes it from
1193 * the pending list and copies request data to userspace buffer. If
Miklos Szeredif9a28422006-06-25 05:48:53 -07001194 * no reply is needed (FORGET) or request has been aborted or there
1195 * was an error during the copying then it's finished by calling
Miklos Szeredi334f4852005-09-09 13:10:27 -07001196 * request_end(). Otherwise add it to the processing list, and set
1197 * the 'sent' flag.
1198 */
Miklos Szeredic3021622010-05-25 15:06:07 +02001199static ssize_t fuse_dev_do_read(struct fuse_conn *fc, struct file *file,
1200 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001201{
1202 int err;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001203 struct fuse_req *req;
1204 struct fuse_in *in;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001205 unsigned reqsize;
1206
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001207 restart:
Miklos Szeredid7133112006-04-10 22:54:55 -07001208 spin_lock(&fc->lock);
Jeff Dikee5ac1d12006-04-10 22:54:53 -07001209 err = -EAGAIN;
1210 if ((file->f_flags & O_NONBLOCK) && fc->connected &&
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001211 !request_pending(fc))
Jeff Dikee5ac1d12006-04-10 22:54:53 -07001212 goto err_unlock;
1213
Miklos Szeredi334f4852005-09-09 13:10:27 -07001214 request_wait(fc);
1215 err = -ENODEV;
Miklos Szeredi9ba7cbb2006-01-16 22:14:34 -08001216 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001217 goto err_unlock;
1218 err = -ERESTARTSYS;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001219 if (!request_pending(fc))
Miklos Szeredi334f4852005-09-09 13:10:27 -07001220 goto err_unlock;
1221
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001222 if (!list_empty(&fc->interrupts)) {
1223 req = list_entry(fc->interrupts.next, struct fuse_req,
1224 intr_entry);
Miklos Szeredic3021622010-05-25 15:06:07 +02001225 return fuse_read_interrupt(fc, cs, nbytes, req);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001226 }
1227
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001228 if (forget_pending(fc)) {
1229 if (list_empty(&fc->pending) || fc->forget_batch-- > 0)
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001230 return fuse_read_forget(fc, cs, nbytes);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001231
1232 if (fc->forget_batch <= -8)
1233 fc->forget_batch = 16;
1234 }
1235
Miklos Szeredi334f4852005-09-09 13:10:27 -07001236 req = list_entry(fc->pending.next, struct fuse_req, list);
Miklos Szeredi83cfd492006-01-16 22:14:31 -08001237 req->state = FUSE_REQ_READING;
Miklos Szeredid77a1d52006-01-16 22:14:31 -08001238 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001239
1240 in = &req->in;
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001241 reqsize = in->h.len;
1242 /* If request is too large, reply with an error and restart the read */
Miklos Szeredic3021622010-05-25 15:06:07 +02001243 if (nbytes < reqsize) {
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001244 req->out.h.error = -EIO;
1245 /* SETXATTR is special, since it may contain too large data */
1246 if (in->h.opcode == FUSE_SETXATTR)
1247 req->out.h.error = -E2BIG;
1248 request_end(fc, req);
1249 goto restart;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001250 }
Miklos Szeredid7133112006-04-10 22:54:55 -07001251 spin_unlock(&fc->lock);
Miklos Szeredic3021622010-05-25 15:06:07 +02001252 cs->req = req;
1253 err = fuse_copy_one(cs, &in->h, sizeof(in->h));
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001254 if (!err)
Miklos Szeredic3021622010-05-25 15:06:07 +02001255 err = fuse_copy_args(cs, in->numargs, in->argpages,
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001256 (struct fuse_arg *) in->args, 0);
Miklos Szeredic3021622010-05-25 15:06:07 +02001257 fuse_copy_finish(cs);
Miklos Szeredid7133112006-04-10 22:54:55 -07001258 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001259 req->locked = 0;
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -07001260 if (req->aborted) {
1261 request_end(fc, req);
1262 return -ENODEV;
1263 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001264 if (err) {
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -07001265 req->out.h.error = -EIO;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001266 request_end(fc, req);
1267 return err;
1268 }
1269 if (!req->isreply)
1270 request_end(fc, req);
1271 else {
Miklos Szeredi83cfd492006-01-16 22:14:31 -08001272 req->state = FUSE_REQ_SENT;
Miklos Szeredid77a1d52006-01-16 22:14:31 -08001273 list_move_tail(&req->list, &fc->processing);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001274 if (req->interrupted)
1275 queue_interrupt(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -07001276 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001277 }
1278 return reqsize;
1279
1280 err_unlock:
Miklos Szeredid7133112006-04-10 22:54:55 -07001281 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001282 return err;
1283}
1284
Miklos Szeredic3021622010-05-25 15:06:07 +02001285static ssize_t fuse_dev_read(struct kiocb *iocb, const struct iovec *iov,
1286 unsigned long nr_segs, loff_t pos)
1287{
1288 struct fuse_copy_state cs;
1289 struct file *file = iocb->ki_filp;
1290 struct fuse_conn *fc = fuse_get_conn(file);
1291 if (!fc)
1292 return -EPERM;
1293
1294 fuse_copy_init(&cs, fc, 1, iov, nr_segs);
1295
1296 return fuse_dev_do_read(fc, file, &cs, iov_length(iov, nr_segs));
1297}
1298
1299static int fuse_dev_pipe_buf_steal(struct pipe_inode_info *pipe,
1300 struct pipe_buffer *buf)
1301{
1302 return 1;
1303}
1304
1305static const struct pipe_buf_operations fuse_dev_pipe_buf_ops = {
1306 .can_merge = 0,
1307 .map = generic_pipe_buf_map,
1308 .unmap = generic_pipe_buf_unmap,
1309 .confirm = generic_pipe_buf_confirm,
1310 .release = generic_pipe_buf_release,
1311 .steal = fuse_dev_pipe_buf_steal,
1312 .get = generic_pipe_buf_get,
1313};
1314
1315static ssize_t fuse_dev_splice_read(struct file *in, loff_t *ppos,
1316 struct pipe_inode_info *pipe,
1317 size_t len, unsigned int flags)
1318{
1319 int ret;
1320 int page_nr = 0;
1321 int do_wakeup = 0;
1322 struct pipe_buffer *bufs;
1323 struct fuse_copy_state cs;
1324 struct fuse_conn *fc = fuse_get_conn(in);
1325 if (!fc)
1326 return -EPERM;
1327
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001328 bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), GFP_KERNEL);
Miklos Szeredic3021622010-05-25 15:06:07 +02001329 if (!bufs)
1330 return -ENOMEM;
1331
1332 fuse_copy_init(&cs, fc, 1, NULL, 0);
1333 cs.pipebufs = bufs;
1334 cs.pipe = pipe;
1335 ret = fuse_dev_do_read(fc, in, &cs, len);
1336 if (ret < 0)
1337 goto out;
1338
1339 ret = 0;
1340 pipe_lock(pipe);
1341
1342 if (!pipe->readers) {
1343 send_sig(SIGPIPE, current, 0);
1344 if (!ret)
1345 ret = -EPIPE;
1346 goto out_unlock;
1347 }
1348
1349 if (pipe->nrbufs + cs.nr_segs > pipe->buffers) {
1350 ret = -EIO;
1351 goto out_unlock;
1352 }
1353
1354 while (page_nr < cs.nr_segs) {
1355 int newbuf = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
1356 struct pipe_buffer *buf = pipe->bufs + newbuf;
1357
1358 buf->page = bufs[page_nr].page;
1359 buf->offset = bufs[page_nr].offset;
1360 buf->len = bufs[page_nr].len;
1361 buf->ops = &fuse_dev_pipe_buf_ops;
1362
1363 pipe->nrbufs++;
1364 page_nr++;
1365 ret += buf->len;
1366
Al Viro6447a3c2013-03-21 11:01:38 -04001367 if (pipe->files)
Miklos Szeredic3021622010-05-25 15:06:07 +02001368 do_wakeup = 1;
1369 }
1370
1371out_unlock:
1372 pipe_unlock(pipe);
1373
1374 if (do_wakeup) {
1375 smp_mb();
1376 if (waitqueue_active(&pipe->wait))
1377 wake_up_interruptible(&pipe->wait);
1378 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
1379 }
1380
1381out:
1382 for (; page_nr < cs.nr_segs; page_nr++)
1383 page_cache_release(bufs[page_nr].page);
1384
1385 kfree(bufs);
1386 return ret;
1387}
1388
Tejun Heo95668a62008-11-26 12:03:55 +01001389static int fuse_notify_poll(struct fuse_conn *fc, unsigned int size,
1390 struct fuse_copy_state *cs)
1391{
1392 struct fuse_notify_poll_wakeup_out outarg;
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001393 int err = -EINVAL;
Tejun Heo95668a62008-11-26 12:03:55 +01001394
1395 if (size != sizeof(outarg))
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001396 goto err;
Tejun Heo95668a62008-11-26 12:03:55 +01001397
1398 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1399 if (err)
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001400 goto err;
Tejun Heo95668a62008-11-26 12:03:55 +01001401
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001402 fuse_copy_finish(cs);
Tejun Heo95668a62008-11-26 12:03:55 +01001403 return fuse_notify_poll_wakeup(fc, &outarg);
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001404
1405err:
1406 fuse_copy_finish(cs);
1407 return err;
Tejun Heo95668a62008-11-26 12:03:55 +01001408}
1409
John Muir3b463ae2009-05-31 11:13:57 -04001410static int fuse_notify_inval_inode(struct fuse_conn *fc, unsigned int size,
1411 struct fuse_copy_state *cs)
1412{
1413 struct fuse_notify_inval_inode_out outarg;
1414 int err = -EINVAL;
1415
1416 if (size != sizeof(outarg))
1417 goto err;
1418
1419 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1420 if (err)
1421 goto err;
1422 fuse_copy_finish(cs);
1423
1424 down_read(&fc->killsb);
1425 err = -ENOENT;
Miklos Szeredib21dda42010-02-05 12:08:31 +01001426 if (fc->sb) {
1427 err = fuse_reverse_inval_inode(fc->sb, outarg.ino,
1428 outarg.off, outarg.len);
1429 }
John Muir3b463ae2009-05-31 11:13:57 -04001430 up_read(&fc->killsb);
1431 return err;
1432
1433err:
1434 fuse_copy_finish(cs);
1435 return err;
1436}
1437
1438static int fuse_notify_inval_entry(struct fuse_conn *fc, unsigned int size,
1439 struct fuse_copy_state *cs)
1440{
1441 struct fuse_notify_inval_entry_out outarg;
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001442 int err = -ENOMEM;
1443 char *buf;
John Muir3b463ae2009-05-31 11:13:57 -04001444 struct qstr name;
1445
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001446 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1447 if (!buf)
1448 goto err;
1449
1450 err = -EINVAL;
John Muir3b463ae2009-05-31 11:13:57 -04001451 if (size < sizeof(outarg))
1452 goto err;
1453
1454 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1455 if (err)
1456 goto err;
1457
1458 err = -ENAMETOOLONG;
1459 if (outarg.namelen > FUSE_NAME_MAX)
1460 goto err;
1461
Miklos Szeredic2183d12011-08-24 10:20:17 +02001462 err = -EINVAL;
1463 if (size != sizeof(outarg) + outarg.namelen + 1)
1464 goto err;
1465
John Muir3b463ae2009-05-31 11:13:57 -04001466 name.name = buf;
1467 name.len = outarg.namelen;
1468 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1469 if (err)
1470 goto err;
1471 fuse_copy_finish(cs);
1472 buf[outarg.namelen] = 0;
1473 name.hash = full_name_hash(name.name, name.len);
1474
1475 down_read(&fc->killsb);
1476 err = -ENOENT;
Miklos Szeredib21dda42010-02-05 12:08:31 +01001477 if (fc->sb)
John Muir451d0f52011-12-06 21:50:06 +01001478 err = fuse_reverse_inval_entry(fc->sb, outarg.parent, 0, &name);
1479 up_read(&fc->killsb);
1480 kfree(buf);
1481 return err;
1482
1483err:
1484 kfree(buf);
1485 fuse_copy_finish(cs);
1486 return err;
1487}
1488
1489static int fuse_notify_delete(struct fuse_conn *fc, unsigned int size,
1490 struct fuse_copy_state *cs)
1491{
1492 struct fuse_notify_delete_out outarg;
1493 int err = -ENOMEM;
1494 char *buf;
1495 struct qstr name;
1496
1497 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1498 if (!buf)
1499 goto err;
1500
1501 err = -EINVAL;
1502 if (size < sizeof(outarg))
1503 goto err;
1504
1505 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1506 if (err)
1507 goto err;
1508
1509 err = -ENAMETOOLONG;
1510 if (outarg.namelen > FUSE_NAME_MAX)
1511 goto err;
1512
1513 err = -EINVAL;
1514 if (size != sizeof(outarg) + outarg.namelen + 1)
1515 goto err;
1516
1517 name.name = buf;
1518 name.len = outarg.namelen;
1519 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1520 if (err)
1521 goto err;
1522 fuse_copy_finish(cs);
1523 buf[outarg.namelen] = 0;
1524 name.hash = full_name_hash(name.name, name.len);
1525
1526 down_read(&fc->killsb);
1527 err = -ENOENT;
1528 if (fc->sb)
1529 err = fuse_reverse_inval_entry(fc->sb, outarg.parent,
1530 outarg.child, &name);
John Muir3b463ae2009-05-31 11:13:57 -04001531 up_read(&fc->killsb);
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001532 kfree(buf);
John Muir3b463ae2009-05-31 11:13:57 -04001533 return err;
1534
1535err:
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001536 kfree(buf);
John Muir3b463ae2009-05-31 11:13:57 -04001537 fuse_copy_finish(cs);
1538 return err;
1539}
1540
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001541static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
1542 struct fuse_copy_state *cs)
1543{
1544 struct fuse_notify_store_out outarg;
1545 struct inode *inode;
1546 struct address_space *mapping;
1547 u64 nodeid;
1548 int err;
1549 pgoff_t index;
1550 unsigned int offset;
1551 unsigned int num;
1552 loff_t file_size;
1553 loff_t end;
1554
1555 err = -EINVAL;
1556 if (size < sizeof(outarg))
1557 goto out_finish;
1558
1559 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1560 if (err)
1561 goto out_finish;
1562
1563 err = -EINVAL;
1564 if (size - sizeof(outarg) != outarg.size)
1565 goto out_finish;
1566
1567 nodeid = outarg.nodeid;
1568
1569 down_read(&fc->killsb);
1570
1571 err = -ENOENT;
1572 if (!fc->sb)
1573 goto out_up_killsb;
1574
1575 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1576 if (!inode)
1577 goto out_up_killsb;
1578
1579 mapping = inode->i_mapping;
1580 index = outarg.offset >> PAGE_CACHE_SHIFT;
1581 offset = outarg.offset & ~PAGE_CACHE_MASK;
1582 file_size = i_size_read(inode);
1583 end = outarg.offset + outarg.size;
1584 if (end > file_size) {
1585 file_size = end;
1586 fuse_write_update_size(inode, file_size);
1587 }
1588
1589 num = outarg.size;
1590 while (num) {
1591 struct page *page;
1592 unsigned int this_num;
1593
1594 err = -ENOMEM;
1595 page = find_or_create_page(mapping, index,
1596 mapping_gfp_mask(mapping));
1597 if (!page)
1598 goto out_iput;
1599
1600 this_num = min_t(unsigned, num, PAGE_CACHE_SIZE - offset);
1601 err = fuse_copy_page(cs, &page, offset, this_num, 0);
1602 if (!err && offset == 0 && (num != 0 || file_size == end))
1603 SetPageUptodate(page);
1604 unlock_page(page);
1605 page_cache_release(page);
1606
1607 if (err)
1608 goto out_iput;
1609
1610 num -= this_num;
1611 offset = 0;
1612 index++;
1613 }
1614
1615 err = 0;
1616
1617out_iput:
1618 iput(inode);
1619out_up_killsb:
1620 up_read(&fc->killsb);
1621out_finish:
1622 fuse_copy_finish(cs);
1623 return err;
1624}
1625
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001626static void fuse_retrieve_end(struct fuse_conn *fc, struct fuse_req *req)
1627{
Miklos Szeredi0be85572010-10-27 15:34:46 -07001628 release_pages(req->pages, req->num_pages, 0);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001629}
1630
1631static int fuse_retrieve(struct fuse_conn *fc, struct inode *inode,
1632 struct fuse_notify_retrieve_out *outarg)
1633{
1634 int err;
1635 struct address_space *mapping = inode->i_mapping;
1636 struct fuse_req *req;
1637 pgoff_t index;
1638 loff_t file_size;
1639 unsigned int num;
1640 unsigned int offset;
Geert Uytterhoeven01574432010-09-30 22:06:21 +02001641 size_t total_len = 0;
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001642 int num_pages;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001643
1644 offset = outarg->offset & ~PAGE_CACHE_MASK;
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001645 file_size = i_size_read(inode);
1646
1647 num = outarg->size;
1648 if (outarg->offset > file_size)
1649 num = 0;
1650 else if (outarg->offset + num > file_size)
1651 num = file_size - outarg->offset;
1652
1653 num_pages = (num + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1654 num_pages = min(num_pages, FUSE_MAX_PAGES_PER_REQ);
1655
1656 req = fuse_get_req(fc, num_pages);
1657 if (IS_ERR(req))
1658 return PTR_ERR(req);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001659
1660 req->in.h.opcode = FUSE_NOTIFY_REPLY;
1661 req->in.h.nodeid = outarg->nodeid;
1662 req->in.numargs = 2;
1663 req->in.argpages = 1;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001664 req->page_descs[0].offset = offset;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001665 req->end = fuse_retrieve_end;
1666
1667 index = outarg->offset >> PAGE_CACHE_SHIFT;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001668
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001669 while (num && req->num_pages < num_pages) {
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001670 struct page *page;
1671 unsigned int this_num;
1672
1673 page = find_get_page(mapping, index);
1674 if (!page)
1675 break;
1676
1677 this_num = min_t(unsigned, num, PAGE_CACHE_SIZE - offset);
1678 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001679 req->page_descs[req->num_pages].length = this_num;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001680 req->num_pages++;
1681
Miklos Szeredic9e67d42012-09-04 18:45:54 +02001682 offset = 0;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001683 num -= this_num;
1684 total_len += this_num;
Miklos Szeredi48706d02011-12-13 10:36:59 +01001685 index++;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001686 }
1687 req->misc.retrieve_in.offset = outarg->offset;
1688 req->misc.retrieve_in.size = total_len;
1689 req->in.args[0].size = sizeof(req->misc.retrieve_in);
1690 req->in.args[0].value = &req->misc.retrieve_in;
1691 req->in.args[1].size = total_len;
1692
1693 err = fuse_request_send_notify_reply(fc, req, outarg->notify_unique);
1694 if (err)
1695 fuse_retrieve_end(fc, req);
1696
1697 return err;
1698}
1699
1700static int fuse_notify_retrieve(struct fuse_conn *fc, unsigned int size,
1701 struct fuse_copy_state *cs)
1702{
1703 struct fuse_notify_retrieve_out outarg;
1704 struct inode *inode;
1705 int err;
1706
1707 err = -EINVAL;
1708 if (size != sizeof(outarg))
1709 goto copy_finish;
1710
1711 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1712 if (err)
1713 goto copy_finish;
1714
1715 fuse_copy_finish(cs);
1716
1717 down_read(&fc->killsb);
1718 err = -ENOENT;
1719 if (fc->sb) {
1720 u64 nodeid = outarg.nodeid;
1721
1722 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1723 if (inode) {
1724 err = fuse_retrieve(fc, inode, &outarg);
1725 iput(inode);
1726 }
1727 }
1728 up_read(&fc->killsb);
1729
1730 return err;
1731
1732copy_finish:
1733 fuse_copy_finish(cs);
1734 return err;
1735}
1736
Tejun Heo85993962008-11-26 12:03:55 +01001737static int fuse_notify(struct fuse_conn *fc, enum fuse_notify_code code,
1738 unsigned int size, struct fuse_copy_state *cs)
1739{
1740 switch (code) {
Tejun Heo95668a62008-11-26 12:03:55 +01001741 case FUSE_NOTIFY_POLL:
1742 return fuse_notify_poll(fc, size, cs);
1743
John Muir3b463ae2009-05-31 11:13:57 -04001744 case FUSE_NOTIFY_INVAL_INODE:
1745 return fuse_notify_inval_inode(fc, size, cs);
1746
1747 case FUSE_NOTIFY_INVAL_ENTRY:
1748 return fuse_notify_inval_entry(fc, size, cs);
1749
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001750 case FUSE_NOTIFY_STORE:
1751 return fuse_notify_store(fc, size, cs);
1752
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001753 case FUSE_NOTIFY_RETRIEVE:
1754 return fuse_notify_retrieve(fc, size, cs);
1755
John Muir451d0f52011-12-06 21:50:06 +01001756 case FUSE_NOTIFY_DELETE:
1757 return fuse_notify_delete(fc, size, cs);
1758
Tejun Heo85993962008-11-26 12:03:55 +01001759 default:
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001760 fuse_copy_finish(cs);
Tejun Heo85993962008-11-26 12:03:55 +01001761 return -EINVAL;
1762 }
1763}
1764
Miklos Szeredi334f4852005-09-09 13:10:27 -07001765/* Look up request on processing list by unique ID */
1766static struct fuse_req *request_find(struct fuse_conn *fc, u64 unique)
1767{
Dong Fang05726ac2013-07-30 22:50:01 -04001768 struct fuse_req *req;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001769
Dong Fang05726ac2013-07-30 22:50:01 -04001770 list_for_each_entry(req, &fc->processing, list) {
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001771 if (req->in.h.unique == unique || req->intr_unique == unique)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001772 return req;
1773 }
1774 return NULL;
1775}
1776
1777static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
1778 unsigned nbytes)
1779{
1780 unsigned reqsize = sizeof(struct fuse_out_header);
1781
1782 if (out->h.error)
1783 return nbytes != reqsize ? -EINVAL : 0;
1784
1785 reqsize += len_args(out->numargs, out->args);
1786
1787 if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
1788 return -EINVAL;
1789 else if (reqsize > nbytes) {
1790 struct fuse_arg *lastarg = &out->args[out->numargs-1];
1791 unsigned diffsize = reqsize - nbytes;
1792 if (diffsize > lastarg->size)
1793 return -EINVAL;
1794 lastarg->size -= diffsize;
1795 }
1796 return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
1797 out->page_zeroing);
1798}
1799
1800/*
1801 * Write a single reply to a request. First the header is copied from
1802 * the write buffer. The request is then searched on the processing
1803 * list by the unique ID found in the header. If found, then remove
1804 * it from the list and copy the rest of the buffer to the request.
1805 * The request is finished by calling request_end()
1806 */
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001807static ssize_t fuse_dev_do_write(struct fuse_conn *fc,
1808 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001809{
1810 int err;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001811 struct fuse_req *req;
1812 struct fuse_out_header oh;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001813
Miklos Szeredi334f4852005-09-09 13:10:27 -07001814 if (nbytes < sizeof(struct fuse_out_header))
1815 return -EINVAL;
1816
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001817 err = fuse_copy_one(cs, &oh, sizeof(oh));
Miklos Szeredi334f4852005-09-09 13:10:27 -07001818 if (err)
1819 goto err_finish;
Tejun Heo85993962008-11-26 12:03:55 +01001820
Miklos Szeredi334f4852005-09-09 13:10:27 -07001821 err = -EINVAL;
Tejun Heo85993962008-11-26 12:03:55 +01001822 if (oh.len != nbytes)
1823 goto err_finish;
1824
1825 /*
1826 * Zero oh.unique indicates unsolicited notification message
1827 * and error contains notification code.
1828 */
1829 if (!oh.unique) {
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001830 err = fuse_notify(fc, oh.error, nbytes - sizeof(oh), cs);
Tejun Heo85993962008-11-26 12:03:55 +01001831 return err ? err : nbytes;
1832 }
1833
1834 err = -EINVAL;
1835 if (oh.error <= -1000 || oh.error > 0)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001836 goto err_finish;
1837
Miklos Szeredid7133112006-04-10 22:54:55 -07001838 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001839 err = -ENOENT;
1840 if (!fc->connected)
1841 goto err_unlock;
1842
Miklos Szeredi334f4852005-09-09 13:10:27 -07001843 req = request_find(fc, oh.unique);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001844 if (!req)
1845 goto err_unlock;
1846
Miklos Szeredif9a28422006-06-25 05:48:53 -07001847 if (req->aborted) {
Miklos Szeredid7133112006-04-10 22:54:55 -07001848 spin_unlock(&fc->lock);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001849 fuse_copy_finish(cs);
Miklos Szeredid7133112006-04-10 22:54:55 -07001850 spin_lock(&fc->lock);
Miklos Szeredi222f1d62006-01-16 22:14:25 -08001851 request_end(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001852 return -ENOENT;
1853 }
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001854 /* Is it an interrupt reply? */
1855 if (req->intr_unique == oh.unique) {
1856 err = -EINVAL;
1857 if (nbytes != sizeof(struct fuse_out_header))
1858 goto err_unlock;
1859
1860 if (oh.error == -ENOSYS)
1861 fc->no_interrupt = 1;
1862 else if (oh.error == -EAGAIN)
1863 queue_interrupt(fc, req);
1864
1865 spin_unlock(&fc->lock);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001866 fuse_copy_finish(cs);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001867 return nbytes;
1868 }
1869
1870 req->state = FUSE_REQ_WRITING;
Miklos Szeredid77a1d52006-01-16 22:14:31 -08001871 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001872 req->out.h = oh;
1873 req->locked = 1;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001874 cs->req = req;
Miklos Szeredice534fb2010-05-25 15:06:07 +02001875 if (!req->out.page_replace)
1876 cs->move_pages = 0;
Miklos Szeredid7133112006-04-10 22:54:55 -07001877 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001878
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001879 err = copy_out_args(cs, &req->out, nbytes);
1880 fuse_copy_finish(cs);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001881
Miklos Szeredid7133112006-04-10 22:54:55 -07001882 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001883 req->locked = 0;
1884 if (!err) {
Miklos Szeredif9a28422006-06-25 05:48:53 -07001885 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001886 err = -ENOENT;
Miklos Szeredif9a28422006-06-25 05:48:53 -07001887 } else if (!req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001888 req->out.h.error = -EIO;
1889 request_end(fc, req);
1890
1891 return err ? err : nbytes;
1892
1893 err_unlock:
Miklos Szeredid7133112006-04-10 22:54:55 -07001894 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001895 err_finish:
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001896 fuse_copy_finish(cs);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001897 return err;
1898}
1899
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001900static ssize_t fuse_dev_write(struct kiocb *iocb, const struct iovec *iov,
1901 unsigned long nr_segs, loff_t pos)
1902{
1903 struct fuse_copy_state cs;
1904 struct fuse_conn *fc = fuse_get_conn(iocb->ki_filp);
1905 if (!fc)
1906 return -EPERM;
1907
Miklos Szeredic3021622010-05-25 15:06:07 +02001908 fuse_copy_init(&cs, fc, 0, iov, nr_segs);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001909
1910 return fuse_dev_do_write(fc, &cs, iov_length(iov, nr_segs));
1911}
1912
1913static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe,
1914 struct file *out, loff_t *ppos,
1915 size_t len, unsigned int flags)
1916{
1917 unsigned nbuf;
1918 unsigned idx;
1919 struct pipe_buffer *bufs;
1920 struct fuse_copy_state cs;
1921 struct fuse_conn *fc;
1922 size_t rem;
1923 ssize_t ret;
1924
1925 fc = fuse_get_conn(out);
1926 if (!fc)
1927 return -EPERM;
1928
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001929 bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), GFP_KERNEL);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001930 if (!bufs)
1931 return -ENOMEM;
1932
1933 pipe_lock(pipe);
1934 nbuf = 0;
1935 rem = 0;
1936 for (idx = 0; idx < pipe->nrbufs && rem < len; idx++)
1937 rem += pipe->bufs[(pipe->curbuf + idx) & (pipe->buffers - 1)].len;
1938
1939 ret = -EINVAL;
1940 if (rem < len) {
1941 pipe_unlock(pipe);
1942 goto out;
1943 }
1944
1945 rem = len;
1946 while (rem) {
1947 struct pipe_buffer *ibuf;
1948 struct pipe_buffer *obuf;
1949
1950 BUG_ON(nbuf >= pipe->buffers);
1951 BUG_ON(!pipe->nrbufs);
1952 ibuf = &pipe->bufs[pipe->curbuf];
1953 obuf = &bufs[nbuf];
1954
1955 if (rem >= ibuf->len) {
1956 *obuf = *ibuf;
1957 ibuf->ops = NULL;
1958 pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
1959 pipe->nrbufs--;
1960 } else {
1961 ibuf->ops->get(pipe, ibuf);
1962 *obuf = *ibuf;
1963 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1964 obuf->len = rem;
1965 ibuf->offset += obuf->len;
1966 ibuf->len -= obuf->len;
1967 }
1968 nbuf++;
1969 rem -= obuf->len;
1970 }
1971 pipe_unlock(pipe);
1972
Miklos Szeredic3021622010-05-25 15:06:07 +02001973 fuse_copy_init(&cs, fc, 0, NULL, nbuf);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001974 cs.pipebufs = bufs;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001975 cs.pipe = pipe;
1976
Miklos Szeredice534fb2010-05-25 15:06:07 +02001977 if (flags & SPLICE_F_MOVE)
1978 cs.move_pages = 1;
1979
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001980 ret = fuse_dev_do_write(fc, &cs, len);
1981
1982 for (idx = 0; idx < nbuf; idx++) {
1983 struct pipe_buffer *buf = &bufs[idx];
1984 buf->ops->release(pipe, buf);
1985 }
1986out:
1987 kfree(bufs);
1988 return ret;
1989}
1990
Miklos Szeredi334f4852005-09-09 13:10:27 -07001991static unsigned fuse_dev_poll(struct file *file, poll_table *wait)
1992{
Miklos Szeredi334f4852005-09-09 13:10:27 -07001993 unsigned mask = POLLOUT | POLLWRNORM;
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07001994 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001995 if (!fc)
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07001996 return POLLERR;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001997
1998 poll_wait(file, &fc->waitq, wait);
1999
Miklos Szeredid7133112006-04-10 22:54:55 -07002000 spin_lock(&fc->lock);
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07002001 if (!fc->connected)
2002 mask = POLLERR;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07002003 else if (request_pending(fc))
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07002004 mask |= POLLIN | POLLRDNORM;
Miklos Szeredid7133112006-04-10 22:54:55 -07002005 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002006
2007 return mask;
2008}
2009
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002010/*
2011 * Abort all requests on the given list (pending or processing)
2012 *
Miklos Szeredid7133112006-04-10 22:54:55 -07002013 * This function releases and reacquires fc->lock
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002014 */
Miklos Szeredi334f4852005-09-09 13:10:27 -07002015static void end_requests(struct fuse_conn *fc, struct list_head *head)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02002016__releases(fc->lock)
2017__acquires(fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -07002018{
2019 while (!list_empty(head)) {
2020 struct fuse_req *req;
2021 req = list_entry(head->next, struct fuse_req, list);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002022 req->out.h.error = -ECONNABORTED;
2023 request_end(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -07002024 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002025 }
2026}
2027
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002028/*
2029 * Abort requests under I/O
2030 *
Miklos Szeredif9a28422006-06-25 05:48:53 -07002031 * The requests are set to aborted and finished, and the request
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002032 * waiter is woken up. This will make request_wait_answer() wait
2033 * until the request is unlocked and then return.
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08002034 *
2035 * If the request is asynchronous, then the end function needs to be
2036 * called after waiting for the request to be unlocked (if it was
2037 * locked).
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002038 */
2039static void end_io_requests(struct fuse_conn *fc)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02002040__releases(fc->lock)
2041__acquires(fc->lock)
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002042{
2043 while (!list_empty(&fc->io)) {
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08002044 struct fuse_req *req =
2045 list_entry(fc->io.next, struct fuse_req, list);
2046 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
2047
Miklos Szeredif9a28422006-06-25 05:48:53 -07002048 req->aborted = 1;
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002049 req->out.h.error = -ECONNABORTED;
2050 req->state = FUSE_REQ_FINISHED;
2051 list_del_init(&req->list);
2052 wake_up(&req->waitq);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08002053 if (end) {
2054 req->end = NULL;
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08002055 __fuse_get_request(req);
Miklos Szeredid7133112006-04-10 22:54:55 -07002056 spin_unlock(&fc->lock);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08002057 wait_event(req->waitq, !req->locked);
2058 end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +01002059 fuse_put_request(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -07002060 spin_lock(&fc->lock);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08002061 }
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002062 }
2063}
2064
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002065static void end_queued_requests(struct fuse_conn *fc)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02002066__releases(fc->lock)
2067__acquires(fc->lock)
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002068{
2069 fc->max_background = UINT_MAX;
2070 flush_bg_queue(fc);
2071 end_requests(fc, &fc->pending);
2072 end_requests(fc, &fc->processing);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01002073 while (forget_pending(fc))
Miklos Szeredi02c048b2010-12-07 20:16:56 +01002074 kfree(dequeue_forget(fc, 1, NULL));
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002075}
2076
Bryan Green357ccf22011-03-01 16:43:52 -08002077static void end_polls(struct fuse_conn *fc)
2078{
2079 struct rb_node *p;
2080
2081 p = rb_first(&fc->polled_files);
2082
2083 while (p) {
2084 struct fuse_file *ff;
2085 ff = rb_entry(p, struct fuse_file, polled_node);
2086 wake_up_interruptible_all(&ff->poll_wait);
2087
2088 p = rb_next(p);
2089 }
2090}
2091
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002092/*
2093 * Abort all requests.
2094 *
2095 * Emergency exit in case of a malicious or accidental deadlock, or
2096 * just a hung filesystem.
2097 *
2098 * The same effect is usually achievable through killing the
2099 * filesystem daemon and all users of the filesystem. The exception
2100 * is the combination of an asynchronous request and the tricky
2101 * deadlock (see Documentation/filesystems/fuse.txt).
2102 *
2103 * During the aborting, progression of requests from the pending and
2104 * processing lists onto the io list, and progression of new requests
2105 * onto the pending list is prevented by req->connected being false.
2106 *
2107 * Progression of requests under I/O to the processing list is
Miklos Szeredif9a28422006-06-25 05:48:53 -07002108 * prevented by the req->aborted flag being true for these requests.
2109 * For this reason requests on the io list must be aborted first.
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002110 */
2111void fuse_abort_conn(struct fuse_conn *fc)
2112{
Miklos Szeredid7133112006-04-10 22:54:55 -07002113 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002114 if (fc->connected) {
2115 fc->connected = 0;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -07002116 fc->blocked = 0;
Maxim Patlasov796523fb2013-03-21 18:02:15 +04002117 fc->initialized = 1;
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002118 end_io_requests(fc);
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002119 end_queued_requests(fc);
Bryan Green357ccf22011-03-01 16:43:52 -08002120 end_polls(fc);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002121 wake_up_all(&fc->waitq);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -07002122 wake_up_all(&fc->blocked_waitq);
Jeff Dike385a17b2006-04-10 22:54:52 -07002123 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002124 }
Miklos Szeredid7133112006-04-10 22:54:55 -07002125 spin_unlock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002126}
Tejun Heo08cbf542009-04-14 10:54:53 +09002127EXPORT_SYMBOL_GPL(fuse_abort_conn);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002128
Tejun Heo08cbf542009-04-14 10:54:53 +09002129int fuse_dev_release(struct inode *inode, struct file *file)
Miklos Szeredi334f4852005-09-09 13:10:27 -07002130{
Miklos Szeredi0720b312006-04-10 22:54:55 -07002131 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002132 if (fc) {
Miklos Szeredid7133112006-04-10 22:54:55 -07002133 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -07002134 fc->connected = 0;
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002135 fc->blocked = 0;
Maxim Patlasov0aada882013-03-21 18:02:28 +04002136 fc->initialized = 1;
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002137 end_queued_requests(fc);
Bryan Green357ccf22011-03-01 16:43:52 -08002138 end_polls(fc);
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002139 wake_up_all(&fc->blocked_waitq);
Miklos Szeredid7133112006-04-10 22:54:55 -07002140 spin_unlock(&fc->lock);
Miklos Szeredibafa9652006-06-25 05:48:51 -07002141 fuse_conn_put(fc);
Jeff Dike385a17b2006-04-10 22:54:52 -07002142 }
Miklos Szeredif543f252006-01-16 22:14:35 -08002143
Miklos Szeredi334f4852005-09-09 13:10:27 -07002144 return 0;
2145}
Tejun Heo08cbf542009-04-14 10:54:53 +09002146EXPORT_SYMBOL_GPL(fuse_dev_release);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002147
Jeff Dike385a17b2006-04-10 22:54:52 -07002148static int fuse_dev_fasync(int fd, struct file *file, int on)
2149{
2150 struct fuse_conn *fc = fuse_get_conn(file);
2151 if (!fc)
Miklos Szeredia87046d2006-04-10 22:54:56 -07002152 return -EPERM;
Jeff Dike385a17b2006-04-10 22:54:52 -07002153
2154 /* No locking - fasync_helper does its own locking */
2155 return fasync_helper(fd, file, on, &fc->fasync);
2156}
2157
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002158const struct file_operations fuse_dev_operations = {
Miklos Szeredi334f4852005-09-09 13:10:27 -07002159 .owner = THIS_MODULE,
2160 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002161 .read = do_sync_read,
2162 .aio_read = fuse_dev_read,
Miklos Szeredic3021622010-05-25 15:06:07 +02002163 .splice_read = fuse_dev_splice_read,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002164 .write = do_sync_write,
2165 .aio_write = fuse_dev_write,
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002166 .splice_write = fuse_dev_splice_write,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002167 .poll = fuse_dev_poll,
2168 .release = fuse_dev_release,
Jeff Dike385a17b2006-04-10 22:54:52 -07002169 .fasync = fuse_dev_fasync,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002170};
Tejun Heo08cbf542009-04-14 10:54:53 +09002171EXPORT_SYMBOL_GPL(fuse_dev_operations);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002172
2173static struct miscdevice fuse_miscdevice = {
2174 .minor = FUSE_MINOR,
2175 .name = "fuse",
2176 .fops = &fuse_dev_operations,
2177};
2178
2179int __init fuse_dev_init(void)
2180{
2181 int err = -ENOMEM;
2182 fuse_req_cachep = kmem_cache_create("fuse_request",
2183 sizeof(struct fuse_req),
Paul Mundt20c2df82007-07-20 10:11:58 +09002184 0, 0, NULL);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002185 if (!fuse_req_cachep)
2186 goto out;
2187
2188 err = misc_register(&fuse_miscdevice);
2189 if (err)
2190 goto out_cache_clean;
2191
2192 return 0;
2193
2194 out_cache_clean:
2195 kmem_cache_destroy(fuse_req_cachep);
2196 out:
2197 return err;
2198}
2199
2200void fuse_dev_cleanup(void)
2201{
2202 misc_deregister(&fuse_miscdevice);
2203 kmem_cache_destroy(fuse_req_cachep);
2204}