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