blob: f7b571ddf99e5a9dde4bc138b4b4d4079d549cde [file] [log] [blame]
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -07001/*
2 * linux/fs/9p/vfs_file.c
3 *
4 * This file contians vfs file ops for 9P2000.
5 *
6 * Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com>
7 * Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov>
8 *
9 * This program is free software; you can redistribute it and/or modify
Eric Van Hensbergen42e8c502006-03-25 03:07:28 -080010 * it under the terms of the GNU General Public License version 2
11 * as published by the Free Software Foundation.
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070012 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to:
20 * Free Software Foundation
21 * 51 Franklin Street, Fifth Floor
22 * Boston, MA 02111-1301 USA
23 *
24 */
25
26#include <linux/module.h>
27#include <linux/errno.h>
28#include <linux/fs.h>
Al Viro914e2632006-10-18 13:55:46 -040029#include <linux/sched.h>
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070030#include <linux/file.h>
31#include <linux/stat.h>
32#include <linux/string.h>
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070033#include <linux/inet.h>
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070034#include <linux/list.h>
Abhishek Kulkarni637d0202009-09-22 11:34:05 -050035#include <linux/pagemap.h>
M. Mohan Kumara0990272010-09-27 11:34:24 +053036#include <linux/utsname.h>
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070037#include <asm/uaccess.h>
38#include <linux/idr.h>
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050039#include <net/9p/9p.h>
40#include <net/9p/client.h>
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070041
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070042#include "v9fs.h"
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070043#include "v9fs_vfs.h"
44#include "fid.h"
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -050045#include "cache.h"
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070046
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +053047static const struct vm_operations_struct v9fs_file_vm_ops;
48
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070049/**
50 * v9fs_file_open - open a file (or directory)
51 * @inode: inode to be opened
52 * @file: file being opened
53 *
54 */
55
56int v9fs_file_open(struct inode *inode, struct file *file)
57{
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -080058 int err;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050059 struct v9fs_session_info *v9ses;
60 struct p9_fid *fid;
61 int omode;
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070062
M. Mohan Kumaref565472010-06-22 19:47:50 +053063 P9_DPRINTK(P9_DEBUG_VFS, "inode: %p file: %p\n", inode, file);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050064 v9ses = v9fs_inode2v9ses(inode);
M. Mohan Kumaref565472010-06-22 19:47:50 +053065 if (v9fs_proto_dotl(v9ses))
66 omode = file->f_flags;
67 else
68 omode = v9fs_uflags2omode(file->f_flags,
69 v9fs_proto_dotu(v9ses));
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050070 fid = file->private_data;
71 if (!fid) {
72 fid = v9fs_fid_clone(file->f_path.dentry);
73 if (IS_ERR(fid))
74 return PTR_ERR(fid);
75
76 err = p9_client_open(fid, omode);
Eric Van Hensbergen9523a842007-07-13 13:01:27 -050077 if (err < 0) {
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050078 p9_client_clunk(fid);
79 return err;
80 }
M. Mohan Kumaref565472010-06-22 19:47:50 +053081 if (file->f_flags & O_TRUNC) {
Abhishek Kulkarni7549ae32009-09-22 11:34:05 -050082 i_size_write(inode, 0);
Eric Van Hensbergen9523a842007-07-13 13:01:27 -050083 inode->i_blocks = 0;
84 }
M. Mohan Kumaref565472010-06-22 19:47:50 +053085 if ((file->f_flags & O_APPEND) &&
86 (!v9fs_proto_dotu(v9ses) && !v9fs_proto_dotl(v9ses)))
Eric Van Hensbergen2e4bef42008-06-24 17:39:39 -050087 generic_file_llseek(file, 0, SEEK_END);
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -080088 }
89
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050090 file->private_data = fid;
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +053091 if (v9ses->cache && !inode->i_private) {
92 /*
93 * clone a fid and add it to inode->i_private
94 * we do it during open time instead of
95 * page dirty time via write_begin/page_mkwrite
96 * because we want write after unlink usecase
97 * to work.
98 */
99 fid = v9fs_writeback_fid(file->f_path.dentry);
100 if (IS_ERR(fid)) {
101 err = PTR_ERR(fid);
102 goto out_error;
103 }
104 inode->i_private = (void *) fid;
105 }
Aneesh Kumar K.V29236f42011-02-28 17:03:54 +0530106#ifdef CONFIG_9P_FSCACHE
Aneesh Kumar K.V46848de2011-02-28 17:03:55 +0530107 if (v9ses->cache)
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500108 v9fs_cache_inode_set_cookie(inode, file);
Aneesh Kumar K.V29236f42011-02-28 17:03:54 +0530109#endif
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700110 return 0;
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +0530111out_error:
112 p9_client_clunk(file->private_data);
113 file->private_data = NULL;
114 return err;
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700115}
116
117/**
118 * v9fs_file_lock - lock a file (or directory)
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600119 * @filp: file to be locked
120 * @cmd: lock command
121 * @fl: file lock structure
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700122 *
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600123 * Bugs: this looks like a local only lock, we should extend into 9P
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700124 * by using open exclusive
125 */
126
127static int v9fs_file_lock(struct file *filp, int cmd, struct file_lock *fl)
128{
129 int res = 0;
Josef "Jeff" Sipekd6f787b2006-12-08 02:36:45 -0800130 struct inode *inode = filp->f_path.dentry->d_inode;
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700131
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500132 P9_DPRINTK(P9_DEBUG_VFS, "filp: %p lock: %p\n", filp, fl);
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700133
134 /* No mandatory locks */
Sachin Prabhuf78233d2010-03-13 09:03:55 -0600135 if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700136 return -ENOLCK;
137
138 if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) {
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800139 filemap_write_and_wait(inode->i_mapping);
Andrew Mortonfc0ecff2007-02-10 01:45:39 -0800140 invalidate_mapping_pages(&inode->i_data, 0, -1);
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700141 }
142
143 return res;
144}
145
M. Mohan Kumara0990272010-09-27 11:34:24 +0530146static int v9fs_file_do_lock(struct file *filp, int cmd, struct file_lock *fl)
147{
148 struct p9_flock flock;
149 struct p9_fid *fid;
150 uint8_t status;
151 int res = 0;
152 unsigned char fl_type;
153
154 fid = filp->private_data;
155 BUG_ON(fid == NULL);
156
157 if ((fl->fl_flags & FL_POSIX) != FL_POSIX)
158 BUG();
159
160 res = posix_lock_file_wait(filp, fl);
161 if (res < 0)
162 goto out;
163
164 /* convert posix lock to p9 tlock args */
165 memset(&flock, 0, sizeof(flock));
166 flock.type = fl->fl_type;
167 flock.start = fl->fl_start;
168 if (fl->fl_end == OFFSET_MAX)
169 flock.length = 0;
170 else
171 flock.length = fl->fl_end - fl->fl_start + 1;
172 flock.proc_id = fl->fl_pid;
173 flock.client_id = utsname()->nodename;
174 if (IS_SETLKW(cmd))
175 flock.flags = P9_LOCK_FLAGS_BLOCK;
176
177 /*
178 * if its a blocked request and we get P9_LOCK_BLOCKED as the status
179 * for lock request, keep on trying
180 */
181 for (;;) {
182 res = p9_client_lock_dotl(fid, &flock, &status);
183 if (res < 0)
184 break;
185
186 if (status != P9_LOCK_BLOCKED)
187 break;
188 if (status == P9_LOCK_BLOCKED && !IS_SETLKW(cmd))
189 break;
190 schedule_timeout_interruptible(P9_LOCK_TIMEOUT);
191 }
192
193 /* map 9p status to VFS status */
194 switch (status) {
195 case P9_LOCK_SUCCESS:
196 res = 0;
197 break;
198 case P9_LOCK_BLOCKED:
199 res = -EAGAIN;
200 break;
201 case P9_LOCK_ERROR:
202 case P9_LOCK_GRACE:
203 res = -ENOLCK;
204 break;
205 default:
206 BUG();
207 }
208
209 /*
210 * incase server returned error for lock request, revert
211 * it locally
212 */
213 if (res < 0 && fl->fl_type != F_UNLCK) {
214 fl_type = fl->fl_type;
215 fl->fl_type = F_UNLCK;
216 res = posix_lock_file_wait(filp, fl);
217 fl->fl_type = fl_type;
218 }
219out:
220 return res;
221}
222
M. Mohan Kumar1d769cd2010-09-27 12:22:13 +0530223static int v9fs_file_getlock(struct file *filp, struct file_lock *fl)
224{
225 struct p9_getlock glock;
226 struct p9_fid *fid;
227 int res = 0;
228
229 fid = filp->private_data;
230 BUG_ON(fid == NULL);
231
232 posix_test_lock(filp, fl);
233 /*
234 * if we have a conflicting lock locally, no need to validate
235 * with server
236 */
237 if (fl->fl_type != F_UNLCK)
238 return res;
239
240 /* convert posix lock to p9 tgetlock args */
241 memset(&glock, 0, sizeof(glock));
242 glock.type = fl->fl_type;
243 glock.start = fl->fl_start;
244 if (fl->fl_end == OFFSET_MAX)
245 glock.length = 0;
246 else
247 glock.length = fl->fl_end - fl->fl_start + 1;
248 glock.proc_id = fl->fl_pid;
249 glock.client_id = utsname()->nodename;
250
251 res = p9_client_getlock_dotl(fid, &glock);
252 if (res < 0)
253 return res;
254 if (glock.type != F_UNLCK) {
255 fl->fl_type = glock.type;
256 fl->fl_start = glock.start;
257 if (glock.length == 0)
258 fl->fl_end = OFFSET_MAX;
259 else
260 fl->fl_end = glock.start + glock.length - 1;
261 fl->fl_pid = glock.proc_id;
262 } else
263 fl->fl_type = F_UNLCK;
264
265 return res;
266}
267
M. Mohan Kumara0990272010-09-27 11:34:24 +0530268/**
269 * v9fs_file_lock_dotl - lock a file (or directory)
270 * @filp: file to be locked
271 * @cmd: lock command
272 * @fl: file lock structure
273 *
274 */
275
276static int v9fs_file_lock_dotl(struct file *filp, int cmd, struct file_lock *fl)
277{
278 struct inode *inode = filp->f_path.dentry->d_inode;
279 int ret = -ENOLCK;
280
281 P9_DPRINTK(P9_DEBUG_VFS, "filp: %p cmd:%d lock: %p name: %s\n", filp,
282 cmd, fl, filp->f_path.dentry->d_name.name);
283
284 /* No mandatory locks */
285 if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
286 goto out_err;
287
288 if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) {
289 filemap_write_and_wait(inode->i_mapping);
290 invalidate_mapping_pages(&inode->i_data, 0, -1);
291 }
292
293 if (IS_SETLK(cmd) || IS_SETLKW(cmd))
294 ret = v9fs_file_do_lock(filp, cmd, fl);
M. Mohan Kumar1d769cd2010-09-27 12:22:13 +0530295 else if (IS_GETLK(cmd))
296 ret = v9fs_file_getlock(filp, fl);
M. Mohan Kumara0990272010-09-27 11:34:24 +0530297 else
298 ret = -EINVAL;
299out_err:
300 return ret;
301}
302
303/**
304 * v9fs_file_flock_dotl - lock a file
305 * @filp: file to be locked
306 * @cmd: lock command
307 * @fl: file lock structure
308 *
309 */
310
311static int v9fs_file_flock_dotl(struct file *filp, int cmd,
312 struct file_lock *fl)
313{
314 struct inode *inode = filp->f_path.dentry->d_inode;
315 int ret = -ENOLCK;
316
317 P9_DPRINTK(P9_DEBUG_VFS, "filp: %p cmd:%d lock: %p name: %s\n", filp,
318 cmd, fl, filp->f_path.dentry->d_name.name);
319
320 /* No mandatory locks */
321 if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
322 goto out_err;
323
324 if (!(fl->fl_flags & FL_FLOCK))
325 goto out_err;
326
327 if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) {
328 filemap_write_and_wait(inode->i_mapping);
329 invalidate_mapping_pages(&inode->i_data, 0, -1);
330 }
331 /* Convert flock to posix lock */
332 fl->fl_owner = (fl_owner_t)filp;
333 fl->fl_start = 0;
334 fl->fl_end = OFFSET_MAX;
335 fl->fl_flags |= FL_POSIX;
336 fl->fl_flags ^= FL_FLOCK;
337
338 if (IS_SETLK(cmd) | IS_SETLKW(cmd))
339 ret = v9fs_file_do_lock(filp, cmd, fl);
340 else
341 ret = -EINVAL;
342out_err:
343 return ret;
344}
345
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700346/**
Aneesh Kumar K.V17311772011-02-28 17:03:56 +0530347 * v9fs_fid_readn - read from a fid
348 * @fid: fid to read
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700349 * @data: data buffer to read data into
Eric Van Hensbergenfbedadc2008-10-13 20:36:16 -0500350 * @udata: user data buffer to read data into
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700351 * @count: size of buffer
352 * @offset: offset at which to read data
353 *
354 */
Eric Van Hensbergenfbedadc2008-10-13 20:36:16 -0500355ssize_t
Aneesh Kumar K.V17311772011-02-28 17:03:56 +0530356v9fs_fid_readn(struct p9_fid *fid, char *data, char __user *udata, u32 count,
Eric Van Hensbergenfbedadc2008-10-13 20:36:16 -0500357 u64 offset)
358{
M. Mohan Kumar97e84422010-06-04 11:59:07 +0000359 int n, total, size;
Eric Van Hensbergenfbedadc2008-10-13 20:36:16 -0500360
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500361 P9_DPRINTK(P9_DEBUG_VFS, "fid %d offset %llu count %d\n", fid->fid,
Aneesh Kumar K.V17311772011-02-28 17:03:56 +0530362 (long long unsigned) offset, count);
Eric Van Hensbergenfbedadc2008-10-13 20:36:16 -0500363 n = 0;
364 total = 0;
M. Mohan Kumar97e84422010-06-04 11:59:07 +0000365 size = fid->iounit ? fid->iounit : fid->clnt->msize - P9_IOHDRSZ;
Eric Van Hensbergenfbedadc2008-10-13 20:36:16 -0500366 do {
367 n = p9_client_read(fid, data, udata, offset, count);
368 if (n <= 0)
369 break;
370
371 if (data)
372 data += n;
373 if (udata)
374 udata += n;
375
376 offset += n;
377 count -= n;
378 total += n;
M. Mohan Kumar97e84422010-06-04 11:59:07 +0000379 } while (count > 0 && n == size);
Eric Van Hensbergenfbedadc2008-10-13 20:36:16 -0500380
381 if (n < 0)
382 total = n;
383
384 return total;
385}
386
387/**
Aneesh Kumar K.V17311772011-02-28 17:03:56 +0530388 * v9fs_file_readn - read from a file
389 * @filp: file pointer to read
390 * @data: data buffer to read data into
391 * @udata: user data buffer to read data into
392 * @count: size of buffer
393 * @offset: offset at which to read data
394 *
395 */
396ssize_t
397v9fs_file_readn(struct file *filp, char *data, char __user *udata, u32 count,
398 u64 offset)
399{
400 return v9fs_fid_readn(filp->private_data, data, udata, count, offset);
401}
402
403/**
Eric Van Hensbergenfbedadc2008-10-13 20:36:16 -0500404 * v9fs_file_read - read from a file
405 * @filp: file pointer to read
406 * @udata: user data buffer to read data into
407 * @count: size of buffer
408 * @offset: offset at which to read data
409 *
410 */
411
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700412static ssize_t
Eric Van Hensbergenfbedadc2008-10-13 20:36:16 -0500413v9fs_file_read(struct file *filp, char __user *udata, size_t count,
Latchesar Ionkov19cba8a2005-10-11 08:29:03 -0700414 loff_t * offset)
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700415{
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500416 int ret;
417 struct p9_fid *fid;
M. Mohan Kumar97e84422010-06-04 11:59:07 +0000418 size_t size;
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700419
Eric Van Hensbergenea2e7992008-10-22 18:48:45 -0500420 P9_DPRINTK(P9_DEBUG_VFS, "count %zu offset %lld\n", count, *offset);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500421 fid = filp->private_data;
Eric Van Hensbergenfbedadc2008-10-13 20:36:16 -0500422
M. Mohan Kumar97e84422010-06-04 11:59:07 +0000423 size = fid->iounit ? fid->iounit : fid->clnt->msize - P9_IOHDRSZ;
424 if (count > size)
Eric Van Hensbergenfbedadc2008-10-13 20:36:16 -0500425 ret = v9fs_file_readn(filp, NULL, udata, count, *offset);
426 else
427 ret = p9_client_read(fid, NULL, udata, *offset, count);
428
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500429 if (ret > 0)
430 *offset += ret;
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700431
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500432 return ret;
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700433}
434
Aneesh Kumar K.V17311772011-02-28 17:03:56 +0530435ssize_t
436v9fs_file_write_internal(struct inode *inode, struct p9_fid *fid,
437 const char __user *data, size_t count,
438 loff_t *offset, int invalidate)
439{
440 int n;
441 size_t total = 0;
442 struct p9_client *clnt;
443 loff_t origin = *offset;
444 unsigned long pg_start, pg_end;
445
446 P9_DPRINTK(P9_DEBUG_VFS, "data %p count %d offset %x\n", data,
447 (int)count, (int)*offset);
448
449 clnt = fid->clnt;
450 do {
451 n = p9_client_write(fid, NULL, data+total, origin+total, count);
452 if (n <= 0)
453 break;
454 count -= n;
455 total += n;
456 } while (count > 0);
457
458 if (invalidate && (total > 0)) {
459 pg_start = origin >> PAGE_CACHE_SHIFT;
460 pg_end = (origin + total - 1) >> PAGE_CACHE_SHIFT;
461 if (inode->i_mapping && inode->i_mapping->nrpages)
462 invalidate_inode_pages2_range(inode->i_mapping,
463 pg_start, pg_end);
464 *offset += total;
465 i_size_write(inode, i_size_read(inode) + total);
466 inode->i_blocks = (i_size_read(inode) + 512 - 1) >> 9;
467 }
468 if (n < 0)
469 return n;
470
471 return total;
472}
473
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700474/**
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700475 * v9fs_file_write - write to a file
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600476 * @filp: file pointer to write
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700477 * @data: data buffer to write data from
478 * @count: size of buffer
479 * @offset: offset at which to write data
480 *
481 */
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700482static ssize_t
483v9fs_file_write(struct file *filp, const char __user * data,
Aneesh Kumar K.V17311772011-02-28 17:03:56 +0530484 size_t count, loff_t *offset)
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700485{
Aneesh Kumar K.V17311772011-02-28 17:03:56 +0530486 ssize_t retval = 0;
jvraofc0f2962010-03-08 22:07:02 +0000487 loff_t origin = *offset;
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700488
Eric Van Hensbergendfb0ec22008-10-13 20:36:16 -0500489
Harsh Prateek Bora3834b122010-08-03 11:55:40 +0000490 retval = generic_write_checks(filp, &origin, &count, 0);
491 if (retval)
492 goto out;
493
494 retval = -EINVAL;
495 if ((ssize_t) count < 0)
496 goto out;
497 retval = 0;
498 if (!count)
499 goto out;
500
Aneesh Kumar K.V17311772011-02-28 17:03:56 +0530501 return v9fs_file_write_internal(filp->f_path.dentry->d_inode,
502 filp->private_data,
503 data, count, offset, 1);
Harsh Prateek Bora3834b122010-08-03 11:55:40 +0000504out:
505 return retval;
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700506}
507
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530508
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200509static int v9fs_file_fsync(struct file *filp, int datasync)
M. Mohan Kumar7a4439c2010-02-08 15:36:48 -0600510{
511 struct p9_fid *fid;
512 struct p9_wstat wstat;
513 int retval;
514
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200515 P9_DPRINTK(P9_DEBUG_VFS, "filp %p datasync %x\n", filp, datasync);
M. Mohan Kumar7a4439c2010-02-08 15:36:48 -0600516
517 fid = filp->private_data;
518 v9fs_blank_wstat(&wstat);
519
520 retval = p9_client_wstat(fid, &wstat);
521 return retval;
522}
523
Venkateswararao Jujjuri (JV)b165d602010-10-22 10:13:12 -0700524int v9fs_file_fsync_dotl(struct file *filp, int datasync)
Venkateswararao Jujjuri (JV)920e65d2010-09-22 17:19:19 -0700525{
526 struct p9_fid *fid;
527 int retval;
528
529 P9_DPRINTK(P9_DEBUG_VFS, "v9fs_file_fsync_dotl: filp %p datasync %x\n",
530 filp, datasync);
531
532 fid = filp->private_data;
533
Venkateswararao Jujjuri (JV)b165d602010-10-22 10:13:12 -0700534 retval = p9_client_fsync(fid, datasync);
Venkateswararao Jujjuri (JV)920e65d2010-09-22 17:19:19 -0700535 return retval;
536}
537
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530538static int
539v9fs_file_mmap(struct file *file, struct vm_area_struct *vma)
540{
541 int retval;
542
543 retval = generic_file_mmap(file, vma);
544 if (!retval)
545 vma->vm_ops = &v9fs_file_vm_ops;
546
547 return retval;
548}
549
550static int
551v9fs_vm_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
552{
553 struct page *page = vmf->page;
554 struct file *filp = vma->vm_file;
555 struct inode *inode = filp->f_path.dentry->d_inode;
556
557
558 P9_DPRINTK(P9_DEBUG_VFS, "page %p fid %lx\n",
559 page, (unsigned long)filp->private_data);
560
561 /* make sure the cache has finished storing the page */
562 v9fs_fscache_wait_on_page_write(inode, page);
563 BUG_ON(!inode->i_private);
564 lock_page(page);
565 if (page->mapping != inode->i_mapping)
566 goto out_unlock;
567
568 return VM_FAULT_LOCKED;
569out_unlock:
570 unlock_page(page);
571 return VM_FAULT_NOPAGE;
572}
573
574static const struct vm_operations_struct v9fs_file_vm_ops = {
575 .fault = filemap_fault,
576 .page_mkwrite = v9fs_vm_page_mkwrite,
577};
578
Aneesh Kumar K.V29236f42011-02-28 17:03:54 +0530579const struct file_operations v9fs_cached_file_operations = {
Eric Van Hensbergene03abc02007-02-11 13:21:39 -0600580 .llseek = generic_file_llseek,
581 .read = do_sync_read,
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530582 .write = do_sync_write,
Eric Van Hensbergene03abc02007-02-11 13:21:39 -0600583 .aio_read = generic_file_aio_read,
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530584 .aio_write = generic_file_aio_write,
Eric Van Hensbergene03abc02007-02-11 13:21:39 -0600585 .open = v9fs_file_open,
586 .release = v9fs_dir_release,
587 .lock = v9fs_file_lock,
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530588 .mmap = v9fs_file_mmap,
M. Mohan Kumar7a4439c2010-02-08 15:36:48 -0600589 .fsync = v9fs_file_fsync,
Eric Van Hensbergene03abc02007-02-11 13:21:39 -0600590};
591
Aneesh Kumar K.V29236f42011-02-28 17:03:54 +0530592const struct file_operations v9fs_cached_file_operations_dotl = {
Venkateswararao Jujjuri (JV)b04faaf2010-09-22 16:30:52 -0700593 .llseek = generic_file_llseek,
594 .read = do_sync_read,
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530595 .write = do_sync_write,
Venkateswararao Jujjuri (JV)b04faaf2010-09-22 16:30:52 -0700596 .aio_read = generic_file_aio_read,
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530597 .aio_write = generic_file_aio_write,
Venkateswararao Jujjuri (JV)b04faaf2010-09-22 16:30:52 -0700598 .open = v9fs_file_open,
599 .release = v9fs_dir_release,
M. Mohan Kumara0990272010-09-27 11:34:24 +0530600 .lock = v9fs_file_lock_dotl,
601 .flock = v9fs_file_flock_dotl,
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530602 .mmap = v9fs_file_mmap,
Venkateswararao Jujjuri (JV)920e65d2010-09-22 17:19:19 -0700603 .fsync = v9fs_file_fsync_dotl,
Venkateswararao Jujjuri (JV)b04faaf2010-09-22 16:30:52 -0700604};
605
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800606const struct file_operations v9fs_file_operations = {
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700607 .llseek = generic_file_llseek,
608 .read = v9fs_file_read,
609 .write = v9fs_file_write,
610 .open = v9fs_file_open,
611 .release = v9fs_dir_release,
612 .lock = v9fs_file_lock,
Eric Van Hensbergen14b88692008-02-06 19:25:05 -0600613 .mmap = generic_file_readonly_mmap,
M. Mohan Kumar7a4439c2010-02-08 15:36:48 -0600614 .fsync = v9fs_file_fsync,
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700615};
Sripathi Kodi9b6533c2010-03-25 12:41:54 +0000616
617const struct file_operations v9fs_file_operations_dotl = {
618 .llseek = generic_file_llseek,
619 .read = v9fs_file_read,
620 .write = v9fs_file_write,
621 .open = v9fs_file_open,
622 .release = v9fs_dir_release,
M. Mohan Kumara0990272010-09-27 11:34:24 +0530623 .lock = v9fs_file_lock_dotl,
624 .flock = v9fs_file_flock_dotl,
Sripathi Kodi9b6533c2010-03-25 12:41:54 +0000625 .mmap = generic_file_readonly_mmap,
Venkateswararao Jujjuri (JV)920e65d2010-09-22 17:19:19 -0700626 .fsync = v9fs_file_fsync_dotl,
Sripathi Kodi9b6533c2010-03-25 12:41:54 +0000627};