blob: 9612e5fc0ae2416b53ac78168ab1bddd900b307d [file] [log] [blame]
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -07001/*
2 * linux/fs/9p/vfs_file.c
3 *
4 * This file contians vfs file ops for 9P2000.
5 *
6 * Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com>
7 * Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov>
8 *
9 * This program is free software; you can redistribute it and/or modify
Eric Van Hensbergen42e8c502006-03-25 03:07:28 -080010 * it under the terms of the GNU General Public License version 2
11 * as published by the Free Software Foundation.
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070012 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to:
20 * Free Software Foundation
21 * 51 Franklin Street, Fifth Floor
22 * Boston, MA 02111-1301 USA
23 *
24 */
25
26#include <linux/module.h>
27#include <linux/errno.h>
28#include <linux/fs.h>
Al Viro914e2632006-10-18 13:55:46 -040029#include <linux/sched.h>
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070030#include <linux/file.h>
31#include <linux/stat.h>
32#include <linux/string.h>
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070033#include <linux/inet.h>
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070034#include <linux/list.h>
Abhishek Kulkarni637d0202009-09-22 11:34:05 -050035#include <linux/pagemap.h>
M. Mohan Kumara0990272010-09-27 11:34:24 +053036#include <linux/utsname.h>
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070037#include <asm/uaccess.h>
38#include <linux/idr.h>
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050039#include <net/9p/9p.h>
40#include <net/9p/client.h>
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070041
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070042#include "v9fs.h"
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070043#include "v9fs_vfs.h"
44#include "fid.h"
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -050045#include "cache.h"
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070046
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +053047static const struct vm_operations_struct v9fs_file_vm_ops;
Dominique Martinetfb89b452014-01-10 13:44:09 +010048static const struct vm_operations_struct v9fs_mmap_file_vm_ops;
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +053049
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070050/**
51 * v9fs_file_open - open a file (or directory)
52 * @inode: inode to be opened
53 * @file: file being opened
54 *
55 */
56
57int v9fs_file_open(struct inode *inode, struct file *file)
58{
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -080059 int err;
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +053060 struct v9fs_inode *v9inode;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050061 struct v9fs_session_info *v9ses;
62 struct p9_fid *fid;
63 int omode;
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070064
Joe Perches5d385152011-11-28 10:40:46 -080065 p9_debug(P9_DEBUG_VFS, "inode: %p file: %p\n", inode, file);
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +053066 v9inode = V9FS_I(inode);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050067 v9ses = v9fs_inode2v9ses(inode);
M. Mohan Kumaref565472010-06-22 19:47:50 +053068 if (v9fs_proto_dotl(v9ses))
Aneesh Kumar K.Vf88657c2011-08-03 19:55:32 +053069 omode = v9fs_open_to_dotl_flags(file->f_flags);
M. Mohan Kumaref565472010-06-22 19:47:50 +053070 else
71 omode = v9fs_uflags2omode(file->f_flags,
72 v9fs_proto_dotu(v9ses));
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050073 fid = file->private_data;
74 if (!fid) {
75 fid = v9fs_fid_clone(file->f_path.dentry);
76 if (IS_ERR(fid))
77 return PTR_ERR(fid);
78
79 err = p9_client_open(fid, omode);
Eric Van Hensbergen9523a842007-07-13 13:01:27 -050080 if (err < 0) {
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050081 p9_client_clunk(fid);
82 return err;
83 }
M. Mohan Kumaref565472010-06-22 19:47:50 +053084 if ((file->f_flags & O_APPEND) &&
85 (!v9fs_proto_dotu(v9ses) && !v9fs_proto_dotl(v9ses)))
Eric Van Hensbergen2e4bef42008-06-24 17:39:39 -050086 generic_file_llseek(file, 0, SEEK_END);
Latchesar Ionkov6a3124a2006-03-02 02:54:30 -080087 }
88
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050089 file->private_data = fid;
Aneesh Kumar K.V5a7e0a82011-03-08 16:39:46 +053090 mutex_lock(&v9inode->v_mutex);
Dominique Martinetfb89b452014-01-10 13:44:09 +010091 if ((v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) &&
92 !v9inode->writeback_fid &&
Aneesh Kumar K.V7add6972011-03-08 16:39:49 +053093 ((file->f_flags & O_ACCMODE) != O_RDONLY)) {
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +053094 /*
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +053095 * clone a fid and add it to writeback_fid
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +053096 * we do it during open time instead of
97 * page dirty time via write_begin/page_mkwrite
98 * because we want write after unlink usecase
99 * to work.
100 */
101 fid = v9fs_writeback_fid(file->f_path.dentry);
102 if (IS_ERR(fid)) {
103 err = PTR_ERR(fid);
Aneesh Kumar K.V5a7e0a82011-03-08 16:39:46 +0530104 mutex_unlock(&v9inode->v_mutex);
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +0530105 goto out_error;
106 }
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530107 v9inode->writeback_fid = (void *) fid;
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +0530108 }
Aneesh Kumar K.V5a7e0a82011-03-08 16:39:46 +0530109 mutex_unlock(&v9inode->v_mutex);
Dominique Martinetfb89b452014-01-10 13:44:09 +0100110 if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE)
Abhishek Kulkarni60e78d22009-09-23 13:00:27 -0500111 v9fs_cache_inode_set_cookie(inode, file);
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700112 return 0;
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +0530113out_error:
114 p9_client_clunk(file->private_data);
115 file->private_data = NULL;
116 return err;
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700117}
118
119/**
120 * v9fs_file_lock - lock a file (or directory)
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600121 * @filp: file to be locked
122 * @cmd: lock command
123 * @fl: file lock structure
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700124 *
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600125 * Bugs: this looks like a local only lock, we should extend into 9P
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700126 * by using open exclusive
127 */
128
129static int v9fs_file_lock(struct file *filp, int cmd, struct file_lock *fl)
130{
131 int res = 0;
Al Viro496ad9a2013-01-23 17:07:38 -0500132 struct inode *inode = file_inode(filp);
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700133
Joe Perches5d385152011-11-28 10:40:46 -0800134 p9_debug(P9_DEBUG_VFS, "filp: %p lock: %p\n", filp, fl);
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700135
136 /* No mandatory locks */
Sachin Prabhuf78233d2010-03-13 09:03:55 -0600137 if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700138 return -ENOLCK;
139
140 if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) {
OGAWA Hirofumi28fd1292006-01-08 01:02:14 -0800141 filemap_write_and_wait(inode->i_mapping);
Andrew Mortonfc0ecff2007-02-10 01:45:39 -0800142 invalidate_mapping_pages(&inode->i_data, 0, -1);
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700143 }
144
145 return res;
146}
147
M. Mohan Kumara0990272010-09-27 11:34:24 +0530148static int v9fs_file_do_lock(struct file *filp, int cmd, struct file_lock *fl)
149{
150 struct p9_flock flock;
151 struct p9_fid *fid;
152 uint8_t status;
153 int res = 0;
154 unsigned char fl_type;
155
156 fid = filp->private_data;
157 BUG_ON(fid == NULL);
158
159 if ((fl->fl_flags & FL_POSIX) != FL_POSIX)
160 BUG();
161
162 res = posix_lock_file_wait(filp, fl);
163 if (res < 0)
164 goto out;
165
166 /* convert posix lock to p9 tlock args */
167 memset(&flock, 0, sizeof(flock));
Jim Garlick51b8b4f2011-08-21 00:21:18 +0530168 /* map the lock type */
169 switch (fl->fl_type) {
170 case F_RDLCK:
171 flock.type = P9_LOCK_TYPE_RDLCK;
172 break;
173 case F_WRLCK:
174 flock.type = P9_LOCK_TYPE_WRLCK;
175 break;
176 case F_UNLCK:
177 flock.type = P9_LOCK_TYPE_UNLCK;
178 break;
179 }
M. Mohan Kumara0990272010-09-27 11:34:24 +0530180 flock.start = fl->fl_start;
181 if (fl->fl_end == OFFSET_MAX)
182 flock.length = 0;
183 else
184 flock.length = fl->fl_end - fl->fl_start + 1;
185 flock.proc_id = fl->fl_pid;
Will Deacon50192ab2013-08-21 18:24:47 +0100186 flock.client_id = fid->clnt->name;
M. Mohan Kumara0990272010-09-27 11:34:24 +0530187 if (IS_SETLKW(cmd))
188 flock.flags = P9_LOCK_FLAGS_BLOCK;
189
190 /*
191 * if its a blocked request and we get P9_LOCK_BLOCKED as the status
192 * for lock request, keep on trying
193 */
194 for (;;) {
195 res = p9_client_lock_dotl(fid, &flock, &status);
196 if (res < 0)
Kirill A. Shutemovad804922014-12-29 15:00:18 +0200197 goto out_unlock;
M. Mohan Kumara0990272010-09-27 11:34:24 +0530198
199 if (status != P9_LOCK_BLOCKED)
200 break;
201 if (status == P9_LOCK_BLOCKED && !IS_SETLKW(cmd))
202 break;
Jim Garlicka0ea7872012-01-03 15:27:50 -0600203 if (schedule_timeout_interruptible(P9_LOCK_TIMEOUT) != 0)
204 break;
M. Mohan Kumara0990272010-09-27 11:34:24 +0530205 }
206
207 /* map 9p status to VFS status */
208 switch (status) {
209 case P9_LOCK_SUCCESS:
210 res = 0;
211 break;
212 case P9_LOCK_BLOCKED:
213 res = -EAGAIN;
214 break;
Kirill A. Shutemovb642f722014-12-29 15:00:19 +0200215 default:
216 WARN_ONCE(1, "unknown lock status code: %d\n", status);
217 /* fallthough */
M. Mohan Kumara0990272010-09-27 11:34:24 +0530218 case P9_LOCK_ERROR:
219 case P9_LOCK_GRACE:
220 res = -ENOLCK;
221 break;
M. Mohan Kumara0990272010-09-27 11:34:24 +0530222 }
223
Kirill A. Shutemovad804922014-12-29 15:00:18 +0200224out_unlock:
M. Mohan Kumara0990272010-09-27 11:34:24 +0530225 /*
226 * incase server returned error for lock request, revert
227 * it locally
228 */
229 if (res < 0 && fl->fl_type != F_UNLCK) {
230 fl_type = fl->fl_type;
231 fl->fl_type = F_UNLCK;
232 res = posix_lock_file_wait(filp, fl);
233 fl->fl_type = fl_type;
234 }
235out:
236 return res;
237}
238
M. Mohan Kumar1d769cd2010-09-27 12:22:13 +0530239static int v9fs_file_getlock(struct file *filp, struct file_lock *fl)
240{
241 struct p9_getlock glock;
242 struct p9_fid *fid;
243 int res = 0;
244
245 fid = filp->private_data;
246 BUG_ON(fid == NULL);
247
248 posix_test_lock(filp, fl);
249 /*
250 * if we have a conflicting lock locally, no need to validate
251 * with server
252 */
253 if (fl->fl_type != F_UNLCK)
254 return res;
255
256 /* convert posix lock to p9 tgetlock args */
257 memset(&glock, 0, sizeof(glock));
Jim Garlick51b8b4f2011-08-21 00:21:18 +0530258 glock.type = P9_LOCK_TYPE_UNLCK;
M. Mohan Kumar1d769cd2010-09-27 12:22:13 +0530259 glock.start = fl->fl_start;
260 if (fl->fl_end == OFFSET_MAX)
261 glock.length = 0;
262 else
263 glock.length = fl->fl_end - fl->fl_start + 1;
264 glock.proc_id = fl->fl_pid;
Will Deacon50192ab2013-08-21 18:24:47 +0100265 glock.client_id = fid->clnt->name;
M. Mohan Kumar1d769cd2010-09-27 12:22:13 +0530266
267 res = p9_client_getlock_dotl(fid, &glock);
268 if (res < 0)
269 return res;
Jim Garlick51b8b4f2011-08-21 00:21:18 +0530270 /* map 9p lock type to os lock type */
271 switch (glock.type) {
272 case P9_LOCK_TYPE_RDLCK:
273 fl->fl_type = F_RDLCK;
274 break;
275 case P9_LOCK_TYPE_WRLCK:
276 fl->fl_type = F_WRLCK;
277 break;
278 case P9_LOCK_TYPE_UNLCK:
279 fl->fl_type = F_UNLCK;
280 break;
281 }
282 if (glock.type != P9_LOCK_TYPE_UNLCK) {
M. Mohan Kumar1d769cd2010-09-27 12:22:13 +0530283 fl->fl_start = glock.start;
284 if (glock.length == 0)
285 fl->fl_end = OFFSET_MAX;
286 else
287 fl->fl_end = glock.start + glock.length - 1;
288 fl->fl_pid = glock.proc_id;
Jim Garlick51b8b4f2011-08-21 00:21:18 +0530289 }
M. Mohan Kumar1d769cd2010-09-27 12:22:13 +0530290 return res;
291}
292
M. Mohan Kumara0990272010-09-27 11:34:24 +0530293/**
294 * v9fs_file_lock_dotl - lock a file (or directory)
295 * @filp: file to be locked
296 * @cmd: lock command
297 * @fl: file lock structure
298 *
299 */
300
301static int v9fs_file_lock_dotl(struct file *filp, int cmd, struct file_lock *fl)
302{
Al Viro496ad9a2013-01-23 17:07:38 -0500303 struct inode *inode = file_inode(filp);
M. Mohan Kumara0990272010-09-27 11:34:24 +0530304 int ret = -ENOLCK;
305
Al Viro4b8e9922014-08-19 20:17:38 -0400306 p9_debug(P9_DEBUG_VFS, "filp: %p cmd:%d lock: %p name: %pD\n",
307 filp, cmd, fl, filp);
M. Mohan Kumara0990272010-09-27 11:34:24 +0530308
309 /* No mandatory locks */
310 if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
311 goto out_err;
312
313 if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) {
314 filemap_write_and_wait(inode->i_mapping);
315 invalidate_mapping_pages(&inode->i_data, 0, -1);
316 }
317
318 if (IS_SETLK(cmd) || IS_SETLKW(cmd))
319 ret = v9fs_file_do_lock(filp, cmd, fl);
M. Mohan Kumar1d769cd2010-09-27 12:22:13 +0530320 else if (IS_GETLK(cmd))
321 ret = v9fs_file_getlock(filp, fl);
M. Mohan Kumara0990272010-09-27 11:34:24 +0530322 else
323 ret = -EINVAL;
324out_err:
325 return ret;
326}
327
328/**
329 * v9fs_file_flock_dotl - lock a file
330 * @filp: file to be locked
331 * @cmd: lock command
332 * @fl: file lock structure
333 *
334 */
335
336static int v9fs_file_flock_dotl(struct file *filp, int cmd,
337 struct file_lock *fl)
338{
Al Viro496ad9a2013-01-23 17:07:38 -0500339 struct inode *inode = file_inode(filp);
M. Mohan Kumara0990272010-09-27 11:34:24 +0530340 int ret = -ENOLCK;
341
Al Viro4b8e9922014-08-19 20:17:38 -0400342 p9_debug(P9_DEBUG_VFS, "filp: %p cmd:%d lock: %p name: %pD\n",
343 filp, cmd, fl, filp);
M. Mohan Kumara0990272010-09-27 11:34:24 +0530344
345 /* No mandatory locks */
346 if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
347 goto out_err;
348
349 if (!(fl->fl_flags & FL_FLOCK))
350 goto out_err;
351
352 if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) {
353 filemap_write_and_wait(inode->i_mapping);
354 invalidate_mapping_pages(&inode->i_data, 0, -1);
355 }
356 /* Convert flock to posix lock */
M. Mohan Kumara0990272010-09-27 11:34:24 +0530357 fl->fl_flags |= FL_POSIX;
358 fl->fl_flags ^= FL_FLOCK;
359
360 if (IS_SETLK(cmd) | IS_SETLKW(cmd))
361 ret = v9fs_file_do_lock(filp, cmd, fl);
362 else
363 ret = -EINVAL;
364out_err:
365 return ret;
366}
367
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700368/**
Aneesh Kumar K.V17311772011-02-28 17:03:56 +0530369 * v9fs_fid_readn - read from a fid
370 * @fid: fid to read
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700371 * @data: data buffer to read data into
Eric Van Hensbergenfbedadc2008-10-13 20:36:16 -0500372 * @udata: user data buffer to read data into
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700373 * @count: size of buffer
374 * @offset: offset at which to read data
375 *
376 */
Eric Van Hensbergenfbedadc2008-10-13 20:36:16 -0500377ssize_t
Aneesh Kumar K.V17311772011-02-28 17:03:56 +0530378v9fs_fid_readn(struct p9_fid *fid, char *data, char __user *udata, u32 count,
Eric Van Hensbergenfbedadc2008-10-13 20:36:16 -0500379 u64 offset)
380{
M. Mohan Kumar97e84422010-06-04 11:59:07 +0000381 int n, total, size;
Eric Van Hensbergenfbedadc2008-10-13 20:36:16 -0500382
Joe Perches5d385152011-11-28 10:40:46 -0800383 p9_debug(P9_DEBUG_VFS, "fid %d offset %llu count %d\n",
384 fid->fid, (long long unsigned)offset, count);
Eric Van Hensbergenfbedadc2008-10-13 20:36:16 -0500385 n = 0;
386 total = 0;
M. Mohan Kumar97e84422010-06-04 11:59:07 +0000387 size = fid->iounit ? fid->iounit : fid->clnt->msize - P9_IOHDRSZ;
Eric Van Hensbergenfbedadc2008-10-13 20:36:16 -0500388 do {
389 n = p9_client_read(fid, data, udata, offset, count);
390 if (n <= 0)
391 break;
392
393 if (data)
394 data += n;
395 if (udata)
396 udata += n;
397
398 offset += n;
399 count -= n;
400 total += n;
M. Mohan Kumar97e84422010-06-04 11:59:07 +0000401 } while (count > 0 && n == size);
Eric Van Hensbergenfbedadc2008-10-13 20:36:16 -0500402
403 if (n < 0)
404 total = n;
405
406 return total;
407}
408
409/**
Aneesh Kumar K.V17311772011-02-28 17:03:56 +0530410 * v9fs_file_readn - read from a file
411 * @filp: file pointer to read
412 * @data: data buffer to read data into
413 * @udata: user data buffer to read data into
414 * @count: size of buffer
415 * @offset: offset at which to read data
416 *
417 */
418ssize_t
419v9fs_file_readn(struct file *filp, char *data, char __user *udata, u32 count,
420 u64 offset)
421{
422 return v9fs_fid_readn(filp->private_data, data, udata, count, offset);
423}
424
425/**
Eric Van Hensbergenfbedadc2008-10-13 20:36:16 -0500426 * v9fs_file_read - read from a file
427 * @filp: file pointer to read
428 * @udata: user data buffer to read data into
429 * @count: size of buffer
430 * @offset: offset at which to read data
431 *
432 */
433
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700434static ssize_t
Eric Van Hensbergenfbedadc2008-10-13 20:36:16 -0500435v9fs_file_read(struct file *filp, char __user *udata, size_t count,
Latchesar Ionkov19cba8a2005-10-11 08:29:03 -0700436 loff_t * offset)
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700437{
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500438 int ret;
439 struct p9_fid *fid;
M. Mohan Kumar97e84422010-06-04 11:59:07 +0000440 size_t size;
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700441
Joe Perches5d385152011-11-28 10:40:46 -0800442 p9_debug(P9_DEBUG_VFS, "count %zu offset %lld\n", count, *offset);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500443 fid = filp->private_data;
Eric Van Hensbergenfbedadc2008-10-13 20:36:16 -0500444
M. Mohan Kumar97e84422010-06-04 11:59:07 +0000445 size = fid->iounit ? fid->iounit : fid->clnt->msize - P9_IOHDRSZ;
446 if (count > size)
Eric Van Hensbergenfbedadc2008-10-13 20:36:16 -0500447 ret = v9fs_file_readn(filp, NULL, udata, count, *offset);
448 else
449 ret = p9_client_read(fid, NULL, udata, *offset, count);
450
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500451 if (ret > 0)
452 *offset += ret;
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700453
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500454 return ret;
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700455}
456
Aneesh Kumar K.V17311772011-02-28 17:03:56 +0530457ssize_t
458v9fs_file_write_internal(struct inode *inode, struct p9_fid *fid,
459 const char __user *data, size_t count,
460 loff_t *offset, int invalidate)
461{
462 int n;
Aneesh Kumar K.Vfa6ea162011-02-28 17:04:04 +0530463 loff_t i_size;
Aneesh Kumar K.V17311772011-02-28 17:03:56 +0530464 size_t total = 0;
Aneesh Kumar K.V17311772011-02-28 17:03:56 +0530465 loff_t origin = *offset;
466 unsigned long pg_start, pg_end;
467
Joe Perches5d385152011-11-28 10:40:46 -0800468 p9_debug(P9_DEBUG_VFS, "data %p count %d offset %x\n",
469 data, (int)count, (int)*offset);
Aneesh Kumar K.V17311772011-02-28 17:03:56 +0530470
Aneesh Kumar K.V17311772011-02-28 17:03:56 +0530471 do {
472 n = p9_client_write(fid, NULL, data+total, origin+total, count);
473 if (n <= 0)
474 break;
475 count -= n;
476 total += n;
477 } while (count > 0);
478
479 if (invalidate && (total > 0)) {
480 pg_start = origin >> PAGE_CACHE_SHIFT;
481 pg_end = (origin + total - 1) >> PAGE_CACHE_SHIFT;
482 if (inode->i_mapping && inode->i_mapping->nrpages)
483 invalidate_inode_pages2_range(inode->i_mapping,
484 pg_start, pg_end);
485 *offset += total;
Aneesh Kumar K.Vfa6ea162011-02-28 17:04:04 +0530486 i_size = i_size_read(inode);
487 if (*offset > i_size) {
488 inode_add_bytes(inode, *offset - i_size);
489 i_size_write(inode, *offset);
490 }
Aneesh Kumar K.V17311772011-02-28 17:03:56 +0530491 }
492 if (n < 0)
493 return n;
494
495 return total;
496}
497
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700498/**
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700499 * v9fs_file_write - write to a file
Eric Van Hensbergenee443992008-03-05 07:08:09 -0600500 * @filp: file pointer to write
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700501 * @data: data buffer to write data from
502 * @count: size of buffer
503 * @offset: offset at which to write data
504 *
505 */
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700506static ssize_t
507v9fs_file_write(struct file *filp, const char __user * data,
Aneesh Kumar K.V17311772011-02-28 17:03:56 +0530508 size_t count, loff_t *offset)
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700509{
Aneesh Kumar K.V17311772011-02-28 17:03:56 +0530510 ssize_t retval = 0;
jvraofc0f2962010-03-08 22:07:02 +0000511 loff_t origin = *offset;
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700512
Eric Van Hensbergendfb0ec22008-10-13 20:36:16 -0500513
Harsh Prateek Bora3834b122010-08-03 11:55:40 +0000514 retval = generic_write_checks(filp, &origin, &count, 0);
515 if (retval)
516 goto out;
517
518 retval = -EINVAL;
519 if ((ssize_t) count < 0)
520 goto out;
521 retval = 0;
522 if (!count)
523 goto out;
524
Al Viro496ad9a2013-01-23 17:07:38 -0500525 retval = v9fs_file_write_internal(file_inode(filp),
Aneesh Kumar K.V17311772011-02-28 17:03:56 +0530526 filp->private_data,
M. Mohan Kumaraaf0ef12011-03-16 21:40:49 +0530527 data, count, &origin, 1);
528 /* update offset on successful write */
529 if (retval > 0)
530 *offset = origin;
Harsh Prateek Bora3834b122010-08-03 11:55:40 +0000531out:
532 return retval;
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700533}
534
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530535
Josef Bacik02c24a82011-07-16 20:44:56 -0400536static int v9fs_file_fsync(struct file *filp, loff_t start, loff_t end,
537 int datasync)
M. Mohan Kumar7a4439c2010-02-08 15:36:48 -0600538{
539 struct p9_fid *fid;
Josef Bacik02c24a82011-07-16 20:44:56 -0400540 struct inode *inode = filp->f_mapping->host;
M. Mohan Kumar7a4439c2010-02-08 15:36:48 -0600541 struct p9_wstat wstat;
542 int retval;
543
Josef Bacik02c24a82011-07-16 20:44:56 -0400544 retval = filemap_write_and_wait_range(inode->i_mapping, start, end);
545 if (retval)
546 return retval;
547
548 mutex_lock(&inode->i_mutex);
Joe Perches5d385152011-11-28 10:40:46 -0800549 p9_debug(P9_DEBUG_VFS, "filp %p datasync %x\n", filp, datasync);
M. Mohan Kumar7a4439c2010-02-08 15:36:48 -0600550
551 fid = filp->private_data;
552 v9fs_blank_wstat(&wstat);
553
554 retval = p9_client_wstat(fid, &wstat);
Josef Bacik02c24a82011-07-16 20:44:56 -0400555 mutex_unlock(&inode->i_mutex);
556
M. Mohan Kumar7a4439c2010-02-08 15:36:48 -0600557 return retval;
558}
559
Josef Bacik02c24a82011-07-16 20:44:56 -0400560int v9fs_file_fsync_dotl(struct file *filp, loff_t start, loff_t end,
561 int datasync)
Venkateswararao Jujjuri (JV)920e65d2010-09-22 17:19:19 -0700562{
563 struct p9_fid *fid;
Josef Bacik02c24a82011-07-16 20:44:56 -0400564 struct inode *inode = filp->f_mapping->host;
Venkateswararao Jujjuri (JV)920e65d2010-09-22 17:19:19 -0700565 int retval;
566
Josef Bacik02c24a82011-07-16 20:44:56 -0400567 retval = filemap_write_and_wait_range(inode->i_mapping, start, end);
568 if (retval)
569 return retval;
570
571 mutex_lock(&inode->i_mutex);
Joe Perches5d385152011-11-28 10:40:46 -0800572 p9_debug(P9_DEBUG_VFS, "filp %p datasync %x\n", filp, datasync);
Venkateswararao Jujjuri (JV)920e65d2010-09-22 17:19:19 -0700573
574 fid = filp->private_data;
575
Venkateswararao Jujjuri (JV)b165d602010-10-22 10:13:12 -0700576 retval = p9_client_fsync(fid, datasync);
Josef Bacik02c24a82011-07-16 20:44:56 -0400577 mutex_unlock(&inode->i_mutex);
578
Venkateswararao Jujjuri (JV)920e65d2010-09-22 17:19:19 -0700579 return retval;
580}
581
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530582static int
Dominique Martinetfb89b452014-01-10 13:44:09 +0100583v9fs_file_mmap(struct file *filp, struct vm_area_struct *vma)
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530584{
585 int retval;
586
Dominique Martinetfb89b452014-01-10 13:44:09 +0100587
588 retval = generic_file_mmap(filp, vma);
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530589 if (!retval)
590 vma->vm_ops = &v9fs_file_vm_ops;
591
592 return retval;
593}
594
595static int
Dominique Martinetfb89b452014-01-10 13:44:09 +0100596v9fs_mmap_file_mmap(struct file *filp, struct vm_area_struct *vma)
597{
598 int retval;
599 struct inode *inode;
600 struct v9fs_inode *v9inode;
601 struct p9_fid *fid;
602
603 inode = file_inode(filp);
604 v9inode = V9FS_I(inode);
605 mutex_lock(&v9inode->v_mutex);
606 if (!v9inode->writeback_fid &&
607 (vma->vm_flags & VM_WRITE)) {
608 /*
609 * clone a fid and add it to writeback_fid
610 * we do it during mmap instead of
611 * page dirty time via write_begin/page_mkwrite
612 * because we want write after unlink usecase
613 * to work.
614 */
615 fid = v9fs_writeback_fid(filp->f_path.dentry);
616 if (IS_ERR(fid)) {
617 retval = PTR_ERR(fid);
618 mutex_unlock(&v9inode->v_mutex);
619 return retval;
620 }
621 v9inode->writeback_fid = (void *) fid;
622 }
623 mutex_unlock(&v9inode->v_mutex);
624
625 retval = generic_file_mmap(filp, vma);
626 if (!retval)
627 vma->vm_ops = &v9fs_mmap_file_vm_ops;
628
629 return retval;
630}
631
632static int
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530633v9fs_vm_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
634{
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530635 struct v9fs_inode *v9inode;
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530636 struct page *page = vmf->page;
637 struct file *filp = vma->vm_file;
Al Viro496ad9a2013-01-23 17:07:38 -0500638 struct inode *inode = file_inode(filp);
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530639
640
Joe Perches5d385152011-11-28 10:40:46 -0800641 p9_debug(P9_DEBUG_VFS, "page %p fid %lx\n",
642 page, (unsigned long)filp->private_data);
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530643
Jan Kara120c2bc2012-06-12 16:20:25 +0200644 /* Update file times before taking page lock */
645 file_update_time(filp);
646
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530647 v9inode = V9FS_I(inode);
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530648 /* make sure the cache has finished storing the page */
649 v9fs_fscache_wait_on_page_write(inode, page);
Aneesh Kumar K.V6b39f6d2011-02-28 17:04:03 +0530650 BUG_ON(!v9inode->writeback_fid);
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530651 lock_page(page);
652 if (page->mapping != inode->i_mapping)
653 goto out_unlock;
Darrick J. Wong13575ca2013-02-21 16:42:53 -0800654 wait_for_stable_page(page);
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530655
656 return VM_FAULT_LOCKED;
657out_unlock:
658 unlock_page(page);
659 return VM_FAULT_NOPAGE;
660}
661
Aneesh Kumar K.Ve959b542011-02-28 17:04:04 +0530662static ssize_t
663v9fs_direct_read(struct file *filp, char __user *udata, size_t count,
664 loff_t *offsetp)
665{
666 loff_t size, offset;
667 struct inode *inode;
668 struct address_space *mapping;
669
670 offset = *offsetp;
671 mapping = filp->f_mapping;
672 inode = mapping->host;
673 if (!count)
674 return 0;
675 size = i_size_read(inode);
676 if (offset < size)
677 filemap_write_and_wait_range(mapping, offset,
678 offset + count - 1);
679
680 return v9fs_file_read(filp, udata, count, offsetp);
681}
682
683/**
684 * v9fs_cached_file_read - read from a file
685 * @filp: file pointer to read
Fabian Frederickfd2916b2014-06-04 16:06:26 -0700686 * @data: user data buffer to read data into
Aneesh Kumar K.Ve959b542011-02-28 17:04:04 +0530687 * @count: size of buffer
688 * @offset: offset at which to read data
689 *
690 */
691static ssize_t
692v9fs_cached_file_read(struct file *filp, char __user *data, size_t count,
693 loff_t *offset)
694{
695 if (filp->f_flags & O_DIRECT)
696 return v9fs_direct_read(filp, data, count, offset);
Al Viroaad4f8b2014-04-02 14:33:16 -0400697 return new_sync_read(filp, data, count, offset);
Aneesh Kumar K.Ve959b542011-02-28 17:04:04 +0530698}
699
Dominique Martinetfb89b452014-01-10 13:44:09 +0100700/**
701 * v9fs_mmap_file_read - read from a file
702 * @filp: file pointer to read
Fabian Frederickfd2916b2014-06-04 16:06:26 -0700703 * @data: user data buffer to read data into
Dominique Martinetfb89b452014-01-10 13:44:09 +0100704 * @count: size of buffer
705 * @offset: offset at which to read data
706 *
707 */
708static ssize_t
709v9fs_mmap_file_read(struct file *filp, char __user *data, size_t count,
710 loff_t *offset)
711{
712 /* TODO: Check if there are dirty pages */
713 return v9fs_file_read(filp, data, count, offset);
714}
715
Aneesh Kumar K.Ve959b542011-02-28 17:04:04 +0530716static ssize_t
717v9fs_direct_write(struct file *filp, const char __user * data,
718 size_t count, loff_t *offsetp)
719{
720 loff_t offset;
721 ssize_t retval;
722 struct inode *inode;
723 struct address_space *mapping;
724
725 offset = *offsetp;
726 mapping = filp->f_mapping;
727 inode = mapping->host;
728 if (!count)
729 return 0;
730
731 mutex_lock(&inode->i_mutex);
732 retval = filemap_write_and_wait_range(mapping, offset,
733 offset + count - 1);
734 if (retval)
735 goto err_out;
736 /*
737 * After a write we want buffered reads to be sure to go to disk to get
738 * the new data. We invalidate clean cached page from the region we're
739 * about to write. We do this *before* the write so that if we fail
740 * here we fall back to buffered write
741 */
742 if (mapping->nrpages) {
743 pgoff_t pg_start = offset >> PAGE_CACHE_SHIFT;
744 pgoff_t pg_end = (offset + count - 1) >> PAGE_CACHE_SHIFT;
745
746 retval = invalidate_inode_pages2_range(mapping,
747 pg_start, pg_end);
748 /*
749 * If a page can not be invalidated, fall back
750 * to buffered write.
751 */
752 if (retval) {
753 if (retval == -EBUSY)
754 goto buff_write;
755 goto err_out;
756 }
757 }
758 retval = v9fs_file_write(filp, data, count, offsetp);
759err_out:
760 mutex_unlock(&inode->i_mutex);
761 return retval;
762
763buff_write:
764 mutex_unlock(&inode->i_mutex);
Al Viro81742022014-04-03 03:17:43 -0400765 return new_sync_write(filp, data, count, offsetp);
Aneesh Kumar K.Ve959b542011-02-28 17:04:04 +0530766}
767
768/**
769 * v9fs_cached_file_write - write to a file
770 * @filp: file pointer to write
771 * @data: data buffer to write data from
772 * @count: size of buffer
773 * @offset: offset at which to write data
774 *
775 */
776static ssize_t
777v9fs_cached_file_write(struct file *filp, const char __user * data,
778 size_t count, loff_t *offset)
779{
780
781 if (filp->f_flags & O_DIRECT)
782 return v9fs_direct_write(filp, data, count, offset);
Al Viro81742022014-04-03 03:17:43 -0400783 return new_sync_write(filp, data, count, offset);
Aneesh Kumar K.Ve959b542011-02-28 17:04:04 +0530784}
785
Dominique Martinetfb89b452014-01-10 13:44:09 +0100786
787/**
788 * v9fs_mmap_file_write - write to a file
789 * @filp: file pointer to write
790 * @data: data buffer to write data from
791 * @count: size of buffer
792 * @offset: offset at which to write data
793 *
794 */
795static ssize_t
796v9fs_mmap_file_write(struct file *filp, const char __user *data,
797 size_t count, loff_t *offset)
798{
799 /*
800 * TODO: invalidate mmaps on filp's inode between
801 * offset and offset+count
802 */
803 return v9fs_file_write(filp, data, count, offset);
804}
805
806static void v9fs_mmap_vm_close(struct vm_area_struct *vma)
807{
808 struct inode *inode;
809
810 struct writeback_control wbc = {
811 .nr_to_write = LONG_MAX,
812 .sync_mode = WB_SYNC_ALL,
813 .range_start = vma->vm_pgoff * PAGE_SIZE,
814 /* absolute end, byte at end included */
815 .range_end = vma->vm_pgoff * PAGE_SIZE +
816 (vma->vm_end - vma->vm_start - 1),
817 };
818
819
820 p9_debug(P9_DEBUG_VFS, "9p VMA close, %p, flushing", vma);
821
822 inode = file_inode(vma->vm_file);
823
824 if (!mapping_cap_writeback_dirty(inode->i_mapping))
825 wbc.nr_to_write = 0;
826
827 might_sleep();
828 sync_inode(inode, &wbc);
829}
830
831
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530832static const struct vm_operations_struct v9fs_file_vm_ops = {
833 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -0700834 .map_pages = filemap_map_pages,
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530835 .page_mkwrite = v9fs_vm_page_mkwrite,
836};
837
Dominique Martinetfb89b452014-01-10 13:44:09 +0100838static const struct vm_operations_struct v9fs_mmap_file_vm_ops = {
839 .close = v9fs_mmap_vm_close,
840 .fault = filemap_fault,
Kirill A. Shutemovf1820362014-04-07 15:37:19 -0700841 .map_pages = filemap_map_pages,
Dominique Martinetfb89b452014-01-10 13:44:09 +0100842 .page_mkwrite = v9fs_vm_page_mkwrite,
Dominique Martinetfb89b452014-01-10 13:44:09 +0100843};
844
Aneesh Kumar K.Ve959b542011-02-28 17:04:04 +0530845
Aneesh Kumar K.V29236f42011-02-28 17:03:54 +0530846const struct file_operations v9fs_cached_file_operations = {
Eric Van Hensbergene03abc02007-02-11 13:21:39 -0600847 .llseek = generic_file_llseek,
Aneesh Kumar K.Ve959b542011-02-28 17:04:04 +0530848 .read = v9fs_cached_file_read,
849 .write = v9fs_cached_file_write,
Al Viroaad4f8b2014-04-02 14:33:16 -0400850 .read_iter = generic_file_read_iter,
Al Viro81742022014-04-03 03:17:43 -0400851 .write_iter = generic_file_write_iter,
Eric Van Hensbergene03abc02007-02-11 13:21:39 -0600852 .open = v9fs_file_open,
853 .release = v9fs_dir_release,
854 .lock = v9fs_file_lock,
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530855 .mmap = v9fs_file_mmap,
M. Mohan Kumar7a4439c2010-02-08 15:36:48 -0600856 .fsync = v9fs_file_fsync,
Eric Van Hensbergene03abc02007-02-11 13:21:39 -0600857};
858
Aneesh Kumar K.V29236f42011-02-28 17:03:54 +0530859const struct file_operations v9fs_cached_file_operations_dotl = {
Venkateswararao Jujjuri (JV)b04faaf2010-09-22 16:30:52 -0700860 .llseek = generic_file_llseek,
Aneesh Kumar K.Ve959b542011-02-28 17:04:04 +0530861 .read = v9fs_cached_file_read,
862 .write = v9fs_cached_file_write,
Al Viroaad4f8b2014-04-02 14:33:16 -0400863 .read_iter = generic_file_read_iter,
Al Viro81742022014-04-03 03:17:43 -0400864 .write_iter = generic_file_write_iter,
Venkateswararao Jujjuri (JV)b04faaf2010-09-22 16:30:52 -0700865 .open = v9fs_file_open,
866 .release = v9fs_dir_release,
M. Mohan Kumara0990272010-09-27 11:34:24 +0530867 .lock = v9fs_file_lock_dotl,
868 .flock = v9fs_file_flock_dotl,
Aneesh Kumar K.V7263ceb2011-02-28 17:03:58 +0530869 .mmap = v9fs_file_mmap,
Venkateswararao Jujjuri (JV)920e65d2010-09-22 17:19:19 -0700870 .fsync = v9fs_file_fsync_dotl,
Venkateswararao Jujjuri (JV)b04faaf2010-09-22 16:30:52 -0700871};
872
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800873const struct file_operations v9fs_file_operations = {
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700874 .llseek = generic_file_llseek,
875 .read = v9fs_file_read,
876 .write = v9fs_file_write,
877 .open = v9fs_file_open,
878 .release = v9fs_dir_release,
879 .lock = v9fs_file_lock,
Eric Van Hensbergen14b88692008-02-06 19:25:05 -0600880 .mmap = generic_file_readonly_mmap,
M. Mohan Kumar7a4439c2010-02-08 15:36:48 -0600881 .fsync = v9fs_file_fsync,
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700882};
Sripathi Kodi9b6533c2010-03-25 12:41:54 +0000883
884const struct file_operations v9fs_file_operations_dotl = {
885 .llseek = generic_file_llseek,
886 .read = v9fs_file_read,
887 .write = v9fs_file_write,
888 .open = v9fs_file_open,
889 .release = v9fs_dir_release,
M. Mohan Kumara0990272010-09-27 11:34:24 +0530890 .lock = v9fs_file_lock_dotl,
891 .flock = v9fs_file_flock_dotl,
Sripathi Kodi9b6533c2010-03-25 12:41:54 +0000892 .mmap = generic_file_readonly_mmap,
Venkateswararao Jujjuri (JV)920e65d2010-09-22 17:19:19 -0700893 .fsync = v9fs_file_fsync_dotl,
Sripathi Kodi9b6533c2010-03-25 12:41:54 +0000894};
Dominique Martinetfb89b452014-01-10 13:44:09 +0100895
896const struct file_operations v9fs_mmap_file_operations = {
897 .llseek = generic_file_llseek,
898 .read = v9fs_mmap_file_read,
899 .write = v9fs_mmap_file_write,
900 .open = v9fs_file_open,
901 .release = v9fs_dir_release,
902 .lock = v9fs_file_lock,
903 .mmap = v9fs_mmap_file_mmap,
904 .fsync = v9fs_file_fsync,
905};
906
907const struct file_operations v9fs_mmap_file_operations_dotl = {
908 .llseek = generic_file_llseek,
909 .read = v9fs_mmap_file_read,
910 .write = v9fs_mmap_file_write,
911 .open = v9fs_file_open,
912 .release = v9fs_dir_release,
913 .lock = v9fs_file_lock_dotl,
914 .flock = v9fs_file_flock_dotl,
915 .mmap = v9fs_mmap_file_mmap,
916 .fsync = v9fs_file_fsync_dotl,
917};