blob: 3b979abb7b54cd5de5907692409bb06968a8f2d2 [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
Miklos Szeredide155222015-07-01 16:25:57 +0200171 err = -ECONNREFUSED;
172 if (fc->conn_error)
173 goto out;
174
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400175 req = fuse_request_alloc(npages);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200176 err = -ENOMEM;
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400177 if (!req) {
178 if (for_background)
179 wake_up(&fc->blocked_waitq);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200180 goto out;
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400181 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700182
Miklos Szeredi33649c92006-06-25 05:48:52 -0700183 fuse_req_init_context(req);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200184 req->waiting = 1;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400185 req->background = for_background;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700186 return req;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200187
188 out:
189 atomic_dec(&fc->num_waiting);
190 return ERR_PTR(err);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700191}
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400192
193struct fuse_req *fuse_get_req(struct fuse_conn *fc, unsigned npages)
194{
195 return __fuse_get_req(fc, npages, false);
196}
Tejun Heo08cbf542009-04-14 10:54:53 +0900197EXPORT_SYMBOL_GPL(fuse_get_req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700198
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400199struct fuse_req *fuse_get_req_for_background(struct fuse_conn *fc,
200 unsigned npages)
201{
202 return __fuse_get_req(fc, npages, true);
203}
204EXPORT_SYMBOL_GPL(fuse_get_req_for_background);
205
Miklos Szeredi33649c92006-06-25 05:48:52 -0700206/*
207 * Return request in fuse_file->reserved_req. However that may
208 * currently be in use. If that is the case, wait for it to become
209 * available.
210 */
211static struct fuse_req *get_reserved_req(struct fuse_conn *fc,
212 struct file *file)
213{
214 struct fuse_req *req = NULL;
215 struct fuse_file *ff = file->private_data;
216
217 do {
Miklos Szeredide5e3de2007-10-16 23:31:00 -0700218 wait_event(fc->reserved_req_waitq, ff->reserved_req);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700219 spin_lock(&fc->lock);
220 if (ff->reserved_req) {
221 req = ff->reserved_req;
222 ff->reserved_req = NULL;
Al Virocb0942b2012-08-27 14:48:26 -0400223 req->stolen_file = get_file(file);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700224 }
225 spin_unlock(&fc->lock);
226 } while (!req);
227
228 return req;
229}
230
231/*
232 * Put stolen request back into fuse_file->reserved_req
233 */
234static void put_reserved_req(struct fuse_conn *fc, struct fuse_req *req)
235{
236 struct file *file = req->stolen_file;
237 struct fuse_file *ff = file->private_data;
238
239 spin_lock(&fc->lock);
Maxim Patlasovb2430d72012-10-26 19:49:24 +0400240 fuse_request_init(req, req->pages, req->page_descs, req->max_pages);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700241 BUG_ON(ff->reserved_req);
242 ff->reserved_req = req;
Miklos Szeredide5e3de2007-10-16 23:31:00 -0700243 wake_up_all(&fc->reserved_req_waitq);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700244 spin_unlock(&fc->lock);
245 fput(file);
246}
247
248/*
249 * Gets a requests for a file operation, always succeeds
250 *
251 * This is used for sending the FLUSH request, which must get to
252 * userspace, due to POSIX locks which may need to be unlocked.
253 *
254 * If allocation fails due to OOM, use the reserved request in
255 * fuse_file.
256 *
257 * This is very unlikely to deadlock accidentally, since the
258 * filesystem should not have it's own file open. If deadlock is
259 * intentional, it can still be broken by "aborting" the filesystem.
260 */
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400261struct fuse_req *fuse_get_req_nofail_nopages(struct fuse_conn *fc,
262 struct file *file)
Miklos Szeredi33649c92006-06-25 05:48:52 -0700263{
264 struct fuse_req *req;
265
266 atomic_inc(&fc->num_waiting);
Maxim Patlasov0aada882013-03-21 18:02:28 +0400267 wait_event(fc->blocked_waitq, fc->initialized);
Miklos Szeredi9759bd512015-01-06 10:45:35 +0100268 /* Matches smp_wmb() in fuse_set_initialized() */
269 smp_rmb();
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400270 req = fuse_request_alloc(0);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700271 if (!req)
272 req = get_reserved_req(fc, file);
273
274 fuse_req_init_context(req);
275 req->waiting = 1;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400276 req->background = 0;
Miklos Szeredi33649c92006-06-25 05:48:52 -0700277 return req;
278}
279
Miklos Szeredi334f4852005-09-09 13:10:27 -0700280void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req)
281{
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800282 if (atomic_dec_and_test(&req->count)) {
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400283 if (unlikely(req->background)) {
284 /*
285 * We get here in the unlikely case that a background
286 * request was allocated but not sent
287 */
288 spin_lock(&fc->lock);
289 if (!fc->blocked)
290 wake_up(&fc->blocked_waitq);
291 spin_unlock(&fc->lock);
292 }
293
Miklos Szeredi73e0e732015-07-01 16:25:56 +0200294 if (req->waiting) {
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200295 atomic_dec(&fc->num_waiting);
Miklos Szeredi73e0e732015-07-01 16:25:56 +0200296 req->waiting = 0;
297 }
Miklos Szeredi33649c92006-06-25 05:48:52 -0700298
299 if (req->stolen_file)
300 put_reserved_req(fc, req);
301 else
302 fuse_request_free(req);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800303 }
304}
Tejun Heo08cbf542009-04-14 10:54:53 +0900305EXPORT_SYMBOL_GPL(fuse_put_request);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800306
Miklos Szeredid12def12008-02-06 01:38:39 -0800307static unsigned len_args(unsigned numargs, struct fuse_arg *args)
308{
309 unsigned nbytes = 0;
310 unsigned i;
311
312 for (i = 0; i < numargs; i++)
313 nbytes += args[i].size;
314
315 return nbytes;
316}
317
318static u64 fuse_get_unique(struct fuse_conn *fc)
319{
320 fc->reqctr++;
321 /* zero is special */
322 if (fc->reqctr == 0)
323 fc->reqctr = 1;
324
325 return fc->reqctr;
326}
327
328static void queue_request(struct fuse_conn *fc, struct fuse_req *req)
329{
Miklos Szeredid12def12008-02-06 01:38:39 -0800330 req->in.h.len = sizeof(struct fuse_in_header) +
331 len_args(req->in.numargs, (struct fuse_arg *) req->in.args);
332 list_add_tail(&req->list, &fc->pending);
333 req->state = FUSE_REQ_PENDING;
Miklos Szeredid12def12008-02-06 01:38:39 -0800334 wake_up(&fc->waitq);
335 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
336}
337
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100338void fuse_queue_forget(struct fuse_conn *fc, struct fuse_forget_link *forget,
339 u64 nodeid, u64 nlookup)
340{
Miklos Szeredi02c048b2010-12-07 20:16:56 +0100341 forget->forget_one.nodeid = nodeid;
342 forget->forget_one.nlookup = nlookup;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100343
344 spin_lock(&fc->lock);
Miklos Szeredi5dfcc872011-09-12 09:38:03 +0200345 if (fc->connected) {
346 fc->forget_list_tail->next = forget;
347 fc->forget_list_tail = forget;
348 wake_up(&fc->waitq);
349 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
350 } else {
351 kfree(forget);
352 }
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100353 spin_unlock(&fc->lock);
354}
355
Miklos Szeredid12def12008-02-06 01:38:39 -0800356static void flush_bg_queue(struct fuse_conn *fc)
357{
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700358 while (fc->active_background < fc->max_background &&
Miklos Szeredid12def12008-02-06 01:38:39 -0800359 !list_empty(&fc->bg_queue)) {
360 struct fuse_req *req;
361
362 req = list_entry(fc->bg_queue.next, struct fuse_req, list);
363 list_del(&req->list);
364 fc->active_background++;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200365 req->in.h.unique = fuse_get_unique(fc);
Miklos Szeredid12def12008-02-06 01:38:39 -0800366 queue_request(fc, req);
367 }
368}
369
Miklos Szeredi6dbbcb12006-04-26 10:49:06 +0200370/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700371 * This function is called when a request is finished. Either a reply
Miklos Szeredif9a28422006-06-25 05:48:53 -0700372 * has arrived or it was aborted (and not yet sent) or some error
Miklos Szeredif43b1552006-01-16 22:14:26 -0800373 * occurred during communication with userspace, or the device file
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700374 * was closed. The requester thread is woken up (if still waiting),
375 * the 'end' callback is called if given, else the reference to the
376 * request is released
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800377 *
Miklos Szeredid7133112006-04-10 22:54:55 -0700378 * Called with fc->lock, unlocks it
Miklos Szeredi334f4852005-09-09 13:10:27 -0700379 */
380static void request_end(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +0200381__releases(fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700382{
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700383 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
384 req->end = NULL;
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200385 list_del_init(&req->list);
386 list_del_init(&req->intr_entry);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800387 req->state = FUSE_REQ_FINISHED;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700388 if (req->background) {
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400389 req->background = 0;
390
391 if (fc->num_background == fc->max_background)
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700392 fc->blocked = 0;
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400393
394 /* Wake up next waiter, if any */
Miklos Szeredi3c18ef82013-04-17 21:50:58 +0200395 if (!fc->blocked && waitqueue_active(&fc->blocked_waitq))
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400396 wake_up(&fc->blocked_waitq);
397
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700398 if (fc->num_background == fc->congestion_threshold &&
Tejun Heoa325f9b2009-04-14 10:54:52 +0900399 fc->connected && fc->bdi_initialized) {
Jens Axboe8aa7e842009-07-09 14:52:32 +0200400 clear_bdi_congested(&fc->bdi, BLK_RW_SYNC);
401 clear_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
Miklos Szeredif92b99b2007-10-16 23:30:59 -0700402 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700403 fc->num_background--;
Miklos Szeredid12def12008-02-06 01:38:39 -0800404 fc->active_background--;
405 flush_bg_queue(fc);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700406 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700407 spin_unlock(&fc->lock);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700408 wake_up(&req->waitq);
409 if (end)
410 end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100411 fuse_put_request(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700412}
413
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700414static void wait_answer_interruptible(struct fuse_conn *fc,
415 struct fuse_req *req)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +0200416__releases(fc->lock)
417__acquires(fc->lock)
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700418{
419 if (signal_pending(current))
420 return;
421
422 spin_unlock(&fc->lock);
423 wait_event_interruptible(req->waitq, req->state == FUSE_REQ_FINISHED);
424 spin_lock(&fc->lock);
425}
426
427static void queue_interrupt(struct fuse_conn *fc, struct fuse_req *req)
428{
429 list_add_tail(&req->intr_entry, &fc->interrupts);
430 wake_up(&fc->waitq);
431 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
432}
433
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700434static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +0200435__releases(fc->lock)
436__acquires(fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700437{
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700438 if (!fc->no_interrupt) {
439 /* Any signal may interrupt this */
440 wait_answer_interruptible(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700441
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700442 if (req->state == FUSE_REQ_FINISHED)
443 return;
444
445 req->interrupted = 1;
446 if (req->state == FUSE_REQ_SENT)
447 queue_interrupt(fc, req);
448 }
449
Miklos Szeredia131de02007-10-16 23:31:04 -0700450 if (!req->force) {
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700451 sigset_t oldset;
452
453 /* Only fatal signals may interrupt this */
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700454 block_sigs(&oldset);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700455 wait_answer_interruptible(fc, req);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700456 restore_sigs(&oldset);
Miklos Szeredia131de02007-10-16 23:31:04 -0700457
Miklos Szeredia131de02007-10-16 23:31:04 -0700458 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 Szeredi334f4852005-09-09 13:10:27 -0700477}
478
Eric Wong6a4e9222013-02-04 13:04:44 +0000479static void __fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700480{
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400481 BUG_ON(req->background);
Miklos Szeredid7133112006-04-10 22:54:55 -0700482 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700483 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700484 req->out.h.error = -ENOTCONN;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700485 else {
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200486 req->in.h.unique = fuse_get_unique(fc);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700487 queue_request(fc, req);
488 /* acquire extra reference, since request is still needed
489 after request_end() */
490 __fuse_get_request(req);
491
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700492 request_wait_answer(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700493 }
Miklos Szeredid7133112006-04-10 22:54:55 -0700494 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700495}
Eric Wong6a4e9222013-02-04 13:04:44 +0000496
497void fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
498{
499 req->isreply = 1;
Miklos Szeredi5437f242015-07-01 16:25:56 +0200500 if (!req->waiting) {
501 req->waiting = 1;
502 atomic_inc(&fc->num_waiting);
503 }
Eric Wong6a4e9222013-02-04 13:04:44 +0000504 __fuse_request_send(fc, req);
505}
Tejun Heo08cbf542009-04-14 10:54:53 +0900506EXPORT_SYMBOL_GPL(fuse_request_send);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700507
Miklos Szeredi21f62172015-01-06 10:45:35 +0100508static void fuse_adjust_compat(struct fuse_conn *fc, struct fuse_args *args)
509{
510 if (fc->minor < 4 && args->in.h.opcode == FUSE_STATFS)
511 args->out.args[0].size = FUSE_COMPAT_STATFS_SIZE;
512
513 if (fc->minor < 9) {
514 switch (args->in.h.opcode) {
515 case FUSE_LOOKUP:
516 case FUSE_CREATE:
517 case FUSE_MKNOD:
518 case FUSE_MKDIR:
519 case FUSE_SYMLINK:
520 case FUSE_LINK:
521 args->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
522 break;
523 case FUSE_GETATTR:
524 case FUSE_SETATTR:
525 args->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
526 break;
527 }
528 }
529 if (fc->minor < 12) {
530 switch (args->in.h.opcode) {
531 case FUSE_CREATE:
532 args->in.args[0].size = sizeof(struct fuse_open_in);
533 break;
534 case FUSE_MKNOD:
535 args->in.args[0].size = FUSE_COMPAT_MKNOD_IN_SIZE;
536 break;
537 }
538 }
539}
540
Miklos Szeredi70781872014-12-12 09:49:05 +0100541ssize_t fuse_simple_request(struct fuse_conn *fc, struct fuse_args *args)
542{
543 struct fuse_req *req;
544 ssize_t ret;
545
546 req = fuse_get_req(fc, 0);
547 if (IS_ERR(req))
548 return PTR_ERR(req);
549
Miklos Szeredi21f62172015-01-06 10:45:35 +0100550 /* Needs to be done after fuse_get_req() so that fc->minor is valid */
551 fuse_adjust_compat(fc, args);
552
Miklos Szeredi70781872014-12-12 09:49:05 +0100553 req->in.h.opcode = args->in.h.opcode;
554 req->in.h.nodeid = args->in.h.nodeid;
555 req->in.numargs = args->in.numargs;
556 memcpy(req->in.args, args->in.args,
557 args->in.numargs * sizeof(struct fuse_in_arg));
558 req->out.argvar = args->out.argvar;
559 req->out.numargs = args->out.numargs;
560 memcpy(req->out.args, args->out.args,
561 args->out.numargs * sizeof(struct fuse_arg));
562 fuse_request_send(fc, req);
563 ret = req->out.h.error;
564 if (!ret && args->out.argvar) {
565 BUG_ON(args->out.numargs != 1);
566 ret = req->out.args[0].size;
567 }
568 fuse_put_request(fc, req);
569
570 return ret;
571}
572
Miklos Szeredif0139aa2015-07-01 16:25:57 +0200573/*
574 * Called under fc->lock
575 *
576 * fc->connected must have been checked previously
577 */
578void fuse_request_send_background_locked(struct fuse_conn *fc,
579 struct fuse_req *req)
Miklos Szeredid12def12008-02-06 01:38:39 -0800580{
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400581 BUG_ON(!req->background);
Miklos Szeredi5437f242015-07-01 16:25:56 +0200582 if (!req->waiting) {
583 req->waiting = 1;
584 atomic_inc(&fc->num_waiting);
585 }
Miklos Szeredif0139aa2015-07-01 16:25:57 +0200586 req->isreply = 1;
Miklos Szeredid12def12008-02-06 01:38:39 -0800587 fc->num_background++;
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700588 if (fc->num_background == fc->max_background)
Miklos Szeredid12def12008-02-06 01:38:39 -0800589 fc->blocked = 1;
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700590 if (fc->num_background == fc->congestion_threshold &&
Tejun Heoa325f9b2009-04-14 10:54:52 +0900591 fc->bdi_initialized) {
Jens Axboe8aa7e842009-07-09 14:52:32 +0200592 set_bdi_congested(&fc->bdi, BLK_RW_SYNC);
593 set_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
Miklos Szeredid12def12008-02-06 01:38:39 -0800594 }
595 list_add_tail(&req->list, &fc->bg_queue);
596 flush_bg_queue(fc);
597}
598
Miklos Szeredif0139aa2015-07-01 16:25:57 +0200599void fuse_request_send_background(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700600{
Miklos Szeredi42dc6212015-07-01 16:25:56 +0200601 BUG_ON(!req->end);
Miklos Szeredid7133112006-04-10 22:54:55 -0700602 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700603 if (fc->connected) {
Miklos Szeredif0139aa2015-07-01 16:25:57 +0200604 fuse_request_send_background_locked(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700605 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700606 } else {
Miklos Szeredi42dc6212015-07-01 16:25:56 +0200607 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700608 req->out.h.error = -ENOTCONN;
Miklos Szeredi42dc6212015-07-01 16:25:56 +0200609 req->end(fc, req);
610 fuse_put_request(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700611 }
612}
Tejun Heo08cbf542009-04-14 10:54:53 +0900613EXPORT_SYMBOL_GPL(fuse_request_send_background);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700614
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200615static int fuse_request_send_notify_reply(struct fuse_conn *fc,
616 struct fuse_req *req, u64 unique)
617{
618 int err = -ENODEV;
619
620 req->isreply = 0;
621 req->in.h.unique = unique;
622 spin_lock(&fc->lock);
623 if (fc->connected) {
624 queue_request(fc, req);
625 err = 0;
626 }
627 spin_unlock(&fc->lock);
628
629 return err;
630}
631
Anand V. Avati0b05b182012-08-19 08:53:23 -0400632void fuse_force_forget(struct file *file, u64 nodeid)
633{
Al Viro6131ffa2013-02-27 16:59:05 -0500634 struct inode *inode = file_inode(file);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400635 struct fuse_conn *fc = get_fuse_conn(inode);
636 struct fuse_req *req;
637 struct fuse_forget_in inarg;
638
639 memset(&inarg, 0, sizeof(inarg));
640 inarg.nlookup = 1;
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400641 req = fuse_get_req_nofail_nopages(fc, file);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400642 req->in.h.opcode = FUSE_FORGET;
643 req->in.h.nodeid = nodeid;
644 req->in.numargs = 1;
645 req->in.args[0].size = sizeof(inarg);
646 req->in.args[0].value = &inarg;
647 req->isreply = 0;
Eric Wong6a4e9222013-02-04 13:04:44 +0000648 __fuse_request_send(fc, req);
649 /* ignore errors */
650 fuse_put_request(fc, req);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400651}
652
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700653/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700654 * Lock the request. Up to the next unlock_request() there mustn't be
655 * anything that could cause a page-fault. If the request was already
Miklos Szeredif9a28422006-06-25 05:48:53 -0700656 * aborted bail out.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700657 */
Miklos Szeredid7133112006-04-10 22:54:55 -0700658static int lock_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700659{
660 int err = 0;
661 if (req) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700662 spin_lock(&fc->lock);
Miklos Szeredif9a28422006-06-25 05:48:53 -0700663 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700664 err = -ENOENT;
665 else
666 req->locked = 1;
Miklos Szeredid7133112006-04-10 22:54:55 -0700667 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700668 }
669 return err;
670}
671
672/*
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200673 * Unlock request. If it was aborted while locked, caller is responsible
674 * for unlocking and ending the request.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700675 */
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200676static int unlock_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700677{
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200678 int err = 0;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700679 if (req) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700680 spin_lock(&fc->lock);
Miklos Szeredif9a28422006-06-25 05:48:53 -0700681 if (req->aborted)
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200682 err = -ENOENT;
683 else
684 req->locked = 0;
Miklos Szeredid7133112006-04-10 22:54:55 -0700685 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700686 }
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200687 return err;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700688}
689
690struct fuse_copy_state {
Miklos Szeredid7133112006-04-10 22:54:55 -0700691 struct fuse_conn *fc;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700692 int write;
693 struct fuse_req *req;
Al Viro6c09e942015-04-03 22:06:08 -0400694 struct iov_iter *iter;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200695 struct pipe_buffer *pipebufs;
696 struct pipe_buffer *currbuf;
697 struct pipe_inode_info *pipe;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700698 unsigned long nr_segs;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700699 struct page *pg;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700700 unsigned len;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200701 unsigned offset;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200702 unsigned move_pages:1;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700703};
704
Al Viro6c09e942015-04-03 22:06:08 -0400705static void fuse_copy_init(struct fuse_copy_state *cs,
706 struct fuse_conn *fc,
Miklos Szeredic3021622010-05-25 15:06:07 +0200707 int write,
Al Viro6c09e942015-04-03 22:06:08 -0400708 struct iov_iter *iter)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700709{
710 memset(cs, 0, sizeof(*cs));
Miklos Szeredid7133112006-04-10 22:54:55 -0700711 cs->fc = fc;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700712 cs->write = write;
Al Viro6c09e942015-04-03 22:06:08 -0400713 cs->iter = iter;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700714}
715
716/* Unmap and put previous page of userspace buffer */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800717static void fuse_copy_finish(struct fuse_copy_state *cs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700718{
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200719 if (cs->currbuf) {
720 struct pipe_buffer *buf = cs->currbuf;
721
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200722 if (cs->write)
Miklos Szeredic3021622010-05-25 15:06:07 +0200723 buf->len = PAGE_SIZE - cs->len;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200724 cs->currbuf = NULL;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200725 } else if (cs->pg) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700726 if (cs->write) {
727 flush_dcache_page(cs->pg);
728 set_page_dirty_lock(cs->pg);
729 }
730 put_page(cs->pg);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700731 }
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200732 cs->pg = NULL;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700733}
734
735/*
736 * Get another pagefull of userspace buffer, and map it to kernel
737 * address space, and lock request
738 */
739static int fuse_copy_fill(struct fuse_copy_state *cs)
740{
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200741 struct page *page;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700742 int err;
743
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200744 err = unlock_request(cs->fc, cs->req);
745 if (err)
746 return err;
747
Miklos Szeredi334f4852005-09-09 13:10:27 -0700748 fuse_copy_finish(cs);
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200749 if (cs->pipebufs) {
750 struct pipe_buffer *buf = cs->pipebufs;
751
Miklos Szeredic3021622010-05-25 15:06:07 +0200752 if (!cs->write) {
753 err = buf->ops->confirm(cs->pipe, buf);
754 if (err)
755 return err;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200756
Miklos Szeredic3021622010-05-25 15:06:07 +0200757 BUG_ON(!cs->nr_segs);
758 cs->currbuf = buf;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200759 cs->pg = buf->page;
760 cs->offset = buf->offset;
Miklos Szeredic3021622010-05-25 15:06:07 +0200761 cs->len = buf->len;
Miklos Szeredic3021622010-05-25 15:06:07 +0200762 cs->pipebufs++;
763 cs->nr_segs--;
764 } else {
Miklos Szeredic3021622010-05-25 15:06:07 +0200765 if (cs->nr_segs == cs->pipe->buffers)
766 return -EIO;
767
768 page = alloc_page(GFP_HIGHUSER);
769 if (!page)
770 return -ENOMEM;
771
772 buf->page = page;
773 buf->offset = 0;
774 buf->len = 0;
775
776 cs->currbuf = buf;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200777 cs->pg = page;
778 cs->offset = 0;
Miklos Szeredic3021622010-05-25 15:06:07 +0200779 cs->len = PAGE_SIZE;
780 cs->pipebufs++;
781 cs->nr_segs++;
782 }
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200783 } else {
Al Viro6c09e942015-04-03 22:06:08 -0400784 size_t off;
785 err = iov_iter_get_pages(cs->iter, &page, PAGE_SIZE, 1, &off);
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200786 if (err < 0)
787 return err;
Al Viro6c09e942015-04-03 22:06:08 -0400788 BUG_ON(!err);
789 cs->len = err;
790 cs->offset = off;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200791 cs->pg = page;
Al Viro6c09e942015-04-03 22:06:08 -0400792 cs->offset = off;
793 iov_iter_advance(cs->iter, err);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700794 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700795
Miklos Szeredid7133112006-04-10 22:54:55 -0700796 return lock_request(cs->fc, cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700797}
798
799/* Do as much copy to/from userspace buffer as we can */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800800static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700801{
802 unsigned ncpy = min(*size, cs->len);
803 if (val) {
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200804 void *pgaddr = kmap_atomic(cs->pg);
805 void *buf = pgaddr + cs->offset;
806
Miklos Szeredi334f4852005-09-09 13:10:27 -0700807 if (cs->write)
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200808 memcpy(buf, *val, ncpy);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700809 else
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200810 memcpy(*val, buf, ncpy);
811
812 kunmap_atomic(pgaddr);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700813 *val += ncpy;
814 }
815 *size -= ncpy;
816 cs->len -= ncpy;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200817 cs->offset += ncpy;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700818 return ncpy;
819}
820
Miklos Szeredice534fb2010-05-25 15:06:07 +0200821static int fuse_check_page(struct page *page)
822{
823 if (page_mapcount(page) ||
824 page->mapping != NULL ||
825 page_count(page) != 1 ||
826 (page->flags & PAGE_FLAGS_CHECK_AT_PREP &
827 ~(1 << PG_locked |
828 1 << PG_referenced |
829 1 << PG_uptodate |
830 1 << PG_lru |
831 1 << PG_active |
832 1 << PG_reclaim))) {
833 printk(KERN_WARNING "fuse: trying to steal weird page\n");
834 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);
835 return 1;
836 }
837 return 0;
838}
839
840static int fuse_try_move_page(struct fuse_copy_state *cs, struct page **pagep)
841{
842 int err;
843 struct page *oldpage = *pagep;
844 struct page *newpage;
845 struct pipe_buffer *buf = cs->pipebufs;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200846
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200847 err = unlock_request(cs->fc, cs->req);
848 if (err)
849 return err;
850
Miklos Szeredice534fb2010-05-25 15:06:07 +0200851 fuse_copy_finish(cs);
852
853 err = buf->ops->confirm(cs->pipe, buf);
854 if (err)
855 return err;
856
857 BUG_ON(!cs->nr_segs);
858 cs->currbuf = buf;
859 cs->len = buf->len;
860 cs->pipebufs++;
861 cs->nr_segs--;
862
863 if (cs->len != PAGE_SIZE)
864 goto out_fallback;
865
866 if (buf->ops->steal(cs->pipe, buf) != 0)
867 goto out_fallback;
868
869 newpage = buf->page;
870
Miklos Szerediaa991b32015-02-26 11:45:47 +0100871 if (!PageUptodate(newpage))
872 SetPageUptodate(newpage);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200873
874 ClearPageMappedToDisk(newpage);
875
876 if (fuse_check_page(newpage) != 0)
877 goto out_fallback_unlock;
878
Miklos Szeredice534fb2010-05-25 15:06:07 +0200879 /*
880 * This is a new and locked page, it shouldn't be mapped or
881 * have any special flags on it
882 */
883 if (WARN_ON(page_mapped(oldpage)))
884 goto out_fallback_unlock;
885 if (WARN_ON(page_has_private(oldpage)))
886 goto out_fallback_unlock;
887 if (WARN_ON(PageDirty(oldpage) || PageWriteback(oldpage)))
888 goto out_fallback_unlock;
889 if (WARN_ON(PageMlocked(oldpage)))
890 goto out_fallback_unlock;
891
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700892 err = replace_page_cache_page(oldpage, newpage, GFP_KERNEL);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200893 if (err) {
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700894 unlock_page(newpage);
895 return err;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200896 }
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700897
Miklos Szeredice534fb2010-05-25 15:06:07 +0200898 page_cache_get(newpage);
899
900 if (!(buf->flags & PIPE_BUF_FLAG_LRU))
901 lru_cache_add_file(newpage);
902
903 err = 0;
904 spin_lock(&cs->fc->lock);
905 if (cs->req->aborted)
906 err = -ENOENT;
907 else
908 *pagep = newpage;
909 spin_unlock(&cs->fc->lock);
910
911 if (err) {
912 unlock_page(newpage);
913 page_cache_release(newpage);
914 return err;
915 }
916
917 unlock_page(oldpage);
918 page_cache_release(oldpage);
919 cs->len = 0;
920
921 return 0;
922
923out_fallback_unlock:
924 unlock_page(newpage);
925out_fallback:
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200926 cs->pg = buf->page;
927 cs->offset = buf->offset;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200928
929 err = lock_request(cs->fc, cs->req);
930 if (err)
931 return err;
932
933 return 1;
934}
935
Miklos Szeredic3021622010-05-25 15:06:07 +0200936static int fuse_ref_page(struct fuse_copy_state *cs, struct page *page,
937 unsigned offset, unsigned count)
938{
939 struct pipe_buffer *buf;
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200940 int err;
Miklos Szeredic3021622010-05-25 15:06:07 +0200941
942 if (cs->nr_segs == cs->pipe->buffers)
943 return -EIO;
944
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200945 err = unlock_request(cs->fc, cs->req);
946 if (err)
947 return err;
948
Miklos Szeredic3021622010-05-25 15:06:07 +0200949 fuse_copy_finish(cs);
950
951 buf = cs->pipebufs;
952 page_cache_get(page);
953 buf->page = page;
954 buf->offset = offset;
955 buf->len = count;
956
957 cs->pipebufs++;
958 cs->nr_segs++;
959 cs->len = 0;
960
961 return 0;
962}
963
Miklos Szeredi334f4852005-09-09 13:10:27 -0700964/*
965 * Copy a page in the request to/from the userspace buffer. Must be
966 * done atomically
967 */
Miklos Szeredice534fb2010-05-25 15:06:07 +0200968static int fuse_copy_page(struct fuse_copy_state *cs, struct page **pagep,
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800969 unsigned offset, unsigned count, int zeroing)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700970{
Miklos Szeredice534fb2010-05-25 15:06:07 +0200971 int err;
972 struct page *page = *pagep;
973
Miklos Szeredib6777c42010-10-26 14:22:27 -0700974 if (page && zeroing && count < PAGE_SIZE)
975 clear_highpage(page);
976
Miklos Szeredi334f4852005-09-09 13:10:27 -0700977 while (count) {
Miklos Szeredic3021622010-05-25 15:06:07 +0200978 if (cs->write && cs->pipebufs && page) {
979 return fuse_ref_page(cs, page, offset, count);
980 } else if (!cs->len) {
Miklos Szeredice534fb2010-05-25 15:06:07 +0200981 if (cs->move_pages && page &&
982 offset == 0 && count == PAGE_SIZE) {
983 err = fuse_try_move_page(cs, pagep);
984 if (err <= 0)
985 return err;
986 } else {
987 err = fuse_copy_fill(cs);
988 if (err)
989 return err;
990 }
Miklos Szeredi1729a162008-11-26 12:03:54 +0100991 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700992 if (page) {
Cong Wang2408f6e2011-11-25 23:14:30 +0800993 void *mapaddr = kmap_atomic(page);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700994 void *buf = mapaddr + offset;
995 offset += fuse_copy_do(cs, &buf, &count);
Cong Wang2408f6e2011-11-25 23:14:30 +0800996 kunmap_atomic(mapaddr);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700997 } else
998 offset += fuse_copy_do(cs, NULL, &count);
999 }
1000 if (page && !cs->write)
1001 flush_dcache_page(page);
1002 return 0;
1003}
1004
1005/* Copy pages in the request to/from userspace buffer */
1006static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
1007 int zeroing)
1008{
1009 unsigned i;
1010 struct fuse_req *req = cs->req;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001011
1012 for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
Miklos Szeredice534fb2010-05-25 15:06:07 +02001013 int err;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001014 unsigned offset = req->page_descs[i].offset;
1015 unsigned count = min(nbytes, req->page_descs[i].length);
Miklos Szeredice534fb2010-05-25 15:06:07 +02001016
1017 err = fuse_copy_page(cs, &req->pages[i], offset, count,
1018 zeroing);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001019 if (err)
1020 return err;
1021
1022 nbytes -= count;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001023 }
1024 return 0;
1025}
1026
1027/* Copy a single argument in the request to/from userspace buffer */
1028static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
1029{
1030 while (size) {
Miklos Szeredi1729a162008-11-26 12:03:54 +01001031 if (!cs->len) {
1032 int err = fuse_copy_fill(cs);
1033 if (err)
1034 return err;
1035 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001036 fuse_copy_do(cs, &val, &size);
1037 }
1038 return 0;
1039}
1040
1041/* Copy request arguments to/from userspace buffer */
1042static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
1043 unsigned argpages, struct fuse_arg *args,
1044 int zeroing)
1045{
1046 int err = 0;
1047 unsigned i;
1048
1049 for (i = 0; !err && i < numargs; i++) {
1050 struct fuse_arg *arg = &args[i];
1051 if (i == numargs - 1 && argpages)
1052 err = fuse_copy_pages(cs, arg->size, zeroing);
1053 else
1054 err = fuse_copy_one(cs, arg->value, arg->size);
1055 }
1056 return err;
1057}
1058
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001059static int forget_pending(struct fuse_conn *fc)
1060{
1061 return fc->forget_list_head.next != NULL;
1062}
1063
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001064static int request_pending(struct fuse_conn *fc)
1065{
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001066 return !list_empty(&fc->pending) || !list_empty(&fc->interrupts) ||
1067 forget_pending(fc);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001068}
1069
Miklos Szeredi334f4852005-09-09 13:10:27 -07001070/* Wait until a request is available on the pending list */
1071static void request_wait(struct fuse_conn *fc)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001072__releases(fc->lock)
1073__acquires(fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001074{
1075 DECLARE_WAITQUEUE(wait, current);
1076
1077 add_wait_queue_exclusive(&fc->waitq, &wait);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001078 while (fc->connected && !request_pending(fc)) {
Miklos Szeredi334f4852005-09-09 13:10:27 -07001079 set_current_state(TASK_INTERRUPTIBLE);
1080 if (signal_pending(current))
1081 break;
1082
Miklos Szeredid7133112006-04-10 22:54:55 -07001083 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001084 schedule();
Miklos Szeredid7133112006-04-10 22:54:55 -07001085 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001086 }
1087 set_current_state(TASK_RUNNING);
1088 remove_wait_queue(&fc->waitq, &wait);
1089}
1090
1091/*
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001092 * Transfer an interrupt request to userspace
1093 *
1094 * Unlike other requests this is assembled on demand, without a need
1095 * to allocate a separate fuse_req structure.
1096 *
1097 * Called with fc->lock held, releases it
1098 */
Miklos Szeredic3021622010-05-25 15:06:07 +02001099static int fuse_read_interrupt(struct fuse_conn *fc, struct fuse_copy_state *cs,
1100 size_t nbytes, struct fuse_req *req)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001101__releases(fc->lock)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001102{
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001103 struct fuse_in_header ih;
1104 struct fuse_interrupt_in arg;
1105 unsigned reqsize = sizeof(ih) + sizeof(arg);
1106 int err;
1107
1108 list_del_init(&req->intr_entry);
1109 req->intr_unique = fuse_get_unique(fc);
1110 memset(&ih, 0, sizeof(ih));
1111 memset(&arg, 0, sizeof(arg));
1112 ih.len = reqsize;
1113 ih.opcode = FUSE_INTERRUPT;
1114 ih.unique = req->intr_unique;
1115 arg.unique = req->in.h.unique;
1116
1117 spin_unlock(&fc->lock);
Miklos Szeredic3021622010-05-25 15:06:07 +02001118 if (nbytes < reqsize)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001119 return -EINVAL;
1120
Miklos Szeredic3021622010-05-25 15:06:07 +02001121 err = fuse_copy_one(cs, &ih, sizeof(ih));
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001122 if (!err)
Miklos Szeredic3021622010-05-25 15:06:07 +02001123 err = fuse_copy_one(cs, &arg, sizeof(arg));
1124 fuse_copy_finish(cs);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001125
1126 return err ? err : reqsize;
1127}
1128
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001129static struct fuse_forget_link *dequeue_forget(struct fuse_conn *fc,
1130 unsigned max,
1131 unsigned *countp)
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001132{
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001133 struct fuse_forget_link *head = fc->forget_list_head.next;
1134 struct fuse_forget_link **newhead = &head;
1135 unsigned count;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001136
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001137 for (count = 0; *newhead != NULL && count < max; count++)
1138 newhead = &(*newhead)->next;
1139
1140 fc->forget_list_head.next = *newhead;
1141 *newhead = NULL;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001142 if (fc->forget_list_head.next == NULL)
1143 fc->forget_list_tail = &fc->forget_list_head;
1144
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001145 if (countp != NULL)
1146 *countp = count;
1147
1148 return head;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001149}
1150
1151static int fuse_read_single_forget(struct fuse_conn *fc,
1152 struct fuse_copy_state *cs,
1153 size_t nbytes)
1154__releases(fc->lock)
1155{
1156 int err;
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001157 struct fuse_forget_link *forget = dequeue_forget(fc, 1, NULL);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001158 struct fuse_forget_in arg = {
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001159 .nlookup = forget->forget_one.nlookup,
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001160 };
1161 struct fuse_in_header ih = {
1162 .opcode = FUSE_FORGET,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001163 .nodeid = forget->forget_one.nodeid,
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001164 .unique = fuse_get_unique(fc),
1165 .len = sizeof(ih) + sizeof(arg),
1166 };
1167
1168 spin_unlock(&fc->lock);
1169 kfree(forget);
1170 if (nbytes < ih.len)
1171 return -EINVAL;
1172
1173 err = fuse_copy_one(cs, &ih, sizeof(ih));
1174 if (!err)
1175 err = fuse_copy_one(cs, &arg, sizeof(arg));
1176 fuse_copy_finish(cs);
1177
1178 if (err)
1179 return err;
1180
1181 return ih.len;
1182}
1183
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001184static int fuse_read_batch_forget(struct fuse_conn *fc,
1185 struct fuse_copy_state *cs, size_t nbytes)
1186__releases(fc->lock)
1187{
1188 int err;
1189 unsigned max_forgets;
1190 unsigned count;
1191 struct fuse_forget_link *head;
1192 struct fuse_batch_forget_in arg = { .count = 0 };
1193 struct fuse_in_header ih = {
1194 .opcode = FUSE_BATCH_FORGET,
1195 .unique = fuse_get_unique(fc),
1196 .len = sizeof(ih) + sizeof(arg),
1197 };
1198
1199 if (nbytes < ih.len) {
1200 spin_unlock(&fc->lock);
1201 return -EINVAL;
1202 }
1203
1204 max_forgets = (nbytes - ih.len) / sizeof(struct fuse_forget_one);
1205 head = dequeue_forget(fc, max_forgets, &count);
1206 spin_unlock(&fc->lock);
1207
1208 arg.count = count;
1209 ih.len += count * sizeof(struct fuse_forget_one);
1210 err = fuse_copy_one(cs, &ih, sizeof(ih));
1211 if (!err)
1212 err = fuse_copy_one(cs, &arg, sizeof(arg));
1213
1214 while (head) {
1215 struct fuse_forget_link *forget = head;
1216
1217 if (!err) {
1218 err = fuse_copy_one(cs, &forget->forget_one,
1219 sizeof(forget->forget_one));
1220 }
1221 head = forget->next;
1222 kfree(forget);
1223 }
1224
1225 fuse_copy_finish(cs);
1226
1227 if (err)
1228 return err;
1229
1230 return ih.len;
1231}
1232
1233static int fuse_read_forget(struct fuse_conn *fc, struct fuse_copy_state *cs,
1234 size_t nbytes)
1235__releases(fc->lock)
1236{
1237 if (fc->minor < 16 || fc->forget_list_head.next->next == NULL)
1238 return fuse_read_single_forget(fc, cs, nbytes);
1239 else
1240 return fuse_read_batch_forget(fc, cs, nbytes);
1241}
1242
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001243/*
Miklos Szeredi334f4852005-09-09 13:10:27 -07001244 * Read a single request into the userspace filesystem's buffer. This
1245 * function waits until a request is available, then removes it from
1246 * the pending list and copies request data to userspace buffer. If
Miklos Szeredif9a28422006-06-25 05:48:53 -07001247 * no reply is needed (FORGET) or request has been aborted or there
1248 * was an error during the copying then it's finished by calling
Miklos Szeredi334f4852005-09-09 13:10:27 -07001249 * request_end(). Otherwise add it to the processing list, and set
1250 * the 'sent' flag.
1251 */
Miklos Szeredic3021622010-05-25 15:06:07 +02001252static ssize_t fuse_dev_do_read(struct fuse_conn *fc, struct file *file,
1253 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001254{
1255 int err;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001256 struct fuse_req *req;
1257 struct fuse_in *in;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001258 unsigned reqsize;
1259
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001260 restart:
Miklos Szeredid7133112006-04-10 22:54:55 -07001261 spin_lock(&fc->lock);
Jeff Dikee5ac1d12006-04-10 22:54:53 -07001262 err = -EAGAIN;
1263 if ((file->f_flags & O_NONBLOCK) && fc->connected &&
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001264 !request_pending(fc))
Jeff Dikee5ac1d12006-04-10 22:54:53 -07001265 goto err_unlock;
1266
Miklos Szeredi334f4852005-09-09 13:10:27 -07001267 request_wait(fc);
1268 err = -ENODEV;
Miklos Szeredi9ba7cbb2006-01-16 22:14:34 -08001269 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001270 goto err_unlock;
1271 err = -ERESTARTSYS;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001272 if (!request_pending(fc))
Miklos Szeredi334f4852005-09-09 13:10:27 -07001273 goto err_unlock;
1274
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001275 if (!list_empty(&fc->interrupts)) {
1276 req = list_entry(fc->interrupts.next, struct fuse_req,
1277 intr_entry);
Miklos Szeredic3021622010-05-25 15:06:07 +02001278 return fuse_read_interrupt(fc, cs, nbytes, req);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001279 }
1280
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001281 if (forget_pending(fc)) {
1282 if (list_empty(&fc->pending) || fc->forget_batch-- > 0)
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001283 return fuse_read_forget(fc, cs, nbytes);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001284
1285 if (fc->forget_batch <= -8)
1286 fc->forget_batch = 16;
1287 }
1288
Miklos Szeredi334f4852005-09-09 13:10:27 -07001289 req = list_entry(fc->pending.next, struct fuse_req, list);
Miklos Szeredi83cfd492006-01-16 22:14:31 -08001290 req->state = FUSE_REQ_READING;
Miklos Szeredid77a1d52006-01-16 22:14:31 -08001291 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001292
1293 in = &req->in;
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001294 reqsize = in->h.len;
1295 /* If request is too large, reply with an error and restart the read */
Miklos Szeredic3021622010-05-25 15:06:07 +02001296 if (nbytes < reqsize) {
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001297 req->out.h.error = -EIO;
1298 /* SETXATTR is special, since it may contain too large data */
1299 if (in->h.opcode == FUSE_SETXATTR)
1300 req->out.h.error = -E2BIG;
1301 request_end(fc, req);
1302 goto restart;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001303 }
Miklos Szeredid7133112006-04-10 22:54:55 -07001304 spin_unlock(&fc->lock);
Miklos Szeredic3021622010-05-25 15:06:07 +02001305 cs->req = req;
1306 err = fuse_copy_one(cs, &in->h, sizeof(in->h));
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001307 if (!err)
Miklos Szeredic3021622010-05-25 15:06:07 +02001308 err = fuse_copy_args(cs, in->numargs, in->argpages,
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001309 (struct fuse_arg *) in->args, 0);
Miklos Szeredic3021622010-05-25 15:06:07 +02001310 fuse_copy_finish(cs);
Miklos Szeredid7133112006-04-10 22:54:55 -07001311 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001312 req->locked = 0;
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +02001313 if (!fc->connected) {
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -07001314 request_end(fc, req);
1315 return -ENODEV;
1316 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001317 if (err) {
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -07001318 req->out.h.error = -EIO;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001319 request_end(fc, req);
1320 return err;
1321 }
1322 if (!req->isreply)
1323 request_end(fc, req);
1324 else {
Miklos Szeredi83cfd492006-01-16 22:14:31 -08001325 req->state = FUSE_REQ_SENT;
Miklos Szeredid77a1d52006-01-16 22:14:31 -08001326 list_move_tail(&req->list, &fc->processing);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001327 if (req->interrupted)
1328 queue_interrupt(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -07001329 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001330 }
1331 return reqsize;
1332
1333 err_unlock:
Miklos Szeredid7133112006-04-10 22:54:55 -07001334 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001335 return err;
1336}
1337
Tom Van Braeckel94e4fe22015-01-12 05:22:16 +01001338static int fuse_dev_open(struct inode *inode, struct file *file)
1339{
1340 /*
1341 * The fuse device's file's private_data is used to hold
1342 * the fuse_conn(ection) when it is mounted, and is used to
1343 * keep track of whether the file has been mounted already.
1344 */
1345 file->private_data = NULL;
1346 return 0;
1347}
1348
Al Virofbdbacc2015-04-03 21:53:39 -04001349static ssize_t fuse_dev_read(struct kiocb *iocb, struct iov_iter *to)
Miklos Szeredic3021622010-05-25 15:06:07 +02001350{
1351 struct fuse_copy_state cs;
1352 struct file *file = iocb->ki_filp;
1353 struct fuse_conn *fc = fuse_get_conn(file);
1354 if (!fc)
1355 return -EPERM;
1356
Al Virofbdbacc2015-04-03 21:53:39 -04001357 if (!iter_is_iovec(to))
1358 return -EINVAL;
Miklos Szeredic3021622010-05-25 15:06:07 +02001359
Al Viro6c09e942015-04-03 22:06:08 -04001360 fuse_copy_init(&cs, fc, 1, to);
Al Virofbdbacc2015-04-03 21:53:39 -04001361
1362 return fuse_dev_do_read(fc, file, &cs, iov_iter_count(to));
Miklos Szeredic3021622010-05-25 15:06:07 +02001363}
1364
Miklos Szeredic3021622010-05-25 15:06:07 +02001365static ssize_t fuse_dev_splice_read(struct file *in, loff_t *ppos,
1366 struct pipe_inode_info *pipe,
1367 size_t len, unsigned int flags)
1368{
1369 int ret;
1370 int page_nr = 0;
1371 int do_wakeup = 0;
1372 struct pipe_buffer *bufs;
1373 struct fuse_copy_state cs;
1374 struct fuse_conn *fc = fuse_get_conn(in);
1375 if (!fc)
1376 return -EPERM;
1377
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001378 bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), GFP_KERNEL);
Miklos Szeredic3021622010-05-25 15:06:07 +02001379 if (!bufs)
1380 return -ENOMEM;
1381
Al Viro6c09e942015-04-03 22:06:08 -04001382 fuse_copy_init(&cs, fc, 1, NULL);
Miklos Szeredic3021622010-05-25 15:06:07 +02001383 cs.pipebufs = bufs;
1384 cs.pipe = pipe;
1385 ret = fuse_dev_do_read(fc, in, &cs, len);
1386 if (ret < 0)
1387 goto out;
1388
1389 ret = 0;
1390 pipe_lock(pipe);
1391
1392 if (!pipe->readers) {
1393 send_sig(SIGPIPE, current, 0);
1394 if (!ret)
1395 ret = -EPIPE;
1396 goto out_unlock;
1397 }
1398
1399 if (pipe->nrbufs + cs.nr_segs > pipe->buffers) {
1400 ret = -EIO;
1401 goto out_unlock;
1402 }
1403
1404 while (page_nr < cs.nr_segs) {
1405 int newbuf = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
1406 struct pipe_buffer *buf = pipe->bufs + newbuf;
1407
1408 buf->page = bufs[page_nr].page;
1409 buf->offset = bufs[page_nr].offset;
1410 buf->len = bufs[page_nr].len;
Miklos Szeredi28a625c2014-01-22 19:36:57 +01001411 /*
1412 * Need to be careful about this. Having buf->ops in module
1413 * code can Oops if the buffer persists after module unload.
1414 */
1415 buf->ops = &nosteal_pipe_buf_ops;
Miklos Szeredic3021622010-05-25 15:06:07 +02001416
1417 pipe->nrbufs++;
1418 page_nr++;
1419 ret += buf->len;
1420
Al Viro6447a3c2013-03-21 11:01:38 -04001421 if (pipe->files)
Miklos Szeredic3021622010-05-25 15:06:07 +02001422 do_wakeup = 1;
1423 }
1424
1425out_unlock:
1426 pipe_unlock(pipe);
1427
1428 if (do_wakeup) {
1429 smp_mb();
1430 if (waitqueue_active(&pipe->wait))
1431 wake_up_interruptible(&pipe->wait);
1432 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
1433 }
1434
1435out:
1436 for (; page_nr < cs.nr_segs; page_nr++)
1437 page_cache_release(bufs[page_nr].page);
1438
1439 kfree(bufs);
1440 return ret;
1441}
1442
Tejun Heo95668a62008-11-26 12:03:55 +01001443static int fuse_notify_poll(struct fuse_conn *fc, unsigned int size,
1444 struct fuse_copy_state *cs)
1445{
1446 struct fuse_notify_poll_wakeup_out outarg;
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001447 int err = -EINVAL;
Tejun Heo95668a62008-11-26 12:03:55 +01001448
1449 if (size != sizeof(outarg))
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001450 goto err;
Tejun Heo95668a62008-11-26 12:03:55 +01001451
1452 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1453 if (err)
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001454 goto err;
Tejun Heo95668a62008-11-26 12:03:55 +01001455
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001456 fuse_copy_finish(cs);
Tejun Heo95668a62008-11-26 12:03:55 +01001457 return fuse_notify_poll_wakeup(fc, &outarg);
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001458
1459err:
1460 fuse_copy_finish(cs);
1461 return err;
Tejun Heo95668a62008-11-26 12:03:55 +01001462}
1463
John Muir3b463ae2009-05-31 11:13:57 -04001464static int fuse_notify_inval_inode(struct fuse_conn *fc, unsigned int size,
1465 struct fuse_copy_state *cs)
1466{
1467 struct fuse_notify_inval_inode_out outarg;
1468 int err = -EINVAL;
1469
1470 if (size != sizeof(outarg))
1471 goto err;
1472
1473 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1474 if (err)
1475 goto err;
1476 fuse_copy_finish(cs);
1477
1478 down_read(&fc->killsb);
1479 err = -ENOENT;
Miklos Szeredib21dda42010-02-05 12:08:31 +01001480 if (fc->sb) {
1481 err = fuse_reverse_inval_inode(fc->sb, outarg.ino,
1482 outarg.off, outarg.len);
1483 }
John Muir3b463ae2009-05-31 11:13:57 -04001484 up_read(&fc->killsb);
1485 return err;
1486
1487err:
1488 fuse_copy_finish(cs);
1489 return err;
1490}
1491
1492static int fuse_notify_inval_entry(struct fuse_conn *fc, unsigned int size,
1493 struct fuse_copy_state *cs)
1494{
1495 struct fuse_notify_inval_entry_out outarg;
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001496 int err = -ENOMEM;
1497 char *buf;
John Muir3b463ae2009-05-31 11:13:57 -04001498 struct qstr name;
1499
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001500 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1501 if (!buf)
1502 goto err;
1503
1504 err = -EINVAL;
John Muir3b463ae2009-05-31 11:13:57 -04001505 if (size < sizeof(outarg))
1506 goto err;
1507
1508 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1509 if (err)
1510 goto err;
1511
1512 err = -ENAMETOOLONG;
1513 if (outarg.namelen > FUSE_NAME_MAX)
1514 goto err;
1515
Miklos Szeredic2183d12011-08-24 10:20:17 +02001516 err = -EINVAL;
1517 if (size != sizeof(outarg) + outarg.namelen + 1)
1518 goto err;
1519
John Muir3b463ae2009-05-31 11:13:57 -04001520 name.name = buf;
1521 name.len = outarg.namelen;
1522 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1523 if (err)
1524 goto err;
1525 fuse_copy_finish(cs);
1526 buf[outarg.namelen] = 0;
1527 name.hash = full_name_hash(name.name, name.len);
1528
1529 down_read(&fc->killsb);
1530 err = -ENOENT;
Miklos Szeredib21dda42010-02-05 12:08:31 +01001531 if (fc->sb)
John Muir451d0f52011-12-06 21:50:06 +01001532 err = fuse_reverse_inval_entry(fc->sb, outarg.parent, 0, &name);
1533 up_read(&fc->killsb);
1534 kfree(buf);
1535 return err;
1536
1537err:
1538 kfree(buf);
1539 fuse_copy_finish(cs);
1540 return err;
1541}
1542
1543static int fuse_notify_delete(struct fuse_conn *fc, unsigned int size,
1544 struct fuse_copy_state *cs)
1545{
1546 struct fuse_notify_delete_out outarg;
1547 int err = -ENOMEM;
1548 char *buf;
1549 struct qstr name;
1550
1551 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1552 if (!buf)
1553 goto err;
1554
1555 err = -EINVAL;
1556 if (size < sizeof(outarg))
1557 goto err;
1558
1559 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1560 if (err)
1561 goto err;
1562
1563 err = -ENAMETOOLONG;
1564 if (outarg.namelen > FUSE_NAME_MAX)
1565 goto err;
1566
1567 err = -EINVAL;
1568 if (size != sizeof(outarg) + outarg.namelen + 1)
1569 goto err;
1570
1571 name.name = buf;
1572 name.len = outarg.namelen;
1573 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1574 if (err)
1575 goto err;
1576 fuse_copy_finish(cs);
1577 buf[outarg.namelen] = 0;
1578 name.hash = full_name_hash(name.name, name.len);
1579
1580 down_read(&fc->killsb);
1581 err = -ENOENT;
1582 if (fc->sb)
1583 err = fuse_reverse_inval_entry(fc->sb, outarg.parent,
1584 outarg.child, &name);
John Muir3b463ae2009-05-31 11:13:57 -04001585 up_read(&fc->killsb);
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001586 kfree(buf);
John Muir3b463ae2009-05-31 11:13:57 -04001587 return err;
1588
1589err:
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001590 kfree(buf);
John Muir3b463ae2009-05-31 11:13:57 -04001591 fuse_copy_finish(cs);
1592 return err;
1593}
1594
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001595static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
1596 struct fuse_copy_state *cs)
1597{
1598 struct fuse_notify_store_out outarg;
1599 struct inode *inode;
1600 struct address_space *mapping;
1601 u64 nodeid;
1602 int err;
1603 pgoff_t index;
1604 unsigned int offset;
1605 unsigned int num;
1606 loff_t file_size;
1607 loff_t end;
1608
1609 err = -EINVAL;
1610 if (size < sizeof(outarg))
1611 goto out_finish;
1612
1613 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1614 if (err)
1615 goto out_finish;
1616
1617 err = -EINVAL;
1618 if (size - sizeof(outarg) != outarg.size)
1619 goto out_finish;
1620
1621 nodeid = outarg.nodeid;
1622
1623 down_read(&fc->killsb);
1624
1625 err = -ENOENT;
1626 if (!fc->sb)
1627 goto out_up_killsb;
1628
1629 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1630 if (!inode)
1631 goto out_up_killsb;
1632
1633 mapping = inode->i_mapping;
1634 index = outarg.offset >> PAGE_CACHE_SHIFT;
1635 offset = outarg.offset & ~PAGE_CACHE_MASK;
1636 file_size = i_size_read(inode);
1637 end = outarg.offset + outarg.size;
1638 if (end > file_size) {
1639 file_size = end;
1640 fuse_write_update_size(inode, file_size);
1641 }
1642
1643 num = outarg.size;
1644 while (num) {
1645 struct page *page;
1646 unsigned int this_num;
1647
1648 err = -ENOMEM;
1649 page = find_or_create_page(mapping, index,
1650 mapping_gfp_mask(mapping));
1651 if (!page)
1652 goto out_iput;
1653
1654 this_num = min_t(unsigned, num, PAGE_CACHE_SIZE - offset);
1655 err = fuse_copy_page(cs, &page, offset, this_num, 0);
Miklos Szeredi063ec1e2014-01-22 19:36:58 +01001656 if (!err && offset == 0 &&
1657 (this_num == PAGE_CACHE_SIZE || file_size == end))
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001658 SetPageUptodate(page);
1659 unlock_page(page);
1660 page_cache_release(page);
1661
1662 if (err)
1663 goto out_iput;
1664
1665 num -= this_num;
1666 offset = 0;
1667 index++;
1668 }
1669
1670 err = 0;
1671
1672out_iput:
1673 iput(inode);
1674out_up_killsb:
1675 up_read(&fc->killsb);
1676out_finish:
1677 fuse_copy_finish(cs);
1678 return err;
1679}
1680
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001681static void fuse_retrieve_end(struct fuse_conn *fc, struct fuse_req *req)
1682{
Mel Gormanb745bc82014-06-04 16:10:22 -07001683 release_pages(req->pages, req->num_pages, false);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001684}
1685
1686static int fuse_retrieve(struct fuse_conn *fc, struct inode *inode,
1687 struct fuse_notify_retrieve_out *outarg)
1688{
1689 int err;
1690 struct address_space *mapping = inode->i_mapping;
1691 struct fuse_req *req;
1692 pgoff_t index;
1693 loff_t file_size;
1694 unsigned int num;
1695 unsigned int offset;
Geert Uytterhoeven01574432010-09-30 22:06:21 +02001696 size_t total_len = 0;
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001697 int num_pages;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001698
1699 offset = outarg->offset & ~PAGE_CACHE_MASK;
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001700 file_size = i_size_read(inode);
1701
1702 num = outarg->size;
1703 if (outarg->offset > file_size)
1704 num = 0;
1705 else if (outarg->offset + num > file_size)
1706 num = file_size - outarg->offset;
1707
1708 num_pages = (num + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1709 num_pages = min(num_pages, FUSE_MAX_PAGES_PER_REQ);
1710
1711 req = fuse_get_req(fc, num_pages);
1712 if (IS_ERR(req))
1713 return PTR_ERR(req);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001714
1715 req->in.h.opcode = FUSE_NOTIFY_REPLY;
1716 req->in.h.nodeid = outarg->nodeid;
1717 req->in.numargs = 2;
1718 req->in.argpages = 1;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001719 req->page_descs[0].offset = offset;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001720 req->end = fuse_retrieve_end;
1721
1722 index = outarg->offset >> PAGE_CACHE_SHIFT;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001723
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001724 while (num && req->num_pages < num_pages) {
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001725 struct page *page;
1726 unsigned int this_num;
1727
1728 page = find_get_page(mapping, index);
1729 if (!page)
1730 break;
1731
1732 this_num = min_t(unsigned, num, PAGE_CACHE_SIZE - offset);
1733 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001734 req->page_descs[req->num_pages].length = this_num;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001735 req->num_pages++;
1736
Miklos Szeredic9e67d42012-09-04 18:45:54 +02001737 offset = 0;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001738 num -= this_num;
1739 total_len += this_num;
Miklos Szeredi48706d02011-12-13 10:36:59 +01001740 index++;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001741 }
1742 req->misc.retrieve_in.offset = outarg->offset;
1743 req->misc.retrieve_in.size = total_len;
1744 req->in.args[0].size = sizeof(req->misc.retrieve_in);
1745 req->in.args[0].value = &req->misc.retrieve_in;
1746 req->in.args[1].size = total_len;
1747
1748 err = fuse_request_send_notify_reply(fc, req, outarg->notify_unique);
1749 if (err)
1750 fuse_retrieve_end(fc, req);
1751
1752 return err;
1753}
1754
1755static int fuse_notify_retrieve(struct fuse_conn *fc, unsigned int size,
1756 struct fuse_copy_state *cs)
1757{
1758 struct fuse_notify_retrieve_out outarg;
1759 struct inode *inode;
1760 int err;
1761
1762 err = -EINVAL;
1763 if (size != sizeof(outarg))
1764 goto copy_finish;
1765
1766 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1767 if (err)
1768 goto copy_finish;
1769
1770 fuse_copy_finish(cs);
1771
1772 down_read(&fc->killsb);
1773 err = -ENOENT;
1774 if (fc->sb) {
1775 u64 nodeid = outarg.nodeid;
1776
1777 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1778 if (inode) {
1779 err = fuse_retrieve(fc, inode, &outarg);
1780 iput(inode);
1781 }
1782 }
1783 up_read(&fc->killsb);
1784
1785 return err;
1786
1787copy_finish:
1788 fuse_copy_finish(cs);
1789 return err;
1790}
1791
Tejun Heo85993962008-11-26 12:03:55 +01001792static int fuse_notify(struct fuse_conn *fc, enum fuse_notify_code code,
1793 unsigned int size, struct fuse_copy_state *cs)
1794{
Miklos Szeredi0d278362015-02-26 11:45:47 +01001795 /* Don't try to move pages (yet) */
1796 cs->move_pages = 0;
1797
Tejun Heo85993962008-11-26 12:03:55 +01001798 switch (code) {
Tejun Heo95668a62008-11-26 12:03:55 +01001799 case FUSE_NOTIFY_POLL:
1800 return fuse_notify_poll(fc, size, cs);
1801
John Muir3b463ae2009-05-31 11:13:57 -04001802 case FUSE_NOTIFY_INVAL_INODE:
1803 return fuse_notify_inval_inode(fc, size, cs);
1804
1805 case FUSE_NOTIFY_INVAL_ENTRY:
1806 return fuse_notify_inval_entry(fc, size, cs);
1807
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001808 case FUSE_NOTIFY_STORE:
1809 return fuse_notify_store(fc, size, cs);
1810
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001811 case FUSE_NOTIFY_RETRIEVE:
1812 return fuse_notify_retrieve(fc, size, cs);
1813
John Muir451d0f52011-12-06 21:50:06 +01001814 case FUSE_NOTIFY_DELETE:
1815 return fuse_notify_delete(fc, size, cs);
1816
Tejun Heo85993962008-11-26 12:03:55 +01001817 default:
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001818 fuse_copy_finish(cs);
Tejun Heo85993962008-11-26 12:03:55 +01001819 return -EINVAL;
1820 }
1821}
1822
Miklos Szeredi334f4852005-09-09 13:10:27 -07001823/* Look up request on processing list by unique ID */
1824static struct fuse_req *request_find(struct fuse_conn *fc, u64 unique)
1825{
Dong Fang05726ac2013-07-30 22:50:01 -04001826 struct fuse_req *req;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001827
Dong Fang05726ac2013-07-30 22:50:01 -04001828 list_for_each_entry(req, &fc->processing, list) {
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001829 if (req->in.h.unique == unique || req->intr_unique == unique)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001830 return req;
1831 }
1832 return NULL;
1833}
1834
1835static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
1836 unsigned nbytes)
1837{
1838 unsigned reqsize = sizeof(struct fuse_out_header);
1839
1840 if (out->h.error)
1841 return nbytes != reqsize ? -EINVAL : 0;
1842
1843 reqsize += len_args(out->numargs, out->args);
1844
1845 if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
1846 return -EINVAL;
1847 else if (reqsize > nbytes) {
1848 struct fuse_arg *lastarg = &out->args[out->numargs-1];
1849 unsigned diffsize = reqsize - nbytes;
1850 if (diffsize > lastarg->size)
1851 return -EINVAL;
1852 lastarg->size -= diffsize;
1853 }
1854 return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
1855 out->page_zeroing);
1856}
1857
1858/*
1859 * Write a single reply to a request. First the header is copied from
1860 * the write buffer. The request is then searched on the processing
1861 * list by the unique ID found in the header. If found, then remove
1862 * it from the list and copy the rest of the buffer to the request.
1863 * The request is finished by calling request_end()
1864 */
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001865static ssize_t fuse_dev_do_write(struct fuse_conn *fc,
1866 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001867{
1868 int err;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001869 struct fuse_req *req;
1870 struct fuse_out_header oh;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001871
Miklos Szeredi334f4852005-09-09 13:10:27 -07001872 if (nbytes < sizeof(struct fuse_out_header))
1873 return -EINVAL;
1874
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001875 err = fuse_copy_one(cs, &oh, sizeof(oh));
Miklos Szeredi334f4852005-09-09 13:10:27 -07001876 if (err)
1877 goto err_finish;
Tejun Heo85993962008-11-26 12:03:55 +01001878
Miklos Szeredi334f4852005-09-09 13:10:27 -07001879 err = -EINVAL;
Tejun Heo85993962008-11-26 12:03:55 +01001880 if (oh.len != nbytes)
1881 goto err_finish;
1882
1883 /*
1884 * Zero oh.unique indicates unsolicited notification message
1885 * and error contains notification code.
1886 */
1887 if (!oh.unique) {
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001888 err = fuse_notify(fc, oh.error, nbytes - sizeof(oh), cs);
Tejun Heo85993962008-11-26 12:03:55 +01001889 return err ? err : nbytes;
1890 }
1891
1892 err = -EINVAL;
1893 if (oh.error <= -1000 || oh.error > 0)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001894 goto err_finish;
1895
Miklos Szeredid7133112006-04-10 22:54:55 -07001896 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001897 err = -ENOENT;
1898 if (!fc->connected)
1899 goto err_unlock;
1900
Miklos Szeredi334f4852005-09-09 13:10:27 -07001901 req = request_find(fc, oh.unique);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001902 if (!req)
1903 goto err_unlock;
1904
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001905 /* Is it an interrupt reply? */
1906 if (req->intr_unique == oh.unique) {
1907 err = -EINVAL;
1908 if (nbytes != sizeof(struct fuse_out_header))
1909 goto err_unlock;
1910
1911 if (oh.error == -ENOSYS)
1912 fc->no_interrupt = 1;
1913 else if (oh.error == -EAGAIN)
1914 queue_interrupt(fc, req);
1915
1916 spin_unlock(&fc->lock);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001917 fuse_copy_finish(cs);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001918 return nbytes;
1919 }
1920
1921 req->state = FUSE_REQ_WRITING;
Miklos Szeredid77a1d52006-01-16 22:14:31 -08001922 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001923 req->out.h = oh;
1924 req->locked = 1;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001925 cs->req = req;
Miklos Szeredice534fb2010-05-25 15:06:07 +02001926 if (!req->out.page_replace)
1927 cs->move_pages = 0;
Miklos Szeredid7133112006-04-10 22:54:55 -07001928 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001929
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001930 err = copy_out_args(cs, &req->out, nbytes);
1931 fuse_copy_finish(cs);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001932
Miklos Szeredid7133112006-04-10 22:54:55 -07001933 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001934 req->locked = 0;
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +02001935 if (!fc->connected)
1936 err = -ENOENT;
1937 else if (err)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001938 req->out.h.error = -EIO;
1939 request_end(fc, req);
1940
1941 return err ? err : nbytes;
1942
1943 err_unlock:
Miklos Szeredid7133112006-04-10 22:54:55 -07001944 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001945 err_finish:
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001946 fuse_copy_finish(cs);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001947 return err;
1948}
1949
Al Virofbdbacc2015-04-03 21:53:39 -04001950static ssize_t fuse_dev_write(struct kiocb *iocb, struct iov_iter *from)
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001951{
1952 struct fuse_copy_state cs;
1953 struct fuse_conn *fc = fuse_get_conn(iocb->ki_filp);
1954 if (!fc)
1955 return -EPERM;
1956
Al Virofbdbacc2015-04-03 21:53:39 -04001957 if (!iter_is_iovec(from))
1958 return -EINVAL;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001959
Al Viro6c09e942015-04-03 22:06:08 -04001960 fuse_copy_init(&cs, fc, 0, from);
Al Virofbdbacc2015-04-03 21:53:39 -04001961
1962 return fuse_dev_do_write(fc, &cs, iov_iter_count(from));
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001963}
1964
1965static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe,
1966 struct file *out, loff_t *ppos,
1967 size_t len, unsigned int flags)
1968{
1969 unsigned nbuf;
1970 unsigned idx;
1971 struct pipe_buffer *bufs;
1972 struct fuse_copy_state cs;
1973 struct fuse_conn *fc;
1974 size_t rem;
1975 ssize_t ret;
1976
1977 fc = fuse_get_conn(out);
1978 if (!fc)
1979 return -EPERM;
1980
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001981 bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), GFP_KERNEL);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001982 if (!bufs)
1983 return -ENOMEM;
1984
1985 pipe_lock(pipe);
1986 nbuf = 0;
1987 rem = 0;
1988 for (idx = 0; idx < pipe->nrbufs && rem < len; idx++)
1989 rem += pipe->bufs[(pipe->curbuf + idx) & (pipe->buffers - 1)].len;
1990
1991 ret = -EINVAL;
1992 if (rem < len) {
1993 pipe_unlock(pipe);
1994 goto out;
1995 }
1996
1997 rem = len;
1998 while (rem) {
1999 struct pipe_buffer *ibuf;
2000 struct pipe_buffer *obuf;
2001
2002 BUG_ON(nbuf >= pipe->buffers);
2003 BUG_ON(!pipe->nrbufs);
2004 ibuf = &pipe->bufs[pipe->curbuf];
2005 obuf = &bufs[nbuf];
2006
2007 if (rem >= ibuf->len) {
2008 *obuf = *ibuf;
2009 ibuf->ops = NULL;
2010 pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
2011 pipe->nrbufs--;
2012 } else {
2013 ibuf->ops->get(pipe, ibuf);
2014 *obuf = *ibuf;
2015 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
2016 obuf->len = rem;
2017 ibuf->offset += obuf->len;
2018 ibuf->len -= obuf->len;
2019 }
2020 nbuf++;
2021 rem -= obuf->len;
2022 }
2023 pipe_unlock(pipe);
2024
Al Viro6c09e942015-04-03 22:06:08 -04002025 fuse_copy_init(&cs, fc, 0, NULL);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002026 cs.pipebufs = bufs;
Al Viro6c09e942015-04-03 22:06:08 -04002027 cs.nr_segs = nbuf;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002028 cs.pipe = pipe;
2029
Miklos Szeredice534fb2010-05-25 15:06:07 +02002030 if (flags & SPLICE_F_MOVE)
2031 cs.move_pages = 1;
2032
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002033 ret = fuse_dev_do_write(fc, &cs, len);
2034
2035 for (idx = 0; idx < nbuf; idx++) {
2036 struct pipe_buffer *buf = &bufs[idx];
2037 buf->ops->release(pipe, buf);
2038 }
2039out:
2040 kfree(bufs);
2041 return ret;
2042}
2043
Miklos Szeredi334f4852005-09-09 13:10:27 -07002044static unsigned fuse_dev_poll(struct file *file, poll_table *wait)
2045{
Miklos Szeredi334f4852005-09-09 13:10:27 -07002046 unsigned mask = POLLOUT | POLLWRNORM;
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07002047 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002048 if (!fc)
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07002049 return POLLERR;
Miklos Szeredi334f4852005-09-09 13:10:27 -07002050
2051 poll_wait(file, &fc->waitq, wait);
2052
Miklos Szeredid7133112006-04-10 22:54:55 -07002053 spin_lock(&fc->lock);
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07002054 if (!fc->connected)
2055 mask = POLLERR;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07002056 else if (request_pending(fc))
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07002057 mask |= POLLIN | POLLRDNORM;
Miklos Szeredid7133112006-04-10 22:54:55 -07002058 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002059
2060 return mask;
2061}
2062
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002063/*
2064 * Abort all requests on the given list (pending or processing)
2065 *
Miklos Szeredid7133112006-04-10 22:54:55 -07002066 * This function releases and reacquires fc->lock
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002067 */
Miklos Szeredi334f4852005-09-09 13:10:27 -07002068static void end_requests(struct fuse_conn *fc, struct list_head *head)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02002069__releases(fc->lock)
2070__acquires(fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -07002071{
2072 while (!list_empty(head)) {
2073 struct fuse_req *req;
2074 req = list_entry(head->next, struct fuse_req, list);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002075 req->out.h.error = -ECONNABORTED;
2076 request_end(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -07002077 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002078 }
2079}
2080
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002081/*
2082 * Abort requests under I/O
2083 *
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +02002084 * Separate out unlocked requests, they should be finished off immediately.
2085 * Locked requests will be finished after unlock; see unlock_request().
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08002086 *
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +02002087 * Next finish off the unlocked requests. It is possible that some request will
2088 * finish before we can. This is OK, the request will in that case be removed
2089 * from the list before we touch it.
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002090 */
2091static void end_io_requests(struct fuse_conn *fc)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02002092__releases(fc->lock)
2093__acquires(fc->lock)
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002094{
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +02002095 struct fuse_req *req, *next;
2096 LIST_HEAD(to_end);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08002097
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +02002098 list_for_each_entry_safe(req, next, &fc->io, list) {
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002099 req->out.h.error = -ECONNABORTED;
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +02002100 req->aborted = 1;
2101 if (!req->locked)
2102 list_move(&req->list, &to_end);
2103 }
2104 while (!list_empty(&to_end)) {
2105 req = list_first_entry(&to_end, struct fuse_req, list);
2106 __fuse_get_request(req);
2107 request_end(fc, req);
2108 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002109 }
2110}
2111
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002112static void end_queued_requests(struct fuse_conn *fc)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02002113__releases(fc->lock)
2114__acquires(fc->lock)
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002115{
2116 fc->max_background = UINT_MAX;
2117 flush_bg_queue(fc);
2118 end_requests(fc, &fc->pending);
2119 end_requests(fc, &fc->processing);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01002120 while (forget_pending(fc))
Miklos Szeredi02c048b2010-12-07 20:16:56 +01002121 kfree(dequeue_forget(fc, 1, NULL));
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002122}
2123
Bryan Green357ccf22011-03-01 16:43:52 -08002124static void end_polls(struct fuse_conn *fc)
2125{
2126 struct rb_node *p;
2127
2128 p = rb_first(&fc->polled_files);
2129
2130 while (p) {
2131 struct fuse_file *ff;
2132 ff = rb_entry(p, struct fuse_file, polled_node);
2133 wake_up_interruptible_all(&ff->poll_wait);
2134
2135 p = rb_next(p);
2136 }
2137}
2138
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002139/*
2140 * Abort all requests.
2141 *
2142 * Emergency exit in case of a malicious or accidental deadlock, or
2143 * just a hung filesystem.
2144 *
2145 * The same effect is usually achievable through killing the
2146 * filesystem daemon and all users of the filesystem. The exception
2147 * is the combination of an asynchronous request and the tricky
2148 * deadlock (see Documentation/filesystems/fuse.txt).
2149 *
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +02002150 * Request progression from one list to the next is prevented by
2151 * fc->connected being false.
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002152 */
2153void fuse_abort_conn(struct fuse_conn *fc)
2154{
Miklos Szeredid7133112006-04-10 22:54:55 -07002155 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002156 if (fc->connected) {
2157 fc->connected = 0;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -07002158 fc->blocked = 0;
Miklos Szeredi9759bd512015-01-06 10:45:35 +01002159 fuse_set_initialized(fc);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002160 end_io_requests(fc);
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002161 end_queued_requests(fc);
Bryan Green357ccf22011-03-01 16:43:52 -08002162 end_polls(fc);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002163 wake_up_all(&fc->waitq);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -07002164 wake_up_all(&fc->blocked_waitq);
Jeff Dike385a17b2006-04-10 22:54:52 -07002165 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002166 }
Miklos Szeredid7133112006-04-10 22:54:55 -07002167 spin_unlock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002168}
Tejun Heo08cbf542009-04-14 10:54:53 +09002169EXPORT_SYMBOL_GPL(fuse_abort_conn);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002170
Tejun Heo08cbf542009-04-14 10:54:53 +09002171int fuse_dev_release(struct inode *inode, struct file *file)
Miklos Szeredi334f4852005-09-09 13:10:27 -07002172{
Miklos Szeredi0720b312006-04-10 22:54:55 -07002173 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002174 if (fc) {
Miklos Szerediccd0a0b2015-07-01 16:25:57 +02002175 WARN_ON(!list_empty(&fc->io));
2176 WARN_ON(fc->fasync != NULL);
2177 fuse_abort_conn(fc);
Miklos Szeredibafa9652006-06-25 05:48:51 -07002178 fuse_conn_put(fc);
Jeff Dike385a17b2006-04-10 22:54:52 -07002179 }
Miklos Szeredif543f252006-01-16 22:14:35 -08002180
Miklos Szeredi334f4852005-09-09 13:10:27 -07002181 return 0;
2182}
Tejun Heo08cbf542009-04-14 10:54:53 +09002183EXPORT_SYMBOL_GPL(fuse_dev_release);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002184
Jeff Dike385a17b2006-04-10 22:54:52 -07002185static int fuse_dev_fasync(int fd, struct file *file, int on)
2186{
2187 struct fuse_conn *fc = fuse_get_conn(file);
2188 if (!fc)
Miklos Szeredia87046d2006-04-10 22:54:56 -07002189 return -EPERM;
Jeff Dike385a17b2006-04-10 22:54:52 -07002190
2191 /* No locking - fasync_helper does its own locking */
2192 return fasync_helper(fd, file, on, &fc->fasync);
2193}
2194
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002195const struct file_operations fuse_dev_operations = {
Miklos Szeredi334f4852005-09-09 13:10:27 -07002196 .owner = THIS_MODULE,
Tom Van Braeckel94e4fe22015-01-12 05:22:16 +01002197 .open = fuse_dev_open,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002198 .llseek = no_llseek,
Al Virofbdbacc2015-04-03 21:53:39 -04002199 .read_iter = fuse_dev_read,
Miklos Szeredic3021622010-05-25 15:06:07 +02002200 .splice_read = fuse_dev_splice_read,
Al Virofbdbacc2015-04-03 21:53:39 -04002201 .write_iter = fuse_dev_write,
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002202 .splice_write = fuse_dev_splice_write,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002203 .poll = fuse_dev_poll,
2204 .release = fuse_dev_release,
Jeff Dike385a17b2006-04-10 22:54:52 -07002205 .fasync = fuse_dev_fasync,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002206};
Tejun Heo08cbf542009-04-14 10:54:53 +09002207EXPORT_SYMBOL_GPL(fuse_dev_operations);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002208
2209static struct miscdevice fuse_miscdevice = {
2210 .minor = FUSE_MINOR,
2211 .name = "fuse",
2212 .fops = &fuse_dev_operations,
2213};
2214
2215int __init fuse_dev_init(void)
2216{
2217 int err = -ENOMEM;
2218 fuse_req_cachep = kmem_cache_create("fuse_request",
2219 sizeof(struct fuse_req),
Paul Mundt20c2df82007-07-20 10:11:58 +09002220 0, 0, NULL);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002221 if (!fuse_req_cachep)
2222 goto out;
2223
2224 err = misc_register(&fuse_miscdevice);
2225 if (err)
2226 goto out_cache_clean;
2227
2228 return 0;
2229
2230 out_cache_clean:
2231 kmem_cache_destroy(fuse_req_cachep);
2232 out:
2233 return err;
2234}
2235
2236void fuse_dev_cleanup(void)
2237{
2238 misc_deregister(&fuse_miscdevice);
2239 kmem_cache_destroy(fuse_req_cachep);
2240}