blob: 8016cd059db126ab7620680f75ac3d1292e026e8 [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 Szeredicc080e92015-07-01 16:26:08 +020028static struct fuse_dev *fuse_get_dev(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 */
Miklos Szeredicc080e92015-07-01 16:26:08 +020034 return ACCESS_ONCE(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 Szeredi33e14b42015-07-01 16:26:01 +020051 __set_bit(FR_PENDING, &req->flags);
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
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400102void __fuse_get_request(struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700103{
104 atomic_inc(&req->count);
105}
106
107/* Must be called with > 1 refcount */
108static void __fuse_put_request(struct fuse_req *req)
109{
110 BUG_ON(atomic_read(&req->count) < 2);
111 atomic_dec(&req->count);
112}
113
Miklos Szeredi33649c92006-06-25 05:48:52 -0700114static void fuse_req_init_context(struct fuse_req *req)
115{
Eric W. Biederman499dcf22012-02-07 16:26:03 -0800116 req->in.h.uid = from_kuid_munged(&init_user_ns, current_fsuid());
117 req->in.h.gid = from_kgid_munged(&init_user_ns, current_fsgid());
Miklos Szeredi33649c92006-06-25 05:48:52 -0700118 req->in.h.pid = current->pid;
119}
120
Miklos Szeredi9759bd512015-01-06 10:45:35 +0100121void fuse_set_initialized(struct fuse_conn *fc)
122{
123 /* Make sure stores before this are seen on another CPU */
124 smp_wmb();
125 fc->initialized = 1;
126}
127
Maxim Patlasov0aada882013-03-21 18:02:28 +0400128static bool fuse_block_alloc(struct fuse_conn *fc, bool for_background)
129{
130 return !fc->initialized || (for_background && fc->blocked);
131}
132
Miklos Szeredi6465d762018-07-26 16:13:11 +0200133static void fuse_drop_waiting(struct fuse_conn *fc)
134{
135 if (fc->connected) {
136 atomic_dec(&fc->num_waiting);
137 } else if (atomic_dec_and_test(&fc->num_waiting)) {
138 /* wake up aborters */
139 wake_up_all(&fc->blocked_waitq);
140 }
141}
142
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400143static struct fuse_req *__fuse_get_req(struct fuse_conn *fc, unsigned npages,
144 bool for_background)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700145{
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700146 struct fuse_req *req;
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700147 int err;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200148 atomic_inc(&fc->num_waiting);
Maxim Patlasov0aada882013-03-21 18:02:28 +0400149
150 if (fuse_block_alloc(fc, for_background)) {
Maxim Patlasov0aada882013-03-21 18:02:28 +0400151 err = -EINTR;
Al Viro7d3a07f2016-07-19 03:08:27 -0400152 if (wait_event_killable_exclusive(fc->blocked_waitq,
153 !fuse_block_alloc(fc, for_background)))
Maxim Patlasov0aada882013-03-21 18:02:28 +0400154 goto out;
155 }
Miklos Szeredi9759bd512015-01-06 10:45:35 +0100156 /* Matches smp_wmb() in fuse_set_initialized() */
157 smp_rmb();
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
Miklos Szeredide155222015-07-01 16:25:57 +0200163 err = -ECONNREFUSED;
164 if (fc->conn_error)
165 goto out;
166
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400167 req = fuse_request_alloc(npages);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200168 err = -ENOMEM;
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400169 if (!req) {
170 if (for_background)
171 wake_up(&fc->blocked_waitq);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200172 goto out;
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400173 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700174
Miklos Szeredi33649c92006-06-25 05:48:52 -0700175 fuse_req_init_context(req);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200176 __set_bit(FR_WAITING, &req->flags);
177 if (for_background)
178 __set_bit(FR_BACKGROUND, &req->flags);
179
Miklos Szeredi334f4852005-09-09 13:10:27 -0700180 return req;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200181
182 out:
Miklos Szeredi6465d762018-07-26 16:13:11 +0200183 fuse_drop_waiting(fc);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200184 return ERR_PTR(err);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700185}
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400186
187struct fuse_req *fuse_get_req(struct fuse_conn *fc, unsigned npages)
188{
189 return __fuse_get_req(fc, npages, false);
190}
Tejun Heo08cbf542009-04-14 10:54:53 +0900191EXPORT_SYMBOL_GPL(fuse_get_req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700192
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400193struct fuse_req *fuse_get_req_for_background(struct fuse_conn *fc,
194 unsigned npages)
195{
196 return __fuse_get_req(fc, npages, true);
197}
198EXPORT_SYMBOL_GPL(fuse_get_req_for_background);
199
Miklos Szeredi33649c92006-06-25 05:48:52 -0700200/*
201 * Return request in fuse_file->reserved_req. However that may
202 * currently be in use. If that is the case, wait for it to become
203 * available.
204 */
205static struct fuse_req *get_reserved_req(struct fuse_conn *fc,
206 struct file *file)
207{
208 struct fuse_req *req = NULL;
209 struct fuse_file *ff = file->private_data;
210
211 do {
Miklos Szeredide5e3de2007-10-16 23:31:00 -0700212 wait_event(fc->reserved_req_waitq, ff->reserved_req);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700213 spin_lock(&fc->lock);
214 if (ff->reserved_req) {
215 req = ff->reserved_req;
216 ff->reserved_req = NULL;
Al Virocb0942b2012-08-27 14:48:26 -0400217 req->stolen_file = get_file(file);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700218 }
219 spin_unlock(&fc->lock);
220 } while (!req);
221
222 return req;
223}
224
225/*
226 * Put stolen request back into fuse_file->reserved_req
227 */
228static void put_reserved_req(struct fuse_conn *fc, struct fuse_req *req)
229{
230 struct file *file = req->stolen_file;
231 struct fuse_file *ff = file->private_data;
232
233 spin_lock(&fc->lock);
Maxim Patlasovb2430d72012-10-26 19:49:24 +0400234 fuse_request_init(req, req->pages, req->page_descs, req->max_pages);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700235 BUG_ON(ff->reserved_req);
236 ff->reserved_req = req;
Miklos Szeredide5e3de2007-10-16 23:31:00 -0700237 wake_up_all(&fc->reserved_req_waitq);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700238 spin_unlock(&fc->lock);
239 fput(file);
240}
241
242/*
243 * Gets a requests for a file operation, always succeeds
244 *
245 * This is used for sending the FLUSH request, which must get to
246 * userspace, due to POSIX locks which may need to be unlocked.
247 *
248 * If allocation fails due to OOM, use the reserved request in
249 * fuse_file.
250 *
251 * This is very unlikely to deadlock accidentally, since the
252 * filesystem should not have it's own file open. If deadlock is
253 * intentional, it can still be broken by "aborting" the filesystem.
254 */
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400255struct fuse_req *fuse_get_req_nofail_nopages(struct fuse_conn *fc,
256 struct file *file)
Miklos Szeredi33649c92006-06-25 05:48:52 -0700257{
258 struct fuse_req *req;
259
260 atomic_inc(&fc->num_waiting);
Maxim Patlasov0aada882013-03-21 18:02:28 +0400261 wait_event(fc->blocked_waitq, fc->initialized);
Miklos Szeredi9759bd512015-01-06 10:45:35 +0100262 /* Matches smp_wmb() in fuse_set_initialized() */
263 smp_rmb();
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400264 req = fuse_request_alloc(0);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700265 if (!req)
266 req = get_reserved_req(fc, file);
267
268 fuse_req_init_context(req);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200269 __set_bit(FR_WAITING, &req->flags);
270 __clear_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700271 return req;
272}
273
Miklos Szeredi334f4852005-09-09 13:10:27 -0700274void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req)
275{
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800276 if (atomic_dec_and_test(&req->count)) {
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200277 if (test_bit(FR_BACKGROUND, &req->flags)) {
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400278 /*
279 * We get here in the unlikely case that a background
280 * request was allocated but not sent
281 */
282 spin_lock(&fc->lock);
283 if (!fc->blocked)
284 wake_up(&fc->blocked_waitq);
285 spin_unlock(&fc->lock);
286 }
287
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200288 if (test_bit(FR_WAITING, &req->flags)) {
289 __clear_bit(FR_WAITING, &req->flags);
Miklos Szeredi6465d762018-07-26 16:13:11 +0200290 fuse_drop_waiting(fc);
Miklos Szeredi73e0e732015-07-01 16:25:56 +0200291 }
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
Miklos Szeredif88996a2015-07-01 16:26:01 +0200312static u64 fuse_get_unique(struct fuse_iqueue *fiq)
Miklos Szeredid12def12008-02-06 01:38:39 -0800313{
Miklos Szeredif88996a2015-07-01 16:26:01 +0200314 return ++fiq->reqctr;
Miklos Szeredid12def12008-02-06 01:38:39 -0800315}
316
Miklos Szeredif88996a2015-07-01 16:26:01 +0200317static void queue_request(struct fuse_iqueue *fiq, struct fuse_req *req)
Miklos Szeredid12def12008-02-06 01:38:39 -0800318{
Miklos Szeredid12def12008-02-06 01:38:39 -0800319 req->in.h.len = sizeof(struct fuse_in_header) +
320 len_args(req->in.numargs, (struct fuse_arg *) req->in.args);
Miklos Szeredif88996a2015-07-01 16:26:01 +0200321 list_add_tail(&req->list, &fiq->pending);
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200322 wake_up_locked(&fiq->waitq);
Miklos Szeredif88996a2015-07-01 16:26:01 +0200323 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
Miklos Szeredid12def12008-02-06 01:38:39 -0800324}
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 Szeredif88996a2015-07-01 16:26:01 +0200329 struct fuse_iqueue *fiq = &fc->iq;
330
Miklos Szeredi02c048b2010-12-07 20:16:56 +0100331 forget->forget_one.nodeid = nodeid;
332 forget->forget_one.nlookup = nlookup;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100333
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200334 spin_lock(&fiq->waitq.lock);
Miklos Szeredie16714d2015-07-01 16:26:01 +0200335 if (fiq->connected) {
Miklos Szeredif88996a2015-07-01 16:26:01 +0200336 fiq->forget_list_tail->next = forget;
337 fiq->forget_list_tail = forget;
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200338 wake_up_locked(&fiq->waitq);
Miklos Szeredif88996a2015-07-01 16:26:01 +0200339 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
Miklos Szeredi5dfcc872011-09-12 09:38:03 +0200340 } else {
341 kfree(forget);
342 }
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200343 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100344}
345
Miklos Szeredid12def12008-02-06 01:38:39 -0800346static void flush_bg_queue(struct fuse_conn *fc)
347{
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700348 while (fc->active_background < fc->max_background &&
Miklos Szeredid12def12008-02-06 01:38:39 -0800349 !list_empty(&fc->bg_queue)) {
350 struct fuse_req *req;
Miklos Szeredif88996a2015-07-01 16:26:01 +0200351 struct fuse_iqueue *fiq = &fc->iq;
Miklos Szeredid12def12008-02-06 01:38:39 -0800352
353 req = list_entry(fc->bg_queue.next, struct fuse_req, list);
354 list_del(&req->list);
355 fc->active_background++;
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200356 spin_lock(&fiq->waitq.lock);
Miklos Szeredif88996a2015-07-01 16:26:01 +0200357 req->in.h.unique = fuse_get_unique(fiq);
358 queue_request(fiq, req);
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200359 spin_unlock(&fiq->waitq.lock);
Miklos Szeredid12def12008-02-06 01:38:39 -0800360 }
361}
362
Miklos Szeredi6dbbcb12006-04-26 10:49:06 +0200363/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700364 * This function is called when a request is finished. Either a reply
Miklos Szeredif9a28422006-06-25 05:48:53 -0700365 * has arrived or it was aborted (and not yet sent) or some error
Miklos Szeredif43b1552006-01-16 22:14:26 -0800366 * occurred during communication with userspace, or the device file
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700367 * was closed. The requester thread is woken up (if still waiting),
368 * the 'end' callback is called if given, else the reference to the
369 * request is released
Miklos Szeredi334f4852005-09-09 13:10:27 -0700370 */
371static void request_end(struct fuse_conn *fc, struct fuse_req *req)
372{
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200373 struct fuse_iqueue *fiq = &fc->iq;
Miklos Szeredi365ae712015-07-01 16:26:06 +0200374
Miklos Szerediefe28002015-07-01 16:26:07 +0200375 if (test_and_set_bit(FR_FINISHED, &req->flags))
Miklos Szeredi6465d762018-07-26 16:13:11 +0200376 goto put_request;
Miklos Szeredi365ae712015-07-01 16:26:06 +0200377
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200378 spin_lock(&fiq->waitq.lock);
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200379 list_del_init(&req->intr_entry);
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200380 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200381 WARN_ON(test_bit(FR_PENDING, &req->flags));
382 WARN_ON(test_bit(FR_SENT, &req->flags));
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200383 if (test_bit(FR_BACKGROUND, &req->flags)) {
Miklos Szerediefe28002015-07-01 16:26:07 +0200384 spin_lock(&fc->lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200385 clear_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredi7238fcb2018-09-28 16:43:22 +0200386 if (fc->num_background == fc->max_background) {
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700387 fc->blocked = 0;
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400388 wake_up(&fc->blocked_waitq);
Miklos Szeredi7238fcb2018-09-28 16:43:22 +0200389 } else if (!fc->blocked) {
390 /*
391 * Wake up next waiter, if any. It's okay to use
392 * waitqueue_active(), as we've already synced up
393 * fc->blocked with waiters with the wake_up() call
394 * above.
395 */
396 if (waitqueue_active(&fc->blocked_waitq))
397 wake_up(&fc->blocked_waitq);
398 }
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400399
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700400 if (fc->num_background == fc->congestion_threshold &&
Tejun Heoa325f9b2009-04-14 10:54:52 +0900401 fc->connected && fc->bdi_initialized) {
Jens Axboe8aa7e842009-07-09 14:52:32 +0200402 clear_bdi_congested(&fc->bdi, BLK_RW_SYNC);
403 clear_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
Miklos Szeredif92b99b2007-10-16 23:30:59 -0700404 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700405 fc->num_background--;
Miklos Szeredid12def12008-02-06 01:38:39 -0800406 fc->active_background--;
407 flush_bg_queue(fc);
Miklos Szerediefe28002015-07-01 16:26:07 +0200408 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700409 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700410 wake_up(&req->waitq);
Miklos Szeredi1e6881c2015-07-01 16:26:07 +0200411 if (req->end)
412 req->end(fc, req);
Miklos Szeredi6465d762018-07-26 16:13:11 +0200413put_request:
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100414 fuse_put_request(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700415}
416
Miklos Szeredif88996a2015-07-01 16:26:01 +0200417static void queue_interrupt(struct fuse_iqueue *fiq, struct fuse_req *req)
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700418{
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200419 spin_lock(&fiq->waitq.lock);
Sahitya Tummala72834482017-02-08 20:30:56 +0530420 if (test_bit(FR_FINISHED, &req->flags)) {
421 spin_unlock(&fiq->waitq.lock);
422 return;
423 }
Miklos Szeredi8f7bb362015-07-01 16:26:03 +0200424 if (list_empty(&req->intr_entry)) {
425 list_add_tail(&req->intr_entry, &fiq->interrupts);
426 wake_up_locked(&fiq->waitq);
427 }
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200428 spin_unlock(&fiq->waitq.lock);
Miklos Szeredif88996a2015-07-01 16:26:01 +0200429 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700430}
431
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700432static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700433{
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200434 struct fuse_iqueue *fiq = &fc->iq;
Miklos Szeredic4775262015-07-01 16:26:00 +0200435 int err;
436
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700437 if (!fc->no_interrupt) {
438 /* Any signal may interrupt this */
Miklos Szeredic4775262015-07-01 16:26:00 +0200439 err = wait_event_interruptible(req->waitq,
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200440 test_bit(FR_FINISHED, &req->flags));
Miklos Szeredic4775262015-07-01 16:26:00 +0200441 if (!err)
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700442 return;
443
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200444 set_bit(FR_INTERRUPTED, &req->flags);
Miklos Szeredi8f7bb362015-07-01 16:26:03 +0200445 /* matches barrier in fuse_dev_do_read() */
446 smp_mb__after_atomic();
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200447 if (test_bit(FR_SENT, &req->flags))
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200448 queue_interrupt(fiq, req);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700449 }
450
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200451 if (!test_bit(FR_FORCE, &req->flags)) {
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700452 /* Only fatal signals may interrupt this */
Al Viro7d3a07f2016-07-19 03:08:27 -0400453 err = wait_event_killable(req->waitq,
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200454 test_bit(FR_FINISHED, &req->flags));
Miklos Szeredic4775262015-07-01 16:26:00 +0200455 if (!err)
Miklos Szeredia131de02007-10-16 23:31:04 -0700456 return;
457
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200458 spin_lock(&fiq->waitq.lock);
Miklos Szeredia131de02007-10-16 23:31:04 -0700459 /* Request is not yet in userspace, bail out */
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200460 if (test_bit(FR_PENDING, &req->flags)) {
Miklos Szeredia131de02007-10-16 23:31:04 -0700461 list_del(&req->list);
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200462 spin_unlock(&fiq->waitq.lock);
Miklos Szeredia131de02007-10-16 23:31:04 -0700463 __fuse_put_request(req);
464 req->out.h.error = -EINTR;
465 return;
466 }
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200467 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700468 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700469
Miklos Szeredia131de02007-10-16 23:31:04 -0700470 /*
471 * Either request is already in userspace, or it was forced.
472 * Wait it out.
473 */
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200474 wait_event(req->waitq, test_bit(FR_FINISHED, &req->flags));
Miklos Szeredi334f4852005-09-09 13:10:27 -0700475}
476
Eric Wong6a4e9222013-02-04 13:04:44 +0000477static void __fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700478{
Miklos Szeredie16714d2015-07-01 16:26:01 +0200479 struct fuse_iqueue *fiq = &fc->iq;
480
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200481 BUG_ON(test_bit(FR_BACKGROUND, &req->flags));
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200482 spin_lock(&fiq->waitq.lock);
Miklos Szeredie16714d2015-07-01 16:26:01 +0200483 if (!fiq->connected) {
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200484 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700485 req->out.h.error = -ENOTCONN;
Miklos Szeredic4775262015-07-01 16:26:00 +0200486 } else {
Miklos Szeredif88996a2015-07-01 16:26:01 +0200487 req->in.h.unique = fuse_get_unique(fiq);
488 queue_request(fiq, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700489 /* acquire extra reference, since request is still needed
490 after request_end() */
491 __fuse_get_request(req);
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200492 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700493
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700494 request_wait_answer(fc, req);
Miklos Szeredic4775262015-07-01 16:26:00 +0200495 /* Pairs with smp_wmb() in request_end() */
496 smp_rmb();
Miklos Szeredi334f4852005-09-09 13:10:27 -0700497 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700498}
Eric Wong6a4e9222013-02-04 13:04:44 +0000499
500void fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
501{
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200502 __set_bit(FR_ISREPLY, &req->flags);
503 if (!test_bit(FR_WAITING, &req->flags)) {
504 __set_bit(FR_WAITING, &req->flags);
Miklos Szeredi5437f242015-07-01 16:25:56 +0200505 atomic_inc(&fc->num_waiting);
506 }
Eric Wong6a4e9222013-02-04 13:04:44 +0000507 __fuse_request_send(fc, req);
508}
Tejun Heo08cbf542009-04-14 10:54:53 +0900509EXPORT_SYMBOL_GPL(fuse_request_send);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700510
Miklos Szeredi21f62172015-01-06 10:45:35 +0100511static void fuse_adjust_compat(struct fuse_conn *fc, struct fuse_args *args)
512{
513 if (fc->minor < 4 && args->in.h.opcode == FUSE_STATFS)
514 args->out.args[0].size = FUSE_COMPAT_STATFS_SIZE;
515
516 if (fc->minor < 9) {
517 switch (args->in.h.opcode) {
518 case FUSE_LOOKUP:
519 case FUSE_CREATE:
520 case FUSE_MKNOD:
521 case FUSE_MKDIR:
522 case FUSE_SYMLINK:
523 case FUSE_LINK:
524 args->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
525 break;
526 case FUSE_GETATTR:
527 case FUSE_SETATTR:
528 args->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
529 break;
530 }
531 }
532 if (fc->minor < 12) {
533 switch (args->in.h.opcode) {
534 case FUSE_CREATE:
535 args->in.args[0].size = sizeof(struct fuse_open_in);
536 break;
537 case FUSE_MKNOD:
538 args->in.args[0].size = FUSE_COMPAT_MKNOD_IN_SIZE;
539 break;
540 }
541 }
542}
543
Miklos Szeredi70781872014-12-12 09:49:05 +0100544ssize_t fuse_simple_request(struct fuse_conn *fc, struct fuse_args *args)
545{
546 struct fuse_req *req;
547 ssize_t ret;
548
549 req = fuse_get_req(fc, 0);
550 if (IS_ERR(req))
551 return PTR_ERR(req);
552
Miklos Szeredi21f62172015-01-06 10:45:35 +0100553 /* Needs to be done after fuse_get_req() so that fc->minor is valid */
554 fuse_adjust_compat(fc, args);
555
Miklos Szeredi70781872014-12-12 09:49:05 +0100556 req->in.h.opcode = args->in.h.opcode;
557 req->in.h.nodeid = args->in.h.nodeid;
558 req->in.numargs = args->in.numargs;
559 memcpy(req->in.args, args->in.args,
560 args->in.numargs * sizeof(struct fuse_in_arg));
561 req->out.argvar = args->out.argvar;
562 req->out.numargs = args->out.numargs;
563 memcpy(req->out.args, args->out.args,
564 args->out.numargs * sizeof(struct fuse_arg));
565 fuse_request_send(fc, req);
566 ret = req->out.h.error;
567 if (!ret && args->out.argvar) {
568 BUG_ON(args->out.numargs != 1);
569 ret = req->out.args[0].size;
570 }
571 fuse_put_request(fc, req);
572
573 return ret;
574}
575
Miklos Szeredif0139aa2015-07-01 16:25:57 +0200576/*
577 * Called under fc->lock
578 *
579 * fc->connected must have been checked previously
580 */
581void fuse_request_send_background_locked(struct fuse_conn *fc,
582 struct fuse_req *req)
Miklos Szeredid12def12008-02-06 01:38:39 -0800583{
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200584 BUG_ON(!test_bit(FR_BACKGROUND, &req->flags));
585 if (!test_bit(FR_WAITING, &req->flags)) {
586 __set_bit(FR_WAITING, &req->flags);
Miklos Szeredi5437f242015-07-01 16:25:56 +0200587 atomic_inc(&fc->num_waiting);
588 }
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200589 __set_bit(FR_ISREPLY, &req->flags);
Miklos Szeredid12def12008-02-06 01:38:39 -0800590 fc->num_background++;
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700591 if (fc->num_background == fc->max_background)
Miklos Szeredid12def12008-02-06 01:38:39 -0800592 fc->blocked = 1;
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700593 if (fc->num_background == fc->congestion_threshold &&
Tejun Heoa325f9b2009-04-14 10:54:52 +0900594 fc->bdi_initialized) {
Jens Axboe8aa7e842009-07-09 14:52:32 +0200595 set_bdi_congested(&fc->bdi, BLK_RW_SYNC);
596 set_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
Miklos Szeredid12def12008-02-06 01:38:39 -0800597 }
598 list_add_tail(&req->list, &fc->bg_queue);
599 flush_bg_queue(fc);
600}
601
Miklos Szeredif0139aa2015-07-01 16:25:57 +0200602void fuse_request_send_background(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700603{
Miklos Szeredi42dc6212015-07-01 16:25:56 +0200604 BUG_ON(!req->end);
Miklos Szeredid7133112006-04-10 22:54:55 -0700605 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700606 if (fc->connected) {
Miklos Szeredif0139aa2015-07-01 16:25:57 +0200607 fuse_request_send_background_locked(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700608 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700609 } else {
Miklos Szeredi42dc6212015-07-01 16:25:56 +0200610 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700611 req->out.h.error = -ENOTCONN;
Miklos Szeredi42dc6212015-07-01 16:25:56 +0200612 req->end(fc, req);
613 fuse_put_request(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700614 }
615}
Tejun Heo08cbf542009-04-14 10:54:53 +0900616EXPORT_SYMBOL_GPL(fuse_request_send_background);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700617
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200618static int fuse_request_send_notify_reply(struct fuse_conn *fc,
619 struct fuse_req *req, u64 unique)
620{
621 int err = -ENODEV;
Miklos Szeredif88996a2015-07-01 16:26:01 +0200622 struct fuse_iqueue *fiq = &fc->iq;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200623
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200624 __clear_bit(FR_ISREPLY, &req->flags);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200625 req->in.h.unique = unique;
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200626 spin_lock(&fiq->waitq.lock);
Miklos Szeredie16714d2015-07-01 16:26:01 +0200627 if (fiq->connected) {
Miklos Szeredif88996a2015-07-01 16:26:01 +0200628 queue_request(fiq, req);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200629 err = 0;
630 }
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200631 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200632
633 return err;
634}
635
Anand V. Avati0b05b182012-08-19 08:53:23 -0400636void fuse_force_forget(struct file *file, u64 nodeid)
637{
Al Viro6131ffa2013-02-27 16:59:05 -0500638 struct inode *inode = file_inode(file);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400639 struct fuse_conn *fc = get_fuse_conn(inode);
640 struct fuse_req *req;
641 struct fuse_forget_in inarg;
642
643 memset(&inarg, 0, sizeof(inarg));
644 inarg.nlookup = 1;
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400645 req = fuse_get_req_nofail_nopages(fc, file);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400646 req->in.h.opcode = FUSE_FORGET;
647 req->in.h.nodeid = nodeid;
648 req->in.numargs = 1;
649 req->in.args[0].size = sizeof(inarg);
650 req->in.args[0].value = &inarg;
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200651 __clear_bit(FR_ISREPLY, &req->flags);
Eric Wong6a4e9222013-02-04 13:04:44 +0000652 __fuse_request_send(fc, req);
653 /* ignore errors */
654 fuse_put_request(fc, req);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400655}
656
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700657/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700658 * Lock the request. Up to the next unlock_request() there mustn't be
659 * anything that could cause a page-fault. If the request was already
Miklos Szeredif9a28422006-06-25 05:48:53 -0700660 * aborted bail out.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700661 */
Miklos Szeredidc008092015-07-01 16:25:58 +0200662static int lock_request(struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700663{
664 int err = 0;
665 if (req) {
Miklos Szeredidc008092015-07-01 16:25:58 +0200666 spin_lock(&req->waitq.lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200667 if (test_bit(FR_ABORTED, &req->flags))
Miklos Szeredi334f4852005-09-09 13:10:27 -0700668 err = -ENOENT;
669 else
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200670 set_bit(FR_LOCKED, &req->flags);
Miklos Szeredidc008092015-07-01 16:25:58 +0200671 spin_unlock(&req->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700672 }
673 return err;
674}
675
676/*
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200677 * Unlock request. If it was aborted while locked, caller is responsible
678 * for unlocking and ending the request.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700679 */
Miklos Szeredidc008092015-07-01 16:25:58 +0200680static int unlock_request(struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700681{
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200682 int err = 0;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700683 if (req) {
Miklos Szeredidc008092015-07-01 16:25:58 +0200684 spin_lock(&req->waitq.lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200685 if (test_bit(FR_ABORTED, &req->flags))
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200686 err = -ENOENT;
687 else
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200688 clear_bit(FR_LOCKED, &req->flags);
Miklos Szeredidc008092015-07-01 16:25:58 +0200689 spin_unlock(&req->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700690 }
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200691 return err;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700692}
693
694struct fuse_copy_state {
695 int write;
696 struct fuse_req *req;
Al Viro6c09e942015-04-03 22:06:08 -0400697 struct iov_iter *iter;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200698 struct pipe_buffer *pipebufs;
699 struct pipe_buffer *currbuf;
700 struct pipe_inode_info *pipe;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700701 unsigned long nr_segs;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700702 struct page *pg;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700703 unsigned len;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200704 unsigned offset;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200705 unsigned move_pages:1;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700706};
707
Miklos Szeredidc008092015-07-01 16:25:58 +0200708static void fuse_copy_init(struct fuse_copy_state *cs, int write,
Al Viro6c09e942015-04-03 22:06:08 -0400709 struct iov_iter *iter)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700710{
711 memset(cs, 0, sizeof(*cs));
712 cs->write = write;
Al Viro6c09e942015-04-03 22:06:08 -0400713 cs->iter = iter;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700714}
715
716/* Unmap and put previous page of userspace buffer */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800717static void fuse_copy_finish(struct fuse_copy_state *cs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700718{
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200719 if (cs->currbuf) {
720 struct pipe_buffer *buf = cs->currbuf;
721
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200722 if (cs->write)
Miklos Szeredic3021622010-05-25 15:06:07 +0200723 buf->len = PAGE_SIZE - cs->len;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200724 cs->currbuf = NULL;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200725 } else if (cs->pg) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700726 if (cs->write) {
727 flush_dcache_page(cs->pg);
728 set_page_dirty_lock(cs->pg);
729 }
730 put_page(cs->pg);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700731 }
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200732 cs->pg = NULL;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700733}
734
735/*
736 * Get another pagefull of userspace buffer, and map it to kernel
737 * address space, and lock request
738 */
739static int fuse_copy_fill(struct fuse_copy_state *cs)
740{
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200741 struct page *page;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700742 int err;
743
Miklos Szeredidc008092015-07-01 16:25:58 +0200744 err = unlock_request(cs->req);
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200745 if (err)
746 return err;
747
Miklos Szeredi334f4852005-09-09 13:10:27 -0700748 fuse_copy_finish(cs);
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200749 if (cs->pipebufs) {
750 struct pipe_buffer *buf = cs->pipebufs;
751
Miklos Szeredic3021622010-05-25 15:06:07 +0200752 if (!cs->write) {
Miklos Szeredifba597d2016-09-27 10:45:12 +0200753 err = pipe_buf_confirm(cs->pipe, buf);
Miklos Szeredic3021622010-05-25 15:06:07 +0200754 if (err)
755 return err;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200756
Miklos Szeredic3021622010-05-25 15:06:07 +0200757 BUG_ON(!cs->nr_segs);
758 cs->currbuf = buf;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200759 cs->pg = buf->page;
760 cs->offset = buf->offset;
Miklos Szeredic3021622010-05-25 15:06:07 +0200761 cs->len = buf->len;
Miklos Szeredic3021622010-05-25 15:06:07 +0200762 cs->pipebufs++;
763 cs->nr_segs--;
764 } else {
Miklos Szeredic3021622010-05-25 15:06:07 +0200765 if (cs->nr_segs == cs->pipe->buffers)
766 return -EIO;
767
768 page = alloc_page(GFP_HIGHUSER);
769 if (!page)
770 return -ENOMEM;
771
772 buf->page = page;
773 buf->offset = 0;
774 buf->len = 0;
775
776 cs->currbuf = buf;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200777 cs->pg = page;
778 cs->offset = 0;
Miklos Szeredic3021622010-05-25 15:06:07 +0200779 cs->len = PAGE_SIZE;
780 cs->pipebufs++;
781 cs->nr_segs++;
782 }
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200783 } else {
Al Viro6c09e942015-04-03 22:06:08 -0400784 size_t off;
785 err = iov_iter_get_pages(cs->iter, &page, PAGE_SIZE, 1, &off);
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200786 if (err < 0)
787 return err;
Al Viro6c09e942015-04-03 22:06:08 -0400788 BUG_ON(!err);
789 cs->len = err;
790 cs->offset = off;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200791 cs->pg = page;
Al Viro6c09e942015-04-03 22:06:08 -0400792 iov_iter_advance(cs->iter, err);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700793 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700794
Miklos Szeredidc008092015-07-01 16:25:58 +0200795 return lock_request(cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700796}
797
798/* Do as much copy to/from userspace buffer as we can */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800799static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700800{
801 unsigned ncpy = min(*size, cs->len);
802 if (val) {
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200803 void *pgaddr = kmap_atomic(cs->pg);
804 void *buf = pgaddr + cs->offset;
805
Miklos Szeredi334f4852005-09-09 13:10:27 -0700806 if (cs->write)
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200807 memcpy(buf, *val, ncpy);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700808 else
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200809 memcpy(*val, buf, ncpy);
810
811 kunmap_atomic(pgaddr);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700812 *val += ncpy;
813 }
814 *size -= ncpy;
815 cs->len -= ncpy;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200816 cs->offset += ncpy;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700817 return ncpy;
818}
819
Miklos Szeredice534fb2010-05-25 15:06:07 +0200820static int fuse_check_page(struct page *page)
821{
822 if (page_mapcount(page) ||
823 page->mapping != NULL ||
824 page_count(page) != 1 ||
825 (page->flags & PAGE_FLAGS_CHECK_AT_PREP &
826 ~(1 << PG_locked |
827 1 << PG_referenced |
828 1 << PG_uptodate |
829 1 << PG_lru |
830 1 << PG_active |
831 1 << PG_reclaim))) {
832 printk(KERN_WARNING "fuse: trying to steal weird page\n");
833 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);
834 return 1;
835 }
836 return 0;
837}
838
839static int fuse_try_move_page(struct fuse_copy_state *cs, struct page **pagep)
840{
841 int err;
842 struct page *oldpage = *pagep;
843 struct page *newpage;
844 struct pipe_buffer *buf = cs->pipebufs;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200845
Miklos Szeredidc008092015-07-01 16:25:58 +0200846 err = unlock_request(cs->req);
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200847 if (err)
848 return err;
849
Miklos Szeredice534fb2010-05-25 15:06:07 +0200850 fuse_copy_finish(cs);
851
Miklos Szeredifba597d2016-09-27 10:45:12 +0200852 err = pipe_buf_confirm(cs->pipe, buf);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200853 if (err)
854 return err;
855
856 BUG_ON(!cs->nr_segs);
857 cs->currbuf = buf;
858 cs->len = buf->len;
859 cs->pipebufs++;
860 cs->nr_segs--;
861
862 if (cs->len != PAGE_SIZE)
863 goto out_fallback;
864
Miklos Szeredica76f5b2016-09-27 10:45:12 +0200865 if (pipe_buf_steal(cs->pipe, buf) != 0)
Miklos Szeredice534fb2010-05-25 15:06:07 +0200866 goto out_fallback;
867
868 newpage = buf->page;
869
Miklos Szerediaa991b32015-02-26 11:45:47 +0100870 if (!PageUptodate(newpage))
871 SetPageUptodate(newpage);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200872
873 ClearPageMappedToDisk(newpage);
874
875 if (fuse_check_page(newpage) != 0)
876 goto out_fallback_unlock;
877
Miklos Szeredice534fb2010-05-25 15:06:07 +0200878 /*
879 * This is a new and locked page, it shouldn't be mapped or
880 * have any special flags on it
881 */
882 if (WARN_ON(page_mapped(oldpage)))
883 goto out_fallback_unlock;
884 if (WARN_ON(page_has_private(oldpage)))
885 goto out_fallback_unlock;
886 if (WARN_ON(PageDirty(oldpage) || PageWriteback(oldpage)))
887 goto out_fallback_unlock;
888 if (WARN_ON(PageMlocked(oldpage)))
889 goto out_fallback_unlock;
890
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700891 err = replace_page_cache_page(oldpage, newpage, GFP_KERNEL);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200892 if (err) {
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700893 unlock_page(newpage);
894 return err;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200895 }
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700896
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300897 get_page(newpage);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200898
899 if (!(buf->flags & PIPE_BUF_FLAG_LRU))
900 lru_cache_add_file(newpage);
901
902 err = 0;
Miklos Szeredidc008092015-07-01 16:25:58 +0200903 spin_lock(&cs->req->waitq.lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200904 if (test_bit(FR_ABORTED, &cs->req->flags))
Miklos Szeredice534fb2010-05-25 15:06:07 +0200905 err = -ENOENT;
906 else
907 *pagep = newpage;
Miklos Szeredidc008092015-07-01 16:25:58 +0200908 spin_unlock(&cs->req->waitq.lock);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200909
910 if (err) {
911 unlock_page(newpage);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300912 put_page(newpage);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200913 return err;
914 }
915
916 unlock_page(oldpage);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300917 put_page(oldpage);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200918 cs->len = 0;
919
920 return 0;
921
922out_fallback_unlock:
923 unlock_page(newpage);
924out_fallback:
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200925 cs->pg = buf->page;
926 cs->offset = buf->offset;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200927
Miklos Szeredidc008092015-07-01 16:25:58 +0200928 err = lock_request(cs->req);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200929 if (err)
930 return err;
931
932 return 1;
933}
934
Miklos Szeredic3021622010-05-25 15:06:07 +0200935static int fuse_ref_page(struct fuse_copy_state *cs, struct page *page,
936 unsigned offset, unsigned count)
937{
938 struct pipe_buffer *buf;
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200939 int err;
Miklos Szeredic3021622010-05-25 15:06:07 +0200940
941 if (cs->nr_segs == cs->pipe->buffers)
942 return -EIO;
943
Miklos Szeredidc008092015-07-01 16:25:58 +0200944 err = unlock_request(cs->req);
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200945 if (err)
946 return err;
947
Miklos Szeredic3021622010-05-25 15:06:07 +0200948 fuse_copy_finish(cs);
949
950 buf = cs->pipebufs;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300951 get_page(page);
Miklos Szeredic3021622010-05-25 15:06:07 +0200952 buf->page = page;
953 buf->offset = offset;
954 buf->len = count;
955
956 cs->pipebufs++;
957 cs->nr_segs++;
958 cs->len = 0;
959
960 return 0;
961}
962
Miklos Szeredi334f4852005-09-09 13:10:27 -0700963/*
964 * Copy a page in the request to/from the userspace buffer. Must be
965 * done atomically
966 */
Miklos Szeredice534fb2010-05-25 15:06:07 +0200967static int fuse_copy_page(struct fuse_copy_state *cs, struct page **pagep,
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800968 unsigned offset, unsigned count, int zeroing)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700969{
Miklos Szeredice534fb2010-05-25 15:06:07 +0200970 int err;
971 struct page *page = *pagep;
972
Miklos Szeredib6777c42010-10-26 14:22:27 -0700973 if (page && zeroing && count < PAGE_SIZE)
974 clear_highpage(page);
975
Miklos Szeredi334f4852005-09-09 13:10:27 -0700976 while (count) {
Miklos Szeredic3021622010-05-25 15:06:07 +0200977 if (cs->write && cs->pipebufs && page) {
978 return fuse_ref_page(cs, page, offset, count);
979 } else if (!cs->len) {
Miklos Szeredice534fb2010-05-25 15:06:07 +0200980 if (cs->move_pages && page &&
981 offset == 0 && count == PAGE_SIZE) {
982 err = fuse_try_move_page(cs, pagep);
983 if (err <= 0)
984 return err;
985 } else {
986 err = fuse_copy_fill(cs);
987 if (err)
988 return err;
989 }
Miklos Szeredi1729a162008-11-26 12:03:54 +0100990 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700991 if (page) {
Cong Wang2408f6e2011-11-25 23:14:30 +0800992 void *mapaddr = kmap_atomic(page);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700993 void *buf = mapaddr + offset;
994 offset += fuse_copy_do(cs, &buf, &count);
Cong Wang2408f6e2011-11-25 23:14:30 +0800995 kunmap_atomic(mapaddr);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700996 } else
997 offset += fuse_copy_do(cs, NULL, &count);
998 }
999 if (page && !cs->write)
1000 flush_dcache_page(page);
1001 return 0;
1002}
1003
1004/* Copy pages in the request to/from userspace buffer */
1005static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
1006 int zeroing)
1007{
1008 unsigned i;
1009 struct fuse_req *req = cs->req;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001010
1011 for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
Miklos Szeredice534fb2010-05-25 15:06:07 +02001012 int err;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001013 unsigned offset = req->page_descs[i].offset;
1014 unsigned count = min(nbytes, req->page_descs[i].length);
Miklos Szeredice534fb2010-05-25 15:06:07 +02001015
1016 err = fuse_copy_page(cs, &req->pages[i], offset, count,
1017 zeroing);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001018 if (err)
1019 return err;
1020
1021 nbytes -= count;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001022 }
1023 return 0;
1024}
1025
1026/* Copy a single argument in the request to/from userspace buffer */
1027static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
1028{
1029 while (size) {
Miklos Szeredi1729a162008-11-26 12:03:54 +01001030 if (!cs->len) {
1031 int err = fuse_copy_fill(cs);
1032 if (err)
1033 return err;
1034 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001035 fuse_copy_do(cs, &val, &size);
1036 }
1037 return 0;
1038}
1039
1040/* Copy request arguments to/from userspace buffer */
1041static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
1042 unsigned argpages, struct fuse_arg *args,
1043 int zeroing)
1044{
1045 int err = 0;
1046 unsigned i;
1047
1048 for (i = 0; !err && i < numargs; i++) {
1049 struct fuse_arg *arg = &args[i];
1050 if (i == numargs - 1 && argpages)
1051 err = fuse_copy_pages(cs, arg->size, zeroing);
1052 else
1053 err = fuse_copy_one(cs, arg->value, arg->size);
1054 }
1055 return err;
1056}
1057
Miklos Szeredif88996a2015-07-01 16:26:01 +02001058static int forget_pending(struct fuse_iqueue *fiq)
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001059{
Miklos Szeredif88996a2015-07-01 16:26:01 +02001060 return fiq->forget_list_head.next != NULL;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001061}
1062
Miklos Szeredif88996a2015-07-01 16:26:01 +02001063static int request_pending(struct fuse_iqueue *fiq)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001064{
Miklos Szeredif88996a2015-07-01 16:26:01 +02001065 return !list_empty(&fiq->pending) || !list_empty(&fiq->interrupts) ||
1066 forget_pending(fiq);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001067}
1068
Miklos Szeredi334f4852005-09-09 13:10:27 -07001069/*
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001070 * Transfer an interrupt request to userspace
1071 *
1072 * Unlike other requests this is assembled on demand, without a need
1073 * to allocate a separate fuse_req structure.
1074 *
Miklos Szeredifd22d622015-07-01 16:26:03 +02001075 * Called with fiq->waitq.lock held, releases it
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001076 */
Miklos Szeredifd22d622015-07-01 16:26:03 +02001077static int fuse_read_interrupt(struct fuse_iqueue *fiq,
1078 struct fuse_copy_state *cs,
Miklos Szeredic3021622010-05-25 15:06:07 +02001079 size_t nbytes, struct fuse_req *req)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001080__releases(fiq->waitq.lock)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001081{
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001082 struct fuse_in_header ih;
1083 struct fuse_interrupt_in arg;
1084 unsigned reqsize = sizeof(ih) + sizeof(arg);
1085 int err;
1086
1087 list_del_init(&req->intr_entry);
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001088 req->intr_unique = fuse_get_unique(fiq);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001089 memset(&ih, 0, sizeof(ih));
1090 memset(&arg, 0, sizeof(arg));
1091 ih.len = reqsize;
1092 ih.opcode = FUSE_INTERRUPT;
1093 ih.unique = req->intr_unique;
1094 arg.unique = req->in.h.unique;
1095
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001096 spin_unlock(&fiq->waitq.lock);
Miklos Szeredic3021622010-05-25 15:06:07 +02001097 if (nbytes < reqsize)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001098 return -EINVAL;
1099
Miklos Szeredic3021622010-05-25 15:06:07 +02001100 err = fuse_copy_one(cs, &ih, sizeof(ih));
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001101 if (!err)
Miklos Szeredic3021622010-05-25 15:06:07 +02001102 err = fuse_copy_one(cs, &arg, sizeof(arg));
1103 fuse_copy_finish(cs);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001104
1105 return err ? err : reqsize;
1106}
1107
Miklos Szeredif88996a2015-07-01 16:26:01 +02001108static struct fuse_forget_link *dequeue_forget(struct fuse_iqueue *fiq,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001109 unsigned max,
1110 unsigned *countp)
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001111{
Miklos Szeredif88996a2015-07-01 16:26:01 +02001112 struct fuse_forget_link *head = fiq->forget_list_head.next;
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001113 struct fuse_forget_link **newhead = &head;
1114 unsigned count;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001115
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001116 for (count = 0; *newhead != NULL && count < max; count++)
1117 newhead = &(*newhead)->next;
1118
Miklos Szeredif88996a2015-07-01 16:26:01 +02001119 fiq->forget_list_head.next = *newhead;
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001120 *newhead = NULL;
Miklos Szeredif88996a2015-07-01 16:26:01 +02001121 if (fiq->forget_list_head.next == NULL)
1122 fiq->forget_list_tail = &fiq->forget_list_head;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001123
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001124 if (countp != NULL)
1125 *countp = count;
1126
1127 return head;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001128}
1129
Miklos Szeredifd22d622015-07-01 16:26:03 +02001130static int fuse_read_single_forget(struct fuse_iqueue *fiq,
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001131 struct fuse_copy_state *cs,
1132 size_t nbytes)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001133__releases(fiq->waitq.lock)
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001134{
1135 int err;
Miklos Szeredif88996a2015-07-01 16:26:01 +02001136 struct fuse_forget_link *forget = dequeue_forget(fiq, 1, NULL);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001137 struct fuse_forget_in arg = {
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001138 .nlookup = forget->forget_one.nlookup,
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001139 };
1140 struct fuse_in_header ih = {
1141 .opcode = FUSE_FORGET,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001142 .nodeid = forget->forget_one.nodeid,
Miklos Szeredif88996a2015-07-01 16:26:01 +02001143 .unique = fuse_get_unique(fiq),
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001144 .len = sizeof(ih) + sizeof(arg),
1145 };
1146
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001147 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001148 kfree(forget);
1149 if (nbytes < ih.len)
1150 return -EINVAL;
1151
1152 err = fuse_copy_one(cs, &ih, sizeof(ih));
1153 if (!err)
1154 err = fuse_copy_one(cs, &arg, sizeof(arg));
1155 fuse_copy_finish(cs);
1156
1157 if (err)
1158 return err;
1159
1160 return ih.len;
1161}
1162
Miklos Szeredifd22d622015-07-01 16:26:03 +02001163static int fuse_read_batch_forget(struct fuse_iqueue *fiq,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001164 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001165__releases(fiq->waitq.lock)
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001166{
1167 int err;
1168 unsigned max_forgets;
1169 unsigned count;
1170 struct fuse_forget_link *head;
1171 struct fuse_batch_forget_in arg = { .count = 0 };
1172 struct fuse_in_header ih = {
1173 .opcode = FUSE_BATCH_FORGET,
Miklos Szeredif88996a2015-07-01 16:26:01 +02001174 .unique = fuse_get_unique(fiq),
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001175 .len = sizeof(ih) + sizeof(arg),
1176 };
1177
1178 if (nbytes < ih.len) {
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001179 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001180 return -EINVAL;
1181 }
1182
1183 max_forgets = (nbytes - ih.len) / sizeof(struct fuse_forget_one);
Miklos Szeredif88996a2015-07-01 16:26:01 +02001184 head = dequeue_forget(fiq, max_forgets, &count);
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001185 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001186
1187 arg.count = count;
1188 ih.len += count * sizeof(struct fuse_forget_one);
1189 err = fuse_copy_one(cs, &ih, sizeof(ih));
1190 if (!err)
1191 err = fuse_copy_one(cs, &arg, sizeof(arg));
1192
1193 while (head) {
1194 struct fuse_forget_link *forget = head;
1195
1196 if (!err) {
1197 err = fuse_copy_one(cs, &forget->forget_one,
1198 sizeof(forget->forget_one));
1199 }
1200 head = forget->next;
1201 kfree(forget);
1202 }
1203
1204 fuse_copy_finish(cs);
1205
1206 if (err)
1207 return err;
1208
1209 return ih.len;
1210}
1211
Miklos Szeredifd22d622015-07-01 16:26:03 +02001212static int fuse_read_forget(struct fuse_conn *fc, struct fuse_iqueue *fiq,
1213 struct fuse_copy_state *cs,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001214 size_t nbytes)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001215__releases(fiq->waitq.lock)
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001216{
Miklos Szeredif88996a2015-07-01 16:26:01 +02001217 if (fc->minor < 16 || fiq->forget_list_head.next->next == NULL)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001218 return fuse_read_single_forget(fiq, cs, nbytes);
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001219 else
Miklos Szeredifd22d622015-07-01 16:26:03 +02001220 return fuse_read_batch_forget(fiq, cs, nbytes);
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001221}
1222
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001223/*
Miklos Szeredi334f4852005-09-09 13:10:27 -07001224 * Read a single request into the userspace filesystem's buffer. This
1225 * function waits until a request is available, then removes it from
1226 * the pending list and copies request data to userspace buffer. If
Miklos Szeredif9a28422006-06-25 05:48:53 -07001227 * no reply is needed (FORGET) or request has been aborted or there
1228 * was an error during the copying then it's finished by calling
Miklos Szeredi334f4852005-09-09 13:10:27 -07001229 * request_end(). Otherwise add it to the processing list, and set
1230 * the 'sent' flag.
1231 */
Miklos Szeredic36960462015-07-01 16:26:09 +02001232static ssize_t fuse_dev_do_read(struct fuse_dev *fud, struct file *file,
Miklos Szeredic3021622010-05-25 15:06:07 +02001233 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001234{
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001235 ssize_t err;
Miklos Szeredic36960462015-07-01 16:26:09 +02001236 struct fuse_conn *fc = fud->fc;
Miklos Szeredif88996a2015-07-01 16:26:01 +02001237 struct fuse_iqueue *fiq = &fc->iq;
Miklos Szeredic36960462015-07-01 16:26:09 +02001238 struct fuse_pqueue *fpq = &fud->pq;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001239 struct fuse_req *req;
1240 struct fuse_in *in;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001241 unsigned reqsize;
1242
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001243 restart:
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001244 spin_lock(&fiq->waitq.lock);
Jeff Dikee5ac1d12006-04-10 22:54:53 -07001245 err = -EAGAIN;
Miklos Szeredie16714d2015-07-01 16:26:01 +02001246 if ((file->f_flags & O_NONBLOCK) && fiq->connected &&
Miklos Szeredif88996a2015-07-01 16:26:01 +02001247 !request_pending(fiq))
Jeff Dikee5ac1d12006-04-10 22:54:53 -07001248 goto err_unlock;
1249
Miklos Szeredi52509212015-07-01 16:26:03 +02001250 err = wait_event_interruptible_exclusive_locked(fiq->waitq,
1251 !fiq->connected || request_pending(fiq));
1252 if (err)
1253 goto err_unlock;
1254
Miklos Szeredi334f4852005-09-09 13:10:27 -07001255 err = -ENODEV;
Miklos Szeredie16714d2015-07-01 16:26:01 +02001256 if (!fiq->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001257 goto err_unlock;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001258
Miklos Szeredif88996a2015-07-01 16:26:01 +02001259 if (!list_empty(&fiq->interrupts)) {
1260 req = list_entry(fiq->interrupts.next, struct fuse_req,
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001261 intr_entry);
Miklos Szeredifd22d622015-07-01 16:26:03 +02001262 return fuse_read_interrupt(fiq, cs, nbytes, req);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001263 }
1264
Miklos Szeredif88996a2015-07-01 16:26:01 +02001265 if (forget_pending(fiq)) {
1266 if (list_empty(&fiq->pending) || fiq->forget_batch-- > 0)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001267 return fuse_read_forget(fc, fiq, cs, nbytes);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001268
Miklos Szeredif88996a2015-07-01 16:26:01 +02001269 if (fiq->forget_batch <= -8)
1270 fiq->forget_batch = 16;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001271 }
1272
Miklos Szeredif88996a2015-07-01 16:26:01 +02001273 req = list_entry(fiq->pending.next, struct fuse_req, list);
Miklos Szeredi33e14b42015-07-01 16:26:01 +02001274 clear_bit(FR_PENDING, &req->flags);
Miklos Szeredief759252015-07-01 16:26:02 +02001275 list_del_init(&req->list);
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001276 spin_unlock(&fiq->waitq.lock);
1277
Miklos Szeredi334f4852005-09-09 13:10:27 -07001278 in = &req->in;
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001279 reqsize = in->h.len;
1280 /* If request is too large, reply with an error and restart the read */
Miklos Szeredic3021622010-05-25 15:06:07 +02001281 if (nbytes < reqsize) {
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001282 req->out.h.error = -EIO;
1283 /* SETXATTR is special, since it may contain too large data */
1284 if (in->h.opcode == FUSE_SETXATTR)
1285 req->out.h.error = -E2BIG;
1286 request_end(fc, req);
1287 goto restart;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001288 }
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001289 spin_lock(&fpq->lock);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001290 list_add(&req->list, &fpq->io);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001291 spin_unlock(&fpq->lock);
Miklos Szeredic3021622010-05-25 15:06:07 +02001292 cs->req = req;
1293 err = fuse_copy_one(cs, &in->h, sizeof(in->h));
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001294 if (!err)
Miklos Szeredic3021622010-05-25 15:06:07 +02001295 err = fuse_copy_args(cs, in->numargs, in->argpages,
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001296 (struct fuse_arg *) in->args, 0);
Miklos Szeredic3021622010-05-25 15:06:07 +02001297 fuse_copy_finish(cs);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001298 spin_lock(&fpq->lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001299 clear_bit(FR_LOCKED, &req->flags);
Miklos Szeredie96edd92015-07-01 16:26:04 +02001300 if (!fpq->connected) {
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001301 err = -ENODEV;
1302 goto out_end;
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -07001303 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001304 if (err) {
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -07001305 req->out.h.error = -EIO;
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001306 goto out_end;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001307 }
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001308 if (!test_bit(FR_ISREPLY, &req->flags)) {
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001309 err = reqsize;
1310 goto out_end;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001311 }
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001312 list_move_tail(&req->list, &fpq->processing);
Kirill Tkhai7996b1c2018-09-25 12:28:55 +03001313 __fuse_get_request(req);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001314 set_bit(FR_SENT, &req->flags);
Miklos Szeredic31ccf12018-09-28 16:43:22 +02001315 spin_unlock(&fpq->lock);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001316 /* matches barrier in request_wait_answer() */
1317 smp_mb__after_atomic();
1318 if (test_bit(FR_INTERRUPTED, &req->flags))
1319 queue_interrupt(fiq, req);
Kirill Tkhai7996b1c2018-09-25 12:28:55 +03001320 fuse_put_request(fc, req);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001321
Miklos Szeredi334f4852005-09-09 13:10:27 -07001322 return reqsize;
1323
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001324out_end:
Miklos Szeredi77cd9d42015-07-01 16:26:06 +02001325 if (!test_bit(FR_PRIVATE, &req->flags))
1326 list_del_init(&req->list);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001327 spin_unlock(&fpq->lock);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001328 request_end(fc, req);
1329 return err;
1330
Miklos Szeredi334f4852005-09-09 13:10:27 -07001331 err_unlock:
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001332 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001333 return err;
1334}
1335
Tom Van Braeckel94e4fe22015-01-12 05:22:16 +01001336static int fuse_dev_open(struct inode *inode, struct file *file)
1337{
1338 /*
1339 * The fuse device's file's private_data is used to hold
1340 * the fuse_conn(ection) when it is mounted, and is used to
1341 * keep track of whether the file has been mounted already.
1342 */
1343 file->private_data = NULL;
1344 return 0;
1345}
1346
Al Virofbdbacc2015-04-03 21:53:39 -04001347static ssize_t fuse_dev_read(struct kiocb *iocb, struct iov_iter *to)
Miklos Szeredic3021622010-05-25 15:06:07 +02001348{
1349 struct fuse_copy_state cs;
1350 struct file *file = iocb->ki_filp;
Miklos Szeredicc080e92015-07-01 16:26:08 +02001351 struct fuse_dev *fud = fuse_get_dev(file);
1352
1353 if (!fud)
Miklos Szeredic3021622010-05-25 15:06:07 +02001354 return -EPERM;
1355
Al Virofbdbacc2015-04-03 21:53:39 -04001356 if (!iter_is_iovec(to))
1357 return -EINVAL;
Miklos Szeredic3021622010-05-25 15:06:07 +02001358
Miklos Szeredidc008092015-07-01 16:25:58 +02001359 fuse_copy_init(&cs, 1, to);
Al Virofbdbacc2015-04-03 21:53:39 -04001360
Miklos Szeredic36960462015-07-01 16:26:09 +02001361 return fuse_dev_do_read(fud, file, &cs, iov_iter_count(to));
Miklos Szeredic3021622010-05-25 15:06:07 +02001362}
1363
Miklos Szeredic3021622010-05-25 15:06:07 +02001364static ssize_t fuse_dev_splice_read(struct file *in, loff_t *ppos,
1365 struct pipe_inode_info *pipe,
1366 size_t len, unsigned int flags)
1367{
Al Virod82718e2016-09-17 22:56:25 -04001368 int total, ret;
Miklos Szeredic3021622010-05-25 15:06:07 +02001369 int page_nr = 0;
Miklos Szeredic3021622010-05-25 15:06:07 +02001370 struct pipe_buffer *bufs;
1371 struct fuse_copy_state cs;
Miklos Szeredicc080e92015-07-01 16:26:08 +02001372 struct fuse_dev *fud = fuse_get_dev(in);
1373
1374 if (!fud)
Miklos Szeredic3021622010-05-25 15:06:07 +02001375 return -EPERM;
1376
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001377 bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), GFP_KERNEL);
Miklos Szeredic3021622010-05-25 15:06:07 +02001378 if (!bufs)
1379 return -ENOMEM;
1380
Miklos Szeredidc008092015-07-01 16:25:58 +02001381 fuse_copy_init(&cs, 1, NULL);
Miklos Szeredic3021622010-05-25 15:06:07 +02001382 cs.pipebufs = bufs;
1383 cs.pipe = pipe;
Miklos Szeredic36960462015-07-01 16:26:09 +02001384 ret = fuse_dev_do_read(fud, in, &cs, len);
Miklos Szeredic3021622010-05-25 15:06:07 +02001385 if (ret < 0)
1386 goto out;
1387
Miklos Szeredic3021622010-05-25 15:06:07 +02001388 if (pipe->nrbufs + cs.nr_segs > pipe->buffers) {
1389 ret = -EIO;
Al Virod82718e2016-09-17 22:56:25 -04001390 goto out;
Miklos Szeredic3021622010-05-25 15:06:07 +02001391 }
1392
Al Virod82718e2016-09-17 22:56:25 -04001393 for (ret = total = 0; page_nr < cs.nr_segs; total += ret) {
Miklos Szeredi28a625c2014-01-22 19:36:57 +01001394 /*
1395 * Need to be careful about this. Having buf->ops in module
1396 * code can Oops if the buffer persists after module unload.
1397 */
Al Virod82718e2016-09-17 22:56:25 -04001398 bufs[page_nr].ops = &nosteal_pipe_buf_ops;
Miklos Szeredi80a04772017-02-16 15:08:20 +01001399 bufs[page_nr].flags = 0;
Al Virod82718e2016-09-17 22:56:25 -04001400 ret = add_to_pipe(pipe, &bufs[page_nr++]);
1401 if (unlikely(ret < 0))
1402 break;
Miklos Szeredic3021622010-05-25 15:06:07 +02001403 }
Al Virod82718e2016-09-17 22:56:25 -04001404 if (total)
1405 ret = total;
Miklos Szeredic3021622010-05-25 15:06:07 +02001406out:
1407 for (; page_nr < cs.nr_segs; page_nr++)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001408 put_page(bufs[page_nr].page);
Miklos Szeredic3021622010-05-25 15:06:07 +02001409
1410 kfree(bufs);
1411 return ret;
1412}
1413
Tejun Heo95668a62008-11-26 12:03:55 +01001414static int fuse_notify_poll(struct fuse_conn *fc, unsigned int size,
1415 struct fuse_copy_state *cs)
1416{
1417 struct fuse_notify_poll_wakeup_out outarg;
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001418 int err = -EINVAL;
Tejun Heo95668a62008-11-26 12:03:55 +01001419
1420 if (size != sizeof(outarg))
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001421 goto err;
Tejun Heo95668a62008-11-26 12:03:55 +01001422
1423 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1424 if (err)
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001425 goto err;
Tejun Heo95668a62008-11-26 12:03:55 +01001426
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001427 fuse_copy_finish(cs);
Tejun Heo95668a62008-11-26 12:03:55 +01001428 return fuse_notify_poll_wakeup(fc, &outarg);
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001429
1430err:
1431 fuse_copy_finish(cs);
1432 return err;
Tejun Heo95668a62008-11-26 12:03:55 +01001433}
1434
John Muir3b463ae2009-05-31 11:13:57 -04001435static int fuse_notify_inval_inode(struct fuse_conn *fc, unsigned int size,
1436 struct fuse_copy_state *cs)
1437{
1438 struct fuse_notify_inval_inode_out outarg;
1439 int err = -EINVAL;
1440
1441 if (size != sizeof(outarg))
1442 goto err;
1443
1444 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1445 if (err)
1446 goto err;
1447 fuse_copy_finish(cs);
1448
1449 down_read(&fc->killsb);
1450 err = -ENOENT;
Miklos Szeredib21dda42010-02-05 12:08:31 +01001451 if (fc->sb) {
1452 err = fuse_reverse_inval_inode(fc->sb, outarg.ino,
1453 outarg.off, outarg.len);
1454 }
John Muir3b463ae2009-05-31 11:13:57 -04001455 up_read(&fc->killsb);
1456 return err;
1457
1458err:
1459 fuse_copy_finish(cs);
1460 return err;
1461}
1462
1463static int fuse_notify_inval_entry(struct fuse_conn *fc, unsigned int size,
1464 struct fuse_copy_state *cs)
1465{
1466 struct fuse_notify_inval_entry_out outarg;
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001467 int err = -ENOMEM;
1468 char *buf;
John Muir3b463ae2009-05-31 11:13:57 -04001469 struct qstr name;
1470
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001471 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1472 if (!buf)
1473 goto err;
1474
1475 err = -EINVAL;
John Muir3b463ae2009-05-31 11:13:57 -04001476 if (size < sizeof(outarg))
1477 goto err;
1478
1479 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1480 if (err)
1481 goto err;
1482
1483 err = -ENAMETOOLONG;
1484 if (outarg.namelen > FUSE_NAME_MAX)
1485 goto err;
1486
Miklos Szeredic2183d12011-08-24 10:20:17 +02001487 err = -EINVAL;
1488 if (size != sizeof(outarg) + outarg.namelen + 1)
1489 goto err;
1490
John Muir3b463ae2009-05-31 11:13:57 -04001491 name.name = buf;
1492 name.len = outarg.namelen;
1493 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1494 if (err)
1495 goto err;
1496 fuse_copy_finish(cs);
1497 buf[outarg.namelen] = 0;
John Muir3b463ae2009-05-31 11:13:57 -04001498
1499 down_read(&fc->killsb);
1500 err = -ENOENT;
Miklos Szeredib21dda42010-02-05 12:08:31 +01001501 if (fc->sb)
John Muir451d0f52011-12-06 21:50:06 +01001502 err = fuse_reverse_inval_entry(fc->sb, outarg.parent, 0, &name);
1503 up_read(&fc->killsb);
1504 kfree(buf);
1505 return err;
1506
1507err:
1508 kfree(buf);
1509 fuse_copy_finish(cs);
1510 return err;
1511}
1512
1513static int fuse_notify_delete(struct fuse_conn *fc, unsigned int size,
1514 struct fuse_copy_state *cs)
1515{
1516 struct fuse_notify_delete_out outarg;
1517 int err = -ENOMEM;
1518 char *buf;
1519 struct qstr name;
1520
1521 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1522 if (!buf)
1523 goto err;
1524
1525 err = -EINVAL;
1526 if (size < sizeof(outarg))
1527 goto err;
1528
1529 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1530 if (err)
1531 goto err;
1532
1533 err = -ENAMETOOLONG;
1534 if (outarg.namelen > FUSE_NAME_MAX)
1535 goto err;
1536
1537 err = -EINVAL;
1538 if (size != sizeof(outarg) + outarg.namelen + 1)
1539 goto err;
1540
1541 name.name = buf;
1542 name.len = outarg.namelen;
1543 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1544 if (err)
1545 goto err;
1546 fuse_copy_finish(cs);
1547 buf[outarg.namelen] = 0;
John Muir451d0f52011-12-06 21:50:06 +01001548
1549 down_read(&fc->killsb);
1550 err = -ENOENT;
1551 if (fc->sb)
1552 err = fuse_reverse_inval_entry(fc->sb, outarg.parent,
1553 outarg.child, &name);
John Muir3b463ae2009-05-31 11:13:57 -04001554 up_read(&fc->killsb);
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001555 kfree(buf);
John Muir3b463ae2009-05-31 11:13:57 -04001556 return err;
1557
1558err:
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001559 kfree(buf);
John Muir3b463ae2009-05-31 11:13:57 -04001560 fuse_copy_finish(cs);
1561 return err;
1562}
1563
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001564static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
1565 struct fuse_copy_state *cs)
1566{
1567 struct fuse_notify_store_out outarg;
1568 struct inode *inode;
1569 struct address_space *mapping;
1570 u64 nodeid;
1571 int err;
1572 pgoff_t index;
1573 unsigned int offset;
1574 unsigned int num;
1575 loff_t file_size;
1576 loff_t end;
1577
1578 err = -EINVAL;
1579 if (size < sizeof(outarg))
1580 goto out_finish;
1581
1582 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1583 if (err)
1584 goto out_finish;
1585
1586 err = -EINVAL;
1587 if (size - sizeof(outarg) != outarg.size)
1588 goto out_finish;
1589
1590 nodeid = outarg.nodeid;
1591
1592 down_read(&fc->killsb);
1593
1594 err = -ENOENT;
1595 if (!fc->sb)
1596 goto out_up_killsb;
1597
1598 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1599 if (!inode)
1600 goto out_up_killsb;
1601
1602 mapping = inode->i_mapping;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001603 index = outarg.offset >> PAGE_SHIFT;
1604 offset = outarg.offset & ~PAGE_MASK;
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001605 file_size = i_size_read(inode);
1606 end = outarg.offset + outarg.size;
1607 if (end > file_size) {
1608 file_size = end;
1609 fuse_write_update_size(inode, file_size);
1610 }
1611
1612 num = outarg.size;
1613 while (num) {
1614 struct page *page;
1615 unsigned int this_num;
1616
1617 err = -ENOMEM;
1618 page = find_or_create_page(mapping, index,
1619 mapping_gfp_mask(mapping));
1620 if (!page)
1621 goto out_iput;
1622
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001623 this_num = min_t(unsigned, num, PAGE_SIZE - offset);
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001624 err = fuse_copy_page(cs, &page, offset, this_num, 0);
Miklos Szeredi063ec1e2014-01-22 19:36:58 +01001625 if (!err && offset == 0 &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001626 (this_num == PAGE_SIZE || file_size == end))
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001627 SetPageUptodate(page);
1628 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001629 put_page(page);
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001630
1631 if (err)
1632 goto out_iput;
1633
1634 num -= this_num;
1635 offset = 0;
1636 index++;
1637 }
1638
1639 err = 0;
1640
1641out_iput:
1642 iput(inode);
1643out_up_killsb:
1644 up_read(&fc->killsb);
1645out_finish:
1646 fuse_copy_finish(cs);
1647 return err;
1648}
1649
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001650static void fuse_retrieve_end(struct fuse_conn *fc, struct fuse_req *req)
1651{
Mel Gormanb745bc82014-06-04 16:10:22 -07001652 release_pages(req->pages, req->num_pages, false);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001653}
1654
1655static int fuse_retrieve(struct fuse_conn *fc, struct inode *inode,
1656 struct fuse_notify_retrieve_out *outarg)
1657{
1658 int err;
1659 struct address_space *mapping = inode->i_mapping;
1660 struct fuse_req *req;
1661 pgoff_t index;
1662 loff_t file_size;
1663 unsigned int num;
1664 unsigned int offset;
Geert Uytterhoeven01574432010-09-30 22:06:21 +02001665 size_t total_len = 0;
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001666 int num_pages;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001667
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001668 offset = outarg->offset & ~PAGE_MASK;
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001669 file_size = i_size_read(inode);
1670
Kirill Smelkov4edf9072019-03-27 10:15:19 +00001671 num = min(outarg->size, fc->max_write);
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001672 if (outarg->offset > file_size)
1673 num = 0;
1674 else if (outarg->offset + num > file_size)
1675 num = file_size - outarg->offset;
1676
1677 num_pages = (num + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1678 num_pages = min(num_pages, FUSE_MAX_PAGES_PER_REQ);
1679
1680 req = fuse_get_req(fc, num_pages);
1681 if (IS_ERR(req))
1682 return PTR_ERR(req);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001683
1684 req->in.h.opcode = FUSE_NOTIFY_REPLY;
1685 req->in.h.nodeid = outarg->nodeid;
1686 req->in.numargs = 2;
1687 req->in.argpages = 1;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001688 req->end = fuse_retrieve_end;
1689
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001690 index = outarg->offset >> PAGE_SHIFT;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001691
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001692 while (num && req->num_pages < num_pages) {
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001693 struct page *page;
1694 unsigned int this_num;
1695
1696 page = find_get_page(mapping, index);
1697 if (!page)
1698 break;
1699
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001700 this_num = min_t(unsigned, num, PAGE_SIZE - offset);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001701 req->pages[req->num_pages] = page;
Miklos Szeredi64702de2019-01-16 10:27:59 +01001702 req->page_descs[req->num_pages].offset = offset;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001703 req->page_descs[req->num_pages].length = this_num;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001704 req->num_pages++;
1705
Miklos Szeredic9e67d42012-09-04 18:45:54 +02001706 offset = 0;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001707 num -= this_num;
1708 total_len += this_num;
Miklos Szeredi48706d02011-12-13 10:36:59 +01001709 index++;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001710 }
1711 req->misc.retrieve_in.offset = outarg->offset;
1712 req->misc.retrieve_in.size = total_len;
1713 req->in.args[0].size = sizeof(req->misc.retrieve_in);
1714 req->in.args[0].value = &req->misc.retrieve_in;
1715 req->in.args[1].size = total_len;
1716
1717 err = fuse_request_send_notify_reply(fc, req, outarg->notify_unique);
Miklos Szeredid180fee2018-11-09 15:52:16 +01001718 if (err) {
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001719 fuse_retrieve_end(fc, req);
Miklos Szeredid180fee2018-11-09 15:52:16 +01001720 fuse_put_request(fc, req);
1721 }
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001722
1723 return err;
1724}
1725
1726static int fuse_notify_retrieve(struct fuse_conn *fc, unsigned int size,
1727 struct fuse_copy_state *cs)
1728{
1729 struct fuse_notify_retrieve_out outarg;
1730 struct inode *inode;
1731 int err;
1732
1733 err = -EINVAL;
1734 if (size != sizeof(outarg))
1735 goto copy_finish;
1736
1737 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1738 if (err)
1739 goto copy_finish;
1740
1741 fuse_copy_finish(cs);
1742
1743 down_read(&fc->killsb);
1744 err = -ENOENT;
1745 if (fc->sb) {
1746 u64 nodeid = outarg.nodeid;
1747
1748 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1749 if (inode) {
1750 err = fuse_retrieve(fc, inode, &outarg);
1751 iput(inode);
1752 }
1753 }
1754 up_read(&fc->killsb);
1755
1756 return err;
1757
1758copy_finish:
1759 fuse_copy_finish(cs);
1760 return err;
1761}
1762
Tejun Heo85993962008-11-26 12:03:55 +01001763static int fuse_notify(struct fuse_conn *fc, enum fuse_notify_code code,
1764 unsigned int size, struct fuse_copy_state *cs)
1765{
Miklos Szeredi0d278362015-02-26 11:45:47 +01001766 /* Don't try to move pages (yet) */
1767 cs->move_pages = 0;
1768
Tejun Heo85993962008-11-26 12:03:55 +01001769 switch (code) {
Tejun Heo95668a62008-11-26 12:03:55 +01001770 case FUSE_NOTIFY_POLL:
1771 return fuse_notify_poll(fc, size, cs);
1772
John Muir3b463ae2009-05-31 11:13:57 -04001773 case FUSE_NOTIFY_INVAL_INODE:
1774 return fuse_notify_inval_inode(fc, size, cs);
1775
1776 case FUSE_NOTIFY_INVAL_ENTRY:
1777 return fuse_notify_inval_entry(fc, size, cs);
1778
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001779 case FUSE_NOTIFY_STORE:
1780 return fuse_notify_store(fc, size, cs);
1781
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001782 case FUSE_NOTIFY_RETRIEVE:
1783 return fuse_notify_retrieve(fc, size, cs);
1784
John Muir451d0f52011-12-06 21:50:06 +01001785 case FUSE_NOTIFY_DELETE:
1786 return fuse_notify_delete(fc, size, cs);
1787
Tejun Heo85993962008-11-26 12:03:55 +01001788 default:
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001789 fuse_copy_finish(cs);
Tejun Heo85993962008-11-26 12:03:55 +01001790 return -EINVAL;
1791 }
1792}
1793
Miklos Szeredi334f4852005-09-09 13:10:27 -07001794/* Look up request on processing list by unique ID */
Miklos Szeredi3a2b5b92015-07-01 16:26:04 +02001795static struct fuse_req *request_find(struct fuse_pqueue *fpq, u64 unique)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001796{
Dong Fang05726ac2013-07-30 22:50:01 -04001797 struct fuse_req *req;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001798
Miklos Szeredi3a2b5b92015-07-01 16:26:04 +02001799 list_for_each_entry(req, &fpq->processing, list) {
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001800 if (req->in.h.unique == unique || req->intr_unique == unique)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001801 return req;
1802 }
1803 return NULL;
1804}
1805
1806static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
1807 unsigned nbytes)
1808{
1809 unsigned reqsize = sizeof(struct fuse_out_header);
1810
1811 if (out->h.error)
1812 return nbytes != reqsize ? -EINVAL : 0;
1813
1814 reqsize += len_args(out->numargs, out->args);
1815
1816 if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
1817 return -EINVAL;
1818 else if (reqsize > nbytes) {
1819 struct fuse_arg *lastarg = &out->args[out->numargs-1];
1820 unsigned diffsize = reqsize - nbytes;
1821 if (diffsize > lastarg->size)
1822 return -EINVAL;
1823 lastarg->size -= diffsize;
1824 }
1825 return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
1826 out->page_zeroing);
1827}
1828
1829/*
1830 * Write a single reply to a request. First the header is copied from
1831 * the write buffer. The request is then searched on the processing
1832 * list by the unique ID found in the header. If found, then remove
1833 * it from the list and copy the rest of the buffer to the request.
1834 * The request is finished by calling request_end()
1835 */
Miklos Szeredic36960462015-07-01 16:26:09 +02001836static ssize_t fuse_dev_do_write(struct fuse_dev *fud,
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001837 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001838{
1839 int err;
Miklos Szeredic36960462015-07-01 16:26:09 +02001840 struct fuse_conn *fc = fud->fc;
1841 struct fuse_pqueue *fpq = &fud->pq;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001842 struct fuse_req *req;
1843 struct fuse_out_header oh;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001844
Miklos Szeredi334f4852005-09-09 13:10:27 -07001845 if (nbytes < sizeof(struct fuse_out_header))
1846 return -EINVAL;
1847
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001848 err = fuse_copy_one(cs, &oh, sizeof(oh));
Miklos Szeredi334f4852005-09-09 13:10:27 -07001849 if (err)
1850 goto err_finish;
Tejun Heo85993962008-11-26 12:03:55 +01001851
Miklos Szeredi334f4852005-09-09 13:10:27 -07001852 err = -EINVAL;
Tejun Heo85993962008-11-26 12:03:55 +01001853 if (oh.len != nbytes)
1854 goto err_finish;
1855
1856 /*
1857 * Zero oh.unique indicates unsolicited notification message
1858 * and error contains notification code.
1859 */
1860 if (!oh.unique) {
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001861 err = fuse_notify(fc, oh.error, nbytes - sizeof(oh), cs);
Tejun Heo85993962008-11-26 12:03:55 +01001862 return err ? err : nbytes;
1863 }
1864
1865 err = -EINVAL;
1866 if (oh.error <= -1000 || oh.error > 0)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001867 goto err_finish;
1868
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001869 spin_lock(&fpq->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001870 err = -ENOENT;
Miklos Szeredie96edd92015-07-01 16:26:04 +02001871 if (!fpq->connected)
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001872 goto err_unlock_pq;
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001873
Miklos Szeredi3a2b5b92015-07-01 16:26:04 +02001874 req = request_find(fpq, oh.unique);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001875 if (!req)
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001876 goto err_unlock_pq;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001877
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001878 /* Is it an interrupt reply? */
1879 if (req->intr_unique == oh.unique) {
Kirill Tkhai0799a932018-09-25 12:52:42 +03001880 __fuse_get_request(req);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001881 spin_unlock(&fpq->lock);
1882
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001883 err = -EINVAL;
Kirill Tkhai0799a932018-09-25 12:52:42 +03001884 if (nbytes != sizeof(struct fuse_out_header)) {
1885 fuse_put_request(fc, req);
Miklos Szeredi46c34a32015-07-01 16:26:07 +02001886 goto err_finish;
Kirill Tkhai0799a932018-09-25 12:52:42 +03001887 }
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001888
1889 if (oh.error == -ENOSYS)
1890 fc->no_interrupt = 1;
1891 else if (oh.error == -EAGAIN)
Miklos Szeredif88996a2015-07-01 16:26:01 +02001892 queue_interrupt(&fc->iq, req);
Kirill Tkhai0799a932018-09-25 12:52:42 +03001893 fuse_put_request(fc, req);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001894
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001895 fuse_copy_finish(cs);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001896 return nbytes;
1897 }
1898
Miklos Szeredi33e14b42015-07-01 16:26:01 +02001899 clear_bit(FR_SENT, &req->flags);
Miklos Szeredi3a2b5b92015-07-01 16:26:04 +02001900 list_move(&req->list, &fpq->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001901 req->out.h = oh;
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001902 set_bit(FR_LOCKED, &req->flags);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001903 spin_unlock(&fpq->lock);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001904 cs->req = req;
Miklos Szeredice534fb2010-05-25 15:06:07 +02001905 if (!req->out.page_replace)
1906 cs->move_pages = 0;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001907
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001908 err = copy_out_args(cs, &req->out, nbytes);
1909 fuse_copy_finish(cs);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001910
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001911 spin_lock(&fpq->lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001912 clear_bit(FR_LOCKED, &req->flags);
Miklos Szeredie96edd92015-07-01 16:26:04 +02001913 if (!fpq->connected)
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +02001914 err = -ENOENT;
1915 else if (err)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001916 req->out.h.error = -EIO;
Miklos Szeredi77cd9d42015-07-01 16:26:06 +02001917 if (!test_bit(FR_PRIVATE, &req->flags))
1918 list_del_init(&req->list);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001919 spin_unlock(&fpq->lock);
Miklos Szeredi46c34a32015-07-01 16:26:07 +02001920
Miklos Szeredi334f4852005-09-09 13:10:27 -07001921 request_end(fc, req);
1922
1923 return err ? err : nbytes;
1924
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001925 err_unlock_pq:
1926 spin_unlock(&fpq->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001927 err_finish:
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001928 fuse_copy_finish(cs);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001929 return err;
1930}
1931
Al Virofbdbacc2015-04-03 21:53:39 -04001932static ssize_t fuse_dev_write(struct kiocb *iocb, struct iov_iter *from)
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001933{
1934 struct fuse_copy_state cs;
Miklos Szeredicc080e92015-07-01 16:26:08 +02001935 struct fuse_dev *fud = fuse_get_dev(iocb->ki_filp);
1936
1937 if (!fud)
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001938 return -EPERM;
1939
Al Virofbdbacc2015-04-03 21:53:39 -04001940 if (!iter_is_iovec(from))
1941 return -EINVAL;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001942
Miklos Szeredidc008092015-07-01 16:25:58 +02001943 fuse_copy_init(&cs, 0, from);
Al Virofbdbacc2015-04-03 21:53:39 -04001944
Miklos Szeredic36960462015-07-01 16:26:09 +02001945 return fuse_dev_do_write(fud, &cs, iov_iter_count(from));
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001946}
1947
1948static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe,
1949 struct file *out, loff_t *ppos,
1950 size_t len, unsigned int flags)
1951{
1952 unsigned nbuf;
1953 unsigned idx;
1954 struct pipe_buffer *bufs;
1955 struct fuse_copy_state cs;
Miklos Szeredicc080e92015-07-01 16:26:08 +02001956 struct fuse_dev *fud;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001957 size_t rem;
1958 ssize_t ret;
1959
Miklos Szeredicc080e92015-07-01 16:26:08 +02001960 fud = fuse_get_dev(out);
1961 if (!fud)
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001962 return -EPERM;
1963
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001964 pipe_lock(pipe);
Andrey Ryabinin67a9e482018-07-17 19:00:33 +03001965
1966 bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), GFP_KERNEL);
1967 if (!bufs) {
1968 pipe_unlock(pipe);
1969 return -ENOMEM;
1970 }
1971
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001972 nbuf = 0;
1973 rem = 0;
1974 for (idx = 0; idx < pipe->nrbufs && rem < len; idx++)
1975 rem += pipe->bufs[(pipe->curbuf + idx) & (pipe->buffers - 1)].len;
1976
1977 ret = -EINVAL;
Matthew Wilcox95570902019-04-05 14:02:10 -07001978 if (rem < len)
1979 goto out_free;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001980
1981 rem = len;
1982 while (rem) {
1983 struct pipe_buffer *ibuf;
1984 struct pipe_buffer *obuf;
1985
1986 BUG_ON(nbuf >= pipe->buffers);
1987 BUG_ON(!pipe->nrbufs);
1988 ibuf = &pipe->bufs[pipe->curbuf];
1989 obuf = &bufs[nbuf];
1990
1991 if (rem >= ibuf->len) {
1992 *obuf = *ibuf;
1993 ibuf->ops = NULL;
1994 pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
1995 pipe->nrbufs--;
1996 } else {
Matthew Wilcox95570902019-04-05 14:02:10 -07001997 if (!pipe_buf_get(pipe, ibuf))
1998 goto out_free;
1999
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002000 *obuf = *ibuf;
2001 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
2002 obuf->len = rem;
2003 ibuf->offset += obuf->len;
2004 ibuf->len -= obuf->len;
2005 }
2006 nbuf++;
2007 rem -= obuf->len;
2008 }
2009 pipe_unlock(pipe);
2010
Miklos Szeredidc008092015-07-01 16:25:58 +02002011 fuse_copy_init(&cs, 0, NULL);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002012 cs.pipebufs = bufs;
Al Viro6c09e942015-04-03 22:06:08 -04002013 cs.nr_segs = nbuf;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002014 cs.pipe = pipe;
2015
Miklos Szeredice534fb2010-05-25 15:06:07 +02002016 if (flags & SPLICE_F_MOVE)
2017 cs.move_pages = 1;
2018
Miklos Szeredic36960462015-07-01 16:26:09 +02002019 ret = fuse_dev_do_write(fud, &cs, len);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002020
Jann Horn50449aa2019-01-12 02:39:05 +01002021 pipe_lock(pipe);
Matthew Wilcox95570902019-04-05 14:02:10 -07002022out_free:
Miklos Szeredia7796382016-09-27 10:45:12 +02002023 for (idx = 0; idx < nbuf; idx++)
2024 pipe_buf_release(pipe, &bufs[idx]);
Jann Horn50449aa2019-01-12 02:39:05 +01002025 pipe_unlock(pipe);
Miklos Szeredia7796382016-09-27 10:45:12 +02002026
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002027 kfree(bufs);
2028 return ret;
2029}
2030
Miklos Szeredi334f4852005-09-09 13:10:27 -07002031static unsigned fuse_dev_poll(struct file *file, poll_table *wait)
2032{
Miklos Szeredi334f4852005-09-09 13:10:27 -07002033 unsigned mask = POLLOUT | POLLWRNORM;
Miklos Szeredif88996a2015-07-01 16:26:01 +02002034 struct fuse_iqueue *fiq;
Miklos Szeredicc080e92015-07-01 16:26:08 +02002035 struct fuse_dev *fud = fuse_get_dev(file);
2036
2037 if (!fud)
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07002038 return POLLERR;
Miklos Szeredi334f4852005-09-09 13:10:27 -07002039
Miklos Szeredicc080e92015-07-01 16:26:08 +02002040 fiq = &fud->fc->iq;
Miklos Szeredif88996a2015-07-01 16:26:01 +02002041 poll_wait(file, &fiq->waitq, wait);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002042
Miklos Szeredi4ce60812015-07-01 16:26:02 +02002043 spin_lock(&fiq->waitq.lock);
Miklos Szeredie16714d2015-07-01 16:26:01 +02002044 if (!fiq->connected)
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07002045 mask = POLLERR;
Miklos Szeredif88996a2015-07-01 16:26:01 +02002046 else if (request_pending(fiq))
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07002047 mask |= POLLIN | POLLRDNORM;
Miklos Szeredi4ce60812015-07-01 16:26:02 +02002048 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002049
2050 return mask;
2051}
2052
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002053/*
2054 * Abort all requests on the given list (pending or processing)
2055 *
Miklos Szeredid7133112006-04-10 22:54:55 -07002056 * This function releases and reacquires fc->lock
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002057 */
Miklos Szeredi334f4852005-09-09 13:10:27 -07002058static void end_requests(struct fuse_conn *fc, struct list_head *head)
2059{
2060 while (!list_empty(head)) {
2061 struct fuse_req *req;
2062 req = list_entry(head->next, struct fuse_req, list);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002063 req->out.h.error = -ECONNABORTED;
Miklos Szeredi33e14b42015-07-01 16:26:01 +02002064 clear_bit(FR_SENT, &req->flags);
Miklos Szeredif377cb72015-07-01 16:26:04 +02002065 list_del_init(&req->list);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002066 request_end(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002067 }
2068}
2069
Bryan Green357ccf22011-03-01 16:43:52 -08002070static void end_polls(struct fuse_conn *fc)
2071{
2072 struct rb_node *p;
2073
2074 p = rb_first(&fc->polled_files);
2075
2076 while (p) {
2077 struct fuse_file *ff;
2078 ff = rb_entry(p, struct fuse_file, polled_node);
2079 wake_up_interruptible_all(&ff->poll_wait);
2080
2081 p = rb_next(p);
2082 }
2083}
2084
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002085/*
2086 * Abort all requests.
2087 *
Miklos Szeredib716d422015-07-01 16:25:59 +02002088 * Emergency exit in case of a malicious or accidental deadlock, or just a hung
2089 * filesystem.
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002090 *
Miklos Szeredib716d422015-07-01 16:25:59 +02002091 * The same effect is usually achievable through killing the filesystem daemon
2092 * and all users of the filesystem. The exception is the combination of an
2093 * asynchronous request and the tricky deadlock (see
2094 * Documentation/filesystems/fuse.txt).
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002095 *
Miklos Szeredib716d422015-07-01 16:25:59 +02002096 * Aborting requests under I/O goes as follows: 1: Separate out unlocked
2097 * requests, they should be finished off immediately. Locked requests will be
2098 * finished after unlock; see unlock_request(). 2: Finish off the unlocked
2099 * requests. It is possible that some request will finish before we can. This
2100 * is OK, the request will in that case be removed from the list before we touch
2101 * it.
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002102 */
2103void fuse_abort_conn(struct fuse_conn *fc)
2104{
Miklos Szeredif88996a2015-07-01 16:26:01 +02002105 struct fuse_iqueue *fiq = &fc->iq;
2106
Miklos Szeredid7133112006-04-10 22:54:55 -07002107 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002108 if (fc->connected) {
Miklos Szeredic36960462015-07-01 16:26:09 +02002109 struct fuse_dev *fud;
Miklos Szeredib716d422015-07-01 16:25:59 +02002110 struct fuse_req *req, *next;
Miklos Szeredi41f98272015-07-01 16:25:59 +02002111 LIST_HEAD(to_end1);
2112 LIST_HEAD(to_end2);
Miklos Szeredib716d422015-07-01 16:25:59 +02002113
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002114 fc->connected = 0;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -07002115 fc->blocked = 0;
Miklos Szeredi9759bd512015-01-06 10:45:35 +01002116 fuse_set_initialized(fc);
Miklos Szeredic36960462015-07-01 16:26:09 +02002117 list_for_each_entry(fud, &fc->devices, entry) {
2118 struct fuse_pqueue *fpq = &fud->pq;
2119
2120 spin_lock(&fpq->lock);
2121 fpq->connected = 0;
2122 list_for_each_entry_safe(req, next, &fpq->io, list) {
2123 req->out.h.error = -ECONNABORTED;
2124 spin_lock(&req->waitq.lock);
2125 set_bit(FR_ABORTED, &req->flags);
2126 if (!test_bit(FR_LOCKED, &req->flags)) {
2127 set_bit(FR_PRIVATE, &req->flags);
Miklos Szeredi501c4ca2018-07-26 16:13:11 +02002128 __fuse_get_request(req);
Miklos Szeredic36960462015-07-01 16:26:09 +02002129 list_move(&req->list, &to_end1);
2130 }
2131 spin_unlock(&req->waitq.lock);
Miklos Szeredi77cd9d42015-07-01 16:26:06 +02002132 }
Miklos Szeredic36960462015-07-01 16:26:09 +02002133 list_splice_init(&fpq->processing, &to_end2);
2134 spin_unlock(&fpq->lock);
Miklos Szeredib716d422015-07-01 16:25:59 +02002135 }
Miklos Szeredi41f98272015-07-01 16:25:59 +02002136 fc->max_background = UINT_MAX;
2137 flush_bg_queue(fc);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002138
Miklos Szeredi4ce60812015-07-01 16:26:02 +02002139 spin_lock(&fiq->waitq.lock);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002140 fiq->connected = 0;
Miklos Szeredif88996a2015-07-01 16:26:01 +02002141 list_splice_init(&fiq->pending, &to_end2);
Tahsin Erdogan0181b362017-01-12 12:04:04 -08002142 list_for_each_entry(req, &to_end2, list)
2143 clear_bit(FR_PENDING, &req->flags);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002144 while (forget_pending(fiq))
2145 kfree(dequeue_forget(fiq, 1, NULL));
Miklos Szeredi4ce60812015-07-01 16:26:02 +02002146 wake_up_all_locked(&fiq->waitq);
2147 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002148 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
Miklos Szerediee314a82015-07-01 16:26:08 +02002149 end_polls(fc);
2150 wake_up_all(&fc->blocked_waitq);
2151 spin_unlock(&fc->lock);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002152
Miklos Szeredi41f98272015-07-01 16:25:59 +02002153 while (!list_empty(&to_end1)) {
2154 req = list_first_entry(&to_end1, struct fuse_req, list);
Miklos Szeredif377cb72015-07-01 16:26:04 +02002155 list_del_init(&req->list);
Miklos Szeredib716d422015-07-01 16:25:59 +02002156 request_end(fc, req);
Miklos Szeredib716d422015-07-01 16:25:59 +02002157 }
Miklos Szeredi41f98272015-07-01 16:25:59 +02002158 end_requests(fc, &to_end2);
Miklos Szerediee314a82015-07-01 16:26:08 +02002159 } else {
2160 spin_unlock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002161 }
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002162}
Tejun Heo08cbf542009-04-14 10:54:53 +09002163EXPORT_SYMBOL_GPL(fuse_abort_conn);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002164
Miklos Szeredi6465d762018-07-26 16:13:11 +02002165void fuse_wait_aborted(struct fuse_conn *fc)
2166{
2167 wait_event(fc->blocked_waitq, atomic_read(&fc->num_waiting) == 0);
2168}
2169
Tejun Heo08cbf542009-04-14 10:54:53 +09002170int fuse_dev_release(struct inode *inode, struct file *file)
Miklos Szeredi334f4852005-09-09 13:10:27 -07002171{
Miklos Szeredicc080e92015-07-01 16:26:08 +02002172 struct fuse_dev *fud = fuse_get_dev(file);
2173
2174 if (fud) {
2175 struct fuse_conn *fc = fud->fc;
Miklos Szeredic36960462015-07-01 16:26:09 +02002176 struct fuse_pqueue *fpq = &fud->pq;
Miklos Szeredic66025c2018-07-26 16:13:11 +02002177 LIST_HEAD(to_end);
Miklos Szeredicc080e92015-07-01 16:26:08 +02002178
Miklos Szeredic66025c2018-07-26 16:13:11 +02002179 spin_lock(&fpq->lock);
Miklos Szeredic36960462015-07-01 16:26:09 +02002180 WARN_ON(!list_empty(&fpq->io));
Miklos Szeredic66025c2018-07-26 16:13:11 +02002181 list_splice_init(&fpq->processing, &to_end);
2182 spin_unlock(&fpq->lock);
2183
2184 end_requests(fc, &to_end);
2185
Miklos Szeredic36960462015-07-01 16:26:09 +02002186 /* Are we the last open device? */
2187 if (atomic_dec_and_test(&fc->dev_count)) {
2188 WARN_ON(fc->iq.fasync != NULL);
2189 fuse_abort_conn(fc);
2190 }
Miklos Szeredicc080e92015-07-01 16:26:08 +02002191 fuse_dev_free(fud);
Jeff Dike385a17b2006-04-10 22:54:52 -07002192 }
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{
Miklos Szeredicc080e92015-07-01 16:26:08 +02002199 struct fuse_dev *fud = fuse_get_dev(file);
2200
2201 if (!fud)
Miklos Szeredia87046d2006-04-10 22:54:56 -07002202 return -EPERM;
Jeff Dike385a17b2006-04-10 22:54:52 -07002203
2204 /* No locking - fasync_helper does its own locking */
Miklos Szeredicc080e92015-07-01 16:26:08 +02002205 return fasync_helper(fd, file, on, &fud->fc->iq.fasync);
Jeff Dike385a17b2006-04-10 22:54:52 -07002206}
2207
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002208static int fuse_device_clone(struct fuse_conn *fc, struct file *new)
2209{
Miklos Szeredicc080e92015-07-01 16:26:08 +02002210 struct fuse_dev *fud;
2211
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002212 if (new->private_data)
2213 return -EINVAL;
2214
Miklos Szeredicc080e92015-07-01 16:26:08 +02002215 fud = fuse_dev_alloc(fc);
2216 if (!fud)
2217 return -ENOMEM;
2218
2219 new->private_data = fud;
Miklos Szeredic36960462015-07-01 16:26:09 +02002220 atomic_inc(&fc->dev_count);
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002221
2222 return 0;
2223}
2224
2225static long fuse_dev_ioctl(struct file *file, unsigned int cmd,
2226 unsigned long arg)
2227{
2228 int err = -ENOTTY;
2229
2230 if (cmd == FUSE_DEV_IOC_CLONE) {
2231 int oldfd;
2232
2233 err = -EFAULT;
2234 if (!get_user(oldfd, (__u32 __user *) arg)) {
2235 struct file *old = fget(oldfd);
2236
2237 err = -EINVAL;
2238 if (old) {
Jann Horn8ed1f0e2015-08-16 20:27:01 +02002239 struct fuse_dev *fud = NULL;
2240
2241 /*
2242 * Check against file->f_op because CUSE
2243 * uses the same ioctl handler.
2244 */
2245 if (old->f_op == file->f_op &&
2246 old->f_cred->user_ns == file->f_cred->user_ns)
2247 fud = fuse_get_dev(old);
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002248
Miklos Szeredicc080e92015-07-01 16:26:08 +02002249 if (fud) {
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002250 mutex_lock(&fuse_mutex);
Miklos Szeredicc080e92015-07-01 16:26:08 +02002251 err = fuse_device_clone(fud->fc, file);
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002252 mutex_unlock(&fuse_mutex);
2253 }
2254 fput(old);
2255 }
2256 }
2257 }
2258 return err;
2259}
2260
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002261const struct file_operations fuse_dev_operations = {
Miklos Szeredi334f4852005-09-09 13:10:27 -07002262 .owner = THIS_MODULE,
Tom Van Braeckel94e4fe22015-01-12 05:22:16 +01002263 .open = fuse_dev_open,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002264 .llseek = no_llseek,
Al Virofbdbacc2015-04-03 21:53:39 -04002265 .read_iter = fuse_dev_read,
Miklos Szeredic3021622010-05-25 15:06:07 +02002266 .splice_read = fuse_dev_splice_read,
Al Virofbdbacc2015-04-03 21:53:39 -04002267 .write_iter = fuse_dev_write,
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002268 .splice_write = fuse_dev_splice_write,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002269 .poll = fuse_dev_poll,
2270 .release = fuse_dev_release,
Jeff Dike385a17b2006-04-10 22:54:52 -07002271 .fasync = fuse_dev_fasync,
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002272 .unlocked_ioctl = fuse_dev_ioctl,
2273 .compat_ioctl = fuse_dev_ioctl,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002274};
Tejun Heo08cbf542009-04-14 10:54:53 +09002275EXPORT_SYMBOL_GPL(fuse_dev_operations);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002276
2277static struct miscdevice fuse_miscdevice = {
2278 .minor = FUSE_MINOR,
2279 .name = "fuse",
2280 .fops = &fuse_dev_operations,
2281};
2282
2283int __init fuse_dev_init(void)
2284{
2285 int err = -ENOMEM;
2286 fuse_req_cachep = kmem_cache_create("fuse_request",
2287 sizeof(struct fuse_req),
Paul Mundt20c2df82007-07-20 10:11:58 +09002288 0, 0, NULL);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002289 if (!fuse_req_cachep)
2290 goto out;
2291
2292 err = misc_register(&fuse_miscdevice);
2293 if (err)
2294 goto out_cache_clean;
2295
2296 return 0;
2297
2298 out_cache_clean:
2299 kmem_cache_destroy(fuse_req_cachep);
2300 out:
2301 return err;
2302}
2303
2304void fuse_dev_cleanup(void)
2305{
2306 misc_deregister(&fuse_miscdevice);
2307 kmem_cache_destroy(fuse_req_cachep);
2308}