blob: 0ac2969c9c2e75b8a88c9c136961bbdb1710f6e3 [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
Al Viroe494b6b2015-04-01 23:59:57 -0400377v9fs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700378{
Al Viroe494b6b2015-04-01 23:59:57 -0400379 struct p9_fid *fid = iocb->ki_filp->private_data;
Al Viroe1200fe62015-04-01 23:42:28 -0400380 int ret, err;
381
Al Viroe494b6b2015-04-01 23:59:57 -0400382 p9_debug(P9_DEBUG_VFS, "count %zu offset %lld\n",
383 iov_iter_count(to), iocb->ki_pos);
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700384
Al Viroe494b6b2015-04-01 23:59:57 -0400385 ret = p9_client_read(fid, iocb->ki_pos, to, &err);
Al Viroe1200fe62015-04-01 23:42:28 -0400386 if (!ret)
387 return err;
Eric Van Hensbergenfbedadc2008-10-13 20:36:16 -0500388
Al Viroe494b6b2015-04-01 23:59:57 -0400389 iocb->ki_pos += ret;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500390 return ret;
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700391}
392
393/**
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700394 * v9fs_file_write - write to a file
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600395 * @filp: file pointer to write
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700396 * @data: data buffer to write data from
397 * @count: size of buffer
398 * @offset: offset at which to write data
399 *
400 */
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700401static ssize_t
Al Viroe494b6b2015-04-01 23:59:57 -0400402v9fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700403{
Al Viroe494b6b2015-04-01 23:59:57 -0400404 struct file *file = iocb->ki_filp;
Aneesh Kumar K.V17311772011-02-28 17:03:56 +0530405 ssize_t retval = 0;
Al Viroe494b6b2015-04-01 23:59:57 -0400406 loff_t origin = iocb->ki_pos;
407 size_t count = iov_iter_count(from);
Al Viroc711a6b2015-04-01 22:04:46 -0400408 int err = 0;
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700409
Al Viroe494b6b2015-04-01 23:59:57 -0400410 retval = generic_write_checks(file, &origin, &count, 0);
Harsh Prateek Bora3834b122010-08-03 11:55:40 +0000411 if (retval)
Al Viroc711a6b2015-04-01 22:04:46 -0400412 return retval;
Harsh Prateek Bora3834b122010-08-03 11:55:40 +0000413
Al Viroe494b6b2015-04-01 23:59:57 -0400414 iov_iter_truncate(from, count);
Al Viroc711a6b2015-04-01 22:04:46 -0400415
Harsh Prateek Bora3834b122010-08-03 11:55:40 +0000416 if (!count)
Al Viroc711a6b2015-04-01 22:04:46 -0400417 return 0;
Harsh Prateek Bora3834b122010-08-03 11:55:40 +0000418
Al Viroe494b6b2015-04-01 23:59:57 -0400419 retval = p9_client_write(file->private_data, origin, from, &err);
Al Viroc711a6b2015-04-01 22:04:46 -0400420 if (retval > 0) {
Al Viroe494b6b2015-04-01 23:59:57 -0400421 struct inode *inode = file_inode(file);
Al Viroc711a6b2015-04-01 22:04:46 -0400422 loff_t i_size;
423 unsigned long pg_start, pg_end;
424 pg_start = origin >> PAGE_CACHE_SHIFT;
425 pg_end = (origin + retval - 1) >> PAGE_CACHE_SHIFT;
426 if (inode->i_mapping && inode->i_mapping->nrpages)
427 invalidate_inode_pages2_range(inode->i_mapping,
428 pg_start, pg_end);
Al Viroe494b6b2015-04-01 23:59:57 -0400429 origin += retval;
Al Viroc711a6b2015-04-01 22:04:46 -0400430 i_size = i_size_read(inode);
Al Viroe494b6b2015-04-01 23:59:57 -0400431 iocb->ki_pos = origin;
432 if (origin > i_size) {
433 inode_add_bytes(inode, origin - i_size);
434 i_size_write(inode, origin);
Al Viroc711a6b2015-04-01 22:04:46 -0400435 }
436 return retval;
437 }
438 return err;
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700439}
440
Josef Bacik02c24a82011-07-16 20:44:56 -0400441static int v9fs_file_fsync(struct file *filp, loff_t start, loff_t end,
442 int datasync)
M. Mohan Kumar7a4439c2010-02-08 15:36:48 -0600443{
444 struct p9_fid *fid;
Josef Bacik02c24a82011-07-16 20:44:56 -0400445 struct inode *inode = filp->f_mapping->host;
M. Mohan Kumar7a4439c2010-02-08 15:36:48 -0600446 struct p9_wstat wstat;
447 int retval;
448
Josef Bacik02c24a82011-07-16 20:44:56 -0400449 retval = filemap_write_and_wait_range(inode->i_mapping, start, end);
450 if (retval)
451 return retval;
452
453 mutex_lock(&inode->i_mutex);
Joe Perches5d385152011-11-28 10:40:46 -0800454 p9_debug(P9_DEBUG_VFS, "filp %p datasync %x\n", filp, datasync);
M. Mohan Kumar7a4439c2010-02-08 15:36:48 -0600455
456 fid = filp->private_data;
457 v9fs_blank_wstat(&wstat);
458
459 retval = p9_client_wstat(fid, &wstat);
Josef Bacik02c24a82011-07-16 20:44:56 -0400460 mutex_unlock(&inode->i_mutex);
461
M. Mohan Kumar7a4439c2010-02-08 15:36:48 -0600462 return retval;
463}
464
Josef Bacik02c24a82011-07-16 20:44:56 -0400465int v9fs_file_fsync_dotl(struct file *filp, loff_t start, loff_t end,
466 int datasync)
Venkateswararao Jujjuri (JV)920e65d2010-09-22 17:19:19 -0700467{
468 struct p9_fid *fid;
Josef Bacik02c24a82011-07-16 20:44:56 -0400469 struct inode *inode = filp->f_mapping->host;
Venkateswararao Jujjuri (JV)920e65d2010-09-22 17:19:19 -0700470 int retval;
471
Josef Bacik02c24a82011-07-16 20:44:56 -0400472 retval = filemap_write_and_wait_range(inode->i_mapping, start, end);
473 if (retval)
474 return retval;
475
476 mutex_lock(&inode->i_mutex);
Joe Perches5d385152011-11-28 10:40:46 -0800477 p9_debug(P9_DEBUG_VFS, "filp %p datasync %x\n", filp, datasync);
Venkateswararao Jujjuri (JV)920e65d2010-09-22 17:19:19 -0700478
479 fid = filp->private_data;
480
Venkateswararao Jujjuri (JV)b165d602010-10-22 10:13:12 -0700481 retval = p9_client_fsync(fid, datasync);
Josef Bacik02c24a82011-07-16 20:44:56 -0400482 mutex_unlock(&inode->i_mutex);
483
Venkateswararao Jujjuri (JV)920e65d2010-09-22 17:19:19 -0700484 return retval;
485}
486
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530487static int
Dominique Martinetfb89b452014-01-10 13:44:09 +0100488v9fs_file_mmap(struct file *filp, struct vm_area_struct *vma)
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530489{
490 int retval;
491
Dominique Martinetfb89b452014-01-10 13:44:09 +0100492
493 retval = generic_file_mmap(filp, vma);
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530494 if (!retval)
495 vma->vm_ops = &v9fs_file_vm_ops;
496
497 return retval;
498}
499
500static int
Dominique Martinetfb89b452014-01-10 13:44:09 +0100501v9fs_mmap_file_mmap(struct file *filp, struct vm_area_struct *vma)
502{
503 int retval;
504 struct inode *inode;
505 struct v9fs_inode *v9inode;
506 struct p9_fid *fid;
507
508 inode = file_inode(filp);
509 v9inode = V9FS_I(inode);
510 mutex_lock(&v9inode->v_mutex);
511 if (!v9inode->writeback_fid &&
512 (vma->vm_flags & VM_WRITE)) {
513 /*
514 * clone a fid and add it to writeback_fid
515 * we do it during mmap instead of
516 * page dirty time via write_begin/page_mkwrite
517 * because we want write after unlink usecase
518 * to work.
519 */
520 fid = v9fs_writeback_fid(filp->f_path.dentry);
521 if (IS_ERR(fid)) {
522 retval = PTR_ERR(fid);
523 mutex_unlock(&v9inode->v_mutex);
524 return retval;
525 }
526 v9inode->writeback_fid = (void *) fid;
527 }
528 mutex_unlock(&v9inode->v_mutex);
529
530 retval = generic_file_mmap(filp, vma);
531 if (!retval)
532 vma->vm_ops = &v9fs_mmap_file_vm_ops;
533
534 return retval;
535}
536
537static int
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530538v9fs_vm_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
539{
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530540 struct v9fs_inode *v9inode;
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530541 struct page *page = vmf->page;
542 struct file *filp = vma->vm_file;
Al Viro496ad9a2013-01-23 17:07:38 -0500543 struct inode *inode = file_inode(filp);
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530544
545
Joe Perches5d385152011-11-28 10:40:46 -0800546 p9_debug(P9_DEBUG_VFS, "page %p fid %lx\n",
547 page, (unsigned long)filp->private_data);
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530548
Jan Kara120c2bc2012-06-12 16:20:25 +0200549 /* Update file times before taking page lock */
550 file_update_time(filp);
551
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530552 v9inode = V9FS_I(inode);
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530553 /* make sure the cache has finished storing the page */
554 v9fs_fscache_wait_on_page_write(inode, page);
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530555 BUG_ON(!v9inode->writeback_fid);
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530556 lock_page(page);
557 if (page->mapping != inode->i_mapping)
558 goto out_unlock;
Darrick J. Wong13575ca2013-02-21 16:42:53 -0800559 wait_for_stable_page(page);
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530560
561 return VM_FAULT_LOCKED;
562out_unlock:
563 unlock_page(page);
564 return VM_FAULT_NOPAGE;
565}
566
Dominique Martinetfb89b452014-01-10 13:44:09 +0100567/**
568 * v9fs_mmap_file_read - read from a file
569 * @filp: file pointer to read
Fabian Frederickfd2916b2014-06-04 16:06:26 -0700570 * @data: user data buffer to read data into
Dominique Martinetfb89b452014-01-10 13:44:09 +0100571 * @count: size of buffer
572 * @offset: offset at which to read data
573 *
574 */
575static ssize_t
Al Viroe494b6b2015-04-01 23:59:57 -0400576v9fs_mmap_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
Dominique Martinetfb89b452014-01-10 13:44:09 +0100577{
578 /* TODO: Check if there are dirty pages */
Al Viroe494b6b2015-04-01 23:59:57 -0400579 return v9fs_file_read_iter(iocb, to);
Dominique Martinetfb89b452014-01-10 13:44:09 +0100580}
581
Dominique Martinetfb89b452014-01-10 13:44:09 +0100582/**
583 * v9fs_mmap_file_write - write to a file
584 * @filp: file pointer to write
585 * @data: data buffer to write data from
586 * @count: size of buffer
587 * @offset: offset at which to write data
588 *
589 */
590static ssize_t
Al Viroe494b6b2015-04-01 23:59:57 -0400591v9fs_mmap_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
Dominique Martinetfb89b452014-01-10 13:44:09 +0100592{
593 /*
594 * TODO: invalidate mmaps on filp's inode between
595 * offset and offset+count
596 */
Al Viroe494b6b2015-04-01 23:59:57 -0400597 return v9fs_file_write_iter(iocb, from);
Dominique Martinetfb89b452014-01-10 13:44:09 +0100598}
599
600static void v9fs_mmap_vm_close(struct vm_area_struct *vma)
601{
602 struct inode *inode;
603
604 struct writeback_control wbc = {
605 .nr_to_write = LONG_MAX,
606 .sync_mode = WB_SYNC_ALL,
607 .range_start = vma->vm_pgoff * PAGE_SIZE,
608 /* absolute end, byte at end included */
609 .range_end = vma->vm_pgoff * PAGE_SIZE +
610 (vma->vm_end - vma->vm_start - 1),
611 };
612
613
614 p9_debug(P9_DEBUG_VFS, "9p VMA close, %p, flushing", vma);
615
616 inode = file_inode(vma->vm_file);
617
618 if (!mapping_cap_writeback_dirty(inode->i_mapping))
619 wbc.nr_to_write = 0;
620
621 might_sleep();
622 sync_inode(inode, &wbc);
623}
624
625
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530626static const struct vm_operations_struct v9fs_file_vm_ops = {
627 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -0700628 .map_pages = filemap_map_pages,
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530629 .page_mkwrite = v9fs_vm_page_mkwrite,
630};
631
Dominique Martinetfb89b452014-01-10 13:44:09 +0100632static const struct vm_operations_struct v9fs_mmap_file_vm_ops = {
633 .close = v9fs_mmap_vm_close,
634 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -0700635 .map_pages = filemap_map_pages,
Dominique Martinetfb89b452014-01-10 13:44:09 +0100636 .page_mkwrite = v9fs_vm_page_mkwrite,
Dominique Martinetfb89b452014-01-10 13:44:09 +0100637};
638
Aneesh Kumar K.Ve959b542011-02-28 17:04:04 +0530639
Aneesh Kumar K.V29236f42011-02-28 17:03:54 +0530640const struct file_operations v9fs_cached_file_operations = {
Eric Van Hensbergene03abc02007-02-11 13:21:39 -0600641 .llseek = generic_file_llseek,
Al Viro42b1ab92015-04-01 23:49:24 -0400642 .read = new_sync_read,
Al Viro9565a542015-04-01 22:32:23 -0400643 .write = new_sync_write,
Al Viroaad4f8b2014-04-02 14:33:16 -0400644 .read_iter = generic_file_read_iter,
Al Viro81742022014-04-03 03:17:43 -0400645 .write_iter = generic_file_write_iter,
Eric Van Hensbergene03abc02007-02-11 13:21:39 -0600646 .open = v9fs_file_open,
647 .release = v9fs_dir_release,
648 .lock = v9fs_file_lock,
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530649 .mmap = v9fs_file_mmap,
M. Mohan Kumar7a4439c2010-02-08 15:36:48 -0600650 .fsync = v9fs_file_fsync,
Eric Van Hensbergene03abc02007-02-11 13:21:39 -0600651};
652
Aneesh Kumar K.V29236f42011-02-28 17:03:54 +0530653const struct file_operations v9fs_cached_file_operations_dotl = {
Venkateswararao Jujjuri (JV)b04faaf2010-09-22 16:30:52 -0700654 .llseek = generic_file_llseek,
Al Viro42b1ab92015-04-01 23:49:24 -0400655 .read = new_sync_read,
Al Viro9565a542015-04-01 22:32:23 -0400656 .write = new_sync_write,
Al Viroaad4f8b2014-04-02 14:33:16 -0400657 .read_iter = generic_file_read_iter,
Al Viro81742022014-04-03 03:17:43 -0400658 .write_iter = generic_file_write_iter,
Venkateswararao Jujjuri (JV)b04faaf2010-09-22 16:30:52 -0700659 .open = v9fs_file_open,
660 .release = v9fs_dir_release,
M. Mohan Kumara0990272010-09-27 11:34:24 +0530661 .lock = v9fs_file_lock_dotl,
662 .flock = v9fs_file_flock_dotl,
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530663 .mmap = v9fs_file_mmap,
Venkateswararao Jujjuri (JV)920e65d2010-09-22 17:19:19 -0700664 .fsync = v9fs_file_fsync_dotl,
Venkateswararao Jujjuri (JV)b04faaf2010-09-22 16:30:52 -0700665};
666
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800667const struct file_operations v9fs_file_operations = {
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700668 .llseek = generic_file_llseek,
Al Viroe494b6b2015-04-01 23:59:57 -0400669 .read = new_sync_read,
670 .write = new_sync_write,
671 .read_iter = v9fs_file_read_iter,
672 .write_iter = v9fs_file_write_iter,
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700673 .open = v9fs_file_open,
674 .release = v9fs_dir_release,
675 .lock = v9fs_file_lock,
Eric Van Hensbergen14b88692008-02-06 19:25:05 -0600676 .mmap = generic_file_readonly_mmap,
M. Mohan Kumar7a4439c2010-02-08 15:36:48 -0600677 .fsync = v9fs_file_fsync,
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700678};
Sripathi Kodi9b6533c2010-03-25 12:41:54 +0000679
680const struct file_operations v9fs_file_operations_dotl = {
681 .llseek = generic_file_llseek,
Al Viroe494b6b2015-04-01 23:59:57 -0400682 .read = new_sync_read,
683 .write = new_sync_write,
684 .read_iter = v9fs_file_read_iter,
685 .write_iter = v9fs_file_write_iter,
Sripathi Kodi9b6533c2010-03-25 12:41:54 +0000686 .open = v9fs_file_open,
687 .release = v9fs_dir_release,
M. Mohan Kumara0990272010-09-27 11:34:24 +0530688 .lock = v9fs_file_lock_dotl,
689 .flock = v9fs_file_flock_dotl,
Sripathi Kodi9b6533c2010-03-25 12:41:54 +0000690 .mmap = generic_file_readonly_mmap,
Venkateswararao Jujjuri (JV)920e65d2010-09-22 17:19:19 -0700691 .fsync = v9fs_file_fsync_dotl,
Sripathi Kodi9b6533c2010-03-25 12:41:54 +0000692};
Dominique Martinetfb89b452014-01-10 13:44:09 +0100693
694const struct file_operations v9fs_mmap_file_operations = {
695 .llseek = generic_file_llseek,
Al Viroe494b6b2015-04-01 23:59:57 -0400696 .read = new_sync_read,
697 .write = new_sync_write,
698 .read_iter = v9fs_mmap_file_read_iter,
699 .write_iter = v9fs_mmap_file_write_iter,
Dominique Martinetfb89b452014-01-10 13:44:09 +0100700 .open = v9fs_file_open,
701 .release = v9fs_dir_release,
702 .lock = v9fs_file_lock,
703 .mmap = v9fs_mmap_file_mmap,
704 .fsync = v9fs_file_fsync,
705};
706
707const struct file_operations v9fs_mmap_file_operations_dotl = {
708 .llseek = generic_file_llseek,
Al Viroe494b6b2015-04-01 23:59:57 -0400709 .read = new_sync_read,
710 .write = new_sync_write,
711 .read_iter = v9fs_mmap_file_read_iter,
712 .write_iter = v9fs_mmap_file_write_iter,
Dominique Martinetfb89b452014-01-10 13:44:09 +0100713 .open = v9fs_file_open,
714 .release = v9fs_dir_release,
715 .lock = v9fs_file_lock_dotl,
716 .flock = v9fs_file_flock_dotl,
717 .mmap = v9fs_mmap_file_mmap,
718 .fsync = v9fs_file_fsync_dotl,
719};