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