blob: 155610fef4a7e28c49781d4b6281f23a3376bc89 [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
Tejun Heob93f8582008-11-26 12:03:55 +0100593static void fuse_request_send_nowait_locked(struct fuse_conn *fc,
594 struct fuse_req *req)
Miklos Szeredid12def12008-02-06 01:38:39 -0800595{
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400596 BUG_ON(!req->background);
Miklos Szeredi5437f242015-07-01 16:25:56 +0200597 if (!req->waiting) {
598 req->waiting = 1;
599 atomic_inc(&fc->num_waiting);
600 }
Miklos Szeredid12def12008-02-06 01:38:39 -0800601 fc->num_background++;
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700602 if (fc->num_background == fc->max_background)
Miklos Szeredid12def12008-02-06 01:38:39 -0800603 fc->blocked = 1;
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700604 if (fc->num_background == fc->congestion_threshold &&
Tejun Heoa325f9b2009-04-14 10:54:52 +0900605 fc->bdi_initialized) {
Jens Axboe8aa7e842009-07-09 14:52:32 +0200606 set_bdi_congested(&fc->bdi, BLK_RW_SYNC);
607 set_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
Miklos Szeredid12def12008-02-06 01:38:39 -0800608 }
609 list_add_tail(&req->list, &fc->bg_queue);
610 flush_bg_queue(fc);
611}
612
Tejun Heob93f8582008-11-26 12:03:55 +0100613static void fuse_request_send_nowait(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700614{
Miklos Szeredi42dc6212015-07-01 16:25:56 +0200615 BUG_ON(!req->end);
Miklos Szeredid7133112006-04-10 22:54:55 -0700616 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700617 if (fc->connected) {
Tejun Heob93f8582008-11-26 12:03:55 +0100618 fuse_request_send_nowait_locked(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700619 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700620 } else {
Miklos Szeredi42dc6212015-07-01 16:25:56 +0200621 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700622 req->out.h.error = -ENOTCONN;
Miklos Szeredi42dc6212015-07-01 16:25:56 +0200623 req->end(fc, req);
624 fuse_put_request(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700625 }
626}
627
Tejun Heob93f8582008-11-26 12:03:55 +0100628void fuse_request_send_background(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700629{
630 req->isreply = 1;
Tejun Heob93f8582008-11-26 12:03:55 +0100631 fuse_request_send_nowait(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700632}
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
Miklos Szeredi334f4852005-09-09 13:10:27 -0700652/*
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700653 * Called under fc->lock
654 *
655 * fc->connected must have been checked previously
656 */
Tejun Heob93f8582008-11-26 12:03:55 +0100657void fuse_request_send_background_locked(struct fuse_conn *fc,
658 struct fuse_req *req)
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700659{
660 req->isreply = 1;
Tejun Heob93f8582008-11-26 12:03:55 +0100661 fuse_request_send_nowait_locked(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700662}
663
Anand V. Avati0b05b182012-08-19 08:53:23 -0400664void fuse_force_forget(struct file *file, u64 nodeid)
665{
Al Viro6131ffa2013-02-27 16:59:05 -0500666 struct inode *inode = file_inode(file);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400667 struct fuse_conn *fc = get_fuse_conn(inode);
668 struct fuse_req *req;
669 struct fuse_forget_in inarg;
670
671 memset(&inarg, 0, sizeof(inarg));
672 inarg.nlookup = 1;
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400673 req = fuse_get_req_nofail_nopages(fc, file);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400674 req->in.h.opcode = FUSE_FORGET;
675 req->in.h.nodeid = nodeid;
676 req->in.numargs = 1;
677 req->in.args[0].size = sizeof(inarg);
678 req->in.args[0].value = &inarg;
679 req->isreply = 0;
Eric Wong6a4e9222013-02-04 13:04:44 +0000680 __fuse_request_send(fc, req);
681 /* ignore errors */
682 fuse_put_request(fc, req);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400683}
684
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700685/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700686 * Lock the request. Up to the next unlock_request() there mustn't be
687 * anything that could cause a page-fault. If the request was already
Miklos Szeredif9a28422006-06-25 05:48:53 -0700688 * aborted bail out.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700689 */
Miklos Szeredid7133112006-04-10 22:54:55 -0700690static int lock_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700691{
692 int err = 0;
693 if (req) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700694 spin_lock(&fc->lock);
Miklos Szeredif9a28422006-06-25 05:48:53 -0700695 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700696 err = -ENOENT;
697 else
698 req->locked = 1;
Miklos Szeredid7133112006-04-10 22:54:55 -0700699 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700700 }
701 return err;
702}
703
704/*
Miklos Szeredif9a28422006-06-25 05:48:53 -0700705 * Unlock request. If it was aborted during being locked, the
Miklos Szeredi334f4852005-09-09 13:10:27 -0700706 * requester thread is currently waiting for it to be unlocked, so
707 * wake it up.
708 */
Miklos Szeredid7133112006-04-10 22:54:55 -0700709static void unlock_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700710{
711 if (req) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700712 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700713 req->locked = 0;
Miklos Szeredif9a28422006-06-25 05:48:53 -0700714 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700715 wake_up(&req->waitq);
Miklos Szeredid7133112006-04-10 22:54:55 -0700716 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700717 }
718}
719
720struct fuse_copy_state {
Miklos Szeredid7133112006-04-10 22:54:55 -0700721 struct fuse_conn *fc;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700722 int write;
723 struct fuse_req *req;
Al Viro6c09e942015-04-03 22:06:08 -0400724 struct iov_iter *iter;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200725 struct pipe_buffer *pipebufs;
726 struct pipe_buffer *currbuf;
727 struct pipe_inode_info *pipe;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700728 unsigned long nr_segs;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700729 struct page *pg;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700730 unsigned len;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200731 unsigned offset;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200732 unsigned move_pages:1;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700733};
734
Al Viro6c09e942015-04-03 22:06:08 -0400735static void fuse_copy_init(struct fuse_copy_state *cs,
736 struct fuse_conn *fc,
Miklos Szeredic3021622010-05-25 15:06:07 +0200737 int write,
Al Viro6c09e942015-04-03 22:06:08 -0400738 struct iov_iter *iter)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700739{
740 memset(cs, 0, sizeof(*cs));
Miklos Szeredid7133112006-04-10 22:54:55 -0700741 cs->fc = fc;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700742 cs->write = write;
Al Viro6c09e942015-04-03 22:06:08 -0400743 cs->iter = iter;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700744}
745
746/* Unmap and put previous page of userspace buffer */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800747static void fuse_copy_finish(struct fuse_copy_state *cs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700748{
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200749 if (cs->currbuf) {
750 struct pipe_buffer *buf = cs->currbuf;
751
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200752 if (cs->write)
Miklos Szeredic3021622010-05-25 15:06:07 +0200753 buf->len = PAGE_SIZE - cs->len;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200754 cs->currbuf = NULL;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200755 } else if (cs->pg) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700756 if (cs->write) {
757 flush_dcache_page(cs->pg);
758 set_page_dirty_lock(cs->pg);
759 }
760 put_page(cs->pg);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700761 }
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200762 cs->pg = NULL;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700763}
764
765/*
766 * Get another pagefull of userspace buffer, and map it to kernel
767 * address space, and lock request
768 */
769static int fuse_copy_fill(struct fuse_copy_state *cs)
770{
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200771 struct page *page;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700772 int err;
773
Miklos Szeredid7133112006-04-10 22:54:55 -0700774 unlock_request(cs->fc, cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700775 fuse_copy_finish(cs);
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200776 if (cs->pipebufs) {
777 struct pipe_buffer *buf = cs->pipebufs;
778
Miklos Szeredic3021622010-05-25 15:06:07 +0200779 if (!cs->write) {
780 err = buf->ops->confirm(cs->pipe, buf);
781 if (err)
782 return err;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200783
Miklos Szeredic3021622010-05-25 15:06:07 +0200784 BUG_ON(!cs->nr_segs);
785 cs->currbuf = buf;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200786 cs->pg = buf->page;
787 cs->offset = buf->offset;
Miklos Szeredic3021622010-05-25 15:06:07 +0200788 cs->len = buf->len;
Miklos Szeredic3021622010-05-25 15:06:07 +0200789 cs->pipebufs++;
790 cs->nr_segs--;
791 } else {
Miklos Szeredic3021622010-05-25 15:06:07 +0200792 if (cs->nr_segs == cs->pipe->buffers)
793 return -EIO;
794
795 page = alloc_page(GFP_HIGHUSER);
796 if (!page)
797 return -ENOMEM;
798
799 buf->page = page;
800 buf->offset = 0;
801 buf->len = 0;
802
803 cs->currbuf = buf;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200804 cs->pg = page;
805 cs->offset = 0;
Miklos Szeredic3021622010-05-25 15:06:07 +0200806 cs->len = PAGE_SIZE;
807 cs->pipebufs++;
808 cs->nr_segs++;
809 }
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200810 } else {
Al Viro6c09e942015-04-03 22:06:08 -0400811 size_t off;
812 err = iov_iter_get_pages(cs->iter, &page, PAGE_SIZE, 1, &off);
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200813 if (err < 0)
814 return err;
Al Viro6c09e942015-04-03 22:06:08 -0400815 BUG_ON(!err);
816 cs->len = err;
817 cs->offset = off;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200818 cs->pg = page;
Al Viro6c09e942015-04-03 22:06:08 -0400819 cs->offset = off;
820 iov_iter_advance(cs->iter, err);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700821 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700822
Miklos Szeredid7133112006-04-10 22:54:55 -0700823 return lock_request(cs->fc, cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700824}
825
826/* Do as much copy to/from userspace buffer as we can */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800827static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700828{
829 unsigned ncpy = min(*size, cs->len);
830 if (val) {
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200831 void *pgaddr = kmap_atomic(cs->pg);
832 void *buf = pgaddr + cs->offset;
833
Miklos Szeredi334f4852005-09-09 13:10:27 -0700834 if (cs->write)
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200835 memcpy(buf, *val, ncpy);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700836 else
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200837 memcpy(*val, buf, ncpy);
838
839 kunmap_atomic(pgaddr);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700840 *val += ncpy;
841 }
842 *size -= ncpy;
843 cs->len -= ncpy;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200844 cs->offset += ncpy;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700845 return ncpy;
846}
847
Miklos Szeredice534fb2010-05-25 15:06:07 +0200848static int fuse_check_page(struct page *page)
849{
850 if (page_mapcount(page) ||
851 page->mapping != NULL ||
852 page_count(page) != 1 ||
853 (page->flags & PAGE_FLAGS_CHECK_AT_PREP &
854 ~(1 << PG_locked |
855 1 << PG_referenced |
856 1 << PG_uptodate |
857 1 << PG_lru |
858 1 << PG_active |
859 1 << PG_reclaim))) {
860 printk(KERN_WARNING "fuse: trying to steal weird page\n");
861 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);
862 return 1;
863 }
864 return 0;
865}
866
867static int fuse_try_move_page(struct fuse_copy_state *cs, struct page **pagep)
868{
869 int err;
870 struct page *oldpage = *pagep;
871 struct page *newpage;
872 struct pipe_buffer *buf = cs->pipebufs;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200873
874 unlock_request(cs->fc, cs->req);
875 fuse_copy_finish(cs);
876
877 err = buf->ops->confirm(cs->pipe, buf);
878 if (err)
879 return err;
880
881 BUG_ON(!cs->nr_segs);
882 cs->currbuf = buf;
883 cs->len = buf->len;
884 cs->pipebufs++;
885 cs->nr_segs--;
886
887 if (cs->len != PAGE_SIZE)
888 goto out_fallback;
889
890 if (buf->ops->steal(cs->pipe, buf) != 0)
891 goto out_fallback;
892
893 newpage = buf->page;
894
Miklos Szerediaa991b32015-02-26 11:45:47 +0100895 if (!PageUptodate(newpage))
896 SetPageUptodate(newpage);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200897
898 ClearPageMappedToDisk(newpage);
899
900 if (fuse_check_page(newpage) != 0)
901 goto out_fallback_unlock;
902
Miklos Szeredice534fb2010-05-25 15:06:07 +0200903 /*
904 * This is a new and locked page, it shouldn't be mapped or
905 * have any special flags on it
906 */
907 if (WARN_ON(page_mapped(oldpage)))
908 goto out_fallback_unlock;
909 if (WARN_ON(page_has_private(oldpage)))
910 goto out_fallback_unlock;
911 if (WARN_ON(PageDirty(oldpage) || PageWriteback(oldpage)))
912 goto out_fallback_unlock;
913 if (WARN_ON(PageMlocked(oldpage)))
914 goto out_fallback_unlock;
915
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700916 err = replace_page_cache_page(oldpage, newpage, GFP_KERNEL);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200917 if (err) {
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700918 unlock_page(newpage);
919 return err;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200920 }
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700921
Miklos Szeredice534fb2010-05-25 15:06:07 +0200922 page_cache_get(newpage);
923
924 if (!(buf->flags & PIPE_BUF_FLAG_LRU))
925 lru_cache_add_file(newpage);
926
927 err = 0;
928 spin_lock(&cs->fc->lock);
929 if (cs->req->aborted)
930 err = -ENOENT;
931 else
932 *pagep = newpage;
933 spin_unlock(&cs->fc->lock);
934
935 if (err) {
936 unlock_page(newpage);
937 page_cache_release(newpage);
938 return err;
939 }
940
941 unlock_page(oldpage);
942 page_cache_release(oldpage);
943 cs->len = 0;
944
945 return 0;
946
947out_fallback_unlock:
948 unlock_page(newpage);
949out_fallback:
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200950 cs->pg = buf->page;
951 cs->offset = buf->offset;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200952
953 err = lock_request(cs->fc, cs->req);
954 if (err)
955 return err;
956
957 return 1;
958}
959
Miklos Szeredic3021622010-05-25 15:06:07 +0200960static int fuse_ref_page(struct fuse_copy_state *cs, struct page *page,
961 unsigned offset, unsigned count)
962{
963 struct pipe_buffer *buf;
964
965 if (cs->nr_segs == cs->pipe->buffers)
966 return -EIO;
967
968 unlock_request(cs->fc, cs->req);
969 fuse_copy_finish(cs);
970
971 buf = cs->pipebufs;
972 page_cache_get(page);
973 buf->page = page;
974 buf->offset = offset;
975 buf->len = count;
976
977 cs->pipebufs++;
978 cs->nr_segs++;
979 cs->len = 0;
980
981 return 0;
982}
983
Miklos Szeredi334f4852005-09-09 13:10:27 -0700984/*
985 * Copy a page in the request to/from the userspace buffer. Must be
986 * done atomically
987 */
Miklos Szeredice534fb2010-05-25 15:06:07 +0200988static int fuse_copy_page(struct fuse_copy_state *cs, struct page **pagep,
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800989 unsigned offset, unsigned count, int zeroing)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700990{
Miklos Szeredice534fb2010-05-25 15:06:07 +0200991 int err;
992 struct page *page = *pagep;
993
Miklos Szeredib6777c42010-10-26 14:22:27 -0700994 if (page && zeroing && count < PAGE_SIZE)
995 clear_highpage(page);
996
Miklos Szeredi334f4852005-09-09 13:10:27 -0700997 while (count) {
Miklos Szeredic3021622010-05-25 15:06:07 +0200998 if (cs->write && cs->pipebufs && page) {
999 return fuse_ref_page(cs, page, offset, count);
1000 } else if (!cs->len) {
Miklos Szeredice534fb2010-05-25 15:06:07 +02001001 if (cs->move_pages && page &&
1002 offset == 0 && count == PAGE_SIZE) {
1003 err = fuse_try_move_page(cs, pagep);
1004 if (err <= 0)
1005 return err;
1006 } else {
1007 err = fuse_copy_fill(cs);
1008 if (err)
1009 return err;
1010 }
Miklos Szeredi1729a162008-11-26 12:03:54 +01001011 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001012 if (page) {
Cong Wang2408f6e2011-11-25 23:14:30 +08001013 void *mapaddr = kmap_atomic(page);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001014 void *buf = mapaddr + offset;
1015 offset += fuse_copy_do(cs, &buf, &count);
Cong Wang2408f6e2011-11-25 23:14:30 +08001016 kunmap_atomic(mapaddr);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001017 } else
1018 offset += fuse_copy_do(cs, NULL, &count);
1019 }
1020 if (page && !cs->write)
1021 flush_dcache_page(page);
1022 return 0;
1023}
1024
1025/* Copy pages in the request to/from userspace buffer */
1026static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
1027 int zeroing)
1028{
1029 unsigned i;
1030 struct fuse_req *req = cs->req;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001031
1032 for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
Miklos Szeredice534fb2010-05-25 15:06:07 +02001033 int err;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001034 unsigned offset = req->page_descs[i].offset;
1035 unsigned count = min(nbytes, req->page_descs[i].length);
Miklos Szeredice534fb2010-05-25 15:06:07 +02001036
1037 err = fuse_copy_page(cs, &req->pages[i], offset, count,
1038 zeroing);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001039 if (err)
1040 return err;
1041
1042 nbytes -= count;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001043 }
1044 return 0;
1045}
1046
1047/* Copy a single argument in the request to/from userspace buffer */
1048static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
1049{
1050 while (size) {
Miklos Szeredi1729a162008-11-26 12:03:54 +01001051 if (!cs->len) {
1052 int err = fuse_copy_fill(cs);
1053 if (err)
1054 return err;
1055 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001056 fuse_copy_do(cs, &val, &size);
1057 }
1058 return 0;
1059}
1060
1061/* Copy request arguments to/from userspace buffer */
1062static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
1063 unsigned argpages, struct fuse_arg *args,
1064 int zeroing)
1065{
1066 int err = 0;
1067 unsigned i;
1068
1069 for (i = 0; !err && i < numargs; i++) {
1070 struct fuse_arg *arg = &args[i];
1071 if (i == numargs - 1 && argpages)
1072 err = fuse_copy_pages(cs, arg->size, zeroing);
1073 else
1074 err = fuse_copy_one(cs, arg->value, arg->size);
1075 }
1076 return err;
1077}
1078
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001079static int forget_pending(struct fuse_conn *fc)
1080{
1081 return fc->forget_list_head.next != NULL;
1082}
1083
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001084static int request_pending(struct fuse_conn *fc)
1085{
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001086 return !list_empty(&fc->pending) || !list_empty(&fc->interrupts) ||
1087 forget_pending(fc);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001088}
1089
Miklos Szeredi334f4852005-09-09 13:10:27 -07001090/* Wait until a request is available on the pending list */
1091static void request_wait(struct fuse_conn *fc)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001092__releases(fc->lock)
1093__acquires(fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001094{
1095 DECLARE_WAITQUEUE(wait, current);
1096
1097 add_wait_queue_exclusive(&fc->waitq, &wait);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001098 while (fc->connected && !request_pending(fc)) {
Miklos Szeredi334f4852005-09-09 13:10:27 -07001099 set_current_state(TASK_INTERRUPTIBLE);
1100 if (signal_pending(current))
1101 break;
1102
Miklos Szeredid7133112006-04-10 22:54:55 -07001103 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001104 schedule();
Miklos Szeredid7133112006-04-10 22:54:55 -07001105 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001106 }
1107 set_current_state(TASK_RUNNING);
1108 remove_wait_queue(&fc->waitq, &wait);
1109}
1110
1111/*
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001112 * Transfer an interrupt request to userspace
1113 *
1114 * Unlike other requests this is assembled on demand, without a need
1115 * to allocate a separate fuse_req structure.
1116 *
1117 * Called with fc->lock held, releases it
1118 */
Miklos Szeredic3021622010-05-25 15:06:07 +02001119static int fuse_read_interrupt(struct fuse_conn *fc, struct fuse_copy_state *cs,
1120 size_t nbytes, struct fuse_req *req)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001121__releases(fc->lock)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001122{
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001123 struct fuse_in_header ih;
1124 struct fuse_interrupt_in arg;
1125 unsigned reqsize = sizeof(ih) + sizeof(arg);
1126 int err;
1127
1128 list_del_init(&req->intr_entry);
1129 req->intr_unique = fuse_get_unique(fc);
1130 memset(&ih, 0, sizeof(ih));
1131 memset(&arg, 0, sizeof(arg));
1132 ih.len = reqsize;
1133 ih.opcode = FUSE_INTERRUPT;
1134 ih.unique = req->intr_unique;
1135 arg.unique = req->in.h.unique;
1136
1137 spin_unlock(&fc->lock);
Miklos Szeredic3021622010-05-25 15:06:07 +02001138 if (nbytes < reqsize)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001139 return -EINVAL;
1140
Miklos Szeredic3021622010-05-25 15:06:07 +02001141 err = fuse_copy_one(cs, &ih, sizeof(ih));
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001142 if (!err)
Miklos Szeredic3021622010-05-25 15:06:07 +02001143 err = fuse_copy_one(cs, &arg, sizeof(arg));
1144 fuse_copy_finish(cs);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001145
1146 return err ? err : reqsize;
1147}
1148
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001149static struct fuse_forget_link *dequeue_forget(struct fuse_conn *fc,
1150 unsigned max,
1151 unsigned *countp)
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001152{
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001153 struct fuse_forget_link *head = fc->forget_list_head.next;
1154 struct fuse_forget_link **newhead = &head;
1155 unsigned count;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001156
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001157 for (count = 0; *newhead != NULL && count < max; count++)
1158 newhead = &(*newhead)->next;
1159
1160 fc->forget_list_head.next = *newhead;
1161 *newhead = NULL;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001162 if (fc->forget_list_head.next == NULL)
1163 fc->forget_list_tail = &fc->forget_list_head;
1164
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001165 if (countp != NULL)
1166 *countp = count;
1167
1168 return head;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001169}
1170
1171static int fuse_read_single_forget(struct fuse_conn *fc,
1172 struct fuse_copy_state *cs,
1173 size_t nbytes)
1174__releases(fc->lock)
1175{
1176 int err;
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001177 struct fuse_forget_link *forget = dequeue_forget(fc, 1, NULL);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001178 struct fuse_forget_in arg = {
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001179 .nlookup = forget->forget_one.nlookup,
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001180 };
1181 struct fuse_in_header ih = {
1182 .opcode = FUSE_FORGET,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001183 .nodeid = forget->forget_one.nodeid,
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001184 .unique = fuse_get_unique(fc),
1185 .len = sizeof(ih) + sizeof(arg),
1186 };
1187
1188 spin_unlock(&fc->lock);
1189 kfree(forget);
1190 if (nbytes < ih.len)
1191 return -EINVAL;
1192
1193 err = fuse_copy_one(cs, &ih, sizeof(ih));
1194 if (!err)
1195 err = fuse_copy_one(cs, &arg, sizeof(arg));
1196 fuse_copy_finish(cs);
1197
1198 if (err)
1199 return err;
1200
1201 return ih.len;
1202}
1203
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001204static int fuse_read_batch_forget(struct fuse_conn *fc,
1205 struct fuse_copy_state *cs, size_t nbytes)
1206__releases(fc->lock)
1207{
1208 int err;
1209 unsigned max_forgets;
1210 unsigned count;
1211 struct fuse_forget_link *head;
1212 struct fuse_batch_forget_in arg = { .count = 0 };
1213 struct fuse_in_header ih = {
1214 .opcode = FUSE_BATCH_FORGET,
1215 .unique = fuse_get_unique(fc),
1216 .len = sizeof(ih) + sizeof(arg),
1217 };
1218
1219 if (nbytes < ih.len) {
1220 spin_unlock(&fc->lock);
1221 return -EINVAL;
1222 }
1223
1224 max_forgets = (nbytes - ih.len) / sizeof(struct fuse_forget_one);
1225 head = dequeue_forget(fc, max_forgets, &count);
1226 spin_unlock(&fc->lock);
1227
1228 arg.count = count;
1229 ih.len += count * sizeof(struct fuse_forget_one);
1230 err = fuse_copy_one(cs, &ih, sizeof(ih));
1231 if (!err)
1232 err = fuse_copy_one(cs, &arg, sizeof(arg));
1233
1234 while (head) {
1235 struct fuse_forget_link *forget = head;
1236
1237 if (!err) {
1238 err = fuse_copy_one(cs, &forget->forget_one,
1239 sizeof(forget->forget_one));
1240 }
1241 head = forget->next;
1242 kfree(forget);
1243 }
1244
1245 fuse_copy_finish(cs);
1246
1247 if (err)
1248 return err;
1249
1250 return ih.len;
1251}
1252
1253static int fuse_read_forget(struct fuse_conn *fc, struct fuse_copy_state *cs,
1254 size_t nbytes)
1255__releases(fc->lock)
1256{
1257 if (fc->minor < 16 || fc->forget_list_head.next->next == NULL)
1258 return fuse_read_single_forget(fc, cs, nbytes);
1259 else
1260 return fuse_read_batch_forget(fc, cs, nbytes);
1261}
1262
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001263/*
Miklos Szeredi334f4852005-09-09 13:10:27 -07001264 * Read a single request into the userspace filesystem's buffer. This
1265 * function waits until a request is available, then removes it from
1266 * the pending list and copies request data to userspace buffer. If
Miklos Szeredif9a28422006-06-25 05:48:53 -07001267 * no reply is needed (FORGET) or request has been aborted or there
1268 * was an error during the copying then it's finished by calling
Miklos Szeredi334f4852005-09-09 13:10:27 -07001269 * request_end(). Otherwise add it to the processing list, and set
1270 * the 'sent' flag.
1271 */
Miklos Szeredic3021622010-05-25 15:06:07 +02001272static ssize_t fuse_dev_do_read(struct fuse_conn *fc, struct file *file,
1273 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001274{
1275 int err;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001276 struct fuse_req *req;
1277 struct fuse_in *in;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001278 unsigned reqsize;
1279
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001280 restart:
Miklos Szeredid7133112006-04-10 22:54:55 -07001281 spin_lock(&fc->lock);
Jeff Dikee5ac1d12006-04-10 22:54:53 -07001282 err = -EAGAIN;
1283 if ((file->f_flags & O_NONBLOCK) && fc->connected &&
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001284 !request_pending(fc))
Jeff Dikee5ac1d12006-04-10 22:54:53 -07001285 goto err_unlock;
1286
Miklos Szeredi334f4852005-09-09 13:10:27 -07001287 request_wait(fc);
1288 err = -ENODEV;
Miklos Szeredi9ba7cbb2006-01-16 22:14:34 -08001289 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001290 goto err_unlock;
1291 err = -ERESTARTSYS;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001292 if (!request_pending(fc))
Miklos Szeredi334f4852005-09-09 13:10:27 -07001293 goto err_unlock;
1294
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001295 if (!list_empty(&fc->interrupts)) {
1296 req = list_entry(fc->interrupts.next, struct fuse_req,
1297 intr_entry);
Miklos Szeredic3021622010-05-25 15:06:07 +02001298 return fuse_read_interrupt(fc, cs, nbytes, req);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001299 }
1300
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001301 if (forget_pending(fc)) {
1302 if (list_empty(&fc->pending) || fc->forget_batch-- > 0)
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001303 return fuse_read_forget(fc, cs, nbytes);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001304
1305 if (fc->forget_batch <= -8)
1306 fc->forget_batch = 16;
1307 }
1308
Miklos Szeredi334f4852005-09-09 13:10:27 -07001309 req = list_entry(fc->pending.next, struct fuse_req, list);
Miklos Szeredi83cfd492006-01-16 22:14:31 -08001310 req->state = FUSE_REQ_READING;
Miklos Szeredid77a1d52006-01-16 22:14:31 -08001311 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001312
1313 in = &req->in;
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001314 reqsize = in->h.len;
1315 /* If request is too large, reply with an error and restart the read */
Miklos Szeredic3021622010-05-25 15:06:07 +02001316 if (nbytes < reqsize) {
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001317 req->out.h.error = -EIO;
1318 /* SETXATTR is special, since it may contain too large data */
1319 if (in->h.opcode == FUSE_SETXATTR)
1320 req->out.h.error = -E2BIG;
1321 request_end(fc, req);
1322 goto restart;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001323 }
Miklos Szeredid7133112006-04-10 22:54:55 -07001324 spin_unlock(&fc->lock);
Miklos Szeredic3021622010-05-25 15:06:07 +02001325 cs->req = req;
1326 err = fuse_copy_one(cs, &in->h, sizeof(in->h));
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001327 if (!err)
Miklos Szeredic3021622010-05-25 15:06:07 +02001328 err = fuse_copy_args(cs, in->numargs, in->argpages,
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001329 (struct fuse_arg *) in->args, 0);
Miklos Szeredic3021622010-05-25 15:06:07 +02001330 fuse_copy_finish(cs);
Miklos Szeredid7133112006-04-10 22:54:55 -07001331 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001332 req->locked = 0;
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -07001333 if (req->aborted) {
1334 request_end(fc, req);
1335 return -ENODEV;
1336 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001337 if (err) {
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -07001338 req->out.h.error = -EIO;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001339 request_end(fc, req);
1340 return err;
1341 }
1342 if (!req->isreply)
1343 request_end(fc, req);
1344 else {
Miklos Szeredi83cfd492006-01-16 22:14:31 -08001345 req->state = FUSE_REQ_SENT;
Miklos Szeredid77a1d52006-01-16 22:14:31 -08001346 list_move_tail(&req->list, &fc->processing);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001347 if (req->interrupted)
1348 queue_interrupt(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -07001349 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001350 }
1351 return reqsize;
1352
1353 err_unlock:
Miklos Szeredid7133112006-04-10 22:54:55 -07001354 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001355 return err;
1356}
1357
Tom Van Braeckel94e4fe22015-01-12 05:22:16 +01001358static int fuse_dev_open(struct inode *inode, struct file *file)
1359{
1360 /*
1361 * The fuse device's file's private_data is used to hold
1362 * the fuse_conn(ection) when it is mounted, and is used to
1363 * keep track of whether the file has been mounted already.
1364 */
1365 file->private_data = NULL;
1366 return 0;
1367}
1368
Al Virofbdbacc2015-04-03 21:53:39 -04001369static ssize_t fuse_dev_read(struct kiocb *iocb, struct iov_iter *to)
Miklos Szeredic3021622010-05-25 15:06:07 +02001370{
1371 struct fuse_copy_state cs;
1372 struct file *file = iocb->ki_filp;
1373 struct fuse_conn *fc = fuse_get_conn(file);
1374 if (!fc)
1375 return -EPERM;
1376
Al Virofbdbacc2015-04-03 21:53:39 -04001377 if (!iter_is_iovec(to))
1378 return -EINVAL;
Miklos Szeredic3021622010-05-25 15:06:07 +02001379
Al Viro6c09e942015-04-03 22:06:08 -04001380 fuse_copy_init(&cs, fc, 1, to);
Al Virofbdbacc2015-04-03 21:53:39 -04001381
1382 return fuse_dev_do_read(fc, file, &cs, iov_iter_count(to));
Miklos Szeredic3021622010-05-25 15:06:07 +02001383}
1384
Miklos Szeredic3021622010-05-25 15:06:07 +02001385static ssize_t fuse_dev_splice_read(struct file *in, loff_t *ppos,
1386 struct pipe_inode_info *pipe,
1387 size_t len, unsigned int flags)
1388{
1389 int ret;
1390 int page_nr = 0;
1391 int do_wakeup = 0;
1392 struct pipe_buffer *bufs;
1393 struct fuse_copy_state cs;
1394 struct fuse_conn *fc = fuse_get_conn(in);
1395 if (!fc)
1396 return -EPERM;
1397
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001398 bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), GFP_KERNEL);
Miklos Szeredic3021622010-05-25 15:06:07 +02001399 if (!bufs)
1400 return -ENOMEM;
1401
Al Viro6c09e942015-04-03 22:06:08 -04001402 fuse_copy_init(&cs, fc, 1, NULL);
Miklos Szeredic3021622010-05-25 15:06:07 +02001403 cs.pipebufs = bufs;
1404 cs.pipe = pipe;
1405 ret = fuse_dev_do_read(fc, in, &cs, len);
1406 if (ret < 0)
1407 goto out;
1408
1409 ret = 0;
1410 pipe_lock(pipe);
1411
1412 if (!pipe->readers) {
1413 send_sig(SIGPIPE, current, 0);
1414 if (!ret)
1415 ret = -EPIPE;
1416 goto out_unlock;
1417 }
1418
1419 if (pipe->nrbufs + cs.nr_segs > pipe->buffers) {
1420 ret = -EIO;
1421 goto out_unlock;
1422 }
1423
1424 while (page_nr < cs.nr_segs) {
1425 int newbuf = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
1426 struct pipe_buffer *buf = pipe->bufs + newbuf;
1427
1428 buf->page = bufs[page_nr].page;
1429 buf->offset = bufs[page_nr].offset;
1430 buf->len = bufs[page_nr].len;
Miklos Szeredi28a625c2014-01-22 19:36:57 +01001431 /*
1432 * Need to be careful about this. Having buf->ops in module
1433 * code can Oops if the buffer persists after module unload.
1434 */
1435 buf->ops = &nosteal_pipe_buf_ops;
Miklos Szeredic3021622010-05-25 15:06:07 +02001436
1437 pipe->nrbufs++;
1438 page_nr++;
1439 ret += buf->len;
1440
Al Viro6447a3c2013-03-21 11:01:38 -04001441 if (pipe->files)
Miklos Szeredic3021622010-05-25 15:06:07 +02001442 do_wakeup = 1;
1443 }
1444
1445out_unlock:
1446 pipe_unlock(pipe);
1447
1448 if (do_wakeup) {
1449 smp_mb();
1450 if (waitqueue_active(&pipe->wait))
1451 wake_up_interruptible(&pipe->wait);
1452 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
1453 }
1454
1455out:
1456 for (; page_nr < cs.nr_segs; page_nr++)
1457 page_cache_release(bufs[page_nr].page);
1458
1459 kfree(bufs);
1460 return ret;
1461}
1462
Tejun Heo95668a62008-11-26 12:03:55 +01001463static int fuse_notify_poll(struct fuse_conn *fc, unsigned int size,
1464 struct fuse_copy_state *cs)
1465{
1466 struct fuse_notify_poll_wakeup_out outarg;
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001467 int err = -EINVAL;
Tejun Heo95668a62008-11-26 12:03:55 +01001468
1469 if (size != sizeof(outarg))
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001470 goto err;
Tejun Heo95668a62008-11-26 12:03:55 +01001471
1472 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1473 if (err)
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001474 goto err;
Tejun Heo95668a62008-11-26 12:03:55 +01001475
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001476 fuse_copy_finish(cs);
Tejun Heo95668a62008-11-26 12:03:55 +01001477 return fuse_notify_poll_wakeup(fc, &outarg);
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001478
1479err:
1480 fuse_copy_finish(cs);
1481 return err;
Tejun Heo95668a62008-11-26 12:03:55 +01001482}
1483
John Muir3b463ae2009-05-31 11:13:57 -04001484static int fuse_notify_inval_inode(struct fuse_conn *fc, unsigned int size,
1485 struct fuse_copy_state *cs)
1486{
1487 struct fuse_notify_inval_inode_out outarg;
1488 int err = -EINVAL;
1489
1490 if (size != sizeof(outarg))
1491 goto err;
1492
1493 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1494 if (err)
1495 goto err;
1496 fuse_copy_finish(cs);
1497
1498 down_read(&fc->killsb);
1499 err = -ENOENT;
Miklos Szeredib21dda42010-02-05 12:08:31 +01001500 if (fc->sb) {
1501 err = fuse_reverse_inval_inode(fc->sb, outarg.ino,
1502 outarg.off, outarg.len);
1503 }
John Muir3b463ae2009-05-31 11:13:57 -04001504 up_read(&fc->killsb);
1505 return err;
1506
1507err:
1508 fuse_copy_finish(cs);
1509 return err;
1510}
1511
1512static int fuse_notify_inval_entry(struct fuse_conn *fc, unsigned int size,
1513 struct fuse_copy_state *cs)
1514{
1515 struct fuse_notify_inval_entry_out outarg;
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001516 int err = -ENOMEM;
1517 char *buf;
John Muir3b463ae2009-05-31 11:13:57 -04001518 struct qstr name;
1519
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001520 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1521 if (!buf)
1522 goto err;
1523
1524 err = -EINVAL;
John Muir3b463ae2009-05-31 11:13:57 -04001525 if (size < sizeof(outarg))
1526 goto err;
1527
1528 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1529 if (err)
1530 goto err;
1531
1532 err = -ENAMETOOLONG;
1533 if (outarg.namelen > FUSE_NAME_MAX)
1534 goto err;
1535
Miklos Szeredic2183d12011-08-24 10:20:17 +02001536 err = -EINVAL;
1537 if (size != sizeof(outarg) + outarg.namelen + 1)
1538 goto err;
1539
John Muir3b463ae2009-05-31 11:13:57 -04001540 name.name = buf;
1541 name.len = outarg.namelen;
1542 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1543 if (err)
1544 goto err;
1545 fuse_copy_finish(cs);
1546 buf[outarg.namelen] = 0;
1547 name.hash = full_name_hash(name.name, name.len);
1548
1549 down_read(&fc->killsb);
1550 err = -ENOENT;
Miklos Szeredib21dda42010-02-05 12:08:31 +01001551 if (fc->sb)
John Muir451d0f52011-12-06 21:50:06 +01001552 err = fuse_reverse_inval_entry(fc->sb, outarg.parent, 0, &name);
1553 up_read(&fc->killsb);
1554 kfree(buf);
1555 return err;
1556
1557err:
1558 kfree(buf);
1559 fuse_copy_finish(cs);
1560 return err;
1561}
1562
1563static int fuse_notify_delete(struct fuse_conn *fc, unsigned int size,
1564 struct fuse_copy_state *cs)
1565{
1566 struct fuse_notify_delete_out outarg;
1567 int err = -ENOMEM;
1568 char *buf;
1569 struct qstr name;
1570
1571 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1572 if (!buf)
1573 goto err;
1574
1575 err = -EINVAL;
1576 if (size < sizeof(outarg))
1577 goto err;
1578
1579 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1580 if (err)
1581 goto err;
1582
1583 err = -ENAMETOOLONG;
1584 if (outarg.namelen > FUSE_NAME_MAX)
1585 goto err;
1586
1587 err = -EINVAL;
1588 if (size != sizeof(outarg) + outarg.namelen + 1)
1589 goto err;
1590
1591 name.name = buf;
1592 name.len = outarg.namelen;
1593 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1594 if (err)
1595 goto err;
1596 fuse_copy_finish(cs);
1597 buf[outarg.namelen] = 0;
1598 name.hash = full_name_hash(name.name, name.len);
1599
1600 down_read(&fc->killsb);
1601 err = -ENOENT;
1602 if (fc->sb)
1603 err = fuse_reverse_inval_entry(fc->sb, outarg.parent,
1604 outarg.child, &name);
John Muir3b463ae2009-05-31 11:13:57 -04001605 up_read(&fc->killsb);
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001606 kfree(buf);
John Muir3b463ae2009-05-31 11:13:57 -04001607 return err;
1608
1609err:
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001610 kfree(buf);
John Muir3b463ae2009-05-31 11:13:57 -04001611 fuse_copy_finish(cs);
1612 return err;
1613}
1614
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001615static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
1616 struct fuse_copy_state *cs)
1617{
1618 struct fuse_notify_store_out outarg;
1619 struct inode *inode;
1620 struct address_space *mapping;
1621 u64 nodeid;
1622 int err;
1623 pgoff_t index;
1624 unsigned int offset;
1625 unsigned int num;
1626 loff_t file_size;
1627 loff_t end;
1628
1629 err = -EINVAL;
1630 if (size < sizeof(outarg))
1631 goto out_finish;
1632
1633 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1634 if (err)
1635 goto out_finish;
1636
1637 err = -EINVAL;
1638 if (size - sizeof(outarg) != outarg.size)
1639 goto out_finish;
1640
1641 nodeid = outarg.nodeid;
1642
1643 down_read(&fc->killsb);
1644
1645 err = -ENOENT;
1646 if (!fc->sb)
1647 goto out_up_killsb;
1648
1649 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1650 if (!inode)
1651 goto out_up_killsb;
1652
1653 mapping = inode->i_mapping;
1654 index = outarg.offset >> PAGE_CACHE_SHIFT;
1655 offset = outarg.offset & ~PAGE_CACHE_MASK;
1656 file_size = i_size_read(inode);
1657 end = outarg.offset + outarg.size;
1658 if (end > file_size) {
1659 file_size = end;
1660 fuse_write_update_size(inode, file_size);
1661 }
1662
1663 num = outarg.size;
1664 while (num) {
1665 struct page *page;
1666 unsigned int this_num;
1667
1668 err = -ENOMEM;
1669 page = find_or_create_page(mapping, index,
1670 mapping_gfp_mask(mapping));
1671 if (!page)
1672 goto out_iput;
1673
1674 this_num = min_t(unsigned, num, PAGE_CACHE_SIZE - offset);
1675 err = fuse_copy_page(cs, &page, offset, this_num, 0);
Miklos Szeredi063ec1e2014-01-22 19:36:58 +01001676 if (!err && offset == 0 &&
1677 (this_num == PAGE_CACHE_SIZE || file_size == end))
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001678 SetPageUptodate(page);
1679 unlock_page(page);
1680 page_cache_release(page);
1681
1682 if (err)
1683 goto out_iput;
1684
1685 num -= this_num;
1686 offset = 0;
1687 index++;
1688 }
1689
1690 err = 0;
1691
1692out_iput:
1693 iput(inode);
1694out_up_killsb:
1695 up_read(&fc->killsb);
1696out_finish:
1697 fuse_copy_finish(cs);
1698 return err;
1699}
1700
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001701static void fuse_retrieve_end(struct fuse_conn *fc, struct fuse_req *req)
1702{
Mel Gormanb745bc82014-06-04 16:10:22 -07001703 release_pages(req->pages, req->num_pages, false);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001704}
1705
1706static int fuse_retrieve(struct fuse_conn *fc, struct inode *inode,
1707 struct fuse_notify_retrieve_out *outarg)
1708{
1709 int err;
1710 struct address_space *mapping = inode->i_mapping;
1711 struct fuse_req *req;
1712 pgoff_t index;
1713 loff_t file_size;
1714 unsigned int num;
1715 unsigned int offset;
Geert Uytterhoeven01574432010-09-30 22:06:21 +02001716 size_t total_len = 0;
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001717 int num_pages;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001718
1719 offset = outarg->offset & ~PAGE_CACHE_MASK;
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001720 file_size = i_size_read(inode);
1721
1722 num = outarg->size;
1723 if (outarg->offset > file_size)
1724 num = 0;
1725 else if (outarg->offset + num > file_size)
1726 num = file_size - outarg->offset;
1727
1728 num_pages = (num + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1729 num_pages = min(num_pages, FUSE_MAX_PAGES_PER_REQ);
1730
1731 req = fuse_get_req(fc, num_pages);
1732 if (IS_ERR(req))
1733 return PTR_ERR(req);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001734
1735 req->in.h.opcode = FUSE_NOTIFY_REPLY;
1736 req->in.h.nodeid = outarg->nodeid;
1737 req->in.numargs = 2;
1738 req->in.argpages = 1;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001739 req->page_descs[0].offset = offset;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001740 req->end = fuse_retrieve_end;
1741
1742 index = outarg->offset >> PAGE_CACHE_SHIFT;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001743
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001744 while (num && req->num_pages < num_pages) {
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001745 struct page *page;
1746 unsigned int this_num;
1747
1748 page = find_get_page(mapping, index);
1749 if (!page)
1750 break;
1751
1752 this_num = min_t(unsigned, num, PAGE_CACHE_SIZE - offset);
1753 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001754 req->page_descs[req->num_pages].length = this_num;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001755 req->num_pages++;
1756
Miklos Szeredic9e67d42012-09-04 18:45:54 +02001757 offset = 0;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001758 num -= this_num;
1759 total_len += this_num;
Miklos Szeredi48706d02011-12-13 10:36:59 +01001760 index++;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001761 }
1762 req->misc.retrieve_in.offset = outarg->offset;
1763 req->misc.retrieve_in.size = total_len;
1764 req->in.args[0].size = sizeof(req->misc.retrieve_in);
1765 req->in.args[0].value = &req->misc.retrieve_in;
1766 req->in.args[1].size = total_len;
1767
1768 err = fuse_request_send_notify_reply(fc, req, outarg->notify_unique);
1769 if (err)
1770 fuse_retrieve_end(fc, req);
1771
1772 return err;
1773}
1774
1775static int fuse_notify_retrieve(struct fuse_conn *fc, unsigned int size,
1776 struct fuse_copy_state *cs)
1777{
1778 struct fuse_notify_retrieve_out outarg;
1779 struct inode *inode;
1780 int err;
1781
1782 err = -EINVAL;
1783 if (size != sizeof(outarg))
1784 goto copy_finish;
1785
1786 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1787 if (err)
1788 goto copy_finish;
1789
1790 fuse_copy_finish(cs);
1791
1792 down_read(&fc->killsb);
1793 err = -ENOENT;
1794 if (fc->sb) {
1795 u64 nodeid = outarg.nodeid;
1796
1797 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1798 if (inode) {
1799 err = fuse_retrieve(fc, inode, &outarg);
1800 iput(inode);
1801 }
1802 }
1803 up_read(&fc->killsb);
1804
1805 return err;
1806
1807copy_finish:
1808 fuse_copy_finish(cs);
1809 return err;
1810}
1811
Tejun Heo85993962008-11-26 12:03:55 +01001812static int fuse_notify(struct fuse_conn *fc, enum fuse_notify_code code,
1813 unsigned int size, struct fuse_copy_state *cs)
1814{
Miklos Szeredi0d278362015-02-26 11:45:47 +01001815 /* Don't try to move pages (yet) */
1816 cs->move_pages = 0;
1817
Tejun Heo85993962008-11-26 12:03:55 +01001818 switch (code) {
Tejun Heo95668a62008-11-26 12:03:55 +01001819 case FUSE_NOTIFY_POLL:
1820 return fuse_notify_poll(fc, size, cs);
1821
John Muir3b463ae2009-05-31 11:13:57 -04001822 case FUSE_NOTIFY_INVAL_INODE:
1823 return fuse_notify_inval_inode(fc, size, cs);
1824
1825 case FUSE_NOTIFY_INVAL_ENTRY:
1826 return fuse_notify_inval_entry(fc, size, cs);
1827
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001828 case FUSE_NOTIFY_STORE:
1829 return fuse_notify_store(fc, size, cs);
1830
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001831 case FUSE_NOTIFY_RETRIEVE:
1832 return fuse_notify_retrieve(fc, size, cs);
1833
John Muir451d0f52011-12-06 21:50:06 +01001834 case FUSE_NOTIFY_DELETE:
1835 return fuse_notify_delete(fc, size, cs);
1836
Tejun Heo85993962008-11-26 12:03:55 +01001837 default:
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001838 fuse_copy_finish(cs);
Tejun Heo85993962008-11-26 12:03:55 +01001839 return -EINVAL;
1840 }
1841}
1842
Miklos Szeredi334f4852005-09-09 13:10:27 -07001843/* Look up request on processing list by unique ID */
1844static struct fuse_req *request_find(struct fuse_conn *fc, u64 unique)
1845{
Dong Fang05726ac2013-07-30 22:50:01 -04001846 struct fuse_req *req;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001847
Dong Fang05726ac2013-07-30 22:50:01 -04001848 list_for_each_entry(req, &fc->processing, list) {
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001849 if (req->in.h.unique == unique || req->intr_unique == unique)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001850 return req;
1851 }
1852 return NULL;
1853}
1854
1855static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
1856 unsigned nbytes)
1857{
1858 unsigned reqsize = sizeof(struct fuse_out_header);
1859
1860 if (out->h.error)
1861 return nbytes != reqsize ? -EINVAL : 0;
1862
1863 reqsize += len_args(out->numargs, out->args);
1864
1865 if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
1866 return -EINVAL;
1867 else if (reqsize > nbytes) {
1868 struct fuse_arg *lastarg = &out->args[out->numargs-1];
1869 unsigned diffsize = reqsize - nbytes;
1870 if (diffsize > lastarg->size)
1871 return -EINVAL;
1872 lastarg->size -= diffsize;
1873 }
1874 return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
1875 out->page_zeroing);
1876}
1877
1878/*
1879 * Write a single reply to a request. First the header is copied from
1880 * the write buffer. The request is then searched on the processing
1881 * list by the unique ID found in the header. If found, then remove
1882 * it from the list and copy the rest of the buffer to the request.
1883 * The request is finished by calling request_end()
1884 */
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001885static ssize_t fuse_dev_do_write(struct fuse_conn *fc,
1886 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001887{
1888 int err;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001889 struct fuse_req *req;
1890 struct fuse_out_header oh;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001891
Miklos Szeredi334f4852005-09-09 13:10:27 -07001892 if (nbytes < sizeof(struct fuse_out_header))
1893 return -EINVAL;
1894
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001895 err = fuse_copy_one(cs, &oh, sizeof(oh));
Miklos Szeredi334f4852005-09-09 13:10:27 -07001896 if (err)
1897 goto err_finish;
Tejun Heo85993962008-11-26 12:03:55 +01001898
Miklos Szeredi334f4852005-09-09 13:10:27 -07001899 err = -EINVAL;
Tejun Heo85993962008-11-26 12:03:55 +01001900 if (oh.len != nbytes)
1901 goto err_finish;
1902
1903 /*
1904 * Zero oh.unique indicates unsolicited notification message
1905 * and error contains notification code.
1906 */
1907 if (!oh.unique) {
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001908 err = fuse_notify(fc, oh.error, nbytes - sizeof(oh), cs);
Tejun Heo85993962008-11-26 12:03:55 +01001909 return err ? err : nbytes;
1910 }
1911
1912 err = -EINVAL;
1913 if (oh.error <= -1000 || oh.error > 0)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001914 goto err_finish;
1915
Miklos Szeredid7133112006-04-10 22:54:55 -07001916 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001917 err = -ENOENT;
1918 if (!fc->connected)
1919 goto err_unlock;
1920
Miklos Szeredi334f4852005-09-09 13:10:27 -07001921 req = request_find(fc, oh.unique);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001922 if (!req)
1923 goto err_unlock;
1924
Miklos Szeredif9a28422006-06-25 05:48:53 -07001925 if (req->aborted) {
Miklos Szeredid7133112006-04-10 22:54:55 -07001926 spin_unlock(&fc->lock);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001927 fuse_copy_finish(cs);
Miklos Szeredid7133112006-04-10 22:54:55 -07001928 spin_lock(&fc->lock);
Miklos Szeredi222f1d62006-01-16 22:14:25 -08001929 request_end(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001930 return -ENOENT;
1931 }
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001932 /* Is it an interrupt reply? */
1933 if (req->intr_unique == oh.unique) {
1934 err = -EINVAL;
1935 if (nbytes != sizeof(struct fuse_out_header))
1936 goto err_unlock;
1937
1938 if (oh.error == -ENOSYS)
1939 fc->no_interrupt = 1;
1940 else if (oh.error == -EAGAIN)
1941 queue_interrupt(fc, req);
1942
1943 spin_unlock(&fc->lock);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001944 fuse_copy_finish(cs);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001945 return nbytes;
1946 }
1947
1948 req->state = FUSE_REQ_WRITING;
Miklos Szeredid77a1d52006-01-16 22:14:31 -08001949 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001950 req->out.h = oh;
1951 req->locked = 1;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001952 cs->req = req;
Miklos Szeredice534fb2010-05-25 15:06:07 +02001953 if (!req->out.page_replace)
1954 cs->move_pages = 0;
Miklos Szeredid7133112006-04-10 22:54:55 -07001955 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001956
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001957 err = copy_out_args(cs, &req->out, nbytes);
1958 fuse_copy_finish(cs);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001959
Miklos Szeredid7133112006-04-10 22:54:55 -07001960 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001961 req->locked = 0;
1962 if (!err) {
Miklos Szeredif9a28422006-06-25 05:48:53 -07001963 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001964 err = -ENOENT;
Miklos Szeredif9a28422006-06-25 05:48:53 -07001965 } else if (!req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001966 req->out.h.error = -EIO;
1967 request_end(fc, req);
1968
1969 return err ? err : nbytes;
1970
1971 err_unlock:
Miklos Szeredid7133112006-04-10 22:54:55 -07001972 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001973 err_finish:
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001974 fuse_copy_finish(cs);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001975 return err;
1976}
1977
Al Virofbdbacc2015-04-03 21:53:39 -04001978static ssize_t fuse_dev_write(struct kiocb *iocb, struct iov_iter *from)
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001979{
1980 struct fuse_copy_state cs;
1981 struct fuse_conn *fc = fuse_get_conn(iocb->ki_filp);
1982 if (!fc)
1983 return -EPERM;
1984
Al Virofbdbacc2015-04-03 21:53:39 -04001985 if (!iter_is_iovec(from))
1986 return -EINVAL;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001987
Al Viro6c09e942015-04-03 22:06:08 -04001988 fuse_copy_init(&cs, fc, 0, from);
Al Virofbdbacc2015-04-03 21:53:39 -04001989
1990 return fuse_dev_do_write(fc, &cs, iov_iter_count(from));
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001991}
1992
1993static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe,
1994 struct file *out, loff_t *ppos,
1995 size_t len, unsigned int flags)
1996{
1997 unsigned nbuf;
1998 unsigned idx;
1999 struct pipe_buffer *bufs;
2000 struct fuse_copy_state cs;
2001 struct fuse_conn *fc;
2002 size_t rem;
2003 ssize_t ret;
2004
2005 fc = fuse_get_conn(out);
2006 if (!fc)
2007 return -EPERM;
2008
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01002009 bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), GFP_KERNEL);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002010 if (!bufs)
2011 return -ENOMEM;
2012
2013 pipe_lock(pipe);
2014 nbuf = 0;
2015 rem = 0;
2016 for (idx = 0; idx < pipe->nrbufs && rem < len; idx++)
2017 rem += pipe->bufs[(pipe->curbuf + idx) & (pipe->buffers - 1)].len;
2018
2019 ret = -EINVAL;
2020 if (rem < len) {
2021 pipe_unlock(pipe);
2022 goto out;
2023 }
2024
2025 rem = len;
2026 while (rem) {
2027 struct pipe_buffer *ibuf;
2028 struct pipe_buffer *obuf;
2029
2030 BUG_ON(nbuf >= pipe->buffers);
2031 BUG_ON(!pipe->nrbufs);
2032 ibuf = &pipe->bufs[pipe->curbuf];
2033 obuf = &bufs[nbuf];
2034
2035 if (rem >= ibuf->len) {
2036 *obuf = *ibuf;
2037 ibuf->ops = NULL;
2038 pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
2039 pipe->nrbufs--;
2040 } else {
2041 ibuf->ops->get(pipe, ibuf);
2042 *obuf = *ibuf;
2043 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
2044 obuf->len = rem;
2045 ibuf->offset += obuf->len;
2046 ibuf->len -= obuf->len;
2047 }
2048 nbuf++;
2049 rem -= obuf->len;
2050 }
2051 pipe_unlock(pipe);
2052
Al Viro6c09e942015-04-03 22:06:08 -04002053 fuse_copy_init(&cs, fc, 0, NULL);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002054 cs.pipebufs = bufs;
Al Viro6c09e942015-04-03 22:06:08 -04002055 cs.nr_segs = nbuf;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002056 cs.pipe = pipe;
2057
Miklos Szeredice534fb2010-05-25 15:06:07 +02002058 if (flags & SPLICE_F_MOVE)
2059 cs.move_pages = 1;
2060
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002061 ret = fuse_dev_do_write(fc, &cs, len);
2062
2063 for (idx = 0; idx < nbuf; idx++) {
2064 struct pipe_buffer *buf = &bufs[idx];
2065 buf->ops->release(pipe, buf);
2066 }
2067out:
2068 kfree(bufs);
2069 return ret;
2070}
2071
Miklos Szeredi334f4852005-09-09 13:10:27 -07002072static unsigned fuse_dev_poll(struct file *file, poll_table *wait)
2073{
Miklos Szeredi334f4852005-09-09 13:10:27 -07002074 unsigned mask = POLLOUT | POLLWRNORM;
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07002075 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002076 if (!fc)
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07002077 return POLLERR;
Miklos Szeredi334f4852005-09-09 13:10:27 -07002078
2079 poll_wait(file, &fc->waitq, wait);
2080
Miklos Szeredid7133112006-04-10 22:54:55 -07002081 spin_lock(&fc->lock);
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07002082 if (!fc->connected)
2083 mask = POLLERR;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07002084 else if (request_pending(fc))
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07002085 mask |= POLLIN | POLLRDNORM;
Miklos Szeredid7133112006-04-10 22:54:55 -07002086 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002087
2088 return mask;
2089}
2090
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002091/*
2092 * Abort all requests on the given list (pending or processing)
2093 *
Miklos Szeredid7133112006-04-10 22:54:55 -07002094 * This function releases and reacquires fc->lock
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002095 */
Miklos Szeredi334f4852005-09-09 13:10:27 -07002096static void end_requests(struct fuse_conn *fc, struct list_head *head)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02002097__releases(fc->lock)
2098__acquires(fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -07002099{
2100 while (!list_empty(head)) {
2101 struct fuse_req *req;
2102 req = list_entry(head->next, struct fuse_req, list);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002103 req->out.h.error = -ECONNABORTED;
2104 request_end(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -07002105 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002106 }
2107}
2108
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002109/*
2110 * Abort requests under I/O
2111 *
Miklos Szeredif9a28422006-06-25 05:48:53 -07002112 * The requests are set to aborted and finished, and the request
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002113 * waiter is woken up. This will make request_wait_answer() wait
2114 * until the request is unlocked and then return.
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08002115 *
2116 * If the request is asynchronous, then the end function needs to be
2117 * called after waiting for the request to be unlocked (if it was
2118 * locked).
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002119 */
2120static void end_io_requests(struct fuse_conn *fc)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02002121__releases(fc->lock)
2122__acquires(fc->lock)
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002123{
2124 while (!list_empty(&fc->io)) {
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08002125 struct fuse_req *req =
2126 list_entry(fc->io.next, struct fuse_req, list);
2127 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
2128
Miklos Szeredif9a28422006-06-25 05:48:53 -07002129 req->aborted = 1;
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002130 req->out.h.error = -ECONNABORTED;
2131 req->state = FUSE_REQ_FINISHED;
2132 list_del_init(&req->list);
2133 wake_up(&req->waitq);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08002134 if (end) {
2135 req->end = NULL;
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08002136 __fuse_get_request(req);
Miklos Szeredid7133112006-04-10 22:54:55 -07002137 spin_unlock(&fc->lock);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08002138 wait_event(req->waitq, !req->locked);
2139 end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +01002140 fuse_put_request(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -07002141 spin_lock(&fc->lock);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08002142 }
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002143 }
2144}
2145
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002146static void end_queued_requests(struct fuse_conn *fc)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02002147__releases(fc->lock)
2148__acquires(fc->lock)
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002149{
2150 fc->max_background = UINT_MAX;
2151 flush_bg_queue(fc);
2152 end_requests(fc, &fc->pending);
2153 end_requests(fc, &fc->processing);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01002154 while (forget_pending(fc))
Miklos Szeredi02c048b2010-12-07 20:16:56 +01002155 kfree(dequeue_forget(fc, 1, NULL));
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002156}
2157
Bryan Green357ccf22011-03-01 16:43:52 -08002158static void end_polls(struct fuse_conn *fc)
2159{
2160 struct rb_node *p;
2161
2162 p = rb_first(&fc->polled_files);
2163
2164 while (p) {
2165 struct fuse_file *ff;
2166 ff = rb_entry(p, struct fuse_file, polled_node);
2167 wake_up_interruptible_all(&ff->poll_wait);
2168
2169 p = rb_next(p);
2170 }
2171}
2172
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002173/*
2174 * Abort all requests.
2175 *
2176 * Emergency exit in case of a malicious or accidental deadlock, or
2177 * just a hung filesystem.
2178 *
2179 * The same effect is usually achievable through killing the
2180 * filesystem daemon and all users of the filesystem. The exception
2181 * is the combination of an asynchronous request and the tricky
2182 * deadlock (see Documentation/filesystems/fuse.txt).
2183 *
2184 * During the aborting, progression of requests from the pending and
2185 * processing lists onto the io list, and progression of new requests
2186 * onto the pending list is prevented by req->connected being false.
2187 *
2188 * Progression of requests under I/O to the processing list is
Miklos Szeredif9a28422006-06-25 05:48:53 -07002189 * prevented by the req->aborted flag being true for these requests.
2190 * For this reason requests on the io list must be aborted first.
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002191 */
2192void fuse_abort_conn(struct fuse_conn *fc)
2193{
Miklos Szeredid7133112006-04-10 22:54:55 -07002194 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002195 if (fc->connected) {
2196 fc->connected = 0;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -07002197 fc->blocked = 0;
Miklos Szeredi9759bd512015-01-06 10:45:35 +01002198 fuse_set_initialized(fc);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002199 end_io_requests(fc);
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002200 end_queued_requests(fc);
Bryan Green357ccf22011-03-01 16:43:52 -08002201 end_polls(fc);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002202 wake_up_all(&fc->waitq);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -07002203 wake_up_all(&fc->blocked_waitq);
Jeff Dike385a17b2006-04-10 22:54:52 -07002204 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002205 }
Miklos Szeredid7133112006-04-10 22:54:55 -07002206 spin_unlock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002207}
Tejun Heo08cbf542009-04-14 10:54:53 +09002208EXPORT_SYMBOL_GPL(fuse_abort_conn);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002209
Tejun Heo08cbf542009-04-14 10:54:53 +09002210int fuse_dev_release(struct inode *inode, struct file *file)
Miklos Szeredi334f4852005-09-09 13:10:27 -07002211{
Miklos Szeredi0720b312006-04-10 22:54:55 -07002212 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002213 if (fc) {
Miklos Szeredid7133112006-04-10 22:54:55 -07002214 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -07002215 fc->connected = 0;
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002216 fc->blocked = 0;
Miklos Szeredi9759bd512015-01-06 10:45:35 +01002217 fuse_set_initialized(fc);
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002218 end_queued_requests(fc);
Bryan Green357ccf22011-03-01 16:43:52 -08002219 end_polls(fc);
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002220 wake_up_all(&fc->blocked_waitq);
Miklos Szeredid7133112006-04-10 22:54:55 -07002221 spin_unlock(&fc->lock);
Miklos Szeredibafa9652006-06-25 05:48:51 -07002222 fuse_conn_put(fc);
Jeff Dike385a17b2006-04-10 22:54:52 -07002223 }
Miklos Szeredif543f252006-01-16 22:14:35 -08002224
Miklos Szeredi334f4852005-09-09 13:10:27 -07002225 return 0;
2226}
Tejun Heo08cbf542009-04-14 10:54:53 +09002227EXPORT_SYMBOL_GPL(fuse_dev_release);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002228
Jeff Dike385a17b2006-04-10 22:54:52 -07002229static int fuse_dev_fasync(int fd, struct file *file, int on)
2230{
2231 struct fuse_conn *fc = fuse_get_conn(file);
2232 if (!fc)
Miklos Szeredia87046d2006-04-10 22:54:56 -07002233 return -EPERM;
Jeff Dike385a17b2006-04-10 22:54:52 -07002234
2235 /* No locking - fasync_helper does its own locking */
2236 return fasync_helper(fd, file, on, &fc->fasync);
2237}
2238
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002239const struct file_operations fuse_dev_operations = {
Miklos Szeredi334f4852005-09-09 13:10:27 -07002240 .owner = THIS_MODULE,
Tom Van Braeckel94e4fe22015-01-12 05:22:16 +01002241 .open = fuse_dev_open,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002242 .llseek = no_llseek,
Al Virofbdbacc2015-04-03 21:53:39 -04002243 .read_iter = fuse_dev_read,
Miklos Szeredic3021622010-05-25 15:06:07 +02002244 .splice_read = fuse_dev_splice_read,
Al Virofbdbacc2015-04-03 21:53:39 -04002245 .write_iter = fuse_dev_write,
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002246 .splice_write = fuse_dev_splice_write,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002247 .poll = fuse_dev_poll,
2248 .release = fuse_dev_release,
Jeff Dike385a17b2006-04-10 22:54:52 -07002249 .fasync = fuse_dev_fasync,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002250};
Tejun Heo08cbf542009-04-14 10:54:53 +09002251EXPORT_SYMBOL_GPL(fuse_dev_operations);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002252
2253static struct miscdevice fuse_miscdevice = {
2254 .minor = FUSE_MINOR,
2255 .name = "fuse",
2256 .fops = &fuse_dev_operations,
2257};
2258
2259int __init fuse_dev_init(void)
2260{
2261 int err = -ENOMEM;
2262 fuse_req_cachep = kmem_cache_create("fuse_request",
2263 sizeof(struct fuse_req),
Paul Mundt20c2df82007-07-20 10:11:58 +09002264 0, 0, NULL);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002265 if (!fuse_req_cachep)
2266 goto out;
2267
2268 err = misc_register(&fuse_miscdevice);
2269 if (err)
2270 goto out_cache_clean;
2271
2272 return 0;
2273
2274 out_cache_clean:
2275 kmem_cache_destroy(fuse_req_cachep);
2276 out:
2277 return err;
2278}
2279
2280void fuse_dev_cleanup(void)
2281{
2282 misc_deregister(&fuse_miscdevice);
2283 kmem_cache_destroy(fuse_req_cachep);
2284}