Thomas Gleixner | 1f32761 | 2019-05-28 09:57:16 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 2 | /* |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 3 | * This file contians vfs file ops for 9P2000. |
| 4 | * |
| 5 | * Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com> |
| 6 | * Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov> |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/errno.h> |
| 11 | #include <linux/fs.h> |
Al Viro | 914e263 | 2006-10-18 13:55:46 -0400 | [diff] [blame] | 12 | #include <linux/sched.h> |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 13 | #include <linux/file.h> |
| 14 | #include <linux/stat.h> |
| 15 | #include <linux/string.h> |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 16 | #include <linux/inet.h> |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 17 | #include <linux/list.h> |
Abhishek Kulkarni | 637d020 | 2009-09-22 11:34:05 -0500 | [diff] [blame] | 18 | #include <linux/pagemap.h> |
M. Mohan Kumar | a099027 | 2010-09-27 11:34:24 +0530 | [diff] [blame] | 19 | #include <linux/utsname.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 20 | #include <linux/uaccess.h> |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 21 | #include <linux/idr.h> |
Al Viro | 070b365 | 2015-04-01 20:17:51 -0400 | [diff] [blame] | 22 | #include <linux/uio.h> |
Al Viro | ce85dd5 | 2015-04-02 12:02:03 -0400 | [diff] [blame] | 23 | #include <linux/slab.h> |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 24 | #include <net/9p/9p.h> |
| 25 | #include <net/9p/client.h> |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 26 | |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 27 | #include "v9fs.h" |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 28 | #include "v9fs_vfs.h" |
| 29 | #include "fid.h" |
Abhishek Kulkarni | 60e78d2 | 2009-09-23 13:00:27 -0500 | [diff] [blame] | 30 | #include "cache.h" |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 31 | |
Aneesh Kumar K.V | 7263ceb | 2011-02-28 17:03:58 +0530 | [diff] [blame] | 32 | static const struct vm_operations_struct v9fs_file_vm_ops; |
Dominique Martinet | fb89b45 | 2014-01-10 13:44:09 +0100 | [diff] [blame] | 33 | static const struct vm_operations_struct v9fs_mmap_file_vm_ops; |
Aneesh Kumar K.V | 7263ceb | 2011-02-28 17:03:58 +0530 | [diff] [blame] | 34 | |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 35 | /** |
| 36 | * v9fs_file_open - open a file (or directory) |
| 37 | * @inode: inode to be opened |
| 38 | * @file: file being opened |
| 39 | * |
| 40 | */ |
| 41 | |
| 42 | int v9fs_file_open(struct inode *inode, struct file *file) |
| 43 | { |
Latchesar Ionkov | 6a3124a | 2006-03-02 02:54:30 -0800 | [diff] [blame] | 44 | int err; |
Aneesh Kumar K.V | 6b39f6d | 2011-02-28 17:04:03 +0530 | [diff] [blame] | 45 | struct v9fs_inode *v9inode; |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 46 | struct v9fs_session_info *v9ses; |
Dominique Martinet | 5bfe97d | 2020-11-19 15:11:04 +0100 | [diff] [blame] | 47 | struct p9_fid *fid, *writeback_fid; |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 48 | int omode; |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 49 | |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 50 | p9_debug(P9_DEBUG_VFS, "inode: %p file: %p\n", inode, file); |
Aneesh Kumar K.V | 6b39f6d | 2011-02-28 17:04:03 +0530 | [diff] [blame] | 51 | v9inode = V9FS_I(inode); |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 52 | v9ses = v9fs_inode2v9ses(inode); |
M. Mohan Kumar | ef56547 | 2010-06-22 19:47:50 +0530 | [diff] [blame] | 53 | if (v9fs_proto_dotl(v9ses)) |
Aneesh Kumar K.V | f88657c | 2011-08-03 19:55:32 +0530 | [diff] [blame] | 54 | omode = v9fs_open_to_dotl_flags(file->f_flags); |
M. Mohan Kumar | ef56547 | 2010-06-22 19:47:50 +0530 | [diff] [blame] | 55 | else |
| 56 | omode = v9fs_uflags2omode(file->f_flags, |
| 57 | v9fs_proto_dotu(v9ses)); |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 58 | fid = file->private_data; |
| 59 | if (!fid) { |
Miklos Szeredi | b403f0e | 2016-06-29 10:54:23 +0200 | [diff] [blame] | 60 | fid = v9fs_fid_clone(file_dentry(file)); |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 61 | if (IS_ERR(fid)) |
| 62 | return PTR_ERR(fid); |
| 63 | |
| 64 | err = p9_client_open(fid, omode); |
Eric Van Hensbergen | 9523a84 | 2007-07-13 13:01:27 -0500 | [diff] [blame] | 65 | if (err < 0) { |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 66 | p9_client_clunk(fid); |
| 67 | return err; |
| 68 | } |
M. Mohan Kumar | ef56547 | 2010-06-22 19:47:50 +0530 | [diff] [blame] | 69 | if ((file->f_flags & O_APPEND) && |
| 70 | (!v9fs_proto_dotu(v9ses) && !v9fs_proto_dotl(v9ses))) |
Eric Van Hensbergen | 2e4bef4 | 2008-06-24 17:39:39 -0500 | [diff] [blame] | 71 | generic_file_llseek(file, 0, SEEK_END); |
Latchesar Ionkov | 6a3124a | 2006-03-02 02:54:30 -0800 | [diff] [blame] | 72 | } |
| 73 | |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 74 | file->private_data = fid; |
Aneesh Kumar K.V | 5a7e0a8 | 2011-03-08 16:39:46 +0530 | [diff] [blame] | 75 | mutex_lock(&v9inode->v_mutex); |
Dominique Martinet | fb89b45 | 2014-01-10 13:44:09 +0100 | [diff] [blame] | 76 | if ((v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) && |
| 77 | !v9inode->writeback_fid && |
Aneesh Kumar K.V | 7add697 | 2011-03-08 16:39:49 +0530 | [diff] [blame] | 78 | ((file->f_flags & O_ACCMODE) != O_RDONLY)) { |
Aneesh Kumar K.V | 3cf387d | 2011-02-28 17:03:57 +0530 | [diff] [blame] | 79 | /* |
Aneesh Kumar K.V | 6b39f6d | 2011-02-28 17:04:03 +0530 | [diff] [blame] | 80 | * clone a fid and add it to writeback_fid |
Aneesh Kumar K.V | 3cf387d | 2011-02-28 17:03:57 +0530 | [diff] [blame] | 81 | * we do it during open time instead of |
| 82 | * page dirty time via write_begin/page_mkwrite |
| 83 | * because we want write after unlink usecase |
| 84 | * to work. |
| 85 | */ |
Dominique Martinet | 5bfe97d | 2020-11-19 15:11:04 +0100 | [diff] [blame] | 86 | writeback_fid = v9fs_writeback_fid(file_dentry(file)); |
Yang Yingliang | f8b139e | 2021-03-30 21:06:32 +0800 | [diff] [blame] | 87 | if (IS_ERR(writeback_fid)) { |
| 88 | err = PTR_ERR(writeback_fid); |
Aneesh Kumar K.V | 5a7e0a8 | 2011-03-08 16:39:46 +0530 | [diff] [blame] | 89 | mutex_unlock(&v9inode->v_mutex); |
Aneesh Kumar K.V | 3cf387d | 2011-02-28 17:03:57 +0530 | [diff] [blame] | 90 | goto out_error; |
| 91 | } |
Dominique Martinet | 5bfe97d | 2020-11-19 15:11:04 +0100 | [diff] [blame] | 92 | v9inode->writeback_fid = (void *) writeback_fid; |
Aneesh Kumar K.V | 3cf387d | 2011-02-28 17:03:57 +0530 | [diff] [blame] | 93 | } |
Aneesh Kumar K.V | 5a7e0a8 | 2011-03-08 16:39:46 +0530 | [diff] [blame] | 94 | mutex_unlock(&v9inode->v_mutex); |
Dominique Martinet | fb89b45 | 2014-01-10 13:44:09 +0100 | [diff] [blame] | 95 | if (v9ses->cache == CACHE_LOOSE || v9ses->cache == CACHE_FSCACHE) |
Abhishek Kulkarni | 60e78d2 | 2009-09-23 13:00:27 -0500 | [diff] [blame] | 96 | v9fs_cache_inode_set_cookie(inode, file); |
Greg Kurz | 987a648 | 2020-09-23 22:11:44 +0800 | [diff] [blame] | 97 | v9fs_open_fid_add(inode, fid); |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 98 | return 0; |
Aneesh Kumar K.V | 3cf387d | 2011-02-28 17:03:57 +0530 | [diff] [blame] | 99 | out_error: |
| 100 | p9_client_clunk(file->private_data); |
| 101 | file->private_data = NULL; |
| 102 | return err; |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /** |
| 106 | * v9fs_file_lock - lock a file (or directory) |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 107 | * @filp: file to be locked |
| 108 | * @cmd: lock command |
| 109 | * @fl: file lock structure |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 110 | * |
Eric Van Hensbergen | ee44399 | 2008-03-05 07:08:09 -0600 | [diff] [blame] | 111 | * 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] | 112 | * by using open exclusive |
| 113 | */ |
| 114 | |
| 115 | static int v9fs_file_lock(struct file *filp, int cmd, struct file_lock *fl) |
| 116 | { |
| 117 | int res = 0; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 118 | struct inode *inode = file_inode(filp); |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 119 | |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 120 | p9_debug(P9_DEBUG_VFS, "filp: %p lock: %p\n", filp, fl); |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 121 | |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 122 | if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) { |
OGAWA Hirofumi | 28fd129 | 2006-01-08 01:02:14 -0800 | [diff] [blame] | 123 | filemap_write_and_wait(inode->i_mapping); |
Andrew Morton | fc0ecff | 2007-02-10 01:45:39 -0800 | [diff] [blame] | 124 | invalidate_mapping_pages(&inode->i_data, 0, -1); |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | return res; |
| 128 | } |
| 129 | |
M. Mohan Kumar | a099027 | 2010-09-27 11:34:24 +0530 | [diff] [blame] | 130 | static int v9fs_file_do_lock(struct file *filp, int cmd, struct file_lock *fl) |
| 131 | { |
| 132 | struct p9_flock flock; |
| 133 | struct p9_fid *fid; |
Dominique Martinet | 5c4086b | 2015-01-09 12:56:07 +0100 | [diff] [blame] | 134 | uint8_t status = P9_LOCK_ERROR; |
M. Mohan Kumar | a099027 | 2010-09-27 11:34:24 +0530 | [diff] [blame] | 135 | int res = 0; |
| 136 | unsigned char fl_type; |
Dinu-Razvan Chis-Serban | 5e172f7 | 2018-09-05 16:44:12 +0900 | [diff] [blame] | 137 | struct v9fs_session_info *v9ses; |
M. Mohan Kumar | a099027 | 2010-09-27 11:34:24 +0530 | [diff] [blame] | 138 | |
| 139 | fid = filp->private_data; |
| 140 | BUG_ON(fid == NULL); |
| 141 | |
| 142 | if ((fl->fl_flags & FL_POSIX) != FL_POSIX) |
| 143 | BUG(); |
| 144 | |
Benjamin Coddington | 4f65636 | 2015-10-22 13:38:14 -0400 | [diff] [blame] | 145 | res = locks_lock_file_wait(filp, fl); |
M. Mohan Kumar | a099027 | 2010-09-27 11:34:24 +0530 | [diff] [blame] | 146 | if (res < 0) |
| 147 | goto out; |
| 148 | |
| 149 | /* convert posix lock to p9 tlock args */ |
| 150 | memset(&flock, 0, sizeof(flock)); |
Jim Garlick | 51b8b4f | 2011-08-21 00:21:18 +0530 | [diff] [blame] | 151 | /* map the lock type */ |
| 152 | switch (fl->fl_type) { |
| 153 | case F_RDLCK: |
| 154 | flock.type = P9_LOCK_TYPE_RDLCK; |
| 155 | break; |
| 156 | case F_WRLCK: |
| 157 | flock.type = P9_LOCK_TYPE_WRLCK; |
| 158 | break; |
| 159 | case F_UNLCK: |
| 160 | flock.type = P9_LOCK_TYPE_UNLCK; |
| 161 | break; |
| 162 | } |
M. Mohan Kumar | a099027 | 2010-09-27 11:34:24 +0530 | [diff] [blame] | 163 | flock.start = fl->fl_start; |
| 164 | if (fl->fl_end == OFFSET_MAX) |
| 165 | flock.length = 0; |
| 166 | else |
| 167 | flock.length = fl->fl_end - fl->fl_start + 1; |
| 168 | flock.proc_id = fl->fl_pid; |
Will Deacon | 50192ab | 2013-08-21 18:24:47 +0100 | [diff] [blame] | 169 | flock.client_id = fid->clnt->name; |
M. Mohan Kumar | a099027 | 2010-09-27 11:34:24 +0530 | [diff] [blame] | 170 | if (IS_SETLKW(cmd)) |
| 171 | flock.flags = P9_LOCK_FLAGS_BLOCK; |
| 172 | |
Dinu-Razvan Chis-Serban | 5e172f7 | 2018-09-05 16:44:12 +0900 | [diff] [blame] | 173 | v9ses = v9fs_inode2v9ses(file_inode(filp)); |
| 174 | |
M. Mohan Kumar | a099027 | 2010-09-27 11:34:24 +0530 | [diff] [blame] | 175 | /* |
| 176 | * if its a blocked request and we get P9_LOCK_BLOCKED as the status |
| 177 | * for lock request, keep on trying |
| 178 | */ |
| 179 | for (;;) { |
| 180 | res = p9_client_lock_dotl(fid, &flock, &status); |
| 181 | if (res < 0) |
Kirill A. Shutemov | ad80492 | 2014-12-29 15:00:18 +0200 | [diff] [blame] | 182 | goto out_unlock; |
M. Mohan Kumar | a099027 | 2010-09-27 11:34:24 +0530 | [diff] [blame] | 183 | |
| 184 | if (status != P9_LOCK_BLOCKED) |
| 185 | break; |
| 186 | if (status == P9_LOCK_BLOCKED && !IS_SETLKW(cmd)) |
| 187 | break; |
Dinu-Razvan Chis-Serban | 5e172f7 | 2018-09-05 16:44:12 +0900 | [diff] [blame] | 188 | if (schedule_timeout_interruptible(v9ses->session_lock_timeout) |
| 189 | != 0) |
Jim Garlick | a0ea787 | 2012-01-03 15:27:50 -0600 | [diff] [blame] | 190 | break; |
Dominique Martinet | b4dc44b | 2018-09-08 01:18:43 +0900 | [diff] [blame] | 191 | /* |
| 192 | * p9_client_lock_dotl overwrites flock.client_id with the |
| 193 | * server message, free and reuse the client name |
| 194 | */ |
| 195 | if (flock.client_id != fid->clnt->name) { |
| 196 | kfree(flock.client_id); |
| 197 | flock.client_id = fid->clnt->name; |
| 198 | } |
M. Mohan Kumar | a099027 | 2010-09-27 11:34:24 +0530 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | /* map 9p status to VFS status */ |
| 202 | switch (status) { |
| 203 | case P9_LOCK_SUCCESS: |
| 204 | res = 0; |
| 205 | break; |
| 206 | case P9_LOCK_BLOCKED: |
| 207 | res = -EAGAIN; |
| 208 | break; |
Kirill A. Shutemov | b642f72 | 2014-12-29 15:00:19 +0200 | [diff] [blame] | 209 | default: |
| 210 | WARN_ONCE(1, "unknown lock status code: %d\n", status); |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 211 | fallthrough; |
M. Mohan Kumar | a099027 | 2010-09-27 11:34:24 +0530 | [diff] [blame] | 212 | case P9_LOCK_ERROR: |
| 213 | case P9_LOCK_GRACE: |
| 214 | res = -ENOLCK; |
| 215 | break; |
M. Mohan Kumar | a099027 | 2010-09-27 11:34:24 +0530 | [diff] [blame] | 216 | } |
| 217 | |
Kirill A. Shutemov | ad80492 | 2014-12-29 15:00:18 +0200 | [diff] [blame] | 218 | out_unlock: |
M. Mohan Kumar | a099027 | 2010-09-27 11:34:24 +0530 | [diff] [blame] | 219 | /* |
| 220 | * incase server returned error for lock request, revert |
| 221 | * it locally |
| 222 | */ |
| 223 | if (res < 0 && fl->fl_type != F_UNLCK) { |
| 224 | fl_type = fl->fl_type; |
| 225 | fl->fl_type = F_UNLCK; |
Dominique Martinet | b647874 | 2015-11-05 18:44:21 -0800 | [diff] [blame] | 226 | /* Even if this fails we want to return the remote error */ |
Linus Torvalds | 2e3078a | 2015-11-05 23:10:54 -0800 | [diff] [blame] | 227 | locks_lock_file_wait(filp, fl); |
M. Mohan Kumar | a099027 | 2010-09-27 11:34:24 +0530 | [diff] [blame] | 228 | fl->fl_type = fl_type; |
| 229 | } |
Dominique Martinet | b4dc44b | 2018-09-08 01:18:43 +0900 | [diff] [blame] | 230 | if (flock.client_id != fid->clnt->name) |
| 231 | kfree(flock.client_id); |
M. Mohan Kumar | a099027 | 2010-09-27 11:34:24 +0530 | [diff] [blame] | 232 | out: |
| 233 | return res; |
| 234 | } |
| 235 | |
M. Mohan Kumar | 1d769cd | 2010-09-27 12:22:13 +0530 | [diff] [blame] | 236 | static int v9fs_file_getlock(struct file *filp, struct file_lock *fl) |
| 237 | { |
| 238 | struct p9_getlock glock; |
| 239 | struct p9_fid *fid; |
| 240 | int res = 0; |
| 241 | |
| 242 | fid = filp->private_data; |
| 243 | BUG_ON(fid == NULL); |
| 244 | |
| 245 | posix_test_lock(filp, fl); |
| 246 | /* |
| 247 | * if we have a conflicting lock locally, no need to validate |
| 248 | * with server |
| 249 | */ |
| 250 | if (fl->fl_type != F_UNLCK) |
| 251 | return res; |
| 252 | |
| 253 | /* convert posix lock to p9 tgetlock args */ |
| 254 | memset(&glock, 0, sizeof(glock)); |
Jim Garlick | 51b8b4f | 2011-08-21 00:21:18 +0530 | [diff] [blame] | 255 | glock.type = P9_LOCK_TYPE_UNLCK; |
M. Mohan Kumar | 1d769cd | 2010-09-27 12:22:13 +0530 | [diff] [blame] | 256 | glock.start = fl->fl_start; |
| 257 | if (fl->fl_end == OFFSET_MAX) |
| 258 | glock.length = 0; |
| 259 | else |
| 260 | glock.length = fl->fl_end - fl->fl_start + 1; |
| 261 | glock.proc_id = fl->fl_pid; |
Will Deacon | 50192ab | 2013-08-21 18:24:47 +0100 | [diff] [blame] | 262 | glock.client_id = fid->clnt->name; |
M. Mohan Kumar | 1d769cd | 2010-09-27 12:22:13 +0530 | [diff] [blame] | 263 | |
| 264 | res = p9_client_getlock_dotl(fid, &glock); |
| 265 | if (res < 0) |
Dominique Martinet | b4dc44b | 2018-09-08 01:18:43 +0900 | [diff] [blame] | 266 | goto out; |
Jim Garlick | 51b8b4f | 2011-08-21 00:21:18 +0530 | [diff] [blame] | 267 | /* map 9p lock type to os lock type */ |
| 268 | switch (glock.type) { |
| 269 | case P9_LOCK_TYPE_RDLCK: |
| 270 | fl->fl_type = F_RDLCK; |
| 271 | break; |
| 272 | case P9_LOCK_TYPE_WRLCK: |
| 273 | fl->fl_type = F_WRLCK; |
| 274 | break; |
| 275 | case P9_LOCK_TYPE_UNLCK: |
| 276 | fl->fl_type = F_UNLCK; |
| 277 | break; |
| 278 | } |
| 279 | if (glock.type != P9_LOCK_TYPE_UNLCK) { |
M. Mohan Kumar | 1d769cd | 2010-09-27 12:22:13 +0530 | [diff] [blame] | 280 | fl->fl_start = glock.start; |
| 281 | if (glock.length == 0) |
| 282 | fl->fl_end = OFFSET_MAX; |
| 283 | else |
| 284 | fl->fl_end = glock.start + glock.length - 1; |
Benjamin Coddington | 9d5b86a | 2017-07-16 10:28:22 -0400 | [diff] [blame] | 285 | fl->fl_pid = -glock.proc_id; |
Jim Garlick | 51b8b4f | 2011-08-21 00:21:18 +0530 | [diff] [blame] | 286 | } |
Dominique Martinet | b4dc44b | 2018-09-08 01:18:43 +0900 | [diff] [blame] | 287 | out: |
| 288 | if (glock.client_id != fid->clnt->name) |
| 289 | kfree(glock.client_id); |
M. Mohan Kumar | 1d769cd | 2010-09-27 12:22:13 +0530 | [diff] [blame] | 290 | return res; |
| 291 | } |
| 292 | |
M. Mohan Kumar | a099027 | 2010-09-27 11:34:24 +0530 | [diff] [blame] | 293 | /** |
| 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 | |
| 301 | static int v9fs_file_lock_dotl(struct file *filp, int cmd, struct file_lock *fl) |
| 302 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 303 | struct inode *inode = file_inode(filp); |
M. Mohan Kumar | a099027 | 2010-09-27 11:34:24 +0530 | [diff] [blame] | 304 | int ret = -ENOLCK; |
| 305 | |
Al Viro | 4b8e992 | 2014-08-19 20:17:38 -0400 | [diff] [blame] | 306 | p9_debug(P9_DEBUG_VFS, "filp: %p cmd:%d lock: %p name: %pD\n", |
| 307 | filp, cmd, fl, filp); |
M. Mohan Kumar | a099027 | 2010-09-27 11:34:24 +0530 | [diff] [blame] | 308 | |
M. Mohan Kumar | a099027 | 2010-09-27 11:34:24 +0530 | [diff] [blame] | 309 | if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) { |
| 310 | filemap_write_and_wait(inode->i_mapping); |
| 311 | invalidate_mapping_pages(&inode->i_data, 0, -1); |
| 312 | } |
| 313 | |
| 314 | if (IS_SETLK(cmd) || IS_SETLKW(cmd)) |
| 315 | ret = v9fs_file_do_lock(filp, cmd, fl); |
M. Mohan Kumar | 1d769cd | 2010-09-27 12:22:13 +0530 | [diff] [blame] | 316 | else if (IS_GETLK(cmd)) |
| 317 | ret = v9fs_file_getlock(filp, fl); |
M. Mohan Kumar | a099027 | 2010-09-27 11:34:24 +0530 | [diff] [blame] | 318 | else |
| 319 | ret = -EINVAL; |
M. Mohan Kumar | a099027 | 2010-09-27 11:34:24 +0530 | [diff] [blame] | 320 | return ret; |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * v9fs_file_flock_dotl - lock a file |
| 325 | * @filp: file to be locked |
| 326 | * @cmd: lock command |
| 327 | * @fl: file lock structure |
| 328 | * |
| 329 | */ |
| 330 | |
| 331 | static int v9fs_file_flock_dotl(struct file *filp, int cmd, |
| 332 | struct file_lock *fl) |
| 333 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 334 | struct inode *inode = file_inode(filp); |
M. Mohan Kumar | a099027 | 2010-09-27 11:34:24 +0530 | [diff] [blame] | 335 | int ret = -ENOLCK; |
| 336 | |
Al Viro | 4b8e992 | 2014-08-19 20:17:38 -0400 | [diff] [blame] | 337 | p9_debug(P9_DEBUG_VFS, "filp: %p cmd:%d lock: %p name: %pD\n", |
| 338 | filp, cmd, fl, filp); |
M. Mohan Kumar | a099027 | 2010-09-27 11:34:24 +0530 | [diff] [blame] | 339 | |
M. Mohan Kumar | a099027 | 2010-09-27 11:34:24 +0530 | [diff] [blame] | 340 | if (!(fl->fl_flags & FL_FLOCK)) |
| 341 | goto out_err; |
| 342 | |
| 343 | if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) { |
| 344 | filemap_write_and_wait(inode->i_mapping); |
| 345 | invalidate_mapping_pages(&inode->i_data, 0, -1); |
| 346 | } |
| 347 | /* Convert flock to posix lock */ |
M. Mohan Kumar | a099027 | 2010-09-27 11:34:24 +0530 | [diff] [blame] | 348 | fl->fl_flags |= FL_POSIX; |
| 349 | fl->fl_flags ^= FL_FLOCK; |
| 350 | |
| 351 | if (IS_SETLK(cmd) | IS_SETLKW(cmd)) |
| 352 | ret = v9fs_file_do_lock(filp, cmd, fl); |
| 353 | else |
| 354 | ret = -EINVAL; |
| 355 | out_err: |
| 356 | return ret; |
| 357 | } |
| 358 | |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 359 | /** |
David Howells | bc86803 | 2021-10-04 22:07:22 +0100 | [diff] [blame] | 360 | * v9fs_file_read_iter - read from a file |
| 361 | * @iocb: The operation parameters |
| 362 | * @to: The buffer to read into |
Eric Van Hensbergen | fbedadc | 2008-10-13 20:36:16 -0500 | [diff] [blame] | 363 | * |
| 364 | */ |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 365 | static ssize_t |
Al Viro | e494b6b | 2015-04-01 23:59:57 -0400 | [diff] [blame] | 366 | v9fs_file_read_iter(struct kiocb *iocb, struct iov_iter *to) |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 367 | { |
Al Viro | e494b6b | 2015-04-01 23:59:57 -0400 | [diff] [blame] | 368 | struct p9_fid *fid = iocb->ki_filp->private_data; |
Vincent Bernat | b5ac1fb2 | 2015-08-15 14:07:44 +0200 | [diff] [blame] | 369 | int ret, err = 0; |
Al Viro | e1200fe6 | 2015-04-01 23:42:28 -0400 | [diff] [blame] | 370 | |
Al Viro | e494b6b | 2015-04-01 23:59:57 -0400 | [diff] [blame] | 371 | p9_debug(P9_DEBUG_VFS, "count %zu offset %lld\n", |
| 372 | iov_iter_count(to), iocb->ki_pos); |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 373 | |
Sergey Alirzaev | 52cbee2 | 2020-02-05 03:34:57 +0300 | [diff] [blame] | 374 | if (iocb->ki_filp->f_flags & O_NONBLOCK) |
| 375 | ret = p9_client_read_once(fid, iocb->ki_pos, to, &err); |
| 376 | else |
| 377 | ret = p9_client_read(fid, iocb->ki_pos, to, &err); |
Al Viro | e1200fe6 | 2015-04-01 23:42:28 -0400 | [diff] [blame] | 378 | if (!ret) |
| 379 | return err; |
Eric Van Hensbergen | fbedadc | 2008-10-13 20:36:16 -0500 | [diff] [blame] | 380 | |
Al Viro | e494b6b | 2015-04-01 23:59:57 -0400 | [diff] [blame] | 381 | iocb->ki_pos += ret; |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 382 | return ret; |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | /** |
David Howells | bc86803 | 2021-10-04 22:07:22 +0100 | [diff] [blame] | 386 | * v9fs_file_write_iter - write to a file |
| 387 | * @iocb: The operation parameters |
| 388 | * @from: The data to write |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 389 | * |
| 390 | */ |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 391 | static ssize_t |
Al Viro | e494b6b | 2015-04-01 23:59:57 -0400 | [diff] [blame] | 392 | v9fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from) |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 393 | { |
Al Viro | e494b6b | 2015-04-01 23:59:57 -0400 | [diff] [blame] | 394 | struct file *file = iocb->ki_filp; |
Al Viro | 3309dd0 | 2015-04-09 12:55:47 -0400 | [diff] [blame] | 395 | ssize_t retval; |
| 396 | loff_t origin; |
Al Viro | c711a6b | 2015-04-01 22:04:46 -0400 | [diff] [blame] | 397 | int err = 0; |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 398 | |
Al Viro | 3309dd0 | 2015-04-09 12:55:47 -0400 | [diff] [blame] | 399 | retval = generic_write_checks(iocb, from); |
| 400 | if (retval <= 0) |
Al Viro | c711a6b | 2015-04-01 22:04:46 -0400 | [diff] [blame] | 401 | return retval; |
Harsh Prateek Bora | 3834b12 | 2010-08-03 11:55:40 +0000 | [diff] [blame] | 402 | |
Al Viro | 3309dd0 | 2015-04-09 12:55:47 -0400 | [diff] [blame] | 403 | origin = iocb->ki_pos; |
| 404 | retval = p9_client_write(file->private_data, iocb->ki_pos, from, &err); |
Al Viro | c711a6b | 2015-04-01 22:04:46 -0400 | [diff] [blame] | 405 | if (retval > 0) { |
Al Viro | e494b6b | 2015-04-01 23:59:57 -0400 | [diff] [blame] | 406 | struct inode *inode = file_inode(file); |
Al Viro | c711a6b | 2015-04-01 22:04:46 -0400 | [diff] [blame] | 407 | loff_t i_size; |
| 408 | unsigned long pg_start, pg_end; |
Dominique Martinet | 6e195b0 | 2021-11-02 22:16:43 +0900 | [diff] [blame] | 409 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 410 | pg_start = origin >> PAGE_SHIFT; |
| 411 | pg_end = (origin + retval - 1) >> PAGE_SHIFT; |
Al Viro | c711a6b | 2015-04-01 22:04:46 -0400 | [diff] [blame] | 412 | if (inode->i_mapping && inode->i_mapping->nrpages) |
| 413 | invalidate_inode_pages2_range(inode->i_mapping, |
| 414 | pg_start, pg_end); |
Al Viro | 3309dd0 | 2015-04-09 12:55:47 -0400 | [diff] [blame] | 415 | iocb->ki_pos += retval; |
Al Viro | c711a6b | 2015-04-01 22:04:46 -0400 | [diff] [blame] | 416 | i_size = i_size_read(inode); |
Al Viro | 3309dd0 | 2015-04-09 12:55:47 -0400 | [diff] [blame] | 417 | if (iocb->ki_pos > i_size) { |
| 418 | inode_add_bytes(inode, iocb->ki_pos - i_size); |
Hou Tao | 5e3cc1e | 2019-01-24 14:35:13 +0800 | [diff] [blame] | 419 | /* |
| 420 | * Need to serialize against i_size_write() in |
| 421 | * v9fs_stat2inode() |
| 422 | */ |
| 423 | v9fs_i_size_write(inode, iocb->ki_pos); |
Al Viro | c711a6b | 2015-04-01 22:04:46 -0400 | [diff] [blame] | 424 | } |
| 425 | return retval; |
| 426 | } |
| 427 | return err; |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 428 | } |
| 429 | |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 430 | static int v9fs_file_fsync(struct file *filp, loff_t start, loff_t end, |
| 431 | int datasync) |
M. Mohan Kumar | 7a4439c | 2010-02-08 15:36:48 -0600 | [diff] [blame] | 432 | { |
| 433 | struct p9_fid *fid; |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 434 | struct inode *inode = filp->f_mapping->host; |
M. Mohan Kumar | 7a4439c | 2010-02-08 15:36:48 -0600 | [diff] [blame] | 435 | struct p9_wstat wstat; |
| 436 | int retval; |
| 437 | |
Jeff Layton | 3b49c9a | 2017-07-07 15:20:52 -0400 | [diff] [blame] | 438 | retval = file_write_and_wait_range(filp, start, end); |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 439 | if (retval) |
| 440 | return retval; |
| 441 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 442 | inode_lock(inode); |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 443 | p9_debug(P9_DEBUG_VFS, "filp %p datasync %x\n", filp, datasync); |
M. Mohan Kumar | 7a4439c | 2010-02-08 15:36:48 -0600 | [diff] [blame] | 444 | |
| 445 | fid = filp->private_data; |
| 446 | v9fs_blank_wstat(&wstat); |
| 447 | |
| 448 | retval = p9_client_wstat(fid, &wstat); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 449 | inode_unlock(inode); |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 450 | |
M. Mohan Kumar | 7a4439c | 2010-02-08 15:36:48 -0600 | [diff] [blame] | 451 | return retval; |
| 452 | } |
| 453 | |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 454 | int v9fs_file_fsync_dotl(struct file *filp, loff_t start, loff_t end, |
| 455 | int datasync) |
Venkateswararao Jujjuri (JV) | 920e65d | 2010-09-22 17:19:19 -0700 | [diff] [blame] | 456 | { |
| 457 | struct p9_fid *fid; |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 458 | struct inode *inode = filp->f_mapping->host; |
Venkateswararao Jujjuri (JV) | 920e65d | 2010-09-22 17:19:19 -0700 | [diff] [blame] | 459 | int retval; |
| 460 | |
Jeff Layton | 3b49c9a | 2017-07-07 15:20:52 -0400 | [diff] [blame] | 461 | retval = file_write_and_wait_range(filp, start, end); |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 462 | if (retval) |
| 463 | return retval; |
| 464 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 465 | inode_lock(inode); |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 466 | p9_debug(P9_DEBUG_VFS, "filp %p datasync %x\n", filp, datasync); |
Venkateswararao Jujjuri (JV) | 920e65d | 2010-09-22 17:19:19 -0700 | [diff] [blame] | 467 | |
| 468 | fid = filp->private_data; |
| 469 | |
Venkateswararao Jujjuri (JV) | b165d60 | 2010-10-22 10:13:12 -0700 | [diff] [blame] | 470 | retval = p9_client_fsync(fid, datasync); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 471 | inode_unlock(inode); |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 472 | |
Venkateswararao Jujjuri (JV) | 920e65d | 2010-09-22 17:19:19 -0700 | [diff] [blame] | 473 | return retval; |
| 474 | } |
| 475 | |
Aneesh Kumar K.V | 7263ceb | 2011-02-28 17:03:58 +0530 | [diff] [blame] | 476 | static int |
Dominique Martinet | fb89b45 | 2014-01-10 13:44:09 +0100 | [diff] [blame] | 477 | v9fs_file_mmap(struct file *filp, struct vm_area_struct *vma) |
Aneesh Kumar K.V | 7263ceb | 2011-02-28 17:03:58 +0530 | [diff] [blame] | 478 | { |
| 479 | int retval; |
| 480 | |
Dominique Martinet | fb89b45 | 2014-01-10 13:44:09 +0100 | [diff] [blame] | 481 | |
| 482 | retval = generic_file_mmap(filp, vma); |
Aneesh Kumar K.V | 7263ceb | 2011-02-28 17:03:58 +0530 | [diff] [blame] | 483 | if (!retval) |
| 484 | vma->vm_ops = &v9fs_file_vm_ops; |
| 485 | |
| 486 | return retval; |
| 487 | } |
| 488 | |
| 489 | static int |
Dominique Martinet | fb89b45 | 2014-01-10 13:44:09 +0100 | [diff] [blame] | 490 | v9fs_mmap_file_mmap(struct file *filp, struct vm_area_struct *vma) |
| 491 | { |
| 492 | int retval; |
| 493 | struct inode *inode; |
| 494 | struct v9fs_inode *v9inode; |
| 495 | struct p9_fid *fid; |
| 496 | |
| 497 | inode = file_inode(filp); |
| 498 | v9inode = V9FS_I(inode); |
| 499 | mutex_lock(&v9inode->v_mutex); |
| 500 | if (!v9inode->writeback_fid && |
Chengguang Xu | c87a37e | 2019-08-20 18:03:25 +0800 | [diff] [blame] | 501 | (vma->vm_flags & VM_SHARED) && |
Dominique Martinet | fb89b45 | 2014-01-10 13:44:09 +0100 | [diff] [blame] | 502 | (vma->vm_flags & VM_WRITE)) { |
| 503 | /* |
| 504 | * clone a fid and add it to writeback_fid |
| 505 | * we do it during mmap instead of |
| 506 | * page dirty time via write_begin/page_mkwrite |
| 507 | * because we want write after unlink usecase |
| 508 | * to work. |
| 509 | */ |
Miklos Szeredi | b403f0e | 2016-06-29 10:54:23 +0200 | [diff] [blame] | 510 | fid = v9fs_writeback_fid(file_dentry(filp)); |
Dominique Martinet | fb89b45 | 2014-01-10 13:44:09 +0100 | [diff] [blame] | 511 | if (IS_ERR(fid)) { |
| 512 | retval = PTR_ERR(fid); |
| 513 | mutex_unlock(&v9inode->v_mutex); |
| 514 | return retval; |
| 515 | } |
| 516 | v9inode->writeback_fid = (void *) fid; |
| 517 | } |
| 518 | mutex_unlock(&v9inode->v_mutex); |
| 519 | |
| 520 | retval = generic_file_mmap(filp, vma); |
| 521 | if (!retval) |
| 522 | vma->vm_ops = &v9fs_mmap_file_vm_ops; |
| 523 | |
| 524 | return retval; |
| 525 | } |
| 526 | |
Souptick Joarder | fe6340e | 2018-07-17 19:14:35 -0700 | [diff] [blame] | 527 | static vm_fault_t |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 528 | v9fs_vm_page_mkwrite(struct vm_fault *vmf) |
Aneesh Kumar K.V | 7263ceb | 2011-02-28 17:03:58 +0530 | [diff] [blame] | 529 | { |
Aneesh Kumar K.V | 6b39f6d | 2011-02-28 17:04:03 +0530 | [diff] [blame] | 530 | struct v9fs_inode *v9inode; |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 531 | struct folio *folio = page_folio(vmf->page); |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 532 | struct file *filp = vmf->vma->vm_file; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 533 | struct inode *inode = file_inode(filp); |
Aneesh Kumar K.V | 7263ceb | 2011-02-28 17:03:58 +0530 | [diff] [blame] | 534 | |
| 535 | |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 536 | p9_debug(P9_DEBUG_VFS, "folio %p fid %lx\n", |
| 537 | folio, (unsigned long)filp->private_data); |
Aneesh Kumar K.V | 7263ceb | 2011-02-28 17:03:58 +0530 | [diff] [blame] | 538 | |
David Howells | eb49794 | 2021-11-02 08:29:55 +0000 | [diff] [blame] | 539 | v9inode = V9FS_I(inode); |
| 540 | |
| 541 | /* Wait for the page to be written to the cache before we allow it to |
| 542 | * be modified. We then assume the entire page will need writing back. |
| 543 | */ |
| 544 | #ifdef CONFIG_9P_FSCACHE |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 545 | if (folio_test_fscache(folio) && |
| 546 | folio_wait_fscache_killable(folio) < 0) |
| 547 | return VM_FAULT_NOPAGE; |
David Howells | eb49794 | 2021-11-02 08:29:55 +0000 | [diff] [blame] | 548 | #endif |
| 549 | |
Jan Kara | 120c2bc | 2012-06-12 16:20:25 +0200 | [diff] [blame] | 550 | /* Update file times before taking page lock */ |
| 551 | file_update_time(filp); |
| 552 | |
Aneesh Kumar K.V | 6b39f6d | 2011-02-28 17:04:03 +0530 | [diff] [blame] | 553 | BUG_ON(!v9inode->writeback_fid); |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 554 | if (folio_lock_killable(folio) < 0) |
David Howells | eb49794 | 2021-11-02 08:29:55 +0000 | [diff] [blame] | 555 | return VM_FAULT_RETRY; |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 556 | if (folio_mapping(folio) != inode->i_mapping) |
Aneesh Kumar K.V | 7263ceb | 2011-02-28 17:03:58 +0530 | [diff] [blame] | 557 | goto out_unlock; |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 558 | folio_wait_stable(folio); |
Aneesh Kumar K.V | 7263ceb | 2011-02-28 17:03:58 +0530 | [diff] [blame] | 559 | |
| 560 | return VM_FAULT_LOCKED; |
| 561 | out_unlock: |
David Howells | 78525c7 | 2021-08-11 09:49:13 +0100 | [diff] [blame] | 562 | folio_unlock(folio); |
Aneesh Kumar K.V | 7263ceb | 2011-02-28 17:03:58 +0530 | [diff] [blame] | 563 | return VM_FAULT_NOPAGE; |
| 564 | } |
| 565 | |
Dominique Martinet | fb89b45 | 2014-01-10 13:44:09 +0100 | [diff] [blame] | 566 | /** |
David Howells | bc86803 | 2021-10-04 22:07:22 +0100 | [diff] [blame] | 567 | * v9fs_mmap_file_read_iter - read from a file |
| 568 | * @iocb: The operation parameters |
| 569 | * @to: The buffer to read into |
Dominique Martinet | fb89b45 | 2014-01-10 13:44:09 +0100 | [diff] [blame] | 570 | * |
| 571 | */ |
| 572 | static ssize_t |
Al Viro | e494b6b | 2015-04-01 23:59:57 -0400 | [diff] [blame] | 573 | v9fs_mmap_file_read_iter(struct kiocb *iocb, struct iov_iter *to) |
Dominique Martinet | fb89b45 | 2014-01-10 13:44:09 +0100 | [diff] [blame] | 574 | { |
| 575 | /* TODO: Check if there are dirty pages */ |
Al Viro | e494b6b | 2015-04-01 23:59:57 -0400 | [diff] [blame] | 576 | return v9fs_file_read_iter(iocb, to); |
Dominique Martinet | fb89b45 | 2014-01-10 13:44:09 +0100 | [diff] [blame] | 577 | } |
| 578 | |
Dominique Martinet | fb89b45 | 2014-01-10 13:44:09 +0100 | [diff] [blame] | 579 | /** |
David Howells | bc86803 | 2021-10-04 22:07:22 +0100 | [diff] [blame] | 580 | * v9fs_mmap_file_write_iter - write to a file |
| 581 | * @iocb: The operation parameters |
| 582 | * @from: The data to write |
Dominique Martinet | fb89b45 | 2014-01-10 13:44:09 +0100 | [diff] [blame] | 583 | * |
| 584 | */ |
| 585 | static ssize_t |
Al Viro | e494b6b | 2015-04-01 23:59:57 -0400 | [diff] [blame] | 586 | v9fs_mmap_file_write_iter(struct kiocb *iocb, struct iov_iter *from) |
Dominique Martinet | fb89b45 | 2014-01-10 13:44:09 +0100 | [diff] [blame] | 587 | { |
| 588 | /* |
| 589 | * TODO: invalidate mmaps on filp's inode between |
| 590 | * offset and offset+count |
| 591 | */ |
Al Viro | e494b6b | 2015-04-01 23:59:57 -0400 | [diff] [blame] | 592 | return v9fs_file_write_iter(iocb, from); |
Dominique Martinet | fb89b45 | 2014-01-10 13:44:09 +0100 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | static void v9fs_mmap_vm_close(struct vm_area_struct *vma) |
| 596 | { |
| 597 | struct inode *inode; |
| 598 | |
| 599 | struct writeback_control wbc = { |
| 600 | .nr_to_write = LONG_MAX, |
| 601 | .sync_mode = WB_SYNC_ALL, |
Matthew Wilcox (Oracle) | f5f7ab1 | 2020-10-04 19:04:22 +0100 | [diff] [blame] | 602 | .range_start = (loff_t)vma->vm_pgoff * PAGE_SIZE, |
Dominique Martinet | fb89b45 | 2014-01-10 13:44:09 +0100 | [diff] [blame] | 603 | /* absolute end, byte at end included */ |
Matthew Wilcox (Oracle) | f5f7ab1 | 2020-10-04 19:04:22 +0100 | [diff] [blame] | 604 | .range_end = (loff_t)vma->vm_pgoff * PAGE_SIZE + |
Dominique Martinet | fb89b45 | 2014-01-10 13:44:09 +0100 | [diff] [blame] | 605 | (vma->vm_end - vma->vm_start - 1), |
| 606 | }; |
| 607 | |
Chengguang Xu | c87a37e | 2019-08-20 18:03:25 +0800 | [diff] [blame] | 608 | if (!(vma->vm_flags & VM_SHARED)) |
| 609 | return; |
Dominique Martinet | fb89b45 | 2014-01-10 13:44:09 +0100 | [diff] [blame] | 610 | |
| 611 | p9_debug(P9_DEBUG_VFS, "9p VMA close, %p, flushing", vma); |
| 612 | |
| 613 | inode = file_inode(vma->vm_file); |
Josef Bacik | 25d23cd | 2021-07-14 14:47:24 -0400 | [diff] [blame] | 614 | filemap_fdatawrite_wbc(inode->i_mapping, &wbc); |
Dominique Martinet | fb89b45 | 2014-01-10 13:44:09 +0100 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | |
Aneesh Kumar K.V | 7263ceb | 2011-02-28 17:03:58 +0530 | [diff] [blame] | 618 | static const struct vm_operations_struct v9fs_file_vm_ops = { |
| 619 | .fault = filemap_fault, |
Kirill A. Shutemov | f182036 | 2014-04-07 15:37:19 -0700 | [diff] [blame] | 620 | .map_pages = filemap_map_pages, |
Aneesh Kumar K.V | 7263ceb | 2011-02-28 17:03:58 +0530 | [diff] [blame] | 621 | .page_mkwrite = v9fs_vm_page_mkwrite, |
| 622 | }; |
| 623 | |
Dominique Martinet | fb89b45 | 2014-01-10 13:44:09 +0100 | [diff] [blame] | 624 | static const struct vm_operations_struct v9fs_mmap_file_vm_ops = { |
| 625 | .close = v9fs_mmap_vm_close, |
| 626 | .fault = filemap_fault, |
Kirill A. Shutemov | f182036 | 2014-04-07 15:37:19 -0700 | [diff] [blame] | 627 | .map_pages = filemap_map_pages, |
Dominique Martinet | fb89b45 | 2014-01-10 13:44:09 +0100 | [diff] [blame] | 628 | .page_mkwrite = v9fs_vm_page_mkwrite, |
Dominique Martinet | fb89b45 | 2014-01-10 13:44:09 +0100 | [diff] [blame] | 629 | }; |
| 630 | |
Aneesh Kumar K.V | e959b54 | 2011-02-28 17:04:04 +0530 | [diff] [blame] | 631 | |
Aneesh Kumar K.V | 29236f4 | 2011-02-28 17:03:54 +0530 | [diff] [blame] | 632 | const struct file_operations v9fs_cached_file_operations = { |
Eric Van Hensbergen | e03abc0 | 2007-02-11 13:21:39 -0600 | [diff] [blame] | 633 | .llseek = generic_file_llseek, |
Al Viro | aad4f8b | 2014-04-02 14:33:16 -0400 | [diff] [blame] | 634 | .read_iter = generic_file_read_iter, |
Al Viro | 8174202 | 2014-04-03 03:17:43 -0400 | [diff] [blame] | 635 | .write_iter = generic_file_write_iter, |
Eric Van Hensbergen | e03abc0 | 2007-02-11 13:21:39 -0600 | [diff] [blame] | 636 | .open = v9fs_file_open, |
| 637 | .release = v9fs_dir_release, |
| 638 | .lock = v9fs_file_lock, |
Aneesh Kumar K.V | 7263ceb | 2011-02-28 17:03:58 +0530 | [diff] [blame] | 639 | .mmap = v9fs_file_mmap, |
Toke Høiland-Jørgensen | cf03f31 | 2020-12-01 14:54:09 +0100 | [diff] [blame] | 640 | .splice_read = generic_file_splice_read, |
Dominique Martinet | 960f4f8 | 2020-12-01 16:09:37 +0100 | [diff] [blame] | 641 | .splice_write = iter_file_splice_write, |
M. Mohan Kumar | 7a4439c | 2010-02-08 15:36:48 -0600 | [diff] [blame] | 642 | .fsync = v9fs_file_fsync, |
Eric Van Hensbergen | e03abc0 | 2007-02-11 13:21:39 -0600 | [diff] [blame] | 643 | }; |
| 644 | |
Aneesh Kumar K.V | 29236f4 | 2011-02-28 17:03:54 +0530 | [diff] [blame] | 645 | const struct file_operations v9fs_cached_file_operations_dotl = { |
Venkateswararao Jujjuri (JV) | b04faaf | 2010-09-22 16:30:52 -0700 | [diff] [blame] | 646 | .llseek = generic_file_llseek, |
Al Viro | aad4f8b | 2014-04-02 14:33:16 -0400 | [diff] [blame] | 647 | .read_iter = generic_file_read_iter, |
Al Viro | 8174202 | 2014-04-03 03:17:43 -0400 | [diff] [blame] | 648 | .write_iter = generic_file_write_iter, |
Venkateswararao Jujjuri (JV) | b04faaf | 2010-09-22 16:30:52 -0700 | [diff] [blame] | 649 | .open = v9fs_file_open, |
| 650 | .release = v9fs_dir_release, |
M. Mohan Kumar | a099027 | 2010-09-27 11:34:24 +0530 | [diff] [blame] | 651 | .lock = v9fs_file_lock_dotl, |
| 652 | .flock = v9fs_file_flock_dotl, |
Aneesh Kumar K.V | 7263ceb | 2011-02-28 17:03:58 +0530 | [diff] [blame] | 653 | .mmap = v9fs_file_mmap, |
Toke Høiland-Jørgensen | cf03f31 | 2020-12-01 14:54:09 +0100 | [diff] [blame] | 654 | .splice_read = generic_file_splice_read, |
Dominique Martinet | 960f4f8 | 2020-12-01 16:09:37 +0100 | [diff] [blame] | 655 | .splice_write = iter_file_splice_write, |
Venkateswararao Jujjuri (JV) | 920e65d | 2010-09-22 17:19:19 -0700 | [diff] [blame] | 656 | .fsync = v9fs_file_fsync_dotl, |
Venkateswararao Jujjuri (JV) | b04faaf | 2010-09-22 16:30:52 -0700 | [diff] [blame] | 657 | }; |
| 658 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 659 | const struct file_operations v9fs_file_operations = { |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 660 | .llseek = generic_file_llseek, |
Al Viro | e494b6b | 2015-04-01 23:59:57 -0400 | [diff] [blame] | 661 | .read_iter = v9fs_file_read_iter, |
| 662 | .write_iter = v9fs_file_write_iter, |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 663 | .open = v9fs_file_open, |
| 664 | .release = v9fs_dir_release, |
| 665 | .lock = v9fs_file_lock, |
Eric Van Hensbergen | 14b8869 | 2008-02-06 19:25:05 -0600 | [diff] [blame] | 666 | .mmap = generic_file_readonly_mmap, |
Toke Høiland-Jørgensen | cf03f31 | 2020-12-01 14:54:09 +0100 | [diff] [blame] | 667 | .splice_read = generic_file_splice_read, |
Dominique Martinet | 960f4f8 | 2020-12-01 16:09:37 +0100 | [diff] [blame] | 668 | .splice_write = iter_file_splice_write, |
M. Mohan Kumar | 7a4439c | 2010-02-08 15:36:48 -0600 | [diff] [blame] | 669 | .fsync = v9fs_file_fsync, |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 670 | }; |
Sripathi Kodi | 9b6533c | 2010-03-25 12:41:54 +0000 | [diff] [blame] | 671 | |
| 672 | const struct file_operations v9fs_file_operations_dotl = { |
| 673 | .llseek = generic_file_llseek, |
Al Viro | e494b6b | 2015-04-01 23:59:57 -0400 | [diff] [blame] | 674 | .read_iter = v9fs_file_read_iter, |
| 675 | .write_iter = v9fs_file_write_iter, |
Sripathi Kodi | 9b6533c | 2010-03-25 12:41:54 +0000 | [diff] [blame] | 676 | .open = v9fs_file_open, |
| 677 | .release = v9fs_dir_release, |
M. Mohan Kumar | a099027 | 2010-09-27 11:34:24 +0530 | [diff] [blame] | 678 | .lock = v9fs_file_lock_dotl, |
| 679 | .flock = v9fs_file_flock_dotl, |
Sripathi Kodi | 9b6533c | 2010-03-25 12:41:54 +0000 | [diff] [blame] | 680 | .mmap = generic_file_readonly_mmap, |
Toke Høiland-Jørgensen | cf03f31 | 2020-12-01 14:54:09 +0100 | [diff] [blame] | 681 | .splice_read = generic_file_splice_read, |
Dominique Martinet | 960f4f8 | 2020-12-01 16:09:37 +0100 | [diff] [blame] | 682 | .splice_write = iter_file_splice_write, |
Venkateswararao Jujjuri (JV) | 920e65d | 2010-09-22 17:19:19 -0700 | [diff] [blame] | 683 | .fsync = v9fs_file_fsync_dotl, |
Sripathi Kodi | 9b6533c | 2010-03-25 12:41:54 +0000 | [diff] [blame] | 684 | }; |
Dominique Martinet | fb89b45 | 2014-01-10 13:44:09 +0100 | [diff] [blame] | 685 | |
| 686 | const struct file_operations v9fs_mmap_file_operations = { |
| 687 | .llseek = generic_file_llseek, |
Al Viro | e494b6b | 2015-04-01 23:59:57 -0400 | [diff] [blame] | 688 | .read_iter = v9fs_mmap_file_read_iter, |
| 689 | .write_iter = v9fs_mmap_file_write_iter, |
Dominique Martinet | fb89b45 | 2014-01-10 13:44:09 +0100 | [diff] [blame] | 690 | .open = v9fs_file_open, |
| 691 | .release = v9fs_dir_release, |
| 692 | .lock = v9fs_file_lock, |
| 693 | .mmap = v9fs_mmap_file_mmap, |
Toke Høiland-Jørgensen | cf03f31 | 2020-12-01 14:54:09 +0100 | [diff] [blame] | 694 | .splice_read = generic_file_splice_read, |
Dominique Martinet | 960f4f8 | 2020-12-01 16:09:37 +0100 | [diff] [blame] | 695 | .splice_write = iter_file_splice_write, |
Dominique Martinet | fb89b45 | 2014-01-10 13:44:09 +0100 | [diff] [blame] | 696 | .fsync = v9fs_file_fsync, |
| 697 | }; |
| 698 | |
| 699 | const struct file_operations v9fs_mmap_file_operations_dotl = { |
| 700 | .llseek = generic_file_llseek, |
Al Viro | e494b6b | 2015-04-01 23:59:57 -0400 | [diff] [blame] | 701 | .read_iter = v9fs_mmap_file_read_iter, |
| 702 | .write_iter = v9fs_mmap_file_write_iter, |
Dominique Martinet | fb89b45 | 2014-01-10 13:44:09 +0100 | [diff] [blame] | 703 | .open = v9fs_file_open, |
| 704 | .release = v9fs_dir_release, |
| 705 | .lock = v9fs_file_lock_dotl, |
| 706 | .flock = v9fs_file_flock_dotl, |
| 707 | .mmap = v9fs_mmap_file_mmap, |
Toke Høiland-Jørgensen | cf03f31 | 2020-12-01 14:54:09 +0100 | [diff] [blame] | 708 | .splice_read = generic_file_splice_read, |
Dominique Martinet | 960f4f8 | 2020-12-01 16:09:37 +0100 | [diff] [blame] | 709 | .splice_write = iter_file_splice_write, |
Dominique Martinet | fb89b45 | 2014-01-10 13:44:09 +0100 | [diff] [blame] | 710 | .fsync = v9fs_file_fsync_dotl, |
| 711 | }; |