blob: 6aa4803510e7e0df1ad2317c24d781ecd5bfbfd0 [file] [log] [blame]
Miklos Szeredi334f4852005-09-09 13:10:27 -07001/*
2 FUSE: Filesystem in Userspace
Miklos Szeredi1729a162008-11-26 12:03:54 +01003 Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu>
Miklos Szeredi334f4852005-09-09 13:10:27 -07004
5 This program can be distributed under the terms of the GNU GPL.
6 See the file COPYING.
7*/
8
9#include "fuse_i.h"
10
11#include <linux/init.h>
12#include <linux/module.h>
13#include <linux/poll.h>
14#include <linux/uio.h>
15#include <linux/miscdevice.h>
16#include <linux/pagemap.h>
17#include <linux/file.h>
18#include <linux/slab.h>
Miklos Szeredidd3bb142010-05-25 15:06:06 +020019#include <linux/pipe_fs_i.h>
Miklos Szeredice534fb2010-05-25 15:06:07 +020020#include <linux/swap.h>
21#include <linux/splice.h>
Miklos Szeredi334f4852005-09-09 13:10:27 -070022
23MODULE_ALIAS_MISCDEV(FUSE_MINOR);
Kay Sievers578454f2010-05-20 18:07:20 +020024MODULE_ALIAS("devname:fuse");
Miklos Szeredi334f4852005-09-09 13:10:27 -070025
Christoph Lametere18b8902006-12-06 20:33:20 -080026static struct kmem_cache *fuse_req_cachep;
Miklos Szeredi334f4852005-09-09 13:10:27 -070027
Miklos Szeredi8bfc0162006-01-16 22:14:28 -080028static struct fuse_conn *fuse_get_conn(struct file *file)
Miklos Szeredi334f4852005-09-09 13:10:27 -070029{
Miklos Szeredi0720b312006-04-10 22:54:55 -070030 /*
31 * Lockless access is OK, because file->private data is set
32 * once during mount and is valid until the file is released.
33 */
34 return file->private_data;
Miklos Szeredi334f4852005-09-09 13:10:27 -070035}
36
Maxim Patlasov4250c062012-10-26 19:48:07 +040037static void fuse_request_init(struct fuse_req *req, struct page **pages,
Maxim Patlasovb2430d72012-10-26 19:49:24 +040038 struct fuse_page_desc *page_descs,
Maxim Patlasov4250c062012-10-26 19:48:07 +040039 unsigned npages)
Miklos Szeredi334f4852005-09-09 13:10:27 -070040{
41 memset(req, 0, sizeof(*req));
Maxim Patlasov4250c062012-10-26 19:48:07 +040042 memset(pages, 0, sizeof(*pages) * npages);
Maxim Patlasovb2430d72012-10-26 19:49:24 +040043 memset(page_descs, 0, sizeof(*page_descs) * npages);
Miklos Szeredi334f4852005-09-09 13:10:27 -070044 INIT_LIST_HEAD(&req->list);
Miklos Szeredia4d27e72006-06-25 05:48:54 -070045 INIT_LIST_HEAD(&req->intr_entry);
Miklos Szeredi334f4852005-09-09 13:10:27 -070046 init_waitqueue_head(&req->waitq);
47 atomic_set(&req->count, 1);
Maxim Patlasov4250c062012-10-26 19:48:07 +040048 req->pages = pages;
Maxim Patlasovb2430d72012-10-26 19:49:24 +040049 req->page_descs = page_descs;
Maxim Patlasov4250c062012-10-26 19:48:07 +040050 req->max_pages = npages;
Miklos Szeredi334f4852005-09-09 13:10:27 -070051}
52
Maxim Patlasov4250c062012-10-26 19:48:07 +040053static struct fuse_req *__fuse_request_alloc(unsigned npages, gfp_t flags)
Miklos Szeredi334f4852005-09-09 13:10:27 -070054{
Maxim Patlasov4250c062012-10-26 19:48:07 +040055 struct fuse_req *req = kmem_cache_alloc(fuse_req_cachep, flags);
56 if (req) {
57 struct page **pages;
Maxim Patlasovb2430d72012-10-26 19:49:24 +040058 struct fuse_page_desc *page_descs;
Maxim Patlasov4250c062012-10-26 19:48:07 +040059
Maxim Patlasovb2430d72012-10-26 19:49:24 +040060 if (npages <= FUSE_REQ_INLINE_PAGES) {
Maxim Patlasov4250c062012-10-26 19:48:07 +040061 pages = req->inline_pages;
Maxim Patlasovb2430d72012-10-26 19:49:24 +040062 page_descs = req->inline_page_descs;
63 } else {
Maxim Patlasov4250c062012-10-26 19:48:07 +040064 pages = kmalloc(sizeof(struct page *) * npages, flags);
Maxim Patlasovb2430d72012-10-26 19:49:24 +040065 page_descs = kmalloc(sizeof(struct fuse_page_desc) *
66 npages, flags);
67 }
Maxim Patlasov4250c062012-10-26 19:48:07 +040068
Maxim Patlasovb2430d72012-10-26 19:49:24 +040069 if (!pages || !page_descs) {
70 kfree(pages);
71 kfree(page_descs);
Maxim Patlasov4250c062012-10-26 19:48:07 +040072 kmem_cache_free(fuse_req_cachep, req);
73 return NULL;
74 }
75
Maxim Patlasovb2430d72012-10-26 19:49:24 +040076 fuse_request_init(req, pages, page_descs, npages);
Maxim Patlasov4250c062012-10-26 19:48:07 +040077 }
Miklos Szeredi334f4852005-09-09 13:10:27 -070078 return req;
79}
Maxim Patlasov4250c062012-10-26 19:48:07 +040080
81struct fuse_req *fuse_request_alloc(unsigned npages)
82{
83 return __fuse_request_alloc(npages, GFP_KERNEL);
84}
Tejun Heo08cbf542009-04-14 10:54:53 +090085EXPORT_SYMBOL_GPL(fuse_request_alloc);
Miklos Szeredi334f4852005-09-09 13:10:27 -070086
Maxim Patlasov4250c062012-10-26 19:48:07 +040087struct fuse_req *fuse_request_alloc_nofs(unsigned npages)
Miklos Szeredi3be5a522008-04-30 00:54:41 -070088{
Maxim Patlasov4250c062012-10-26 19:48:07 +040089 return __fuse_request_alloc(npages, GFP_NOFS);
Miklos Szeredi3be5a522008-04-30 00:54:41 -070090}
91
Miklos Szeredi334f4852005-09-09 13:10:27 -070092void fuse_request_free(struct fuse_req *req)
93{
Maxim Patlasovb2430d72012-10-26 19:49:24 +040094 if (req->pages != req->inline_pages) {
Maxim Patlasov4250c062012-10-26 19:48:07 +040095 kfree(req->pages);
Maxim Patlasovb2430d72012-10-26 19:49:24 +040096 kfree(req->page_descs);
97 }
Miklos Szeredi334f4852005-09-09 13:10:27 -070098 kmem_cache_free(fuse_req_cachep, req);
99}
100
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800101static void block_sigs(sigset_t *oldset)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700102{
103 sigset_t mask;
104
105 siginitsetinv(&mask, sigmask(SIGKILL));
106 sigprocmask(SIG_BLOCK, &mask, oldset);
107}
108
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800109static void restore_sigs(sigset_t *oldset)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700110{
111 sigprocmask(SIG_SETMASK, oldset, NULL);
112}
113
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400114void __fuse_get_request(struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700115{
116 atomic_inc(&req->count);
117}
118
119/* Must be called with > 1 refcount */
120static void __fuse_put_request(struct fuse_req *req)
121{
122 BUG_ON(atomic_read(&req->count) < 2);
123 atomic_dec(&req->count);
124}
125
Miklos Szeredi33649c92006-06-25 05:48:52 -0700126static void fuse_req_init_context(struct fuse_req *req)
127{
Eric W. Biederman499dcf22012-02-07 16:26:03 -0800128 req->in.h.uid = from_kuid_munged(&init_user_ns, current_fsuid());
129 req->in.h.gid = from_kgid_munged(&init_user_ns, current_fsgid());
Miklos Szeredi33649c92006-06-25 05:48:52 -0700130 req->in.h.pid = current->pid;
131}
132
Miklos Szeredi9759bd512015-01-06 10:45:35 +0100133void fuse_set_initialized(struct fuse_conn *fc)
134{
135 /* Make sure stores before this are seen on another CPU */
136 smp_wmb();
137 fc->initialized = 1;
138}
139
Maxim Patlasov0aada882013-03-21 18:02:28 +0400140static bool fuse_block_alloc(struct fuse_conn *fc, bool for_background)
141{
142 return !fc->initialized || (for_background && fc->blocked);
143}
144
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400145static struct fuse_req *__fuse_get_req(struct fuse_conn *fc, unsigned npages,
146 bool for_background)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700147{
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700148 struct fuse_req *req;
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700149 int err;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200150 atomic_inc(&fc->num_waiting);
Maxim Patlasov0aada882013-03-21 18:02:28 +0400151
152 if (fuse_block_alloc(fc, for_background)) {
153 sigset_t oldset;
154 int intr;
155
156 block_sigs(&oldset);
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400157 intr = wait_event_interruptible_exclusive(fc->blocked_waitq,
Maxim Patlasov0aada882013-03-21 18:02:28 +0400158 !fuse_block_alloc(fc, for_background));
159 restore_sigs(&oldset);
160 err = -EINTR;
161 if (intr)
162 goto out;
163 }
Miklos Szeredi9759bd512015-01-06 10:45:35 +0100164 /* Matches smp_wmb() in fuse_set_initialized() */
165 smp_rmb();
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700166
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700167 err = -ENOTCONN;
168 if (!fc->connected)
169 goto out;
170
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400171 req = fuse_request_alloc(npages);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200172 err = -ENOMEM;
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400173 if (!req) {
174 if (for_background)
175 wake_up(&fc->blocked_waitq);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200176 goto out;
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400177 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700178
Miklos Szeredi33649c92006-06-25 05:48:52 -0700179 fuse_req_init_context(req);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200180 req->waiting = 1;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400181 req->background = for_background;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700182 return req;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200183
184 out:
185 atomic_dec(&fc->num_waiting);
186 return ERR_PTR(err);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700187}
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400188
189struct fuse_req *fuse_get_req(struct fuse_conn *fc, unsigned npages)
190{
191 return __fuse_get_req(fc, npages, false);
192}
Tejun Heo08cbf542009-04-14 10:54:53 +0900193EXPORT_SYMBOL_GPL(fuse_get_req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700194
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400195struct fuse_req *fuse_get_req_for_background(struct fuse_conn *fc,
196 unsigned npages)
197{
198 return __fuse_get_req(fc, npages, true);
199}
200EXPORT_SYMBOL_GPL(fuse_get_req_for_background);
201
Miklos Szeredi33649c92006-06-25 05:48:52 -0700202/*
203 * Return request in fuse_file->reserved_req. However that may
204 * currently be in use. If that is the case, wait for it to become
205 * available.
206 */
207static struct fuse_req *get_reserved_req(struct fuse_conn *fc,
208 struct file *file)
209{
210 struct fuse_req *req = NULL;
211 struct fuse_file *ff = file->private_data;
212
213 do {
Miklos Szeredide5e3de2007-10-16 23:31:00 -0700214 wait_event(fc->reserved_req_waitq, ff->reserved_req);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700215 spin_lock(&fc->lock);
216 if (ff->reserved_req) {
217 req = ff->reserved_req;
218 ff->reserved_req = NULL;
Al Virocb0942b2012-08-27 14:48:26 -0400219 req->stolen_file = get_file(file);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700220 }
221 spin_unlock(&fc->lock);
222 } while (!req);
223
224 return req;
225}
226
227/*
228 * Put stolen request back into fuse_file->reserved_req
229 */
230static void put_reserved_req(struct fuse_conn *fc, struct fuse_req *req)
231{
232 struct file *file = req->stolen_file;
233 struct fuse_file *ff = file->private_data;
234
235 spin_lock(&fc->lock);
Maxim Patlasovb2430d72012-10-26 19:49:24 +0400236 fuse_request_init(req, req->pages, req->page_descs, req->max_pages);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700237 BUG_ON(ff->reserved_req);
238 ff->reserved_req = req;
Miklos Szeredide5e3de2007-10-16 23:31:00 -0700239 wake_up_all(&fc->reserved_req_waitq);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700240 spin_unlock(&fc->lock);
241 fput(file);
242}
243
244/*
245 * Gets a requests for a file operation, always succeeds
246 *
247 * This is used for sending the FLUSH request, which must get to
248 * userspace, due to POSIX locks which may need to be unlocked.
249 *
250 * If allocation fails due to OOM, use the reserved request in
251 * fuse_file.
252 *
253 * This is very unlikely to deadlock accidentally, since the
254 * filesystem should not have it's own file open. If deadlock is
255 * intentional, it can still be broken by "aborting" the filesystem.
256 */
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400257struct fuse_req *fuse_get_req_nofail_nopages(struct fuse_conn *fc,
258 struct file *file)
Miklos Szeredi33649c92006-06-25 05:48:52 -0700259{
260 struct fuse_req *req;
261
262 atomic_inc(&fc->num_waiting);
Maxim Patlasov0aada882013-03-21 18:02:28 +0400263 wait_event(fc->blocked_waitq, fc->initialized);
Miklos Szeredi9759bd512015-01-06 10:45:35 +0100264 /* Matches smp_wmb() in fuse_set_initialized() */
265 smp_rmb();
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400266 req = fuse_request_alloc(0);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700267 if (!req)
268 req = get_reserved_req(fc, file);
269
270 fuse_req_init_context(req);
271 req->waiting = 1;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400272 req->background = 0;
Miklos Szeredi33649c92006-06-25 05:48:52 -0700273 return req;
274}
275
Miklos Szeredi334f4852005-09-09 13:10:27 -0700276void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req)
277{
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800278 if (atomic_dec_and_test(&req->count)) {
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400279 if (unlikely(req->background)) {
280 /*
281 * We get here in the unlikely case that a background
282 * request was allocated but not sent
283 */
284 spin_lock(&fc->lock);
285 if (!fc->blocked)
286 wake_up(&fc->blocked_waitq);
287 spin_unlock(&fc->lock);
288 }
289
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200290 if (req->waiting)
291 atomic_dec(&fc->num_waiting);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700292
293 if (req->stolen_file)
294 put_reserved_req(fc, req);
295 else
296 fuse_request_free(req);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800297 }
298}
Tejun Heo08cbf542009-04-14 10:54:53 +0900299EXPORT_SYMBOL_GPL(fuse_put_request);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800300
Miklos Szeredid12def12008-02-06 01:38:39 -0800301static unsigned len_args(unsigned numargs, struct fuse_arg *args)
302{
303 unsigned nbytes = 0;
304 unsigned i;
305
306 for (i = 0; i < numargs; i++)
307 nbytes += args[i].size;
308
309 return nbytes;
310}
311
312static u64 fuse_get_unique(struct fuse_conn *fc)
313{
314 fc->reqctr++;
315 /* zero is special */
316 if (fc->reqctr == 0)
317 fc->reqctr = 1;
318
319 return fc->reqctr;
320}
321
322static void queue_request(struct fuse_conn *fc, struct fuse_req *req)
323{
Miklos Szeredid12def12008-02-06 01:38:39 -0800324 req->in.h.len = sizeof(struct fuse_in_header) +
325 len_args(req->in.numargs, (struct fuse_arg *) req->in.args);
326 list_add_tail(&req->list, &fc->pending);
327 req->state = FUSE_REQ_PENDING;
328 if (!req->waiting) {
329 req->waiting = 1;
330 atomic_inc(&fc->num_waiting);
331 }
332 wake_up(&fc->waitq);
333 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
334}
335
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100336void fuse_queue_forget(struct fuse_conn *fc, struct fuse_forget_link *forget,
337 u64 nodeid, u64 nlookup)
338{
Miklos Szeredi02c048b2010-12-07 20:16:56 +0100339 forget->forget_one.nodeid = nodeid;
340 forget->forget_one.nlookup = nlookup;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100341
342 spin_lock(&fc->lock);
Miklos Szeredi5dfcc872011-09-12 09:38:03 +0200343 if (fc->connected) {
344 fc->forget_list_tail->next = forget;
345 fc->forget_list_tail = forget;
346 wake_up(&fc->waitq);
347 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
348 } else {
349 kfree(forget);
350 }
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100351 spin_unlock(&fc->lock);
352}
353
Miklos Szeredid12def12008-02-06 01:38:39 -0800354static void flush_bg_queue(struct fuse_conn *fc)
355{
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700356 while (fc->active_background < fc->max_background &&
Miklos Szeredid12def12008-02-06 01:38:39 -0800357 !list_empty(&fc->bg_queue)) {
358 struct fuse_req *req;
359
360 req = list_entry(fc->bg_queue.next, struct fuse_req, list);
361 list_del(&req->list);
362 fc->active_background++;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200363 req->in.h.unique = fuse_get_unique(fc);
Miklos Szeredid12def12008-02-06 01:38:39 -0800364 queue_request(fc, req);
365 }
366}
367
Miklos Szeredi6dbbcb12006-04-26 10:49:06 +0200368/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700369 * This function is called when a request is finished. Either a reply
Miklos Szeredif9a28422006-06-25 05:48:53 -0700370 * has arrived or it was aborted (and not yet sent) or some error
Miklos Szeredif43b1552006-01-16 22:14:26 -0800371 * occurred during communication with userspace, or the device file
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700372 * was closed. The requester thread is woken up (if still waiting),
373 * the 'end' callback is called if given, else the reference to the
374 * request is released
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800375 *
Miklos Szeredid7133112006-04-10 22:54:55 -0700376 * Called with fc->lock, unlocks it
Miklos Szeredi334f4852005-09-09 13:10:27 -0700377 */
378static void request_end(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +0200379__releases(fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700380{
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700381 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
382 req->end = NULL;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800383 list_del(&req->list);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700384 list_del(&req->intr_entry);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800385 req->state = FUSE_REQ_FINISHED;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700386 if (req->background) {
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400387 req->background = 0;
388
389 if (fc->num_background == fc->max_background)
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700390 fc->blocked = 0;
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400391
392 /* Wake up next waiter, if any */
Miklos Szeredi3c18ef82013-04-17 21:50:58 +0200393 if (!fc->blocked && waitqueue_active(&fc->blocked_waitq))
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400394 wake_up(&fc->blocked_waitq);
395
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700396 if (fc->num_background == fc->congestion_threshold &&
Tejun Heoa325f9b2009-04-14 10:54:52 +0900397 fc->connected && fc->bdi_initialized) {
Jens Axboe8aa7e842009-07-09 14:52:32 +0200398 clear_bdi_congested(&fc->bdi, BLK_RW_SYNC);
399 clear_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
Miklos Szeredif92b99b2007-10-16 23:30:59 -0700400 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700401 fc->num_background--;
Miklos Szeredid12def12008-02-06 01:38:39 -0800402 fc->active_background--;
403 flush_bg_queue(fc);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700404 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700405 spin_unlock(&fc->lock);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700406 wake_up(&req->waitq);
407 if (end)
408 end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100409 fuse_put_request(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700410}
411
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700412static void wait_answer_interruptible(struct fuse_conn *fc,
413 struct fuse_req *req)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +0200414__releases(fc->lock)
415__acquires(fc->lock)
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700416{
417 if (signal_pending(current))
418 return;
419
420 spin_unlock(&fc->lock);
421 wait_event_interruptible(req->waitq, req->state == FUSE_REQ_FINISHED);
422 spin_lock(&fc->lock);
423}
424
425static void queue_interrupt(struct fuse_conn *fc, struct fuse_req *req)
426{
427 list_add_tail(&req->intr_entry, &fc->interrupts);
428 wake_up(&fc->waitq);
429 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
430}
431
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700432static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +0200433__releases(fc->lock)
434__acquires(fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700435{
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700436 if (!fc->no_interrupt) {
437 /* Any signal may interrupt this */
438 wait_answer_interruptible(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700439
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700440 if (req->aborted)
441 goto aborted;
442 if (req->state == FUSE_REQ_FINISHED)
443 return;
444
445 req->interrupted = 1;
446 if (req->state == FUSE_REQ_SENT)
447 queue_interrupt(fc, req);
448 }
449
Miklos Szeredia131de02007-10-16 23:31:04 -0700450 if (!req->force) {
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700451 sigset_t oldset;
452
453 /* Only fatal signals may interrupt this */
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700454 block_sigs(&oldset);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700455 wait_answer_interruptible(fc, req);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700456 restore_sigs(&oldset);
Miklos Szeredia131de02007-10-16 23:31:04 -0700457
458 if (req->aborted)
459 goto aborted;
460 if (req->state == FUSE_REQ_FINISHED)
461 return;
462
463 /* Request is not yet in userspace, bail out */
464 if (req->state == FUSE_REQ_PENDING) {
465 list_del(&req->list);
466 __fuse_put_request(req);
467 req->out.h.error = -EINTR;
468 return;
469 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700470 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700471
Miklos Szeredia131de02007-10-16 23:31:04 -0700472 /*
473 * Either request is already in userspace, or it was forced.
474 * Wait it out.
475 */
476 spin_unlock(&fc->lock);
477 wait_event(req->waitq, req->state == FUSE_REQ_FINISHED);
478 spin_lock(&fc->lock);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700479
Miklos Szeredia131de02007-10-16 23:31:04 -0700480 if (!req->aborted)
481 return;
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700482
483 aborted:
Miklos Szeredia131de02007-10-16 23:31:04 -0700484 BUG_ON(req->state != FUSE_REQ_FINISHED);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700485 if (req->locked) {
486 /* This is uninterruptible sleep, because data is
487 being copied to/from the buffers of req. During
488 locked state, there mustn't be any filesystem
489 operation (e.g. page fault), since that could lead
490 to deadlock */
Miklos Szeredid7133112006-04-10 22:54:55 -0700491 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700492 wait_event(req->waitq, !req->locked);
Miklos Szeredid7133112006-04-10 22:54:55 -0700493 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700494 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700495}
496
Eric Wong6a4e9222013-02-04 13:04:44 +0000497static void __fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700498{
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400499 BUG_ON(req->background);
Miklos Szeredid7133112006-04-10 22:54:55 -0700500 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700501 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700502 req->out.h.error = -ENOTCONN;
503 else if (fc->conn_error)
504 req->out.h.error = -ECONNREFUSED;
505 else {
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200506 req->in.h.unique = fuse_get_unique(fc);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700507 queue_request(fc, req);
508 /* acquire extra reference, since request is still needed
509 after request_end() */
510 __fuse_get_request(req);
511
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700512 request_wait_answer(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700513 }
Miklos Szeredid7133112006-04-10 22:54:55 -0700514 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700515}
Eric Wong6a4e9222013-02-04 13:04:44 +0000516
517void fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
518{
519 req->isreply = 1;
520 __fuse_request_send(fc, req);
521}
Tejun Heo08cbf542009-04-14 10:54:53 +0900522EXPORT_SYMBOL_GPL(fuse_request_send);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700523
Miklos Szeredi21f62172015-01-06 10:45:35 +0100524static void fuse_adjust_compat(struct fuse_conn *fc, struct fuse_args *args)
525{
526 if (fc->minor < 4 && args->in.h.opcode == FUSE_STATFS)
527 args->out.args[0].size = FUSE_COMPAT_STATFS_SIZE;
528
529 if (fc->minor < 9) {
530 switch (args->in.h.opcode) {
531 case FUSE_LOOKUP:
532 case FUSE_CREATE:
533 case FUSE_MKNOD:
534 case FUSE_MKDIR:
535 case FUSE_SYMLINK:
536 case FUSE_LINK:
537 args->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
538 break;
539 case FUSE_GETATTR:
540 case FUSE_SETATTR:
541 args->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
542 break;
543 }
544 }
545 if (fc->minor < 12) {
546 switch (args->in.h.opcode) {
547 case FUSE_CREATE:
548 args->in.args[0].size = sizeof(struct fuse_open_in);
549 break;
550 case FUSE_MKNOD:
551 args->in.args[0].size = FUSE_COMPAT_MKNOD_IN_SIZE;
552 break;
553 }
554 }
555}
556
Miklos Szeredi70781872014-12-12 09:49:05 +0100557ssize_t fuse_simple_request(struct fuse_conn *fc, struct fuse_args *args)
558{
559 struct fuse_req *req;
560 ssize_t ret;
561
562 req = fuse_get_req(fc, 0);
563 if (IS_ERR(req))
564 return PTR_ERR(req);
565
Miklos Szeredi21f62172015-01-06 10:45:35 +0100566 /* Needs to be done after fuse_get_req() so that fc->minor is valid */
567 fuse_adjust_compat(fc, args);
568
Miklos Szeredi70781872014-12-12 09:49:05 +0100569 req->in.h.opcode = args->in.h.opcode;
570 req->in.h.nodeid = args->in.h.nodeid;
571 req->in.numargs = args->in.numargs;
572 memcpy(req->in.args, args->in.args,
573 args->in.numargs * sizeof(struct fuse_in_arg));
574 req->out.argvar = args->out.argvar;
575 req->out.numargs = args->out.numargs;
576 memcpy(req->out.args, args->out.args,
577 args->out.numargs * sizeof(struct fuse_arg));
578 fuse_request_send(fc, req);
579 ret = req->out.h.error;
580 if (!ret && args->out.argvar) {
581 BUG_ON(args->out.numargs != 1);
582 ret = req->out.args[0].size;
583 }
584 fuse_put_request(fc, req);
585
586 return ret;
587}
588
Tejun Heob93f8582008-11-26 12:03:55 +0100589static void fuse_request_send_nowait_locked(struct fuse_conn *fc,
590 struct fuse_req *req)
Miklos Szeredid12def12008-02-06 01:38:39 -0800591{
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400592 BUG_ON(!req->background);
Miklos Szeredid12def12008-02-06 01:38:39 -0800593 fc->num_background++;
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700594 if (fc->num_background == fc->max_background)
Miklos Szeredid12def12008-02-06 01:38:39 -0800595 fc->blocked = 1;
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700596 if (fc->num_background == fc->congestion_threshold &&
Tejun Heoa325f9b2009-04-14 10:54:52 +0900597 fc->bdi_initialized) {
Jens Axboe8aa7e842009-07-09 14:52:32 +0200598 set_bdi_congested(&fc->bdi, BLK_RW_SYNC);
599 set_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
Miklos Szeredid12def12008-02-06 01:38:39 -0800600 }
601 list_add_tail(&req->list, &fc->bg_queue);
602 flush_bg_queue(fc);
603}
604
Tejun Heob93f8582008-11-26 12:03:55 +0100605static void fuse_request_send_nowait(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700606{
Miklos Szeredi42dc6212015-07-01 16:25:56 +0200607 BUG_ON(!req->end);
Miklos Szeredid7133112006-04-10 22:54:55 -0700608 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700609 if (fc->connected) {
Tejun Heob93f8582008-11-26 12:03:55 +0100610 fuse_request_send_nowait_locked(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700611 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700612 } else {
Miklos Szeredi42dc6212015-07-01 16:25:56 +0200613 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700614 req->out.h.error = -ENOTCONN;
Miklos Szeredi42dc6212015-07-01 16:25:56 +0200615 req->end(fc, req);
616 fuse_put_request(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700617 }
618}
619
Tejun Heob93f8582008-11-26 12:03:55 +0100620void fuse_request_send_background(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700621{
622 req->isreply = 1;
Tejun Heob93f8582008-11-26 12:03:55 +0100623 fuse_request_send_nowait(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700624}
Tejun Heo08cbf542009-04-14 10:54:53 +0900625EXPORT_SYMBOL_GPL(fuse_request_send_background);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700626
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200627static int fuse_request_send_notify_reply(struct fuse_conn *fc,
628 struct fuse_req *req, u64 unique)
629{
630 int err = -ENODEV;
631
632 req->isreply = 0;
633 req->in.h.unique = unique;
634 spin_lock(&fc->lock);
635 if (fc->connected) {
636 queue_request(fc, req);
637 err = 0;
638 }
639 spin_unlock(&fc->lock);
640
641 return err;
642}
643
Miklos Szeredi334f4852005-09-09 13:10:27 -0700644/*
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700645 * Called under fc->lock
646 *
647 * fc->connected must have been checked previously
648 */
Tejun Heob93f8582008-11-26 12:03:55 +0100649void fuse_request_send_background_locked(struct fuse_conn *fc,
650 struct fuse_req *req)
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700651{
652 req->isreply = 1;
Tejun Heob93f8582008-11-26 12:03:55 +0100653 fuse_request_send_nowait_locked(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700654}
655
Anand V. Avati0b05b182012-08-19 08:53:23 -0400656void fuse_force_forget(struct file *file, u64 nodeid)
657{
Al Viro6131ffa2013-02-27 16:59:05 -0500658 struct inode *inode = file_inode(file);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400659 struct fuse_conn *fc = get_fuse_conn(inode);
660 struct fuse_req *req;
661 struct fuse_forget_in inarg;
662
663 memset(&inarg, 0, sizeof(inarg));
664 inarg.nlookup = 1;
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400665 req = fuse_get_req_nofail_nopages(fc, file);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400666 req->in.h.opcode = FUSE_FORGET;
667 req->in.h.nodeid = nodeid;
668 req->in.numargs = 1;
669 req->in.args[0].size = sizeof(inarg);
670 req->in.args[0].value = &inarg;
671 req->isreply = 0;
Eric Wong6a4e9222013-02-04 13:04:44 +0000672 __fuse_request_send(fc, req);
673 /* ignore errors */
674 fuse_put_request(fc, req);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400675}
676
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700677/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700678 * Lock the request. Up to the next unlock_request() there mustn't be
679 * anything that could cause a page-fault. If the request was already
Miklos Szeredif9a28422006-06-25 05:48:53 -0700680 * aborted bail out.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700681 */
Miklos Szeredid7133112006-04-10 22:54:55 -0700682static int lock_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700683{
684 int err = 0;
685 if (req) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700686 spin_lock(&fc->lock);
Miklos Szeredif9a28422006-06-25 05:48:53 -0700687 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700688 err = -ENOENT;
689 else
690 req->locked = 1;
Miklos Szeredid7133112006-04-10 22:54:55 -0700691 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700692 }
693 return err;
694}
695
696/*
Miklos Szeredif9a28422006-06-25 05:48:53 -0700697 * Unlock request. If it was aborted during being locked, the
Miklos Szeredi334f4852005-09-09 13:10:27 -0700698 * requester thread is currently waiting for it to be unlocked, so
699 * wake it up.
700 */
Miklos Szeredid7133112006-04-10 22:54:55 -0700701static void unlock_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700702{
703 if (req) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700704 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700705 req->locked = 0;
Miklos Szeredif9a28422006-06-25 05:48:53 -0700706 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700707 wake_up(&req->waitq);
Miklos Szeredid7133112006-04-10 22:54:55 -0700708 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700709 }
710}
711
712struct fuse_copy_state {
Miklos Szeredid7133112006-04-10 22:54:55 -0700713 struct fuse_conn *fc;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700714 int write;
715 struct fuse_req *req;
Al Viro6c09e942015-04-03 22:06:08 -0400716 struct iov_iter *iter;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200717 struct pipe_buffer *pipebufs;
718 struct pipe_buffer *currbuf;
719 struct pipe_inode_info *pipe;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700720 unsigned long nr_segs;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700721 struct page *pg;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700722 unsigned len;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200723 unsigned offset;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200724 unsigned move_pages:1;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700725};
726
Al Viro6c09e942015-04-03 22:06:08 -0400727static void fuse_copy_init(struct fuse_copy_state *cs,
728 struct fuse_conn *fc,
Miklos Szeredic3021622010-05-25 15:06:07 +0200729 int write,
Al Viro6c09e942015-04-03 22:06:08 -0400730 struct iov_iter *iter)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700731{
732 memset(cs, 0, sizeof(*cs));
Miklos Szeredid7133112006-04-10 22:54:55 -0700733 cs->fc = fc;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700734 cs->write = write;
Al Viro6c09e942015-04-03 22:06:08 -0400735 cs->iter = iter;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700736}
737
738/* Unmap and put previous page of userspace buffer */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800739static void fuse_copy_finish(struct fuse_copy_state *cs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700740{
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200741 if (cs->currbuf) {
742 struct pipe_buffer *buf = cs->currbuf;
743
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200744 if (cs->write)
Miklos Szeredic3021622010-05-25 15:06:07 +0200745 buf->len = PAGE_SIZE - cs->len;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200746 cs->currbuf = NULL;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200747 } else if (cs->pg) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700748 if (cs->write) {
749 flush_dcache_page(cs->pg);
750 set_page_dirty_lock(cs->pg);
751 }
752 put_page(cs->pg);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700753 }
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200754 cs->pg = NULL;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700755}
756
757/*
758 * Get another pagefull of userspace buffer, and map it to kernel
759 * address space, and lock request
760 */
761static int fuse_copy_fill(struct fuse_copy_state *cs)
762{
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200763 struct page *page;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700764 int err;
765
Miklos Szeredid7133112006-04-10 22:54:55 -0700766 unlock_request(cs->fc, cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700767 fuse_copy_finish(cs);
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200768 if (cs->pipebufs) {
769 struct pipe_buffer *buf = cs->pipebufs;
770
Miklos Szeredic3021622010-05-25 15:06:07 +0200771 if (!cs->write) {
772 err = buf->ops->confirm(cs->pipe, buf);
773 if (err)
774 return err;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200775
Miklos Szeredic3021622010-05-25 15:06:07 +0200776 BUG_ON(!cs->nr_segs);
777 cs->currbuf = buf;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200778 cs->pg = buf->page;
779 cs->offset = buf->offset;
Miklos Szeredic3021622010-05-25 15:06:07 +0200780 cs->len = buf->len;
Miklos Szeredic3021622010-05-25 15:06:07 +0200781 cs->pipebufs++;
782 cs->nr_segs--;
783 } else {
Miklos Szeredic3021622010-05-25 15:06:07 +0200784 if (cs->nr_segs == cs->pipe->buffers)
785 return -EIO;
786
787 page = alloc_page(GFP_HIGHUSER);
788 if (!page)
789 return -ENOMEM;
790
791 buf->page = page;
792 buf->offset = 0;
793 buf->len = 0;
794
795 cs->currbuf = buf;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200796 cs->pg = page;
797 cs->offset = 0;
Miklos Szeredic3021622010-05-25 15:06:07 +0200798 cs->len = PAGE_SIZE;
799 cs->pipebufs++;
800 cs->nr_segs++;
801 }
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200802 } else {
Al Viro6c09e942015-04-03 22:06:08 -0400803 size_t off;
804 err = iov_iter_get_pages(cs->iter, &page, PAGE_SIZE, 1, &off);
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200805 if (err < 0)
806 return err;
Al Viro6c09e942015-04-03 22:06:08 -0400807 BUG_ON(!err);
808 cs->len = err;
809 cs->offset = off;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200810 cs->pg = page;
Al Viro6c09e942015-04-03 22:06:08 -0400811 cs->offset = off;
812 iov_iter_advance(cs->iter, err);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700813 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700814
Miklos Szeredid7133112006-04-10 22:54:55 -0700815 return lock_request(cs->fc, cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700816}
817
818/* Do as much copy to/from userspace buffer as we can */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800819static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700820{
821 unsigned ncpy = min(*size, cs->len);
822 if (val) {
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200823 void *pgaddr = kmap_atomic(cs->pg);
824 void *buf = pgaddr + cs->offset;
825
Miklos Szeredi334f4852005-09-09 13:10:27 -0700826 if (cs->write)
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200827 memcpy(buf, *val, ncpy);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700828 else
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200829 memcpy(*val, buf, ncpy);
830
831 kunmap_atomic(pgaddr);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700832 *val += ncpy;
833 }
834 *size -= ncpy;
835 cs->len -= ncpy;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200836 cs->offset += ncpy;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700837 return ncpy;
838}
839
Miklos Szeredice534fb2010-05-25 15:06:07 +0200840static int fuse_check_page(struct page *page)
841{
842 if (page_mapcount(page) ||
843 page->mapping != NULL ||
844 page_count(page) != 1 ||
845 (page->flags & PAGE_FLAGS_CHECK_AT_PREP &
846 ~(1 << PG_locked |
847 1 << PG_referenced |
848 1 << PG_uptodate |
849 1 << PG_lru |
850 1 << PG_active |
851 1 << PG_reclaim))) {
852 printk(KERN_WARNING "fuse: trying to steal weird page\n");
853 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);
854 return 1;
855 }
856 return 0;
857}
858
859static int fuse_try_move_page(struct fuse_copy_state *cs, struct page **pagep)
860{
861 int err;
862 struct page *oldpage = *pagep;
863 struct page *newpage;
864 struct pipe_buffer *buf = cs->pipebufs;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200865
866 unlock_request(cs->fc, cs->req);
867 fuse_copy_finish(cs);
868
869 err = buf->ops->confirm(cs->pipe, buf);
870 if (err)
871 return err;
872
873 BUG_ON(!cs->nr_segs);
874 cs->currbuf = buf;
875 cs->len = buf->len;
876 cs->pipebufs++;
877 cs->nr_segs--;
878
879 if (cs->len != PAGE_SIZE)
880 goto out_fallback;
881
882 if (buf->ops->steal(cs->pipe, buf) != 0)
883 goto out_fallback;
884
885 newpage = buf->page;
886
Miklos Szerediaa991b32015-02-26 11:45:47 +0100887 if (!PageUptodate(newpage))
888 SetPageUptodate(newpage);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200889
890 ClearPageMappedToDisk(newpage);
891
892 if (fuse_check_page(newpage) != 0)
893 goto out_fallback_unlock;
894
Miklos Szeredice534fb2010-05-25 15:06:07 +0200895 /*
896 * This is a new and locked page, it shouldn't be mapped or
897 * have any special flags on it
898 */
899 if (WARN_ON(page_mapped(oldpage)))
900 goto out_fallback_unlock;
901 if (WARN_ON(page_has_private(oldpage)))
902 goto out_fallback_unlock;
903 if (WARN_ON(PageDirty(oldpage) || PageWriteback(oldpage)))
904 goto out_fallback_unlock;
905 if (WARN_ON(PageMlocked(oldpage)))
906 goto out_fallback_unlock;
907
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700908 err = replace_page_cache_page(oldpage, newpage, GFP_KERNEL);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200909 if (err) {
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700910 unlock_page(newpage);
911 return err;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200912 }
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700913
Miklos Szeredice534fb2010-05-25 15:06:07 +0200914 page_cache_get(newpage);
915
916 if (!(buf->flags & PIPE_BUF_FLAG_LRU))
917 lru_cache_add_file(newpage);
918
919 err = 0;
920 spin_lock(&cs->fc->lock);
921 if (cs->req->aborted)
922 err = -ENOENT;
923 else
924 *pagep = newpage;
925 spin_unlock(&cs->fc->lock);
926
927 if (err) {
928 unlock_page(newpage);
929 page_cache_release(newpage);
930 return err;
931 }
932
933 unlock_page(oldpage);
934 page_cache_release(oldpage);
935 cs->len = 0;
936
937 return 0;
938
939out_fallback_unlock:
940 unlock_page(newpage);
941out_fallback:
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200942 cs->pg = buf->page;
943 cs->offset = buf->offset;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200944
945 err = lock_request(cs->fc, cs->req);
946 if (err)
947 return err;
948
949 return 1;
950}
951
Miklos Szeredic3021622010-05-25 15:06:07 +0200952static int fuse_ref_page(struct fuse_copy_state *cs, struct page *page,
953 unsigned offset, unsigned count)
954{
955 struct pipe_buffer *buf;
956
957 if (cs->nr_segs == cs->pipe->buffers)
958 return -EIO;
959
960 unlock_request(cs->fc, cs->req);
961 fuse_copy_finish(cs);
962
963 buf = cs->pipebufs;
964 page_cache_get(page);
965 buf->page = page;
966 buf->offset = offset;
967 buf->len = count;
968
969 cs->pipebufs++;
970 cs->nr_segs++;
971 cs->len = 0;
972
973 return 0;
974}
975
Miklos Szeredi334f4852005-09-09 13:10:27 -0700976/*
977 * Copy a page in the request to/from the userspace buffer. Must be
978 * done atomically
979 */
Miklos Szeredice534fb2010-05-25 15:06:07 +0200980static int fuse_copy_page(struct fuse_copy_state *cs, struct page **pagep,
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800981 unsigned offset, unsigned count, int zeroing)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700982{
Miklos Szeredice534fb2010-05-25 15:06:07 +0200983 int err;
984 struct page *page = *pagep;
985
Miklos Szeredib6777c42010-10-26 14:22:27 -0700986 if (page && zeroing && count < PAGE_SIZE)
987 clear_highpage(page);
988
Miklos Szeredi334f4852005-09-09 13:10:27 -0700989 while (count) {
Miklos Szeredic3021622010-05-25 15:06:07 +0200990 if (cs->write && cs->pipebufs && page) {
991 return fuse_ref_page(cs, page, offset, count);
992 } else if (!cs->len) {
Miklos Szeredice534fb2010-05-25 15:06:07 +0200993 if (cs->move_pages && page &&
994 offset == 0 && count == PAGE_SIZE) {
995 err = fuse_try_move_page(cs, pagep);
996 if (err <= 0)
997 return err;
998 } else {
999 err = fuse_copy_fill(cs);
1000 if (err)
1001 return err;
1002 }
Miklos Szeredi1729a162008-11-26 12:03:54 +01001003 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001004 if (page) {
Cong Wang2408f6e2011-11-25 23:14:30 +08001005 void *mapaddr = kmap_atomic(page);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001006 void *buf = mapaddr + offset;
1007 offset += fuse_copy_do(cs, &buf, &count);
Cong Wang2408f6e2011-11-25 23:14:30 +08001008 kunmap_atomic(mapaddr);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001009 } else
1010 offset += fuse_copy_do(cs, NULL, &count);
1011 }
1012 if (page && !cs->write)
1013 flush_dcache_page(page);
1014 return 0;
1015}
1016
1017/* Copy pages in the request to/from userspace buffer */
1018static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
1019 int zeroing)
1020{
1021 unsigned i;
1022 struct fuse_req *req = cs->req;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001023
1024 for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
Miklos Szeredice534fb2010-05-25 15:06:07 +02001025 int err;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001026 unsigned offset = req->page_descs[i].offset;
1027 unsigned count = min(nbytes, req->page_descs[i].length);
Miklos Szeredice534fb2010-05-25 15:06:07 +02001028
1029 err = fuse_copy_page(cs, &req->pages[i], offset, count,
1030 zeroing);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001031 if (err)
1032 return err;
1033
1034 nbytes -= count;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001035 }
1036 return 0;
1037}
1038
1039/* Copy a single argument in the request to/from userspace buffer */
1040static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
1041{
1042 while (size) {
Miklos Szeredi1729a162008-11-26 12:03:54 +01001043 if (!cs->len) {
1044 int err = fuse_copy_fill(cs);
1045 if (err)
1046 return err;
1047 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001048 fuse_copy_do(cs, &val, &size);
1049 }
1050 return 0;
1051}
1052
1053/* Copy request arguments to/from userspace buffer */
1054static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
1055 unsigned argpages, struct fuse_arg *args,
1056 int zeroing)
1057{
1058 int err = 0;
1059 unsigned i;
1060
1061 for (i = 0; !err && i < numargs; i++) {
1062 struct fuse_arg *arg = &args[i];
1063 if (i == numargs - 1 && argpages)
1064 err = fuse_copy_pages(cs, arg->size, zeroing);
1065 else
1066 err = fuse_copy_one(cs, arg->value, arg->size);
1067 }
1068 return err;
1069}
1070
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001071static int forget_pending(struct fuse_conn *fc)
1072{
1073 return fc->forget_list_head.next != NULL;
1074}
1075
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001076static int request_pending(struct fuse_conn *fc)
1077{
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001078 return !list_empty(&fc->pending) || !list_empty(&fc->interrupts) ||
1079 forget_pending(fc);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001080}
1081
Miklos Szeredi334f4852005-09-09 13:10:27 -07001082/* Wait until a request is available on the pending list */
1083static void request_wait(struct fuse_conn *fc)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001084__releases(fc->lock)
1085__acquires(fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001086{
1087 DECLARE_WAITQUEUE(wait, current);
1088
1089 add_wait_queue_exclusive(&fc->waitq, &wait);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001090 while (fc->connected && !request_pending(fc)) {
Miklos Szeredi334f4852005-09-09 13:10:27 -07001091 set_current_state(TASK_INTERRUPTIBLE);
1092 if (signal_pending(current))
1093 break;
1094
Miklos Szeredid7133112006-04-10 22:54:55 -07001095 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001096 schedule();
Miklos Szeredid7133112006-04-10 22:54:55 -07001097 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001098 }
1099 set_current_state(TASK_RUNNING);
1100 remove_wait_queue(&fc->waitq, &wait);
1101}
1102
1103/*
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001104 * Transfer an interrupt request to userspace
1105 *
1106 * Unlike other requests this is assembled on demand, without a need
1107 * to allocate a separate fuse_req structure.
1108 *
1109 * Called with fc->lock held, releases it
1110 */
Miklos Szeredic3021622010-05-25 15:06:07 +02001111static int fuse_read_interrupt(struct fuse_conn *fc, struct fuse_copy_state *cs,
1112 size_t nbytes, struct fuse_req *req)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001113__releases(fc->lock)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001114{
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001115 struct fuse_in_header ih;
1116 struct fuse_interrupt_in arg;
1117 unsigned reqsize = sizeof(ih) + sizeof(arg);
1118 int err;
1119
1120 list_del_init(&req->intr_entry);
1121 req->intr_unique = fuse_get_unique(fc);
1122 memset(&ih, 0, sizeof(ih));
1123 memset(&arg, 0, sizeof(arg));
1124 ih.len = reqsize;
1125 ih.opcode = FUSE_INTERRUPT;
1126 ih.unique = req->intr_unique;
1127 arg.unique = req->in.h.unique;
1128
1129 spin_unlock(&fc->lock);
Miklos Szeredic3021622010-05-25 15:06:07 +02001130 if (nbytes < reqsize)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001131 return -EINVAL;
1132
Miklos Szeredic3021622010-05-25 15:06:07 +02001133 err = fuse_copy_one(cs, &ih, sizeof(ih));
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001134 if (!err)
Miklos Szeredic3021622010-05-25 15:06:07 +02001135 err = fuse_copy_one(cs, &arg, sizeof(arg));
1136 fuse_copy_finish(cs);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001137
1138 return err ? err : reqsize;
1139}
1140
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001141static struct fuse_forget_link *dequeue_forget(struct fuse_conn *fc,
1142 unsigned max,
1143 unsigned *countp)
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001144{
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001145 struct fuse_forget_link *head = fc->forget_list_head.next;
1146 struct fuse_forget_link **newhead = &head;
1147 unsigned count;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001148
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001149 for (count = 0; *newhead != NULL && count < max; count++)
1150 newhead = &(*newhead)->next;
1151
1152 fc->forget_list_head.next = *newhead;
1153 *newhead = NULL;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001154 if (fc->forget_list_head.next == NULL)
1155 fc->forget_list_tail = &fc->forget_list_head;
1156
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001157 if (countp != NULL)
1158 *countp = count;
1159
1160 return head;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001161}
1162
1163static int fuse_read_single_forget(struct fuse_conn *fc,
1164 struct fuse_copy_state *cs,
1165 size_t nbytes)
1166__releases(fc->lock)
1167{
1168 int err;
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001169 struct fuse_forget_link *forget = dequeue_forget(fc, 1, NULL);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001170 struct fuse_forget_in arg = {
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001171 .nlookup = forget->forget_one.nlookup,
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001172 };
1173 struct fuse_in_header ih = {
1174 .opcode = FUSE_FORGET,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001175 .nodeid = forget->forget_one.nodeid,
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001176 .unique = fuse_get_unique(fc),
1177 .len = sizeof(ih) + sizeof(arg),
1178 };
1179
1180 spin_unlock(&fc->lock);
1181 kfree(forget);
1182 if (nbytes < ih.len)
1183 return -EINVAL;
1184
1185 err = fuse_copy_one(cs, &ih, sizeof(ih));
1186 if (!err)
1187 err = fuse_copy_one(cs, &arg, sizeof(arg));
1188 fuse_copy_finish(cs);
1189
1190 if (err)
1191 return err;
1192
1193 return ih.len;
1194}
1195
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001196static int fuse_read_batch_forget(struct fuse_conn *fc,
1197 struct fuse_copy_state *cs, size_t nbytes)
1198__releases(fc->lock)
1199{
1200 int err;
1201 unsigned max_forgets;
1202 unsigned count;
1203 struct fuse_forget_link *head;
1204 struct fuse_batch_forget_in arg = { .count = 0 };
1205 struct fuse_in_header ih = {
1206 .opcode = FUSE_BATCH_FORGET,
1207 .unique = fuse_get_unique(fc),
1208 .len = sizeof(ih) + sizeof(arg),
1209 };
1210
1211 if (nbytes < ih.len) {
1212 spin_unlock(&fc->lock);
1213 return -EINVAL;
1214 }
1215
1216 max_forgets = (nbytes - ih.len) / sizeof(struct fuse_forget_one);
1217 head = dequeue_forget(fc, max_forgets, &count);
1218 spin_unlock(&fc->lock);
1219
1220 arg.count = count;
1221 ih.len += count * sizeof(struct fuse_forget_one);
1222 err = fuse_copy_one(cs, &ih, sizeof(ih));
1223 if (!err)
1224 err = fuse_copy_one(cs, &arg, sizeof(arg));
1225
1226 while (head) {
1227 struct fuse_forget_link *forget = head;
1228
1229 if (!err) {
1230 err = fuse_copy_one(cs, &forget->forget_one,
1231 sizeof(forget->forget_one));
1232 }
1233 head = forget->next;
1234 kfree(forget);
1235 }
1236
1237 fuse_copy_finish(cs);
1238
1239 if (err)
1240 return err;
1241
1242 return ih.len;
1243}
1244
1245static int fuse_read_forget(struct fuse_conn *fc, struct fuse_copy_state *cs,
1246 size_t nbytes)
1247__releases(fc->lock)
1248{
1249 if (fc->minor < 16 || fc->forget_list_head.next->next == NULL)
1250 return fuse_read_single_forget(fc, cs, nbytes);
1251 else
1252 return fuse_read_batch_forget(fc, cs, nbytes);
1253}
1254
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001255/*
Miklos Szeredi334f4852005-09-09 13:10:27 -07001256 * Read a single request into the userspace filesystem's buffer. This
1257 * function waits until a request is available, then removes it from
1258 * the pending list and copies request data to userspace buffer. If
Miklos Szeredif9a28422006-06-25 05:48:53 -07001259 * no reply is needed (FORGET) or request has been aborted or there
1260 * was an error during the copying then it's finished by calling
Miklos Szeredi334f4852005-09-09 13:10:27 -07001261 * request_end(). Otherwise add it to the processing list, and set
1262 * the 'sent' flag.
1263 */
Miklos Szeredic3021622010-05-25 15:06:07 +02001264static ssize_t fuse_dev_do_read(struct fuse_conn *fc, struct file *file,
1265 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001266{
1267 int err;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001268 struct fuse_req *req;
1269 struct fuse_in *in;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001270 unsigned reqsize;
1271
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001272 restart:
Miklos Szeredid7133112006-04-10 22:54:55 -07001273 spin_lock(&fc->lock);
Jeff Dikee5ac1d12006-04-10 22:54:53 -07001274 err = -EAGAIN;
1275 if ((file->f_flags & O_NONBLOCK) && fc->connected &&
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001276 !request_pending(fc))
Jeff Dikee5ac1d12006-04-10 22:54:53 -07001277 goto err_unlock;
1278
Miklos Szeredi334f4852005-09-09 13:10:27 -07001279 request_wait(fc);
1280 err = -ENODEV;
Miklos Szeredi9ba7cbb2006-01-16 22:14:34 -08001281 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001282 goto err_unlock;
1283 err = -ERESTARTSYS;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001284 if (!request_pending(fc))
Miklos Szeredi334f4852005-09-09 13:10:27 -07001285 goto err_unlock;
1286
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001287 if (!list_empty(&fc->interrupts)) {
1288 req = list_entry(fc->interrupts.next, struct fuse_req,
1289 intr_entry);
Miklos Szeredic3021622010-05-25 15:06:07 +02001290 return fuse_read_interrupt(fc, cs, nbytes, req);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001291 }
1292
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001293 if (forget_pending(fc)) {
1294 if (list_empty(&fc->pending) || fc->forget_batch-- > 0)
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001295 return fuse_read_forget(fc, cs, nbytes);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001296
1297 if (fc->forget_batch <= -8)
1298 fc->forget_batch = 16;
1299 }
1300
Miklos Szeredi334f4852005-09-09 13:10:27 -07001301 req = list_entry(fc->pending.next, struct fuse_req, list);
Miklos Szeredi83cfd492006-01-16 22:14:31 -08001302 req->state = FUSE_REQ_READING;
Miklos Szeredid77a1d52006-01-16 22:14:31 -08001303 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001304
1305 in = &req->in;
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001306 reqsize = in->h.len;
1307 /* If request is too large, reply with an error and restart the read */
Miklos Szeredic3021622010-05-25 15:06:07 +02001308 if (nbytes < reqsize) {
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001309 req->out.h.error = -EIO;
1310 /* SETXATTR is special, since it may contain too large data */
1311 if (in->h.opcode == FUSE_SETXATTR)
1312 req->out.h.error = -E2BIG;
1313 request_end(fc, req);
1314 goto restart;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001315 }
Miklos Szeredid7133112006-04-10 22:54:55 -07001316 spin_unlock(&fc->lock);
Miklos Szeredic3021622010-05-25 15:06:07 +02001317 cs->req = req;
1318 err = fuse_copy_one(cs, &in->h, sizeof(in->h));
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001319 if (!err)
Miklos Szeredic3021622010-05-25 15:06:07 +02001320 err = fuse_copy_args(cs, in->numargs, in->argpages,
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001321 (struct fuse_arg *) in->args, 0);
Miklos Szeredic3021622010-05-25 15:06:07 +02001322 fuse_copy_finish(cs);
Miklos Szeredid7133112006-04-10 22:54:55 -07001323 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001324 req->locked = 0;
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -07001325 if (req->aborted) {
1326 request_end(fc, req);
1327 return -ENODEV;
1328 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001329 if (err) {
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -07001330 req->out.h.error = -EIO;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001331 request_end(fc, req);
1332 return err;
1333 }
1334 if (!req->isreply)
1335 request_end(fc, req);
1336 else {
Miklos Szeredi83cfd492006-01-16 22:14:31 -08001337 req->state = FUSE_REQ_SENT;
Miklos Szeredid77a1d52006-01-16 22:14:31 -08001338 list_move_tail(&req->list, &fc->processing);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001339 if (req->interrupted)
1340 queue_interrupt(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -07001341 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001342 }
1343 return reqsize;
1344
1345 err_unlock:
Miklos Szeredid7133112006-04-10 22:54:55 -07001346 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001347 return err;
1348}
1349
Tom Van Braeckel94e4fe22015-01-12 05:22:16 +01001350static int fuse_dev_open(struct inode *inode, struct file *file)
1351{
1352 /*
1353 * The fuse device's file's private_data is used to hold
1354 * the fuse_conn(ection) when it is mounted, and is used to
1355 * keep track of whether the file has been mounted already.
1356 */
1357 file->private_data = NULL;
1358 return 0;
1359}
1360
Al Virofbdbacc2015-04-03 21:53:39 -04001361static ssize_t fuse_dev_read(struct kiocb *iocb, struct iov_iter *to)
Miklos Szeredic3021622010-05-25 15:06:07 +02001362{
1363 struct fuse_copy_state cs;
1364 struct file *file = iocb->ki_filp;
1365 struct fuse_conn *fc = fuse_get_conn(file);
1366 if (!fc)
1367 return -EPERM;
1368
Al Virofbdbacc2015-04-03 21:53:39 -04001369 if (!iter_is_iovec(to))
1370 return -EINVAL;
Miklos Szeredic3021622010-05-25 15:06:07 +02001371
Al Viro6c09e942015-04-03 22:06:08 -04001372 fuse_copy_init(&cs, fc, 1, to);
Al Virofbdbacc2015-04-03 21:53:39 -04001373
1374 return fuse_dev_do_read(fc, file, &cs, iov_iter_count(to));
Miklos Szeredic3021622010-05-25 15:06:07 +02001375}
1376
Miklos Szeredic3021622010-05-25 15:06:07 +02001377static ssize_t fuse_dev_splice_read(struct file *in, loff_t *ppos,
1378 struct pipe_inode_info *pipe,
1379 size_t len, unsigned int flags)
1380{
1381 int ret;
1382 int page_nr = 0;
1383 int do_wakeup = 0;
1384 struct pipe_buffer *bufs;
1385 struct fuse_copy_state cs;
1386 struct fuse_conn *fc = fuse_get_conn(in);
1387 if (!fc)
1388 return -EPERM;
1389
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001390 bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), GFP_KERNEL);
Miklos Szeredic3021622010-05-25 15:06:07 +02001391 if (!bufs)
1392 return -ENOMEM;
1393
Al Viro6c09e942015-04-03 22:06:08 -04001394 fuse_copy_init(&cs, fc, 1, NULL);
Miklos Szeredic3021622010-05-25 15:06:07 +02001395 cs.pipebufs = bufs;
1396 cs.pipe = pipe;
1397 ret = fuse_dev_do_read(fc, in, &cs, len);
1398 if (ret < 0)
1399 goto out;
1400
1401 ret = 0;
1402 pipe_lock(pipe);
1403
1404 if (!pipe->readers) {
1405 send_sig(SIGPIPE, current, 0);
1406 if (!ret)
1407 ret = -EPIPE;
1408 goto out_unlock;
1409 }
1410
1411 if (pipe->nrbufs + cs.nr_segs > pipe->buffers) {
1412 ret = -EIO;
1413 goto out_unlock;
1414 }
1415
1416 while (page_nr < cs.nr_segs) {
1417 int newbuf = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
1418 struct pipe_buffer *buf = pipe->bufs + newbuf;
1419
1420 buf->page = bufs[page_nr].page;
1421 buf->offset = bufs[page_nr].offset;
1422 buf->len = bufs[page_nr].len;
Miklos Szeredi28a625c2014-01-22 19:36:57 +01001423 /*
1424 * Need to be careful about this. Having buf->ops in module
1425 * code can Oops if the buffer persists after module unload.
1426 */
1427 buf->ops = &nosteal_pipe_buf_ops;
Miklos Szeredic3021622010-05-25 15:06:07 +02001428
1429 pipe->nrbufs++;
1430 page_nr++;
1431 ret += buf->len;
1432
Al Viro6447a3c2013-03-21 11:01:38 -04001433 if (pipe->files)
Miklos Szeredic3021622010-05-25 15:06:07 +02001434 do_wakeup = 1;
1435 }
1436
1437out_unlock:
1438 pipe_unlock(pipe);
1439
1440 if (do_wakeup) {
1441 smp_mb();
1442 if (waitqueue_active(&pipe->wait))
1443 wake_up_interruptible(&pipe->wait);
1444 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
1445 }
1446
1447out:
1448 for (; page_nr < cs.nr_segs; page_nr++)
1449 page_cache_release(bufs[page_nr].page);
1450
1451 kfree(bufs);
1452 return ret;
1453}
1454
Tejun Heo95668a62008-11-26 12:03:55 +01001455static int fuse_notify_poll(struct fuse_conn *fc, unsigned int size,
1456 struct fuse_copy_state *cs)
1457{
1458 struct fuse_notify_poll_wakeup_out outarg;
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001459 int err = -EINVAL;
Tejun Heo95668a62008-11-26 12:03:55 +01001460
1461 if (size != sizeof(outarg))
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001462 goto err;
Tejun Heo95668a62008-11-26 12:03:55 +01001463
1464 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1465 if (err)
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001466 goto err;
Tejun Heo95668a62008-11-26 12:03:55 +01001467
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001468 fuse_copy_finish(cs);
Tejun Heo95668a62008-11-26 12:03:55 +01001469 return fuse_notify_poll_wakeup(fc, &outarg);
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001470
1471err:
1472 fuse_copy_finish(cs);
1473 return err;
Tejun Heo95668a62008-11-26 12:03:55 +01001474}
1475
John Muir3b463ae2009-05-31 11:13:57 -04001476static int fuse_notify_inval_inode(struct fuse_conn *fc, unsigned int size,
1477 struct fuse_copy_state *cs)
1478{
1479 struct fuse_notify_inval_inode_out outarg;
1480 int err = -EINVAL;
1481
1482 if (size != sizeof(outarg))
1483 goto err;
1484
1485 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1486 if (err)
1487 goto err;
1488 fuse_copy_finish(cs);
1489
1490 down_read(&fc->killsb);
1491 err = -ENOENT;
Miklos Szeredib21dda42010-02-05 12:08:31 +01001492 if (fc->sb) {
1493 err = fuse_reverse_inval_inode(fc->sb, outarg.ino,
1494 outarg.off, outarg.len);
1495 }
John Muir3b463ae2009-05-31 11:13:57 -04001496 up_read(&fc->killsb);
1497 return err;
1498
1499err:
1500 fuse_copy_finish(cs);
1501 return err;
1502}
1503
1504static int fuse_notify_inval_entry(struct fuse_conn *fc, unsigned int size,
1505 struct fuse_copy_state *cs)
1506{
1507 struct fuse_notify_inval_entry_out outarg;
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001508 int err = -ENOMEM;
1509 char *buf;
John Muir3b463ae2009-05-31 11:13:57 -04001510 struct qstr name;
1511
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001512 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1513 if (!buf)
1514 goto err;
1515
1516 err = -EINVAL;
John Muir3b463ae2009-05-31 11:13:57 -04001517 if (size < sizeof(outarg))
1518 goto err;
1519
1520 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1521 if (err)
1522 goto err;
1523
1524 err = -ENAMETOOLONG;
1525 if (outarg.namelen > FUSE_NAME_MAX)
1526 goto err;
1527
Miklos Szeredic2183d12011-08-24 10:20:17 +02001528 err = -EINVAL;
1529 if (size != sizeof(outarg) + outarg.namelen + 1)
1530 goto err;
1531
John Muir3b463ae2009-05-31 11:13:57 -04001532 name.name = buf;
1533 name.len = outarg.namelen;
1534 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1535 if (err)
1536 goto err;
1537 fuse_copy_finish(cs);
1538 buf[outarg.namelen] = 0;
1539 name.hash = full_name_hash(name.name, name.len);
1540
1541 down_read(&fc->killsb);
1542 err = -ENOENT;
Miklos Szeredib21dda42010-02-05 12:08:31 +01001543 if (fc->sb)
John Muir451d0f52011-12-06 21:50:06 +01001544 err = fuse_reverse_inval_entry(fc->sb, outarg.parent, 0, &name);
1545 up_read(&fc->killsb);
1546 kfree(buf);
1547 return err;
1548
1549err:
1550 kfree(buf);
1551 fuse_copy_finish(cs);
1552 return err;
1553}
1554
1555static int fuse_notify_delete(struct fuse_conn *fc, unsigned int size,
1556 struct fuse_copy_state *cs)
1557{
1558 struct fuse_notify_delete_out outarg;
1559 int err = -ENOMEM;
1560 char *buf;
1561 struct qstr name;
1562
1563 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1564 if (!buf)
1565 goto err;
1566
1567 err = -EINVAL;
1568 if (size < sizeof(outarg))
1569 goto err;
1570
1571 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1572 if (err)
1573 goto err;
1574
1575 err = -ENAMETOOLONG;
1576 if (outarg.namelen > FUSE_NAME_MAX)
1577 goto err;
1578
1579 err = -EINVAL;
1580 if (size != sizeof(outarg) + outarg.namelen + 1)
1581 goto err;
1582
1583 name.name = buf;
1584 name.len = outarg.namelen;
1585 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1586 if (err)
1587 goto err;
1588 fuse_copy_finish(cs);
1589 buf[outarg.namelen] = 0;
1590 name.hash = full_name_hash(name.name, name.len);
1591
1592 down_read(&fc->killsb);
1593 err = -ENOENT;
1594 if (fc->sb)
1595 err = fuse_reverse_inval_entry(fc->sb, outarg.parent,
1596 outarg.child, &name);
John Muir3b463ae2009-05-31 11:13:57 -04001597 up_read(&fc->killsb);
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001598 kfree(buf);
John Muir3b463ae2009-05-31 11:13:57 -04001599 return err;
1600
1601err:
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001602 kfree(buf);
John Muir3b463ae2009-05-31 11:13:57 -04001603 fuse_copy_finish(cs);
1604 return err;
1605}
1606
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001607static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
1608 struct fuse_copy_state *cs)
1609{
1610 struct fuse_notify_store_out outarg;
1611 struct inode *inode;
1612 struct address_space *mapping;
1613 u64 nodeid;
1614 int err;
1615 pgoff_t index;
1616 unsigned int offset;
1617 unsigned int num;
1618 loff_t file_size;
1619 loff_t end;
1620
1621 err = -EINVAL;
1622 if (size < sizeof(outarg))
1623 goto out_finish;
1624
1625 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1626 if (err)
1627 goto out_finish;
1628
1629 err = -EINVAL;
1630 if (size - sizeof(outarg) != outarg.size)
1631 goto out_finish;
1632
1633 nodeid = outarg.nodeid;
1634
1635 down_read(&fc->killsb);
1636
1637 err = -ENOENT;
1638 if (!fc->sb)
1639 goto out_up_killsb;
1640
1641 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1642 if (!inode)
1643 goto out_up_killsb;
1644
1645 mapping = inode->i_mapping;
1646 index = outarg.offset >> PAGE_CACHE_SHIFT;
1647 offset = outarg.offset & ~PAGE_CACHE_MASK;
1648 file_size = i_size_read(inode);
1649 end = outarg.offset + outarg.size;
1650 if (end > file_size) {
1651 file_size = end;
1652 fuse_write_update_size(inode, file_size);
1653 }
1654
1655 num = outarg.size;
1656 while (num) {
1657 struct page *page;
1658 unsigned int this_num;
1659
1660 err = -ENOMEM;
1661 page = find_or_create_page(mapping, index,
1662 mapping_gfp_mask(mapping));
1663 if (!page)
1664 goto out_iput;
1665
1666 this_num = min_t(unsigned, num, PAGE_CACHE_SIZE - offset);
1667 err = fuse_copy_page(cs, &page, offset, this_num, 0);
Miklos Szeredi063ec1e2014-01-22 19:36:58 +01001668 if (!err && offset == 0 &&
1669 (this_num == PAGE_CACHE_SIZE || file_size == end))
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001670 SetPageUptodate(page);
1671 unlock_page(page);
1672 page_cache_release(page);
1673
1674 if (err)
1675 goto out_iput;
1676
1677 num -= this_num;
1678 offset = 0;
1679 index++;
1680 }
1681
1682 err = 0;
1683
1684out_iput:
1685 iput(inode);
1686out_up_killsb:
1687 up_read(&fc->killsb);
1688out_finish:
1689 fuse_copy_finish(cs);
1690 return err;
1691}
1692
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001693static void fuse_retrieve_end(struct fuse_conn *fc, struct fuse_req *req)
1694{
Mel Gormanb745bc82014-06-04 16:10:22 -07001695 release_pages(req->pages, req->num_pages, false);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001696}
1697
1698static int fuse_retrieve(struct fuse_conn *fc, struct inode *inode,
1699 struct fuse_notify_retrieve_out *outarg)
1700{
1701 int err;
1702 struct address_space *mapping = inode->i_mapping;
1703 struct fuse_req *req;
1704 pgoff_t index;
1705 loff_t file_size;
1706 unsigned int num;
1707 unsigned int offset;
Geert Uytterhoeven01574432010-09-30 22:06:21 +02001708 size_t total_len = 0;
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001709 int num_pages;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001710
1711 offset = outarg->offset & ~PAGE_CACHE_MASK;
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001712 file_size = i_size_read(inode);
1713
1714 num = outarg->size;
1715 if (outarg->offset > file_size)
1716 num = 0;
1717 else if (outarg->offset + num > file_size)
1718 num = file_size - outarg->offset;
1719
1720 num_pages = (num + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1721 num_pages = min(num_pages, FUSE_MAX_PAGES_PER_REQ);
1722
1723 req = fuse_get_req(fc, num_pages);
1724 if (IS_ERR(req))
1725 return PTR_ERR(req);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001726
1727 req->in.h.opcode = FUSE_NOTIFY_REPLY;
1728 req->in.h.nodeid = outarg->nodeid;
1729 req->in.numargs = 2;
1730 req->in.argpages = 1;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001731 req->page_descs[0].offset = offset;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001732 req->end = fuse_retrieve_end;
1733
1734 index = outarg->offset >> PAGE_CACHE_SHIFT;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001735
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001736 while (num && req->num_pages < num_pages) {
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001737 struct page *page;
1738 unsigned int this_num;
1739
1740 page = find_get_page(mapping, index);
1741 if (!page)
1742 break;
1743
1744 this_num = min_t(unsigned, num, PAGE_CACHE_SIZE - offset);
1745 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001746 req->page_descs[req->num_pages].length = this_num;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001747 req->num_pages++;
1748
Miklos Szeredic9e67d42012-09-04 18:45:54 +02001749 offset = 0;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001750 num -= this_num;
1751 total_len += this_num;
Miklos Szeredi48706d02011-12-13 10:36:59 +01001752 index++;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001753 }
1754 req->misc.retrieve_in.offset = outarg->offset;
1755 req->misc.retrieve_in.size = total_len;
1756 req->in.args[0].size = sizeof(req->misc.retrieve_in);
1757 req->in.args[0].value = &req->misc.retrieve_in;
1758 req->in.args[1].size = total_len;
1759
1760 err = fuse_request_send_notify_reply(fc, req, outarg->notify_unique);
1761 if (err)
1762 fuse_retrieve_end(fc, req);
1763
1764 return err;
1765}
1766
1767static int fuse_notify_retrieve(struct fuse_conn *fc, unsigned int size,
1768 struct fuse_copy_state *cs)
1769{
1770 struct fuse_notify_retrieve_out outarg;
1771 struct inode *inode;
1772 int err;
1773
1774 err = -EINVAL;
1775 if (size != sizeof(outarg))
1776 goto copy_finish;
1777
1778 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1779 if (err)
1780 goto copy_finish;
1781
1782 fuse_copy_finish(cs);
1783
1784 down_read(&fc->killsb);
1785 err = -ENOENT;
1786 if (fc->sb) {
1787 u64 nodeid = outarg.nodeid;
1788
1789 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1790 if (inode) {
1791 err = fuse_retrieve(fc, inode, &outarg);
1792 iput(inode);
1793 }
1794 }
1795 up_read(&fc->killsb);
1796
1797 return err;
1798
1799copy_finish:
1800 fuse_copy_finish(cs);
1801 return err;
1802}
1803
Tejun Heo85993962008-11-26 12:03:55 +01001804static int fuse_notify(struct fuse_conn *fc, enum fuse_notify_code code,
1805 unsigned int size, struct fuse_copy_state *cs)
1806{
Miklos Szeredi0d278362015-02-26 11:45:47 +01001807 /* Don't try to move pages (yet) */
1808 cs->move_pages = 0;
1809
Tejun Heo85993962008-11-26 12:03:55 +01001810 switch (code) {
Tejun Heo95668a62008-11-26 12:03:55 +01001811 case FUSE_NOTIFY_POLL:
1812 return fuse_notify_poll(fc, size, cs);
1813
John Muir3b463ae2009-05-31 11:13:57 -04001814 case FUSE_NOTIFY_INVAL_INODE:
1815 return fuse_notify_inval_inode(fc, size, cs);
1816
1817 case FUSE_NOTIFY_INVAL_ENTRY:
1818 return fuse_notify_inval_entry(fc, size, cs);
1819
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001820 case FUSE_NOTIFY_STORE:
1821 return fuse_notify_store(fc, size, cs);
1822
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001823 case FUSE_NOTIFY_RETRIEVE:
1824 return fuse_notify_retrieve(fc, size, cs);
1825
John Muir451d0f52011-12-06 21:50:06 +01001826 case FUSE_NOTIFY_DELETE:
1827 return fuse_notify_delete(fc, size, cs);
1828
Tejun Heo85993962008-11-26 12:03:55 +01001829 default:
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001830 fuse_copy_finish(cs);
Tejun Heo85993962008-11-26 12:03:55 +01001831 return -EINVAL;
1832 }
1833}
1834
Miklos Szeredi334f4852005-09-09 13:10:27 -07001835/* Look up request on processing list by unique ID */
1836static struct fuse_req *request_find(struct fuse_conn *fc, u64 unique)
1837{
Dong Fang05726ac2013-07-30 22:50:01 -04001838 struct fuse_req *req;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001839
Dong Fang05726ac2013-07-30 22:50:01 -04001840 list_for_each_entry(req, &fc->processing, list) {
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001841 if (req->in.h.unique == unique || req->intr_unique == unique)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001842 return req;
1843 }
1844 return NULL;
1845}
1846
1847static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
1848 unsigned nbytes)
1849{
1850 unsigned reqsize = sizeof(struct fuse_out_header);
1851
1852 if (out->h.error)
1853 return nbytes != reqsize ? -EINVAL : 0;
1854
1855 reqsize += len_args(out->numargs, out->args);
1856
1857 if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
1858 return -EINVAL;
1859 else if (reqsize > nbytes) {
1860 struct fuse_arg *lastarg = &out->args[out->numargs-1];
1861 unsigned diffsize = reqsize - nbytes;
1862 if (diffsize > lastarg->size)
1863 return -EINVAL;
1864 lastarg->size -= diffsize;
1865 }
1866 return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
1867 out->page_zeroing);
1868}
1869
1870/*
1871 * Write a single reply to a request. First the header is copied from
1872 * the write buffer. The request is then searched on the processing
1873 * list by the unique ID found in the header. If found, then remove
1874 * it from the list and copy the rest of the buffer to the request.
1875 * The request is finished by calling request_end()
1876 */
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001877static ssize_t fuse_dev_do_write(struct fuse_conn *fc,
1878 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001879{
1880 int err;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001881 struct fuse_req *req;
1882 struct fuse_out_header oh;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001883
Miklos Szeredi334f4852005-09-09 13:10:27 -07001884 if (nbytes < sizeof(struct fuse_out_header))
1885 return -EINVAL;
1886
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001887 err = fuse_copy_one(cs, &oh, sizeof(oh));
Miklos Szeredi334f4852005-09-09 13:10:27 -07001888 if (err)
1889 goto err_finish;
Tejun Heo85993962008-11-26 12:03:55 +01001890
Miklos Szeredi334f4852005-09-09 13:10:27 -07001891 err = -EINVAL;
Tejun Heo85993962008-11-26 12:03:55 +01001892 if (oh.len != nbytes)
1893 goto err_finish;
1894
1895 /*
1896 * Zero oh.unique indicates unsolicited notification message
1897 * and error contains notification code.
1898 */
1899 if (!oh.unique) {
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001900 err = fuse_notify(fc, oh.error, nbytes - sizeof(oh), cs);
Tejun Heo85993962008-11-26 12:03:55 +01001901 return err ? err : nbytes;
1902 }
1903
1904 err = -EINVAL;
1905 if (oh.error <= -1000 || oh.error > 0)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001906 goto err_finish;
1907
Miklos Szeredid7133112006-04-10 22:54:55 -07001908 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001909 err = -ENOENT;
1910 if (!fc->connected)
1911 goto err_unlock;
1912
Miklos Szeredi334f4852005-09-09 13:10:27 -07001913 req = request_find(fc, oh.unique);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001914 if (!req)
1915 goto err_unlock;
1916
Miklos Szeredif9a28422006-06-25 05:48:53 -07001917 if (req->aborted) {
Miklos Szeredid7133112006-04-10 22:54:55 -07001918 spin_unlock(&fc->lock);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001919 fuse_copy_finish(cs);
Miklos Szeredid7133112006-04-10 22:54:55 -07001920 spin_lock(&fc->lock);
Miklos Szeredi222f1d62006-01-16 22:14:25 -08001921 request_end(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001922 return -ENOENT;
1923 }
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001924 /* Is it an interrupt reply? */
1925 if (req->intr_unique == oh.unique) {
1926 err = -EINVAL;
1927 if (nbytes != sizeof(struct fuse_out_header))
1928 goto err_unlock;
1929
1930 if (oh.error == -ENOSYS)
1931 fc->no_interrupt = 1;
1932 else if (oh.error == -EAGAIN)
1933 queue_interrupt(fc, req);
1934
1935 spin_unlock(&fc->lock);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001936 fuse_copy_finish(cs);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001937 return nbytes;
1938 }
1939
1940 req->state = FUSE_REQ_WRITING;
Miklos Szeredid77a1d52006-01-16 22:14:31 -08001941 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001942 req->out.h = oh;
1943 req->locked = 1;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001944 cs->req = req;
Miklos Szeredice534fb2010-05-25 15:06:07 +02001945 if (!req->out.page_replace)
1946 cs->move_pages = 0;
Miklos Szeredid7133112006-04-10 22:54:55 -07001947 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001948
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001949 err = copy_out_args(cs, &req->out, nbytes);
1950 fuse_copy_finish(cs);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001951
Miklos Szeredid7133112006-04-10 22:54:55 -07001952 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001953 req->locked = 0;
1954 if (!err) {
Miklos Szeredif9a28422006-06-25 05:48:53 -07001955 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001956 err = -ENOENT;
Miklos Szeredif9a28422006-06-25 05:48:53 -07001957 } else if (!req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001958 req->out.h.error = -EIO;
1959 request_end(fc, req);
1960
1961 return err ? err : nbytes;
1962
1963 err_unlock:
Miklos Szeredid7133112006-04-10 22:54:55 -07001964 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001965 err_finish:
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001966 fuse_copy_finish(cs);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001967 return err;
1968}
1969
Al Virofbdbacc2015-04-03 21:53:39 -04001970static ssize_t fuse_dev_write(struct kiocb *iocb, struct iov_iter *from)
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001971{
1972 struct fuse_copy_state cs;
1973 struct fuse_conn *fc = fuse_get_conn(iocb->ki_filp);
1974 if (!fc)
1975 return -EPERM;
1976
Al Virofbdbacc2015-04-03 21:53:39 -04001977 if (!iter_is_iovec(from))
1978 return -EINVAL;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001979
Al Viro6c09e942015-04-03 22:06:08 -04001980 fuse_copy_init(&cs, fc, 0, from);
Al Virofbdbacc2015-04-03 21:53:39 -04001981
1982 return fuse_dev_do_write(fc, &cs, iov_iter_count(from));
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001983}
1984
1985static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe,
1986 struct file *out, loff_t *ppos,
1987 size_t len, unsigned int flags)
1988{
1989 unsigned nbuf;
1990 unsigned idx;
1991 struct pipe_buffer *bufs;
1992 struct fuse_copy_state cs;
1993 struct fuse_conn *fc;
1994 size_t rem;
1995 ssize_t ret;
1996
1997 fc = fuse_get_conn(out);
1998 if (!fc)
1999 return -EPERM;
2000
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01002001 bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), GFP_KERNEL);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002002 if (!bufs)
2003 return -ENOMEM;
2004
2005 pipe_lock(pipe);
2006 nbuf = 0;
2007 rem = 0;
2008 for (idx = 0; idx < pipe->nrbufs && rem < len; idx++)
2009 rem += pipe->bufs[(pipe->curbuf + idx) & (pipe->buffers - 1)].len;
2010
2011 ret = -EINVAL;
2012 if (rem < len) {
2013 pipe_unlock(pipe);
2014 goto out;
2015 }
2016
2017 rem = len;
2018 while (rem) {
2019 struct pipe_buffer *ibuf;
2020 struct pipe_buffer *obuf;
2021
2022 BUG_ON(nbuf >= pipe->buffers);
2023 BUG_ON(!pipe->nrbufs);
2024 ibuf = &pipe->bufs[pipe->curbuf];
2025 obuf = &bufs[nbuf];
2026
2027 if (rem >= ibuf->len) {
2028 *obuf = *ibuf;
2029 ibuf->ops = NULL;
2030 pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
2031 pipe->nrbufs--;
2032 } else {
2033 ibuf->ops->get(pipe, ibuf);
2034 *obuf = *ibuf;
2035 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
2036 obuf->len = rem;
2037 ibuf->offset += obuf->len;
2038 ibuf->len -= obuf->len;
2039 }
2040 nbuf++;
2041 rem -= obuf->len;
2042 }
2043 pipe_unlock(pipe);
2044
Al Viro6c09e942015-04-03 22:06:08 -04002045 fuse_copy_init(&cs, fc, 0, NULL);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002046 cs.pipebufs = bufs;
Al Viro6c09e942015-04-03 22:06:08 -04002047 cs.nr_segs = nbuf;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002048 cs.pipe = pipe;
2049
Miklos Szeredice534fb2010-05-25 15:06:07 +02002050 if (flags & SPLICE_F_MOVE)
2051 cs.move_pages = 1;
2052
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002053 ret = fuse_dev_do_write(fc, &cs, len);
2054
2055 for (idx = 0; idx < nbuf; idx++) {
2056 struct pipe_buffer *buf = &bufs[idx];
2057 buf->ops->release(pipe, buf);
2058 }
2059out:
2060 kfree(bufs);
2061 return ret;
2062}
2063
Miklos Szeredi334f4852005-09-09 13:10:27 -07002064static unsigned fuse_dev_poll(struct file *file, poll_table *wait)
2065{
Miklos Szeredi334f4852005-09-09 13:10:27 -07002066 unsigned mask = POLLOUT | POLLWRNORM;
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07002067 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002068 if (!fc)
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07002069 return POLLERR;
Miklos Szeredi334f4852005-09-09 13:10:27 -07002070
2071 poll_wait(file, &fc->waitq, wait);
2072
Miklos Szeredid7133112006-04-10 22:54:55 -07002073 spin_lock(&fc->lock);
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07002074 if (!fc->connected)
2075 mask = POLLERR;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07002076 else if (request_pending(fc))
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07002077 mask |= POLLIN | POLLRDNORM;
Miklos Szeredid7133112006-04-10 22:54:55 -07002078 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002079
2080 return mask;
2081}
2082
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002083/*
2084 * Abort all requests on the given list (pending or processing)
2085 *
Miklos Szeredid7133112006-04-10 22:54:55 -07002086 * This function releases and reacquires fc->lock
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002087 */
Miklos Szeredi334f4852005-09-09 13:10:27 -07002088static void end_requests(struct fuse_conn *fc, struct list_head *head)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02002089__releases(fc->lock)
2090__acquires(fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -07002091{
2092 while (!list_empty(head)) {
2093 struct fuse_req *req;
2094 req = list_entry(head->next, struct fuse_req, list);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002095 req->out.h.error = -ECONNABORTED;
2096 request_end(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -07002097 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002098 }
2099}
2100
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002101/*
2102 * Abort requests under I/O
2103 *
Miklos Szeredif9a28422006-06-25 05:48:53 -07002104 * The requests are set to aborted and finished, and the request
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002105 * waiter is woken up. This will make request_wait_answer() wait
2106 * until the request is unlocked and then return.
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08002107 *
2108 * If the request is asynchronous, then the end function needs to be
2109 * called after waiting for the request to be unlocked (if it was
2110 * locked).
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002111 */
2112static void end_io_requests(struct fuse_conn *fc)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02002113__releases(fc->lock)
2114__acquires(fc->lock)
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002115{
2116 while (!list_empty(&fc->io)) {
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08002117 struct fuse_req *req =
2118 list_entry(fc->io.next, struct fuse_req, list);
2119 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
2120
Miklos Szeredif9a28422006-06-25 05:48:53 -07002121 req->aborted = 1;
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002122 req->out.h.error = -ECONNABORTED;
2123 req->state = FUSE_REQ_FINISHED;
2124 list_del_init(&req->list);
2125 wake_up(&req->waitq);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08002126 if (end) {
2127 req->end = NULL;
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08002128 __fuse_get_request(req);
Miklos Szeredid7133112006-04-10 22:54:55 -07002129 spin_unlock(&fc->lock);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08002130 wait_event(req->waitq, !req->locked);
2131 end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +01002132 fuse_put_request(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -07002133 spin_lock(&fc->lock);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08002134 }
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002135 }
2136}
2137
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002138static void end_queued_requests(struct fuse_conn *fc)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02002139__releases(fc->lock)
2140__acquires(fc->lock)
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002141{
2142 fc->max_background = UINT_MAX;
2143 flush_bg_queue(fc);
2144 end_requests(fc, &fc->pending);
2145 end_requests(fc, &fc->processing);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01002146 while (forget_pending(fc))
Miklos Szeredi02c048b2010-12-07 20:16:56 +01002147 kfree(dequeue_forget(fc, 1, NULL));
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002148}
2149
Bryan Green357ccf22011-03-01 16:43:52 -08002150static void end_polls(struct fuse_conn *fc)
2151{
2152 struct rb_node *p;
2153
2154 p = rb_first(&fc->polled_files);
2155
2156 while (p) {
2157 struct fuse_file *ff;
2158 ff = rb_entry(p, struct fuse_file, polled_node);
2159 wake_up_interruptible_all(&ff->poll_wait);
2160
2161 p = rb_next(p);
2162 }
2163}
2164
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002165/*
2166 * Abort all requests.
2167 *
2168 * Emergency exit in case of a malicious or accidental deadlock, or
2169 * just a hung filesystem.
2170 *
2171 * The same effect is usually achievable through killing the
2172 * filesystem daemon and all users of the filesystem. The exception
2173 * is the combination of an asynchronous request and the tricky
2174 * deadlock (see Documentation/filesystems/fuse.txt).
2175 *
2176 * During the aborting, progression of requests from the pending and
2177 * processing lists onto the io list, and progression of new requests
2178 * onto the pending list is prevented by req->connected being false.
2179 *
2180 * Progression of requests under I/O to the processing list is
Miklos Szeredif9a28422006-06-25 05:48:53 -07002181 * prevented by the req->aborted flag being true for these requests.
2182 * For this reason requests on the io list must be aborted first.
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002183 */
2184void fuse_abort_conn(struct fuse_conn *fc)
2185{
Miklos Szeredid7133112006-04-10 22:54:55 -07002186 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002187 if (fc->connected) {
2188 fc->connected = 0;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -07002189 fc->blocked = 0;
Miklos Szeredi9759bd512015-01-06 10:45:35 +01002190 fuse_set_initialized(fc);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002191 end_io_requests(fc);
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002192 end_queued_requests(fc);
Bryan Green357ccf22011-03-01 16:43:52 -08002193 end_polls(fc);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002194 wake_up_all(&fc->waitq);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -07002195 wake_up_all(&fc->blocked_waitq);
Jeff Dike385a17b2006-04-10 22:54:52 -07002196 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002197 }
Miklos Szeredid7133112006-04-10 22:54:55 -07002198 spin_unlock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002199}
Tejun Heo08cbf542009-04-14 10:54:53 +09002200EXPORT_SYMBOL_GPL(fuse_abort_conn);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002201
Tejun Heo08cbf542009-04-14 10:54:53 +09002202int fuse_dev_release(struct inode *inode, struct file *file)
Miklos Szeredi334f4852005-09-09 13:10:27 -07002203{
Miklos Szeredi0720b312006-04-10 22:54:55 -07002204 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002205 if (fc) {
Miklos Szeredid7133112006-04-10 22:54:55 -07002206 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -07002207 fc->connected = 0;
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002208 fc->blocked = 0;
Miklos Szeredi9759bd512015-01-06 10:45:35 +01002209 fuse_set_initialized(fc);
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002210 end_queued_requests(fc);
Bryan Green357ccf22011-03-01 16:43:52 -08002211 end_polls(fc);
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002212 wake_up_all(&fc->blocked_waitq);
Miklos Szeredid7133112006-04-10 22:54:55 -07002213 spin_unlock(&fc->lock);
Miklos Szeredibafa9652006-06-25 05:48:51 -07002214 fuse_conn_put(fc);
Jeff Dike385a17b2006-04-10 22:54:52 -07002215 }
Miklos Szeredif543f252006-01-16 22:14:35 -08002216
Miklos Szeredi334f4852005-09-09 13:10:27 -07002217 return 0;
2218}
Tejun Heo08cbf542009-04-14 10:54:53 +09002219EXPORT_SYMBOL_GPL(fuse_dev_release);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002220
Jeff Dike385a17b2006-04-10 22:54:52 -07002221static int fuse_dev_fasync(int fd, struct file *file, int on)
2222{
2223 struct fuse_conn *fc = fuse_get_conn(file);
2224 if (!fc)
Miklos Szeredia87046d2006-04-10 22:54:56 -07002225 return -EPERM;
Jeff Dike385a17b2006-04-10 22:54:52 -07002226
2227 /* No locking - fasync_helper does its own locking */
2228 return fasync_helper(fd, file, on, &fc->fasync);
2229}
2230
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002231const struct file_operations fuse_dev_operations = {
Miklos Szeredi334f4852005-09-09 13:10:27 -07002232 .owner = THIS_MODULE,
Tom Van Braeckel94e4fe22015-01-12 05:22:16 +01002233 .open = fuse_dev_open,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002234 .llseek = no_llseek,
Al Virofbdbacc2015-04-03 21:53:39 -04002235 .read_iter = fuse_dev_read,
Miklos Szeredic3021622010-05-25 15:06:07 +02002236 .splice_read = fuse_dev_splice_read,
Al Virofbdbacc2015-04-03 21:53:39 -04002237 .write_iter = fuse_dev_write,
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002238 .splice_write = fuse_dev_splice_write,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002239 .poll = fuse_dev_poll,
2240 .release = fuse_dev_release,
Jeff Dike385a17b2006-04-10 22:54:52 -07002241 .fasync = fuse_dev_fasync,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002242};
Tejun Heo08cbf542009-04-14 10:54:53 +09002243EXPORT_SYMBOL_GPL(fuse_dev_operations);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002244
2245static struct miscdevice fuse_miscdevice = {
2246 .minor = FUSE_MINOR,
2247 .name = "fuse",
2248 .fops = &fuse_dev_operations,
2249};
2250
2251int __init fuse_dev_init(void)
2252{
2253 int err = -ENOMEM;
2254 fuse_req_cachep = kmem_cache_create("fuse_request",
2255 sizeof(struct fuse_req),
Paul Mundt20c2df82007-07-20 10:11:58 +09002256 0, 0, NULL);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002257 if (!fuse_req_cachep)
2258 goto out;
2259
2260 err = misc_register(&fuse_miscdevice);
2261 if (err)
2262 goto out_cache_clean;
2263
2264 return 0;
2265
2266 out_cache_clean:
2267 kmem_cache_destroy(fuse_req_cachep);
2268 out:
2269 return err;
2270}
2271
2272void fuse_dev_cleanup(void)
2273{
2274 misc_deregister(&fuse_miscdevice);
2275 kmem_cache_destroy(fuse_req_cachep);
2276}