blob: af37ae138252acaa8892ce6d598adab10534f9cf [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,
38 unsigned npages)
Miklos Szeredi334f4852005-09-09 13:10:27 -070039{
40 memset(req, 0, sizeof(*req));
Maxim Patlasov4250c062012-10-26 19:48:07 +040041 memset(pages, 0, sizeof(*pages) * npages);
Miklos Szeredi334f4852005-09-09 13:10:27 -070042 INIT_LIST_HEAD(&req->list);
Miklos Szeredia4d27e72006-06-25 05:48:54 -070043 INIT_LIST_HEAD(&req->intr_entry);
Miklos Szeredi334f4852005-09-09 13:10:27 -070044 init_waitqueue_head(&req->waitq);
45 atomic_set(&req->count, 1);
Maxim Patlasov4250c062012-10-26 19:48:07 +040046 req->pages = pages;
47 req->max_pages = npages;
Miklos Szeredi334f4852005-09-09 13:10:27 -070048}
49
Maxim Patlasov4250c062012-10-26 19:48:07 +040050static struct fuse_req *__fuse_request_alloc(unsigned npages, gfp_t flags)
Miklos Szeredi334f4852005-09-09 13:10:27 -070051{
Maxim Patlasov4250c062012-10-26 19:48:07 +040052 struct fuse_req *req = kmem_cache_alloc(fuse_req_cachep, flags);
53 if (req) {
54 struct page **pages;
55
56 if (npages <= FUSE_REQ_INLINE_PAGES)
57 pages = req->inline_pages;
58 else
59 pages = kmalloc(sizeof(struct page *) * npages, flags);
60
61 if (!pages) {
62 kmem_cache_free(fuse_req_cachep, req);
63 return NULL;
64 }
65
66 fuse_request_init(req, pages, npages);
67 }
Miklos Szeredi334f4852005-09-09 13:10:27 -070068 return req;
69}
Maxim Patlasov4250c062012-10-26 19:48:07 +040070
71struct fuse_req *fuse_request_alloc(unsigned npages)
72{
73 return __fuse_request_alloc(npages, GFP_KERNEL);
74}
Tejun Heo08cbf542009-04-14 10:54:53 +090075EXPORT_SYMBOL_GPL(fuse_request_alloc);
Miklos Szeredi334f4852005-09-09 13:10:27 -070076
Maxim Patlasov4250c062012-10-26 19:48:07 +040077struct fuse_req *fuse_request_alloc_nofs(unsigned npages)
Miklos Szeredi3be5a522008-04-30 00:54:41 -070078{
Maxim Patlasov4250c062012-10-26 19:48:07 +040079 return __fuse_request_alloc(npages, GFP_NOFS);
Miklos Szeredi3be5a522008-04-30 00:54:41 -070080}
81
Miklos Szeredi334f4852005-09-09 13:10:27 -070082void fuse_request_free(struct fuse_req *req)
83{
Maxim Patlasov4250c062012-10-26 19:48:07 +040084 if (req->pages != req->inline_pages)
85 kfree(req->pages);
Miklos Szeredi334f4852005-09-09 13:10:27 -070086 kmem_cache_free(fuse_req_cachep, req);
87}
88
Miklos Szeredi8bfc0162006-01-16 22:14:28 -080089static void block_sigs(sigset_t *oldset)
Miklos Szeredi334f4852005-09-09 13:10:27 -070090{
91 sigset_t mask;
92
93 siginitsetinv(&mask, sigmask(SIGKILL));
94 sigprocmask(SIG_BLOCK, &mask, oldset);
95}
96
Miklos Szeredi8bfc0162006-01-16 22:14:28 -080097static void restore_sigs(sigset_t *oldset)
Miklos Szeredi334f4852005-09-09 13:10:27 -070098{
99 sigprocmask(SIG_SETMASK, oldset, NULL);
100}
101
Miklos Szeredi334f4852005-09-09 13:10:27 -0700102static void __fuse_get_request(struct fuse_req *req)
103{
104 atomic_inc(&req->count);
105}
106
107/* Must be called with > 1 refcount */
108static void __fuse_put_request(struct fuse_req *req)
109{
110 BUG_ON(atomic_read(&req->count) < 2);
111 atomic_dec(&req->count);
112}
113
Miklos Szeredi33649c92006-06-25 05:48:52 -0700114static void fuse_req_init_context(struct fuse_req *req)
115{
Eric W. Biederman499dcf22012-02-07 16:26:03 -0800116 req->in.h.uid = from_kuid_munged(&init_user_ns, current_fsuid());
117 req->in.h.gid = from_kgid_munged(&init_user_ns, current_fsgid());
Miklos Szeredi33649c92006-06-25 05:48:52 -0700118 req->in.h.pid = current->pid;
119}
120
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700121struct fuse_req *fuse_get_req(struct fuse_conn *fc)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700122{
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700123 struct fuse_req *req;
124 sigset_t oldset;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200125 int intr;
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700126 int err;
127
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200128 atomic_inc(&fc->num_waiting);
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700129 block_sigs(&oldset);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200130 intr = wait_event_interruptible(fc->blocked_waitq, !fc->blocked);
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700131 restore_sigs(&oldset);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200132 err = -EINTR;
133 if (intr)
134 goto out;
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700135
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700136 err = -ENOTCONN;
137 if (!fc->connected)
138 goto out;
139
Maxim Patlasov4250c062012-10-26 19:48:07 +0400140 req = fuse_request_alloc(FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200141 err = -ENOMEM;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700142 if (!req)
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200143 goto out;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700144
Miklos Szeredi33649c92006-06-25 05:48:52 -0700145 fuse_req_init_context(req);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200146 req->waiting = 1;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700147 return req;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200148
149 out:
150 atomic_dec(&fc->num_waiting);
151 return ERR_PTR(err);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700152}
Tejun Heo08cbf542009-04-14 10:54:53 +0900153EXPORT_SYMBOL_GPL(fuse_get_req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700154
Miklos Szeredi33649c92006-06-25 05:48:52 -0700155/*
156 * Return request in fuse_file->reserved_req. However that may
157 * currently be in use. If that is the case, wait for it to become
158 * available.
159 */
160static struct fuse_req *get_reserved_req(struct fuse_conn *fc,
161 struct file *file)
162{
163 struct fuse_req *req = NULL;
164 struct fuse_file *ff = file->private_data;
165
166 do {
Miklos Szeredide5e3de2007-10-16 23:31:00 -0700167 wait_event(fc->reserved_req_waitq, ff->reserved_req);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700168 spin_lock(&fc->lock);
169 if (ff->reserved_req) {
170 req = ff->reserved_req;
171 ff->reserved_req = NULL;
Al Virocb0942b2012-08-27 14:48:26 -0400172 req->stolen_file = get_file(file);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700173 }
174 spin_unlock(&fc->lock);
175 } while (!req);
176
177 return req;
178}
179
180/*
181 * Put stolen request back into fuse_file->reserved_req
182 */
183static void put_reserved_req(struct fuse_conn *fc, struct fuse_req *req)
184{
185 struct file *file = req->stolen_file;
186 struct fuse_file *ff = file->private_data;
187
188 spin_lock(&fc->lock);
Maxim Patlasov4250c062012-10-26 19:48:07 +0400189 fuse_request_init(req, req->pages, req->max_pages);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700190 BUG_ON(ff->reserved_req);
191 ff->reserved_req = req;
Miklos Szeredide5e3de2007-10-16 23:31:00 -0700192 wake_up_all(&fc->reserved_req_waitq);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700193 spin_unlock(&fc->lock);
194 fput(file);
195}
196
197/*
198 * Gets a requests for a file operation, always succeeds
199 *
200 * This is used for sending the FLUSH request, which must get to
201 * userspace, due to POSIX locks which may need to be unlocked.
202 *
203 * If allocation fails due to OOM, use the reserved request in
204 * fuse_file.
205 *
206 * This is very unlikely to deadlock accidentally, since the
207 * filesystem should not have it's own file open. If deadlock is
208 * intentional, it can still be broken by "aborting" the filesystem.
209 */
210struct fuse_req *fuse_get_req_nofail(struct fuse_conn *fc, struct file *file)
211{
212 struct fuse_req *req;
213
214 atomic_inc(&fc->num_waiting);
215 wait_event(fc->blocked_waitq, !fc->blocked);
Maxim Patlasov4250c062012-10-26 19:48:07 +0400216 req = fuse_request_alloc(FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700217 if (!req)
218 req = get_reserved_req(fc, file);
219
220 fuse_req_init_context(req);
221 req->waiting = 1;
222 return req;
223}
224
Miklos Szeredi334f4852005-09-09 13:10:27 -0700225void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req)
226{
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800227 if (atomic_dec_and_test(&req->count)) {
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200228 if (req->waiting)
229 atomic_dec(&fc->num_waiting);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700230
231 if (req->stolen_file)
232 put_reserved_req(fc, req);
233 else
234 fuse_request_free(req);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800235 }
236}
Tejun Heo08cbf542009-04-14 10:54:53 +0900237EXPORT_SYMBOL_GPL(fuse_put_request);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800238
Miklos Szeredid12def12008-02-06 01:38:39 -0800239static unsigned len_args(unsigned numargs, struct fuse_arg *args)
240{
241 unsigned nbytes = 0;
242 unsigned i;
243
244 for (i = 0; i < numargs; i++)
245 nbytes += args[i].size;
246
247 return nbytes;
248}
249
250static u64 fuse_get_unique(struct fuse_conn *fc)
251{
252 fc->reqctr++;
253 /* zero is special */
254 if (fc->reqctr == 0)
255 fc->reqctr = 1;
256
257 return fc->reqctr;
258}
259
260static void queue_request(struct fuse_conn *fc, struct fuse_req *req)
261{
Miklos Szeredid12def12008-02-06 01:38:39 -0800262 req->in.h.len = sizeof(struct fuse_in_header) +
263 len_args(req->in.numargs, (struct fuse_arg *) req->in.args);
264 list_add_tail(&req->list, &fc->pending);
265 req->state = FUSE_REQ_PENDING;
266 if (!req->waiting) {
267 req->waiting = 1;
268 atomic_inc(&fc->num_waiting);
269 }
270 wake_up(&fc->waitq);
271 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
272}
273
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100274void fuse_queue_forget(struct fuse_conn *fc, struct fuse_forget_link *forget,
275 u64 nodeid, u64 nlookup)
276{
Miklos Szeredi02c048b2010-12-07 20:16:56 +0100277 forget->forget_one.nodeid = nodeid;
278 forget->forget_one.nlookup = nlookup;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100279
280 spin_lock(&fc->lock);
Miklos Szeredi5dfcc872011-09-12 09:38:03 +0200281 if (fc->connected) {
282 fc->forget_list_tail->next = forget;
283 fc->forget_list_tail = forget;
284 wake_up(&fc->waitq);
285 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
286 } else {
287 kfree(forget);
288 }
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100289 spin_unlock(&fc->lock);
290}
291
Miklos Szeredid12def12008-02-06 01:38:39 -0800292static void flush_bg_queue(struct fuse_conn *fc)
293{
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700294 while (fc->active_background < fc->max_background &&
Miklos Szeredid12def12008-02-06 01:38:39 -0800295 !list_empty(&fc->bg_queue)) {
296 struct fuse_req *req;
297
298 req = list_entry(fc->bg_queue.next, struct fuse_req, list);
299 list_del(&req->list);
300 fc->active_background++;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200301 req->in.h.unique = fuse_get_unique(fc);
Miklos Szeredid12def12008-02-06 01:38:39 -0800302 queue_request(fc, req);
303 }
304}
305
Miklos Szeredi6dbbcb12006-04-26 10:49:06 +0200306/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700307 * This function is called when a request is finished. Either a reply
Miklos Szeredif9a28422006-06-25 05:48:53 -0700308 * has arrived or it was aborted (and not yet sent) or some error
Miklos Szeredif43b1552006-01-16 22:14:26 -0800309 * occurred during communication with userspace, or the device file
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700310 * was closed. The requester thread is woken up (if still waiting),
311 * the 'end' callback is called if given, else the reference to the
312 * request is released
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800313 *
Miklos Szeredid7133112006-04-10 22:54:55 -0700314 * Called with fc->lock, unlocks it
Miklos Szeredi334f4852005-09-09 13:10:27 -0700315 */
316static void request_end(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +0200317__releases(fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700318{
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700319 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
320 req->end = NULL;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800321 list_del(&req->list);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700322 list_del(&req->intr_entry);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800323 req->state = FUSE_REQ_FINISHED;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700324 if (req->background) {
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700325 if (fc->num_background == fc->max_background) {
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700326 fc->blocked = 0;
327 wake_up_all(&fc->blocked_waitq);
328 }
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700329 if (fc->num_background == fc->congestion_threshold &&
Tejun Heoa325f9b2009-04-14 10:54:52 +0900330 fc->connected && fc->bdi_initialized) {
Jens Axboe8aa7e842009-07-09 14:52:32 +0200331 clear_bdi_congested(&fc->bdi, BLK_RW_SYNC);
332 clear_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
Miklos Szeredif92b99b2007-10-16 23:30:59 -0700333 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700334 fc->num_background--;
Miklos Szeredid12def12008-02-06 01:38:39 -0800335 fc->active_background--;
336 flush_bg_queue(fc);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700337 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700338 spin_unlock(&fc->lock);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700339 wake_up(&req->waitq);
340 if (end)
341 end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100342 fuse_put_request(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700343}
344
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700345static void wait_answer_interruptible(struct fuse_conn *fc,
346 struct fuse_req *req)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +0200347__releases(fc->lock)
348__acquires(fc->lock)
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700349{
350 if (signal_pending(current))
351 return;
352
353 spin_unlock(&fc->lock);
354 wait_event_interruptible(req->waitq, req->state == FUSE_REQ_FINISHED);
355 spin_lock(&fc->lock);
356}
357
358static void queue_interrupt(struct fuse_conn *fc, struct fuse_req *req)
359{
360 list_add_tail(&req->intr_entry, &fc->interrupts);
361 wake_up(&fc->waitq);
362 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
363}
364
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700365static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +0200366__releases(fc->lock)
367__acquires(fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700368{
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700369 if (!fc->no_interrupt) {
370 /* Any signal may interrupt this */
371 wait_answer_interruptible(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700372
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700373 if (req->aborted)
374 goto aborted;
375 if (req->state == FUSE_REQ_FINISHED)
376 return;
377
378 req->interrupted = 1;
379 if (req->state == FUSE_REQ_SENT)
380 queue_interrupt(fc, req);
381 }
382
Miklos Szeredia131de02007-10-16 23:31:04 -0700383 if (!req->force) {
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700384 sigset_t oldset;
385
386 /* Only fatal signals may interrupt this */
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700387 block_sigs(&oldset);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700388 wait_answer_interruptible(fc, req);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700389 restore_sigs(&oldset);
Miklos Szeredia131de02007-10-16 23:31:04 -0700390
391 if (req->aborted)
392 goto aborted;
393 if (req->state == FUSE_REQ_FINISHED)
394 return;
395
396 /* Request is not yet in userspace, bail out */
397 if (req->state == FUSE_REQ_PENDING) {
398 list_del(&req->list);
399 __fuse_put_request(req);
400 req->out.h.error = -EINTR;
401 return;
402 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700403 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700404
Miklos Szeredia131de02007-10-16 23:31:04 -0700405 /*
406 * Either request is already in userspace, or it was forced.
407 * Wait it out.
408 */
409 spin_unlock(&fc->lock);
410 wait_event(req->waitq, req->state == FUSE_REQ_FINISHED);
411 spin_lock(&fc->lock);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700412
Miklos Szeredia131de02007-10-16 23:31:04 -0700413 if (!req->aborted)
414 return;
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700415
416 aborted:
Miklos Szeredia131de02007-10-16 23:31:04 -0700417 BUG_ON(req->state != FUSE_REQ_FINISHED);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700418 if (req->locked) {
419 /* This is uninterruptible sleep, because data is
420 being copied to/from the buffers of req. During
421 locked state, there mustn't be any filesystem
422 operation (e.g. page fault), since that could lead
423 to deadlock */
Miklos Szeredid7133112006-04-10 22:54:55 -0700424 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700425 wait_event(req->waitq, !req->locked);
Miklos Szeredid7133112006-04-10 22:54:55 -0700426 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700427 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700428}
429
Tejun Heob93f8582008-11-26 12:03:55 +0100430void fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700431{
432 req->isreply = 1;
Miklos Szeredid7133112006-04-10 22:54:55 -0700433 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700434 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700435 req->out.h.error = -ENOTCONN;
436 else if (fc->conn_error)
437 req->out.h.error = -ECONNREFUSED;
438 else {
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200439 req->in.h.unique = fuse_get_unique(fc);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700440 queue_request(fc, req);
441 /* acquire extra reference, since request is still needed
442 after request_end() */
443 __fuse_get_request(req);
444
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700445 request_wait_answer(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700446 }
Miklos Szeredid7133112006-04-10 22:54:55 -0700447 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700448}
Tejun Heo08cbf542009-04-14 10:54:53 +0900449EXPORT_SYMBOL_GPL(fuse_request_send);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700450
Tejun Heob93f8582008-11-26 12:03:55 +0100451static void fuse_request_send_nowait_locked(struct fuse_conn *fc,
452 struct fuse_req *req)
Miklos Szeredid12def12008-02-06 01:38:39 -0800453{
454 req->background = 1;
455 fc->num_background++;
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700456 if (fc->num_background == fc->max_background)
Miklos Szeredid12def12008-02-06 01:38:39 -0800457 fc->blocked = 1;
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700458 if (fc->num_background == fc->congestion_threshold &&
Tejun Heoa325f9b2009-04-14 10:54:52 +0900459 fc->bdi_initialized) {
Jens Axboe8aa7e842009-07-09 14:52:32 +0200460 set_bdi_congested(&fc->bdi, BLK_RW_SYNC);
461 set_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
Miklos Szeredid12def12008-02-06 01:38:39 -0800462 }
463 list_add_tail(&req->list, &fc->bg_queue);
464 flush_bg_queue(fc);
465}
466
Tejun Heob93f8582008-11-26 12:03:55 +0100467static void fuse_request_send_nowait(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700468{
Miklos Szeredid7133112006-04-10 22:54:55 -0700469 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700470 if (fc->connected) {
Tejun Heob93f8582008-11-26 12:03:55 +0100471 fuse_request_send_nowait_locked(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700472 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700473 } else {
474 req->out.h.error = -ENOTCONN;
475 request_end(fc, req);
476 }
477}
478
Tejun Heob93f8582008-11-26 12:03:55 +0100479void fuse_request_send_background(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700480{
481 req->isreply = 1;
Tejun Heob93f8582008-11-26 12:03:55 +0100482 fuse_request_send_nowait(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700483}
Tejun Heo08cbf542009-04-14 10:54:53 +0900484EXPORT_SYMBOL_GPL(fuse_request_send_background);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700485
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200486static int fuse_request_send_notify_reply(struct fuse_conn *fc,
487 struct fuse_req *req, u64 unique)
488{
489 int err = -ENODEV;
490
491 req->isreply = 0;
492 req->in.h.unique = unique;
493 spin_lock(&fc->lock);
494 if (fc->connected) {
495 queue_request(fc, req);
496 err = 0;
497 }
498 spin_unlock(&fc->lock);
499
500 return err;
501}
502
Miklos Szeredi334f4852005-09-09 13:10:27 -0700503/*
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700504 * Called under fc->lock
505 *
506 * fc->connected must have been checked previously
507 */
Tejun Heob93f8582008-11-26 12:03:55 +0100508void fuse_request_send_background_locked(struct fuse_conn *fc,
509 struct fuse_req *req)
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700510{
511 req->isreply = 1;
Tejun Heob93f8582008-11-26 12:03:55 +0100512 fuse_request_send_nowait_locked(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700513}
514
Anand V. Avati0b05b182012-08-19 08:53:23 -0400515void fuse_force_forget(struct file *file, u64 nodeid)
516{
517 struct inode *inode = file->f_path.dentry->d_inode;
518 struct fuse_conn *fc = get_fuse_conn(inode);
519 struct fuse_req *req;
520 struct fuse_forget_in inarg;
521
522 memset(&inarg, 0, sizeof(inarg));
523 inarg.nlookup = 1;
524 req = fuse_get_req_nofail(fc, file);
525 req->in.h.opcode = FUSE_FORGET;
526 req->in.h.nodeid = nodeid;
527 req->in.numargs = 1;
528 req->in.args[0].size = sizeof(inarg);
529 req->in.args[0].value = &inarg;
530 req->isreply = 0;
531 fuse_request_send_nowait(fc, req);
532}
533
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700534/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700535 * Lock the request. Up to the next unlock_request() there mustn't be
536 * anything that could cause a page-fault. If the request was already
Miklos Szeredif9a28422006-06-25 05:48:53 -0700537 * aborted bail out.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700538 */
Miklos Szeredid7133112006-04-10 22:54:55 -0700539static int lock_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700540{
541 int err = 0;
542 if (req) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700543 spin_lock(&fc->lock);
Miklos Szeredif9a28422006-06-25 05:48:53 -0700544 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700545 err = -ENOENT;
546 else
547 req->locked = 1;
Miklos Szeredid7133112006-04-10 22:54:55 -0700548 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700549 }
550 return err;
551}
552
553/*
Miklos Szeredif9a28422006-06-25 05:48:53 -0700554 * Unlock request. If it was aborted during being locked, the
Miklos Szeredi334f4852005-09-09 13:10:27 -0700555 * requester thread is currently waiting for it to be unlocked, so
556 * wake it up.
557 */
Miklos Szeredid7133112006-04-10 22:54:55 -0700558static void unlock_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700559{
560 if (req) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700561 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700562 req->locked = 0;
Miklos Szeredif9a28422006-06-25 05:48:53 -0700563 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700564 wake_up(&req->waitq);
Miklos Szeredid7133112006-04-10 22:54:55 -0700565 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700566 }
567}
568
569struct fuse_copy_state {
Miklos Szeredid7133112006-04-10 22:54:55 -0700570 struct fuse_conn *fc;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700571 int write;
572 struct fuse_req *req;
573 const struct iovec *iov;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200574 struct pipe_buffer *pipebufs;
575 struct pipe_buffer *currbuf;
576 struct pipe_inode_info *pipe;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700577 unsigned long nr_segs;
578 unsigned long seglen;
579 unsigned long addr;
580 struct page *pg;
581 void *mapaddr;
582 void *buf;
583 unsigned len;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200584 unsigned move_pages:1;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700585};
586
Miklos Szeredid7133112006-04-10 22:54:55 -0700587static void fuse_copy_init(struct fuse_copy_state *cs, struct fuse_conn *fc,
Miklos Szeredic3021622010-05-25 15:06:07 +0200588 int write,
Miklos Szeredid7133112006-04-10 22:54:55 -0700589 const struct iovec *iov, unsigned long nr_segs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700590{
591 memset(cs, 0, sizeof(*cs));
Miklos Szeredid7133112006-04-10 22:54:55 -0700592 cs->fc = fc;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700593 cs->write = write;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700594 cs->iov = iov;
595 cs->nr_segs = nr_segs;
596}
597
598/* Unmap and put previous page of userspace buffer */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800599static void fuse_copy_finish(struct fuse_copy_state *cs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700600{
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200601 if (cs->currbuf) {
602 struct pipe_buffer *buf = cs->currbuf;
603
Miklos Szeredic3021622010-05-25 15:06:07 +0200604 if (!cs->write) {
605 buf->ops->unmap(cs->pipe, buf, cs->mapaddr);
606 } else {
Miklos Szeredi7909b1c2010-07-12 14:41:40 +0200607 kunmap(buf->page);
Miklos Szeredic3021622010-05-25 15:06:07 +0200608 buf->len = PAGE_SIZE - cs->len;
609 }
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200610 cs->currbuf = NULL;
611 cs->mapaddr = NULL;
612 } else if (cs->mapaddr) {
Miklos Szeredi7909b1c2010-07-12 14:41:40 +0200613 kunmap(cs->pg);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700614 if (cs->write) {
615 flush_dcache_page(cs->pg);
616 set_page_dirty_lock(cs->pg);
617 }
618 put_page(cs->pg);
619 cs->mapaddr = NULL;
620 }
621}
622
623/*
624 * Get another pagefull of userspace buffer, and map it to kernel
625 * address space, and lock request
626 */
627static int fuse_copy_fill(struct fuse_copy_state *cs)
628{
629 unsigned long offset;
630 int err;
631
Miklos Szeredid7133112006-04-10 22:54:55 -0700632 unlock_request(cs->fc, cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700633 fuse_copy_finish(cs);
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200634 if (cs->pipebufs) {
635 struct pipe_buffer *buf = cs->pipebufs;
636
Miklos Szeredic3021622010-05-25 15:06:07 +0200637 if (!cs->write) {
638 err = buf->ops->confirm(cs->pipe, buf);
639 if (err)
640 return err;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200641
Miklos Szeredic3021622010-05-25 15:06:07 +0200642 BUG_ON(!cs->nr_segs);
643 cs->currbuf = buf;
Miklos Szeredi7909b1c2010-07-12 14:41:40 +0200644 cs->mapaddr = buf->ops->map(cs->pipe, buf, 0);
Miklos Szeredic3021622010-05-25 15:06:07 +0200645 cs->len = buf->len;
646 cs->buf = cs->mapaddr + buf->offset;
647 cs->pipebufs++;
648 cs->nr_segs--;
649 } else {
650 struct page *page;
651
652 if (cs->nr_segs == cs->pipe->buffers)
653 return -EIO;
654
655 page = alloc_page(GFP_HIGHUSER);
656 if (!page)
657 return -ENOMEM;
658
659 buf->page = page;
660 buf->offset = 0;
661 buf->len = 0;
662
663 cs->currbuf = buf;
Miklos Szeredi7909b1c2010-07-12 14:41:40 +0200664 cs->mapaddr = kmap(page);
Miklos Szeredic3021622010-05-25 15:06:07 +0200665 cs->buf = cs->mapaddr;
666 cs->len = PAGE_SIZE;
667 cs->pipebufs++;
668 cs->nr_segs++;
669 }
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200670 } else {
671 if (!cs->seglen) {
672 BUG_ON(!cs->nr_segs);
673 cs->seglen = cs->iov[0].iov_len;
674 cs->addr = (unsigned long) cs->iov[0].iov_base;
675 cs->iov++;
676 cs->nr_segs--;
677 }
678 err = get_user_pages_fast(cs->addr, 1, cs->write, &cs->pg);
679 if (err < 0)
680 return err;
681 BUG_ON(err != 1);
682 offset = cs->addr % PAGE_SIZE;
Miklos Szeredi7909b1c2010-07-12 14:41:40 +0200683 cs->mapaddr = kmap(cs->pg);
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200684 cs->buf = cs->mapaddr + offset;
685 cs->len = min(PAGE_SIZE - offset, cs->seglen);
686 cs->seglen -= cs->len;
687 cs->addr += cs->len;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700688 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700689
Miklos Szeredid7133112006-04-10 22:54:55 -0700690 return lock_request(cs->fc, cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700691}
692
693/* Do as much copy to/from userspace buffer as we can */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800694static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700695{
696 unsigned ncpy = min(*size, cs->len);
697 if (val) {
698 if (cs->write)
699 memcpy(cs->buf, *val, ncpy);
700 else
701 memcpy(*val, cs->buf, ncpy);
702 *val += ncpy;
703 }
704 *size -= ncpy;
705 cs->len -= ncpy;
706 cs->buf += ncpy;
707 return ncpy;
708}
709
Miklos Szeredice534fb2010-05-25 15:06:07 +0200710static int fuse_check_page(struct page *page)
711{
712 if (page_mapcount(page) ||
713 page->mapping != NULL ||
714 page_count(page) != 1 ||
715 (page->flags & PAGE_FLAGS_CHECK_AT_PREP &
716 ~(1 << PG_locked |
717 1 << PG_referenced |
718 1 << PG_uptodate |
719 1 << PG_lru |
720 1 << PG_active |
721 1 << PG_reclaim))) {
722 printk(KERN_WARNING "fuse: trying to steal weird page\n");
723 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);
724 return 1;
725 }
726 return 0;
727}
728
729static int fuse_try_move_page(struct fuse_copy_state *cs, struct page **pagep)
730{
731 int err;
732 struct page *oldpage = *pagep;
733 struct page *newpage;
734 struct pipe_buffer *buf = cs->pipebufs;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200735
736 unlock_request(cs->fc, cs->req);
737 fuse_copy_finish(cs);
738
739 err = buf->ops->confirm(cs->pipe, buf);
740 if (err)
741 return err;
742
743 BUG_ON(!cs->nr_segs);
744 cs->currbuf = buf;
745 cs->len = buf->len;
746 cs->pipebufs++;
747 cs->nr_segs--;
748
749 if (cs->len != PAGE_SIZE)
750 goto out_fallback;
751
752 if (buf->ops->steal(cs->pipe, buf) != 0)
753 goto out_fallback;
754
755 newpage = buf->page;
756
757 if (WARN_ON(!PageUptodate(newpage)))
758 return -EIO;
759
760 ClearPageMappedToDisk(newpage);
761
762 if (fuse_check_page(newpage) != 0)
763 goto out_fallback_unlock;
764
Miklos Szeredice534fb2010-05-25 15:06:07 +0200765 /*
766 * This is a new and locked page, it shouldn't be mapped or
767 * have any special flags on it
768 */
769 if (WARN_ON(page_mapped(oldpage)))
770 goto out_fallback_unlock;
771 if (WARN_ON(page_has_private(oldpage)))
772 goto out_fallback_unlock;
773 if (WARN_ON(PageDirty(oldpage) || PageWriteback(oldpage)))
774 goto out_fallback_unlock;
775 if (WARN_ON(PageMlocked(oldpage)))
776 goto out_fallback_unlock;
777
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700778 err = replace_page_cache_page(oldpage, newpage, GFP_KERNEL);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200779 if (err) {
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700780 unlock_page(newpage);
781 return err;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200782 }
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700783
Miklos Szeredice534fb2010-05-25 15:06:07 +0200784 page_cache_get(newpage);
785
786 if (!(buf->flags & PIPE_BUF_FLAG_LRU))
787 lru_cache_add_file(newpage);
788
789 err = 0;
790 spin_lock(&cs->fc->lock);
791 if (cs->req->aborted)
792 err = -ENOENT;
793 else
794 *pagep = newpage;
795 spin_unlock(&cs->fc->lock);
796
797 if (err) {
798 unlock_page(newpage);
799 page_cache_release(newpage);
800 return err;
801 }
802
803 unlock_page(oldpage);
804 page_cache_release(oldpage);
805 cs->len = 0;
806
807 return 0;
808
809out_fallback_unlock:
810 unlock_page(newpage);
811out_fallback:
812 cs->mapaddr = buf->ops->map(cs->pipe, buf, 1);
813 cs->buf = cs->mapaddr + buf->offset;
814
815 err = lock_request(cs->fc, cs->req);
816 if (err)
817 return err;
818
819 return 1;
820}
821
Miklos Szeredic3021622010-05-25 15:06:07 +0200822static int fuse_ref_page(struct fuse_copy_state *cs, struct page *page,
823 unsigned offset, unsigned count)
824{
825 struct pipe_buffer *buf;
826
827 if (cs->nr_segs == cs->pipe->buffers)
828 return -EIO;
829
830 unlock_request(cs->fc, cs->req);
831 fuse_copy_finish(cs);
832
833 buf = cs->pipebufs;
834 page_cache_get(page);
835 buf->page = page;
836 buf->offset = offset;
837 buf->len = count;
838
839 cs->pipebufs++;
840 cs->nr_segs++;
841 cs->len = 0;
842
843 return 0;
844}
845
Miklos Szeredi334f4852005-09-09 13:10:27 -0700846/*
847 * Copy a page in the request to/from the userspace buffer. Must be
848 * done atomically
849 */
Miklos Szeredice534fb2010-05-25 15:06:07 +0200850static int fuse_copy_page(struct fuse_copy_state *cs, struct page **pagep,
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800851 unsigned offset, unsigned count, int zeroing)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700852{
Miklos Szeredice534fb2010-05-25 15:06:07 +0200853 int err;
854 struct page *page = *pagep;
855
Miklos Szeredib6777c42010-10-26 14:22:27 -0700856 if (page && zeroing && count < PAGE_SIZE)
857 clear_highpage(page);
858
Miklos Szeredi334f4852005-09-09 13:10:27 -0700859 while (count) {
Miklos Szeredic3021622010-05-25 15:06:07 +0200860 if (cs->write && cs->pipebufs && page) {
861 return fuse_ref_page(cs, page, offset, count);
862 } else if (!cs->len) {
Miklos Szeredice534fb2010-05-25 15:06:07 +0200863 if (cs->move_pages && page &&
864 offset == 0 && count == PAGE_SIZE) {
865 err = fuse_try_move_page(cs, pagep);
866 if (err <= 0)
867 return err;
868 } else {
869 err = fuse_copy_fill(cs);
870 if (err)
871 return err;
872 }
Miklos Szeredi1729a162008-11-26 12:03:54 +0100873 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700874 if (page) {
Cong Wang2408f6e2011-11-25 23:14:30 +0800875 void *mapaddr = kmap_atomic(page);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700876 void *buf = mapaddr + offset;
877 offset += fuse_copy_do(cs, &buf, &count);
Cong Wang2408f6e2011-11-25 23:14:30 +0800878 kunmap_atomic(mapaddr);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700879 } else
880 offset += fuse_copy_do(cs, NULL, &count);
881 }
882 if (page && !cs->write)
883 flush_dcache_page(page);
884 return 0;
885}
886
887/* Copy pages in the request to/from userspace buffer */
888static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
889 int zeroing)
890{
891 unsigned i;
892 struct fuse_req *req = cs->req;
893 unsigned offset = req->page_offset;
894 unsigned count = min(nbytes, (unsigned) PAGE_SIZE - offset);
895
896 for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
Miklos Szeredice534fb2010-05-25 15:06:07 +0200897 int err;
898
899 err = fuse_copy_page(cs, &req->pages[i], offset, count,
900 zeroing);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700901 if (err)
902 return err;
903
904 nbytes -= count;
905 count = min(nbytes, (unsigned) PAGE_SIZE);
906 offset = 0;
907 }
908 return 0;
909}
910
911/* Copy a single argument in the request to/from userspace buffer */
912static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
913{
914 while (size) {
Miklos Szeredi1729a162008-11-26 12:03:54 +0100915 if (!cs->len) {
916 int err = fuse_copy_fill(cs);
917 if (err)
918 return err;
919 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700920 fuse_copy_do(cs, &val, &size);
921 }
922 return 0;
923}
924
925/* Copy request arguments to/from userspace buffer */
926static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
927 unsigned argpages, struct fuse_arg *args,
928 int zeroing)
929{
930 int err = 0;
931 unsigned i;
932
933 for (i = 0; !err && i < numargs; i++) {
934 struct fuse_arg *arg = &args[i];
935 if (i == numargs - 1 && argpages)
936 err = fuse_copy_pages(cs, arg->size, zeroing);
937 else
938 err = fuse_copy_one(cs, arg->value, arg->size);
939 }
940 return err;
941}
942
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100943static int forget_pending(struct fuse_conn *fc)
944{
945 return fc->forget_list_head.next != NULL;
946}
947
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700948static int request_pending(struct fuse_conn *fc)
949{
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100950 return !list_empty(&fc->pending) || !list_empty(&fc->interrupts) ||
951 forget_pending(fc);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700952}
953
Miklos Szeredi334f4852005-09-09 13:10:27 -0700954/* Wait until a request is available on the pending list */
955static void request_wait(struct fuse_conn *fc)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +0200956__releases(fc->lock)
957__acquires(fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700958{
959 DECLARE_WAITQUEUE(wait, current);
960
961 add_wait_queue_exclusive(&fc->waitq, &wait);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700962 while (fc->connected && !request_pending(fc)) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700963 set_current_state(TASK_INTERRUPTIBLE);
964 if (signal_pending(current))
965 break;
966
Miklos Szeredid7133112006-04-10 22:54:55 -0700967 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700968 schedule();
Miklos Szeredid7133112006-04-10 22:54:55 -0700969 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700970 }
971 set_current_state(TASK_RUNNING);
972 remove_wait_queue(&fc->waitq, &wait);
973}
974
975/*
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700976 * Transfer an interrupt request to userspace
977 *
978 * Unlike other requests this is assembled on demand, without a need
979 * to allocate a separate fuse_req structure.
980 *
981 * Called with fc->lock held, releases it
982 */
Miklos Szeredic3021622010-05-25 15:06:07 +0200983static int fuse_read_interrupt(struct fuse_conn *fc, struct fuse_copy_state *cs,
984 size_t nbytes, struct fuse_req *req)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +0200985__releases(fc->lock)
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700986{
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700987 struct fuse_in_header ih;
988 struct fuse_interrupt_in arg;
989 unsigned reqsize = sizeof(ih) + sizeof(arg);
990 int err;
991
992 list_del_init(&req->intr_entry);
993 req->intr_unique = fuse_get_unique(fc);
994 memset(&ih, 0, sizeof(ih));
995 memset(&arg, 0, sizeof(arg));
996 ih.len = reqsize;
997 ih.opcode = FUSE_INTERRUPT;
998 ih.unique = req->intr_unique;
999 arg.unique = req->in.h.unique;
1000
1001 spin_unlock(&fc->lock);
Miklos Szeredic3021622010-05-25 15:06:07 +02001002 if (nbytes < reqsize)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001003 return -EINVAL;
1004
Miklos Szeredic3021622010-05-25 15:06:07 +02001005 err = fuse_copy_one(cs, &ih, sizeof(ih));
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001006 if (!err)
Miklos Szeredic3021622010-05-25 15:06:07 +02001007 err = fuse_copy_one(cs, &arg, sizeof(arg));
1008 fuse_copy_finish(cs);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001009
1010 return err ? err : reqsize;
1011}
1012
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001013static struct fuse_forget_link *dequeue_forget(struct fuse_conn *fc,
1014 unsigned max,
1015 unsigned *countp)
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001016{
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001017 struct fuse_forget_link *head = fc->forget_list_head.next;
1018 struct fuse_forget_link **newhead = &head;
1019 unsigned count;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001020
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001021 for (count = 0; *newhead != NULL && count < max; count++)
1022 newhead = &(*newhead)->next;
1023
1024 fc->forget_list_head.next = *newhead;
1025 *newhead = NULL;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001026 if (fc->forget_list_head.next == NULL)
1027 fc->forget_list_tail = &fc->forget_list_head;
1028
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001029 if (countp != NULL)
1030 *countp = count;
1031
1032 return head;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001033}
1034
1035static int fuse_read_single_forget(struct fuse_conn *fc,
1036 struct fuse_copy_state *cs,
1037 size_t nbytes)
1038__releases(fc->lock)
1039{
1040 int err;
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001041 struct fuse_forget_link *forget = dequeue_forget(fc, 1, NULL);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001042 struct fuse_forget_in arg = {
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001043 .nlookup = forget->forget_one.nlookup,
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001044 };
1045 struct fuse_in_header ih = {
1046 .opcode = FUSE_FORGET,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001047 .nodeid = forget->forget_one.nodeid,
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001048 .unique = fuse_get_unique(fc),
1049 .len = sizeof(ih) + sizeof(arg),
1050 };
1051
1052 spin_unlock(&fc->lock);
1053 kfree(forget);
1054 if (nbytes < ih.len)
1055 return -EINVAL;
1056
1057 err = fuse_copy_one(cs, &ih, sizeof(ih));
1058 if (!err)
1059 err = fuse_copy_one(cs, &arg, sizeof(arg));
1060 fuse_copy_finish(cs);
1061
1062 if (err)
1063 return err;
1064
1065 return ih.len;
1066}
1067
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001068static int fuse_read_batch_forget(struct fuse_conn *fc,
1069 struct fuse_copy_state *cs, size_t nbytes)
1070__releases(fc->lock)
1071{
1072 int err;
1073 unsigned max_forgets;
1074 unsigned count;
1075 struct fuse_forget_link *head;
1076 struct fuse_batch_forget_in arg = { .count = 0 };
1077 struct fuse_in_header ih = {
1078 .opcode = FUSE_BATCH_FORGET,
1079 .unique = fuse_get_unique(fc),
1080 .len = sizeof(ih) + sizeof(arg),
1081 };
1082
1083 if (nbytes < ih.len) {
1084 spin_unlock(&fc->lock);
1085 return -EINVAL;
1086 }
1087
1088 max_forgets = (nbytes - ih.len) / sizeof(struct fuse_forget_one);
1089 head = dequeue_forget(fc, max_forgets, &count);
1090 spin_unlock(&fc->lock);
1091
1092 arg.count = count;
1093 ih.len += count * sizeof(struct fuse_forget_one);
1094 err = fuse_copy_one(cs, &ih, sizeof(ih));
1095 if (!err)
1096 err = fuse_copy_one(cs, &arg, sizeof(arg));
1097
1098 while (head) {
1099 struct fuse_forget_link *forget = head;
1100
1101 if (!err) {
1102 err = fuse_copy_one(cs, &forget->forget_one,
1103 sizeof(forget->forget_one));
1104 }
1105 head = forget->next;
1106 kfree(forget);
1107 }
1108
1109 fuse_copy_finish(cs);
1110
1111 if (err)
1112 return err;
1113
1114 return ih.len;
1115}
1116
1117static int fuse_read_forget(struct fuse_conn *fc, struct fuse_copy_state *cs,
1118 size_t nbytes)
1119__releases(fc->lock)
1120{
1121 if (fc->minor < 16 || fc->forget_list_head.next->next == NULL)
1122 return fuse_read_single_forget(fc, cs, nbytes);
1123 else
1124 return fuse_read_batch_forget(fc, cs, nbytes);
1125}
1126
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001127/*
Miklos Szeredi334f4852005-09-09 13:10:27 -07001128 * Read a single request into the userspace filesystem's buffer. This
1129 * function waits until a request is available, then removes it from
1130 * the pending list and copies request data to userspace buffer. If
Miklos Szeredif9a28422006-06-25 05:48:53 -07001131 * no reply is needed (FORGET) or request has been aborted or there
1132 * was an error during the copying then it's finished by calling
Miklos Szeredi334f4852005-09-09 13:10:27 -07001133 * request_end(). Otherwise add it to the processing list, and set
1134 * the 'sent' flag.
1135 */
Miklos Szeredic3021622010-05-25 15:06:07 +02001136static ssize_t fuse_dev_do_read(struct fuse_conn *fc, struct file *file,
1137 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001138{
1139 int err;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001140 struct fuse_req *req;
1141 struct fuse_in *in;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001142 unsigned reqsize;
1143
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001144 restart:
Miklos Szeredid7133112006-04-10 22:54:55 -07001145 spin_lock(&fc->lock);
Jeff Dikee5ac1d12006-04-10 22:54:53 -07001146 err = -EAGAIN;
1147 if ((file->f_flags & O_NONBLOCK) && fc->connected &&
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001148 !request_pending(fc))
Jeff Dikee5ac1d12006-04-10 22:54:53 -07001149 goto err_unlock;
1150
Miklos Szeredi334f4852005-09-09 13:10:27 -07001151 request_wait(fc);
1152 err = -ENODEV;
Miklos Szeredi9ba7cbb2006-01-16 22:14:34 -08001153 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001154 goto err_unlock;
1155 err = -ERESTARTSYS;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001156 if (!request_pending(fc))
Miklos Szeredi334f4852005-09-09 13:10:27 -07001157 goto err_unlock;
1158
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001159 if (!list_empty(&fc->interrupts)) {
1160 req = list_entry(fc->interrupts.next, struct fuse_req,
1161 intr_entry);
Miklos Szeredic3021622010-05-25 15:06:07 +02001162 return fuse_read_interrupt(fc, cs, nbytes, req);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001163 }
1164
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001165 if (forget_pending(fc)) {
1166 if (list_empty(&fc->pending) || fc->forget_batch-- > 0)
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001167 return fuse_read_forget(fc, cs, nbytes);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001168
1169 if (fc->forget_batch <= -8)
1170 fc->forget_batch = 16;
1171 }
1172
Miklos Szeredi334f4852005-09-09 13:10:27 -07001173 req = list_entry(fc->pending.next, struct fuse_req, list);
Miklos Szeredi83cfd492006-01-16 22:14:31 -08001174 req->state = FUSE_REQ_READING;
Miklos Szeredid77a1d52006-01-16 22:14:31 -08001175 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001176
1177 in = &req->in;
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001178 reqsize = in->h.len;
1179 /* If request is too large, reply with an error and restart the read */
Miklos Szeredic3021622010-05-25 15:06:07 +02001180 if (nbytes < reqsize) {
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001181 req->out.h.error = -EIO;
1182 /* SETXATTR is special, since it may contain too large data */
1183 if (in->h.opcode == FUSE_SETXATTR)
1184 req->out.h.error = -E2BIG;
1185 request_end(fc, req);
1186 goto restart;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001187 }
Miklos Szeredid7133112006-04-10 22:54:55 -07001188 spin_unlock(&fc->lock);
Miklos Szeredic3021622010-05-25 15:06:07 +02001189 cs->req = req;
1190 err = fuse_copy_one(cs, &in->h, sizeof(in->h));
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001191 if (!err)
Miklos Szeredic3021622010-05-25 15:06:07 +02001192 err = fuse_copy_args(cs, in->numargs, in->argpages,
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001193 (struct fuse_arg *) in->args, 0);
Miklos Szeredic3021622010-05-25 15:06:07 +02001194 fuse_copy_finish(cs);
Miklos Szeredid7133112006-04-10 22:54:55 -07001195 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001196 req->locked = 0;
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -07001197 if (req->aborted) {
1198 request_end(fc, req);
1199 return -ENODEV;
1200 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001201 if (err) {
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -07001202 req->out.h.error = -EIO;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001203 request_end(fc, req);
1204 return err;
1205 }
1206 if (!req->isreply)
1207 request_end(fc, req);
1208 else {
Miklos Szeredi83cfd492006-01-16 22:14:31 -08001209 req->state = FUSE_REQ_SENT;
Miklos Szeredid77a1d52006-01-16 22:14:31 -08001210 list_move_tail(&req->list, &fc->processing);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001211 if (req->interrupted)
1212 queue_interrupt(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -07001213 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001214 }
1215 return reqsize;
1216
1217 err_unlock:
Miklos Szeredid7133112006-04-10 22:54:55 -07001218 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001219 return err;
1220}
1221
Miklos Szeredic3021622010-05-25 15:06:07 +02001222static ssize_t fuse_dev_read(struct kiocb *iocb, const struct iovec *iov,
1223 unsigned long nr_segs, loff_t pos)
1224{
1225 struct fuse_copy_state cs;
1226 struct file *file = iocb->ki_filp;
1227 struct fuse_conn *fc = fuse_get_conn(file);
1228 if (!fc)
1229 return -EPERM;
1230
1231 fuse_copy_init(&cs, fc, 1, iov, nr_segs);
1232
1233 return fuse_dev_do_read(fc, file, &cs, iov_length(iov, nr_segs));
1234}
1235
1236static int fuse_dev_pipe_buf_steal(struct pipe_inode_info *pipe,
1237 struct pipe_buffer *buf)
1238{
1239 return 1;
1240}
1241
1242static const struct pipe_buf_operations fuse_dev_pipe_buf_ops = {
1243 .can_merge = 0,
1244 .map = generic_pipe_buf_map,
1245 .unmap = generic_pipe_buf_unmap,
1246 .confirm = generic_pipe_buf_confirm,
1247 .release = generic_pipe_buf_release,
1248 .steal = fuse_dev_pipe_buf_steal,
1249 .get = generic_pipe_buf_get,
1250};
1251
1252static ssize_t fuse_dev_splice_read(struct file *in, loff_t *ppos,
1253 struct pipe_inode_info *pipe,
1254 size_t len, unsigned int flags)
1255{
1256 int ret;
1257 int page_nr = 0;
1258 int do_wakeup = 0;
1259 struct pipe_buffer *bufs;
1260 struct fuse_copy_state cs;
1261 struct fuse_conn *fc = fuse_get_conn(in);
1262 if (!fc)
1263 return -EPERM;
1264
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001265 bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), GFP_KERNEL);
Miklos Szeredic3021622010-05-25 15:06:07 +02001266 if (!bufs)
1267 return -ENOMEM;
1268
1269 fuse_copy_init(&cs, fc, 1, NULL, 0);
1270 cs.pipebufs = bufs;
1271 cs.pipe = pipe;
1272 ret = fuse_dev_do_read(fc, in, &cs, len);
1273 if (ret < 0)
1274 goto out;
1275
1276 ret = 0;
1277 pipe_lock(pipe);
1278
1279 if (!pipe->readers) {
1280 send_sig(SIGPIPE, current, 0);
1281 if (!ret)
1282 ret = -EPIPE;
1283 goto out_unlock;
1284 }
1285
1286 if (pipe->nrbufs + cs.nr_segs > pipe->buffers) {
1287 ret = -EIO;
1288 goto out_unlock;
1289 }
1290
1291 while (page_nr < cs.nr_segs) {
1292 int newbuf = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
1293 struct pipe_buffer *buf = pipe->bufs + newbuf;
1294
1295 buf->page = bufs[page_nr].page;
1296 buf->offset = bufs[page_nr].offset;
1297 buf->len = bufs[page_nr].len;
1298 buf->ops = &fuse_dev_pipe_buf_ops;
1299
1300 pipe->nrbufs++;
1301 page_nr++;
1302 ret += buf->len;
1303
1304 if (pipe->inode)
1305 do_wakeup = 1;
1306 }
1307
1308out_unlock:
1309 pipe_unlock(pipe);
1310
1311 if (do_wakeup) {
1312 smp_mb();
1313 if (waitqueue_active(&pipe->wait))
1314 wake_up_interruptible(&pipe->wait);
1315 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
1316 }
1317
1318out:
1319 for (; page_nr < cs.nr_segs; page_nr++)
1320 page_cache_release(bufs[page_nr].page);
1321
1322 kfree(bufs);
1323 return ret;
1324}
1325
Tejun Heo95668a62008-11-26 12:03:55 +01001326static int fuse_notify_poll(struct fuse_conn *fc, unsigned int size,
1327 struct fuse_copy_state *cs)
1328{
1329 struct fuse_notify_poll_wakeup_out outarg;
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001330 int err = -EINVAL;
Tejun Heo95668a62008-11-26 12:03:55 +01001331
1332 if (size != sizeof(outarg))
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001333 goto err;
Tejun Heo95668a62008-11-26 12:03:55 +01001334
1335 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1336 if (err)
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001337 goto err;
Tejun Heo95668a62008-11-26 12:03:55 +01001338
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001339 fuse_copy_finish(cs);
Tejun Heo95668a62008-11-26 12:03:55 +01001340 return fuse_notify_poll_wakeup(fc, &outarg);
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001341
1342err:
1343 fuse_copy_finish(cs);
1344 return err;
Tejun Heo95668a62008-11-26 12:03:55 +01001345}
1346
John Muir3b463ae2009-05-31 11:13:57 -04001347static int fuse_notify_inval_inode(struct fuse_conn *fc, unsigned int size,
1348 struct fuse_copy_state *cs)
1349{
1350 struct fuse_notify_inval_inode_out outarg;
1351 int err = -EINVAL;
1352
1353 if (size != sizeof(outarg))
1354 goto err;
1355
1356 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1357 if (err)
1358 goto err;
1359 fuse_copy_finish(cs);
1360
1361 down_read(&fc->killsb);
1362 err = -ENOENT;
Miklos Szeredib21dda42010-02-05 12:08:31 +01001363 if (fc->sb) {
1364 err = fuse_reverse_inval_inode(fc->sb, outarg.ino,
1365 outarg.off, outarg.len);
1366 }
John Muir3b463ae2009-05-31 11:13:57 -04001367 up_read(&fc->killsb);
1368 return err;
1369
1370err:
1371 fuse_copy_finish(cs);
1372 return err;
1373}
1374
1375static int fuse_notify_inval_entry(struct fuse_conn *fc, unsigned int size,
1376 struct fuse_copy_state *cs)
1377{
1378 struct fuse_notify_inval_entry_out outarg;
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001379 int err = -ENOMEM;
1380 char *buf;
John Muir3b463ae2009-05-31 11:13:57 -04001381 struct qstr name;
1382
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001383 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1384 if (!buf)
1385 goto err;
1386
1387 err = -EINVAL;
John Muir3b463ae2009-05-31 11:13:57 -04001388 if (size < sizeof(outarg))
1389 goto err;
1390
1391 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1392 if (err)
1393 goto err;
1394
1395 err = -ENAMETOOLONG;
1396 if (outarg.namelen > FUSE_NAME_MAX)
1397 goto err;
1398
Miklos Szeredic2183d12011-08-24 10:20:17 +02001399 err = -EINVAL;
1400 if (size != sizeof(outarg) + outarg.namelen + 1)
1401 goto err;
1402
John Muir3b463ae2009-05-31 11:13:57 -04001403 name.name = buf;
1404 name.len = outarg.namelen;
1405 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1406 if (err)
1407 goto err;
1408 fuse_copy_finish(cs);
1409 buf[outarg.namelen] = 0;
1410 name.hash = full_name_hash(name.name, name.len);
1411
1412 down_read(&fc->killsb);
1413 err = -ENOENT;
Miklos Szeredib21dda42010-02-05 12:08:31 +01001414 if (fc->sb)
John Muir451d0f52011-12-06 21:50:06 +01001415 err = fuse_reverse_inval_entry(fc->sb, outarg.parent, 0, &name);
1416 up_read(&fc->killsb);
1417 kfree(buf);
1418 return err;
1419
1420err:
1421 kfree(buf);
1422 fuse_copy_finish(cs);
1423 return err;
1424}
1425
1426static int fuse_notify_delete(struct fuse_conn *fc, unsigned int size,
1427 struct fuse_copy_state *cs)
1428{
1429 struct fuse_notify_delete_out outarg;
1430 int err = -ENOMEM;
1431 char *buf;
1432 struct qstr name;
1433
1434 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1435 if (!buf)
1436 goto err;
1437
1438 err = -EINVAL;
1439 if (size < sizeof(outarg))
1440 goto err;
1441
1442 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1443 if (err)
1444 goto err;
1445
1446 err = -ENAMETOOLONG;
1447 if (outarg.namelen > FUSE_NAME_MAX)
1448 goto err;
1449
1450 err = -EINVAL;
1451 if (size != sizeof(outarg) + outarg.namelen + 1)
1452 goto err;
1453
1454 name.name = buf;
1455 name.len = outarg.namelen;
1456 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1457 if (err)
1458 goto err;
1459 fuse_copy_finish(cs);
1460 buf[outarg.namelen] = 0;
1461 name.hash = full_name_hash(name.name, name.len);
1462
1463 down_read(&fc->killsb);
1464 err = -ENOENT;
1465 if (fc->sb)
1466 err = fuse_reverse_inval_entry(fc->sb, outarg.parent,
1467 outarg.child, &name);
John Muir3b463ae2009-05-31 11:13:57 -04001468 up_read(&fc->killsb);
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001469 kfree(buf);
John Muir3b463ae2009-05-31 11:13:57 -04001470 return err;
1471
1472err:
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001473 kfree(buf);
John Muir3b463ae2009-05-31 11:13:57 -04001474 fuse_copy_finish(cs);
1475 return err;
1476}
1477
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001478static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
1479 struct fuse_copy_state *cs)
1480{
1481 struct fuse_notify_store_out outarg;
1482 struct inode *inode;
1483 struct address_space *mapping;
1484 u64 nodeid;
1485 int err;
1486 pgoff_t index;
1487 unsigned int offset;
1488 unsigned int num;
1489 loff_t file_size;
1490 loff_t end;
1491
1492 err = -EINVAL;
1493 if (size < sizeof(outarg))
1494 goto out_finish;
1495
1496 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1497 if (err)
1498 goto out_finish;
1499
1500 err = -EINVAL;
1501 if (size - sizeof(outarg) != outarg.size)
1502 goto out_finish;
1503
1504 nodeid = outarg.nodeid;
1505
1506 down_read(&fc->killsb);
1507
1508 err = -ENOENT;
1509 if (!fc->sb)
1510 goto out_up_killsb;
1511
1512 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1513 if (!inode)
1514 goto out_up_killsb;
1515
1516 mapping = inode->i_mapping;
1517 index = outarg.offset >> PAGE_CACHE_SHIFT;
1518 offset = outarg.offset & ~PAGE_CACHE_MASK;
1519 file_size = i_size_read(inode);
1520 end = outarg.offset + outarg.size;
1521 if (end > file_size) {
1522 file_size = end;
1523 fuse_write_update_size(inode, file_size);
1524 }
1525
1526 num = outarg.size;
1527 while (num) {
1528 struct page *page;
1529 unsigned int this_num;
1530
1531 err = -ENOMEM;
1532 page = find_or_create_page(mapping, index,
1533 mapping_gfp_mask(mapping));
1534 if (!page)
1535 goto out_iput;
1536
1537 this_num = min_t(unsigned, num, PAGE_CACHE_SIZE - offset);
1538 err = fuse_copy_page(cs, &page, offset, this_num, 0);
1539 if (!err && offset == 0 && (num != 0 || file_size == end))
1540 SetPageUptodate(page);
1541 unlock_page(page);
1542 page_cache_release(page);
1543
1544 if (err)
1545 goto out_iput;
1546
1547 num -= this_num;
1548 offset = 0;
1549 index++;
1550 }
1551
1552 err = 0;
1553
1554out_iput:
1555 iput(inode);
1556out_up_killsb:
1557 up_read(&fc->killsb);
1558out_finish:
1559 fuse_copy_finish(cs);
1560 return err;
1561}
1562
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001563static void fuse_retrieve_end(struct fuse_conn *fc, struct fuse_req *req)
1564{
Miklos Szeredi0be85572010-10-27 15:34:46 -07001565 release_pages(req->pages, req->num_pages, 0);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001566}
1567
1568static int fuse_retrieve(struct fuse_conn *fc, struct inode *inode,
1569 struct fuse_notify_retrieve_out *outarg)
1570{
1571 int err;
1572 struct address_space *mapping = inode->i_mapping;
1573 struct fuse_req *req;
1574 pgoff_t index;
1575 loff_t file_size;
1576 unsigned int num;
1577 unsigned int offset;
Geert Uytterhoeven01574432010-09-30 22:06:21 +02001578 size_t total_len = 0;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001579
1580 req = fuse_get_req(fc);
1581 if (IS_ERR(req))
1582 return PTR_ERR(req);
1583
1584 offset = outarg->offset & ~PAGE_CACHE_MASK;
1585
1586 req->in.h.opcode = FUSE_NOTIFY_REPLY;
1587 req->in.h.nodeid = outarg->nodeid;
1588 req->in.numargs = 2;
1589 req->in.argpages = 1;
1590 req->page_offset = offset;
1591 req->end = fuse_retrieve_end;
1592
1593 index = outarg->offset >> PAGE_CACHE_SHIFT;
1594 file_size = i_size_read(inode);
1595 num = outarg->size;
1596 if (outarg->offset > file_size)
1597 num = 0;
1598 else if (outarg->offset + num > file_size)
1599 num = file_size - outarg->offset;
1600
Miklos Szeredi48706d02011-12-13 10:36:59 +01001601 while (num && req->num_pages < FUSE_MAX_PAGES_PER_REQ) {
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001602 struct page *page;
1603 unsigned int this_num;
1604
1605 page = find_get_page(mapping, index);
1606 if (!page)
1607 break;
1608
1609 this_num = min_t(unsigned, num, PAGE_CACHE_SIZE - offset);
1610 req->pages[req->num_pages] = page;
1611 req->num_pages++;
1612
Miklos Szeredic9e67d42012-09-04 18:45:54 +02001613 offset = 0;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001614 num -= this_num;
1615 total_len += this_num;
Miklos Szeredi48706d02011-12-13 10:36:59 +01001616 index++;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001617 }
1618 req->misc.retrieve_in.offset = outarg->offset;
1619 req->misc.retrieve_in.size = total_len;
1620 req->in.args[0].size = sizeof(req->misc.retrieve_in);
1621 req->in.args[0].value = &req->misc.retrieve_in;
1622 req->in.args[1].size = total_len;
1623
1624 err = fuse_request_send_notify_reply(fc, req, outarg->notify_unique);
1625 if (err)
1626 fuse_retrieve_end(fc, req);
1627
1628 return err;
1629}
1630
1631static int fuse_notify_retrieve(struct fuse_conn *fc, unsigned int size,
1632 struct fuse_copy_state *cs)
1633{
1634 struct fuse_notify_retrieve_out outarg;
1635 struct inode *inode;
1636 int err;
1637
1638 err = -EINVAL;
1639 if (size != sizeof(outarg))
1640 goto copy_finish;
1641
1642 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1643 if (err)
1644 goto copy_finish;
1645
1646 fuse_copy_finish(cs);
1647
1648 down_read(&fc->killsb);
1649 err = -ENOENT;
1650 if (fc->sb) {
1651 u64 nodeid = outarg.nodeid;
1652
1653 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1654 if (inode) {
1655 err = fuse_retrieve(fc, inode, &outarg);
1656 iput(inode);
1657 }
1658 }
1659 up_read(&fc->killsb);
1660
1661 return err;
1662
1663copy_finish:
1664 fuse_copy_finish(cs);
1665 return err;
1666}
1667
Tejun Heo85993962008-11-26 12:03:55 +01001668static int fuse_notify(struct fuse_conn *fc, enum fuse_notify_code code,
1669 unsigned int size, struct fuse_copy_state *cs)
1670{
1671 switch (code) {
Tejun Heo95668a62008-11-26 12:03:55 +01001672 case FUSE_NOTIFY_POLL:
1673 return fuse_notify_poll(fc, size, cs);
1674
John Muir3b463ae2009-05-31 11:13:57 -04001675 case FUSE_NOTIFY_INVAL_INODE:
1676 return fuse_notify_inval_inode(fc, size, cs);
1677
1678 case FUSE_NOTIFY_INVAL_ENTRY:
1679 return fuse_notify_inval_entry(fc, size, cs);
1680
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001681 case FUSE_NOTIFY_STORE:
1682 return fuse_notify_store(fc, size, cs);
1683
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001684 case FUSE_NOTIFY_RETRIEVE:
1685 return fuse_notify_retrieve(fc, size, cs);
1686
John Muir451d0f52011-12-06 21:50:06 +01001687 case FUSE_NOTIFY_DELETE:
1688 return fuse_notify_delete(fc, size, cs);
1689
Tejun Heo85993962008-11-26 12:03:55 +01001690 default:
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001691 fuse_copy_finish(cs);
Tejun Heo85993962008-11-26 12:03:55 +01001692 return -EINVAL;
1693 }
1694}
1695
Miklos Szeredi334f4852005-09-09 13:10:27 -07001696/* Look up request on processing list by unique ID */
1697static struct fuse_req *request_find(struct fuse_conn *fc, u64 unique)
1698{
1699 struct list_head *entry;
1700
1701 list_for_each(entry, &fc->processing) {
1702 struct fuse_req *req;
1703 req = list_entry(entry, struct fuse_req, list);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001704 if (req->in.h.unique == unique || req->intr_unique == unique)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001705 return req;
1706 }
1707 return NULL;
1708}
1709
1710static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
1711 unsigned nbytes)
1712{
1713 unsigned reqsize = sizeof(struct fuse_out_header);
1714
1715 if (out->h.error)
1716 return nbytes != reqsize ? -EINVAL : 0;
1717
1718 reqsize += len_args(out->numargs, out->args);
1719
1720 if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
1721 return -EINVAL;
1722 else if (reqsize > nbytes) {
1723 struct fuse_arg *lastarg = &out->args[out->numargs-1];
1724 unsigned diffsize = reqsize - nbytes;
1725 if (diffsize > lastarg->size)
1726 return -EINVAL;
1727 lastarg->size -= diffsize;
1728 }
1729 return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
1730 out->page_zeroing);
1731}
1732
1733/*
1734 * Write a single reply to a request. First the header is copied from
1735 * the write buffer. The request is then searched on the processing
1736 * list by the unique ID found in the header. If found, then remove
1737 * it from the list and copy the rest of the buffer to the request.
1738 * The request is finished by calling request_end()
1739 */
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001740static ssize_t fuse_dev_do_write(struct fuse_conn *fc,
1741 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001742{
1743 int err;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001744 struct fuse_req *req;
1745 struct fuse_out_header oh;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001746
Miklos Szeredi334f4852005-09-09 13:10:27 -07001747 if (nbytes < sizeof(struct fuse_out_header))
1748 return -EINVAL;
1749
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001750 err = fuse_copy_one(cs, &oh, sizeof(oh));
Miklos Szeredi334f4852005-09-09 13:10:27 -07001751 if (err)
1752 goto err_finish;
Tejun Heo85993962008-11-26 12:03:55 +01001753
Miklos Szeredi334f4852005-09-09 13:10:27 -07001754 err = -EINVAL;
Tejun Heo85993962008-11-26 12:03:55 +01001755 if (oh.len != nbytes)
1756 goto err_finish;
1757
1758 /*
1759 * Zero oh.unique indicates unsolicited notification message
1760 * and error contains notification code.
1761 */
1762 if (!oh.unique) {
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001763 err = fuse_notify(fc, oh.error, nbytes - sizeof(oh), cs);
Tejun Heo85993962008-11-26 12:03:55 +01001764 return err ? err : nbytes;
1765 }
1766
1767 err = -EINVAL;
1768 if (oh.error <= -1000 || oh.error > 0)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001769 goto err_finish;
1770
Miklos Szeredid7133112006-04-10 22:54:55 -07001771 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001772 err = -ENOENT;
1773 if (!fc->connected)
1774 goto err_unlock;
1775
Miklos Szeredi334f4852005-09-09 13:10:27 -07001776 req = request_find(fc, oh.unique);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001777 if (!req)
1778 goto err_unlock;
1779
Miklos Szeredif9a28422006-06-25 05:48:53 -07001780 if (req->aborted) {
Miklos Szeredid7133112006-04-10 22:54:55 -07001781 spin_unlock(&fc->lock);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001782 fuse_copy_finish(cs);
Miklos Szeredid7133112006-04-10 22:54:55 -07001783 spin_lock(&fc->lock);
Miklos Szeredi222f1d62006-01-16 22:14:25 -08001784 request_end(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001785 return -ENOENT;
1786 }
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001787 /* Is it an interrupt reply? */
1788 if (req->intr_unique == oh.unique) {
1789 err = -EINVAL;
1790 if (nbytes != sizeof(struct fuse_out_header))
1791 goto err_unlock;
1792
1793 if (oh.error == -ENOSYS)
1794 fc->no_interrupt = 1;
1795 else if (oh.error == -EAGAIN)
1796 queue_interrupt(fc, req);
1797
1798 spin_unlock(&fc->lock);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001799 fuse_copy_finish(cs);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001800 return nbytes;
1801 }
1802
1803 req->state = FUSE_REQ_WRITING;
Miklos Szeredid77a1d52006-01-16 22:14:31 -08001804 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001805 req->out.h = oh;
1806 req->locked = 1;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001807 cs->req = req;
Miklos Szeredice534fb2010-05-25 15:06:07 +02001808 if (!req->out.page_replace)
1809 cs->move_pages = 0;
Miklos Szeredid7133112006-04-10 22:54:55 -07001810 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001811
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001812 err = copy_out_args(cs, &req->out, nbytes);
1813 fuse_copy_finish(cs);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001814
Miklos Szeredid7133112006-04-10 22:54:55 -07001815 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001816 req->locked = 0;
1817 if (!err) {
Miklos Szeredif9a28422006-06-25 05:48:53 -07001818 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001819 err = -ENOENT;
Miklos Szeredif9a28422006-06-25 05:48:53 -07001820 } else if (!req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001821 req->out.h.error = -EIO;
1822 request_end(fc, req);
1823
1824 return err ? err : nbytes;
1825
1826 err_unlock:
Miklos Szeredid7133112006-04-10 22:54:55 -07001827 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001828 err_finish:
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001829 fuse_copy_finish(cs);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001830 return err;
1831}
1832
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001833static ssize_t fuse_dev_write(struct kiocb *iocb, const struct iovec *iov,
1834 unsigned long nr_segs, loff_t pos)
1835{
1836 struct fuse_copy_state cs;
1837 struct fuse_conn *fc = fuse_get_conn(iocb->ki_filp);
1838 if (!fc)
1839 return -EPERM;
1840
Miklos Szeredic3021622010-05-25 15:06:07 +02001841 fuse_copy_init(&cs, fc, 0, iov, nr_segs);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001842
1843 return fuse_dev_do_write(fc, &cs, iov_length(iov, nr_segs));
1844}
1845
1846static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe,
1847 struct file *out, loff_t *ppos,
1848 size_t len, unsigned int flags)
1849{
1850 unsigned nbuf;
1851 unsigned idx;
1852 struct pipe_buffer *bufs;
1853 struct fuse_copy_state cs;
1854 struct fuse_conn *fc;
1855 size_t rem;
1856 ssize_t ret;
1857
1858 fc = fuse_get_conn(out);
1859 if (!fc)
1860 return -EPERM;
1861
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001862 bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), GFP_KERNEL);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001863 if (!bufs)
1864 return -ENOMEM;
1865
1866 pipe_lock(pipe);
1867 nbuf = 0;
1868 rem = 0;
1869 for (idx = 0; idx < pipe->nrbufs && rem < len; idx++)
1870 rem += pipe->bufs[(pipe->curbuf + idx) & (pipe->buffers - 1)].len;
1871
1872 ret = -EINVAL;
1873 if (rem < len) {
1874 pipe_unlock(pipe);
1875 goto out;
1876 }
1877
1878 rem = len;
1879 while (rem) {
1880 struct pipe_buffer *ibuf;
1881 struct pipe_buffer *obuf;
1882
1883 BUG_ON(nbuf >= pipe->buffers);
1884 BUG_ON(!pipe->nrbufs);
1885 ibuf = &pipe->bufs[pipe->curbuf];
1886 obuf = &bufs[nbuf];
1887
1888 if (rem >= ibuf->len) {
1889 *obuf = *ibuf;
1890 ibuf->ops = NULL;
1891 pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
1892 pipe->nrbufs--;
1893 } else {
1894 ibuf->ops->get(pipe, ibuf);
1895 *obuf = *ibuf;
1896 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1897 obuf->len = rem;
1898 ibuf->offset += obuf->len;
1899 ibuf->len -= obuf->len;
1900 }
1901 nbuf++;
1902 rem -= obuf->len;
1903 }
1904 pipe_unlock(pipe);
1905
Miklos Szeredic3021622010-05-25 15:06:07 +02001906 fuse_copy_init(&cs, fc, 0, NULL, nbuf);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001907 cs.pipebufs = bufs;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001908 cs.pipe = pipe;
1909
Miklos Szeredice534fb2010-05-25 15:06:07 +02001910 if (flags & SPLICE_F_MOVE)
1911 cs.move_pages = 1;
1912
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001913 ret = fuse_dev_do_write(fc, &cs, len);
1914
1915 for (idx = 0; idx < nbuf; idx++) {
1916 struct pipe_buffer *buf = &bufs[idx];
1917 buf->ops->release(pipe, buf);
1918 }
1919out:
1920 kfree(bufs);
1921 return ret;
1922}
1923
Miklos Szeredi334f4852005-09-09 13:10:27 -07001924static unsigned fuse_dev_poll(struct file *file, poll_table *wait)
1925{
Miklos Szeredi334f4852005-09-09 13:10:27 -07001926 unsigned mask = POLLOUT | POLLWRNORM;
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07001927 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001928 if (!fc)
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07001929 return POLLERR;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001930
1931 poll_wait(file, &fc->waitq, wait);
1932
Miklos Szeredid7133112006-04-10 22:54:55 -07001933 spin_lock(&fc->lock);
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07001934 if (!fc->connected)
1935 mask = POLLERR;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001936 else if (request_pending(fc))
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07001937 mask |= POLLIN | POLLRDNORM;
Miklos Szeredid7133112006-04-10 22:54:55 -07001938 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001939
1940 return mask;
1941}
1942
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001943/*
1944 * Abort all requests on the given list (pending or processing)
1945 *
Miklos Szeredid7133112006-04-10 22:54:55 -07001946 * This function releases and reacquires fc->lock
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001947 */
Miklos Szeredi334f4852005-09-09 13:10:27 -07001948static void end_requests(struct fuse_conn *fc, struct list_head *head)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001949__releases(fc->lock)
1950__acquires(fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001951{
1952 while (!list_empty(head)) {
1953 struct fuse_req *req;
1954 req = list_entry(head->next, struct fuse_req, list);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001955 req->out.h.error = -ECONNABORTED;
1956 request_end(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -07001957 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001958 }
1959}
1960
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001961/*
1962 * Abort requests under I/O
1963 *
Miklos Szeredif9a28422006-06-25 05:48:53 -07001964 * The requests are set to aborted and finished, and the request
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001965 * waiter is woken up. This will make request_wait_answer() wait
1966 * until the request is unlocked and then return.
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08001967 *
1968 * If the request is asynchronous, then the end function needs to be
1969 * called after waiting for the request to be unlocked (if it was
1970 * locked).
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001971 */
1972static void end_io_requests(struct fuse_conn *fc)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001973__releases(fc->lock)
1974__acquires(fc->lock)
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001975{
1976 while (!list_empty(&fc->io)) {
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08001977 struct fuse_req *req =
1978 list_entry(fc->io.next, struct fuse_req, list);
1979 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
1980
Miklos Szeredif9a28422006-06-25 05:48:53 -07001981 req->aborted = 1;
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001982 req->out.h.error = -ECONNABORTED;
1983 req->state = FUSE_REQ_FINISHED;
1984 list_del_init(&req->list);
1985 wake_up(&req->waitq);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08001986 if (end) {
1987 req->end = NULL;
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08001988 __fuse_get_request(req);
Miklos Szeredid7133112006-04-10 22:54:55 -07001989 spin_unlock(&fc->lock);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08001990 wait_event(req->waitq, !req->locked);
1991 end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +01001992 fuse_put_request(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -07001993 spin_lock(&fc->lock);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08001994 }
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001995 }
1996}
1997
Miklos Szeredi595afaf2010-09-07 13:42:41 +02001998static void end_queued_requests(struct fuse_conn *fc)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001999__releases(fc->lock)
2000__acquires(fc->lock)
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002001{
2002 fc->max_background = UINT_MAX;
2003 flush_bg_queue(fc);
2004 end_requests(fc, &fc->pending);
2005 end_requests(fc, &fc->processing);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01002006 while (forget_pending(fc))
Miklos Szeredi02c048b2010-12-07 20:16:56 +01002007 kfree(dequeue_forget(fc, 1, NULL));
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002008}
2009
Bryan Green357ccf22011-03-01 16:43:52 -08002010static void end_polls(struct fuse_conn *fc)
2011{
2012 struct rb_node *p;
2013
2014 p = rb_first(&fc->polled_files);
2015
2016 while (p) {
2017 struct fuse_file *ff;
2018 ff = rb_entry(p, struct fuse_file, polled_node);
2019 wake_up_interruptible_all(&ff->poll_wait);
2020
2021 p = rb_next(p);
2022 }
2023}
2024
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002025/*
2026 * Abort all requests.
2027 *
2028 * Emergency exit in case of a malicious or accidental deadlock, or
2029 * just a hung filesystem.
2030 *
2031 * The same effect is usually achievable through killing the
2032 * filesystem daemon and all users of the filesystem. The exception
2033 * is the combination of an asynchronous request and the tricky
2034 * deadlock (see Documentation/filesystems/fuse.txt).
2035 *
2036 * During the aborting, progression of requests from the pending and
2037 * processing lists onto the io list, and progression of new requests
2038 * onto the pending list is prevented by req->connected being false.
2039 *
2040 * Progression of requests under I/O to the processing list is
Miklos Szeredif9a28422006-06-25 05:48:53 -07002041 * prevented by the req->aborted flag being true for these requests.
2042 * For this reason requests on the io list must be aborted first.
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002043 */
2044void fuse_abort_conn(struct fuse_conn *fc)
2045{
Miklos Szeredid7133112006-04-10 22:54:55 -07002046 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002047 if (fc->connected) {
2048 fc->connected = 0;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -07002049 fc->blocked = 0;
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002050 end_io_requests(fc);
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002051 end_queued_requests(fc);
Bryan Green357ccf22011-03-01 16:43:52 -08002052 end_polls(fc);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002053 wake_up_all(&fc->waitq);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -07002054 wake_up_all(&fc->blocked_waitq);
Jeff Dike385a17b2006-04-10 22:54:52 -07002055 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002056 }
Miklos Szeredid7133112006-04-10 22:54:55 -07002057 spin_unlock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002058}
Tejun Heo08cbf542009-04-14 10:54:53 +09002059EXPORT_SYMBOL_GPL(fuse_abort_conn);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002060
Tejun Heo08cbf542009-04-14 10:54:53 +09002061int fuse_dev_release(struct inode *inode, struct file *file)
Miklos Szeredi334f4852005-09-09 13:10:27 -07002062{
Miklos Szeredi0720b312006-04-10 22:54:55 -07002063 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002064 if (fc) {
Miklos Szeredid7133112006-04-10 22:54:55 -07002065 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -07002066 fc->connected = 0;
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002067 fc->blocked = 0;
2068 end_queued_requests(fc);
Bryan Green357ccf22011-03-01 16:43:52 -08002069 end_polls(fc);
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002070 wake_up_all(&fc->blocked_waitq);
Miklos Szeredid7133112006-04-10 22:54:55 -07002071 spin_unlock(&fc->lock);
Miklos Szeredibafa9652006-06-25 05:48:51 -07002072 fuse_conn_put(fc);
Jeff Dike385a17b2006-04-10 22:54:52 -07002073 }
Miklos Szeredif543f252006-01-16 22:14:35 -08002074
Miklos Szeredi334f4852005-09-09 13:10:27 -07002075 return 0;
2076}
Tejun Heo08cbf542009-04-14 10:54:53 +09002077EXPORT_SYMBOL_GPL(fuse_dev_release);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002078
Jeff Dike385a17b2006-04-10 22:54:52 -07002079static int fuse_dev_fasync(int fd, struct file *file, int on)
2080{
2081 struct fuse_conn *fc = fuse_get_conn(file);
2082 if (!fc)
Miklos Szeredia87046d2006-04-10 22:54:56 -07002083 return -EPERM;
Jeff Dike385a17b2006-04-10 22:54:52 -07002084
2085 /* No locking - fasync_helper does its own locking */
2086 return fasync_helper(fd, file, on, &fc->fasync);
2087}
2088
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002089const struct file_operations fuse_dev_operations = {
Miklos Szeredi334f4852005-09-09 13:10:27 -07002090 .owner = THIS_MODULE,
2091 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002092 .read = do_sync_read,
2093 .aio_read = fuse_dev_read,
Miklos Szeredic3021622010-05-25 15:06:07 +02002094 .splice_read = fuse_dev_splice_read,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002095 .write = do_sync_write,
2096 .aio_write = fuse_dev_write,
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002097 .splice_write = fuse_dev_splice_write,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002098 .poll = fuse_dev_poll,
2099 .release = fuse_dev_release,
Jeff Dike385a17b2006-04-10 22:54:52 -07002100 .fasync = fuse_dev_fasync,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002101};
Tejun Heo08cbf542009-04-14 10:54:53 +09002102EXPORT_SYMBOL_GPL(fuse_dev_operations);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002103
2104static struct miscdevice fuse_miscdevice = {
2105 .minor = FUSE_MINOR,
2106 .name = "fuse",
2107 .fops = &fuse_dev_operations,
2108};
2109
2110int __init fuse_dev_init(void)
2111{
2112 int err = -ENOMEM;
2113 fuse_req_cachep = kmem_cache_create("fuse_request",
2114 sizeof(struct fuse_req),
Paul Mundt20c2df82007-07-20 10:11:58 +09002115 0, 0, NULL);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002116 if (!fuse_req_cachep)
2117 goto out;
2118
2119 err = misc_register(&fuse_miscdevice);
2120 if (err)
2121 goto out_cache_clean;
2122
2123 return 0;
2124
2125 out_cache_clean:
2126 kmem_cache_destroy(fuse_req_cachep);
2127 out:
2128 return err;
2129}
2130
2131void fuse_dev_cleanup(void)
2132{
2133 misc_deregister(&fuse_miscdevice);
2134 kmem_cache_destroy(fuse_req_cachep);
2135}