blob: c847d6b225e26eee2f143e52443f502d21b5186a [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
Miklos Szeredi21f62172015-01-06 10:45:35 +0100514static void fuse_adjust_compat(struct fuse_conn *fc, struct fuse_args *args)
515{
516 if (fc->minor < 4 && args->in.h.opcode == FUSE_STATFS)
517 args->out.args[0].size = FUSE_COMPAT_STATFS_SIZE;
518
519 if (fc->minor < 9) {
520 switch (args->in.h.opcode) {
521 case FUSE_LOOKUP:
522 case FUSE_CREATE:
523 case FUSE_MKNOD:
524 case FUSE_MKDIR:
525 case FUSE_SYMLINK:
526 case FUSE_LINK:
527 args->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
528 break;
529 case FUSE_GETATTR:
530 case FUSE_SETATTR:
531 args->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
532 break;
533 }
534 }
535 if (fc->minor < 12) {
536 switch (args->in.h.opcode) {
537 case FUSE_CREATE:
538 args->in.args[0].size = sizeof(struct fuse_open_in);
539 break;
540 case FUSE_MKNOD:
541 args->in.args[0].size = FUSE_COMPAT_MKNOD_IN_SIZE;
542 break;
543 }
544 }
545}
546
Miklos Szeredi70781872014-12-12 09:49:05 +0100547ssize_t fuse_simple_request(struct fuse_conn *fc, struct fuse_args *args)
548{
549 struct fuse_req *req;
550 ssize_t ret;
551
552 req = fuse_get_req(fc, 0);
553 if (IS_ERR(req))
554 return PTR_ERR(req);
555
Miklos Szeredi21f62172015-01-06 10:45:35 +0100556 /* Needs to be done after fuse_get_req() so that fc->minor is valid */
557 fuse_adjust_compat(fc, args);
558
Miklos Szeredi70781872014-12-12 09:49:05 +0100559 req->in.h.opcode = args->in.h.opcode;
560 req->in.h.nodeid = args->in.h.nodeid;
561 req->in.numargs = args->in.numargs;
562 memcpy(req->in.args, args->in.args,
563 args->in.numargs * sizeof(struct fuse_in_arg));
564 req->out.argvar = args->out.argvar;
565 req->out.numargs = args->out.numargs;
566 memcpy(req->out.args, args->out.args,
567 args->out.numargs * sizeof(struct fuse_arg));
568 fuse_request_send(fc, req);
569 ret = req->out.h.error;
570 if (!ret && args->out.argvar) {
571 BUG_ON(args->out.numargs != 1);
572 ret = req->out.args[0].size;
573 }
574 fuse_put_request(fc, req);
575
576 return ret;
577}
578
Tejun Heob93f8582008-11-26 12:03:55 +0100579static void fuse_request_send_nowait_locked(struct fuse_conn *fc,
580 struct fuse_req *req)
Miklos Szeredid12def12008-02-06 01:38:39 -0800581{
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400582 BUG_ON(!req->background);
Miklos Szeredid12def12008-02-06 01:38:39 -0800583 fc->num_background++;
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700584 if (fc->num_background == fc->max_background)
Miklos Szeredid12def12008-02-06 01:38:39 -0800585 fc->blocked = 1;
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700586 if (fc->num_background == fc->congestion_threshold &&
Tejun Heoa325f9b2009-04-14 10:54:52 +0900587 fc->bdi_initialized) {
Jens Axboe8aa7e842009-07-09 14:52:32 +0200588 set_bdi_congested(&fc->bdi, BLK_RW_SYNC);
589 set_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
Miklos Szeredid12def12008-02-06 01:38:39 -0800590 }
591 list_add_tail(&req->list, &fc->bg_queue);
592 flush_bg_queue(fc);
593}
594
Tejun Heob93f8582008-11-26 12:03:55 +0100595static void fuse_request_send_nowait(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700596{
Miklos Szeredid7133112006-04-10 22:54:55 -0700597 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700598 if (fc->connected) {
Tejun Heob93f8582008-11-26 12:03:55 +0100599 fuse_request_send_nowait_locked(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700600 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700601 } else {
602 req->out.h.error = -ENOTCONN;
603 request_end(fc, req);
604 }
605}
606
Tejun Heob93f8582008-11-26 12:03:55 +0100607void fuse_request_send_background(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700608{
609 req->isreply = 1;
Tejun Heob93f8582008-11-26 12:03:55 +0100610 fuse_request_send_nowait(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700611}
Tejun Heo08cbf542009-04-14 10:54:53 +0900612EXPORT_SYMBOL_GPL(fuse_request_send_background);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700613
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200614static int fuse_request_send_notify_reply(struct fuse_conn *fc,
615 struct fuse_req *req, u64 unique)
616{
617 int err = -ENODEV;
618
619 req->isreply = 0;
620 req->in.h.unique = unique;
621 spin_lock(&fc->lock);
622 if (fc->connected) {
623 queue_request(fc, req);
624 err = 0;
625 }
626 spin_unlock(&fc->lock);
627
628 return err;
629}
630
Miklos Szeredi334f4852005-09-09 13:10:27 -0700631/*
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700632 * Called under fc->lock
633 *
634 * fc->connected must have been checked previously
635 */
Tejun Heob93f8582008-11-26 12:03:55 +0100636void fuse_request_send_background_locked(struct fuse_conn *fc,
637 struct fuse_req *req)
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700638{
639 req->isreply = 1;
Tejun Heob93f8582008-11-26 12:03:55 +0100640 fuse_request_send_nowait_locked(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700641}
642
Anand V. Avati0b05b182012-08-19 08:53:23 -0400643void fuse_force_forget(struct file *file, u64 nodeid)
644{
Al Viro6131ffa2013-02-27 16:59:05 -0500645 struct inode *inode = file_inode(file);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400646 struct fuse_conn *fc = get_fuse_conn(inode);
647 struct fuse_req *req;
648 struct fuse_forget_in inarg;
649
650 memset(&inarg, 0, sizeof(inarg));
651 inarg.nlookup = 1;
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400652 req = fuse_get_req_nofail_nopages(fc, file);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400653 req->in.h.opcode = FUSE_FORGET;
654 req->in.h.nodeid = nodeid;
655 req->in.numargs = 1;
656 req->in.args[0].size = sizeof(inarg);
657 req->in.args[0].value = &inarg;
658 req->isreply = 0;
Eric Wong6a4e9222013-02-04 13:04:44 +0000659 __fuse_request_send(fc, req);
660 /* ignore errors */
661 fuse_put_request(fc, req);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400662}
663
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700664/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700665 * Lock the request. Up to the next unlock_request() there mustn't be
666 * anything that could cause a page-fault. If the request was already
Miklos Szeredif9a28422006-06-25 05:48:53 -0700667 * aborted bail out.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700668 */
Miklos Szeredid7133112006-04-10 22:54:55 -0700669static int lock_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700670{
671 int err = 0;
672 if (req) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700673 spin_lock(&fc->lock);
Miklos Szeredif9a28422006-06-25 05:48:53 -0700674 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700675 err = -ENOENT;
676 else
677 req->locked = 1;
Miklos Szeredid7133112006-04-10 22:54:55 -0700678 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700679 }
680 return err;
681}
682
683/*
Miklos Szeredif9a28422006-06-25 05:48:53 -0700684 * Unlock request. If it was aborted during being locked, the
Miklos Szeredi334f4852005-09-09 13:10:27 -0700685 * requester thread is currently waiting for it to be unlocked, so
686 * wake it up.
687 */
Miklos Szeredid7133112006-04-10 22:54:55 -0700688static void unlock_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700689{
690 if (req) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700691 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700692 req->locked = 0;
Miklos Szeredif9a28422006-06-25 05:48:53 -0700693 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700694 wake_up(&req->waitq);
Miklos Szeredid7133112006-04-10 22:54:55 -0700695 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700696 }
697}
698
699struct fuse_copy_state {
Miklos Szeredid7133112006-04-10 22:54:55 -0700700 struct fuse_conn *fc;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700701 int write;
702 struct fuse_req *req;
703 const struct iovec *iov;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200704 struct pipe_buffer *pipebufs;
705 struct pipe_buffer *currbuf;
706 struct pipe_inode_info *pipe;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700707 unsigned long nr_segs;
708 unsigned long seglen;
709 unsigned long addr;
710 struct page *pg;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700711 unsigned len;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200712 unsigned offset;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200713 unsigned move_pages:1;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700714};
715
Miklos Szeredid7133112006-04-10 22:54:55 -0700716static void fuse_copy_init(struct fuse_copy_state *cs, struct fuse_conn *fc,
Miklos Szeredic3021622010-05-25 15:06:07 +0200717 int write,
Miklos Szeredid7133112006-04-10 22:54:55 -0700718 const struct iovec *iov, unsigned long nr_segs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700719{
720 memset(cs, 0, sizeof(*cs));
Miklos Szeredid7133112006-04-10 22:54:55 -0700721 cs->fc = fc;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700722 cs->write = write;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700723 cs->iov = iov;
724 cs->nr_segs = nr_segs;
725}
726
727/* Unmap and put previous page of userspace buffer */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800728static void fuse_copy_finish(struct fuse_copy_state *cs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700729{
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200730 if (cs->currbuf) {
731 struct pipe_buffer *buf = cs->currbuf;
732
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200733 if (cs->write)
Miklos Szeredic3021622010-05-25 15:06:07 +0200734 buf->len = PAGE_SIZE - cs->len;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200735 cs->currbuf = NULL;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200736 } else if (cs->pg) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700737 if (cs->write) {
738 flush_dcache_page(cs->pg);
739 set_page_dirty_lock(cs->pg);
740 }
741 put_page(cs->pg);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700742 }
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200743 cs->pg = NULL;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700744}
745
746/*
747 * Get another pagefull of userspace buffer, and map it to kernel
748 * address space, and lock request
749 */
750static int fuse_copy_fill(struct fuse_copy_state *cs)
751{
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200752 struct page *page;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700753 int err;
754
Miklos Szeredid7133112006-04-10 22:54:55 -0700755 unlock_request(cs->fc, cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700756 fuse_copy_finish(cs);
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200757 if (cs->pipebufs) {
758 struct pipe_buffer *buf = cs->pipebufs;
759
Miklos Szeredic3021622010-05-25 15:06:07 +0200760 if (!cs->write) {
761 err = buf->ops->confirm(cs->pipe, buf);
762 if (err)
763 return err;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200764
Miklos Szeredic3021622010-05-25 15:06:07 +0200765 BUG_ON(!cs->nr_segs);
766 cs->currbuf = buf;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200767 cs->pg = buf->page;
768 cs->offset = buf->offset;
Miklos Szeredic3021622010-05-25 15:06:07 +0200769 cs->len = buf->len;
Miklos Szeredic3021622010-05-25 15:06:07 +0200770 cs->pipebufs++;
771 cs->nr_segs--;
772 } else {
Miklos Szeredic3021622010-05-25 15:06:07 +0200773 if (cs->nr_segs == cs->pipe->buffers)
774 return -EIO;
775
776 page = alloc_page(GFP_HIGHUSER);
777 if (!page)
778 return -ENOMEM;
779
780 buf->page = page;
781 buf->offset = 0;
782 buf->len = 0;
783
784 cs->currbuf = buf;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200785 cs->pg = page;
786 cs->offset = 0;
Miklos Szeredic3021622010-05-25 15:06:07 +0200787 cs->len = PAGE_SIZE;
788 cs->pipebufs++;
789 cs->nr_segs++;
790 }
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200791 } else {
792 if (!cs->seglen) {
793 BUG_ON(!cs->nr_segs);
794 cs->seglen = cs->iov[0].iov_len;
795 cs->addr = (unsigned long) cs->iov[0].iov_base;
796 cs->iov++;
797 cs->nr_segs--;
798 }
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200799 err = get_user_pages_fast(cs->addr, 1, cs->write, &page);
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200800 if (err < 0)
801 return err;
802 BUG_ON(err != 1);
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200803 cs->pg = page;
804 cs->offset = cs->addr % PAGE_SIZE;
805 cs->len = min(PAGE_SIZE - cs->offset, cs->seglen);
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200806 cs->seglen -= cs->len;
807 cs->addr += cs->len;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700808 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700809
Miklos Szeredid7133112006-04-10 22:54:55 -0700810 return lock_request(cs->fc, cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700811}
812
813/* Do as much copy to/from userspace buffer as we can */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800814static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700815{
816 unsigned ncpy = min(*size, cs->len);
817 if (val) {
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200818 void *pgaddr = kmap_atomic(cs->pg);
819 void *buf = pgaddr + cs->offset;
820
Miklos Szeredi334f4852005-09-09 13:10:27 -0700821 if (cs->write)
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200822 memcpy(buf, *val, ncpy);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700823 else
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200824 memcpy(*val, buf, ncpy);
825
826 kunmap_atomic(pgaddr);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700827 *val += ncpy;
828 }
829 *size -= ncpy;
830 cs->len -= ncpy;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200831 cs->offset += ncpy;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700832 return ncpy;
833}
834
Miklos Szeredice534fb2010-05-25 15:06:07 +0200835static int fuse_check_page(struct page *page)
836{
837 if (page_mapcount(page) ||
838 page->mapping != NULL ||
839 page_count(page) != 1 ||
840 (page->flags & PAGE_FLAGS_CHECK_AT_PREP &
841 ~(1 << PG_locked |
842 1 << PG_referenced |
843 1 << PG_uptodate |
844 1 << PG_lru |
845 1 << PG_active |
846 1 << PG_reclaim))) {
847 printk(KERN_WARNING "fuse: trying to steal weird page\n");
848 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);
849 return 1;
850 }
851 return 0;
852}
853
854static int fuse_try_move_page(struct fuse_copy_state *cs, struct page **pagep)
855{
856 int err;
857 struct page *oldpage = *pagep;
858 struct page *newpage;
859 struct pipe_buffer *buf = cs->pipebufs;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200860
861 unlock_request(cs->fc, cs->req);
862 fuse_copy_finish(cs);
863
864 err = buf->ops->confirm(cs->pipe, buf);
865 if (err)
866 return err;
867
868 BUG_ON(!cs->nr_segs);
869 cs->currbuf = buf;
870 cs->len = buf->len;
871 cs->pipebufs++;
872 cs->nr_segs--;
873
874 if (cs->len != PAGE_SIZE)
875 goto out_fallback;
876
877 if (buf->ops->steal(cs->pipe, buf) != 0)
878 goto out_fallback;
879
880 newpage = buf->page;
881
882 if (WARN_ON(!PageUptodate(newpage)))
883 return -EIO;
884
885 ClearPageMappedToDisk(newpage);
886
887 if (fuse_check_page(newpage) != 0)
888 goto out_fallback_unlock;
889
Miklos Szeredice534fb2010-05-25 15:06:07 +0200890 /*
891 * This is a new and locked page, it shouldn't be mapped or
892 * have any special flags on it
893 */
894 if (WARN_ON(page_mapped(oldpage)))
895 goto out_fallback_unlock;
896 if (WARN_ON(page_has_private(oldpage)))
897 goto out_fallback_unlock;
898 if (WARN_ON(PageDirty(oldpage) || PageWriteback(oldpage)))
899 goto out_fallback_unlock;
900 if (WARN_ON(PageMlocked(oldpage)))
901 goto out_fallback_unlock;
902
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700903 err = replace_page_cache_page(oldpage, newpage, GFP_KERNEL);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200904 if (err) {
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700905 unlock_page(newpage);
906 return err;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200907 }
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700908
Miklos Szeredice534fb2010-05-25 15:06:07 +0200909 page_cache_get(newpage);
910
911 if (!(buf->flags & PIPE_BUF_FLAG_LRU))
912 lru_cache_add_file(newpage);
913
914 err = 0;
915 spin_lock(&cs->fc->lock);
916 if (cs->req->aborted)
917 err = -ENOENT;
918 else
919 *pagep = newpage;
920 spin_unlock(&cs->fc->lock);
921
922 if (err) {
923 unlock_page(newpage);
924 page_cache_release(newpage);
925 return err;
926 }
927
928 unlock_page(oldpage);
929 page_cache_release(oldpage);
930 cs->len = 0;
931
932 return 0;
933
934out_fallback_unlock:
935 unlock_page(newpage);
936out_fallback:
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200937 cs->pg = buf->page;
938 cs->offset = buf->offset;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200939
940 err = lock_request(cs->fc, cs->req);
941 if (err)
942 return err;
943
944 return 1;
945}
946
Miklos Szeredic3021622010-05-25 15:06:07 +0200947static int fuse_ref_page(struct fuse_copy_state *cs, struct page *page,
948 unsigned offset, unsigned count)
949{
950 struct pipe_buffer *buf;
951
952 if (cs->nr_segs == cs->pipe->buffers)
953 return -EIO;
954
955 unlock_request(cs->fc, cs->req);
956 fuse_copy_finish(cs);
957
958 buf = cs->pipebufs;
959 page_cache_get(page);
960 buf->page = page;
961 buf->offset = offset;
962 buf->len = count;
963
964 cs->pipebufs++;
965 cs->nr_segs++;
966 cs->len = 0;
967
968 return 0;
969}
970
Miklos Szeredi334f4852005-09-09 13:10:27 -0700971/*
972 * Copy a page in the request to/from the userspace buffer. Must be
973 * done atomically
974 */
Miklos Szeredice534fb2010-05-25 15:06:07 +0200975static int fuse_copy_page(struct fuse_copy_state *cs, struct page **pagep,
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800976 unsigned offset, unsigned count, int zeroing)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700977{
Miklos Szeredice534fb2010-05-25 15:06:07 +0200978 int err;
979 struct page *page = *pagep;
980
Miklos Szeredib6777c42010-10-26 14:22:27 -0700981 if (page && zeroing && count < PAGE_SIZE)
982 clear_highpage(page);
983
Miklos Szeredi334f4852005-09-09 13:10:27 -0700984 while (count) {
Miklos Szeredic3021622010-05-25 15:06:07 +0200985 if (cs->write && cs->pipebufs && page) {
986 return fuse_ref_page(cs, page, offset, count);
987 } else if (!cs->len) {
Miklos Szeredice534fb2010-05-25 15:06:07 +0200988 if (cs->move_pages && page &&
989 offset == 0 && count == PAGE_SIZE) {
990 err = fuse_try_move_page(cs, pagep);
991 if (err <= 0)
992 return err;
993 } else {
994 err = fuse_copy_fill(cs);
995 if (err)
996 return err;
997 }
Miklos Szeredi1729a162008-11-26 12:03:54 +0100998 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700999 if (page) {
Cong Wang2408f6e2011-11-25 23:14:30 +08001000 void *mapaddr = kmap_atomic(page);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001001 void *buf = mapaddr + offset;
1002 offset += fuse_copy_do(cs, &buf, &count);
Cong Wang2408f6e2011-11-25 23:14:30 +08001003 kunmap_atomic(mapaddr);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001004 } else
1005 offset += fuse_copy_do(cs, NULL, &count);
1006 }
1007 if (page && !cs->write)
1008 flush_dcache_page(page);
1009 return 0;
1010}
1011
1012/* Copy pages in the request to/from userspace buffer */
1013static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
1014 int zeroing)
1015{
1016 unsigned i;
1017 struct fuse_req *req = cs->req;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001018
1019 for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
Miklos Szeredice534fb2010-05-25 15:06:07 +02001020 int err;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001021 unsigned offset = req->page_descs[i].offset;
1022 unsigned count = min(nbytes, req->page_descs[i].length);
Miklos Szeredice534fb2010-05-25 15:06:07 +02001023
1024 err = fuse_copy_page(cs, &req->pages[i], offset, count,
1025 zeroing);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001026 if (err)
1027 return err;
1028
1029 nbytes -= count;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001030 }
1031 return 0;
1032}
1033
1034/* Copy a single argument in the request to/from userspace buffer */
1035static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
1036{
1037 while (size) {
Miklos Szeredi1729a162008-11-26 12:03:54 +01001038 if (!cs->len) {
1039 int err = fuse_copy_fill(cs);
1040 if (err)
1041 return err;
1042 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001043 fuse_copy_do(cs, &val, &size);
1044 }
1045 return 0;
1046}
1047
1048/* Copy request arguments to/from userspace buffer */
1049static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
1050 unsigned argpages, struct fuse_arg *args,
1051 int zeroing)
1052{
1053 int err = 0;
1054 unsigned i;
1055
1056 for (i = 0; !err && i < numargs; i++) {
1057 struct fuse_arg *arg = &args[i];
1058 if (i == numargs - 1 && argpages)
1059 err = fuse_copy_pages(cs, arg->size, zeroing);
1060 else
1061 err = fuse_copy_one(cs, arg->value, arg->size);
1062 }
1063 return err;
1064}
1065
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001066static int forget_pending(struct fuse_conn *fc)
1067{
1068 return fc->forget_list_head.next != NULL;
1069}
1070
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001071static int request_pending(struct fuse_conn *fc)
1072{
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001073 return !list_empty(&fc->pending) || !list_empty(&fc->interrupts) ||
1074 forget_pending(fc);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001075}
1076
Miklos Szeredi334f4852005-09-09 13:10:27 -07001077/* Wait until a request is available on the pending list */
1078static void request_wait(struct fuse_conn *fc)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001079__releases(fc->lock)
1080__acquires(fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001081{
1082 DECLARE_WAITQUEUE(wait, current);
1083
1084 add_wait_queue_exclusive(&fc->waitq, &wait);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001085 while (fc->connected && !request_pending(fc)) {
Miklos Szeredi334f4852005-09-09 13:10:27 -07001086 set_current_state(TASK_INTERRUPTIBLE);
1087 if (signal_pending(current))
1088 break;
1089
Miklos Szeredid7133112006-04-10 22:54:55 -07001090 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001091 schedule();
Miklos Szeredid7133112006-04-10 22:54:55 -07001092 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001093 }
1094 set_current_state(TASK_RUNNING);
1095 remove_wait_queue(&fc->waitq, &wait);
1096}
1097
1098/*
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001099 * Transfer an interrupt request to userspace
1100 *
1101 * Unlike other requests this is assembled on demand, without a need
1102 * to allocate a separate fuse_req structure.
1103 *
1104 * Called with fc->lock held, releases it
1105 */
Miklos Szeredic3021622010-05-25 15:06:07 +02001106static int fuse_read_interrupt(struct fuse_conn *fc, struct fuse_copy_state *cs,
1107 size_t nbytes, struct fuse_req *req)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001108__releases(fc->lock)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001109{
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001110 struct fuse_in_header ih;
1111 struct fuse_interrupt_in arg;
1112 unsigned reqsize = sizeof(ih) + sizeof(arg);
1113 int err;
1114
1115 list_del_init(&req->intr_entry);
1116 req->intr_unique = fuse_get_unique(fc);
1117 memset(&ih, 0, sizeof(ih));
1118 memset(&arg, 0, sizeof(arg));
1119 ih.len = reqsize;
1120 ih.opcode = FUSE_INTERRUPT;
1121 ih.unique = req->intr_unique;
1122 arg.unique = req->in.h.unique;
1123
1124 spin_unlock(&fc->lock);
Miklos Szeredic3021622010-05-25 15:06:07 +02001125 if (nbytes < reqsize)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001126 return -EINVAL;
1127
Miklos Szeredic3021622010-05-25 15:06:07 +02001128 err = fuse_copy_one(cs, &ih, sizeof(ih));
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001129 if (!err)
Miklos Szeredic3021622010-05-25 15:06:07 +02001130 err = fuse_copy_one(cs, &arg, sizeof(arg));
1131 fuse_copy_finish(cs);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001132
1133 return err ? err : reqsize;
1134}
1135
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001136static struct fuse_forget_link *dequeue_forget(struct fuse_conn *fc,
1137 unsigned max,
1138 unsigned *countp)
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001139{
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001140 struct fuse_forget_link *head = fc->forget_list_head.next;
1141 struct fuse_forget_link **newhead = &head;
1142 unsigned count;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001143
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001144 for (count = 0; *newhead != NULL && count < max; count++)
1145 newhead = &(*newhead)->next;
1146
1147 fc->forget_list_head.next = *newhead;
1148 *newhead = NULL;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001149 if (fc->forget_list_head.next == NULL)
1150 fc->forget_list_tail = &fc->forget_list_head;
1151
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001152 if (countp != NULL)
1153 *countp = count;
1154
1155 return head;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001156}
1157
1158static int fuse_read_single_forget(struct fuse_conn *fc,
1159 struct fuse_copy_state *cs,
1160 size_t nbytes)
1161__releases(fc->lock)
1162{
1163 int err;
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001164 struct fuse_forget_link *forget = dequeue_forget(fc, 1, NULL);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001165 struct fuse_forget_in arg = {
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001166 .nlookup = forget->forget_one.nlookup,
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001167 };
1168 struct fuse_in_header ih = {
1169 .opcode = FUSE_FORGET,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001170 .nodeid = forget->forget_one.nodeid,
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001171 .unique = fuse_get_unique(fc),
1172 .len = sizeof(ih) + sizeof(arg),
1173 };
1174
1175 spin_unlock(&fc->lock);
1176 kfree(forget);
1177 if (nbytes < ih.len)
1178 return -EINVAL;
1179
1180 err = fuse_copy_one(cs, &ih, sizeof(ih));
1181 if (!err)
1182 err = fuse_copy_one(cs, &arg, sizeof(arg));
1183 fuse_copy_finish(cs);
1184
1185 if (err)
1186 return err;
1187
1188 return ih.len;
1189}
1190
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001191static int fuse_read_batch_forget(struct fuse_conn *fc,
1192 struct fuse_copy_state *cs, size_t nbytes)
1193__releases(fc->lock)
1194{
1195 int err;
1196 unsigned max_forgets;
1197 unsigned count;
1198 struct fuse_forget_link *head;
1199 struct fuse_batch_forget_in arg = { .count = 0 };
1200 struct fuse_in_header ih = {
1201 .opcode = FUSE_BATCH_FORGET,
1202 .unique = fuse_get_unique(fc),
1203 .len = sizeof(ih) + sizeof(arg),
1204 };
1205
1206 if (nbytes < ih.len) {
1207 spin_unlock(&fc->lock);
1208 return -EINVAL;
1209 }
1210
1211 max_forgets = (nbytes - ih.len) / sizeof(struct fuse_forget_one);
1212 head = dequeue_forget(fc, max_forgets, &count);
1213 spin_unlock(&fc->lock);
1214
1215 arg.count = count;
1216 ih.len += count * sizeof(struct fuse_forget_one);
1217 err = fuse_copy_one(cs, &ih, sizeof(ih));
1218 if (!err)
1219 err = fuse_copy_one(cs, &arg, sizeof(arg));
1220
1221 while (head) {
1222 struct fuse_forget_link *forget = head;
1223
1224 if (!err) {
1225 err = fuse_copy_one(cs, &forget->forget_one,
1226 sizeof(forget->forget_one));
1227 }
1228 head = forget->next;
1229 kfree(forget);
1230 }
1231
1232 fuse_copy_finish(cs);
1233
1234 if (err)
1235 return err;
1236
1237 return ih.len;
1238}
1239
1240static int fuse_read_forget(struct fuse_conn *fc, struct fuse_copy_state *cs,
1241 size_t nbytes)
1242__releases(fc->lock)
1243{
1244 if (fc->minor < 16 || fc->forget_list_head.next->next == NULL)
1245 return fuse_read_single_forget(fc, cs, nbytes);
1246 else
1247 return fuse_read_batch_forget(fc, cs, nbytes);
1248}
1249
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001250/*
Miklos Szeredi334f4852005-09-09 13:10:27 -07001251 * Read a single request into the userspace filesystem's buffer. This
1252 * function waits until a request is available, then removes it from
1253 * the pending list and copies request data to userspace buffer. If
Miklos Szeredif9a28422006-06-25 05:48:53 -07001254 * no reply is needed (FORGET) or request has been aborted or there
1255 * was an error during the copying then it's finished by calling
Miklos Szeredi334f4852005-09-09 13:10:27 -07001256 * request_end(). Otherwise add it to the processing list, and set
1257 * the 'sent' flag.
1258 */
Miklos Szeredic3021622010-05-25 15:06:07 +02001259static ssize_t fuse_dev_do_read(struct fuse_conn *fc, struct file *file,
1260 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001261{
1262 int err;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001263 struct fuse_req *req;
1264 struct fuse_in *in;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001265 unsigned reqsize;
1266
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001267 restart:
Miklos Szeredid7133112006-04-10 22:54:55 -07001268 spin_lock(&fc->lock);
Jeff Dikee5ac1d12006-04-10 22:54:53 -07001269 err = -EAGAIN;
1270 if ((file->f_flags & O_NONBLOCK) && fc->connected &&
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001271 !request_pending(fc))
Jeff Dikee5ac1d12006-04-10 22:54:53 -07001272 goto err_unlock;
1273
Miklos Szeredi334f4852005-09-09 13:10:27 -07001274 request_wait(fc);
1275 err = -ENODEV;
Miklos Szeredi9ba7cbb2006-01-16 22:14:34 -08001276 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001277 goto err_unlock;
1278 err = -ERESTARTSYS;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001279 if (!request_pending(fc))
Miklos Szeredi334f4852005-09-09 13:10:27 -07001280 goto err_unlock;
1281
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001282 if (!list_empty(&fc->interrupts)) {
1283 req = list_entry(fc->interrupts.next, struct fuse_req,
1284 intr_entry);
Miklos Szeredic3021622010-05-25 15:06:07 +02001285 return fuse_read_interrupt(fc, cs, nbytes, req);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001286 }
1287
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001288 if (forget_pending(fc)) {
1289 if (list_empty(&fc->pending) || fc->forget_batch-- > 0)
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001290 return fuse_read_forget(fc, cs, nbytes);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001291
1292 if (fc->forget_batch <= -8)
1293 fc->forget_batch = 16;
1294 }
1295
Miklos Szeredi334f4852005-09-09 13:10:27 -07001296 req = list_entry(fc->pending.next, struct fuse_req, list);
Miklos Szeredi83cfd492006-01-16 22:14:31 -08001297 req->state = FUSE_REQ_READING;
Miklos Szeredid77a1d52006-01-16 22:14:31 -08001298 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001299
1300 in = &req->in;
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001301 reqsize = in->h.len;
1302 /* If request is too large, reply with an error and restart the read */
Miklos Szeredic3021622010-05-25 15:06:07 +02001303 if (nbytes < reqsize) {
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001304 req->out.h.error = -EIO;
1305 /* SETXATTR is special, since it may contain too large data */
1306 if (in->h.opcode == FUSE_SETXATTR)
1307 req->out.h.error = -E2BIG;
1308 request_end(fc, req);
1309 goto restart;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001310 }
Miklos Szeredid7133112006-04-10 22:54:55 -07001311 spin_unlock(&fc->lock);
Miklos Szeredic3021622010-05-25 15:06:07 +02001312 cs->req = req;
1313 err = fuse_copy_one(cs, &in->h, sizeof(in->h));
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001314 if (!err)
Miklos Szeredic3021622010-05-25 15:06:07 +02001315 err = fuse_copy_args(cs, in->numargs, in->argpages,
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001316 (struct fuse_arg *) in->args, 0);
Miklos Szeredic3021622010-05-25 15:06:07 +02001317 fuse_copy_finish(cs);
Miklos Szeredid7133112006-04-10 22:54:55 -07001318 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001319 req->locked = 0;
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -07001320 if (req->aborted) {
1321 request_end(fc, req);
1322 return -ENODEV;
1323 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001324 if (err) {
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -07001325 req->out.h.error = -EIO;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001326 request_end(fc, req);
1327 return err;
1328 }
1329 if (!req->isreply)
1330 request_end(fc, req);
1331 else {
Miklos Szeredi83cfd492006-01-16 22:14:31 -08001332 req->state = FUSE_REQ_SENT;
Miklos Szeredid77a1d52006-01-16 22:14:31 -08001333 list_move_tail(&req->list, &fc->processing);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001334 if (req->interrupted)
1335 queue_interrupt(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -07001336 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001337 }
1338 return reqsize;
1339
1340 err_unlock:
Miklos Szeredid7133112006-04-10 22:54:55 -07001341 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001342 return err;
1343}
1344
Miklos Szeredic3021622010-05-25 15:06:07 +02001345static ssize_t fuse_dev_read(struct kiocb *iocb, const struct iovec *iov,
1346 unsigned long nr_segs, loff_t pos)
1347{
1348 struct fuse_copy_state cs;
1349 struct file *file = iocb->ki_filp;
1350 struct fuse_conn *fc = fuse_get_conn(file);
1351 if (!fc)
1352 return -EPERM;
1353
1354 fuse_copy_init(&cs, fc, 1, iov, nr_segs);
1355
1356 return fuse_dev_do_read(fc, file, &cs, iov_length(iov, nr_segs));
1357}
1358
Miklos Szeredic3021622010-05-25 15:06:07 +02001359static ssize_t fuse_dev_splice_read(struct file *in, loff_t *ppos,
1360 struct pipe_inode_info *pipe,
1361 size_t len, unsigned int flags)
1362{
1363 int ret;
1364 int page_nr = 0;
1365 int do_wakeup = 0;
1366 struct pipe_buffer *bufs;
1367 struct fuse_copy_state cs;
1368 struct fuse_conn *fc = fuse_get_conn(in);
1369 if (!fc)
1370 return -EPERM;
1371
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001372 bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), GFP_KERNEL);
Miklos Szeredic3021622010-05-25 15:06:07 +02001373 if (!bufs)
1374 return -ENOMEM;
1375
1376 fuse_copy_init(&cs, fc, 1, NULL, 0);
1377 cs.pipebufs = bufs;
1378 cs.pipe = pipe;
1379 ret = fuse_dev_do_read(fc, in, &cs, len);
1380 if (ret < 0)
1381 goto out;
1382
1383 ret = 0;
1384 pipe_lock(pipe);
1385
1386 if (!pipe->readers) {
1387 send_sig(SIGPIPE, current, 0);
1388 if (!ret)
1389 ret = -EPIPE;
1390 goto out_unlock;
1391 }
1392
1393 if (pipe->nrbufs + cs.nr_segs > pipe->buffers) {
1394 ret = -EIO;
1395 goto out_unlock;
1396 }
1397
1398 while (page_nr < cs.nr_segs) {
1399 int newbuf = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
1400 struct pipe_buffer *buf = pipe->bufs + newbuf;
1401
1402 buf->page = bufs[page_nr].page;
1403 buf->offset = bufs[page_nr].offset;
1404 buf->len = bufs[page_nr].len;
Miklos Szeredi28a625c2014-01-22 19:36:57 +01001405 /*
1406 * Need to be careful about this. Having buf->ops in module
1407 * code can Oops if the buffer persists after module unload.
1408 */
1409 buf->ops = &nosteal_pipe_buf_ops;
Miklos Szeredic3021622010-05-25 15:06:07 +02001410
1411 pipe->nrbufs++;
1412 page_nr++;
1413 ret += buf->len;
1414
Al Viro6447a3c2013-03-21 11:01:38 -04001415 if (pipe->files)
Miklos Szeredic3021622010-05-25 15:06:07 +02001416 do_wakeup = 1;
1417 }
1418
1419out_unlock:
1420 pipe_unlock(pipe);
1421
1422 if (do_wakeup) {
1423 smp_mb();
1424 if (waitqueue_active(&pipe->wait))
1425 wake_up_interruptible(&pipe->wait);
1426 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
1427 }
1428
1429out:
1430 for (; page_nr < cs.nr_segs; page_nr++)
1431 page_cache_release(bufs[page_nr].page);
1432
1433 kfree(bufs);
1434 return ret;
1435}
1436
Tejun Heo95668a62008-11-26 12:03:55 +01001437static int fuse_notify_poll(struct fuse_conn *fc, unsigned int size,
1438 struct fuse_copy_state *cs)
1439{
1440 struct fuse_notify_poll_wakeup_out outarg;
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001441 int err = -EINVAL;
Tejun Heo95668a62008-11-26 12:03:55 +01001442
1443 if (size != sizeof(outarg))
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001444 goto err;
Tejun Heo95668a62008-11-26 12:03:55 +01001445
1446 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1447 if (err)
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001448 goto err;
Tejun Heo95668a62008-11-26 12:03:55 +01001449
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001450 fuse_copy_finish(cs);
Tejun Heo95668a62008-11-26 12:03:55 +01001451 return fuse_notify_poll_wakeup(fc, &outarg);
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001452
1453err:
1454 fuse_copy_finish(cs);
1455 return err;
Tejun Heo95668a62008-11-26 12:03:55 +01001456}
1457
John Muir3b463ae2009-05-31 11:13:57 -04001458static int fuse_notify_inval_inode(struct fuse_conn *fc, unsigned int size,
1459 struct fuse_copy_state *cs)
1460{
1461 struct fuse_notify_inval_inode_out outarg;
1462 int err = -EINVAL;
1463
1464 if (size != sizeof(outarg))
1465 goto err;
1466
1467 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1468 if (err)
1469 goto err;
1470 fuse_copy_finish(cs);
1471
1472 down_read(&fc->killsb);
1473 err = -ENOENT;
Miklos Szeredib21dda42010-02-05 12:08:31 +01001474 if (fc->sb) {
1475 err = fuse_reverse_inval_inode(fc->sb, outarg.ino,
1476 outarg.off, outarg.len);
1477 }
John Muir3b463ae2009-05-31 11:13:57 -04001478 up_read(&fc->killsb);
1479 return err;
1480
1481err:
1482 fuse_copy_finish(cs);
1483 return err;
1484}
1485
1486static int fuse_notify_inval_entry(struct fuse_conn *fc, unsigned int size,
1487 struct fuse_copy_state *cs)
1488{
1489 struct fuse_notify_inval_entry_out outarg;
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001490 int err = -ENOMEM;
1491 char *buf;
John Muir3b463ae2009-05-31 11:13:57 -04001492 struct qstr name;
1493
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001494 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1495 if (!buf)
1496 goto err;
1497
1498 err = -EINVAL;
John Muir3b463ae2009-05-31 11:13:57 -04001499 if (size < sizeof(outarg))
1500 goto err;
1501
1502 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1503 if (err)
1504 goto err;
1505
1506 err = -ENAMETOOLONG;
1507 if (outarg.namelen > FUSE_NAME_MAX)
1508 goto err;
1509
Miklos Szeredic2183d12011-08-24 10:20:17 +02001510 err = -EINVAL;
1511 if (size != sizeof(outarg) + outarg.namelen + 1)
1512 goto err;
1513
John Muir3b463ae2009-05-31 11:13:57 -04001514 name.name = buf;
1515 name.len = outarg.namelen;
1516 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1517 if (err)
1518 goto err;
1519 fuse_copy_finish(cs);
1520 buf[outarg.namelen] = 0;
1521 name.hash = full_name_hash(name.name, name.len);
1522
1523 down_read(&fc->killsb);
1524 err = -ENOENT;
Miklos Szeredib21dda42010-02-05 12:08:31 +01001525 if (fc->sb)
John Muir451d0f52011-12-06 21:50:06 +01001526 err = fuse_reverse_inval_entry(fc->sb, outarg.parent, 0, &name);
1527 up_read(&fc->killsb);
1528 kfree(buf);
1529 return err;
1530
1531err:
1532 kfree(buf);
1533 fuse_copy_finish(cs);
1534 return err;
1535}
1536
1537static int fuse_notify_delete(struct fuse_conn *fc, unsigned int size,
1538 struct fuse_copy_state *cs)
1539{
1540 struct fuse_notify_delete_out outarg;
1541 int err = -ENOMEM;
1542 char *buf;
1543 struct qstr name;
1544
1545 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1546 if (!buf)
1547 goto err;
1548
1549 err = -EINVAL;
1550 if (size < sizeof(outarg))
1551 goto err;
1552
1553 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1554 if (err)
1555 goto err;
1556
1557 err = -ENAMETOOLONG;
1558 if (outarg.namelen > FUSE_NAME_MAX)
1559 goto err;
1560
1561 err = -EINVAL;
1562 if (size != sizeof(outarg) + outarg.namelen + 1)
1563 goto err;
1564
1565 name.name = buf;
1566 name.len = outarg.namelen;
1567 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1568 if (err)
1569 goto err;
1570 fuse_copy_finish(cs);
1571 buf[outarg.namelen] = 0;
1572 name.hash = full_name_hash(name.name, name.len);
1573
1574 down_read(&fc->killsb);
1575 err = -ENOENT;
1576 if (fc->sb)
1577 err = fuse_reverse_inval_entry(fc->sb, outarg.parent,
1578 outarg.child, &name);
John Muir3b463ae2009-05-31 11:13:57 -04001579 up_read(&fc->killsb);
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001580 kfree(buf);
John Muir3b463ae2009-05-31 11:13:57 -04001581 return err;
1582
1583err:
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001584 kfree(buf);
John Muir3b463ae2009-05-31 11:13:57 -04001585 fuse_copy_finish(cs);
1586 return err;
1587}
1588
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001589static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
1590 struct fuse_copy_state *cs)
1591{
1592 struct fuse_notify_store_out outarg;
1593 struct inode *inode;
1594 struct address_space *mapping;
1595 u64 nodeid;
1596 int err;
1597 pgoff_t index;
1598 unsigned int offset;
1599 unsigned int num;
1600 loff_t file_size;
1601 loff_t end;
1602
1603 err = -EINVAL;
1604 if (size < sizeof(outarg))
1605 goto out_finish;
1606
1607 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1608 if (err)
1609 goto out_finish;
1610
1611 err = -EINVAL;
1612 if (size - sizeof(outarg) != outarg.size)
1613 goto out_finish;
1614
1615 nodeid = outarg.nodeid;
1616
1617 down_read(&fc->killsb);
1618
1619 err = -ENOENT;
1620 if (!fc->sb)
1621 goto out_up_killsb;
1622
1623 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1624 if (!inode)
1625 goto out_up_killsb;
1626
1627 mapping = inode->i_mapping;
1628 index = outarg.offset >> PAGE_CACHE_SHIFT;
1629 offset = outarg.offset & ~PAGE_CACHE_MASK;
1630 file_size = i_size_read(inode);
1631 end = outarg.offset + outarg.size;
1632 if (end > file_size) {
1633 file_size = end;
1634 fuse_write_update_size(inode, file_size);
1635 }
1636
1637 num = outarg.size;
1638 while (num) {
1639 struct page *page;
1640 unsigned int this_num;
1641
1642 err = -ENOMEM;
1643 page = find_or_create_page(mapping, index,
1644 mapping_gfp_mask(mapping));
1645 if (!page)
1646 goto out_iput;
1647
1648 this_num = min_t(unsigned, num, PAGE_CACHE_SIZE - offset);
1649 err = fuse_copy_page(cs, &page, offset, this_num, 0);
Miklos Szeredi063ec1e2014-01-22 19:36:58 +01001650 if (!err && offset == 0 &&
1651 (this_num == PAGE_CACHE_SIZE || file_size == end))
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001652 SetPageUptodate(page);
1653 unlock_page(page);
1654 page_cache_release(page);
1655
1656 if (err)
1657 goto out_iput;
1658
1659 num -= this_num;
1660 offset = 0;
1661 index++;
1662 }
1663
1664 err = 0;
1665
1666out_iput:
1667 iput(inode);
1668out_up_killsb:
1669 up_read(&fc->killsb);
1670out_finish:
1671 fuse_copy_finish(cs);
1672 return err;
1673}
1674
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001675static void fuse_retrieve_end(struct fuse_conn *fc, struct fuse_req *req)
1676{
Mel Gormanb745bc82014-06-04 16:10:22 -07001677 release_pages(req->pages, req->num_pages, false);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001678}
1679
1680static int fuse_retrieve(struct fuse_conn *fc, struct inode *inode,
1681 struct fuse_notify_retrieve_out *outarg)
1682{
1683 int err;
1684 struct address_space *mapping = inode->i_mapping;
1685 struct fuse_req *req;
1686 pgoff_t index;
1687 loff_t file_size;
1688 unsigned int num;
1689 unsigned int offset;
Geert Uytterhoeven01574432010-09-30 22:06:21 +02001690 size_t total_len = 0;
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001691 int num_pages;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001692
1693 offset = outarg->offset & ~PAGE_CACHE_MASK;
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001694 file_size = i_size_read(inode);
1695
1696 num = outarg->size;
1697 if (outarg->offset > file_size)
1698 num = 0;
1699 else if (outarg->offset + num > file_size)
1700 num = file_size - outarg->offset;
1701
1702 num_pages = (num + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1703 num_pages = min(num_pages, FUSE_MAX_PAGES_PER_REQ);
1704
1705 req = fuse_get_req(fc, num_pages);
1706 if (IS_ERR(req))
1707 return PTR_ERR(req);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001708
1709 req->in.h.opcode = FUSE_NOTIFY_REPLY;
1710 req->in.h.nodeid = outarg->nodeid;
1711 req->in.numargs = 2;
1712 req->in.argpages = 1;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001713 req->page_descs[0].offset = offset;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001714 req->end = fuse_retrieve_end;
1715
1716 index = outarg->offset >> PAGE_CACHE_SHIFT;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001717
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001718 while (num && req->num_pages < num_pages) {
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001719 struct page *page;
1720 unsigned int this_num;
1721
1722 page = find_get_page(mapping, index);
1723 if (!page)
1724 break;
1725
1726 this_num = min_t(unsigned, num, PAGE_CACHE_SIZE - offset);
1727 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001728 req->page_descs[req->num_pages].length = this_num;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001729 req->num_pages++;
1730
Miklos Szeredic9e67d42012-09-04 18:45:54 +02001731 offset = 0;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001732 num -= this_num;
1733 total_len += this_num;
Miklos Szeredi48706d02011-12-13 10:36:59 +01001734 index++;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001735 }
1736 req->misc.retrieve_in.offset = outarg->offset;
1737 req->misc.retrieve_in.size = total_len;
1738 req->in.args[0].size = sizeof(req->misc.retrieve_in);
1739 req->in.args[0].value = &req->misc.retrieve_in;
1740 req->in.args[1].size = total_len;
1741
1742 err = fuse_request_send_notify_reply(fc, req, outarg->notify_unique);
1743 if (err)
1744 fuse_retrieve_end(fc, req);
1745
1746 return err;
1747}
1748
1749static int fuse_notify_retrieve(struct fuse_conn *fc, unsigned int size,
1750 struct fuse_copy_state *cs)
1751{
1752 struct fuse_notify_retrieve_out outarg;
1753 struct inode *inode;
1754 int err;
1755
1756 err = -EINVAL;
1757 if (size != sizeof(outarg))
1758 goto copy_finish;
1759
1760 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1761 if (err)
1762 goto copy_finish;
1763
1764 fuse_copy_finish(cs);
1765
1766 down_read(&fc->killsb);
1767 err = -ENOENT;
1768 if (fc->sb) {
1769 u64 nodeid = outarg.nodeid;
1770
1771 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1772 if (inode) {
1773 err = fuse_retrieve(fc, inode, &outarg);
1774 iput(inode);
1775 }
1776 }
1777 up_read(&fc->killsb);
1778
1779 return err;
1780
1781copy_finish:
1782 fuse_copy_finish(cs);
1783 return err;
1784}
1785
Tejun Heo85993962008-11-26 12:03:55 +01001786static int fuse_notify(struct fuse_conn *fc, enum fuse_notify_code code,
1787 unsigned int size, struct fuse_copy_state *cs)
1788{
1789 switch (code) {
Tejun Heo95668a62008-11-26 12:03:55 +01001790 case FUSE_NOTIFY_POLL:
1791 return fuse_notify_poll(fc, size, cs);
1792
John Muir3b463ae2009-05-31 11:13:57 -04001793 case FUSE_NOTIFY_INVAL_INODE:
1794 return fuse_notify_inval_inode(fc, size, cs);
1795
1796 case FUSE_NOTIFY_INVAL_ENTRY:
1797 return fuse_notify_inval_entry(fc, size, cs);
1798
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001799 case FUSE_NOTIFY_STORE:
1800 return fuse_notify_store(fc, size, cs);
1801
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001802 case FUSE_NOTIFY_RETRIEVE:
1803 return fuse_notify_retrieve(fc, size, cs);
1804
John Muir451d0f52011-12-06 21:50:06 +01001805 case FUSE_NOTIFY_DELETE:
1806 return fuse_notify_delete(fc, size, cs);
1807
Tejun Heo85993962008-11-26 12:03:55 +01001808 default:
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001809 fuse_copy_finish(cs);
Tejun Heo85993962008-11-26 12:03:55 +01001810 return -EINVAL;
1811 }
1812}
1813
Miklos Szeredi334f4852005-09-09 13:10:27 -07001814/* Look up request on processing list by unique ID */
1815static struct fuse_req *request_find(struct fuse_conn *fc, u64 unique)
1816{
Dong Fang05726ac2013-07-30 22:50:01 -04001817 struct fuse_req *req;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001818
Dong Fang05726ac2013-07-30 22:50:01 -04001819 list_for_each_entry(req, &fc->processing, list) {
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001820 if (req->in.h.unique == unique || req->intr_unique == unique)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001821 return req;
1822 }
1823 return NULL;
1824}
1825
1826static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
1827 unsigned nbytes)
1828{
1829 unsigned reqsize = sizeof(struct fuse_out_header);
1830
1831 if (out->h.error)
1832 return nbytes != reqsize ? -EINVAL : 0;
1833
1834 reqsize += len_args(out->numargs, out->args);
1835
1836 if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
1837 return -EINVAL;
1838 else if (reqsize > nbytes) {
1839 struct fuse_arg *lastarg = &out->args[out->numargs-1];
1840 unsigned diffsize = reqsize - nbytes;
1841 if (diffsize > lastarg->size)
1842 return -EINVAL;
1843 lastarg->size -= diffsize;
1844 }
1845 return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
1846 out->page_zeroing);
1847}
1848
1849/*
1850 * Write a single reply to a request. First the header is copied from
1851 * the write buffer. The request is then searched on the processing
1852 * list by the unique ID found in the header. If found, then remove
1853 * it from the list and copy the rest of the buffer to the request.
1854 * The request is finished by calling request_end()
1855 */
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001856static ssize_t fuse_dev_do_write(struct fuse_conn *fc,
1857 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001858{
1859 int err;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001860 struct fuse_req *req;
1861 struct fuse_out_header oh;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001862
Miklos Szeredi334f4852005-09-09 13:10:27 -07001863 if (nbytes < sizeof(struct fuse_out_header))
1864 return -EINVAL;
1865
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001866 err = fuse_copy_one(cs, &oh, sizeof(oh));
Miklos Szeredi334f4852005-09-09 13:10:27 -07001867 if (err)
1868 goto err_finish;
Tejun Heo85993962008-11-26 12:03:55 +01001869
Miklos Szeredi334f4852005-09-09 13:10:27 -07001870 err = -EINVAL;
Tejun Heo85993962008-11-26 12:03:55 +01001871 if (oh.len != nbytes)
1872 goto err_finish;
1873
1874 /*
1875 * Zero oh.unique indicates unsolicited notification message
1876 * and error contains notification code.
1877 */
1878 if (!oh.unique) {
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001879 err = fuse_notify(fc, oh.error, nbytes - sizeof(oh), cs);
Tejun Heo85993962008-11-26 12:03:55 +01001880 return err ? err : nbytes;
1881 }
1882
1883 err = -EINVAL;
1884 if (oh.error <= -1000 || oh.error > 0)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001885 goto err_finish;
1886
Miklos Szeredid7133112006-04-10 22:54:55 -07001887 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001888 err = -ENOENT;
1889 if (!fc->connected)
1890 goto err_unlock;
1891
Miklos Szeredi334f4852005-09-09 13:10:27 -07001892 req = request_find(fc, oh.unique);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001893 if (!req)
1894 goto err_unlock;
1895
Miklos Szeredif9a28422006-06-25 05:48:53 -07001896 if (req->aborted) {
Miklos Szeredid7133112006-04-10 22:54:55 -07001897 spin_unlock(&fc->lock);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001898 fuse_copy_finish(cs);
Miklos Szeredid7133112006-04-10 22:54:55 -07001899 spin_lock(&fc->lock);
Miklos Szeredi222f1d62006-01-16 22:14:25 -08001900 request_end(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001901 return -ENOENT;
1902 }
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001903 /* Is it an interrupt reply? */
1904 if (req->intr_unique == oh.unique) {
1905 err = -EINVAL;
1906 if (nbytes != sizeof(struct fuse_out_header))
1907 goto err_unlock;
1908
1909 if (oh.error == -ENOSYS)
1910 fc->no_interrupt = 1;
1911 else if (oh.error == -EAGAIN)
1912 queue_interrupt(fc, req);
1913
1914 spin_unlock(&fc->lock);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001915 fuse_copy_finish(cs);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001916 return nbytes;
1917 }
1918
1919 req->state = FUSE_REQ_WRITING;
Miklos Szeredid77a1d52006-01-16 22:14:31 -08001920 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001921 req->out.h = oh;
1922 req->locked = 1;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001923 cs->req = req;
Miklos Szeredice534fb2010-05-25 15:06:07 +02001924 if (!req->out.page_replace)
1925 cs->move_pages = 0;
Miklos Szeredid7133112006-04-10 22:54:55 -07001926 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001927
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001928 err = copy_out_args(cs, &req->out, nbytes);
1929 fuse_copy_finish(cs);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001930
Miklos Szeredid7133112006-04-10 22:54:55 -07001931 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001932 req->locked = 0;
1933 if (!err) {
Miklos Szeredif9a28422006-06-25 05:48:53 -07001934 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001935 err = -ENOENT;
Miklos Szeredif9a28422006-06-25 05:48:53 -07001936 } else if (!req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001937 req->out.h.error = -EIO;
1938 request_end(fc, req);
1939
1940 return err ? err : nbytes;
1941
1942 err_unlock:
Miklos Szeredid7133112006-04-10 22:54:55 -07001943 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001944 err_finish:
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001945 fuse_copy_finish(cs);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001946 return err;
1947}
1948
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001949static ssize_t fuse_dev_write(struct kiocb *iocb, const struct iovec *iov,
1950 unsigned long nr_segs, loff_t pos)
1951{
1952 struct fuse_copy_state cs;
1953 struct fuse_conn *fc = fuse_get_conn(iocb->ki_filp);
1954 if (!fc)
1955 return -EPERM;
1956
Miklos Szeredic3021622010-05-25 15:06:07 +02001957 fuse_copy_init(&cs, fc, 0, iov, nr_segs);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001958
1959 return fuse_dev_do_write(fc, &cs, iov_length(iov, nr_segs));
1960}
1961
1962static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe,
1963 struct file *out, loff_t *ppos,
1964 size_t len, unsigned int flags)
1965{
1966 unsigned nbuf;
1967 unsigned idx;
1968 struct pipe_buffer *bufs;
1969 struct fuse_copy_state cs;
1970 struct fuse_conn *fc;
1971 size_t rem;
1972 ssize_t ret;
1973
1974 fc = fuse_get_conn(out);
1975 if (!fc)
1976 return -EPERM;
1977
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001978 bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), GFP_KERNEL);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001979 if (!bufs)
1980 return -ENOMEM;
1981
1982 pipe_lock(pipe);
1983 nbuf = 0;
1984 rem = 0;
1985 for (idx = 0; idx < pipe->nrbufs && rem < len; idx++)
1986 rem += pipe->bufs[(pipe->curbuf + idx) & (pipe->buffers - 1)].len;
1987
1988 ret = -EINVAL;
1989 if (rem < len) {
1990 pipe_unlock(pipe);
1991 goto out;
1992 }
1993
1994 rem = len;
1995 while (rem) {
1996 struct pipe_buffer *ibuf;
1997 struct pipe_buffer *obuf;
1998
1999 BUG_ON(nbuf >= pipe->buffers);
2000 BUG_ON(!pipe->nrbufs);
2001 ibuf = &pipe->bufs[pipe->curbuf];
2002 obuf = &bufs[nbuf];
2003
2004 if (rem >= ibuf->len) {
2005 *obuf = *ibuf;
2006 ibuf->ops = NULL;
2007 pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
2008 pipe->nrbufs--;
2009 } else {
2010 ibuf->ops->get(pipe, ibuf);
2011 *obuf = *ibuf;
2012 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
2013 obuf->len = rem;
2014 ibuf->offset += obuf->len;
2015 ibuf->len -= obuf->len;
2016 }
2017 nbuf++;
2018 rem -= obuf->len;
2019 }
2020 pipe_unlock(pipe);
2021
Miklos Szeredic3021622010-05-25 15:06:07 +02002022 fuse_copy_init(&cs, fc, 0, NULL, nbuf);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002023 cs.pipebufs = bufs;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002024 cs.pipe = pipe;
2025
Miklos Szeredice534fb2010-05-25 15:06:07 +02002026 if (flags & SPLICE_F_MOVE)
2027 cs.move_pages = 1;
2028
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002029 ret = fuse_dev_do_write(fc, &cs, len);
2030
2031 for (idx = 0; idx < nbuf; idx++) {
2032 struct pipe_buffer *buf = &bufs[idx];
2033 buf->ops->release(pipe, buf);
2034 }
2035out:
2036 kfree(bufs);
2037 return ret;
2038}
2039
Miklos Szeredi334f4852005-09-09 13:10:27 -07002040static unsigned fuse_dev_poll(struct file *file, poll_table *wait)
2041{
Miklos Szeredi334f4852005-09-09 13:10:27 -07002042 unsigned mask = POLLOUT | POLLWRNORM;
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07002043 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002044 if (!fc)
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07002045 return POLLERR;
Miklos Szeredi334f4852005-09-09 13:10:27 -07002046
2047 poll_wait(file, &fc->waitq, wait);
2048
Miklos Szeredid7133112006-04-10 22:54:55 -07002049 spin_lock(&fc->lock);
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07002050 if (!fc->connected)
2051 mask = POLLERR;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07002052 else if (request_pending(fc))
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07002053 mask |= POLLIN | POLLRDNORM;
Miklos Szeredid7133112006-04-10 22:54:55 -07002054 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002055
2056 return mask;
2057}
2058
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002059/*
2060 * Abort all requests on the given list (pending or processing)
2061 *
Miklos Szeredid7133112006-04-10 22:54:55 -07002062 * This function releases and reacquires fc->lock
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002063 */
Miklos Szeredi334f4852005-09-09 13:10:27 -07002064static void end_requests(struct fuse_conn *fc, struct list_head *head)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02002065__releases(fc->lock)
2066__acquires(fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -07002067{
2068 while (!list_empty(head)) {
2069 struct fuse_req *req;
2070 req = list_entry(head->next, struct fuse_req, list);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002071 req->out.h.error = -ECONNABORTED;
2072 request_end(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -07002073 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002074 }
2075}
2076
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002077/*
2078 * Abort requests under I/O
2079 *
Miklos Szeredif9a28422006-06-25 05:48:53 -07002080 * The requests are set to aborted and finished, and the request
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002081 * waiter is woken up. This will make request_wait_answer() wait
2082 * until the request is unlocked and then return.
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08002083 *
2084 * If the request is asynchronous, then the end function needs to be
2085 * called after waiting for the request to be unlocked (if it was
2086 * locked).
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002087 */
2088static void end_io_requests(struct fuse_conn *fc)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02002089__releases(fc->lock)
2090__acquires(fc->lock)
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002091{
2092 while (!list_empty(&fc->io)) {
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08002093 struct fuse_req *req =
2094 list_entry(fc->io.next, struct fuse_req, list);
2095 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
2096
Miklos Szeredif9a28422006-06-25 05:48:53 -07002097 req->aborted = 1;
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002098 req->out.h.error = -ECONNABORTED;
2099 req->state = FUSE_REQ_FINISHED;
2100 list_del_init(&req->list);
2101 wake_up(&req->waitq);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08002102 if (end) {
2103 req->end = NULL;
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08002104 __fuse_get_request(req);
Miklos Szeredid7133112006-04-10 22:54:55 -07002105 spin_unlock(&fc->lock);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08002106 wait_event(req->waitq, !req->locked);
2107 end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +01002108 fuse_put_request(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -07002109 spin_lock(&fc->lock);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08002110 }
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002111 }
2112}
2113
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002114static void end_queued_requests(struct fuse_conn *fc)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02002115__releases(fc->lock)
2116__acquires(fc->lock)
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002117{
2118 fc->max_background = UINT_MAX;
2119 flush_bg_queue(fc);
2120 end_requests(fc, &fc->pending);
2121 end_requests(fc, &fc->processing);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01002122 while (forget_pending(fc))
Miklos Szeredi02c048b2010-12-07 20:16:56 +01002123 kfree(dequeue_forget(fc, 1, NULL));
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002124}
2125
Bryan Green357ccf22011-03-01 16:43:52 -08002126static void end_polls(struct fuse_conn *fc)
2127{
2128 struct rb_node *p;
2129
2130 p = rb_first(&fc->polled_files);
2131
2132 while (p) {
2133 struct fuse_file *ff;
2134 ff = rb_entry(p, struct fuse_file, polled_node);
2135 wake_up_interruptible_all(&ff->poll_wait);
2136
2137 p = rb_next(p);
2138 }
2139}
2140
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002141/*
2142 * Abort all requests.
2143 *
2144 * Emergency exit in case of a malicious or accidental deadlock, or
2145 * just a hung filesystem.
2146 *
2147 * The same effect is usually achievable through killing the
2148 * filesystem daemon and all users of the filesystem. The exception
2149 * is the combination of an asynchronous request and the tricky
2150 * deadlock (see Documentation/filesystems/fuse.txt).
2151 *
2152 * During the aborting, progression of requests from the pending and
2153 * processing lists onto the io list, and progression of new requests
2154 * onto the pending list is prevented by req->connected being false.
2155 *
2156 * Progression of requests under I/O to the processing list is
Miklos Szeredif9a28422006-06-25 05:48:53 -07002157 * prevented by the req->aborted flag being true for these requests.
2158 * For this reason requests on the io list must be aborted first.
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002159 */
2160void fuse_abort_conn(struct fuse_conn *fc)
2161{
Miklos Szeredid7133112006-04-10 22:54:55 -07002162 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002163 if (fc->connected) {
2164 fc->connected = 0;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -07002165 fc->blocked = 0;
Maxim Patlasov796523fb2013-03-21 18:02:15 +04002166 fc->initialized = 1;
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002167 end_io_requests(fc);
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002168 end_queued_requests(fc);
Bryan Green357ccf22011-03-01 16:43:52 -08002169 end_polls(fc);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002170 wake_up_all(&fc->waitq);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -07002171 wake_up_all(&fc->blocked_waitq);
Jeff Dike385a17b2006-04-10 22:54:52 -07002172 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002173 }
Miklos Szeredid7133112006-04-10 22:54:55 -07002174 spin_unlock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002175}
Tejun Heo08cbf542009-04-14 10:54:53 +09002176EXPORT_SYMBOL_GPL(fuse_abort_conn);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002177
Tejun Heo08cbf542009-04-14 10:54:53 +09002178int fuse_dev_release(struct inode *inode, struct file *file)
Miklos Szeredi334f4852005-09-09 13:10:27 -07002179{
Miklos Szeredi0720b312006-04-10 22:54:55 -07002180 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002181 if (fc) {
Miklos Szeredid7133112006-04-10 22:54:55 -07002182 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -07002183 fc->connected = 0;
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002184 fc->blocked = 0;
Maxim Patlasov0aada882013-03-21 18:02:28 +04002185 fc->initialized = 1;
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002186 end_queued_requests(fc);
Bryan Green357ccf22011-03-01 16:43:52 -08002187 end_polls(fc);
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002188 wake_up_all(&fc->blocked_waitq);
Miklos Szeredid7133112006-04-10 22:54:55 -07002189 spin_unlock(&fc->lock);
Miklos Szeredibafa9652006-06-25 05:48:51 -07002190 fuse_conn_put(fc);
Jeff Dike385a17b2006-04-10 22:54:52 -07002191 }
Miklos Szeredif543f252006-01-16 22:14:35 -08002192
Miklos Szeredi334f4852005-09-09 13:10:27 -07002193 return 0;
2194}
Tejun Heo08cbf542009-04-14 10:54:53 +09002195EXPORT_SYMBOL_GPL(fuse_dev_release);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002196
Jeff Dike385a17b2006-04-10 22:54:52 -07002197static int fuse_dev_fasync(int fd, struct file *file, int on)
2198{
2199 struct fuse_conn *fc = fuse_get_conn(file);
2200 if (!fc)
Miklos Szeredia87046d2006-04-10 22:54:56 -07002201 return -EPERM;
Jeff Dike385a17b2006-04-10 22:54:52 -07002202
2203 /* No locking - fasync_helper does its own locking */
2204 return fasync_helper(fd, file, on, &fc->fasync);
2205}
2206
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002207const struct file_operations fuse_dev_operations = {
Miklos Szeredi334f4852005-09-09 13:10:27 -07002208 .owner = THIS_MODULE,
2209 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002210 .read = do_sync_read,
2211 .aio_read = fuse_dev_read,
Miklos Szeredic3021622010-05-25 15:06:07 +02002212 .splice_read = fuse_dev_splice_read,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002213 .write = do_sync_write,
2214 .aio_write = fuse_dev_write,
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002215 .splice_write = fuse_dev_splice_write,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002216 .poll = fuse_dev_poll,
2217 .release = fuse_dev_release,
Jeff Dike385a17b2006-04-10 22:54:52 -07002218 .fasync = fuse_dev_fasync,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002219};
Tejun Heo08cbf542009-04-14 10:54:53 +09002220EXPORT_SYMBOL_GPL(fuse_dev_operations);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002221
2222static struct miscdevice fuse_miscdevice = {
2223 .minor = FUSE_MINOR,
2224 .name = "fuse",
2225 .fops = &fuse_dev_operations,
2226};
2227
2228int __init fuse_dev_init(void)
2229{
2230 int err = -ENOMEM;
2231 fuse_req_cachep = kmem_cache_create("fuse_request",
2232 sizeof(struct fuse_req),
Paul Mundt20c2df82007-07-20 10:11:58 +09002233 0, 0, NULL);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002234 if (!fuse_req_cachep)
2235 goto out;
2236
2237 err = misc_register(&fuse_miscdevice);
2238 if (err)
2239 goto out_cache_clean;
2240
2241 return 0;
2242
2243 out_cache_clean:
2244 kmem_cache_destroy(fuse_req_cachep);
2245 out:
2246 return err;
2247}
2248
2249void fuse_dev_cleanup(void)
2250{
2251 misc_deregister(&fuse_miscdevice);
2252 kmem_cache_destroy(fuse_req_cachep);
2253}