blob: 98c4307a1f035ee08673cf25a3cff9caf4afd363 [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;
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +053059 struct v9fs_inode *v9inode;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050060 struct v9fs_session_info *v9ses;
61 struct p9_fid *fid;
62 int omode;
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070063
M. Mohan Kumaref565472010-06-22 19:47:50 +053064 P9_DPRINTK(P9_DEBUG_VFS, "inode: %p file: %p\n", inode, file);
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +053065 v9inode = V9FS_I(inode);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050066 v9ses = v9fs_inode2v9ses(inode);
M. Mohan Kumaref565472010-06-22 19:47:50 +053067 if (v9fs_proto_dotl(v9ses))
68 omode = file->f_flags;
69 else
70 omode = v9fs_uflags2omode(file->f_flags,
71 v9fs_proto_dotu(v9ses));
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050072 fid = file->private_data;
73 if (!fid) {
74 fid = v9fs_fid_clone(file->f_path.dentry);
75 if (IS_ERR(fid))
76 return PTR_ERR(fid);
77
78 err = p9_client_open(fid, omode);
Eric Van Hensbergen9523a842007-07-13 13:01:27 -050079 if (err < 0) {
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050080 p9_client_clunk(fid);
81 return err;
82 }
M. Mohan Kumaref565472010-06-22 19:47:50 +053083 if (file->f_flags & O_TRUNC) {
Abhishek Kulkarni7549ae32009-09-22 11:34:05 -050084 i_size_write(inode, 0);
Eric Van Hensbergen9523a842007-07-13 13:01:27 -050085 inode->i_blocks = 0;
86 }
M. Mohan Kumaref565472010-06-22 19:47:50 +053087 if ((file->f_flags & O_APPEND) &&
88 (!v9fs_proto_dotu(v9ses) && !v9fs_proto_dotl(v9ses)))
Eric Van Hensbergen2e4bef42008-06-24 17:39:39 -050089 generic_file_llseek(file, 0, SEEK_END);
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -080090 }
91
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050092 file->private_data = fid;
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +053093 if (v9ses->cache && !v9inode->writeback_fid) {
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +053094 /*
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +053095 * clone a fid and add it to writeback_fid
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +053096 * we do it during open time instead of
97 * page dirty time via write_begin/page_mkwrite
98 * because we want write after unlink usecase
99 * to work.
100 */
101 fid = v9fs_writeback_fid(file->f_path.dentry);
102 if (IS_ERR(fid)) {
103 err = PTR_ERR(fid);
104 goto out_error;
105 }
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530106 v9inode->writeback_fid = (void *) fid;
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +0530107 }
Aneesh Kumar K.V29236f42011-02-28 17:03:54 +0530108#ifdef CONFIG_9P_FSCACHE
Aneesh Kumar K.V46848de2011-02-28 17:03:55 +0530109 if (v9ses->cache)
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500110 v9fs_cache_inode_set_cookie(inode, file);
Aneesh Kumar K.V29236f42011-02-28 17:03:54 +0530111#endif
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700112 return 0;
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +0530113out_error:
114 p9_client_clunk(file->private_data);
115 file->private_data = NULL;
116 return err;
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700117}
118
119/**
120 * v9fs_file_lock - lock a file (or directory)
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600121 * @filp: file to be locked
122 * @cmd: lock command
123 * @fl: file lock structure
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700124 *
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600125 * Bugs: this looks like a local only lock, we should extend into 9P
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700126 * by using open exclusive
127 */
128
129static int v9fs_file_lock(struct file *filp, int cmd, struct file_lock *fl)
130{
131 int res = 0;
Josef "Jeff" Sipekd6f787b2006-12-08 02:36:45 -0800132 struct inode *inode = filp->f_path.dentry->d_inode;
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700133
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500134 P9_DPRINTK(P9_DEBUG_VFS, "filp: %p lock: %p\n", filp, fl);
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700135
136 /* No mandatory locks */
Sachin Prabhuf78233d2010-03-13 09:03:55 -0600137 if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700138 return -ENOLCK;
139
140 if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) {
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800141 filemap_write_and_wait(inode->i_mapping);
Andrew Mortonfc0ecff2007-02-10 01:45:39 -0800142 invalidate_mapping_pages(&inode->i_data, 0, -1);
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700143 }
144
145 return res;
146}
147
M. Mohan Kumara0990272010-09-27 11:34:24 +0530148static int v9fs_file_do_lock(struct file *filp, int cmd, struct file_lock *fl)
149{
150 struct p9_flock flock;
151 struct p9_fid *fid;
152 uint8_t status;
153 int res = 0;
154 unsigned char fl_type;
155
156 fid = filp->private_data;
157 BUG_ON(fid == NULL);
158
159 if ((fl->fl_flags & FL_POSIX) != FL_POSIX)
160 BUG();
161
162 res = posix_lock_file_wait(filp, fl);
163 if (res < 0)
164 goto out;
165
166 /* convert posix lock to p9 tlock args */
167 memset(&flock, 0, sizeof(flock));
168 flock.type = fl->fl_type;
169 flock.start = fl->fl_start;
170 if (fl->fl_end == OFFSET_MAX)
171 flock.length = 0;
172 else
173 flock.length = fl->fl_end - fl->fl_start + 1;
174 flock.proc_id = fl->fl_pid;
175 flock.client_id = utsname()->nodename;
176 if (IS_SETLKW(cmd))
177 flock.flags = P9_LOCK_FLAGS_BLOCK;
178
179 /*
180 * if its a blocked request and we get P9_LOCK_BLOCKED as the status
181 * for lock request, keep on trying
182 */
183 for (;;) {
184 res = p9_client_lock_dotl(fid, &flock, &status);
185 if (res < 0)
186 break;
187
188 if (status != P9_LOCK_BLOCKED)
189 break;
190 if (status == P9_LOCK_BLOCKED && !IS_SETLKW(cmd))
191 break;
192 schedule_timeout_interruptible(P9_LOCK_TIMEOUT);
193 }
194
195 /* map 9p status to VFS status */
196 switch (status) {
197 case P9_LOCK_SUCCESS:
198 res = 0;
199 break;
200 case P9_LOCK_BLOCKED:
201 res = -EAGAIN;
202 break;
203 case P9_LOCK_ERROR:
204 case P9_LOCK_GRACE:
205 res = -ENOLCK;
206 break;
207 default:
208 BUG();
209 }
210
211 /*
212 * incase server returned error for lock request, revert
213 * it locally
214 */
215 if (res < 0 && fl->fl_type != F_UNLCK) {
216 fl_type = fl->fl_type;
217 fl->fl_type = F_UNLCK;
218 res = posix_lock_file_wait(filp, fl);
219 fl->fl_type = fl_type;
220 }
221out:
222 return res;
223}
224
M. Mohan Kumar1d769cd2010-09-27 12:22:13 +0530225static int v9fs_file_getlock(struct file *filp, struct file_lock *fl)
226{
227 struct p9_getlock glock;
228 struct p9_fid *fid;
229 int res = 0;
230
231 fid = filp->private_data;
232 BUG_ON(fid == NULL);
233
234 posix_test_lock(filp, fl);
235 /*
236 * if we have a conflicting lock locally, no need to validate
237 * with server
238 */
239 if (fl->fl_type != F_UNLCK)
240 return res;
241
242 /* convert posix lock to p9 tgetlock args */
243 memset(&glock, 0, sizeof(glock));
244 glock.type = fl->fl_type;
245 glock.start = fl->fl_start;
246 if (fl->fl_end == OFFSET_MAX)
247 glock.length = 0;
248 else
249 glock.length = fl->fl_end - fl->fl_start + 1;
250 glock.proc_id = fl->fl_pid;
251 glock.client_id = utsname()->nodename;
252
253 res = p9_client_getlock_dotl(fid, &glock);
254 if (res < 0)
255 return res;
256 if (glock.type != F_UNLCK) {
257 fl->fl_type = glock.type;
258 fl->fl_start = glock.start;
259 if (glock.length == 0)
260 fl->fl_end = OFFSET_MAX;
261 else
262 fl->fl_end = glock.start + glock.length - 1;
263 fl->fl_pid = glock.proc_id;
264 } else
265 fl->fl_type = F_UNLCK;
266
267 return res;
268}
269
M. Mohan Kumara0990272010-09-27 11:34:24 +0530270/**
271 * v9fs_file_lock_dotl - lock a file (or directory)
272 * @filp: file to be locked
273 * @cmd: lock command
274 * @fl: file lock structure
275 *
276 */
277
278static int v9fs_file_lock_dotl(struct file *filp, int cmd, struct file_lock *fl)
279{
280 struct inode *inode = filp->f_path.dentry->d_inode;
281 int ret = -ENOLCK;
282
283 P9_DPRINTK(P9_DEBUG_VFS, "filp: %p cmd:%d lock: %p name: %s\n", filp,
284 cmd, fl, filp->f_path.dentry->d_name.name);
285
286 /* No mandatory locks */
287 if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
288 goto out_err;
289
290 if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) {
291 filemap_write_and_wait(inode->i_mapping);
292 invalidate_mapping_pages(&inode->i_data, 0, -1);
293 }
294
295 if (IS_SETLK(cmd) || IS_SETLKW(cmd))
296 ret = v9fs_file_do_lock(filp, cmd, fl);
M. Mohan Kumar1d769cd2010-09-27 12:22:13 +0530297 else if (IS_GETLK(cmd))
298 ret = v9fs_file_getlock(filp, fl);
M. Mohan Kumara0990272010-09-27 11:34:24 +0530299 else
300 ret = -EINVAL;
301out_err:
302 return ret;
303}
304
305/**
306 * v9fs_file_flock_dotl - lock a file
307 * @filp: file to be locked
308 * @cmd: lock command
309 * @fl: file lock structure
310 *
311 */
312
313static int v9fs_file_flock_dotl(struct file *filp, int cmd,
314 struct file_lock *fl)
315{
316 struct inode *inode = filp->f_path.dentry->d_inode;
317 int ret = -ENOLCK;
318
319 P9_DPRINTK(P9_DEBUG_VFS, "filp: %p cmd:%d lock: %p name: %s\n", filp,
320 cmd, fl, filp->f_path.dentry->d_name.name);
321
322 /* No mandatory locks */
323 if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
324 goto out_err;
325
326 if (!(fl->fl_flags & FL_FLOCK))
327 goto out_err;
328
329 if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) {
330 filemap_write_and_wait(inode->i_mapping);
331 invalidate_mapping_pages(&inode->i_data, 0, -1);
332 }
333 /* Convert flock to posix lock */
334 fl->fl_owner = (fl_owner_t)filp;
335 fl->fl_start = 0;
336 fl->fl_end = OFFSET_MAX;
337 fl->fl_flags |= FL_POSIX;
338 fl->fl_flags ^= FL_FLOCK;
339
340 if (IS_SETLK(cmd) | IS_SETLKW(cmd))
341 ret = v9fs_file_do_lock(filp, cmd, fl);
342 else
343 ret = -EINVAL;
344out_err:
345 return ret;
346}
347
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700348/**
Aneesh Kumar K.V17311772011-02-28 17:03:56 +0530349 * v9fs_fid_readn - read from a fid
350 * @fid: fid to read
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700351 * @data: data buffer to read data into
Eric Van Hensbergenfbedadc2008-10-13 20:36:16 -0500352 * @udata: user data buffer to read data into
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700353 * @count: size of buffer
354 * @offset: offset at which to read data
355 *
356 */
Eric Van Hensbergenfbedadc2008-10-13 20:36:16 -0500357ssize_t
Aneesh Kumar K.V17311772011-02-28 17:03:56 +0530358v9fs_fid_readn(struct p9_fid *fid, char *data, char __user *udata, u32 count,
Eric Van Hensbergenfbedadc2008-10-13 20:36:16 -0500359 u64 offset)
360{
M. Mohan Kumar97e84422010-06-04 11:59:07 +0000361 int n, total, size;
Eric Van Hensbergenfbedadc2008-10-13 20:36:16 -0500362
Eric Van Hensbergen51a87c52008-10-16 08:30:07 -0500363 P9_DPRINTK(P9_DEBUG_VFS, "fid %d offset %llu count %d\n", fid->fid,
Aneesh Kumar K.V17311772011-02-28 17:03:56 +0530364 (long long unsigned) offset, count);
Eric Van Hensbergenfbedadc2008-10-13 20:36:16 -0500365 n = 0;
366 total = 0;
M. Mohan Kumar97e84422010-06-04 11:59:07 +0000367 size = fid->iounit ? fid->iounit : fid->clnt->msize - P9_IOHDRSZ;
Eric Van Hensbergenfbedadc2008-10-13 20:36:16 -0500368 do {
369 n = p9_client_read(fid, data, udata, offset, count);
370 if (n <= 0)
371 break;
372
373 if (data)
374 data += n;
375 if (udata)
376 udata += n;
377
378 offset += n;
379 count -= n;
380 total += n;
M. Mohan Kumar97e84422010-06-04 11:59:07 +0000381 } while (count > 0 && n == size);
Eric Van Hensbergenfbedadc2008-10-13 20:36:16 -0500382
383 if (n < 0)
384 total = n;
385
386 return total;
387}
388
389/**
Aneesh Kumar K.V17311772011-02-28 17:03:56 +0530390 * v9fs_file_readn - read from a file
391 * @filp: file pointer to read
392 * @data: data buffer to read data into
393 * @udata: user data buffer to read data into
394 * @count: size of buffer
395 * @offset: offset at which to read data
396 *
397 */
398ssize_t
399v9fs_file_readn(struct file *filp, char *data, char __user *udata, u32 count,
400 u64 offset)
401{
402 return v9fs_fid_readn(filp->private_data, data, udata, count, offset);
403}
404
405/**
Eric Van Hensbergenfbedadc2008-10-13 20:36:16 -0500406 * v9fs_file_read - read from a file
407 * @filp: file pointer to read
408 * @udata: user data buffer to read data into
409 * @count: size of buffer
410 * @offset: offset at which to read data
411 *
412 */
413
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700414static ssize_t
Eric Van Hensbergenfbedadc2008-10-13 20:36:16 -0500415v9fs_file_read(struct file *filp, char __user *udata, size_t count,
Latchesar Ionkov19cba8a2005-10-11 08:29:03 -0700416 loff_t * offset)
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700417{
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500418 int ret;
419 struct p9_fid *fid;
M. Mohan Kumar97e84422010-06-04 11:59:07 +0000420 size_t size;
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700421
Eric Van Hensbergenea2e7992008-10-22 18:48:45 -0500422 P9_DPRINTK(P9_DEBUG_VFS, "count %zu offset %lld\n", count, *offset);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500423 fid = filp->private_data;
Eric Van Hensbergenfbedadc2008-10-13 20:36:16 -0500424
M. Mohan Kumar97e84422010-06-04 11:59:07 +0000425 size = fid->iounit ? fid->iounit : fid->clnt->msize - P9_IOHDRSZ;
426 if (count > size)
Eric Van Hensbergenfbedadc2008-10-13 20:36:16 -0500427 ret = v9fs_file_readn(filp, NULL, udata, count, *offset);
428 else
429 ret = p9_client_read(fid, NULL, udata, *offset, count);
430
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500431 if (ret > 0)
432 *offset += ret;
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700433
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500434 return ret;
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700435}
436
Aneesh Kumar K.V17311772011-02-28 17:03:56 +0530437ssize_t
438v9fs_file_write_internal(struct inode *inode, struct p9_fid *fid,
439 const char __user *data, size_t count,
440 loff_t *offset, int invalidate)
441{
442 int n;
443 size_t total = 0;
444 struct p9_client *clnt;
445 loff_t origin = *offset;
446 unsigned long pg_start, pg_end;
447
448 P9_DPRINTK(P9_DEBUG_VFS, "data %p count %d offset %x\n", data,
449 (int)count, (int)*offset);
450
451 clnt = fid->clnt;
452 do {
453 n = p9_client_write(fid, NULL, data+total, origin+total, count);
454 if (n <= 0)
455 break;
456 count -= n;
457 total += n;
458 } while (count > 0);
459
460 if (invalidate && (total > 0)) {
461 pg_start = origin >> PAGE_CACHE_SHIFT;
462 pg_end = (origin + total - 1) >> PAGE_CACHE_SHIFT;
463 if (inode->i_mapping && inode->i_mapping->nrpages)
464 invalidate_inode_pages2_range(inode->i_mapping,
465 pg_start, pg_end);
466 *offset += total;
467 i_size_write(inode, i_size_read(inode) + total);
468 inode->i_blocks = (i_size_read(inode) + 512 - 1) >> 9;
469 }
470 if (n < 0)
471 return n;
472
473 return total;
474}
475
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700476/**
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700477 * v9fs_file_write - write to a file
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600478 * @filp: file pointer to write
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700479 * @data: data buffer to write data from
480 * @count: size of buffer
481 * @offset: offset at which to write data
482 *
483 */
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700484static ssize_t
485v9fs_file_write(struct file *filp, const char __user * data,
Aneesh Kumar K.V17311772011-02-28 17:03:56 +0530486 size_t count, loff_t *offset)
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700487{
Aneesh Kumar K.V17311772011-02-28 17:03:56 +0530488 ssize_t retval = 0;
jvraofc0f2962010-03-08 22:07:02 +0000489 loff_t origin = *offset;
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700490
Eric Van Hensbergendfb0ec22008-10-13 20:36:16 -0500491
Harsh Prateek Bora3834b122010-08-03 11:55:40 +0000492 retval = generic_write_checks(filp, &origin, &count, 0);
493 if (retval)
494 goto out;
495
496 retval = -EINVAL;
497 if ((ssize_t) count < 0)
498 goto out;
499 retval = 0;
500 if (!count)
501 goto out;
502
Aneesh Kumar K.V17311772011-02-28 17:03:56 +0530503 return v9fs_file_write_internal(filp->f_path.dentry->d_inode,
504 filp->private_data,
505 data, count, offset, 1);
Harsh Prateek Bora3834b122010-08-03 11:55:40 +0000506out:
507 return retval;
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700508}
509
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530510
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200511static int v9fs_file_fsync(struct file *filp, int datasync)
M. Mohan Kumar7a4439c2010-02-08 15:36:48 -0600512{
513 struct p9_fid *fid;
514 struct p9_wstat wstat;
515 int retval;
516
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200517 P9_DPRINTK(P9_DEBUG_VFS, "filp %p datasync %x\n", filp, datasync);
M. Mohan Kumar7a4439c2010-02-08 15:36:48 -0600518
519 fid = filp->private_data;
520 v9fs_blank_wstat(&wstat);
521
522 retval = p9_client_wstat(fid, &wstat);
523 return retval;
524}
525
Venkateswararao Jujjuri (JV)b165d602010-10-22 10:13:12 -0700526int v9fs_file_fsync_dotl(struct file *filp, int datasync)
Venkateswararao Jujjuri (JV)920e65d2010-09-22 17:19:19 -0700527{
528 struct p9_fid *fid;
529 int retval;
530
531 P9_DPRINTK(P9_DEBUG_VFS, "v9fs_file_fsync_dotl: filp %p datasync %x\n",
532 filp, datasync);
533
534 fid = filp->private_data;
535
Venkateswararao Jujjuri (JV)b165d602010-10-22 10:13:12 -0700536 retval = p9_client_fsync(fid, datasync);
Venkateswararao Jujjuri (JV)920e65d2010-09-22 17:19:19 -0700537 return retval;
538}
539
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530540static int
541v9fs_file_mmap(struct file *file, struct vm_area_struct *vma)
542{
543 int retval;
544
545 retval = generic_file_mmap(file, vma);
546 if (!retval)
547 vma->vm_ops = &v9fs_file_vm_ops;
548
549 return retval;
550}
551
552static int
553v9fs_vm_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
554{
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530555 struct v9fs_inode *v9inode;
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530556 struct page *page = vmf->page;
557 struct file *filp = vma->vm_file;
558 struct inode *inode = filp->f_path.dentry->d_inode;
559
560
561 P9_DPRINTK(P9_DEBUG_VFS, "page %p fid %lx\n",
562 page, (unsigned long)filp->private_data);
563
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530564 v9inode = V9FS_I(inode);
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530565 /* make sure the cache has finished storing the page */
566 v9fs_fscache_wait_on_page_write(inode, page);
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530567 BUG_ON(!v9inode->writeback_fid);
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530568 lock_page(page);
569 if (page->mapping != inode->i_mapping)
570 goto out_unlock;
571
572 return VM_FAULT_LOCKED;
573out_unlock:
574 unlock_page(page);
575 return VM_FAULT_NOPAGE;
576}
577
578static const struct vm_operations_struct v9fs_file_vm_ops = {
579 .fault = filemap_fault,
580 .page_mkwrite = v9fs_vm_page_mkwrite,
581};
582
Aneesh Kumar K.V29236f42011-02-28 17:03:54 +0530583const struct file_operations v9fs_cached_file_operations = {
Eric Van Hensbergene03abc02007-02-11 13:21:39 -0600584 .llseek = generic_file_llseek,
585 .read = do_sync_read,
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530586 .write = do_sync_write,
Eric Van Hensbergene03abc02007-02-11 13:21:39 -0600587 .aio_read = generic_file_aio_read,
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530588 .aio_write = generic_file_aio_write,
Eric Van Hensbergene03abc02007-02-11 13:21:39 -0600589 .open = v9fs_file_open,
590 .release = v9fs_dir_release,
591 .lock = v9fs_file_lock,
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530592 .mmap = v9fs_file_mmap,
M. Mohan Kumar7a4439c2010-02-08 15:36:48 -0600593 .fsync = v9fs_file_fsync,
Eric Van Hensbergene03abc02007-02-11 13:21:39 -0600594};
595
Aneesh Kumar K.V29236f42011-02-28 17:03:54 +0530596const struct file_operations v9fs_cached_file_operations_dotl = {
Venkateswararao Jujjuri (JV)b04faaf2010-09-22 16:30:52 -0700597 .llseek = generic_file_llseek,
598 .read = do_sync_read,
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530599 .write = do_sync_write,
Venkateswararao Jujjuri (JV)b04faaf2010-09-22 16:30:52 -0700600 .aio_read = generic_file_aio_read,
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530601 .aio_write = generic_file_aio_write,
Venkateswararao Jujjuri (JV)b04faaf2010-09-22 16:30:52 -0700602 .open = v9fs_file_open,
603 .release = v9fs_dir_release,
M. Mohan Kumara0990272010-09-27 11:34:24 +0530604 .lock = v9fs_file_lock_dotl,
605 .flock = v9fs_file_flock_dotl,
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530606 .mmap = v9fs_file_mmap,
Venkateswararao Jujjuri (JV)920e65d2010-09-22 17:19:19 -0700607 .fsync = v9fs_file_fsync_dotl,
Venkateswararao Jujjuri (JV)b04faaf2010-09-22 16:30:52 -0700608};
609
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800610const struct file_operations v9fs_file_operations = {
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700611 .llseek = generic_file_llseek,
612 .read = v9fs_file_read,
613 .write = v9fs_file_write,
614 .open = v9fs_file_open,
615 .release = v9fs_dir_release,
616 .lock = v9fs_file_lock,
Eric Van Hensbergen14b88692008-02-06 19:25:05 -0600617 .mmap = generic_file_readonly_mmap,
M. Mohan Kumar7a4439c2010-02-08 15:36:48 -0600618 .fsync = v9fs_file_fsync,
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700619};
Sripathi Kodi9b6533c2010-03-25 12:41:54 +0000620
621const struct file_operations v9fs_file_operations_dotl = {
622 .llseek = generic_file_llseek,
623 .read = v9fs_file_read,
624 .write = v9fs_file_write,
625 .open = v9fs_file_open,
626 .release = v9fs_dir_release,
M. Mohan Kumara0990272010-09-27 11:34:24 +0530627 .lock = v9fs_file_lock_dotl,
628 .flock = v9fs_file_flock_dotl,
Sripathi Kodi9b6533c2010-03-25 12:41:54 +0000629 .mmap = generic_file_readonly_mmap,
Venkateswararao Jujjuri (JV)920e65d2010-09-22 17:19:19 -0700630 .fsync = v9fs_file_fsync_dotl,
Sripathi Kodi9b6533c2010-03-25 12:41:54 +0000631};