Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * File operations used by nfsd. Some of these have been ripped from |
| 4 | * other parts of the kernel because they weren't exported, others |
| 5 | * are partial duplicates with added or changed functionality. |
| 6 | * |
| 7 | * Note that several functions dget() the dentry upon which they want |
| 8 | * to act, most notably those that create directory entries. Response |
| 9 | * dentry's are dput()'d if necessary in the release callback. |
| 10 | * So if you notice code paths that apparently fail to dput() the |
| 11 | * dentry, don't worry--they have been taken care of. |
| 12 | * |
| 13 | * Copyright (C) 1995-1999 Olaf Kirch <okir@monad.swb.de> |
| 14 | * Zerocpy NFS support (C) 2002 Hirokazu Takahashi <taka@valinux.co.jp> |
| 15 | */ |
| 16 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/fs.h> |
| 18 | #include <linux/file.h> |
Jens Axboe | d6b29d7 | 2007-06-04 09:59:47 +0200 | [diff] [blame] | 19 | #include <linux/splice.h> |
Anna Schumaker | 95d871f | 2014-11-07 14:44:26 -0500 | [diff] [blame] | 20 | #include <linux/falloc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/fcntl.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/namei.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/delay.h> |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 24 | #include <linux/fsnotify.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/posix_acl_xattr.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/xattr.h> |
Boaz Harrosh | 9a74af2 | 2009-12-03 20:30:56 +0200 | [diff] [blame] | 27 | #include <linux/jhash.h> |
| 28 | #include <linux/ima.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 29 | #include <linux/slab.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 30 | #include <linux/uaccess.h> |
Ben Myers | f501912 | 2010-02-17 14:05:11 -0600 | [diff] [blame] | 31 | #include <linux/exportfs.h> |
| 32 | #include <linux/writeback.h> |
David Quigley | 18032ca | 2013-05-02 13:19:10 -0400 | [diff] [blame] | 33 | #include <linux/security.h> |
Boaz Harrosh | 9a74af2 | 2009-12-03 20:30:56 +0200 | [diff] [blame] | 34 | |
| 35 | #ifdef CONFIG_NFSD_V3 |
| 36 | #include "xdr3.h" |
| 37 | #endif /* CONFIG_NFSD_V3 */ |
| 38 | |
Christoph Hellwig | 5be196e | 2006-01-09 20:51:55 -0800 | [diff] [blame] | 39 | #ifdef CONFIG_NFSD_V4 |
Christoph Hellwig | ffa0160 | 2015-12-03 12:59:52 +0100 | [diff] [blame] | 40 | #include "../internal.h" |
J. Bruce Fields | 2ca72e1 | 2011-01-04 17:37:15 -0500 | [diff] [blame] | 41 | #include "acl.h" |
| 42 | #include "idmap.h" |
Trond Myklebust | a2f4c3f | 2021-12-18 20:38:00 -0500 | [diff] [blame] | 43 | #include "xdr4.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #endif /* CONFIG_NFSD_V4 */ |
| 45 | |
Boaz Harrosh | 9a74af2 | 2009-12-03 20:30:56 +0200 | [diff] [blame] | 46 | #include "nfsd.h" |
| 47 | #include "vfs.h" |
Jeff Layton | b493523 | 2019-08-18 14:18:49 -0400 | [diff] [blame] | 48 | #include "filecache.h" |
Jeff Layton | 6e8b50d | 2015-11-17 06:52:23 -0500 | [diff] [blame] | 49 | #include "trace.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
| 51 | #define NFSDDBG_FACILITY NFSDDBG_FILEOP |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | /* |
| 54 | * Called from nfsd_lookup and encode_dirent. Check if we have crossed |
| 55 | * a mount point. |
J.Bruce Fields | e0bb89e | 2006-12-13 00:35:25 -0800 | [diff] [blame] | 56 | * Returns -EAGAIN or -ETIMEDOUT leaving *dpp and *expp unchanged, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | * or nfs_ok having possibly changed *dpp and *expp |
| 58 | */ |
| 59 | int |
| 60 | nfsd_cross_mnt(struct svc_rqst *rqstp, struct dentry **dpp, |
| 61 | struct svc_export **expp) |
| 62 | { |
| 63 | struct svc_export *exp = *expp, *exp2 = NULL; |
| 64 | struct dentry *dentry = *dpp; |
Al Viro | 91c9fa8 | 2009-04-18 02:42:05 -0400 | [diff] [blame] | 65 | struct path path = {.mnt = mntget(exp->ex_path.mnt), |
| 66 | .dentry = dget(dentry)}; |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 67 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
Al Viro | 7cc90cc | 2011-03-18 09:04:20 -0400 | [diff] [blame] | 69 | err = follow_down(&path); |
David Howells | cc53ce5 | 2011-01-14 18:45:26 +0000 | [diff] [blame] | 70 | if (err < 0) |
| 71 | goto out; |
NeilBrown | 99bbf6e | 2017-03-15 12:40:44 +1100 | [diff] [blame] | 72 | if (path.mnt == exp->ex_path.mnt && path.dentry == dentry && |
| 73 | nfsd_mountpoint(dentry, exp) == 2) { |
| 74 | /* This is only a mountpoint in some other namespace */ |
| 75 | path_put(&path); |
| 76 | goto out; |
| 77 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
Al Viro | 91c9fa8 | 2009-04-18 02:42:05 -0400 | [diff] [blame] | 79 | exp2 = rqst_exp_get_by_name(rqstp, &path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | if (IS_ERR(exp2)) { |
J. Bruce Fields | 3b6cee7bc | 2009-10-25 21:18:19 -0400 | [diff] [blame] | 81 | err = PTR_ERR(exp2); |
| 82 | /* |
| 83 | * We normally allow NFS clients to continue |
| 84 | * "underneath" a mountpoint that is not exported. |
| 85 | * The exception is V4ROOT, where no traversal is ever |
| 86 | * allowed without an explicit export of the new |
| 87 | * directory. |
| 88 | */ |
| 89 | if (err == -ENOENT && !(exp->ex_flags & NFSEXP_V4ROOT)) |
| 90 | err = 0; |
Al Viro | 91c9fa8 | 2009-04-18 02:42:05 -0400 | [diff] [blame] | 91 | path_put(&path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | goto out; |
| 93 | } |
Steve Dickson | 3c394dd | 2009-09-09 15:02:40 -0400 | [diff] [blame] | 94 | if (nfsd_v4client(rqstp) || |
| 95 | (exp->ex_flags & NFSEXP_CROSSMOUNT) || EX_NOHIDE(exp2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | /* successfully crossed mount point */ |
Al Viro | 1644ccc | 2009-04-18 02:32:31 -0400 | [diff] [blame] | 97 | /* |
Al Viro | 91c9fa8 | 2009-04-18 02:42:05 -0400 | [diff] [blame] | 98 | * This is subtle: path.dentry is *not* on path.mnt |
| 99 | * at this point. The only reason we are safe is that |
| 100 | * original mnt is pinned down by exp, so we should |
| 101 | * put path *before* putting exp |
Al Viro | 1644ccc | 2009-04-18 02:32:31 -0400 | [diff] [blame] | 102 | */ |
Al Viro | 91c9fa8 | 2009-04-18 02:42:05 -0400 | [diff] [blame] | 103 | *dpp = path.dentry; |
| 104 | path.dentry = dentry; |
Al Viro | 1644ccc | 2009-04-18 02:32:31 -0400 | [diff] [blame] | 105 | *expp = exp2; |
Al Viro | 91c9fa8 | 2009-04-18 02:42:05 -0400 | [diff] [blame] | 106 | exp2 = exp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | } |
Al Viro | 91c9fa8 | 2009-04-18 02:42:05 -0400 | [diff] [blame] | 108 | path_put(&path); |
| 109 | exp_put(exp2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | out: |
| 111 | return err; |
| 112 | } |
| 113 | |
J. Bruce Fields | 289ede4 | 2009-09-26 20:32:24 -0400 | [diff] [blame] | 114 | static void follow_to_parent(struct path *path) |
| 115 | { |
| 116 | struct dentry *dp; |
| 117 | |
| 118 | while (path->dentry == path->mnt->mnt_root && follow_up(path)) |
| 119 | ; |
| 120 | dp = dget_parent(path->dentry); |
| 121 | dput(path->dentry); |
| 122 | path->dentry = dp; |
| 123 | } |
| 124 | |
| 125 | static int nfsd_lookup_parent(struct svc_rqst *rqstp, struct dentry *dparent, struct svc_export **exp, struct dentry **dentryp) |
| 126 | { |
| 127 | struct svc_export *exp2; |
| 128 | struct path path = {.mnt = mntget((*exp)->ex_path.mnt), |
| 129 | .dentry = dget(dparent)}; |
| 130 | |
| 131 | follow_to_parent(&path); |
| 132 | |
| 133 | exp2 = rqst_exp_parent(rqstp, &path); |
| 134 | if (PTR_ERR(exp2) == -ENOENT) { |
| 135 | *dentryp = dget(dparent); |
| 136 | } else if (IS_ERR(exp2)) { |
| 137 | path_put(&path); |
| 138 | return PTR_ERR(exp2); |
| 139 | } else { |
| 140 | *dentryp = dget(path.dentry); |
| 141 | exp_put(*exp); |
| 142 | *exp = exp2; |
| 143 | } |
| 144 | path_put(&path); |
| 145 | return 0; |
| 146 | } |
| 147 | |
J. Bruce Fields | 82ead7f | 2009-10-25 21:33:15 -0400 | [diff] [blame] | 148 | /* |
| 149 | * For nfsd purposes, we treat V4ROOT exports as though there was an |
| 150 | * export at *every* directory. |
NeilBrown | 99bbf6e | 2017-03-15 12:40:44 +1100 | [diff] [blame] | 151 | * We return: |
| 152 | * '1' if this dentry *must* be an export point, |
| 153 | * '2' if it might be, if there is really a mount here, and |
| 154 | * '0' if there is no chance of an export point here. |
J. Bruce Fields | 82ead7f | 2009-10-25 21:33:15 -0400 | [diff] [blame] | 155 | */ |
J. Bruce Fields | 3227fa4 | 2009-10-25 21:43:01 -0400 | [diff] [blame] | 156 | int nfsd_mountpoint(struct dentry *dentry, struct svc_export *exp) |
J. Bruce Fields | 82ead7f | 2009-10-25 21:33:15 -0400 | [diff] [blame] | 157 | { |
NeilBrown | 99bbf6e | 2017-03-15 12:40:44 +1100 | [diff] [blame] | 158 | if (!d_inode(dentry)) |
| 159 | return 0; |
| 160 | if (exp->ex_flags & NFSEXP_V4ROOT) |
J. Bruce Fields | 82ead7f | 2009-10-25 21:33:15 -0400 | [diff] [blame] | 161 | return 1; |
Trond Myklebust | 11fcee02 | 2011-09-12 19:37:26 -0400 | [diff] [blame] | 162 | if (nfsd4_is_junction(dentry)) |
| 163 | return 1; |
NeilBrown | 99bbf6e | 2017-03-15 12:40:44 +1100 | [diff] [blame] | 164 | if (d_mountpoint(dentry)) |
| 165 | /* |
| 166 | * Might only be a mountpoint in a different namespace, |
| 167 | * but we need to check. |
| 168 | */ |
| 169 | return 2; |
| 170 | return 0; |
J. Bruce Fields | 82ead7f | 2009-10-25 21:33:15 -0400 | [diff] [blame] | 171 | } |
| 172 | |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 173 | __be32 |
J. Bruce Fields | 6c0a654 | 2007-07-17 04:04:47 -0700 | [diff] [blame] | 174 | nfsd_lookup_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp, |
Chuck Lever | 5a022fc | 2007-11-01 16:57:09 -0400 | [diff] [blame] | 175 | const char *name, unsigned int len, |
J. Bruce Fields | 6c0a654 | 2007-07-17 04:04:47 -0700 | [diff] [blame] | 176 | struct svc_export **exp_ret, struct dentry **dentry_ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | { |
| 178 | struct svc_export *exp; |
| 179 | struct dentry *dparent; |
| 180 | struct dentry *dentry; |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 181 | int host_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | |
| 183 | dprintk("nfsd: nfsd_lookup(fh %s, %.*s)\n", SVCFH_fmt(fhp), len,name); |
| 184 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | dparent = fhp->fh_dentry; |
Kinglong Mee | bf18f16 | 2014-06-10 22:06:44 +0800 | [diff] [blame] | 186 | exp = exp_get(fhp->fh_export); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | /* Lookup the name, but don't follow links */ |
| 189 | if (isdotent(name, len)) { |
| 190 | if (len==1) |
| 191 | dentry = dget(dparent); |
Jan Blunck | 5477549 | 2008-02-14 19:38:39 -0800 | [diff] [blame] | 192 | else if (dparent != exp->ex_path.dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | dentry = dget_parent(dparent); |
J. Bruce Fields | fed8381 | 2009-09-26 16:53:01 -0400 | [diff] [blame] | 194 | else if (!EX_NOHIDE(exp) && !nfsd_v4client(rqstp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | dentry = dget(dparent); /* .. == . just like at / */ |
| 196 | else { |
| 197 | /* checking mountpoint crossing is very different when stepping up */ |
J. Bruce Fields | 289ede4 | 2009-09-26 20:32:24 -0400 | [diff] [blame] | 198 | host_err = nfsd_lookup_parent(rqstp, dparent, &exp, &dentry); |
| 199 | if (host_err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | goto out_nfserr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | } |
| 202 | } else { |
J. Bruce Fields | 4335723 | 2014-01-24 18:04:40 -0500 | [diff] [blame] | 203 | /* |
| 204 | * In the nfsd4_open() case, this may be held across |
| 205 | * subsequent open and delegation acquisition which may |
| 206 | * need to take the child's i_mutex: |
| 207 | */ |
| 208 | fh_lock_nested(fhp, I_MUTEX_PARENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | dentry = lookup_one_len(name, dparent, len); |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 210 | host_err = PTR_ERR(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | if (IS_ERR(dentry)) |
| 212 | goto out_nfserr; |
J. Bruce Fields | 82ead7f | 2009-10-25 21:33:15 -0400 | [diff] [blame] | 213 | if (nfsd_mountpoint(dentry, exp)) { |
NeilBrown | bbddca8 | 2016-01-07 16:08:20 -0500 | [diff] [blame] | 214 | /* |
| 215 | * We don't need the i_mutex after all. It's |
| 216 | * still possible we could open this (regular |
| 217 | * files can be mountpoints too), but the |
| 218 | * i_mutex is just there to prevent renames of |
| 219 | * something that we might be about to delegate, |
| 220 | * and a mountpoint won't be renamed: |
| 221 | */ |
| 222 | fh_unlock(fhp); |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 223 | if ((host_err = nfsd_cross_mnt(rqstp, &dentry, &exp))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | dput(dentry); |
| 225 | goto out_nfserr; |
| 226 | } |
| 227 | } |
| 228 | } |
J. Bruce Fields | 6c0a654 | 2007-07-17 04:04:47 -0700 | [diff] [blame] | 229 | *dentry_ret = dentry; |
| 230 | *exp_ret = exp; |
| 231 | return 0; |
| 232 | |
| 233 | out_nfserr: |
| 234 | exp_put(exp); |
| 235 | return nfserrno(host_err); |
| 236 | } |
| 237 | |
| 238 | /* |
| 239 | * Look up one component of a pathname. |
| 240 | * N.B. After this call _both_ fhp and resfh need an fh_put |
| 241 | * |
| 242 | * If the lookup would cross a mountpoint, and the mounted filesystem |
| 243 | * is exported to the client with NFSEXP_NOHIDE, then the lookup is |
| 244 | * accepted as it stands and the mounted directory is |
| 245 | * returned. Otherwise the covered directory is returned. |
| 246 | * NOTE: this mountpoint crossing is not supported properly by all |
| 247 | * clients and is explicitly disallowed for NFSv3 |
NeilBrown | ef5825e | 2021-09-02 11:14:47 +1000 | [diff] [blame] | 248 | * NeilBrown <neilb@cse.unsw.edu.au> |
J. Bruce Fields | 6c0a654 | 2007-07-17 04:04:47 -0700 | [diff] [blame] | 249 | */ |
| 250 | __be32 |
| 251 | nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name, |
Chuck Lever | 5a022fc | 2007-11-01 16:57:09 -0400 | [diff] [blame] | 252 | unsigned int len, struct svc_fh *resfh) |
J. Bruce Fields | 6c0a654 | 2007-07-17 04:04:47 -0700 | [diff] [blame] | 253 | { |
| 254 | struct svc_export *exp; |
| 255 | struct dentry *dentry; |
| 256 | __be32 err; |
| 257 | |
J. Bruce Fields | 29a78a3 | 2011-04-09 11:28:53 -0400 | [diff] [blame] | 258 | err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC); |
| 259 | if (err) |
| 260 | return err; |
J. Bruce Fields | 6c0a654 | 2007-07-17 04:04:47 -0700 | [diff] [blame] | 261 | err = nfsd_lookup_dentry(rqstp, fhp, name, len, &exp, &dentry); |
| 262 | if (err) |
| 263 | return err; |
Andy Adamson | 32c1eb0 | 2007-07-17 04:04:48 -0700 | [diff] [blame] | 264 | err = check_nfsd_access(exp, rqstp); |
| 265 | if (err) |
| 266 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | /* |
| 268 | * Note: we compose the file handle now, but as the |
| 269 | * dentry may be negative, it may need to be updated. |
| 270 | */ |
| 271 | err = fh_compose(resfh, exp, dentry, fhp); |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 272 | if (!err && d_really_is_negative(dentry)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | err = nfserr_noent; |
Andy Adamson | 32c1eb0 | 2007-07-17 04:04:48 -0700 | [diff] [blame] | 274 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | dput(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | exp_put(exp); |
| 277 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Ben Myers | f501912 | 2010-02-17 14:05:11 -0600 | [diff] [blame] | 280 | /* |
| 281 | * Commit metadata changes to stable storage. |
| 282 | */ |
| 283 | static int |
Trond Myklebust | 57f6403 | 2019-12-18 14:57:23 -0500 | [diff] [blame] | 284 | commit_inode_metadata(struct inode *inode) |
Ben Myers | f501912 | 2010-02-17 14:05:11 -0600 | [diff] [blame] | 285 | { |
Ben Myers | f501912 | 2010-02-17 14:05:11 -0600 | [diff] [blame] | 286 | const struct export_operations *export_ops = inode->i_sb->s_export_op; |
Ben Myers | f501912 | 2010-02-17 14:05:11 -0600 | [diff] [blame] | 287 | |
Christoph Hellwig | c3765016 | 2010-10-06 10:48:20 +0200 | [diff] [blame] | 288 | if (export_ops->commit_metadata) |
| 289 | return export_ops->commit_metadata(inode); |
| 290 | return sync_inode_metadata(inode, 1); |
Ben Myers | f501912 | 2010-02-17 14:05:11 -0600 | [diff] [blame] | 291 | } |
J. Bruce Fields | 6c0a654 | 2007-07-17 04:04:47 -0700 | [diff] [blame] | 292 | |
Trond Myklebust | 57f6403 | 2019-12-18 14:57:23 -0500 | [diff] [blame] | 293 | static int |
| 294 | commit_metadata(struct svc_fh *fhp) |
| 295 | { |
| 296 | struct inode *inode = d_inode(fhp->fh_dentry); |
| 297 | |
| 298 | if (!EX_ISSYNC(fhp->fh_export)) |
| 299 | return 0; |
| 300 | return commit_inode_metadata(inode); |
| 301 | } |
| 302 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | /* |
Christoph Hellwig | 818e5a2 | 2013-11-18 05:07:30 -0800 | [diff] [blame] | 304 | * Go over the attributes and take care of the small differences between |
| 305 | * NFS semantics and what Linux expects. |
| 306 | */ |
| 307 | static void |
| 308 | nfsd_sanitize_attrs(struct inode *inode, struct iattr *iap) |
| 309 | { |
Christoph Hellwig | 818e5a2 | 2013-11-18 05:07:30 -0800 | [diff] [blame] | 310 | /* sanitize the mode change */ |
| 311 | if (iap->ia_valid & ATTR_MODE) { |
| 312 | iap->ia_mode &= S_IALLUGO; |
| 313 | iap->ia_mode |= (inode->i_mode & ~S_IALLUGO); |
| 314 | } |
| 315 | |
| 316 | /* Revoke setuid/setgid on chown */ |
| 317 | if (!S_ISDIR(inode->i_mode) && |
Stanislav Kholmanskikh | c4fa6d7 | 2013-12-11 14:16:36 +0400 | [diff] [blame] | 318 | ((iap->ia_valid & ATTR_UID) || (iap->ia_valid & ATTR_GID))) { |
Christoph Hellwig | 818e5a2 | 2013-11-18 05:07:30 -0800 | [diff] [blame] | 319 | iap->ia_valid |= ATTR_KILL_PRIV; |
| 320 | if (iap->ia_valid & ATTR_MODE) { |
| 321 | /* we're setting mode too, just clear the s*id bits */ |
| 322 | iap->ia_mode &= ~S_ISUID; |
| 323 | if (iap->ia_mode & S_IXGRP) |
| 324 | iap->ia_mode &= ~S_ISGID; |
| 325 | } else { |
| 326 | /* set ATTR_KILL_* bits and let VFS handle it */ |
| 327 | iap->ia_valid |= (ATTR_KILL_SUID | ATTR_KILL_SGID); |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | |
J. Bruce Fields | 0839ffb | 2017-02-09 14:20:42 -0500 | [diff] [blame] | 332 | static __be32 |
| 333 | nfsd_get_write_access(struct svc_rqst *rqstp, struct svc_fh *fhp, |
| 334 | struct iattr *iap) |
| 335 | { |
| 336 | struct inode *inode = d_inode(fhp->fh_dentry); |
J. Bruce Fields | 0839ffb | 2017-02-09 14:20:42 -0500 | [diff] [blame] | 337 | |
| 338 | if (iap->ia_size < inode->i_size) { |
| 339 | __be32 err; |
| 340 | |
| 341 | err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry, |
| 342 | NFSD_MAY_TRUNC | NFSD_MAY_OWNER_OVERRIDE); |
| 343 | if (err) |
| 344 | return err; |
| 345 | } |
Jeff Layton | f7e33bd | 2021-08-19 14:56:38 -0400 | [diff] [blame] | 346 | return nfserrno(get_write_access(inode)); |
J. Bruce Fields | 0839ffb | 2017-02-09 14:20:42 -0500 | [diff] [blame] | 347 | } |
| 348 | |
Christoph Hellwig | 818e5a2 | 2013-11-18 05:07:30 -0800 | [diff] [blame] | 349 | /* |
| 350 | * Set various file attributes. After this call fhp needs an fh_put. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | */ |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 352 | __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap, |
Arnd Bergmann | 2a1aa48 | 2019-11-03 17:50:18 +0100 | [diff] [blame] | 354 | int check_guard, time64_t guardtime) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | { |
| 356 | struct dentry *dentry; |
| 357 | struct inode *inode; |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 358 | int accmode = NFSD_MAY_SATTR; |
Al Viro | 175a4eb | 2011-07-26 03:30:54 -0400 | [diff] [blame] | 359 | umode_t ftype = 0; |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 360 | __be32 err; |
| 361 | int host_err; |
J. Bruce Fields | 9f67f18 | 2014-02-24 14:59:47 -0500 | [diff] [blame] | 362 | bool get_write_count; |
Christoph Hellwig | 758e99f | 2017-02-20 17:04:42 -0500 | [diff] [blame] | 363 | bool size_change = (iap->ia_valid & ATTR_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | |
zhengbin | 255fbca | 2018-11-30 16:04:25 +0800 | [diff] [blame] | 365 | if (iap->ia_valid & ATTR_SIZE) { |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 366 | accmode |= NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | ftype = S_IFREG; |
zhengbin | 255fbca | 2018-11-30 16:04:25 +0800 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | /* |
| 371 | * If utimes(2) and friends are called with times not NULL, we should |
| 372 | * not set NFSD_MAY_WRITE bit. Otherwise fh_verify->nfsd_permission |
Geert Uytterhoeven | e977cc8 | 2019-05-27 14:21:32 +0200 | [diff] [blame] | 373 | * will return EACCES, when the caller's effective UID does not match |
zhengbin | 255fbca | 2018-11-30 16:04:25 +0800 | [diff] [blame] | 374 | * the owner of the file, and the caller is not privileged. In this |
| 375 | * situation, we should return EPERM(notify_change will return this). |
| 376 | */ |
| 377 | if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME)) { |
| 378 | accmode |= NFSD_MAY_OWNER_OVERRIDE; |
| 379 | if (!(iap->ia_valid & (ATTR_ATIME_SET | ATTR_MTIME_SET))) |
| 380 | accmode |= NFSD_MAY_WRITE; |
| 381 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | |
J. Bruce Fields | 9f67f18 | 2014-02-24 14:59:47 -0500 | [diff] [blame] | 383 | /* Callers that do fh_verify should do the fh_want_write: */ |
| 384 | get_write_count = !fhp->fh_dentry; |
| 385 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | /* Get inode */ |
| 387 | err = fh_verify(rqstp, fhp, ftype, accmode); |
NeilBrown | 15b7a1b | 2005-11-07 01:00:23 -0800 | [diff] [blame] | 388 | if (err) |
Christoph Hellwig | 758e99f | 2017-02-20 17:04:42 -0500 | [diff] [blame] | 389 | return err; |
J. Bruce Fields | 9f67f18 | 2014-02-24 14:59:47 -0500 | [diff] [blame] | 390 | if (get_write_count) { |
| 391 | host_err = fh_want_write(fhp); |
| 392 | if (host_err) |
Christoph Hellwig | 758e99f | 2017-02-20 17:04:42 -0500 | [diff] [blame] | 393 | goto out; |
J. Bruce Fields | 9f67f18 | 2014-02-24 14:59:47 -0500 | [diff] [blame] | 394 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | |
| 396 | dentry = fhp->fh_dentry; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 397 | inode = d_inode(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | |
NeilBrown | 15b7a1b | 2005-11-07 01:00:23 -0800 | [diff] [blame] | 399 | /* Ignore any mode updates on symlinks */ |
| 400 | if (S_ISLNK(inode->i_mode)) |
| 401 | iap->ia_valid &= ~ATTR_MODE; |
| 402 | |
| 403 | if (!iap->ia_valid) |
Christoph Hellwig | 758e99f | 2017-02-20 17:04:42 -0500 | [diff] [blame] | 404 | return 0; |
NeilBrown | 15b7a1b | 2005-11-07 01:00:23 -0800 | [diff] [blame] | 405 | |
Christoph Hellwig | 818e5a2 | 2013-11-18 05:07:30 -0800 | [diff] [blame] | 406 | nfsd_sanitize_attrs(inode, iap); |
| 407 | |
Christoph Hellwig | 758e99f | 2017-02-20 17:04:42 -0500 | [diff] [blame] | 408 | if (check_guard && guardtime != inode->i_ctime.tv_sec) |
| 409 | return nfserr_notsync; |
| 410 | |
Christoph Hellwig | 9c85fca | 2007-03-07 15:26:25 +0000 | [diff] [blame] | 411 | /* |
Christoph Hellwig | 818e5a2 | 2013-11-18 05:07:30 -0800 | [diff] [blame] | 412 | * The size case is special, it changes the file in addition to the |
Christoph Hellwig | 783112f | 2017-02-20 07:21:33 +0100 | [diff] [blame] | 413 | * attributes, and file systems don't expect it to be mixed with |
| 414 | * "random" attribute changes. We thus split out the size change |
| 415 | * into a separate call to ->setattr, and do the rest as a separate |
| 416 | * setattr call. |
Christoph Hellwig | 9c85fca | 2007-03-07 15:26:25 +0000 | [diff] [blame] | 417 | */ |
Christoph Hellwig | 758e99f | 2017-02-20 17:04:42 -0500 | [diff] [blame] | 418 | if (size_change) { |
J. Bruce Fields | 0839ffb | 2017-02-09 14:20:42 -0500 | [diff] [blame] | 419 | err = nfsd_get_write_access(rqstp, fhp, iap); |
| 420 | if (err) |
Christoph Hellwig | 758e99f | 2017-02-20 17:04:42 -0500 | [diff] [blame] | 421 | return err; |
Christoph Hellwig | 783112f | 2017-02-20 07:21:33 +0100 | [diff] [blame] | 422 | } |
Christoph Hellwig | f0c6312 | 2014-09-07 12:15:52 -0700 | [diff] [blame] | 423 | |
Christoph Hellwig | 783112f | 2017-02-20 07:21:33 +0100 | [diff] [blame] | 424 | fh_lock(fhp); |
| 425 | if (size_change) { |
Christoph Hellwig | f0c6312 | 2014-09-07 12:15:52 -0700 | [diff] [blame] | 426 | /* |
J. Bruce Fields | 0839ffb | 2017-02-09 14:20:42 -0500 | [diff] [blame] | 427 | * RFC5661, Section 18.30.4: |
| 428 | * Changing the size of a file with SETATTR indirectly |
| 429 | * changes the time_modify and change attributes. |
| 430 | * |
| 431 | * (and similar for the older RFCs) |
Christoph Hellwig | f0c6312 | 2014-09-07 12:15:52 -0700 | [diff] [blame] | 432 | */ |
Christoph Hellwig | 783112f | 2017-02-20 07:21:33 +0100 | [diff] [blame] | 433 | struct iattr size_attr = { |
| 434 | .ia_valid = ATTR_SIZE | ATTR_CTIME | ATTR_MTIME, |
| 435 | .ia_size = iap->ia_size, |
| 436 | }; |
| 437 | |
Chuck Lever | e6faac3 | 2022-01-31 13:01:53 -0500 | [diff] [blame] | 438 | host_err = -EFBIG; |
| 439 | if (iap->ia_size < 0) |
| 440 | goto out_unlock; |
| 441 | |
Christian Brauner | 2f221d6 | 2021-01-21 14:19:26 +0100 | [diff] [blame] | 442 | host_err = notify_change(&init_user_ns, dentry, &size_attr, NULL); |
Christoph Hellwig | 783112f | 2017-02-20 07:21:33 +0100 | [diff] [blame] | 443 | if (host_err) |
| 444 | goto out_unlock; |
| 445 | iap->ia_valid &= ~ATTR_SIZE; |
| 446 | |
| 447 | /* |
| 448 | * Avoid the additional setattr call below if the only other |
| 449 | * attribute that the client sends is the mtime, as we update |
| 450 | * it as part of the size change above. |
| 451 | */ |
| 452 | if ((iap->ia_valid & ~ATTR_MTIME) == 0) |
| 453 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | } |
| 455 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | iap->ia_valid |= ATTR_CTIME; |
Christian Brauner | 2f221d6 | 2021-01-21 14:19:26 +0100 | [diff] [blame] | 457 | host_err = notify_change(&init_user_ns, dentry, iap, NULL); |
Christoph Hellwig | 987da47 | 2013-11-18 05:07:47 -0800 | [diff] [blame] | 458 | |
Christoph Hellwig | 783112f | 2017-02-20 07:21:33 +0100 | [diff] [blame] | 459 | out_unlock: |
| 460 | fh_unlock(fhp); |
J. Bruce Fields | 0839ffb | 2017-02-09 14:20:42 -0500 | [diff] [blame] | 461 | if (size_change) |
| 462 | put_write_access(inode); |
J. Bruce Fields | 0839ffb | 2017-02-09 14:20:42 -0500 | [diff] [blame] | 463 | out: |
Christoph Hellwig | 758e99f | 2017-02-20 17:04:42 -0500 | [diff] [blame] | 464 | if (!host_err) |
| 465 | host_err = commit_metadata(fhp); |
| 466 | return nfserrno(host_err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | } |
| 468 | |
Christoph Hellwig | 5be196e | 2006-01-09 20:51:55 -0800 | [diff] [blame] | 469 | #if defined(CONFIG_NFSD_V4) |
Chuck Lever | 9b4146e | 2012-01-04 16:26:43 -0500 | [diff] [blame] | 470 | /* |
| 471 | * NFS junction information is stored in an extended attribute. |
| 472 | */ |
| 473 | #define NFSD_JUNCTION_XATTR_NAME XATTR_TRUSTED_PREFIX "junction.nfs" |
| 474 | |
| 475 | /** |
| 476 | * nfsd4_is_junction - Test if an object could be an NFS junction |
| 477 | * |
| 478 | * @dentry: object to test |
| 479 | * |
| 480 | * Returns 1 if "dentry" appears to contain NFS junction information. |
| 481 | * Otherwise 0 is returned. |
| 482 | */ |
Trond Myklebust | 11fcee02 | 2011-09-12 19:37:26 -0400 | [diff] [blame] | 483 | int nfsd4_is_junction(struct dentry *dentry) |
| 484 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 485 | struct inode *inode = d_inode(dentry); |
Trond Myklebust | 11fcee02 | 2011-09-12 19:37:26 -0400 | [diff] [blame] | 486 | |
| 487 | if (inode == NULL) |
| 488 | return 0; |
| 489 | if (inode->i_mode & S_IXUGO) |
| 490 | return 0; |
| 491 | if (!(inode->i_mode & S_ISVTX)) |
| 492 | return 0; |
Tycho Andersen | c7c7a1a1 | 2021-01-21 14:19:28 +0100 | [diff] [blame] | 493 | if (vfs_getxattr(&init_user_ns, dentry, NFSD_JUNCTION_XATTR_NAME, |
| 494 | NULL, 0) <= 0) |
Trond Myklebust | 11fcee02 | 2011-09-12 19:37:26 -0400 | [diff] [blame] | 495 | return 0; |
| 496 | return 1; |
| 497 | } |
David Quigley | 18032ca | 2013-05-02 13:19:10 -0400 | [diff] [blame] | 498 | #ifdef CONFIG_NFSD_V4_SECURITY_LABEL |
| 499 | __be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp, |
| 500 | struct xdr_netobj *label) |
| 501 | { |
| 502 | __be32 error; |
| 503 | int host_error; |
| 504 | struct dentry *dentry; |
| 505 | |
| 506 | error = fh_verify(rqstp, fhp, 0 /* S_IFREG */, NFSD_MAY_SATTR); |
| 507 | if (error) |
| 508 | return error; |
| 509 | |
| 510 | dentry = fhp->fh_dentry; |
| 511 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 512 | inode_lock(d_inode(dentry)); |
David Quigley | 18032ca | 2013-05-02 13:19:10 -0400 | [diff] [blame] | 513 | host_error = security_inode_setsecctx(dentry, label->data, label->len); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 514 | inode_unlock(d_inode(dentry)); |
David Quigley | 18032ca | 2013-05-02 13:19:10 -0400 | [diff] [blame] | 515 | return nfserrno(host_error); |
| 516 | } |
| 517 | #else |
| 518 | __be32 nfsd4_set_nfs4_label(struct svc_rqst *rqstp, struct svc_fh *fhp, |
| 519 | struct xdr_netobj *label) |
| 520 | { |
| 521 | return nfserr_notsupp; |
| 522 | } |
| 523 | #endif |
| 524 | |
Trond Myklebust | a2f4c3f | 2021-12-18 20:38:00 -0500 | [diff] [blame] | 525 | static struct nfsd4_compound_state *nfsd4_get_cstate(struct svc_rqst *rqstp) |
| 526 | { |
| 527 | return &((struct nfsd4_compoundres *)rqstp->rq_resp)->cstate; |
| 528 | } |
| 529 | |
| 530 | __be32 nfsd4_clone_file_range(struct svc_rqst *rqstp, |
| 531 | struct nfsd_file *nf_src, u64 src_pos, |
| 532 | struct nfsd_file *nf_dst, u64 dst_pos, |
| 533 | u64 count, bool sync) |
Christoph Hellwig | ffa0160 | 2015-12-03 12:59:52 +0100 | [diff] [blame] | 534 | { |
Trond Myklebust | b66ae6d | 2020-01-06 13:40:32 -0500 | [diff] [blame] | 535 | struct file *src = nf_src->nf_file; |
| 536 | struct file *dst = nf_dst->nf_file; |
Trond Myklebust | 555dbf1 | 2021-12-18 20:38:01 -0500 | [diff] [blame] | 537 | errseq_t since; |
Darrick J. Wong | 42ec3d4 | 2018-10-30 10:41:49 +1100 | [diff] [blame] | 538 | loff_t cloned; |
Trond Myklebust | 1b28d75 | 2020-01-06 13:40:33 -0500 | [diff] [blame] | 539 | __be32 ret = 0; |
Darrick J. Wong | 42ec3d4 | 2018-10-30 10:41:49 +1100 | [diff] [blame] | 540 | |
Trond Myklebust | 555dbf1 | 2021-12-18 20:38:01 -0500 | [diff] [blame] | 541 | since = READ_ONCE(dst->f_wb_err); |
Darrick J. Wong | 452ce65 | 2018-10-30 10:41:56 +1100 | [diff] [blame] | 542 | cloned = vfs_clone_file_range(src, src_pos, dst, dst_pos, count, 0); |
Trond Myklebust | 1b28d75 | 2020-01-06 13:40:33 -0500 | [diff] [blame] | 543 | if (cloned < 0) { |
| 544 | ret = nfserrno(cloned); |
| 545 | goto out_err; |
| 546 | } |
| 547 | if (count && cloned != count) { |
| 548 | ret = nfserrno(-EINVAL); |
| 549 | goto out_err; |
| 550 | } |
Trond Myklebust | a25e3726 | 2019-11-27 17:05:51 -0500 | [diff] [blame] | 551 | if (sync) { |
| 552 | loff_t dst_end = count ? dst_pos + count - 1 : LLONG_MAX; |
| 553 | int status = vfs_fsync_range(dst, dst_pos, dst_end, 0); |
Trond Myklebust | 57f6403 | 2019-12-18 14:57:23 -0500 | [diff] [blame] | 554 | |
| 555 | if (!status) |
Trond Myklebust | 555dbf1 | 2021-12-18 20:38:01 -0500 | [diff] [blame] | 556 | status = filemap_check_wb_err(dst->f_mapping, since); |
| 557 | if (!status) |
Trond Myklebust | 57f6403 | 2019-12-18 14:57:23 -0500 | [diff] [blame] | 558 | status = commit_inode_metadata(file_inode(src)); |
Trond Myklebust | 1b28d75 | 2020-01-06 13:40:33 -0500 | [diff] [blame] | 559 | if (status < 0) { |
Chuck Lever | 75acacb | 2021-12-28 14:27:56 -0500 | [diff] [blame] | 560 | struct nfsd_net *nn = net_generic(nf_dst->nf_net, |
| 561 | nfsd_net_id); |
| 562 | |
Trond Myklebust | a2f4c3f | 2021-12-18 20:38:00 -0500 | [diff] [blame] | 563 | trace_nfsd_clone_file_range_err(rqstp, |
| 564 | &nfsd4_get_cstate(rqstp)->save_fh, |
| 565 | src_pos, |
| 566 | &nfsd4_get_cstate(rqstp)->current_fh, |
| 567 | dst_pos, |
| 568 | count, status); |
Chuck Lever | 75acacb | 2021-12-28 14:27:56 -0500 | [diff] [blame] | 569 | nfsd_reset_write_verifier(nn); |
| 570 | trace_nfsd_writeverf_reset(nn, rqstp, status); |
Trond Myklebust | 1b28d75 | 2020-01-06 13:40:33 -0500 | [diff] [blame] | 571 | ret = nfserrno(status); |
| 572 | } |
Trond Myklebust | a25e3726 | 2019-11-27 17:05:51 -0500 | [diff] [blame] | 573 | } |
Trond Myklebust | 1b28d75 | 2020-01-06 13:40:33 -0500 | [diff] [blame] | 574 | out_err: |
Trond Myklebust | 1b28d75 | 2020-01-06 13:40:33 -0500 | [diff] [blame] | 575 | return ret; |
Christoph Hellwig | ffa0160 | 2015-12-03 12:59:52 +0100 | [diff] [blame] | 576 | } |
| 577 | |
Anna Schumaker | 29ae7f9 | 2016-09-07 15:57:30 -0400 | [diff] [blame] | 578 | ssize_t nfsd_copy_file_range(struct file *src, u64 src_pos, struct file *dst, |
| 579 | u64 dst_pos, u64 count) |
| 580 | { |
| 581 | |
| 582 | /* |
| 583 | * Limit copy to 4MB to prevent indefinitely blocking an nfsd |
| 584 | * thread and client rpc slot. The choice of 4MB is somewhat |
| 585 | * arbitrary. We might instead base this on r/wsize, or make it |
| 586 | * tunable, or use a time instead of a byte limit, or implement |
| 587 | * asynchronous copy. In theory a client could also recognize a |
| 588 | * limit like this and pipeline multiple COPY requests. |
| 589 | */ |
| 590 | count = min_t(u64, count, 1 << 22); |
| 591 | return vfs_copy_file_range(src, src_pos, dst, dst_pos, count, 0); |
| 592 | } |
| 593 | |
Anna Schumaker | 95d871f | 2014-11-07 14:44:26 -0500 | [diff] [blame] | 594 | __be32 nfsd4_vfs_fallocate(struct svc_rqst *rqstp, struct svc_fh *fhp, |
| 595 | struct file *file, loff_t offset, loff_t len, |
| 596 | int flags) |
| 597 | { |
Anna Schumaker | 95d871f | 2014-11-07 14:44:26 -0500 | [diff] [blame] | 598 | int error; |
| 599 | |
| 600 | if (!S_ISREG(file_inode(file)->i_mode)) |
| 601 | return nfserr_inval; |
| 602 | |
Anna Schumaker | 95d871f | 2014-11-07 14:44:26 -0500 | [diff] [blame] | 603 | error = vfs_fallocate(file, flags, offset, len); |
| 604 | if (!error) |
| 605 | error = commit_metadata(fhp); |
| 606 | |
| 607 | return nfserrno(error); |
| 608 | } |
J. Bruce Fields | 6a85d6c | 2010-07-06 12:39:12 -0400 | [diff] [blame] | 609 | #endif /* defined(CONFIG_NFSD_V4) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | |
| 611 | #ifdef CONFIG_NFSD_V3 |
| 612 | /* |
| 613 | * Check server access rights to a file system object |
| 614 | */ |
| 615 | struct accessmap { |
| 616 | u32 access; |
| 617 | int how; |
| 618 | }; |
| 619 | static struct accessmap nfs3_regaccess[] = { |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 620 | { NFS3_ACCESS_READ, NFSD_MAY_READ }, |
| 621 | { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC }, |
| 622 | { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_TRUNC }, |
| 623 | { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | |
Frank van der Linden | c11d7fd | 2020-06-23 22:39:24 +0000 | [diff] [blame] | 625 | #ifdef CONFIG_NFSD_V4 |
| 626 | { NFS4_ACCESS_XAREAD, NFSD_MAY_READ }, |
| 627 | { NFS4_ACCESS_XAWRITE, NFSD_MAY_WRITE }, |
| 628 | { NFS4_ACCESS_XALIST, NFSD_MAY_READ }, |
| 629 | #endif |
| 630 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | { 0, 0 } |
| 632 | }; |
| 633 | |
| 634 | static struct accessmap nfs3_diraccess[] = { |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 635 | { NFS3_ACCESS_READ, NFSD_MAY_READ }, |
| 636 | { NFS3_ACCESS_LOOKUP, NFSD_MAY_EXEC }, |
| 637 | { NFS3_ACCESS_MODIFY, NFSD_MAY_EXEC|NFSD_MAY_WRITE|NFSD_MAY_TRUNC}, |
| 638 | { NFS3_ACCESS_EXTEND, NFSD_MAY_EXEC|NFSD_MAY_WRITE }, |
| 639 | { NFS3_ACCESS_DELETE, NFSD_MAY_REMOVE }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | |
Frank van der Linden | c11d7fd | 2020-06-23 22:39:24 +0000 | [diff] [blame] | 641 | #ifdef CONFIG_NFSD_V4 |
| 642 | { NFS4_ACCESS_XAREAD, NFSD_MAY_READ }, |
| 643 | { NFS4_ACCESS_XAWRITE, NFSD_MAY_WRITE }, |
| 644 | { NFS4_ACCESS_XALIST, NFSD_MAY_READ }, |
| 645 | #endif |
| 646 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | { 0, 0 } |
| 648 | }; |
| 649 | |
| 650 | static struct accessmap nfs3_anyaccess[] = { |
| 651 | /* Some clients - Solaris 2.6 at least, make an access call |
| 652 | * to the server to check for access for things like /dev/null |
| 653 | * (which really, the server doesn't care about). So |
| 654 | * We provide simple access checking for them, looking |
| 655 | * mainly at mode bits, and we make sure to ignore read-only |
| 656 | * filesystem checks |
| 657 | */ |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 658 | { NFS3_ACCESS_READ, NFSD_MAY_READ }, |
| 659 | { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC }, |
| 660 | { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS }, |
| 661 | { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | |
| 663 | { 0, 0 } |
| 664 | }; |
| 665 | |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 666 | __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access, u32 *supported) |
| 668 | { |
| 669 | struct accessmap *map; |
| 670 | struct svc_export *export; |
| 671 | struct dentry *dentry; |
| 672 | u32 query, result = 0, sresult = 0; |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 673 | __be32 error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 675 | error = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | if (error) |
| 677 | goto out; |
| 678 | |
| 679 | export = fhp->fh_export; |
| 680 | dentry = fhp->fh_dentry; |
| 681 | |
David Howells | e36cb0b | 2015-01-29 12:02:35 +0000 | [diff] [blame] | 682 | if (d_is_reg(dentry)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | map = nfs3_regaccess; |
David Howells | e36cb0b | 2015-01-29 12:02:35 +0000 | [diff] [blame] | 684 | else if (d_is_dir(dentry)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | map = nfs3_diraccess; |
| 686 | else |
| 687 | map = nfs3_anyaccess; |
| 688 | |
| 689 | |
| 690 | query = *access; |
| 691 | for (; map->access; map++) { |
| 692 | if (map->access & query) { |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 693 | __be32 err2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | |
| 695 | sresult |= map->access; |
| 696 | |
J. Bruce Fields | 0ec757d | 2007-07-17 04:04:48 -0700 | [diff] [blame] | 697 | err2 = nfsd_permission(rqstp, export, dentry, map->how); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | switch (err2) { |
| 699 | case nfs_ok: |
| 700 | result |= map->access; |
| 701 | break; |
| 702 | |
| 703 | /* the following error codes just mean the access was not allowed, |
| 704 | * rather than an error occurred */ |
| 705 | case nfserr_rofs: |
| 706 | case nfserr_acces: |
| 707 | case nfserr_perm: |
| 708 | /* simply don't "or" in the access bit. */ |
| 709 | break; |
| 710 | default: |
| 711 | error = err2; |
| 712 | goto out; |
| 713 | } |
| 714 | } |
| 715 | } |
| 716 | *access = result; |
| 717 | if (supported) |
| 718 | *supported = sresult; |
| 719 | |
| 720 | out: |
| 721 | return error; |
| 722 | } |
| 723 | #endif /* CONFIG_NFSD_V3 */ |
| 724 | |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 725 | int nfsd_open_break_lease(struct inode *inode, int access) |
J. Bruce Fields | 105f462 | 2011-06-07 11:50:23 -0400 | [diff] [blame] | 726 | { |
| 727 | unsigned int mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | |
J. Bruce Fields | 105f462 | 2011-06-07 11:50:23 -0400 | [diff] [blame] | 729 | if (access & NFSD_MAY_NOT_BREAK_LEASE) |
| 730 | return 0; |
| 731 | mode = (access & NFSD_MAY_WRITE) ? O_WRONLY : O_RDONLY; |
| 732 | return break_lease(inode, mode | O_NONBLOCK); |
| 733 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | |
| 735 | /* |
| 736 | * Open an existing file or directory. |
Bernd Schubert | 999448a | 2012-03-18 22:44:49 -0400 | [diff] [blame] | 737 | * The may_flags argument indicates the type of open (read/write/lock) |
| 738 | * and additional flags. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | * N.B. After this call fhp needs an fh_put |
| 740 | */ |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 741 | static __be32 |
| 742 | __nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type, |
Bernd Schubert | 999448a | 2012-03-18 22:44:49 -0400 | [diff] [blame] | 743 | int may_flags, struct file **filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | { |
Al Viro | 765927b | 2012-06-26 21:58:53 +0400 | [diff] [blame] | 745 | struct path path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | struct inode *inode; |
Kinglong Mee | 8519f99 | 2014-09-03 08:14:06 +0800 | [diff] [blame] | 747 | struct file *file; |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 748 | int flags = O_RDONLY|O_LARGEFILE; |
| 749 | __be32 err; |
Jeff Layton | 9188525 | 2010-03-19 08:06:28 -0400 | [diff] [blame] | 750 | int host_err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | |
Al Viro | 765927b | 2012-06-26 21:58:53 +0400 | [diff] [blame] | 752 | path.mnt = fhp->fh_export->ex_path.mnt; |
| 753 | path.dentry = fhp->fh_dentry; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 754 | inode = d_inode(path.dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | err = nfserr_perm; |
Bernd Schubert | 999448a | 2012-03-18 22:44:49 -0400 | [diff] [blame] | 757 | if (IS_APPEND(inode) && (may_flags & NFSD_MAY_WRITE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | |
| 760 | if (!inode->i_fop) |
| 761 | goto out; |
| 762 | |
Bernd Schubert | 999448a | 2012-03-18 22:44:49 -0400 | [diff] [blame] | 763 | host_err = nfsd_open_break_lease(inode, may_flags); |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 764 | if (host_err) /* NOMEM or WOULDBLOCK */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | goto out_nfserr; |
| 766 | |
Bernd Schubert | 999448a | 2012-03-18 22:44:49 -0400 | [diff] [blame] | 767 | if (may_flags & NFSD_MAY_WRITE) { |
| 768 | if (may_flags & NFSD_MAY_READ) |
J. Bruce Fields | 9ecb6a0 | 2006-06-30 01:56:17 -0700 | [diff] [blame] | 769 | flags = O_RDWR|O_LARGEFILE; |
| 770 | else |
| 771 | flags = O_WRONLY|O_LARGEFILE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | } |
Bernd Schubert | 999448a | 2012-03-18 22:44:49 -0400 | [diff] [blame] | 773 | |
Kinglong Mee | 8519f99 | 2014-09-03 08:14:06 +0800 | [diff] [blame] | 774 | file = dentry_open(&path, flags, current_cred()); |
| 775 | if (IS_ERR(file)) { |
| 776 | host_err = PTR_ERR(file); |
| 777 | goto out_nfserr; |
Bernd Schubert | 06effdb | 2012-03-18 22:44:50 -0400 | [diff] [blame] | 778 | } |
| 779 | |
Al Viro | 6035a27 | 2018-06-08 13:40:10 -0400 | [diff] [blame] | 780 | host_err = ima_file_check(file, may_flags); |
Kinglong Mee | 8519f99 | 2014-09-03 08:14:06 +0800 | [diff] [blame] | 781 | if (host_err) { |
Christoph Hellwig | fd89145 | 2015-04-28 15:41:16 +0200 | [diff] [blame] | 782 | fput(file); |
Kinglong Mee | 8519f99 | 2014-09-03 08:14:06 +0800 | [diff] [blame] | 783 | goto out_nfserr; |
| 784 | } |
| 785 | |
| 786 | if (may_flags & NFSD_MAY_64BIT_COOKIE) |
| 787 | file->f_mode |= FMODE_64BITHASH; |
| 788 | else |
| 789 | file->f_mode |= FMODE_32BITHASH; |
| 790 | |
| 791 | *filp = file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | out_nfserr: |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 793 | err = nfserrno(host_err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | out: |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 795 | return err; |
| 796 | } |
| 797 | |
| 798 | __be32 |
| 799 | nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type, |
| 800 | int may_flags, struct file **filp) |
| 801 | { |
| 802 | __be32 err; |
Jeff Layton | 12bcbd4 | 2021-12-18 20:37:56 -0500 | [diff] [blame] | 803 | bool retried = false; |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 804 | |
| 805 | validate_process_creds(); |
| 806 | /* |
| 807 | * If we get here, then the client has already done an "open", |
| 808 | * and (hopefully) checked permission - so allow OWNER_OVERRIDE |
| 809 | * in case a chmod has now revoked permission. |
| 810 | * |
| 811 | * Arguably we should also allow the owner override for |
| 812 | * directories, but we never have and it doesn't seem to have |
| 813 | * caused anyone a problem. If we were to change this, note |
| 814 | * also that our filldir callbacks would need a variant of |
| 815 | * lookup_one_len that doesn't check permissions. |
| 816 | */ |
| 817 | if (type == S_IFREG) |
| 818 | may_flags |= NFSD_MAY_OWNER_OVERRIDE; |
Jeff Layton | 12bcbd4 | 2021-12-18 20:37:56 -0500 | [diff] [blame] | 819 | retry: |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 820 | err = fh_verify(rqstp, fhp, type, may_flags); |
Jeff Layton | 12bcbd4 | 2021-12-18 20:37:56 -0500 | [diff] [blame] | 821 | if (!err) { |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 822 | err = __nfsd_open(rqstp, fhp, type, may_flags, filp); |
Jeff Layton | 12bcbd4 | 2021-12-18 20:37:56 -0500 | [diff] [blame] | 823 | if (err == nfserr_stale && !retried) { |
| 824 | retried = true; |
| 825 | fh_put(fhp); |
| 826 | goto retry; |
| 827 | } |
| 828 | } |
David Howells | e0e8173 | 2009-09-02 09:13:40 +0100 | [diff] [blame] | 829 | validate_process_creds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | return err; |
| 831 | } |
| 832 | |
Jeff Layton | 65294c1 | 2019-08-18 14:18:48 -0400 | [diff] [blame] | 833 | __be32 |
| 834 | nfsd_open_verified(struct svc_rqst *rqstp, struct svc_fh *fhp, umode_t type, |
| 835 | int may_flags, struct file **filp) |
| 836 | { |
| 837 | __be32 err; |
| 838 | |
| 839 | validate_process_creds(); |
| 840 | err = __nfsd_open(rqstp, fhp, type, may_flags, filp); |
| 841 | validate_process_creds(); |
| 842 | return err; |
| 843 | } |
| 844 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | /* |
Jens Axboe | cf8208d | 2007-06-12 21:22:14 +0200 | [diff] [blame] | 846 | * Grab and keep cached pages associated with a file in the svc_rqst |
| 847 | * so that they can be passed to the network sendmsg/sendpage routines |
| 848 | * directly. They will be released after the sending has completed. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | */ |
| 850 | static int |
Jens Axboe | cf8208d | 2007-06-12 21:22:14 +0200 | [diff] [blame] | 851 | nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf, |
| 852 | struct splice_desc *sd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | { |
Jens Axboe | cf8208d | 2007-06-12 21:22:14 +0200 | [diff] [blame] | 854 | struct svc_rqst *rqstp = sd->u.data; |
J. Bruce Fields | afc5940 | 2012-12-10 18:01:37 -0500 | [diff] [blame] | 855 | struct page **pp = rqstp->rq_next_page; |
Jens Axboe | cf8208d | 2007-06-12 21:22:14 +0200 | [diff] [blame] | 856 | struct page *page = buf->page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | |
| 858 | if (rqstp->rq_res.page_len == 0) { |
Chuck Lever | 496d83cf | 2021-06-28 17:24:27 -0400 | [diff] [blame] | 859 | svc_rqst_replace_page(rqstp, page); |
Jens Axboe | cf8208d | 2007-06-12 21:22:14 +0200 | [diff] [blame] | 860 | rqstp->rq_res.page_base = buf->offset; |
NeilBrown | 4452435 | 2006-10-04 02:15:46 -0700 | [diff] [blame] | 861 | } else if (page != pp[-1]) { |
Chuck Lever | 496d83cf | 2021-06-28 17:24:27 -0400 | [diff] [blame] | 862 | svc_rqst_replace_page(rqstp, page); |
Chuck Lever | c7e0b78 | 2021-06-28 16:34:20 -0400 | [diff] [blame] | 863 | } |
| 864 | rqstp->rq_res.page_len += sd->len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | |
Chuck Lever | c7e0b78 | 2021-06-28 16:34:20 -0400 | [diff] [blame] | 866 | return sd->len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | } |
| 868 | |
Jens Axboe | cf8208d | 2007-06-12 21:22:14 +0200 | [diff] [blame] | 869 | static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe, |
| 870 | struct splice_desc *sd) |
| 871 | { |
| 872 | return __splice_from_pipe(pipe, sd, nfsd_splice_actor); |
| 873 | } |
| 874 | |
Trond Myklebust | 83a6307 | 2019-08-26 13:03:11 -0400 | [diff] [blame] | 875 | static u32 nfsd_eof_on_read(struct file *file, loff_t offset, ssize_t len, |
| 876 | size_t expected) |
| 877 | { |
| 878 | if (expected != 0 && len == 0) |
| 879 | return 1; |
| 880 | if (offset+len >= i_size_read(file_inode(file))) |
| 881 | return 1; |
| 882 | return 0; |
| 883 | } |
| 884 | |
Chuck Lever | 87c5942 | 2018-03-28 13:29:11 -0400 | [diff] [blame] | 885 | static __be32 nfsd_finish_read(struct svc_rqst *rqstp, struct svc_fh *fhp, |
| 886 | struct file *file, loff_t offset, |
Trond Myklebust | 83a6307 | 2019-08-26 13:03:11 -0400 | [diff] [blame] | 887 | unsigned long *count, u32 *eof, ssize_t host_err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | { |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 889 | if (host_err >= 0) { |
Amir Goldstein | 20ad856 | 2021-01-06 09:52:36 +0200 | [diff] [blame] | 890 | nfsd_stats_io_read_add(fhp->fh_export, host_err); |
Trond Myklebust | 83a6307 | 2019-08-26 13:03:11 -0400 | [diff] [blame] | 891 | *eof = nfsd_eof_on_read(file, offset, host_err, *count); |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 892 | *count = host_err; |
Eric Paris | 2a12a9d | 2009-12-17 21:24:21 -0500 | [diff] [blame] | 893 | fsnotify_access(file); |
Chuck Lever | 87c5942 | 2018-03-28 13:29:11 -0400 | [diff] [blame] | 894 | trace_nfsd_read_io_done(rqstp, fhp, offset, *count); |
J. Bruce Fields | dc97618 | 2014-03-18 17:01:51 -0400 | [diff] [blame] | 895 | return 0; |
Chuck Lever | 87c5942 | 2018-03-28 13:29:11 -0400 | [diff] [blame] | 896 | } else { |
| 897 | trace_nfsd_read_err(rqstp, fhp, offset, host_err); |
J. Bruce Fields | dc97618 | 2014-03-18 17:01:51 -0400 | [diff] [blame] | 898 | return nfserrno(host_err); |
Chuck Lever | 87c5942 | 2018-03-28 13:29:11 -0400 | [diff] [blame] | 899 | } |
J. Bruce Fields | dc97618 | 2014-03-18 17:01:51 -0400 | [diff] [blame] | 900 | } |
| 901 | |
Chuck Lever | 87c5942 | 2018-03-28 13:29:11 -0400 | [diff] [blame] | 902 | __be32 nfsd_splice_read(struct svc_rqst *rqstp, struct svc_fh *fhp, |
Trond Myklebust | 83a6307 | 2019-08-26 13:03:11 -0400 | [diff] [blame] | 903 | struct file *file, loff_t offset, unsigned long *count, |
| 904 | u32 *eof) |
J. Bruce Fields | dc97618 | 2014-03-18 17:01:51 -0400 | [diff] [blame] | 905 | { |
| 906 | struct splice_desc sd = { |
| 907 | .len = 0, |
| 908 | .total_len = *count, |
| 909 | .pos = offset, |
| 910 | .u.data = rqstp, |
| 911 | }; |
Trond Myklebust | 83a6307 | 2019-08-26 13:03:11 -0400 | [diff] [blame] | 912 | ssize_t host_err; |
J. Bruce Fields | dc97618 | 2014-03-18 17:01:51 -0400 | [diff] [blame] | 913 | |
Chuck Lever | 87c5942 | 2018-03-28 13:29:11 -0400 | [diff] [blame] | 914 | trace_nfsd_read_splice(rqstp, fhp, offset, *count); |
J. Bruce Fields | dc97618 | 2014-03-18 17:01:51 -0400 | [diff] [blame] | 915 | rqstp->rq_next_page = rqstp->rq_respages + 1; |
| 916 | host_err = splice_direct_to_actor(file, &sd, nfsd_direct_splice_actor); |
Trond Myklebust | 83a6307 | 2019-08-26 13:03:11 -0400 | [diff] [blame] | 917 | return nfsd_finish_read(rqstp, fhp, file, offset, count, eof, host_err); |
J. Bruce Fields | dc97618 | 2014-03-18 17:01:51 -0400 | [diff] [blame] | 918 | } |
| 919 | |
Chuck Lever | 87c5942 | 2018-03-28 13:29:11 -0400 | [diff] [blame] | 920 | __be32 nfsd_readv(struct svc_rqst *rqstp, struct svc_fh *fhp, |
| 921 | struct file *file, loff_t offset, |
Trond Myklebust | 83a6307 | 2019-08-26 13:03:11 -0400 | [diff] [blame] | 922 | struct kvec *vec, int vlen, unsigned long *count, |
| 923 | u32 *eof) |
J. Bruce Fields | dc97618 | 2014-03-18 17:01:51 -0400 | [diff] [blame] | 924 | { |
Christoph Hellwig | 73da852 | 2017-05-27 11:16:53 +0300 | [diff] [blame] | 925 | struct iov_iter iter; |
Trond Myklebust | 83a6307 | 2019-08-26 13:03:11 -0400 | [diff] [blame] | 926 | loff_t ppos = offset; |
| 927 | ssize_t host_err; |
J. Bruce Fields | dc97618 | 2014-03-18 17:01:51 -0400 | [diff] [blame] | 928 | |
Chuck Lever | 87c5942 | 2018-03-28 13:29:11 -0400 | [diff] [blame] | 929 | trace_nfsd_read_vector(rqstp, fhp, offset, *count); |
David Howells | aa563d7 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 930 | iov_iter_kvec(&iter, READ, vec, vlen, *count); |
Trond Myklebust | 83a6307 | 2019-08-26 13:03:11 -0400 | [diff] [blame] | 931 | host_err = vfs_iter_read(file, &iter, &ppos, 0); |
| 932 | return nfsd_finish_read(rqstp, fhp, file, offset, count, eof, host_err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | } |
| 934 | |
J. Bruce Fields | d911df7 | 2009-06-15 16:03:53 -0700 | [diff] [blame] | 935 | /* |
| 936 | * Gathered writes: If another process is currently writing to the file, |
| 937 | * there's a high chance this is another nfsd (triggered by a bulk write |
| 938 | * from a client's biod). Rather than syncing the file with each write |
| 939 | * request, we sleep for 10 msec. |
| 940 | * |
| 941 | * I don't know if this roughly approximates C. Juszak's idea of |
| 942 | * gathered writes, but it's a nice and simple solution (IMHO), and it |
| 943 | * seems to work:-) |
| 944 | * |
| 945 | * Note: we do this only in the NFSv2 case, since v3 and higher have a |
| 946 | * better tool (separate unstable writes and commits) for solving this |
| 947 | * problem. |
| 948 | */ |
| 949 | static int wait_for_concurrent_writes(struct file *file) |
| 950 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 951 | struct inode *inode = file_inode(file); |
J. Bruce Fields | d911df7 | 2009-06-15 16:03:53 -0700 | [diff] [blame] | 952 | static ino_t last_ino; |
| 953 | static dev_t last_dev; |
| 954 | int err = 0; |
| 955 | |
| 956 | if (atomic_read(&inode->i_writecount) > 1 |
| 957 | || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) { |
| 958 | dprintk("nfsd: write defer %d\n", task_pid_nr(current)); |
| 959 | msleep(10); |
| 960 | dprintk("nfsd: write resume %d\n", task_pid_nr(current)); |
| 961 | } |
| 962 | |
| 963 | if (inode->i_state & I_DIRTY) { |
| 964 | dprintk("nfsd: write sync %d\n", task_pid_nr(current)); |
Christoph Hellwig | 8018ab0 | 2010-03-22 17:32:25 +0100 | [diff] [blame] | 965 | err = vfs_fsync(file, 0); |
J. Bruce Fields | d911df7 | 2009-06-15 16:03:53 -0700 | [diff] [blame] | 966 | } |
| 967 | last_ino = inode->i_ino; |
| 968 | last_dev = inode->i_sb->s_dev; |
| 969 | return err; |
| 970 | } |
| 971 | |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 972 | __be32 |
Trond Myklebust | 16f8f89 | 2020-01-06 13:40:29 -0500 | [diff] [blame] | 973 | nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfsd_file *nf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | loff_t offset, struct kvec *vec, int vlen, |
Trond Myklebust | 19e0663 | 2020-01-06 13:40:37 -0500 | [diff] [blame] | 975 | unsigned long *cnt, int stable, |
| 976 | __be32 *verf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | { |
Chuck Lever | fb7622c | 2021-12-28 12:41:32 -0500 | [diff] [blame] | 978 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Trond Myklebust | 16f8f89 | 2020-01-06 13:40:29 -0500 | [diff] [blame] | 979 | struct file *file = nf->nf_file; |
Trond Myklebust | 01cbf38 | 2020-11-30 17:03:19 -0500 | [diff] [blame] | 980 | struct super_block *sb = file_inode(file)->i_sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | struct svc_export *exp; |
Christoph Hellwig | 73da852 | 2017-05-27 11:16:53 +0300 | [diff] [blame] | 982 | struct iov_iter iter; |
Trond Myklebust | 555dbf1 | 2021-12-18 20:38:01 -0500 | [diff] [blame] | 983 | errseq_t since; |
Chuck Lever | d890be1 | 2018-03-27 10:53:27 -0400 | [diff] [blame] | 984 | __be32 nfserr; |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 985 | int host_err; |
Trond Myklebust | 48e03bc | 2009-06-05 12:35:15 -0400 | [diff] [blame] | 986 | int use_wgather; |
Kent Overstreet | e49dbbf | 2013-03-22 11:18:24 -0700 | [diff] [blame] | 987 | loff_t pos = offset; |
Trond Myklebust | 01cbf38 | 2020-11-30 17:03:19 -0500 | [diff] [blame] | 988 | unsigned long exp_op_flags = 0; |
NeilBrown | 8658452 | 2014-05-12 11:22:47 +1000 | [diff] [blame] | 989 | unsigned int pflags = current->flags; |
Christoph Hellwig | ddef7ed2 | 2017-07-06 18:58:37 +0200 | [diff] [blame] | 990 | rwf_t flags = 0; |
Trond Myklebust | 01cbf38 | 2020-11-30 17:03:19 -0500 | [diff] [blame] | 991 | bool restore_flags = false; |
NeilBrown | 8658452 | 2014-05-12 11:22:47 +1000 | [diff] [blame] | 992 | |
Chuck Lever | d890be1 | 2018-03-27 10:53:27 -0400 | [diff] [blame] | 993 | trace_nfsd_write_opened(rqstp, fhp, offset, *cnt); |
| 994 | |
Trond Myklebust | 01cbf38 | 2020-11-30 17:03:19 -0500 | [diff] [blame] | 995 | if (sb->s_export_op) |
| 996 | exp_op_flags = sb->s_export_op->flags; |
| 997 | |
| 998 | if (test_bit(RQ_LOCAL, &rqstp->rq_flags) && |
| 999 | !(exp_op_flags & EXPORT_OP_REMOTE_FS)) { |
NeilBrown | 8658452 | 2014-05-12 11:22:47 +1000 | [diff] [blame] | 1000 | /* |
NeilBrown | a37b071 | 2020-06-01 21:48:18 -0700 | [diff] [blame] | 1001 | * We want throttling in balance_dirty_pages() |
| 1002 | * and shrink_inactive_list() to only consider |
| 1003 | * the backingdev we are writing to, so that nfs to |
NeilBrown | 8658452 | 2014-05-12 11:22:47 +1000 | [diff] [blame] | 1004 | * localhost doesn't cause nfsd to lock up due to all |
| 1005 | * the client's dirty pages or its congested queue. |
| 1006 | */ |
NeilBrown | a37b071 | 2020-06-01 21:48:18 -0700 | [diff] [blame] | 1007 | current->flags |= PF_LOCAL_THROTTLE; |
Trond Myklebust | 01cbf38 | 2020-11-30 17:03:19 -0500 | [diff] [blame] | 1008 | restore_flags = true; |
| 1009 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | |
Kinglong Mee | 865d50b | 2016-12-31 21:00:21 +0800 | [diff] [blame] | 1011 | exp = fhp->fh_export; |
Trond Myklebust | 48e03bc | 2009-06-05 12:35:15 -0400 | [diff] [blame] | 1012 | use_wgather = (rqstp->rq_vers == 2) && EX_WGATHER(exp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1014 | if (!EX_ISSYNC(exp)) |
Kinglong Mee | 54bbb7d | 2016-12-31 20:59:53 +0800 | [diff] [blame] | 1015 | stable = NFS_UNSTABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1016 | |
Christoph Hellwig | 24368aa | 2016-04-07 08:52:04 -0700 | [diff] [blame] | 1017 | if (stable && !use_wgather) |
| 1018 | flags |= RWF_SYNC; |
| 1019 | |
David Howells | aa563d7 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 1020 | iov_iter_kvec(&iter, WRITE, vec, vlen, *cnt); |
Trond Myklebust | 555dbf1 | 2021-12-18 20:38:01 -0500 | [diff] [blame] | 1021 | since = READ_ONCE(file->f_wb_err); |
Chuck Lever | 33388b3 | 2021-12-28 14:19:41 -0500 | [diff] [blame] | 1022 | if (verf) |
Chuck Lever | 3988a57 | 2021-12-30 10:22:05 -0500 | [diff] [blame] | 1023 | nfsd_copy_write_verifier(verf, nn); |
Chuck Lever | 33388b3 | 2021-12-28 14:19:41 -0500 | [diff] [blame] | 1024 | host_err = vfs_iter_write(file, &iter, &pos, flags); |
Trond Myklebust | 7bf94c6 | 2020-01-06 13:40:31 -0500 | [diff] [blame] | 1025 | if (host_err < 0) { |
Chuck Lever | 3988a57 | 2021-12-30 10:22:05 -0500 | [diff] [blame] | 1026 | nfsd_reset_write_verifier(nn); |
Chuck Lever | 75acacb | 2021-12-28 14:27:56 -0500 | [diff] [blame] | 1027 | trace_nfsd_writeverf_reset(nn, rqstp, host_err); |
J. Bruce Fields | e4636d5 | 2009-06-15 19:07:13 -0700 | [diff] [blame] | 1028 | goto out_nfserr; |
Trond Myklebust | 7bf94c6 | 2020-01-06 13:40:31 -0500 | [diff] [blame] | 1029 | } |
Trond Myklebust | 09a80f2 | 2019-12-17 12:33:33 -0500 | [diff] [blame] | 1030 | *cnt = host_err; |
Amir Goldstein | 20ad856 | 2021-01-06 09:52:36 +0200 | [diff] [blame] | 1031 | nfsd_stats_io_write_add(exp, *cnt); |
Eric Paris | 2a12a9d | 2009-12-17 21:24:21 -0500 | [diff] [blame] | 1032 | fsnotify_modify(file); |
Trond Myklebust | 555dbf1 | 2021-12-18 20:38:01 -0500 | [diff] [blame] | 1033 | host_err = filemap_check_wb_err(file->f_mapping, since); |
| 1034 | if (host_err < 0) |
| 1035 | goto out_nfserr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | |
Trond Myklebust | bbf2f09 | 2019-09-02 13:02:58 -0400 | [diff] [blame] | 1037 | if (stable && use_wgather) { |
Christoph Hellwig | 24368aa | 2016-04-07 08:52:04 -0700 | [diff] [blame] | 1038 | host_err = wait_for_concurrent_writes(file); |
Chuck Lever | 75acacb | 2021-12-28 14:27:56 -0500 | [diff] [blame] | 1039 | if (host_err < 0) { |
Chuck Lever | 3988a57 | 2021-12-30 10:22:05 -0500 | [diff] [blame] | 1040 | nfsd_reset_write_verifier(nn); |
Chuck Lever | 75acacb | 2021-12-28 14:27:56 -0500 | [diff] [blame] | 1041 | trace_nfsd_writeverf_reset(nn, rqstp, host_err); |
| 1042 | } |
Trond Myklebust | bbf2f09 | 2019-09-02 13:02:58 -0400 | [diff] [blame] | 1043 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | |
J. Bruce Fields | e4636d5 | 2009-06-15 19:07:13 -0700 | [diff] [blame] | 1045 | out_nfserr: |
Chuck Lever | d890be1 | 2018-03-27 10:53:27 -0400 | [diff] [blame] | 1046 | if (host_err >= 0) { |
| 1047 | trace_nfsd_write_io_done(rqstp, fhp, offset, *cnt); |
| 1048 | nfserr = nfs_ok; |
| 1049 | } else { |
| 1050 | trace_nfsd_write_err(rqstp, fhp, offset, host_err); |
| 1051 | nfserr = nfserrno(host_err); |
| 1052 | } |
Trond Myklebust | 01cbf38 | 2020-11-30 17:03:19 -0500 | [diff] [blame] | 1053 | if (restore_flags) |
NeilBrown | a37b071 | 2020-06-01 21:48:18 -0700 | [diff] [blame] | 1054 | current_restore_flags(pflags, PF_LOCAL_THROTTLE); |
Chuck Lever | d890be1 | 2018-03-27 10:53:27 -0400 | [diff] [blame] | 1055 | return nfserr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | } |
| 1057 | |
J. Bruce Fields | dc97618 | 2014-03-18 17:01:51 -0400 | [diff] [blame] | 1058 | /* |
| 1059 | * Read data from a file. count must contain the requested read count |
| 1060 | * on entry. On return, *count contains the number of bytes actually read. |
| 1061 | * N.B. After this call fhp needs an fh_put |
| 1062 | */ |
| 1063 | __be32 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp, |
Trond Myklebust | 83a6307 | 2019-08-26 13:03:11 -0400 | [diff] [blame] | 1064 | loff_t offset, struct kvec *vec, int vlen, unsigned long *count, |
| 1065 | u32 *eof) |
J. Bruce Fields | dc97618 | 2014-03-18 17:01:51 -0400 | [diff] [blame] | 1066 | { |
Jeff Layton | 48cd7b5 | 2019-08-18 14:18:50 -0400 | [diff] [blame] | 1067 | struct nfsd_file *nf; |
J. Bruce Fields | dc97618 | 2014-03-18 17:01:51 -0400 | [diff] [blame] | 1068 | struct file *file; |
J. Bruce Fields | dc97618 | 2014-03-18 17:01:51 -0400 | [diff] [blame] | 1069 | __be32 err; |
| 1070 | |
Chuck Lever | f394b62 | 2018-03-27 10:53:11 -0400 | [diff] [blame] | 1071 | trace_nfsd_read_start(rqstp, fhp, offset, *count); |
Jeff Layton | 48cd7b5 | 2019-08-18 14:18:50 -0400 | [diff] [blame] | 1072 | err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf); |
J. Bruce Fields | dc97618 | 2014-03-18 17:01:51 -0400 | [diff] [blame] | 1073 | if (err) |
| 1074 | return err; |
| 1075 | |
Jeff Layton | 48cd7b5 | 2019-08-18 14:18:50 -0400 | [diff] [blame] | 1076 | file = nf->nf_file; |
Christoph Hellwig | a4058c5 | 2017-05-27 11:16:54 +0300 | [diff] [blame] | 1077 | if (file->f_op->splice_read && test_bit(RQ_SPLICE_OK, &rqstp->rq_flags)) |
Trond Myklebust | 83a6307 | 2019-08-26 13:03:11 -0400 | [diff] [blame] | 1078 | err = nfsd_splice_read(rqstp, fhp, file, offset, count, eof); |
Christoph Hellwig | a4058c5 | 2017-05-27 11:16:54 +0300 | [diff] [blame] | 1079 | else |
Trond Myklebust | 83a6307 | 2019-08-26 13:03:11 -0400 | [diff] [blame] | 1080 | err = nfsd_readv(rqstp, fhp, file, offset, vec, vlen, count, eof); |
Jeff Layton | 6e8b50d | 2015-11-17 06:52:23 -0500 | [diff] [blame] | 1081 | |
Jeff Layton | 48cd7b5 | 2019-08-18 14:18:50 -0400 | [diff] [blame] | 1082 | nfsd_file_put(nf); |
J. Bruce Fields | dc97618 | 2014-03-18 17:01:51 -0400 | [diff] [blame] | 1083 | |
Chuck Lever | f394b62 | 2018-03-27 10:53:11 -0400 | [diff] [blame] | 1084 | trace_nfsd_read_done(rqstp, fhp, offset, *count); |
Jeff Layton | 6e8b50d | 2015-11-17 06:52:23 -0500 | [diff] [blame] | 1085 | |
J. Bruce Fields | fa0a212 | 2010-07-27 16:48:54 -0400 | [diff] [blame] | 1086 | return err; |
| 1087 | } |
| 1088 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | /* |
| 1090 | * Write data to a file. |
| 1091 | * The stable flag requests synchronous writes. |
| 1092 | * N.B. After this call fhp needs an fh_put |
| 1093 | */ |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1094 | __be32 |
Kinglong Mee | 52e380e | 2016-12-31 21:00:13 +0800 | [diff] [blame] | 1095 | nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t offset, |
Trond Myklebust | 19e0663 | 2020-01-06 13:40:37 -0500 | [diff] [blame] | 1096 | struct kvec *vec, int vlen, unsigned long *cnt, int stable, |
| 1097 | __be32 *verf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1098 | { |
Jeff Layton | b493523 | 2019-08-18 14:18:49 -0400 | [diff] [blame] | 1099 | struct nfsd_file *nf; |
| 1100 | __be32 err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | |
Chuck Lever | f394b62 | 2018-03-27 10:53:11 -0400 | [diff] [blame] | 1102 | trace_nfsd_write_start(rqstp, fhp, offset, *cnt); |
Jeff Layton | 6e8b50d | 2015-11-17 06:52:23 -0500 | [diff] [blame] | 1103 | |
Jeff Layton | b493523 | 2019-08-18 14:18:49 -0400 | [diff] [blame] | 1104 | err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_WRITE, &nf); |
Kinglong Mee | 52e380e | 2016-12-31 21:00:13 +0800 | [diff] [blame] | 1105 | if (err) |
| 1106 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | |
Trond Myklebust | 16f8f89 | 2020-01-06 13:40:29 -0500 | [diff] [blame] | 1108 | err = nfsd_vfs_write(rqstp, fhp, nf, offset, vec, |
Trond Myklebust | 19e0663 | 2020-01-06 13:40:37 -0500 | [diff] [blame] | 1109 | vlen, cnt, stable, verf); |
Jeff Layton | b493523 | 2019-08-18 14:18:49 -0400 | [diff] [blame] | 1110 | nfsd_file_put(nf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | out: |
Chuck Lever | f394b62 | 2018-03-27 10:53:11 -0400 | [diff] [blame] | 1112 | trace_nfsd_write_done(rqstp, fhp, offset, *cnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | return err; |
| 1114 | } |
| 1115 | |
| 1116 | #ifdef CONFIG_NFSD_V3 |
Chuck Lever | 3f96502 | 2022-01-24 15:50:31 -0500 | [diff] [blame] | 1117 | /** |
| 1118 | * nfsd_commit - Commit pending writes to stable storage |
| 1119 | * @rqstp: RPC request being processed |
| 1120 | * @fhp: NFS filehandle |
| 1121 | * @offset: raw offset from beginning of file |
| 1122 | * @count: raw count of bytes to sync |
| 1123 | * @verf: filled in with the server's current write verifier |
Trond Myklebust | aa696a6 | 2010-01-29 16:44:25 -0500 | [diff] [blame] | 1124 | * |
Chuck Lever | 3f96502 | 2022-01-24 15:50:31 -0500 | [diff] [blame] | 1125 | * Note: we guarantee that data that lies within the range specified |
| 1126 | * by the 'offset' and 'count' parameters will be synced. The server |
| 1127 | * is permitted to sync data that lies outside this range at the |
| 1128 | * same time. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1129 | * |
| 1130 | * Unfortunately we cannot lock the file to make sure we return full WCC |
| 1131 | * data to the client, as locking happens lower down in the filesystem. |
Chuck Lever | 3f96502 | 2022-01-24 15:50:31 -0500 | [diff] [blame] | 1132 | * |
| 1133 | * Return values: |
| 1134 | * An nfsstat value in network byte order. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | */ |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1136 | __be32 |
Chuck Lever | 3f96502 | 2022-01-24 15:50:31 -0500 | [diff] [blame] | 1137 | nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp, u64 offset, |
| 1138 | u32 count, __be32 *verf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | { |
Chuck Lever | 3f96502 | 2022-01-24 15:50:31 -0500 | [diff] [blame] | 1140 | u64 maxbytes; |
| 1141 | loff_t start, end; |
Chuck Lever | 2c445a0 | 2021-12-28 14:26:03 -0500 | [diff] [blame] | 1142 | struct nfsd_net *nn; |
Jeff Layton | 5920afa | 2019-08-18 14:18:51 -0400 | [diff] [blame] | 1143 | struct nfsd_file *nf; |
Chuck Lever | 3f96502 | 2022-01-24 15:50:31 -0500 | [diff] [blame] | 1144 | __be32 err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | |
Jeff Layton | 5920afa | 2019-08-18 14:18:51 -0400 | [diff] [blame] | 1146 | err = nfsd_file_acquire(rqstp, fhp, |
| 1147 | NFSD_MAY_WRITE|NFSD_MAY_NOT_BREAK_LEASE, &nf); |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 1148 | if (err) |
Trond Myklebust | aa696a6 | 2010-01-29 16:44:25 -0500 | [diff] [blame] | 1149 | goto out; |
Chuck Lever | 3f96502 | 2022-01-24 15:50:31 -0500 | [diff] [blame] | 1150 | |
| 1151 | /* |
| 1152 | * Convert the client-provided (offset, count) range to a |
| 1153 | * (start, end) range. If the client-provided range falls |
| 1154 | * outside the maximum file size of the underlying FS, |
| 1155 | * clamp the sync range appropriately. |
| 1156 | */ |
| 1157 | start = 0; |
| 1158 | end = LLONG_MAX; |
| 1159 | maxbytes = (u64)fhp->fh_dentry->d_sb->s_maxbytes; |
| 1160 | if (offset < maxbytes) { |
| 1161 | start = offset; |
| 1162 | if (count && (offset + count - 1 < maxbytes)) |
| 1163 | end = offset + count - 1; |
| 1164 | } |
| 1165 | |
Chuck Lever | 2c445a0 | 2021-12-28 14:26:03 -0500 | [diff] [blame] | 1166 | nn = net_generic(nf->nf_net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | if (EX_ISSYNC(fhp->fh_export)) { |
Trond Myklebust | 555dbf1 | 2021-12-18 20:38:01 -0500 | [diff] [blame] | 1168 | errseq_t since = READ_ONCE(nf->nf_file->f_wb_err); |
| 1169 | int err2; |
Trond Myklebust | aa696a6 | 2010-01-29 16:44:25 -0500 | [diff] [blame] | 1170 | |
Chuck Lever | 3f96502 | 2022-01-24 15:50:31 -0500 | [diff] [blame] | 1171 | err2 = vfs_fsync_range(nf->nf_file, start, end, 0); |
Trond Myklebust | bbf2f09 | 2019-09-02 13:02:58 -0400 | [diff] [blame] | 1172 | switch (err2) { |
| 1173 | case 0: |
Chuck Lever | 3988a57 | 2021-12-30 10:22:05 -0500 | [diff] [blame] | 1174 | nfsd_copy_write_verifier(verf, nn); |
Trond Myklebust | 555dbf1 | 2021-12-18 20:38:01 -0500 | [diff] [blame] | 1175 | err2 = filemap_check_wb_err(nf->nf_file->f_mapping, |
| 1176 | since); |
Trond Myklebust | bbf2f09 | 2019-09-02 13:02:58 -0400 | [diff] [blame] | 1177 | break; |
| 1178 | case -EINVAL: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1179 | err = nfserr_notsupp; |
Trond Myklebust | bbf2f09 | 2019-09-02 13:02:58 -0400 | [diff] [blame] | 1180 | break; |
| 1181 | default: |
Chuck Lever | 3988a57 | 2021-12-30 10:22:05 -0500 | [diff] [blame] | 1182 | nfsd_reset_write_verifier(nn); |
Chuck Lever | 75acacb | 2021-12-28 14:27:56 -0500 | [diff] [blame] | 1183 | trace_nfsd_writeverf_reset(nn, rqstp, err2); |
Trond Myklebust | bbf2f09 | 2019-09-02 13:02:58 -0400 | [diff] [blame] | 1184 | } |
Trond Myklebust | 555dbf1 | 2021-12-18 20:38:01 -0500 | [diff] [blame] | 1185 | err = nfserrno(err2); |
Trond Myklebust | 524ff1a | 2020-01-06 13:40:36 -0500 | [diff] [blame] | 1186 | } else |
Chuck Lever | 3988a57 | 2021-12-30 10:22:05 -0500 | [diff] [blame] | 1187 | nfsd_copy_write_verifier(verf, nn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1188 | |
Jeff Layton | 5920afa | 2019-08-18 14:18:51 -0400 | [diff] [blame] | 1189 | nfsd_file_put(nf); |
Trond Myklebust | aa696a6 | 2010-01-29 16:44:25 -0500 | [diff] [blame] | 1190 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | return err; |
| 1192 | } |
| 1193 | #endif /* CONFIG_NFSD_V3 */ |
| 1194 | |
Adrian Bunk | f2b0dee | 2008-02-13 23:30:26 +0200 | [diff] [blame] | 1195 | static __be32 |
J. Bruce Fields | 5c002b3 | 2007-11-30 16:55:23 -0500 | [diff] [blame] | 1196 | nfsd_create_setattr(struct svc_rqst *rqstp, struct svc_fh *resfhp, |
| 1197 | struct iattr *iap) |
| 1198 | { |
| 1199 | /* |
| 1200 | * Mode has already been set earlier in create: |
| 1201 | */ |
| 1202 | iap->ia_valid &= ~ATTR_MODE; |
| 1203 | /* |
| 1204 | * Setting uid/gid works only for root. Irix appears to |
| 1205 | * send along the gid on create when it tries to implement |
| 1206 | * setgid directories via NFS: |
| 1207 | */ |
Eric W. Biederman | 6fab877 | 2013-02-02 06:53:11 -0800 | [diff] [blame] | 1208 | if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) |
J. Bruce Fields | 5c002b3 | 2007-11-30 16:55:23 -0500 | [diff] [blame] | 1209 | iap->ia_valid &= ~(ATTR_UID|ATTR_GID); |
| 1210 | if (iap->ia_valid) |
Arnd Bergmann | 2a1aa48 | 2019-11-03 17:50:18 +0100 | [diff] [blame] | 1211 | return nfsd_setattr(rqstp, resfhp, iap, 0, (time64_t)0); |
Trond Myklebust | 0f3a24b | 2014-07-01 18:27:53 -0400 | [diff] [blame] | 1212 | /* Callers expect file metadata to be committed here */ |
Jeff Layton | 722b620 | 2014-07-03 07:54:19 -0400 | [diff] [blame] | 1213 | return nfserrno(commit_metadata(resfhp)); |
J. Bruce Fields | 5c002b3 | 2007-11-30 16:55:23 -0500 | [diff] [blame] | 1214 | } |
| 1215 | |
wengang wang | 4ac35c2 | 2009-02-10 11:27:51 +0800 | [diff] [blame] | 1216 | /* HPUX client sometimes creates a file in mode 000, and sets size to 0. |
| 1217 | * setting size to 0 may fail for some specific file systems by the permission |
| 1218 | * checking which requires WRITE permission but the mode is 000. |
| 1219 | * we ignore the resizing(to 0) on the just new created file, since the size is |
| 1220 | * 0 after file created. |
| 1221 | * |
| 1222 | * call this only after vfs_create() is called. |
| 1223 | * */ |
| 1224 | static void |
| 1225 | nfsd_check_ignore_resizing(struct iattr *iap) |
| 1226 | { |
| 1227 | if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0)) |
| 1228 | iap->ia_valid &= ~ATTR_SIZE; |
| 1229 | } |
| 1230 | |
J. Bruce Fields | b44061d | 2016-07-20 16:16:06 -0400 | [diff] [blame] | 1231 | /* The parent directory should already be locked: */ |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1232 | __be32 |
J. Bruce Fields | b44061d | 2016-07-20 16:16:06 -0400 | [diff] [blame] | 1233 | nfsd_create_locked(struct svc_rqst *rqstp, struct svc_fh *fhp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | char *fname, int flen, struct iattr *iap, |
| 1235 | int type, dev_t rdev, struct svc_fh *resfhp) |
| 1236 | { |
Dan Carpenter | 2b11885 | 2016-08-03 22:05:00 +0300 | [diff] [blame] | 1237 | struct dentry *dentry, *dchild; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | struct inode *dirp; |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1239 | __be32 err; |
J. Bruce Fields | 5c002b3 | 2007-11-30 16:55:23 -0500 | [diff] [blame] | 1240 | __be32 err2; |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1241 | int host_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1243 | dentry = fhp->fh_dentry; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1244 | dirp = d_inode(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1245 | |
J. Bruce Fields | b44061d | 2016-07-20 16:16:06 -0400 | [diff] [blame] | 1246 | dchild = dget(resfhp->fh_dentry); |
| 1247 | if (!fhp->fh_locked) { |
| 1248 | WARN_ONCE(1, "nfsd_create: parent %pd2 not locked!\n", |
Al Viro | a6a9f18 | 2013-09-16 10:57:01 -0400 | [diff] [blame] | 1249 | dentry); |
J. Bruce Fields | b44061d | 2016-07-20 16:16:06 -0400 | [diff] [blame] | 1250 | err = nfserr_io; |
| 1251 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1253 | |
Oleg Drokin | 7eed34f | 2016-07-14 23:20:22 -0400 | [diff] [blame] | 1254 | err = nfsd_permission(rqstp, fhp->fh_export, dentry, NFSD_MAY_CREATE); |
| 1255 | if (err) |
| 1256 | goto out; |
| 1257 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | if (!(iap->ia_valid & ATTR_MODE)) |
| 1259 | iap->ia_mode = 0; |
| 1260 | iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type; |
| 1261 | |
J. Bruce Fields | 22cf841 | 2020-06-16 16:43:18 -0400 | [diff] [blame] | 1262 | if (!IS_POSIXACL(dirp)) |
| 1263 | iap->ia_mode &= ~current_umask(); |
| 1264 | |
J. Bruce Fields | 088406b | 2006-11-08 17:44:59 -0800 | [diff] [blame] | 1265 | err = 0; |
Jan Kara | 4a55c10 | 2012-06-12 16:20:33 +0200 | [diff] [blame] | 1266 | host_err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | switch (type) { |
| 1268 | case S_IFREG: |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 1269 | host_err = vfs_create(&init_user_ns, dirp, dchild, iap->ia_mode, true); |
wengang wang | 4ac35c2 | 2009-02-10 11:27:51 +0800 | [diff] [blame] | 1270 | if (!host_err) |
| 1271 | nfsd_check_ignore_resizing(iap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1272 | break; |
| 1273 | case S_IFDIR: |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 1274 | host_err = vfs_mkdir(&init_user_ns, dirp, dchild, iap->ia_mode); |
Al Viro | 3819bb0 | 2018-05-11 17:03:19 -0400 | [diff] [blame] | 1275 | if (!host_err && unlikely(d_unhashed(dchild))) { |
| 1276 | struct dentry *d; |
| 1277 | d = lookup_one_len(dchild->d_name.name, |
| 1278 | dchild->d_parent, |
| 1279 | dchild->d_name.len); |
| 1280 | if (IS_ERR(d)) { |
| 1281 | host_err = PTR_ERR(d); |
| 1282 | break; |
| 1283 | } |
| 1284 | if (unlikely(d_is_negative(d))) { |
| 1285 | dput(d); |
| 1286 | err = nfserr_serverfault; |
| 1287 | goto out; |
| 1288 | } |
| 1289 | dput(resfhp->fh_dentry); |
| 1290 | resfhp->fh_dentry = dget(d); |
| 1291 | err = fh_update(resfhp); |
| 1292 | dput(dchild); |
| 1293 | dchild = d; |
| 1294 | if (err) |
| 1295 | goto out; |
| 1296 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1297 | break; |
| 1298 | case S_IFCHR: |
| 1299 | case S_IFBLK: |
| 1300 | case S_IFIFO: |
| 1301 | case S_IFSOCK: |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 1302 | host_err = vfs_mknod(&init_user_ns, dirp, dchild, |
| 1303 | iap->ia_mode, rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | break; |
J. Bruce Fields | 71423274 | 2016-07-22 12:03:46 -0400 | [diff] [blame] | 1305 | default: |
| 1306 | printk(KERN_WARNING "nfsd: bad file type %o in nfsd_create\n", |
| 1307 | type); |
| 1308 | host_err = -EINVAL; |
Dave Hansen | 463c319 | 2008-02-15 14:37:57 -0800 | [diff] [blame] | 1309 | } |
Jan Kara | 4a55c10 | 2012-06-12 16:20:33 +0200 | [diff] [blame] | 1310 | if (host_err < 0) |
Dave Hansen | 463c319 | 2008-02-15 14:37:57 -0800 | [diff] [blame] | 1311 | goto out_nfserr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1312 | |
Ben Myers | f501912 | 2010-02-17 14:05:11 -0600 | [diff] [blame] | 1313 | err = nfsd_create_setattr(rqstp, resfhp, iap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | |
Ben Myers | f501912 | 2010-02-17 14:05:11 -0600 | [diff] [blame] | 1315 | /* |
Trond Myklebust | 0f3a24b | 2014-07-01 18:27:53 -0400 | [diff] [blame] | 1316 | * nfsd_create_setattr already committed the child. Transactional |
| 1317 | * filesystems had a chance to commit changes for both parent and |
J. Bruce Fields | b44061d | 2016-07-20 16:16:06 -0400 | [diff] [blame] | 1318 | * child simultaneously making the following commit_metadata a |
Trond Myklebust | 0f3a24b | 2014-07-01 18:27:53 -0400 | [diff] [blame] | 1319 | * noop. |
Ben Myers | f501912 | 2010-02-17 14:05:11 -0600 | [diff] [blame] | 1320 | */ |
| 1321 | err2 = nfserrno(commit_metadata(fhp)); |
J. Bruce Fields | 5c002b3 | 2007-11-30 16:55:23 -0500 | [diff] [blame] | 1322 | if (err2) |
| 1323 | err = err2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1324 | /* |
| 1325 | * Update the file handle to get the new inode info. |
| 1326 | */ |
| 1327 | if (!err) |
| 1328 | err = fh_update(resfhp); |
| 1329 | out: |
Dan Carpenter | 2b11885 | 2016-08-03 22:05:00 +0300 | [diff] [blame] | 1330 | dput(dchild); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1331 | return err; |
| 1332 | |
| 1333 | out_nfserr: |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1334 | err = nfserrno(host_err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | goto out; |
| 1336 | } |
| 1337 | |
J. Bruce Fields | b44061d | 2016-07-20 16:16:06 -0400 | [diff] [blame] | 1338 | /* |
| 1339 | * Create a filesystem object (regular, directory, special). |
| 1340 | * Note that the parent directory is left locked. |
| 1341 | * |
| 1342 | * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp |
| 1343 | */ |
| 1344 | __be32 |
| 1345 | nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp, |
| 1346 | char *fname, int flen, struct iattr *iap, |
| 1347 | int type, dev_t rdev, struct svc_fh *resfhp) |
| 1348 | { |
| 1349 | struct dentry *dentry, *dchild = NULL; |
J. Bruce Fields | b44061d | 2016-07-20 16:16:06 -0400 | [diff] [blame] | 1350 | __be32 err; |
| 1351 | int host_err; |
| 1352 | |
| 1353 | if (isdotent(fname, flen)) |
| 1354 | return nfserr_exist; |
| 1355 | |
J. Bruce Fields | fa08139 | 2016-07-21 16:00:12 -0400 | [diff] [blame] | 1356 | err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_NOP); |
J. Bruce Fields | b44061d | 2016-07-20 16:16:06 -0400 | [diff] [blame] | 1357 | if (err) |
| 1358 | return err; |
| 1359 | |
| 1360 | dentry = fhp->fh_dentry; |
J. Bruce Fields | b44061d | 2016-07-20 16:16:06 -0400 | [diff] [blame] | 1361 | |
| 1362 | host_err = fh_want_write(fhp); |
| 1363 | if (host_err) |
| 1364 | return nfserrno(host_err); |
| 1365 | |
| 1366 | fh_lock_nested(fhp, I_MUTEX_PARENT); |
| 1367 | dchild = lookup_one_len(fname, dentry, flen); |
| 1368 | host_err = PTR_ERR(dchild); |
| 1369 | if (IS_ERR(dchild)) |
| 1370 | return nfserrno(host_err); |
| 1371 | err = fh_compose(resfhp, fhp->fh_export, dchild, fhp); |
Josef Bacik | 502aa0a | 2016-08-10 14:46:27 -0400 | [diff] [blame] | 1372 | /* |
| 1373 | * We unconditionally drop our ref to dchild as fh_compose will have |
| 1374 | * already grabbed its own ref for it. |
| 1375 | */ |
| 1376 | dput(dchild); |
| 1377 | if (err) |
J. Bruce Fields | b44061d | 2016-07-20 16:16:06 -0400 | [diff] [blame] | 1378 | return err; |
J. Bruce Fields | b44061d | 2016-07-20 16:16:06 -0400 | [diff] [blame] | 1379 | return nfsd_create_locked(rqstp, fhp, fname, flen, iap, type, |
| 1380 | rdev, resfhp); |
| 1381 | } |
| 1382 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1383 | #ifdef CONFIG_NFSD_V3 |
Mi Jinlong | ac6721a | 2011-04-20 17:06:25 +0800 | [diff] [blame] | 1384 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1385 | /* |
Mi Jinlong | ac6721a | 2011-04-20 17:06:25 +0800 | [diff] [blame] | 1386 | * NFSv3 and NFSv4 version of nfsd_create |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1387 | */ |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1388 | __be32 |
Mi Jinlong | ac6721a | 2011-04-20 17:06:25 +0800 | [diff] [blame] | 1389 | do_nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1390 | char *fname, int flen, struct iattr *iap, |
| 1391 | struct svc_fh *resfhp, int createmode, u32 *verifier, |
J. Bruce Fields | 856121b | 2011-10-13 11:37:11 -0400 | [diff] [blame] | 1392 | bool *truncp, bool *created) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | { |
| 1394 | struct dentry *dentry, *dchild = NULL; |
| 1395 | struct inode *dirp; |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1396 | __be32 err; |
| 1397 | int host_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | __u32 v_mtime=0, v_atime=0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1399 | |
| 1400 | err = nfserr_perm; |
| 1401 | if (!flen) |
| 1402 | goto out; |
| 1403 | err = nfserr_exist; |
| 1404 | if (isdotent(fname, flen)) |
| 1405 | goto out; |
| 1406 | if (!(iap->ia_valid & ATTR_MODE)) |
| 1407 | iap->ia_mode = 0; |
Sachin Prabhu | 1574dff | 2011-04-20 13:09:35 +0100 | [diff] [blame] | 1408 | err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | if (err) |
| 1410 | goto out; |
| 1411 | |
| 1412 | dentry = fhp->fh_dentry; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1413 | dirp = d_inode(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | |
Jan Kara | 4a55c10 | 2012-06-12 16:20:33 +0200 | [diff] [blame] | 1415 | host_err = fh_want_write(fhp); |
| 1416 | if (host_err) |
| 1417 | goto out_nfserr; |
| 1418 | |
Peter Zijlstra | 12fd352 | 2006-10-02 02:18:03 -0700 | [diff] [blame] | 1419 | fh_lock_nested(fhp, I_MUTEX_PARENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1420 | |
| 1421 | /* |
| 1422 | * Compose the response file handle. |
| 1423 | */ |
| 1424 | dchild = lookup_one_len(fname, dentry, flen); |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1425 | host_err = PTR_ERR(dchild); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | if (IS_ERR(dchild)) |
| 1427 | goto out_nfserr; |
| 1428 | |
Sachin Prabhu | 1574dff | 2011-04-20 13:09:35 +0100 | [diff] [blame] | 1429 | /* If file doesn't exist, check for permissions to create one */ |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1430 | if (d_really_is_negative(dchild)) { |
Sachin Prabhu | 1574dff | 2011-04-20 13:09:35 +0100 | [diff] [blame] | 1431 | err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE); |
| 1432 | if (err) |
| 1433 | goto out; |
| 1434 | } |
| 1435 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1436 | err = fh_compose(resfhp, fhp->fh_export, dchild, fhp); |
| 1437 | if (err) |
| 1438 | goto out; |
| 1439 | |
Mi Jinlong | ac6721a | 2011-04-20 17:06:25 +0800 | [diff] [blame] | 1440 | if (nfsd_create_is_exclusive(createmode)) { |
Peter Staubach | c397852 | 2007-01-26 00:57:00 -0800 | [diff] [blame] | 1441 | /* solaris7 gets confused (bugid 4218508) if these have |
J. Bruce Fields | 2336d69 | 2021-10-15 14:42:11 -0400 | [diff] [blame] | 1442 | * the high bit set, as do xfs filesystems without the |
| 1443 | * "bigtime" feature. So just clear the high bits. If this is |
Jeff Layton | 749997e | 2007-07-31 00:37:51 -0700 | [diff] [blame] | 1444 | * ever changed to use different attrs for storing the |
| 1445 | * verifier, then do_open_lookup() will also need to be fixed |
| 1446 | * accordingly. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1447 | */ |
| 1448 | v_mtime = verifier[0]&0x7fffffff; |
| 1449 | v_atime = verifier[1]&0x7fffffff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1450 | } |
| 1451 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1452 | if (d_really_is_positive(dchild)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1453 | err = 0; |
| 1454 | |
| 1455 | switch (createmode) { |
| 1456 | case NFS3_CREATE_UNCHECKED: |
David Howells | e36cb0b | 2015-01-29 12:02:35 +0000 | [diff] [blame] | 1457 | if (! d_is_reg(dchild)) |
J. Bruce Fields | 9dc4e6c | 2012-04-09 18:06:49 -0400 | [diff] [blame] | 1458 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1459 | else if (truncp) { |
| 1460 | /* in nfsv4, we need to treat this case a little |
| 1461 | * differently. we don't want to truncate the |
| 1462 | * file now; this would be wrong if the OPEN |
| 1463 | * fails for some other reason. furthermore, |
| 1464 | * if the size is nonzero, we should ignore it |
| 1465 | * according to spec! |
| 1466 | */ |
| 1467 | *truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size; |
| 1468 | } |
| 1469 | else { |
| 1470 | iap->ia_valid &= ATTR_SIZE; |
| 1471 | goto set_attr; |
| 1472 | } |
| 1473 | break; |
| 1474 | case NFS3_CREATE_EXCLUSIVE: |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1475 | if ( d_inode(dchild)->i_mtime.tv_sec == v_mtime |
| 1476 | && d_inode(dchild)->i_atime.tv_sec == v_atime |
| 1477 | && d_inode(dchild)->i_size == 0 ) { |
Neil Brown | 7007c90 | 2012-12-07 15:40:55 -0500 | [diff] [blame] | 1478 | if (created) |
zhengbin | 384a7cca | 2019-12-25 11:19:34 +0800 | [diff] [blame] | 1479 | *created = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1480 | break; |
Neil Brown | 7007c90 | 2012-12-07 15:40:55 -0500 | [diff] [blame] | 1481 | } |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 1482 | fallthrough; |
Mi Jinlong | ac6721a | 2011-04-20 17:06:25 +0800 | [diff] [blame] | 1483 | case NFS4_CREATE_EXCLUSIVE4_1: |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1484 | if ( d_inode(dchild)->i_mtime.tv_sec == v_mtime |
| 1485 | && d_inode(dchild)->i_atime.tv_sec == v_atime |
| 1486 | && d_inode(dchild)->i_size == 0 ) { |
Neil Brown | 7007c90 | 2012-12-07 15:40:55 -0500 | [diff] [blame] | 1487 | if (created) |
zhengbin | 384a7cca | 2019-12-25 11:19:34 +0800 | [diff] [blame] | 1488 | *created = true; |
Mi Jinlong | ac6721a | 2011-04-20 17:06:25 +0800 | [diff] [blame] | 1489 | goto set_attr; |
Neil Brown | 7007c90 | 2012-12-07 15:40:55 -0500 | [diff] [blame] | 1490 | } |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 1491 | fallthrough; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1492 | case NFS3_CREATE_GUARDED: |
| 1493 | err = nfserr_exist; |
| 1494 | } |
Al Viro | bad0dcf | 2011-11-23 12:03:18 -0500 | [diff] [blame] | 1495 | fh_drop_write(fhp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1496 | goto out; |
| 1497 | } |
| 1498 | |
J. Bruce Fields | 22cf841 | 2020-06-16 16:43:18 -0400 | [diff] [blame] | 1499 | if (!IS_POSIXACL(dirp)) |
| 1500 | iap->ia_mode &= ~current_umask(); |
| 1501 | |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 1502 | host_err = vfs_create(&init_user_ns, dirp, dchild, iap->ia_mode, true); |
Dave Hansen | 463c319 | 2008-02-15 14:37:57 -0800 | [diff] [blame] | 1503 | if (host_err < 0) { |
Al Viro | bad0dcf | 2011-11-23 12:03:18 -0500 | [diff] [blame] | 1504 | fh_drop_write(fhp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1505 | goto out_nfserr; |
Dave Hansen | 463c319 | 2008-02-15 14:37:57 -0800 | [diff] [blame] | 1506 | } |
J. Bruce Fields | 81ac95c | 2006-11-08 17:44:40 -0800 | [diff] [blame] | 1507 | if (created) |
zhengbin | 384a7cca | 2019-12-25 11:19:34 +0800 | [diff] [blame] | 1508 | *created = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1509 | |
wengang wang | 4ac35c2 | 2009-02-10 11:27:51 +0800 | [diff] [blame] | 1510 | nfsd_check_ignore_resizing(iap); |
| 1511 | |
Mi Jinlong | ac6721a | 2011-04-20 17:06:25 +0800 | [diff] [blame] | 1512 | if (nfsd_create_is_exclusive(createmode)) { |
Peter Staubach | c397852 | 2007-01-26 00:57:00 -0800 | [diff] [blame] | 1513 | /* Cram the verifier into atime/mtime */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1514 | iap->ia_valid = ATTR_MTIME|ATTR_ATIME |
Peter Staubach | c397852 | 2007-01-26 00:57:00 -0800 | [diff] [blame] | 1515 | | ATTR_MTIME_SET|ATTR_ATIME_SET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1516 | /* XXX someone who knows this better please fix it for nsec */ |
| 1517 | iap->ia_mtime.tv_sec = v_mtime; |
| 1518 | iap->ia_atime.tv_sec = v_atime; |
| 1519 | iap->ia_mtime.tv_nsec = 0; |
| 1520 | iap->ia_atime.tv_nsec = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | } |
| 1522 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1523 | set_attr: |
Ben Myers | f501912 | 2010-02-17 14:05:11 -0600 | [diff] [blame] | 1524 | err = nfsd_create_setattr(rqstp, resfhp, iap); |
| 1525 | |
| 1526 | /* |
Trond Myklebust | 0f3a24b | 2014-07-01 18:27:53 -0400 | [diff] [blame] | 1527 | * nfsd_create_setattr already committed the child |
| 1528 | * (and possibly also the parent). |
Ben Myers | f501912 | 2010-02-17 14:05:11 -0600 | [diff] [blame] | 1529 | */ |
| 1530 | if (!err) |
| 1531 | err = nfserrno(commit_metadata(fhp)); |
YAMAMOTO Takashi | f193fba | 2006-01-18 17:43:13 -0800 | [diff] [blame] | 1532 | |
| 1533 | /* |
| 1534 | * Update the filehandle to get the new inode info. |
| 1535 | */ |
| 1536 | if (!err) |
| 1537 | err = fh_update(resfhp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1538 | |
| 1539 | out: |
| 1540 | fh_unlock(fhp); |
| 1541 | if (dchild && !IS_ERR(dchild)) |
| 1542 | dput(dchild); |
Jan Kara | 4a55c10 | 2012-06-12 16:20:33 +0200 | [diff] [blame] | 1543 | fh_drop_write(fhp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 | return err; |
| 1545 | |
| 1546 | out_nfserr: |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1547 | err = nfserrno(host_err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1548 | goto out; |
| 1549 | } |
| 1550 | #endif /* CONFIG_NFSD_V3 */ |
| 1551 | |
| 1552 | /* |
| 1553 | * Read a symlink. On entry, *lenp must contain the maximum path length that |
| 1554 | * fits into the buffer. On return, it contains the true length. |
| 1555 | * N.B. After this call fhp needs an fh_put |
| 1556 | */ |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1557 | __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1558 | nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp) |
| 1559 | { |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1560 | __be32 err; |
Al Viro | 4d7edbc | 2017-05-27 16:11:23 -0400 | [diff] [blame] | 1561 | const char *link; |
Al Viro | 68ac123 | 2012-03-15 08:21:57 -0400 | [diff] [blame] | 1562 | struct path path; |
Al Viro | 4d7edbc | 2017-05-27 16:11:23 -0400 | [diff] [blame] | 1563 | DEFINE_DELAYED_CALL(done); |
| 1564 | int len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1565 | |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 1566 | err = fh_verify(rqstp, fhp, S_IFLNK, NFSD_MAY_NOP); |
Al Viro | 4d7edbc | 2017-05-27 16:11:23 -0400 | [diff] [blame] | 1567 | if (unlikely(err)) |
| 1568 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1569 | |
Al Viro | 68ac123 | 2012-03-15 08:21:57 -0400 | [diff] [blame] | 1570 | path.mnt = fhp->fh_export->ex_path.mnt; |
| 1571 | path.dentry = fhp->fh_dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1572 | |
Al Viro | 4d7edbc | 2017-05-27 16:11:23 -0400 | [diff] [blame] | 1573 | if (unlikely(!d_is_symlink(path.dentry))) |
| 1574 | return nfserr_inval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1575 | |
Al Viro | 68ac123 | 2012-03-15 08:21:57 -0400 | [diff] [blame] | 1576 | touch_atime(&path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1577 | |
Al Viro | 4d7edbc | 2017-05-27 16:11:23 -0400 | [diff] [blame] | 1578 | link = vfs_get_link(path.dentry, &done); |
| 1579 | if (IS_ERR(link)) |
| 1580 | return nfserrno(PTR_ERR(link)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1581 | |
Al Viro | 4d7edbc | 2017-05-27 16:11:23 -0400 | [diff] [blame] | 1582 | len = strlen(link); |
| 1583 | if (len < *lenp) |
| 1584 | *lenp = len; |
| 1585 | memcpy(buf, link, *lenp); |
| 1586 | do_delayed_call(&done); |
| 1587 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1588 | } |
| 1589 | |
| 1590 | /* |
| 1591 | * Create a symlink and look up its inode |
| 1592 | * N.B. After this call _both_ fhp and resfhp need an fh_put |
| 1593 | */ |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1594 | __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1595 | nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp, |
| 1596 | char *fname, int flen, |
J. Bruce Fields | 52ee043 | 2014-06-20 11:52:21 -0400 | [diff] [blame] | 1597 | char *path, |
Kinglong Mee | 1e444f5 | 2014-07-01 17:48:02 +0800 | [diff] [blame] | 1598 | struct svc_fh *resfhp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1599 | { |
| 1600 | struct dentry *dentry, *dnew; |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1601 | __be32 err, cerr; |
| 1602 | int host_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1603 | |
| 1604 | err = nfserr_noent; |
J. Bruce Fields | 52ee043 | 2014-06-20 11:52:21 -0400 | [diff] [blame] | 1605 | if (!flen || path[0] == '\0') |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1606 | goto out; |
| 1607 | err = nfserr_exist; |
| 1608 | if (isdotent(fname, flen)) |
| 1609 | goto out; |
| 1610 | |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 1611 | err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1612 | if (err) |
| 1613 | goto out; |
Jan Kara | 4a55c10 | 2012-06-12 16:20:33 +0200 | [diff] [blame] | 1614 | |
| 1615 | host_err = fh_want_write(fhp); |
| 1616 | if (host_err) |
| 1617 | goto out_nfserr; |
| 1618 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1619 | fh_lock(fhp); |
| 1620 | dentry = fhp->fh_dentry; |
| 1621 | dnew = lookup_one_len(fname, dentry, flen); |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1622 | host_err = PTR_ERR(dnew); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1623 | if (IS_ERR(dnew)) |
| 1624 | goto out_nfserr; |
| 1625 | |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 1626 | host_err = vfs_symlink(&init_user_ns, d_inode(dentry), dnew, path); |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1627 | err = nfserrno(host_err); |
J. Bruce Fields | eeeadbb | 2021-05-14 18:21:37 -0400 | [diff] [blame] | 1628 | fh_unlock(fhp); |
Ben Myers | f501912 | 2010-02-17 14:05:11 -0600 | [diff] [blame] | 1629 | if (!err) |
| 1630 | err = nfserrno(commit_metadata(fhp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1631 | |
Al Viro | bad0dcf | 2011-11-23 12:03:18 -0500 | [diff] [blame] | 1632 | fh_drop_write(fhp); |
Dave Hansen | 75c3f29 | 2008-02-15 14:37:45 -0800 | [diff] [blame] | 1633 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1634 | cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp); |
| 1635 | dput(dnew); |
| 1636 | if (err==0) err = cerr; |
| 1637 | out: |
| 1638 | return err; |
| 1639 | |
| 1640 | out_nfserr: |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1641 | err = nfserrno(host_err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | goto out; |
| 1643 | } |
| 1644 | |
| 1645 | /* |
| 1646 | * Create a hardlink |
| 1647 | * N.B. After this call _both_ ffhp and tfhp need an fh_put |
| 1648 | */ |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1649 | __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1650 | nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp, |
| 1651 | char *name, int len, struct svc_fh *tfhp) |
| 1652 | { |
| 1653 | struct dentry *ddir, *dnew, *dold; |
J. Bruce Fields | 55b1335 | 2010-07-19 16:38:24 -0400 | [diff] [blame] | 1654 | struct inode *dirp; |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1655 | __be32 err; |
| 1656 | int host_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1657 | |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 1658 | err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_CREATE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1659 | if (err) |
| 1660 | goto out; |
J. Bruce Fields | 7d818a7 | 2011-08-15 16:59:55 -0400 | [diff] [blame] | 1661 | err = fh_verify(rqstp, tfhp, 0, NFSD_MAY_NOP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1662 | if (err) |
| 1663 | goto out; |
J. Bruce Fields | 7d818a7 | 2011-08-15 16:59:55 -0400 | [diff] [blame] | 1664 | err = nfserr_isdir; |
David Howells | e36cb0b | 2015-01-29 12:02:35 +0000 | [diff] [blame] | 1665 | if (d_is_dir(tfhp->fh_dentry)) |
J. Bruce Fields | 7d818a7 | 2011-08-15 16:59:55 -0400 | [diff] [blame] | 1666 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1667 | err = nfserr_perm; |
| 1668 | if (!len) |
| 1669 | goto out; |
| 1670 | err = nfserr_exist; |
| 1671 | if (isdotent(name, len)) |
| 1672 | goto out; |
| 1673 | |
Jan Kara | 4a55c10 | 2012-06-12 16:20:33 +0200 | [diff] [blame] | 1674 | host_err = fh_want_write(tfhp); |
| 1675 | if (host_err) { |
| 1676 | err = nfserrno(host_err); |
| 1677 | goto out; |
| 1678 | } |
| 1679 | |
Peter Zijlstra | 12fd352 | 2006-10-02 02:18:03 -0700 | [diff] [blame] | 1680 | fh_lock_nested(ffhp, I_MUTEX_PARENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1681 | ddir = ffhp->fh_dentry; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1682 | dirp = d_inode(ddir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1683 | |
| 1684 | dnew = lookup_one_len(name, ddir, len); |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1685 | host_err = PTR_ERR(dnew); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1686 | if (IS_ERR(dnew)) |
| 1687 | goto out_nfserr; |
| 1688 | |
| 1689 | dold = tfhp->fh_dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1690 | |
J. Bruce Fields | 4795bb3 | 2011-01-11 13:55:46 -0500 | [diff] [blame] | 1691 | err = nfserr_noent; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1692 | if (d_really_is_negative(dold)) |
Jan Kara | 4a55c10 | 2012-06-12 16:20:33 +0200 | [diff] [blame] | 1693 | goto out_dput; |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 1694 | host_err = vfs_link(dold, &init_user_ns, dirp, dnew, NULL); |
J. Bruce Fields | eeeadbb | 2021-05-14 18:21:37 -0400 | [diff] [blame] | 1695 | fh_unlock(ffhp); |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1696 | if (!host_err) { |
Ben Myers | f501912 | 2010-02-17 14:05:11 -0600 | [diff] [blame] | 1697 | err = nfserrno(commit_metadata(ffhp)); |
| 1698 | if (!err) |
| 1699 | err = nfserrno(commit_metadata(tfhp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1700 | } else { |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1701 | if (host_err == -EXDEV && rqstp->rq_vers == 2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1702 | err = nfserr_acces; |
| 1703 | else |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1704 | err = nfserrno(host_err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1705 | } |
Dave Hansen | 75c3f29 | 2008-02-15 14:37:45 -0800 | [diff] [blame] | 1706 | out_dput: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1707 | dput(dnew); |
David M. Richter | 270d56e | 2006-06-30 01:56:15 -0700 | [diff] [blame] | 1708 | out_unlock: |
| 1709 | fh_unlock(ffhp); |
Jan Kara | 4a55c10 | 2012-06-12 16:20:33 +0200 | [diff] [blame] | 1710 | fh_drop_write(tfhp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1711 | out: |
| 1712 | return err; |
| 1713 | |
| 1714 | out_nfserr: |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1715 | err = nfserrno(host_err); |
David M. Richter | 270d56e | 2006-06-30 01:56:15 -0700 | [diff] [blame] | 1716 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1717 | } |
| 1718 | |
Jeff Layton | 7775ec5 | 2019-08-18 14:18:57 -0400 | [diff] [blame] | 1719 | static void |
| 1720 | nfsd_close_cached_files(struct dentry *dentry) |
| 1721 | { |
| 1722 | struct inode *inode = d_inode(dentry); |
| 1723 | |
| 1724 | if (inode && S_ISREG(inode->i_mode)) |
| 1725 | nfsd_file_close_inode_sync(inode); |
| 1726 | } |
| 1727 | |
| 1728 | static bool |
| 1729 | nfsd_has_cached_files(struct dentry *dentry) |
| 1730 | { |
| 1731 | bool ret = false; |
| 1732 | struct inode *inode = d_inode(dentry); |
| 1733 | |
| 1734 | if (inode && S_ISREG(inode->i_mode)) |
| 1735 | ret = nfsd_file_is_cached(inode); |
| 1736 | return ret; |
| 1737 | } |
| 1738 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1739 | /* |
| 1740 | * Rename a file |
| 1741 | * N.B. After this call _both_ ffhp and tfhp need an fh_put |
| 1742 | */ |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1743 | __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1744 | nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen, |
| 1745 | struct svc_fh *tfhp, char *tname, int tlen) |
| 1746 | { |
| 1747 | struct dentry *fdentry, *tdentry, *odentry, *ndentry, *trap; |
| 1748 | struct inode *fdir, *tdir; |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1749 | __be32 err; |
| 1750 | int host_err; |
Jeff Layton | 7f84b48 | 2020-11-30 17:03:16 -0500 | [diff] [blame] | 1751 | bool close_cached = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1752 | |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 1753 | err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_REMOVE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1754 | if (err) |
| 1755 | goto out; |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 1756 | err = fh_verify(rqstp, tfhp, S_IFDIR, NFSD_MAY_CREATE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1757 | if (err) |
| 1758 | goto out; |
| 1759 | |
| 1760 | fdentry = ffhp->fh_dentry; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1761 | fdir = d_inode(fdentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1762 | |
| 1763 | tdentry = tfhp->fh_dentry; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1764 | tdir = d_inode(tdentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1765 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1766 | err = nfserr_perm; |
| 1767 | if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen)) |
| 1768 | goto out; |
| 1769 | |
Jeff Layton | 7775ec5 | 2019-08-18 14:18:57 -0400 | [diff] [blame] | 1770 | retry: |
Jan Kara | 4a55c10 | 2012-06-12 16:20:33 +0200 | [diff] [blame] | 1771 | host_err = fh_want_write(ffhp); |
| 1772 | if (host_err) { |
| 1773 | err = nfserrno(host_err); |
| 1774 | goto out; |
| 1775 | } |
| 1776 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1777 | /* cannot use fh_lock as we need deadlock protective ordering |
| 1778 | * so do it by hand */ |
| 1779 | trap = lock_rename(tdentry, fdentry); |
Jeff Layton | aaf91ec | 2015-09-17 08:28:39 -0400 | [diff] [blame] | 1780 | ffhp->fh_locked = tfhp->fh_locked = true; |
Chuck Lever | fcb5e3f | 2021-12-24 14:36:49 -0500 | [diff] [blame] | 1781 | fh_fill_pre_attrs(ffhp); |
| 1782 | fh_fill_pre_attrs(tfhp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1783 | |
| 1784 | odentry = lookup_one_len(fname, fdentry, flen); |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1785 | host_err = PTR_ERR(odentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1786 | if (IS_ERR(odentry)) |
| 1787 | goto out_nfserr; |
| 1788 | |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1789 | host_err = -ENOENT; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1790 | if (d_really_is_negative(odentry)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1791 | goto out_dput_old; |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1792 | host_err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1793 | if (odentry == trap) |
| 1794 | goto out_dput_old; |
| 1795 | |
| 1796 | ndentry = lookup_one_len(tname, tdentry, tlen); |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1797 | host_err = PTR_ERR(ndentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1798 | if (IS_ERR(ndentry)) |
| 1799 | goto out_dput_old; |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1800 | host_err = -ENOTEMPTY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1801 | if (ndentry == trap) |
| 1802 | goto out_dput_new; |
| 1803 | |
Dave Hansen | 9079b1e | 2008-02-15 14:37:49 -0800 | [diff] [blame] | 1804 | host_err = -EXDEV; |
| 1805 | if (ffhp->fh_export->ex_path.mnt != tfhp->fh_export->ex_path.mnt) |
| 1806 | goto out_dput_new; |
J. Bruce Fields | aa387d6c | 2013-04-15 16:03:46 -0400 | [diff] [blame] | 1807 | if (ffhp->fh_export->ex_path.dentry != tfhp->fh_export->ex_path.dentry) |
| 1808 | goto out_dput_new; |
Dave Hansen | 9079b1e | 2008-02-15 14:37:49 -0800 | [diff] [blame] | 1809 | |
Jeff Layton | 7f84b48 | 2020-11-30 17:03:16 -0500 | [diff] [blame] | 1810 | if ((ndentry->d_sb->s_export_op->flags & EXPORT_OP_CLOSE_BEFORE_UNLINK) && |
| 1811 | nfsd_has_cached_files(ndentry)) { |
| 1812 | close_cached = true; |
Jeff Layton | 7775ec5 | 2019-08-18 14:18:57 -0400 | [diff] [blame] | 1813 | goto out_dput_old; |
| 1814 | } else { |
Christian Brauner | 9fe6145 | 2021-01-21 14:19:32 +0100 | [diff] [blame] | 1815 | struct renamedata rd = { |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 1816 | .old_mnt_userns = &init_user_ns, |
Christian Brauner | 9fe6145 | 2021-01-21 14:19:32 +0100 | [diff] [blame] | 1817 | .old_dir = fdir, |
| 1818 | .old_dentry = odentry, |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 1819 | .new_mnt_userns = &init_user_ns, |
Christian Brauner | 9fe6145 | 2021-01-21 14:19:32 +0100 | [diff] [blame] | 1820 | .new_dir = tdir, |
| 1821 | .new_dentry = ndentry, |
| 1822 | }; |
| 1823 | host_err = vfs_rename(&rd); |
Jeff Layton | 7775ec5 | 2019-08-18 14:18:57 -0400 | [diff] [blame] | 1824 | if (!host_err) { |
| 1825 | host_err = commit_metadata(tfhp); |
| 1826 | if (!host_err) |
| 1827 | host_err = commit_metadata(ffhp); |
| 1828 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1829 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1830 | out_dput_new: |
| 1831 | dput(ndentry); |
| 1832 | out_dput_old: |
| 1833 | dput(odentry); |
| 1834 | out_nfserr: |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1835 | err = nfserrno(host_err); |
J. Bruce Fields | fbb74a3 | 2014-03-28 16:43:17 -0400 | [diff] [blame] | 1836 | /* |
| 1837 | * We cannot rely on fh_unlock on the two filehandles, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1838 | * as that would do the wrong thing if the two directories |
J. Bruce Fields | fbb74a3 | 2014-03-28 16:43:17 -0400 | [diff] [blame] | 1839 | * were the same, so again we do it by hand. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1840 | */ |
Jeff Layton | 7f84b48 | 2020-11-30 17:03:16 -0500 | [diff] [blame] | 1841 | if (!close_cached) { |
Chuck Lever | fcb5e3f | 2021-12-24 14:36:49 -0500 | [diff] [blame] | 1842 | fh_fill_post_attrs(ffhp); |
| 1843 | fh_fill_post_attrs(tfhp); |
Jeff Layton | 7775ec5 | 2019-08-18 14:18:57 -0400 | [diff] [blame] | 1844 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1845 | unlock_rename(tdentry, fdentry); |
Jeff Layton | aaf91ec | 2015-09-17 08:28:39 -0400 | [diff] [blame] | 1846 | ffhp->fh_locked = tfhp->fh_locked = false; |
Jan Kara | 4a55c10 | 2012-06-12 16:20:33 +0200 | [diff] [blame] | 1847 | fh_drop_write(ffhp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1848 | |
Jeff Layton | 7775ec5 | 2019-08-18 14:18:57 -0400 | [diff] [blame] | 1849 | /* |
| 1850 | * If the target dentry has cached open files, then we need to try to |
| 1851 | * close them prior to doing the rename. Flushing delayed fput |
| 1852 | * shouldn't be done with locks held however, so we delay it until this |
| 1853 | * point and then reattempt the whole shebang. |
| 1854 | */ |
Jeff Layton | 7f84b48 | 2020-11-30 17:03:16 -0500 | [diff] [blame] | 1855 | if (close_cached) { |
| 1856 | close_cached = false; |
Jeff Layton | 7775ec5 | 2019-08-18 14:18:57 -0400 | [diff] [blame] | 1857 | nfsd_close_cached_files(ndentry); |
| 1858 | dput(ndentry); |
| 1859 | goto retry; |
| 1860 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1861 | out: |
| 1862 | return err; |
| 1863 | } |
| 1864 | |
| 1865 | /* |
| 1866 | * Unlink a file or directory |
| 1867 | * N.B. After this call fhp needs an fh_put |
| 1868 | */ |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1869 | __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1870 | nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type, |
| 1871 | char *fname, int flen) |
| 1872 | { |
| 1873 | struct dentry *dentry, *rdentry; |
| 1874 | struct inode *dirp; |
Yu Hsiang Huang | e5d74a2 | 2021-05-14 11:58:29 +0800 | [diff] [blame] | 1875 | struct inode *rinode; |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1876 | __be32 err; |
| 1877 | int host_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1878 | |
| 1879 | err = nfserr_acces; |
| 1880 | if (!flen || isdotent(fname, flen)) |
| 1881 | goto out; |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 1882 | err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_REMOVE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1883 | if (err) |
| 1884 | goto out; |
| 1885 | |
Jan Kara | 4a55c10 | 2012-06-12 16:20:33 +0200 | [diff] [blame] | 1886 | host_err = fh_want_write(fhp); |
| 1887 | if (host_err) |
| 1888 | goto out_nfserr; |
| 1889 | |
Peter Zijlstra | 12fd352 | 2006-10-02 02:18:03 -0700 | [diff] [blame] | 1890 | fh_lock_nested(fhp, I_MUTEX_PARENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1891 | dentry = fhp->fh_dentry; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1892 | dirp = d_inode(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1893 | |
| 1894 | rdentry = lookup_one_len(fname, dentry, flen); |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 1895 | host_err = PTR_ERR(rdentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1896 | if (IS_ERR(rdentry)) |
J. Bruce Fields | 0ca0c9d | 2019-04-12 16:26:30 -0400 | [diff] [blame] | 1897 | goto out_drop_write; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1898 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1899 | if (d_really_is_negative(rdentry)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1900 | dput(rdentry); |
J. Bruce Fields | 0ca0c9d | 2019-04-12 16:26:30 -0400 | [diff] [blame] | 1901 | host_err = -ENOENT; |
| 1902 | goto out_drop_write; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1903 | } |
Yu Hsiang Huang | e5d74a2 | 2021-05-14 11:58:29 +0800 | [diff] [blame] | 1904 | rinode = d_inode(rdentry); |
| 1905 | ihold(rinode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1906 | |
| 1907 | if (!type) |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1908 | type = d_inode(rdentry)->i_mode & S_IFMT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1909 | |
Jeff Layton | 7775ec5 | 2019-08-18 14:18:57 -0400 | [diff] [blame] | 1910 | if (type != S_IFDIR) { |
Jeff Layton | 7f84b48 | 2020-11-30 17:03:16 -0500 | [diff] [blame] | 1911 | if (rdentry->d_sb->s_export_op->flags & EXPORT_OP_CLOSE_BEFORE_UNLINK) |
| 1912 | nfsd_close_cached_files(rdentry); |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 1913 | host_err = vfs_unlink(&init_user_ns, dirp, rdentry, NULL); |
Jeff Layton | 7775ec5 | 2019-08-18 14:18:57 -0400 | [diff] [blame] | 1914 | } else { |
Christian Brauner | 6521f89 | 2021-01-21 14:19:33 +0100 | [diff] [blame] | 1915 | host_err = vfs_rmdir(&init_user_ns, dirp, rdentry); |
Jeff Layton | 7775ec5 | 2019-08-18 14:18:57 -0400 | [diff] [blame] | 1916 | } |
| 1917 | |
J. Bruce Fields | eeeadbb | 2021-05-14 18:21:37 -0400 | [diff] [blame] | 1918 | fh_unlock(fhp); |
J. Bruce Fields | 541ce98 | 2011-01-14 20:00:02 -0500 | [diff] [blame] | 1919 | if (!host_err) |
| 1920 | host_err = commit_metadata(fhp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1921 | dput(rdentry); |
Yu Hsiang Huang | e5d74a2 | 2021-05-14 11:58:29 +0800 | [diff] [blame] | 1922 | iput(rinode); /* truncate the inode here */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1923 | |
J. Bruce Fields | 0ca0c9d | 2019-04-12 16:26:30 -0400 | [diff] [blame] | 1924 | out_drop_write: |
| 1925 | fh_drop_write(fhp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1926 | out_nfserr: |
NeilBrown | 466e16f | 2019-11-28 13:56:43 +1100 | [diff] [blame] | 1927 | if (host_err == -EBUSY) { |
| 1928 | /* name is mounted-on. There is no perfect |
| 1929 | * error status. |
| 1930 | */ |
| 1931 | if (nfsd_v4client(rqstp)) |
| 1932 | err = nfserr_file_open; |
| 1933 | else |
| 1934 | err = nfserr_acces; |
| 1935 | } else { |
| 1936 | err = nfserrno(host_err); |
| 1937 | } |
YAMAMOTO Takashi | f193fba | 2006-01-18 17:43:13 -0800 | [diff] [blame] | 1938 | out: |
| 1939 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1940 | } |
| 1941 | |
| 1942 | /* |
David Woodhouse | 14f7dd6 | 2008-07-31 20:29:12 +0100 | [diff] [blame] | 1943 | * We do this buffering because we must not call back into the file |
| 1944 | * system's ->lookup() method from the filldir callback. That may well |
| 1945 | * deadlock a number of file systems. |
| 1946 | * |
| 1947 | * This is based heavily on the implementation of same in XFS. |
| 1948 | */ |
| 1949 | struct buffered_dirent { |
| 1950 | u64 ino; |
| 1951 | loff_t offset; |
| 1952 | int namlen; |
| 1953 | unsigned int d_type; |
| 1954 | char name[]; |
| 1955 | }; |
| 1956 | |
| 1957 | struct readdir_data { |
Al Viro | 5c0ba4e | 2013-05-15 13:52:59 -0400 | [diff] [blame] | 1958 | struct dir_context ctx; |
David Woodhouse | 14f7dd6 | 2008-07-31 20:29:12 +0100 | [diff] [blame] | 1959 | char *dirent; |
| 1960 | size_t used; |
Al Viro | 53c9c5c | 2008-08-24 07:29:52 -0400 | [diff] [blame] | 1961 | int full; |
David Woodhouse | 14f7dd6 | 2008-07-31 20:29:12 +0100 | [diff] [blame] | 1962 | }; |
| 1963 | |
Miklos Szeredi | ac7576f | 2014-10-30 17:37:34 +0100 | [diff] [blame] | 1964 | static int nfsd_buffered_filldir(struct dir_context *ctx, const char *name, |
| 1965 | int namlen, loff_t offset, u64 ino, |
| 1966 | unsigned int d_type) |
David Woodhouse | 2628b76 | 2008-07-31 17:16:51 +0100 | [diff] [blame] | 1967 | { |
Miklos Szeredi | ac7576f | 2014-10-30 17:37:34 +0100 | [diff] [blame] | 1968 | struct readdir_data *buf = |
| 1969 | container_of(ctx, struct readdir_data, ctx); |
David Woodhouse | 14f7dd6 | 2008-07-31 20:29:12 +0100 | [diff] [blame] | 1970 | struct buffered_dirent *de = (void *)(buf->dirent + buf->used); |
| 1971 | unsigned int reclen; |
| 1972 | |
| 1973 | reclen = ALIGN(sizeof(struct buffered_dirent) + namlen, sizeof(u64)); |
Al Viro | 53c9c5c | 2008-08-24 07:29:52 -0400 | [diff] [blame] | 1974 | if (buf->used + reclen > PAGE_SIZE) { |
| 1975 | buf->full = 1; |
David Woodhouse | 14f7dd6 | 2008-07-31 20:29:12 +0100 | [diff] [blame] | 1976 | return -EINVAL; |
Al Viro | 53c9c5c | 2008-08-24 07:29:52 -0400 | [diff] [blame] | 1977 | } |
David Woodhouse | 14f7dd6 | 2008-07-31 20:29:12 +0100 | [diff] [blame] | 1978 | |
| 1979 | de->namlen = namlen; |
| 1980 | de->offset = offset; |
| 1981 | de->ino = ino; |
| 1982 | de->d_type = d_type; |
| 1983 | memcpy(de->name, name, namlen); |
| 1984 | buf->used += reclen; |
| 1985 | |
| 1986 | return 0; |
| 1987 | } |
| 1988 | |
Chuck Lever | 6019ce07 | 2021-03-05 13:57:40 -0500 | [diff] [blame] | 1989 | static __be32 nfsd_buffered_readdir(struct file *file, struct svc_fh *fhp, |
| 1990 | nfsd_filldir_t func, struct readdir_cd *cdp, |
| 1991 | loff_t *offsetp) |
David Woodhouse | 14f7dd6 | 2008-07-31 20:29:12 +0100 | [diff] [blame] | 1992 | { |
David Woodhouse | 14f7dd6 | 2008-07-31 20:29:12 +0100 | [diff] [blame] | 1993 | struct buffered_dirent *de; |
David Woodhouse | 2628b76 | 2008-07-31 17:16:51 +0100 | [diff] [blame] | 1994 | int host_err; |
David Woodhouse | 14f7dd6 | 2008-07-31 20:29:12 +0100 | [diff] [blame] | 1995 | int size; |
| 1996 | loff_t offset; |
Al Viro | ac6614b | 2013-05-22 22:22:04 -0400 | [diff] [blame] | 1997 | struct readdir_data buf = { |
| 1998 | .ctx.actor = nfsd_buffered_filldir, |
| 1999 | .dirent = (void *)__get_free_page(GFP_KERNEL) |
| 2000 | }; |
David Woodhouse | 2628b76 | 2008-07-31 17:16:51 +0100 | [diff] [blame] | 2001 | |
David Woodhouse | 14f7dd6 | 2008-07-31 20:29:12 +0100 | [diff] [blame] | 2002 | if (!buf.dirent) |
David Woodhouse | 2f9092e | 2009-04-20 23:18:37 +0100 | [diff] [blame] | 2003 | return nfserrno(-ENOMEM); |
David Woodhouse | 2628b76 | 2008-07-31 17:16:51 +0100 | [diff] [blame] | 2004 | |
David Woodhouse | 14f7dd6 | 2008-07-31 20:29:12 +0100 | [diff] [blame] | 2005 | offset = *offsetp; |
David Woodhouse | 14f7dd6 | 2008-07-31 20:29:12 +0100 | [diff] [blame] | 2006 | |
| 2007 | while (1) { |
| 2008 | unsigned int reclen; |
| 2009 | |
Doug Nazar | b726e92 | 2008-11-05 06:16:28 -0500 | [diff] [blame] | 2010 | cdp->err = nfserr_eof; /* will be cleared on successful read */ |
David Woodhouse | 14f7dd6 | 2008-07-31 20:29:12 +0100 | [diff] [blame] | 2011 | buf.used = 0; |
Al Viro | 53c9c5c | 2008-08-24 07:29:52 -0400 | [diff] [blame] | 2012 | buf.full = 0; |
David Woodhouse | 14f7dd6 | 2008-07-31 20:29:12 +0100 | [diff] [blame] | 2013 | |
Al Viro | 5c0ba4e | 2013-05-15 13:52:59 -0400 | [diff] [blame] | 2014 | host_err = iterate_dir(file, &buf.ctx); |
Al Viro | 53c9c5c | 2008-08-24 07:29:52 -0400 | [diff] [blame] | 2015 | if (buf.full) |
| 2016 | host_err = 0; |
| 2017 | |
| 2018 | if (host_err < 0) |
David Woodhouse | 14f7dd6 | 2008-07-31 20:29:12 +0100 | [diff] [blame] | 2019 | break; |
| 2020 | |
| 2021 | size = buf.used; |
| 2022 | |
| 2023 | if (!size) |
| 2024 | break; |
| 2025 | |
David Woodhouse | 14f7dd6 | 2008-07-31 20:29:12 +0100 | [diff] [blame] | 2026 | de = (struct buffered_dirent *)buf.dirent; |
| 2027 | while (size > 0) { |
| 2028 | offset = de->offset; |
| 2029 | |
| 2030 | if (func(cdp, de->name, de->namlen, de->offset, |
| 2031 | de->ino, de->d_type)) |
David Woodhouse | 2f9092e | 2009-04-20 23:18:37 +0100 | [diff] [blame] | 2032 | break; |
David Woodhouse | 14f7dd6 | 2008-07-31 20:29:12 +0100 | [diff] [blame] | 2033 | |
| 2034 | if (cdp->err != nfs_ok) |
David Woodhouse | 2f9092e | 2009-04-20 23:18:37 +0100 | [diff] [blame] | 2035 | break; |
David Woodhouse | 14f7dd6 | 2008-07-31 20:29:12 +0100 | [diff] [blame] | 2036 | |
Chuck Lever | 6019ce07 | 2021-03-05 13:57:40 -0500 | [diff] [blame] | 2037 | trace_nfsd_dirent(fhp, de->ino, de->name, de->namlen); |
| 2038 | |
David Woodhouse | 14f7dd6 | 2008-07-31 20:29:12 +0100 | [diff] [blame] | 2039 | reclen = ALIGN(sizeof(*de) + de->namlen, |
| 2040 | sizeof(u64)); |
| 2041 | size -= reclen; |
| 2042 | de = (struct buffered_dirent *)((char *)de + reclen); |
| 2043 | } |
David Woodhouse | 2f9092e | 2009-04-20 23:18:37 +0100 | [diff] [blame] | 2044 | if (size > 0) /* We bailed out early */ |
| 2045 | break; |
| 2046 | |
David Woodhouse | c002a6c | 2008-08-17 17:21:18 +0100 | [diff] [blame] | 2047 | offset = vfs_llseek(file, 0, SEEK_CUR); |
David Woodhouse | 14f7dd6 | 2008-07-31 20:29:12 +0100 | [diff] [blame] | 2048 | } |
| 2049 | |
David Woodhouse | 14f7dd6 | 2008-07-31 20:29:12 +0100 | [diff] [blame] | 2050 | free_page((unsigned long)(buf.dirent)); |
David Woodhouse | 2628b76 | 2008-07-31 17:16:51 +0100 | [diff] [blame] | 2051 | |
| 2052 | if (host_err) |
| 2053 | return nfserrno(host_err); |
David Woodhouse | 14f7dd6 | 2008-07-31 20:29:12 +0100 | [diff] [blame] | 2054 | |
| 2055 | *offsetp = offset; |
| 2056 | return cdp->err; |
David Woodhouse | 2628b76 | 2008-07-31 17:16:51 +0100 | [diff] [blame] | 2057 | } |
| 2058 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2059 | /* |
| 2060 | * Read entries from a directory. |
| 2061 | * The NFSv3/4 verifier we ignore for now. |
| 2062 | */ |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 2063 | __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2064 | nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp, |
Miklos Szeredi | ac7576f | 2014-10-30 17:37:34 +0100 | [diff] [blame] | 2065 | struct readdir_cd *cdp, nfsd_filldir_t func) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2066 | { |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 2067 | __be32 err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2068 | struct file *file; |
| 2069 | loff_t offset = *offsetp; |
Bernd Schubert | 06effdb | 2012-03-18 22:44:50 -0400 | [diff] [blame] | 2070 | int may_flags = NFSD_MAY_READ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2071 | |
Bernd Schubert | 06effdb | 2012-03-18 22:44:50 -0400 | [diff] [blame] | 2072 | /* NFSv2 only supports 32 bit cookies */ |
| 2073 | if (rqstp->rq_vers > 2) |
| 2074 | may_flags |= NFSD_MAY_64BIT_COOKIE; |
| 2075 | |
| 2076 | err = nfsd_open(rqstp, fhp, S_IFDIR, may_flags, &file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2077 | if (err) |
| 2078 | goto out; |
| 2079 | |
Jeff Layton | b108fe6 | 2012-04-25 15:30:00 -0400 | [diff] [blame] | 2080 | offset = vfs_llseek(file, offset, SEEK_SET); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2081 | if (offset < 0) { |
| 2082 | err = nfserrno((int)offset); |
| 2083 | goto out_close; |
| 2084 | } |
| 2085 | |
Chuck Lever | 6019ce07 | 2021-03-05 13:57:40 -0500 | [diff] [blame] | 2086 | err = nfsd_buffered_readdir(file, fhp, func, cdp, offsetp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2087 | |
| 2088 | if (err == nfserr_eof || err == nfserr_toosmall) |
| 2089 | err = nfs_ok; /* can still be found in ->err */ |
| 2090 | out_close: |
Christoph Hellwig | fd89145 | 2015-04-28 15:41:16 +0200 | [diff] [blame] | 2091 | fput(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2092 | out: |
| 2093 | return err; |
| 2094 | } |
| 2095 | |
| 2096 | /* |
| 2097 | * Get file system stats |
| 2098 | * N.B. After this call fhp needs an fh_put |
| 2099 | */ |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 2100 | __be32 |
J. Bruce Fields | 04716e6 | 2008-08-07 13:00:20 -0400 | [diff] [blame] | 2101 | nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat, int access) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2102 | { |
Christoph Hellwig | ebabe9a | 2010-07-07 18:53:11 +0200 | [diff] [blame] | 2103 | __be32 err; |
| 2104 | |
| 2105 | err = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP | access); |
Takashi Iwai | f6360ef | 2010-08-13 15:53:49 +0200 | [diff] [blame] | 2106 | if (!err) { |
| 2107 | struct path path = { |
| 2108 | .mnt = fhp->fh_export->ex_path.mnt, |
| 2109 | .dentry = fhp->fh_dentry, |
| 2110 | }; |
| 2111 | if (vfs_statfs(&path, stat)) |
| 2112 | err = nfserr_io; |
| 2113 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2114 | return err; |
| 2115 | } |
| 2116 | |
J. Bruce Fields | c7d5140 | 2007-07-19 01:49:20 -0700 | [diff] [blame] | 2117 | static int exp_rdonly(struct svc_rqst *rqstp, struct svc_export *exp) |
J. Bruce Fields | e22841c | 2007-07-19 01:49:20 -0700 | [diff] [blame] | 2118 | { |
J. Bruce Fields | c7d5140 | 2007-07-19 01:49:20 -0700 | [diff] [blame] | 2119 | return nfsexp_flags(rqstp, exp) & NFSEXP_READONLY; |
J. Bruce Fields | e22841c | 2007-07-19 01:49:20 -0700 | [diff] [blame] | 2120 | } |
| 2121 | |
Frank van der Linden | 3211944 | 2020-06-23 22:39:23 +0000 | [diff] [blame] | 2122 | #ifdef CONFIG_NFSD_V4 |
| 2123 | /* |
| 2124 | * Helper function to translate error numbers. In the case of xattr operations, |
| 2125 | * some error codes need to be translated outside of the standard translations. |
| 2126 | * |
| 2127 | * ENODATA needs to be translated to nfserr_noxattr. |
| 2128 | * E2BIG to nfserr_xattr2big. |
| 2129 | * |
| 2130 | * Additionally, vfs_listxattr can return -ERANGE. This means that the |
| 2131 | * file has too many extended attributes to retrieve inside an |
| 2132 | * XATTR_LIST_MAX sized buffer. This is a bug in the xattr implementation: |
| 2133 | * filesystems will allow the adding of extended attributes until they hit |
| 2134 | * their own internal limit. This limit may be larger than XATTR_LIST_MAX. |
| 2135 | * So, at that point, the attributes are present and valid, but can't |
| 2136 | * be retrieved using listxattr, since the upper level xattr code enforces |
| 2137 | * the XATTR_LIST_MAX limit. |
| 2138 | * |
| 2139 | * This bug means that we need to deal with listxattr returning -ERANGE. The |
| 2140 | * best mapping is to return TOOSMALL. |
| 2141 | */ |
| 2142 | static __be32 |
| 2143 | nfsd_xattr_errno(int err) |
| 2144 | { |
| 2145 | switch (err) { |
| 2146 | case -ENODATA: |
| 2147 | return nfserr_noxattr; |
| 2148 | case -E2BIG: |
| 2149 | return nfserr_xattr2big; |
| 2150 | case -ERANGE: |
| 2151 | return nfserr_toosmall; |
| 2152 | } |
| 2153 | return nfserrno(err); |
| 2154 | } |
| 2155 | |
| 2156 | /* |
| 2157 | * Retrieve the specified user extended attribute. To avoid always |
| 2158 | * having to allocate the maximum size (since we are not getting |
| 2159 | * a maximum size from the RPC), do a probe + alloc. Hold a reader |
| 2160 | * lock on i_rwsem to prevent the extended attribute from changing |
| 2161 | * size while we're doing this. |
| 2162 | */ |
| 2163 | __be32 |
| 2164 | nfsd_getxattr(struct svc_rqst *rqstp, struct svc_fh *fhp, char *name, |
| 2165 | void **bufp, int *lenp) |
| 2166 | { |
| 2167 | ssize_t len; |
| 2168 | __be32 err; |
| 2169 | char *buf; |
| 2170 | struct inode *inode; |
| 2171 | struct dentry *dentry; |
| 2172 | |
| 2173 | err = fh_verify(rqstp, fhp, 0, NFSD_MAY_READ); |
| 2174 | if (err) |
| 2175 | return err; |
| 2176 | |
| 2177 | err = nfs_ok; |
| 2178 | dentry = fhp->fh_dentry; |
| 2179 | inode = d_inode(dentry); |
| 2180 | |
| 2181 | inode_lock_shared(inode); |
| 2182 | |
Tycho Andersen | c7c7a1a1 | 2021-01-21 14:19:28 +0100 | [diff] [blame] | 2183 | len = vfs_getxattr(&init_user_ns, dentry, name, NULL, 0); |
Frank van der Linden | 3211944 | 2020-06-23 22:39:23 +0000 | [diff] [blame] | 2184 | |
| 2185 | /* |
| 2186 | * Zero-length attribute, just return. |
| 2187 | */ |
| 2188 | if (len == 0) { |
| 2189 | *bufp = NULL; |
| 2190 | *lenp = 0; |
| 2191 | goto out; |
| 2192 | } |
| 2193 | |
| 2194 | if (len < 0) { |
| 2195 | err = nfsd_xattr_errno(len); |
| 2196 | goto out; |
| 2197 | } |
| 2198 | |
| 2199 | if (len > *lenp) { |
| 2200 | err = nfserr_toosmall; |
| 2201 | goto out; |
| 2202 | } |
| 2203 | |
| 2204 | buf = kvmalloc(len, GFP_KERNEL | GFP_NOFS); |
| 2205 | if (buf == NULL) { |
| 2206 | err = nfserr_jukebox; |
| 2207 | goto out; |
| 2208 | } |
| 2209 | |
Tycho Andersen | c7c7a1a1 | 2021-01-21 14:19:28 +0100 | [diff] [blame] | 2210 | len = vfs_getxattr(&init_user_ns, dentry, name, buf, len); |
Frank van der Linden | 3211944 | 2020-06-23 22:39:23 +0000 | [diff] [blame] | 2211 | if (len <= 0) { |
| 2212 | kvfree(buf); |
| 2213 | buf = NULL; |
| 2214 | err = nfsd_xattr_errno(len); |
| 2215 | } |
| 2216 | |
| 2217 | *lenp = len; |
| 2218 | *bufp = buf; |
| 2219 | |
| 2220 | out: |
| 2221 | inode_unlock_shared(inode); |
| 2222 | |
| 2223 | return err; |
| 2224 | } |
| 2225 | |
| 2226 | /* |
| 2227 | * Retrieve the xattr names. Since we can't know how many are |
| 2228 | * user extended attributes, we must get all attributes here, |
| 2229 | * and have the XDR encode filter out the "user." ones. |
| 2230 | * |
| 2231 | * While this could always just allocate an XATTR_LIST_MAX |
| 2232 | * buffer, that's a waste, so do a probe + allocate. To |
| 2233 | * avoid any changes between the probe and allocate, wrap |
| 2234 | * this in inode_lock. |
| 2235 | */ |
| 2236 | __be32 |
| 2237 | nfsd_listxattr(struct svc_rqst *rqstp, struct svc_fh *fhp, char **bufp, |
| 2238 | int *lenp) |
| 2239 | { |
| 2240 | ssize_t len; |
| 2241 | __be32 err; |
| 2242 | char *buf; |
| 2243 | struct inode *inode; |
| 2244 | struct dentry *dentry; |
| 2245 | |
| 2246 | err = fh_verify(rqstp, fhp, 0, NFSD_MAY_READ); |
| 2247 | if (err) |
| 2248 | return err; |
| 2249 | |
| 2250 | dentry = fhp->fh_dentry; |
| 2251 | inode = d_inode(dentry); |
| 2252 | *lenp = 0; |
| 2253 | |
| 2254 | inode_lock_shared(inode); |
| 2255 | |
| 2256 | len = vfs_listxattr(dentry, NULL, 0); |
| 2257 | if (len <= 0) { |
| 2258 | err = nfsd_xattr_errno(len); |
| 2259 | goto out; |
| 2260 | } |
| 2261 | |
| 2262 | if (len > XATTR_LIST_MAX) { |
| 2263 | err = nfserr_xattr2big; |
| 2264 | goto out; |
| 2265 | } |
| 2266 | |
| 2267 | /* |
| 2268 | * We're holding i_rwsem - use GFP_NOFS. |
| 2269 | */ |
| 2270 | buf = kvmalloc(len, GFP_KERNEL | GFP_NOFS); |
| 2271 | if (buf == NULL) { |
| 2272 | err = nfserr_jukebox; |
| 2273 | goto out; |
| 2274 | } |
| 2275 | |
| 2276 | len = vfs_listxattr(dentry, buf, len); |
| 2277 | if (len <= 0) { |
| 2278 | kvfree(buf); |
| 2279 | err = nfsd_xattr_errno(len); |
| 2280 | goto out; |
| 2281 | } |
| 2282 | |
| 2283 | *lenp = len; |
| 2284 | *bufp = buf; |
| 2285 | |
| 2286 | err = nfs_ok; |
| 2287 | out: |
| 2288 | inode_unlock_shared(inode); |
| 2289 | |
| 2290 | return err; |
| 2291 | } |
| 2292 | |
| 2293 | /* |
| 2294 | * Removexattr and setxattr need to call fh_lock to both lock the inode |
| 2295 | * and set the change attribute. Since the top-level vfs_removexattr |
| 2296 | * and vfs_setxattr calls already do their own inode_lock calls, call |
| 2297 | * the _locked variant. Pass in a NULL pointer for delegated_inode, |
| 2298 | * and let the client deal with NFS4ERR_DELAY (same as with e.g. |
| 2299 | * setattr and remove). |
| 2300 | */ |
| 2301 | __be32 |
| 2302 | nfsd_removexattr(struct svc_rqst *rqstp, struct svc_fh *fhp, char *name) |
| 2303 | { |
Chuck Lever | 8237284 | 2020-09-11 14:47:48 -0400 | [diff] [blame] | 2304 | __be32 err; |
| 2305 | int ret; |
Frank van der Linden | 3211944 | 2020-06-23 22:39:23 +0000 | [diff] [blame] | 2306 | |
| 2307 | err = fh_verify(rqstp, fhp, 0, NFSD_MAY_WRITE); |
| 2308 | if (err) |
| 2309 | return err; |
| 2310 | |
| 2311 | ret = fh_want_write(fhp); |
| 2312 | if (ret) |
| 2313 | return nfserrno(ret); |
| 2314 | |
| 2315 | fh_lock(fhp); |
| 2316 | |
Tycho Andersen | c7c7a1a1 | 2021-01-21 14:19:28 +0100 | [diff] [blame] | 2317 | ret = __vfs_removexattr_locked(&init_user_ns, fhp->fh_dentry, |
| 2318 | name, NULL); |
Frank van der Linden | 3211944 | 2020-06-23 22:39:23 +0000 | [diff] [blame] | 2319 | |
| 2320 | fh_unlock(fhp); |
| 2321 | fh_drop_write(fhp); |
| 2322 | |
| 2323 | return nfsd_xattr_errno(ret); |
| 2324 | } |
| 2325 | |
| 2326 | __be32 |
| 2327 | nfsd_setxattr(struct svc_rqst *rqstp, struct svc_fh *fhp, char *name, |
| 2328 | void *buf, u32 len, u32 flags) |
| 2329 | { |
Chuck Lever | 8237284 | 2020-09-11 14:47:48 -0400 | [diff] [blame] | 2330 | __be32 err; |
| 2331 | int ret; |
Frank van der Linden | 3211944 | 2020-06-23 22:39:23 +0000 | [diff] [blame] | 2332 | |
| 2333 | err = fh_verify(rqstp, fhp, 0, NFSD_MAY_WRITE); |
| 2334 | if (err) |
| 2335 | return err; |
| 2336 | |
| 2337 | ret = fh_want_write(fhp); |
| 2338 | if (ret) |
| 2339 | return nfserrno(ret); |
| 2340 | fh_lock(fhp); |
| 2341 | |
Tycho Andersen | c7c7a1a1 | 2021-01-21 14:19:28 +0100 | [diff] [blame] | 2342 | ret = __vfs_setxattr_locked(&init_user_ns, fhp->fh_dentry, name, buf, |
| 2343 | len, flags, NULL); |
Frank van der Linden | 3211944 | 2020-06-23 22:39:23 +0000 | [diff] [blame] | 2344 | |
| 2345 | fh_unlock(fhp); |
| 2346 | fh_drop_write(fhp); |
| 2347 | |
| 2348 | return nfsd_xattr_errno(ret); |
| 2349 | } |
| 2350 | #endif |
| 2351 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2352 | /* |
| 2353 | * Check for a user's access permissions to this inode. |
| 2354 | */ |
Al Viro | 6264d69 | 2006-10-19 23:28:58 -0700 | [diff] [blame] | 2355 | __be32 |
J. Bruce Fields | 0ec757d | 2007-07-17 04:04:48 -0700 | [diff] [blame] | 2356 | nfsd_permission(struct svc_rqst *rqstp, struct svc_export *exp, |
| 2357 | struct dentry *dentry, int acc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2358 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 2359 | struct inode *inode = d_inode(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2360 | int err; |
| 2361 | |
J. Bruce Fields | aea9339 | 2011-04-10 10:35:12 -0400 | [diff] [blame] | 2362 | if ((acc & NFSD_MAY_MASK) == NFSD_MAY_NOP) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2363 | return 0; |
| 2364 | #if 0 |
| 2365 | dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n", |
| 2366 | acc, |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 2367 | (acc & NFSD_MAY_READ)? " read" : "", |
| 2368 | (acc & NFSD_MAY_WRITE)? " write" : "", |
| 2369 | (acc & NFSD_MAY_EXEC)? " exec" : "", |
| 2370 | (acc & NFSD_MAY_SATTR)? " sattr" : "", |
| 2371 | (acc & NFSD_MAY_TRUNC)? " trunc" : "", |
| 2372 | (acc & NFSD_MAY_LOCK)? " lock" : "", |
| 2373 | (acc & NFSD_MAY_OWNER_OVERRIDE)? " owneroverride" : "", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2374 | inode->i_mode, |
| 2375 | IS_IMMUTABLE(inode)? " immut" : "", |
| 2376 | IS_APPEND(inode)? " append" : "", |
Dave Hansen | 2c463e9 | 2008-02-15 14:37:56 -0800 | [diff] [blame] | 2377 | __mnt_is_readonly(exp->ex_path.mnt)? " ro" : ""); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2378 | dprintk(" owner %d/%d user %d/%d\n", |
David Howells | 5cc0a84 | 2008-11-14 10:38:58 +1100 | [diff] [blame] | 2379 | inode->i_uid, inode->i_gid, current_fsuid(), current_fsgid()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2380 | #endif |
| 2381 | |
| 2382 | /* Normally we reject any write/sattr etc access on a read-only file |
| 2383 | * system. But if it is IRIX doing check on write-access for a |
| 2384 | * device special file, we ignore rofs. |
| 2385 | */ |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 2386 | if (!(acc & NFSD_MAY_LOCAL_ACCESS)) |
| 2387 | if (acc & (NFSD_MAY_WRITE | NFSD_MAY_SATTR | NFSD_MAY_TRUNC)) { |
Dave Hansen | 2c463e9 | 2008-02-15 14:37:56 -0800 | [diff] [blame] | 2388 | if (exp_rdonly(rqstp, exp) || |
| 2389 | __mnt_is_readonly(exp->ex_path.mnt)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2390 | return nfserr_rofs; |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 2391 | if (/* (acc & NFSD_MAY_WRITE) && */ IS_IMMUTABLE(inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2392 | return nfserr_perm; |
| 2393 | } |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 2394 | if ((acc & NFSD_MAY_TRUNC) && IS_APPEND(inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2395 | return nfserr_perm; |
| 2396 | |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 2397 | if (acc & NFSD_MAY_LOCK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2398 | /* If we cannot rely on authentication in NLM requests, |
| 2399 | * just allow locks, otherwise require read permission, or |
| 2400 | * ownership |
| 2401 | */ |
| 2402 | if (exp->ex_flags & NFSEXP_NOAUTHNLM) |
| 2403 | return 0; |
| 2404 | else |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 2405 | acc = NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2406 | } |
| 2407 | /* |
| 2408 | * The file owner always gets access permission for accesses that |
| 2409 | * would normally be checked at open time. This is to make |
| 2410 | * file access work even when the client has done a fchmod(fd, 0). |
| 2411 | * |
| 2412 | * However, `cp foo bar' should fail nevertheless when bar is |
| 2413 | * readonly. A sensible way to do this might be to reject all |
| 2414 | * attempts to truncate a read-only file, because a creat() call |
| 2415 | * always implies file truncation. |
| 2416 | * ... but this isn't really fair. A process may reasonably call |
| 2417 | * ftruncate on an open file descriptor on a file with perm 000. |
| 2418 | * We must trust the client to do permission checking - using "ACCESS" |
| 2419 | * with NFSv3. |
| 2420 | */ |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 2421 | if ((acc & NFSD_MAY_OWNER_OVERRIDE) && |
Eric W. Biederman | 6fab877 | 2013-02-02 06:53:11 -0800 | [diff] [blame] | 2422 | uid_eq(inode->i_uid, current_fsuid())) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2423 | return 0; |
| 2424 | |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 2425 | /* This assumes NFSD_MAY_{READ,WRITE,EXEC} == MAY_{READ,WRITE,EXEC} */ |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 2426 | err = inode_permission(&init_user_ns, inode, |
| 2427 | acc & (MAY_READ | MAY_WRITE | MAY_EXEC)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2428 | |
| 2429 | /* Allow read access to binaries even when mode 111 */ |
| 2430 | if (err == -EACCES && S_ISREG(inode->i_mode) && |
J. Bruce Fields | a043226 | 2011-08-25 10:48:39 -0400 | [diff] [blame] | 2431 | (acc == (NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE) || |
| 2432 | acc == (NFSD_MAY_READ | NFSD_MAY_READ_IF_EXEC))) |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 2433 | err = inode_permission(&init_user_ns, inode, MAY_EXEC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2434 | |
| 2435 | return err? nfserrno(err) : 0; |
| 2436 | } |