blob: e5c541b7c7af268ad9ee4c2983e748c59d84d46b [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 Szeredi825d6d32015-07-01 16:25:58 +0200184 __set_bit(FR_WAITING, &req->flags);
185 if (for_background)
186 __set_bit(FR_BACKGROUND, &req->flags);
187
Miklos Szeredi334f4852005-09-09 13:10:27 -0700188 return req;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200189
190 out:
191 atomic_dec(&fc->num_waiting);
192 return ERR_PTR(err);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700193}
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400194
195struct fuse_req *fuse_get_req(struct fuse_conn *fc, unsigned npages)
196{
197 return __fuse_get_req(fc, npages, false);
198}
Tejun Heo08cbf542009-04-14 10:54:53 +0900199EXPORT_SYMBOL_GPL(fuse_get_req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700200
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400201struct fuse_req *fuse_get_req_for_background(struct fuse_conn *fc,
202 unsigned npages)
203{
204 return __fuse_get_req(fc, npages, true);
205}
206EXPORT_SYMBOL_GPL(fuse_get_req_for_background);
207
Miklos Szeredi33649c92006-06-25 05:48:52 -0700208/*
209 * Return request in fuse_file->reserved_req. However that may
210 * currently be in use. If that is the case, wait for it to become
211 * available.
212 */
213static struct fuse_req *get_reserved_req(struct fuse_conn *fc,
214 struct file *file)
215{
216 struct fuse_req *req = NULL;
217 struct fuse_file *ff = file->private_data;
218
219 do {
Miklos Szeredide5e3de2007-10-16 23:31:00 -0700220 wait_event(fc->reserved_req_waitq, ff->reserved_req);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700221 spin_lock(&fc->lock);
222 if (ff->reserved_req) {
223 req = ff->reserved_req;
224 ff->reserved_req = NULL;
Al Virocb0942b2012-08-27 14:48:26 -0400225 req->stolen_file = get_file(file);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700226 }
227 spin_unlock(&fc->lock);
228 } while (!req);
229
230 return req;
231}
232
233/*
234 * Put stolen request back into fuse_file->reserved_req
235 */
236static void put_reserved_req(struct fuse_conn *fc, struct fuse_req *req)
237{
238 struct file *file = req->stolen_file;
239 struct fuse_file *ff = file->private_data;
240
241 spin_lock(&fc->lock);
Maxim Patlasovb2430d72012-10-26 19:49:24 +0400242 fuse_request_init(req, req->pages, req->page_descs, req->max_pages);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700243 BUG_ON(ff->reserved_req);
244 ff->reserved_req = req;
Miklos Szeredide5e3de2007-10-16 23:31:00 -0700245 wake_up_all(&fc->reserved_req_waitq);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700246 spin_unlock(&fc->lock);
247 fput(file);
248}
249
250/*
251 * Gets a requests for a file operation, always succeeds
252 *
253 * This is used for sending the FLUSH request, which must get to
254 * userspace, due to POSIX locks which may need to be unlocked.
255 *
256 * If allocation fails due to OOM, use the reserved request in
257 * fuse_file.
258 *
259 * This is very unlikely to deadlock accidentally, since the
260 * filesystem should not have it's own file open. If deadlock is
261 * intentional, it can still be broken by "aborting" the filesystem.
262 */
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400263struct fuse_req *fuse_get_req_nofail_nopages(struct fuse_conn *fc,
264 struct file *file)
Miklos Szeredi33649c92006-06-25 05:48:52 -0700265{
266 struct fuse_req *req;
267
268 atomic_inc(&fc->num_waiting);
Maxim Patlasov0aada882013-03-21 18:02:28 +0400269 wait_event(fc->blocked_waitq, fc->initialized);
Miklos Szeredi9759bd512015-01-06 10:45:35 +0100270 /* Matches smp_wmb() in fuse_set_initialized() */
271 smp_rmb();
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400272 req = fuse_request_alloc(0);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700273 if (!req)
274 req = get_reserved_req(fc, file);
275
276 fuse_req_init_context(req);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200277 __set_bit(FR_WAITING, &req->flags);
278 __clear_bit(FR_BACKGROUND, &req->flags);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700279 return req;
280}
281
Miklos Szeredi334f4852005-09-09 13:10:27 -0700282void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req)
283{
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800284 if (atomic_dec_and_test(&req->count)) {
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200285 if (test_bit(FR_BACKGROUND, &req->flags)) {
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400286 /*
287 * We get here in the unlikely case that a background
288 * request was allocated but not sent
289 */
290 spin_lock(&fc->lock);
291 if (!fc->blocked)
292 wake_up(&fc->blocked_waitq);
293 spin_unlock(&fc->lock);
294 }
295
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200296 if (test_bit(FR_WAITING, &req->flags)) {
297 __clear_bit(FR_WAITING, &req->flags);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200298 atomic_dec(&fc->num_waiting);
Miklos Szeredi73e0e732015-07-01 16:25:56 +0200299 }
Miklos Szeredi33649c92006-06-25 05:48:52 -0700300
301 if (req->stolen_file)
302 put_reserved_req(fc, req);
303 else
304 fuse_request_free(req);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800305 }
306}
Tejun Heo08cbf542009-04-14 10:54:53 +0900307EXPORT_SYMBOL_GPL(fuse_put_request);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800308
Miklos Szeredid12def12008-02-06 01:38:39 -0800309static unsigned len_args(unsigned numargs, struct fuse_arg *args)
310{
311 unsigned nbytes = 0;
312 unsigned i;
313
314 for (i = 0; i < numargs; i++)
315 nbytes += args[i].size;
316
317 return nbytes;
318}
319
320static u64 fuse_get_unique(struct fuse_conn *fc)
321{
Miklos Szeredi7d2e0a02015-07-01 16:26:00 +0200322 return ++fc->reqctr;
Miklos Szeredid12def12008-02-06 01:38:39 -0800323}
324
325static void queue_request(struct fuse_conn *fc, struct fuse_req *req)
326{
Miklos Szeredid12def12008-02-06 01:38:39 -0800327 req->in.h.len = sizeof(struct fuse_in_header) +
328 len_args(req->in.numargs, (struct fuse_arg *) req->in.args);
329 list_add_tail(&req->list, &fc->pending);
Miklos Szeredid12def12008-02-06 01:38:39 -0800330 wake_up(&fc->waitq);
331 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
332}
333
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100334void fuse_queue_forget(struct fuse_conn *fc, struct fuse_forget_link *forget,
335 u64 nodeid, u64 nlookup)
336{
Miklos Szeredi02c048b2010-12-07 20:16:56 +0100337 forget->forget_one.nodeid = nodeid;
338 forget->forget_one.nlookup = nlookup;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100339
340 spin_lock(&fc->lock);
Miklos Szeredi5dfcc872011-09-12 09:38:03 +0200341 if (fc->connected) {
342 fc->forget_list_tail->next = forget;
343 fc->forget_list_tail = forget;
344 wake_up(&fc->waitq);
345 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
346 } else {
347 kfree(forget);
348 }
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100349 spin_unlock(&fc->lock);
350}
351
Miklos Szeredid12def12008-02-06 01:38:39 -0800352static void flush_bg_queue(struct fuse_conn *fc)
353{
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700354 while (fc->active_background < fc->max_background &&
Miklos Szeredid12def12008-02-06 01:38:39 -0800355 !list_empty(&fc->bg_queue)) {
356 struct fuse_req *req;
357
358 req = list_entry(fc->bg_queue.next, struct fuse_req, list);
359 list_del(&req->list);
360 fc->active_background++;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200361 req->in.h.unique = fuse_get_unique(fc);
Miklos Szeredid12def12008-02-06 01:38:39 -0800362 queue_request(fc, req);
363 }
364}
365
Miklos Szeredi6dbbcb12006-04-26 10:49:06 +0200366/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700367 * This function is called when a request is finished. Either a reply
Miklos Szeredif9a28422006-06-25 05:48:53 -0700368 * has arrived or it was aborted (and not yet sent) or some error
Miklos Szeredif43b1552006-01-16 22:14:26 -0800369 * occurred during communication with userspace, or the device file
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700370 * was closed. The requester thread is woken up (if still waiting),
371 * the 'end' callback is called if given, else the reference to the
372 * request is released
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800373 *
Miklos Szeredid7133112006-04-10 22:54:55 -0700374 * Called with fc->lock, unlocks it
Miklos Szeredi334f4852005-09-09 13:10:27 -0700375 */
376static void request_end(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +0200377__releases(fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700378{
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700379 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
380 req->end = NULL;
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200381 list_del_init(&req->list);
382 list_del_init(&req->intr_entry);
Miklos Szeredic4775262015-07-01 16:26:00 +0200383 smp_wmb();
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800384 req->state = FUSE_REQ_FINISHED;
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200385 if (test_bit(FR_BACKGROUND, &req->flags)) {
386 clear_bit(FR_BACKGROUND, &req->flags);
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400387 if (fc->num_background == fc->max_background)
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700388 fc->blocked = 0;
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400389
390 /* Wake up next waiter, if any */
Miklos Szeredi3c18ef82013-04-17 21:50:58 +0200391 if (!fc->blocked && waitqueue_active(&fc->blocked_waitq))
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400392 wake_up(&fc->blocked_waitq);
393
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700394 if (fc->num_background == fc->congestion_threshold &&
Tejun Heoa325f9b2009-04-14 10:54:52 +0900395 fc->connected && fc->bdi_initialized) {
Jens Axboe8aa7e842009-07-09 14:52:32 +0200396 clear_bdi_congested(&fc->bdi, BLK_RW_SYNC);
397 clear_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
Miklos Szeredif92b99b2007-10-16 23:30:59 -0700398 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700399 fc->num_background--;
Miklos Szeredid12def12008-02-06 01:38:39 -0800400 fc->active_background--;
401 flush_bg_queue(fc);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700402 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700403 spin_unlock(&fc->lock);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700404 wake_up(&req->waitq);
405 if (end)
406 end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100407 fuse_put_request(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700408}
409
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700410static void queue_interrupt(struct fuse_conn *fc, struct fuse_req *req)
411{
412 list_add_tail(&req->intr_entry, &fc->interrupts);
413 wake_up(&fc->waitq);
414 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
415}
416
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700417static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700418{
Miklos Szeredic4775262015-07-01 16:26:00 +0200419 int err;
420
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700421 if (!fc->no_interrupt) {
422 /* Any signal may interrupt this */
Miklos Szeredic4775262015-07-01 16:26:00 +0200423 err = wait_event_interruptible(req->waitq,
424 req->state == FUSE_REQ_FINISHED);
425 if (!err)
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700426 return;
427
Miklos Szeredic4775262015-07-01 16:26:00 +0200428 spin_lock(&fc->lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200429 set_bit(FR_INTERRUPTED, &req->flags);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700430 if (req->state == FUSE_REQ_SENT)
431 queue_interrupt(fc, req);
Miklos Szeredic4775262015-07-01 16:26:00 +0200432 spin_unlock(&fc->lock);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700433 }
434
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200435 if (!test_bit(FR_FORCE, &req->flags)) {
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700436 sigset_t oldset;
437
438 /* Only fatal signals may interrupt this */
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700439 block_sigs(&oldset);
Miklos Szeredic4775262015-07-01 16:26:00 +0200440 err = wait_event_interruptible(req->waitq,
441 req->state == FUSE_REQ_FINISHED);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700442 restore_sigs(&oldset);
Miklos Szeredia131de02007-10-16 23:31:04 -0700443
Miklos Szeredic4775262015-07-01 16:26:00 +0200444 if (!err)
Miklos Szeredia131de02007-10-16 23:31:04 -0700445 return;
446
Miklos Szeredic4775262015-07-01 16:26:00 +0200447 spin_lock(&fc->lock);
Miklos Szeredia131de02007-10-16 23:31:04 -0700448 /* Request is not yet in userspace, bail out */
449 if (req->state == FUSE_REQ_PENDING) {
450 list_del(&req->list);
Miklos Szeredic4775262015-07-01 16:26:00 +0200451 spin_unlock(&fc->lock);
Miklos Szeredia131de02007-10-16 23:31:04 -0700452 __fuse_put_request(req);
453 req->out.h.error = -EINTR;
454 return;
455 }
Miklos Szeredic4775262015-07-01 16:26:00 +0200456 spin_unlock(&fc->lock);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700457 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700458
Miklos Szeredia131de02007-10-16 23:31:04 -0700459 /*
460 * Either request is already in userspace, or it was forced.
461 * Wait it out.
462 */
Miklos Szeredia131de02007-10-16 23:31:04 -0700463 wait_event(req->waitq, req->state == FUSE_REQ_FINISHED);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700464}
465
Eric Wong6a4e9222013-02-04 13:04:44 +0000466static void __fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700467{
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200468 BUG_ON(test_bit(FR_BACKGROUND, &req->flags));
Miklos Szeredid7133112006-04-10 22:54:55 -0700469 spin_lock(&fc->lock);
Miklos Szeredic4775262015-07-01 16:26:00 +0200470 if (!fc->connected) {
471 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700472 req->out.h.error = -ENOTCONN;
Miklos Szeredic4775262015-07-01 16:26:00 +0200473 } else {
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200474 req->in.h.unique = fuse_get_unique(fc);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700475 queue_request(fc, req);
476 /* acquire extra reference, since request is still needed
477 after request_end() */
478 __fuse_get_request(req);
Miklos Szeredic4775262015-07-01 16:26:00 +0200479 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700480
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700481 request_wait_answer(fc, req);
Miklos Szeredic4775262015-07-01 16:26:00 +0200482 /* Pairs with smp_wmb() in request_end() */
483 smp_rmb();
Miklos Szeredi334f4852005-09-09 13:10:27 -0700484 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700485}
Eric Wong6a4e9222013-02-04 13:04:44 +0000486
487void fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
488{
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200489 __set_bit(FR_ISREPLY, &req->flags);
490 if (!test_bit(FR_WAITING, &req->flags)) {
491 __set_bit(FR_WAITING, &req->flags);
Miklos Szeredi5437f242015-07-01 16:25:56 +0200492 atomic_inc(&fc->num_waiting);
493 }
Eric Wong6a4e9222013-02-04 13:04:44 +0000494 __fuse_request_send(fc, req);
495}
Tejun Heo08cbf542009-04-14 10:54:53 +0900496EXPORT_SYMBOL_GPL(fuse_request_send);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700497
Miklos Szeredi21f62172015-01-06 10:45:35 +0100498static void fuse_adjust_compat(struct fuse_conn *fc, struct fuse_args *args)
499{
500 if (fc->minor < 4 && args->in.h.opcode == FUSE_STATFS)
501 args->out.args[0].size = FUSE_COMPAT_STATFS_SIZE;
502
503 if (fc->minor < 9) {
504 switch (args->in.h.opcode) {
505 case FUSE_LOOKUP:
506 case FUSE_CREATE:
507 case FUSE_MKNOD:
508 case FUSE_MKDIR:
509 case FUSE_SYMLINK:
510 case FUSE_LINK:
511 args->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
512 break;
513 case FUSE_GETATTR:
514 case FUSE_SETATTR:
515 args->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
516 break;
517 }
518 }
519 if (fc->minor < 12) {
520 switch (args->in.h.opcode) {
521 case FUSE_CREATE:
522 args->in.args[0].size = sizeof(struct fuse_open_in);
523 break;
524 case FUSE_MKNOD:
525 args->in.args[0].size = FUSE_COMPAT_MKNOD_IN_SIZE;
526 break;
527 }
528 }
529}
530
Miklos Szeredi70781872014-12-12 09:49:05 +0100531ssize_t fuse_simple_request(struct fuse_conn *fc, struct fuse_args *args)
532{
533 struct fuse_req *req;
534 ssize_t ret;
535
536 req = fuse_get_req(fc, 0);
537 if (IS_ERR(req))
538 return PTR_ERR(req);
539
Miklos Szeredi21f62172015-01-06 10:45:35 +0100540 /* Needs to be done after fuse_get_req() so that fc->minor is valid */
541 fuse_adjust_compat(fc, args);
542
Miklos Szeredi70781872014-12-12 09:49:05 +0100543 req->in.h.opcode = args->in.h.opcode;
544 req->in.h.nodeid = args->in.h.nodeid;
545 req->in.numargs = args->in.numargs;
546 memcpy(req->in.args, args->in.args,
547 args->in.numargs * sizeof(struct fuse_in_arg));
548 req->out.argvar = args->out.argvar;
549 req->out.numargs = args->out.numargs;
550 memcpy(req->out.args, args->out.args,
551 args->out.numargs * sizeof(struct fuse_arg));
552 fuse_request_send(fc, req);
553 ret = req->out.h.error;
554 if (!ret && args->out.argvar) {
555 BUG_ON(args->out.numargs != 1);
556 ret = req->out.args[0].size;
557 }
558 fuse_put_request(fc, req);
559
560 return ret;
561}
562
Miklos Szeredif0139aa2015-07-01 16:25:57 +0200563/*
564 * Called under fc->lock
565 *
566 * fc->connected must have been checked previously
567 */
568void fuse_request_send_background_locked(struct fuse_conn *fc,
569 struct fuse_req *req)
Miklos Szeredid12def12008-02-06 01:38:39 -0800570{
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200571 BUG_ON(!test_bit(FR_BACKGROUND, &req->flags));
572 if (!test_bit(FR_WAITING, &req->flags)) {
573 __set_bit(FR_WAITING, &req->flags);
Miklos Szeredi5437f242015-07-01 16:25:56 +0200574 atomic_inc(&fc->num_waiting);
575 }
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200576 __set_bit(FR_ISREPLY, &req->flags);
Miklos Szeredid12def12008-02-06 01:38:39 -0800577 fc->num_background++;
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700578 if (fc->num_background == fc->max_background)
Miklos Szeredid12def12008-02-06 01:38:39 -0800579 fc->blocked = 1;
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700580 if (fc->num_background == fc->congestion_threshold &&
Tejun Heoa325f9b2009-04-14 10:54:52 +0900581 fc->bdi_initialized) {
Jens Axboe8aa7e842009-07-09 14:52:32 +0200582 set_bdi_congested(&fc->bdi, BLK_RW_SYNC);
583 set_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
Miklos Szeredid12def12008-02-06 01:38:39 -0800584 }
585 list_add_tail(&req->list, &fc->bg_queue);
586 flush_bg_queue(fc);
587}
588
Miklos Szeredif0139aa2015-07-01 16:25:57 +0200589void fuse_request_send_background(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700590{
Miklos Szeredi42dc6212015-07-01 16:25:56 +0200591 BUG_ON(!req->end);
Miklos Szeredid7133112006-04-10 22:54:55 -0700592 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700593 if (fc->connected) {
Miklos Szeredif0139aa2015-07-01 16:25:57 +0200594 fuse_request_send_background_locked(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700595 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700596 } else {
Miklos Szeredi42dc6212015-07-01 16:25:56 +0200597 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700598 req->out.h.error = -ENOTCONN;
Miklos Szeredi42dc6212015-07-01 16:25:56 +0200599 req->end(fc, req);
600 fuse_put_request(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700601 }
602}
Tejun Heo08cbf542009-04-14 10:54:53 +0900603EXPORT_SYMBOL_GPL(fuse_request_send_background);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700604
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200605static int fuse_request_send_notify_reply(struct fuse_conn *fc,
606 struct fuse_req *req, u64 unique)
607{
608 int err = -ENODEV;
609
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200610 __clear_bit(FR_ISREPLY, &req->flags);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200611 req->in.h.unique = unique;
612 spin_lock(&fc->lock);
613 if (fc->connected) {
614 queue_request(fc, req);
615 err = 0;
616 }
617 spin_unlock(&fc->lock);
618
619 return err;
620}
621
Anand V. Avati0b05b182012-08-19 08:53:23 -0400622void fuse_force_forget(struct file *file, u64 nodeid)
623{
Al Viro6131ffa2013-02-27 16:59:05 -0500624 struct inode *inode = file_inode(file);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400625 struct fuse_conn *fc = get_fuse_conn(inode);
626 struct fuse_req *req;
627 struct fuse_forget_in inarg;
628
629 memset(&inarg, 0, sizeof(inarg));
630 inarg.nlookup = 1;
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400631 req = fuse_get_req_nofail_nopages(fc, file);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400632 req->in.h.opcode = FUSE_FORGET;
633 req->in.h.nodeid = nodeid;
634 req->in.numargs = 1;
635 req->in.args[0].size = sizeof(inarg);
636 req->in.args[0].value = &inarg;
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200637 __clear_bit(FR_ISREPLY, &req->flags);
Eric Wong6a4e9222013-02-04 13:04:44 +0000638 __fuse_request_send(fc, req);
639 /* ignore errors */
640 fuse_put_request(fc, req);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400641}
642
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700643/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700644 * Lock the request. Up to the next unlock_request() there mustn't be
645 * anything that could cause a page-fault. If the request was already
Miklos Szeredif9a28422006-06-25 05:48:53 -0700646 * aborted bail out.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700647 */
Miklos Szeredidc008092015-07-01 16:25:58 +0200648static int lock_request(struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700649{
650 int err = 0;
651 if (req) {
Miklos Szeredidc008092015-07-01 16:25:58 +0200652 spin_lock(&req->waitq.lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200653 if (test_bit(FR_ABORTED, &req->flags))
Miklos Szeredi334f4852005-09-09 13:10:27 -0700654 err = -ENOENT;
655 else
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200656 set_bit(FR_LOCKED, &req->flags);
Miklos Szeredidc008092015-07-01 16:25:58 +0200657 spin_unlock(&req->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700658 }
659 return err;
660}
661
662/*
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200663 * Unlock request. If it was aborted while locked, caller is responsible
664 * for unlocking and ending the request.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700665 */
Miklos Szeredidc008092015-07-01 16:25:58 +0200666static int unlock_request(struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700667{
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200668 int err = 0;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700669 if (req) {
Miklos Szeredidc008092015-07-01 16:25:58 +0200670 spin_lock(&req->waitq.lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200671 if (test_bit(FR_ABORTED, &req->flags))
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200672 err = -ENOENT;
673 else
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200674 clear_bit(FR_LOCKED, &req->flags);
Miklos Szeredidc008092015-07-01 16:25:58 +0200675 spin_unlock(&req->waitq.lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700676 }
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200677 return err;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700678}
679
680struct fuse_copy_state {
681 int write;
682 struct fuse_req *req;
Al Viro6c09e942015-04-03 22:06:08 -0400683 struct iov_iter *iter;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200684 struct pipe_buffer *pipebufs;
685 struct pipe_buffer *currbuf;
686 struct pipe_inode_info *pipe;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700687 unsigned long nr_segs;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700688 struct page *pg;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700689 unsigned len;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200690 unsigned offset;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200691 unsigned move_pages:1;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700692};
693
Miklos Szeredidc008092015-07-01 16:25:58 +0200694static void fuse_copy_init(struct fuse_copy_state *cs, int write,
Al Viro6c09e942015-04-03 22:06:08 -0400695 struct iov_iter *iter)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700696{
697 memset(cs, 0, sizeof(*cs));
698 cs->write = write;
Al Viro6c09e942015-04-03 22:06:08 -0400699 cs->iter = iter;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700700}
701
702/* Unmap and put previous page of userspace buffer */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800703static void fuse_copy_finish(struct fuse_copy_state *cs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700704{
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200705 if (cs->currbuf) {
706 struct pipe_buffer *buf = cs->currbuf;
707
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200708 if (cs->write)
Miklos Szeredic3021622010-05-25 15:06:07 +0200709 buf->len = PAGE_SIZE - cs->len;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200710 cs->currbuf = NULL;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200711 } else if (cs->pg) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700712 if (cs->write) {
713 flush_dcache_page(cs->pg);
714 set_page_dirty_lock(cs->pg);
715 }
716 put_page(cs->pg);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700717 }
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200718 cs->pg = NULL;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700719}
720
721/*
722 * Get another pagefull of userspace buffer, and map it to kernel
723 * address space, and lock request
724 */
725static int fuse_copy_fill(struct fuse_copy_state *cs)
726{
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200727 struct page *page;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700728 int err;
729
Miklos Szeredidc008092015-07-01 16:25:58 +0200730 err = unlock_request(cs->req);
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200731 if (err)
732 return err;
733
Miklos Szeredi334f4852005-09-09 13:10:27 -0700734 fuse_copy_finish(cs);
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200735 if (cs->pipebufs) {
736 struct pipe_buffer *buf = cs->pipebufs;
737
Miklos Szeredic3021622010-05-25 15:06:07 +0200738 if (!cs->write) {
739 err = buf->ops->confirm(cs->pipe, buf);
740 if (err)
741 return err;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200742
Miklos Szeredic3021622010-05-25 15:06:07 +0200743 BUG_ON(!cs->nr_segs);
744 cs->currbuf = buf;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200745 cs->pg = buf->page;
746 cs->offset = buf->offset;
Miklos Szeredic3021622010-05-25 15:06:07 +0200747 cs->len = buf->len;
Miklos Szeredic3021622010-05-25 15:06:07 +0200748 cs->pipebufs++;
749 cs->nr_segs--;
750 } else {
Miklos Szeredic3021622010-05-25 15:06:07 +0200751 if (cs->nr_segs == cs->pipe->buffers)
752 return -EIO;
753
754 page = alloc_page(GFP_HIGHUSER);
755 if (!page)
756 return -ENOMEM;
757
758 buf->page = page;
759 buf->offset = 0;
760 buf->len = 0;
761
762 cs->currbuf = buf;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200763 cs->pg = page;
764 cs->offset = 0;
Miklos Szeredic3021622010-05-25 15:06:07 +0200765 cs->len = PAGE_SIZE;
766 cs->pipebufs++;
767 cs->nr_segs++;
768 }
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200769 } else {
Al Viro6c09e942015-04-03 22:06:08 -0400770 size_t off;
771 err = iov_iter_get_pages(cs->iter, &page, PAGE_SIZE, 1, &off);
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200772 if (err < 0)
773 return err;
Al Viro6c09e942015-04-03 22:06:08 -0400774 BUG_ON(!err);
775 cs->len = err;
776 cs->offset = off;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200777 cs->pg = page;
Al Viro6c09e942015-04-03 22:06:08 -0400778 cs->offset = off;
779 iov_iter_advance(cs->iter, err);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700780 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700781
Miklos Szeredidc008092015-07-01 16:25:58 +0200782 return lock_request(cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700783}
784
785/* Do as much copy to/from userspace buffer as we can */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800786static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700787{
788 unsigned ncpy = min(*size, cs->len);
789 if (val) {
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200790 void *pgaddr = kmap_atomic(cs->pg);
791 void *buf = pgaddr + cs->offset;
792
Miklos Szeredi334f4852005-09-09 13:10:27 -0700793 if (cs->write)
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200794 memcpy(buf, *val, ncpy);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700795 else
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200796 memcpy(*val, buf, ncpy);
797
798 kunmap_atomic(pgaddr);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700799 *val += ncpy;
800 }
801 *size -= ncpy;
802 cs->len -= ncpy;
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200803 cs->offset += ncpy;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700804 return ncpy;
805}
806
Miklos Szeredice534fb2010-05-25 15:06:07 +0200807static int fuse_check_page(struct page *page)
808{
809 if (page_mapcount(page) ||
810 page->mapping != NULL ||
811 page_count(page) != 1 ||
812 (page->flags & PAGE_FLAGS_CHECK_AT_PREP &
813 ~(1 << PG_locked |
814 1 << PG_referenced |
815 1 << PG_uptodate |
816 1 << PG_lru |
817 1 << PG_active |
818 1 << PG_reclaim))) {
819 printk(KERN_WARNING "fuse: trying to steal weird page\n");
820 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);
821 return 1;
822 }
823 return 0;
824}
825
826static int fuse_try_move_page(struct fuse_copy_state *cs, struct page **pagep)
827{
828 int err;
829 struct page *oldpage = *pagep;
830 struct page *newpage;
831 struct pipe_buffer *buf = cs->pipebufs;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200832
Miklos Szeredidc008092015-07-01 16:25:58 +0200833 err = unlock_request(cs->req);
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200834 if (err)
835 return err;
836
Miklos Szeredice534fb2010-05-25 15:06:07 +0200837 fuse_copy_finish(cs);
838
839 err = buf->ops->confirm(cs->pipe, buf);
840 if (err)
841 return err;
842
843 BUG_ON(!cs->nr_segs);
844 cs->currbuf = buf;
845 cs->len = buf->len;
846 cs->pipebufs++;
847 cs->nr_segs--;
848
849 if (cs->len != PAGE_SIZE)
850 goto out_fallback;
851
852 if (buf->ops->steal(cs->pipe, buf) != 0)
853 goto out_fallback;
854
855 newpage = buf->page;
856
Miklos Szerediaa991b32015-02-26 11:45:47 +0100857 if (!PageUptodate(newpage))
858 SetPageUptodate(newpage);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200859
860 ClearPageMappedToDisk(newpage);
861
862 if (fuse_check_page(newpage) != 0)
863 goto out_fallback_unlock;
864
Miklos Szeredice534fb2010-05-25 15:06:07 +0200865 /*
866 * This is a new and locked page, it shouldn't be mapped or
867 * have any special flags on it
868 */
869 if (WARN_ON(page_mapped(oldpage)))
870 goto out_fallback_unlock;
871 if (WARN_ON(page_has_private(oldpage)))
872 goto out_fallback_unlock;
873 if (WARN_ON(PageDirty(oldpage) || PageWriteback(oldpage)))
874 goto out_fallback_unlock;
875 if (WARN_ON(PageMlocked(oldpage)))
876 goto out_fallback_unlock;
877
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700878 err = replace_page_cache_page(oldpage, newpage, GFP_KERNEL);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200879 if (err) {
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700880 unlock_page(newpage);
881 return err;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200882 }
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700883
Miklos Szeredice534fb2010-05-25 15:06:07 +0200884 page_cache_get(newpage);
885
886 if (!(buf->flags & PIPE_BUF_FLAG_LRU))
887 lru_cache_add_file(newpage);
888
889 err = 0;
Miklos Szeredidc008092015-07-01 16:25:58 +0200890 spin_lock(&cs->req->waitq.lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +0200891 if (test_bit(FR_ABORTED, &cs->req->flags))
Miklos Szeredice534fb2010-05-25 15:06:07 +0200892 err = -ENOENT;
893 else
894 *pagep = newpage;
Miklos Szeredidc008092015-07-01 16:25:58 +0200895 spin_unlock(&cs->req->waitq.lock);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200896
897 if (err) {
898 unlock_page(newpage);
899 page_cache_release(newpage);
900 return err;
901 }
902
903 unlock_page(oldpage);
904 page_cache_release(oldpage);
905 cs->len = 0;
906
907 return 0;
908
909out_fallback_unlock:
910 unlock_page(newpage);
911out_fallback:
Miklos Szeredic55a01d2014-07-07 15:28:51 +0200912 cs->pg = buf->page;
913 cs->offset = buf->offset;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200914
Miklos Szeredidc008092015-07-01 16:25:58 +0200915 err = lock_request(cs->req);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200916 if (err)
917 return err;
918
919 return 1;
920}
921
Miklos Szeredic3021622010-05-25 15:06:07 +0200922static int fuse_ref_page(struct fuse_copy_state *cs, struct page *page,
923 unsigned offset, unsigned count)
924{
925 struct pipe_buffer *buf;
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200926 int err;
Miklos Szeredic3021622010-05-25 15:06:07 +0200927
928 if (cs->nr_segs == cs->pipe->buffers)
929 return -EIO;
930
Miklos Szeredidc008092015-07-01 16:25:58 +0200931 err = unlock_request(cs->req);
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +0200932 if (err)
933 return err;
934
Miklos Szeredic3021622010-05-25 15:06:07 +0200935 fuse_copy_finish(cs);
936
937 buf = cs->pipebufs;
938 page_cache_get(page);
939 buf->page = page;
940 buf->offset = offset;
941 buf->len = count;
942
943 cs->pipebufs++;
944 cs->nr_segs++;
945 cs->len = 0;
946
947 return 0;
948}
949
Miklos Szeredi334f4852005-09-09 13:10:27 -0700950/*
951 * Copy a page in the request to/from the userspace buffer. Must be
952 * done atomically
953 */
Miklos Szeredice534fb2010-05-25 15:06:07 +0200954static int fuse_copy_page(struct fuse_copy_state *cs, struct page **pagep,
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800955 unsigned offset, unsigned count, int zeroing)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700956{
Miklos Szeredice534fb2010-05-25 15:06:07 +0200957 int err;
958 struct page *page = *pagep;
959
Miklos Szeredib6777c42010-10-26 14:22:27 -0700960 if (page && zeroing && count < PAGE_SIZE)
961 clear_highpage(page);
962
Miklos Szeredi334f4852005-09-09 13:10:27 -0700963 while (count) {
Miklos Szeredic3021622010-05-25 15:06:07 +0200964 if (cs->write && cs->pipebufs && page) {
965 return fuse_ref_page(cs, page, offset, count);
966 } else if (!cs->len) {
Miklos Szeredice534fb2010-05-25 15:06:07 +0200967 if (cs->move_pages && page &&
968 offset == 0 && count == PAGE_SIZE) {
969 err = fuse_try_move_page(cs, pagep);
970 if (err <= 0)
971 return err;
972 } else {
973 err = fuse_copy_fill(cs);
974 if (err)
975 return err;
976 }
Miklos Szeredi1729a162008-11-26 12:03:54 +0100977 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700978 if (page) {
Cong Wang2408f6e2011-11-25 23:14:30 +0800979 void *mapaddr = kmap_atomic(page);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700980 void *buf = mapaddr + offset;
981 offset += fuse_copy_do(cs, &buf, &count);
Cong Wang2408f6e2011-11-25 23:14:30 +0800982 kunmap_atomic(mapaddr);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700983 } else
984 offset += fuse_copy_do(cs, NULL, &count);
985 }
986 if (page && !cs->write)
987 flush_dcache_page(page);
988 return 0;
989}
990
991/* Copy pages in the request to/from userspace buffer */
992static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
993 int zeroing)
994{
995 unsigned i;
996 struct fuse_req *req = cs->req;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700997
998 for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
Miklos Szeredice534fb2010-05-25 15:06:07 +0200999 int err;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001000 unsigned offset = req->page_descs[i].offset;
1001 unsigned count = min(nbytes, req->page_descs[i].length);
Miklos Szeredice534fb2010-05-25 15:06:07 +02001002
1003 err = fuse_copy_page(cs, &req->pages[i], offset, count,
1004 zeroing);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001005 if (err)
1006 return err;
1007
1008 nbytes -= count;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001009 }
1010 return 0;
1011}
1012
1013/* Copy a single argument in the request to/from userspace buffer */
1014static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
1015{
1016 while (size) {
Miklos Szeredi1729a162008-11-26 12:03:54 +01001017 if (!cs->len) {
1018 int err = fuse_copy_fill(cs);
1019 if (err)
1020 return err;
1021 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001022 fuse_copy_do(cs, &val, &size);
1023 }
1024 return 0;
1025}
1026
1027/* Copy request arguments to/from userspace buffer */
1028static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
1029 unsigned argpages, struct fuse_arg *args,
1030 int zeroing)
1031{
1032 int err = 0;
1033 unsigned i;
1034
1035 for (i = 0; !err && i < numargs; i++) {
1036 struct fuse_arg *arg = &args[i];
1037 if (i == numargs - 1 && argpages)
1038 err = fuse_copy_pages(cs, arg->size, zeroing);
1039 else
1040 err = fuse_copy_one(cs, arg->value, arg->size);
1041 }
1042 return err;
1043}
1044
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001045static int forget_pending(struct fuse_conn *fc)
1046{
1047 return fc->forget_list_head.next != NULL;
1048}
1049
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001050static int request_pending(struct fuse_conn *fc)
1051{
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001052 return !list_empty(&fc->pending) || !list_empty(&fc->interrupts) ||
1053 forget_pending(fc);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001054}
1055
Miklos Szeredi334f4852005-09-09 13:10:27 -07001056/* Wait until a request is available on the pending list */
1057static void request_wait(struct fuse_conn *fc)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001058__releases(fc->lock)
1059__acquires(fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001060{
1061 DECLARE_WAITQUEUE(wait, current);
1062
1063 add_wait_queue_exclusive(&fc->waitq, &wait);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001064 while (fc->connected && !request_pending(fc)) {
Miklos Szeredi334f4852005-09-09 13:10:27 -07001065 set_current_state(TASK_INTERRUPTIBLE);
1066 if (signal_pending(current))
1067 break;
1068
Miklos Szeredid7133112006-04-10 22:54:55 -07001069 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001070 schedule();
Miklos Szeredid7133112006-04-10 22:54:55 -07001071 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001072 }
1073 set_current_state(TASK_RUNNING);
1074 remove_wait_queue(&fc->waitq, &wait);
1075}
1076
1077/*
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001078 * Transfer an interrupt request to userspace
1079 *
1080 * Unlike other requests this is assembled on demand, without a need
1081 * to allocate a separate fuse_req structure.
1082 *
1083 * Called with fc->lock held, releases it
1084 */
Miklos Szeredic3021622010-05-25 15:06:07 +02001085static int fuse_read_interrupt(struct fuse_conn *fc, struct fuse_copy_state *cs,
1086 size_t nbytes, struct fuse_req *req)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001087__releases(fc->lock)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001088{
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001089 struct fuse_in_header ih;
1090 struct fuse_interrupt_in arg;
1091 unsigned reqsize = sizeof(ih) + sizeof(arg);
1092 int err;
1093
1094 list_del_init(&req->intr_entry);
1095 req->intr_unique = fuse_get_unique(fc);
1096 memset(&ih, 0, sizeof(ih));
1097 memset(&arg, 0, sizeof(arg));
1098 ih.len = reqsize;
1099 ih.opcode = FUSE_INTERRUPT;
1100 ih.unique = req->intr_unique;
1101 arg.unique = req->in.h.unique;
1102
1103 spin_unlock(&fc->lock);
Miklos Szeredic3021622010-05-25 15:06:07 +02001104 if (nbytes < reqsize)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001105 return -EINVAL;
1106
Miklos Szeredic3021622010-05-25 15:06:07 +02001107 err = fuse_copy_one(cs, &ih, sizeof(ih));
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001108 if (!err)
Miklos Szeredic3021622010-05-25 15:06:07 +02001109 err = fuse_copy_one(cs, &arg, sizeof(arg));
1110 fuse_copy_finish(cs);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001111
1112 return err ? err : reqsize;
1113}
1114
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001115static struct fuse_forget_link *dequeue_forget(struct fuse_conn *fc,
1116 unsigned max,
1117 unsigned *countp)
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001118{
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001119 struct fuse_forget_link *head = fc->forget_list_head.next;
1120 struct fuse_forget_link **newhead = &head;
1121 unsigned count;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001122
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001123 for (count = 0; *newhead != NULL && count < max; count++)
1124 newhead = &(*newhead)->next;
1125
1126 fc->forget_list_head.next = *newhead;
1127 *newhead = NULL;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001128 if (fc->forget_list_head.next == NULL)
1129 fc->forget_list_tail = &fc->forget_list_head;
1130
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001131 if (countp != NULL)
1132 *countp = count;
1133
1134 return head;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001135}
1136
1137static int fuse_read_single_forget(struct fuse_conn *fc,
1138 struct fuse_copy_state *cs,
1139 size_t nbytes)
1140__releases(fc->lock)
1141{
1142 int err;
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001143 struct fuse_forget_link *forget = dequeue_forget(fc, 1, NULL);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001144 struct fuse_forget_in arg = {
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001145 .nlookup = forget->forget_one.nlookup,
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001146 };
1147 struct fuse_in_header ih = {
1148 .opcode = FUSE_FORGET,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001149 .nodeid = forget->forget_one.nodeid,
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001150 .unique = fuse_get_unique(fc),
1151 .len = sizeof(ih) + sizeof(arg),
1152 };
1153
1154 spin_unlock(&fc->lock);
1155 kfree(forget);
1156 if (nbytes < ih.len)
1157 return -EINVAL;
1158
1159 err = fuse_copy_one(cs, &ih, sizeof(ih));
1160 if (!err)
1161 err = fuse_copy_one(cs, &arg, sizeof(arg));
1162 fuse_copy_finish(cs);
1163
1164 if (err)
1165 return err;
1166
1167 return ih.len;
1168}
1169
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001170static int fuse_read_batch_forget(struct fuse_conn *fc,
1171 struct fuse_copy_state *cs, size_t nbytes)
1172__releases(fc->lock)
1173{
1174 int err;
1175 unsigned max_forgets;
1176 unsigned count;
1177 struct fuse_forget_link *head;
1178 struct fuse_batch_forget_in arg = { .count = 0 };
1179 struct fuse_in_header ih = {
1180 .opcode = FUSE_BATCH_FORGET,
1181 .unique = fuse_get_unique(fc),
1182 .len = sizeof(ih) + sizeof(arg),
1183 };
1184
1185 if (nbytes < ih.len) {
1186 spin_unlock(&fc->lock);
1187 return -EINVAL;
1188 }
1189
1190 max_forgets = (nbytes - ih.len) / sizeof(struct fuse_forget_one);
1191 head = dequeue_forget(fc, max_forgets, &count);
1192 spin_unlock(&fc->lock);
1193
1194 arg.count = count;
1195 ih.len += count * sizeof(struct fuse_forget_one);
1196 err = fuse_copy_one(cs, &ih, sizeof(ih));
1197 if (!err)
1198 err = fuse_copy_one(cs, &arg, sizeof(arg));
1199
1200 while (head) {
1201 struct fuse_forget_link *forget = head;
1202
1203 if (!err) {
1204 err = fuse_copy_one(cs, &forget->forget_one,
1205 sizeof(forget->forget_one));
1206 }
1207 head = forget->next;
1208 kfree(forget);
1209 }
1210
1211 fuse_copy_finish(cs);
1212
1213 if (err)
1214 return err;
1215
1216 return ih.len;
1217}
1218
1219static int fuse_read_forget(struct fuse_conn *fc, struct fuse_copy_state *cs,
1220 size_t nbytes)
1221__releases(fc->lock)
1222{
1223 if (fc->minor < 16 || fc->forget_list_head.next->next == NULL)
1224 return fuse_read_single_forget(fc, cs, nbytes);
1225 else
1226 return fuse_read_batch_forget(fc, cs, nbytes);
1227}
1228
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001229/*
Miklos Szeredi334f4852005-09-09 13:10:27 -07001230 * Read a single request into the userspace filesystem's buffer. This
1231 * function waits until a request is available, then removes it from
1232 * the pending list and copies request data to userspace buffer. If
Miklos Szeredif9a28422006-06-25 05:48:53 -07001233 * no reply is needed (FORGET) or request has been aborted or there
1234 * was an error during the copying then it's finished by calling
Miklos Szeredi334f4852005-09-09 13:10:27 -07001235 * request_end(). Otherwise add it to the processing list, and set
1236 * the 'sent' flag.
1237 */
Miklos Szeredic3021622010-05-25 15:06:07 +02001238static ssize_t fuse_dev_do_read(struct fuse_conn *fc, struct file *file,
1239 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001240{
1241 int err;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001242 struct fuse_req *req;
1243 struct fuse_in *in;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001244 unsigned reqsize;
1245
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001246 restart:
Miklos Szeredid7133112006-04-10 22:54:55 -07001247 spin_lock(&fc->lock);
Jeff Dikee5ac1d12006-04-10 22:54:53 -07001248 err = -EAGAIN;
1249 if ((file->f_flags & O_NONBLOCK) && fc->connected &&
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001250 !request_pending(fc))
Jeff Dikee5ac1d12006-04-10 22:54:53 -07001251 goto err_unlock;
1252
Miklos Szeredi334f4852005-09-09 13:10:27 -07001253 request_wait(fc);
1254 err = -ENODEV;
Miklos Szeredi9ba7cbb2006-01-16 22:14:34 -08001255 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001256 goto err_unlock;
1257 err = -ERESTARTSYS;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001258 if (!request_pending(fc))
Miklos Szeredi334f4852005-09-09 13:10:27 -07001259 goto err_unlock;
1260
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001261 if (!list_empty(&fc->interrupts)) {
1262 req = list_entry(fc->interrupts.next, struct fuse_req,
1263 intr_entry);
Miklos Szeredic3021622010-05-25 15:06:07 +02001264 return fuse_read_interrupt(fc, cs, nbytes, req);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001265 }
1266
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001267 if (forget_pending(fc)) {
1268 if (list_empty(&fc->pending) || fc->forget_batch-- > 0)
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001269 return fuse_read_forget(fc, cs, nbytes);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001270
1271 if (fc->forget_batch <= -8)
1272 fc->forget_batch = 16;
1273 }
1274
Miklos Szeredi334f4852005-09-09 13:10:27 -07001275 req = list_entry(fc->pending.next, struct fuse_req, list);
Miklos Szeredi7a3b2c72015-07-01 16:26:00 +02001276 req->state = FUSE_REQ_IO;
Miklos Szeredid77a1d52006-01-16 22:14:31 -08001277 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001278
1279 in = &req->in;
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001280 reqsize = in->h.len;
1281 /* If request is too large, reply with an error and restart the read */
Miklos Szeredic3021622010-05-25 15:06:07 +02001282 if (nbytes < reqsize) {
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001283 req->out.h.error = -EIO;
1284 /* SETXATTR is special, since it may contain too large data */
1285 if (in->h.opcode == FUSE_SETXATTR)
1286 req->out.h.error = -E2BIG;
1287 request_end(fc, req);
1288 goto restart;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001289 }
Miklos Szeredid7133112006-04-10 22:54:55 -07001290 spin_unlock(&fc->lock);
Miklos Szeredic3021622010-05-25 15:06:07 +02001291 cs->req = req;
1292 err = fuse_copy_one(cs, &in->h, sizeof(in->h));
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001293 if (!err)
Miklos Szeredic3021622010-05-25 15:06:07 +02001294 err = fuse_copy_args(cs, in->numargs, in->argpages,
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001295 (struct fuse_arg *) in->args, 0);
Miklos Szeredic3021622010-05-25 15:06:07 +02001296 fuse_copy_finish(cs);
Miklos Szeredid7133112006-04-10 22:54:55 -07001297 spin_lock(&fc->lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001298 clear_bit(FR_LOCKED, &req->flags);
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +02001299 if (!fc->connected) {
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -07001300 request_end(fc, req);
1301 return -ENODEV;
1302 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001303 if (err) {
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -07001304 req->out.h.error = -EIO;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001305 request_end(fc, req);
1306 return err;
1307 }
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001308 if (!test_bit(FR_ISREPLY, &req->flags)) {
Miklos Szeredi334f4852005-09-09 13:10:27 -07001309 request_end(fc, req);
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001310 } else {
Miklos Szeredi83cfd492006-01-16 22:14:31 -08001311 req->state = FUSE_REQ_SENT;
Miklos Szeredid77a1d52006-01-16 22:14:31 -08001312 list_move_tail(&req->list, &fc->processing);
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001313 if (test_bit(FR_INTERRUPTED, &req->flags))
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001314 queue_interrupt(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -07001315 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001316 }
1317 return reqsize;
1318
1319 err_unlock:
Miklos Szeredid7133112006-04-10 22:54:55 -07001320 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001321 return err;
1322}
1323
Tom Van Braeckel94e4fe22015-01-12 05:22:16 +01001324static int fuse_dev_open(struct inode *inode, struct file *file)
1325{
1326 /*
1327 * The fuse device's file's private_data is used to hold
1328 * the fuse_conn(ection) when it is mounted, and is used to
1329 * keep track of whether the file has been mounted already.
1330 */
1331 file->private_data = NULL;
1332 return 0;
1333}
1334
Al Virofbdbacc2015-04-03 21:53:39 -04001335static ssize_t fuse_dev_read(struct kiocb *iocb, struct iov_iter *to)
Miklos Szeredic3021622010-05-25 15:06:07 +02001336{
1337 struct fuse_copy_state cs;
1338 struct file *file = iocb->ki_filp;
1339 struct fuse_conn *fc = fuse_get_conn(file);
1340 if (!fc)
1341 return -EPERM;
1342
Al Virofbdbacc2015-04-03 21:53:39 -04001343 if (!iter_is_iovec(to))
1344 return -EINVAL;
Miklos Szeredic3021622010-05-25 15:06:07 +02001345
Miklos Szeredidc008092015-07-01 16:25:58 +02001346 fuse_copy_init(&cs, 1, to);
Al Virofbdbacc2015-04-03 21:53:39 -04001347
1348 return fuse_dev_do_read(fc, file, &cs, iov_iter_count(to));
Miklos Szeredic3021622010-05-25 15:06:07 +02001349}
1350
Miklos Szeredic3021622010-05-25 15:06:07 +02001351static ssize_t fuse_dev_splice_read(struct file *in, loff_t *ppos,
1352 struct pipe_inode_info *pipe,
1353 size_t len, unsigned int flags)
1354{
1355 int ret;
1356 int page_nr = 0;
1357 int do_wakeup = 0;
1358 struct pipe_buffer *bufs;
1359 struct fuse_copy_state cs;
1360 struct fuse_conn *fc = fuse_get_conn(in);
1361 if (!fc)
1362 return -EPERM;
1363
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001364 bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), GFP_KERNEL);
Miklos Szeredic3021622010-05-25 15:06:07 +02001365 if (!bufs)
1366 return -ENOMEM;
1367
Miklos Szeredidc008092015-07-01 16:25:58 +02001368 fuse_copy_init(&cs, 1, NULL);
Miklos Szeredic3021622010-05-25 15:06:07 +02001369 cs.pipebufs = bufs;
1370 cs.pipe = pipe;
1371 ret = fuse_dev_do_read(fc, in, &cs, len);
1372 if (ret < 0)
1373 goto out;
1374
1375 ret = 0;
1376 pipe_lock(pipe);
1377
1378 if (!pipe->readers) {
1379 send_sig(SIGPIPE, current, 0);
1380 if (!ret)
1381 ret = -EPIPE;
1382 goto out_unlock;
1383 }
1384
1385 if (pipe->nrbufs + cs.nr_segs > pipe->buffers) {
1386 ret = -EIO;
1387 goto out_unlock;
1388 }
1389
1390 while (page_nr < cs.nr_segs) {
1391 int newbuf = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
1392 struct pipe_buffer *buf = pipe->bufs + newbuf;
1393
1394 buf->page = bufs[page_nr].page;
1395 buf->offset = bufs[page_nr].offset;
1396 buf->len = bufs[page_nr].len;
Miklos Szeredi28a625c2014-01-22 19:36:57 +01001397 /*
1398 * Need to be careful about this. Having buf->ops in module
1399 * code can Oops if the buffer persists after module unload.
1400 */
1401 buf->ops = &nosteal_pipe_buf_ops;
Miklos Szeredic3021622010-05-25 15:06:07 +02001402
1403 pipe->nrbufs++;
1404 page_nr++;
1405 ret += buf->len;
1406
Al Viro6447a3c2013-03-21 11:01:38 -04001407 if (pipe->files)
Miklos Szeredic3021622010-05-25 15:06:07 +02001408 do_wakeup = 1;
1409 }
1410
1411out_unlock:
1412 pipe_unlock(pipe);
1413
1414 if (do_wakeup) {
1415 smp_mb();
1416 if (waitqueue_active(&pipe->wait))
1417 wake_up_interruptible(&pipe->wait);
1418 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
1419 }
1420
1421out:
1422 for (; page_nr < cs.nr_segs; page_nr++)
1423 page_cache_release(bufs[page_nr].page);
1424
1425 kfree(bufs);
1426 return ret;
1427}
1428
Tejun Heo95668a62008-11-26 12:03:55 +01001429static int fuse_notify_poll(struct fuse_conn *fc, unsigned int size,
1430 struct fuse_copy_state *cs)
1431{
1432 struct fuse_notify_poll_wakeup_out outarg;
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001433 int err = -EINVAL;
Tejun Heo95668a62008-11-26 12:03:55 +01001434
1435 if (size != sizeof(outarg))
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001436 goto err;
Tejun Heo95668a62008-11-26 12:03:55 +01001437
1438 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1439 if (err)
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001440 goto err;
Tejun Heo95668a62008-11-26 12:03:55 +01001441
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001442 fuse_copy_finish(cs);
Tejun Heo95668a62008-11-26 12:03:55 +01001443 return fuse_notify_poll_wakeup(fc, &outarg);
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001444
1445err:
1446 fuse_copy_finish(cs);
1447 return err;
Tejun Heo95668a62008-11-26 12:03:55 +01001448}
1449
John Muir3b463ae2009-05-31 11:13:57 -04001450static int fuse_notify_inval_inode(struct fuse_conn *fc, unsigned int size,
1451 struct fuse_copy_state *cs)
1452{
1453 struct fuse_notify_inval_inode_out outarg;
1454 int err = -EINVAL;
1455
1456 if (size != sizeof(outarg))
1457 goto err;
1458
1459 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1460 if (err)
1461 goto err;
1462 fuse_copy_finish(cs);
1463
1464 down_read(&fc->killsb);
1465 err = -ENOENT;
Miklos Szeredib21dda42010-02-05 12:08:31 +01001466 if (fc->sb) {
1467 err = fuse_reverse_inval_inode(fc->sb, outarg.ino,
1468 outarg.off, outarg.len);
1469 }
John Muir3b463ae2009-05-31 11:13:57 -04001470 up_read(&fc->killsb);
1471 return err;
1472
1473err:
1474 fuse_copy_finish(cs);
1475 return err;
1476}
1477
1478static int fuse_notify_inval_entry(struct fuse_conn *fc, unsigned int size,
1479 struct fuse_copy_state *cs)
1480{
1481 struct fuse_notify_inval_entry_out outarg;
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001482 int err = -ENOMEM;
1483 char *buf;
John Muir3b463ae2009-05-31 11:13:57 -04001484 struct qstr name;
1485
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001486 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1487 if (!buf)
1488 goto err;
1489
1490 err = -EINVAL;
John Muir3b463ae2009-05-31 11:13:57 -04001491 if (size < sizeof(outarg))
1492 goto err;
1493
1494 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1495 if (err)
1496 goto err;
1497
1498 err = -ENAMETOOLONG;
1499 if (outarg.namelen > FUSE_NAME_MAX)
1500 goto err;
1501
Miklos Szeredic2183d12011-08-24 10:20:17 +02001502 err = -EINVAL;
1503 if (size != sizeof(outarg) + outarg.namelen + 1)
1504 goto err;
1505
John Muir3b463ae2009-05-31 11:13:57 -04001506 name.name = buf;
1507 name.len = outarg.namelen;
1508 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1509 if (err)
1510 goto err;
1511 fuse_copy_finish(cs);
1512 buf[outarg.namelen] = 0;
1513 name.hash = full_name_hash(name.name, name.len);
1514
1515 down_read(&fc->killsb);
1516 err = -ENOENT;
Miklos Szeredib21dda42010-02-05 12:08:31 +01001517 if (fc->sb)
John Muir451d0f52011-12-06 21:50:06 +01001518 err = fuse_reverse_inval_entry(fc->sb, outarg.parent, 0, &name);
1519 up_read(&fc->killsb);
1520 kfree(buf);
1521 return err;
1522
1523err:
1524 kfree(buf);
1525 fuse_copy_finish(cs);
1526 return err;
1527}
1528
1529static int fuse_notify_delete(struct fuse_conn *fc, unsigned int size,
1530 struct fuse_copy_state *cs)
1531{
1532 struct fuse_notify_delete_out outarg;
1533 int err = -ENOMEM;
1534 char *buf;
1535 struct qstr name;
1536
1537 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1538 if (!buf)
1539 goto err;
1540
1541 err = -EINVAL;
1542 if (size < sizeof(outarg))
1543 goto err;
1544
1545 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1546 if (err)
1547 goto err;
1548
1549 err = -ENAMETOOLONG;
1550 if (outarg.namelen > FUSE_NAME_MAX)
1551 goto err;
1552
1553 err = -EINVAL;
1554 if (size != sizeof(outarg) + outarg.namelen + 1)
1555 goto err;
1556
1557 name.name = buf;
1558 name.len = outarg.namelen;
1559 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1560 if (err)
1561 goto err;
1562 fuse_copy_finish(cs);
1563 buf[outarg.namelen] = 0;
1564 name.hash = full_name_hash(name.name, name.len);
1565
1566 down_read(&fc->killsb);
1567 err = -ENOENT;
1568 if (fc->sb)
1569 err = fuse_reverse_inval_entry(fc->sb, outarg.parent,
1570 outarg.child, &name);
John Muir3b463ae2009-05-31 11:13:57 -04001571 up_read(&fc->killsb);
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001572 kfree(buf);
John Muir3b463ae2009-05-31 11:13:57 -04001573 return err;
1574
1575err:
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001576 kfree(buf);
John Muir3b463ae2009-05-31 11:13:57 -04001577 fuse_copy_finish(cs);
1578 return err;
1579}
1580
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001581static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
1582 struct fuse_copy_state *cs)
1583{
1584 struct fuse_notify_store_out outarg;
1585 struct inode *inode;
1586 struct address_space *mapping;
1587 u64 nodeid;
1588 int err;
1589 pgoff_t index;
1590 unsigned int offset;
1591 unsigned int num;
1592 loff_t file_size;
1593 loff_t end;
1594
1595 err = -EINVAL;
1596 if (size < sizeof(outarg))
1597 goto out_finish;
1598
1599 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1600 if (err)
1601 goto out_finish;
1602
1603 err = -EINVAL;
1604 if (size - sizeof(outarg) != outarg.size)
1605 goto out_finish;
1606
1607 nodeid = outarg.nodeid;
1608
1609 down_read(&fc->killsb);
1610
1611 err = -ENOENT;
1612 if (!fc->sb)
1613 goto out_up_killsb;
1614
1615 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1616 if (!inode)
1617 goto out_up_killsb;
1618
1619 mapping = inode->i_mapping;
1620 index = outarg.offset >> PAGE_CACHE_SHIFT;
1621 offset = outarg.offset & ~PAGE_CACHE_MASK;
1622 file_size = i_size_read(inode);
1623 end = outarg.offset + outarg.size;
1624 if (end > file_size) {
1625 file_size = end;
1626 fuse_write_update_size(inode, file_size);
1627 }
1628
1629 num = outarg.size;
1630 while (num) {
1631 struct page *page;
1632 unsigned int this_num;
1633
1634 err = -ENOMEM;
1635 page = find_or_create_page(mapping, index,
1636 mapping_gfp_mask(mapping));
1637 if (!page)
1638 goto out_iput;
1639
1640 this_num = min_t(unsigned, num, PAGE_CACHE_SIZE - offset);
1641 err = fuse_copy_page(cs, &page, offset, this_num, 0);
Miklos Szeredi063ec1e2014-01-22 19:36:58 +01001642 if (!err && offset == 0 &&
1643 (this_num == PAGE_CACHE_SIZE || file_size == end))
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001644 SetPageUptodate(page);
1645 unlock_page(page);
1646 page_cache_release(page);
1647
1648 if (err)
1649 goto out_iput;
1650
1651 num -= this_num;
1652 offset = 0;
1653 index++;
1654 }
1655
1656 err = 0;
1657
1658out_iput:
1659 iput(inode);
1660out_up_killsb:
1661 up_read(&fc->killsb);
1662out_finish:
1663 fuse_copy_finish(cs);
1664 return err;
1665}
1666
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001667static void fuse_retrieve_end(struct fuse_conn *fc, struct fuse_req *req)
1668{
Mel Gormanb745bc82014-06-04 16:10:22 -07001669 release_pages(req->pages, req->num_pages, false);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001670}
1671
1672static int fuse_retrieve(struct fuse_conn *fc, struct inode *inode,
1673 struct fuse_notify_retrieve_out *outarg)
1674{
1675 int err;
1676 struct address_space *mapping = inode->i_mapping;
1677 struct fuse_req *req;
1678 pgoff_t index;
1679 loff_t file_size;
1680 unsigned int num;
1681 unsigned int offset;
Geert Uytterhoeven01574432010-09-30 22:06:21 +02001682 size_t total_len = 0;
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001683 int num_pages;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001684
1685 offset = outarg->offset & ~PAGE_CACHE_MASK;
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001686 file_size = i_size_read(inode);
1687
1688 num = outarg->size;
1689 if (outarg->offset > file_size)
1690 num = 0;
1691 else if (outarg->offset + num > file_size)
1692 num = file_size - outarg->offset;
1693
1694 num_pages = (num + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1695 num_pages = min(num_pages, FUSE_MAX_PAGES_PER_REQ);
1696
1697 req = fuse_get_req(fc, num_pages);
1698 if (IS_ERR(req))
1699 return PTR_ERR(req);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001700
1701 req->in.h.opcode = FUSE_NOTIFY_REPLY;
1702 req->in.h.nodeid = outarg->nodeid;
1703 req->in.numargs = 2;
1704 req->in.argpages = 1;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001705 req->page_descs[0].offset = offset;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001706 req->end = fuse_retrieve_end;
1707
1708 index = outarg->offset >> PAGE_CACHE_SHIFT;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001709
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001710 while (num && req->num_pages < num_pages) {
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001711 struct page *page;
1712 unsigned int this_num;
1713
1714 page = find_get_page(mapping, index);
1715 if (!page)
1716 break;
1717
1718 this_num = min_t(unsigned, num, PAGE_CACHE_SIZE - offset);
1719 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001720 req->page_descs[req->num_pages].length = this_num;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001721 req->num_pages++;
1722
Miklos Szeredic9e67d42012-09-04 18:45:54 +02001723 offset = 0;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001724 num -= this_num;
1725 total_len += this_num;
Miklos Szeredi48706d02011-12-13 10:36:59 +01001726 index++;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001727 }
1728 req->misc.retrieve_in.offset = outarg->offset;
1729 req->misc.retrieve_in.size = total_len;
1730 req->in.args[0].size = sizeof(req->misc.retrieve_in);
1731 req->in.args[0].value = &req->misc.retrieve_in;
1732 req->in.args[1].size = total_len;
1733
1734 err = fuse_request_send_notify_reply(fc, req, outarg->notify_unique);
1735 if (err)
1736 fuse_retrieve_end(fc, req);
1737
1738 return err;
1739}
1740
1741static int fuse_notify_retrieve(struct fuse_conn *fc, unsigned int size,
1742 struct fuse_copy_state *cs)
1743{
1744 struct fuse_notify_retrieve_out outarg;
1745 struct inode *inode;
1746 int err;
1747
1748 err = -EINVAL;
1749 if (size != sizeof(outarg))
1750 goto copy_finish;
1751
1752 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1753 if (err)
1754 goto copy_finish;
1755
1756 fuse_copy_finish(cs);
1757
1758 down_read(&fc->killsb);
1759 err = -ENOENT;
1760 if (fc->sb) {
1761 u64 nodeid = outarg.nodeid;
1762
1763 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1764 if (inode) {
1765 err = fuse_retrieve(fc, inode, &outarg);
1766 iput(inode);
1767 }
1768 }
1769 up_read(&fc->killsb);
1770
1771 return err;
1772
1773copy_finish:
1774 fuse_copy_finish(cs);
1775 return err;
1776}
1777
Tejun Heo85993962008-11-26 12:03:55 +01001778static int fuse_notify(struct fuse_conn *fc, enum fuse_notify_code code,
1779 unsigned int size, struct fuse_copy_state *cs)
1780{
Miklos Szeredi0d278362015-02-26 11:45:47 +01001781 /* Don't try to move pages (yet) */
1782 cs->move_pages = 0;
1783
Tejun Heo85993962008-11-26 12:03:55 +01001784 switch (code) {
Tejun Heo95668a62008-11-26 12:03:55 +01001785 case FUSE_NOTIFY_POLL:
1786 return fuse_notify_poll(fc, size, cs);
1787
John Muir3b463ae2009-05-31 11:13:57 -04001788 case FUSE_NOTIFY_INVAL_INODE:
1789 return fuse_notify_inval_inode(fc, size, cs);
1790
1791 case FUSE_NOTIFY_INVAL_ENTRY:
1792 return fuse_notify_inval_entry(fc, size, cs);
1793
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001794 case FUSE_NOTIFY_STORE:
1795 return fuse_notify_store(fc, size, cs);
1796
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001797 case FUSE_NOTIFY_RETRIEVE:
1798 return fuse_notify_retrieve(fc, size, cs);
1799
John Muir451d0f52011-12-06 21:50:06 +01001800 case FUSE_NOTIFY_DELETE:
1801 return fuse_notify_delete(fc, size, cs);
1802
Tejun Heo85993962008-11-26 12:03:55 +01001803 default:
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001804 fuse_copy_finish(cs);
Tejun Heo85993962008-11-26 12:03:55 +01001805 return -EINVAL;
1806 }
1807}
1808
Miklos Szeredi334f4852005-09-09 13:10:27 -07001809/* Look up request on processing list by unique ID */
1810static struct fuse_req *request_find(struct fuse_conn *fc, u64 unique)
1811{
Dong Fang05726ac2013-07-30 22:50:01 -04001812 struct fuse_req *req;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001813
Dong Fang05726ac2013-07-30 22:50:01 -04001814 list_for_each_entry(req, &fc->processing, list) {
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001815 if (req->in.h.unique == unique || req->intr_unique == unique)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001816 return req;
1817 }
1818 return NULL;
1819}
1820
1821static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
1822 unsigned nbytes)
1823{
1824 unsigned reqsize = sizeof(struct fuse_out_header);
1825
1826 if (out->h.error)
1827 return nbytes != reqsize ? -EINVAL : 0;
1828
1829 reqsize += len_args(out->numargs, out->args);
1830
1831 if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
1832 return -EINVAL;
1833 else if (reqsize > nbytes) {
1834 struct fuse_arg *lastarg = &out->args[out->numargs-1];
1835 unsigned diffsize = reqsize - nbytes;
1836 if (diffsize > lastarg->size)
1837 return -EINVAL;
1838 lastarg->size -= diffsize;
1839 }
1840 return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
1841 out->page_zeroing);
1842}
1843
1844/*
1845 * Write a single reply to a request. First the header is copied from
1846 * the write buffer. The request is then searched on the processing
1847 * list by the unique ID found in the header. If found, then remove
1848 * it from the list and copy the rest of the buffer to the request.
1849 * The request is finished by calling request_end()
1850 */
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001851static ssize_t fuse_dev_do_write(struct fuse_conn *fc,
1852 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001853{
1854 int err;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001855 struct fuse_req *req;
1856 struct fuse_out_header oh;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001857
Miklos Szeredi334f4852005-09-09 13:10:27 -07001858 if (nbytes < sizeof(struct fuse_out_header))
1859 return -EINVAL;
1860
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001861 err = fuse_copy_one(cs, &oh, sizeof(oh));
Miklos Szeredi334f4852005-09-09 13:10:27 -07001862 if (err)
1863 goto err_finish;
Tejun Heo85993962008-11-26 12:03:55 +01001864
Miklos Szeredi334f4852005-09-09 13:10:27 -07001865 err = -EINVAL;
Tejun Heo85993962008-11-26 12:03:55 +01001866 if (oh.len != nbytes)
1867 goto err_finish;
1868
1869 /*
1870 * Zero oh.unique indicates unsolicited notification message
1871 * and error contains notification code.
1872 */
1873 if (!oh.unique) {
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001874 err = fuse_notify(fc, oh.error, nbytes - sizeof(oh), cs);
Tejun Heo85993962008-11-26 12:03:55 +01001875 return err ? err : nbytes;
1876 }
1877
1878 err = -EINVAL;
1879 if (oh.error <= -1000 || oh.error > 0)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001880 goto err_finish;
1881
Miklos Szeredid7133112006-04-10 22:54:55 -07001882 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001883 err = -ENOENT;
1884 if (!fc->connected)
1885 goto err_unlock;
1886
Miklos Szeredi334f4852005-09-09 13:10:27 -07001887 req = request_find(fc, oh.unique);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001888 if (!req)
1889 goto err_unlock;
1890
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001891 /* Is it an interrupt reply? */
1892 if (req->intr_unique == oh.unique) {
1893 err = -EINVAL;
1894 if (nbytes != sizeof(struct fuse_out_header))
1895 goto err_unlock;
1896
1897 if (oh.error == -ENOSYS)
1898 fc->no_interrupt = 1;
1899 else if (oh.error == -EAGAIN)
1900 queue_interrupt(fc, req);
1901
1902 spin_unlock(&fc->lock);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001903 fuse_copy_finish(cs);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001904 return nbytes;
1905 }
1906
Miklos Szeredi7a3b2c72015-07-01 16:26:00 +02001907 req->state = FUSE_REQ_IO;
Miklos Szeredid77a1d52006-01-16 22:14:31 -08001908 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001909 req->out.h = oh;
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001910 set_bit(FR_LOCKED, &req->flags);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001911 cs->req = req;
Miklos Szeredice534fb2010-05-25 15:06:07 +02001912 if (!req->out.page_replace)
1913 cs->move_pages = 0;
Miklos Szeredid7133112006-04-10 22:54:55 -07001914 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001915
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001916 err = copy_out_args(cs, &req->out, nbytes);
1917 fuse_copy_finish(cs);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001918
Miklos Szeredid7133112006-04-10 22:54:55 -07001919 spin_lock(&fc->lock);
Miklos Szeredi825d6d32015-07-01 16:25:58 +02001920 clear_bit(FR_LOCKED, &req->flags);
Miklos Szeredi0d8e84b2015-07-01 16:25:58 +02001921 if (!fc->connected)
1922 err = -ENOENT;
1923 else if (err)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001924 req->out.h.error = -EIO;
1925 request_end(fc, req);
1926
1927 return err ? err : nbytes;
1928
1929 err_unlock:
Miklos Szeredid7133112006-04-10 22:54:55 -07001930 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001931 err_finish:
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001932 fuse_copy_finish(cs);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001933 return err;
1934}
1935
Al Virofbdbacc2015-04-03 21:53:39 -04001936static ssize_t fuse_dev_write(struct kiocb *iocb, struct iov_iter *from)
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001937{
1938 struct fuse_copy_state cs;
1939 struct fuse_conn *fc = fuse_get_conn(iocb->ki_filp);
1940 if (!fc)
1941 return -EPERM;
1942
Al Virofbdbacc2015-04-03 21:53:39 -04001943 if (!iter_is_iovec(from))
1944 return -EINVAL;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001945
Miklos Szeredidc008092015-07-01 16:25:58 +02001946 fuse_copy_init(&cs, 0, from);
Al Virofbdbacc2015-04-03 21:53:39 -04001947
1948 return fuse_dev_do_write(fc, &cs, iov_iter_count(from));
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001949}
1950
1951static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe,
1952 struct file *out, loff_t *ppos,
1953 size_t len, unsigned int flags)
1954{
1955 unsigned nbuf;
1956 unsigned idx;
1957 struct pipe_buffer *bufs;
1958 struct fuse_copy_state cs;
1959 struct fuse_conn *fc;
1960 size_t rem;
1961 ssize_t ret;
1962
1963 fc = fuse_get_conn(out);
1964 if (!fc)
1965 return -EPERM;
1966
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001967 bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), GFP_KERNEL);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001968 if (!bufs)
1969 return -ENOMEM;
1970
1971 pipe_lock(pipe);
1972 nbuf = 0;
1973 rem = 0;
1974 for (idx = 0; idx < pipe->nrbufs && rem < len; idx++)
1975 rem += pipe->bufs[(pipe->curbuf + idx) & (pipe->buffers - 1)].len;
1976
1977 ret = -EINVAL;
1978 if (rem < len) {
1979 pipe_unlock(pipe);
1980 goto out;
1981 }
1982
1983 rem = len;
1984 while (rem) {
1985 struct pipe_buffer *ibuf;
1986 struct pipe_buffer *obuf;
1987
1988 BUG_ON(nbuf >= pipe->buffers);
1989 BUG_ON(!pipe->nrbufs);
1990 ibuf = &pipe->bufs[pipe->curbuf];
1991 obuf = &bufs[nbuf];
1992
1993 if (rem >= ibuf->len) {
1994 *obuf = *ibuf;
1995 ibuf->ops = NULL;
1996 pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
1997 pipe->nrbufs--;
1998 } else {
1999 ibuf->ops->get(pipe, ibuf);
2000 *obuf = *ibuf;
2001 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
2002 obuf->len = rem;
2003 ibuf->offset += obuf->len;
2004 ibuf->len -= obuf->len;
2005 }
2006 nbuf++;
2007 rem -= obuf->len;
2008 }
2009 pipe_unlock(pipe);
2010
Miklos Szeredidc008092015-07-01 16:25:58 +02002011 fuse_copy_init(&cs, 0, NULL);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002012 cs.pipebufs = bufs;
Al Viro6c09e942015-04-03 22:06:08 -04002013 cs.nr_segs = nbuf;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002014 cs.pipe = pipe;
2015
Miklos Szeredice534fb2010-05-25 15:06:07 +02002016 if (flags & SPLICE_F_MOVE)
2017 cs.move_pages = 1;
2018
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002019 ret = fuse_dev_do_write(fc, &cs, len);
2020
2021 for (idx = 0; idx < nbuf; idx++) {
2022 struct pipe_buffer *buf = &bufs[idx];
2023 buf->ops->release(pipe, buf);
2024 }
2025out:
2026 kfree(bufs);
2027 return ret;
2028}
2029
Miklos Szeredi334f4852005-09-09 13:10:27 -07002030static unsigned fuse_dev_poll(struct file *file, poll_table *wait)
2031{
Miklos Szeredi334f4852005-09-09 13:10:27 -07002032 unsigned mask = POLLOUT | POLLWRNORM;
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07002033 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002034 if (!fc)
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07002035 return POLLERR;
Miklos Szeredi334f4852005-09-09 13:10:27 -07002036
2037 poll_wait(file, &fc->waitq, wait);
2038
Miklos Szeredid7133112006-04-10 22:54:55 -07002039 spin_lock(&fc->lock);
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07002040 if (!fc->connected)
2041 mask = POLLERR;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07002042 else if (request_pending(fc))
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07002043 mask |= POLLIN | POLLRDNORM;
Miklos Szeredid7133112006-04-10 22:54:55 -07002044 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002045
2046 return mask;
2047}
2048
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002049/*
2050 * Abort all requests on the given list (pending or processing)
2051 *
Miklos Szeredid7133112006-04-10 22:54:55 -07002052 * This function releases and reacquires fc->lock
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002053 */
Miklos Szeredi334f4852005-09-09 13:10:27 -07002054static void end_requests(struct fuse_conn *fc, struct list_head *head)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02002055__releases(fc->lock)
2056__acquires(fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -07002057{
2058 while (!list_empty(head)) {
2059 struct fuse_req *req;
2060 req = list_entry(head->next, struct fuse_req, list);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002061 req->out.h.error = -ECONNABORTED;
2062 request_end(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -07002063 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002064 }
2065}
2066
Bryan Green357ccf22011-03-01 16:43:52 -08002067static void end_polls(struct fuse_conn *fc)
2068{
2069 struct rb_node *p;
2070
2071 p = rb_first(&fc->polled_files);
2072
2073 while (p) {
2074 struct fuse_file *ff;
2075 ff = rb_entry(p, struct fuse_file, polled_node);
2076 wake_up_interruptible_all(&ff->poll_wait);
2077
2078 p = rb_next(p);
2079 }
2080}
2081
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002082/*
2083 * Abort all requests.
2084 *
Miklos Szeredib716d422015-07-01 16:25:59 +02002085 * Emergency exit in case of a malicious or accidental deadlock, or just a hung
2086 * filesystem.
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002087 *
Miklos Szeredib716d422015-07-01 16:25:59 +02002088 * The same effect is usually achievable through killing the filesystem daemon
2089 * and all users of the filesystem. The exception is the combination of an
2090 * asynchronous request and the tricky deadlock (see
2091 * Documentation/filesystems/fuse.txt).
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002092 *
Miklos Szeredib716d422015-07-01 16:25:59 +02002093 * Aborting requests under I/O goes as follows: 1: Separate out unlocked
2094 * requests, they should be finished off immediately. Locked requests will be
2095 * finished after unlock; see unlock_request(). 2: Finish off the unlocked
2096 * requests. It is possible that some request will finish before we can. This
2097 * is OK, the request will in that case be removed from the list before we touch
2098 * it.
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002099 */
2100void fuse_abort_conn(struct fuse_conn *fc)
2101{
Miklos Szeredid7133112006-04-10 22:54:55 -07002102 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002103 if (fc->connected) {
Miklos Szeredib716d422015-07-01 16:25:59 +02002104 struct fuse_req *req, *next;
Miklos Szeredi41f98272015-07-01 16:25:59 +02002105 LIST_HEAD(to_end1);
2106 LIST_HEAD(to_end2);
Miklos Szeredib716d422015-07-01 16:25:59 +02002107
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002108 fc->connected = 0;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -07002109 fc->blocked = 0;
Miklos Szeredi9759bd512015-01-06 10:45:35 +01002110 fuse_set_initialized(fc);
Miklos Szeredib716d422015-07-01 16:25:59 +02002111 list_for_each_entry_safe(req, next, &fc->io, list) {
2112 req->out.h.error = -ECONNABORTED;
2113 spin_lock(&req->waitq.lock);
2114 set_bit(FR_ABORTED, &req->flags);
2115 if (!test_bit(FR_LOCKED, &req->flags))
Miklos Szeredi41f98272015-07-01 16:25:59 +02002116 list_move(&req->list, &to_end1);
Miklos Szeredib716d422015-07-01 16:25:59 +02002117 spin_unlock(&req->waitq.lock);
2118 }
Miklos Szeredi41f98272015-07-01 16:25:59 +02002119 fc->max_background = UINT_MAX;
2120 flush_bg_queue(fc);
2121 list_splice_init(&fc->pending, &to_end2);
2122 list_splice_init(&fc->processing, &to_end2);
2123 while (!list_empty(&to_end1)) {
2124 req = list_first_entry(&to_end1, struct fuse_req, list);
Miklos Szeredib716d422015-07-01 16:25:59 +02002125 __fuse_get_request(req);
2126 request_end(fc, req);
2127 spin_lock(&fc->lock);
2128 }
Miklos Szeredi41f98272015-07-01 16:25:59 +02002129 end_requests(fc, &to_end2);
Miklos Szeredib716d422015-07-01 16:25:59 +02002130 while (forget_pending(fc))
2131 kfree(dequeue_forget(fc, 1, NULL));
Bryan Green357ccf22011-03-01 16:43:52 -08002132 end_polls(fc);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002133 wake_up_all(&fc->waitq);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -07002134 wake_up_all(&fc->blocked_waitq);
Jeff Dike385a17b2006-04-10 22:54:52 -07002135 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002136 }
Miklos Szeredid7133112006-04-10 22:54:55 -07002137 spin_unlock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002138}
Tejun Heo08cbf542009-04-14 10:54:53 +09002139EXPORT_SYMBOL_GPL(fuse_abort_conn);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002140
Tejun Heo08cbf542009-04-14 10:54:53 +09002141int fuse_dev_release(struct inode *inode, struct file *file)
Miklos Szeredi334f4852005-09-09 13:10:27 -07002142{
Miklos Szeredi0720b312006-04-10 22:54:55 -07002143 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002144 if (fc) {
Miklos Szerediccd0a0b2015-07-01 16:25:57 +02002145 WARN_ON(!list_empty(&fc->io));
2146 WARN_ON(fc->fasync != NULL);
2147 fuse_abort_conn(fc);
Miklos Szeredibafa9652006-06-25 05:48:51 -07002148 fuse_conn_put(fc);
Jeff Dike385a17b2006-04-10 22:54:52 -07002149 }
Miklos Szeredif543f252006-01-16 22:14:35 -08002150
Miklos Szeredi334f4852005-09-09 13:10:27 -07002151 return 0;
2152}
Tejun Heo08cbf542009-04-14 10:54:53 +09002153EXPORT_SYMBOL_GPL(fuse_dev_release);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002154
Jeff Dike385a17b2006-04-10 22:54:52 -07002155static int fuse_dev_fasync(int fd, struct file *file, int on)
2156{
2157 struct fuse_conn *fc = fuse_get_conn(file);
2158 if (!fc)
Miklos Szeredia87046d2006-04-10 22:54:56 -07002159 return -EPERM;
Jeff Dike385a17b2006-04-10 22:54:52 -07002160
2161 /* No locking - fasync_helper does its own locking */
2162 return fasync_helper(fd, file, on, &fc->fasync);
2163}
2164
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002165const struct file_operations fuse_dev_operations = {
Miklos Szeredi334f4852005-09-09 13:10:27 -07002166 .owner = THIS_MODULE,
Tom Van Braeckel94e4fe22015-01-12 05:22:16 +01002167 .open = fuse_dev_open,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002168 .llseek = no_llseek,
Al Virofbdbacc2015-04-03 21:53:39 -04002169 .read_iter = fuse_dev_read,
Miklos Szeredic3021622010-05-25 15:06:07 +02002170 .splice_read = fuse_dev_splice_read,
Al Virofbdbacc2015-04-03 21:53:39 -04002171 .write_iter = fuse_dev_write,
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002172 .splice_write = fuse_dev_splice_write,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002173 .poll = fuse_dev_poll,
2174 .release = fuse_dev_release,
Jeff Dike385a17b2006-04-10 22:54:52 -07002175 .fasync = fuse_dev_fasync,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002176};
Tejun Heo08cbf542009-04-14 10:54:53 +09002177EXPORT_SYMBOL_GPL(fuse_dev_operations);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002178
2179static struct miscdevice fuse_miscdevice = {
2180 .minor = FUSE_MINOR,
2181 .name = "fuse",
2182 .fops = &fuse_dev_operations,
2183};
2184
2185int __init fuse_dev_init(void)
2186{
2187 int err = -ENOMEM;
2188 fuse_req_cachep = kmem_cache_create("fuse_request",
2189 sizeof(struct fuse_req),
Paul Mundt20c2df82007-07-20 10:11:58 +09002190 0, 0, NULL);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002191 if (!fuse_req_cachep)
2192 goto out;
2193
2194 err = misc_register(&fuse_miscdevice);
2195 if (err)
2196 goto out_cache_clean;
2197
2198 return 0;
2199
2200 out_cache_clean:
2201 kmem_cache_destroy(fuse_req_cachep);
2202 out:
2203 return err;
2204}
2205
2206void fuse_dev_cleanup(void)
2207{
2208 misc_deregister(&fuse_miscdevice);
2209 kmem_cache_destroy(fuse_req_cachep);
2210}