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