blob: 9e0ed3e714cb215e8e87c574b615b8aed9694861 [file] [log] [blame]
Miklos Szeredi334f4852005-09-09 13:10:27 -07001/*
2 FUSE: Filesystem in Userspace
Miklos Szeredi1729a162008-11-26 12:03:54 +01003 Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu>
Miklos Szeredi334f4852005-09-09 13:10:27 -07004
5 This program can be distributed under the terms of the GNU GPL.
6 See the file COPYING.
7*/
8
9#include "fuse_i.h"
10
11#include <linux/init.h>
12#include <linux/module.h>
13#include <linux/poll.h>
14#include <linux/uio.h>
15#include <linux/miscdevice.h>
16#include <linux/pagemap.h>
17#include <linux/file.h>
18#include <linux/slab.h>
Miklos Szeredidd3bb142010-05-25 15:06:06 +020019#include <linux/pipe_fs_i.h>
Miklos Szeredice534fb2010-05-25 15:06:07 +020020#include <linux/swap.h>
21#include <linux/splice.h>
Miklos Szeredi334f4852005-09-09 13:10:27 -070022
23MODULE_ALIAS_MISCDEV(FUSE_MINOR);
Kay Sievers578454f2010-05-20 18:07:20 +020024MODULE_ALIAS("devname:fuse");
Miklos Szeredi334f4852005-09-09 13:10:27 -070025
Christoph Lametere18b8902006-12-06 20:33:20 -080026static struct kmem_cache *fuse_req_cachep;
Miklos Szeredi334f4852005-09-09 13:10:27 -070027
Miklos Szeredi8bfc0162006-01-16 22:14:28 -080028static struct fuse_conn *fuse_get_conn(struct file *file)
Miklos Szeredi334f4852005-09-09 13:10:27 -070029{
Miklos Szeredi0720b312006-04-10 22:54:55 -070030 /*
31 * Lockless access is OK, because file->private data is set
32 * once during mount and is valid until the file is released.
33 */
34 return file->private_data;
Miklos Szeredi334f4852005-09-09 13:10:27 -070035}
36
Maxim Patlasov4250c062012-10-26 19:48:07 +040037static void fuse_request_init(struct fuse_req *req, struct page **pages,
Maxim Patlasovb2430d72012-10-26 19:49:24 +040038 struct fuse_page_desc *page_descs,
Maxim Patlasov4250c062012-10-26 19:48:07 +040039 unsigned npages)
Miklos Szeredi334f4852005-09-09 13:10:27 -070040{
41 memset(req, 0, sizeof(*req));
Maxim Patlasov4250c062012-10-26 19:48:07 +040042 memset(pages, 0, sizeof(*pages) * npages);
Maxim Patlasovb2430d72012-10-26 19:49:24 +040043 memset(page_descs, 0, sizeof(*page_descs) * npages);
Miklos Szeredi334f4852005-09-09 13:10:27 -070044 INIT_LIST_HEAD(&req->list);
Miklos Szeredia4d27e72006-06-25 05:48:54 -070045 INIT_LIST_HEAD(&req->intr_entry);
Miklos Szeredi334f4852005-09-09 13:10:27 -070046 init_waitqueue_head(&req->waitq);
47 atomic_set(&req->count, 1);
Maxim Patlasov4250c062012-10-26 19:48:07 +040048 req->pages = pages;
Maxim Patlasovb2430d72012-10-26 19:49:24 +040049 req->page_descs = page_descs;
Maxim Patlasov4250c062012-10-26 19:48:07 +040050 req->max_pages = npages;
Miklos Szeredi334f4852005-09-09 13:10:27 -070051}
52
Maxim Patlasov4250c062012-10-26 19:48:07 +040053static struct fuse_req *__fuse_request_alloc(unsigned npages, gfp_t flags)
Miklos Szeredi334f4852005-09-09 13:10:27 -070054{
Maxim Patlasov4250c062012-10-26 19:48:07 +040055 struct fuse_req *req = kmem_cache_alloc(fuse_req_cachep, flags);
56 if (req) {
57 struct page **pages;
Maxim Patlasovb2430d72012-10-26 19:49:24 +040058 struct fuse_page_desc *page_descs;
Maxim Patlasov4250c062012-10-26 19:48:07 +040059
Maxim Patlasovb2430d72012-10-26 19:49:24 +040060 if (npages <= FUSE_REQ_INLINE_PAGES) {
Maxim Patlasov4250c062012-10-26 19:48:07 +040061 pages = req->inline_pages;
Maxim Patlasovb2430d72012-10-26 19:49:24 +040062 page_descs = req->inline_page_descs;
63 } else {
Maxim Patlasov4250c062012-10-26 19:48:07 +040064 pages = kmalloc(sizeof(struct page *) * npages, flags);
Maxim Patlasovb2430d72012-10-26 19:49:24 +040065 page_descs = kmalloc(sizeof(struct fuse_page_desc) *
66 npages, flags);
67 }
Maxim Patlasov4250c062012-10-26 19:48:07 +040068
Maxim Patlasovb2430d72012-10-26 19:49:24 +040069 if (!pages || !page_descs) {
70 kfree(pages);
71 kfree(page_descs);
Maxim Patlasov4250c062012-10-26 19:48:07 +040072 kmem_cache_free(fuse_req_cachep, req);
73 return NULL;
74 }
75
Maxim Patlasovb2430d72012-10-26 19:49:24 +040076 fuse_request_init(req, pages, page_descs, npages);
Maxim Patlasov4250c062012-10-26 19:48:07 +040077 }
Miklos Szeredi334f4852005-09-09 13:10:27 -070078 return req;
79}
Maxim Patlasov4250c062012-10-26 19:48:07 +040080
81struct fuse_req *fuse_request_alloc(unsigned npages)
82{
83 return __fuse_request_alloc(npages, GFP_KERNEL);
84}
Tejun Heo08cbf542009-04-14 10:54:53 +090085EXPORT_SYMBOL_GPL(fuse_request_alloc);
Miklos Szeredi334f4852005-09-09 13:10:27 -070086
Maxim Patlasov4250c062012-10-26 19:48:07 +040087struct fuse_req *fuse_request_alloc_nofs(unsigned npages)
Miklos Szeredi3be5a522008-04-30 00:54:41 -070088{
Maxim Patlasov4250c062012-10-26 19:48:07 +040089 return __fuse_request_alloc(npages, GFP_NOFS);
Miklos Szeredi3be5a522008-04-30 00:54:41 -070090}
91
Miklos Szeredi334f4852005-09-09 13:10:27 -070092void fuse_request_free(struct fuse_req *req)
93{
Maxim Patlasovb2430d72012-10-26 19:49:24 +040094 if (req->pages != req->inline_pages) {
Maxim Patlasov4250c062012-10-26 19:48:07 +040095 kfree(req->pages);
Maxim Patlasovb2430d72012-10-26 19:49:24 +040096 kfree(req->page_descs);
97 }
Miklos Szeredi334f4852005-09-09 13:10:27 -070098 kmem_cache_free(fuse_req_cachep, req);
99}
100
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800101static void block_sigs(sigset_t *oldset)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700102{
103 sigset_t mask;
104
105 siginitsetinv(&mask, sigmask(SIGKILL));
106 sigprocmask(SIG_BLOCK, &mask, oldset);
107}
108
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800109static void restore_sigs(sigset_t *oldset)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700110{
111 sigprocmask(SIG_SETMASK, oldset, NULL);
112}
113
Maxim Patlasov36cf66e2012-12-14 19:20:51 +0400114void __fuse_get_request(struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700115{
116 atomic_inc(&req->count);
117}
118
119/* Must be called with > 1 refcount */
120static void __fuse_put_request(struct fuse_req *req)
121{
122 BUG_ON(atomic_read(&req->count) < 2);
123 atomic_dec(&req->count);
124}
125
Miklos Szeredi33649c92006-06-25 05:48:52 -0700126static void fuse_req_init_context(struct fuse_req *req)
127{
Eric W. Biederman499dcf22012-02-07 16:26:03 -0800128 req->in.h.uid = from_kuid_munged(&init_user_ns, current_fsuid());
129 req->in.h.gid = from_kgid_munged(&init_user_ns, current_fsgid());
Miklos Szeredi33649c92006-06-25 05:48:52 -0700130 req->in.h.pid = current->pid;
131}
132
Miklos Szeredi9759bd512015-01-06 10:45:35 +0100133void fuse_set_initialized(struct fuse_conn *fc)
134{
135 /* Make sure stores before this are seen on another CPU */
136 smp_wmb();
137 fc->initialized = 1;
138}
139
Maxim Patlasov0aada882013-03-21 18:02:28 +0400140static bool fuse_block_alloc(struct fuse_conn *fc, bool for_background)
141{
142 return !fc->initialized || (for_background && fc->blocked);
143}
144
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400145static struct fuse_req *__fuse_get_req(struct fuse_conn *fc, unsigned npages,
146 bool for_background)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700147{
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700148 struct fuse_req *req;
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700149 int err;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200150 atomic_inc(&fc->num_waiting);
Maxim Patlasov0aada882013-03-21 18:02:28 +0400151
152 if (fuse_block_alloc(fc, for_background)) {
153 sigset_t oldset;
154 int intr;
155
156 block_sigs(&oldset);
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400157 intr = wait_event_interruptible_exclusive(fc->blocked_waitq,
Maxim Patlasov0aada882013-03-21 18:02:28 +0400158 !fuse_block_alloc(fc, for_background));
159 restore_sigs(&oldset);
160 err = -EINTR;
161 if (intr)
162 goto out;
163 }
Miklos Szeredi9759bd512015-01-06 10:45:35 +0100164 /* Matches smp_wmb() in fuse_set_initialized() */
165 smp_rmb();
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700166
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700167 err = -ENOTCONN;
168 if (!fc->connected)
169 goto out;
170
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400171 req = fuse_request_alloc(npages);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200172 err = -ENOMEM;
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400173 if (!req) {
174 if (for_background)
175 wake_up(&fc->blocked_waitq);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200176 goto out;
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400177 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700178
Miklos Szeredi33649c92006-06-25 05:48:52 -0700179 fuse_req_init_context(req);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200180 req->waiting = 1;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400181 req->background = for_background;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700182 return req;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200183
184 out:
185 atomic_dec(&fc->num_waiting);
186 return ERR_PTR(err);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700187}
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400188
189struct fuse_req *fuse_get_req(struct fuse_conn *fc, unsigned npages)
190{
191 return __fuse_get_req(fc, npages, false);
192}
Tejun Heo08cbf542009-04-14 10:54:53 +0900193EXPORT_SYMBOL_GPL(fuse_get_req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700194
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400195struct fuse_req *fuse_get_req_for_background(struct fuse_conn *fc,
196 unsigned npages)
197{
198 return __fuse_get_req(fc, npages, true);
199}
200EXPORT_SYMBOL_GPL(fuse_get_req_for_background);
201
Miklos Szeredi33649c92006-06-25 05:48:52 -0700202/*
203 * Return request in fuse_file->reserved_req. However that may
204 * currently be in use. If that is the case, wait for it to become
205 * available.
206 */
207static struct fuse_req *get_reserved_req(struct fuse_conn *fc,
208 struct file *file)
209{
210 struct fuse_req *req = NULL;
211 struct fuse_file *ff = file->private_data;
212
213 do {
Miklos Szeredide5e3de2007-10-16 23:31:00 -0700214 wait_event(fc->reserved_req_waitq, ff->reserved_req);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700215 spin_lock(&fc->lock);
216 if (ff->reserved_req) {
217 req = ff->reserved_req;
218 ff->reserved_req = NULL;
Al Virocb0942b2012-08-27 14:48:26 -0400219 req->stolen_file = get_file(file);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700220 }
221 spin_unlock(&fc->lock);
222 } while (!req);
223
224 return req;
225}
226
227/*
228 * Put stolen request back into fuse_file->reserved_req
229 */
230static void put_reserved_req(struct fuse_conn *fc, struct fuse_req *req)
231{
232 struct file *file = req->stolen_file;
233 struct fuse_file *ff = file->private_data;
234
235 spin_lock(&fc->lock);
Maxim Patlasovb2430d72012-10-26 19:49:24 +0400236 fuse_request_init(req, req->pages, req->page_descs, req->max_pages);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700237 BUG_ON(ff->reserved_req);
238 ff->reserved_req = req;
Miklos Szeredide5e3de2007-10-16 23:31:00 -0700239 wake_up_all(&fc->reserved_req_waitq);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700240 spin_unlock(&fc->lock);
241 fput(file);
242}
243
244/*
245 * Gets a requests for a file operation, always succeeds
246 *
247 * This is used for sending the FLUSH request, which must get to
248 * userspace, due to POSIX locks which may need to be unlocked.
249 *
250 * If allocation fails due to OOM, use the reserved request in
251 * fuse_file.
252 *
253 * This is very unlikely to deadlock accidentally, since the
254 * filesystem should not have it's own file open. If deadlock is
255 * intentional, it can still be broken by "aborting" the filesystem.
256 */
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400257struct fuse_req *fuse_get_req_nofail_nopages(struct fuse_conn *fc,
258 struct file *file)
Miklos Szeredi33649c92006-06-25 05:48:52 -0700259{
260 struct fuse_req *req;
261
262 atomic_inc(&fc->num_waiting);
Maxim Patlasov0aada882013-03-21 18:02:28 +0400263 wait_event(fc->blocked_waitq, fc->initialized);
Miklos Szeredi9759bd512015-01-06 10:45:35 +0100264 /* Matches smp_wmb() in fuse_set_initialized() */
265 smp_rmb();
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400266 req = fuse_request_alloc(0);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700267 if (!req)
268 req = get_reserved_req(fc, file);
269
270 fuse_req_init_context(req);
271 req->waiting = 1;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400272 req->background = 0;
Miklos Szeredi33649c92006-06-25 05:48:52 -0700273 return req;
274}
275
Miklos Szeredi334f4852005-09-09 13:10:27 -0700276void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req)
277{
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800278 if (atomic_dec_and_test(&req->count)) {
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400279 if (unlikely(req->background)) {
280 /*
281 * We get here in the unlikely case that a background
282 * request was allocated but not sent
283 */
284 spin_lock(&fc->lock);
285 if (!fc->blocked)
286 wake_up(&fc->blocked_waitq);
287 spin_unlock(&fc->lock);
288 }
289
Miklos Szeredi73e0e732015-07-01 16:25:56 +0200290 if (req->waiting) {
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200291 atomic_dec(&fc->num_waiting);
Miklos Szeredi73e0e732015-07-01 16:25:56 +0200292 req->waiting = 0;
293 }
Miklos Szeredi33649c92006-06-25 05:48:52 -0700294
295 if (req->stolen_file)
296 put_reserved_req(fc, req);
297 else
298 fuse_request_free(req);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800299 }
300}
Tejun Heo08cbf542009-04-14 10:54:53 +0900301EXPORT_SYMBOL_GPL(fuse_put_request);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800302
Miklos Szeredid12def12008-02-06 01:38:39 -0800303static unsigned len_args(unsigned numargs, struct fuse_arg *args)
304{
305 unsigned nbytes = 0;
306 unsigned i;
307
308 for (i = 0; i < numargs; i++)
309 nbytes += args[i].size;
310
311 return nbytes;
312}
313
314static u64 fuse_get_unique(struct fuse_conn *fc)
315{
316 fc->reqctr++;
317 /* zero is special */
318 if (fc->reqctr == 0)
319 fc->reqctr = 1;
320
321 return fc->reqctr;
322}
323
324static void queue_request(struct fuse_conn *fc, struct fuse_req *req)
325{
Miklos Szeredid12def12008-02-06 01:38:39 -0800326 req->in.h.len = sizeof(struct fuse_in_header) +
327 len_args(req->in.numargs, (struct fuse_arg *) req->in.args);
328 list_add_tail(&req->list, &fc->pending);
329 req->state = FUSE_REQ_PENDING;
Miklos Szeredid12def12008-02-06 01:38:39 -0800330 wake_up(&fc->waitq);
331 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
332}
333
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100334void fuse_queue_forget(struct fuse_conn *fc, struct fuse_forget_link *forget,
335 u64 nodeid, u64 nlookup)
336{
Miklos Szeredi02c048b2010-12-07 20:16:56 +0100337 forget->forget_one.nodeid = nodeid;
338 forget->forget_one.nlookup = nlookup;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100339
340 spin_lock(&fc->lock);
Miklos Szeredi5dfcc872011-09-12 09:38:03 +0200341 if (fc->connected) {
342 fc->forget_list_tail->next = forget;
343 fc->forget_list_tail = forget;
344 wake_up(&fc->waitq);
345 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
346 } else {
347 kfree(forget);
348 }
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100349 spin_unlock(&fc->lock);
350}
351
Miklos Szeredid12def12008-02-06 01:38:39 -0800352static void flush_bg_queue(struct fuse_conn *fc)
353{
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700354 while (fc->active_background < fc->max_background &&
Miklos Szeredid12def12008-02-06 01:38:39 -0800355 !list_empty(&fc->bg_queue)) {
356 struct fuse_req *req;
357
358 req = list_entry(fc->bg_queue.next, struct fuse_req, list);
359 list_del(&req->list);
360 fc->active_background++;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200361 req->in.h.unique = fuse_get_unique(fc);
Miklos Szeredid12def12008-02-06 01:38:39 -0800362 queue_request(fc, req);
363 }
364}
365
Miklos Szeredi6dbbcb12006-04-26 10:49:06 +0200366/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700367 * This function is called when a request is finished. Either a reply
Miklos Szeredif9a28422006-06-25 05:48:53 -0700368 * has arrived or it was aborted (and not yet sent) or some error
Miklos Szeredif43b1552006-01-16 22:14:26 -0800369 * occurred during communication with userspace, or the device file
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700370 * was closed. The requester thread is woken up (if still waiting),
371 * the 'end' callback is called if given, else the reference to the
372 * request is released
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800373 *
Miklos Szeredid7133112006-04-10 22:54:55 -0700374 * Called with fc->lock, unlocks it
Miklos Szeredi334f4852005-09-09 13:10:27 -0700375 */
376static void request_end(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +0200377__releases(fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700378{
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700379 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
380 req->end = NULL;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800381 list_del(&req->list);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700382 list_del(&req->intr_entry);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800383 req->state = FUSE_REQ_FINISHED;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700384 if (req->background) {
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400385 req->background = 0;
386
387 if (fc->num_background == fc->max_background)
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700388 fc->blocked = 0;
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400389
390 /* Wake up next waiter, if any */
Miklos Szeredi3c18ef82013-04-17 21:50:58 +0200391 if (!fc->blocked && waitqueue_active(&fc->blocked_waitq))
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400392 wake_up(&fc->blocked_waitq);
393
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700394 if (fc->num_background == fc->congestion_threshold &&
Tejun Heoa325f9b2009-04-14 10:54:52 +0900395 fc->connected && fc->bdi_initialized) {
Jens Axboe8aa7e842009-07-09 14:52:32 +0200396 clear_bdi_congested(&fc->bdi, BLK_RW_SYNC);
397 clear_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
Miklos Szeredif92b99b2007-10-16 23:30:59 -0700398 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700399 fc->num_background--;
Miklos Szeredid12def12008-02-06 01:38:39 -0800400 fc->active_background--;
401 flush_bg_queue(fc);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700402 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700403 spin_unlock(&fc->lock);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700404 wake_up(&req->waitq);
405 if (end)
406 end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100407 fuse_put_request(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700408}
409
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700410static void wait_answer_interruptible(struct fuse_conn *fc,
411 struct fuse_req *req)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +0200412__releases(fc->lock)
413__acquires(fc->lock)
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700414{
415 if (signal_pending(current))
416 return;
417
418 spin_unlock(&fc->lock);
419 wait_event_interruptible(req->waitq, req->state == FUSE_REQ_FINISHED);
420 spin_lock(&fc->lock);
421}
422
423static void queue_interrupt(struct fuse_conn *fc, struct fuse_req *req)
424{
425 list_add_tail(&req->intr_entry, &fc->interrupts);
426 wake_up(&fc->waitq);
427 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
428}
429
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700430static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +0200431__releases(fc->lock)
432__acquires(fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700433{
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700434 if (!fc->no_interrupt) {
435 /* Any signal may interrupt this */
436 wait_answer_interruptible(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700437
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700438 if (req->aborted)
439 goto aborted;
440 if (req->state == FUSE_REQ_FINISHED)
441 return;
442
443 req->interrupted = 1;
444 if (req->state == FUSE_REQ_SENT)
445 queue_interrupt(fc, req);
446 }
447
Miklos Szeredia131de02007-10-16 23:31:04 -0700448 if (!req->force) {
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700449 sigset_t oldset;
450
451 /* Only fatal signals may interrupt this */
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700452 block_sigs(&oldset);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700453 wait_answer_interruptible(fc, req);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700454 restore_sigs(&oldset);
Miklos Szeredia131de02007-10-16 23:31:04 -0700455
456 if (req->aborted)
457 goto aborted;
458 if (req->state == FUSE_REQ_FINISHED)
459 return;
460
461 /* Request is not yet in userspace, bail out */
462 if (req->state == FUSE_REQ_PENDING) {
463 list_del(&req->list);
464 __fuse_put_request(req);
465 req->out.h.error = -EINTR;
466 return;
467 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700468 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700469
Miklos Szeredia131de02007-10-16 23:31:04 -0700470 /*
471 * Either request is already in userspace, or it was forced.
472 * Wait it out.
473 */
474 spin_unlock(&fc->lock);
475 wait_event(req->waitq, req->state == FUSE_REQ_FINISHED);
476 spin_lock(&fc->lock);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700477
Miklos Szeredia131de02007-10-16 23:31:04 -0700478 if (!req->aborted)
479 return;
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700480
481 aborted:
Miklos Szeredia131de02007-10-16 23:31:04 -0700482 BUG_ON(req->state != FUSE_REQ_FINISHED);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700483 if (req->locked) {
484 /* This is uninterruptible sleep, because data is
485 being copied to/from the buffers of req. During
486 locked state, there mustn't be any filesystem
487 operation (e.g. page fault), since that could lead
488 to deadlock */
Miklos Szeredid7133112006-04-10 22:54:55 -0700489 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700490 wait_event(req->waitq, !req->locked);
Miklos Szeredid7133112006-04-10 22:54:55 -0700491 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700492 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700493}
494
Eric Wong6a4e9222013-02-04 13:04:44 +0000495static void __fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700496{
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400497 BUG_ON(req->background);
Miklos Szeredid7133112006-04-10 22:54:55 -0700498 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700499 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700500 req->out.h.error = -ENOTCONN;
501 else if (fc->conn_error)
502 req->out.h.error = -ECONNREFUSED;
503 else {
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200504 req->in.h.unique = fuse_get_unique(fc);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700505 queue_request(fc, req);
506 /* acquire extra reference, since request is still needed
507 after request_end() */
508 __fuse_get_request(req);
509
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700510 request_wait_answer(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700511 }
Miklos Szeredid7133112006-04-10 22:54:55 -0700512 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700513}
Eric Wong6a4e9222013-02-04 13:04:44 +0000514
515void fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
516{
517 req->isreply = 1;
Miklos Szeredi5437f242015-07-01 16:25:56 +0200518 if (!req->waiting) {
519 req->waiting = 1;
520 atomic_inc(&fc->num_waiting);
521 }
Eric Wong6a4e9222013-02-04 13:04:44 +0000522 __fuse_request_send(fc, req);
523}
Tejun Heo08cbf542009-04-14 10:54:53 +0900524EXPORT_SYMBOL_GPL(fuse_request_send);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700525
Miklos Szeredi21f62172015-01-06 10:45:35 +0100526static void fuse_adjust_compat(struct fuse_conn *fc, struct fuse_args *args)
527{
528 if (fc->minor < 4 && args->in.h.opcode == FUSE_STATFS)
529 args->out.args[0].size = FUSE_COMPAT_STATFS_SIZE;
530
531 if (fc->minor < 9) {
532 switch (args->in.h.opcode) {
533 case FUSE_LOOKUP:
534 case FUSE_CREATE:
535 case FUSE_MKNOD:
536 case FUSE_MKDIR:
537 case FUSE_SYMLINK:
538 case FUSE_LINK:
539 args->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
540 break;
541 case FUSE_GETATTR:
542 case FUSE_SETATTR:
543 args->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
544 break;
545 }
546 }
547 if (fc->minor < 12) {
548 switch (args->in.h.opcode) {
549 case FUSE_CREATE:
550 args->in.args[0].size = sizeof(struct fuse_open_in);
551 break;
552 case FUSE_MKNOD:
553 args->in.args[0].size = FUSE_COMPAT_MKNOD_IN_SIZE;
554 break;
555 }
556 }
557}
558
Miklos Szeredi70781872014-12-12 09:49:05 +0100559ssize_t fuse_simple_request(struct fuse_conn *fc, struct fuse_args *args)
560{
561 struct fuse_req *req;
562 ssize_t ret;
563
564 req = fuse_get_req(fc, 0);
565 if (IS_ERR(req))
566 return PTR_ERR(req);
567
Miklos Szeredi21f62172015-01-06 10:45:35 +0100568 /* Needs to be done after fuse_get_req() so that fc->minor is valid */
569 fuse_adjust_compat(fc, args);
570
Miklos Szeredi70781872014-12-12 09:49:05 +0100571 req->in.h.opcode = args->in.h.opcode;
572 req->in.h.nodeid = args->in.h.nodeid;
573 req->in.numargs = args->in.numargs;
574 memcpy(req->in.args, args->in.args,
575 args->in.numargs * sizeof(struct fuse_in_arg));
576 req->out.argvar = args->out.argvar;
577 req->out.numargs = args->out.numargs;
578 memcpy(req->out.args, args->out.args,
579 args->out.numargs * sizeof(struct fuse_arg));
580 fuse_request_send(fc, req);
581 ret = req->out.h.error;
582 if (!ret && args->out.argvar) {
583 BUG_ON(args->out.numargs != 1);
584 ret = req->out.args[0].size;
585 }
586 fuse_put_request(fc, req);
587
588 return ret;
589}
590
Tejun Heob93f8582008-11-26 12:03:55 +0100591static void fuse_request_send_nowait_locked(struct fuse_conn *fc,
592 struct fuse_req *req)
Miklos Szeredid12def12008-02-06 01:38:39 -0800593{
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400594 BUG_ON(!req->background);
Miklos Szeredi5437f242015-07-01 16:25:56 +0200595 if (!req->waiting) {
596 req->waiting = 1;
597 atomic_inc(&fc->num_waiting);
598 }
Miklos Szeredid12def12008-02-06 01:38:39 -0800599 fc->num_background++;
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700600 if (fc->num_background == fc->max_background)
Miklos Szeredid12def12008-02-06 01:38:39 -0800601 fc->blocked = 1;
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700602 if (fc->num_background == fc->congestion_threshold &&
Tejun Heoa325f9b2009-04-14 10:54:52 +0900603 fc->bdi_initialized) {
Jens Axboe8aa7e842009-07-09 14:52:32 +0200604 set_bdi_congested(&fc->bdi, BLK_RW_SYNC);
605 set_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
Miklos Szeredid12def12008-02-06 01:38:39 -0800606 }
607 list_add_tail(&req->list, &fc->bg_queue);
608 flush_bg_queue(fc);
609}
610
Tejun Heob93f8582008-11-26 12:03:55 +0100611static void fuse_request_send_nowait(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700612{
Miklos Szeredi42dc6212015-07-01 16:25:56 +0200613 BUG_ON(!req->end);
Miklos Szeredid7133112006-04-10 22:54:55 -0700614 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700615 if (fc->connected) {
Tejun Heob93f8582008-11-26 12:03:55 +0100616 fuse_request_send_nowait_locked(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700617 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700618 } else {
Miklos Szeredi42dc6212015-07-01 16:25:56 +0200619 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700620 req->out.h.error = -ENOTCONN;
Miklos Szeredi42dc6212015-07-01 16:25:56 +0200621 req->end(fc, req);
622 fuse_put_request(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700623 }
624}
625
Tejun Heob93f8582008-11-26 12:03:55 +0100626void fuse_request_send_background(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700627{
628 req->isreply = 1;
Tejun Heob93f8582008-11-26 12:03:55 +0100629 fuse_request_send_nowait(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700630}
Tejun Heo08cbf542009-04-14 10:54:53 +0900631EXPORT_SYMBOL_GPL(fuse_request_send_background);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700632
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200633static int fuse_request_send_notify_reply(struct fuse_conn *fc,
634 struct fuse_req *req, u64 unique)
635{
636 int err = -ENODEV;
637
638 req->isreply = 0;
639 req->in.h.unique = unique;
640 spin_lock(&fc->lock);
641 if (fc->connected) {
642 queue_request(fc, req);
643 err = 0;
644 }
645 spin_unlock(&fc->lock);
646
647 return err;
648}
649
Miklos Szeredi334f4852005-09-09 13:10:27 -0700650/*
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700651 * Called under fc->lock
652 *
653 * fc->connected must have been checked previously
654 */
Tejun Heob93f8582008-11-26 12:03:55 +0100655void fuse_request_send_background_locked(struct fuse_conn *fc,
656 struct fuse_req *req)
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700657{
658 req->isreply = 1;
Tejun Heob93f8582008-11-26 12:03:55 +0100659 fuse_request_send_nowait_locked(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700660}
661
Anand V. Avati0b05b182012-08-19 08:53:23 -0400662void fuse_force_forget(struct file *file, u64 nodeid)
663{
Al Viro6131ffa2013-02-27 16:59:05 -0500664 struct inode *inode = file_inode(file);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400665 struct fuse_conn *fc = get_fuse_conn(inode);
666 struct fuse_req *req;
667 struct fuse_forget_in inarg;
668
669 memset(&inarg, 0, sizeof(inarg));
670 inarg.nlookup = 1;
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400671 req = fuse_get_req_nofail_nopages(fc, file);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400672 req->in.h.opcode = FUSE_FORGET;
673 req->in.h.nodeid = nodeid;
674 req->in.numargs = 1;
675 req->in.args[0].size = sizeof(inarg);
676 req->in.args[0].value = &inarg;
677 req->isreply = 0;
Eric Wong6a4e9222013-02-04 13:04:44 +0000678 __fuse_request_send(fc, req);
679 /* ignore errors */
680 fuse_put_request(fc, req);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400681}
682
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700683/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700684 * Lock the request. Up to the next unlock_request() there mustn't be
685 * anything that could cause a page-fault. If the request was already
Miklos Szeredif9a28422006-06-25 05:48:53 -0700686 * aborted bail out.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700687 */
Miklos Szeredid7133112006-04-10 22:54:55 -0700688static int lock_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700689{
690 int err = 0;
691 if (req) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700692 spin_lock(&fc->lock);
Miklos Szeredif9a28422006-06-25 05:48:53 -0700693 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700694 err = -ENOENT;
695 else
696 req->locked = 1;
Miklos Szeredid7133112006-04-10 22:54:55 -0700697 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700698 }
699 return err;
700}
701
702/*
Miklos Szeredif9a28422006-06-25 05:48:53 -0700703 * Unlock request. If it was aborted during being locked, the
Miklos Szeredi334f4852005-09-09 13:10:27 -0700704 * requester thread is currently waiting for it to be unlocked, so
705 * wake it up.
706 */
Miklos Szeredid7133112006-04-10 22:54:55 -0700707static void unlock_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700708{
709 if (req) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700710 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700711 req->locked = 0;
Miklos Szeredif9a28422006-06-25 05:48:53 -0700712 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700713 wake_up(&req->waitq);
Miklos Szeredid7133112006-04-10 22:54:55 -0700714 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700715 }
716}
717
718struct fuse_copy_state {
Miklos Szeredid7133112006-04-10 22:54:55 -0700719 struct fuse_conn *fc;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700720 int write;
721 struct fuse_req *req;
Al Viro6c09e942015-04-03 22:06:08 -0400722 struct iov_iter *iter;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200723 struct pipe_buffer *pipebufs;
724 struct pipe_buffer *currbuf;
725 struct pipe_inode_info *pipe;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700726 unsigned long nr_segs;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700727 struct page *pg;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700728 unsigned len;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200729 unsigned offset;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200730 unsigned move_pages:1;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700731};
732
Al Viro6c09e942015-04-03 22:06:08 -0400733static void fuse_copy_init(struct fuse_copy_state *cs,
734 struct fuse_conn *fc,
Miklos Szeredic3021622010-05-25 15:06:07 +0200735 int write,
Al Viro6c09e942015-04-03 22:06:08 -0400736 struct iov_iter *iter)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700737{
738 memset(cs, 0, sizeof(*cs));
Miklos Szeredid7133112006-04-10 22:54:55 -0700739 cs->fc = fc;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700740 cs->write = write;
Al Viro6c09e942015-04-03 22:06:08 -0400741 cs->iter = iter;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700742}
743
744/* Unmap and put previous page of userspace buffer */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800745static void fuse_copy_finish(struct fuse_copy_state *cs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700746{
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200747 if (cs->currbuf) {
748 struct pipe_buffer *buf = cs->currbuf;
749
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200750 if (cs->write)
Miklos Szeredic3021622010-05-25 15:06:07 +0200751 buf->len = PAGE_SIZE - cs->len;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200752 cs->currbuf = NULL;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200753 } else if (cs->pg) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700754 if (cs->write) {
755 flush_dcache_page(cs->pg);
756 set_page_dirty_lock(cs->pg);
757 }
758 put_page(cs->pg);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700759 }
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200760 cs->pg = NULL;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700761}
762
763/*
764 * Get another pagefull of userspace buffer, and map it to kernel
765 * address space, and lock request
766 */
767static int fuse_copy_fill(struct fuse_copy_state *cs)
768{
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200769 struct page *page;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700770 int err;
771
Miklos Szeredid7133112006-04-10 22:54:55 -0700772 unlock_request(cs->fc, cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700773 fuse_copy_finish(cs);
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200774 if (cs->pipebufs) {
775 struct pipe_buffer *buf = cs->pipebufs;
776
Miklos Szeredic3021622010-05-25 15:06:07 +0200777 if (!cs->write) {
778 err = buf->ops->confirm(cs->pipe, buf);
779 if (err)
780 return err;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200781
Miklos Szeredic3021622010-05-25 15:06:07 +0200782 BUG_ON(!cs->nr_segs);
783 cs->currbuf = buf;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200784 cs->pg = buf->page;
785 cs->offset = buf->offset;
Miklos Szeredic3021622010-05-25 15:06:07 +0200786 cs->len = buf->len;
Miklos Szeredic3021622010-05-25 15:06:07 +0200787 cs->pipebufs++;
788 cs->nr_segs--;
789 } else {
Miklos Szeredic3021622010-05-25 15:06:07 +0200790 if (cs->nr_segs == cs->pipe->buffers)
791 return -EIO;
792
793 page = alloc_page(GFP_HIGHUSER);
794 if (!page)
795 return -ENOMEM;
796
797 buf->page = page;
798 buf->offset = 0;
799 buf->len = 0;
800
801 cs->currbuf = buf;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200802 cs->pg = page;
803 cs->offset = 0;
Miklos Szeredic3021622010-05-25 15:06:07 +0200804 cs->len = PAGE_SIZE;
805 cs->pipebufs++;
806 cs->nr_segs++;
807 }
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200808 } else {
Al Viro6c09e942015-04-03 22:06:08 -0400809 size_t off;
810 err = iov_iter_get_pages(cs->iter, &page, PAGE_SIZE, 1, &off);
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200811 if (err < 0)
812 return err;
Al Viro6c09e942015-04-03 22:06:08 -0400813 BUG_ON(!err);
814 cs->len = err;
815 cs->offset = off;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200816 cs->pg = page;
Al Viro6c09e942015-04-03 22:06:08 -0400817 cs->offset = off;
818 iov_iter_advance(cs->iter, err);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700819 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700820
Miklos Szeredid7133112006-04-10 22:54:55 -0700821 return lock_request(cs->fc, cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700822}
823
824/* Do as much copy to/from userspace buffer as we can */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800825static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700826{
827 unsigned ncpy = min(*size, cs->len);
828 if (val) {
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200829 void *pgaddr = kmap_atomic(cs->pg);
830 void *buf = pgaddr + cs->offset;
831
Miklos Szeredi334f4852005-09-09 13:10:27 -0700832 if (cs->write)
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200833 memcpy(buf, *val, ncpy);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700834 else
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200835 memcpy(*val, buf, ncpy);
836
837 kunmap_atomic(pgaddr);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700838 *val += ncpy;
839 }
840 *size -= ncpy;
841 cs->len -= ncpy;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200842 cs->offset += ncpy;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700843 return ncpy;
844}
845
Miklos Szeredice534fb2010-05-25 15:06:07 +0200846static int fuse_check_page(struct page *page)
847{
848 if (page_mapcount(page) ||
849 page->mapping != NULL ||
850 page_count(page) != 1 ||
851 (page->flags & PAGE_FLAGS_CHECK_AT_PREP &
852 ~(1 << PG_locked |
853 1 << PG_referenced |
854 1 << PG_uptodate |
855 1 << PG_lru |
856 1 << PG_active |
857 1 << PG_reclaim))) {
858 printk(KERN_WARNING "fuse: trying to steal weird page\n");
859 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);
860 return 1;
861 }
862 return 0;
863}
864
865static int fuse_try_move_page(struct fuse_copy_state *cs, struct page **pagep)
866{
867 int err;
868 struct page *oldpage = *pagep;
869 struct page *newpage;
870 struct pipe_buffer *buf = cs->pipebufs;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200871
872 unlock_request(cs->fc, cs->req);
873 fuse_copy_finish(cs);
874
875 err = buf->ops->confirm(cs->pipe, buf);
876 if (err)
877 return err;
878
879 BUG_ON(!cs->nr_segs);
880 cs->currbuf = buf;
881 cs->len = buf->len;
882 cs->pipebufs++;
883 cs->nr_segs--;
884
885 if (cs->len != PAGE_SIZE)
886 goto out_fallback;
887
888 if (buf->ops->steal(cs->pipe, buf) != 0)
889 goto out_fallback;
890
891 newpage = buf->page;
892
Miklos Szerediaa991b32015-02-26 11:45:47 +0100893 if (!PageUptodate(newpage))
894 SetPageUptodate(newpage);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200895
896 ClearPageMappedToDisk(newpage);
897
898 if (fuse_check_page(newpage) != 0)
899 goto out_fallback_unlock;
900
Miklos Szeredice534fb2010-05-25 15:06:07 +0200901 /*
902 * This is a new and locked page, it shouldn't be mapped or
903 * have any special flags on it
904 */
905 if (WARN_ON(page_mapped(oldpage)))
906 goto out_fallback_unlock;
907 if (WARN_ON(page_has_private(oldpage)))
908 goto out_fallback_unlock;
909 if (WARN_ON(PageDirty(oldpage) || PageWriteback(oldpage)))
910 goto out_fallback_unlock;
911 if (WARN_ON(PageMlocked(oldpage)))
912 goto out_fallback_unlock;
913
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700914 err = replace_page_cache_page(oldpage, newpage, GFP_KERNEL);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200915 if (err) {
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700916 unlock_page(newpage);
917 return err;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200918 }
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700919
Miklos Szeredice534fb2010-05-25 15:06:07 +0200920 page_cache_get(newpage);
921
922 if (!(buf->flags & PIPE_BUF_FLAG_LRU))
923 lru_cache_add_file(newpage);
924
925 err = 0;
926 spin_lock(&cs->fc->lock);
927 if (cs->req->aborted)
928 err = -ENOENT;
929 else
930 *pagep = newpage;
931 spin_unlock(&cs->fc->lock);
932
933 if (err) {
934 unlock_page(newpage);
935 page_cache_release(newpage);
936 return err;
937 }
938
939 unlock_page(oldpage);
940 page_cache_release(oldpage);
941 cs->len = 0;
942
943 return 0;
944
945out_fallback_unlock:
946 unlock_page(newpage);
947out_fallback:
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200948 cs->pg = buf->page;
949 cs->offset = buf->offset;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200950
951 err = lock_request(cs->fc, cs->req);
952 if (err)
953 return err;
954
955 return 1;
956}
957
Miklos Szeredic3021622010-05-25 15:06:07 +0200958static int fuse_ref_page(struct fuse_copy_state *cs, struct page *page,
959 unsigned offset, unsigned count)
960{
961 struct pipe_buffer *buf;
962
963 if (cs->nr_segs == cs->pipe->buffers)
964 return -EIO;
965
966 unlock_request(cs->fc, cs->req);
967 fuse_copy_finish(cs);
968
969 buf = cs->pipebufs;
970 page_cache_get(page);
971 buf->page = page;
972 buf->offset = offset;
973 buf->len = count;
974
975 cs->pipebufs++;
976 cs->nr_segs++;
977 cs->len = 0;
978
979 return 0;
980}
981
Miklos Szeredi334f4852005-09-09 13:10:27 -0700982/*
983 * Copy a page in the request to/from the userspace buffer. Must be
984 * done atomically
985 */
Miklos Szeredice534fb2010-05-25 15:06:07 +0200986static int fuse_copy_page(struct fuse_copy_state *cs, struct page **pagep,
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800987 unsigned offset, unsigned count, int zeroing)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700988{
Miklos Szeredice534fb2010-05-25 15:06:07 +0200989 int err;
990 struct page *page = *pagep;
991
Miklos Szeredib6777c42010-10-26 14:22:27 -0700992 if (page && zeroing && count < PAGE_SIZE)
993 clear_highpage(page);
994
Miklos Szeredi334f4852005-09-09 13:10:27 -0700995 while (count) {
Miklos Szeredic3021622010-05-25 15:06:07 +0200996 if (cs->write && cs->pipebufs && page) {
997 return fuse_ref_page(cs, page, offset, count);
998 } else if (!cs->len) {
Miklos Szeredice534fb2010-05-25 15:06:07 +0200999 if (cs->move_pages && page &&
1000 offset == 0 && count == PAGE_SIZE) {
1001 err = fuse_try_move_page(cs, pagep);
1002 if (err <= 0)
1003 return err;
1004 } else {
1005 err = fuse_copy_fill(cs);
1006 if (err)
1007 return err;
1008 }
Miklos Szeredi1729a162008-11-26 12:03:54 +01001009 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001010 if (page) {
Cong Wang2408f6e2011-11-25 23:14:30 +08001011 void *mapaddr = kmap_atomic(page);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001012 void *buf = mapaddr + offset;
1013 offset += fuse_copy_do(cs, &buf, &count);
Cong Wang2408f6e2011-11-25 23:14:30 +08001014 kunmap_atomic(mapaddr);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001015 } else
1016 offset += fuse_copy_do(cs, NULL, &count);
1017 }
1018 if (page && !cs->write)
1019 flush_dcache_page(page);
1020 return 0;
1021}
1022
1023/* Copy pages in the request to/from userspace buffer */
1024static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
1025 int zeroing)
1026{
1027 unsigned i;
1028 struct fuse_req *req = cs->req;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001029
1030 for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
Miklos Szeredice534fb2010-05-25 15:06:07 +02001031 int err;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001032 unsigned offset = req->page_descs[i].offset;
1033 unsigned count = min(nbytes, req->page_descs[i].length);
Miklos Szeredice534fb2010-05-25 15:06:07 +02001034
1035 err = fuse_copy_page(cs, &req->pages[i], offset, count,
1036 zeroing);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001037 if (err)
1038 return err;
1039
1040 nbytes -= count;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001041 }
1042 return 0;
1043}
1044
1045/* Copy a single argument in the request to/from userspace buffer */
1046static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
1047{
1048 while (size) {
Miklos Szeredi1729a162008-11-26 12:03:54 +01001049 if (!cs->len) {
1050 int err = fuse_copy_fill(cs);
1051 if (err)
1052 return err;
1053 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001054 fuse_copy_do(cs, &val, &size);
1055 }
1056 return 0;
1057}
1058
1059/* Copy request arguments to/from userspace buffer */
1060static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
1061 unsigned argpages, struct fuse_arg *args,
1062 int zeroing)
1063{
1064 int err = 0;
1065 unsigned i;
1066
1067 for (i = 0; !err && i < numargs; i++) {
1068 struct fuse_arg *arg = &args[i];
1069 if (i == numargs - 1 && argpages)
1070 err = fuse_copy_pages(cs, arg->size, zeroing);
1071 else
1072 err = fuse_copy_one(cs, arg->value, arg->size);
1073 }
1074 return err;
1075}
1076
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001077static int forget_pending(struct fuse_conn *fc)
1078{
1079 return fc->forget_list_head.next != NULL;
1080}
1081
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001082static int request_pending(struct fuse_conn *fc)
1083{
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001084 return !list_empty(&fc->pending) || !list_empty(&fc->interrupts) ||
1085 forget_pending(fc);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001086}
1087
Miklos Szeredi334f4852005-09-09 13:10:27 -07001088/* Wait until a request is available on the pending list */
1089static void request_wait(struct fuse_conn *fc)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001090__releases(fc->lock)
1091__acquires(fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001092{
1093 DECLARE_WAITQUEUE(wait, current);
1094
1095 add_wait_queue_exclusive(&fc->waitq, &wait);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001096 while (fc->connected && !request_pending(fc)) {
Miklos Szeredi334f4852005-09-09 13:10:27 -07001097 set_current_state(TASK_INTERRUPTIBLE);
1098 if (signal_pending(current))
1099 break;
1100
Miklos Szeredid7133112006-04-10 22:54:55 -07001101 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001102 schedule();
Miklos Szeredid7133112006-04-10 22:54:55 -07001103 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001104 }
1105 set_current_state(TASK_RUNNING);
1106 remove_wait_queue(&fc->waitq, &wait);
1107}
1108
1109/*
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001110 * Transfer an interrupt request to userspace
1111 *
1112 * Unlike other requests this is assembled on demand, without a need
1113 * to allocate a separate fuse_req structure.
1114 *
1115 * Called with fc->lock held, releases it
1116 */
Miklos Szeredic3021622010-05-25 15:06:07 +02001117static int fuse_read_interrupt(struct fuse_conn *fc, struct fuse_copy_state *cs,
1118 size_t nbytes, struct fuse_req *req)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001119__releases(fc->lock)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001120{
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001121 struct fuse_in_header ih;
1122 struct fuse_interrupt_in arg;
1123 unsigned reqsize = sizeof(ih) + sizeof(arg);
1124 int err;
1125
1126 list_del_init(&req->intr_entry);
1127 req->intr_unique = fuse_get_unique(fc);
1128 memset(&ih, 0, sizeof(ih));
1129 memset(&arg, 0, sizeof(arg));
1130 ih.len = reqsize;
1131 ih.opcode = FUSE_INTERRUPT;
1132 ih.unique = req->intr_unique;
1133 arg.unique = req->in.h.unique;
1134
1135 spin_unlock(&fc->lock);
Miklos Szeredic3021622010-05-25 15:06:07 +02001136 if (nbytes < reqsize)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001137 return -EINVAL;
1138
Miklos Szeredic3021622010-05-25 15:06:07 +02001139 err = fuse_copy_one(cs, &ih, sizeof(ih));
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001140 if (!err)
Miklos Szeredic3021622010-05-25 15:06:07 +02001141 err = fuse_copy_one(cs, &arg, sizeof(arg));
1142 fuse_copy_finish(cs);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001143
1144 return err ? err : reqsize;
1145}
1146
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001147static struct fuse_forget_link *dequeue_forget(struct fuse_conn *fc,
1148 unsigned max,
1149 unsigned *countp)
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001150{
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001151 struct fuse_forget_link *head = fc->forget_list_head.next;
1152 struct fuse_forget_link **newhead = &head;
1153 unsigned count;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001154
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001155 for (count = 0; *newhead != NULL && count < max; count++)
1156 newhead = &(*newhead)->next;
1157
1158 fc->forget_list_head.next = *newhead;
1159 *newhead = NULL;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001160 if (fc->forget_list_head.next == NULL)
1161 fc->forget_list_tail = &fc->forget_list_head;
1162
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001163 if (countp != NULL)
1164 *countp = count;
1165
1166 return head;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001167}
1168
1169static int fuse_read_single_forget(struct fuse_conn *fc,
1170 struct fuse_copy_state *cs,
1171 size_t nbytes)
1172__releases(fc->lock)
1173{
1174 int err;
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001175 struct fuse_forget_link *forget = dequeue_forget(fc, 1, NULL);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001176 struct fuse_forget_in arg = {
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001177 .nlookup = forget->forget_one.nlookup,
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001178 };
1179 struct fuse_in_header ih = {
1180 .opcode = FUSE_FORGET,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001181 .nodeid = forget->forget_one.nodeid,
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001182 .unique = fuse_get_unique(fc),
1183 .len = sizeof(ih) + sizeof(arg),
1184 };
1185
1186 spin_unlock(&fc->lock);
1187 kfree(forget);
1188 if (nbytes < ih.len)
1189 return -EINVAL;
1190
1191 err = fuse_copy_one(cs, &ih, sizeof(ih));
1192 if (!err)
1193 err = fuse_copy_one(cs, &arg, sizeof(arg));
1194 fuse_copy_finish(cs);
1195
1196 if (err)
1197 return err;
1198
1199 return ih.len;
1200}
1201
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001202static int fuse_read_batch_forget(struct fuse_conn *fc,
1203 struct fuse_copy_state *cs, size_t nbytes)
1204__releases(fc->lock)
1205{
1206 int err;
1207 unsigned max_forgets;
1208 unsigned count;
1209 struct fuse_forget_link *head;
1210 struct fuse_batch_forget_in arg = { .count = 0 };
1211 struct fuse_in_header ih = {
1212 .opcode = FUSE_BATCH_FORGET,
1213 .unique = fuse_get_unique(fc),
1214 .len = sizeof(ih) + sizeof(arg),
1215 };
1216
1217 if (nbytes < ih.len) {
1218 spin_unlock(&fc->lock);
1219 return -EINVAL;
1220 }
1221
1222 max_forgets = (nbytes - ih.len) / sizeof(struct fuse_forget_one);
1223 head = dequeue_forget(fc, max_forgets, &count);
1224 spin_unlock(&fc->lock);
1225
1226 arg.count = count;
1227 ih.len += count * sizeof(struct fuse_forget_one);
1228 err = fuse_copy_one(cs, &ih, sizeof(ih));
1229 if (!err)
1230 err = fuse_copy_one(cs, &arg, sizeof(arg));
1231
1232 while (head) {
1233 struct fuse_forget_link *forget = head;
1234
1235 if (!err) {
1236 err = fuse_copy_one(cs, &forget->forget_one,
1237 sizeof(forget->forget_one));
1238 }
1239 head = forget->next;
1240 kfree(forget);
1241 }
1242
1243 fuse_copy_finish(cs);
1244
1245 if (err)
1246 return err;
1247
1248 return ih.len;
1249}
1250
1251static int fuse_read_forget(struct fuse_conn *fc, struct fuse_copy_state *cs,
1252 size_t nbytes)
1253__releases(fc->lock)
1254{
1255 if (fc->minor < 16 || fc->forget_list_head.next->next == NULL)
1256 return fuse_read_single_forget(fc, cs, nbytes);
1257 else
1258 return fuse_read_batch_forget(fc, cs, nbytes);
1259}
1260
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001261/*
Miklos Szeredi334f4852005-09-09 13:10:27 -07001262 * Read a single request into the userspace filesystem's buffer. This
1263 * function waits until a request is available, then removes it from
1264 * the pending list and copies request data to userspace buffer. If
Miklos Szeredif9a28422006-06-25 05:48:53 -07001265 * no reply is needed (FORGET) or request has been aborted or there
1266 * was an error during the copying then it's finished by calling
Miklos Szeredi334f4852005-09-09 13:10:27 -07001267 * request_end(). Otherwise add it to the processing list, and set
1268 * the 'sent' flag.
1269 */
Miklos Szeredic3021622010-05-25 15:06:07 +02001270static ssize_t fuse_dev_do_read(struct fuse_conn *fc, struct file *file,
1271 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001272{
1273 int err;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001274 struct fuse_req *req;
1275 struct fuse_in *in;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001276 unsigned reqsize;
1277
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001278 restart:
Miklos Szeredid7133112006-04-10 22:54:55 -07001279 spin_lock(&fc->lock);
Jeff Dikee5ac1d12006-04-10 22:54:53 -07001280 err = -EAGAIN;
1281 if ((file->f_flags & O_NONBLOCK) && fc->connected &&
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001282 !request_pending(fc))
Jeff Dikee5ac1d12006-04-10 22:54:53 -07001283 goto err_unlock;
1284
Miklos Szeredi334f4852005-09-09 13:10:27 -07001285 request_wait(fc);
1286 err = -ENODEV;
Miklos Szeredi9ba7cbb2006-01-16 22:14:34 -08001287 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001288 goto err_unlock;
1289 err = -ERESTARTSYS;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001290 if (!request_pending(fc))
Miklos Szeredi334f4852005-09-09 13:10:27 -07001291 goto err_unlock;
1292
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001293 if (!list_empty(&fc->interrupts)) {
1294 req = list_entry(fc->interrupts.next, struct fuse_req,
1295 intr_entry);
Miklos Szeredic3021622010-05-25 15:06:07 +02001296 return fuse_read_interrupt(fc, cs, nbytes, req);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001297 }
1298
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001299 if (forget_pending(fc)) {
1300 if (list_empty(&fc->pending) || fc->forget_batch-- > 0)
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001301 return fuse_read_forget(fc, cs, nbytes);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001302
1303 if (fc->forget_batch <= -8)
1304 fc->forget_batch = 16;
1305 }
1306
Miklos Szeredi334f4852005-09-09 13:10:27 -07001307 req = list_entry(fc->pending.next, struct fuse_req, list);
Miklos Szeredi83cfd492006-01-16 22:14:31 -08001308 req->state = FUSE_REQ_READING;
Miklos Szeredid77a1d52006-01-16 22:14:31 -08001309 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001310
1311 in = &req->in;
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001312 reqsize = in->h.len;
1313 /* If request is too large, reply with an error and restart the read */
Miklos Szeredic3021622010-05-25 15:06:07 +02001314 if (nbytes < reqsize) {
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001315 req->out.h.error = -EIO;
1316 /* SETXATTR is special, since it may contain too large data */
1317 if (in->h.opcode == FUSE_SETXATTR)
1318 req->out.h.error = -E2BIG;
1319 request_end(fc, req);
1320 goto restart;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001321 }
Miklos Szeredid7133112006-04-10 22:54:55 -07001322 spin_unlock(&fc->lock);
Miklos Szeredic3021622010-05-25 15:06:07 +02001323 cs->req = req;
1324 err = fuse_copy_one(cs, &in->h, sizeof(in->h));
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001325 if (!err)
Miklos Szeredic3021622010-05-25 15:06:07 +02001326 err = fuse_copy_args(cs, in->numargs, in->argpages,
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001327 (struct fuse_arg *) in->args, 0);
Miklos Szeredic3021622010-05-25 15:06:07 +02001328 fuse_copy_finish(cs);
Miklos Szeredid7133112006-04-10 22:54:55 -07001329 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001330 req->locked = 0;
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -07001331 if (req->aborted) {
1332 request_end(fc, req);
1333 return -ENODEV;
1334 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001335 if (err) {
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -07001336 req->out.h.error = -EIO;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001337 request_end(fc, req);
1338 return err;
1339 }
1340 if (!req->isreply)
1341 request_end(fc, req);
1342 else {
Miklos Szeredi83cfd492006-01-16 22:14:31 -08001343 req->state = FUSE_REQ_SENT;
Miklos Szeredid77a1d52006-01-16 22:14:31 -08001344 list_move_tail(&req->list, &fc->processing);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001345 if (req->interrupted)
1346 queue_interrupt(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -07001347 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001348 }
1349 return reqsize;
1350
1351 err_unlock:
Miklos Szeredid7133112006-04-10 22:54:55 -07001352 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001353 return err;
1354}
1355
Tom Van Braeckel94e4fe22015-01-12 05:22:16 +01001356static int fuse_dev_open(struct inode *inode, struct file *file)
1357{
1358 /*
1359 * The fuse device's file's private_data is used to hold
1360 * the fuse_conn(ection) when it is mounted, and is used to
1361 * keep track of whether the file has been mounted already.
1362 */
1363 file->private_data = NULL;
1364 return 0;
1365}
1366
Al Virofbdbacc2015-04-03 21:53:39 -04001367static ssize_t fuse_dev_read(struct kiocb *iocb, struct iov_iter *to)
Miklos Szeredic3021622010-05-25 15:06:07 +02001368{
1369 struct fuse_copy_state cs;
1370 struct file *file = iocb->ki_filp;
1371 struct fuse_conn *fc = fuse_get_conn(file);
1372 if (!fc)
1373 return -EPERM;
1374
Al Virofbdbacc2015-04-03 21:53:39 -04001375 if (!iter_is_iovec(to))
1376 return -EINVAL;
Miklos Szeredic3021622010-05-25 15:06:07 +02001377
Al Viro6c09e942015-04-03 22:06:08 -04001378 fuse_copy_init(&cs, fc, 1, to);
Al Virofbdbacc2015-04-03 21:53:39 -04001379
1380 return fuse_dev_do_read(fc, file, &cs, iov_iter_count(to));
Miklos Szeredic3021622010-05-25 15:06:07 +02001381}
1382
Miklos Szeredic3021622010-05-25 15:06:07 +02001383static ssize_t fuse_dev_splice_read(struct file *in, loff_t *ppos,
1384 struct pipe_inode_info *pipe,
1385 size_t len, unsigned int flags)
1386{
1387 int ret;
1388 int page_nr = 0;
1389 int do_wakeup = 0;
1390 struct pipe_buffer *bufs;
1391 struct fuse_copy_state cs;
1392 struct fuse_conn *fc = fuse_get_conn(in);
1393 if (!fc)
1394 return -EPERM;
1395
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001396 bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), GFP_KERNEL);
Miklos Szeredic3021622010-05-25 15:06:07 +02001397 if (!bufs)
1398 return -ENOMEM;
1399
Al Viro6c09e942015-04-03 22:06:08 -04001400 fuse_copy_init(&cs, fc, 1, NULL);
Miklos Szeredic3021622010-05-25 15:06:07 +02001401 cs.pipebufs = bufs;
1402 cs.pipe = pipe;
1403 ret = fuse_dev_do_read(fc, in, &cs, len);
1404 if (ret < 0)
1405 goto out;
1406
1407 ret = 0;
1408 pipe_lock(pipe);
1409
1410 if (!pipe->readers) {
1411 send_sig(SIGPIPE, current, 0);
1412 if (!ret)
1413 ret = -EPIPE;
1414 goto out_unlock;
1415 }
1416
1417 if (pipe->nrbufs + cs.nr_segs > pipe->buffers) {
1418 ret = -EIO;
1419 goto out_unlock;
1420 }
1421
1422 while (page_nr < cs.nr_segs) {
1423 int newbuf = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
1424 struct pipe_buffer *buf = pipe->bufs + newbuf;
1425
1426 buf->page = bufs[page_nr].page;
1427 buf->offset = bufs[page_nr].offset;
1428 buf->len = bufs[page_nr].len;
Miklos Szeredi28a625c2014-01-22 19:36:57 +01001429 /*
1430 * Need to be careful about this. Having buf->ops in module
1431 * code can Oops if the buffer persists after module unload.
1432 */
1433 buf->ops = &nosteal_pipe_buf_ops;
Miklos Szeredic3021622010-05-25 15:06:07 +02001434
1435 pipe->nrbufs++;
1436 page_nr++;
1437 ret += buf->len;
1438
Al Viro6447a3c2013-03-21 11:01:38 -04001439 if (pipe->files)
Miklos Szeredic3021622010-05-25 15:06:07 +02001440 do_wakeup = 1;
1441 }
1442
1443out_unlock:
1444 pipe_unlock(pipe);
1445
1446 if (do_wakeup) {
1447 smp_mb();
1448 if (waitqueue_active(&pipe->wait))
1449 wake_up_interruptible(&pipe->wait);
1450 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
1451 }
1452
1453out:
1454 for (; page_nr < cs.nr_segs; page_nr++)
1455 page_cache_release(bufs[page_nr].page);
1456
1457 kfree(bufs);
1458 return ret;
1459}
1460
Tejun Heo95668a62008-11-26 12:03:55 +01001461static int fuse_notify_poll(struct fuse_conn *fc, unsigned int size,
1462 struct fuse_copy_state *cs)
1463{
1464 struct fuse_notify_poll_wakeup_out outarg;
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001465 int err = -EINVAL;
Tejun Heo95668a62008-11-26 12:03:55 +01001466
1467 if (size != sizeof(outarg))
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001468 goto err;
Tejun Heo95668a62008-11-26 12:03:55 +01001469
1470 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1471 if (err)
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001472 goto err;
Tejun Heo95668a62008-11-26 12:03:55 +01001473
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001474 fuse_copy_finish(cs);
Tejun Heo95668a62008-11-26 12:03:55 +01001475 return fuse_notify_poll_wakeup(fc, &outarg);
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001476
1477err:
1478 fuse_copy_finish(cs);
1479 return err;
Tejun Heo95668a62008-11-26 12:03:55 +01001480}
1481
John Muir3b463ae2009-05-31 11:13:57 -04001482static int fuse_notify_inval_inode(struct fuse_conn *fc, unsigned int size,
1483 struct fuse_copy_state *cs)
1484{
1485 struct fuse_notify_inval_inode_out outarg;
1486 int err = -EINVAL;
1487
1488 if (size != sizeof(outarg))
1489 goto err;
1490
1491 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1492 if (err)
1493 goto err;
1494 fuse_copy_finish(cs);
1495
1496 down_read(&fc->killsb);
1497 err = -ENOENT;
Miklos Szeredib21dda42010-02-05 12:08:31 +01001498 if (fc->sb) {
1499 err = fuse_reverse_inval_inode(fc->sb, outarg.ino,
1500 outarg.off, outarg.len);
1501 }
John Muir3b463ae2009-05-31 11:13:57 -04001502 up_read(&fc->killsb);
1503 return err;
1504
1505err:
1506 fuse_copy_finish(cs);
1507 return err;
1508}
1509
1510static int fuse_notify_inval_entry(struct fuse_conn *fc, unsigned int size,
1511 struct fuse_copy_state *cs)
1512{
1513 struct fuse_notify_inval_entry_out outarg;
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001514 int err = -ENOMEM;
1515 char *buf;
John Muir3b463ae2009-05-31 11:13:57 -04001516 struct qstr name;
1517
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001518 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1519 if (!buf)
1520 goto err;
1521
1522 err = -EINVAL;
John Muir3b463ae2009-05-31 11:13:57 -04001523 if (size < sizeof(outarg))
1524 goto err;
1525
1526 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1527 if (err)
1528 goto err;
1529
1530 err = -ENAMETOOLONG;
1531 if (outarg.namelen > FUSE_NAME_MAX)
1532 goto err;
1533
Miklos Szeredic2183d12011-08-24 10:20:17 +02001534 err = -EINVAL;
1535 if (size != sizeof(outarg) + outarg.namelen + 1)
1536 goto err;
1537
John Muir3b463ae2009-05-31 11:13:57 -04001538 name.name = buf;
1539 name.len = outarg.namelen;
1540 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1541 if (err)
1542 goto err;
1543 fuse_copy_finish(cs);
1544 buf[outarg.namelen] = 0;
1545 name.hash = full_name_hash(name.name, name.len);
1546
1547 down_read(&fc->killsb);
1548 err = -ENOENT;
Miklos Szeredib21dda42010-02-05 12:08:31 +01001549 if (fc->sb)
John Muir451d0f52011-12-06 21:50:06 +01001550 err = fuse_reverse_inval_entry(fc->sb, outarg.parent, 0, &name);
1551 up_read(&fc->killsb);
1552 kfree(buf);
1553 return err;
1554
1555err:
1556 kfree(buf);
1557 fuse_copy_finish(cs);
1558 return err;
1559}
1560
1561static int fuse_notify_delete(struct fuse_conn *fc, unsigned int size,
1562 struct fuse_copy_state *cs)
1563{
1564 struct fuse_notify_delete_out outarg;
1565 int err = -ENOMEM;
1566 char *buf;
1567 struct qstr name;
1568
1569 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1570 if (!buf)
1571 goto err;
1572
1573 err = -EINVAL;
1574 if (size < sizeof(outarg))
1575 goto err;
1576
1577 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1578 if (err)
1579 goto err;
1580
1581 err = -ENAMETOOLONG;
1582 if (outarg.namelen > FUSE_NAME_MAX)
1583 goto err;
1584
1585 err = -EINVAL;
1586 if (size != sizeof(outarg) + outarg.namelen + 1)
1587 goto err;
1588
1589 name.name = buf;
1590 name.len = outarg.namelen;
1591 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1592 if (err)
1593 goto err;
1594 fuse_copy_finish(cs);
1595 buf[outarg.namelen] = 0;
1596 name.hash = full_name_hash(name.name, name.len);
1597
1598 down_read(&fc->killsb);
1599 err = -ENOENT;
1600 if (fc->sb)
1601 err = fuse_reverse_inval_entry(fc->sb, outarg.parent,
1602 outarg.child, &name);
John Muir3b463ae2009-05-31 11:13:57 -04001603 up_read(&fc->killsb);
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001604 kfree(buf);
John Muir3b463ae2009-05-31 11:13:57 -04001605 return err;
1606
1607err:
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001608 kfree(buf);
John Muir3b463ae2009-05-31 11:13:57 -04001609 fuse_copy_finish(cs);
1610 return err;
1611}
1612
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001613static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
1614 struct fuse_copy_state *cs)
1615{
1616 struct fuse_notify_store_out outarg;
1617 struct inode *inode;
1618 struct address_space *mapping;
1619 u64 nodeid;
1620 int err;
1621 pgoff_t index;
1622 unsigned int offset;
1623 unsigned int num;
1624 loff_t file_size;
1625 loff_t end;
1626
1627 err = -EINVAL;
1628 if (size < sizeof(outarg))
1629 goto out_finish;
1630
1631 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1632 if (err)
1633 goto out_finish;
1634
1635 err = -EINVAL;
1636 if (size - sizeof(outarg) != outarg.size)
1637 goto out_finish;
1638
1639 nodeid = outarg.nodeid;
1640
1641 down_read(&fc->killsb);
1642
1643 err = -ENOENT;
1644 if (!fc->sb)
1645 goto out_up_killsb;
1646
1647 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1648 if (!inode)
1649 goto out_up_killsb;
1650
1651 mapping = inode->i_mapping;
1652 index = outarg.offset >> PAGE_CACHE_SHIFT;
1653 offset = outarg.offset & ~PAGE_CACHE_MASK;
1654 file_size = i_size_read(inode);
1655 end = outarg.offset + outarg.size;
1656 if (end > file_size) {
1657 file_size = end;
1658 fuse_write_update_size(inode, file_size);
1659 }
1660
1661 num = outarg.size;
1662 while (num) {
1663 struct page *page;
1664 unsigned int this_num;
1665
1666 err = -ENOMEM;
1667 page = find_or_create_page(mapping, index,
1668 mapping_gfp_mask(mapping));
1669 if (!page)
1670 goto out_iput;
1671
1672 this_num = min_t(unsigned, num, PAGE_CACHE_SIZE - offset);
1673 err = fuse_copy_page(cs, &page, offset, this_num, 0);
Miklos Szeredi063ec1e2014-01-22 19:36:58 +01001674 if (!err && offset == 0 &&
1675 (this_num == PAGE_CACHE_SIZE || file_size == end))
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001676 SetPageUptodate(page);
1677 unlock_page(page);
1678 page_cache_release(page);
1679
1680 if (err)
1681 goto out_iput;
1682
1683 num -= this_num;
1684 offset = 0;
1685 index++;
1686 }
1687
1688 err = 0;
1689
1690out_iput:
1691 iput(inode);
1692out_up_killsb:
1693 up_read(&fc->killsb);
1694out_finish:
1695 fuse_copy_finish(cs);
1696 return err;
1697}
1698
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001699static void fuse_retrieve_end(struct fuse_conn *fc, struct fuse_req *req)
1700{
Mel Gormanb745bc82014-06-04 16:10:22 -07001701 release_pages(req->pages, req->num_pages, false);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001702}
1703
1704static int fuse_retrieve(struct fuse_conn *fc, struct inode *inode,
1705 struct fuse_notify_retrieve_out *outarg)
1706{
1707 int err;
1708 struct address_space *mapping = inode->i_mapping;
1709 struct fuse_req *req;
1710 pgoff_t index;
1711 loff_t file_size;
1712 unsigned int num;
1713 unsigned int offset;
Geert Uytterhoeven01574432010-09-30 22:06:21 +02001714 size_t total_len = 0;
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001715 int num_pages;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001716
1717 offset = outarg->offset & ~PAGE_CACHE_MASK;
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001718 file_size = i_size_read(inode);
1719
1720 num = outarg->size;
1721 if (outarg->offset > file_size)
1722 num = 0;
1723 else if (outarg->offset + num > file_size)
1724 num = file_size - outarg->offset;
1725
1726 num_pages = (num + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1727 num_pages = min(num_pages, FUSE_MAX_PAGES_PER_REQ);
1728
1729 req = fuse_get_req(fc, num_pages);
1730 if (IS_ERR(req))
1731 return PTR_ERR(req);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001732
1733 req->in.h.opcode = FUSE_NOTIFY_REPLY;
1734 req->in.h.nodeid = outarg->nodeid;
1735 req->in.numargs = 2;
1736 req->in.argpages = 1;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001737 req->page_descs[0].offset = offset;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001738 req->end = fuse_retrieve_end;
1739
1740 index = outarg->offset >> PAGE_CACHE_SHIFT;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001741
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001742 while (num && req->num_pages < num_pages) {
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001743 struct page *page;
1744 unsigned int this_num;
1745
1746 page = find_get_page(mapping, index);
1747 if (!page)
1748 break;
1749
1750 this_num = min_t(unsigned, num, PAGE_CACHE_SIZE - offset);
1751 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001752 req->page_descs[req->num_pages].length = this_num;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001753 req->num_pages++;
1754
Miklos Szeredic9e67d42012-09-04 18:45:54 +02001755 offset = 0;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001756 num -= this_num;
1757 total_len += this_num;
Miklos Szeredi48706d02011-12-13 10:36:59 +01001758 index++;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001759 }
1760 req->misc.retrieve_in.offset = outarg->offset;
1761 req->misc.retrieve_in.size = total_len;
1762 req->in.args[0].size = sizeof(req->misc.retrieve_in);
1763 req->in.args[0].value = &req->misc.retrieve_in;
1764 req->in.args[1].size = total_len;
1765
1766 err = fuse_request_send_notify_reply(fc, req, outarg->notify_unique);
1767 if (err)
1768 fuse_retrieve_end(fc, req);
1769
1770 return err;
1771}
1772
1773static int fuse_notify_retrieve(struct fuse_conn *fc, unsigned int size,
1774 struct fuse_copy_state *cs)
1775{
1776 struct fuse_notify_retrieve_out outarg;
1777 struct inode *inode;
1778 int err;
1779
1780 err = -EINVAL;
1781 if (size != sizeof(outarg))
1782 goto copy_finish;
1783
1784 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1785 if (err)
1786 goto copy_finish;
1787
1788 fuse_copy_finish(cs);
1789
1790 down_read(&fc->killsb);
1791 err = -ENOENT;
1792 if (fc->sb) {
1793 u64 nodeid = outarg.nodeid;
1794
1795 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1796 if (inode) {
1797 err = fuse_retrieve(fc, inode, &outarg);
1798 iput(inode);
1799 }
1800 }
1801 up_read(&fc->killsb);
1802
1803 return err;
1804
1805copy_finish:
1806 fuse_copy_finish(cs);
1807 return err;
1808}
1809
Tejun Heo85993962008-11-26 12:03:55 +01001810static int fuse_notify(struct fuse_conn *fc, enum fuse_notify_code code,
1811 unsigned int size, struct fuse_copy_state *cs)
1812{
Miklos Szeredi0d278362015-02-26 11:45:47 +01001813 /* Don't try to move pages (yet) */
1814 cs->move_pages = 0;
1815
Tejun Heo85993962008-11-26 12:03:55 +01001816 switch (code) {
Tejun Heo95668a62008-11-26 12:03:55 +01001817 case FUSE_NOTIFY_POLL:
1818 return fuse_notify_poll(fc, size, cs);
1819
John Muir3b463ae2009-05-31 11:13:57 -04001820 case FUSE_NOTIFY_INVAL_INODE:
1821 return fuse_notify_inval_inode(fc, size, cs);
1822
1823 case FUSE_NOTIFY_INVAL_ENTRY:
1824 return fuse_notify_inval_entry(fc, size, cs);
1825
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001826 case FUSE_NOTIFY_STORE:
1827 return fuse_notify_store(fc, size, cs);
1828
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001829 case FUSE_NOTIFY_RETRIEVE:
1830 return fuse_notify_retrieve(fc, size, cs);
1831
John Muir451d0f52011-12-06 21:50:06 +01001832 case FUSE_NOTIFY_DELETE:
1833 return fuse_notify_delete(fc, size, cs);
1834
Tejun Heo85993962008-11-26 12:03:55 +01001835 default:
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001836 fuse_copy_finish(cs);
Tejun Heo85993962008-11-26 12:03:55 +01001837 return -EINVAL;
1838 }
1839}
1840
Miklos Szeredi334f4852005-09-09 13:10:27 -07001841/* Look up request on processing list by unique ID */
1842static struct fuse_req *request_find(struct fuse_conn *fc, u64 unique)
1843{
Dong Fang05726ac2013-07-30 22:50:01 -04001844 struct fuse_req *req;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001845
Dong Fang05726ac2013-07-30 22:50:01 -04001846 list_for_each_entry(req, &fc->processing, list) {
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001847 if (req->in.h.unique == unique || req->intr_unique == unique)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001848 return req;
1849 }
1850 return NULL;
1851}
1852
1853static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
1854 unsigned nbytes)
1855{
1856 unsigned reqsize = sizeof(struct fuse_out_header);
1857
1858 if (out->h.error)
1859 return nbytes != reqsize ? -EINVAL : 0;
1860
1861 reqsize += len_args(out->numargs, out->args);
1862
1863 if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
1864 return -EINVAL;
1865 else if (reqsize > nbytes) {
1866 struct fuse_arg *lastarg = &out->args[out->numargs-1];
1867 unsigned diffsize = reqsize - nbytes;
1868 if (diffsize > lastarg->size)
1869 return -EINVAL;
1870 lastarg->size -= diffsize;
1871 }
1872 return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
1873 out->page_zeroing);
1874}
1875
1876/*
1877 * Write a single reply to a request. First the header is copied from
1878 * the write buffer. The request is then searched on the processing
1879 * list by the unique ID found in the header. If found, then remove
1880 * it from the list and copy the rest of the buffer to the request.
1881 * The request is finished by calling request_end()
1882 */
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001883static ssize_t fuse_dev_do_write(struct fuse_conn *fc,
1884 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001885{
1886 int err;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001887 struct fuse_req *req;
1888 struct fuse_out_header oh;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001889
Miklos Szeredi334f4852005-09-09 13:10:27 -07001890 if (nbytes < sizeof(struct fuse_out_header))
1891 return -EINVAL;
1892
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001893 err = fuse_copy_one(cs, &oh, sizeof(oh));
Miklos Szeredi334f4852005-09-09 13:10:27 -07001894 if (err)
1895 goto err_finish;
Tejun Heo85993962008-11-26 12:03:55 +01001896
Miklos Szeredi334f4852005-09-09 13:10:27 -07001897 err = -EINVAL;
Tejun Heo85993962008-11-26 12:03:55 +01001898 if (oh.len != nbytes)
1899 goto err_finish;
1900
1901 /*
1902 * Zero oh.unique indicates unsolicited notification message
1903 * and error contains notification code.
1904 */
1905 if (!oh.unique) {
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001906 err = fuse_notify(fc, oh.error, nbytes - sizeof(oh), cs);
Tejun Heo85993962008-11-26 12:03:55 +01001907 return err ? err : nbytes;
1908 }
1909
1910 err = -EINVAL;
1911 if (oh.error <= -1000 || oh.error > 0)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001912 goto err_finish;
1913
Miklos Szeredid7133112006-04-10 22:54:55 -07001914 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001915 err = -ENOENT;
1916 if (!fc->connected)
1917 goto err_unlock;
1918
Miklos Szeredi334f4852005-09-09 13:10:27 -07001919 req = request_find(fc, oh.unique);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001920 if (!req)
1921 goto err_unlock;
1922
Miklos Szeredif9a28422006-06-25 05:48:53 -07001923 if (req->aborted) {
Miklos Szeredid7133112006-04-10 22:54:55 -07001924 spin_unlock(&fc->lock);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001925 fuse_copy_finish(cs);
Miklos Szeredid7133112006-04-10 22:54:55 -07001926 spin_lock(&fc->lock);
Miklos Szeredi222f1d62006-01-16 22:14:25 -08001927 request_end(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001928 return -ENOENT;
1929 }
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001930 /* Is it an interrupt reply? */
1931 if (req->intr_unique == oh.unique) {
1932 err = -EINVAL;
1933 if (nbytes != sizeof(struct fuse_out_header))
1934 goto err_unlock;
1935
1936 if (oh.error == -ENOSYS)
1937 fc->no_interrupt = 1;
1938 else if (oh.error == -EAGAIN)
1939 queue_interrupt(fc, req);
1940
1941 spin_unlock(&fc->lock);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001942 fuse_copy_finish(cs);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001943 return nbytes;
1944 }
1945
1946 req->state = FUSE_REQ_WRITING;
Miklos Szeredid77a1d52006-01-16 22:14:31 -08001947 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001948 req->out.h = oh;
1949 req->locked = 1;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001950 cs->req = req;
Miklos Szeredice534fb2010-05-25 15:06:07 +02001951 if (!req->out.page_replace)
1952 cs->move_pages = 0;
Miklos Szeredid7133112006-04-10 22:54:55 -07001953 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001954
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001955 err = copy_out_args(cs, &req->out, nbytes);
1956 fuse_copy_finish(cs);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001957
Miklos Szeredid7133112006-04-10 22:54:55 -07001958 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001959 req->locked = 0;
1960 if (!err) {
Miklos Szeredif9a28422006-06-25 05:48:53 -07001961 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001962 err = -ENOENT;
Miklos Szeredif9a28422006-06-25 05:48:53 -07001963 } else if (!req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001964 req->out.h.error = -EIO;
1965 request_end(fc, req);
1966
1967 return err ? err : nbytes;
1968
1969 err_unlock:
Miklos Szeredid7133112006-04-10 22:54:55 -07001970 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001971 err_finish:
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001972 fuse_copy_finish(cs);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001973 return err;
1974}
1975
Al Virofbdbacc2015-04-03 21:53:39 -04001976static ssize_t fuse_dev_write(struct kiocb *iocb, struct iov_iter *from)
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001977{
1978 struct fuse_copy_state cs;
1979 struct fuse_conn *fc = fuse_get_conn(iocb->ki_filp);
1980 if (!fc)
1981 return -EPERM;
1982
Al Virofbdbacc2015-04-03 21:53:39 -04001983 if (!iter_is_iovec(from))
1984 return -EINVAL;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001985
Al Viro6c09e942015-04-03 22:06:08 -04001986 fuse_copy_init(&cs, fc, 0, from);
Al Virofbdbacc2015-04-03 21:53:39 -04001987
1988 return fuse_dev_do_write(fc, &cs, iov_iter_count(from));
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001989}
1990
1991static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe,
1992 struct file *out, loff_t *ppos,
1993 size_t len, unsigned int flags)
1994{
1995 unsigned nbuf;
1996 unsigned idx;
1997 struct pipe_buffer *bufs;
1998 struct fuse_copy_state cs;
1999 struct fuse_conn *fc;
2000 size_t rem;
2001 ssize_t ret;
2002
2003 fc = fuse_get_conn(out);
2004 if (!fc)
2005 return -EPERM;
2006
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01002007 bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), GFP_KERNEL);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002008 if (!bufs)
2009 return -ENOMEM;
2010
2011 pipe_lock(pipe);
2012 nbuf = 0;
2013 rem = 0;
2014 for (idx = 0; idx < pipe->nrbufs && rem < len; idx++)
2015 rem += pipe->bufs[(pipe->curbuf + idx) & (pipe->buffers - 1)].len;
2016
2017 ret = -EINVAL;
2018 if (rem < len) {
2019 pipe_unlock(pipe);
2020 goto out;
2021 }
2022
2023 rem = len;
2024 while (rem) {
2025 struct pipe_buffer *ibuf;
2026 struct pipe_buffer *obuf;
2027
2028 BUG_ON(nbuf >= pipe->buffers);
2029 BUG_ON(!pipe->nrbufs);
2030 ibuf = &pipe->bufs[pipe->curbuf];
2031 obuf = &bufs[nbuf];
2032
2033 if (rem >= ibuf->len) {
2034 *obuf = *ibuf;
2035 ibuf->ops = NULL;
2036 pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
2037 pipe->nrbufs--;
2038 } else {
2039 ibuf->ops->get(pipe, ibuf);
2040 *obuf = *ibuf;
2041 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
2042 obuf->len = rem;
2043 ibuf->offset += obuf->len;
2044 ibuf->len -= obuf->len;
2045 }
2046 nbuf++;
2047 rem -= obuf->len;
2048 }
2049 pipe_unlock(pipe);
2050
Al Viro6c09e942015-04-03 22:06:08 -04002051 fuse_copy_init(&cs, fc, 0, NULL);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002052 cs.pipebufs = bufs;
Al Viro6c09e942015-04-03 22:06:08 -04002053 cs.nr_segs = nbuf;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002054 cs.pipe = pipe;
2055
Miklos Szeredice534fb2010-05-25 15:06:07 +02002056 if (flags & SPLICE_F_MOVE)
2057 cs.move_pages = 1;
2058
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002059 ret = fuse_dev_do_write(fc, &cs, len);
2060
2061 for (idx = 0; idx < nbuf; idx++) {
2062 struct pipe_buffer *buf = &bufs[idx];
2063 buf->ops->release(pipe, buf);
2064 }
2065out:
2066 kfree(bufs);
2067 return ret;
2068}
2069
Miklos Szeredi334f4852005-09-09 13:10:27 -07002070static unsigned fuse_dev_poll(struct file *file, poll_table *wait)
2071{
Miklos Szeredi334f4852005-09-09 13:10:27 -07002072 unsigned mask = POLLOUT | POLLWRNORM;
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07002073 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002074 if (!fc)
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07002075 return POLLERR;
Miklos Szeredi334f4852005-09-09 13:10:27 -07002076
2077 poll_wait(file, &fc->waitq, wait);
2078
Miklos Szeredid7133112006-04-10 22:54:55 -07002079 spin_lock(&fc->lock);
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07002080 if (!fc->connected)
2081 mask = POLLERR;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07002082 else if (request_pending(fc))
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07002083 mask |= POLLIN | POLLRDNORM;
Miklos Szeredid7133112006-04-10 22:54:55 -07002084 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002085
2086 return mask;
2087}
2088
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002089/*
2090 * Abort all requests on the given list (pending or processing)
2091 *
Miklos Szeredid7133112006-04-10 22:54:55 -07002092 * This function releases and reacquires fc->lock
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002093 */
Miklos Szeredi334f4852005-09-09 13:10:27 -07002094static void end_requests(struct fuse_conn *fc, struct list_head *head)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02002095__releases(fc->lock)
2096__acquires(fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -07002097{
2098 while (!list_empty(head)) {
2099 struct fuse_req *req;
2100 req = list_entry(head->next, struct fuse_req, list);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002101 req->out.h.error = -ECONNABORTED;
2102 request_end(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -07002103 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002104 }
2105}
2106
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002107/*
2108 * Abort requests under I/O
2109 *
Miklos Szeredif9a28422006-06-25 05:48:53 -07002110 * The requests are set to aborted and finished, and the request
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002111 * waiter is woken up. This will make request_wait_answer() wait
2112 * until the request is unlocked and then return.
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08002113 *
2114 * If the request is asynchronous, then the end function needs to be
2115 * called after waiting for the request to be unlocked (if it was
2116 * locked).
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002117 */
2118static void end_io_requests(struct fuse_conn *fc)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02002119__releases(fc->lock)
2120__acquires(fc->lock)
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002121{
2122 while (!list_empty(&fc->io)) {
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08002123 struct fuse_req *req =
2124 list_entry(fc->io.next, struct fuse_req, list);
2125 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
2126
Miklos Szeredif9a28422006-06-25 05:48:53 -07002127 req->aborted = 1;
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002128 req->out.h.error = -ECONNABORTED;
2129 req->state = FUSE_REQ_FINISHED;
2130 list_del_init(&req->list);
2131 wake_up(&req->waitq);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08002132 if (end) {
2133 req->end = NULL;
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08002134 __fuse_get_request(req);
Miklos Szeredid7133112006-04-10 22:54:55 -07002135 spin_unlock(&fc->lock);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08002136 wait_event(req->waitq, !req->locked);
2137 end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +01002138 fuse_put_request(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -07002139 spin_lock(&fc->lock);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08002140 }
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002141 }
2142}
2143
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002144static void end_queued_requests(struct fuse_conn *fc)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02002145__releases(fc->lock)
2146__acquires(fc->lock)
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002147{
2148 fc->max_background = UINT_MAX;
2149 flush_bg_queue(fc);
2150 end_requests(fc, &fc->pending);
2151 end_requests(fc, &fc->processing);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01002152 while (forget_pending(fc))
Miklos Szeredi02c048b2010-12-07 20:16:56 +01002153 kfree(dequeue_forget(fc, 1, NULL));
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002154}
2155
Bryan Green357ccf22011-03-01 16:43:52 -08002156static void end_polls(struct fuse_conn *fc)
2157{
2158 struct rb_node *p;
2159
2160 p = rb_first(&fc->polled_files);
2161
2162 while (p) {
2163 struct fuse_file *ff;
2164 ff = rb_entry(p, struct fuse_file, polled_node);
2165 wake_up_interruptible_all(&ff->poll_wait);
2166
2167 p = rb_next(p);
2168 }
2169}
2170
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002171/*
2172 * Abort all requests.
2173 *
2174 * Emergency exit in case of a malicious or accidental deadlock, or
2175 * just a hung filesystem.
2176 *
2177 * The same effect is usually achievable through killing the
2178 * filesystem daemon and all users of the filesystem. The exception
2179 * is the combination of an asynchronous request and the tricky
2180 * deadlock (see Documentation/filesystems/fuse.txt).
2181 *
2182 * During the aborting, progression of requests from the pending and
2183 * processing lists onto the io list, and progression of new requests
2184 * onto the pending list is prevented by req->connected being false.
2185 *
2186 * Progression of requests under I/O to the processing list is
Miklos Szeredif9a28422006-06-25 05:48:53 -07002187 * prevented by the req->aborted flag being true for these requests.
2188 * For this reason requests on the io list must be aborted first.
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002189 */
2190void fuse_abort_conn(struct fuse_conn *fc)
2191{
Miklos Szeredid7133112006-04-10 22:54:55 -07002192 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002193 if (fc->connected) {
2194 fc->connected = 0;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -07002195 fc->blocked = 0;
Miklos Szeredi9759bd512015-01-06 10:45:35 +01002196 fuse_set_initialized(fc);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002197 end_io_requests(fc);
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002198 end_queued_requests(fc);
Bryan Green357ccf22011-03-01 16:43:52 -08002199 end_polls(fc);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002200 wake_up_all(&fc->waitq);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -07002201 wake_up_all(&fc->blocked_waitq);
Jeff Dike385a17b2006-04-10 22:54:52 -07002202 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002203 }
Miklos Szeredid7133112006-04-10 22:54:55 -07002204 spin_unlock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002205}
Tejun Heo08cbf542009-04-14 10:54:53 +09002206EXPORT_SYMBOL_GPL(fuse_abort_conn);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002207
Tejun Heo08cbf542009-04-14 10:54:53 +09002208int fuse_dev_release(struct inode *inode, struct file *file)
Miklos Szeredi334f4852005-09-09 13:10:27 -07002209{
Miklos Szeredi0720b312006-04-10 22:54:55 -07002210 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002211 if (fc) {
Miklos Szeredid7133112006-04-10 22:54:55 -07002212 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -07002213 fc->connected = 0;
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002214 fc->blocked = 0;
Miklos Szeredi9759bd512015-01-06 10:45:35 +01002215 fuse_set_initialized(fc);
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002216 end_queued_requests(fc);
Bryan Green357ccf22011-03-01 16:43:52 -08002217 end_polls(fc);
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002218 wake_up_all(&fc->blocked_waitq);
Miklos Szeredid7133112006-04-10 22:54:55 -07002219 spin_unlock(&fc->lock);
Miklos Szeredibafa9652006-06-25 05:48:51 -07002220 fuse_conn_put(fc);
Jeff Dike385a17b2006-04-10 22:54:52 -07002221 }
Miklos Szeredif543f252006-01-16 22:14:35 -08002222
Miklos Szeredi334f4852005-09-09 13:10:27 -07002223 return 0;
2224}
Tejun Heo08cbf542009-04-14 10:54:53 +09002225EXPORT_SYMBOL_GPL(fuse_dev_release);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002226
Jeff Dike385a17b2006-04-10 22:54:52 -07002227static int fuse_dev_fasync(int fd, struct file *file, int on)
2228{
2229 struct fuse_conn *fc = fuse_get_conn(file);
2230 if (!fc)
Miklos Szeredia87046d2006-04-10 22:54:56 -07002231 return -EPERM;
Jeff Dike385a17b2006-04-10 22:54:52 -07002232
2233 /* No locking - fasync_helper does its own locking */
2234 return fasync_helper(fd, file, on, &fc->fasync);
2235}
2236
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002237const struct file_operations fuse_dev_operations = {
Miklos Szeredi334f4852005-09-09 13:10:27 -07002238 .owner = THIS_MODULE,
Tom Van Braeckel94e4fe22015-01-12 05:22:16 +01002239 .open = fuse_dev_open,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002240 .llseek = no_llseek,
Al Virofbdbacc2015-04-03 21:53:39 -04002241 .read_iter = fuse_dev_read,
Miklos Szeredic3021622010-05-25 15:06:07 +02002242 .splice_read = fuse_dev_splice_read,
Al Virofbdbacc2015-04-03 21:53:39 -04002243 .write_iter = fuse_dev_write,
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002244 .splice_write = fuse_dev_splice_write,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002245 .poll = fuse_dev_poll,
2246 .release = fuse_dev_release,
Jeff Dike385a17b2006-04-10 22:54:52 -07002247 .fasync = fuse_dev_fasync,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002248};
Tejun Heo08cbf542009-04-14 10:54:53 +09002249EXPORT_SYMBOL_GPL(fuse_dev_operations);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002250
2251static struct miscdevice fuse_miscdevice = {
2252 .minor = FUSE_MINOR,
2253 .name = "fuse",
2254 .fops = &fuse_dev_operations,
2255};
2256
2257int __init fuse_dev_init(void)
2258{
2259 int err = -ENOMEM;
2260 fuse_req_cachep = kmem_cache_create("fuse_request",
2261 sizeof(struct fuse_req),
Paul Mundt20c2df82007-07-20 10:11:58 +09002262 0, 0, NULL);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002263 if (!fuse_req_cachep)
2264 goto out;
2265
2266 err = misc_register(&fuse_miscdevice);
2267 if (err)
2268 goto out_cache_clean;
2269
2270 return 0;
2271
2272 out_cache_clean:
2273 kmem_cache_destroy(fuse_req_cachep);
2274 out:
2275 return err;
2276}
2277
2278void fuse_dev_cleanup(void)
2279{
2280 misc_deregister(&fuse_miscdevice);
2281 kmem_cache_destroy(fuse_req_cachep);
2282}