blob: a54d81f9097f88bc84b86d16651db0ae77f96eaf [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/**
Aneesh Kumar K.V17311772011-02-28 17:03:56 +0530368 * v9fs_fid_readn - read from a fid
369 * @fid: fid to read
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700370 * @data: data buffer to read data into
Eric Van Hensbergenfbedadc2008-10-13 20:36:16 -0500371 * @udata: user data buffer to read data into
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700372 * @count: size of buffer
373 * @offset: offset at which to read data
374 *
375 */
Eric Van Hensbergenfbedadc2008-10-13 20:36:16 -0500376ssize_t
Aneesh Kumar K.V17311772011-02-28 17:03:56 +0530377v9fs_fid_readn(struct p9_fid *fid, char *data, char __user *udata, u32 count,
Eric Van Hensbergenfbedadc2008-10-13 20:36:16 -0500378 u64 offset)
379{
M. Mohan Kumar97e84422010-06-04 11:59:07 +0000380 int n, total, size;
Eric Van Hensbergenfbedadc2008-10-13 20:36:16 -0500381
Joe Perches5d385152011-11-28 10:40:46 -0800382 p9_debug(P9_DEBUG_VFS, "fid %d offset %llu count %d\n",
383 fid->fid, (long long unsigned)offset, count);
Eric Van Hensbergenfbedadc2008-10-13 20:36:16 -0500384 n = 0;
385 total = 0;
M. Mohan Kumar97e84422010-06-04 11:59:07 +0000386 size = fid->iounit ? fid->iounit : fid->clnt->msize - P9_IOHDRSZ;
Eric Van Hensbergenfbedadc2008-10-13 20:36:16 -0500387 do {
388 n = p9_client_read(fid, data, udata, offset, count);
389 if (n <= 0)
390 break;
391
392 if (data)
393 data += n;
394 if (udata)
395 udata += n;
396
397 offset += n;
398 count -= n;
399 total += n;
M. Mohan Kumar97e84422010-06-04 11:59:07 +0000400 } while (count > 0 && n == size);
Eric Van Hensbergenfbedadc2008-10-13 20:36:16 -0500401
402 if (n < 0)
403 total = n;
404
405 return total;
406}
407
408/**
Aneesh Kumar K.V17311772011-02-28 17:03:56 +0530409 * v9fs_file_readn - read from a file
410 * @filp: file pointer to read
411 * @data: data buffer to read data into
412 * @udata: user data buffer to read data into
413 * @count: size of buffer
414 * @offset: offset at which to read data
415 *
416 */
417ssize_t
418v9fs_file_readn(struct file *filp, char *data, char __user *udata, u32 count,
419 u64 offset)
420{
421 return v9fs_fid_readn(filp->private_data, data, udata, count, offset);
422}
423
424/**
Eric Van Hensbergenfbedadc2008-10-13 20:36:16 -0500425 * v9fs_file_read - read from a file
426 * @filp: file pointer to read
427 * @udata: user data buffer to read data into
428 * @count: size of buffer
429 * @offset: offset at which to read data
430 *
431 */
432
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700433static ssize_t
Eric Van Hensbergenfbedadc2008-10-13 20:36:16 -0500434v9fs_file_read(struct file *filp, char __user *udata, size_t count,
Latchesar Ionkov19cba8a2005-10-11 08:29:03 -0700435 loff_t * offset)
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700436{
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500437 int ret;
438 struct p9_fid *fid;
M. Mohan Kumar97e84422010-06-04 11:59:07 +0000439 size_t size;
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700440
Joe Perches5d385152011-11-28 10:40:46 -0800441 p9_debug(P9_DEBUG_VFS, "count %zu offset %lld\n", count, *offset);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500442 fid = filp->private_data;
Eric Van Hensbergenfbedadc2008-10-13 20:36:16 -0500443
M. Mohan Kumar97e84422010-06-04 11:59:07 +0000444 size = fid->iounit ? fid->iounit : fid->clnt->msize - P9_IOHDRSZ;
445 if (count > size)
Eric Van Hensbergenfbedadc2008-10-13 20:36:16 -0500446 ret = v9fs_file_readn(filp, NULL, udata, count, *offset);
447 else
448 ret = p9_client_read(fid, NULL, udata, *offset, count);
449
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500450 if (ret > 0)
451 *offset += ret;
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700452
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500453 return ret;
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700454}
455
456/**
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700457 * v9fs_file_write - write to a file
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600458 * @filp: file pointer to write
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700459 * @data: data buffer to write data from
460 * @count: size of buffer
461 * @offset: offset at which to write data
462 *
463 */
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700464static ssize_t
465v9fs_file_write(struct file *filp, const char __user * data,
Aneesh Kumar K.V17311772011-02-28 17:03:56 +0530466 size_t count, loff_t *offset)
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700467{
Aneesh Kumar K.V17311772011-02-28 17:03:56 +0530468 ssize_t retval = 0;
jvraofc0f2962010-03-08 22:07:02 +0000469 loff_t origin = *offset;
Al Viroc711a6b2015-04-01 22:04:46 -0400470 struct iovec iov = {.iov_base = (void __user *)data, .iov_len = count};
471 struct iov_iter from;
472 int err = 0;
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700473
Al Viroc711a6b2015-04-01 22:04:46 -0400474 iov_iter_init(&from, WRITE, &iov, 1, count);
Eric Van Hensbergendfb0ec22008-10-13 20:36:16 -0500475
Harsh Prateek Bora3834b122010-08-03 11:55:40 +0000476 retval = generic_write_checks(filp, &origin, &count, 0);
477 if (retval)
Al Viroc711a6b2015-04-01 22:04:46 -0400478 return retval;
Harsh Prateek Bora3834b122010-08-03 11:55:40 +0000479
Al Viroc711a6b2015-04-01 22:04:46 -0400480 iov_iter_truncate(&from, count);
481
482 p9_debug(P9_DEBUG_VFS, "data %p count %d offset %x\n",
483 data, (int)count, (int)*offset);
484
Harsh Prateek Bora3834b122010-08-03 11:55:40 +0000485 retval = 0;
486 if (!count)
Al Viroc711a6b2015-04-01 22:04:46 -0400487 return 0;
Harsh Prateek Bora3834b122010-08-03 11:55:40 +0000488
Al Viroc711a6b2015-04-01 22:04:46 -0400489 retval = p9_client_write(filp->private_data, origin, &from, &err);
490 if (retval > 0) {
491 struct inode *inode = file_inode(filp);
492 loff_t i_size;
493 unsigned long pg_start, pg_end;
494 pg_start = origin >> PAGE_CACHE_SHIFT;
495 pg_end = (origin + retval - 1) >> PAGE_CACHE_SHIFT;
496 if (inode->i_mapping && inode->i_mapping->nrpages)
497 invalidate_inode_pages2_range(inode->i_mapping,
498 pg_start, pg_end);
499 *offset += retval;
500 i_size = i_size_read(inode);
501 if (*offset > i_size) {
502 inode_add_bytes(inode, *offset - i_size);
503 i_size_write(inode, *offset);
504 }
505 return retval;
506 }
507 return err;
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700508}
509
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530510
Josef Bacik02c24a82011-07-16 20:44:56 -0400511static int v9fs_file_fsync(struct file *filp, loff_t start, loff_t end,
512 int datasync)
M. Mohan Kumar7a4439c2010-02-08 15:36:48 -0600513{
514 struct p9_fid *fid;
Josef Bacik02c24a82011-07-16 20:44:56 -0400515 struct inode *inode = filp->f_mapping->host;
M. Mohan Kumar7a4439c2010-02-08 15:36:48 -0600516 struct p9_wstat wstat;
517 int retval;
518
Josef Bacik02c24a82011-07-16 20:44:56 -0400519 retval = filemap_write_and_wait_range(inode->i_mapping, start, end);
520 if (retval)
521 return retval;
522
523 mutex_lock(&inode->i_mutex);
Joe Perches5d385152011-11-28 10:40:46 -0800524 p9_debug(P9_DEBUG_VFS, "filp %p datasync %x\n", filp, datasync);
M. Mohan Kumar7a4439c2010-02-08 15:36:48 -0600525
526 fid = filp->private_data;
527 v9fs_blank_wstat(&wstat);
528
529 retval = p9_client_wstat(fid, &wstat);
Josef Bacik02c24a82011-07-16 20:44:56 -0400530 mutex_unlock(&inode->i_mutex);
531
M. Mohan Kumar7a4439c2010-02-08 15:36:48 -0600532 return retval;
533}
534
Josef Bacik02c24a82011-07-16 20:44:56 -0400535int v9fs_file_fsync_dotl(struct file *filp, loff_t start, loff_t end,
536 int datasync)
Venkateswararao Jujjuri (JV)920e65d2010-09-22 17:19:19 -0700537{
538 struct p9_fid *fid;
Josef Bacik02c24a82011-07-16 20:44:56 -0400539 struct inode *inode = filp->f_mapping->host;
Venkateswararao Jujjuri (JV)920e65d2010-09-22 17:19:19 -0700540 int retval;
541
Josef Bacik02c24a82011-07-16 20:44:56 -0400542 retval = filemap_write_and_wait_range(inode->i_mapping, start, end);
543 if (retval)
544 return retval;
545
546 mutex_lock(&inode->i_mutex);
Joe Perches5d385152011-11-28 10:40:46 -0800547 p9_debug(P9_DEBUG_VFS, "filp %p datasync %x\n", filp, datasync);
Venkateswararao Jujjuri (JV)920e65d2010-09-22 17:19:19 -0700548
549 fid = filp->private_data;
550
Venkateswararao Jujjuri (JV)b165d602010-10-22 10:13:12 -0700551 retval = p9_client_fsync(fid, datasync);
Josef Bacik02c24a82011-07-16 20:44:56 -0400552 mutex_unlock(&inode->i_mutex);
553
Venkateswararao Jujjuri (JV)920e65d2010-09-22 17:19:19 -0700554 return retval;
555}
556
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530557static int
Dominique Martinetfb89b452014-01-10 13:44:09 +0100558v9fs_file_mmap(struct file *filp, struct vm_area_struct *vma)
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530559{
560 int retval;
561
Dominique Martinetfb89b452014-01-10 13:44:09 +0100562
563 retval = generic_file_mmap(filp, vma);
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530564 if (!retval)
565 vma->vm_ops = &v9fs_file_vm_ops;
566
567 return retval;
568}
569
570static int
Dominique Martinetfb89b452014-01-10 13:44:09 +0100571v9fs_mmap_file_mmap(struct file *filp, struct vm_area_struct *vma)
572{
573 int retval;
574 struct inode *inode;
575 struct v9fs_inode *v9inode;
576 struct p9_fid *fid;
577
578 inode = file_inode(filp);
579 v9inode = V9FS_I(inode);
580 mutex_lock(&v9inode->v_mutex);
581 if (!v9inode->writeback_fid &&
582 (vma->vm_flags & VM_WRITE)) {
583 /*
584 * clone a fid and add it to writeback_fid
585 * we do it during mmap instead of
586 * page dirty time via write_begin/page_mkwrite
587 * because we want write after unlink usecase
588 * to work.
589 */
590 fid = v9fs_writeback_fid(filp->f_path.dentry);
591 if (IS_ERR(fid)) {
592 retval = PTR_ERR(fid);
593 mutex_unlock(&v9inode->v_mutex);
594 return retval;
595 }
596 v9inode->writeback_fid = (void *) fid;
597 }
598 mutex_unlock(&v9inode->v_mutex);
599
600 retval = generic_file_mmap(filp, vma);
601 if (!retval)
602 vma->vm_ops = &v9fs_mmap_file_vm_ops;
603
604 return retval;
605}
606
607static int
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530608v9fs_vm_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
609{
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530610 struct v9fs_inode *v9inode;
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530611 struct page *page = vmf->page;
612 struct file *filp = vma->vm_file;
Al Viro496ad9a2013-01-23 17:07:38 -0500613 struct inode *inode = file_inode(filp);
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530614
615
Joe Perches5d385152011-11-28 10:40:46 -0800616 p9_debug(P9_DEBUG_VFS, "page %p fid %lx\n",
617 page, (unsigned long)filp->private_data);
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530618
Jan Kara120c2bc2012-06-12 16:20:25 +0200619 /* Update file times before taking page lock */
620 file_update_time(filp);
621
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530622 v9inode = V9FS_I(inode);
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530623 /* make sure the cache has finished storing the page */
624 v9fs_fscache_wait_on_page_write(inode, page);
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530625 BUG_ON(!v9inode->writeback_fid);
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530626 lock_page(page);
627 if (page->mapping != inode->i_mapping)
628 goto out_unlock;
Darrick J. Wong13575ca2013-02-21 16:42:53 -0800629 wait_for_stable_page(page);
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530630
631 return VM_FAULT_LOCKED;
632out_unlock:
633 unlock_page(page);
634 return VM_FAULT_NOPAGE;
635}
636
Aneesh Kumar K.Ve959b542011-02-28 17:04:04 +0530637static ssize_t
638v9fs_direct_read(struct file *filp, char __user *udata, size_t count,
639 loff_t *offsetp)
640{
641 loff_t size, offset;
642 struct inode *inode;
643 struct address_space *mapping;
644
645 offset = *offsetp;
646 mapping = filp->f_mapping;
647 inode = mapping->host;
648 if (!count)
649 return 0;
650 size = i_size_read(inode);
651 if (offset < size)
652 filemap_write_and_wait_range(mapping, offset,
653 offset + count - 1);
654
655 return v9fs_file_read(filp, udata, count, offsetp);
656}
657
658/**
659 * v9fs_cached_file_read - read from a file
660 * @filp: file pointer to read
Fabian Frederickfd2916b2014-06-04 16:06:26 -0700661 * @data: user data buffer to read data into
Aneesh Kumar K.Ve959b542011-02-28 17:04:04 +0530662 * @count: size of buffer
663 * @offset: offset at which to read data
664 *
665 */
666static ssize_t
667v9fs_cached_file_read(struct file *filp, char __user *data, size_t count,
668 loff_t *offset)
669{
670 if (filp->f_flags & O_DIRECT)
671 return v9fs_direct_read(filp, data, count, offset);
Al Viroaad4f8b2014-04-02 14:33:16 -0400672 return new_sync_read(filp, data, count, offset);
Aneesh Kumar K.Ve959b542011-02-28 17:04:04 +0530673}
674
Dominique Martinetfb89b452014-01-10 13:44:09 +0100675/**
676 * v9fs_mmap_file_read - read from a file
677 * @filp: file pointer to read
Fabian Frederickfd2916b2014-06-04 16:06:26 -0700678 * @data: user data buffer to read data into
Dominique Martinetfb89b452014-01-10 13:44:09 +0100679 * @count: size of buffer
680 * @offset: offset at which to read data
681 *
682 */
683static ssize_t
684v9fs_mmap_file_read(struct file *filp, char __user *data, size_t count,
685 loff_t *offset)
686{
687 /* TODO: Check if there are dirty pages */
688 return v9fs_file_read(filp, data, count, offset);
689}
690
Aneesh Kumar K.Ve959b542011-02-28 17:04:04 +0530691static ssize_t
692v9fs_direct_write(struct file *filp, const char __user * data,
693 size_t count, loff_t *offsetp)
694{
695 loff_t offset;
696 ssize_t retval;
697 struct inode *inode;
698 struct address_space *mapping;
699
700 offset = *offsetp;
701 mapping = filp->f_mapping;
702 inode = mapping->host;
703 if (!count)
704 return 0;
705
706 mutex_lock(&inode->i_mutex);
707 retval = filemap_write_and_wait_range(mapping, offset,
708 offset + count - 1);
709 if (retval)
710 goto err_out;
711 /*
712 * After a write we want buffered reads to be sure to go to disk to get
713 * the new data. We invalidate clean cached page from the region we're
714 * about to write. We do this *before* the write so that if we fail
715 * here we fall back to buffered write
716 */
717 if (mapping->nrpages) {
718 pgoff_t pg_start = offset >> PAGE_CACHE_SHIFT;
719 pgoff_t pg_end = (offset + count - 1) >> PAGE_CACHE_SHIFT;
720
721 retval = invalidate_inode_pages2_range(mapping,
722 pg_start, pg_end);
723 /*
724 * If a page can not be invalidated, fall back
725 * to buffered write.
726 */
727 if (retval) {
728 if (retval == -EBUSY)
729 goto buff_write;
730 goto err_out;
731 }
732 }
733 retval = v9fs_file_write(filp, data, count, offsetp);
734err_out:
735 mutex_unlock(&inode->i_mutex);
736 return retval;
737
738buff_write:
739 mutex_unlock(&inode->i_mutex);
Al Viro81742022014-04-03 03:17:43 -0400740 return new_sync_write(filp, data, count, offsetp);
Aneesh Kumar K.Ve959b542011-02-28 17:04:04 +0530741}
742
743/**
744 * v9fs_cached_file_write - write to a file
745 * @filp: file pointer to write
746 * @data: data buffer to write data from
747 * @count: size of buffer
748 * @offset: offset at which to write data
749 *
750 */
751static ssize_t
752v9fs_cached_file_write(struct file *filp, const char __user * data,
753 size_t count, loff_t *offset)
754{
755
756 if (filp->f_flags & O_DIRECT)
757 return v9fs_direct_write(filp, data, count, offset);
Al Viro81742022014-04-03 03:17:43 -0400758 return new_sync_write(filp, data, count, offset);
Aneesh Kumar K.Ve959b542011-02-28 17:04:04 +0530759}
760
Dominique Martinetfb89b452014-01-10 13:44:09 +0100761
762/**
763 * v9fs_mmap_file_write - write to a file
764 * @filp: file pointer to write
765 * @data: data buffer to write data from
766 * @count: size of buffer
767 * @offset: offset at which to write data
768 *
769 */
770static ssize_t
771v9fs_mmap_file_write(struct file *filp, const char __user *data,
772 size_t count, loff_t *offset)
773{
774 /*
775 * TODO: invalidate mmaps on filp's inode between
776 * offset and offset+count
777 */
778 return v9fs_file_write(filp, data, count, offset);
779}
780
781static void v9fs_mmap_vm_close(struct vm_area_struct *vma)
782{
783 struct inode *inode;
784
785 struct writeback_control wbc = {
786 .nr_to_write = LONG_MAX,
787 .sync_mode = WB_SYNC_ALL,
788 .range_start = vma->vm_pgoff * PAGE_SIZE,
789 /* absolute end, byte at end included */
790 .range_end = vma->vm_pgoff * PAGE_SIZE +
791 (vma->vm_end - vma->vm_start - 1),
792 };
793
794
795 p9_debug(P9_DEBUG_VFS, "9p VMA close, %p, flushing", vma);
796
797 inode = file_inode(vma->vm_file);
798
799 if (!mapping_cap_writeback_dirty(inode->i_mapping))
800 wbc.nr_to_write = 0;
801
802 might_sleep();
803 sync_inode(inode, &wbc);
804}
805
806
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530807static const struct vm_operations_struct v9fs_file_vm_ops = {
808 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -0700809 .map_pages = filemap_map_pages,
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530810 .page_mkwrite = v9fs_vm_page_mkwrite,
811};
812
Dominique Martinetfb89b452014-01-10 13:44:09 +0100813static const struct vm_operations_struct v9fs_mmap_file_vm_ops = {
814 .close = v9fs_mmap_vm_close,
815 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -0700816 .map_pages = filemap_map_pages,
Dominique Martinetfb89b452014-01-10 13:44:09 +0100817 .page_mkwrite = v9fs_vm_page_mkwrite,
Dominique Martinetfb89b452014-01-10 13:44:09 +0100818};
819
Aneesh Kumar K.Ve959b542011-02-28 17:04:04 +0530820
Aneesh Kumar K.V29236f42011-02-28 17:03:54 +0530821const struct file_operations v9fs_cached_file_operations = {
Eric Van Hensbergene03abc02007-02-11 13:21:39 -0600822 .llseek = generic_file_llseek,
Aneesh Kumar K.Ve959b542011-02-28 17:04:04 +0530823 .read = v9fs_cached_file_read,
824 .write = v9fs_cached_file_write,
Al Viroaad4f8b2014-04-02 14:33:16 -0400825 .read_iter = generic_file_read_iter,
Al Viro81742022014-04-03 03:17:43 -0400826 .write_iter = generic_file_write_iter,
Eric Van Hensbergene03abc02007-02-11 13:21:39 -0600827 .open = v9fs_file_open,
828 .release = v9fs_dir_release,
829 .lock = v9fs_file_lock,
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530830 .mmap = v9fs_file_mmap,
M. Mohan Kumar7a4439c2010-02-08 15:36:48 -0600831 .fsync = v9fs_file_fsync,
Eric Van Hensbergene03abc02007-02-11 13:21:39 -0600832};
833
Aneesh Kumar K.V29236f42011-02-28 17:03:54 +0530834const struct file_operations v9fs_cached_file_operations_dotl = {
Venkateswararao Jujjuri (JV)b04faaf2010-09-22 16:30:52 -0700835 .llseek = generic_file_llseek,
Aneesh Kumar K.Ve959b542011-02-28 17:04:04 +0530836 .read = v9fs_cached_file_read,
837 .write = v9fs_cached_file_write,
Al Viroaad4f8b2014-04-02 14:33:16 -0400838 .read_iter = generic_file_read_iter,
Al Viro81742022014-04-03 03:17:43 -0400839 .write_iter = generic_file_write_iter,
Venkateswararao Jujjuri (JV)b04faaf2010-09-22 16:30:52 -0700840 .open = v9fs_file_open,
841 .release = v9fs_dir_release,
M. Mohan Kumara0990272010-09-27 11:34:24 +0530842 .lock = v9fs_file_lock_dotl,
843 .flock = v9fs_file_flock_dotl,
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530844 .mmap = v9fs_file_mmap,
Venkateswararao Jujjuri (JV)920e65d2010-09-22 17:19:19 -0700845 .fsync = v9fs_file_fsync_dotl,
Venkateswararao Jujjuri (JV)b04faaf2010-09-22 16:30:52 -0700846};
847
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800848const struct file_operations v9fs_file_operations = {
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700849 .llseek = generic_file_llseek,
850 .read = v9fs_file_read,
851 .write = v9fs_file_write,
852 .open = v9fs_file_open,
853 .release = v9fs_dir_release,
854 .lock = v9fs_file_lock,
Eric Van Hensbergen14b88692008-02-06 19:25:05 -0600855 .mmap = generic_file_readonly_mmap,
M. Mohan Kumar7a4439c2010-02-08 15:36:48 -0600856 .fsync = v9fs_file_fsync,
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700857};
Sripathi Kodi9b6533c2010-03-25 12:41:54 +0000858
859const struct file_operations v9fs_file_operations_dotl = {
860 .llseek = generic_file_llseek,
861 .read = v9fs_file_read,
862 .write = v9fs_file_write,
863 .open = v9fs_file_open,
864 .release = v9fs_dir_release,
M. Mohan Kumara0990272010-09-27 11:34:24 +0530865 .lock = v9fs_file_lock_dotl,
866 .flock = v9fs_file_flock_dotl,
Sripathi Kodi9b6533c2010-03-25 12:41:54 +0000867 .mmap = generic_file_readonly_mmap,
Venkateswararao Jujjuri (JV)920e65d2010-09-22 17:19:19 -0700868 .fsync = v9fs_file_fsync_dotl,
Sripathi Kodi9b6533c2010-03-25 12:41:54 +0000869};
Dominique Martinetfb89b452014-01-10 13:44:09 +0100870
871const struct file_operations v9fs_mmap_file_operations = {
872 .llseek = generic_file_llseek,
873 .read = v9fs_mmap_file_read,
874 .write = v9fs_mmap_file_write,
875 .open = v9fs_file_open,
876 .release = v9fs_dir_release,
877 .lock = v9fs_file_lock,
878 .mmap = v9fs_mmap_file_mmap,
879 .fsync = v9fs_file_fsync,
880};
881
882const struct file_operations v9fs_mmap_file_operations_dotl = {
883 .llseek = generic_file_llseek,
884 .read = v9fs_mmap_file_read,
885 .write = v9fs_mmap_file_write,
886 .open = v9fs_file_open,
887 .release = v9fs_dir_release,
888 .lock = v9fs_file_lock_dotl,
889 .flock = v9fs_file_flock_dotl,
890 .mmap = v9fs_mmap_file_mmap,
891 .fsync = v9fs_file_fsync_dotl,
892};