blob: 3673105889628714a4f5c02fcfa73f0301ba3320 [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
Miklos Szeredi334f4852005-09-09 13:10:27 -0700114static void __fuse_get_request(struct fuse_req *req)
115{
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
Maxim Patlasov0aada882013-03-21 18:02:28 +0400133static bool fuse_block_alloc(struct fuse_conn *fc, bool for_background)
134{
135 return !fc->initialized || (for_background && fc->blocked);
136}
137
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400138static struct fuse_req *__fuse_get_req(struct fuse_conn *fc, unsigned npages,
139 bool for_background)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700140{
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700141 struct fuse_req *req;
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700142 int err;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200143 atomic_inc(&fc->num_waiting);
Maxim Patlasov0aada882013-03-21 18:02:28 +0400144
145 if (fuse_block_alloc(fc, for_background)) {
146 sigset_t oldset;
147 int intr;
148
149 block_sigs(&oldset);
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400150 intr = wait_event_interruptible_exclusive(fc->blocked_waitq,
Maxim Patlasov0aada882013-03-21 18:02:28 +0400151 !fuse_block_alloc(fc, for_background));
152 restore_sigs(&oldset);
153 err = -EINTR;
154 if (intr)
155 goto out;
156 }
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700157
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700158 err = -ENOTCONN;
159 if (!fc->connected)
160 goto out;
161
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400162 req = fuse_request_alloc(npages);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200163 err = -ENOMEM;
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400164 if (!req) {
165 if (for_background)
166 wake_up(&fc->blocked_waitq);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200167 goto out;
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400168 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700169
Miklos Szeredi33649c92006-06-25 05:48:52 -0700170 fuse_req_init_context(req);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200171 req->waiting = 1;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400172 req->background = for_background;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700173 return req;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200174
175 out:
176 atomic_dec(&fc->num_waiting);
177 return ERR_PTR(err);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700178}
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400179
180struct fuse_req *fuse_get_req(struct fuse_conn *fc, unsigned npages)
181{
182 return __fuse_get_req(fc, npages, false);
183}
Tejun Heo08cbf542009-04-14 10:54:53 +0900184EXPORT_SYMBOL_GPL(fuse_get_req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700185
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400186struct fuse_req *fuse_get_req_for_background(struct fuse_conn *fc,
187 unsigned npages)
188{
189 return __fuse_get_req(fc, npages, true);
190}
191EXPORT_SYMBOL_GPL(fuse_get_req_for_background);
192
Miklos Szeredi33649c92006-06-25 05:48:52 -0700193/*
194 * Return request in fuse_file->reserved_req. However that may
195 * currently be in use. If that is the case, wait for it to become
196 * available.
197 */
198static struct fuse_req *get_reserved_req(struct fuse_conn *fc,
199 struct file *file)
200{
201 struct fuse_req *req = NULL;
202 struct fuse_file *ff = file->private_data;
203
204 do {
Miklos Szeredide5e3de2007-10-16 23:31:00 -0700205 wait_event(fc->reserved_req_waitq, ff->reserved_req);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700206 spin_lock(&fc->lock);
207 if (ff->reserved_req) {
208 req = ff->reserved_req;
209 ff->reserved_req = NULL;
Al Virocb0942b2012-08-27 14:48:26 -0400210 req->stolen_file = get_file(file);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700211 }
212 spin_unlock(&fc->lock);
213 } while (!req);
214
215 return req;
216}
217
218/*
219 * Put stolen request back into fuse_file->reserved_req
220 */
221static void put_reserved_req(struct fuse_conn *fc, struct fuse_req *req)
222{
223 struct file *file = req->stolen_file;
224 struct fuse_file *ff = file->private_data;
225
226 spin_lock(&fc->lock);
Maxim Patlasovb2430d72012-10-26 19:49:24 +0400227 fuse_request_init(req, req->pages, req->page_descs, req->max_pages);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700228 BUG_ON(ff->reserved_req);
229 ff->reserved_req = req;
Miklos Szeredide5e3de2007-10-16 23:31:00 -0700230 wake_up_all(&fc->reserved_req_waitq);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700231 spin_unlock(&fc->lock);
232 fput(file);
233}
234
235/*
236 * Gets a requests for a file operation, always succeeds
237 *
238 * This is used for sending the FLUSH request, which must get to
239 * userspace, due to POSIX locks which may need to be unlocked.
240 *
241 * If allocation fails due to OOM, use the reserved request in
242 * fuse_file.
243 *
244 * This is very unlikely to deadlock accidentally, since the
245 * filesystem should not have it's own file open. If deadlock is
246 * intentional, it can still be broken by "aborting" the filesystem.
247 */
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400248struct fuse_req *fuse_get_req_nofail_nopages(struct fuse_conn *fc,
249 struct file *file)
Miklos Szeredi33649c92006-06-25 05:48:52 -0700250{
251 struct fuse_req *req;
252
253 atomic_inc(&fc->num_waiting);
Maxim Patlasov0aada882013-03-21 18:02:28 +0400254 wait_event(fc->blocked_waitq, fc->initialized);
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400255 req = fuse_request_alloc(0);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700256 if (!req)
257 req = get_reserved_req(fc, file);
258
259 fuse_req_init_context(req);
260 req->waiting = 1;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400261 req->background = 0;
Miklos Szeredi33649c92006-06-25 05:48:52 -0700262 return req;
263}
264
Miklos Szeredi334f4852005-09-09 13:10:27 -0700265void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req)
266{
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800267 if (atomic_dec_and_test(&req->count)) {
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400268 if (unlikely(req->background)) {
269 /*
270 * We get here in the unlikely case that a background
271 * request was allocated but not sent
272 */
273 spin_lock(&fc->lock);
274 if (!fc->blocked)
275 wake_up(&fc->blocked_waitq);
276 spin_unlock(&fc->lock);
277 }
278
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200279 if (req->waiting)
280 atomic_dec(&fc->num_waiting);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700281
282 if (req->stolen_file)
283 put_reserved_req(fc, req);
284 else
285 fuse_request_free(req);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800286 }
287}
Tejun Heo08cbf542009-04-14 10:54:53 +0900288EXPORT_SYMBOL_GPL(fuse_put_request);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800289
Miklos Szeredid12def12008-02-06 01:38:39 -0800290static unsigned len_args(unsigned numargs, struct fuse_arg *args)
291{
292 unsigned nbytes = 0;
293 unsigned i;
294
295 for (i = 0; i < numargs; i++)
296 nbytes += args[i].size;
297
298 return nbytes;
299}
300
301static u64 fuse_get_unique(struct fuse_conn *fc)
302{
303 fc->reqctr++;
304 /* zero is special */
305 if (fc->reqctr == 0)
306 fc->reqctr = 1;
307
308 return fc->reqctr;
309}
310
311static void queue_request(struct fuse_conn *fc, struct fuse_req *req)
312{
Miklos Szeredid12def12008-02-06 01:38:39 -0800313 req->in.h.len = sizeof(struct fuse_in_header) +
314 len_args(req->in.numargs, (struct fuse_arg *) req->in.args);
315 list_add_tail(&req->list, &fc->pending);
316 req->state = FUSE_REQ_PENDING;
317 if (!req->waiting) {
318 req->waiting = 1;
319 atomic_inc(&fc->num_waiting);
320 }
321 wake_up(&fc->waitq);
322 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
323}
324
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100325void fuse_queue_forget(struct fuse_conn *fc, struct fuse_forget_link *forget,
326 u64 nodeid, u64 nlookup)
327{
Miklos Szeredi02c048b2010-12-07 20:16:56 +0100328 forget->forget_one.nodeid = nodeid;
329 forget->forget_one.nlookup = nlookup;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100330
331 spin_lock(&fc->lock);
Miklos Szeredi5dfcc872011-09-12 09:38:03 +0200332 if (fc->connected) {
333 fc->forget_list_tail->next = forget;
334 fc->forget_list_tail = forget;
335 wake_up(&fc->waitq);
336 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
337 } else {
338 kfree(forget);
339 }
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100340 spin_unlock(&fc->lock);
341}
342
Miklos Szeredid12def12008-02-06 01:38:39 -0800343static void flush_bg_queue(struct fuse_conn *fc)
344{
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700345 while (fc->active_background < fc->max_background &&
Miklos Szeredid12def12008-02-06 01:38:39 -0800346 !list_empty(&fc->bg_queue)) {
347 struct fuse_req *req;
348
349 req = list_entry(fc->bg_queue.next, struct fuse_req, list);
350 list_del(&req->list);
351 fc->active_background++;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200352 req->in.h.unique = fuse_get_unique(fc);
Miklos Szeredid12def12008-02-06 01:38:39 -0800353 queue_request(fc, req);
354 }
355}
356
Miklos Szeredi6dbbcb12006-04-26 10:49:06 +0200357/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700358 * This function is called when a request is finished. Either a reply
Miklos Szeredif9a28422006-06-25 05:48:53 -0700359 * has arrived or it was aborted (and not yet sent) or some error
Miklos Szeredif43b1552006-01-16 22:14:26 -0800360 * occurred during communication with userspace, or the device file
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700361 * was closed. The requester thread is woken up (if still waiting),
362 * the 'end' callback is called if given, else the reference to the
363 * request is released
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800364 *
Miklos Szeredid7133112006-04-10 22:54:55 -0700365 * Called with fc->lock, unlocks it
Miklos Szeredi334f4852005-09-09 13:10:27 -0700366 */
367static void request_end(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +0200368__releases(fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700369{
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700370 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
371 req->end = NULL;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800372 list_del(&req->list);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700373 list_del(&req->intr_entry);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800374 req->state = FUSE_REQ_FINISHED;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700375 if (req->background) {
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400376 req->background = 0;
377
378 if (fc->num_background == fc->max_background)
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700379 fc->blocked = 0;
Maxim Patlasov722d2be2013-03-21 18:02:36 +0400380
381 /* Wake up next waiter, if any */
382 if (!fc->blocked)
383 wake_up(&fc->blocked_waitq);
384
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700385 if (fc->num_background == fc->congestion_threshold &&
Tejun Heoa325f9b2009-04-14 10:54:52 +0900386 fc->connected && fc->bdi_initialized) {
Jens Axboe8aa7e842009-07-09 14:52:32 +0200387 clear_bdi_congested(&fc->bdi, BLK_RW_SYNC);
388 clear_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
Miklos Szeredif92b99b2007-10-16 23:30:59 -0700389 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700390 fc->num_background--;
Miklos Szeredid12def12008-02-06 01:38:39 -0800391 fc->active_background--;
392 flush_bg_queue(fc);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700393 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700394 spin_unlock(&fc->lock);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700395 wake_up(&req->waitq);
396 if (end)
397 end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100398 fuse_put_request(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700399}
400
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700401static void wait_answer_interruptible(struct fuse_conn *fc,
402 struct fuse_req *req)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +0200403__releases(fc->lock)
404__acquires(fc->lock)
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700405{
406 if (signal_pending(current))
407 return;
408
409 spin_unlock(&fc->lock);
410 wait_event_interruptible(req->waitq, req->state == FUSE_REQ_FINISHED);
411 spin_lock(&fc->lock);
412}
413
414static void queue_interrupt(struct fuse_conn *fc, struct fuse_req *req)
415{
416 list_add_tail(&req->intr_entry, &fc->interrupts);
417 wake_up(&fc->waitq);
418 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
419}
420
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700421static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +0200422__releases(fc->lock)
423__acquires(fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700424{
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700425 if (!fc->no_interrupt) {
426 /* Any signal may interrupt this */
427 wait_answer_interruptible(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700428
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700429 if (req->aborted)
430 goto aborted;
431 if (req->state == FUSE_REQ_FINISHED)
432 return;
433
434 req->interrupted = 1;
435 if (req->state == FUSE_REQ_SENT)
436 queue_interrupt(fc, req);
437 }
438
Miklos Szeredia131de02007-10-16 23:31:04 -0700439 if (!req->force) {
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700440 sigset_t oldset;
441
442 /* Only fatal signals may interrupt this */
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700443 block_sigs(&oldset);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700444 wait_answer_interruptible(fc, req);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700445 restore_sigs(&oldset);
Miklos Szeredia131de02007-10-16 23:31:04 -0700446
447 if (req->aborted)
448 goto aborted;
449 if (req->state == FUSE_REQ_FINISHED)
450 return;
451
452 /* Request is not yet in userspace, bail out */
453 if (req->state == FUSE_REQ_PENDING) {
454 list_del(&req->list);
455 __fuse_put_request(req);
456 req->out.h.error = -EINTR;
457 return;
458 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700459 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700460
Miklos Szeredia131de02007-10-16 23:31:04 -0700461 /*
462 * Either request is already in userspace, or it was forced.
463 * Wait it out.
464 */
465 spin_unlock(&fc->lock);
466 wait_event(req->waitq, req->state == FUSE_REQ_FINISHED);
467 spin_lock(&fc->lock);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700468
Miklos Szeredia131de02007-10-16 23:31:04 -0700469 if (!req->aborted)
470 return;
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700471
472 aborted:
Miklos Szeredia131de02007-10-16 23:31:04 -0700473 BUG_ON(req->state != FUSE_REQ_FINISHED);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700474 if (req->locked) {
475 /* This is uninterruptible sleep, because data is
476 being copied to/from the buffers of req. During
477 locked state, there mustn't be any filesystem
478 operation (e.g. page fault), since that could lead
479 to deadlock */
Miklos Szeredid7133112006-04-10 22:54:55 -0700480 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700481 wait_event(req->waitq, !req->locked);
Miklos Szeredid7133112006-04-10 22:54:55 -0700482 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700483 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700484}
485
Eric Wong6a4e9222013-02-04 13:04:44 +0000486static void __fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700487{
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400488 BUG_ON(req->background);
Miklos Szeredid7133112006-04-10 22:54:55 -0700489 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700490 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700491 req->out.h.error = -ENOTCONN;
492 else if (fc->conn_error)
493 req->out.h.error = -ECONNREFUSED;
494 else {
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200495 req->in.h.unique = fuse_get_unique(fc);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700496 queue_request(fc, req);
497 /* acquire extra reference, since request is still needed
498 after request_end() */
499 __fuse_get_request(req);
500
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700501 request_wait_answer(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700502 }
Miklos Szeredid7133112006-04-10 22:54:55 -0700503 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700504}
Eric Wong6a4e9222013-02-04 13:04:44 +0000505
506void fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
507{
508 req->isreply = 1;
509 __fuse_request_send(fc, req);
510}
Tejun Heo08cbf542009-04-14 10:54:53 +0900511EXPORT_SYMBOL_GPL(fuse_request_send);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700512
Tejun Heob93f8582008-11-26 12:03:55 +0100513static void fuse_request_send_nowait_locked(struct fuse_conn *fc,
514 struct fuse_req *req)
Miklos Szeredid12def12008-02-06 01:38:39 -0800515{
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400516 BUG_ON(!req->background);
Miklos Szeredid12def12008-02-06 01:38:39 -0800517 fc->num_background++;
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700518 if (fc->num_background == fc->max_background)
Miklos Szeredid12def12008-02-06 01:38:39 -0800519 fc->blocked = 1;
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700520 if (fc->num_background == fc->congestion_threshold &&
Tejun Heoa325f9b2009-04-14 10:54:52 +0900521 fc->bdi_initialized) {
Jens Axboe8aa7e842009-07-09 14:52:32 +0200522 set_bdi_congested(&fc->bdi, BLK_RW_SYNC);
523 set_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
Miklos Szeredid12def12008-02-06 01:38:39 -0800524 }
525 list_add_tail(&req->list, &fc->bg_queue);
526 flush_bg_queue(fc);
527}
528
Tejun Heob93f8582008-11-26 12:03:55 +0100529static void fuse_request_send_nowait(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700530{
Miklos Szeredid7133112006-04-10 22:54:55 -0700531 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700532 if (fc->connected) {
Tejun Heob93f8582008-11-26 12:03:55 +0100533 fuse_request_send_nowait_locked(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700534 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700535 } else {
536 req->out.h.error = -ENOTCONN;
537 request_end(fc, req);
538 }
539}
540
Tejun Heob93f8582008-11-26 12:03:55 +0100541void fuse_request_send_background(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700542{
543 req->isreply = 1;
Tejun Heob93f8582008-11-26 12:03:55 +0100544 fuse_request_send_nowait(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700545}
Tejun Heo08cbf542009-04-14 10:54:53 +0900546EXPORT_SYMBOL_GPL(fuse_request_send_background);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700547
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200548static int fuse_request_send_notify_reply(struct fuse_conn *fc,
549 struct fuse_req *req, u64 unique)
550{
551 int err = -ENODEV;
552
553 req->isreply = 0;
554 req->in.h.unique = unique;
555 spin_lock(&fc->lock);
556 if (fc->connected) {
557 queue_request(fc, req);
558 err = 0;
559 }
560 spin_unlock(&fc->lock);
561
562 return err;
563}
564
Miklos Szeredi334f4852005-09-09 13:10:27 -0700565/*
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700566 * Called under fc->lock
567 *
568 * fc->connected must have been checked previously
569 */
Tejun Heob93f8582008-11-26 12:03:55 +0100570void fuse_request_send_background_locked(struct fuse_conn *fc,
571 struct fuse_req *req)
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700572{
573 req->isreply = 1;
Tejun Heob93f8582008-11-26 12:03:55 +0100574 fuse_request_send_nowait_locked(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700575}
576
Anand V. Avati0b05b182012-08-19 08:53:23 -0400577void fuse_force_forget(struct file *file, u64 nodeid)
578{
Al Viro6131ffa2013-02-27 16:59:05 -0500579 struct inode *inode = file_inode(file);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400580 struct fuse_conn *fc = get_fuse_conn(inode);
581 struct fuse_req *req;
582 struct fuse_forget_in inarg;
583
584 memset(&inarg, 0, sizeof(inarg));
585 inarg.nlookup = 1;
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400586 req = fuse_get_req_nofail_nopages(fc, file);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400587 req->in.h.opcode = FUSE_FORGET;
588 req->in.h.nodeid = nodeid;
589 req->in.numargs = 1;
590 req->in.args[0].size = sizeof(inarg);
591 req->in.args[0].value = &inarg;
592 req->isreply = 0;
Eric Wong6a4e9222013-02-04 13:04:44 +0000593 __fuse_request_send(fc, req);
594 /* ignore errors */
595 fuse_put_request(fc, req);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400596}
597
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700598/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700599 * Lock the request. Up to the next unlock_request() there mustn't be
600 * anything that could cause a page-fault. If the request was already
Miklos Szeredif9a28422006-06-25 05:48:53 -0700601 * aborted bail out.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700602 */
Miklos Szeredid7133112006-04-10 22:54:55 -0700603static int lock_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700604{
605 int err = 0;
606 if (req) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700607 spin_lock(&fc->lock);
Miklos Szeredif9a28422006-06-25 05:48:53 -0700608 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700609 err = -ENOENT;
610 else
611 req->locked = 1;
Miklos Szeredid7133112006-04-10 22:54:55 -0700612 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700613 }
614 return err;
615}
616
617/*
Miklos Szeredif9a28422006-06-25 05:48:53 -0700618 * Unlock request. If it was aborted during being locked, the
Miklos Szeredi334f4852005-09-09 13:10:27 -0700619 * requester thread is currently waiting for it to be unlocked, so
620 * wake it up.
621 */
Miklos Szeredid7133112006-04-10 22:54:55 -0700622static void unlock_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700623{
624 if (req) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700625 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700626 req->locked = 0;
Miklos Szeredif9a28422006-06-25 05:48:53 -0700627 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700628 wake_up(&req->waitq);
Miklos Szeredid7133112006-04-10 22:54:55 -0700629 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700630 }
631}
632
633struct fuse_copy_state {
Miklos Szeredid7133112006-04-10 22:54:55 -0700634 struct fuse_conn *fc;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700635 int write;
636 struct fuse_req *req;
637 const struct iovec *iov;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200638 struct pipe_buffer *pipebufs;
639 struct pipe_buffer *currbuf;
640 struct pipe_inode_info *pipe;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700641 unsigned long nr_segs;
642 unsigned long seglen;
643 unsigned long addr;
644 struct page *pg;
645 void *mapaddr;
646 void *buf;
647 unsigned len;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200648 unsigned move_pages:1;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700649};
650
Miklos Szeredid7133112006-04-10 22:54:55 -0700651static void fuse_copy_init(struct fuse_copy_state *cs, struct fuse_conn *fc,
Miklos Szeredic3021622010-05-25 15:06:07 +0200652 int write,
Miklos Szeredid7133112006-04-10 22:54:55 -0700653 const struct iovec *iov, unsigned long nr_segs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700654{
655 memset(cs, 0, sizeof(*cs));
Miklos Szeredid7133112006-04-10 22:54:55 -0700656 cs->fc = fc;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700657 cs->write = write;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700658 cs->iov = iov;
659 cs->nr_segs = nr_segs;
660}
661
662/* Unmap and put previous page of userspace buffer */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800663static void fuse_copy_finish(struct fuse_copy_state *cs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700664{
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200665 if (cs->currbuf) {
666 struct pipe_buffer *buf = cs->currbuf;
667
Miklos Szeredic3021622010-05-25 15:06:07 +0200668 if (!cs->write) {
669 buf->ops->unmap(cs->pipe, buf, cs->mapaddr);
670 } else {
Miklos Szeredi7909b1c2010-07-12 14:41:40 +0200671 kunmap(buf->page);
Miklos Szeredic3021622010-05-25 15:06:07 +0200672 buf->len = PAGE_SIZE - cs->len;
673 }
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200674 cs->currbuf = NULL;
675 cs->mapaddr = NULL;
676 } else if (cs->mapaddr) {
Miklos Szeredi7909b1c2010-07-12 14:41:40 +0200677 kunmap(cs->pg);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700678 if (cs->write) {
679 flush_dcache_page(cs->pg);
680 set_page_dirty_lock(cs->pg);
681 }
682 put_page(cs->pg);
683 cs->mapaddr = NULL;
684 }
685}
686
687/*
688 * Get another pagefull of userspace buffer, and map it to kernel
689 * address space, and lock request
690 */
691static int fuse_copy_fill(struct fuse_copy_state *cs)
692{
693 unsigned long offset;
694 int err;
695
Miklos Szeredid7133112006-04-10 22:54:55 -0700696 unlock_request(cs->fc, cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700697 fuse_copy_finish(cs);
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200698 if (cs->pipebufs) {
699 struct pipe_buffer *buf = cs->pipebufs;
700
Miklos Szeredic3021622010-05-25 15:06:07 +0200701 if (!cs->write) {
702 err = buf->ops->confirm(cs->pipe, buf);
703 if (err)
704 return err;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200705
Miklos Szeredic3021622010-05-25 15:06:07 +0200706 BUG_ON(!cs->nr_segs);
707 cs->currbuf = buf;
Miklos Szeredi7909b1c2010-07-12 14:41:40 +0200708 cs->mapaddr = buf->ops->map(cs->pipe, buf, 0);
Miklos Szeredic3021622010-05-25 15:06:07 +0200709 cs->len = buf->len;
710 cs->buf = cs->mapaddr + buf->offset;
711 cs->pipebufs++;
712 cs->nr_segs--;
713 } else {
714 struct page *page;
715
716 if (cs->nr_segs == cs->pipe->buffers)
717 return -EIO;
718
719 page = alloc_page(GFP_HIGHUSER);
720 if (!page)
721 return -ENOMEM;
722
723 buf->page = page;
724 buf->offset = 0;
725 buf->len = 0;
726
727 cs->currbuf = buf;
Miklos Szeredi7909b1c2010-07-12 14:41:40 +0200728 cs->mapaddr = kmap(page);
Miklos Szeredic3021622010-05-25 15:06:07 +0200729 cs->buf = cs->mapaddr;
730 cs->len = PAGE_SIZE;
731 cs->pipebufs++;
732 cs->nr_segs++;
733 }
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200734 } else {
735 if (!cs->seglen) {
736 BUG_ON(!cs->nr_segs);
737 cs->seglen = cs->iov[0].iov_len;
738 cs->addr = (unsigned long) cs->iov[0].iov_base;
739 cs->iov++;
740 cs->nr_segs--;
741 }
742 err = get_user_pages_fast(cs->addr, 1, cs->write, &cs->pg);
743 if (err < 0)
744 return err;
745 BUG_ON(err != 1);
746 offset = cs->addr % PAGE_SIZE;
Miklos Szeredi7909b1c2010-07-12 14:41:40 +0200747 cs->mapaddr = kmap(cs->pg);
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200748 cs->buf = cs->mapaddr + offset;
749 cs->len = min(PAGE_SIZE - offset, cs->seglen);
750 cs->seglen -= cs->len;
751 cs->addr += cs->len;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700752 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700753
Miklos Szeredid7133112006-04-10 22:54:55 -0700754 return lock_request(cs->fc, cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700755}
756
757/* Do as much copy to/from userspace buffer as we can */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800758static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700759{
760 unsigned ncpy = min(*size, cs->len);
761 if (val) {
762 if (cs->write)
763 memcpy(cs->buf, *val, ncpy);
764 else
765 memcpy(*val, cs->buf, ncpy);
766 *val += ncpy;
767 }
768 *size -= ncpy;
769 cs->len -= ncpy;
770 cs->buf += ncpy;
771 return ncpy;
772}
773
Miklos Szeredice534fb2010-05-25 15:06:07 +0200774static int fuse_check_page(struct page *page)
775{
776 if (page_mapcount(page) ||
777 page->mapping != NULL ||
778 page_count(page) != 1 ||
779 (page->flags & PAGE_FLAGS_CHECK_AT_PREP &
780 ~(1 << PG_locked |
781 1 << PG_referenced |
782 1 << PG_uptodate |
783 1 << PG_lru |
784 1 << PG_active |
785 1 << PG_reclaim))) {
786 printk(KERN_WARNING "fuse: trying to steal weird page\n");
787 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);
788 return 1;
789 }
790 return 0;
791}
792
793static int fuse_try_move_page(struct fuse_copy_state *cs, struct page **pagep)
794{
795 int err;
796 struct page *oldpage = *pagep;
797 struct page *newpage;
798 struct pipe_buffer *buf = cs->pipebufs;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200799
800 unlock_request(cs->fc, cs->req);
801 fuse_copy_finish(cs);
802
803 err = buf->ops->confirm(cs->pipe, buf);
804 if (err)
805 return err;
806
807 BUG_ON(!cs->nr_segs);
808 cs->currbuf = buf;
809 cs->len = buf->len;
810 cs->pipebufs++;
811 cs->nr_segs--;
812
813 if (cs->len != PAGE_SIZE)
814 goto out_fallback;
815
816 if (buf->ops->steal(cs->pipe, buf) != 0)
817 goto out_fallback;
818
819 newpage = buf->page;
820
821 if (WARN_ON(!PageUptodate(newpage)))
822 return -EIO;
823
824 ClearPageMappedToDisk(newpage);
825
826 if (fuse_check_page(newpage) != 0)
827 goto out_fallback_unlock;
828
Miklos Szeredice534fb2010-05-25 15:06:07 +0200829 /*
830 * This is a new and locked page, it shouldn't be mapped or
831 * have any special flags on it
832 */
833 if (WARN_ON(page_mapped(oldpage)))
834 goto out_fallback_unlock;
835 if (WARN_ON(page_has_private(oldpage)))
836 goto out_fallback_unlock;
837 if (WARN_ON(PageDirty(oldpage) || PageWriteback(oldpage)))
838 goto out_fallback_unlock;
839 if (WARN_ON(PageMlocked(oldpage)))
840 goto out_fallback_unlock;
841
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700842 err = replace_page_cache_page(oldpage, newpage, GFP_KERNEL);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200843 if (err) {
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700844 unlock_page(newpage);
845 return err;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200846 }
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700847
Miklos Szeredice534fb2010-05-25 15:06:07 +0200848 page_cache_get(newpage);
849
850 if (!(buf->flags & PIPE_BUF_FLAG_LRU))
851 lru_cache_add_file(newpage);
852
853 err = 0;
854 spin_lock(&cs->fc->lock);
855 if (cs->req->aborted)
856 err = -ENOENT;
857 else
858 *pagep = newpage;
859 spin_unlock(&cs->fc->lock);
860
861 if (err) {
862 unlock_page(newpage);
863 page_cache_release(newpage);
864 return err;
865 }
866
867 unlock_page(oldpage);
868 page_cache_release(oldpage);
869 cs->len = 0;
870
871 return 0;
872
873out_fallback_unlock:
874 unlock_page(newpage);
875out_fallback:
876 cs->mapaddr = buf->ops->map(cs->pipe, buf, 1);
877 cs->buf = cs->mapaddr + buf->offset;
878
879 err = lock_request(cs->fc, cs->req);
880 if (err)
881 return err;
882
883 return 1;
884}
885
Miklos Szeredic3021622010-05-25 15:06:07 +0200886static int fuse_ref_page(struct fuse_copy_state *cs, struct page *page,
887 unsigned offset, unsigned count)
888{
889 struct pipe_buffer *buf;
890
891 if (cs->nr_segs == cs->pipe->buffers)
892 return -EIO;
893
894 unlock_request(cs->fc, cs->req);
895 fuse_copy_finish(cs);
896
897 buf = cs->pipebufs;
898 page_cache_get(page);
899 buf->page = page;
900 buf->offset = offset;
901 buf->len = count;
902
903 cs->pipebufs++;
904 cs->nr_segs++;
905 cs->len = 0;
906
907 return 0;
908}
909
Miklos Szeredi334f4852005-09-09 13:10:27 -0700910/*
911 * Copy a page in the request to/from the userspace buffer. Must be
912 * done atomically
913 */
Miklos Szeredice534fb2010-05-25 15:06:07 +0200914static int fuse_copy_page(struct fuse_copy_state *cs, struct page **pagep,
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800915 unsigned offset, unsigned count, int zeroing)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700916{
Miklos Szeredice534fb2010-05-25 15:06:07 +0200917 int err;
918 struct page *page = *pagep;
919
Miklos Szeredib6777c42010-10-26 14:22:27 -0700920 if (page && zeroing && count < PAGE_SIZE)
921 clear_highpage(page);
922
Miklos Szeredi334f4852005-09-09 13:10:27 -0700923 while (count) {
Miklos Szeredic3021622010-05-25 15:06:07 +0200924 if (cs->write && cs->pipebufs && page) {
925 return fuse_ref_page(cs, page, offset, count);
926 } else if (!cs->len) {
Miklos Szeredice534fb2010-05-25 15:06:07 +0200927 if (cs->move_pages && page &&
928 offset == 0 && count == PAGE_SIZE) {
929 err = fuse_try_move_page(cs, pagep);
930 if (err <= 0)
931 return err;
932 } else {
933 err = fuse_copy_fill(cs);
934 if (err)
935 return err;
936 }
Miklos Szeredi1729a162008-11-26 12:03:54 +0100937 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700938 if (page) {
Cong Wang2408f6e2011-11-25 23:14:30 +0800939 void *mapaddr = kmap_atomic(page);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700940 void *buf = mapaddr + offset;
941 offset += fuse_copy_do(cs, &buf, &count);
Cong Wang2408f6e2011-11-25 23:14:30 +0800942 kunmap_atomic(mapaddr);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700943 } else
944 offset += fuse_copy_do(cs, NULL, &count);
945 }
946 if (page && !cs->write)
947 flush_dcache_page(page);
948 return 0;
949}
950
951/* Copy pages in the request to/from userspace buffer */
952static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
953 int zeroing)
954{
955 unsigned i;
956 struct fuse_req *req = cs->req;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700957
958 for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
Miklos Szeredice534fb2010-05-25 15:06:07 +0200959 int err;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400960 unsigned offset = req->page_descs[i].offset;
961 unsigned count = min(nbytes, req->page_descs[i].length);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200962
963 err = fuse_copy_page(cs, &req->pages[i], offset, count,
964 zeroing);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700965 if (err)
966 return err;
967
968 nbytes -= count;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700969 }
970 return 0;
971}
972
973/* Copy a single argument in the request to/from userspace buffer */
974static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
975{
976 while (size) {
Miklos Szeredi1729a162008-11-26 12:03:54 +0100977 if (!cs->len) {
978 int err = fuse_copy_fill(cs);
979 if (err)
980 return err;
981 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700982 fuse_copy_do(cs, &val, &size);
983 }
984 return 0;
985}
986
987/* Copy request arguments to/from userspace buffer */
988static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
989 unsigned argpages, struct fuse_arg *args,
990 int zeroing)
991{
992 int err = 0;
993 unsigned i;
994
995 for (i = 0; !err && i < numargs; i++) {
996 struct fuse_arg *arg = &args[i];
997 if (i == numargs - 1 && argpages)
998 err = fuse_copy_pages(cs, arg->size, zeroing);
999 else
1000 err = fuse_copy_one(cs, arg->value, arg->size);
1001 }
1002 return err;
1003}
1004
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001005static int forget_pending(struct fuse_conn *fc)
1006{
1007 return fc->forget_list_head.next != NULL;
1008}
1009
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001010static int request_pending(struct fuse_conn *fc)
1011{
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001012 return !list_empty(&fc->pending) || !list_empty(&fc->interrupts) ||
1013 forget_pending(fc);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001014}
1015
Miklos Szeredi334f4852005-09-09 13:10:27 -07001016/* Wait until a request is available on the pending list */
1017static void request_wait(struct fuse_conn *fc)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001018__releases(fc->lock)
1019__acquires(fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001020{
1021 DECLARE_WAITQUEUE(wait, current);
1022
1023 add_wait_queue_exclusive(&fc->waitq, &wait);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001024 while (fc->connected && !request_pending(fc)) {
Miklos Szeredi334f4852005-09-09 13:10:27 -07001025 set_current_state(TASK_INTERRUPTIBLE);
1026 if (signal_pending(current))
1027 break;
1028
Miklos Szeredid7133112006-04-10 22:54:55 -07001029 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001030 schedule();
Miklos Szeredid7133112006-04-10 22:54:55 -07001031 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001032 }
1033 set_current_state(TASK_RUNNING);
1034 remove_wait_queue(&fc->waitq, &wait);
1035}
1036
1037/*
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001038 * Transfer an interrupt request to userspace
1039 *
1040 * Unlike other requests this is assembled on demand, without a need
1041 * to allocate a separate fuse_req structure.
1042 *
1043 * Called with fc->lock held, releases it
1044 */
Miklos Szeredic3021622010-05-25 15:06:07 +02001045static int fuse_read_interrupt(struct fuse_conn *fc, struct fuse_copy_state *cs,
1046 size_t nbytes, struct fuse_req *req)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001047__releases(fc->lock)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001048{
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001049 struct fuse_in_header ih;
1050 struct fuse_interrupt_in arg;
1051 unsigned reqsize = sizeof(ih) + sizeof(arg);
1052 int err;
1053
1054 list_del_init(&req->intr_entry);
1055 req->intr_unique = fuse_get_unique(fc);
1056 memset(&ih, 0, sizeof(ih));
1057 memset(&arg, 0, sizeof(arg));
1058 ih.len = reqsize;
1059 ih.opcode = FUSE_INTERRUPT;
1060 ih.unique = req->intr_unique;
1061 arg.unique = req->in.h.unique;
1062
1063 spin_unlock(&fc->lock);
Miklos Szeredic3021622010-05-25 15:06:07 +02001064 if (nbytes < reqsize)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001065 return -EINVAL;
1066
Miklos Szeredic3021622010-05-25 15:06:07 +02001067 err = fuse_copy_one(cs, &ih, sizeof(ih));
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001068 if (!err)
Miklos Szeredic3021622010-05-25 15:06:07 +02001069 err = fuse_copy_one(cs, &arg, sizeof(arg));
1070 fuse_copy_finish(cs);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001071
1072 return err ? err : reqsize;
1073}
1074
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001075static struct fuse_forget_link *dequeue_forget(struct fuse_conn *fc,
1076 unsigned max,
1077 unsigned *countp)
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001078{
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001079 struct fuse_forget_link *head = fc->forget_list_head.next;
1080 struct fuse_forget_link **newhead = &head;
1081 unsigned count;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001082
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001083 for (count = 0; *newhead != NULL && count < max; count++)
1084 newhead = &(*newhead)->next;
1085
1086 fc->forget_list_head.next = *newhead;
1087 *newhead = NULL;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001088 if (fc->forget_list_head.next == NULL)
1089 fc->forget_list_tail = &fc->forget_list_head;
1090
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001091 if (countp != NULL)
1092 *countp = count;
1093
1094 return head;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001095}
1096
1097static int fuse_read_single_forget(struct fuse_conn *fc,
1098 struct fuse_copy_state *cs,
1099 size_t nbytes)
1100__releases(fc->lock)
1101{
1102 int err;
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001103 struct fuse_forget_link *forget = dequeue_forget(fc, 1, NULL);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001104 struct fuse_forget_in arg = {
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001105 .nlookup = forget->forget_one.nlookup,
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001106 };
1107 struct fuse_in_header ih = {
1108 .opcode = FUSE_FORGET,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001109 .nodeid = forget->forget_one.nodeid,
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001110 .unique = fuse_get_unique(fc),
1111 .len = sizeof(ih) + sizeof(arg),
1112 };
1113
1114 spin_unlock(&fc->lock);
1115 kfree(forget);
1116 if (nbytes < ih.len)
1117 return -EINVAL;
1118
1119 err = fuse_copy_one(cs, &ih, sizeof(ih));
1120 if (!err)
1121 err = fuse_copy_one(cs, &arg, sizeof(arg));
1122 fuse_copy_finish(cs);
1123
1124 if (err)
1125 return err;
1126
1127 return ih.len;
1128}
1129
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001130static int fuse_read_batch_forget(struct fuse_conn *fc,
1131 struct fuse_copy_state *cs, size_t nbytes)
1132__releases(fc->lock)
1133{
1134 int err;
1135 unsigned max_forgets;
1136 unsigned count;
1137 struct fuse_forget_link *head;
1138 struct fuse_batch_forget_in arg = { .count = 0 };
1139 struct fuse_in_header ih = {
1140 .opcode = FUSE_BATCH_FORGET,
1141 .unique = fuse_get_unique(fc),
1142 .len = sizeof(ih) + sizeof(arg),
1143 };
1144
1145 if (nbytes < ih.len) {
1146 spin_unlock(&fc->lock);
1147 return -EINVAL;
1148 }
1149
1150 max_forgets = (nbytes - ih.len) / sizeof(struct fuse_forget_one);
1151 head = dequeue_forget(fc, max_forgets, &count);
1152 spin_unlock(&fc->lock);
1153
1154 arg.count = count;
1155 ih.len += count * sizeof(struct fuse_forget_one);
1156 err = fuse_copy_one(cs, &ih, sizeof(ih));
1157 if (!err)
1158 err = fuse_copy_one(cs, &arg, sizeof(arg));
1159
1160 while (head) {
1161 struct fuse_forget_link *forget = head;
1162
1163 if (!err) {
1164 err = fuse_copy_one(cs, &forget->forget_one,
1165 sizeof(forget->forget_one));
1166 }
1167 head = forget->next;
1168 kfree(forget);
1169 }
1170
1171 fuse_copy_finish(cs);
1172
1173 if (err)
1174 return err;
1175
1176 return ih.len;
1177}
1178
1179static int fuse_read_forget(struct fuse_conn *fc, struct fuse_copy_state *cs,
1180 size_t nbytes)
1181__releases(fc->lock)
1182{
1183 if (fc->minor < 16 || fc->forget_list_head.next->next == NULL)
1184 return fuse_read_single_forget(fc, cs, nbytes);
1185 else
1186 return fuse_read_batch_forget(fc, cs, nbytes);
1187}
1188
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001189/*
Miklos Szeredi334f4852005-09-09 13:10:27 -07001190 * Read a single request into the userspace filesystem's buffer. This
1191 * function waits until a request is available, then removes it from
1192 * the pending list and copies request data to userspace buffer. If
Miklos Szeredif9a28422006-06-25 05:48:53 -07001193 * no reply is needed (FORGET) or request has been aborted or there
1194 * was an error during the copying then it's finished by calling
Miklos Szeredi334f4852005-09-09 13:10:27 -07001195 * request_end(). Otherwise add it to the processing list, and set
1196 * the 'sent' flag.
1197 */
Miklos Szeredic3021622010-05-25 15:06:07 +02001198static ssize_t fuse_dev_do_read(struct fuse_conn *fc, struct file *file,
1199 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001200{
1201 int err;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001202 struct fuse_req *req;
1203 struct fuse_in *in;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001204 unsigned reqsize;
1205
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001206 restart:
Miklos Szeredid7133112006-04-10 22:54:55 -07001207 spin_lock(&fc->lock);
Jeff Dikee5ac1d12006-04-10 22:54:53 -07001208 err = -EAGAIN;
1209 if ((file->f_flags & O_NONBLOCK) && fc->connected &&
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001210 !request_pending(fc))
Jeff Dikee5ac1d12006-04-10 22:54:53 -07001211 goto err_unlock;
1212
Miklos Szeredi334f4852005-09-09 13:10:27 -07001213 request_wait(fc);
1214 err = -ENODEV;
Miklos Szeredi9ba7cbb2006-01-16 22:14:34 -08001215 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001216 goto err_unlock;
1217 err = -ERESTARTSYS;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001218 if (!request_pending(fc))
Miklos Szeredi334f4852005-09-09 13:10:27 -07001219 goto err_unlock;
1220
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001221 if (!list_empty(&fc->interrupts)) {
1222 req = list_entry(fc->interrupts.next, struct fuse_req,
1223 intr_entry);
Miklos Szeredic3021622010-05-25 15:06:07 +02001224 return fuse_read_interrupt(fc, cs, nbytes, req);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001225 }
1226
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001227 if (forget_pending(fc)) {
1228 if (list_empty(&fc->pending) || fc->forget_batch-- > 0)
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001229 return fuse_read_forget(fc, cs, nbytes);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001230
1231 if (fc->forget_batch <= -8)
1232 fc->forget_batch = 16;
1233 }
1234
Miklos Szeredi334f4852005-09-09 13:10:27 -07001235 req = list_entry(fc->pending.next, struct fuse_req, list);
Miklos Szeredi83cfd492006-01-16 22:14:31 -08001236 req->state = FUSE_REQ_READING;
Miklos Szeredid77a1d52006-01-16 22:14:31 -08001237 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001238
1239 in = &req->in;
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001240 reqsize = in->h.len;
1241 /* If request is too large, reply with an error and restart the read */
Miklos Szeredic3021622010-05-25 15:06:07 +02001242 if (nbytes < reqsize) {
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001243 req->out.h.error = -EIO;
1244 /* SETXATTR is special, since it may contain too large data */
1245 if (in->h.opcode == FUSE_SETXATTR)
1246 req->out.h.error = -E2BIG;
1247 request_end(fc, req);
1248 goto restart;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001249 }
Miklos Szeredid7133112006-04-10 22:54:55 -07001250 spin_unlock(&fc->lock);
Miklos Szeredic3021622010-05-25 15:06:07 +02001251 cs->req = req;
1252 err = fuse_copy_one(cs, &in->h, sizeof(in->h));
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001253 if (!err)
Miklos Szeredic3021622010-05-25 15:06:07 +02001254 err = fuse_copy_args(cs, in->numargs, in->argpages,
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001255 (struct fuse_arg *) in->args, 0);
Miklos Szeredic3021622010-05-25 15:06:07 +02001256 fuse_copy_finish(cs);
Miklos Szeredid7133112006-04-10 22:54:55 -07001257 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001258 req->locked = 0;
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -07001259 if (req->aborted) {
1260 request_end(fc, req);
1261 return -ENODEV;
1262 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001263 if (err) {
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -07001264 req->out.h.error = -EIO;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001265 request_end(fc, req);
1266 return err;
1267 }
1268 if (!req->isreply)
1269 request_end(fc, req);
1270 else {
Miklos Szeredi83cfd492006-01-16 22:14:31 -08001271 req->state = FUSE_REQ_SENT;
Miklos Szeredid77a1d52006-01-16 22:14:31 -08001272 list_move_tail(&req->list, &fc->processing);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001273 if (req->interrupted)
1274 queue_interrupt(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -07001275 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001276 }
1277 return reqsize;
1278
1279 err_unlock:
Miklos Szeredid7133112006-04-10 22:54:55 -07001280 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001281 return err;
1282}
1283
Miklos Szeredic3021622010-05-25 15:06:07 +02001284static ssize_t fuse_dev_read(struct kiocb *iocb, const struct iovec *iov,
1285 unsigned long nr_segs, loff_t pos)
1286{
1287 struct fuse_copy_state cs;
1288 struct file *file = iocb->ki_filp;
1289 struct fuse_conn *fc = fuse_get_conn(file);
1290 if (!fc)
1291 return -EPERM;
1292
1293 fuse_copy_init(&cs, fc, 1, iov, nr_segs);
1294
1295 return fuse_dev_do_read(fc, file, &cs, iov_length(iov, nr_segs));
1296}
1297
1298static int fuse_dev_pipe_buf_steal(struct pipe_inode_info *pipe,
1299 struct pipe_buffer *buf)
1300{
1301 return 1;
1302}
1303
1304static const struct pipe_buf_operations fuse_dev_pipe_buf_ops = {
1305 .can_merge = 0,
1306 .map = generic_pipe_buf_map,
1307 .unmap = generic_pipe_buf_unmap,
1308 .confirm = generic_pipe_buf_confirm,
1309 .release = generic_pipe_buf_release,
1310 .steal = fuse_dev_pipe_buf_steal,
1311 .get = generic_pipe_buf_get,
1312};
1313
1314static ssize_t fuse_dev_splice_read(struct file *in, loff_t *ppos,
1315 struct pipe_inode_info *pipe,
1316 size_t len, unsigned int flags)
1317{
1318 int ret;
1319 int page_nr = 0;
1320 int do_wakeup = 0;
1321 struct pipe_buffer *bufs;
1322 struct fuse_copy_state cs;
1323 struct fuse_conn *fc = fuse_get_conn(in);
1324 if (!fc)
1325 return -EPERM;
1326
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001327 bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), GFP_KERNEL);
Miklos Szeredic3021622010-05-25 15:06:07 +02001328 if (!bufs)
1329 return -ENOMEM;
1330
1331 fuse_copy_init(&cs, fc, 1, NULL, 0);
1332 cs.pipebufs = bufs;
1333 cs.pipe = pipe;
1334 ret = fuse_dev_do_read(fc, in, &cs, len);
1335 if (ret < 0)
1336 goto out;
1337
1338 ret = 0;
1339 pipe_lock(pipe);
1340
1341 if (!pipe->readers) {
1342 send_sig(SIGPIPE, current, 0);
1343 if (!ret)
1344 ret = -EPIPE;
1345 goto out_unlock;
1346 }
1347
1348 if (pipe->nrbufs + cs.nr_segs > pipe->buffers) {
1349 ret = -EIO;
1350 goto out_unlock;
1351 }
1352
1353 while (page_nr < cs.nr_segs) {
1354 int newbuf = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
1355 struct pipe_buffer *buf = pipe->bufs + newbuf;
1356
1357 buf->page = bufs[page_nr].page;
1358 buf->offset = bufs[page_nr].offset;
1359 buf->len = bufs[page_nr].len;
1360 buf->ops = &fuse_dev_pipe_buf_ops;
1361
1362 pipe->nrbufs++;
1363 page_nr++;
1364 ret += buf->len;
1365
1366 if (pipe->inode)
1367 do_wakeup = 1;
1368 }
1369
1370out_unlock:
1371 pipe_unlock(pipe);
1372
1373 if (do_wakeup) {
1374 smp_mb();
1375 if (waitqueue_active(&pipe->wait))
1376 wake_up_interruptible(&pipe->wait);
1377 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
1378 }
1379
1380out:
1381 for (; page_nr < cs.nr_segs; page_nr++)
1382 page_cache_release(bufs[page_nr].page);
1383
1384 kfree(bufs);
1385 return ret;
1386}
1387
Tejun Heo95668a62008-11-26 12:03:55 +01001388static int fuse_notify_poll(struct fuse_conn *fc, unsigned int size,
1389 struct fuse_copy_state *cs)
1390{
1391 struct fuse_notify_poll_wakeup_out outarg;
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001392 int err = -EINVAL;
Tejun Heo95668a62008-11-26 12:03:55 +01001393
1394 if (size != sizeof(outarg))
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001395 goto err;
Tejun Heo95668a62008-11-26 12:03:55 +01001396
1397 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1398 if (err)
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001399 goto err;
Tejun Heo95668a62008-11-26 12:03:55 +01001400
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001401 fuse_copy_finish(cs);
Tejun Heo95668a62008-11-26 12:03:55 +01001402 return fuse_notify_poll_wakeup(fc, &outarg);
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001403
1404err:
1405 fuse_copy_finish(cs);
1406 return err;
Tejun Heo95668a62008-11-26 12:03:55 +01001407}
1408
John Muir3b463ae2009-05-31 11:13:57 -04001409static int fuse_notify_inval_inode(struct fuse_conn *fc, unsigned int size,
1410 struct fuse_copy_state *cs)
1411{
1412 struct fuse_notify_inval_inode_out outarg;
1413 int err = -EINVAL;
1414
1415 if (size != sizeof(outarg))
1416 goto err;
1417
1418 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1419 if (err)
1420 goto err;
1421 fuse_copy_finish(cs);
1422
1423 down_read(&fc->killsb);
1424 err = -ENOENT;
Miklos Szeredib21dda42010-02-05 12:08:31 +01001425 if (fc->sb) {
1426 err = fuse_reverse_inval_inode(fc->sb, outarg.ino,
1427 outarg.off, outarg.len);
1428 }
John Muir3b463ae2009-05-31 11:13:57 -04001429 up_read(&fc->killsb);
1430 return err;
1431
1432err:
1433 fuse_copy_finish(cs);
1434 return err;
1435}
1436
1437static int fuse_notify_inval_entry(struct fuse_conn *fc, unsigned int size,
1438 struct fuse_copy_state *cs)
1439{
1440 struct fuse_notify_inval_entry_out outarg;
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001441 int err = -ENOMEM;
1442 char *buf;
John Muir3b463ae2009-05-31 11:13:57 -04001443 struct qstr name;
1444
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001445 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1446 if (!buf)
1447 goto err;
1448
1449 err = -EINVAL;
John Muir3b463ae2009-05-31 11:13:57 -04001450 if (size < sizeof(outarg))
1451 goto err;
1452
1453 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1454 if (err)
1455 goto err;
1456
1457 err = -ENAMETOOLONG;
1458 if (outarg.namelen > FUSE_NAME_MAX)
1459 goto err;
1460
Miklos Szeredic2183d12011-08-24 10:20:17 +02001461 err = -EINVAL;
1462 if (size != sizeof(outarg) + outarg.namelen + 1)
1463 goto err;
1464
John Muir3b463ae2009-05-31 11:13:57 -04001465 name.name = buf;
1466 name.len = outarg.namelen;
1467 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1468 if (err)
1469 goto err;
1470 fuse_copy_finish(cs);
1471 buf[outarg.namelen] = 0;
1472 name.hash = full_name_hash(name.name, name.len);
1473
1474 down_read(&fc->killsb);
1475 err = -ENOENT;
Miklos Szeredib21dda42010-02-05 12:08:31 +01001476 if (fc->sb)
John Muir451d0f52011-12-06 21:50:06 +01001477 err = fuse_reverse_inval_entry(fc->sb, outarg.parent, 0, &name);
1478 up_read(&fc->killsb);
1479 kfree(buf);
1480 return err;
1481
1482err:
1483 kfree(buf);
1484 fuse_copy_finish(cs);
1485 return err;
1486}
1487
1488static int fuse_notify_delete(struct fuse_conn *fc, unsigned int size,
1489 struct fuse_copy_state *cs)
1490{
1491 struct fuse_notify_delete_out outarg;
1492 int err = -ENOMEM;
1493 char *buf;
1494 struct qstr name;
1495
1496 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1497 if (!buf)
1498 goto err;
1499
1500 err = -EINVAL;
1501 if (size < sizeof(outarg))
1502 goto err;
1503
1504 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1505 if (err)
1506 goto err;
1507
1508 err = -ENAMETOOLONG;
1509 if (outarg.namelen > FUSE_NAME_MAX)
1510 goto err;
1511
1512 err = -EINVAL;
1513 if (size != sizeof(outarg) + outarg.namelen + 1)
1514 goto err;
1515
1516 name.name = buf;
1517 name.len = outarg.namelen;
1518 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1519 if (err)
1520 goto err;
1521 fuse_copy_finish(cs);
1522 buf[outarg.namelen] = 0;
1523 name.hash = full_name_hash(name.name, name.len);
1524
1525 down_read(&fc->killsb);
1526 err = -ENOENT;
1527 if (fc->sb)
1528 err = fuse_reverse_inval_entry(fc->sb, outarg.parent,
1529 outarg.child, &name);
John Muir3b463ae2009-05-31 11:13:57 -04001530 up_read(&fc->killsb);
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001531 kfree(buf);
John Muir3b463ae2009-05-31 11:13:57 -04001532 return err;
1533
1534err:
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001535 kfree(buf);
John Muir3b463ae2009-05-31 11:13:57 -04001536 fuse_copy_finish(cs);
1537 return err;
1538}
1539
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001540static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
1541 struct fuse_copy_state *cs)
1542{
1543 struct fuse_notify_store_out outarg;
1544 struct inode *inode;
1545 struct address_space *mapping;
1546 u64 nodeid;
1547 int err;
1548 pgoff_t index;
1549 unsigned int offset;
1550 unsigned int num;
1551 loff_t file_size;
1552 loff_t end;
1553
1554 err = -EINVAL;
1555 if (size < sizeof(outarg))
1556 goto out_finish;
1557
1558 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1559 if (err)
1560 goto out_finish;
1561
1562 err = -EINVAL;
1563 if (size - sizeof(outarg) != outarg.size)
1564 goto out_finish;
1565
1566 nodeid = outarg.nodeid;
1567
1568 down_read(&fc->killsb);
1569
1570 err = -ENOENT;
1571 if (!fc->sb)
1572 goto out_up_killsb;
1573
1574 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1575 if (!inode)
1576 goto out_up_killsb;
1577
1578 mapping = inode->i_mapping;
1579 index = outarg.offset >> PAGE_CACHE_SHIFT;
1580 offset = outarg.offset & ~PAGE_CACHE_MASK;
1581 file_size = i_size_read(inode);
1582 end = outarg.offset + outarg.size;
1583 if (end > file_size) {
1584 file_size = end;
1585 fuse_write_update_size(inode, file_size);
1586 }
1587
1588 num = outarg.size;
1589 while (num) {
1590 struct page *page;
1591 unsigned int this_num;
1592
1593 err = -ENOMEM;
1594 page = find_or_create_page(mapping, index,
1595 mapping_gfp_mask(mapping));
1596 if (!page)
1597 goto out_iput;
1598
1599 this_num = min_t(unsigned, num, PAGE_CACHE_SIZE - offset);
1600 err = fuse_copy_page(cs, &page, offset, this_num, 0);
1601 if (!err && offset == 0 && (num != 0 || file_size == end))
1602 SetPageUptodate(page);
1603 unlock_page(page);
1604 page_cache_release(page);
1605
1606 if (err)
1607 goto out_iput;
1608
1609 num -= this_num;
1610 offset = 0;
1611 index++;
1612 }
1613
1614 err = 0;
1615
1616out_iput:
1617 iput(inode);
1618out_up_killsb:
1619 up_read(&fc->killsb);
1620out_finish:
1621 fuse_copy_finish(cs);
1622 return err;
1623}
1624
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001625static void fuse_retrieve_end(struct fuse_conn *fc, struct fuse_req *req)
1626{
Miklos Szeredi0be85572010-10-27 15:34:46 -07001627 release_pages(req->pages, req->num_pages, 0);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001628}
1629
1630static int fuse_retrieve(struct fuse_conn *fc, struct inode *inode,
1631 struct fuse_notify_retrieve_out *outarg)
1632{
1633 int err;
1634 struct address_space *mapping = inode->i_mapping;
1635 struct fuse_req *req;
1636 pgoff_t index;
1637 loff_t file_size;
1638 unsigned int num;
1639 unsigned int offset;
Geert Uytterhoeven01574432010-09-30 22:06:21 +02001640 size_t total_len = 0;
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001641 int num_pages;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001642
1643 offset = outarg->offset & ~PAGE_CACHE_MASK;
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001644 file_size = i_size_read(inode);
1645
1646 num = outarg->size;
1647 if (outarg->offset > file_size)
1648 num = 0;
1649 else if (outarg->offset + num > file_size)
1650 num = file_size - outarg->offset;
1651
1652 num_pages = (num + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1653 num_pages = min(num_pages, FUSE_MAX_PAGES_PER_REQ);
1654
1655 req = fuse_get_req(fc, num_pages);
1656 if (IS_ERR(req))
1657 return PTR_ERR(req);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001658
1659 req->in.h.opcode = FUSE_NOTIFY_REPLY;
1660 req->in.h.nodeid = outarg->nodeid;
1661 req->in.numargs = 2;
1662 req->in.argpages = 1;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001663 req->page_descs[0].offset = offset;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001664 req->end = fuse_retrieve_end;
1665
1666 index = outarg->offset >> PAGE_CACHE_SHIFT;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001667
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001668 while (num && req->num_pages < num_pages) {
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001669 struct page *page;
1670 unsigned int this_num;
1671
1672 page = find_get_page(mapping, index);
1673 if (!page)
1674 break;
1675
1676 this_num = min_t(unsigned, num, PAGE_CACHE_SIZE - offset);
1677 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001678 req->page_descs[req->num_pages].length = this_num;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001679 req->num_pages++;
1680
Miklos Szeredic9e67d42012-09-04 18:45:54 +02001681 offset = 0;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001682 num -= this_num;
1683 total_len += this_num;
Miklos Szeredi48706d02011-12-13 10:36:59 +01001684 index++;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001685 }
1686 req->misc.retrieve_in.offset = outarg->offset;
1687 req->misc.retrieve_in.size = total_len;
1688 req->in.args[0].size = sizeof(req->misc.retrieve_in);
1689 req->in.args[0].value = &req->misc.retrieve_in;
1690 req->in.args[1].size = total_len;
1691
1692 err = fuse_request_send_notify_reply(fc, req, outarg->notify_unique);
1693 if (err)
1694 fuse_retrieve_end(fc, req);
1695
1696 return err;
1697}
1698
1699static int fuse_notify_retrieve(struct fuse_conn *fc, unsigned int size,
1700 struct fuse_copy_state *cs)
1701{
1702 struct fuse_notify_retrieve_out outarg;
1703 struct inode *inode;
1704 int err;
1705
1706 err = -EINVAL;
1707 if (size != sizeof(outarg))
1708 goto copy_finish;
1709
1710 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1711 if (err)
1712 goto copy_finish;
1713
1714 fuse_copy_finish(cs);
1715
1716 down_read(&fc->killsb);
1717 err = -ENOENT;
1718 if (fc->sb) {
1719 u64 nodeid = outarg.nodeid;
1720
1721 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1722 if (inode) {
1723 err = fuse_retrieve(fc, inode, &outarg);
1724 iput(inode);
1725 }
1726 }
1727 up_read(&fc->killsb);
1728
1729 return err;
1730
1731copy_finish:
1732 fuse_copy_finish(cs);
1733 return err;
1734}
1735
Tejun Heo85993962008-11-26 12:03:55 +01001736static int fuse_notify(struct fuse_conn *fc, enum fuse_notify_code code,
1737 unsigned int size, struct fuse_copy_state *cs)
1738{
1739 switch (code) {
Tejun Heo95668a62008-11-26 12:03:55 +01001740 case FUSE_NOTIFY_POLL:
1741 return fuse_notify_poll(fc, size, cs);
1742
John Muir3b463ae2009-05-31 11:13:57 -04001743 case FUSE_NOTIFY_INVAL_INODE:
1744 return fuse_notify_inval_inode(fc, size, cs);
1745
1746 case FUSE_NOTIFY_INVAL_ENTRY:
1747 return fuse_notify_inval_entry(fc, size, cs);
1748
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001749 case FUSE_NOTIFY_STORE:
1750 return fuse_notify_store(fc, size, cs);
1751
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001752 case FUSE_NOTIFY_RETRIEVE:
1753 return fuse_notify_retrieve(fc, size, cs);
1754
John Muir451d0f52011-12-06 21:50:06 +01001755 case FUSE_NOTIFY_DELETE:
1756 return fuse_notify_delete(fc, size, cs);
1757
Tejun Heo85993962008-11-26 12:03:55 +01001758 default:
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001759 fuse_copy_finish(cs);
Tejun Heo85993962008-11-26 12:03:55 +01001760 return -EINVAL;
1761 }
1762}
1763
Miklos Szeredi334f4852005-09-09 13:10:27 -07001764/* Look up request on processing list by unique ID */
1765static struct fuse_req *request_find(struct fuse_conn *fc, u64 unique)
1766{
1767 struct list_head *entry;
1768
1769 list_for_each(entry, &fc->processing) {
1770 struct fuse_req *req;
1771 req = list_entry(entry, struct fuse_req, list);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001772 if (req->in.h.unique == unique || req->intr_unique == unique)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001773 return req;
1774 }
1775 return NULL;
1776}
1777
1778static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
1779 unsigned nbytes)
1780{
1781 unsigned reqsize = sizeof(struct fuse_out_header);
1782
1783 if (out->h.error)
1784 return nbytes != reqsize ? -EINVAL : 0;
1785
1786 reqsize += len_args(out->numargs, out->args);
1787
1788 if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
1789 return -EINVAL;
1790 else if (reqsize > nbytes) {
1791 struct fuse_arg *lastarg = &out->args[out->numargs-1];
1792 unsigned diffsize = reqsize - nbytes;
1793 if (diffsize > lastarg->size)
1794 return -EINVAL;
1795 lastarg->size -= diffsize;
1796 }
1797 return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
1798 out->page_zeroing);
1799}
1800
1801/*
1802 * Write a single reply to a request. First the header is copied from
1803 * the write buffer. The request is then searched on the processing
1804 * list by the unique ID found in the header. If found, then remove
1805 * it from the list and copy the rest of the buffer to the request.
1806 * The request is finished by calling request_end()
1807 */
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001808static ssize_t fuse_dev_do_write(struct fuse_conn *fc,
1809 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001810{
1811 int err;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001812 struct fuse_req *req;
1813 struct fuse_out_header oh;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001814
Miklos Szeredi334f4852005-09-09 13:10:27 -07001815 if (nbytes < sizeof(struct fuse_out_header))
1816 return -EINVAL;
1817
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001818 err = fuse_copy_one(cs, &oh, sizeof(oh));
Miklos Szeredi334f4852005-09-09 13:10:27 -07001819 if (err)
1820 goto err_finish;
Tejun Heo85993962008-11-26 12:03:55 +01001821
Miklos Szeredi334f4852005-09-09 13:10:27 -07001822 err = -EINVAL;
Tejun Heo85993962008-11-26 12:03:55 +01001823 if (oh.len != nbytes)
1824 goto err_finish;
1825
1826 /*
1827 * Zero oh.unique indicates unsolicited notification message
1828 * and error contains notification code.
1829 */
1830 if (!oh.unique) {
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001831 err = fuse_notify(fc, oh.error, nbytes - sizeof(oh), cs);
Tejun Heo85993962008-11-26 12:03:55 +01001832 return err ? err : nbytes;
1833 }
1834
1835 err = -EINVAL;
1836 if (oh.error <= -1000 || oh.error > 0)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001837 goto err_finish;
1838
Miklos Szeredid7133112006-04-10 22:54:55 -07001839 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001840 err = -ENOENT;
1841 if (!fc->connected)
1842 goto err_unlock;
1843
Miklos Szeredi334f4852005-09-09 13:10:27 -07001844 req = request_find(fc, oh.unique);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001845 if (!req)
1846 goto err_unlock;
1847
Miklos Szeredif9a28422006-06-25 05:48:53 -07001848 if (req->aborted) {
Miklos Szeredid7133112006-04-10 22:54:55 -07001849 spin_unlock(&fc->lock);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001850 fuse_copy_finish(cs);
Miklos Szeredid7133112006-04-10 22:54:55 -07001851 spin_lock(&fc->lock);
Miklos Szeredi222f1d62006-01-16 22:14:25 -08001852 request_end(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001853 return -ENOENT;
1854 }
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001855 /* Is it an interrupt reply? */
1856 if (req->intr_unique == oh.unique) {
1857 err = -EINVAL;
1858 if (nbytes != sizeof(struct fuse_out_header))
1859 goto err_unlock;
1860
1861 if (oh.error == -ENOSYS)
1862 fc->no_interrupt = 1;
1863 else if (oh.error == -EAGAIN)
1864 queue_interrupt(fc, req);
1865
1866 spin_unlock(&fc->lock);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001867 fuse_copy_finish(cs);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001868 return nbytes;
1869 }
1870
1871 req->state = FUSE_REQ_WRITING;
Miklos Szeredid77a1d52006-01-16 22:14:31 -08001872 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001873 req->out.h = oh;
1874 req->locked = 1;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001875 cs->req = req;
Miklos Szeredice534fb2010-05-25 15:06:07 +02001876 if (!req->out.page_replace)
1877 cs->move_pages = 0;
Miklos Szeredid7133112006-04-10 22:54:55 -07001878 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001879
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001880 err = copy_out_args(cs, &req->out, nbytes);
1881 fuse_copy_finish(cs);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001882
Miklos Szeredid7133112006-04-10 22:54:55 -07001883 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001884 req->locked = 0;
1885 if (!err) {
Miklos Szeredif9a28422006-06-25 05:48:53 -07001886 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001887 err = -ENOENT;
Miklos Szeredif9a28422006-06-25 05:48:53 -07001888 } else if (!req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001889 req->out.h.error = -EIO;
1890 request_end(fc, req);
1891
1892 return err ? err : nbytes;
1893
1894 err_unlock:
Miklos Szeredid7133112006-04-10 22:54:55 -07001895 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001896 err_finish:
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001897 fuse_copy_finish(cs);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001898 return err;
1899}
1900
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001901static ssize_t fuse_dev_write(struct kiocb *iocb, const struct iovec *iov,
1902 unsigned long nr_segs, loff_t pos)
1903{
1904 struct fuse_copy_state cs;
1905 struct fuse_conn *fc = fuse_get_conn(iocb->ki_filp);
1906 if (!fc)
1907 return -EPERM;
1908
Miklos Szeredic3021622010-05-25 15:06:07 +02001909 fuse_copy_init(&cs, fc, 0, iov, nr_segs);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001910
1911 return fuse_dev_do_write(fc, &cs, iov_length(iov, nr_segs));
1912}
1913
1914static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe,
1915 struct file *out, loff_t *ppos,
1916 size_t len, unsigned int flags)
1917{
1918 unsigned nbuf;
1919 unsigned idx;
1920 struct pipe_buffer *bufs;
1921 struct fuse_copy_state cs;
1922 struct fuse_conn *fc;
1923 size_t rem;
1924 ssize_t ret;
1925
1926 fc = fuse_get_conn(out);
1927 if (!fc)
1928 return -EPERM;
1929
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001930 bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), GFP_KERNEL);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001931 if (!bufs)
1932 return -ENOMEM;
1933
1934 pipe_lock(pipe);
1935 nbuf = 0;
1936 rem = 0;
1937 for (idx = 0; idx < pipe->nrbufs && rem < len; idx++)
1938 rem += pipe->bufs[(pipe->curbuf + idx) & (pipe->buffers - 1)].len;
1939
1940 ret = -EINVAL;
1941 if (rem < len) {
1942 pipe_unlock(pipe);
1943 goto out;
1944 }
1945
1946 rem = len;
1947 while (rem) {
1948 struct pipe_buffer *ibuf;
1949 struct pipe_buffer *obuf;
1950
1951 BUG_ON(nbuf >= pipe->buffers);
1952 BUG_ON(!pipe->nrbufs);
1953 ibuf = &pipe->bufs[pipe->curbuf];
1954 obuf = &bufs[nbuf];
1955
1956 if (rem >= ibuf->len) {
1957 *obuf = *ibuf;
1958 ibuf->ops = NULL;
1959 pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
1960 pipe->nrbufs--;
1961 } else {
1962 ibuf->ops->get(pipe, ibuf);
1963 *obuf = *ibuf;
1964 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1965 obuf->len = rem;
1966 ibuf->offset += obuf->len;
1967 ibuf->len -= obuf->len;
1968 }
1969 nbuf++;
1970 rem -= obuf->len;
1971 }
1972 pipe_unlock(pipe);
1973
Miklos Szeredic3021622010-05-25 15:06:07 +02001974 fuse_copy_init(&cs, fc, 0, NULL, nbuf);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001975 cs.pipebufs = bufs;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001976 cs.pipe = pipe;
1977
Miklos Szeredice534fb2010-05-25 15:06:07 +02001978 if (flags & SPLICE_F_MOVE)
1979 cs.move_pages = 1;
1980
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001981 ret = fuse_dev_do_write(fc, &cs, len);
1982
1983 for (idx = 0; idx < nbuf; idx++) {
1984 struct pipe_buffer *buf = &bufs[idx];
1985 buf->ops->release(pipe, buf);
1986 }
1987out:
1988 kfree(bufs);
1989 return ret;
1990}
1991
Miklos Szeredi334f4852005-09-09 13:10:27 -07001992static unsigned fuse_dev_poll(struct file *file, poll_table *wait)
1993{
Miklos Szeredi334f4852005-09-09 13:10:27 -07001994 unsigned mask = POLLOUT | POLLWRNORM;
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07001995 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001996 if (!fc)
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07001997 return POLLERR;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001998
1999 poll_wait(file, &fc->waitq, wait);
2000
Miklos Szeredid7133112006-04-10 22:54:55 -07002001 spin_lock(&fc->lock);
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07002002 if (!fc->connected)
2003 mask = POLLERR;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07002004 else if (request_pending(fc))
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07002005 mask |= POLLIN | POLLRDNORM;
Miklos Szeredid7133112006-04-10 22:54:55 -07002006 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002007
2008 return mask;
2009}
2010
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002011/*
2012 * Abort all requests on the given list (pending or processing)
2013 *
Miklos Szeredid7133112006-04-10 22:54:55 -07002014 * This function releases and reacquires fc->lock
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002015 */
Miklos Szeredi334f4852005-09-09 13:10:27 -07002016static void end_requests(struct fuse_conn *fc, struct list_head *head)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02002017__releases(fc->lock)
2018__acquires(fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -07002019{
2020 while (!list_empty(head)) {
2021 struct fuse_req *req;
2022 req = list_entry(head->next, struct fuse_req, list);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002023 req->out.h.error = -ECONNABORTED;
2024 request_end(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -07002025 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002026 }
2027}
2028
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002029/*
2030 * Abort requests under I/O
2031 *
Miklos Szeredif9a28422006-06-25 05:48:53 -07002032 * The requests are set to aborted and finished, and the request
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002033 * waiter is woken up. This will make request_wait_answer() wait
2034 * until the request is unlocked and then return.
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08002035 *
2036 * If the request is asynchronous, then the end function needs to be
2037 * called after waiting for the request to be unlocked (if it was
2038 * locked).
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002039 */
2040static void end_io_requests(struct fuse_conn *fc)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02002041__releases(fc->lock)
2042__acquires(fc->lock)
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002043{
2044 while (!list_empty(&fc->io)) {
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08002045 struct fuse_req *req =
2046 list_entry(fc->io.next, struct fuse_req, list);
2047 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
2048
Miklos Szeredif9a28422006-06-25 05:48:53 -07002049 req->aborted = 1;
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002050 req->out.h.error = -ECONNABORTED;
2051 req->state = FUSE_REQ_FINISHED;
2052 list_del_init(&req->list);
2053 wake_up(&req->waitq);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08002054 if (end) {
2055 req->end = NULL;
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08002056 __fuse_get_request(req);
Miklos Szeredid7133112006-04-10 22:54:55 -07002057 spin_unlock(&fc->lock);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08002058 wait_event(req->waitq, !req->locked);
2059 end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +01002060 fuse_put_request(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -07002061 spin_lock(&fc->lock);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08002062 }
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002063 }
2064}
2065
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002066static void end_queued_requests(struct fuse_conn *fc)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02002067__releases(fc->lock)
2068__acquires(fc->lock)
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002069{
2070 fc->max_background = UINT_MAX;
2071 flush_bg_queue(fc);
2072 end_requests(fc, &fc->pending);
2073 end_requests(fc, &fc->processing);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01002074 while (forget_pending(fc))
Miklos Szeredi02c048b2010-12-07 20:16:56 +01002075 kfree(dequeue_forget(fc, 1, NULL));
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002076}
2077
Bryan Green357ccf22011-03-01 16:43:52 -08002078static void end_polls(struct fuse_conn *fc)
2079{
2080 struct rb_node *p;
2081
2082 p = rb_first(&fc->polled_files);
2083
2084 while (p) {
2085 struct fuse_file *ff;
2086 ff = rb_entry(p, struct fuse_file, polled_node);
2087 wake_up_interruptible_all(&ff->poll_wait);
2088
2089 p = rb_next(p);
2090 }
2091}
2092
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002093/*
2094 * Abort all requests.
2095 *
2096 * Emergency exit in case of a malicious or accidental deadlock, or
2097 * just a hung filesystem.
2098 *
2099 * The same effect is usually achievable through killing the
2100 * filesystem daemon and all users of the filesystem. The exception
2101 * is the combination of an asynchronous request and the tricky
2102 * deadlock (see Documentation/filesystems/fuse.txt).
2103 *
2104 * During the aborting, progression of requests from the pending and
2105 * processing lists onto the io list, and progression of new requests
2106 * onto the pending list is prevented by req->connected being false.
2107 *
2108 * Progression of requests under I/O to the processing list is
Miklos Szeredif9a28422006-06-25 05:48:53 -07002109 * prevented by the req->aborted flag being true for these requests.
2110 * For this reason requests on the io list must be aborted first.
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002111 */
2112void fuse_abort_conn(struct fuse_conn *fc)
2113{
Miklos Szeredid7133112006-04-10 22:54:55 -07002114 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002115 if (fc->connected) {
2116 fc->connected = 0;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -07002117 fc->blocked = 0;
Maxim Patlasov796523fb2013-03-21 18:02:15 +04002118 fc->initialized = 1;
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002119 end_io_requests(fc);
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002120 end_queued_requests(fc);
Bryan Green357ccf22011-03-01 16:43:52 -08002121 end_polls(fc);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002122 wake_up_all(&fc->waitq);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -07002123 wake_up_all(&fc->blocked_waitq);
Jeff Dike385a17b2006-04-10 22:54:52 -07002124 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002125 }
Miklos Szeredid7133112006-04-10 22:54:55 -07002126 spin_unlock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002127}
Tejun Heo08cbf542009-04-14 10:54:53 +09002128EXPORT_SYMBOL_GPL(fuse_abort_conn);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002129
Tejun Heo08cbf542009-04-14 10:54:53 +09002130int fuse_dev_release(struct inode *inode, struct file *file)
Miklos Szeredi334f4852005-09-09 13:10:27 -07002131{
Miklos Szeredi0720b312006-04-10 22:54:55 -07002132 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002133 if (fc) {
Miklos Szeredid7133112006-04-10 22:54:55 -07002134 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -07002135 fc->connected = 0;
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002136 fc->blocked = 0;
Maxim Patlasov0aada882013-03-21 18:02:28 +04002137 fc->initialized = 1;
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002138 end_queued_requests(fc);
Bryan Green357ccf22011-03-01 16:43:52 -08002139 end_polls(fc);
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002140 wake_up_all(&fc->blocked_waitq);
Miklos Szeredid7133112006-04-10 22:54:55 -07002141 spin_unlock(&fc->lock);
Miklos Szeredibafa9652006-06-25 05:48:51 -07002142 fuse_conn_put(fc);
Jeff Dike385a17b2006-04-10 22:54:52 -07002143 }
Miklos Szeredif543f252006-01-16 22:14:35 -08002144
Miklos Szeredi334f4852005-09-09 13:10:27 -07002145 return 0;
2146}
Tejun Heo08cbf542009-04-14 10:54:53 +09002147EXPORT_SYMBOL_GPL(fuse_dev_release);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002148
Jeff Dike385a17b2006-04-10 22:54:52 -07002149static int fuse_dev_fasync(int fd, struct file *file, int on)
2150{
2151 struct fuse_conn *fc = fuse_get_conn(file);
2152 if (!fc)
Miklos Szeredia87046d2006-04-10 22:54:56 -07002153 return -EPERM;
Jeff Dike385a17b2006-04-10 22:54:52 -07002154
2155 /* No locking - fasync_helper does its own locking */
2156 return fasync_helper(fd, file, on, &fc->fasync);
2157}
2158
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002159const struct file_operations fuse_dev_operations = {
Miklos Szeredi334f4852005-09-09 13:10:27 -07002160 .owner = THIS_MODULE,
2161 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002162 .read = do_sync_read,
2163 .aio_read = fuse_dev_read,
Miklos Szeredic3021622010-05-25 15:06:07 +02002164 .splice_read = fuse_dev_splice_read,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002165 .write = do_sync_write,
2166 .aio_write = fuse_dev_write,
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002167 .splice_write = fuse_dev_splice_write,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002168 .poll = fuse_dev_poll,
2169 .release = fuse_dev_release,
Jeff Dike385a17b2006-04-10 22:54:52 -07002170 .fasync = fuse_dev_fasync,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002171};
Tejun Heo08cbf542009-04-14 10:54:53 +09002172EXPORT_SYMBOL_GPL(fuse_dev_operations);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002173
2174static struct miscdevice fuse_miscdevice = {
2175 .minor = FUSE_MINOR,
2176 .name = "fuse",
2177 .fops = &fuse_dev_operations,
2178};
2179
2180int __init fuse_dev_init(void)
2181{
2182 int err = -ENOMEM;
2183 fuse_req_cachep = kmem_cache_create("fuse_request",
2184 sizeof(struct fuse_req),
Paul Mundt20c2df82007-07-20 10:11:58 +09002185 0, 0, NULL);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002186 if (!fuse_req_cachep)
2187 goto out;
2188
2189 err = misc_register(&fuse_miscdevice);
2190 if (err)
2191 goto out_cache_clean;
2192
2193 return 0;
2194
2195 out_cache_clean:
2196 kmem_cache_destroy(fuse_req_cachep);
2197 out:
2198 return err;
2199}
2200
2201void fuse_dev_cleanup(void)
2202{
2203 misc_deregister(&fuse_miscdevice);
2204 kmem_cache_destroy(fuse_req_cachep);
2205}