blob: 881d950c3e6a515437ec63035f48f01554c0b047 [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);
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400386 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
389 /* Wake up next waiter, if any */
Miklos Szeredi3c18ef82013-04-17 21:50:58 +0200390 if (!fc->blocked && waitqueue_active(&fc->blocked_waitq))
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400391 wake_up(&fc->blocked_waitq);
392
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700393 if (fc->num_background == fc->congestion_threshold &&
Tejun Heoa325f9b2009-04-14 10:54:52 +0900394 fc->connected && fc->bdi_initialized) {
Jens Axboe8aa7e842009-07-09 14:52:32 +0200395 clear_bdi_congested(&fc->bdi, BLK_RW_SYNC);
396 clear_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
Miklos Szeredif92b99b2007-10-16 23:30:59 -0700397 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700398 fc->num_background--;
Miklos Szeredid12def12008-02-06 01:38:39 -0800399 fc->active_background--;
400 flush_bg_queue(fc);
Miklos Szerediefe28002015-07-01 16:26:07 +0200401 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700402 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700403 wake_up(&req->waitq);
Miklos Szeredi1e6881c2015-07-01 16:26:07 +0200404 if (req->end)
405 req->end(fc, req);
Miklos Szeredi6465d762018-07-26 16:13:11 +0200406put_request:
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100407 fuse_put_request(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700408}
409
Miklos Szeredif88996a2015-07-01 16:26:01 +0200410static void queue_interrupt(struct fuse_iqueue *fiq, struct fuse_req *req)
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700411{
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200412 spin_lock(&fiq->waitq.lock);
Sahitya Tummala72834482017-02-08 20:30:56 +0530413 if (test_bit(FR_FINISHED, &req->flags)) {
414 spin_unlock(&fiq->waitq.lock);
415 return;
416 }
Miklos Szeredi8f7bb362015-07-01 16:26:03 +0200417 if (list_empty(&req->intr_entry)) {
418 list_add_tail(&req->intr_entry, &fiq->interrupts);
419 wake_up_locked(&fiq->waitq);
420 }
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200421 spin_unlock(&fiq->waitq.lock);
Miklos Szeredif88996a2015-07-01 16:26:01 +0200422 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700423}
424
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700425static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700426{
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200427 struct fuse_iqueue *fiq = &fc->iq;
Miklos Szeredic4775262015-07-01 16:26:00 +0200428 int err;
429
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700430 if (!fc->no_interrupt) {
431 /* Any signal may interrupt this */
Miklos Szeredic4775262015-07-01 16:26:00 +0200432 err = wait_event_interruptible(req->waitq,
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200433 test_bit(FR_FINISHED, &req->flags));
Miklos Szeredic4775262015-07-01 16:26:00 +0200434 if (!err)
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700435 return;
436
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200437 set_bit(FR_INTERRUPTED, &req->flags);
Miklos Szeredi8f7bb362015-07-01 16:26:03 +0200438 /* matches barrier in fuse_dev_do_read() */
439 smp_mb__after_atomic();
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200440 if (test_bit(FR_SENT, &req->flags))
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200441 queue_interrupt(fiq, req);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700442 }
443
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200444 if (!test_bit(FR_FORCE, &req->flags)) {
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700445 /* Only fatal signals may interrupt this */
Al Viro7d3a07f2016-07-19 03:08:27 -0400446 err = wait_event_killable(req->waitq,
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200447 test_bit(FR_FINISHED, &req->flags));
Miklos Szeredic4775262015-07-01 16:26:00 +0200448 if (!err)
Miklos Szeredia131de02007-10-16 23:31:04 -0700449 return;
450
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200451 spin_lock(&fiq->waitq.lock);
Miklos Szeredia131de02007-10-16 23:31:04 -0700452 /* Request is not yet in userspace, bail out */
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200453 if (test_bit(FR_PENDING, &req->flags)) {
Miklos Szeredia131de02007-10-16 23:31:04 -0700454 list_del(&req->list);
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200455 spin_unlock(&fiq->waitq.lock);
Miklos Szeredia131de02007-10-16 23:31:04 -0700456 __fuse_put_request(req);
457 req->out.h.error = -EINTR;
458 return;
459 }
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200460 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700461 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700462
Miklos Szeredia131de02007-10-16 23:31:04 -0700463 /*
464 * Either request is already in userspace, or it was forced.
465 * Wait it out.
466 */
Miklos Szeredi33e14b42015-07-01 16:26:01 +0200467 wait_event(req->waitq, test_bit(FR_FINISHED, &req->flags));
Miklos Szeredi334f4852005-09-09 13:10:27 -0700468}
469
Eric Wong6a4e9222013-02-04 13:04:44 +0000470static void __fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700471{
Miklos Szeredie16714d2015-07-01 16:26:01 +0200472 struct fuse_iqueue *fiq = &fc->iq;
473
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200474 BUG_ON(test_bit(FR_BACKGROUND, &req->flags));
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200475 spin_lock(&fiq->waitq.lock);
Miklos Szeredie16714d2015-07-01 16:26:01 +0200476 if (!fiq->connected) {
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200477 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700478 req->out.h.error = -ENOTCONN;
Miklos Szeredic4775262015-07-01 16:26:00 +0200479 } else {
Miklos Szeredif88996a2015-07-01 16:26:01 +0200480 req->in.h.unique = fuse_get_unique(fiq);
481 queue_request(fiq, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700482 /* acquire extra reference, since request is still needed
483 after request_end() */
484 __fuse_get_request(req);
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200485 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700486
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700487 request_wait_answer(fc, req);
Miklos Szeredic4775262015-07-01 16:26:00 +0200488 /* Pairs with smp_wmb() in request_end() */
489 smp_rmb();
Miklos Szeredi334f4852005-09-09 13:10:27 -0700490 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700491}
Eric Wong6a4e9222013-02-04 13:04:44 +0000492
493void fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
494{
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200495 __set_bit(FR_ISREPLY, &req->flags);
496 if (!test_bit(FR_WAITING, &req->flags)) {
497 __set_bit(FR_WAITING, &req->flags);
Miklos Szeredi5437f242015-07-01 16:25:56 +0200498 atomic_inc(&fc->num_waiting);
499 }
Eric Wong6a4e9222013-02-04 13:04:44 +0000500 __fuse_request_send(fc, req);
501}
Tejun Heo08cbf542009-04-14 10:54:53 +0900502EXPORT_SYMBOL_GPL(fuse_request_send);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700503
Miklos Szeredi21f62172015-01-06 10:45:35 +0100504static void fuse_adjust_compat(struct fuse_conn *fc, struct fuse_args *args)
505{
506 if (fc->minor < 4 && args->in.h.opcode == FUSE_STATFS)
507 args->out.args[0].size = FUSE_COMPAT_STATFS_SIZE;
508
509 if (fc->minor < 9) {
510 switch (args->in.h.opcode) {
511 case FUSE_LOOKUP:
512 case FUSE_CREATE:
513 case FUSE_MKNOD:
514 case FUSE_MKDIR:
515 case FUSE_SYMLINK:
516 case FUSE_LINK:
517 args->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
518 break;
519 case FUSE_GETATTR:
520 case FUSE_SETATTR:
521 args->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
522 break;
523 }
524 }
525 if (fc->minor < 12) {
526 switch (args->in.h.opcode) {
527 case FUSE_CREATE:
528 args->in.args[0].size = sizeof(struct fuse_open_in);
529 break;
530 case FUSE_MKNOD:
531 args->in.args[0].size = FUSE_COMPAT_MKNOD_IN_SIZE;
532 break;
533 }
534 }
535}
536
Miklos Szeredi70781872014-12-12 09:49:05 +0100537ssize_t fuse_simple_request(struct fuse_conn *fc, struct fuse_args *args)
538{
539 struct fuse_req *req;
540 ssize_t ret;
541
542 req = fuse_get_req(fc, 0);
543 if (IS_ERR(req))
544 return PTR_ERR(req);
545
Miklos Szeredi21f62172015-01-06 10:45:35 +0100546 /* Needs to be done after fuse_get_req() so that fc->minor is valid */
547 fuse_adjust_compat(fc, args);
548
Miklos Szeredi70781872014-12-12 09:49:05 +0100549 req->in.h.opcode = args->in.h.opcode;
550 req->in.h.nodeid = args->in.h.nodeid;
551 req->in.numargs = args->in.numargs;
552 memcpy(req->in.args, args->in.args,
553 args->in.numargs * sizeof(struct fuse_in_arg));
554 req->out.argvar = args->out.argvar;
555 req->out.numargs = args->out.numargs;
556 memcpy(req->out.args, args->out.args,
557 args->out.numargs * sizeof(struct fuse_arg));
558 fuse_request_send(fc, req);
559 ret = req->out.h.error;
560 if (!ret && args->out.argvar) {
561 BUG_ON(args->out.numargs != 1);
562 ret = req->out.args[0].size;
563 }
564 fuse_put_request(fc, req);
565
566 return ret;
567}
568
Miklos Szeredif0139aa2015-07-01 16:25:57 +0200569/*
570 * Called under fc->lock
571 *
572 * fc->connected must have been checked previously
573 */
574void fuse_request_send_background_locked(struct fuse_conn *fc,
575 struct fuse_req *req)
Miklos Szeredid12def12008-02-06 01:38:39 -0800576{
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200577 BUG_ON(!test_bit(FR_BACKGROUND, &req->flags));
578 if (!test_bit(FR_WAITING, &req->flags)) {
579 __set_bit(FR_WAITING, &req->flags);
Miklos Szeredi5437f242015-07-01 16:25:56 +0200580 atomic_inc(&fc->num_waiting);
581 }
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200582 __set_bit(FR_ISREPLY, &req->flags);
Miklos Szeredid12def12008-02-06 01:38:39 -0800583 fc->num_background++;
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700584 if (fc->num_background == fc->max_background)
Miklos Szeredid12def12008-02-06 01:38:39 -0800585 fc->blocked = 1;
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700586 if (fc->num_background == fc->congestion_threshold &&
Tejun Heoa325f9b2009-04-14 10:54:52 +0900587 fc->bdi_initialized) {
Jens Axboe8aa7e842009-07-09 14:52:32 +0200588 set_bdi_congested(&fc->bdi, BLK_RW_SYNC);
589 set_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
Miklos Szeredid12def12008-02-06 01:38:39 -0800590 }
591 list_add_tail(&req->list, &fc->bg_queue);
592 flush_bg_queue(fc);
593}
594
Miklos Szeredif0139aa2015-07-01 16:25:57 +0200595void fuse_request_send_background(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700596{
Miklos Szeredi42dc6212015-07-01 16:25:56 +0200597 BUG_ON(!req->end);
Miklos Szeredid7133112006-04-10 22:54:55 -0700598 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700599 if (fc->connected) {
Miklos Szeredif0139aa2015-07-01 16:25:57 +0200600 fuse_request_send_background_locked(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700601 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700602 } else {
Miklos Szeredi42dc6212015-07-01 16:25:56 +0200603 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700604 req->out.h.error = -ENOTCONN;
Miklos Szeredi42dc6212015-07-01 16:25:56 +0200605 req->end(fc, req);
606 fuse_put_request(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700607 }
608}
Tejun Heo08cbf542009-04-14 10:54:53 +0900609EXPORT_SYMBOL_GPL(fuse_request_send_background);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700610
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200611static int fuse_request_send_notify_reply(struct fuse_conn *fc,
612 struct fuse_req *req, u64 unique)
613{
614 int err = -ENODEV;
Miklos Szeredif88996a2015-07-01 16:26:01 +0200615 struct fuse_iqueue *fiq = &fc->iq;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200616
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200617 __clear_bit(FR_ISREPLY, &req->flags);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200618 req->in.h.unique = unique;
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200619 spin_lock(&fiq->waitq.lock);
Miklos Szeredie16714d2015-07-01 16:26:01 +0200620 if (fiq->connected) {
Miklos Szeredif88996a2015-07-01 16:26:01 +0200621 queue_request(fiq, req);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200622 err = 0;
623 }
Miklos Szeredi4ce60812015-07-01 16:26:02 +0200624 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200625
626 return err;
627}
628
Anand V. Avati0b05b182012-08-19 08:53:23 -0400629void fuse_force_forget(struct file *file, u64 nodeid)
630{
Al Viro6131ffa2013-02-27 16:59:05 -0500631 struct inode *inode = file_inode(file);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400632 struct fuse_conn *fc = get_fuse_conn(inode);
633 struct fuse_req *req;
634 struct fuse_forget_in inarg;
635
636 memset(&inarg, 0, sizeof(inarg));
637 inarg.nlookup = 1;
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400638 req = fuse_get_req_nofail_nopages(fc, file);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400639 req->in.h.opcode = FUSE_FORGET;
640 req->in.h.nodeid = nodeid;
641 req->in.numargs = 1;
642 req->in.args[0].size = sizeof(inarg);
643 req->in.args[0].value = &inarg;
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200644 __clear_bit(FR_ISREPLY, &req->flags);
Eric Wong6a4e9222013-02-04 13:04:44 +0000645 __fuse_request_send(fc, req);
646 /* ignore errors */
647 fuse_put_request(fc, req);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400648}
649
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700650/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700651 * Lock the request. Up to the next unlock_request() there mustn't be
652 * anything that could cause a page-fault. If the request was already
Miklos Szeredif9a28422006-06-25 05:48:53 -0700653 * aborted bail out.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700654 */
Miklos Szeredidc008092015-07-01 16:25:58 +0200655static int lock_request(struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700656{
657 int err = 0;
658 if (req) {
Miklos Szeredidc008092015-07-01 16:25:58 +0200659 spin_lock(&req->waitq.lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200660 if (test_bit(FR_ABORTED, &req->flags))
Miklos Szeredi334f4852005-09-09 13:10:27 -0700661 err = -ENOENT;
662 else
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200663 set_bit(FR_LOCKED, &req->flags);
Miklos Szeredidc008092015-07-01 16:25:58 +0200664 spin_unlock(&req->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700665 }
666 return err;
667}
668
669/*
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200670 * Unlock request. If it was aborted while locked, caller is responsible
671 * for unlocking and ending the request.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700672 */
Miklos Szeredidc008092015-07-01 16:25:58 +0200673static int unlock_request(struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700674{
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200675 int err = 0;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700676 if (req) {
Miklos Szeredidc008092015-07-01 16:25:58 +0200677 spin_lock(&req->waitq.lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200678 if (test_bit(FR_ABORTED, &req->flags))
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200679 err = -ENOENT;
680 else
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200681 clear_bit(FR_LOCKED, &req->flags);
Miklos Szeredidc008092015-07-01 16:25:58 +0200682 spin_unlock(&req->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700683 }
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200684 return err;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700685}
686
687struct fuse_copy_state {
688 int write;
689 struct fuse_req *req;
Al Viro6c09e942015-04-03 22:06:08 -0400690 struct iov_iter *iter;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200691 struct pipe_buffer *pipebufs;
692 struct pipe_buffer *currbuf;
693 struct pipe_inode_info *pipe;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700694 unsigned long nr_segs;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700695 struct page *pg;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700696 unsigned len;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200697 unsigned offset;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200698 unsigned move_pages:1;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700699};
700
Miklos Szeredidc008092015-07-01 16:25:58 +0200701static void fuse_copy_init(struct fuse_copy_state *cs, int write,
Al Viro6c09e942015-04-03 22:06:08 -0400702 struct iov_iter *iter)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700703{
704 memset(cs, 0, sizeof(*cs));
705 cs->write = write;
Al Viro6c09e942015-04-03 22:06:08 -0400706 cs->iter = iter;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700707}
708
709/* Unmap and put previous page of userspace buffer */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800710static void fuse_copy_finish(struct fuse_copy_state *cs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700711{
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200712 if (cs->currbuf) {
713 struct pipe_buffer *buf = cs->currbuf;
714
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200715 if (cs->write)
Miklos Szeredic3021622010-05-25 15:06:07 +0200716 buf->len = PAGE_SIZE - cs->len;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200717 cs->currbuf = NULL;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200718 } else if (cs->pg) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700719 if (cs->write) {
720 flush_dcache_page(cs->pg);
721 set_page_dirty_lock(cs->pg);
722 }
723 put_page(cs->pg);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700724 }
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200725 cs->pg = NULL;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700726}
727
728/*
729 * Get another pagefull of userspace buffer, and map it to kernel
730 * address space, and lock request
731 */
732static int fuse_copy_fill(struct fuse_copy_state *cs)
733{
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200734 struct page *page;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700735 int err;
736
Miklos Szeredidc008092015-07-01 16:25:58 +0200737 err = unlock_request(cs->req);
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200738 if (err)
739 return err;
740
Miklos Szeredi334f4852005-09-09 13:10:27 -0700741 fuse_copy_finish(cs);
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200742 if (cs->pipebufs) {
743 struct pipe_buffer *buf = cs->pipebufs;
744
Miklos Szeredic3021622010-05-25 15:06:07 +0200745 if (!cs->write) {
Miklos Szeredifba597d2016-09-27 10:45:12 +0200746 err = pipe_buf_confirm(cs->pipe, buf);
Miklos Szeredic3021622010-05-25 15:06:07 +0200747 if (err)
748 return err;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200749
Miklos Szeredic3021622010-05-25 15:06:07 +0200750 BUG_ON(!cs->nr_segs);
751 cs->currbuf = buf;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200752 cs->pg = buf->page;
753 cs->offset = buf->offset;
Miklos Szeredic3021622010-05-25 15:06:07 +0200754 cs->len = buf->len;
Miklos Szeredic3021622010-05-25 15:06:07 +0200755 cs->pipebufs++;
756 cs->nr_segs--;
757 } else {
Miklos Szeredic3021622010-05-25 15:06:07 +0200758 if (cs->nr_segs == cs->pipe->buffers)
759 return -EIO;
760
761 page = alloc_page(GFP_HIGHUSER);
762 if (!page)
763 return -ENOMEM;
764
765 buf->page = page;
766 buf->offset = 0;
767 buf->len = 0;
768
769 cs->currbuf = buf;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200770 cs->pg = page;
771 cs->offset = 0;
Miklos Szeredic3021622010-05-25 15:06:07 +0200772 cs->len = PAGE_SIZE;
773 cs->pipebufs++;
774 cs->nr_segs++;
775 }
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200776 } else {
Al Viro6c09e942015-04-03 22:06:08 -0400777 size_t off;
778 err = iov_iter_get_pages(cs->iter, &page, PAGE_SIZE, 1, &off);
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200779 if (err < 0)
780 return err;
Al Viro6c09e942015-04-03 22:06:08 -0400781 BUG_ON(!err);
782 cs->len = err;
783 cs->offset = off;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200784 cs->pg = page;
Al Viro6c09e942015-04-03 22:06:08 -0400785 iov_iter_advance(cs->iter, err);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700786 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700787
Miklos Szeredidc008092015-07-01 16:25:58 +0200788 return lock_request(cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700789}
790
791/* Do as much copy to/from userspace buffer as we can */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800792static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700793{
794 unsigned ncpy = min(*size, cs->len);
795 if (val) {
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200796 void *pgaddr = kmap_atomic(cs->pg);
797 void *buf = pgaddr + cs->offset;
798
Miklos Szeredi334f4852005-09-09 13:10:27 -0700799 if (cs->write)
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200800 memcpy(buf, *val, ncpy);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700801 else
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200802 memcpy(*val, buf, ncpy);
803
804 kunmap_atomic(pgaddr);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700805 *val += ncpy;
806 }
807 *size -= ncpy;
808 cs->len -= ncpy;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200809 cs->offset += ncpy;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700810 return ncpy;
811}
812
Miklos Szeredice534fb2010-05-25 15:06:07 +0200813static int fuse_check_page(struct page *page)
814{
815 if (page_mapcount(page) ||
816 page->mapping != NULL ||
817 page_count(page) != 1 ||
818 (page->flags & PAGE_FLAGS_CHECK_AT_PREP &
819 ~(1 << PG_locked |
820 1 << PG_referenced |
821 1 << PG_uptodate |
822 1 << PG_lru |
823 1 << PG_active |
824 1 << PG_reclaim))) {
825 printk(KERN_WARNING "fuse: trying to steal weird page\n");
826 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);
827 return 1;
828 }
829 return 0;
830}
831
832static int fuse_try_move_page(struct fuse_copy_state *cs, struct page **pagep)
833{
834 int err;
835 struct page *oldpage = *pagep;
836 struct page *newpage;
837 struct pipe_buffer *buf = cs->pipebufs;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200838
Miklos Szeredidc008092015-07-01 16:25:58 +0200839 err = unlock_request(cs->req);
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200840 if (err)
841 return err;
842
Miklos Szeredice534fb2010-05-25 15:06:07 +0200843 fuse_copy_finish(cs);
844
Miklos Szeredifba597d2016-09-27 10:45:12 +0200845 err = pipe_buf_confirm(cs->pipe, buf);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200846 if (err)
847 return err;
848
849 BUG_ON(!cs->nr_segs);
850 cs->currbuf = buf;
851 cs->len = buf->len;
852 cs->pipebufs++;
853 cs->nr_segs--;
854
855 if (cs->len != PAGE_SIZE)
856 goto out_fallback;
857
Miklos Szeredica76f5b2016-09-27 10:45:12 +0200858 if (pipe_buf_steal(cs->pipe, buf) != 0)
Miklos Szeredice534fb2010-05-25 15:06:07 +0200859 goto out_fallback;
860
861 newpage = buf->page;
862
Miklos Szerediaa991b32015-02-26 11:45:47 +0100863 if (!PageUptodate(newpage))
864 SetPageUptodate(newpage);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200865
866 ClearPageMappedToDisk(newpage);
867
868 if (fuse_check_page(newpage) != 0)
869 goto out_fallback_unlock;
870
Miklos Szeredice534fb2010-05-25 15:06:07 +0200871 /*
872 * This is a new and locked page, it shouldn't be mapped or
873 * have any special flags on it
874 */
875 if (WARN_ON(page_mapped(oldpage)))
876 goto out_fallback_unlock;
877 if (WARN_ON(page_has_private(oldpage)))
878 goto out_fallback_unlock;
879 if (WARN_ON(PageDirty(oldpage) || PageWriteback(oldpage)))
880 goto out_fallback_unlock;
881 if (WARN_ON(PageMlocked(oldpage)))
882 goto out_fallback_unlock;
883
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700884 err = replace_page_cache_page(oldpage, newpage, GFP_KERNEL);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200885 if (err) {
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700886 unlock_page(newpage);
887 return err;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200888 }
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700889
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300890 get_page(newpage);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200891
892 if (!(buf->flags & PIPE_BUF_FLAG_LRU))
893 lru_cache_add_file(newpage);
894
895 err = 0;
Miklos Szeredidc008092015-07-01 16:25:58 +0200896 spin_lock(&cs->req->waitq.lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200897 if (test_bit(FR_ABORTED, &cs->req->flags))
Miklos Szeredice534fb2010-05-25 15:06:07 +0200898 err = -ENOENT;
899 else
900 *pagep = newpage;
Miklos Szeredidc008092015-07-01 16:25:58 +0200901 spin_unlock(&cs->req->waitq.lock);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200902
903 if (err) {
904 unlock_page(newpage);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300905 put_page(newpage);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200906 return err;
907 }
908
909 unlock_page(oldpage);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300910 put_page(oldpage);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200911 cs->len = 0;
912
913 return 0;
914
915out_fallback_unlock:
916 unlock_page(newpage);
917out_fallback:
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200918 cs->pg = buf->page;
919 cs->offset = buf->offset;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200920
Miklos Szeredidc008092015-07-01 16:25:58 +0200921 err = lock_request(cs->req);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200922 if (err)
923 return err;
924
925 return 1;
926}
927
Miklos Szeredic3021622010-05-25 15:06:07 +0200928static int fuse_ref_page(struct fuse_copy_state *cs, struct page *page,
929 unsigned offset, unsigned count)
930{
931 struct pipe_buffer *buf;
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200932 int err;
Miklos Szeredic3021622010-05-25 15:06:07 +0200933
934 if (cs->nr_segs == cs->pipe->buffers)
935 return -EIO;
936
Miklos Szeredidc008092015-07-01 16:25:58 +0200937 err = unlock_request(cs->req);
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200938 if (err)
939 return err;
940
Miklos Szeredic3021622010-05-25 15:06:07 +0200941 fuse_copy_finish(cs);
942
943 buf = cs->pipebufs;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300944 get_page(page);
Miklos Szeredic3021622010-05-25 15:06:07 +0200945 buf->page = page;
946 buf->offset = offset;
947 buf->len = count;
948
949 cs->pipebufs++;
950 cs->nr_segs++;
951 cs->len = 0;
952
953 return 0;
954}
955
Miklos Szeredi334f4852005-09-09 13:10:27 -0700956/*
957 * Copy a page in the request to/from the userspace buffer. Must be
958 * done atomically
959 */
Miklos Szeredice534fb2010-05-25 15:06:07 +0200960static int fuse_copy_page(struct fuse_copy_state *cs, struct page **pagep,
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800961 unsigned offset, unsigned count, int zeroing)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700962{
Miklos Szeredice534fb2010-05-25 15:06:07 +0200963 int err;
964 struct page *page = *pagep;
965
Miklos Szeredib6777c42010-10-26 14:22:27 -0700966 if (page && zeroing && count < PAGE_SIZE)
967 clear_highpage(page);
968
Miklos Szeredi334f4852005-09-09 13:10:27 -0700969 while (count) {
Miklos Szeredic3021622010-05-25 15:06:07 +0200970 if (cs->write && cs->pipebufs && page) {
971 return fuse_ref_page(cs, page, offset, count);
972 } else if (!cs->len) {
Miklos Szeredice534fb2010-05-25 15:06:07 +0200973 if (cs->move_pages && page &&
974 offset == 0 && count == PAGE_SIZE) {
975 err = fuse_try_move_page(cs, pagep);
976 if (err <= 0)
977 return err;
978 } else {
979 err = fuse_copy_fill(cs);
980 if (err)
981 return err;
982 }
Miklos Szeredi1729a162008-11-26 12:03:54 +0100983 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700984 if (page) {
Cong Wang2408f6e2011-11-25 23:14:30 +0800985 void *mapaddr = kmap_atomic(page);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700986 void *buf = mapaddr + offset;
987 offset += fuse_copy_do(cs, &buf, &count);
Cong Wang2408f6e2011-11-25 23:14:30 +0800988 kunmap_atomic(mapaddr);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700989 } else
990 offset += fuse_copy_do(cs, NULL, &count);
991 }
992 if (page && !cs->write)
993 flush_dcache_page(page);
994 return 0;
995}
996
997/* Copy pages in the request to/from userspace buffer */
998static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
999 int zeroing)
1000{
1001 unsigned i;
1002 struct fuse_req *req = cs->req;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001003
1004 for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
Miklos Szeredice534fb2010-05-25 15:06:07 +02001005 int err;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001006 unsigned offset = req->page_descs[i].offset;
1007 unsigned count = min(nbytes, req->page_descs[i].length);
Miklos Szeredice534fb2010-05-25 15:06:07 +02001008
1009 err = fuse_copy_page(cs, &req->pages[i], offset, count,
1010 zeroing);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001011 if (err)
1012 return err;
1013
1014 nbytes -= count;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001015 }
1016 return 0;
1017}
1018
1019/* Copy a single argument in the request to/from userspace buffer */
1020static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
1021{
1022 while (size) {
Miklos Szeredi1729a162008-11-26 12:03:54 +01001023 if (!cs->len) {
1024 int err = fuse_copy_fill(cs);
1025 if (err)
1026 return err;
1027 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001028 fuse_copy_do(cs, &val, &size);
1029 }
1030 return 0;
1031}
1032
1033/* Copy request arguments to/from userspace buffer */
1034static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
1035 unsigned argpages, struct fuse_arg *args,
1036 int zeroing)
1037{
1038 int err = 0;
1039 unsigned i;
1040
1041 for (i = 0; !err && i < numargs; i++) {
1042 struct fuse_arg *arg = &args[i];
1043 if (i == numargs - 1 && argpages)
1044 err = fuse_copy_pages(cs, arg->size, zeroing);
1045 else
1046 err = fuse_copy_one(cs, arg->value, arg->size);
1047 }
1048 return err;
1049}
1050
Miklos Szeredif88996a2015-07-01 16:26:01 +02001051static int forget_pending(struct fuse_iqueue *fiq)
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001052{
Miklos Szeredif88996a2015-07-01 16:26:01 +02001053 return fiq->forget_list_head.next != NULL;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001054}
1055
Miklos Szeredif88996a2015-07-01 16:26:01 +02001056static int request_pending(struct fuse_iqueue *fiq)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001057{
Miklos Szeredif88996a2015-07-01 16:26:01 +02001058 return !list_empty(&fiq->pending) || !list_empty(&fiq->interrupts) ||
1059 forget_pending(fiq);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001060}
1061
Miklos Szeredi334f4852005-09-09 13:10:27 -07001062/*
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001063 * Transfer an interrupt request to userspace
1064 *
1065 * Unlike other requests this is assembled on demand, without a need
1066 * to allocate a separate fuse_req structure.
1067 *
Miklos Szeredifd22d622015-07-01 16:26:03 +02001068 * Called with fiq->waitq.lock held, releases it
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001069 */
Miklos Szeredifd22d622015-07-01 16:26:03 +02001070static int fuse_read_interrupt(struct fuse_iqueue *fiq,
1071 struct fuse_copy_state *cs,
Miklos Szeredic3021622010-05-25 15:06:07 +02001072 size_t nbytes, struct fuse_req *req)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001073__releases(fiq->waitq.lock)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001074{
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001075 struct fuse_in_header ih;
1076 struct fuse_interrupt_in arg;
1077 unsigned reqsize = sizeof(ih) + sizeof(arg);
1078 int err;
1079
1080 list_del_init(&req->intr_entry);
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001081 req->intr_unique = fuse_get_unique(fiq);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001082 memset(&ih, 0, sizeof(ih));
1083 memset(&arg, 0, sizeof(arg));
1084 ih.len = reqsize;
1085 ih.opcode = FUSE_INTERRUPT;
1086 ih.unique = req->intr_unique;
1087 arg.unique = req->in.h.unique;
1088
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001089 spin_unlock(&fiq->waitq.lock);
Miklos Szeredic3021622010-05-25 15:06:07 +02001090 if (nbytes < reqsize)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001091 return -EINVAL;
1092
Miklos Szeredic3021622010-05-25 15:06:07 +02001093 err = fuse_copy_one(cs, &ih, sizeof(ih));
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001094 if (!err)
Miklos Szeredic3021622010-05-25 15:06:07 +02001095 err = fuse_copy_one(cs, &arg, sizeof(arg));
1096 fuse_copy_finish(cs);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001097
1098 return err ? err : reqsize;
1099}
1100
Miklos Szeredif88996a2015-07-01 16:26:01 +02001101static struct fuse_forget_link *dequeue_forget(struct fuse_iqueue *fiq,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001102 unsigned max,
1103 unsigned *countp)
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001104{
Miklos Szeredif88996a2015-07-01 16:26:01 +02001105 struct fuse_forget_link *head = fiq->forget_list_head.next;
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001106 struct fuse_forget_link **newhead = &head;
1107 unsigned count;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001108
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001109 for (count = 0; *newhead != NULL && count < max; count++)
1110 newhead = &(*newhead)->next;
1111
Miklos Szeredif88996a2015-07-01 16:26:01 +02001112 fiq->forget_list_head.next = *newhead;
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001113 *newhead = NULL;
Miklos Szeredif88996a2015-07-01 16:26:01 +02001114 if (fiq->forget_list_head.next == NULL)
1115 fiq->forget_list_tail = &fiq->forget_list_head;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001116
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001117 if (countp != NULL)
1118 *countp = count;
1119
1120 return head;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001121}
1122
Miklos Szeredifd22d622015-07-01 16:26:03 +02001123static int fuse_read_single_forget(struct fuse_iqueue *fiq,
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001124 struct fuse_copy_state *cs,
1125 size_t nbytes)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001126__releases(fiq->waitq.lock)
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001127{
1128 int err;
Miklos Szeredif88996a2015-07-01 16:26:01 +02001129 struct fuse_forget_link *forget = dequeue_forget(fiq, 1, NULL);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001130 struct fuse_forget_in arg = {
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001131 .nlookup = forget->forget_one.nlookup,
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001132 };
1133 struct fuse_in_header ih = {
1134 .opcode = FUSE_FORGET,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001135 .nodeid = forget->forget_one.nodeid,
Miklos Szeredif88996a2015-07-01 16:26:01 +02001136 .unique = fuse_get_unique(fiq),
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001137 .len = sizeof(ih) + sizeof(arg),
1138 };
1139
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001140 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001141 kfree(forget);
1142 if (nbytes < ih.len)
1143 return -EINVAL;
1144
1145 err = fuse_copy_one(cs, &ih, sizeof(ih));
1146 if (!err)
1147 err = fuse_copy_one(cs, &arg, sizeof(arg));
1148 fuse_copy_finish(cs);
1149
1150 if (err)
1151 return err;
1152
1153 return ih.len;
1154}
1155
Miklos Szeredifd22d622015-07-01 16:26:03 +02001156static int fuse_read_batch_forget(struct fuse_iqueue *fiq,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001157 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001158__releases(fiq->waitq.lock)
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001159{
1160 int err;
1161 unsigned max_forgets;
1162 unsigned count;
1163 struct fuse_forget_link *head;
1164 struct fuse_batch_forget_in arg = { .count = 0 };
1165 struct fuse_in_header ih = {
1166 .opcode = FUSE_BATCH_FORGET,
Miklos Szeredif88996a2015-07-01 16:26:01 +02001167 .unique = fuse_get_unique(fiq),
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001168 .len = sizeof(ih) + sizeof(arg),
1169 };
1170
1171 if (nbytes < ih.len) {
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001172 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001173 return -EINVAL;
1174 }
1175
1176 max_forgets = (nbytes - ih.len) / sizeof(struct fuse_forget_one);
Miklos Szeredif88996a2015-07-01 16:26:01 +02001177 head = dequeue_forget(fiq, max_forgets, &count);
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001178 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001179
1180 arg.count = count;
1181 ih.len += count * sizeof(struct fuse_forget_one);
1182 err = fuse_copy_one(cs, &ih, sizeof(ih));
1183 if (!err)
1184 err = fuse_copy_one(cs, &arg, sizeof(arg));
1185
1186 while (head) {
1187 struct fuse_forget_link *forget = head;
1188
1189 if (!err) {
1190 err = fuse_copy_one(cs, &forget->forget_one,
1191 sizeof(forget->forget_one));
1192 }
1193 head = forget->next;
1194 kfree(forget);
1195 }
1196
1197 fuse_copy_finish(cs);
1198
1199 if (err)
1200 return err;
1201
1202 return ih.len;
1203}
1204
Miklos Szeredifd22d622015-07-01 16:26:03 +02001205static int fuse_read_forget(struct fuse_conn *fc, struct fuse_iqueue *fiq,
1206 struct fuse_copy_state *cs,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001207 size_t nbytes)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001208__releases(fiq->waitq.lock)
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001209{
Miklos Szeredif88996a2015-07-01 16:26:01 +02001210 if (fc->minor < 16 || fiq->forget_list_head.next->next == NULL)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001211 return fuse_read_single_forget(fiq, cs, nbytes);
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001212 else
Miklos Szeredifd22d622015-07-01 16:26:03 +02001213 return fuse_read_batch_forget(fiq, cs, nbytes);
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001214}
1215
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001216/*
Miklos Szeredi334f4852005-09-09 13:10:27 -07001217 * Read a single request into the userspace filesystem's buffer. This
1218 * function waits until a request is available, then removes it from
1219 * the pending list and copies request data to userspace buffer. If
Miklos Szeredif9a28422006-06-25 05:48:53 -07001220 * no reply is needed (FORGET) or request has been aborted or there
1221 * was an error during the copying then it's finished by calling
Miklos Szeredi334f4852005-09-09 13:10:27 -07001222 * request_end(). Otherwise add it to the processing list, and set
1223 * the 'sent' flag.
1224 */
Miklos Szeredic36960462015-07-01 16:26:09 +02001225static ssize_t fuse_dev_do_read(struct fuse_dev *fud, struct file *file,
Miklos Szeredic3021622010-05-25 15:06:07 +02001226 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001227{
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001228 ssize_t err;
Miklos Szeredic36960462015-07-01 16:26:09 +02001229 struct fuse_conn *fc = fud->fc;
Miklos Szeredif88996a2015-07-01 16:26:01 +02001230 struct fuse_iqueue *fiq = &fc->iq;
Miklos Szeredic36960462015-07-01 16:26:09 +02001231 struct fuse_pqueue *fpq = &fud->pq;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001232 struct fuse_req *req;
1233 struct fuse_in *in;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001234 unsigned reqsize;
1235
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001236 restart:
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001237 spin_lock(&fiq->waitq.lock);
Jeff Dikee5ac1d12006-04-10 22:54:53 -07001238 err = -EAGAIN;
Miklos Szeredie16714d2015-07-01 16:26:01 +02001239 if ((file->f_flags & O_NONBLOCK) && fiq->connected &&
Miklos Szeredif88996a2015-07-01 16:26:01 +02001240 !request_pending(fiq))
Jeff Dikee5ac1d12006-04-10 22:54:53 -07001241 goto err_unlock;
1242
Miklos Szeredi52509212015-07-01 16:26:03 +02001243 err = wait_event_interruptible_exclusive_locked(fiq->waitq,
1244 !fiq->connected || request_pending(fiq));
1245 if (err)
1246 goto err_unlock;
1247
Miklos Szeredi334f4852005-09-09 13:10:27 -07001248 err = -ENODEV;
Miklos Szeredie16714d2015-07-01 16:26:01 +02001249 if (!fiq->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001250 goto err_unlock;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001251
Miklos Szeredif88996a2015-07-01 16:26:01 +02001252 if (!list_empty(&fiq->interrupts)) {
1253 req = list_entry(fiq->interrupts.next, struct fuse_req,
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001254 intr_entry);
Miklos Szeredifd22d622015-07-01 16:26:03 +02001255 return fuse_read_interrupt(fiq, cs, nbytes, req);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001256 }
1257
Miklos Szeredif88996a2015-07-01 16:26:01 +02001258 if (forget_pending(fiq)) {
1259 if (list_empty(&fiq->pending) || fiq->forget_batch-- > 0)
Miklos Szeredifd22d622015-07-01 16:26:03 +02001260 return fuse_read_forget(fc, fiq, cs, nbytes);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001261
Miklos Szeredif88996a2015-07-01 16:26:01 +02001262 if (fiq->forget_batch <= -8)
1263 fiq->forget_batch = 16;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001264 }
1265
Miklos Szeredif88996a2015-07-01 16:26:01 +02001266 req = list_entry(fiq->pending.next, struct fuse_req, list);
Miklos Szeredi33e14b42015-07-01 16:26:01 +02001267 clear_bit(FR_PENDING, &req->flags);
Miklos Szeredief759252015-07-01 16:26:02 +02001268 list_del_init(&req->list);
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001269 spin_unlock(&fiq->waitq.lock);
1270
Miklos Szeredi334f4852005-09-09 13:10:27 -07001271 in = &req->in;
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001272 reqsize = in->h.len;
1273 /* If request is too large, reply with an error and restart the read */
Miklos Szeredic3021622010-05-25 15:06:07 +02001274 if (nbytes < reqsize) {
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001275 req->out.h.error = -EIO;
1276 /* SETXATTR is special, since it may contain too large data */
1277 if (in->h.opcode == FUSE_SETXATTR)
1278 req->out.h.error = -E2BIG;
1279 request_end(fc, req);
1280 goto restart;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001281 }
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001282 spin_lock(&fpq->lock);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001283 list_add(&req->list, &fpq->io);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001284 spin_unlock(&fpq->lock);
Miklos Szeredic3021622010-05-25 15:06:07 +02001285 cs->req = req;
1286 err = fuse_copy_one(cs, &in->h, sizeof(in->h));
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001287 if (!err)
Miklos Szeredic3021622010-05-25 15:06:07 +02001288 err = fuse_copy_args(cs, in->numargs, in->argpages,
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001289 (struct fuse_arg *) in->args, 0);
Miklos Szeredic3021622010-05-25 15:06:07 +02001290 fuse_copy_finish(cs);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001291 spin_lock(&fpq->lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001292 clear_bit(FR_LOCKED, &req->flags);
Miklos Szeredie96edd92015-07-01 16:26:04 +02001293 if (!fpq->connected) {
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001294 err = -ENODEV;
1295 goto out_end;
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -07001296 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001297 if (err) {
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -07001298 req->out.h.error = -EIO;
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001299 goto out_end;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001300 }
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001301 if (!test_bit(FR_ISREPLY, &req->flags)) {
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001302 err = reqsize;
1303 goto out_end;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001304 }
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001305 list_move_tail(&req->list, &fpq->processing);
Kirill Tkhai7996b1c2018-09-25 12:28:55 +03001306 __fuse_get_request(req);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001307 spin_unlock(&fpq->lock);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001308 set_bit(FR_SENT, &req->flags);
1309 /* matches barrier in request_wait_answer() */
1310 smp_mb__after_atomic();
1311 if (test_bit(FR_INTERRUPTED, &req->flags))
1312 queue_interrupt(fiq, req);
Kirill Tkhai7996b1c2018-09-25 12:28:55 +03001313 fuse_put_request(fc, req);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001314
Miklos Szeredi334f4852005-09-09 13:10:27 -07001315 return reqsize;
1316
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001317out_end:
Miklos Szeredi77cd9d42015-07-01 16:26:06 +02001318 if (!test_bit(FR_PRIVATE, &req->flags))
1319 list_del_init(&req->list);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001320 spin_unlock(&fpq->lock);
Miklos Szeredi82cbdcd2015-07-01 16:26:05 +02001321 request_end(fc, req);
1322 return err;
1323
Miklos Szeredi334f4852005-09-09 13:10:27 -07001324 err_unlock:
Miklos Szeredi4ce60812015-07-01 16:26:02 +02001325 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001326 return err;
1327}
1328
Tom Van Braeckel94e4fe22015-01-12 05:22:16 +01001329static int fuse_dev_open(struct inode *inode, struct file *file)
1330{
1331 /*
1332 * The fuse device's file's private_data is used to hold
1333 * the fuse_conn(ection) when it is mounted, and is used to
1334 * keep track of whether the file has been mounted already.
1335 */
1336 file->private_data = NULL;
1337 return 0;
1338}
1339
Al Virofbdbacc2015-04-03 21:53:39 -04001340static ssize_t fuse_dev_read(struct kiocb *iocb, struct iov_iter *to)
Miklos Szeredic3021622010-05-25 15:06:07 +02001341{
1342 struct fuse_copy_state cs;
1343 struct file *file = iocb->ki_filp;
Miklos Szeredicc080e92015-07-01 16:26:08 +02001344 struct fuse_dev *fud = fuse_get_dev(file);
1345
1346 if (!fud)
Miklos Szeredic3021622010-05-25 15:06:07 +02001347 return -EPERM;
1348
Al Virofbdbacc2015-04-03 21:53:39 -04001349 if (!iter_is_iovec(to))
1350 return -EINVAL;
Miklos Szeredic3021622010-05-25 15:06:07 +02001351
Miklos Szeredidc008092015-07-01 16:25:58 +02001352 fuse_copy_init(&cs, 1, to);
Al Virofbdbacc2015-04-03 21:53:39 -04001353
Miklos Szeredic36960462015-07-01 16:26:09 +02001354 return fuse_dev_do_read(fud, file, &cs, iov_iter_count(to));
Miklos Szeredic3021622010-05-25 15:06:07 +02001355}
1356
Miklos Szeredic3021622010-05-25 15:06:07 +02001357static ssize_t fuse_dev_splice_read(struct file *in, loff_t *ppos,
1358 struct pipe_inode_info *pipe,
1359 size_t len, unsigned int flags)
1360{
Al Virod82718e2016-09-17 22:56:25 -04001361 int total, ret;
Miklos Szeredic3021622010-05-25 15:06:07 +02001362 int page_nr = 0;
Miklos Szeredic3021622010-05-25 15:06:07 +02001363 struct pipe_buffer *bufs;
1364 struct fuse_copy_state cs;
Miklos Szeredicc080e92015-07-01 16:26:08 +02001365 struct fuse_dev *fud = fuse_get_dev(in);
1366
1367 if (!fud)
Miklos Szeredic3021622010-05-25 15:06:07 +02001368 return -EPERM;
1369
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001370 bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), GFP_KERNEL);
Miklos Szeredic3021622010-05-25 15:06:07 +02001371 if (!bufs)
1372 return -ENOMEM;
1373
Miklos Szeredidc008092015-07-01 16:25:58 +02001374 fuse_copy_init(&cs, 1, NULL);
Miklos Szeredic3021622010-05-25 15:06:07 +02001375 cs.pipebufs = bufs;
1376 cs.pipe = pipe;
Miklos Szeredic36960462015-07-01 16:26:09 +02001377 ret = fuse_dev_do_read(fud, in, &cs, len);
Miklos Szeredic3021622010-05-25 15:06:07 +02001378 if (ret < 0)
1379 goto out;
1380
Miklos Szeredic3021622010-05-25 15:06:07 +02001381 if (pipe->nrbufs + cs.nr_segs > pipe->buffers) {
1382 ret = -EIO;
Al Virod82718e2016-09-17 22:56:25 -04001383 goto out;
Miklos Szeredic3021622010-05-25 15:06:07 +02001384 }
1385
Al Virod82718e2016-09-17 22:56:25 -04001386 for (ret = total = 0; page_nr < cs.nr_segs; total += ret) {
Miklos Szeredi28a625c2014-01-22 19:36:57 +01001387 /*
1388 * Need to be careful about this. Having buf->ops in module
1389 * code can Oops if the buffer persists after module unload.
1390 */
Al Virod82718e2016-09-17 22:56:25 -04001391 bufs[page_nr].ops = &nosteal_pipe_buf_ops;
Miklos Szeredi80a04772017-02-16 15:08:20 +01001392 bufs[page_nr].flags = 0;
Al Virod82718e2016-09-17 22:56:25 -04001393 ret = add_to_pipe(pipe, &bufs[page_nr++]);
1394 if (unlikely(ret < 0))
1395 break;
Miklos Szeredic3021622010-05-25 15:06:07 +02001396 }
Al Virod82718e2016-09-17 22:56:25 -04001397 if (total)
1398 ret = total;
Miklos Szeredic3021622010-05-25 15:06:07 +02001399out:
1400 for (; page_nr < cs.nr_segs; page_nr++)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001401 put_page(bufs[page_nr].page);
Miklos Szeredic3021622010-05-25 15:06:07 +02001402
1403 kfree(bufs);
1404 return ret;
1405}
1406
Tejun Heo95668a62008-11-26 12:03:55 +01001407static int fuse_notify_poll(struct fuse_conn *fc, unsigned int size,
1408 struct fuse_copy_state *cs)
1409{
1410 struct fuse_notify_poll_wakeup_out outarg;
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001411 int err = -EINVAL;
Tejun Heo95668a62008-11-26 12:03:55 +01001412
1413 if (size != sizeof(outarg))
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001414 goto err;
Tejun Heo95668a62008-11-26 12:03:55 +01001415
1416 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1417 if (err)
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001418 goto err;
Tejun Heo95668a62008-11-26 12:03:55 +01001419
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001420 fuse_copy_finish(cs);
Tejun Heo95668a62008-11-26 12:03:55 +01001421 return fuse_notify_poll_wakeup(fc, &outarg);
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001422
1423err:
1424 fuse_copy_finish(cs);
1425 return err;
Tejun Heo95668a62008-11-26 12:03:55 +01001426}
1427
John Muir3b463ae2009-05-31 11:13:57 -04001428static int fuse_notify_inval_inode(struct fuse_conn *fc, unsigned int size,
1429 struct fuse_copy_state *cs)
1430{
1431 struct fuse_notify_inval_inode_out outarg;
1432 int err = -EINVAL;
1433
1434 if (size != sizeof(outarg))
1435 goto err;
1436
1437 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1438 if (err)
1439 goto err;
1440 fuse_copy_finish(cs);
1441
1442 down_read(&fc->killsb);
1443 err = -ENOENT;
Miklos Szeredib21dda42010-02-05 12:08:31 +01001444 if (fc->sb) {
1445 err = fuse_reverse_inval_inode(fc->sb, outarg.ino,
1446 outarg.off, outarg.len);
1447 }
John Muir3b463ae2009-05-31 11:13:57 -04001448 up_read(&fc->killsb);
1449 return err;
1450
1451err:
1452 fuse_copy_finish(cs);
1453 return err;
1454}
1455
1456static int fuse_notify_inval_entry(struct fuse_conn *fc, unsigned int size,
1457 struct fuse_copy_state *cs)
1458{
1459 struct fuse_notify_inval_entry_out outarg;
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001460 int err = -ENOMEM;
1461 char *buf;
John Muir3b463ae2009-05-31 11:13:57 -04001462 struct qstr name;
1463
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001464 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1465 if (!buf)
1466 goto err;
1467
1468 err = -EINVAL;
John Muir3b463ae2009-05-31 11:13:57 -04001469 if (size < sizeof(outarg))
1470 goto err;
1471
1472 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1473 if (err)
1474 goto err;
1475
1476 err = -ENAMETOOLONG;
1477 if (outarg.namelen > FUSE_NAME_MAX)
1478 goto err;
1479
Miklos Szeredic2183d12011-08-24 10:20:17 +02001480 err = -EINVAL;
1481 if (size != sizeof(outarg) + outarg.namelen + 1)
1482 goto err;
1483
John Muir3b463ae2009-05-31 11:13:57 -04001484 name.name = buf;
1485 name.len = outarg.namelen;
1486 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1487 if (err)
1488 goto err;
1489 fuse_copy_finish(cs);
1490 buf[outarg.namelen] = 0;
John Muir3b463ae2009-05-31 11:13:57 -04001491
1492 down_read(&fc->killsb);
1493 err = -ENOENT;
Miklos Szeredib21dda42010-02-05 12:08:31 +01001494 if (fc->sb)
John Muir451d0f52011-12-06 21:50:06 +01001495 err = fuse_reverse_inval_entry(fc->sb, outarg.parent, 0, &name);
1496 up_read(&fc->killsb);
1497 kfree(buf);
1498 return err;
1499
1500err:
1501 kfree(buf);
1502 fuse_copy_finish(cs);
1503 return err;
1504}
1505
1506static int fuse_notify_delete(struct fuse_conn *fc, unsigned int size,
1507 struct fuse_copy_state *cs)
1508{
1509 struct fuse_notify_delete_out outarg;
1510 int err = -ENOMEM;
1511 char *buf;
1512 struct qstr name;
1513
1514 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1515 if (!buf)
1516 goto err;
1517
1518 err = -EINVAL;
1519 if (size < sizeof(outarg))
1520 goto err;
1521
1522 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1523 if (err)
1524 goto err;
1525
1526 err = -ENAMETOOLONG;
1527 if (outarg.namelen > FUSE_NAME_MAX)
1528 goto err;
1529
1530 err = -EINVAL;
1531 if (size != sizeof(outarg) + outarg.namelen + 1)
1532 goto err;
1533
1534 name.name = buf;
1535 name.len = outarg.namelen;
1536 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1537 if (err)
1538 goto err;
1539 fuse_copy_finish(cs);
1540 buf[outarg.namelen] = 0;
John Muir451d0f52011-12-06 21:50:06 +01001541
1542 down_read(&fc->killsb);
1543 err = -ENOENT;
1544 if (fc->sb)
1545 err = fuse_reverse_inval_entry(fc->sb, outarg.parent,
1546 outarg.child, &name);
John Muir3b463ae2009-05-31 11:13:57 -04001547 up_read(&fc->killsb);
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001548 kfree(buf);
John Muir3b463ae2009-05-31 11:13:57 -04001549 return err;
1550
1551err:
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001552 kfree(buf);
John Muir3b463ae2009-05-31 11:13:57 -04001553 fuse_copy_finish(cs);
1554 return err;
1555}
1556
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001557static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
1558 struct fuse_copy_state *cs)
1559{
1560 struct fuse_notify_store_out outarg;
1561 struct inode *inode;
1562 struct address_space *mapping;
1563 u64 nodeid;
1564 int err;
1565 pgoff_t index;
1566 unsigned int offset;
1567 unsigned int num;
1568 loff_t file_size;
1569 loff_t end;
1570
1571 err = -EINVAL;
1572 if (size < sizeof(outarg))
1573 goto out_finish;
1574
1575 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1576 if (err)
1577 goto out_finish;
1578
1579 err = -EINVAL;
1580 if (size - sizeof(outarg) != outarg.size)
1581 goto out_finish;
1582
1583 nodeid = outarg.nodeid;
1584
1585 down_read(&fc->killsb);
1586
1587 err = -ENOENT;
1588 if (!fc->sb)
1589 goto out_up_killsb;
1590
1591 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1592 if (!inode)
1593 goto out_up_killsb;
1594
1595 mapping = inode->i_mapping;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001596 index = outarg.offset >> PAGE_SHIFT;
1597 offset = outarg.offset & ~PAGE_MASK;
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001598 file_size = i_size_read(inode);
1599 end = outarg.offset + outarg.size;
1600 if (end > file_size) {
1601 file_size = end;
1602 fuse_write_update_size(inode, file_size);
1603 }
1604
1605 num = outarg.size;
1606 while (num) {
1607 struct page *page;
1608 unsigned int this_num;
1609
1610 err = -ENOMEM;
1611 page = find_or_create_page(mapping, index,
1612 mapping_gfp_mask(mapping));
1613 if (!page)
1614 goto out_iput;
1615
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001616 this_num = min_t(unsigned, num, PAGE_SIZE - offset);
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001617 err = fuse_copy_page(cs, &page, offset, this_num, 0);
Miklos Szeredi063ec1e2014-01-22 19:36:58 +01001618 if (!err && offset == 0 &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001619 (this_num == PAGE_SIZE || file_size == end))
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001620 SetPageUptodate(page);
1621 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001622 put_page(page);
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001623
1624 if (err)
1625 goto out_iput;
1626
1627 num -= this_num;
1628 offset = 0;
1629 index++;
1630 }
1631
1632 err = 0;
1633
1634out_iput:
1635 iput(inode);
1636out_up_killsb:
1637 up_read(&fc->killsb);
1638out_finish:
1639 fuse_copy_finish(cs);
1640 return err;
1641}
1642
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001643static void fuse_retrieve_end(struct fuse_conn *fc, struct fuse_req *req)
1644{
Mel Gormanb745bc82014-06-04 16:10:22 -07001645 release_pages(req->pages, req->num_pages, false);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001646}
1647
1648static int fuse_retrieve(struct fuse_conn *fc, struct inode *inode,
1649 struct fuse_notify_retrieve_out *outarg)
1650{
1651 int err;
1652 struct address_space *mapping = inode->i_mapping;
1653 struct fuse_req *req;
1654 pgoff_t index;
1655 loff_t file_size;
1656 unsigned int num;
1657 unsigned int offset;
Geert Uytterhoeven01574432010-09-30 22:06:21 +02001658 size_t total_len = 0;
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001659 int num_pages;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001660
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001661 offset = outarg->offset & ~PAGE_MASK;
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001662 file_size = i_size_read(inode);
1663
1664 num = outarg->size;
1665 if (outarg->offset > file_size)
1666 num = 0;
1667 else if (outarg->offset + num > file_size)
1668 num = file_size - outarg->offset;
1669
1670 num_pages = (num + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1671 num_pages = min(num_pages, FUSE_MAX_PAGES_PER_REQ);
1672
1673 req = fuse_get_req(fc, num_pages);
1674 if (IS_ERR(req))
1675 return PTR_ERR(req);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001676
1677 req->in.h.opcode = FUSE_NOTIFY_REPLY;
1678 req->in.h.nodeid = outarg->nodeid;
1679 req->in.numargs = 2;
1680 req->in.argpages = 1;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001681 req->page_descs[0].offset = offset;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001682 req->end = fuse_retrieve_end;
1683
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001684 index = outarg->offset >> PAGE_SHIFT;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001685
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001686 while (num && req->num_pages < num_pages) {
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001687 struct page *page;
1688 unsigned int this_num;
1689
1690 page = find_get_page(mapping, index);
1691 if (!page)
1692 break;
1693
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001694 this_num = min_t(unsigned, num, PAGE_SIZE - offset);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001695 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001696 req->page_descs[req->num_pages].length = this_num;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001697 req->num_pages++;
1698
Miklos Szeredic9e67d42012-09-04 18:45:54 +02001699 offset = 0;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001700 num -= this_num;
1701 total_len += this_num;
Miklos Szeredi48706d02011-12-13 10:36:59 +01001702 index++;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001703 }
1704 req->misc.retrieve_in.offset = outarg->offset;
1705 req->misc.retrieve_in.size = total_len;
1706 req->in.args[0].size = sizeof(req->misc.retrieve_in);
1707 req->in.args[0].value = &req->misc.retrieve_in;
1708 req->in.args[1].size = total_len;
1709
1710 err = fuse_request_send_notify_reply(fc, req, outarg->notify_unique);
1711 if (err)
1712 fuse_retrieve_end(fc, req);
1713
1714 return err;
1715}
1716
1717static int fuse_notify_retrieve(struct fuse_conn *fc, unsigned int size,
1718 struct fuse_copy_state *cs)
1719{
1720 struct fuse_notify_retrieve_out outarg;
1721 struct inode *inode;
1722 int err;
1723
1724 err = -EINVAL;
1725 if (size != sizeof(outarg))
1726 goto copy_finish;
1727
1728 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1729 if (err)
1730 goto copy_finish;
1731
1732 fuse_copy_finish(cs);
1733
1734 down_read(&fc->killsb);
1735 err = -ENOENT;
1736 if (fc->sb) {
1737 u64 nodeid = outarg.nodeid;
1738
1739 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1740 if (inode) {
1741 err = fuse_retrieve(fc, inode, &outarg);
1742 iput(inode);
1743 }
1744 }
1745 up_read(&fc->killsb);
1746
1747 return err;
1748
1749copy_finish:
1750 fuse_copy_finish(cs);
1751 return err;
1752}
1753
Tejun Heo85993962008-11-26 12:03:55 +01001754static int fuse_notify(struct fuse_conn *fc, enum fuse_notify_code code,
1755 unsigned int size, struct fuse_copy_state *cs)
1756{
Miklos Szeredi0d278362015-02-26 11:45:47 +01001757 /* Don't try to move pages (yet) */
1758 cs->move_pages = 0;
1759
Tejun Heo85993962008-11-26 12:03:55 +01001760 switch (code) {
Tejun Heo95668a62008-11-26 12:03:55 +01001761 case FUSE_NOTIFY_POLL:
1762 return fuse_notify_poll(fc, size, cs);
1763
John Muir3b463ae2009-05-31 11:13:57 -04001764 case FUSE_NOTIFY_INVAL_INODE:
1765 return fuse_notify_inval_inode(fc, size, cs);
1766
1767 case FUSE_NOTIFY_INVAL_ENTRY:
1768 return fuse_notify_inval_entry(fc, size, cs);
1769
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001770 case FUSE_NOTIFY_STORE:
1771 return fuse_notify_store(fc, size, cs);
1772
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001773 case FUSE_NOTIFY_RETRIEVE:
1774 return fuse_notify_retrieve(fc, size, cs);
1775
John Muir451d0f52011-12-06 21:50:06 +01001776 case FUSE_NOTIFY_DELETE:
1777 return fuse_notify_delete(fc, size, cs);
1778
Tejun Heo85993962008-11-26 12:03:55 +01001779 default:
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001780 fuse_copy_finish(cs);
Tejun Heo85993962008-11-26 12:03:55 +01001781 return -EINVAL;
1782 }
1783}
1784
Miklos Szeredi334f4852005-09-09 13:10:27 -07001785/* Look up request on processing list by unique ID */
Miklos Szeredi3a2b5b92015-07-01 16:26:04 +02001786static struct fuse_req *request_find(struct fuse_pqueue *fpq, u64 unique)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001787{
Dong Fang05726ac2013-07-30 22:50:01 -04001788 struct fuse_req *req;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001789
Miklos Szeredi3a2b5b92015-07-01 16:26:04 +02001790 list_for_each_entry(req, &fpq->processing, list) {
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001791 if (req->in.h.unique == unique || req->intr_unique == unique)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001792 return req;
1793 }
1794 return NULL;
1795}
1796
1797static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
1798 unsigned nbytes)
1799{
1800 unsigned reqsize = sizeof(struct fuse_out_header);
1801
1802 if (out->h.error)
1803 return nbytes != reqsize ? -EINVAL : 0;
1804
1805 reqsize += len_args(out->numargs, out->args);
1806
1807 if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
1808 return -EINVAL;
1809 else if (reqsize > nbytes) {
1810 struct fuse_arg *lastarg = &out->args[out->numargs-1];
1811 unsigned diffsize = reqsize - nbytes;
1812 if (diffsize > lastarg->size)
1813 return -EINVAL;
1814 lastarg->size -= diffsize;
1815 }
1816 return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
1817 out->page_zeroing);
1818}
1819
1820/*
1821 * Write a single reply to a request. First the header is copied from
1822 * the write buffer. The request is then searched on the processing
1823 * list by the unique ID found in the header. If found, then remove
1824 * it from the list and copy the rest of the buffer to the request.
1825 * The request is finished by calling request_end()
1826 */
Miklos Szeredic36960462015-07-01 16:26:09 +02001827static ssize_t fuse_dev_do_write(struct fuse_dev *fud,
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001828 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001829{
1830 int err;
Miklos Szeredic36960462015-07-01 16:26:09 +02001831 struct fuse_conn *fc = fud->fc;
1832 struct fuse_pqueue *fpq = &fud->pq;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001833 struct fuse_req *req;
1834 struct fuse_out_header oh;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001835
Miklos Szeredi334f4852005-09-09 13:10:27 -07001836 if (nbytes < sizeof(struct fuse_out_header))
1837 return -EINVAL;
1838
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001839 err = fuse_copy_one(cs, &oh, sizeof(oh));
Miklos Szeredi334f4852005-09-09 13:10:27 -07001840 if (err)
1841 goto err_finish;
Tejun Heo85993962008-11-26 12:03:55 +01001842
Miklos Szeredi334f4852005-09-09 13:10:27 -07001843 err = -EINVAL;
Tejun Heo85993962008-11-26 12:03:55 +01001844 if (oh.len != nbytes)
1845 goto err_finish;
1846
1847 /*
1848 * Zero oh.unique indicates unsolicited notification message
1849 * and error contains notification code.
1850 */
1851 if (!oh.unique) {
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001852 err = fuse_notify(fc, oh.error, nbytes - sizeof(oh), cs);
Tejun Heo85993962008-11-26 12:03:55 +01001853 return err ? err : nbytes;
1854 }
1855
1856 err = -EINVAL;
1857 if (oh.error <= -1000 || oh.error > 0)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001858 goto err_finish;
1859
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001860 spin_lock(&fpq->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001861 err = -ENOENT;
Miklos Szeredie96edd92015-07-01 16:26:04 +02001862 if (!fpq->connected)
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001863 goto err_unlock_pq;
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001864
Miklos Szeredi3a2b5b92015-07-01 16:26:04 +02001865 req = request_find(fpq, oh.unique);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001866 if (!req)
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001867 goto err_unlock_pq;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001868
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001869 /* Is it an interrupt reply? */
1870 if (req->intr_unique == oh.unique) {
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001871 spin_unlock(&fpq->lock);
1872
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001873 err = -EINVAL;
1874 if (nbytes != sizeof(struct fuse_out_header))
Miklos Szeredi46c34a32015-07-01 16:26:07 +02001875 goto err_finish;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001876
1877 if (oh.error == -ENOSYS)
1878 fc->no_interrupt = 1;
1879 else if (oh.error == -EAGAIN)
Miklos Szeredif88996a2015-07-01 16:26:01 +02001880 queue_interrupt(&fc->iq, req);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001881
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001882 fuse_copy_finish(cs);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001883 return nbytes;
1884 }
1885
Miklos Szeredi33e14b42015-07-01 16:26:01 +02001886 clear_bit(FR_SENT, &req->flags);
Miklos Szeredi3a2b5b92015-07-01 16:26:04 +02001887 list_move(&req->list, &fpq->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001888 req->out.h = oh;
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001889 set_bit(FR_LOCKED, &req->flags);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001890 spin_unlock(&fpq->lock);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001891 cs->req = req;
Miklos Szeredice534fb2010-05-25 15:06:07 +02001892 if (!req->out.page_replace)
1893 cs->move_pages = 0;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001894
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001895 err = copy_out_args(cs, &req->out, nbytes);
1896 fuse_copy_finish(cs);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001897
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001898 spin_lock(&fpq->lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001899 clear_bit(FR_LOCKED, &req->flags);
Miklos Szeredie96edd92015-07-01 16:26:04 +02001900 if (!fpq->connected)
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +02001901 err = -ENOENT;
1902 else if (err)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001903 req->out.h.error = -EIO;
Miklos Szeredi77cd9d42015-07-01 16:26:06 +02001904 if (!test_bit(FR_PRIVATE, &req->flags))
1905 list_del_init(&req->list);
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001906 spin_unlock(&fpq->lock);
Miklos Szeredi46c34a32015-07-01 16:26:07 +02001907
Miklos Szeredi334f4852005-09-09 13:10:27 -07001908 request_end(fc, req);
1909
1910 return err ? err : nbytes;
1911
Miklos Szeredi45a91cb2015-07-01 16:26:06 +02001912 err_unlock_pq:
1913 spin_unlock(&fpq->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001914 err_finish:
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001915 fuse_copy_finish(cs);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001916 return err;
1917}
1918
Al Virofbdbacc2015-04-03 21:53:39 -04001919static ssize_t fuse_dev_write(struct kiocb *iocb, struct iov_iter *from)
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001920{
1921 struct fuse_copy_state cs;
Miklos Szeredicc080e92015-07-01 16:26:08 +02001922 struct fuse_dev *fud = fuse_get_dev(iocb->ki_filp);
1923
1924 if (!fud)
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001925 return -EPERM;
1926
Al Virofbdbacc2015-04-03 21:53:39 -04001927 if (!iter_is_iovec(from))
1928 return -EINVAL;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001929
Miklos Szeredidc008092015-07-01 16:25:58 +02001930 fuse_copy_init(&cs, 0, from);
Al Virofbdbacc2015-04-03 21:53:39 -04001931
Miklos Szeredic36960462015-07-01 16:26:09 +02001932 return fuse_dev_do_write(fud, &cs, iov_iter_count(from));
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001933}
1934
1935static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe,
1936 struct file *out, loff_t *ppos,
1937 size_t len, unsigned int flags)
1938{
1939 unsigned nbuf;
1940 unsigned idx;
1941 struct pipe_buffer *bufs;
1942 struct fuse_copy_state cs;
Miklos Szeredicc080e92015-07-01 16:26:08 +02001943 struct fuse_dev *fud;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001944 size_t rem;
1945 ssize_t ret;
1946
Miklos Szeredicc080e92015-07-01 16:26:08 +02001947 fud = fuse_get_dev(out);
1948 if (!fud)
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001949 return -EPERM;
1950
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001951 pipe_lock(pipe);
Andrey Ryabinin67a9e482018-07-17 19:00:33 +03001952
1953 bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), GFP_KERNEL);
1954 if (!bufs) {
1955 pipe_unlock(pipe);
1956 return -ENOMEM;
1957 }
1958
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001959 nbuf = 0;
1960 rem = 0;
1961 for (idx = 0; idx < pipe->nrbufs && rem < len; idx++)
1962 rem += pipe->bufs[(pipe->curbuf + idx) & (pipe->buffers - 1)].len;
1963
1964 ret = -EINVAL;
1965 if (rem < len) {
1966 pipe_unlock(pipe);
1967 goto out;
1968 }
1969
1970 rem = len;
1971 while (rem) {
1972 struct pipe_buffer *ibuf;
1973 struct pipe_buffer *obuf;
1974
1975 BUG_ON(nbuf >= pipe->buffers);
1976 BUG_ON(!pipe->nrbufs);
1977 ibuf = &pipe->bufs[pipe->curbuf];
1978 obuf = &bufs[nbuf];
1979
1980 if (rem >= ibuf->len) {
1981 *obuf = *ibuf;
1982 ibuf->ops = NULL;
1983 pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
1984 pipe->nrbufs--;
1985 } else {
Miklos Szeredi7bf2d1d2016-09-27 10:45:12 +02001986 pipe_buf_get(pipe, ibuf);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001987 *obuf = *ibuf;
1988 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1989 obuf->len = rem;
1990 ibuf->offset += obuf->len;
1991 ibuf->len -= obuf->len;
1992 }
1993 nbuf++;
1994 rem -= obuf->len;
1995 }
1996 pipe_unlock(pipe);
1997
Miklos Szeredidc008092015-07-01 16:25:58 +02001998 fuse_copy_init(&cs, 0, NULL);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001999 cs.pipebufs = bufs;
Al Viro6c09e942015-04-03 22:06:08 -04002000 cs.nr_segs = nbuf;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002001 cs.pipe = pipe;
2002
Miklos Szeredice534fb2010-05-25 15:06:07 +02002003 if (flags & SPLICE_F_MOVE)
2004 cs.move_pages = 1;
2005
Miklos Szeredic36960462015-07-01 16:26:09 +02002006 ret = fuse_dev_do_write(fud, &cs, len);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002007
Miklos Szeredia7796382016-09-27 10:45:12 +02002008 for (idx = 0; idx < nbuf; idx++)
2009 pipe_buf_release(pipe, &bufs[idx]);
2010
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002011out:
2012 kfree(bufs);
2013 return ret;
2014}
2015
Miklos Szeredi334f4852005-09-09 13:10:27 -07002016static unsigned fuse_dev_poll(struct file *file, poll_table *wait)
2017{
Miklos Szeredi334f4852005-09-09 13:10:27 -07002018 unsigned mask = POLLOUT | POLLWRNORM;
Miklos Szeredif88996a2015-07-01 16:26:01 +02002019 struct fuse_iqueue *fiq;
Miklos Szeredicc080e92015-07-01 16:26:08 +02002020 struct fuse_dev *fud = fuse_get_dev(file);
2021
2022 if (!fud)
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07002023 return POLLERR;
Miklos Szeredi334f4852005-09-09 13:10:27 -07002024
Miklos Szeredicc080e92015-07-01 16:26:08 +02002025 fiq = &fud->fc->iq;
Miklos Szeredif88996a2015-07-01 16:26:01 +02002026 poll_wait(file, &fiq->waitq, wait);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002027
Miklos Szeredi4ce60812015-07-01 16:26:02 +02002028 spin_lock(&fiq->waitq.lock);
Miklos Szeredie16714d2015-07-01 16:26:01 +02002029 if (!fiq->connected)
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07002030 mask = POLLERR;
Miklos Szeredif88996a2015-07-01 16:26:01 +02002031 else if (request_pending(fiq))
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07002032 mask |= POLLIN | POLLRDNORM;
Miklos Szeredi4ce60812015-07-01 16:26:02 +02002033 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002034
2035 return mask;
2036}
2037
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002038/*
2039 * Abort all requests on the given list (pending or processing)
2040 *
Miklos Szeredid7133112006-04-10 22:54:55 -07002041 * This function releases and reacquires fc->lock
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002042 */
Miklos Szeredi334f4852005-09-09 13:10:27 -07002043static void end_requests(struct fuse_conn *fc, struct list_head *head)
2044{
2045 while (!list_empty(head)) {
2046 struct fuse_req *req;
2047 req = list_entry(head->next, struct fuse_req, list);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002048 req->out.h.error = -ECONNABORTED;
Miklos Szeredi33e14b42015-07-01 16:26:01 +02002049 clear_bit(FR_SENT, &req->flags);
Miklos Szeredif377cb72015-07-01 16:26:04 +02002050 list_del_init(&req->list);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002051 request_end(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002052 }
2053}
2054
Bryan Green357ccf22011-03-01 16:43:52 -08002055static void end_polls(struct fuse_conn *fc)
2056{
2057 struct rb_node *p;
2058
2059 p = rb_first(&fc->polled_files);
2060
2061 while (p) {
2062 struct fuse_file *ff;
2063 ff = rb_entry(p, struct fuse_file, polled_node);
2064 wake_up_interruptible_all(&ff->poll_wait);
2065
2066 p = rb_next(p);
2067 }
2068}
2069
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002070/*
2071 * Abort all requests.
2072 *
Miklos Szeredib716d422015-07-01 16:25:59 +02002073 * Emergency exit in case of a malicious or accidental deadlock, or just a hung
2074 * filesystem.
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002075 *
Miklos Szeredib716d422015-07-01 16:25:59 +02002076 * The same effect is usually achievable through killing the filesystem daemon
2077 * and all users of the filesystem. The exception is the combination of an
2078 * asynchronous request and the tricky deadlock (see
2079 * Documentation/filesystems/fuse.txt).
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002080 *
Miklos Szeredib716d422015-07-01 16:25:59 +02002081 * Aborting requests under I/O goes as follows: 1: Separate out unlocked
2082 * requests, they should be finished off immediately. Locked requests will be
2083 * finished after unlock; see unlock_request(). 2: Finish off the unlocked
2084 * requests. It is possible that some request will finish before we can. This
2085 * is OK, the request will in that case be removed from the list before we touch
2086 * it.
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002087 */
2088void fuse_abort_conn(struct fuse_conn *fc)
2089{
Miklos Szeredif88996a2015-07-01 16:26:01 +02002090 struct fuse_iqueue *fiq = &fc->iq;
2091
Miklos Szeredid7133112006-04-10 22:54:55 -07002092 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002093 if (fc->connected) {
Miklos Szeredic36960462015-07-01 16:26:09 +02002094 struct fuse_dev *fud;
Miklos Szeredib716d422015-07-01 16:25:59 +02002095 struct fuse_req *req, *next;
Miklos Szeredi41f98272015-07-01 16:25:59 +02002096 LIST_HEAD(to_end1);
2097 LIST_HEAD(to_end2);
Miklos Szeredib716d422015-07-01 16:25:59 +02002098
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002099 fc->connected = 0;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -07002100 fc->blocked = 0;
Miklos Szeredi9759bd512015-01-06 10:45:35 +01002101 fuse_set_initialized(fc);
Miklos Szeredic36960462015-07-01 16:26:09 +02002102 list_for_each_entry(fud, &fc->devices, entry) {
2103 struct fuse_pqueue *fpq = &fud->pq;
2104
2105 spin_lock(&fpq->lock);
2106 fpq->connected = 0;
2107 list_for_each_entry_safe(req, next, &fpq->io, list) {
2108 req->out.h.error = -ECONNABORTED;
2109 spin_lock(&req->waitq.lock);
2110 set_bit(FR_ABORTED, &req->flags);
2111 if (!test_bit(FR_LOCKED, &req->flags)) {
2112 set_bit(FR_PRIVATE, &req->flags);
Miklos Szeredi501c4ca2018-07-26 16:13:11 +02002113 __fuse_get_request(req);
Miklos Szeredic36960462015-07-01 16:26:09 +02002114 list_move(&req->list, &to_end1);
2115 }
2116 spin_unlock(&req->waitq.lock);
Miklos Szeredi77cd9d42015-07-01 16:26:06 +02002117 }
Miklos Szeredic36960462015-07-01 16:26:09 +02002118 list_splice_init(&fpq->processing, &to_end2);
2119 spin_unlock(&fpq->lock);
Miklos Szeredib716d422015-07-01 16:25:59 +02002120 }
Miklos Szeredi41f98272015-07-01 16:25:59 +02002121 fc->max_background = UINT_MAX;
2122 flush_bg_queue(fc);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002123
Miklos Szeredi4ce60812015-07-01 16:26:02 +02002124 spin_lock(&fiq->waitq.lock);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002125 fiq->connected = 0;
Miklos Szeredif88996a2015-07-01 16:26:01 +02002126 list_splice_init(&fiq->pending, &to_end2);
Tahsin Erdogan0181b362017-01-12 12:04:04 -08002127 list_for_each_entry(req, &to_end2, list)
2128 clear_bit(FR_PENDING, &req->flags);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002129 while (forget_pending(fiq))
2130 kfree(dequeue_forget(fiq, 1, NULL));
Miklos Szeredi4ce60812015-07-01 16:26:02 +02002131 wake_up_all_locked(&fiq->waitq);
2132 spin_unlock(&fiq->waitq.lock);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002133 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
Miklos Szerediee314a82015-07-01 16:26:08 +02002134 end_polls(fc);
2135 wake_up_all(&fc->blocked_waitq);
2136 spin_unlock(&fc->lock);
Miklos Szeredi8c911892015-07-01 16:26:02 +02002137
Miklos Szeredi41f98272015-07-01 16:25:59 +02002138 while (!list_empty(&to_end1)) {
2139 req = list_first_entry(&to_end1, struct fuse_req, list);
Miklos Szeredif377cb72015-07-01 16:26:04 +02002140 list_del_init(&req->list);
Miklos Szeredib716d422015-07-01 16:25:59 +02002141 request_end(fc, req);
Miklos Szeredib716d422015-07-01 16:25:59 +02002142 }
Miklos Szeredi41f98272015-07-01 16:25:59 +02002143 end_requests(fc, &to_end2);
Miklos Szerediee314a82015-07-01 16:26:08 +02002144 } else {
2145 spin_unlock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002146 }
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002147}
Tejun Heo08cbf542009-04-14 10:54:53 +09002148EXPORT_SYMBOL_GPL(fuse_abort_conn);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002149
Miklos Szeredi6465d762018-07-26 16:13:11 +02002150void fuse_wait_aborted(struct fuse_conn *fc)
2151{
2152 wait_event(fc->blocked_waitq, atomic_read(&fc->num_waiting) == 0);
2153}
2154
Tejun Heo08cbf542009-04-14 10:54:53 +09002155int fuse_dev_release(struct inode *inode, struct file *file)
Miklos Szeredi334f4852005-09-09 13:10:27 -07002156{
Miklos Szeredicc080e92015-07-01 16:26:08 +02002157 struct fuse_dev *fud = fuse_get_dev(file);
2158
2159 if (fud) {
2160 struct fuse_conn *fc = fud->fc;
Miklos Szeredic36960462015-07-01 16:26:09 +02002161 struct fuse_pqueue *fpq = &fud->pq;
Miklos Szeredic66025c2018-07-26 16:13:11 +02002162 LIST_HEAD(to_end);
Miklos Szeredicc080e92015-07-01 16:26:08 +02002163
Miklos Szeredic66025c2018-07-26 16:13:11 +02002164 spin_lock(&fpq->lock);
Miklos Szeredic36960462015-07-01 16:26:09 +02002165 WARN_ON(!list_empty(&fpq->io));
Miklos Szeredic66025c2018-07-26 16:13:11 +02002166 list_splice_init(&fpq->processing, &to_end);
2167 spin_unlock(&fpq->lock);
2168
2169 end_requests(fc, &to_end);
2170
Miklos Szeredic36960462015-07-01 16:26:09 +02002171 /* Are we the last open device? */
2172 if (atomic_dec_and_test(&fc->dev_count)) {
2173 WARN_ON(fc->iq.fasync != NULL);
2174 fuse_abort_conn(fc);
2175 }
Miklos Szeredicc080e92015-07-01 16:26:08 +02002176 fuse_dev_free(fud);
Jeff Dike385a17b2006-04-10 22:54:52 -07002177 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07002178 return 0;
2179}
Tejun Heo08cbf542009-04-14 10:54:53 +09002180EXPORT_SYMBOL_GPL(fuse_dev_release);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002181
Jeff Dike385a17b2006-04-10 22:54:52 -07002182static int fuse_dev_fasync(int fd, struct file *file, int on)
2183{
Miklos Szeredicc080e92015-07-01 16:26:08 +02002184 struct fuse_dev *fud = fuse_get_dev(file);
2185
2186 if (!fud)
Miklos Szeredia87046d2006-04-10 22:54:56 -07002187 return -EPERM;
Jeff Dike385a17b2006-04-10 22:54:52 -07002188
2189 /* No locking - fasync_helper does its own locking */
Miklos Szeredicc080e92015-07-01 16:26:08 +02002190 return fasync_helper(fd, file, on, &fud->fc->iq.fasync);
Jeff Dike385a17b2006-04-10 22:54:52 -07002191}
2192
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002193static int fuse_device_clone(struct fuse_conn *fc, struct file *new)
2194{
Miklos Szeredicc080e92015-07-01 16:26:08 +02002195 struct fuse_dev *fud;
2196
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002197 if (new->private_data)
2198 return -EINVAL;
2199
Miklos Szeredicc080e92015-07-01 16:26:08 +02002200 fud = fuse_dev_alloc(fc);
2201 if (!fud)
2202 return -ENOMEM;
2203
2204 new->private_data = fud;
Miklos Szeredic36960462015-07-01 16:26:09 +02002205 atomic_inc(&fc->dev_count);
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002206
2207 return 0;
2208}
2209
2210static long fuse_dev_ioctl(struct file *file, unsigned int cmd,
2211 unsigned long arg)
2212{
2213 int err = -ENOTTY;
2214
2215 if (cmd == FUSE_DEV_IOC_CLONE) {
2216 int oldfd;
2217
2218 err = -EFAULT;
2219 if (!get_user(oldfd, (__u32 __user *) arg)) {
2220 struct file *old = fget(oldfd);
2221
2222 err = -EINVAL;
2223 if (old) {
Jann Horn8ed1f0e2015-08-16 20:27:01 +02002224 struct fuse_dev *fud = NULL;
2225
2226 /*
2227 * Check against file->f_op because CUSE
2228 * uses the same ioctl handler.
2229 */
2230 if (old->f_op == file->f_op &&
2231 old->f_cred->user_ns == file->f_cred->user_ns)
2232 fud = fuse_get_dev(old);
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002233
Miklos Szeredicc080e92015-07-01 16:26:08 +02002234 if (fud) {
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002235 mutex_lock(&fuse_mutex);
Miklos Szeredicc080e92015-07-01 16:26:08 +02002236 err = fuse_device_clone(fud->fc, file);
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002237 mutex_unlock(&fuse_mutex);
2238 }
2239 fput(old);
2240 }
2241 }
2242 }
2243 return err;
2244}
2245
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002246const struct file_operations fuse_dev_operations = {
Miklos Szeredi334f4852005-09-09 13:10:27 -07002247 .owner = THIS_MODULE,
Tom Van Braeckel94e4fe22015-01-12 05:22:16 +01002248 .open = fuse_dev_open,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002249 .llseek = no_llseek,
Al Virofbdbacc2015-04-03 21:53:39 -04002250 .read_iter = fuse_dev_read,
Miklos Szeredic3021622010-05-25 15:06:07 +02002251 .splice_read = fuse_dev_splice_read,
Al Virofbdbacc2015-04-03 21:53:39 -04002252 .write_iter = fuse_dev_write,
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002253 .splice_write = fuse_dev_splice_write,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002254 .poll = fuse_dev_poll,
2255 .release = fuse_dev_release,
Jeff Dike385a17b2006-04-10 22:54:52 -07002256 .fasync = fuse_dev_fasync,
Miklos Szeredi00c570f2015-07-01 16:26:08 +02002257 .unlocked_ioctl = fuse_dev_ioctl,
2258 .compat_ioctl = fuse_dev_ioctl,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002259};
Tejun Heo08cbf542009-04-14 10:54:53 +09002260EXPORT_SYMBOL_GPL(fuse_dev_operations);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002261
2262static struct miscdevice fuse_miscdevice = {
2263 .minor = FUSE_MINOR,
2264 .name = "fuse",
2265 .fops = &fuse_dev_operations,
2266};
2267
2268int __init fuse_dev_init(void)
2269{
2270 int err = -ENOMEM;
2271 fuse_req_cachep = kmem_cache_create("fuse_request",
2272 sizeof(struct fuse_req),
Paul Mundt20c2df82007-07-20 10:11:58 +09002273 0, 0, NULL);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002274 if (!fuse_req_cachep)
2275 goto out;
2276
2277 err = misc_register(&fuse_miscdevice);
2278 if (err)
2279 goto out_cache_clean;
2280
2281 return 0;
2282
2283 out_cache_clean:
2284 kmem_cache_destroy(fuse_req_cachep);
2285 out:
2286 return err;
2287}
2288
2289void fuse_dev_cleanup(void)
2290{
2291 misc_deregister(&fuse_miscdevice);
2292 kmem_cache_destroy(fuse_req_cachep);
2293}