blob: 39c2fe3ba93d07e20cc06277a6d0cc00e6b5576c [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 Patlasov8b41e672013-03-21 18:02:04 +0400133static struct fuse_req *__fuse_get_req(struct fuse_conn *fc, unsigned npages,
134 bool for_background)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700135{
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700136 struct fuse_req *req;
137 sigset_t oldset;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200138 int intr;
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700139 int err;
140
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200141 atomic_inc(&fc->num_waiting);
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700142 block_sigs(&oldset);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200143 intr = wait_event_interruptible(fc->blocked_waitq, !fc->blocked);
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700144 restore_sigs(&oldset);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200145 err = -EINTR;
146 if (intr)
147 goto out;
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700148
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700149 err = -ENOTCONN;
150 if (!fc->connected)
151 goto out;
152
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400153 req = fuse_request_alloc(npages);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200154 err = -ENOMEM;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700155 if (!req)
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200156 goto out;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700157
Miklos Szeredi33649c92006-06-25 05:48:52 -0700158 fuse_req_init_context(req);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200159 req->waiting = 1;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400160 req->background = for_background;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700161 return req;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200162
163 out:
164 atomic_dec(&fc->num_waiting);
165 return ERR_PTR(err);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700166}
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400167
168struct fuse_req *fuse_get_req(struct fuse_conn *fc, unsigned npages)
169{
170 return __fuse_get_req(fc, npages, false);
171}
Tejun Heo08cbf542009-04-14 10:54:53 +0900172EXPORT_SYMBOL_GPL(fuse_get_req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700173
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400174struct fuse_req *fuse_get_req_for_background(struct fuse_conn *fc,
175 unsigned npages)
176{
177 return __fuse_get_req(fc, npages, true);
178}
179EXPORT_SYMBOL_GPL(fuse_get_req_for_background);
180
Miklos Szeredi33649c92006-06-25 05:48:52 -0700181/*
182 * Return request in fuse_file->reserved_req. However that may
183 * currently be in use. If that is the case, wait for it to become
184 * available.
185 */
186static struct fuse_req *get_reserved_req(struct fuse_conn *fc,
187 struct file *file)
188{
189 struct fuse_req *req = NULL;
190 struct fuse_file *ff = file->private_data;
191
192 do {
Miklos Szeredide5e3de2007-10-16 23:31:00 -0700193 wait_event(fc->reserved_req_waitq, ff->reserved_req);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700194 spin_lock(&fc->lock);
195 if (ff->reserved_req) {
196 req = ff->reserved_req;
197 ff->reserved_req = NULL;
Al Virocb0942b2012-08-27 14:48:26 -0400198 req->stolen_file = get_file(file);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700199 }
200 spin_unlock(&fc->lock);
201 } while (!req);
202
203 return req;
204}
205
206/*
207 * Put stolen request back into fuse_file->reserved_req
208 */
209static void put_reserved_req(struct fuse_conn *fc, struct fuse_req *req)
210{
211 struct file *file = req->stolen_file;
212 struct fuse_file *ff = file->private_data;
213
214 spin_lock(&fc->lock);
Maxim Patlasovb2430d72012-10-26 19:49:24 +0400215 fuse_request_init(req, req->pages, req->page_descs, req->max_pages);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700216 BUG_ON(ff->reserved_req);
217 ff->reserved_req = req;
Miklos Szeredide5e3de2007-10-16 23:31:00 -0700218 wake_up_all(&fc->reserved_req_waitq);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700219 spin_unlock(&fc->lock);
220 fput(file);
221}
222
223/*
224 * Gets a requests for a file operation, always succeeds
225 *
226 * This is used for sending the FLUSH request, which must get to
227 * userspace, due to POSIX locks which may need to be unlocked.
228 *
229 * If allocation fails due to OOM, use the reserved request in
230 * fuse_file.
231 *
232 * This is very unlikely to deadlock accidentally, since the
233 * filesystem should not have it's own file open. If deadlock is
234 * intentional, it can still be broken by "aborting" the filesystem.
235 */
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400236struct fuse_req *fuse_get_req_nofail_nopages(struct fuse_conn *fc,
237 struct file *file)
Miklos Szeredi33649c92006-06-25 05:48:52 -0700238{
239 struct fuse_req *req;
240
241 atomic_inc(&fc->num_waiting);
242 wait_event(fc->blocked_waitq, !fc->blocked);
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400243 req = fuse_request_alloc(0);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700244 if (!req)
245 req = get_reserved_req(fc, file);
246
247 fuse_req_init_context(req);
248 req->waiting = 1;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400249 req->background = 0;
Miklos Szeredi33649c92006-06-25 05:48:52 -0700250 return req;
251}
252
Miklos Szeredi334f4852005-09-09 13:10:27 -0700253void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req)
254{
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800255 if (atomic_dec_and_test(&req->count)) {
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200256 if (req->waiting)
257 atomic_dec(&fc->num_waiting);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700258
259 if (req->stolen_file)
260 put_reserved_req(fc, req);
261 else
262 fuse_request_free(req);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800263 }
264}
Tejun Heo08cbf542009-04-14 10:54:53 +0900265EXPORT_SYMBOL_GPL(fuse_put_request);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800266
Miklos Szeredid12def12008-02-06 01:38:39 -0800267static unsigned len_args(unsigned numargs, struct fuse_arg *args)
268{
269 unsigned nbytes = 0;
270 unsigned i;
271
272 for (i = 0; i < numargs; i++)
273 nbytes += args[i].size;
274
275 return nbytes;
276}
277
278static u64 fuse_get_unique(struct fuse_conn *fc)
279{
280 fc->reqctr++;
281 /* zero is special */
282 if (fc->reqctr == 0)
283 fc->reqctr = 1;
284
285 return fc->reqctr;
286}
287
288static void queue_request(struct fuse_conn *fc, struct fuse_req *req)
289{
Miklos Szeredid12def12008-02-06 01:38:39 -0800290 req->in.h.len = sizeof(struct fuse_in_header) +
291 len_args(req->in.numargs, (struct fuse_arg *) req->in.args);
292 list_add_tail(&req->list, &fc->pending);
293 req->state = FUSE_REQ_PENDING;
294 if (!req->waiting) {
295 req->waiting = 1;
296 atomic_inc(&fc->num_waiting);
297 }
298 wake_up(&fc->waitq);
299 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
300}
301
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100302void fuse_queue_forget(struct fuse_conn *fc, struct fuse_forget_link *forget,
303 u64 nodeid, u64 nlookup)
304{
Miklos Szeredi02c048b2010-12-07 20:16:56 +0100305 forget->forget_one.nodeid = nodeid;
306 forget->forget_one.nlookup = nlookup;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100307
308 spin_lock(&fc->lock);
Miklos Szeredi5dfcc872011-09-12 09:38:03 +0200309 if (fc->connected) {
310 fc->forget_list_tail->next = forget;
311 fc->forget_list_tail = forget;
312 wake_up(&fc->waitq);
313 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
314 } else {
315 kfree(forget);
316 }
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100317 spin_unlock(&fc->lock);
318}
319
Miklos Szeredid12def12008-02-06 01:38:39 -0800320static void flush_bg_queue(struct fuse_conn *fc)
321{
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700322 while (fc->active_background < fc->max_background &&
Miklos Szeredid12def12008-02-06 01:38:39 -0800323 !list_empty(&fc->bg_queue)) {
324 struct fuse_req *req;
325
326 req = list_entry(fc->bg_queue.next, struct fuse_req, list);
327 list_del(&req->list);
328 fc->active_background++;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200329 req->in.h.unique = fuse_get_unique(fc);
Miklos Szeredid12def12008-02-06 01:38:39 -0800330 queue_request(fc, req);
331 }
332}
333
Miklos Szeredi6dbbcb12006-04-26 10:49:06 +0200334/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700335 * This function is called when a request is finished. Either a reply
Miklos Szeredif9a28422006-06-25 05:48:53 -0700336 * has arrived or it was aborted (and not yet sent) or some error
Miklos Szeredif43b1552006-01-16 22:14:26 -0800337 * occurred during communication with userspace, or the device file
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700338 * was closed. The requester thread is woken up (if still waiting),
339 * the 'end' callback is called if given, else the reference to the
340 * request is released
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800341 *
Miklos Szeredid7133112006-04-10 22:54:55 -0700342 * Called with fc->lock, unlocks it
Miklos Szeredi334f4852005-09-09 13:10:27 -0700343 */
344static void request_end(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +0200345__releases(fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700346{
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700347 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
348 req->end = NULL;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800349 list_del(&req->list);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700350 list_del(&req->intr_entry);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800351 req->state = FUSE_REQ_FINISHED;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700352 if (req->background) {
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700353 if (fc->num_background == fc->max_background) {
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700354 fc->blocked = 0;
355 wake_up_all(&fc->blocked_waitq);
356 }
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700357 if (fc->num_background == fc->congestion_threshold &&
Tejun Heoa325f9b2009-04-14 10:54:52 +0900358 fc->connected && fc->bdi_initialized) {
Jens Axboe8aa7e842009-07-09 14:52:32 +0200359 clear_bdi_congested(&fc->bdi, BLK_RW_SYNC);
360 clear_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
Miklos Szeredif92b99b2007-10-16 23:30:59 -0700361 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700362 fc->num_background--;
Miklos Szeredid12def12008-02-06 01:38:39 -0800363 fc->active_background--;
364 flush_bg_queue(fc);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700365 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700366 spin_unlock(&fc->lock);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700367 wake_up(&req->waitq);
368 if (end)
369 end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100370 fuse_put_request(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700371}
372
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700373static void wait_answer_interruptible(struct fuse_conn *fc,
374 struct fuse_req *req)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +0200375__releases(fc->lock)
376__acquires(fc->lock)
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700377{
378 if (signal_pending(current))
379 return;
380
381 spin_unlock(&fc->lock);
382 wait_event_interruptible(req->waitq, req->state == FUSE_REQ_FINISHED);
383 spin_lock(&fc->lock);
384}
385
386static void queue_interrupt(struct fuse_conn *fc, struct fuse_req *req)
387{
388 list_add_tail(&req->intr_entry, &fc->interrupts);
389 wake_up(&fc->waitq);
390 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
391}
392
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700393static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +0200394__releases(fc->lock)
395__acquires(fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700396{
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700397 if (!fc->no_interrupt) {
398 /* Any signal may interrupt this */
399 wait_answer_interruptible(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700400
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700401 if (req->aborted)
402 goto aborted;
403 if (req->state == FUSE_REQ_FINISHED)
404 return;
405
406 req->interrupted = 1;
407 if (req->state == FUSE_REQ_SENT)
408 queue_interrupt(fc, req);
409 }
410
Miklos Szeredia131de02007-10-16 23:31:04 -0700411 if (!req->force) {
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700412 sigset_t oldset;
413
414 /* Only fatal signals may interrupt this */
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700415 block_sigs(&oldset);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700416 wait_answer_interruptible(fc, req);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700417 restore_sigs(&oldset);
Miklos Szeredia131de02007-10-16 23:31:04 -0700418
419 if (req->aborted)
420 goto aborted;
421 if (req->state == FUSE_REQ_FINISHED)
422 return;
423
424 /* Request is not yet in userspace, bail out */
425 if (req->state == FUSE_REQ_PENDING) {
426 list_del(&req->list);
427 __fuse_put_request(req);
428 req->out.h.error = -EINTR;
429 return;
430 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700431 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700432
Miklos Szeredia131de02007-10-16 23:31:04 -0700433 /*
434 * Either request is already in userspace, or it was forced.
435 * Wait it out.
436 */
437 spin_unlock(&fc->lock);
438 wait_event(req->waitq, req->state == FUSE_REQ_FINISHED);
439 spin_lock(&fc->lock);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700440
Miklos Szeredia131de02007-10-16 23:31:04 -0700441 if (!req->aborted)
442 return;
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700443
444 aborted:
Miklos Szeredia131de02007-10-16 23:31:04 -0700445 BUG_ON(req->state != FUSE_REQ_FINISHED);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700446 if (req->locked) {
447 /* This is uninterruptible sleep, because data is
448 being copied to/from the buffers of req. During
449 locked state, there mustn't be any filesystem
450 operation (e.g. page fault), since that could lead
451 to deadlock */
Miklos Szeredid7133112006-04-10 22:54:55 -0700452 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700453 wait_event(req->waitq, !req->locked);
Miklos Szeredid7133112006-04-10 22:54:55 -0700454 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700455 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700456}
457
Eric Wong6a4e9222013-02-04 13:04:44 +0000458static void __fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700459{
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400460 BUG_ON(req->background);
Miklos Szeredid7133112006-04-10 22:54:55 -0700461 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700462 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700463 req->out.h.error = -ENOTCONN;
464 else if (fc->conn_error)
465 req->out.h.error = -ECONNREFUSED;
466 else {
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200467 req->in.h.unique = fuse_get_unique(fc);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700468 queue_request(fc, req);
469 /* acquire extra reference, since request is still needed
470 after request_end() */
471 __fuse_get_request(req);
472
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700473 request_wait_answer(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700474 }
Miklos Szeredid7133112006-04-10 22:54:55 -0700475 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700476}
Eric Wong6a4e9222013-02-04 13:04:44 +0000477
478void fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
479{
480 req->isreply = 1;
481 __fuse_request_send(fc, req);
482}
Tejun Heo08cbf542009-04-14 10:54:53 +0900483EXPORT_SYMBOL_GPL(fuse_request_send);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700484
Tejun Heob93f8582008-11-26 12:03:55 +0100485static void fuse_request_send_nowait_locked(struct fuse_conn *fc,
486 struct fuse_req *req)
Miklos Szeredid12def12008-02-06 01:38:39 -0800487{
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400488 BUG_ON(!req->background);
Miklos Szeredid12def12008-02-06 01:38:39 -0800489 fc->num_background++;
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700490 if (fc->num_background == fc->max_background)
Miklos Szeredid12def12008-02-06 01:38:39 -0800491 fc->blocked = 1;
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700492 if (fc->num_background == fc->congestion_threshold &&
Tejun Heoa325f9b2009-04-14 10:54:52 +0900493 fc->bdi_initialized) {
Jens Axboe8aa7e842009-07-09 14:52:32 +0200494 set_bdi_congested(&fc->bdi, BLK_RW_SYNC);
495 set_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
Miklos Szeredid12def12008-02-06 01:38:39 -0800496 }
497 list_add_tail(&req->list, &fc->bg_queue);
498 flush_bg_queue(fc);
499}
500
Tejun Heob93f8582008-11-26 12:03:55 +0100501static void fuse_request_send_nowait(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700502{
Miklos Szeredid7133112006-04-10 22:54:55 -0700503 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700504 if (fc->connected) {
Tejun Heob93f8582008-11-26 12:03:55 +0100505 fuse_request_send_nowait_locked(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700506 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700507 } else {
508 req->out.h.error = -ENOTCONN;
509 request_end(fc, req);
510 }
511}
512
Tejun Heob93f8582008-11-26 12:03:55 +0100513void fuse_request_send_background(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700514{
515 req->isreply = 1;
Tejun Heob93f8582008-11-26 12:03:55 +0100516 fuse_request_send_nowait(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700517}
Tejun Heo08cbf542009-04-14 10:54:53 +0900518EXPORT_SYMBOL_GPL(fuse_request_send_background);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700519
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200520static int fuse_request_send_notify_reply(struct fuse_conn *fc,
521 struct fuse_req *req, u64 unique)
522{
523 int err = -ENODEV;
524
525 req->isreply = 0;
526 req->in.h.unique = unique;
527 spin_lock(&fc->lock);
528 if (fc->connected) {
529 queue_request(fc, req);
530 err = 0;
531 }
532 spin_unlock(&fc->lock);
533
534 return err;
535}
536
Miklos Szeredi334f4852005-09-09 13:10:27 -0700537/*
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700538 * Called under fc->lock
539 *
540 * fc->connected must have been checked previously
541 */
Tejun Heob93f8582008-11-26 12:03:55 +0100542void fuse_request_send_background_locked(struct fuse_conn *fc,
543 struct fuse_req *req)
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700544{
545 req->isreply = 1;
Tejun Heob93f8582008-11-26 12:03:55 +0100546 fuse_request_send_nowait_locked(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700547}
548
Anand V. Avati0b05b182012-08-19 08:53:23 -0400549void fuse_force_forget(struct file *file, u64 nodeid)
550{
Al Viro6131ffa2013-02-27 16:59:05 -0500551 struct inode *inode = file_inode(file);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400552 struct fuse_conn *fc = get_fuse_conn(inode);
553 struct fuse_req *req;
554 struct fuse_forget_in inarg;
555
556 memset(&inarg, 0, sizeof(inarg));
557 inarg.nlookup = 1;
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400558 req = fuse_get_req_nofail_nopages(fc, file);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400559 req->in.h.opcode = FUSE_FORGET;
560 req->in.h.nodeid = nodeid;
561 req->in.numargs = 1;
562 req->in.args[0].size = sizeof(inarg);
563 req->in.args[0].value = &inarg;
564 req->isreply = 0;
Eric Wong6a4e9222013-02-04 13:04:44 +0000565 __fuse_request_send(fc, req);
566 /* ignore errors */
567 fuse_put_request(fc, req);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400568}
569
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700570/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700571 * Lock the request. Up to the next unlock_request() there mustn't be
572 * anything that could cause a page-fault. If the request was already
Miklos Szeredif9a28422006-06-25 05:48:53 -0700573 * aborted bail out.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700574 */
Miklos Szeredid7133112006-04-10 22:54:55 -0700575static int lock_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700576{
577 int err = 0;
578 if (req) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700579 spin_lock(&fc->lock);
Miklos Szeredif9a28422006-06-25 05:48:53 -0700580 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700581 err = -ENOENT;
582 else
583 req->locked = 1;
Miklos Szeredid7133112006-04-10 22:54:55 -0700584 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700585 }
586 return err;
587}
588
589/*
Miklos Szeredif9a28422006-06-25 05:48:53 -0700590 * Unlock request. If it was aborted during being locked, the
Miklos Szeredi334f4852005-09-09 13:10:27 -0700591 * requester thread is currently waiting for it to be unlocked, so
592 * wake it up.
593 */
Miklos Szeredid7133112006-04-10 22:54:55 -0700594static void unlock_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700595{
596 if (req) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700597 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700598 req->locked = 0;
Miklos Szeredif9a28422006-06-25 05:48:53 -0700599 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700600 wake_up(&req->waitq);
Miklos Szeredid7133112006-04-10 22:54:55 -0700601 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700602 }
603}
604
605struct fuse_copy_state {
Miklos Szeredid7133112006-04-10 22:54:55 -0700606 struct fuse_conn *fc;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700607 int write;
608 struct fuse_req *req;
609 const struct iovec *iov;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200610 struct pipe_buffer *pipebufs;
611 struct pipe_buffer *currbuf;
612 struct pipe_inode_info *pipe;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700613 unsigned long nr_segs;
614 unsigned long seglen;
615 unsigned long addr;
616 struct page *pg;
617 void *mapaddr;
618 void *buf;
619 unsigned len;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200620 unsigned move_pages:1;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700621};
622
Miklos Szeredid7133112006-04-10 22:54:55 -0700623static void fuse_copy_init(struct fuse_copy_state *cs, struct fuse_conn *fc,
Miklos Szeredic3021622010-05-25 15:06:07 +0200624 int write,
Miklos Szeredid7133112006-04-10 22:54:55 -0700625 const struct iovec *iov, unsigned long nr_segs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700626{
627 memset(cs, 0, sizeof(*cs));
Miklos Szeredid7133112006-04-10 22:54:55 -0700628 cs->fc = fc;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700629 cs->write = write;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700630 cs->iov = iov;
631 cs->nr_segs = nr_segs;
632}
633
634/* Unmap and put previous page of userspace buffer */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800635static void fuse_copy_finish(struct fuse_copy_state *cs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700636{
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200637 if (cs->currbuf) {
638 struct pipe_buffer *buf = cs->currbuf;
639
Miklos Szeredic3021622010-05-25 15:06:07 +0200640 if (!cs->write) {
641 buf->ops->unmap(cs->pipe, buf, cs->mapaddr);
642 } else {
Miklos Szeredi7909b1c2010-07-12 14:41:40 +0200643 kunmap(buf->page);
Miklos Szeredic3021622010-05-25 15:06:07 +0200644 buf->len = PAGE_SIZE - cs->len;
645 }
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200646 cs->currbuf = NULL;
647 cs->mapaddr = NULL;
648 } else if (cs->mapaddr) {
Miklos Szeredi7909b1c2010-07-12 14:41:40 +0200649 kunmap(cs->pg);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700650 if (cs->write) {
651 flush_dcache_page(cs->pg);
652 set_page_dirty_lock(cs->pg);
653 }
654 put_page(cs->pg);
655 cs->mapaddr = NULL;
656 }
657}
658
659/*
660 * Get another pagefull of userspace buffer, and map it to kernel
661 * address space, and lock request
662 */
663static int fuse_copy_fill(struct fuse_copy_state *cs)
664{
665 unsigned long offset;
666 int err;
667
Miklos Szeredid7133112006-04-10 22:54:55 -0700668 unlock_request(cs->fc, cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700669 fuse_copy_finish(cs);
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200670 if (cs->pipebufs) {
671 struct pipe_buffer *buf = cs->pipebufs;
672
Miklos Szeredic3021622010-05-25 15:06:07 +0200673 if (!cs->write) {
674 err = buf->ops->confirm(cs->pipe, buf);
675 if (err)
676 return err;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200677
Miklos Szeredic3021622010-05-25 15:06:07 +0200678 BUG_ON(!cs->nr_segs);
679 cs->currbuf = buf;
Miklos Szeredi7909b1c2010-07-12 14:41:40 +0200680 cs->mapaddr = buf->ops->map(cs->pipe, buf, 0);
Miklos Szeredic3021622010-05-25 15:06:07 +0200681 cs->len = buf->len;
682 cs->buf = cs->mapaddr + buf->offset;
683 cs->pipebufs++;
684 cs->nr_segs--;
685 } else {
686 struct page *page;
687
688 if (cs->nr_segs == cs->pipe->buffers)
689 return -EIO;
690
691 page = alloc_page(GFP_HIGHUSER);
692 if (!page)
693 return -ENOMEM;
694
695 buf->page = page;
696 buf->offset = 0;
697 buf->len = 0;
698
699 cs->currbuf = buf;
Miklos Szeredi7909b1c2010-07-12 14:41:40 +0200700 cs->mapaddr = kmap(page);
Miklos Szeredic3021622010-05-25 15:06:07 +0200701 cs->buf = cs->mapaddr;
702 cs->len = PAGE_SIZE;
703 cs->pipebufs++;
704 cs->nr_segs++;
705 }
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200706 } else {
707 if (!cs->seglen) {
708 BUG_ON(!cs->nr_segs);
709 cs->seglen = cs->iov[0].iov_len;
710 cs->addr = (unsigned long) cs->iov[0].iov_base;
711 cs->iov++;
712 cs->nr_segs--;
713 }
714 err = get_user_pages_fast(cs->addr, 1, cs->write, &cs->pg);
715 if (err < 0)
716 return err;
717 BUG_ON(err != 1);
718 offset = cs->addr % PAGE_SIZE;
Miklos Szeredi7909b1c2010-07-12 14:41:40 +0200719 cs->mapaddr = kmap(cs->pg);
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200720 cs->buf = cs->mapaddr + offset;
721 cs->len = min(PAGE_SIZE - offset, cs->seglen);
722 cs->seglen -= cs->len;
723 cs->addr += cs->len;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700724 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700725
Miklos Szeredid7133112006-04-10 22:54:55 -0700726 return lock_request(cs->fc, cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700727}
728
729/* Do as much copy to/from userspace buffer as we can */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800730static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700731{
732 unsigned ncpy = min(*size, cs->len);
733 if (val) {
734 if (cs->write)
735 memcpy(cs->buf, *val, ncpy);
736 else
737 memcpy(*val, cs->buf, ncpy);
738 *val += ncpy;
739 }
740 *size -= ncpy;
741 cs->len -= ncpy;
742 cs->buf += ncpy;
743 return ncpy;
744}
745
Miklos Szeredice534fb2010-05-25 15:06:07 +0200746static int fuse_check_page(struct page *page)
747{
748 if (page_mapcount(page) ||
749 page->mapping != NULL ||
750 page_count(page) != 1 ||
751 (page->flags & PAGE_FLAGS_CHECK_AT_PREP &
752 ~(1 << PG_locked |
753 1 << PG_referenced |
754 1 << PG_uptodate |
755 1 << PG_lru |
756 1 << PG_active |
757 1 << PG_reclaim))) {
758 printk(KERN_WARNING "fuse: trying to steal weird page\n");
759 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);
760 return 1;
761 }
762 return 0;
763}
764
765static int fuse_try_move_page(struct fuse_copy_state *cs, struct page **pagep)
766{
767 int err;
768 struct page *oldpage = *pagep;
769 struct page *newpage;
770 struct pipe_buffer *buf = cs->pipebufs;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200771
772 unlock_request(cs->fc, cs->req);
773 fuse_copy_finish(cs);
774
775 err = buf->ops->confirm(cs->pipe, buf);
776 if (err)
777 return err;
778
779 BUG_ON(!cs->nr_segs);
780 cs->currbuf = buf;
781 cs->len = buf->len;
782 cs->pipebufs++;
783 cs->nr_segs--;
784
785 if (cs->len != PAGE_SIZE)
786 goto out_fallback;
787
788 if (buf->ops->steal(cs->pipe, buf) != 0)
789 goto out_fallback;
790
791 newpage = buf->page;
792
793 if (WARN_ON(!PageUptodate(newpage)))
794 return -EIO;
795
796 ClearPageMappedToDisk(newpage);
797
798 if (fuse_check_page(newpage) != 0)
799 goto out_fallback_unlock;
800
Miklos Szeredice534fb2010-05-25 15:06:07 +0200801 /*
802 * This is a new and locked page, it shouldn't be mapped or
803 * have any special flags on it
804 */
805 if (WARN_ON(page_mapped(oldpage)))
806 goto out_fallback_unlock;
807 if (WARN_ON(page_has_private(oldpage)))
808 goto out_fallback_unlock;
809 if (WARN_ON(PageDirty(oldpage) || PageWriteback(oldpage)))
810 goto out_fallback_unlock;
811 if (WARN_ON(PageMlocked(oldpage)))
812 goto out_fallback_unlock;
813
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700814 err = replace_page_cache_page(oldpage, newpage, GFP_KERNEL);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200815 if (err) {
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700816 unlock_page(newpage);
817 return err;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200818 }
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700819
Miklos Szeredice534fb2010-05-25 15:06:07 +0200820 page_cache_get(newpage);
821
822 if (!(buf->flags & PIPE_BUF_FLAG_LRU))
823 lru_cache_add_file(newpage);
824
825 err = 0;
826 spin_lock(&cs->fc->lock);
827 if (cs->req->aborted)
828 err = -ENOENT;
829 else
830 *pagep = newpage;
831 spin_unlock(&cs->fc->lock);
832
833 if (err) {
834 unlock_page(newpage);
835 page_cache_release(newpage);
836 return err;
837 }
838
839 unlock_page(oldpage);
840 page_cache_release(oldpage);
841 cs->len = 0;
842
843 return 0;
844
845out_fallback_unlock:
846 unlock_page(newpage);
847out_fallback:
848 cs->mapaddr = buf->ops->map(cs->pipe, buf, 1);
849 cs->buf = cs->mapaddr + buf->offset;
850
851 err = lock_request(cs->fc, cs->req);
852 if (err)
853 return err;
854
855 return 1;
856}
857
Miklos Szeredic3021622010-05-25 15:06:07 +0200858static int fuse_ref_page(struct fuse_copy_state *cs, struct page *page,
859 unsigned offset, unsigned count)
860{
861 struct pipe_buffer *buf;
862
863 if (cs->nr_segs == cs->pipe->buffers)
864 return -EIO;
865
866 unlock_request(cs->fc, cs->req);
867 fuse_copy_finish(cs);
868
869 buf = cs->pipebufs;
870 page_cache_get(page);
871 buf->page = page;
872 buf->offset = offset;
873 buf->len = count;
874
875 cs->pipebufs++;
876 cs->nr_segs++;
877 cs->len = 0;
878
879 return 0;
880}
881
Miklos Szeredi334f4852005-09-09 13:10:27 -0700882/*
883 * Copy a page in the request to/from the userspace buffer. Must be
884 * done atomically
885 */
Miklos Szeredice534fb2010-05-25 15:06:07 +0200886static int fuse_copy_page(struct fuse_copy_state *cs, struct page **pagep,
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800887 unsigned offset, unsigned count, int zeroing)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700888{
Miklos Szeredice534fb2010-05-25 15:06:07 +0200889 int err;
890 struct page *page = *pagep;
891
Miklos Szeredib6777c42010-10-26 14:22:27 -0700892 if (page && zeroing && count < PAGE_SIZE)
893 clear_highpage(page);
894
Miklos Szeredi334f4852005-09-09 13:10:27 -0700895 while (count) {
Miklos Szeredic3021622010-05-25 15:06:07 +0200896 if (cs->write && cs->pipebufs && page) {
897 return fuse_ref_page(cs, page, offset, count);
898 } else if (!cs->len) {
Miklos Szeredice534fb2010-05-25 15:06:07 +0200899 if (cs->move_pages && page &&
900 offset == 0 && count == PAGE_SIZE) {
901 err = fuse_try_move_page(cs, pagep);
902 if (err <= 0)
903 return err;
904 } else {
905 err = fuse_copy_fill(cs);
906 if (err)
907 return err;
908 }
Miklos Szeredi1729a162008-11-26 12:03:54 +0100909 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700910 if (page) {
Cong Wang2408f6e2011-11-25 23:14:30 +0800911 void *mapaddr = kmap_atomic(page);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700912 void *buf = mapaddr + offset;
913 offset += fuse_copy_do(cs, &buf, &count);
Cong Wang2408f6e2011-11-25 23:14:30 +0800914 kunmap_atomic(mapaddr);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700915 } else
916 offset += fuse_copy_do(cs, NULL, &count);
917 }
918 if (page && !cs->write)
919 flush_dcache_page(page);
920 return 0;
921}
922
923/* Copy pages in the request to/from userspace buffer */
924static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
925 int zeroing)
926{
927 unsigned i;
928 struct fuse_req *req = cs->req;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700929
930 for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
Miklos Szeredice534fb2010-05-25 15:06:07 +0200931 int err;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400932 unsigned offset = req->page_descs[i].offset;
933 unsigned count = min(nbytes, req->page_descs[i].length);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200934
935 err = fuse_copy_page(cs, &req->pages[i], offset, count,
936 zeroing);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700937 if (err)
938 return err;
939
940 nbytes -= count;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700941 }
942 return 0;
943}
944
945/* Copy a single argument in the request to/from userspace buffer */
946static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
947{
948 while (size) {
Miklos Szeredi1729a162008-11-26 12:03:54 +0100949 if (!cs->len) {
950 int err = fuse_copy_fill(cs);
951 if (err)
952 return err;
953 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700954 fuse_copy_do(cs, &val, &size);
955 }
956 return 0;
957}
958
959/* Copy request arguments to/from userspace buffer */
960static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
961 unsigned argpages, struct fuse_arg *args,
962 int zeroing)
963{
964 int err = 0;
965 unsigned i;
966
967 for (i = 0; !err && i < numargs; i++) {
968 struct fuse_arg *arg = &args[i];
969 if (i == numargs - 1 && argpages)
970 err = fuse_copy_pages(cs, arg->size, zeroing);
971 else
972 err = fuse_copy_one(cs, arg->value, arg->size);
973 }
974 return err;
975}
976
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100977static int forget_pending(struct fuse_conn *fc)
978{
979 return fc->forget_list_head.next != NULL;
980}
981
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700982static int request_pending(struct fuse_conn *fc)
983{
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100984 return !list_empty(&fc->pending) || !list_empty(&fc->interrupts) ||
985 forget_pending(fc);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700986}
987
Miklos Szeredi334f4852005-09-09 13:10:27 -0700988/* Wait until a request is available on the pending list */
989static void request_wait(struct fuse_conn *fc)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +0200990__releases(fc->lock)
991__acquires(fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700992{
993 DECLARE_WAITQUEUE(wait, current);
994
995 add_wait_queue_exclusive(&fc->waitq, &wait);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700996 while (fc->connected && !request_pending(fc)) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700997 set_current_state(TASK_INTERRUPTIBLE);
998 if (signal_pending(current))
999 break;
1000
Miklos Szeredid7133112006-04-10 22:54:55 -07001001 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001002 schedule();
Miklos Szeredid7133112006-04-10 22:54:55 -07001003 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001004 }
1005 set_current_state(TASK_RUNNING);
1006 remove_wait_queue(&fc->waitq, &wait);
1007}
1008
1009/*
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001010 * Transfer an interrupt request to userspace
1011 *
1012 * Unlike other requests this is assembled on demand, without a need
1013 * to allocate a separate fuse_req structure.
1014 *
1015 * Called with fc->lock held, releases it
1016 */
Miklos Szeredic3021622010-05-25 15:06:07 +02001017static int fuse_read_interrupt(struct fuse_conn *fc, struct fuse_copy_state *cs,
1018 size_t nbytes, struct fuse_req *req)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001019__releases(fc->lock)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001020{
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001021 struct fuse_in_header ih;
1022 struct fuse_interrupt_in arg;
1023 unsigned reqsize = sizeof(ih) + sizeof(arg);
1024 int err;
1025
1026 list_del_init(&req->intr_entry);
1027 req->intr_unique = fuse_get_unique(fc);
1028 memset(&ih, 0, sizeof(ih));
1029 memset(&arg, 0, sizeof(arg));
1030 ih.len = reqsize;
1031 ih.opcode = FUSE_INTERRUPT;
1032 ih.unique = req->intr_unique;
1033 arg.unique = req->in.h.unique;
1034
1035 spin_unlock(&fc->lock);
Miklos Szeredic3021622010-05-25 15:06:07 +02001036 if (nbytes < reqsize)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001037 return -EINVAL;
1038
Miklos Szeredic3021622010-05-25 15:06:07 +02001039 err = fuse_copy_one(cs, &ih, sizeof(ih));
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001040 if (!err)
Miklos Szeredic3021622010-05-25 15:06:07 +02001041 err = fuse_copy_one(cs, &arg, sizeof(arg));
1042 fuse_copy_finish(cs);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001043
1044 return err ? err : reqsize;
1045}
1046
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001047static struct fuse_forget_link *dequeue_forget(struct fuse_conn *fc,
1048 unsigned max,
1049 unsigned *countp)
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001050{
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001051 struct fuse_forget_link *head = fc->forget_list_head.next;
1052 struct fuse_forget_link **newhead = &head;
1053 unsigned count;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001054
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001055 for (count = 0; *newhead != NULL && count < max; count++)
1056 newhead = &(*newhead)->next;
1057
1058 fc->forget_list_head.next = *newhead;
1059 *newhead = NULL;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001060 if (fc->forget_list_head.next == NULL)
1061 fc->forget_list_tail = &fc->forget_list_head;
1062
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001063 if (countp != NULL)
1064 *countp = count;
1065
1066 return head;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001067}
1068
1069static int fuse_read_single_forget(struct fuse_conn *fc,
1070 struct fuse_copy_state *cs,
1071 size_t nbytes)
1072__releases(fc->lock)
1073{
1074 int err;
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001075 struct fuse_forget_link *forget = dequeue_forget(fc, 1, NULL);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001076 struct fuse_forget_in arg = {
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001077 .nlookup = forget->forget_one.nlookup,
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001078 };
1079 struct fuse_in_header ih = {
1080 .opcode = FUSE_FORGET,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001081 .nodeid = forget->forget_one.nodeid,
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001082 .unique = fuse_get_unique(fc),
1083 .len = sizeof(ih) + sizeof(arg),
1084 };
1085
1086 spin_unlock(&fc->lock);
1087 kfree(forget);
1088 if (nbytes < ih.len)
1089 return -EINVAL;
1090
1091 err = fuse_copy_one(cs, &ih, sizeof(ih));
1092 if (!err)
1093 err = fuse_copy_one(cs, &arg, sizeof(arg));
1094 fuse_copy_finish(cs);
1095
1096 if (err)
1097 return err;
1098
1099 return ih.len;
1100}
1101
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001102static int fuse_read_batch_forget(struct fuse_conn *fc,
1103 struct fuse_copy_state *cs, size_t nbytes)
1104__releases(fc->lock)
1105{
1106 int err;
1107 unsigned max_forgets;
1108 unsigned count;
1109 struct fuse_forget_link *head;
1110 struct fuse_batch_forget_in arg = { .count = 0 };
1111 struct fuse_in_header ih = {
1112 .opcode = FUSE_BATCH_FORGET,
1113 .unique = fuse_get_unique(fc),
1114 .len = sizeof(ih) + sizeof(arg),
1115 };
1116
1117 if (nbytes < ih.len) {
1118 spin_unlock(&fc->lock);
1119 return -EINVAL;
1120 }
1121
1122 max_forgets = (nbytes - ih.len) / sizeof(struct fuse_forget_one);
1123 head = dequeue_forget(fc, max_forgets, &count);
1124 spin_unlock(&fc->lock);
1125
1126 arg.count = count;
1127 ih.len += count * sizeof(struct fuse_forget_one);
1128 err = fuse_copy_one(cs, &ih, sizeof(ih));
1129 if (!err)
1130 err = fuse_copy_one(cs, &arg, sizeof(arg));
1131
1132 while (head) {
1133 struct fuse_forget_link *forget = head;
1134
1135 if (!err) {
1136 err = fuse_copy_one(cs, &forget->forget_one,
1137 sizeof(forget->forget_one));
1138 }
1139 head = forget->next;
1140 kfree(forget);
1141 }
1142
1143 fuse_copy_finish(cs);
1144
1145 if (err)
1146 return err;
1147
1148 return ih.len;
1149}
1150
1151static int fuse_read_forget(struct fuse_conn *fc, struct fuse_copy_state *cs,
1152 size_t nbytes)
1153__releases(fc->lock)
1154{
1155 if (fc->minor < 16 || fc->forget_list_head.next->next == NULL)
1156 return fuse_read_single_forget(fc, cs, nbytes);
1157 else
1158 return fuse_read_batch_forget(fc, cs, nbytes);
1159}
1160
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001161/*
Miklos Szeredi334f4852005-09-09 13:10:27 -07001162 * Read a single request into the userspace filesystem's buffer. This
1163 * function waits until a request is available, then removes it from
1164 * the pending list and copies request data to userspace buffer. If
Miklos Szeredif9a28422006-06-25 05:48:53 -07001165 * no reply is needed (FORGET) or request has been aborted or there
1166 * was an error during the copying then it's finished by calling
Miklos Szeredi334f4852005-09-09 13:10:27 -07001167 * request_end(). Otherwise add it to the processing list, and set
1168 * the 'sent' flag.
1169 */
Miklos Szeredic3021622010-05-25 15:06:07 +02001170static ssize_t fuse_dev_do_read(struct fuse_conn *fc, struct file *file,
1171 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001172{
1173 int err;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001174 struct fuse_req *req;
1175 struct fuse_in *in;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001176 unsigned reqsize;
1177
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001178 restart:
Miklos Szeredid7133112006-04-10 22:54:55 -07001179 spin_lock(&fc->lock);
Jeff Dikee5ac1d12006-04-10 22:54:53 -07001180 err = -EAGAIN;
1181 if ((file->f_flags & O_NONBLOCK) && fc->connected &&
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001182 !request_pending(fc))
Jeff Dikee5ac1d12006-04-10 22:54:53 -07001183 goto err_unlock;
1184
Miklos Szeredi334f4852005-09-09 13:10:27 -07001185 request_wait(fc);
1186 err = -ENODEV;
Miklos Szeredi9ba7cbb2006-01-16 22:14:34 -08001187 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001188 goto err_unlock;
1189 err = -ERESTARTSYS;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001190 if (!request_pending(fc))
Miklos Szeredi334f4852005-09-09 13:10:27 -07001191 goto err_unlock;
1192
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001193 if (!list_empty(&fc->interrupts)) {
1194 req = list_entry(fc->interrupts.next, struct fuse_req,
1195 intr_entry);
Miklos Szeredic3021622010-05-25 15:06:07 +02001196 return fuse_read_interrupt(fc, cs, nbytes, req);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001197 }
1198
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001199 if (forget_pending(fc)) {
1200 if (list_empty(&fc->pending) || fc->forget_batch-- > 0)
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001201 return fuse_read_forget(fc, cs, nbytes);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001202
1203 if (fc->forget_batch <= -8)
1204 fc->forget_batch = 16;
1205 }
1206
Miklos Szeredi334f4852005-09-09 13:10:27 -07001207 req = list_entry(fc->pending.next, struct fuse_req, list);
Miklos Szeredi83cfd492006-01-16 22:14:31 -08001208 req->state = FUSE_REQ_READING;
Miklos Szeredid77a1d52006-01-16 22:14:31 -08001209 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001210
1211 in = &req->in;
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001212 reqsize = in->h.len;
1213 /* If request is too large, reply with an error and restart the read */
Miklos Szeredic3021622010-05-25 15:06:07 +02001214 if (nbytes < reqsize) {
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001215 req->out.h.error = -EIO;
1216 /* SETXATTR is special, since it may contain too large data */
1217 if (in->h.opcode == FUSE_SETXATTR)
1218 req->out.h.error = -E2BIG;
1219 request_end(fc, req);
1220 goto restart;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001221 }
Miklos Szeredid7133112006-04-10 22:54:55 -07001222 spin_unlock(&fc->lock);
Miklos Szeredic3021622010-05-25 15:06:07 +02001223 cs->req = req;
1224 err = fuse_copy_one(cs, &in->h, sizeof(in->h));
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001225 if (!err)
Miklos Szeredic3021622010-05-25 15:06:07 +02001226 err = fuse_copy_args(cs, in->numargs, in->argpages,
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001227 (struct fuse_arg *) in->args, 0);
Miklos Szeredic3021622010-05-25 15:06:07 +02001228 fuse_copy_finish(cs);
Miklos Szeredid7133112006-04-10 22:54:55 -07001229 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001230 req->locked = 0;
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -07001231 if (req->aborted) {
1232 request_end(fc, req);
1233 return -ENODEV;
1234 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001235 if (err) {
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -07001236 req->out.h.error = -EIO;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001237 request_end(fc, req);
1238 return err;
1239 }
1240 if (!req->isreply)
1241 request_end(fc, req);
1242 else {
Miklos Szeredi83cfd492006-01-16 22:14:31 -08001243 req->state = FUSE_REQ_SENT;
Miklos Szeredid77a1d52006-01-16 22:14:31 -08001244 list_move_tail(&req->list, &fc->processing);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001245 if (req->interrupted)
1246 queue_interrupt(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -07001247 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001248 }
1249 return reqsize;
1250
1251 err_unlock:
Miklos Szeredid7133112006-04-10 22:54:55 -07001252 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001253 return err;
1254}
1255
Miklos Szeredic3021622010-05-25 15:06:07 +02001256static ssize_t fuse_dev_read(struct kiocb *iocb, const struct iovec *iov,
1257 unsigned long nr_segs, loff_t pos)
1258{
1259 struct fuse_copy_state cs;
1260 struct file *file = iocb->ki_filp;
1261 struct fuse_conn *fc = fuse_get_conn(file);
1262 if (!fc)
1263 return -EPERM;
1264
1265 fuse_copy_init(&cs, fc, 1, iov, nr_segs);
1266
1267 return fuse_dev_do_read(fc, file, &cs, iov_length(iov, nr_segs));
1268}
1269
1270static int fuse_dev_pipe_buf_steal(struct pipe_inode_info *pipe,
1271 struct pipe_buffer *buf)
1272{
1273 return 1;
1274}
1275
1276static const struct pipe_buf_operations fuse_dev_pipe_buf_ops = {
1277 .can_merge = 0,
1278 .map = generic_pipe_buf_map,
1279 .unmap = generic_pipe_buf_unmap,
1280 .confirm = generic_pipe_buf_confirm,
1281 .release = generic_pipe_buf_release,
1282 .steal = fuse_dev_pipe_buf_steal,
1283 .get = generic_pipe_buf_get,
1284};
1285
1286static ssize_t fuse_dev_splice_read(struct file *in, loff_t *ppos,
1287 struct pipe_inode_info *pipe,
1288 size_t len, unsigned int flags)
1289{
1290 int ret;
1291 int page_nr = 0;
1292 int do_wakeup = 0;
1293 struct pipe_buffer *bufs;
1294 struct fuse_copy_state cs;
1295 struct fuse_conn *fc = fuse_get_conn(in);
1296 if (!fc)
1297 return -EPERM;
1298
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001299 bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), GFP_KERNEL);
Miklos Szeredic3021622010-05-25 15:06:07 +02001300 if (!bufs)
1301 return -ENOMEM;
1302
1303 fuse_copy_init(&cs, fc, 1, NULL, 0);
1304 cs.pipebufs = bufs;
1305 cs.pipe = pipe;
1306 ret = fuse_dev_do_read(fc, in, &cs, len);
1307 if (ret < 0)
1308 goto out;
1309
1310 ret = 0;
1311 pipe_lock(pipe);
1312
1313 if (!pipe->readers) {
1314 send_sig(SIGPIPE, current, 0);
1315 if (!ret)
1316 ret = -EPIPE;
1317 goto out_unlock;
1318 }
1319
1320 if (pipe->nrbufs + cs.nr_segs > pipe->buffers) {
1321 ret = -EIO;
1322 goto out_unlock;
1323 }
1324
1325 while (page_nr < cs.nr_segs) {
1326 int newbuf = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
1327 struct pipe_buffer *buf = pipe->bufs + newbuf;
1328
1329 buf->page = bufs[page_nr].page;
1330 buf->offset = bufs[page_nr].offset;
1331 buf->len = bufs[page_nr].len;
1332 buf->ops = &fuse_dev_pipe_buf_ops;
1333
1334 pipe->nrbufs++;
1335 page_nr++;
1336 ret += buf->len;
1337
1338 if (pipe->inode)
1339 do_wakeup = 1;
1340 }
1341
1342out_unlock:
1343 pipe_unlock(pipe);
1344
1345 if (do_wakeup) {
1346 smp_mb();
1347 if (waitqueue_active(&pipe->wait))
1348 wake_up_interruptible(&pipe->wait);
1349 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
1350 }
1351
1352out:
1353 for (; page_nr < cs.nr_segs; page_nr++)
1354 page_cache_release(bufs[page_nr].page);
1355
1356 kfree(bufs);
1357 return ret;
1358}
1359
Tejun Heo95668a62008-11-26 12:03:55 +01001360static int fuse_notify_poll(struct fuse_conn *fc, unsigned int size,
1361 struct fuse_copy_state *cs)
1362{
1363 struct fuse_notify_poll_wakeup_out outarg;
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001364 int err = -EINVAL;
Tejun Heo95668a62008-11-26 12:03:55 +01001365
1366 if (size != sizeof(outarg))
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001367 goto err;
Tejun Heo95668a62008-11-26 12:03:55 +01001368
1369 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1370 if (err)
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001371 goto err;
Tejun Heo95668a62008-11-26 12:03:55 +01001372
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001373 fuse_copy_finish(cs);
Tejun Heo95668a62008-11-26 12:03:55 +01001374 return fuse_notify_poll_wakeup(fc, &outarg);
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001375
1376err:
1377 fuse_copy_finish(cs);
1378 return err;
Tejun Heo95668a62008-11-26 12:03:55 +01001379}
1380
John Muir3b463ae2009-05-31 11:13:57 -04001381static int fuse_notify_inval_inode(struct fuse_conn *fc, unsigned int size,
1382 struct fuse_copy_state *cs)
1383{
1384 struct fuse_notify_inval_inode_out outarg;
1385 int err = -EINVAL;
1386
1387 if (size != sizeof(outarg))
1388 goto err;
1389
1390 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1391 if (err)
1392 goto err;
1393 fuse_copy_finish(cs);
1394
1395 down_read(&fc->killsb);
1396 err = -ENOENT;
Miklos Szeredib21dda42010-02-05 12:08:31 +01001397 if (fc->sb) {
1398 err = fuse_reverse_inval_inode(fc->sb, outarg.ino,
1399 outarg.off, outarg.len);
1400 }
John Muir3b463ae2009-05-31 11:13:57 -04001401 up_read(&fc->killsb);
1402 return err;
1403
1404err:
1405 fuse_copy_finish(cs);
1406 return err;
1407}
1408
1409static int fuse_notify_inval_entry(struct fuse_conn *fc, unsigned int size,
1410 struct fuse_copy_state *cs)
1411{
1412 struct fuse_notify_inval_entry_out outarg;
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001413 int err = -ENOMEM;
1414 char *buf;
John Muir3b463ae2009-05-31 11:13:57 -04001415 struct qstr name;
1416
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001417 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1418 if (!buf)
1419 goto err;
1420
1421 err = -EINVAL;
John Muir3b463ae2009-05-31 11:13:57 -04001422 if (size < sizeof(outarg))
1423 goto err;
1424
1425 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1426 if (err)
1427 goto err;
1428
1429 err = -ENAMETOOLONG;
1430 if (outarg.namelen > FUSE_NAME_MAX)
1431 goto err;
1432
Miklos Szeredic2183d12011-08-24 10:20:17 +02001433 err = -EINVAL;
1434 if (size != sizeof(outarg) + outarg.namelen + 1)
1435 goto err;
1436
John Muir3b463ae2009-05-31 11:13:57 -04001437 name.name = buf;
1438 name.len = outarg.namelen;
1439 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1440 if (err)
1441 goto err;
1442 fuse_copy_finish(cs);
1443 buf[outarg.namelen] = 0;
1444 name.hash = full_name_hash(name.name, name.len);
1445
1446 down_read(&fc->killsb);
1447 err = -ENOENT;
Miklos Szeredib21dda42010-02-05 12:08:31 +01001448 if (fc->sb)
John Muir451d0f52011-12-06 21:50:06 +01001449 err = fuse_reverse_inval_entry(fc->sb, outarg.parent, 0, &name);
1450 up_read(&fc->killsb);
1451 kfree(buf);
1452 return err;
1453
1454err:
1455 kfree(buf);
1456 fuse_copy_finish(cs);
1457 return err;
1458}
1459
1460static int fuse_notify_delete(struct fuse_conn *fc, unsigned int size,
1461 struct fuse_copy_state *cs)
1462{
1463 struct fuse_notify_delete_out outarg;
1464 int err = -ENOMEM;
1465 char *buf;
1466 struct qstr name;
1467
1468 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1469 if (!buf)
1470 goto err;
1471
1472 err = -EINVAL;
1473 if (size < sizeof(outarg))
1474 goto err;
1475
1476 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1477 if (err)
1478 goto err;
1479
1480 err = -ENAMETOOLONG;
1481 if (outarg.namelen > FUSE_NAME_MAX)
1482 goto err;
1483
1484 err = -EINVAL;
1485 if (size != sizeof(outarg) + outarg.namelen + 1)
1486 goto err;
1487
1488 name.name = buf;
1489 name.len = outarg.namelen;
1490 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1491 if (err)
1492 goto err;
1493 fuse_copy_finish(cs);
1494 buf[outarg.namelen] = 0;
1495 name.hash = full_name_hash(name.name, name.len);
1496
1497 down_read(&fc->killsb);
1498 err = -ENOENT;
1499 if (fc->sb)
1500 err = fuse_reverse_inval_entry(fc->sb, outarg.parent,
1501 outarg.child, &name);
John Muir3b463ae2009-05-31 11:13:57 -04001502 up_read(&fc->killsb);
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001503 kfree(buf);
John Muir3b463ae2009-05-31 11:13:57 -04001504 return err;
1505
1506err:
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001507 kfree(buf);
John Muir3b463ae2009-05-31 11:13:57 -04001508 fuse_copy_finish(cs);
1509 return err;
1510}
1511
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001512static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
1513 struct fuse_copy_state *cs)
1514{
1515 struct fuse_notify_store_out outarg;
1516 struct inode *inode;
1517 struct address_space *mapping;
1518 u64 nodeid;
1519 int err;
1520 pgoff_t index;
1521 unsigned int offset;
1522 unsigned int num;
1523 loff_t file_size;
1524 loff_t end;
1525
1526 err = -EINVAL;
1527 if (size < sizeof(outarg))
1528 goto out_finish;
1529
1530 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1531 if (err)
1532 goto out_finish;
1533
1534 err = -EINVAL;
1535 if (size - sizeof(outarg) != outarg.size)
1536 goto out_finish;
1537
1538 nodeid = outarg.nodeid;
1539
1540 down_read(&fc->killsb);
1541
1542 err = -ENOENT;
1543 if (!fc->sb)
1544 goto out_up_killsb;
1545
1546 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1547 if (!inode)
1548 goto out_up_killsb;
1549
1550 mapping = inode->i_mapping;
1551 index = outarg.offset >> PAGE_CACHE_SHIFT;
1552 offset = outarg.offset & ~PAGE_CACHE_MASK;
1553 file_size = i_size_read(inode);
1554 end = outarg.offset + outarg.size;
1555 if (end > file_size) {
1556 file_size = end;
1557 fuse_write_update_size(inode, file_size);
1558 }
1559
1560 num = outarg.size;
1561 while (num) {
1562 struct page *page;
1563 unsigned int this_num;
1564
1565 err = -ENOMEM;
1566 page = find_or_create_page(mapping, index,
1567 mapping_gfp_mask(mapping));
1568 if (!page)
1569 goto out_iput;
1570
1571 this_num = min_t(unsigned, num, PAGE_CACHE_SIZE - offset);
1572 err = fuse_copy_page(cs, &page, offset, this_num, 0);
1573 if (!err && offset == 0 && (num != 0 || file_size == end))
1574 SetPageUptodate(page);
1575 unlock_page(page);
1576 page_cache_release(page);
1577
1578 if (err)
1579 goto out_iput;
1580
1581 num -= this_num;
1582 offset = 0;
1583 index++;
1584 }
1585
1586 err = 0;
1587
1588out_iput:
1589 iput(inode);
1590out_up_killsb:
1591 up_read(&fc->killsb);
1592out_finish:
1593 fuse_copy_finish(cs);
1594 return err;
1595}
1596
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001597static void fuse_retrieve_end(struct fuse_conn *fc, struct fuse_req *req)
1598{
Miklos Szeredi0be85572010-10-27 15:34:46 -07001599 release_pages(req->pages, req->num_pages, 0);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001600}
1601
1602static int fuse_retrieve(struct fuse_conn *fc, struct inode *inode,
1603 struct fuse_notify_retrieve_out *outarg)
1604{
1605 int err;
1606 struct address_space *mapping = inode->i_mapping;
1607 struct fuse_req *req;
1608 pgoff_t index;
1609 loff_t file_size;
1610 unsigned int num;
1611 unsigned int offset;
Geert Uytterhoeven01574432010-09-30 22:06:21 +02001612 size_t total_len = 0;
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001613 int num_pages;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001614
1615 offset = outarg->offset & ~PAGE_CACHE_MASK;
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001616 file_size = i_size_read(inode);
1617
1618 num = outarg->size;
1619 if (outarg->offset > file_size)
1620 num = 0;
1621 else if (outarg->offset + num > file_size)
1622 num = file_size - outarg->offset;
1623
1624 num_pages = (num + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1625 num_pages = min(num_pages, FUSE_MAX_PAGES_PER_REQ);
1626
1627 req = fuse_get_req(fc, num_pages);
1628 if (IS_ERR(req))
1629 return PTR_ERR(req);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001630
1631 req->in.h.opcode = FUSE_NOTIFY_REPLY;
1632 req->in.h.nodeid = outarg->nodeid;
1633 req->in.numargs = 2;
1634 req->in.argpages = 1;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001635 req->page_descs[0].offset = offset;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001636 req->end = fuse_retrieve_end;
1637
1638 index = outarg->offset >> PAGE_CACHE_SHIFT;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001639
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001640 while (num && req->num_pages < num_pages) {
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001641 struct page *page;
1642 unsigned int this_num;
1643
1644 page = find_get_page(mapping, index);
1645 if (!page)
1646 break;
1647
1648 this_num = min_t(unsigned, num, PAGE_CACHE_SIZE - offset);
1649 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001650 req->page_descs[req->num_pages].length = this_num;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001651 req->num_pages++;
1652
Miklos Szeredic9e67d42012-09-04 18:45:54 +02001653 offset = 0;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001654 num -= this_num;
1655 total_len += this_num;
Miklos Szeredi48706d02011-12-13 10:36:59 +01001656 index++;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001657 }
1658 req->misc.retrieve_in.offset = outarg->offset;
1659 req->misc.retrieve_in.size = total_len;
1660 req->in.args[0].size = sizeof(req->misc.retrieve_in);
1661 req->in.args[0].value = &req->misc.retrieve_in;
1662 req->in.args[1].size = total_len;
1663
1664 err = fuse_request_send_notify_reply(fc, req, outarg->notify_unique);
1665 if (err)
1666 fuse_retrieve_end(fc, req);
1667
1668 return err;
1669}
1670
1671static int fuse_notify_retrieve(struct fuse_conn *fc, unsigned int size,
1672 struct fuse_copy_state *cs)
1673{
1674 struct fuse_notify_retrieve_out outarg;
1675 struct inode *inode;
1676 int err;
1677
1678 err = -EINVAL;
1679 if (size != sizeof(outarg))
1680 goto copy_finish;
1681
1682 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1683 if (err)
1684 goto copy_finish;
1685
1686 fuse_copy_finish(cs);
1687
1688 down_read(&fc->killsb);
1689 err = -ENOENT;
1690 if (fc->sb) {
1691 u64 nodeid = outarg.nodeid;
1692
1693 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1694 if (inode) {
1695 err = fuse_retrieve(fc, inode, &outarg);
1696 iput(inode);
1697 }
1698 }
1699 up_read(&fc->killsb);
1700
1701 return err;
1702
1703copy_finish:
1704 fuse_copy_finish(cs);
1705 return err;
1706}
1707
Tejun Heo85993962008-11-26 12:03:55 +01001708static int fuse_notify(struct fuse_conn *fc, enum fuse_notify_code code,
1709 unsigned int size, struct fuse_copy_state *cs)
1710{
1711 switch (code) {
Tejun Heo95668a62008-11-26 12:03:55 +01001712 case FUSE_NOTIFY_POLL:
1713 return fuse_notify_poll(fc, size, cs);
1714
John Muir3b463ae2009-05-31 11:13:57 -04001715 case FUSE_NOTIFY_INVAL_INODE:
1716 return fuse_notify_inval_inode(fc, size, cs);
1717
1718 case FUSE_NOTIFY_INVAL_ENTRY:
1719 return fuse_notify_inval_entry(fc, size, cs);
1720
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001721 case FUSE_NOTIFY_STORE:
1722 return fuse_notify_store(fc, size, cs);
1723
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001724 case FUSE_NOTIFY_RETRIEVE:
1725 return fuse_notify_retrieve(fc, size, cs);
1726
John Muir451d0f52011-12-06 21:50:06 +01001727 case FUSE_NOTIFY_DELETE:
1728 return fuse_notify_delete(fc, size, cs);
1729
Tejun Heo85993962008-11-26 12:03:55 +01001730 default:
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001731 fuse_copy_finish(cs);
Tejun Heo85993962008-11-26 12:03:55 +01001732 return -EINVAL;
1733 }
1734}
1735
Miklos Szeredi334f4852005-09-09 13:10:27 -07001736/* Look up request on processing list by unique ID */
1737static struct fuse_req *request_find(struct fuse_conn *fc, u64 unique)
1738{
1739 struct list_head *entry;
1740
1741 list_for_each(entry, &fc->processing) {
1742 struct fuse_req *req;
1743 req = list_entry(entry, struct fuse_req, list);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001744 if (req->in.h.unique == unique || req->intr_unique == unique)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001745 return req;
1746 }
1747 return NULL;
1748}
1749
1750static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
1751 unsigned nbytes)
1752{
1753 unsigned reqsize = sizeof(struct fuse_out_header);
1754
1755 if (out->h.error)
1756 return nbytes != reqsize ? -EINVAL : 0;
1757
1758 reqsize += len_args(out->numargs, out->args);
1759
1760 if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
1761 return -EINVAL;
1762 else if (reqsize > nbytes) {
1763 struct fuse_arg *lastarg = &out->args[out->numargs-1];
1764 unsigned diffsize = reqsize - nbytes;
1765 if (diffsize > lastarg->size)
1766 return -EINVAL;
1767 lastarg->size -= diffsize;
1768 }
1769 return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
1770 out->page_zeroing);
1771}
1772
1773/*
1774 * Write a single reply to a request. First the header is copied from
1775 * the write buffer. The request is then searched on the processing
1776 * list by the unique ID found in the header. If found, then remove
1777 * it from the list and copy the rest of the buffer to the request.
1778 * The request is finished by calling request_end()
1779 */
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001780static ssize_t fuse_dev_do_write(struct fuse_conn *fc,
1781 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001782{
1783 int err;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001784 struct fuse_req *req;
1785 struct fuse_out_header oh;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001786
Miklos Szeredi334f4852005-09-09 13:10:27 -07001787 if (nbytes < sizeof(struct fuse_out_header))
1788 return -EINVAL;
1789
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001790 err = fuse_copy_one(cs, &oh, sizeof(oh));
Miklos Szeredi334f4852005-09-09 13:10:27 -07001791 if (err)
1792 goto err_finish;
Tejun Heo85993962008-11-26 12:03:55 +01001793
Miklos Szeredi334f4852005-09-09 13:10:27 -07001794 err = -EINVAL;
Tejun Heo85993962008-11-26 12:03:55 +01001795 if (oh.len != nbytes)
1796 goto err_finish;
1797
1798 /*
1799 * Zero oh.unique indicates unsolicited notification message
1800 * and error contains notification code.
1801 */
1802 if (!oh.unique) {
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001803 err = fuse_notify(fc, oh.error, nbytes - sizeof(oh), cs);
Tejun Heo85993962008-11-26 12:03:55 +01001804 return err ? err : nbytes;
1805 }
1806
1807 err = -EINVAL;
1808 if (oh.error <= -1000 || oh.error > 0)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001809 goto err_finish;
1810
Miklos Szeredid7133112006-04-10 22:54:55 -07001811 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001812 err = -ENOENT;
1813 if (!fc->connected)
1814 goto err_unlock;
1815
Miklos Szeredi334f4852005-09-09 13:10:27 -07001816 req = request_find(fc, oh.unique);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001817 if (!req)
1818 goto err_unlock;
1819
Miklos Szeredif9a28422006-06-25 05:48:53 -07001820 if (req->aborted) {
Miklos Szeredid7133112006-04-10 22:54:55 -07001821 spin_unlock(&fc->lock);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001822 fuse_copy_finish(cs);
Miklos Szeredid7133112006-04-10 22:54:55 -07001823 spin_lock(&fc->lock);
Miklos Szeredi222f1d692006-01-16 22:14:25 -08001824 request_end(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001825 return -ENOENT;
1826 }
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001827 /* Is it an interrupt reply? */
1828 if (req->intr_unique == oh.unique) {
1829 err = -EINVAL;
1830 if (nbytes != sizeof(struct fuse_out_header))
1831 goto err_unlock;
1832
1833 if (oh.error == -ENOSYS)
1834 fc->no_interrupt = 1;
1835 else if (oh.error == -EAGAIN)
1836 queue_interrupt(fc, req);
1837
1838 spin_unlock(&fc->lock);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001839 fuse_copy_finish(cs);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001840 return nbytes;
1841 }
1842
1843 req->state = FUSE_REQ_WRITING;
Miklos Szeredid77a1d52006-01-16 22:14:31 -08001844 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001845 req->out.h = oh;
1846 req->locked = 1;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001847 cs->req = req;
Miklos Szeredice534fb2010-05-25 15:06:07 +02001848 if (!req->out.page_replace)
1849 cs->move_pages = 0;
Miklos Szeredid7133112006-04-10 22:54:55 -07001850 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001851
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001852 err = copy_out_args(cs, &req->out, nbytes);
1853 fuse_copy_finish(cs);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001854
Miklos Szeredid7133112006-04-10 22:54:55 -07001855 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001856 req->locked = 0;
1857 if (!err) {
Miklos Szeredif9a28422006-06-25 05:48:53 -07001858 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001859 err = -ENOENT;
Miklos Szeredif9a28422006-06-25 05:48:53 -07001860 } else if (!req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001861 req->out.h.error = -EIO;
1862 request_end(fc, req);
1863
1864 return err ? err : nbytes;
1865
1866 err_unlock:
Miklos Szeredid7133112006-04-10 22:54:55 -07001867 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001868 err_finish:
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001869 fuse_copy_finish(cs);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001870 return err;
1871}
1872
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001873static ssize_t fuse_dev_write(struct kiocb *iocb, const struct iovec *iov,
1874 unsigned long nr_segs, loff_t pos)
1875{
1876 struct fuse_copy_state cs;
1877 struct fuse_conn *fc = fuse_get_conn(iocb->ki_filp);
1878 if (!fc)
1879 return -EPERM;
1880
Miklos Szeredic3021622010-05-25 15:06:07 +02001881 fuse_copy_init(&cs, fc, 0, iov, nr_segs);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001882
1883 return fuse_dev_do_write(fc, &cs, iov_length(iov, nr_segs));
1884}
1885
1886static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe,
1887 struct file *out, loff_t *ppos,
1888 size_t len, unsigned int flags)
1889{
1890 unsigned nbuf;
1891 unsigned idx;
1892 struct pipe_buffer *bufs;
1893 struct fuse_copy_state cs;
1894 struct fuse_conn *fc;
1895 size_t rem;
1896 ssize_t ret;
1897
1898 fc = fuse_get_conn(out);
1899 if (!fc)
1900 return -EPERM;
1901
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001902 bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), GFP_KERNEL);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001903 if (!bufs)
1904 return -ENOMEM;
1905
1906 pipe_lock(pipe);
1907 nbuf = 0;
1908 rem = 0;
1909 for (idx = 0; idx < pipe->nrbufs && rem < len; idx++)
1910 rem += pipe->bufs[(pipe->curbuf + idx) & (pipe->buffers - 1)].len;
1911
1912 ret = -EINVAL;
1913 if (rem < len) {
1914 pipe_unlock(pipe);
1915 goto out;
1916 }
1917
1918 rem = len;
1919 while (rem) {
1920 struct pipe_buffer *ibuf;
1921 struct pipe_buffer *obuf;
1922
1923 BUG_ON(nbuf >= pipe->buffers);
1924 BUG_ON(!pipe->nrbufs);
1925 ibuf = &pipe->bufs[pipe->curbuf];
1926 obuf = &bufs[nbuf];
1927
1928 if (rem >= ibuf->len) {
1929 *obuf = *ibuf;
1930 ibuf->ops = NULL;
1931 pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
1932 pipe->nrbufs--;
1933 } else {
1934 ibuf->ops->get(pipe, ibuf);
1935 *obuf = *ibuf;
1936 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1937 obuf->len = rem;
1938 ibuf->offset += obuf->len;
1939 ibuf->len -= obuf->len;
1940 }
1941 nbuf++;
1942 rem -= obuf->len;
1943 }
1944 pipe_unlock(pipe);
1945
Miklos Szeredic3021622010-05-25 15:06:07 +02001946 fuse_copy_init(&cs, fc, 0, NULL, nbuf);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001947 cs.pipebufs = bufs;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001948 cs.pipe = pipe;
1949
Miklos Szeredice534fb2010-05-25 15:06:07 +02001950 if (flags & SPLICE_F_MOVE)
1951 cs.move_pages = 1;
1952
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001953 ret = fuse_dev_do_write(fc, &cs, len);
1954
1955 for (idx = 0; idx < nbuf; idx++) {
1956 struct pipe_buffer *buf = &bufs[idx];
1957 buf->ops->release(pipe, buf);
1958 }
1959out:
1960 kfree(bufs);
1961 return ret;
1962}
1963
Miklos Szeredi334f4852005-09-09 13:10:27 -07001964static unsigned fuse_dev_poll(struct file *file, poll_table *wait)
1965{
Miklos Szeredi334f4852005-09-09 13:10:27 -07001966 unsigned mask = POLLOUT | POLLWRNORM;
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07001967 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001968 if (!fc)
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07001969 return POLLERR;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001970
1971 poll_wait(file, &fc->waitq, wait);
1972
Miklos Szeredid7133112006-04-10 22:54:55 -07001973 spin_lock(&fc->lock);
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07001974 if (!fc->connected)
1975 mask = POLLERR;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001976 else if (request_pending(fc))
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07001977 mask |= POLLIN | POLLRDNORM;
Miklos Szeredid7133112006-04-10 22:54:55 -07001978 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001979
1980 return mask;
1981}
1982
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001983/*
1984 * Abort all requests on the given list (pending or processing)
1985 *
Miklos Szeredid7133112006-04-10 22:54:55 -07001986 * This function releases and reacquires fc->lock
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001987 */
Miklos Szeredi334f4852005-09-09 13:10:27 -07001988static void end_requests(struct fuse_conn *fc, struct list_head *head)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001989__releases(fc->lock)
1990__acquires(fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001991{
1992 while (!list_empty(head)) {
1993 struct fuse_req *req;
1994 req = list_entry(head->next, struct fuse_req, list);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001995 req->out.h.error = -ECONNABORTED;
1996 request_end(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -07001997 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001998 }
1999}
2000
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002001/*
2002 * Abort requests under I/O
2003 *
Miklos Szeredif9a28422006-06-25 05:48:53 -07002004 * The requests are set to aborted and finished, and the request
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002005 * waiter is woken up. This will make request_wait_answer() wait
2006 * until the request is unlocked and then return.
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08002007 *
2008 * If the request is asynchronous, then the end function needs to be
2009 * called after waiting for the request to be unlocked (if it was
2010 * locked).
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002011 */
2012static void end_io_requests(struct fuse_conn *fc)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02002013__releases(fc->lock)
2014__acquires(fc->lock)
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002015{
2016 while (!list_empty(&fc->io)) {
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08002017 struct fuse_req *req =
2018 list_entry(fc->io.next, struct fuse_req, list);
2019 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
2020
Miklos Szeredif9a28422006-06-25 05:48:53 -07002021 req->aborted = 1;
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002022 req->out.h.error = -ECONNABORTED;
2023 req->state = FUSE_REQ_FINISHED;
2024 list_del_init(&req->list);
2025 wake_up(&req->waitq);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08002026 if (end) {
2027 req->end = NULL;
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08002028 __fuse_get_request(req);
Miklos Szeredid7133112006-04-10 22:54:55 -07002029 spin_unlock(&fc->lock);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08002030 wait_event(req->waitq, !req->locked);
2031 end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +01002032 fuse_put_request(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -07002033 spin_lock(&fc->lock);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08002034 }
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002035 }
2036}
2037
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002038static void end_queued_requests(struct fuse_conn *fc)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02002039__releases(fc->lock)
2040__acquires(fc->lock)
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002041{
2042 fc->max_background = UINT_MAX;
2043 flush_bg_queue(fc);
2044 end_requests(fc, &fc->pending);
2045 end_requests(fc, &fc->processing);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01002046 while (forget_pending(fc))
Miklos Szeredi02c048b2010-12-07 20:16:56 +01002047 kfree(dequeue_forget(fc, 1, NULL));
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002048}
2049
Bryan Green357ccf22011-03-01 16:43:52 -08002050static void end_polls(struct fuse_conn *fc)
2051{
2052 struct rb_node *p;
2053
2054 p = rb_first(&fc->polled_files);
2055
2056 while (p) {
2057 struct fuse_file *ff;
2058 ff = rb_entry(p, struct fuse_file, polled_node);
2059 wake_up_interruptible_all(&ff->poll_wait);
2060
2061 p = rb_next(p);
2062 }
2063}
2064
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002065/*
2066 * Abort all requests.
2067 *
2068 * Emergency exit in case of a malicious or accidental deadlock, or
2069 * just a hung filesystem.
2070 *
2071 * The same effect is usually achievable through killing the
2072 * filesystem daemon and all users of the filesystem. The exception
2073 * is the combination of an asynchronous request and the tricky
2074 * deadlock (see Documentation/filesystems/fuse.txt).
2075 *
2076 * During the aborting, progression of requests from the pending and
2077 * processing lists onto the io list, and progression of new requests
2078 * onto the pending list is prevented by req->connected being false.
2079 *
2080 * Progression of requests under I/O to the processing list is
Miklos Szeredif9a28422006-06-25 05:48:53 -07002081 * prevented by the req->aborted flag being true for these requests.
2082 * For this reason requests on the io list must be aborted first.
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002083 */
2084void fuse_abort_conn(struct fuse_conn *fc)
2085{
Miklos Szeredid7133112006-04-10 22:54:55 -07002086 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002087 if (fc->connected) {
2088 fc->connected = 0;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -07002089 fc->blocked = 0;
Maxim Patlasov796523fb2013-03-21 18:02:15 +04002090 fc->initialized = 1;
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002091 end_io_requests(fc);
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002092 end_queued_requests(fc);
Bryan Green357ccf22011-03-01 16:43:52 -08002093 end_polls(fc);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002094 wake_up_all(&fc->waitq);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -07002095 wake_up_all(&fc->blocked_waitq);
Jeff Dike385a17b2006-04-10 22:54:52 -07002096 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002097 }
Miklos Szeredid7133112006-04-10 22:54:55 -07002098 spin_unlock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002099}
Tejun Heo08cbf542009-04-14 10:54:53 +09002100EXPORT_SYMBOL_GPL(fuse_abort_conn);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002101
Tejun Heo08cbf542009-04-14 10:54:53 +09002102int fuse_dev_release(struct inode *inode, struct file *file)
Miklos Szeredi334f4852005-09-09 13:10:27 -07002103{
Miklos Szeredi0720b312006-04-10 22:54:55 -07002104 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002105 if (fc) {
Miklos Szeredid7133112006-04-10 22:54:55 -07002106 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -07002107 fc->connected = 0;
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002108 fc->blocked = 0;
2109 end_queued_requests(fc);
Bryan Green357ccf22011-03-01 16:43:52 -08002110 end_polls(fc);
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002111 wake_up_all(&fc->blocked_waitq);
Miklos Szeredid7133112006-04-10 22:54:55 -07002112 spin_unlock(&fc->lock);
Miklos Szeredibafa9652006-06-25 05:48:51 -07002113 fuse_conn_put(fc);
Jeff Dike385a17b2006-04-10 22:54:52 -07002114 }
Miklos Szeredif543f252006-01-16 22:14:35 -08002115
Miklos Szeredi334f4852005-09-09 13:10:27 -07002116 return 0;
2117}
Tejun Heo08cbf542009-04-14 10:54:53 +09002118EXPORT_SYMBOL_GPL(fuse_dev_release);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002119
Jeff Dike385a17b2006-04-10 22:54:52 -07002120static int fuse_dev_fasync(int fd, struct file *file, int on)
2121{
2122 struct fuse_conn *fc = fuse_get_conn(file);
2123 if (!fc)
Miklos Szeredia87046d2006-04-10 22:54:56 -07002124 return -EPERM;
Jeff Dike385a17b2006-04-10 22:54:52 -07002125
2126 /* No locking - fasync_helper does its own locking */
2127 return fasync_helper(fd, file, on, &fc->fasync);
2128}
2129
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002130const struct file_operations fuse_dev_operations = {
Miklos Szeredi334f4852005-09-09 13:10:27 -07002131 .owner = THIS_MODULE,
2132 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002133 .read = do_sync_read,
2134 .aio_read = fuse_dev_read,
Miklos Szeredic3021622010-05-25 15:06:07 +02002135 .splice_read = fuse_dev_splice_read,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002136 .write = do_sync_write,
2137 .aio_write = fuse_dev_write,
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002138 .splice_write = fuse_dev_splice_write,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002139 .poll = fuse_dev_poll,
2140 .release = fuse_dev_release,
Jeff Dike385a17b2006-04-10 22:54:52 -07002141 .fasync = fuse_dev_fasync,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002142};
Tejun Heo08cbf542009-04-14 10:54:53 +09002143EXPORT_SYMBOL_GPL(fuse_dev_operations);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002144
2145static struct miscdevice fuse_miscdevice = {
2146 .minor = FUSE_MINOR,
2147 .name = "fuse",
2148 .fops = &fuse_dev_operations,
2149};
2150
2151int __init fuse_dev_init(void)
2152{
2153 int err = -ENOMEM;
2154 fuse_req_cachep = kmem_cache_create("fuse_request",
2155 sizeof(struct fuse_req),
Paul Mundt20c2df82007-07-20 10:11:58 +09002156 0, 0, NULL);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002157 if (!fuse_req_cachep)
2158 goto out;
2159
2160 err = misc_register(&fuse_miscdevice);
2161 if (err)
2162 goto out_cache_clean;
2163
2164 return 0;
2165
2166 out_cache_clean:
2167 kmem_cache_destroy(fuse_req_cachep);
2168 out:
2169 return err;
2170}
2171
2172void fuse_dev_cleanup(void)
2173{
2174 misc_deregister(&fuse_miscdevice);
2175 kmem_cache_destroy(fuse_req_cachep);
2176}