blob: e5901bf8d600d527c414a1e6446ff838825922ff [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 Szeredid77a1d52006-01-16 22:14:31 -0800385 list_del(&req->list);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700386 list_del(&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->aborted)
443 goto aborted;
444 if (req->state == FUSE_REQ_FINISHED)
445 return;
446
447 req->interrupted = 1;
448 if (req->state == FUSE_REQ_SENT)
449 queue_interrupt(fc, req);
450 }
451
Miklos Szeredia131de02007-10-16 23:31:04 -0700452 if (!req->force) {
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700453 sigset_t oldset;
454
455 /* Only fatal signals may interrupt this */
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700456 block_sigs(&oldset);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700457 wait_answer_interruptible(fc, req);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700458 restore_sigs(&oldset);
Miklos Szeredia131de02007-10-16 23:31:04 -0700459
460 if (req->aborted)
461 goto aborted;
462 if (req->state == FUSE_REQ_FINISHED)
463 return;
464
465 /* Request is not yet in userspace, bail out */
466 if (req->state == FUSE_REQ_PENDING) {
467 list_del(&req->list);
468 __fuse_put_request(req);
469 req->out.h.error = -EINTR;
470 return;
471 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700472 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700473
Miklos Szeredia131de02007-10-16 23:31:04 -0700474 /*
475 * Either request is already in userspace, or it was forced.
476 * Wait it out.
477 */
478 spin_unlock(&fc->lock);
479 wait_event(req->waitq, req->state == FUSE_REQ_FINISHED);
480 spin_lock(&fc->lock);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700481
Miklos Szeredia131de02007-10-16 23:31:04 -0700482 if (!req->aborted)
483 return;
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700484
485 aborted:
Miklos Szeredia131de02007-10-16 23:31:04 -0700486 BUG_ON(req->state != FUSE_REQ_FINISHED);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700487 if (req->locked) {
488 /* This is uninterruptible sleep, because data is
489 being copied to/from the buffers of req. During
490 locked state, there mustn't be any filesystem
491 operation (e.g. page fault), since that could lead
492 to deadlock */
Miklos Szeredid7133112006-04-10 22:54:55 -0700493 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700494 wait_event(req->waitq, !req->locked);
Miklos Szeredid7133112006-04-10 22:54:55 -0700495 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700496 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700497}
498
Eric Wong6a4e9222013-02-04 13:04:44 +0000499static void __fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700500{
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400501 BUG_ON(req->background);
Miklos Szeredid7133112006-04-10 22:54:55 -0700502 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700503 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700504 req->out.h.error = -ENOTCONN;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700505 else {
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200506 req->in.h.unique = fuse_get_unique(fc);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700507 queue_request(fc, req);
508 /* acquire extra reference, since request is still needed
509 after request_end() */
510 __fuse_get_request(req);
511
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700512 request_wait_answer(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700513 }
Miklos Szeredid7133112006-04-10 22:54:55 -0700514 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700515}
Eric Wong6a4e9222013-02-04 13:04:44 +0000516
517void fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
518{
519 req->isreply = 1;
Miklos Szeredi5437f242015-07-01 16:25:56 +0200520 if (!req->waiting) {
521 req->waiting = 1;
522 atomic_inc(&fc->num_waiting);
523 }
Eric Wong6a4e9222013-02-04 13:04:44 +0000524 __fuse_request_send(fc, req);
525}
Tejun Heo08cbf542009-04-14 10:54:53 +0900526EXPORT_SYMBOL_GPL(fuse_request_send);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700527
Miklos Szeredi21f62172015-01-06 10:45:35 +0100528static void fuse_adjust_compat(struct fuse_conn *fc, struct fuse_args *args)
529{
530 if (fc->minor < 4 && args->in.h.opcode == FUSE_STATFS)
531 args->out.args[0].size = FUSE_COMPAT_STATFS_SIZE;
532
533 if (fc->minor < 9) {
534 switch (args->in.h.opcode) {
535 case FUSE_LOOKUP:
536 case FUSE_CREATE:
537 case FUSE_MKNOD:
538 case FUSE_MKDIR:
539 case FUSE_SYMLINK:
540 case FUSE_LINK:
541 args->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
542 break;
543 case FUSE_GETATTR:
544 case FUSE_SETATTR:
545 args->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
546 break;
547 }
548 }
549 if (fc->minor < 12) {
550 switch (args->in.h.opcode) {
551 case FUSE_CREATE:
552 args->in.args[0].size = sizeof(struct fuse_open_in);
553 break;
554 case FUSE_MKNOD:
555 args->in.args[0].size = FUSE_COMPAT_MKNOD_IN_SIZE;
556 break;
557 }
558 }
559}
560
Miklos Szeredi70781872014-12-12 09:49:05 +0100561ssize_t fuse_simple_request(struct fuse_conn *fc, struct fuse_args *args)
562{
563 struct fuse_req *req;
564 ssize_t ret;
565
566 req = fuse_get_req(fc, 0);
567 if (IS_ERR(req))
568 return PTR_ERR(req);
569
Miklos Szeredi21f62172015-01-06 10:45:35 +0100570 /* Needs to be done after fuse_get_req() so that fc->minor is valid */
571 fuse_adjust_compat(fc, args);
572
Miklos Szeredi70781872014-12-12 09:49:05 +0100573 req->in.h.opcode = args->in.h.opcode;
574 req->in.h.nodeid = args->in.h.nodeid;
575 req->in.numargs = args->in.numargs;
576 memcpy(req->in.args, args->in.args,
577 args->in.numargs * sizeof(struct fuse_in_arg));
578 req->out.argvar = args->out.argvar;
579 req->out.numargs = args->out.numargs;
580 memcpy(req->out.args, args->out.args,
581 args->out.numargs * sizeof(struct fuse_arg));
582 fuse_request_send(fc, req);
583 ret = req->out.h.error;
584 if (!ret && args->out.argvar) {
585 BUG_ON(args->out.numargs != 1);
586 ret = req->out.args[0].size;
587 }
588 fuse_put_request(fc, req);
589
590 return ret;
591}
592
Miklos Szeredif0139aa2015-07-01 16:25:57 +0200593/*
594 * Called under fc->lock
595 *
596 * fc->connected must have been checked previously
597 */
598void fuse_request_send_background_locked(struct fuse_conn *fc,
599 struct fuse_req *req)
Miklos Szeredid12def12008-02-06 01:38:39 -0800600{
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400601 BUG_ON(!req->background);
Miklos Szeredi5437f242015-07-01 16:25:56 +0200602 if (!req->waiting) {
603 req->waiting = 1;
604 atomic_inc(&fc->num_waiting);
605 }
Miklos Szeredif0139aa2015-07-01 16:25:57 +0200606 req->isreply = 1;
Miklos Szeredid12def12008-02-06 01:38:39 -0800607 fc->num_background++;
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700608 if (fc->num_background == fc->max_background)
Miklos Szeredid12def12008-02-06 01:38:39 -0800609 fc->blocked = 1;
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700610 if (fc->num_background == fc->congestion_threshold &&
Tejun Heoa325f9b2009-04-14 10:54:52 +0900611 fc->bdi_initialized) {
Jens Axboe8aa7e842009-07-09 14:52:32 +0200612 set_bdi_congested(&fc->bdi, BLK_RW_SYNC);
613 set_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
Miklos Szeredid12def12008-02-06 01:38:39 -0800614 }
615 list_add_tail(&req->list, &fc->bg_queue);
616 flush_bg_queue(fc);
617}
618
Miklos Szeredif0139aa2015-07-01 16:25:57 +0200619void fuse_request_send_background(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700620{
Miklos Szeredi42dc6212015-07-01 16:25:56 +0200621 BUG_ON(!req->end);
Miklos Szeredid7133112006-04-10 22:54:55 -0700622 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700623 if (fc->connected) {
Miklos Szeredif0139aa2015-07-01 16:25:57 +0200624 fuse_request_send_background_locked(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700625 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700626 } else {
Miklos Szeredi42dc6212015-07-01 16:25:56 +0200627 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700628 req->out.h.error = -ENOTCONN;
Miklos Szeredi42dc6212015-07-01 16:25:56 +0200629 req->end(fc, req);
630 fuse_put_request(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700631 }
632}
Tejun Heo08cbf542009-04-14 10:54:53 +0900633EXPORT_SYMBOL_GPL(fuse_request_send_background);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700634
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200635static int fuse_request_send_notify_reply(struct fuse_conn *fc,
636 struct fuse_req *req, u64 unique)
637{
638 int err = -ENODEV;
639
640 req->isreply = 0;
641 req->in.h.unique = unique;
642 spin_lock(&fc->lock);
643 if (fc->connected) {
644 queue_request(fc, req);
645 err = 0;
646 }
647 spin_unlock(&fc->lock);
648
649 return err;
650}
651
Anand V. Avati0b05b182012-08-19 08:53:23 -0400652void fuse_force_forget(struct file *file, u64 nodeid)
653{
Al Viro6131ffa2013-02-27 16:59:05 -0500654 struct inode *inode = file_inode(file);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400655 struct fuse_conn *fc = get_fuse_conn(inode);
656 struct fuse_req *req;
657 struct fuse_forget_in inarg;
658
659 memset(&inarg, 0, sizeof(inarg));
660 inarg.nlookup = 1;
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400661 req = fuse_get_req_nofail_nopages(fc, file);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400662 req->in.h.opcode = FUSE_FORGET;
663 req->in.h.nodeid = nodeid;
664 req->in.numargs = 1;
665 req->in.args[0].size = sizeof(inarg);
666 req->in.args[0].value = &inarg;
667 req->isreply = 0;
Eric Wong6a4e9222013-02-04 13:04:44 +0000668 __fuse_request_send(fc, req);
669 /* ignore errors */
670 fuse_put_request(fc, req);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400671}
672
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700673/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700674 * Lock the request. Up to the next unlock_request() there mustn't be
675 * anything that could cause a page-fault. If the request was already
Miklos Szeredif9a28422006-06-25 05:48:53 -0700676 * aborted bail out.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700677 */
Miklos Szeredid7133112006-04-10 22:54:55 -0700678static int lock_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700679{
680 int err = 0;
681 if (req) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700682 spin_lock(&fc->lock);
Miklos Szeredif9a28422006-06-25 05:48:53 -0700683 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700684 err = -ENOENT;
685 else
686 req->locked = 1;
Miklos Szeredid7133112006-04-10 22:54:55 -0700687 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700688 }
689 return err;
690}
691
692/*
Miklos Szeredif9a28422006-06-25 05:48:53 -0700693 * Unlock request. If it was aborted during being locked, the
Miklos Szeredi334f4852005-09-09 13:10:27 -0700694 * requester thread is currently waiting for it to be unlocked, so
695 * wake it up.
696 */
Miklos Szeredid7133112006-04-10 22:54:55 -0700697static void unlock_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700698{
699 if (req) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700700 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700701 req->locked = 0;
Miklos Szeredif9a28422006-06-25 05:48:53 -0700702 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700703 wake_up(&req->waitq);
Miklos Szeredid7133112006-04-10 22:54:55 -0700704 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700705 }
706}
707
708struct fuse_copy_state {
Miklos Szeredid7133112006-04-10 22:54:55 -0700709 struct fuse_conn *fc;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700710 int write;
711 struct fuse_req *req;
Al Viro6c09e942015-04-03 22:06:08 -0400712 struct iov_iter *iter;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200713 struct pipe_buffer *pipebufs;
714 struct pipe_buffer *currbuf;
715 struct pipe_inode_info *pipe;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700716 unsigned long nr_segs;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700717 struct page *pg;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700718 unsigned len;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200719 unsigned offset;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200720 unsigned move_pages:1;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700721};
722
Al Viro6c09e942015-04-03 22:06:08 -0400723static void fuse_copy_init(struct fuse_copy_state *cs,
724 struct fuse_conn *fc,
Miklos Szeredic3021622010-05-25 15:06:07 +0200725 int write,
Al Viro6c09e942015-04-03 22:06:08 -0400726 struct iov_iter *iter)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700727{
728 memset(cs, 0, sizeof(*cs));
Miklos Szeredid7133112006-04-10 22:54:55 -0700729 cs->fc = fc;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700730 cs->write = write;
Al Viro6c09e942015-04-03 22:06:08 -0400731 cs->iter = iter;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700732}
733
734/* Unmap and put previous page of userspace buffer */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800735static void fuse_copy_finish(struct fuse_copy_state *cs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700736{
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200737 if (cs->currbuf) {
738 struct pipe_buffer *buf = cs->currbuf;
739
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200740 if (cs->write)
Miklos Szeredic3021622010-05-25 15:06:07 +0200741 buf->len = PAGE_SIZE - cs->len;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200742 cs->currbuf = NULL;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200743 } else if (cs->pg) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700744 if (cs->write) {
745 flush_dcache_page(cs->pg);
746 set_page_dirty_lock(cs->pg);
747 }
748 put_page(cs->pg);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700749 }
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200750 cs->pg = NULL;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700751}
752
753/*
754 * Get another pagefull of userspace buffer, and map it to kernel
755 * address space, and lock request
756 */
757static int fuse_copy_fill(struct fuse_copy_state *cs)
758{
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200759 struct page *page;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700760 int err;
761
Miklos Szeredid7133112006-04-10 22:54:55 -0700762 unlock_request(cs->fc, cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700763 fuse_copy_finish(cs);
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200764 if (cs->pipebufs) {
765 struct pipe_buffer *buf = cs->pipebufs;
766
Miklos Szeredic3021622010-05-25 15:06:07 +0200767 if (!cs->write) {
768 err = buf->ops->confirm(cs->pipe, buf);
769 if (err)
770 return err;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200771
Miklos Szeredic3021622010-05-25 15:06:07 +0200772 BUG_ON(!cs->nr_segs);
773 cs->currbuf = buf;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200774 cs->pg = buf->page;
775 cs->offset = buf->offset;
Miklos Szeredic3021622010-05-25 15:06:07 +0200776 cs->len = buf->len;
Miklos Szeredic3021622010-05-25 15:06:07 +0200777 cs->pipebufs++;
778 cs->nr_segs--;
779 } else {
Miklos Szeredic3021622010-05-25 15:06:07 +0200780 if (cs->nr_segs == cs->pipe->buffers)
781 return -EIO;
782
783 page = alloc_page(GFP_HIGHUSER);
784 if (!page)
785 return -ENOMEM;
786
787 buf->page = page;
788 buf->offset = 0;
789 buf->len = 0;
790
791 cs->currbuf = buf;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200792 cs->pg = page;
793 cs->offset = 0;
Miklos Szeredic3021622010-05-25 15:06:07 +0200794 cs->len = PAGE_SIZE;
795 cs->pipebufs++;
796 cs->nr_segs++;
797 }
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200798 } else {
Al Viro6c09e942015-04-03 22:06:08 -0400799 size_t off;
800 err = iov_iter_get_pages(cs->iter, &page, PAGE_SIZE, 1, &off);
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200801 if (err < 0)
802 return err;
Al Viro6c09e942015-04-03 22:06:08 -0400803 BUG_ON(!err);
804 cs->len = err;
805 cs->offset = off;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200806 cs->pg = page;
Al Viro6c09e942015-04-03 22:06:08 -0400807 cs->offset = off;
808 iov_iter_advance(cs->iter, err);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700809 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700810
Miklos Szeredid7133112006-04-10 22:54:55 -0700811 return lock_request(cs->fc, cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700812}
813
814/* Do as much copy to/from userspace buffer as we can */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800815static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700816{
817 unsigned ncpy = min(*size, cs->len);
818 if (val) {
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200819 void *pgaddr = kmap_atomic(cs->pg);
820 void *buf = pgaddr + cs->offset;
821
Miklos Szeredi334f4852005-09-09 13:10:27 -0700822 if (cs->write)
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200823 memcpy(buf, *val, ncpy);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700824 else
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200825 memcpy(*val, buf, ncpy);
826
827 kunmap_atomic(pgaddr);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700828 *val += ncpy;
829 }
830 *size -= ncpy;
831 cs->len -= ncpy;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200832 cs->offset += ncpy;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700833 return ncpy;
834}
835
Miklos Szeredice534fb2010-05-25 15:06:07 +0200836static int fuse_check_page(struct page *page)
837{
838 if (page_mapcount(page) ||
839 page->mapping != NULL ||
840 page_count(page) != 1 ||
841 (page->flags & PAGE_FLAGS_CHECK_AT_PREP &
842 ~(1 << PG_locked |
843 1 << PG_referenced |
844 1 << PG_uptodate |
845 1 << PG_lru |
846 1 << PG_active |
847 1 << PG_reclaim))) {
848 printk(KERN_WARNING "fuse: trying to steal weird page\n");
849 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);
850 return 1;
851 }
852 return 0;
853}
854
855static int fuse_try_move_page(struct fuse_copy_state *cs, struct page **pagep)
856{
857 int err;
858 struct page *oldpage = *pagep;
859 struct page *newpage;
860 struct pipe_buffer *buf = cs->pipebufs;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200861
862 unlock_request(cs->fc, cs->req);
863 fuse_copy_finish(cs);
864
865 err = buf->ops->confirm(cs->pipe, buf);
866 if (err)
867 return err;
868
869 BUG_ON(!cs->nr_segs);
870 cs->currbuf = buf;
871 cs->len = buf->len;
872 cs->pipebufs++;
873 cs->nr_segs--;
874
875 if (cs->len != PAGE_SIZE)
876 goto out_fallback;
877
878 if (buf->ops->steal(cs->pipe, buf) != 0)
879 goto out_fallback;
880
881 newpage = buf->page;
882
Miklos Szerediaa991b32015-02-26 11:45:47 +0100883 if (!PageUptodate(newpage))
884 SetPageUptodate(newpage);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200885
886 ClearPageMappedToDisk(newpage);
887
888 if (fuse_check_page(newpage) != 0)
889 goto out_fallback_unlock;
890
Miklos Szeredice534fb2010-05-25 15:06:07 +0200891 /*
892 * This is a new and locked page, it shouldn't be mapped or
893 * have any special flags on it
894 */
895 if (WARN_ON(page_mapped(oldpage)))
896 goto out_fallback_unlock;
897 if (WARN_ON(page_has_private(oldpage)))
898 goto out_fallback_unlock;
899 if (WARN_ON(PageDirty(oldpage) || PageWriteback(oldpage)))
900 goto out_fallback_unlock;
901 if (WARN_ON(PageMlocked(oldpage)))
902 goto out_fallback_unlock;
903
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700904 err = replace_page_cache_page(oldpage, newpage, GFP_KERNEL);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200905 if (err) {
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700906 unlock_page(newpage);
907 return err;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200908 }
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700909
Miklos Szeredice534fb2010-05-25 15:06:07 +0200910 page_cache_get(newpage);
911
912 if (!(buf->flags & PIPE_BUF_FLAG_LRU))
913 lru_cache_add_file(newpage);
914
915 err = 0;
916 spin_lock(&cs->fc->lock);
917 if (cs->req->aborted)
918 err = -ENOENT;
919 else
920 *pagep = newpage;
921 spin_unlock(&cs->fc->lock);
922
923 if (err) {
924 unlock_page(newpage);
925 page_cache_release(newpage);
926 return err;
927 }
928
929 unlock_page(oldpage);
930 page_cache_release(oldpage);
931 cs->len = 0;
932
933 return 0;
934
935out_fallback_unlock:
936 unlock_page(newpage);
937out_fallback:
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200938 cs->pg = buf->page;
939 cs->offset = buf->offset;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200940
941 err = lock_request(cs->fc, cs->req);
942 if (err)
943 return err;
944
945 return 1;
946}
947
Miklos Szeredic3021622010-05-25 15:06:07 +0200948static int fuse_ref_page(struct fuse_copy_state *cs, struct page *page,
949 unsigned offset, unsigned count)
950{
951 struct pipe_buffer *buf;
952
953 if (cs->nr_segs == cs->pipe->buffers)
954 return -EIO;
955
956 unlock_request(cs->fc, cs->req);
957 fuse_copy_finish(cs);
958
959 buf = cs->pipebufs;
960 page_cache_get(page);
961 buf->page = page;
962 buf->offset = offset;
963 buf->len = count;
964
965 cs->pipebufs++;
966 cs->nr_segs++;
967 cs->len = 0;
968
969 return 0;
970}
971
Miklos Szeredi334f4852005-09-09 13:10:27 -0700972/*
973 * Copy a page in the request to/from the userspace buffer. Must be
974 * done atomically
975 */
Miklos Szeredice534fb2010-05-25 15:06:07 +0200976static int fuse_copy_page(struct fuse_copy_state *cs, struct page **pagep,
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800977 unsigned offset, unsigned count, int zeroing)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700978{
Miklos Szeredice534fb2010-05-25 15:06:07 +0200979 int err;
980 struct page *page = *pagep;
981
Miklos Szeredib6777c42010-10-26 14:22:27 -0700982 if (page && zeroing && count < PAGE_SIZE)
983 clear_highpage(page);
984
Miklos Szeredi334f4852005-09-09 13:10:27 -0700985 while (count) {
Miklos Szeredic3021622010-05-25 15:06:07 +0200986 if (cs->write && cs->pipebufs && page) {
987 return fuse_ref_page(cs, page, offset, count);
988 } else if (!cs->len) {
Miklos Szeredice534fb2010-05-25 15:06:07 +0200989 if (cs->move_pages && page &&
990 offset == 0 && count == PAGE_SIZE) {
991 err = fuse_try_move_page(cs, pagep);
992 if (err <= 0)
993 return err;
994 } else {
995 err = fuse_copy_fill(cs);
996 if (err)
997 return err;
998 }
Miklos Szeredi1729a162008-11-26 12:03:54 +0100999 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001000 if (page) {
Cong Wang2408f6e2011-11-25 23:14:30 +08001001 void *mapaddr = kmap_atomic(page);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001002 void *buf = mapaddr + offset;
1003 offset += fuse_copy_do(cs, &buf, &count);
Cong Wang2408f6e2011-11-25 23:14:30 +08001004 kunmap_atomic(mapaddr);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001005 } else
1006 offset += fuse_copy_do(cs, NULL, &count);
1007 }
1008 if (page && !cs->write)
1009 flush_dcache_page(page);
1010 return 0;
1011}
1012
1013/* Copy pages in the request to/from userspace buffer */
1014static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
1015 int zeroing)
1016{
1017 unsigned i;
1018 struct fuse_req *req = cs->req;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001019
1020 for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
Miklos Szeredice534fb2010-05-25 15:06:07 +02001021 int err;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001022 unsigned offset = req->page_descs[i].offset;
1023 unsigned count = min(nbytes, req->page_descs[i].length);
Miklos Szeredice534fb2010-05-25 15:06:07 +02001024
1025 err = fuse_copy_page(cs, &req->pages[i], offset, count,
1026 zeroing);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001027 if (err)
1028 return err;
1029
1030 nbytes -= count;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001031 }
1032 return 0;
1033}
1034
1035/* Copy a single argument in the request to/from userspace buffer */
1036static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
1037{
1038 while (size) {
Miklos Szeredi1729a162008-11-26 12:03:54 +01001039 if (!cs->len) {
1040 int err = fuse_copy_fill(cs);
1041 if (err)
1042 return err;
1043 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001044 fuse_copy_do(cs, &val, &size);
1045 }
1046 return 0;
1047}
1048
1049/* Copy request arguments to/from userspace buffer */
1050static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
1051 unsigned argpages, struct fuse_arg *args,
1052 int zeroing)
1053{
1054 int err = 0;
1055 unsigned i;
1056
1057 for (i = 0; !err && i < numargs; i++) {
1058 struct fuse_arg *arg = &args[i];
1059 if (i == numargs - 1 && argpages)
1060 err = fuse_copy_pages(cs, arg->size, zeroing);
1061 else
1062 err = fuse_copy_one(cs, arg->value, arg->size);
1063 }
1064 return err;
1065}
1066
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001067static int forget_pending(struct fuse_conn *fc)
1068{
1069 return fc->forget_list_head.next != NULL;
1070}
1071
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001072static int request_pending(struct fuse_conn *fc)
1073{
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001074 return !list_empty(&fc->pending) || !list_empty(&fc->interrupts) ||
1075 forget_pending(fc);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001076}
1077
Miklos Szeredi334f4852005-09-09 13:10:27 -07001078/* Wait until a request is available on the pending list */
1079static void request_wait(struct fuse_conn *fc)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001080__releases(fc->lock)
1081__acquires(fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001082{
1083 DECLARE_WAITQUEUE(wait, current);
1084
1085 add_wait_queue_exclusive(&fc->waitq, &wait);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001086 while (fc->connected && !request_pending(fc)) {
Miklos Szeredi334f4852005-09-09 13:10:27 -07001087 set_current_state(TASK_INTERRUPTIBLE);
1088 if (signal_pending(current))
1089 break;
1090
Miklos Szeredid7133112006-04-10 22:54:55 -07001091 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001092 schedule();
Miklos Szeredid7133112006-04-10 22:54:55 -07001093 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001094 }
1095 set_current_state(TASK_RUNNING);
1096 remove_wait_queue(&fc->waitq, &wait);
1097}
1098
1099/*
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001100 * Transfer an interrupt request to userspace
1101 *
1102 * Unlike other requests this is assembled on demand, without a need
1103 * to allocate a separate fuse_req structure.
1104 *
1105 * Called with fc->lock held, releases it
1106 */
Miklos Szeredic3021622010-05-25 15:06:07 +02001107static int fuse_read_interrupt(struct fuse_conn *fc, struct fuse_copy_state *cs,
1108 size_t nbytes, struct fuse_req *req)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001109__releases(fc->lock)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001110{
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001111 struct fuse_in_header ih;
1112 struct fuse_interrupt_in arg;
1113 unsigned reqsize = sizeof(ih) + sizeof(arg);
1114 int err;
1115
1116 list_del_init(&req->intr_entry);
1117 req->intr_unique = fuse_get_unique(fc);
1118 memset(&ih, 0, sizeof(ih));
1119 memset(&arg, 0, sizeof(arg));
1120 ih.len = reqsize;
1121 ih.opcode = FUSE_INTERRUPT;
1122 ih.unique = req->intr_unique;
1123 arg.unique = req->in.h.unique;
1124
1125 spin_unlock(&fc->lock);
Miklos Szeredic3021622010-05-25 15:06:07 +02001126 if (nbytes < reqsize)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001127 return -EINVAL;
1128
Miklos Szeredic3021622010-05-25 15:06:07 +02001129 err = fuse_copy_one(cs, &ih, sizeof(ih));
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001130 if (!err)
Miklos Szeredic3021622010-05-25 15:06:07 +02001131 err = fuse_copy_one(cs, &arg, sizeof(arg));
1132 fuse_copy_finish(cs);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001133
1134 return err ? err : reqsize;
1135}
1136
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001137static struct fuse_forget_link *dequeue_forget(struct fuse_conn *fc,
1138 unsigned max,
1139 unsigned *countp)
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001140{
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001141 struct fuse_forget_link *head = fc->forget_list_head.next;
1142 struct fuse_forget_link **newhead = &head;
1143 unsigned count;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001144
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001145 for (count = 0; *newhead != NULL && count < max; count++)
1146 newhead = &(*newhead)->next;
1147
1148 fc->forget_list_head.next = *newhead;
1149 *newhead = NULL;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001150 if (fc->forget_list_head.next == NULL)
1151 fc->forget_list_tail = &fc->forget_list_head;
1152
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001153 if (countp != NULL)
1154 *countp = count;
1155
1156 return head;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001157}
1158
1159static int fuse_read_single_forget(struct fuse_conn *fc,
1160 struct fuse_copy_state *cs,
1161 size_t nbytes)
1162__releases(fc->lock)
1163{
1164 int err;
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001165 struct fuse_forget_link *forget = dequeue_forget(fc, 1, NULL);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001166 struct fuse_forget_in arg = {
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001167 .nlookup = forget->forget_one.nlookup,
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001168 };
1169 struct fuse_in_header ih = {
1170 .opcode = FUSE_FORGET,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001171 .nodeid = forget->forget_one.nodeid,
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001172 .unique = fuse_get_unique(fc),
1173 .len = sizeof(ih) + sizeof(arg),
1174 };
1175
1176 spin_unlock(&fc->lock);
1177 kfree(forget);
1178 if (nbytes < ih.len)
1179 return -EINVAL;
1180
1181 err = fuse_copy_one(cs, &ih, sizeof(ih));
1182 if (!err)
1183 err = fuse_copy_one(cs, &arg, sizeof(arg));
1184 fuse_copy_finish(cs);
1185
1186 if (err)
1187 return err;
1188
1189 return ih.len;
1190}
1191
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001192static int fuse_read_batch_forget(struct fuse_conn *fc,
1193 struct fuse_copy_state *cs, size_t nbytes)
1194__releases(fc->lock)
1195{
1196 int err;
1197 unsigned max_forgets;
1198 unsigned count;
1199 struct fuse_forget_link *head;
1200 struct fuse_batch_forget_in arg = { .count = 0 };
1201 struct fuse_in_header ih = {
1202 .opcode = FUSE_BATCH_FORGET,
1203 .unique = fuse_get_unique(fc),
1204 .len = sizeof(ih) + sizeof(arg),
1205 };
1206
1207 if (nbytes < ih.len) {
1208 spin_unlock(&fc->lock);
1209 return -EINVAL;
1210 }
1211
1212 max_forgets = (nbytes - ih.len) / sizeof(struct fuse_forget_one);
1213 head = dequeue_forget(fc, max_forgets, &count);
1214 spin_unlock(&fc->lock);
1215
1216 arg.count = count;
1217 ih.len += count * sizeof(struct fuse_forget_one);
1218 err = fuse_copy_one(cs, &ih, sizeof(ih));
1219 if (!err)
1220 err = fuse_copy_one(cs, &arg, sizeof(arg));
1221
1222 while (head) {
1223 struct fuse_forget_link *forget = head;
1224
1225 if (!err) {
1226 err = fuse_copy_one(cs, &forget->forget_one,
1227 sizeof(forget->forget_one));
1228 }
1229 head = forget->next;
1230 kfree(forget);
1231 }
1232
1233 fuse_copy_finish(cs);
1234
1235 if (err)
1236 return err;
1237
1238 return ih.len;
1239}
1240
1241static int fuse_read_forget(struct fuse_conn *fc, struct fuse_copy_state *cs,
1242 size_t nbytes)
1243__releases(fc->lock)
1244{
1245 if (fc->minor < 16 || fc->forget_list_head.next->next == NULL)
1246 return fuse_read_single_forget(fc, cs, nbytes);
1247 else
1248 return fuse_read_batch_forget(fc, cs, nbytes);
1249}
1250
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001251/*
Miklos Szeredi334f4852005-09-09 13:10:27 -07001252 * Read a single request into the userspace filesystem's buffer. This
1253 * function waits until a request is available, then removes it from
1254 * the pending list and copies request data to userspace buffer. If
Miklos Szeredif9a28422006-06-25 05:48:53 -07001255 * no reply is needed (FORGET) or request has been aborted or there
1256 * was an error during the copying then it's finished by calling
Miklos Szeredi334f4852005-09-09 13:10:27 -07001257 * request_end(). Otherwise add it to the processing list, and set
1258 * the 'sent' flag.
1259 */
Miklos Szeredic3021622010-05-25 15:06:07 +02001260static ssize_t fuse_dev_do_read(struct fuse_conn *fc, struct file *file,
1261 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001262{
1263 int err;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001264 struct fuse_req *req;
1265 struct fuse_in *in;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001266 unsigned reqsize;
1267
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001268 restart:
Miklos Szeredid7133112006-04-10 22:54:55 -07001269 spin_lock(&fc->lock);
Jeff Dikee5ac1d12006-04-10 22:54:53 -07001270 err = -EAGAIN;
1271 if ((file->f_flags & O_NONBLOCK) && fc->connected &&
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001272 !request_pending(fc))
Jeff Dikee5ac1d12006-04-10 22:54:53 -07001273 goto err_unlock;
1274
Miklos Szeredi334f4852005-09-09 13:10:27 -07001275 request_wait(fc);
1276 err = -ENODEV;
Miklos Szeredi9ba7cbb2006-01-16 22:14:34 -08001277 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001278 goto err_unlock;
1279 err = -ERESTARTSYS;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001280 if (!request_pending(fc))
Miklos Szeredi334f4852005-09-09 13:10:27 -07001281 goto err_unlock;
1282
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001283 if (!list_empty(&fc->interrupts)) {
1284 req = list_entry(fc->interrupts.next, struct fuse_req,
1285 intr_entry);
Miklos Szeredic3021622010-05-25 15:06:07 +02001286 return fuse_read_interrupt(fc, cs, nbytes, req);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001287 }
1288
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001289 if (forget_pending(fc)) {
1290 if (list_empty(&fc->pending) || fc->forget_batch-- > 0)
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001291 return fuse_read_forget(fc, cs, nbytes);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001292
1293 if (fc->forget_batch <= -8)
1294 fc->forget_batch = 16;
1295 }
1296
Miklos Szeredi334f4852005-09-09 13:10:27 -07001297 req = list_entry(fc->pending.next, struct fuse_req, list);
Miklos Szeredi83cfd492006-01-16 22:14:31 -08001298 req->state = FUSE_REQ_READING;
Miklos Szeredid77a1d52006-01-16 22:14:31 -08001299 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001300
1301 in = &req->in;
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001302 reqsize = in->h.len;
1303 /* If request is too large, reply with an error and restart the read */
Miklos Szeredic3021622010-05-25 15:06:07 +02001304 if (nbytes < reqsize) {
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001305 req->out.h.error = -EIO;
1306 /* SETXATTR is special, since it may contain too large data */
1307 if (in->h.opcode == FUSE_SETXATTR)
1308 req->out.h.error = -E2BIG;
1309 request_end(fc, req);
1310 goto restart;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001311 }
Miklos Szeredid7133112006-04-10 22:54:55 -07001312 spin_unlock(&fc->lock);
Miklos Szeredic3021622010-05-25 15:06:07 +02001313 cs->req = req;
1314 err = fuse_copy_one(cs, &in->h, sizeof(in->h));
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001315 if (!err)
Miklos Szeredic3021622010-05-25 15:06:07 +02001316 err = fuse_copy_args(cs, in->numargs, in->argpages,
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001317 (struct fuse_arg *) in->args, 0);
Miklos Szeredic3021622010-05-25 15:06:07 +02001318 fuse_copy_finish(cs);
Miklos Szeredid7133112006-04-10 22:54:55 -07001319 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001320 req->locked = 0;
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -07001321 if (req->aborted) {
1322 request_end(fc, req);
1323 return -ENODEV;
1324 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001325 if (err) {
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -07001326 req->out.h.error = -EIO;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001327 request_end(fc, req);
1328 return err;
1329 }
1330 if (!req->isreply)
1331 request_end(fc, req);
1332 else {
Miklos Szeredi83cfd492006-01-16 22:14:31 -08001333 req->state = FUSE_REQ_SENT;
Miklos Szeredid77a1d52006-01-16 22:14:31 -08001334 list_move_tail(&req->list, &fc->processing);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001335 if (req->interrupted)
1336 queue_interrupt(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -07001337 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001338 }
1339 return reqsize;
1340
1341 err_unlock:
Miklos Szeredid7133112006-04-10 22:54:55 -07001342 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001343 return err;
1344}
1345
Tom Van Braeckel94e4fe22015-01-12 05:22:16 +01001346static int fuse_dev_open(struct inode *inode, struct file *file)
1347{
1348 /*
1349 * The fuse device's file's private_data is used to hold
1350 * the fuse_conn(ection) when it is mounted, and is used to
1351 * keep track of whether the file has been mounted already.
1352 */
1353 file->private_data = NULL;
1354 return 0;
1355}
1356
Al Virofbdbacc2015-04-03 21:53:39 -04001357static ssize_t fuse_dev_read(struct kiocb *iocb, struct iov_iter *to)
Miklos Szeredic3021622010-05-25 15:06:07 +02001358{
1359 struct fuse_copy_state cs;
1360 struct file *file = iocb->ki_filp;
1361 struct fuse_conn *fc = fuse_get_conn(file);
1362 if (!fc)
1363 return -EPERM;
1364
Al Virofbdbacc2015-04-03 21:53:39 -04001365 if (!iter_is_iovec(to))
1366 return -EINVAL;
Miklos Szeredic3021622010-05-25 15:06:07 +02001367
Al Viro6c09e942015-04-03 22:06:08 -04001368 fuse_copy_init(&cs, fc, 1, to);
Al Virofbdbacc2015-04-03 21:53:39 -04001369
1370 return fuse_dev_do_read(fc, file, &cs, iov_iter_count(to));
Miklos Szeredic3021622010-05-25 15:06:07 +02001371}
1372
Miklos Szeredic3021622010-05-25 15:06:07 +02001373static ssize_t fuse_dev_splice_read(struct file *in, loff_t *ppos,
1374 struct pipe_inode_info *pipe,
1375 size_t len, unsigned int flags)
1376{
1377 int ret;
1378 int page_nr = 0;
1379 int do_wakeup = 0;
1380 struct pipe_buffer *bufs;
1381 struct fuse_copy_state cs;
1382 struct fuse_conn *fc = fuse_get_conn(in);
1383 if (!fc)
1384 return -EPERM;
1385
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001386 bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), GFP_KERNEL);
Miklos Szeredic3021622010-05-25 15:06:07 +02001387 if (!bufs)
1388 return -ENOMEM;
1389
Al Viro6c09e942015-04-03 22:06:08 -04001390 fuse_copy_init(&cs, fc, 1, NULL);
Miklos Szeredic3021622010-05-25 15:06:07 +02001391 cs.pipebufs = bufs;
1392 cs.pipe = pipe;
1393 ret = fuse_dev_do_read(fc, in, &cs, len);
1394 if (ret < 0)
1395 goto out;
1396
1397 ret = 0;
1398 pipe_lock(pipe);
1399
1400 if (!pipe->readers) {
1401 send_sig(SIGPIPE, current, 0);
1402 if (!ret)
1403 ret = -EPIPE;
1404 goto out_unlock;
1405 }
1406
1407 if (pipe->nrbufs + cs.nr_segs > pipe->buffers) {
1408 ret = -EIO;
1409 goto out_unlock;
1410 }
1411
1412 while (page_nr < cs.nr_segs) {
1413 int newbuf = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
1414 struct pipe_buffer *buf = pipe->bufs + newbuf;
1415
1416 buf->page = bufs[page_nr].page;
1417 buf->offset = bufs[page_nr].offset;
1418 buf->len = bufs[page_nr].len;
Miklos Szeredi28a625c2014-01-22 19:36:57 +01001419 /*
1420 * Need to be careful about this. Having buf->ops in module
1421 * code can Oops if the buffer persists after module unload.
1422 */
1423 buf->ops = &nosteal_pipe_buf_ops;
Miklos Szeredic3021622010-05-25 15:06:07 +02001424
1425 pipe->nrbufs++;
1426 page_nr++;
1427 ret += buf->len;
1428
Al Viro6447a3c2013-03-21 11:01:38 -04001429 if (pipe->files)
Miklos Szeredic3021622010-05-25 15:06:07 +02001430 do_wakeup = 1;
1431 }
1432
1433out_unlock:
1434 pipe_unlock(pipe);
1435
1436 if (do_wakeup) {
1437 smp_mb();
1438 if (waitqueue_active(&pipe->wait))
1439 wake_up_interruptible(&pipe->wait);
1440 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
1441 }
1442
1443out:
1444 for (; page_nr < cs.nr_segs; page_nr++)
1445 page_cache_release(bufs[page_nr].page);
1446
1447 kfree(bufs);
1448 return ret;
1449}
1450
Tejun Heo95668a62008-11-26 12:03:55 +01001451static int fuse_notify_poll(struct fuse_conn *fc, unsigned int size,
1452 struct fuse_copy_state *cs)
1453{
1454 struct fuse_notify_poll_wakeup_out outarg;
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001455 int err = -EINVAL;
Tejun Heo95668a62008-11-26 12:03:55 +01001456
1457 if (size != sizeof(outarg))
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001458 goto err;
Tejun Heo95668a62008-11-26 12:03:55 +01001459
1460 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1461 if (err)
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001462 goto err;
Tejun Heo95668a62008-11-26 12:03:55 +01001463
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001464 fuse_copy_finish(cs);
Tejun Heo95668a62008-11-26 12:03:55 +01001465 return fuse_notify_poll_wakeup(fc, &outarg);
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001466
1467err:
1468 fuse_copy_finish(cs);
1469 return err;
Tejun Heo95668a62008-11-26 12:03:55 +01001470}
1471
John Muir3b463ae2009-05-31 11:13:57 -04001472static int fuse_notify_inval_inode(struct fuse_conn *fc, unsigned int size,
1473 struct fuse_copy_state *cs)
1474{
1475 struct fuse_notify_inval_inode_out outarg;
1476 int err = -EINVAL;
1477
1478 if (size != sizeof(outarg))
1479 goto err;
1480
1481 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1482 if (err)
1483 goto err;
1484 fuse_copy_finish(cs);
1485
1486 down_read(&fc->killsb);
1487 err = -ENOENT;
Miklos Szeredib21dda42010-02-05 12:08:31 +01001488 if (fc->sb) {
1489 err = fuse_reverse_inval_inode(fc->sb, outarg.ino,
1490 outarg.off, outarg.len);
1491 }
John Muir3b463ae2009-05-31 11:13:57 -04001492 up_read(&fc->killsb);
1493 return err;
1494
1495err:
1496 fuse_copy_finish(cs);
1497 return err;
1498}
1499
1500static int fuse_notify_inval_entry(struct fuse_conn *fc, unsigned int size,
1501 struct fuse_copy_state *cs)
1502{
1503 struct fuse_notify_inval_entry_out outarg;
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001504 int err = -ENOMEM;
1505 char *buf;
John Muir3b463ae2009-05-31 11:13:57 -04001506 struct qstr name;
1507
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001508 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1509 if (!buf)
1510 goto err;
1511
1512 err = -EINVAL;
John Muir3b463ae2009-05-31 11:13:57 -04001513 if (size < sizeof(outarg))
1514 goto err;
1515
1516 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1517 if (err)
1518 goto err;
1519
1520 err = -ENAMETOOLONG;
1521 if (outarg.namelen > FUSE_NAME_MAX)
1522 goto err;
1523
Miklos Szeredic2183d12011-08-24 10:20:17 +02001524 err = -EINVAL;
1525 if (size != sizeof(outarg) + outarg.namelen + 1)
1526 goto err;
1527
John Muir3b463ae2009-05-31 11:13:57 -04001528 name.name = buf;
1529 name.len = outarg.namelen;
1530 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1531 if (err)
1532 goto err;
1533 fuse_copy_finish(cs);
1534 buf[outarg.namelen] = 0;
1535 name.hash = full_name_hash(name.name, name.len);
1536
1537 down_read(&fc->killsb);
1538 err = -ENOENT;
Miklos Szeredib21dda42010-02-05 12:08:31 +01001539 if (fc->sb)
John Muir451d0f52011-12-06 21:50:06 +01001540 err = fuse_reverse_inval_entry(fc->sb, outarg.parent, 0, &name);
1541 up_read(&fc->killsb);
1542 kfree(buf);
1543 return err;
1544
1545err:
1546 kfree(buf);
1547 fuse_copy_finish(cs);
1548 return err;
1549}
1550
1551static int fuse_notify_delete(struct fuse_conn *fc, unsigned int size,
1552 struct fuse_copy_state *cs)
1553{
1554 struct fuse_notify_delete_out outarg;
1555 int err = -ENOMEM;
1556 char *buf;
1557 struct qstr name;
1558
1559 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1560 if (!buf)
1561 goto err;
1562
1563 err = -EINVAL;
1564 if (size < sizeof(outarg))
1565 goto err;
1566
1567 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1568 if (err)
1569 goto err;
1570
1571 err = -ENAMETOOLONG;
1572 if (outarg.namelen > FUSE_NAME_MAX)
1573 goto err;
1574
1575 err = -EINVAL;
1576 if (size != sizeof(outarg) + outarg.namelen + 1)
1577 goto err;
1578
1579 name.name = buf;
1580 name.len = outarg.namelen;
1581 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1582 if (err)
1583 goto err;
1584 fuse_copy_finish(cs);
1585 buf[outarg.namelen] = 0;
1586 name.hash = full_name_hash(name.name, name.len);
1587
1588 down_read(&fc->killsb);
1589 err = -ENOENT;
1590 if (fc->sb)
1591 err = fuse_reverse_inval_entry(fc->sb, outarg.parent,
1592 outarg.child, &name);
John Muir3b463ae2009-05-31 11:13:57 -04001593 up_read(&fc->killsb);
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001594 kfree(buf);
John Muir3b463ae2009-05-31 11:13:57 -04001595 return err;
1596
1597err:
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001598 kfree(buf);
John Muir3b463ae2009-05-31 11:13:57 -04001599 fuse_copy_finish(cs);
1600 return err;
1601}
1602
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001603static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
1604 struct fuse_copy_state *cs)
1605{
1606 struct fuse_notify_store_out outarg;
1607 struct inode *inode;
1608 struct address_space *mapping;
1609 u64 nodeid;
1610 int err;
1611 pgoff_t index;
1612 unsigned int offset;
1613 unsigned int num;
1614 loff_t file_size;
1615 loff_t end;
1616
1617 err = -EINVAL;
1618 if (size < sizeof(outarg))
1619 goto out_finish;
1620
1621 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1622 if (err)
1623 goto out_finish;
1624
1625 err = -EINVAL;
1626 if (size - sizeof(outarg) != outarg.size)
1627 goto out_finish;
1628
1629 nodeid = outarg.nodeid;
1630
1631 down_read(&fc->killsb);
1632
1633 err = -ENOENT;
1634 if (!fc->sb)
1635 goto out_up_killsb;
1636
1637 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1638 if (!inode)
1639 goto out_up_killsb;
1640
1641 mapping = inode->i_mapping;
1642 index = outarg.offset >> PAGE_CACHE_SHIFT;
1643 offset = outarg.offset & ~PAGE_CACHE_MASK;
1644 file_size = i_size_read(inode);
1645 end = outarg.offset + outarg.size;
1646 if (end > file_size) {
1647 file_size = end;
1648 fuse_write_update_size(inode, file_size);
1649 }
1650
1651 num = outarg.size;
1652 while (num) {
1653 struct page *page;
1654 unsigned int this_num;
1655
1656 err = -ENOMEM;
1657 page = find_or_create_page(mapping, index,
1658 mapping_gfp_mask(mapping));
1659 if (!page)
1660 goto out_iput;
1661
1662 this_num = min_t(unsigned, num, PAGE_CACHE_SIZE - offset);
1663 err = fuse_copy_page(cs, &page, offset, this_num, 0);
Miklos Szeredi063ec1e2014-01-22 19:36:58 +01001664 if (!err && offset == 0 &&
1665 (this_num == PAGE_CACHE_SIZE || file_size == end))
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001666 SetPageUptodate(page);
1667 unlock_page(page);
1668 page_cache_release(page);
1669
1670 if (err)
1671 goto out_iput;
1672
1673 num -= this_num;
1674 offset = 0;
1675 index++;
1676 }
1677
1678 err = 0;
1679
1680out_iput:
1681 iput(inode);
1682out_up_killsb:
1683 up_read(&fc->killsb);
1684out_finish:
1685 fuse_copy_finish(cs);
1686 return err;
1687}
1688
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001689static void fuse_retrieve_end(struct fuse_conn *fc, struct fuse_req *req)
1690{
Mel Gormanb745bc82014-06-04 16:10:22 -07001691 release_pages(req->pages, req->num_pages, false);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001692}
1693
1694static int fuse_retrieve(struct fuse_conn *fc, struct inode *inode,
1695 struct fuse_notify_retrieve_out *outarg)
1696{
1697 int err;
1698 struct address_space *mapping = inode->i_mapping;
1699 struct fuse_req *req;
1700 pgoff_t index;
1701 loff_t file_size;
1702 unsigned int num;
1703 unsigned int offset;
Geert Uytterhoeven01574432010-09-30 22:06:21 +02001704 size_t total_len = 0;
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001705 int num_pages;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001706
1707 offset = outarg->offset & ~PAGE_CACHE_MASK;
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001708 file_size = i_size_read(inode);
1709
1710 num = outarg->size;
1711 if (outarg->offset > file_size)
1712 num = 0;
1713 else if (outarg->offset + num > file_size)
1714 num = file_size - outarg->offset;
1715
1716 num_pages = (num + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1717 num_pages = min(num_pages, FUSE_MAX_PAGES_PER_REQ);
1718
1719 req = fuse_get_req(fc, num_pages);
1720 if (IS_ERR(req))
1721 return PTR_ERR(req);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001722
1723 req->in.h.opcode = FUSE_NOTIFY_REPLY;
1724 req->in.h.nodeid = outarg->nodeid;
1725 req->in.numargs = 2;
1726 req->in.argpages = 1;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001727 req->page_descs[0].offset = offset;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001728 req->end = fuse_retrieve_end;
1729
1730 index = outarg->offset >> PAGE_CACHE_SHIFT;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001731
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001732 while (num && req->num_pages < num_pages) {
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001733 struct page *page;
1734 unsigned int this_num;
1735
1736 page = find_get_page(mapping, index);
1737 if (!page)
1738 break;
1739
1740 this_num = min_t(unsigned, num, PAGE_CACHE_SIZE - offset);
1741 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001742 req->page_descs[req->num_pages].length = this_num;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001743 req->num_pages++;
1744
Miklos Szeredic9e67d42012-09-04 18:45:54 +02001745 offset = 0;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001746 num -= this_num;
1747 total_len += this_num;
Miklos Szeredi48706d02011-12-13 10:36:59 +01001748 index++;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001749 }
1750 req->misc.retrieve_in.offset = outarg->offset;
1751 req->misc.retrieve_in.size = total_len;
1752 req->in.args[0].size = sizeof(req->misc.retrieve_in);
1753 req->in.args[0].value = &req->misc.retrieve_in;
1754 req->in.args[1].size = total_len;
1755
1756 err = fuse_request_send_notify_reply(fc, req, outarg->notify_unique);
1757 if (err)
1758 fuse_retrieve_end(fc, req);
1759
1760 return err;
1761}
1762
1763static int fuse_notify_retrieve(struct fuse_conn *fc, unsigned int size,
1764 struct fuse_copy_state *cs)
1765{
1766 struct fuse_notify_retrieve_out outarg;
1767 struct inode *inode;
1768 int err;
1769
1770 err = -EINVAL;
1771 if (size != sizeof(outarg))
1772 goto copy_finish;
1773
1774 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1775 if (err)
1776 goto copy_finish;
1777
1778 fuse_copy_finish(cs);
1779
1780 down_read(&fc->killsb);
1781 err = -ENOENT;
1782 if (fc->sb) {
1783 u64 nodeid = outarg.nodeid;
1784
1785 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1786 if (inode) {
1787 err = fuse_retrieve(fc, inode, &outarg);
1788 iput(inode);
1789 }
1790 }
1791 up_read(&fc->killsb);
1792
1793 return err;
1794
1795copy_finish:
1796 fuse_copy_finish(cs);
1797 return err;
1798}
1799
Tejun Heo85993962008-11-26 12:03:55 +01001800static int fuse_notify(struct fuse_conn *fc, enum fuse_notify_code code,
1801 unsigned int size, struct fuse_copy_state *cs)
1802{
Miklos Szeredi0d278362015-02-26 11:45:47 +01001803 /* Don't try to move pages (yet) */
1804 cs->move_pages = 0;
1805
Tejun Heo85993962008-11-26 12:03:55 +01001806 switch (code) {
Tejun Heo95668a62008-11-26 12:03:55 +01001807 case FUSE_NOTIFY_POLL:
1808 return fuse_notify_poll(fc, size, cs);
1809
John Muir3b463ae2009-05-31 11:13:57 -04001810 case FUSE_NOTIFY_INVAL_INODE:
1811 return fuse_notify_inval_inode(fc, size, cs);
1812
1813 case FUSE_NOTIFY_INVAL_ENTRY:
1814 return fuse_notify_inval_entry(fc, size, cs);
1815
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001816 case FUSE_NOTIFY_STORE:
1817 return fuse_notify_store(fc, size, cs);
1818
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001819 case FUSE_NOTIFY_RETRIEVE:
1820 return fuse_notify_retrieve(fc, size, cs);
1821
John Muir451d0f52011-12-06 21:50:06 +01001822 case FUSE_NOTIFY_DELETE:
1823 return fuse_notify_delete(fc, size, cs);
1824
Tejun Heo85993962008-11-26 12:03:55 +01001825 default:
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001826 fuse_copy_finish(cs);
Tejun Heo85993962008-11-26 12:03:55 +01001827 return -EINVAL;
1828 }
1829}
1830
Miklos Szeredi334f4852005-09-09 13:10:27 -07001831/* Look up request on processing list by unique ID */
1832static struct fuse_req *request_find(struct fuse_conn *fc, u64 unique)
1833{
Dong Fang05726ac2013-07-30 22:50:01 -04001834 struct fuse_req *req;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001835
Dong Fang05726ac2013-07-30 22:50:01 -04001836 list_for_each_entry(req, &fc->processing, list) {
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001837 if (req->in.h.unique == unique || req->intr_unique == unique)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001838 return req;
1839 }
1840 return NULL;
1841}
1842
1843static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
1844 unsigned nbytes)
1845{
1846 unsigned reqsize = sizeof(struct fuse_out_header);
1847
1848 if (out->h.error)
1849 return nbytes != reqsize ? -EINVAL : 0;
1850
1851 reqsize += len_args(out->numargs, out->args);
1852
1853 if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
1854 return -EINVAL;
1855 else if (reqsize > nbytes) {
1856 struct fuse_arg *lastarg = &out->args[out->numargs-1];
1857 unsigned diffsize = reqsize - nbytes;
1858 if (diffsize > lastarg->size)
1859 return -EINVAL;
1860 lastarg->size -= diffsize;
1861 }
1862 return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
1863 out->page_zeroing);
1864}
1865
1866/*
1867 * Write a single reply to a request. First the header is copied from
1868 * the write buffer. The request is then searched on the processing
1869 * list by the unique ID found in the header. If found, then remove
1870 * it from the list and copy the rest of the buffer to the request.
1871 * The request is finished by calling request_end()
1872 */
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001873static ssize_t fuse_dev_do_write(struct fuse_conn *fc,
1874 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001875{
1876 int err;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001877 struct fuse_req *req;
1878 struct fuse_out_header oh;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001879
Miklos Szeredi334f4852005-09-09 13:10:27 -07001880 if (nbytes < sizeof(struct fuse_out_header))
1881 return -EINVAL;
1882
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001883 err = fuse_copy_one(cs, &oh, sizeof(oh));
Miklos Szeredi334f4852005-09-09 13:10:27 -07001884 if (err)
1885 goto err_finish;
Tejun Heo85993962008-11-26 12:03:55 +01001886
Miklos Szeredi334f4852005-09-09 13:10:27 -07001887 err = -EINVAL;
Tejun Heo85993962008-11-26 12:03:55 +01001888 if (oh.len != nbytes)
1889 goto err_finish;
1890
1891 /*
1892 * Zero oh.unique indicates unsolicited notification message
1893 * and error contains notification code.
1894 */
1895 if (!oh.unique) {
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001896 err = fuse_notify(fc, oh.error, nbytes - sizeof(oh), cs);
Tejun Heo85993962008-11-26 12:03:55 +01001897 return err ? err : nbytes;
1898 }
1899
1900 err = -EINVAL;
1901 if (oh.error <= -1000 || oh.error > 0)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001902 goto err_finish;
1903
Miklos Szeredid7133112006-04-10 22:54:55 -07001904 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001905 err = -ENOENT;
1906 if (!fc->connected)
1907 goto err_unlock;
1908
Miklos Szeredi334f4852005-09-09 13:10:27 -07001909 req = request_find(fc, oh.unique);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001910 if (!req)
1911 goto err_unlock;
1912
Miklos Szeredif9a28422006-06-25 05:48:53 -07001913 if (req->aborted) {
Miklos Szeredid7133112006-04-10 22:54:55 -07001914 spin_unlock(&fc->lock);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001915 fuse_copy_finish(cs);
Miklos Szeredid7133112006-04-10 22:54:55 -07001916 spin_lock(&fc->lock);
Miklos Szeredi222f1d62006-01-16 22:14:25 -08001917 request_end(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001918 return -ENOENT;
1919 }
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001920 /* Is it an interrupt reply? */
1921 if (req->intr_unique == oh.unique) {
1922 err = -EINVAL;
1923 if (nbytes != sizeof(struct fuse_out_header))
1924 goto err_unlock;
1925
1926 if (oh.error == -ENOSYS)
1927 fc->no_interrupt = 1;
1928 else if (oh.error == -EAGAIN)
1929 queue_interrupt(fc, req);
1930
1931 spin_unlock(&fc->lock);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001932 fuse_copy_finish(cs);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001933 return nbytes;
1934 }
1935
1936 req->state = FUSE_REQ_WRITING;
Miklos Szeredid77a1d52006-01-16 22:14:31 -08001937 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001938 req->out.h = oh;
1939 req->locked = 1;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001940 cs->req = req;
Miklos Szeredice534fb2010-05-25 15:06:07 +02001941 if (!req->out.page_replace)
1942 cs->move_pages = 0;
Miklos Szeredid7133112006-04-10 22:54:55 -07001943 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001944
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001945 err = copy_out_args(cs, &req->out, nbytes);
1946 fuse_copy_finish(cs);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001947
Miklos Szeredid7133112006-04-10 22:54:55 -07001948 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001949 req->locked = 0;
1950 if (!err) {
Miklos Szeredif9a28422006-06-25 05:48:53 -07001951 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001952 err = -ENOENT;
Miklos Szeredif9a28422006-06-25 05:48:53 -07001953 } else if (!req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001954 req->out.h.error = -EIO;
1955 request_end(fc, req);
1956
1957 return err ? err : nbytes;
1958
1959 err_unlock:
Miklos Szeredid7133112006-04-10 22:54:55 -07001960 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001961 err_finish:
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001962 fuse_copy_finish(cs);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001963 return err;
1964}
1965
Al Virofbdbacc2015-04-03 21:53:39 -04001966static ssize_t fuse_dev_write(struct kiocb *iocb, struct iov_iter *from)
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001967{
1968 struct fuse_copy_state cs;
1969 struct fuse_conn *fc = fuse_get_conn(iocb->ki_filp);
1970 if (!fc)
1971 return -EPERM;
1972
Al Virofbdbacc2015-04-03 21:53:39 -04001973 if (!iter_is_iovec(from))
1974 return -EINVAL;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001975
Al Viro6c09e942015-04-03 22:06:08 -04001976 fuse_copy_init(&cs, fc, 0, from);
Al Virofbdbacc2015-04-03 21:53:39 -04001977
1978 return fuse_dev_do_write(fc, &cs, iov_iter_count(from));
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001979}
1980
1981static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe,
1982 struct file *out, loff_t *ppos,
1983 size_t len, unsigned int flags)
1984{
1985 unsigned nbuf;
1986 unsigned idx;
1987 struct pipe_buffer *bufs;
1988 struct fuse_copy_state cs;
1989 struct fuse_conn *fc;
1990 size_t rem;
1991 ssize_t ret;
1992
1993 fc = fuse_get_conn(out);
1994 if (!fc)
1995 return -EPERM;
1996
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001997 bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), GFP_KERNEL);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001998 if (!bufs)
1999 return -ENOMEM;
2000
2001 pipe_lock(pipe);
2002 nbuf = 0;
2003 rem = 0;
2004 for (idx = 0; idx < pipe->nrbufs && rem < len; idx++)
2005 rem += pipe->bufs[(pipe->curbuf + idx) & (pipe->buffers - 1)].len;
2006
2007 ret = -EINVAL;
2008 if (rem < len) {
2009 pipe_unlock(pipe);
2010 goto out;
2011 }
2012
2013 rem = len;
2014 while (rem) {
2015 struct pipe_buffer *ibuf;
2016 struct pipe_buffer *obuf;
2017
2018 BUG_ON(nbuf >= pipe->buffers);
2019 BUG_ON(!pipe->nrbufs);
2020 ibuf = &pipe->bufs[pipe->curbuf];
2021 obuf = &bufs[nbuf];
2022
2023 if (rem >= ibuf->len) {
2024 *obuf = *ibuf;
2025 ibuf->ops = NULL;
2026 pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
2027 pipe->nrbufs--;
2028 } else {
2029 ibuf->ops->get(pipe, ibuf);
2030 *obuf = *ibuf;
2031 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
2032 obuf->len = rem;
2033 ibuf->offset += obuf->len;
2034 ibuf->len -= obuf->len;
2035 }
2036 nbuf++;
2037 rem -= obuf->len;
2038 }
2039 pipe_unlock(pipe);
2040
Al Viro6c09e942015-04-03 22:06:08 -04002041 fuse_copy_init(&cs, fc, 0, NULL);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002042 cs.pipebufs = bufs;
Al Viro6c09e942015-04-03 22:06:08 -04002043 cs.nr_segs = nbuf;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002044 cs.pipe = pipe;
2045
Miklos Szeredice534fb2010-05-25 15:06:07 +02002046 if (flags & SPLICE_F_MOVE)
2047 cs.move_pages = 1;
2048
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002049 ret = fuse_dev_do_write(fc, &cs, len);
2050
2051 for (idx = 0; idx < nbuf; idx++) {
2052 struct pipe_buffer *buf = &bufs[idx];
2053 buf->ops->release(pipe, buf);
2054 }
2055out:
2056 kfree(bufs);
2057 return ret;
2058}
2059
Miklos Szeredi334f4852005-09-09 13:10:27 -07002060static unsigned fuse_dev_poll(struct file *file, poll_table *wait)
2061{
Miklos Szeredi334f4852005-09-09 13:10:27 -07002062 unsigned mask = POLLOUT | POLLWRNORM;
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07002063 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002064 if (!fc)
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07002065 return POLLERR;
Miklos Szeredi334f4852005-09-09 13:10:27 -07002066
2067 poll_wait(file, &fc->waitq, wait);
2068
Miklos Szeredid7133112006-04-10 22:54:55 -07002069 spin_lock(&fc->lock);
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07002070 if (!fc->connected)
2071 mask = POLLERR;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07002072 else if (request_pending(fc))
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07002073 mask |= POLLIN | POLLRDNORM;
Miklos Szeredid7133112006-04-10 22:54:55 -07002074 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002075
2076 return mask;
2077}
2078
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002079/*
2080 * Abort all requests on the given list (pending or processing)
2081 *
Miklos Szeredid7133112006-04-10 22:54:55 -07002082 * This function releases and reacquires fc->lock
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002083 */
Miklos Szeredi334f4852005-09-09 13:10:27 -07002084static void end_requests(struct fuse_conn *fc, struct list_head *head)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02002085__releases(fc->lock)
2086__acquires(fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -07002087{
2088 while (!list_empty(head)) {
2089 struct fuse_req *req;
2090 req = list_entry(head->next, struct fuse_req, list);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002091 req->out.h.error = -ECONNABORTED;
2092 request_end(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -07002093 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002094 }
2095}
2096
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002097/*
2098 * Abort requests under I/O
2099 *
Miklos Szeredif9a28422006-06-25 05:48:53 -07002100 * The requests are set to aborted and finished, and the request
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002101 * waiter is woken up. This will make request_wait_answer() wait
2102 * until the request is unlocked and then return.
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08002103 *
2104 * If the request is asynchronous, then the end function needs to be
2105 * called after waiting for the request to be unlocked (if it was
2106 * locked).
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002107 */
2108static void end_io_requests(struct fuse_conn *fc)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02002109__releases(fc->lock)
2110__acquires(fc->lock)
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002111{
2112 while (!list_empty(&fc->io)) {
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08002113 struct fuse_req *req =
2114 list_entry(fc->io.next, struct fuse_req, list);
2115 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
2116
Miklos Szeredif9a28422006-06-25 05:48:53 -07002117 req->aborted = 1;
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002118 req->out.h.error = -ECONNABORTED;
2119 req->state = FUSE_REQ_FINISHED;
2120 list_del_init(&req->list);
2121 wake_up(&req->waitq);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08002122 if (end) {
2123 req->end = NULL;
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08002124 __fuse_get_request(req);
Miklos Szeredid7133112006-04-10 22:54:55 -07002125 spin_unlock(&fc->lock);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08002126 wait_event(req->waitq, !req->locked);
2127 end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +01002128 fuse_put_request(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -07002129 spin_lock(&fc->lock);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08002130 }
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002131 }
2132}
2133
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002134static void end_queued_requests(struct fuse_conn *fc)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02002135__releases(fc->lock)
2136__acquires(fc->lock)
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002137{
2138 fc->max_background = UINT_MAX;
2139 flush_bg_queue(fc);
2140 end_requests(fc, &fc->pending);
2141 end_requests(fc, &fc->processing);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01002142 while (forget_pending(fc))
Miklos Szeredi02c048b2010-12-07 20:16:56 +01002143 kfree(dequeue_forget(fc, 1, NULL));
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002144}
2145
Bryan Green357ccf22011-03-01 16:43:52 -08002146static void end_polls(struct fuse_conn *fc)
2147{
2148 struct rb_node *p;
2149
2150 p = rb_first(&fc->polled_files);
2151
2152 while (p) {
2153 struct fuse_file *ff;
2154 ff = rb_entry(p, struct fuse_file, polled_node);
2155 wake_up_interruptible_all(&ff->poll_wait);
2156
2157 p = rb_next(p);
2158 }
2159}
2160
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002161/*
2162 * Abort all requests.
2163 *
2164 * Emergency exit in case of a malicious or accidental deadlock, or
2165 * just a hung filesystem.
2166 *
2167 * The same effect is usually achievable through killing the
2168 * filesystem daemon and all users of the filesystem. The exception
2169 * is the combination of an asynchronous request and the tricky
2170 * deadlock (see Documentation/filesystems/fuse.txt).
2171 *
2172 * During the aborting, progression of requests from the pending and
2173 * processing lists onto the io list, and progression of new requests
2174 * onto the pending list is prevented by req->connected being false.
2175 *
2176 * Progression of requests under I/O to the processing list is
Miklos Szeredif9a28422006-06-25 05:48:53 -07002177 * prevented by the req->aborted flag being true for these requests.
2178 * For this reason requests on the io list must be aborted first.
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002179 */
2180void fuse_abort_conn(struct fuse_conn *fc)
2181{
Miklos Szeredid7133112006-04-10 22:54:55 -07002182 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002183 if (fc->connected) {
2184 fc->connected = 0;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -07002185 fc->blocked = 0;
Miklos Szeredi9759bd512015-01-06 10:45:35 +01002186 fuse_set_initialized(fc);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002187 end_io_requests(fc);
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002188 end_queued_requests(fc);
Bryan Green357ccf22011-03-01 16:43:52 -08002189 end_polls(fc);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002190 wake_up_all(&fc->waitq);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -07002191 wake_up_all(&fc->blocked_waitq);
Jeff Dike385a17b2006-04-10 22:54:52 -07002192 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002193 }
Miklos Szeredid7133112006-04-10 22:54:55 -07002194 spin_unlock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002195}
Tejun Heo08cbf542009-04-14 10:54:53 +09002196EXPORT_SYMBOL_GPL(fuse_abort_conn);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002197
Tejun Heo08cbf542009-04-14 10:54:53 +09002198int fuse_dev_release(struct inode *inode, struct file *file)
Miklos Szeredi334f4852005-09-09 13:10:27 -07002199{
Miklos Szeredi0720b312006-04-10 22:54:55 -07002200 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002201 if (fc) {
Miklos Szerediccd0a0b2015-07-01 16:25:57 +02002202 WARN_ON(!list_empty(&fc->io));
2203 WARN_ON(fc->fasync != NULL);
2204 fuse_abort_conn(fc);
Miklos Szeredibafa9652006-06-25 05:48:51 -07002205 fuse_conn_put(fc);
Jeff Dike385a17b2006-04-10 22:54:52 -07002206 }
Miklos Szeredif543f252006-01-16 22:14:35 -08002207
Miklos Szeredi334f4852005-09-09 13:10:27 -07002208 return 0;
2209}
Tejun Heo08cbf542009-04-14 10:54:53 +09002210EXPORT_SYMBOL_GPL(fuse_dev_release);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002211
Jeff Dike385a17b2006-04-10 22:54:52 -07002212static int fuse_dev_fasync(int fd, struct file *file, int on)
2213{
2214 struct fuse_conn *fc = fuse_get_conn(file);
2215 if (!fc)
Miklos Szeredia87046d2006-04-10 22:54:56 -07002216 return -EPERM;
Jeff Dike385a17b2006-04-10 22:54:52 -07002217
2218 /* No locking - fasync_helper does its own locking */
2219 return fasync_helper(fd, file, on, &fc->fasync);
2220}
2221
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002222const struct file_operations fuse_dev_operations = {
Miklos Szeredi334f4852005-09-09 13:10:27 -07002223 .owner = THIS_MODULE,
Tom Van Braeckel94e4fe22015-01-12 05:22:16 +01002224 .open = fuse_dev_open,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002225 .llseek = no_llseek,
Al Virofbdbacc2015-04-03 21:53:39 -04002226 .read_iter = fuse_dev_read,
Miklos Szeredic3021622010-05-25 15:06:07 +02002227 .splice_read = fuse_dev_splice_read,
Al Virofbdbacc2015-04-03 21:53:39 -04002228 .write_iter = fuse_dev_write,
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002229 .splice_write = fuse_dev_splice_write,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002230 .poll = fuse_dev_poll,
2231 .release = fuse_dev_release,
Jeff Dike385a17b2006-04-10 22:54:52 -07002232 .fasync = fuse_dev_fasync,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002233};
Tejun Heo08cbf542009-04-14 10:54:53 +09002234EXPORT_SYMBOL_GPL(fuse_dev_operations);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002235
2236static struct miscdevice fuse_miscdevice = {
2237 .minor = FUSE_MINOR,
2238 .name = "fuse",
2239 .fops = &fuse_dev_operations,
2240};
2241
2242int __init fuse_dev_init(void)
2243{
2244 int err = -ENOMEM;
2245 fuse_req_cachep = kmem_cache_create("fuse_request",
2246 sizeof(struct fuse_req),
Paul Mundt20c2df82007-07-20 10:11:58 +09002247 0, 0, NULL);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002248 if (!fuse_req_cachep)
2249 goto out;
2250
2251 err = misc_register(&fuse_miscdevice);
2252 if (err)
2253 goto out_cache_clean;
2254
2255 return 0;
2256
2257 out_cache_clean:
2258 kmem_cache_destroy(fuse_req_cachep);
2259 out:
2260 return err;
2261}
2262
2263void fuse_dev_cleanup(void)
2264{
2265 misc_deregister(&fuse_miscdevice);
2266 kmem_cache_destroy(fuse_req_cachep);
2267}