blob: d692b85115bd57590867b3a39924f171fd9d983d [file] [log] [blame]
Miklos Szeredi334f4852005-09-09 13:10:27 -07001/*
2 FUSE: Filesystem in Userspace
Miklos Szeredi1729a162008-11-26 12:03:54 +01003 Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu>
Miklos Szeredi334f4852005-09-09 13:10:27 -07004
5 This program can be distributed under the terms of the GNU GPL.
6 See the file COPYING.
7*/
8
9#include "fuse_i.h"
10
11#include <linux/init.h>
12#include <linux/module.h>
13#include <linux/poll.h>
14#include <linux/uio.h>
15#include <linux/miscdevice.h>
16#include <linux/pagemap.h>
17#include <linux/file.h>
18#include <linux/slab.h>
Miklos Szeredidd3bb142010-05-25 15:06:06 +020019#include <linux/pipe_fs_i.h>
Miklos Szeredice534fb2010-05-25 15:06:07 +020020#include <linux/swap.h>
21#include <linux/splice.h>
Miklos Szeredi334f4852005-09-09 13:10:27 -070022
23MODULE_ALIAS_MISCDEV(FUSE_MINOR);
Kay Sievers578454f2010-05-20 18:07:20 +020024MODULE_ALIAS("devname:fuse");
Miklos Szeredi334f4852005-09-09 13:10:27 -070025
Christoph Lametere18b8902006-12-06 20:33:20 -080026static struct kmem_cache *fuse_req_cachep;
Miklos Szeredi334f4852005-09-09 13:10:27 -070027
Miklos Szeredi8bfc0162006-01-16 22:14:28 -080028static struct fuse_conn *fuse_get_conn(struct file *file)
Miklos Szeredi334f4852005-09-09 13:10:27 -070029{
Miklos Szeredi0720b312006-04-10 22:54:55 -070030 /*
31 * Lockless access is OK, because file->private data is set
32 * once during mount and is valid until the file is released.
33 */
34 return file->private_data;
Miklos Szeredi334f4852005-09-09 13:10:27 -070035}
36
Maxim Patlasov4250c062012-10-26 19:48:07 +040037static void fuse_request_init(struct fuse_req *req, struct page **pages,
Maxim Patlasovb2430d72012-10-26 19:49:24 +040038 struct fuse_page_desc *page_descs,
Maxim Patlasov4250c062012-10-26 19:48:07 +040039 unsigned npages)
Miklos Szeredi334f4852005-09-09 13:10:27 -070040{
41 memset(req, 0, sizeof(*req));
Maxim Patlasov4250c062012-10-26 19:48:07 +040042 memset(pages, 0, sizeof(*pages) * npages);
Maxim Patlasovb2430d72012-10-26 19:49:24 +040043 memset(page_descs, 0, sizeof(*page_descs) * npages);
Miklos Szeredi334f4852005-09-09 13:10:27 -070044 INIT_LIST_HEAD(&req->list);
Miklos Szeredia4d27e72006-06-25 05:48:54 -070045 INIT_LIST_HEAD(&req->intr_entry);
Miklos Szeredi334f4852005-09-09 13:10:27 -070046 init_waitqueue_head(&req->waitq);
47 atomic_set(&req->count, 1);
Maxim Patlasov4250c062012-10-26 19:48:07 +040048 req->pages = pages;
Maxim Patlasovb2430d72012-10-26 19:49:24 +040049 req->page_descs = page_descs;
Maxim Patlasov4250c062012-10-26 19:48:07 +040050 req->max_pages = npages;
Miklos Szeredi334f4852005-09-09 13:10:27 -070051}
52
Maxim Patlasov4250c062012-10-26 19:48:07 +040053static struct fuse_req *__fuse_request_alloc(unsigned npages, gfp_t flags)
Miklos Szeredi334f4852005-09-09 13:10:27 -070054{
Maxim Patlasov4250c062012-10-26 19:48:07 +040055 struct fuse_req *req = kmem_cache_alloc(fuse_req_cachep, flags);
56 if (req) {
57 struct page **pages;
Maxim Patlasovb2430d72012-10-26 19:49:24 +040058 struct fuse_page_desc *page_descs;
Maxim Patlasov4250c062012-10-26 19:48:07 +040059
Maxim Patlasovb2430d72012-10-26 19:49:24 +040060 if (npages <= FUSE_REQ_INLINE_PAGES) {
Maxim Patlasov4250c062012-10-26 19:48:07 +040061 pages = req->inline_pages;
Maxim Patlasovb2430d72012-10-26 19:49:24 +040062 page_descs = req->inline_page_descs;
63 } else {
Maxim Patlasov4250c062012-10-26 19:48:07 +040064 pages = kmalloc(sizeof(struct page *) * npages, flags);
Maxim Patlasovb2430d72012-10-26 19:49:24 +040065 page_descs = kmalloc(sizeof(struct fuse_page_desc) *
66 npages, flags);
67 }
Maxim Patlasov4250c062012-10-26 19:48:07 +040068
Maxim Patlasovb2430d72012-10-26 19:49:24 +040069 if (!pages || !page_descs) {
70 kfree(pages);
71 kfree(page_descs);
Maxim Patlasov4250c062012-10-26 19:48:07 +040072 kmem_cache_free(fuse_req_cachep, req);
73 return NULL;
74 }
75
Maxim Patlasovb2430d72012-10-26 19:49:24 +040076 fuse_request_init(req, pages, page_descs, npages);
Maxim Patlasov4250c062012-10-26 19:48:07 +040077 }
Miklos Szeredi334f4852005-09-09 13:10:27 -070078 return req;
79}
Maxim Patlasov4250c062012-10-26 19:48:07 +040080
81struct fuse_req *fuse_request_alloc(unsigned npages)
82{
83 return __fuse_request_alloc(npages, GFP_KERNEL);
84}
Tejun Heo08cbf542009-04-14 10:54:53 +090085EXPORT_SYMBOL_GPL(fuse_request_alloc);
Miklos Szeredi334f4852005-09-09 13:10:27 -070086
Maxim Patlasov4250c062012-10-26 19:48:07 +040087struct fuse_req *fuse_request_alloc_nofs(unsigned npages)
Miklos Szeredi3be5a522008-04-30 00:54:41 -070088{
Maxim Patlasov4250c062012-10-26 19:48:07 +040089 return __fuse_request_alloc(npages, GFP_NOFS);
Miklos Szeredi3be5a522008-04-30 00:54:41 -070090}
91
Miklos Szeredi334f4852005-09-09 13:10:27 -070092void fuse_request_free(struct fuse_req *req)
93{
Maxim Patlasovb2430d72012-10-26 19:49:24 +040094 if (req->pages != req->inline_pages) {
Maxim Patlasov4250c062012-10-26 19:48:07 +040095 kfree(req->pages);
Maxim Patlasovb2430d72012-10-26 19:49:24 +040096 kfree(req->page_descs);
97 }
Miklos Szeredi334f4852005-09-09 13:10:27 -070098 kmem_cache_free(fuse_req_cachep, req);
99}
100
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800101static void block_sigs(sigset_t *oldset)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700102{
103 sigset_t mask;
104
105 siginitsetinv(&mask, sigmask(SIGKILL));
106 sigprocmask(SIG_BLOCK, &mask, oldset);
107}
108
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800109static void restore_sigs(sigset_t *oldset)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700110{
111 sigprocmask(SIG_SETMASK, oldset, NULL);
112}
113
Miklos Szeredi334f4852005-09-09 13:10:27 -0700114static void __fuse_get_request(struct fuse_req *req)
115{
116 atomic_inc(&req->count);
117}
118
119/* Must be called with > 1 refcount */
120static void __fuse_put_request(struct fuse_req *req)
121{
122 BUG_ON(atomic_read(&req->count) < 2);
123 atomic_dec(&req->count);
124}
125
Miklos Szeredi33649c92006-06-25 05:48:52 -0700126static void fuse_req_init_context(struct fuse_req *req)
127{
Eric W. Biederman499dcf22012-02-07 16:26:03 -0800128 req->in.h.uid = from_kuid_munged(&init_user_ns, current_fsuid());
129 req->in.h.gid = from_kgid_munged(&init_user_ns, current_fsgid());
Miklos Szeredi33649c92006-06-25 05:48:52 -0700130 req->in.h.pid = current->pid;
131}
132
Maxim Patlasov0aada882013-03-21 18:02:28 +0400133static bool fuse_block_alloc(struct fuse_conn *fc, bool for_background)
134{
135 return !fc->initialized || (for_background && fc->blocked);
136}
137
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400138static struct fuse_req *__fuse_get_req(struct fuse_conn *fc, unsigned npages,
139 bool for_background)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700140{
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700141 struct fuse_req *req;
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700142 int err;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200143 atomic_inc(&fc->num_waiting);
Maxim Patlasov0aada882013-03-21 18:02:28 +0400144
145 if (fuse_block_alloc(fc, for_background)) {
146 sigset_t oldset;
147 int intr;
148
149 block_sigs(&oldset);
150 intr = wait_event_interruptible(fc->blocked_waitq,
151 !fuse_block_alloc(fc, for_background));
152 restore_sigs(&oldset);
153 err = -EINTR;
154 if (intr)
155 goto out;
156 }
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700157
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700158 err = -ENOTCONN;
159 if (!fc->connected)
160 goto out;
161
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400162 req = fuse_request_alloc(npages);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200163 err = -ENOMEM;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700164 if (!req)
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200165 goto out;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700166
Miklos Szeredi33649c92006-06-25 05:48:52 -0700167 fuse_req_init_context(req);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200168 req->waiting = 1;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400169 req->background = for_background;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700170 return req;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200171
172 out:
173 atomic_dec(&fc->num_waiting);
174 return ERR_PTR(err);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700175}
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400176
177struct fuse_req *fuse_get_req(struct fuse_conn *fc, unsigned npages)
178{
179 return __fuse_get_req(fc, npages, false);
180}
Tejun Heo08cbf542009-04-14 10:54:53 +0900181EXPORT_SYMBOL_GPL(fuse_get_req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700182
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400183struct fuse_req *fuse_get_req_for_background(struct fuse_conn *fc,
184 unsigned npages)
185{
186 return __fuse_get_req(fc, npages, true);
187}
188EXPORT_SYMBOL_GPL(fuse_get_req_for_background);
189
Miklos Szeredi33649c92006-06-25 05:48:52 -0700190/*
191 * Return request in fuse_file->reserved_req. However that may
192 * currently be in use. If that is the case, wait for it to become
193 * available.
194 */
195static struct fuse_req *get_reserved_req(struct fuse_conn *fc,
196 struct file *file)
197{
198 struct fuse_req *req = NULL;
199 struct fuse_file *ff = file->private_data;
200
201 do {
Miklos Szeredide5e3de2007-10-16 23:31:00 -0700202 wait_event(fc->reserved_req_waitq, ff->reserved_req);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700203 spin_lock(&fc->lock);
204 if (ff->reserved_req) {
205 req = ff->reserved_req;
206 ff->reserved_req = NULL;
Al Virocb0942b2012-08-27 14:48:26 -0400207 req->stolen_file = get_file(file);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700208 }
209 spin_unlock(&fc->lock);
210 } while (!req);
211
212 return req;
213}
214
215/*
216 * Put stolen request back into fuse_file->reserved_req
217 */
218static void put_reserved_req(struct fuse_conn *fc, struct fuse_req *req)
219{
220 struct file *file = req->stolen_file;
221 struct fuse_file *ff = file->private_data;
222
223 spin_lock(&fc->lock);
Maxim Patlasovb2430d72012-10-26 19:49:24 +0400224 fuse_request_init(req, req->pages, req->page_descs, req->max_pages);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700225 BUG_ON(ff->reserved_req);
226 ff->reserved_req = req;
Miklos Szeredide5e3de2007-10-16 23:31:00 -0700227 wake_up_all(&fc->reserved_req_waitq);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700228 spin_unlock(&fc->lock);
229 fput(file);
230}
231
232/*
233 * Gets a requests for a file operation, always succeeds
234 *
235 * This is used for sending the FLUSH request, which must get to
236 * userspace, due to POSIX locks which may need to be unlocked.
237 *
238 * If allocation fails due to OOM, use the reserved request in
239 * fuse_file.
240 *
241 * This is very unlikely to deadlock accidentally, since the
242 * filesystem should not have it's own file open. If deadlock is
243 * intentional, it can still be broken by "aborting" the filesystem.
244 */
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400245struct fuse_req *fuse_get_req_nofail_nopages(struct fuse_conn *fc,
246 struct file *file)
Miklos Szeredi33649c92006-06-25 05:48:52 -0700247{
248 struct fuse_req *req;
249
250 atomic_inc(&fc->num_waiting);
Maxim Patlasov0aada882013-03-21 18:02:28 +0400251 wait_event(fc->blocked_waitq, fc->initialized);
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400252 req = fuse_request_alloc(0);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700253 if (!req)
254 req = get_reserved_req(fc, file);
255
256 fuse_req_init_context(req);
257 req->waiting = 1;
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400258 req->background = 0;
Miklos Szeredi33649c92006-06-25 05:48:52 -0700259 return req;
260}
261
Miklos Szeredi334f4852005-09-09 13:10:27 -0700262void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req)
263{
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800264 if (atomic_dec_and_test(&req->count)) {
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200265 if (req->waiting)
266 atomic_dec(&fc->num_waiting);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700267
268 if (req->stolen_file)
269 put_reserved_req(fc, req);
270 else
271 fuse_request_free(req);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800272 }
273}
Tejun Heo08cbf542009-04-14 10:54:53 +0900274EXPORT_SYMBOL_GPL(fuse_put_request);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800275
Miklos Szeredid12def12008-02-06 01:38:39 -0800276static unsigned len_args(unsigned numargs, struct fuse_arg *args)
277{
278 unsigned nbytes = 0;
279 unsigned i;
280
281 for (i = 0; i < numargs; i++)
282 nbytes += args[i].size;
283
284 return nbytes;
285}
286
287static u64 fuse_get_unique(struct fuse_conn *fc)
288{
289 fc->reqctr++;
290 /* zero is special */
291 if (fc->reqctr == 0)
292 fc->reqctr = 1;
293
294 return fc->reqctr;
295}
296
297static void queue_request(struct fuse_conn *fc, struct fuse_req *req)
298{
Miklos Szeredid12def12008-02-06 01:38:39 -0800299 req->in.h.len = sizeof(struct fuse_in_header) +
300 len_args(req->in.numargs, (struct fuse_arg *) req->in.args);
301 list_add_tail(&req->list, &fc->pending);
302 req->state = FUSE_REQ_PENDING;
303 if (!req->waiting) {
304 req->waiting = 1;
305 atomic_inc(&fc->num_waiting);
306 }
307 wake_up(&fc->waitq);
308 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
309}
310
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100311void fuse_queue_forget(struct fuse_conn *fc, struct fuse_forget_link *forget,
312 u64 nodeid, u64 nlookup)
313{
Miklos Szeredi02c048b2010-12-07 20:16:56 +0100314 forget->forget_one.nodeid = nodeid;
315 forget->forget_one.nlookup = nlookup;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100316
317 spin_lock(&fc->lock);
Miklos Szeredi5dfcc872011-09-12 09:38:03 +0200318 if (fc->connected) {
319 fc->forget_list_tail->next = forget;
320 fc->forget_list_tail = forget;
321 wake_up(&fc->waitq);
322 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
323 } else {
324 kfree(forget);
325 }
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100326 spin_unlock(&fc->lock);
327}
328
Miklos Szeredid12def12008-02-06 01:38:39 -0800329static void flush_bg_queue(struct fuse_conn *fc)
330{
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700331 while (fc->active_background < fc->max_background &&
Miklos Szeredid12def12008-02-06 01:38:39 -0800332 !list_empty(&fc->bg_queue)) {
333 struct fuse_req *req;
334
335 req = list_entry(fc->bg_queue.next, struct fuse_req, list);
336 list_del(&req->list);
337 fc->active_background++;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200338 req->in.h.unique = fuse_get_unique(fc);
Miklos Szeredid12def12008-02-06 01:38:39 -0800339 queue_request(fc, req);
340 }
341}
342
Miklos Szeredi6dbbcb12006-04-26 10:49:06 +0200343/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700344 * This function is called when a request is finished. Either a reply
Miklos Szeredif9a28422006-06-25 05:48:53 -0700345 * has arrived or it was aborted (and not yet sent) or some error
Miklos Szeredif43b1552006-01-16 22:14:26 -0800346 * occurred during communication with userspace, or the device file
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700347 * was closed. The requester thread is woken up (if still waiting),
348 * the 'end' callback is called if given, else the reference to the
349 * request is released
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800350 *
Miklos Szeredid7133112006-04-10 22:54:55 -0700351 * Called with fc->lock, unlocks it
Miklos Szeredi334f4852005-09-09 13:10:27 -0700352 */
353static void request_end(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +0200354__releases(fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700355{
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700356 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
357 req->end = NULL;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800358 list_del(&req->list);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700359 list_del(&req->intr_entry);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800360 req->state = FUSE_REQ_FINISHED;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700361 if (req->background) {
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700362 if (fc->num_background == fc->max_background) {
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700363 fc->blocked = 0;
364 wake_up_all(&fc->blocked_waitq);
365 }
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700366 if (fc->num_background == fc->congestion_threshold &&
Tejun Heoa325f9b2009-04-14 10:54:52 +0900367 fc->connected && fc->bdi_initialized) {
Jens Axboe8aa7e842009-07-09 14:52:32 +0200368 clear_bdi_congested(&fc->bdi, BLK_RW_SYNC);
369 clear_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
Miklos Szeredif92b99b2007-10-16 23:30:59 -0700370 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700371 fc->num_background--;
Miklos Szeredid12def12008-02-06 01:38:39 -0800372 fc->active_background--;
373 flush_bg_queue(fc);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700374 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700375 spin_unlock(&fc->lock);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700376 wake_up(&req->waitq);
377 if (end)
378 end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +0100379 fuse_put_request(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700380}
381
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700382static void wait_answer_interruptible(struct fuse_conn *fc,
383 struct fuse_req *req)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +0200384__releases(fc->lock)
385__acquires(fc->lock)
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700386{
387 if (signal_pending(current))
388 return;
389
390 spin_unlock(&fc->lock);
391 wait_event_interruptible(req->waitq, req->state == FUSE_REQ_FINISHED);
392 spin_lock(&fc->lock);
393}
394
395static void queue_interrupt(struct fuse_conn *fc, struct fuse_req *req)
396{
397 list_add_tail(&req->intr_entry, &fc->interrupts);
398 wake_up(&fc->waitq);
399 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
400}
401
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700402static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +0200403__releases(fc->lock)
404__acquires(fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700405{
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700406 if (!fc->no_interrupt) {
407 /* Any signal may interrupt this */
408 wait_answer_interruptible(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700409
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700410 if (req->aborted)
411 goto aborted;
412 if (req->state == FUSE_REQ_FINISHED)
413 return;
414
415 req->interrupted = 1;
416 if (req->state == FUSE_REQ_SENT)
417 queue_interrupt(fc, req);
418 }
419
Miklos Szeredia131de02007-10-16 23:31:04 -0700420 if (!req->force) {
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700421 sigset_t oldset;
422
423 /* Only fatal signals may interrupt this */
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700424 block_sigs(&oldset);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700425 wait_answer_interruptible(fc, req);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700426 restore_sigs(&oldset);
Miklos Szeredia131de02007-10-16 23:31:04 -0700427
428 if (req->aborted)
429 goto aborted;
430 if (req->state == FUSE_REQ_FINISHED)
431 return;
432
433 /* Request is not yet in userspace, bail out */
434 if (req->state == FUSE_REQ_PENDING) {
435 list_del(&req->list);
436 __fuse_put_request(req);
437 req->out.h.error = -EINTR;
438 return;
439 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700440 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700441
Miklos Szeredia131de02007-10-16 23:31:04 -0700442 /*
443 * Either request is already in userspace, or it was forced.
444 * Wait it out.
445 */
446 spin_unlock(&fc->lock);
447 wait_event(req->waitq, req->state == FUSE_REQ_FINISHED);
448 spin_lock(&fc->lock);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700449
Miklos Szeredia131de02007-10-16 23:31:04 -0700450 if (!req->aborted)
451 return;
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700452
453 aborted:
Miklos Szeredia131de02007-10-16 23:31:04 -0700454 BUG_ON(req->state != FUSE_REQ_FINISHED);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700455 if (req->locked) {
456 /* This is uninterruptible sleep, because data is
457 being copied to/from the buffers of req. During
458 locked state, there mustn't be any filesystem
459 operation (e.g. page fault), since that could lead
460 to deadlock */
Miklos Szeredid7133112006-04-10 22:54:55 -0700461 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700462 wait_event(req->waitq, !req->locked);
Miklos Szeredid7133112006-04-10 22:54:55 -0700463 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700464 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700465}
466
Eric Wong6a4e9222013-02-04 13:04:44 +0000467static void __fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700468{
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400469 BUG_ON(req->background);
Miklos Szeredid7133112006-04-10 22:54:55 -0700470 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700471 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700472 req->out.h.error = -ENOTCONN;
473 else if (fc->conn_error)
474 req->out.h.error = -ECONNREFUSED;
475 else {
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200476 req->in.h.unique = fuse_get_unique(fc);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700477 queue_request(fc, req);
478 /* acquire extra reference, since request is still needed
479 after request_end() */
480 __fuse_get_request(req);
481
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700482 request_wait_answer(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700483 }
Miklos Szeredid7133112006-04-10 22:54:55 -0700484 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700485}
Eric Wong6a4e9222013-02-04 13:04:44 +0000486
487void fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
488{
489 req->isreply = 1;
490 __fuse_request_send(fc, req);
491}
Tejun Heo08cbf542009-04-14 10:54:53 +0900492EXPORT_SYMBOL_GPL(fuse_request_send);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700493
Tejun Heob93f8582008-11-26 12:03:55 +0100494static void fuse_request_send_nowait_locked(struct fuse_conn *fc,
495 struct fuse_req *req)
Miklos Szeredid12def12008-02-06 01:38:39 -0800496{
Maxim Patlasov8b41e672013-03-21 18:02:04 +0400497 BUG_ON(!req->background);
Miklos Szeredid12def12008-02-06 01:38:39 -0800498 fc->num_background++;
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700499 if (fc->num_background == fc->max_background)
Miklos Szeredid12def12008-02-06 01:38:39 -0800500 fc->blocked = 1;
Csaba Henk7a6d3c82009-07-01 17:28:41 -0700501 if (fc->num_background == fc->congestion_threshold &&
Tejun Heoa325f9b2009-04-14 10:54:52 +0900502 fc->bdi_initialized) {
Jens Axboe8aa7e842009-07-09 14:52:32 +0200503 set_bdi_congested(&fc->bdi, BLK_RW_SYNC);
504 set_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
Miklos Szeredid12def12008-02-06 01:38:39 -0800505 }
506 list_add_tail(&req->list, &fc->bg_queue);
507 flush_bg_queue(fc);
508}
509
Tejun Heob93f8582008-11-26 12:03:55 +0100510static void fuse_request_send_nowait(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700511{
Miklos Szeredid7133112006-04-10 22:54:55 -0700512 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700513 if (fc->connected) {
Tejun Heob93f8582008-11-26 12:03:55 +0100514 fuse_request_send_nowait_locked(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700515 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700516 } else {
517 req->out.h.error = -ENOTCONN;
518 request_end(fc, req);
519 }
520}
521
Tejun Heob93f8582008-11-26 12:03:55 +0100522void fuse_request_send_background(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700523{
524 req->isreply = 1;
Tejun Heob93f8582008-11-26 12:03:55 +0100525 fuse_request_send_nowait(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700526}
Tejun Heo08cbf542009-04-14 10:54:53 +0900527EXPORT_SYMBOL_GPL(fuse_request_send_background);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700528
Miklos Szeredi2d45ba32010-07-12 14:41:40 +0200529static int fuse_request_send_notify_reply(struct fuse_conn *fc,
530 struct fuse_req *req, u64 unique)
531{
532 int err = -ENODEV;
533
534 req->isreply = 0;
535 req->in.h.unique = unique;
536 spin_lock(&fc->lock);
537 if (fc->connected) {
538 queue_request(fc, req);
539 err = 0;
540 }
541 spin_unlock(&fc->lock);
542
543 return err;
544}
545
Miklos Szeredi334f4852005-09-09 13:10:27 -0700546/*
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700547 * Called under fc->lock
548 *
549 * fc->connected must have been checked previously
550 */
Tejun Heob93f8582008-11-26 12:03:55 +0100551void fuse_request_send_background_locked(struct fuse_conn *fc,
552 struct fuse_req *req)
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700553{
554 req->isreply = 1;
Tejun Heob93f8582008-11-26 12:03:55 +0100555 fuse_request_send_nowait_locked(fc, req);
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700556}
557
Anand V. Avati0b05b182012-08-19 08:53:23 -0400558void fuse_force_forget(struct file *file, u64 nodeid)
559{
Al Viro6131ffa2013-02-27 16:59:05 -0500560 struct inode *inode = file_inode(file);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400561 struct fuse_conn *fc = get_fuse_conn(inode);
562 struct fuse_req *req;
563 struct fuse_forget_in inarg;
564
565 memset(&inarg, 0, sizeof(inarg));
566 inarg.nlookup = 1;
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400567 req = fuse_get_req_nofail_nopages(fc, file);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400568 req->in.h.opcode = FUSE_FORGET;
569 req->in.h.nodeid = nodeid;
570 req->in.numargs = 1;
571 req->in.args[0].size = sizeof(inarg);
572 req->in.args[0].value = &inarg;
573 req->isreply = 0;
Eric Wong6a4e9222013-02-04 13:04:44 +0000574 __fuse_request_send(fc, req);
575 /* ignore errors */
576 fuse_put_request(fc, req);
Anand V. Avati0b05b182012-08-19 08:53:23 -0400577}
578
Miklos Szeredi3be5a522008-04-30 00:54:41 -0700579/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700580 * Lock the request. Up to the next unlock_request() there mustn't be
581 * anything that could cause a page-fault. If the request was already
Miklos Szeredif9a28422006-06-25 05:48:53 -0700582 * aborted bail out.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700583 */
Miklos Szeredid7133112006-04-10 22:54:55 -0700584static int lock_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700585{
586 int err = 0;
587 if (req) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700588 spin_lock(&fc->lock);
Miklos Szeredif9a28422006-06-25 05:48:53 -0700589 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700590 err = -ENOENT;
591 else
592 req->locked = 1;
Miklos Szeredid7133112006-04-10 22:54:55 -0700593 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700594 }
595 return err;
596}
597
598/*
Miklos Szeredif9a28422006-06-25 05:48:53 -0700599 * Unlock request. If it was aborted during being locked, the
Miklos Szeredi334f4852005-09-09 13:10:27 -0700600 * requester thread is currently waiting for it to be unlocked, so
601 * wake it up.
602 */
Miklos Szeredid7133112006-04-10 22:54:55 -0700603static void unlock_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700604{
605 if (req) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700606 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700607 req->locked = 0;
Miklos Szeredif9a28422006-06-25 05:48:53 -0700608 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700609 wake_up(&req->waitq);
Miklos Szeredid7133112006-04-10 22:54:55 -0700610 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700611 }
612}
613
614struct fuse_copy_state {
Miklos Szeredid7133112006-04-10 22:54:55 -0700615 struct fuse_conn *fc;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700616 int write;
617 struct fuse_req *req;
618 const struct iovec *iov;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200619 struct pipe_buffer *pipebufs;
620 struct pipe_buffer *currbuf;
621 struct pipe_inode_info *pipe;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700622 unsigned long nr_segs;
623 unsigned long seglen;
624 unsigned long addr;
625 struct page *pg;
626 void *mapaddr;
627 void *buf;
628 unsigned len;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200629 unsigned move_pages:1;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700630};
631
Miklos Szeredid7133112006-04-10 22:54:55 -0700632static void fuse_copy_init(struct fuse_copy_state *cs, struct fuse_conn *fc,
Miklos Szeredic3021622010-05-25 15:06:07 +0200633 int write,
Miklos Szeredid7133112006-04-10 22:54:55 -0700634 const struct iovec *iov, unsigned long nr_segs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700635{
636 memset(cs, 0, sizeof(*cs));
Miklos Szeredid7133112006-04-10 22:54:55 -0700637 cs->fc = fc;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700638 cs->write = write;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700639 cs->iov = iov;
640 cs->nr_segs = nr_segs;
641}
642
643/* Unmap and put previous page of userspace buffer */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800644static void fuse_copy_finish(struct fuse_copy_state *cs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700645{
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200646 if (cs->currbuf) {
647 struct pipe_buffer *buf = cs->currbuf;
648
Miklos Szeredic3021622010-05-25 15:06:07 +0200649 if (!cs->write) {
650 buf->ops->unmap(cs->pipe, buf, cs->mapaddr);
651 } else {
Miklos Szeredi7909b1c2010-07-12 14:41:40 +0200652 kunmap(buf->page);
Miklos Szeredic3021622010-05-25 15:06:07 +0200653 buf->len = PAGE_SIZE - cs->len;
654 }
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200655 cs->currbuf = NULL;
656 cs->mapaddr = NULL;
657 } else if (cs->mapaddr) {
Miklos Szeredi7909b1c2010-07-12 14:41:40 +0200658 kunmap(cs->pg);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700659 if (cs->write) {
660 flush_dcache_page(cs->pg);
661 set_page_dirty_lock(cs->pg);
662 }
663 put_page(cs->pg);
664 cs->mapaddr = NULL;
665 }
666}
667
668/*
669 * Get another pagefull of userspace buffer, and map it to kernel
670 * address space, and lock request
671 */
672static int fuse_copy_fill(struct fuse_copy_state *cs)
673{
674 unsigned long offset;
675 int err;
676
Miklos Szeredid7133112006-04-10 22:54:55 -0700677 unlock_request(cs->fc, cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700678 fuse_copy_finish(cs);
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200679 if (cs->pipebufs) {
680 struct pipe_buffer *buf = cs->pipebufs;
681
Miklos Szeredic3021622010-05-25 15:06:07 +0200682 if (!cs->write) {
683 err = buf->ops->confirm(cs->pipe, buf);
684 if (err)
685 return err;
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200686
Miklos Szeredic3021622010-05-25 15:06:07 +0200687 BUG_ON(!cs->nr_segs);
688 cs->currbuf = buf;
Miklos Szeredi7909b1c2010-07-12 14:41:40 +0200689 cs->mapaddr = buf->ops->map(cs->pipe, buf, 0);
Miklos Szeredic3021622010-05-25 15:06:07 +0200690 cs->len = buf->len;
691 cs->buf = cs->mapaddr + buf->offset;
692 cs->pipebufs++;
693 cs->nr_segs--;
694 } else {
695 struct page *page;
696
697 if (cs->nr_segs == cs->pipe->buffers)
698 return -EIO;
699
700 page = alloc_page(GFP_HIGHUSER);
701 if (!page)
702 return -ENOMEM;
703
704 buf->page = page;
705 buf->offset = 0;
706 buf->len = 0;
707
708 cs->currbuf = buf;
Miklos Szeredi7909b1c2010-07-12 14:41:40 +0200709 cs->mapaddr = kmap(page);
Miklos Szeredic3021622010-05-25 15:06:07 +0200710 cs->buf = cs->mapaddr;
711 cs->len = PAGE_SIZE;
712 cs->pipebufs++;
713 cs->nr_segs++;
714 }
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200715 } else {
716 if (!cs->seglen) {
717 BUG_ON(!cs->nr_segs);
718 cs->seglen = cs->iov[0].iov_len;
719 cs->addr = (unsigned long) cs->iov[0].iov_base;
720 cs->iov++;
721 cs->nr_segs--;
722 }
723 err = get_user_pages_fast(cs->addr, 1, cs->write, &cs->pg);
724 if (err < 0)
725 return err;
726 BUG_ON(err != 1);
727 offset = cs->addr % PAGE_SIZE;
Miklos Szeredi7909b1c2010-07-12 14:41:40 +0200728 cs->mapaddr = kmap(cs->pg);
Miklos Szeredidd3bb142010-05-25 15:06:06 +0200729 cs->buf = cs->mapaddr + offset;
730 cs->len = min(PAGE_SIZE - offset, cs->seglen);
731 cs->seglen -= cs->len;
732 cs->addr += cs->len;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700733 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700734
Miklos Szeredid7133112006-04-10 22:54:55 -0700735 return lock_request(cs->fc, cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700736}
737
738/* Do as much copy to/from userspace buffer as we can */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800739static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700740{
741 unsigned ncpy = min(*size, cs->len);
742 if (val) {
743 if (cs->write)
744 memcpy(cs->buf, *val, ncpy);
745 else
746 memcpy(*val, cs->buf, ncpy);
747 *val += ncpy;
748 }
749 *size -= ncpy;
750 cs->len -= ncpy;
751 cs->buf += ncpy;
752 return ncpy;
753}
754
Miklos Szeredice534fb2010-05-25 15:06:07 +0200755static int fuse_check_page(struct page *page)
756{
757 if (page_mapcount(page) ||
758 page->mapping != NULL ||
759 page_count(page) != 1 ||
760 (page->flags & PAGE_FLAGS_CHECK_AT_PREP &
761 ~(1 << PG_locked |
762 1 << PG_referenced |
763 1 << PG_uptodate |
764 1 << PG_lru |
765 1 << PG_active |
766 1 << PG_reclaim))) {
767 printk(KERN_WARNING "fuse: trying to steal weird page\n");
768 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);
769 return 1;
770 }
771 return 0;
772}
773
774static int fuse_try_move_page(struct fuse_copy_state *cs, struct page **pagep)
775{
776 int err;
777 struct page *oldpage = *pagep;
778 struct page *newpage;
779 struct pipe_buffer *buf = cs->pipebufs;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200780
781 unlock_request(cs->fc, cs->req);
782 fuse_copy_finish(cs);
783
784 err = buf->ops->confirm(cs->pipe, buf);
785 if (err)
786 return err;
787
788 BUG_ON(!cs->nr_segs);
789 cs->currbuf = buf;
790 cs->len = buf->len;
791 cs->pipebufs++;
792 cs->nr_segs--;
793
794 if (cs->len != PAGE_SIZE)
795 goto out_fallback;
796
797 if (buf->ops->steal(cs->pipe, buf) != 0)
798 goto out_fallback;
799
800 newpage = buf->page;
801
802 if (WARN_ON(!PageUptodate(newpage)))
803 return -EIO;
804
805 ClearPageMappedToDisk(newpage);
806
807 if (fuse_check_page(newpage) != 0)
808 goto out_fallback_unlock;
809
Miklos Szeredice534fb2010-05-25 15:06:07 +0200810 /*
811 * This is a new and locked page, it shouldn't be mapped or
812 * have any special flags on it
813 */
814 if (WARN_ON(page_mapped(oldpage)))
815 goto out_fallback_unlock;
816 if (WARN_ON(page_has_private(oldpage)))
817 goto out_fallback_unlock;
818 if (WARN_ON(PageDirty(oldpage) || PageWriteback(oldpage)))
819 goto out_fallback_unlock;
820 if (WARN_ON(PageMlocked(oldpage)))
821 goto out_fallback_unlock;
822
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700823 err = replace_page_cache_page(oldpage, newpage, GFP_KERNEL);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200824 if (err) {
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700825 unlock_page(newpage);
826 return err;
Miklos Szeredice534fb2010-05-25 15:06:07 +0200827 }
Miklos Szeredief6a3c62011-03-22 16:30:52 -0700828
Miklos Szeredice534fb2010-05-25 15:06:07 +0200829 page_cache_get(newpage);
830
831 if (!(buf->flags & PIPE_BUF_FLAG_LRU))
832 lru_cache_add_file(newpage);
833
834 err = 0;
835 spin_lock(&cs->fc->lock);
836 if (cs->req->aborted)
837 err = -ENOENT;
838 else
839 *pagep = newpage;
840 spin_unlock(&cs->fc->lock);
841
842 if (err) {
843 unlock_page(newpage);
844 page_cache_release(newpage);
845 return err;
846 }
847
848 unlock_page(oldpage);
849 page_cache_release(oldpage);
850 cs->len = 0;
851
852 return 0;
853
854out_fallback_unlock:
855 unlock_page(newpage);
856out_fallback:
857 cs->mapaddr = buf->ops->map(cs->pipe, buf, 1);
858 cs->buf = cs->mapaddr + buf->offset;
859
860 err = lock_request(cs->fc, cs->req);
861 if (err)
862 return err;
863
864 return 1;
865}
866
Miklos Szeredic3021622010-05-25 15:06:07 +0200867static int fuse_ref_page(struct fuse_copy_state *cs, struct page *page,
868 unsigned offset, unsigned count)
869{
870 struct pipe_buffer *buf;
871
872 if (cs->nr_segs == cs->pipe->buffers)
873 return -EIO;
874
875 unlock_request(cs->fc, cs->req);
876 fuse_copy_finish(cs);
877
878 buf = cs->pipebufs;
879 page_cache_get(page);
880 buf->page = page;
881 buf->offset = offset;
882 buf->len = count;
883
884 cs->pipebufs++;
885 cs->nr_segs++;
886 cs->len = 0;
887
888 return 0;
889}
890
Miklos Szeredi334f4852005-09-09 13:10:27 -0700891/*
892 * Copy a page in the request to/from the userspace buffer. Must be
893 * done atomically
894 */
Miklos Szeredice534fb2010-05-25 15:06:07 +0200895static int fuse_copy_page(struct fuse_copy_state *cs, struct page **pagep,
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800896 unsigned offset, unsigned count, int zeroing)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700897{
Miklos Szeredice534fb2010-05-25 15:06:07 +0200898 int err;
899 struct page *page = *pagep;
900
Miklos Szeredib6777c42010-10-26 14:22:27 -0700901 if (page && zeroing && count < PAGE_SIZE)
902 clear_highpage(page);
903
Miklos Szeredi334f4852005-09-09 13:10:27 -0700904 while (count) {
Miklos Szeredic3021622010-05-25 15:06:07 +0200905 if (cs->write && cs->pipebufs && page) {
906 return fuse_ref_page(cs, page, offset, count);
907 } else if (!cs->len) {
Miklos Szeredice534fb2010-05-25 15:06:07 +0200908 if (cs->move_pages && page &&
909 offset == 0 && count == PAGE_SIZE) {
910 err = fuse_try_move_page(cs, pagep);
911 if (err <= 0)
912 return err;
913 } else {
914 err = fuse_copy_fill(cs);
915 if (err)
916 return err;
917 }
Miklos Szeredi1729a162008-11-26 12:03:54 +0100918 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700919 if (page) {
Cong Wang2408f6e2011-11-25 23:14:30 +0800920 void *mapaddr = kmap_atomic(page);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700921 void *buf = mapaddr + offset;
922 offset += fuse_copy_do(cs, &buf, &count);
Cong Wang2408f6e2011-11-25 23:14:30 +0800923 kunmap_atomic(mapaddr);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700924 } else
925 offset += fuse_copy_do(cs, NULL, &count);
926 }
927 if (page && !cs->write)
928 flush_dcache_page(page);
929 return 0;
930}
931
932/* Copy pages in the request to/from userspace buffer */
933static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
934 int zeroing)
935{
936 unsigned i;
937 struct fuse_req *req = cs->req;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700938
939 for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
Miklos Szeredice534fb2010-05-25 15:06:07 +0200940 int err;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +0400941 unsigned offset = req->page_descs[i].offset;
942 unsigned count = min(nbytes, req->page_descs[i].length);
Miklos Szeredice534fb2010-05-25 15:06:07 +0200943
944 err = fuse_copy_page(cs, &req->pages[i], offset, count,
945 zeroing);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700946 if (err)
947 return err;
948
949 nbytes -= count;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700950 }
951 return 0;
952}
953
954/* Copy a single argument in the request to/from userspace buffer */
955static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
956{
957 while (size) {
Miklos Szeredi1729a162008-11-26 12:03:54 +0100958 if (!cs->len) {
959 int err = fuse_copy_fill(cs);
960 if (err)
961 return err;
962 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700963 fuse_copy_do(cs, &val, &size);
964 }
965 return 0;
966}
967
968/* Copy request arguments to/from userspace buffer */
969static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
970 unsigned argpages, struct fuse_arg *args,
971 int zeroing)
972{
973 int err = 0;
974 unsigned i;
975
976 for (i = 0; !err && i < numargs; i++) {
977 struct fuse_arg *arg = &args[i];
978 if (i == numargs - 1 && argpages)
979 err = fuse_copy_pages(cs, arg->size, zeroing);
980 else
981 err = fuse_copy_one(cs, arg->value, arg->size);
982 }
983 return err;
984}
985
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100986static int forget_pending(struct fuse_conn *fc)
987{
988 return fc->forget_list_head.next != NULL;
989}
990
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700991static int request_pending(struct fuse_conn *fc)
992{
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100993 return !list_empty(&fc->pending) || !list_empty(&fc->interrupts) ||
994 forget_pending(fc);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700995}
996
Miklos Szeredi334f4852005-09-09 13:10:27 -0700997/* Wait until a request is available on the pending list */
998static void request_wait(struct fuse_conn *fc)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +0200999__releases(fc->lock)
1000__acquires(fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001001{
1002 DECLARE_WAITQUEUE(wait, current);
1003
1004 add_wait_queue_exclusive(&fc->waitq, &wait);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001005 while (fc->connected && !request_pending(fc)) {
Miklos Szeredi334f4852005-09-09 13:10:27 -07001006 set_current_state(TASK_INTERRUPTIBLE);
1007 if (signal_pending(current))
1008 break;
1009
Miklos Szeredid7133112006-04-10 22:54:55 -07001010 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001011 schedule();
Miklos Szeredid7133112006-04-10 22:54:55 -07001012 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001013 }
1014 set_current_state(TASK_RUNNING);
1015 remove_wait_queue(&fc->waitq, &wait);
1016}
1017
1018/*
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001019 * Transfer an interrupt request to userspace
1020 *
1021 * Unlike other requests this is assembled on demand, without a need
1022 * to allocate a separate fuse_req structure.
1023 *
1024 * Called with fc->lock held, releases it
1025 */
Miklos Szeredic3021622010-05-25 15:06:07 +02001026static int fuse_read_interrupt(struct fuse_conn *fc, struct fuse_copy_state *cs,
1027 size_t nbytes, struct fuse_req *req)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001028__releases(fc->lock)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001029{
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001030 struct fuse_in_header ih;
1031 struct fuse_interrupt_in arg;
1032 unsigned reqsize = sizeof(ih) + sizeof(arg);
1033 int err;
1034
1035 list_del_init(&req->intr_entry);
1036 req->intr_unique = fuse_get_unique(fc);
1037 memset(&ih, 0, sizeof(ih));
1038 memset(&arg, 0, sizeof(arg));
1039 ih.len = reqsize;
1040 ih.opcode = FUSE_INTERRUPT;
1041 ih.unique = req->intr_unique;
1042 arg.unique = req->in.h.unique;
1043
1044 spin_unlock(&fc->lock);
Miklos Szeredic3021622010-05-25 15:06:07 +02001045 if (nbytes < reqsize)
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001046 return -EINVAL;
1047
Miklos Szeredic3021622010-05-25 15:06:07 +02001048 err = fuse_copy_one(cs, &ih, sizeof(ih));
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001049 if (!err)
Miklos Szeredic3021622010-05-25 15:06:07 +02001050 err = fuse_copy_one(cs, &arg, sizeof(arg));
1051 fuse_copy_finish(cs);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001052
1053 return err ? err : reqsize;
1054}
1055
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001056static struct fuse_forget_link *dequeue_forget(struct fuse_conn *fc,
1057 unsigned max,
1058 unsigned *countp)
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001059{
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001060 struct fuse_forget_link *head = fc->forget_list_head.next;
1061 struct fuse_forget_link **newhead = &head;
1062 unsigned count;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001063
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001064 for (count = 0; *newhead != NULL && count < max; count++)
1065 newhead = &(*newhead)->next;
1066
1067 fc->forget_list_head.next = *newhead;
1068 *newhead = NULL;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001069 if (fc->forget_list_head.next == NULL)
1070 fc->forget_list_tail = &fc->forget_list_head;
1071
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001072 if (countp != NULL)
1073 *countp = count;
1074
1075 return head;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001076}
1077
1078static int fuse_read_single_forget(struct fuse_conn *fc,
1079 struct fuse_copy_state *cs,
1080 size_t nbytes)
1081__releases(fc->lock)
1082{
1083 int err;
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001084 struct fuse_forget_link *forget = dequeue_forget(fc, 1, NULL);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001085 struct fuse_forget_in arg = {
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001086 .nlookup = forget->forget_one.nlookup,
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001087 };
1088 struct fuse_in_header ih = {
1089 .opcode = FUSE_FORGET,
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001090 .nodeid = forget->forget_one.nodeid,
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001091 .unique = fuse_get_unique(fc),
1092 .len = sizeof(ih) + sizeof(arg),
1093 };
1094
1095 spin_unlock(&fc->lock);
1096 kfree(forget);
1097 if (nbytes < ih.len)
1098 return -EINVAL;
1099
1100 err = fuse_copy_one(cs, &ih, sizeof(ih));
1101 if (!err)
1102 err = fuse_copy_one(cs, &arg, sizeof(arg));
1103 fuse_copy_finish(cs);
1104
1105 if (err)
1106 return err;
1107
1108 return ih.len;
1109}
1110
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001111static int fuse_read_batch_forget(struct fuse_conn *fc,
1112 struct fuse_copy_state *cs, size_t nbytes)
1113__releases(fc->lock)
1114{
1115 int err;
1116 unsigned max_forgets;
1117 unsigned count;
1118 struct fuse_forget_link *head;
1119 struct fuse_batch_forget_in arg = { .count = 0 };
1120 struct fuse_in_header ih = {
1121 .opcode = FUSE_BATCH_FORGET,
1122 .unique = fuse_get_unique(fc),
1123 .len = sizeof(ih) + sizeof(arg),
1124 };
1125
1126 if (nbytes < ih.len) {
1127 spin_unlock(&fc->lock);
1128 return -EINVAL;
1129 }
1130
1131 max_forgets = (nbytes - ih.len) / sizeof(struct fuse_forget_one);
1132 head = dequeue_forget(fc, max_forgets, &count);
1133 spin_unlock(&fc->lock);
1134
1135 arg.count = count;
1136 ih.len += count * sizeof(struct fuse_forget_one);
1137 err = fuse_copy_one(cs, &ih, sizeof(ih));
1138 if (!err)
1139 err = fuse_copy_one(cs, &arg, sizeof(arg));
1140
1141 while (head) {
1142 struct fuse_forget_link *forget = head;
1143
1144 if (!err) {
1145 err = fuse_copy_one(cs, &forget->forget_one,
1146 sizeof(forget->forget_one));
1147 }
1148 head = forget->next;
1149 kfree(forget);
1150 }
1151
1152 fuse_copy_finish(cs);
1153
1154 if (err)
1155 return err;
1156
1157 return ih.len;
1158}
1159
1160static int fuse_read_forget(struct fuse_conn *fc, struct fuse_copy_state *cs,
1161 size_t nbytes)
1162__releases(fc->lock)
1163{
1164 if (fc->minor < 16 || fc->forget_list_head.next->next == NULL)
1165 return fuse_read_single_forget(fc, cs, nbytes);
1166 else
1167 return fuse_read_batch_forget(fc, cs, nbytes);
1168}
1169
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001170/*
Miklos Szeredi334f4852005-09-09 13:10:27 -07001171 * Read a single request into the userspace filesystem's buffer. This
1172 * function waits until a request is available, then removes it from
1173 * the pending list and copies request data to userspace buffer. If
Miklos Szeredif9a28422006-06-25 05:48:53 -07001174 * no reply is needed (FORGET) or request has been aborted or there
1175 * was an error during the copying then it's finished by calling
Miklos Szeredi334f4852005-09-09 13:10:27 -07001176 * request_end(). Otherwise add it to the processing list, and set
1177 * the 'sent' flag.
1178 */
Miklos Szeredic3021622010-05-25 15:06:07 +02001179static ssize_t fuse_dev_do_read(struct fuse_conn *fc, struct file *file,
1180 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001181{
1182 int err;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001183 struct fuse_req *req;
1184 struct fuse_in *in;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001185 unsigned reqsize;
1186
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001187 restart:
Miklos Szeredid7133112006-04-10 22:54:55 -07001188 spin_lock(&fc->lock);
Jeff Dikee5ac1d12006-04-10 22:54:53 -07001189 err = -EAGAIN;
1190 if ((file->f_flags & O_NONBLOCK) && fc->connected &&
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001191 !request_pending(fc))
Jeff Dikee5ac1d12006-04-10 22:54:53 -07001192 goto err_unlock;
1193
Miklos Szeredi334f4852005-09-09 13:10:27 -07001194 request_wait(fc);
1195 err = -ENODEV;
Miklos Szeredi9ba7cbb2006-01-16 22:14:34 -08001196 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001197 goto err_unlock;
1198 err = -ERESTARTSYS;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001199 if (!request_pending(fc))
Miklos Szeredi334f4852005-09-09 13:10:27 -07001200 goto err_unlock;
1201
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001202 if (!list_empty(&fc->interrupts)) {
1203 req = list_entry(fc->interrupts.next, struct fuse_req,
1204 intr_entry);
Miklos Szeredic3021622010-05-25 15:06:07 +02001205 return fuse_read_interrupt(fc, cs, nbytes, req);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001206 }
1207
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001208 if (forget_pending(fc)) {
1209 if (list_empty(&fc->pending) || fc->forget_batch-- > 0)
Miklos Szeredi02c048b2010-12-07 20:16:56 +01001210 return fuse_read_forget(fc, cs, nbytes);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001211
1212 if (fc->forget_batch <= -8)
1213 fc->forget_batch = 16;
1214 }
1215
Miklos Szeredi334f4852005-09-09 13:10:27 -07001216 req = list_entry(fc->pending.next, struct fuse_req, list);
Miklos Szeredi83cfd492006-01-16 22:14:31 -08001217 req->state = FUSE_REQ_READING;
Miklos Szeredid77a1d52006-01-16 22:14:31 -08001218 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001219
1220 in = &req->in;
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001221 reqsize = in->h.len;
1222 /* If request is too large, reply with an error and restart the read */
Miklos Szeredic3021622010-05-25 15:06:07 +02001223 if (nbytes < reqsize) {
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001224 req->out.h.error = -EIO;
1225 /* SETXATTR is special, since it may contain too large data */
1226 if (in->h.opcode == FUSE_SETXATTR)
1227 req->out.h.error = -E2BIG;
1228 request_end(fc, req);
1229 goto restart;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001230 }
Miklos Szeredid7133112006-04-10 22:54:55 -07001231 spin_unlock(&fc->lock);
Miklos Szeredic3021622010-05-25 15:06:07 +02001232 cs->req = req;
1233 err = fuse_copy_one(cs, &in->h, sizeof(in->h));
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001234 if (!err)
Miklos Szeredic3021622010-05-25 15:06:07 +02001235 err = fuse_copy_args(cs, in->numargs, in->argpages,
Miklos Szeredi1d3d7522006-01-06 00:19:40 -08001236 (struct fuse_arg *) in->args, 0);
Miklos Szeredic3021622010-05-25 15:06:07 +02001237 fuse_copy_finish(cs);
Miklos Szeredid7133112006-04-10 22:54:55 -07001238 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001239 req->locked = 0;
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -07001240 if (req->aborted) {
1241 request_end(fc, req);
1242 return -ENODEV;
1243 }
Miklos Szeredi334f4852005-09-09 13:10:27 -07001244 if (err) {
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -07001245 req->out.h.error = -EIO;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001246 request_end(fc, req);
1247 return err;
1248 }
1249 if (!req->isreply)
1250 request_end(fc, req);
1251 else {
Miklos Szeredi83cfd492006-01-16 22:14:31 -08001252 req->state = FUSE_REQ_SENT;
Miklos Szeredid77a1d52006-01-16 22:14:31 -08001253 list_move_tail(&req->list, &fc->processing);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001254 if (req->interrupted)
1255 queue_interrupt(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -07001256 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001257 }
1258 return reqsize;
1259
1260 err_unlock:
Miklos Szeredid7133112006-04-10 22:54:55 -07001261 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001262 return err;
1263}
1264
Miklos Szeredic3021622010-05-25 15:06:07 +02001265static ssize_t fuse_dev_read(struct kiocb *iocb, const struct iovec *iov,
1266 unsigned long nr_segs, loff_t pos)
1267{
1268 struct fuse_copy_state cs;
1269 struct file *file = iocb->ki_filp;
1270 struct fuse_conn *fc = fuse_get_conn(file);
1271 if (!fc)
1272 return -EPERM;
1273
1274 fuse_copy_init(&cs, fc, 1, iov, nr_segs);
1275
1276 return fuse_dev_do_read(fc, file, &cs, iov_length(iov, nr_segs));
1277}
1278
1279static int fuse_dev_pipe_buf_steal(struct pipe_inode_info *pipe,
1280 struct pipe_buffer *buf)
1281{
1282 return 1;
1283}
1284
1285static const struct pipe_buf_operations fuse_dev_pipe_buf_ops = {
1286 .can_merge = 0,
1287 .map = generic_pipe_buf_map,
1288 .unmap = generic_pipe_buf_unmap,
1289 .confirm = generic_pipe_buf_confirm,
1290 .release = generic_pipe_buf_release,
1291 .steal = fuse_dev_pipe_buf_steal,
1292 .get = generic_pipe_buf_get,
1293};
1294
1295static ssize_t fuse_dev_splice_read(struct file *in, loff_t *ppos,
1296 struct pipe_inode_info *pipe,
1297 size_t len, unsigned int flags)
1298{
1299 int ret;
1300 int page_nr = 0;
1301 int do_wakeup = 0;
1302 struct pipe_buffer *bufs;
1303 struct fuse_copy_state cs;
1304 struct fuse_conn *fc = fuse_get_conn(in);
1305 if (!fc)
1306 return -EPERM;
1307
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001308 bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), GFP_KERNEL);
Miklos Szeredic3021622010-05-25 15:06:07 +02001309 if (!bufs)
1310 return -ENOMEM;
1311
1312 fuse_copy_init(&cs, fc, 1, NULL, 0);
1313 cs.pipebufs = bufs;
1314 cs.pipe = pipe;
1315 ret = fuse_dev_do_read(fc, in, &cs, len);
1316 if (ret < 0)
1317 goto out;
1318
1319 ret = 0;
1320 pipe_lock(pipe);
1321
1322 if (!pipe->readers) {
1323 send_sig(SIGPIPE, current, 0);
1324 if (!ret)
1325 ret = -EPIPE;
1326 goto out_unlock;
1327 }
1328
1329 if (pipe->nrbufs + cs.nr_segs > pipe->buffers) {
1330 ret = -EIO;
1331 goto out_unlock;
1332 }
1333
1334 while (page_nr < cs.nr_segs) {
1335 int newbuf = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
1336 struct pipe_buffer *buf = pipe->bufs + newbuf;
1337
1338 buf->page = bufs[page_nr].page;
1339 buf->offset = bufs[page_nr].offset;
1340 buf->len = bufs[page_nr].len;
1341 buf->ops = &fuse_dev_pipe_buf_ops;
1342
1343 pipe->nrbufs++;
1344 page_nr++;
1345 ret += buf->len;
1346
1347 if (pipe->inode)
1348 do_wakeup = 1;
1349 }
1350
1351out_unlock:
1352 pipe_unlock(pipe);
1353
1354 if (do_wakeup) {
1355 smp_mb();
1356 if (waitqueue_active(&pipe->wait))
1357 wake_up_interruptible(&pipe->wait);
1358 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
1359 }
1360
1361out:
1362 for (; page_nr < cs.nr_segs; page_nr++)
1363 page_cache_release(bufs[page_nr].page);
1364
1365 kfree(bufs);
1366 return ret;
1367}
1368
Tejun Heo95668a62008-11-26 12:03:55 +01001369static int fuse_notify_poll(struct fuse_conn *fc, unsigned int size,
1370 struct fuse_copy_state *cs)
1371{
1372 struct fuse_notify_poll_wakeup_out outarg;
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001373 int err = -EINVAL;
Tejun Heo95668a62008-11-26 12:03:55 +01001374
1375 if (size != sizeof(outarg))
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001376 goto err;
Tejun Heo95668a62008-11-26 12:03:55 +01001377
1378 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1379 if (err)
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001380 goto err;
Tejun Heo95668a62008-11-26 12:03:55 +01001381
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001382 fuse_copy_finish(cs);
Tejun Heo95668a62008-11-26 12:03:55 +01001383 return fuse_notify_poll_wakeup(fc, &outarg);
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001384
1385err:
1386 fuse_copy_finish(cs);
1387 return err;
Tejun Heo95668a62008-11-26 12:03:55 +01001388}
1389
John Muir3b463ae2009-05-31 11:13:57 -04001390static int fuse_notify_inval_inode(struct fuse_conn *fc, unsigned int size,
1391 struct fuse_copy_state *cs)
1392{
1393 struct fuse_notify_inval_inode_out outarg;
1394 int err = -EINVAL;
1395
1396 if (size != sizeof(outarg))
1397 goto err;
1398
1399 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1400 if (err)
1401 goto err;
1402 fuse_copy_finish(cs);
1403
1404 down_read(&fc->killsb);
1405 err = -ENOENT;
Miklos Szeredib21dda42010-02-05 12:08:31 +01001406 if (fc->sb) {
1407 err = fuse_reverse_inval_inode(fc->sb, outarg.ino,
1408 outarg.off, outarg.len);
1409 }
John Muir3b463ae2009-05-31 11:13:57 -04001410 up_read(&fc->killsb);
1411 return err;
1412
1413err:
1414 fuse_copy_finish(cs);
1415 return err;
1416}
1417
1418static int fuse_notify_inval_entry(struct fuse_conn *fc, unsigned int size,
1419 struct fuse_copy_state *cs)
1420{
1421 struct fuse_notify_inval_entry_out outarg;
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001422 int err = -ENOMEM;
1423 char *buf;
John Muir3b463ae2009-05-31 11:13:57 -04001424 struct qstr name;
1425
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001426 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1427 if (!buf)
1428 goto err;
1429
1430 err = -EINVAL;
John Muir3b463ae2009-05-31 11:13:57 -04001431 if (size < sizeof(outarg))
1432 goto err;
1433
1434 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1435 if (err)
1436 goto err;
1437
1438 err = -ENAMETOOLONG;
1439 if (outarg.namelen > FUSE_NAME_MAX)
1440 goto err;
1441
Miklos Szeredic2183d12011-08-24 10:20:17 +02001442 err = -EINVAL;
1443 if (size != sizeof(outarg) + outarg.namelen + 1)
1444 goto err;
1445
John Muir3b463ae2009-05-31 11:13:57 -04001446 name.name = buf;
1447 name.len = outarg.namelen;
1448 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1449 if (err)
1450 goto err;
1451 fuse_copy_finish(cs);
1452 buf[outarg.namelen] = 0;
1453 name.hash = full_name_hash(name.name, name.len);
1454
1455 down_read(&fc->killsb);
1456 err = -ENOENT;
Miklos Szeredib21dda42010-02-05 12:08:31 +01001457 if (fc->sb)
John Muir451d0f52011-12-06 21:50:06 +01001458 err = fuse_reverse_inval_entry(fc->sb, outarg.parent, 0, &name);
1459 up_read(&fc->killsb);
1460 kfree(buf);
1461 return err;
1462
1463err:
1464 kfree(buf);
1465 fuse_copy_finish(cs);
1466 return err;
1467}
1468
1469static int fuse_notify_delete(struct fuse_conn *fc, unsigned int size,
1470 struct fuse_copy_state *cs)
1471{
1472 struct fuse_notify_delete_out outarg;
1473 int err = -ENOMEM;
1474 char *buf;
1475 struct qstr name;
1476
1477 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1478 if (!buf)
1479 goto err;
1480
1481 err = -EINVAL;
1482 if (size < sizeof(outarg))
1483 goto err;
1484
1485 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1486 if (err)
1487 goto err;
1488
1489 err = -ENAMETOOLONG;
1490 if (outarg.namelen > FUSE_NAME_MAX)
1491 goto err;
1492
1493 err = -EINVAL;
1494 if (size != sizeof(outarg) + outarg.namelen + 1)
1495 goto err;
1496
1497 name.name = buf;
1498 name.len = outarg.namelen;
1499 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1500 if (err)
1501 goto err;
1502 fuse_copy_finish(cs);
1503 buf[outarg.namelen] = 0;
1504 name.hash = full_name_hash(name.name, name.len);
1505
1506 down_read(&fc->killsb);
1507 err = -ENOENT;
1508 if (fc->sb)
1509 err = fuse_reverse_inval_entry(fc->sb, outarg.parent,
1510 outarg.child, &name);
John Muir3b463ae2009-05-31 11:13:57 -04001511 up_read(&fc->killsb);
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001512 kfree(buf);
John Muir3b463ae2009-05-31 11:13:57 -04001513 return err;
1514
1515err:
Fang Wenqib2d82ee2009-12-30 18:37:13 +08001516 kfree(buf);
John Muir3b463ae2009-05-31 11:13:57 -04001517 fuse_copy_finish(cs);
1518 return err;
1519}
1520
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001521static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
1522 struct fuse_copy_state *cs)
1523{
1524 struct fuse_notify_store_out outarg;
1525 struct inode *inode;
1526 struct address_space *mapping;
1527 u64 nodeid;
1528 int err;
1529 pgoff_t index;
1530 unsigned int offset;
1531 unsigned int num;
1532 loff_t file_size;
1533 loff_t end;
1534
1535 err = -EINVAL;
1536 if (size < sizeof(outarg))
1537 goto out_finish;
1538
1539 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1540 if (err)
1541 goto out_finish;
1542
1543 err = -EINVAL;
1544 if (size - sizeof(outarg) != outarg.size)
1545 goto out_finish;
1546
1547 nodeid = outarg.nodeid;
1548
1549 down_read(&fc->killsb);
1550
1551 err = -ENOENT;
1552 if (!fc->sb)
1553 goto out_up_killsb;
1554
1555 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1556 if (!inode)
1557 goto out_up_killsb;
1558
1559 mapping = inode->i_mapping;
1560 index = outarg.offset >> PAGE_CACHE_SHIFT;
1561 offset = outarg.offset & ~PAGE_CACHE_MASK;
1562 file_size = i_size_read(inode);
1563 end = outarg.offset + outarg.size;
1564 if (end > file_size) {
1565 file_size = end;
1566 fuse_write_update_size(inode, file_size);
1567 }
1568
1569 num = outarg.size;
1570 while (num) {
1571 struct page *page;
1572 unsigned int this_num;
1573
1574 err = -ENOMEM;
1575 page = find_or_create_page(mapping, index,
1576 mapping_gfp_mask(mapping));
1577 if (!page)
1578 goto out_iput;
1579
1580 this_num = min_t(unsigned, num, PAGE_CACHE_SIZE - offset);
1581 err = fuse_copy_page(cs, &page, offset, this_num, 0);
1582 if (!err && offset == 0 && (num != 0 || file_size == end))
1583 SetPageUptodate(page);
1584 unlock_page(page);
1585 page_cache_release(page);
1586
1587 if (err)
1588 goto out_iput;
1589
1590 num -= this_num;
1591 offset = 0;
1592 index++;
1593 }
1594
1595 err = 0;
1596
1597out_iput:
1598 iput(inode);
1599out_up_killsb:
1600 up_read(&fc->killsb);
1601out_finish:
1602 fuse_copy_finish(cs);
1603 return err;
1604}
1605
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001606static void fuse_retrieve_end(struct fuse_conn *fc, struct fuse_req *req)
1607{
Miklos Szeredi0be85572010-10-27 15:34:46 -07001608 release_pages(req->pages, req->num_pages, 0);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001609}
1610
1611static int fuse_retrieve(struct fuse_conn *fc, struct inode *inode,
1612 struct fuse_notify_retrieve_out *outarg)
1613{
1614 int err;
1615 struct address_space *mapping = inode->i_mapping;
1616 struct fuse_req *req;
1617 pgoff_t index;
1618 loff_t file_size;
1619 unsigned int num;
1620 unsigned int offset;
Geert Uytterhoeven01574432010-09-30 22:06:21 +02001621 size_t total_len = 0;
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001622 int num_pages;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001623
1624 offset = outarg->offset & ~PAGE_CACHE_MASK;
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001625 file_size = i_size_read(inode);
1626
1627 num = outarg->size;
1628 if (outarg->offset > file_size)
1629 num = 0;
1630 else if (outarg->offset + num > file_size)
1631 num = file_size - outarg->offset;
1632
1633 num_pages = (num + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1634 num_pages = min(num_pages, FUSE_MAX_PAGES_PER_REQ);
1635
1636 req = fuse_get_req(fc, num_pages);
1637 if (IS_ERR(req))
1638 return PTR_ERR(req);
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001639
1640 req->in.h.opcode = FUSE_NOTIFY_REPLY;
1641 req->in.h.nodeid = outarg->nodeid;
1642 req->in.numargs = 2;
1643 req->in.argpages = 1;
Maxim Patlasovb2430d72012-10-26 19:49:24 +04001644 req->page_descs[0].offset = offset;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001645 req->end = fuse_retrieve_end;
1646
1647 index = outarg->offset >> PAGE_CACHE_SHIFT;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001648
Maxim Patlasov4d53dc92012-10-26 19:48:42 +04001649 while (num && req->num_pages < num_pages) {
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001650 struct page *page;
1651 unsigned int this_num;
1652
1653 page = find_get_page(mapping, index);
1654 if (!page)
1655 break;
1656
1657 this_num = min_t(unsigned, num, PAGE_CACHE_SIZE - offset);
1658 req->pages[req->num_pages] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001659 req->page_descs[req->num_pages].length = this_num;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001660 req->num_pages++;
1661
Miklos Szeredic9e67d42012-09-04 18:45:54 +02001662 offset = 0;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001663 num -= this_num;
1664 total_len += this_num;
Miklos Szeredi48706d02011-12-13 10:36:59 +01001665 index++;
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001666 }
1667 req->misc.retrieve_in.offset = outarg->offset;
1668 req->misc.retrieve_in.size = total_len;
1669 req->in.args[0].size = sizeof(req->misc.retrieve_in);
1670 req->in.args[0].value = &req->misc.retrieve_in;
1671 req->in.args[1].size = total_len;
1672
1673 err = fuse_request_send_notify_reply(fc, req, outarg->notify_unique);
1674 if (err)
1675 fuse_retrieve_end(fc, req);
1676
1677 return err;
1678}
1679
1680static int fuse_notify_retrieve(struct fuse_conn *fc, unsigned int size,
1681 struct fuse_copy_state *cs)
1682{
1683 struct fuse_notify_retrieve_out outarg;
1684 struct inode *inode;
1685 int err;
1686
1687 err = -EINVAL;
1688 if (size != sizeof(outarg))
1689 goto copy_finish;
1690
1691 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1692 if (err)
1693 goto copy_finish;
1694
1695 fuse_copy_finish(cs);
1696
1697 down_read(&fc->killsb);
1698 err = -ENOENT;
1699 if (fc->sb) {
1700 u64 nodeid = outarg.nodeid;
1701
1702 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1703 if (inode) {
1704 err = fuse_retrieve(fc, inode, &outarg);
1705 iput(inode);
1706 }
1707 }
1708 up_read(&fc->killsb);
1709
1710 return err;
1711
1712copy_finish:
1713 fuse_copy_finish(cs);
1714 return err;
1715}
1716
Tejun Heo85993962008-11-26 12:03:55 +01001717static int fuse_notify(struct fuse_conn *fc, enum fuse_notify_code code,
1718 unsigned int size, struct fuse_copy_state *cs)
1719{
1720 switch (code) {
Tejun Heo95668a62008-11-26 12:03:55 +01001721 case FUSE_NOTIFY_POLL:
1722 return fuse_notify_poll(fc, size, cs);
1723
John Muir3b463ae2009-05-31 11:13:57 -04001724 case FUSE_NOTIFY_INVAL_INODE:
1725 return fuse_notify_inval_inode(fc, size, cs);
1726
1727 case FUSE_NOTIFY_INVAL_ENTRY:
1728 return fuse_notify_inval_entry(fc, size, cs);
1729
Miklos Szeredia1d75f22010-07-12 14:41:40 +02001730 case FUSE_NOTIFY_STORE:
1731 return fuse_notify_store(fc, size, cs);
1732
Miklos Szeredi2d45ba32010-07-12 14:41:40 +02001733 case FUSE_NOTIFY_RETRIEVE:
1734 return fuse_notify_retrieve(fc, size, cs);
1735
John Muir451d0f52011-12-06 21:50:06 +01001736 case FUSE_NOTIFY_DELETE:
1737 return fuse_notify_delete(fc, size, cs);
1738
Tejun Heo85993962008-11-26 12:03:55 +01001739 default:
Miklos Szeredif6d47a12009-01-26 15:00:59 +01001740 fuse_copy_finish(cs);
Tejun Heo85993962008-11-26 12:03:55 +01001741 return -EINVAL;
1742 }
1743}
1744
Miklos Szeredi334f4852005-09-09 13:10:27 -07001745/* Look up request on processing list by unique ID */
1746static struct fuse_req *request_find(struct fuse_conn *fc, u64 unique)
1747{
1748 struct list_head *entry;
1749
1750 list_for_each(entry, &fc->processing) {
1751 struct fuse_req *req;
1752 req = list_entry(entry, struct fuse_req, list);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001753 if (req->in.h.unique == unique || req->intr_unique == unique)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001754 return req;
1755 }
1756 return NULL;
1757}
1758
1759static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
1760 unsigned nbytes)
1761{
1762 unsigned reqsize = sizeof(struct fuse_out_header);
1763
1764 if (out->h.error)
1765 return nbytes != reqsize ? -EINVAL : 0;
1766
1767 reqsize += len_args(out->numargs, out->args);
1768
1769 if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
1770 return -EINVAL;
1771 else if (reqsize > nbytes) {
1772 struct fuse_arg *lastarg = &out->args[out->numargs-1];
1773 unsigned diffsize = reqsize - nbytes;
1774 if (diffsize > lastarg->size)
1775 return -EINVAL;
1776 lastarg->size -= diffsize;
1777 }
1778 return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
1779 out->page_zeroing);
1780}
1781
1782/*
1783 * Write a single reply to a request. First the header is copied from
1784 * the write buffer. The request is then searched on the processing
1785 * list by the unique ID found in the header. If found, then remove
1786 * it from the list and copy the rest of the buffer to the request.
1787 * The request is finished by calling request_end()
1788 */
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001789static ssize_t fuse_dev_do_write(struct fuse_conn *fc,
1790 struct fuse_copy_state *cs, size_t nbytes)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001791{
1792 int err;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001793 struct fuse_req *req;
1794 struct fuse_out_header oh;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001795
Miklos Szeredi334f4852005-09-09 13:10:27 -07001796 if (nbytes < sizeof(struct fuse_out_header))
1797 return -EINVAL;
1798
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001799 err = fuse_copy_one(cs, &oh, sizeof(oh));
Miklos Szeredi334f4852005-09-09 13:10:27 -07001800 if (err)
1801 goto err_finish;
Tejun Heo85993962008-11-26 12:03:55 +01001802
Miklos Szeredi334f4852005-09-09 13:10:27 -07001803 err = -EINVAL;
Tejun Heo85993962008-11-26 12:03:55 +01001804 if (oh.len != nbytes)
1805 goto err_finish;
1806
1807 /*
1808 * Zero oh.unique indicates unsolicited notification message
1809 * and error contains notification code.
1810 */
1811 if (!oh.unique) {
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001812 err = fuse_notify(fc, oh.error, nbytes - sizeof(oh), cs);
Tejun Heo85993962008-11-26 12:03:55 +01001813 return err ? err : nbytes;
1814 }
1815
1816 err = -EINVAL;
1817 if (oh.error <= -1000 || oh.error > 0)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001818 goto err_finish;
1819
Miklos Szeredid7133112006-04-10 22:54:55 -07001820 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001821 err = -ENOENT;
1822 if (!fc->connected)
1823 goto err_unlock;
1824
Miklos Szeredi334f4852005-09-09 13:10:27 -07001825 req = request_find(fc, oh.unique);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001826 if (!req)
1827 goto err_unlock;
1828
Miklos Szeredif9a28422006-06-25 05:48:53 -07001829 if (req->aborted) {
Miklos Szeredid7133112006-04-10 22:54:55 -07001830 spin_unlock(&fc->lock);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001831 fuse_copy_finish(cs);
Miklos Szeredid7133112006-04-10 22:54:55 -07001832 spin_lock(&fc->lock);
Miklos Szeredi222f1d62006-01-16 22:14:25 -08001833 request_end(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001834 return -ENOENT;
1835 }
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001836 /* Is it an interrupt reply? */
1837 if (req->intr_unique == oh.unique) {
1838 err = -EINVAL;
1839 if (nbytes != sizeof(struct fuse_out_header))
1840 goto err_unlock;
1841
1842 if (oh.error == -ENOSYS)
1843 fc->no_interrupt = 1;
1844 else if (oh.error == -EAGAIN)
1845 queue_interrupt(fc, req);
1846
1847 spin_unlock(&fc->lock);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001848 fuse_copy_finish(cs);
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001849 return nbytes;
1850 }
1851
1852 req->state = FUSE_REQ_WRITING;
Miklos Szeredid77a1d52006-01-16 22:14:31 -08001853 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001854 req->out.h = oh;
1855 req->locked = 1;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001856 cs->req = req;
Miklos Szeredice534fb2010-05-25 15:06:07 +02001857 if (!req->out.page_replace)
1858 cs->move_pages = 0;
Miklos Szeredid7133112006-04-10 22:54:55 -07001859 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001860
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001861 err = copy_out_args(cs, &req->out, nbytes);
1862 fuse_copy_finish(cs);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001863
Miklos Szeredid7133112006-04-10 22:54:55 -07001864 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001865 req->locked = 0;
1866 if (!err) {
Miklos Szeredif9a28422006-06-25 05:48:53 -07001867 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001868 err = -ENOENT;
Miklos Szeredif9a28422006-06-25 05:48:53 -07001869 } else if (!req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -07001870 req->out.h.error = -EIO;
1871 request_end(fc, req);
1872
1873 return err ? err : nbytes;
1874
1875 err_unlock:
Miklos Szeredid7133112006-04-10 22:54:55 -07001876 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001877 err_finish:
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001878 fuse_copy_finish(cs);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001879 return err;
1880}
1881
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001882static ssize_t fuse_dev_write(struct kiocb *iocb, const struct iovec *iov,
1883 unsigned long nr_segs, loff_t pos)
1884{
1885 struct fuse_copy_state cs;
1886 struct fuse_conn *fc = fuse_get_conn(iocb->ki_filp);
1887 if (!fc)
1888 return -EPERM;
1889
Miklos Szeredic3021622010-05-25 15:06:07 +02001890 fuse_copy_init(&cs, fc, 0, iov, nr_segs);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001891
1892 return fuse_dev_do_write(fc, &cs, iov_length(iov, nr_segs));
1893}
1894
1895static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe,
1896 struct file *out, loff_t *ppos,
1897 size_t len, unsigned int flags)
1898{
1899 unsigned nbuf;
1900 unsigned idx;
1901 struct pipe_buffer *bufs;
1902 struct fuse_copy_state cs;
1903 struct fuse_conn *fc;
1904 size_t rem;
1905 ssize_t ret;
1906
1907 fc = fuse_get_conn(out);
1908 if (!fc)
1909 return -EPERM;
1910
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01001911 bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), GFP_KERNEL);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001912 if (!bufs)
1913 return -ENOMEM;
1914
1915 pipe_lock(pipe);
1916 nbuf = 0;
1917 rem = 0;
1918 for (idx = 0; idx < pipe->nrbufs && rem < len; idx++)
1919 rem += pipe->bufs[(pipe->curbuf + idx) & (pipe->buffers - 1)].len;
1920
1921 ret = -EINVAL;
1922 if (rem < len) {
1923 pipe_unlock(pipe);
1924 goto out;
1925 }
1926
1927 rem = len;
1928 while (rem) {
1929 struct pipe_buffer *ibuf;
1930 struct pipe_buffer *obuf;
1931
1932 BUG_ON(nbuf >= pipe->buffers);
1933 BUG_ON(!pipe->nrbufs);
1934 ibuf = &pipe->bufs[pipe->curbuf];
1935 obuf = &bufs[nbuf];
1936
1937 if (rem >= ibuf->len) {
1938 *obuf = *ibuf;
1939 ibuf->ops = NULL;
1940 pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
1941 pipe->nrbufs--;
1942 } else {
1943 ibuf->ops->get(pipe, ibuf);
1944 *obuf = *ibuf;
1945 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1946 obuf->len = rem;
1947 ibuf->offset += obuf->len;
1948 ibuf->len -= obuf->len;
1949 }
1950 nbuf++;
1951 rem -= obuf->len;
1952 }
1953 pipe_unlock(pipe);
1954
Miklos Szeredic3021622010-05-25 15:06:07 +02001955 fuse_copy_init(&cs, fc, 0, NULL, nbuf);
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001956 cs.pipebufs = bufs;
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001957 cs.pipe = pipe;
1958
Miklos Szeredice534fb2010-05-25 15:06:07 +02001959 if (flags & SPLICE_F_MOVE)
1960 cs.move_pages = 1;
1961
Miklos Szeredidd3bb142010-05-25 15:06:06 +02001962 ret = fuse_dev_do_write(fc, &cs, len);
1963
1964 for (idx = 0; idx < nbuf; idx++) {
1965 struct pipe_buffer *buf = &bufs[idx];
1966 buf->ops->release(pipe, buf);
1967 }
1968out:
1969 kfree(bufs);
1970 return ret;
1971}
1972
Miklos Szeredi334f4852005-09-09 13:10:27 -07001973static unsigned fuse_dev_poll(struct file *file, poll_table *wait)
1974{
Miklos Szeredi334f4852005-09-09 13:10:27 -07001975 unsigned mask = POLLOUT | POLLWRNORM;
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07001976 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001977 if (!fc)
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07001978 return POLLERR;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001979
1980 poll_wait(file, &fc->waitq, wait);
1981
Miklos Szeredid7133112006-04-10 22:54:55 -07001982 spin_lock(&fc->lock);
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07001983 if (!fc->connected)
1984 mask = POLLERR;
Miklos Szeredia4d27e72006-06-25 05:48:54 -07001985 else if (request_pending(fc))
Miklos Szeredi7025d9a2006-04-10 22:54:50 -07001986 mask |= POLLIN | POLLRDNORM;
Miklos Szeredid7133112006-04-10 22:54:55 -07001987 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001988
1989 return mask;
1990}
1991
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001992/*
1993 * Abort all requests on the given list (pending or processing)
1994 *
Miklos Szeredid7133112006-04-10 22:54:55 -07001995 * This function releases and reacquires fc->lock
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001996 */
Miklos Szeredi334f4852005-09-09 13:10:27 -07001997static void end_requests(struct fuse_conn *fc, struct list_head *head)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02001998__releases(fc->lock)
1999__acquires(fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -07002000{
2001 while (!list_empty(head)) {
2002 struct fuse_req *req;
2003 req = list_entry(head->next, struct fuse_req, list);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002004 req->out.h.error = -ECONNABORTED;
2005 request_end(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -07002006 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002007 }
2008}
2009
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002010/*
2011 * Abort requests under I/O
2012 *
Miklos Szeredif9a28422006-06-25 05:48:53 -07002013 * The requests are set to aborted and finished, and the request
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002014 * waiter is woken up. This will make request_wait_answer() wait
2015 * until the request is unlocked and then return.
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08002016 *
2017 * If the request is asynchronous, then the end function needs to be
2018 * called after waiting for the request to be unlocked (if it was
2019 * locked).
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002020 */
2021static void end_io_requests(struct fuse_conn *fc)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02002022__releases(fc->lock)
2023__acquires(fc->lock)
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002024{
2025 while (!list_empty(&fc->io)) {
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08002026 struct fuse_req *req =
2027 list_entry(fc->io.next, struct fuse_req, list);
2028 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
2029
Miklos Szeredif9a28422006-06-25 05:48:53 -07002030 req->aborted = 1;
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002031 req->out.h.error = -ECONNABORTED;
2032 req->state = FUSE_REQ_FINISHED;
2033 list_del_init(&req->list);
2034 wake_up(&req->waitq);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08002035 if (end) {
2036 req->end = NULL;
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08002037 __fuse_get_request(req);
Miklos Szeredid7133112006-04-10 22:54:55 -07002038 spin_unlock(&fc->lock);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08002039 wait_event(req->waitq, !req->locked);
2040 end(fc, req);
Tejun Heoe9bb09d2008-11-26 12:03:54 +01002041 fuse_put_request(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -07002042 spin_lock(&fc->lock);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -08002043 }
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002044 }
2045}
2046
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002047static void end_queued_requests(struct fuse_conn *fc)
Miklos Szeredib9ca67b2010-09-07 13:42:41 +02002048__releases(fc->lock)
2049__acquires(fc->lock)
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002050{
2051 fc->max_background = UINT_MAX;
2052 flush_bg_queue(fc);
2053 end_requests(fc, &fc->pending);
2054 end_requests(fc, &fc->processing);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +01002055 while (forget_pending(fc))
Miklos Szeredi02c048b2010-12-07 20:16:56 +01002056 kfree(dequeue_forget(fc, 1, NULL));
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002057}
2058
Bryan Green357ccf22011-03-01 16:43:52 -08002059static void end_polls(struct fuse_conn *fc)
2060{
2061 struct rb_node *p;
2062
2063 p = rb_first(&fc->polled_files);
2064
2065 while (p) {
2066 struct fuse_file *ff;
2067 ff = rb_entry(p, struct fuse_file, polled_node);
2068 wake_up_interruptible_all(&ff->poll_wait);
2069
2070 p = rb_next(p);
2071 }
2072}
2073
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002074/*
2075 * Abort all requests.
2076 *
2077 * Emergency exit in case of a malicious or accidental deadlock, or
2078 * just a hung filesystem.
2079 *
2080 * The same effect is usually achievable through killing the
2081 * filesystem daemon and all users of the filesystem. The exception
2082 * is the combination of an asynchronous request and the tricky
2083 * deadlock (see Documentation/filesystems/fuse.txt).
2084 *
2085 * During the aborting, progression of requests from the pending and
2086 * processing lists onto the io list, and progression of new requests
2087 * onto the pending list is prevented by req->connected being false.
2088 *
2089 * Progression of requests under I/O to the processing list is
Miklos Szeredif9a28422006-06-25 05:48:53 -07002090 * prevented by the req->aborted flag being true for these requests.
2091 * For this reason requests on the io list must be aborted first.
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002092 */
2093void fuse_abort_conn(struct fuse_conn *fc)
2094{
Miklos Szeredid7133112006-04-10 22:54:55 -07002095 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002096 if (fc->connected) {
2097 fc->connected = 0;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -07002098 fc->blocked = 0;
Maxim Patlasov796523fb2013-03-21 18:02:15 +04002099 fc->initialized = 1;
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002100 end_io_requests(fc);
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002101 end_queued_requests(fc);
Bryan Green357ccf22011-03-01 16:43:52 -08002102 end_polls(fc);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002103 wake_up_all(&fc->waitq);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -07002104 wake_up_all(&fc->blocked_waitq);
Jeff Dike385a17b2006-04-10 22:54:52 -07002105 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002106 }
Miklos Szeredid7133112006-04-10 22:54:55 -07002107 spin_unlock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002108}
Tejun Heo08cbf542009-04-14 10:54:53 +09002109EXPORT_SYMBOL_GPL(fuse_abort_conn);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08002110
Tejun Heo08cbf542009-04-14 10:54:53 +09002111int fuse_dev_release(struct inode *inode, struct file *file)
Miklos Szeredi334f4852005-09-09 13:10:27 -07002112{
Miklos Szeredi0720b312006-04-10 22:54:55 -07002113 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002114 if (fc) {
Miklos Szeredid7133112006-04-10 22:54:55 -07002115 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -07002116 fc->connected = 0;
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002117 fc->blocked = 0;
Maxim Patlasov0aada882013-03-21 18:02:28 +04002118 fc->initialized = 1;
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002119 end_queued_requests(fc);
Bryan Green357ccf22011-03-01 16:43:52 -08002120 end_polls(fc);
Miklos Szeredi595afaf2010-09-07 13:42:41 +02002121 wake_up_all(&fc->blocked_waitq);
Miklos Szeredid7133112006-04-10 22:54:55 -07002122 spin_unlock(&fc->lock);
Miklos Szeredibafa9652006-06-25 05:48:51 -07002123 fuse_conn_put(fc);
Jeff Dike385a17b2006-04-10 22:54:52 -07002124 }
Miklos Szeredif543f252006-01-16 22:14:35 -08002125
Miklos Szeredi334f4852005-09-09 13:10:27 -07002126 return 0;
2127}
Tejun Heo08cbf542009-04-14 10:54:53 +09002128EXPORT_SYMBOL_GPL(fuse_dev_release);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002129
Jeff Dike385a17b2006-04-10 22:54:52 -07002130static int fuse_dev_fasync(int fd, struct file *file, int on)
2131{
2132 struct fuse_conn *fc = fuse_get_conn(file);
2133 if (!fc)
Miklos Szeredia87046d2006-04-10 22:54:56 -07002134 return -EPERM;
Jeff Dike385a17b2006-04-10 22:54:52 -07002135
2136 /* No locking - fasync_helper does its own locking */
2137 return fasync_helper(fd, file, on, &fc->fasync);
2138}
2139
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002140const struct file_operations fuse_dev_operations = {
Miklos Szeredi334f4852005-09-09 13:10:27 -07002141 .owner = THIS_MODULE,
2142 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002143 .read = do_sync_read,
2144 .aio_read = fuse_dev_read,
Miklos Szeredic3021622010-05-25 15:06:07 +02002145 .splice_read = fuse_dev_splice_read,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07002146 .write = do_sync_write,
2147 .aio_write = fuse_dev_write,
Miklos Szeredidd3bb142010-05-25 15:06:06 +02002148 .splice_write = fuse_dev_splice_write,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002149 .poll = fuse_dev_poll,
2150 .release = fuse_dev_release,
Jeff Dike385a17b2006-04-10 22:54:52 -07002151 .fasync = fuse_dev_fasync,
Miklos Szeredi334f4852005-09-09 13:10:27 -07002152};
Tejun Heo08cbf542009-04-14 10:54:53 +09002153EXPORT_SYMBOL_GPL(fuse_dev_operations);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002154
2155static struct miscdevice fuse_miscdevice = {
2156 .minor = FUSE_MINOR,
2157 .name = "fuse",
2158 .fops = &fuse_dev_operations,
2159};
2160
2161int __init fuse_dev_init(void)
2162{
2163 int err = -ENOMEM;
2164 fuse_req_cachep = kmem_cache_create("fuse_request",
2165 sizeof(struct fuse_req),
Paul Mundt20c2df82007-07-20 10:11:58 +09002166 0, 0, NULL);
Miklos Szeredi334f4852005-09-09 13:10:27 -07002167 if (!fuse_req_cachep)
2168 goto out;
2169
2170 err = misc_register(&fuse_miscdevice);
2171 if (err)
2172 goto out_cache_clean;
2173
2174 return 0;
2175
2176 out_cache_clean:
2177 kmem_cache_destroy(fuse_req_cachep);
2178 out:
2179 return err;
2180}
2181
2182void fuse_dev_cleanup(void)
2183{
2184 misc_deregister(&fuse_miscdevice);
2185 kmem_cache_destroy(fuse_req_cachep);
2186}