blob: d0ed60bc318842393e2af04a9b09a49c16afe622 [file] [log] [blame]
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001/*
2 FUSE: Filesystem in Userspace
Miklos Szeredi56cf34f2006-04-11 21:16:51 +02003 Copyright (C) 2001-2006 Miklos Szeredi <miklos@szeredi.hu>
Miklos Szeredib6aeade2005-09-09 13:10:30 -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/pagemap.h>
12#include <linux/slab.h>
13#include <linux/kernel.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040014#include <linux/sched.h>
Miklos Szeredib6aeade2005-09-09 13:10:30 -070015
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080016static const struct file_operations fuse_direct_io_file_operations;
Miklos Szeredi45323fb2005-09-09 13:10:37 -070017
Miklos Szeredifd72faa2005-11-07 00:59:51 -080018static int fuse_send_open(struct inode *inode, struct file *file, int isdir,
19 struct fuse_open_out *outargp)
Miklos Szeredib6aeade2005-09-09 13:10:30 -070020{
21 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -070022 struct fuse_open_in inarg;
Miklos Szeredifd72faa2005-11-07 00:59:51 -080023 struct fuse_req *req;
24 int err;
25
Miklos Szeredice1d5a42006-04-10 22:54:58 -070026 req = fuse_get_req(fc);
27 if (IS_ERR(req))
28 return PTR_ERR(req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080029
30 memset(&inarg, 0, sizeof(inarg));
31 inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
32 req->in.h.opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
33 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080034 req->in.numargs = 1;
35 req->in.args[0].size = sizeof(inarg);
36 req->in.args[0].value = &inarg;
37 req->out.numargs = 1;
38 req->out.args[0].size = sizeof(*outargp);
39 req->out.args[0].value = outargp;
40 request_send(fc, req);
41 err = req->out.h.error;
42 fuse_put_request(fc, req);
43
44 return err;
45}
46
47struct fuse_file *fuse_file_alloc(void)
48{
49 struct fuse_file *ff;
50 ff = kmalloc(sizeof(struct fuse_file), GFP_KERNEL);
51 if (ff) {
Miklos Szeredi33649c92006-06-25 05:48:52 -070052 ff->reserved_req = fuse_request_alloc();
53 if (!ff->reserved_req) {
Miklos Szeredifd72faa2005-11-07 00:59:51 -080054 kfree(ff);
55 ff = NULL;
56 }
57 }
58 return ff;
59}
60
61void fuse_file_free(struct fuse_file *ff)
62{
Miklos Szeredi33649c92006-06-25 05:48:52 -070063 fuse_request_free(ff->reserved_req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080064 kfree(ff);
65}
66
67void fuse_finish_open(struct inode *inode, struct file *file,
68 struct fuse_file *ff, struct fuse_open_out *outarg)
69{
70 if (outarg->open_flags & FOPEN_DIRECT_IO)
71 file->f_op = &fuse_direct_io_file_operations;
72 if (!(outarg->open_flags & FOPEN_KEEP_CACHE))
Andrew Mortonfc0ecff2007-02-10 01:45:39 -080073 invalidate_mapping_pages(inode->i_mapping, 0, -1);
Miklos Szeredifd72faa2005-11-07 00:59:51 -080074 ff->fh = outarg->fh;
75 file->private_data = ff;
76}
77
78int fuse_open_common(struct inode *inode, struct file *file, int isdir)
79{
Miklos Szeredib6aeade2005-09-09 13:10:30 -070080 struct fuse_open_out outarg;
81 struct fuse_file *ff;
82 int err;
Miklos Szeredib6aeade2005-09-09 13:10:30 -070083
Miklos Szeredidd190d02005-09-30 11:59:02 -070084 /* VFS checks this, but only _after_ ->open() */
85 if (file->f_flags & O_DIRECT)
86 return -EINVAL;
87
Miklos Szeredib6aeade2005-09-09 13:10:30 -070088 err = generic_file_open(inode, file);
89 if (err)
90 return err;
91
92 /* If opening the root node, no lookup has been performed on
93 it, so the attributes must be refreshed */
94 if (get_node_id(inode) == FUSE_ROOT_ID) {
Miklos Szeredifd72faa2005-11-07 00:59:51 -080095 err = fuse_do_getattr(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -070096 if (err)
97 return err;
98 }
99
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800100 ff = fuse_file_alloc();
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700101 if (!ff)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800102 return -ENOMEM;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700103
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800104 err = fuse_send_open(inode, file, isdir, &outarg);
105 if (err)
106 fuse_file_free(ff);
107 else {
108 if (isdir)
109 outarg.open_flags &= ~FOPEN_DIRECT_IO;
110 fuse_finish_open(inode, file, ff, &outarg);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700111 }
112
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700113 return err;
114}
115
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700116struct fuse_req *fuse_release_fill(struct fuse_file *ff, u64 nodeid, int flags,
117 int opcode)
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800118{
Miklos Szeredi33649c92006-06-25 05:48:52 -0700119 struct fuse_req *req = ff->reserved_req;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700120 struct fuse_release_in *inarg = &req->misc.release_in;
121
122 inarg->fh = ff->fh;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800123 inarg->flags = flags;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700124 req->in.h.opcode = opcode;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800125 req->in.h.nodeid = nodeid;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700126 req->in.numargs = 1;
127 req->in.args[0].size = sizeof(struct fuse_release_in);
128 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700129 kfree(ff);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700130
131 return req;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800132}
133
134int fuse_release_common(struct inode *inode, struct file *file, int isdir)
135{
136 struct fuse_file *ff = file->private_data;
137 if (ff) {
138 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700139 struct fuse_req *req;
140
141 req = fuse_release_fill(ff, get_node_id(inode), file->f_flags,
142 isdir ? FUSE_RELEASEDIR : FUSE_RELEASE);
143
144 /* Hold vfsmount and dentry until release is finished */
Josef Sipek7706a9d2006-12-08 02:37:02 -0800145 req->vfsmount = mntget(file->f_path.mnt);
146 req->dentry = dget(file->f_path.dentry);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700147 request_send_background(fc, req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800148 }
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700149
150 /* Return value is ignored by VFS */
151 return 0;
152}
153
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700154static int fuse_open(struct inode *inode, struct file *file)
155{
156 return fuse_open_common(inode, file, 0);
157}
158
159static int fuse_release(struct inode *inode, struct file *file)
160{
161 return fuse_release_common(inode, file, 0);
162}
163
Miklos Szeredi71421252006-06-25 05:48:52 -0700164/*
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700165 * Scramble the ID space with XTEA, so that the value of the files_struct
166 * pointer is not exposed to userspace.
Miklos Szeredi71421252006-06-25 05:48:52 -0700167 */
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700168static u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
Miklos Szeredi71421252006-06-25 05:48:52 -0700169{
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700170 u32 *k = fc->scramble_key;
171 u64 v = (unsigned long) id;
172 u32 v0 = v;
173 u32 v1 = v >> 32;
174 u32 sum = 0;
175 int i;
176
177 for (i = 0; i < 32; i++) {
178 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
179 sum += 0x9E3779B9;
180 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
181 }
182
183 return (u64) v0 + ((u64) v1 << 32);
Miklos Szeredi71421252006-06-25 05:48:52 -0700184}
185
Miklos Szeredi75e1fcc2006-06-23 02:05:12 -0700186static int fuse_flush(struct file *file, fl_owner_t id)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700187{
Josef Sipek7706a9d2006-12-08 02:37:02 -0800188 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700189 struct fuse_conn *fc = get_fuse_conn(inode);
190 struct fuse_file *ff = file->private_data;
191 struct fuse_req *req;
192 struct fuse_flush_in inarg;
193 int err;
194
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800195 if (is_bad_inode(inode))
196 return -EIO;
197
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700198 if (fc->no_flush)
199 return 0;
200
Miklos Szeredi33649c92006-06-25 05:48:52 -0700201 req = fuse_get_req_nofail(fc, file);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700202 memset(&inarg, 0, sizeof(inarg));
203 inarg.fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700204 inarg.lock_owner = fuse_lock_owner_id(fc, id);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700205 req->in.h.opcode = FUSE_FLUSH;
206 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700207 req->in.numargs = 1;
208 req->in.args[0].size = sizeof(inarg);
209 req->in.args[0].value = &inarg;
Miklos Szeredi71421252006-06-25 05:48:52 -0700210 req->force = 1;
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700211 request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700212 err = req->out.h.error;
213 fuse_put_request(fc, req);
214 if (err == -ENOSYS) {
215 fc->no_flush = 1;
216 err = 0;
217 }
218 return err;
219}
220
Miklos Szeredi82547982005-09-09 13:10:38 -0700221int fuse_fsync_common(struct file *file, struct dentry *de, int datasync,
222 int isdir)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700223{
224 struct inode *inode = de->d_inode;
225 struct fuse_conn *fc = get_fuse_conn(inode);
226 struct fuse_file *ff = file->private_data;
227 struct fuse_req *req;
228 struct fuse_fsync_in inarg;
229 int err;
230
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800231 if (is_bad_inode(inode))
232 return -EIO;
233
Miklos Szeredi82547982005-09-09 13:10:38 -0700234 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700235 return 0;
236
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700237 req = fuse_get_req(fc);
238 if (IS_ERR(req))
239 return PTR_ERR(req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700240
241 memset(&inarg, 0, sizeof(inarg));
242 inarg.fh = ff->fh;
243 inarg.fsync_flags = datasync ? 1 : 0;
Miklos Szeredi82547982005-09-09 13:10:38 -0700244 req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700245 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700246 req->in.numargs = 1;
247 req->in.args[0].size = sizeof(inarg);
248 req->in.args[0].value = &inarg;
249 request_send(fc, req);
250 err = req->out.h.error;
251 fuse_put_request(fc, req);
252 if (err == -ENOSYS) {
Miklos Szeredi82547982005-09-09 13:10:38 -0700253 if (isdir)
254 fc->no_fsyncdir = 1;
255 else
256 fc->no_fsync = 1;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700257 err = 0;
258 }
259 return err;
260}
261
Miklos Szeredi82547982005-09-09 13:10:38 -0700262static int fuse_fsync(struct file *file, struct dentry *de, int datasync)
263{
264 return fuse_fsync_common(file, de, datasync, 0);
265}
266
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800267void fuse_read_fill(struct fuse_req *req, struct file *file,
268 struct inode *inode, loff_t pos, size_t count, int opcode)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700269{
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700270 struct fuse_file *ff = file->private_data;
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800271 struct fuse_read_in *inarg = &req->misc.read_in;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700272
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800273 inarg->fh = ff->fh;
274 inarg->offset = pos;
275 inarg->size = count;
276 req->in.h.opcode = opcode;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700277 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700278 req->in.numargs = 1;
279 req->in.args[0].size = sizeof(struct fuse_read_in);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800280 req->in.args[0].value = inarg;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700281 req->out.argpages = 1;
282 req->out.argvar = 1;
283 req->out.numargs = 1;
284 req->out.args[0].size = count;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700285}
286
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800287static size_t fuse_send_read(struct fuse_req *req, struct file *file,
288 struct inode *inode, loff_t pos, size_t count)
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700289{
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800290 struct fuse_conn *fc = get_fuse_conn(inode);
291 fuse_read_fill(req, file, inode, pos, count, FUSE_READ);
292 request_send(fc, req);
293 return req->out.args[0].size;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700294}
295
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700296static int fuse_readpage(struct file *file, struct page *page)
297{
298 struct inode *inode = page->mapping->host;
299 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800300 struct fuse_req *req;
301 int err;
302
303 err = -EIO;
304 if (is_bad_inode(inode))
305 goto out;
306
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700307 req = fuse_get_req(fc);
308 err = PTR_ERR(req);
309 if (IS_ERR(req))
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700310 goto out;
311
312 req->out.page_zeroing = 1;
313 req->num_pages = 1;
314 req->pages[0] = page;
Miklos Szeredi4633a222006-01-06 00:19:36 -0800315 fuse_send_read(req, file, inode, page_offset(page), PAGE_CACHE_SIZE);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700316 err = req->out.h.error;
317 fuse_put_request(fc, req);
318 if (!err)
319 SetPageUptodate(page);
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700320 fuse_invalidate_attr(inode); /* atime changed */
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700321 out:
322 unlock_page(page);
323 return err;
324}
325
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800326static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredidb50b962005-09-09 13:10:33 -0700327{
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800328 int i;
329
330 fuse_invalidate_attr(req->pages[0]->mapping->host); /* atime changed */
331
Miklos Szeredidb50b962005-09-09 13:10:33 -0700332 for (i = 0; i < req->num_pages; i++) {
333 struct page *page = req->pages[i];
334 if (!req->out.h.error)
335 SetPageUptodate(page);
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800336 else
337 SetPageError(page);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700338 unlock_page(page);
339 }
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800340 fuse_put_request(fc, req);
341}
342
343static void fuse_send_readpages(struct fuse_req *req, struct file *file,
344 struct inode *inode)
345{
346 struct fuse_conn *fc = get_fuse_conn(inode);
347 loff_t pos = page_offset(req->pages[0]);
348 size_t count = req->num_pages << PAGE_CACHE_SHIFT;
349 req->out.page_zeroing = 1;
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800350 fuse_read_fill(req, file, inode, pos, count, FUSE_READ);
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800351 if (fc->async_read) {
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700352 get_file(file);
353 req->file = file;
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800354 req->end = fuse_readpages_end;
355 request_send_background(fc, req);
356 } else {
357 request_send(fc, req);
358 fuse_readpages_end(fc, req);
359 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700360}
361
362struct fuse_readpages_data {
363 struct fuse_req *req;
364 struct file *file;
365 struct inode *inode;
366};
367
368static int fuse_readpages_fill(void *_data, struct page *page)
369{
370 struct fuse_readpages_data *data = _data;
371 struct fuse_req *req = data->req;
372 struct inode *inode = data->inode;
373 struct fuse_conn *fc = get_fuse_conn(inode);
374
375 if (req->num_pages &&
376 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
377 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
378 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
Miklos Szeredic1aa96a2006-01-16 22:14:46 -0800379 fuse_send_readpages(req, data->file, inode);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700380 data->req = req = fuse_get_req(fc);
381 if (IS_ERR(req)) {
Miklos Szeredidb50b962005-09-09 13:10:33 -0700382 unlock_page(page);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700383 return PTR_ERR(req);
Miklos Szeredidb50b962005-09-09 13:10:33 -0700384 }
Miklos Szeredidb50b962005-09-09 13:10:33 -0700385 }
386 req->pages[req->num_pages] = page;
387 req->num_pages ++;
388 return 0;
389}
390
391static int fuse_readpages(struct file *file, struct address_space *mapping,
392 struct list_head *pages, unsigned nr_pages)
393{
394 struct inode *inode = mapping->host;
395 struct fuse_conn *fc = get_fuse_conn(inode);
396 struct fuse_readpages_data data;
397 int err;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800398
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700399 err = -EIO;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800400 if (is_bad_inode(inode))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800401 goto out;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800402
Miklos Szeredidb50b962005-09-09 13:10:33 -0700403 data.file = file;
404 data.inode = inode;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700405 data.req = fuse_get_req(fc);
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700406 err = PTR_ERR(data.req);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700407 if (IS_ERR(data.req))
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800408 goto out;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700409
410 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
Miklos Szeredid3406ff2006-04-10 22:54:49 -0700411 if (!err) {
412 if (data.req->num_pages)
413 fuse_send_readpages(data.req, file, inode);
414 else
415 fuse_put_request(fc, data.req);
416 }
OGAWA Hirofumi2e990022006-11-02 22:07:09 -0800417out:
Alexander Zarochentsev1d7ea732006-08-13 23:24:27 -0700418 return err;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700419}
420
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700421static size_t fuse_send_write(struct fuse_req *req, struct file *file,
422 struct inode *inode, loff_t pos, size_t count)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700423{
424 struct fuse_conn *fc = get_fuse_conn(inode);
425 struct fuse_file *ff = file->private_data;
426 struct fuse_write_in inarg;
427 struct fuse_write_out outarg;
428
429 memset(&inarg, 0, sizeof(struct fuse_write_in));
430 inarg.fh = ff->fh;
431 inarg.offset = pos;
432 inarg.size = count;
433 req->in.h.opcode = FUSE_WRITE;
434 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700435 req->in.argpages = 1;
436 req->in.numargs = 2;
437 req->in.args[0].size = sizeof(struct fuse_write_in);
438 req->in.args[0].value = &inarg;
439 req->in.args[1].size = count;
440 req->out.numargs = 1;
441 req->out.args[0].size = sizeof(struct fuse_write_out);
442 req->out.args[0].value = &outarg;
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700443 request_send(fc, req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700444 return outarg.size;
445}
446
447static int fuse_prepare_write(struct file *file, struct page *page,
448 unsigned offset, unsigned to)
449{
450 /* No op */
451 return 0;
452}
453
454static int fuse_commit_write(struct file *file, struct page *page,
455 unsigned offset, unsigned to)
456{
457 int err;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700458 size_t nres;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700459 unsigned count = to - offset;
460 struct inode *inode = page->mapping->host;
461 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi4633a222006-01-06 00:19:36 -0800462 loff_t pos = page_offset(page) + offset;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800463 struct fuse_req *req;
464
465 if (is_bad_inode(inode))
466 return -EIO;
467
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700468 req = fuse_get_req(fc);
469 if (IS_ERR(req))
470 return PTR_ERR(req);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700471
472 req->num_pages = 1;
473 req->pages[0] = page;
474 req->page_offset = offset;
475 nres = fuse_send_write(req, file, inode, pos, count);
476 err = req->out.h.error;
477 fuse_put_request(fc, req);
478 if (!err && nres != count)
479 err = -EIO;
480 if (!err) {
481 pos += count;
Miklos Szeredi9ffbb912006-10-17 00:10:06 -0700482 spin_lock(&fc->lock);
483 if (pos > inode->i_size)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700484 i_size_write(inode, pos);
Miklos Szeredi9ffbb912006-10-17 00:10:06 -0700485 spin_unlock(&fc->lock);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700486
Miklos Szeredi9280f682006-12-21 15:18:23 +0100487 if (offset == 0 && to == PAGE_CACHE_SIZE)
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700488 SetPageUptodate(page);
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700489 }
490 fuse_invalidate_attr(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700491 return err;
492}
493
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700494static void fuse_release_user_pages(struct fuse_req *req, int write)
495{
496 unsigned i;
497
498 for (i = 0; i < req->num_pages; i++) {
499 struct page *page = req->pages[i];
500 if (write)
501 set_page_dirty_lock(page);
502 put_page(page);
503 }
504}
505
506static int fuse_get_user_pages(struct fuse_req *req, const char __user *buf,
507 unsigned nbytes, int write)
508{
509 unsigned long user_addr = (unsigned long) buf;
510 unsigned offset = user_addr & ~PAGE_MASK;
511 int npages;
512
513 /* This doesn't work with nfsd */
514 if (!current->mm)
515 return -EPERM;
516
517 nbytes = min(nbytes, (unsigned) FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
518 npages = (nbytes + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
Miklos Szeredi6ad84ac2006-01-06 00:19:42 -0800519 npages = min(max(npages, 1), FUSE_MAX_PAGES_PER_REQ);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700520 down_read(&current->mm->mmap_sem);
521 npages = get_user_pages(current, current->mm, user_addr, npages, write,
522 0, req->pages, NULL);
523 up_read(&current->mm->mmap_sem);
524 if (npages < 0)
525 return npages;
526
527 req->num_pages = npages;
528 req->page_offset = offset;
529 return 0;
530}
531
532static ssize_t fuse_direct_io(struct file *file, const char __user *buf,
533 size_t count, loff_t *ppos, int write)
534{
Josef Sipek7706a9d2006-12-08 02:37:02 -0800535 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700536 struct fuse_conn *fc = get_fuse_conn(inode);
537 size_t nmax = write ? fc->max_write : fc->max_read;
538 loff_t pos = *ppos;
539 ssize_t res = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800540 struct fuse_req *req;
541
542 if (is_bad_inode(inode))
543 return -EIO;
544
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700545 req = fuse_get_req(fc);
546 if (IS_ERR(req))
547 return PTR_ERR(req);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700548
549 while (count) {
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700550 size_t nres;
551 size_t nbytes = min(count, nmax);
552 int err = fuse_get_user_pages(req, buf, nbytes, !write);
553 if (err) {
554 res = err;
555 break;
556 }
Miklos Szeredi6ad84ac2006-01-06 00:19:42 -0800557 nbytes = (req->num_pages << PAGE_SHIFT) - req->page_offset;
558 nbytes = min(count, nbytes);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700559 if (write)
560 nres = fuse_send_write(req, file, inode, pos, nbytes);
561 else
562 nres = fuse_send_read(req, file, inode, pos, nbytes);
563 fuse_release_user_pages(req, !write);
564 if (req->out.h.error) {
565 if (!res)
566 res = req->out.h.error;
567 break;
568 } else if (nres > nbytes) {
569 res = -EIO;
570 break;
571 }
572 count -= nres;
573 res += nres;
574 pos += nres;
575 buf += nres;
576 if (nres != nbytes)
577 break;
Miklos Szeredi56cf34f2006-04-11 21:16:51 +0200578 if (count) {
579 fuse_put_request(fc, req);
580 req = fuse_get_req(fc);
581 if (IS_ERR(req))
582 break;
583 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700584 }
585 fuse_put_request(fc, req);
586 if (res > 0) {
Miklos Szeredi9ffbb912006-10-17 00:10:06 -0700587 if (write) {
588 spin_lock(&fc->lock);
589 if (pos > inode->i_size)
590 i_size_write(inode, pos);
591 spin_unlock(&fc->lock);
592 }
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700593 *ppos = pos;
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700594 }
595 fuse_invalidate_attr(inode);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700596
597 return res;
598}
599
600static ssize_t fuse_direct_read(struct file *file, char __user *buf,
601 size_t count, loff_t *ppos)
602{
603 return fuse_direct_io(file, buf, count, ppos, 0);
604}
605
606static ssize_t fuse_direct_write(struct file *file, const char __user *buf,
607 size_t count, loff_t *ppos)
608{
Josef Sipek7706a9d2006-12-08 02:37:02 -0800609 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700610 ssize_t res;
611 /* Don't allow parallel writes to the same file */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800612 mutex_lock(&inode->i_mutex);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700613 res = fuse_direct_io(file, buf, count, ppos, 1);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800614 mutex_unlock(&inode->i_mutex);
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700615 return res;
616}
617
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700618static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
619{
620 if ((vma->vm_flags & VM_SHARED)) {
621 if ((vma->vm_flags & VM_WRITE))
622 return -ENODEV;
623 else
624 vma->vm_flags &= ~VM_MAYWRITE;
625 }
626 return generic_file_mmap(file, vma);
627}
628
629static int fuse_set_page_dirty(struct page *page)
630{
631 printk("fuse_set_page_dirty: should not happen\n");
632 dump_stack();
633 return 0;
634}
635
Miklos Szeredi71421252006-06-25 05:48:52 -0700636static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
637 struct file_lock *fl)
638{
639 switch (ffl->type) {
640 case F_UNLCK:
641 break;
642
643 case F_RDLCK:
644 case F_WRLCK:
645 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
646 ffl->end < ffl->start)
647 return -EIO;
648
649 fl->fl_start = ffl->start;
650 fl->fl_end = ffl->end;
651 fl->fl_pid = ffl->pid;
652 break;
653
654 default:
655 return -EIO;
656 }
657 fl->fl_type = ffl->type;
658 return 0;
659}
660
661static void fuse_lk_fill(struct fuse_req *req, struct file *file,
662 const struct file_lock *fl, int opcode, pid_t pid)
663{
Josef Sipek7706a9d2006-12-08 02:37:02 -0800664 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700665 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi71421252006-06-25 05:48:52 -0700666 struct fuse_file *ff = file->private_data;
667 struct fuse_lk_in *arg = &req->misc.lk_in;
668
669 arg->fh = ff->fh;
Miklos Szeredi9c8ef562006-06-25 05:48:55 -0700670 arg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
Miklos Szeredi71421252006-06-25 05:48:52 -0700671 arg->lk.start = fl->fl_start;
672 arg->lk.end = fl->fl_end;
673 arg->lk.type = fl->fl_type;
674 arg->lk.pid = pid;
675 req->in.h.opcode = opcode;
676 req->in.h.nodeid = get_node_id(inode);
677 req->in.numargs = 1;
678 req->in.args[0].size = sizeof(*arg);
679 req->in.args[0].value = arg;
680}
681
682static int fuse_getlk(struct file *file, struct file_lock *fl)
683{
Josef Sipek7706a9d2006-12-08 02:37:02 -0800684 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi71421252006-06-25 05:48:52 -0700685 struct fuse_conn *fc = get_fuse_conn(inode);
686 struct fuse_req *req;
687 struct fuse_lk_out outarg;
688 int err;
689
690 req = fuse_get_req(fc);
691 if (IS_ERR(req))
692 return PTR_ERR(req);
693
694 fuse_lk_fill(req, file, fl, FUSE_GETLK, 0);
695 req->out.numargs = 1;
696 req->out.args[0].size = sizeof(outarg);
697 req->out.args[0].value = &outarg;
698 request_send(fc, req);
699 err = req->out.h.error;
700 fuse_put_request(fc, req);
701 if (!err)
702 err = convert_fuse_file_lock(&outarg.lk, fl);
703
704 return err;
705}
706
707static int fuse_setlk(struct file *file, struct file_lock *fl)
708{
Josef Sipek7706a9d2006-12-08 02:37:02 -0800709 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi71421252006-06-25 05:48:52 -0700710 struct fuse_conn *fc = get_fuse_conn(inode);
711 struct fuse_req *req;
712 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
713 pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0;
714 int err;
715
716 /* Unlock on close is handled by the flush method */
717 if (fl->fl_flags & FL_CLOSE)
718 return 0;
719
720 req = fuse_get_req(fc);
721 if (IS_ERR(req))
722 return PTR_ERR(req);
723
724 fuse_lk_fill(req, file, fl, opcode, pid);
725 request_send(fc, req);
726 err = req->out.h.error;
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700727 /* locking is restartable */
728 if (err == -EINTR)
729 err = -ERESTARTSYS;
Miklos Szeredi71421252006-06-25 05:48:52 -0700730 fuse_put_request(fc, req);
731 return err;
732}
733
734static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
735{
Josef Sipek7706a9d2006-12-08 02:37:02 -0800736 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi71421252006-06-25 05:48:52 -0700737 struct fuse_conn *fc = get_fuse_conn(inode);
738 int err;
739
740 if (cmd == F_GETLK) {
741 if (fc->no_lock) {
Marc Eshel9d6a8c52007-02-21 00:55:18 -0500742 posix_test_lock(file, fl);
Miklos Szeredi71421252006-06-25 05:48:52 -0700743 err = 0;
744 } else
745 err = fuse_getlk(file, fl);
746 } else {
747 if (fc->no_lock)
748 err = posix_lock_file_wait(file, fl);
749 else
750 err = fuse_setlk(file, fl);
751 }
752 return err;
753}
754
Miklos Szeredib2d22722006-12-06 20:35:51 -0800755static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
756{
757 struct inode *inode = mapping->host;
758 struct fuse_conn *fc = get_fuse_conn(inode);
759 struct fuse_req *req;
760 struct fuse_bmap_in inarg;
761 struct fuse_bmap_out outarg;
762 int err;
763
764 if (!inode->i_sb->s_bdev || fc->no_bmap)
765 return 0;
766
767 req = fuse_get_req(fc);
768 if (IS_ERR(req))
769 return 0;
770
771 memset(&inarg, 0, sizeof(inarg));
772 inarg.block = block;
773 inarg.blocksize = inode->i_sb->s_blocksize;
774 req->in.h.opcode = FUSE_BMAP;
775 req->in.h.nodeid = get_node_id(inode);
776 req->in.numargs = 1;
777 req->in.args[0].size = sizeof(inarg);
778 req->in.args[0].value = &inarg;
779 req->out.numargs = 1;
780 req->out.args[0].size = sizeof(outarg);
781 req->out.args[0].value = &outarg;
782 request_send(fc, req);
783 err = req->out.h.error;
784 fuse_put_request(fc, req);
785 if (err == -ENOSYS)
786 fc->no_bmap = 1;
787
788 return err ? 0 : outarg.block;
789}
790
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800791static const struct file_operations fuse_file_operations = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700792 .llseek = generic_file_llseek,
Badari Pulavarty543ade12006-09-30 23:28:48 -0700793 .read = do_sync_read,
794 .aio_read = generic_file_aio_read,
795 .write = do_sync_write,
796 .aio_write = generic_file_aio_write,
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700797 .mmap = fuse_file_mmap,
798 .open = fuse_open,
799 .flush = fuse_flush,
800 .release = fuse_release,
801 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -0700802 .lock = fuse_file_lock,
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700803 .sendfile = generic_file_sendfile,
804};
805
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800806static const struct file_operations fuse_direct_io_file_operations = {
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700807 .llseek = generic_file_llseek,
808 .read = fuse_direct_read,
809 .write = fuse_direct_write,
810 .open = fuse_open,
811 .flush = fuse_flush,
812 .release = fuse_release,
813 .fsync = fuse_fsync,
Miklos Szeredi71421252006-06-25 05:48:52 -0700814 .lock = fuse_file_lock,
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700815 /* no mmap and sendfile */
816};
817
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700818static const struct address_space_operations fuse_file_aops = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700819 .readpage = fuse_readpage,
820 .prepare_write = fuse_prepare_write,
821 .commit_write = fuse_commit_write,
Miklos Szeredidb50b962005-09-09 13:10:33 -0700822 .readpages = fuse_readpages,
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700823 .set_page_dirty = fuse_set_page_dirty,
Miklos Szeredib2d22722006-12-06 20:35:51 -0800824 .bmap = fuse_bmap,
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700825};
826
827void fuse_init_file_inode(struct inode *inode)
828{
Miklos Szeredi45323fb2005-09-09 13:10:37 -0700829 inode->i_fop = &fuse_file_operations;
830 inode->i_data.a_ops = &fuse_file_aops;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700831}