blob: 069f70d3758fcca2f1fcf10405223575c591b6f4 [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>
Al Viro070b3652015-04-01 20:17:51 -040039#include <linux/uio.h>
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050040#include <net/9p/9p.h>
41#include <net/9p/client.h>
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070042
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070043#include "v9fs.h"
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070044#include "v9fs_vfs.h"
45#include "fid.h"
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -050046#include "cache.h"
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070047
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +053048static const struct vm_operations_struct v9fs_file_vm_ops;
Dominique Martinetfb89b452014-01-10 13:44:09 +010049static const struct vm_operations_struct v9fs_mmap_file_vm_ops;
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +053050
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070051/**
52 * v9fs_file_open - open a file (or directory)
53 * @inode: inode to be opened
54 * @file: file being opened
55 *
56 */
57
58int v9fs_file_open(struct inode *inode, struct file *file)
59{
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -080060 int err;
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +053061 struct v9fs_inode *v9inode;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050062 struct v9fs_session_info *v9ses;
63 struct p9_fid *fid;
64 int omode;
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070065
Joe Perches5d385152011-11-28 10:40:46 -080066 p9_debug(P9_DEBUG_VFS, "inode: %p file: %p\n", inode, file);
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +053067 v9inode = V9FS_I(inode);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050068 v9ses = v9fs_inode2v9ses(inode);
M. Mohan Kumaref565472010-06-22 19:47:50 +053069 if (v9fs_proto_dotl(v9ses))
Aneesh Kumar K.Vf88657c2011-08-03 19:55:32 +053070 omode = v9fs_open_to_dotl_flags(file->f_flags);
M. Mohan Kumaref565472010-06-22 19:47:50 +053071 else
72 omode = v9fs_uflags2omode(file->f_flags,
73 v9fs_proto_dotu(v9ses));
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050074 fid = file->private_data;
75 if (!fid) {
76 fid = v9fs_fid_clone(file->f_path.dentry);
77 if (IS_ERR(fid))
78 return PTR_ERR(fid);
79
80 err = p9_client_open(fid, omode);
Eric Van Hensbergen9523a842007-07-13 13:01:27 -050081 if (err < 0) {
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050082 p9_client_clunk(fid);
83 return err;
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.V5a7e0a82011-03-08 16:39:46 +053091 mutex_lock(&v9inode->v_mutex);
Dominique Martinetfb89b452014-01-10 13:44:09 +010092 if ((v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) &&
93 !v9inode->writeback_fid &&
Aneesh Kumar K.V7add6972011-03-08 16:39:49 +053094 ((file->f_flags & O_ACCMODE) != O_RDONLY)) {
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +053095 /*
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +053096 * clone a fid and add it to writeback_fid
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +053097 * we do it during open time instead of
98 * page dirty time via write_begin/page_mkwrite
99 * because we want write after unlink usecase
100 * to work.
101 */
102 fid = v9fs_writeback_fid(file->f_path.dentry);
103 if (IS_ERR(fid)) {
104 err = PTR_ERR(fid);
Aneesh Kumar K.V5a7e0a82011-03-08 16:39:46 +0530105 mutex_unlock(&v9inode->v_mutex);
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +0530106 goto out_error;
107 }
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530108 v9inode->writeback_fid = (void *) fid;
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +0530109 }
Aneesh Kumar K.V5a7e0a82011-03-08 16:39:46 +0530110 mutex_unlock(&v9inode->v_mutex);
Dominique Martinetfb89b452014-01-10 13:44:09 +0100111 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE)
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500112 v9fs_cache_inode_set_cookie(inode, file);
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700113 return 0;
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +0530114out_error:
115 p9_client_clunk(file->private_data);
116 file->private_data = NULL;
117 return err;
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700118}
119
120/**
121 * v9fs_file_lock - lock a file (or directory)
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600122 * @filp: file to be locked
123 * @cmd: lock command
124 * @fl: file lock structure
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700125 *
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600126 * Bugs: this looks like a local only lock, we should extend into 9P
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700127 * by using open exclusive
128 */
129
130static int v9fs_file_lock(struct file *filp, int cmd, struct file_lock *fl)
131{
132 int res = 0;
Al Viro496ad9a2013-01-23 17:07:38 -0500133 struct inode *inode = file_inode(filp);
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700134
Joe Perches5d385152011-11-28 10:40:46 -0800135 p9_debug(P9_DEBUG_VFS, "filp: %p lock: %p\n", filp, fl);
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700136
137 /* No mandatory locks */
Sachin Prabhuf78233d2010-03-13 09:03:55 -0600138 if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700139 return -ENOLCK;
140
141 if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) {
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800142 filemap_write_and_wait(inode->i_mapping);
Andrew Mortonfc0ecff2007-02-10 01:45:39 -0800143 invalidate_mapping_pages(&inode->i_data, 0, -1);
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700144 }
145
146 return res;
147}
148
M. Mohan Kumara0990272010-09-27 11:34:24 +0530149static int v9fs_file_do_lock(struct file *filp, int cmd, struct file_lock *fl)
150{
151 struct p9_flock flock;
152 struct p9_fid *fid;
153 uint8_t status;
154 int res = 0;
155 unsigned char fl_type;
156
157 fid = filp->private_data;
158 BUG_ON(fid == NULL);
159
160 if ((fl->fl_flags & FL_POSIX) != FL_POSIX)
161 BUG();
162
163 res = posix_lock_file_wait(filp, fl);
164 if (res < 0)
165 goto out;
166
167 /* convert posix lock to p9 tlock args */
168 memset(&flock, 0, sizeof(flock));
Jim Garlick51b8b4f2011-08-21 00:21:18 +0530169 /* map the lock type */
170 switch (fl->fl_type) {
171 case F_RDLCK:
172 flock.type = P9_LOCK_TYPE_RDLCK;
173 break;
174 case F_WRLCK:
175 flock.type = P9_LOCK_TYPE_WRLCK;
176 break;
177 case F_UNLCK:
178 flock.type = P9_LOCK_TYPE_UNLCK;
179 break;
180 }
M. Mohan Kumara0990272010-09-27 11:34:24 +0530181 flock.start = fl->fl_start;
182 if (fl->fl_end == OFFSET_MAX)
183 flock.length = 0;
184 else
185 flock.length = fl->fl_end - fl->fl_start + 1;
186 flock.proc_id = fl->fl_pid;
Will Deacon50192ab2013-08-21 18:24:47 +0100187 flock.client_id = fid->clnt->name;
M. Mohan Kumara0990272010-09-27 11:34:24 +0530188 if (IS_SETLKW(cmd))
189 flock.flags = P9_LOCK_FLAGS_BLOCK;
190
191 /*
192 * if its a blocked request and we get P9_LOCK_BLOCKED as the status
193 * for lock request, keep on trying
194 */
195 for (;;) {
196 res = p9_client_lock_dotl(fid, &flock, &status);
197 if (res < 0)
198 break;
199
200 if (status != P9_LOCK_BLOCKED)
201 break;
202 if (status == P9_LOCK_BLOCKED && !IS_SETLKW(cmd))
203 break;
Jim Garlicka0ea7872012-01-03 15:27:50 -0600204 if (schedule_timeout_interruptible(P9_LOCK_TIMEOUT) != 0)
205 break;
M. Mohan Kumara0990272010-09-27 11:34:24 +0530206 }
207
208 /* map 9p status to VFS status */
209 switch (status) {
210 case P9_LOCK_SUCCESS:
211 res = 0;
212 break;
213 case P9_LOCK_BLOCKED:
214 res = -EAGAIN;
215 break;
216 case P9_LOCK_ERROR:
217 case P9_LOCK_GRACE:
218 res = -ENOLCK;
219 break;
220 default:
221 BUG();
222 }
223
224 /*
225 * incase server returned error for lock request, revert
226 * it locally
227 */
228 if (res < 0 && fl->fl_type != F_UNLCK) {
229 fl_type = fl->fl_type;
230 fl->fl_type = F_UNLCK;
231 res = posix_lock_file_wait(filp, fl);
232 fl->fl_type = fl_type;
233 }
234out:
235 return res;
236}
237
M. Mohan Kumar1d769cd2010-09-27 12:22:13 +0530238static int v9fs_file_getlock(struct file *filp, struct file_lock *fl)
239{
240 struct p9_getlock glock;
241 struct p9_fid *fid;
242 int res = 0;
243
244 fid = filp->private_data;
245 BUG_ON(fid == NULL);
246
247 posix_test_lock(filp, fl);
248 /*
249 * if we have a conflicting lock locally, no need to validate
250 * with server
251 */
252 if (fl->fl_type != F_UNLCK)
253 return res;
254
255 /* convert posix lock to p9 tgetlock args */
256 memset(&glock, 0, sizeof(glock));
Jim Garlick51b8b4f2011-08-21 00:21:18 +0530257 glock.type = P9_LOCK_TYPE_UNLCK;
M. Mohan Kumar1d769cd2010-09-27 12:22:13 +0530258 glock.start = fl->fl_start;
259 if (fl->fl_end == OFFSET_MAX)
260 glock.length = 0;
261 else
262 glock.length = fl->fl_end - fl->fl_start + 1;
263 glock.proc_id = fl->fl_pid;
Will Deacon50192ab2013-08-21 18:24:47 +0100264 glock.client_id = fid->clnt->name;
M. Mohan Kumar1d769cd2010-09-27 12:22:13 +0530265
266 res = p9_client_getlock_dotl(fid, &glock);
267 if (res < 0)
268 return res;
Jim Garlick51b8b4f2011-08-21 00:21:18 +0530269 /* map 9p lock type to os lock type */
270 switch (glock.type) {
271 case P9_LOCK_TYPE_RDLCK:
272 fl->fl_type = F_RDLCK;
273 break;
274 case P9_LOCK_TYPE_WRLCK:
275 fl->fl_type = F_WRLCK;
276 break;
277 case P9_LOCK_TYPE_UNLCK:
278 fl->fl_type = F_UNLCK;
279 break;
280 }
281 if (glock.type != P9_LOCK_TYPE_UNLCK) {
M. Mohan Kumar1d769cd2010-09-27 12:22:13 +0530282 fl->fl_start = glock.start;
283 if (glock.length == 0)
284 fl->fl_end = OFFSET_MAX;
285 else
286 fl->fl_end = glock.start + glock.length - 1;
287 fl->fl_pid = glock.proc_id;
Jim Garlick51b8b4f2011-08-21 00:21:18 +0530288 }
M. Mohan Kumar1d769cd2010-09-27 12:22:13 +0530289 return res;
290}
291
M. Mohan Kumara0990272010-09-27 11:34:24 +0530292/**
293 * v9fs_file_lock_dotl - lock a file (or directory)
294 * @filp: file to be locked
295 * @cmd: lock command
296 * @fl: file lock structure
297 *
298 */
299
300static int v9fs_file_lock_dotl(struct file *filp, int cmd, struct file_lock *fl)
301{
Al Viro496ad9a2013-01-23 17:07:38 -0500302 struct inode *inode = file_inode(filp);
M. Mohan Kumara0990272010-09-27 11:34:24 +0530303 int ret = -ENOLCK;
304
Al Viro4b8e9922014-08-19 20:17:38 -0400305 p9_debug(P9_DEBUG_VFS, "filp: %p cmd:%d lock: %p name: %pD\n",
306 filp, cmd, fl, filp);
M. Mohan Kumara0990272010-09-27 11:34:24 +0530307
308 /* No mandatory locks */
309 if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
310 goto out_err;
311
312 if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) {
313 filemap_write_and_wait(inode->i_mapping);
314 invalidate_mapping_pages(&inode->i_data, 0, -1);
315 }
316
317 if (IS_SETLK(cmd) || IS_SETLKW(cmd))
318 ret = v9fs_file_do_lock(filp, cmd, fl);
M. Mohan Kumar1d769cd2010-09-27 12:22:13 +0530319 else if (IS_GETLK(cmd))
320 ret = v9fs_file_getlock(filp, fl);
M. Mohan Kumara0990272010-09-27 11:34:24 +0530321 else
322 ret = -EINVAL;
323out_err:
324 return ret;
325}
326
327/**
328 * v9fs_file_flock_dotl - lock a file
329 * @filp: file to be locked
330 * @cmd: lock command
331 * @fl: file lock structure
332 *
333 */
334
335static int v9fs_file_flock_dotl(struct file *filp, int cmd,
336 struct file_lock *fl)
337{
Al Viro496ad9a2013-01-23 17:07:38 -0500338 struct inode *inode = file_inode(filp);
M. Mohan Kumara0990272010-09-27 11:34:24 +0530339 int ret = -ENOLCK;
340
Al Viro4b8e9922014-08-19 20:17:38 -0400341 p9_debug(P9_DEBUG_VFS, "filp: %p cmd:%d lock: %p name: %pD\n",
342 filp, cmd, fl, filp);
M. Mohan Kumara0990272010-09-27 11:34:24 +0530343
344 /* No mandatory locks */
345 if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
346 goto out_err;
347
348 if (!(fl->fl_flags & FL_FLOCK))
349 goto out_err;
350
351 if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) {
352 filemap_write_and_wait(inode->i_mapping);
353 invalidate_mapping_pages(&inode->i_data, 0, -1);
354 }
355 /* Convert flock to posix lock */
M. Mohan Kumara0990272010-09-27 11:34:24 +0530356 fl->fl_flags |= FL_POSIX;
357 fl->fl_flags ^= FL_FLOCK;
358
359 if (IS_SETLK(cmd) | IS_SETLKW(cmd))
360 ret = v9fs_file_do_lock(filp, cmd, fl);
361 else
362 ret = -EINVAL;
363out_err:
364 return ret;
365}
366
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700367/**
Eric Van Hensbergenfbedadc2008-10-13 20:36:16 -0500368 * v9fs_file_read - read from a file
369 * @filp: file pointer to read
370 * @udata: user data buffer to read data into
371 * @count: size of buffer
372 * @offset: offset at which to read data
373 *
374 */
375
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700376static ssize_t
Eric Van Hensbergenfbedadc2008-10-13 20:36:16 -0500377v9fs_file_read(struct file *filp, char __user *udata, size_t count,
Latchesar Ionkov19cba8a2005-10-11 08:29:03 -0700378 loff_t * offset)
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700379{
Al Viroe1200fe62015-04-01 23:42:28 -0400380 struct p9_fid *fid = filp->private_data;
381 struct iovec iov = {.iov_base = udata, .iov_len = count};
382 struct iov_iter to;
383 int ret, err;
384
385 iov_iter_init(&to, READ, &iov, 1, count);
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700386
Joe Perches5d385152011-11-28 10:40:46 -0800387 p9_debug(P9_DEBUG_VFS, "count %zu offset %lld\n", count, *offset);
Eric Van Hensbergenfbedadc2008-10-13 20:36:16 -0500388
Al Viroe1200fe62015-04-01 23:42:28 -0400389 ret = p9_client_read(fid, *offset, &to, &err);
390 if (!ret)
391 return err;
Eric Van Hensbergenfbedadc2008-10-13 20:36:16 -0500392
Al Viroe1200fe62015-04-01 23:42:28 -0400393 *offset += ret;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500394 return ret;
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700395}
396
397/**
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700398 * v9fs_file_write - write to a file
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600399 * @filp: file pointer to write
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700400 * @data: data buffer to write data from
401 * @count: size of buffer
402 * @offset: offset at which to write data
403 *
404 */
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700405static ssize_t
406v9fs_file_write(struct file *filp, const char __user * data,
Aneesh Kumar K.V17311772011-02-28 17:03:56 +0530407 size_t count, loff_t *offset)
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700408{
Aneesh Kumar K.V17311772011-02-28 17:03:56 +0530409 ssize_t retval = 0;
jvraofc0f2962010-03-08 22:07:02 +0000410 loff_t origin = *offset;
Al Viroc711a6b2015-04-01 22:04:46 -0400411 struct iovec iov = {.iov_base = (void __user *)data, .iov_len = count};
412 struct iov_iter from;
413 int err = 0;
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700414
Al Viroc711a6b2015-04-01 22:04:46 -0400415 iov_iter_init(&from, WRITE, &iov, 1, count);
Eric Van Hensbergendfb0ec22008-10-13 20:36:16 -0500416
Harsh Prateek Bora3834b122010-08-03 11:55:40 +0000417 retval = generic_write_checks(filp, &origin, &count, 0);
418 if (retval)
Al Viroc711a6b2015-04-01 22:04:46 -0400419 return retval;
Harsh Prateek Bora3834b122010-08-03 11:55:40 +0000420
Al Viroc711a6b2015-04-01 22:04:46 -0400421 iov_iter_truncate(&from, count);
422
423 p9_debug(P9_DEBUG_VFS, "data %p count %d offset %x\n",
424 data, (int)count, (int)*offset);
425
Harsh Prateek Bora3834b122010-08-03 11:55:40 +0000426 retval = 0;
427 if (!count)
Al Viroc711a6b2015-04-01 22:04:46 -0400428 return 0;
Harsh Prateek Bora3834b122010-08-03 11:55:40 +0000429
Al Viroc711a6b2015-04-01 22:04:46 -0400430 retval = p9_client_write(filp->private_data, origin, &from, &err);
431 if (retval > 0) {
432 struct inode *inode = file_inode(filp);
433 loff_t i_size;
434 unsigned long pg_start, pg_end;
435 pg_start = origin >> PAGE_CACHE_SHIFT;
436 pg_end = (origin + retval - 1) >> PAGE_CACHE_SHIFT;
437 if (inode->i_mapping && inode->i_mapping->nrpages)
438 invalidate_inode_pages2_range(inode->i_mapping,
439 pg_start, pg_end);
440 *offset += retval;
441 i_size = i_size_read(inode);
442 if (*offset > i_size) {
443 inode_add_bytes(inode, *offset - i_size);
444 i_size_write(inode, *offset);
445 }
446 return retval;
447 }
448 return err;
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700449}
450
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530451
Josef Bacik02c24a82011-07-16 20:44:56 -0400452static int v9fs_file_fsync(struct file *filp, loff_t start, loff_t end,
453 int datasync)
M. Mohan Kumar7a4439c2010-02-08 15:36:48 -0600454{
455 struct p9_fid *fid;
Josef Bacik02c24a82011-07-16 20:44:56 -0400456 struct inode *inode = filp->f_mapping->host;
M. Mohan Kumar7a4439c2010-02-08 15:36:48 -0600457 struct p9_wstat wstat;
458 int retval;
459
Josef Bacik02c24a82011-07-16 20:44:56 -0400460 retval = filemap_write_and_wait_range(inode->i_mapping, start, end);
461 if (retval)
462 return retval;
463
464 mutex_lock(&inode->i_mutex);
Joe Perches5d385152011-11-28 10:40:46 -0800465 p9_debug(P9_DEBUG_VFS, "filp %p datasync %x\n", filp, datasync);
M. Mohan Kumar7a4439c2010-02-08 15:36:48 -0600466
467 fid = filp->private_data;
468 v9fs_blank_wstat(&wstat);
469
470 retval = p9_client_wstat(fid, &wstat);
Josef Bacik02c24a82011-07-16 20:44:56 -0400471 mutex_unlock(&inode->i_mutex);
472
M. Mohan Kumar7a4439c2010-02-08 15:36:48 -0600473 return retval;
474}
475
Josef Bacik02c24a82011-07-16 20:44:56 -0400476int v9fs_file_fsync_dotl(struct file *filp, loff_t start, loff_t end,
477 int datasync)
Venkateswararao Jujjuri (JV)920e65d2010-09-22 17:19:19 -0700478{
479 struct p9_fid *fid;
Josef Bacik02c24a82011-07-16 20:44:56 -0400480 struct inode *inode = filp->f_mapping->host;
Venkateswararao Jujjuri (JV)920e65d2010-09-22 17:19:19 -0700481 int retval;
482
Josef Bacik02c24a82011-07-16 20:44:56 -0400483 retval = filemap_write_and_wait_range(inode->i_mapping, start, end);
484 if (retval)
485 return retval;
486
487 mutex_lock(&inode->i_mutex);
Joe Perches5d385152011-11-28 10:40:46 -0800488 p9_debug(P9_DEBUG_VFS, "filp %p datasync %x\n", filp, datasync);
Venkateswararao Jujjuri (JV)920e65d2010-09-22 17:19:19 -0700489
490 fid = filp->private_data;
491
Venkateswararao Jujjuri (JV)b165d602010-10-22 10:13:12 -0700492 retval = p9_client_fsync(fid, datasync);
Josef Bacik02c24a82011-07-16 20:44:56 -0400493 mutex_unlock(&inode->i_mutex);
494
Venkateswararao Jujjuri (JV)920e65d2010-09-22 17:19:19 -0700495 return retval;
496}
497
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530498static int
Dominique Martinetfb89b452014-01-10 13:44:09 +0100499v9fs_file_mmap(struct file *filp, struct vm_area_struct *vma)
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530500{
501 int retval;
502
Dominique Martinetfb89b452014-01-10 13:44:09 +0100503
504 retval = generic_file_mmap(filp, vma);
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530505 if (!retval)
506 vma->vm_ops = &v9fs_file_vm_ops;
507
508 return retval;
509}
510
511static int
Dominique Martinetfb89b452014-01-10 13:44:09 +0100512v9fs_mmap_file_mmap(struct file *filp, struct vm_area_struct *vma)
513{
514 int retval;
515 struct inode *inode;
516 struct v9fs_inode *v9inode;
517 struct p9_fid *fid;
518
519 inode = file_inode(filp);
520 v9inode = V9FS_I(inode);
521 mutex_lock(&v9inode->v_mutex);
522 if (!v9inode->writeback_fid &&
523 (vma->vm_flags & VM_WRITE)) {
524 /*
525 * clone a fid and add it to writeback_fid
526 * we do it during mmap instead of
527 * page dirty time via write_begin/page_mkwrite
528 * because we want write after unlink usecase
529 * to work.
530 */
531 fid = v9fs_writeback_fid(filp->f_path.dentry);
532 if (IS_ERR(fid)) {
533 retval = PTR_ERR(fid);
534 mutex_unlock(&v9inode->v_mutex);
535 return retval;
536 }
537 v9inode->writeback_fid = (void *) fid;
538 }
539 mutex_unlock(&v9inode->v_mutex);
540
541 retval = generic_file_mmap(filp, vma);
542 if (!retval)
543 vma->vm_ops = &v9fs_mmap_file_vm_ops;
544
545 return retval;
546}
547
548static int
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530549v9fs_vm_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
550{
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530551 struct v9fs_inode *v9inode;
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530552 struct page *page = vmf->page;
553 struct file *filp = vma->vm_file;
Al Viro496ad9a2013-01-23 17:07:38 -0500554 struct inode *inode = file_inode(filp);
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530555
556
Joe Perches5d385152011-11-28 10:40:46 -0800557 p9_debug(P9_DEBUG_VFS, "page %p fid %lx\n",
558 page, (unsigned long)filp->private_data);
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530559
Jan Kara120c2bc2012-06-12 16:20:25 +0200560 /* Update file times before taking page lock */
561 file_update_time(filp);
562
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530563 v9inode = V9FS_I(inode);
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530564 /* make sure the cache has finished storing the page */
565 v9fs_fscache_wait_on_page_write(inode, page);
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530566 BUG_ON(!v9inode->writeback_fid);
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530567 lock_page(page);
568 if (page->mapping != inode->i_mapping)
569 goto out_unlock;
Darrick J. Wong13575ca2013-02-21 16:42:53 -0800570 wait_for_stable_page(page);
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530571
572 return VM_FAULT_LOCKED;
573out_unlock:
574 unlock_page(page);
575 return VM_FAULT_NOPAGE;
576}
577
Aneesh Kumar K.Ve959b542011-02-28 17:04:04 +0530578static ssize_t
579v9fs_direct_read(struct file *filp, char __user *udata, size_t count,
580 loff_t *offsetp)
581{
582 loff_t size, offset;
583 struct inode *inode;
584 struct address_space *mapping;
585
586 offset = *offsetp;
587 mapping = filp->f_mapping;
588 inode = mapping->host;
589 if (!count)
590 return 0;
591 size = i_size_read(inode);
592 if (offset < size)
593 filemap_write_and_wait_range(mapping, offset,
594 offset + count - 1);
595
596 return v9fs_file_read(filp, udata, count, offsetp);
597}
598
599/**
600 * v9fs_cached_file_read - read from a file
601 * @filp: file pointer to read
Fabian Frederickfd2916b2014-06-04 16:06:26 -0700602 * @data: user data buffer to read data into
Aneesh Kumar K.Ve959b542011-02-28 17:04:04 +0530603 * @count: size of buffer
604 * @offset: offset at which to read data
605 *
606 */
607static ssize_t
608v9fs_cached_file_read(struct file *filp, char __user *data, size_t count,
609 loff_t *offset)
610{
611 if (filp->f_flags & O_DIRECT)
612 return v9fs_direct_read(filp, data, count, offset);
Al Viroaad4f8b2014-04-02 14:33:16 -0400613 return new_sync_read(filp, data, count, offset);
Aneesh Kumar K.Ve959b542011-02-28 17:04:04 +0530614}
615
Dominique Martinetfb89b452014-01-10 13:44:09 +0100616/**
617 * v9fs_mmap_file_read - read from a file
618 * @filp: file pointer to read
Fabian Frederickfd2916b2014-06-04 16:06:26 -0700619 * @data: user data buffer to read data into
Dominique Martinetfb89b452014-01-10 13:44:09 +0100620 * @count: size of buffer
621 * @offset: offset at which to read data
622 *
623 */
624static ssize_t
625v9fs_mmap_file_read(struct file *filp, char __user *data, size_t count,
626 loff_t *offset)
627{
628 /* TODO: Check if there are dirty pages */
629 return v9fs_file_read(filp, data, count, offset);
630}
631
Dominique Martinetfb89b452014-01-10 13:44:09 +0100632/**
633 * v9fs_mmap_file_write - write to a file
634 * @filp: file pointer to write
635 * @data: data buffer to write data from
636 * @count: size of buffer
637 * @offset: offset at which to write data
638 *
639 */
640static ssize_t
641v9fs_mmap_file_write(struct file *filp, const char __user *data,
642 size_t count, loff_t *offset)
643{
644 /*
645 * TODO: invalidate mmaps on filp's inode between
646 * offset and offset+count
647 */
648 return v9fs_file_write(filp, data, count, offset);
649}
650
651static void v9fs_mmap_vm_close(struct vm_area_struct *vma)
652{
653 struct inode *inode;
654
655 struct writeback_control wbc = {
656 .nr_to_write = LONG_MAX,
657 .sync_mode = WB_SYNC_ALL,
658 .range_start = vma->vm_pgoff * PAGE_SIZE,
659 /* absolute end, byte at end included */
660 .range_end = vma->vm_pgoff * PAGE_SIZE +
661 (vma->vm_end - vma->vm_start - 1),
662 };
663
664
665 p9_debug(P9_DEBUG_VFS, "9p VMA close, %p, flushing", vma);
666
667 inode = file_inode(vma->vm_file);
668
669 if (!mapping_cap_writeback_dirty(inode->i_mapping))
670 wbc.nr_to_write = 0;
671
672 might_sleep();
673 sync_inode(inode, &wbc);
674}
675
676
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530677static const struct vm_operations_struct v9fs_file_vm_ops = {
678 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -0700679 .map_pages = filemap_map_pages,
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530680 .page_mkwrite = v9fs_vm_page_mkwrite,
681};
682
Dominique Martinetfb89b452014-01-10 13:44:09 +0100683static const struct vm_operations_struct v9fs_mmap_file_vm_ops = {
684 .close = v9fs_mmap_vm_close,
685 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -0700686 .map_pages = filemap_map_pages,
Dominique Martinetfb89b452014-01-10 13:44:09 +0100687 .page_mkwrite = v9fs_vm_page_mkwrite,
Dominique Martinetfb89b452014-01-10 13:44:09 +0100688};
689
Aneesh Kumar K.Ve959b542011-02-28 17:04:04 +0530690
Aneesh Kumar K.V29236f42011-02-28 17:03:54 +0530691const struct file_operations v9fs_cached_file_operations = {
Eric Van Hensbergene03abc02007-02-11 13:21:39 -0600692 .llseek = generic_file_llseek,
Aneesh Kumar K.Ve959b542011-02-28 17:04:04 +0530693 .read = v9fs_cached_file_read,
Al Viro9565a542015-04-01 22:32:23 -0400694 .write = new_sync_write,
Al Viroaad4f8b2014-04-02 14:33:16 -0400695 .read_iter = generic_file_read_iter,
Al Viro81742022014-04-03 03:17:43 -0400696 .write_iter = generic_file_write_iter,
Eric Van Hensbergene03abc02007-02-11 13:21:39 -0600697 .open = v9fs_file_open,
698 .release = v9fs_dir_release,
699 .lock = v9fs_file_lock,
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530700 .mmap = v9fs_file_mmap,
M. Mohan Kumar7a4439c2010-02-08 15:36:48 -0600701 .fsync = v9fs_file_fsync,
Eric Van Hensbergene03abc02007-02-11 13:21:39 -0600702};
703
Aneesh Kumar K.V29236f42011-02-28 17:03:54 +0530704const struct file_operations v9fs_cached_file_operations_dotl = {
Venkateswararao Jujjuri (JV)b04faaf2010-09-22 16:30:52 -0700705 .llseek = generic_file_llseek,
Aneesh Kumar K.Ve959b542011-02-28 17:04:04 +0530706 .read = v9fs_cached_file_read,
Al Viro9565a542015-04-01 22:32:23 -0400707 .write = new_sync_write,
Al Viroaad4f8b2014-04-02 14:33:16 -0400708 .read_iter = generic_file_read_iter,
Al Viro81742022014-04-03 03:17:43 -0400709 .write_iter = generic_file_write_iter,
Venkateswararao Jujjuri (JV)b04faaf2010-09-22 16:30:52 -0700710 .open = v9fs_file_open,
711 .release = v9fs_dir_release,
M. Mohan Kumara0990272010-09-27 11:34:24 +0530712 .lock = v9fs_file_lock_dotl,
713 .flock = v9fs_file_flock_dotl,
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530714 .mmap = v9fs_file_mmap,
Venkateswararao Jujjuri (JV)920e65d2010-09-22 17:19:19 -0700715 .fsync = v9fs_file_fsync_dotl,
Venkateswararao Jujjuri (JV)b04faaf2010-09-22 16:30:52 -0700716};
717
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800718const struct file_operations v9fs_file_operations = {
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700719 .llseek = generic_file_llseek,
720 .read = v9fs_file_read,
721 .write = v9fs_file_write,
722 .open = v9fs_file_open,
723 .release = v9fs_dir_release,
724 .lock = v9fs_file_lock,
Eric Van Hensbergen14b88692008-02-06 19:25:05 -0600725 .mmap = generic_file_readonly_mmap,
M. Mohan Kumar7a4439c2010-02-08 15:36:48 -0600726 .fsync = v9fs_file_fsync,
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700727};
Sripathi Kodi9b6533c2010-03-25 12:41:54 +0000728
729const struct file_operations v9fs_file_operations_dotl = {
730 .llseek = generic_file_llseek,
731 .read = v9fs_file_read,
732 .write = v9fs_file_write,
733 .open = v9fs_file_open,
734 .release = v9fs_dir_release,
M. Mohan Kumara0990272010-09-27 11:34:24 +0530735 .lock = v9fs_file_lock_dotl,
736 .flock = v9fs_file_flock_dotl,
Sripathi Kodi9b6533c2010-03-25 12:41:54 +0000737 .mmap = generic_file_readonly_mmap,
Venkateswararao Jujjuri (JV)920e65d2010-09-22 17:19:19 -0700738 .fsync = v9fs_file_fsync_dotl,
Sripathi Kodi9b6533c2010-03-25 12:41:54 +0000739};
Dominique Martinetfb89b452014-01-10 13:44:09 +0100740
741const struct file_operations v9fs_mmap_file_operations = {
742 .llseek = generic_file_llseek,
743 .read = v9fs_mmap_file_read,
744 .write = v9fs_mmap_file_write,
745 .open = v9fs_file_open,
746 .release = v9fs_dir_release,
747 .lock = v9fs_file_lock,
748 .mmap = v9fs_mmap_file_mmap,
749 .fsync = v9fs_file_fsync,
750};
751
752const struct file_operations v9fs_mmap_file_operations_dotl = {
753 .llseek = generic_file_llseek,
754 .read = v9fs_mmap_file_read,
755 .write = v9fs_mmap_file_write,
756 .open = v9fs_file_open,
757 .release = v9fs_dir_release,
758 .lock = v9fs_file_lock_dotl,
759 .flock = v9fs_file_flock_dotl,
760 .mmap = v9fs_mmap_file_mmap,
761 .fsync = v9fs_file_fsync_dotl,
762};