Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 1 | /* |
| 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 Hensbergen | 42e8c50 | 2006-03-25 03:07:28 -0800 | [diff] [blame] | 10 | * it under the terms of the GNU General Public License version 2 |
| 11 | * as published by the Free Software Foundation. |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 12 | * |
| 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 Viro | 914e263 | 2006-10-18 13:55:46 -0400 | [diff] [blame] | 29 | #include <linux/sched.h> |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 30 | #include <linux/file.h> |
| 31 | #include <linux/stat.h> |
| 32 | #include <linux/string.h> |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 33 | #include <linux/inet.h> |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 34 | #include <linux/list.h> |
Abhishek Kulkarni | 637d020 | 2009-09-22 11:34:05 -0500 | [diff] [blame] | 35 | #include <linux/pagemap.h> |
M. Mohan Kumar | a099027 | 2010-09-27 11:34:24 +0530 | [diff] [blame] | 36 | #include <linux/utsname.h> |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 37 | #include <asm/uaccess.h> |
| 38 | #include <linux/idr.h> |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 39 | #include <net/9p/9p.h> |
| 40 | #include <net/9p/client.h> |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 41 | |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 42 | #include "v9fs.h" |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 43 | #include "v9fs_vfs.h" |
| 44 | #include "fid.h" |
Abhishek Kulkarni | 60e78d2 | 2009-09-23 13:00:27 -0500 | [diff] [blame] | 45 | #include "cache.h" |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * v9fs_file_open - open a file (or directory) |
| 49 | * @inode: inode to be opened |
| 50 | * @file: file being opened |
| 51 | * |
| 52 | */ |
| 53 | |
| 54 | int v9fs_file_open(struct inode *inode, struct file *file) |
| 55 | { |
Latchesar Ionkov | 6a3124a | 2006-03-02 02:54:30 -0800 | [diff] [blame] | 56 | int err; |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 57 | struct v9fs_session_info *v9ses; |
| 58 | struct p9_fid *fid; |
| 59 | int omode; |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 60 | |
M. Mohan Kumar | ef56547 | 2010-06-22 19:47:50 +0530 | [diff] [blame] | 61 | P9_DPRINTK(P9_DEBUG_VFS, "inode: %p file: %p\n", inode, file); |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 62 | v9ses = v9fs_inode2v9ses(inode); |
M. Mohan Kumar | ef56547 | 2010-06-22 19:47:50 +0530 | [diff] [blame] | 63 | if (v9fs_proto_dotl(v9ses)) |
| 64 | omode = file->f_flags; |
| 65 | else |
| 66 | omode = v9fs_uflags2omode(file->f_flags, |
| 67 | v9fs_proto_dotu(v9ses)); |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 68 | fid = file->private_data; |
| 69 | if (!fid) { |
| 70 | fid = v9fs_fid_clone(file->f_path.dentry); |
| 71 | if (IS_ERR(fid)) |
| 72 | return PTR_ERR(fid); |
| 73 | |
| 74 | err = p9_client_open(fid, omode); |
Eric Van Hensbergen | 9523a84 | 2007-07-13 13:01:27 -0500 | [diff] [blame] | 75 | if (err < 0) { |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 76 | p9_client_clunk(fid); |
| 77 | return err; |
| 78 | } |
M. Mohan Kumar | ef56547 | 2010-06-22 19:47:50 +0530 | [diff] [blame] | 79 | if (file->f_flags & O_TRUNC) { |
Abhishek Kulkarni | 7549ae3e | 2009-09-22 11:34:05 -0500 | [diff] [blame] | 80 | i_size_write(inode, 0); |
Eric Van Hensbergen | 9523a84 | 2007-07-13 13:01:27 -0500 | [diff] [blame] | 81 | inode->i_blocks = 0; |
| 82 | } |
M. Mohan Kumar | ef56547 | 2010-06-22 19:47:50 +0530 | [diff] [blame] | 83 | if ((file->f_flags & O_APPEND) && |
| 84 | (!v9fs_proto_dotu(v9ses) && !v9fs_proto_dotl(v9ses))) |
Eric Van Hensbergen | 2e4bef4 | 2008-06-24 17:39:39 -0500 | [diff] [blame] | 85 | generic_file_llseek(file, 0, SEEK_END); |
Latchesar Ionkov | 6a3124a | 2006-03-02 02:54:30 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 88 | file->private_data = fid; |
Aneesh Kumar K.V | 29236f4 | 2011-02-28 17:03:54 +0530 | [diff] [blame] | 89 | #ifdef CONFIG_9P_FSCACHE |
Aneesh Kumar K.V | 46848de | 2011-02-28 17:03:55 +0530 | [diff] [blame^] | 90 | if (v9ses->cache) |
Abhishek Kulkarni | 60e78d2 | 2009-09-23 13:00:27 -0500 | [diff] [blame] | 91 | v9fs_cache_inode_set_cookie(inode, file); |
Aneesh Kumar K.V | 29236f4 | 2011-02-28 17:03:54 +0530 | [diff] [blame] | 92 | #endif |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * v9fs_file_lock - lock a file (or directory) |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 98 | * @filp: file to be locked |
| 99 | * @cmd: lock command |
| 100 | * @fl: file lock structure |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 101 | * |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 102 | * Bugs: this looks like a local only lock, we should extend into 9P |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 103 | * by using open exclusive |
| 104 | */ |
| 105 | |
| 106 | static int v9fs_file_lock(struct file *filp, int cmd, struct file_lock *fl) |
| 107 | { |
| 108 | int res = 0; |
Josef "Jeff" Sipek | d6f787b | 2006-12-08 02:36:45 -0800 | [diff] [blame] | 109 | struct inode *inode = filp->f_path.dentry->d_inode; |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 110 | |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 111 | P9_DPRINTK(P9_DEBUG_VFS, "filp: %p lock: %p\n", filp, fl); |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 112 | |
| 113 | /* No mandatory locks */ |
Sachin Prabhu | f78233d | 2010-03-13 09:03:55 -0600 | [diff] [blame] | 114 | if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK) |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 115 | return -ENOLCK; |
| 116 | |
| 117 | if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) { |
OGAWA Hirofumi | 28fd129 | 2006-01-08 01:02:14 -0800 | [diff] [blame] | 118 | filemap_write_and_wait(inode->i_mapping); |
Andrew Morton | fc0ecff | 2007-02-10 01:45:39 -0800 | [diff] [blame] | 119 | invalidate_mapping_pages(&inode->i_data, 0, -1); |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | return res; |
| 123 | } |
| 124 | |
M. Mohan Kumar | a099027 | 2010-09-27 11:34:24 +0530 | [diff] [blame] | 125 | static int v9fs_file_do_lock(struct file *filp, int cmd, struct file_lock *fl) |
| 126 | { |
| 127 | struct p9_flock flock; |
| 128 | struct p9_fid *fid; |
| 129 | uint8_t status; |
| 130 | int res = 0; |
| 131 | unsigned char fl_type; |
| 132 | |
| 133 | fid = filp->private_data; |
| 134 | BUG_ON(fid == NULL); |
| 135 | |
| 136 | if ((fl->fl_flags & FL_POSIX) != FL_POSIX) |
| 137 | BUG(); |
| 138 | |
| 139 | res = posix_lock_file_wait(filp, fl); |
| 140 | if (res < 0) |
| 141 | goto out; |
| 142 | |
| 143 | /* convert posix lock to p9 tlock args */ |
| 144 | memset(&flock, 0, sizeof(flock)); |
| 145 | flock.type = fl->fl_type; |
| 146 | flock.start = fl->fl_start; |
| 147 | if (fl->fl_end == OFFSET_MAX) |
| 148 | flock.length = 0; |
| 149 | else |
| 150 | flock.length = fl->fl_end - fl->fl_start + 1; |
| 151 | flock.proc_id = fl->fl_pid; |
| 152 | flock.client_id = utsname()->nodename; |
| 153 | if (IS_SETLKW(cmd)) |
| 154 | flock.flags = P9_LOCK_FLAGS_BLOCK; |
| 155 | |
| 156 | /* |
| 157 | * if its a blocked request and we get P9_LOCK_BLOCKED as the status |
| 158 | * for lock request, keep on trying |
| 159 | */ |
| 160 | for (;;) { |
| 161 | res = p9_client_lock_dotl(fid, &flock, &status); |
| 162 | if (res < 0) |
| 163 | break; |
| 164 | |
| 165 | if (status != P9_LOCK_BLOCKED) |
| 166 | break; |
| 167 | if (status == P9_LOCK_BLOCKED && !IS_SETLKW(cmd)) |
| 168 | break; |
| 169 | schedule_timeout_interruptible(P9_LOCK_TIMEOUT); |
| 170 | } |
| 171 | |
| 172 | /* map 9p status to VFS status */ |
| 173 | switch (status) { |
| 174 | case P9_LOCK_SUCCESS: |
| 175 | res = 0; |
| 176 | break; |
| 177 | case P9_LOCK_BLOCKED: |
| 178 | res = -EAGAIN; |
| 179 | break; |
| 180 | case P9_LOCK_ERROR: |
| 181 | case P9_LOCK_GRACE: |
| 182 | res = -ENOLCK; |
| 183 | break; |
| 184 | default: |
| 185 | BUG(); |
| 186 | } |
| 187 | |
| 188 | /* |
| 189 | * incase server returned error for lock request, revert |
| 190 | * it locally |
| 191 | */ |
| 192 | if (res < 0 && fl->fl_type != F_UNLCK) { |
| 193 | fl_type = fl->fl_type; |
| 194 | fl->fl_type = F_UNLCK; |
| 195 | res = posix_lock_file_wait(filp, fl); |
| 196 | fl->fl_type = fl_type; |
| 197 | } |
| 198 | out: |
| 199 | return res; |
| 200 | } |
| 201 | |
M. Mohan Kumar | 1d769cd | 2010-09-27 12:22:13 +0530 | [diff] [blame] | 202 | static int v9fs_file_getlock(struct file *filp, struct file_lock *fl) |
| 203 | { |
| 204 | struct p9_getlock glock; |
| 205 | struct p9_fid *fid; |
| 206 | int res = 0; |
| 207 | |
| 208 | fid = filp->private_data; |
| 209 | BUG_ON(fid == NULL); |
| 210 | |
| 211 | posix_test_lock(filp, fl); |
| 212 | /* |
| 213 | * if we have a conflicting lock locally, no need to validate |
| 214 | * with server |
| 215 | */ |
| 216 | if (fl->fl_type != F_UNLCK) |
| 217 | return res; |
| 218 | |
| 219 | /* convert posix lock to p9 tgetlock args */ |
| 220 | memset(&glock, 0, sizeof(glock)); |
| 221 | glock.type = fl->fl_type; |
| 222 | glock.start = fl->fl_start; |
| 223 | if (fl->fl_end == OFFSET_MAX) |
| 224 | glock.length = 0; |
| 225 | else |
| 226 | glock.length = fl->fl_end - fl->fl_start + 1; |
| 227 | glock.proc_id = fl->fl_pid; |
| 228 | glock.client_id = utsname()->nodename; |
| 229 | |
| 230 | res = p9_client_getlock_dotl(fid, &glock); |
| 231 | if (res < 0) |
| 232 | return res; |
| 233 | if (glock.type != F_UNLCK) { |
| 234 | fl->fl_type = glock.type; |
| 235 | fl->fl_start = glock.start; |
| 236 | if (glock.length == 0) |
| 237 | fl->fl_end = OFFSET_MAX; |
| 238 | else |
| 239 | fl->fl_end = glock.start + glock.length - 1; |
| 240 | fl->fl_pid = glock.proc_id; |
| 241 | } else |
| 242 | fl->fl_type = F_UNLCK; |
| 243 | |
| 244 | return res; |
| 245 | } |
| 246 | |
M. Mohan Kumar | a099027 | 2010-09-27 11:34:24 +0530 | [diff] [blame] | 247 | /** |
| 248 | * v9fs_file_lock_dotl - lock a file (or directory) |
| 249 | * @filp: file to be locked |
| 250 | * @cmd: lock command |
| 251 | * @fl: file lock structure |
| 252 | * |
| 253 | */ |
| 254 | |
| 255 | static int v9fs_file_lock_dotl(struct file *filp, int cmd, struct file_lock *fl) |
| 256 | { |
| 257 | struct inode *inode = filp->f_path.dentry->d_inode; |
| 258 | int ret = -ENOLCK; |
| 259 | |
| 260 | P9_DPRINTK(P9_DEBUG_VFS, "filp: %p cmd:%d lock: %p name: %s\n", filp, |
| 261 | cmd, fl, filp->f_path.dentry->d_name.name); |
| 262 | |
| 263 | /* No mandatory locks */ |
| 264 | if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK) |
| 265 | goto out_err; |
| 266 | |
| 267 | if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) { |
| 268 | filemap_write_and_wait(inode->i_mapping); |
| 269 | invalidate_mapping_pages(&inode->i_data, 0, -1); |
| 270 | } |
| 271 | |
| 272 | if (IS_SETLK(cmd) || IS_SETLKW(cmd)) |
| 273 | ret = v9fs_file_do_lock(filp, cmd, fl); |
M. Mohan Kumar | 1d769cd | 2010-09-27 12:22:13 +0530 | [diff] [blame] | 274 | else if (IS_GETLK(cmd)) |
| 275 | ret = v9fs_file_getlock(filp, fl); |
M. Mohan Kumar | a099027 | 2010-09-27 11:34:24 +0530 | [diff] [blame] | 276 | else |
| 277 | ret = -EINVAL; |
| 278 | out_err: |
| 279 | return ret; |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * v9fs_file_flock_dotl - lock a file |
| 284 | * @filp: file to be locked |
| 285 | * @cmd: lock command |
| 286 | * @fl: file lock structure |
| 287 | * |
| 288 | */ |
| 289 | |
| 290 | static int v9fs_file_flock_dotl(struct file *filp, int cmd, |
| 291 | struct file_lock *fl) |
| 292 | { |
| 293 | struct inode *inode = filp->f_path.dentry->d_inode; |
| 294 | int ret = -ENOLCK; |
| 295 | |
| 296 | P9_DPRINTK(P9_DEBUG_VFS, "filp: %p cmd:%d lock: %p name: %s\n", filp, |
| 297 | cmd, fl, filp->f_path.dentry->d_name.name); |
| 298 | |
| 299 | /* No mandatory locks */ |
| 300 | if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK) |
| 301 | goto out_err; |
| 302 | |
| 303 | if (!(fl->fl_flags & FL_FLOCK)) |
| 304 | goto out_err; |
| 305 | |
| 306 | if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) { |
| 307 | filemap_write_and_wait(inode->i_mapping); |
| 308 | invalidate_mapping_pages(&inode->i_data, 0, -1); |
| 309 | } |
| 310 | /* Convert flock to posix lock */ |
| 311 | fl->fl_owner = (fl_owner_t)filp; |
| 312 | fl->fl_start = 0; |
| 313 | fl->fl_end = OFFSET_MAX; |
| 314 | fl->fl_flags |= FL_POSIX; |
| 315 | fl->fl_flags ^= FL_FLOCK; |
| 316 | |
| 317 | if (IS_SETLK(cmd) | IS_SETLKW(cmd)) |
| 318 | ret = v9fs_file_do_lock(filp, cmd, fl); |
| 319 | else |
| 320 | ret = -EINVAL; |
| 321 | out_err: |
| 322 | return ret; |
| 323 | } |
| 324 | |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 325 | /** |
Eric Van Hensbergen | fbedadc | 2008-10-13 20:36:16 -0500 | [diff] [blame] | 326 | * v9fs_file_readn - read from a file |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 327 | * @filp: file pointer to read |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 328 | * @data: data buffer to read data into |
Eric Van Hensbergen | fbedadc | 2008-10-13 20:36:16 -0500 | [diff] [blame] | 329 | * @udata: user data buffer to read data into |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 330 | * @count: size of buffer |
| 331 | * @offset: offset at which to read data |
| 332 | * |
| 333 | */ |
Eric Van Hensbergen | fbedadc | 2008-10-13 20:36:16 -0500 | [diff] [blame] | 334 | |
| 335 | ssize_t |
| 336 | v9fs_file_readn(struct file *filp, char *data, char __user *udata, u32 count, |
| 337 | u64 offset) |
| 338 | { |
M. Mohan Kumar | 97e8442 | 2010-06-04 11:59:07 +0000 | [diff] [blame] | 339 | int n, total, size; |
Eric Van Hensbergen | fbedadc | 2008-10-13 20:36:16 -0500 | [diff] [blame] | 340 | struct p9_fid *fid = filp->private_data; |
| 341 | |
Eric Van Hensbergen | 51a87c5 | 2008-10-16 08:30:07 -0500 | [diff] [blame] | 342 | P9_DPRINTK(P9_DEBUG_VFS, "fid %d offset %llu count %d\n", fid->fid, |
Eric Van Hensbergen | fbedadc | 2008-10-13 20:36:16 -0500 | [diff] [blame] | 343 | (long long unsigned) offset, count); |
| 344 | |
| 345 | n = 0; |
| 346 | total = 0; |
M. Mohan Kumar | 97e8442 | 2010-06-04 11:59:07 +0000 | [diff] [blame] | 347 | size = fid->iounit ? fid->iounit : fid->clnt->msize - P9_IOHDRSZ; |
Eric Van Hensbergen | fbedadc | 2008-10-13 20:36:16 -0500 | [diff] [blame] | 348 | do { |
| 349 | n = p9_client_read(fid, data, udata, offset, count); |
| 350 | if (n <= 0) |
| 351 | break; |
| 352 | |
| 353 | if (data) |
| 354 | data += n; |
| 355 | if (udata) |
| 356 | udata += n; |
| 357 | |
| 358 | offset += n; |
| 359 | count -= n; |
| 360 | total += n; |
M. Mohan Kumar | 97e8442 | 2010-06-04 11:59:07 +0000 | [diff] [blame] | 361 | } while (count > 0 && n == size); |
Eric Van Hensbergen | fbedadc | 2008-10-13 20:36:16 -0500 | [diff] [blame] | 362 | |
| 363 | if (n < 0) |
| 364 | total = n; |
| 365 | |
| 366 | return total; |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * v9fs_file_read - read from a file |
| 371 | * @filp: file pointer to read |
| 372 | * @udata: user data buffer to read data into |
| 373 | * @count: size of buffer |
| 374 | * @offset: offset at which to read data |
| 375 | * |
| 376 | */ |
| 377 | |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 378 | static ssize_t |
Eric Van Hensbergen | fbedadc | 2008-10-13 20:36:16 -0500 | [diff] [blame] | 379 | v9fs_file_read(struct file *filp, char __user *udata, size_t count, |
Latchesar Ionkov | 19cba8a | 2005-10-11 08:29:03 -0700 | [diff] [blame] | 380 | loff_t * offset) |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 381 | { |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 382 | int ret; |
| 383 | struct p9_fid *fid; |
M. Mohan Kumar | 97e8442 | 2010-06-04 11:59:07 +0000 | [diff] [blame] | 384 | size_t size; |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 385 | |
Eric Van Hensbergen | ea2e799 | 2008-10-22 18:48:45 -0500 | [diff] [blame] | 386 | P9_DPRINTK(P9_DEBUG_VFS, "count %zu offset %lld\n", count, *offset); |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 387 | fid = filp->private_data; |
Eric Van Hensbergen | fbedadc | 2008-10-13 20:36:16 -0500 | [diff] [blame] | 388 | |
M. Mohan Kumar | 97e8442 | 2010-06-04 11:59:07 +0000 | [diff] [blame] | 389 | size = fid->iounit ? fid->iounit : fid->clnt->msize - P9_IOHDRSZ; |
| 390 | if (count > size) |
Eric Van Hensbergen | fbedadc | 2008-10-13 20:36:16 -0500 | [diff] [blame] | 391 | ret = v9fs_file_readn(filp, NULL, udata, count, *offset); |
| 392 | else |
| 393 | ret = p9_client_read(fid, NULL, udata, *offset, count); |
| 394 | |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 395 | if (ret > 0) |
| 396 | *offset += ret; |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 397 | |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 398 | return ret; |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | /** |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 402 | * v9fs_file_write - write to a file |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 403 | * @filp: file pointer to write |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 404 | * @data: data buffer to write data from |
| 405 | * @count: size of buffer |
| 406 | * @offset: offset at which to write data |
| 407 | * |
| 408 | */ |
| 409 | |
| 410 | static ssize_t |
| 411 | v9fs_file_write(struct file *filp, const char __user * data, |
| 412 | size_t count, loff_t * offset) |
| 413 | { |
Harsh Prateek Bora | 3834b12 | 2010-08-03 11:55:40 +0000 | [diff] [blame] | 414 | ssize_t retval; |
| 415 | size_t total = 0; |
jvrao | 8d40fa2 | 2010-08-30 13:23:20 -0500 | [diff] [blame] | 416 | int n; |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 417 | struct p9_fid *fid; |
Eric Van Hensbergen | dfb0ec2 | 2008-10-13 20:36:16 -0500 | [diff] [blame] | 418 | struct p9_client *clnt; |
Eric Van Hensbergen | 9523a84 | 2007-07-13 13:01:27 -0500 | [diff] [blame] | 419 | struct inode *inode = filp->f_path.dentry->d_inode; |
jvrao | fc0f296 | 2010-03-08 22:07:02 +0000 | [diff] [blame] | 420 | loff_t origin = *offset; |
Abhishek Kulkarni | 637d020 | 2009-09-22 11:34:05 -0500 | [diff] [blame] | 421 | unsigned long pg_start, pg_end; |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 422 | |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 423 | P9_DPRINTK(P9_DEBUG_VFS, "data %p count %d offset %x\n", data, |
| 424 | (int)count, (int)*offset); |
Latchesar Ionkov | 19cba8a | 2005-10-11 08:29:03 -0700 | [diff] [blame] | 425 | |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 426 | fid = filp->private_data; |
Eric Van Hensbergen | dfb0ec2 | 2008-10-13 20:36:16 -0500 | [diff] [blame] | 427 | clnt = fid->clnt; |
| 428 | |
Harsh Prateek Bora | 3834b12 | 2010-08-03 11:55:40 +0000 | [diff] [blame] | 429 | retval = generic_write_checks(filp, &origin, &count, 0); |
| 430 | if (retval) |
| 431 | goto out; |
| 432 | |
| 433 | retval = -EINVAL; |
| 434 | if ((ssize_t) count < 0) |
| 435 | goto out; |
| 436 | retval = 0; |
| 437 | if (!count) |
| 438 | goto out; |
| 439 | |
Eric Van Hensbergen | dfb0ec2 | 2008-10-13 20:36:16 -0500 | [diff] [blame] | 440 | do { |
jvrao | 8d40fa2 | 2010-08-30 13:23:20 -0500 | [diff] [blame] | 441 | n = p9_client_write(fid, NULL, data+total, origin+total, count); |
Eric Van Hensbergen | dfb0ec2 | 2008-10-13 20:36:16 -0500 | [diff] [blame] | 442 | if (n <= 0) |
| 443 | break; |
| 444 | count -= n; |
| 445 | total += n; |
| 446 | } while (count > 0); |
| 447 | |
| 448 | if (total > 0) { |
Abhishek Kulkarni | 637d020 | 2009-09-22 11:34:05 -0500 | [diff] [blame] | 449 | pg_start = origin >> PAGE_CACHE_SHIFT; |
| 450 | pg_end = (origin + total - 1) >> PAGE_CACHE_SHIFT; |
Abhishek Kulkarni | 60e78d2 | 2009-09-23 13:00:27 -0500 | [diff] [blame] | 451 | if (inode->i_mapping && inode->i_mapping->nrpages) |
| 452 | invalidate_inode_pages2_range(inode->i_mapping, |
| 453 | pg_start, pg_end); |
Eric Van Hensbergen | dfb0ec2 | 2008-10-13 20:36:16 -0500 | [diff] [blame] | 454 | *offset += total; |
Abhishek Kulkarni | 637d020 | 2009-09-22 11:34:05 -0500 | [diff] [blame] | 455 | i_size_write(inode, i_size_read(inode) + total); |
Abhishek Kulkarni | 7549ae3e | 2009-09-22 11:34:05 -0500 | [diff] [blame] | 456 | inode->i_blocks = (i_size_read(inode) + 512 - 1) >> 9; |
Eric Van Hensbergen | 9523a84 | 2007-07-13 13:01:27 -0500 | [diff] [blame] | 457 | } |
| 458 | |
Eric Van Hensbergen | dfb0ec2 | 2008-10-13 20:36:16 -0500 | [diff] [blame] | 459 | if (n < 0) |
Harsh Prateek Bora | 3834b12 | 2010-08-03 11:55:40 +0000 | [diff] [blame] | 460 | retval = n; |
| 461 | else |
| 462 | retval = total; |
| 463 | out: |
| 464 | return retval; |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 465 | } |
| 466 | |
Christoph Hellwig | 7ea8085 | 2010-05-26 17:53:25 +0200 | [diff] [blame] | 467 | static int v9fs_file_fsync(struct file *filp, int datasync) |
M. Mohan Kumar | 7a4439c | 2010-02-08 15:36:48 -0600 | [diff] [blame] | 468 | { |
| 469 | struct p9_fid *fid; |
| 470 | struct p9_wstat wstat; |
| 471 | int retval; |
| 472 | |
Christoph Hellwig | 7ea8085 | 2010-05-26 17:53:25 +0200 | [diff] [blame] | 473 | P9_DPRINTK(P9_DEBUG_VFS, "filp %p datasync %x\n", filp, datasync); |
M. Mohan Kumar | 7a4439c | 2010-02-08 15:36:48 -0600 | [diff] [blame] | 474 | |
| 475 | fid = filp->private_data; |
| 476 | v9fs_blank_wstat(&wstat); |
| 477 | |
| 478 | retval = p9_client_wstat(fid, &wstat); |
| 479 | return retval; |
| 480 | } |
| 481 | |
Venkateswararao Jujjuri (JV) | b165d60 | 2010-10-22 10:13:12 -0700 | [diff] [blame] | 482 | int v9fs_file_fsync_dotl(struct file *filp, int datasync) |
Venkateswararao Jujjuri (JV) | 920e65d | 2010-09-22 17:19:19 -0700 | [diff] [blame] | 483 | { |
| 484 | struct p9_fid *fid; |
| 485 | int retval; |
| 486 | |
| 487 | P9_DPRINTK(P9_DEBUG_VFS, "v9fs_file_fsync_dotl: filp %p datasync %x\n", |
| 488 | filp, datasync); |
| 489 | |
| 490 | fid = filp->private_data; |
| 491 | |
Venkateswararao Jujjuri (JV) | b165d60 | 2010-10-22 10:13:12 -0700 | [diff] [blame] | 492 | retval = p9_client_fsync(fid, datasync); |
Venkateswararao Jujjuri (JV) | 920e65d | 2010-09-22 17:19:19 -0700 | [diff] [blame] | 493 | return retval; |
| 494 | } |
| 495 | |
Aneesh Kumar K.V | 29236f4 | 2011-02-28 17:03:54 +0530 | [diff] [blame] | 496 | const struct file_operations v9fs_cached_file_operations = { |
Eric Van Hensbergen | e03abc0 | 2007-02-11 13:21:39 -0600 | [diff] [blame] | 497 | .llseek = generic_file_llseek, |
| 498 | .read = do_sync_read, |
| 499 | .aio_read = generic_file_aio_read, |
| 500 | .write = v9fs_file_write, |
| 501 | .open = v9fs_file_open, |
| 502 | .release = v9fs_dir_release, |
| 503 | .lock = v9fs_file_lock, |
Eric Van Hensbergen | 14b8869 | 2008-02-06 19:25:05 -0600 | [diff] [blame] | 504 | .mmap = generic_file_readonly_mmap, |
M. Mohan Kumar | 7a4439c | 2010-02-08 15:36:48 -0600 | [diff] [blame] | 505 | .fsync = v9fs_file_fsync, |
Eric Van Hensbergen | e03abc0 | 2007-02-11 13:21:39 -0600 | [diff] [blame] | 506 | }; |
| 507 | |
Aneesh Kumar K.V | 29236f4 | 2011-02-28 17:03:54 +0530 | [diff] [blame] | 508 | const struct file_operations v9fs_cached_file_operations_dotl = { |
Venkateswararao Jujjuri (JV) | b04faaf | 2010-09-22 16:30:52 -0700 | [diff] [blame] | 509 | .llseek = generic_file_llseek, |
| 510 | .read = do_sync_read, |
| 511 | .aio_read = generic_file_aio_read, |
| 512 | .write = v9fs_file_write, |
| 513 | .open = v9fs_file_open, |
| 514 | .release = v9fs_dir_release, |
M. Mohan Kumar | a099027 | 2010-09-27 11:34:24 +0530 | [diff] [blame] | 515 | .lock = v9fs_file_lock_dotl, |
| 516 | .flock = v9fs_file_flock_dotl, |
Venkateswararao Jujjuri (JV) | b04faaf | 2010-09-22 16:30:52 -0700 | [diff] [blame] | 517 | .mmap = generic_file_readonly_mmap, |
Venkateswararao Jujjuri (JV) | 920e65d | 2010-09-22 17:19:19 -0700 | [diff] [blame] | 518 | .fsync = v9fs_file_fsync_dotl, |
Venkateswararao Jujjuri (JV) | b04faaf | 2010-09-22 16:30:52 -0700 | [diff] [blame] | 519 | }; |
| 520 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 521 | const struct file_operations v9fs_file_operations = { |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 522 | .llseek = generic_file_llseek, |
| 523 | .read = v9fs_file_read, |
| 524 | .write = v9fs_file_write, |
| 525 | .open = v9fs_file_open, |
| 526 | .release = v9fs_dir_release, |
| 527 | .lock = v9fs_file_lock, |
Eric Van Hensbergen | 14b8869 | 2008-02-06 19:25:05 -0600 | [diff] [blame] | 528 | .mmap = generic_file_readonly_mmap, |
M. Mohan Kumar | 7a4439c | 2010-02-08 15:36:48 -0600 | [diff] [blame] | 529 | .fsync = v9fs_file_fsync, |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 530 | }; |
Sripathi Kodi | 9b6533c | 2010-03-25 12:41:54 +0000 | [diff] [blame] | 531 | |
| 532 | const struct file_operations v9fs_file_operations_dotl = { |
| 533 | .llseek = generic_file_llseek, |
| 534 | .read = v9fs_file_read, |
| 535 | .write = v9fs_file_write, |
| 536 | .open = v9fs_file_open, |
| 537 | .release = v9fs_dir_release, |
M. Mohan Kumar | a099027 | 2010-09-27 11:34:24 +0530 | [diff] [blame] | 538 | .lock = v9fs_file_lock_dotl, |
| 539 | .flock = v9fs_file_flock_dotl, |
Sripathi Kodi | 9b6533c | 2010-03-25 12:41:54 +0000 | [diff] [blame] | 540 | .mmap = generic_file_readonly_mmap, |
Venkateswararao Jujjuri (JV) | 920e65d | 2010-09-22 17:19:19 -0700 | [diff] [blame] | 541 | .fsync = v9fs_file_fsync_dotl, |
Sripathi Kodi | 9b6533c | 2010-03-25 12:41:54 +0000 | [diff] [blame] | 542 | }; |