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 | * XDR support for nfsd/protocol version 3. |
| 4 | * |
| 5 | * Copyright (C) 1995, 1996, 1997 Olaf Kirch <okir@monad.swb.de> |
| 6 | * |
| 7 | * 2003-08-09 Jamie Lokier: Use htonl() for nanoseconds, not htons()! |
| 8 | */ |
| 9 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/namei.h> |
Stanislav Kinsbursky | b9c0ef8 | 2012-12-06 14:23:19 +0300 | [diff] [blame] | 11 | #include <linux/sunrpc/svc_xprt.h> |
Boaz Harrosh | 9a74af2 | 2009-12-03 20:30:56 +0200 | [diff] [blame] | 12 | #include "xdr3.h" |
J. Bruce Fields | 2e8138a | 2007-11-15 17:05:43 -0500 | [diff] [blame] | 13 | #include "auth.h" |
Stanislav Kinsbursky | b9c0ef8 | 2012-12-06 14:23:19 +0300 | [diff] [blame] | 14 | #include "netns.h" |
Al Viro | 3dadecc | 2013-01-24 02:18:08 -0500 | [diff] [blame] | 15 | #include "vfs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | |
| 17 | #define NFSDDBG_FACILITY NFSDDBG_XDR |
| 18 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
| 20 | /* |
Chuck Lever | 8b70449 | 2020-11-06 13:08:45 -0500 | [diff] [blame] | 21 | * Force construction of an empty post-op attr |
| 22 | */ |
| 23 | static const struct svc_fh nfs3svc_null_fh = { |
| 24 | .fh_no_wcc = true, |
| 25 | }; |
| 26 | |
| 27 | /* |
Chuck Lever | 0a139d1 | 2020-10-22 13:42:13 -0400 | [diff] [blame] | 28 | * time_delta. {1, 0} means the server is accurate only |
| 29 | * to the nearest second. |
| 30 | */ |
| 31 | static const struct timespec64 nfs3svc_time_delta = { |
| 32 | .tv_sec = 1, |
| 33 | .tv_nsec = 0, |
| 34 | }; |
| 35 | |
| 36 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | * Mapping of S_IF* types to NFS file types |
| 38 | */ |
Chuck Lever | 2c42f80 | 2020-10-21 11:58:41 -0400 | [diff] [blame] | 39 | static const u32 nfs3_ftypes[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | NF3NON, NF3FIFO, NF3CHR, NF3BAD, |
| 41 | NF3DIR, NF3BAD, NF3BLK, NF3BAD, |
| 42 | NF3REG, NF3BAD, NF3LNK, NF3BAD, |
| 43 | NF3SOCK, NF3BAD, NF3LNK, NF3BAD, |
| 44 | }; |
| 45 | |
Trond Myklebust | 27c438f | 2019-09-02 13:02:56 -0400 | [diff] [blame] | 46 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | /* |
Chuck Lever | 9575363 | 2020-10-20 14:30:02 -0400 | [diff] [blame] | 48 | * Basic NFSv3 data types (RFC 1813 Sections 2.5 and 2.6) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | */ |
Chuck Lever | 9575363 | 2020-10-20 14:30:02 -0400 | [diff] [blame] | 50 | |
Adrian Bunk | 3ee6f61 | 2006-12-06 20:40:23 -0800 | [diff] [blame] | 51 | static __be32 * |
Arnd Bergmann | 92c5e46 | 2019-10-31 14:55:32 +0100 | [diff] [blame] | 52 | encode_time3(__be32 *p, struct timespec64 *time) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | { |
| 54 | *p++ = htonl((u32) time->tv_sec); *p++ = htonl(time->tv_nsec); |
| 55 | return p; |
| 56 | } |
| 57 | |
Chuck Lever | 2c42f80 | 2020-10-21 11:58:41 -0400 | [diff] [blame] | 58 | static __be32 * |
| 59 | encode_nfstime3(__be32 *p, const struct timespec64 *time) |
| 60 | { |
| 61 | *p++ = cpu_to_be32((u32)time->tv_sec); |
| 62 | *p++ = cpu_to_be32(time->tv_nsec); |
| 63 | |
| 64 | return p; |
| 65 | } |
| 66 | |
Chuck Lever | 9cde936 | 2020-10-20 15:48:22 -0400 | [diff] [blame] | 67 | static bool |
| 68 | svcxdr_decode_nfstime3(struct xdr_stream *xdr, struct timespec64 *timep) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | { |
Chuck Lever | 9cde936 | 2020-10-20 15:48:22 -0400 | [diff] [blame] | 70 | __be32 *p; |
| 71 | |
| 72 | p = xdr_inline_decode(xdr, XDR_UNIT * 2); |
| 73 | if (!p) |
| 74 | return false; |
| 75 | timep->tv_sec = be32_to_cpup(p++); |
| 76 | timep->tv_nsec = be32_to_cpup(p); |
| 77 | |
| 78 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Chuck Lever | 05027ea | 2020-11-17 11:52:04 -0500 | [diff] [blame] | 81 | /** |
| 82 | * svcxdr_decode_nfs_fh3 - Decode an NFSv3 file handle |
| 83 | * @xdr: XDR stream positioned at an undecoded NFSv3 FH |
| 84 | * @fhp: OUT: filled-in server file handle |
| 85 | * |
| 86 | * Return values: |
| 87 | * %false: The encoded file handle was not valid |
| 88 | * %true: @fhp has been initialized |
| 89 | */ |
| 90 | bool |
Chuck Lever | 9575363 | 2020-10-20 14:30:02 -0400 | [diff] [blame] | 91 | svcxdr_decode_nfs_fh3(struct xdr_stream *xdr, struct svc_fh *fhp) |
| 92 | { |
| 93 | __be32 *p; |
| 94 | u32 size; |
| 95 | |
| 96 | if (xdr_stream_decode_u32(xdr, &size) < 0) |
| 97 | return false; |
| 98 | if (size == 0 || size > NFS3_FHSIZE) |
| 99 | return false; |
| 100 | p = xdr_inline_decode(xdr, size); |
| 101 | if (!p) |
| 102 | return false; |
| 103 | fh_init(fhp, NFS3_FHSIZE); |
| 104 | fhp->fh_handle.fh_size = size; |
| 105 | memcpy(&fhp->fh_handle.fh_base, p, size); |
| 106 | |
| 107 | return true; |
| 108 | } |
| 109 | |
Chuck Lever | 2c42f80 | 2020-10-21 11:58:41 -0400 | [diff] [blame] | 110 | static bool |
| 111 | svcxdr_encode_nfsstat3(struct xdr_stream *xdr, __be32 status) |
| 112 | { |
| 113 | __be32 *p; |
| 114 | |
| 115 | p = xdr_reserve_space(xdr, sizeof(status)); |
| 116 | if (!p) |
| 117 | return false; |
| 118 | *p = status; |
| 119 | |
| 120 | return true; |
| 121 | } |
| 122 | |
Chuck Lever | 5cf3533 | 2020-10-22 14:46:58 -0400 | [diff] [blame] | 123 | static bool |
| 124 | svcxdr_encode_nfs_fh3(struct xdr_stream *xdr, const struct svc_fh *fhp) |
| 125 | { |
| 126 | u32 size = fhp->fh_handle.fh_size; |
| 127 | __be32 *p; |
| 128 | |
| 129 | p = xdr_reserve_space(xdr, XDR_UNIT + size); |
| 130 | if (!p) |
| 131 | return false; |
| 132 | *p++ = cpu_to_be32(size); |
| 133 | if (size) |
| 134 | p[XDR_QUADLEN(size) - 1] = 0; |
| 135 | memcpy(p, &fhp->fh_handle.fh_base, size); |
| 136 | |
| 137 | return true; |
| 138 | } |
| 139 | |
Chuck Lever | 78315b3 | 2020-10-22 15:27:23 -0400 | [diff] [blame] | 140 | static bool |
| 141 | svcxdr_encode_post_op_fh3(struct xdr_stream *xdr, const struct svc_fh *fhp) |
| 142 | { |
| 143 | if (xdr_stream_encode_item_present(xdr) < 0) |
| 144 | return false; |
| 145 | if (!svcxdr_encode_nfs_fh3(xdr, fhp)) |
| 146 | return false; |
| 147 | |
| 148 | return true; |
| 149 | } |
| 150 | |
Adrian Bunk | 3ee6f61 | 2006-12-06 20:40:23 -0800 | [diff] [blame] | 151 | static __be32 * |
Al Viro | 91f0716 | 2006-10-19 23:28:57 -0700 | [diff] [blame] | 152 | encode_fh(__be32 *p, struct svc_fh *fhp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | { |
| 154 | unsigned int size = fhp->fh_handle.fh_size; |
| 155 | *p++ = htonl(size); |
| 156 | if (size) p[XDR_QUADLEN(size)-1]=0; |
| 157 | memcpy(p, &fhp->fh_handle.fh_base, size); |
| 158 | return p + XDR_QUADLEN(size); |
| 159 | } |
| 160 | |
Chuck Lever | 54d1d43 | 2020-10-20 15:42:33 -0400 | [diff] [blame] | 161 | static bool |
Chuck Lever | ecb7a08 | 2020-10-22 15:26:31 -0400 | [diff] [blame] | 162 | svcxdr_encode_writeverf3(struct xdr_stream *xdr, const __be32 *verf) |
| 163 | { |
| 164 | __be32 *p; |
| 165 | |
| 166 | p = xdr_reserve_space(xdr, NFS3_WRITEVERFSIZE); |
| 167 | if (!p) |
| 168 | return false; |
| 169 | memcpy(p, verf, NFS3_WRITEVERFSIZE); |
| 170 | |
| 171 | return true; |
| 172 | } |
| 173 | |
| 174 | static bool |
Chuck Lever | 54d1d43 | 2020-10-20 15:42:33 -0400 | [diff] [blame] | 175 | svcxdr_decode_filename3(struct xdr_stream *xdr, char **name, unsigned int *len) |
| 176 | { |
| 177 | u32 size, i; |
| 178 | __be32 *p; |
| 179 | char *c; |
| 180 | |
| 181 | if (xdr_stream_decode_u32(xdr, &size) < 0) |
| 182 | return false; |
| 183 | if (size == 0 || size > NFS3_MAXNAMLEN) |
| 184 | return false; |
| 185 | p = xdr_inline_decode(xdr, size); |
| 186 | if (!p) |
| 187 | return false; |
| 188 | |
| 189 | *len = size; |
| 190 | *name = (char *)p; |
| 191 | for (i = 0, c = *name; i < size; i++, c++) { |
| 192 | if (*c == '\0' || *c == '/') |
| 193 | return false; |
| 194 | } |
| 195 | |
| 196 | return true; |
| 197 | } |
| 198 | |
| 199 | static bool |
| 200 | svcxdr_decode_diropargs3(struct xdr_stream *xdr, struct svc_fh *fhp, |
| 201 | char **name, unsigned int *len) |
| 202 | { |
| 203 | return svcxdr_decode_nfs_fh3(xdr, fhp) && |
| 204 | svcxdr_decode_filename3(xdr, name, len); |
| 205 | } |
| 206 | |
Chuck Lever | 9cde936 | 2020-10-20 15:48:22 -0400 | [diff] [blame] | 207 | static bool |
| 208 | svcxdr_decode_sattr3(struct svc_rqst *rqstp, struct xdr_stream *xdr, |
| 209 | struct iattr *iap) |
| 210 | { |
| 211 | u32 set_it; |
| 212 | |
| 213 | iap->ia_valid = 0; |
| 214 | |
| 215 | if (xdr_stream_decode_bool(xdr, &set_it) < 0) |
| 216 | return false; |
| 217 | if (set_it) { |
| 218 | u32 mode; |
| 219 | |
| 220 | if (xdr_stream_decode_u32(xdr, &mode) < 0) |
| 221 | return false; |
| 222 | iap->ia_valid |= ATTR_MODE; |
| 223 | iap->ia_mode = mode; |
| 224 | } |
| 225 | if (xdr_stream_decode_bool(xdr, &set_it) < 0) |
| 226 | return false; |
| 227 | if (set_it) { |
| 228 | u32 uid; |
| 229 | |
| 230 | if (xdr_stream_decode_u32(xdr, &uid) < 0) |
| 231 | return false; |
| 232 | iap->ia_uid = make_kuid(nfsd_user_namespace(rqstp), uid); |
| 233 | if (uid_valid(iap->ia_uid)) |
| 234 | iap->ia_valid |= ATTR_UID; |
| 235 | } |
| 236 | if (xdr_stream_decode_bool(xdr, &set_it) < 0) |
| 237 | return false; |
| 238 | if (set_it) { |
| 239 | u32 gid; |
| 240 | |
| 241 | if (xdr_stream_decode_u32(xdr, &gid) < 0) |
| 242 | return false; |
| 243 | iap->ia_gid = make_kgid(nfsd_user_namespace(rqstp), gid); |
| 244 | if (gid_valid(iap->ia_gid)) |
| 245 | iap->ia_valid |= ATTR_GID; |
| 246 | } |
| 247 | if (xdr_stream_decode_bool(xdr, &set_it) < 0) |
| 248 | return false; |
| 249 | if (set_it) { |
| 250 | u64 newsize; |
| 251 | |
| 252 | if (xdr_stream_decode_u64(xdr, &newsize) < 0) |
| 253 | return false; |
| 254 | iap->ia_valid |= ATTR_SIZE; |
| 255 | iap->ia_size = min_t(u64, newsize, NFS_OFFSET_MAX); |
| 256 | } |
| 257 | if (xdr_stream_decode_u32(xdr, &set_it) < 0) |
| 258 | return false; |
| 259 | switch (set_it) { |
| 260 | case DONT_CHANGE: |
| 261 | break; |
| 262 | case SET_TO_SERVER_TIME: |
| 263 | iap->ia_valid |= ATTR_ATIME; |
| 264 | break; |
| 265 | case SET_TO_CLIENT_TIME: |
| 266 | if (!svcxdr_decode_nfstime3(xdr, &iap->ia_atime)) |
| 267 | return false; |
| 268 | iap->ia_valid |= ATTR_ATIME | ATTR_ATIME_SET; |
| 269 | break; |
| 270 | default: |
| 271 | return false; |
| 272 | } |
| 273 | if (xdr_stream_decode_u32(xdr, &set_it) < 0) |
| 274 | return false; |
| 275 | switch (set_it) { |
| 276 | case DONT_CHANGE: |
| 277 | break; |
| 278 | case SET_TO_SERVER_TIME: |
| 279 | iap->ia_valid |= ATTR_MTIME; |
| 280 | break; |
| 281 | case SET_TO_CLIENT_TIME: |
| 282 | if (!svcxdr_decode_nfstime3(xdr, &iap->ia_mtime)) |
| 283 | return false; |
| 284 | iap->ia_valid |= ATTR_MTIME | ATTR_MTIME_SET; |
| 285 | break; |
| 286 | default: |
| 287 | return false; |
| 288 | } |
| 289 | |
| 290 | return true; |
| 291 | } |
| 292 | |
| 293 | static bool |
| 294 | svcxdr_decode_sattrguard3(struct xdr_stream *xdr, struct nfsd3_sattrargs *args) |
| 295 | { |
| 296 | __be32 *p; |
| 297 | u32 check; |
| 298 | |
| 299 | if (xdr_stream_decode_bool(xdr, &check) < 0) |
| 300 | return false; |
| 301 | if (check) { |
| 302 | p = xdr_inline_decode(xdr, XDR_UNIT * 2); |
| 303 | if (!p) |
| 304 | return false; |
| 305 | args->check_guard = 1; |
| 306 | args->guardtime = be32_to_cpup(p); |
| 307 | } else |
| 308 | args->check_guard = 0; |
| 309 | |
| 310 | return true; |
| 311 | } |
| 312 | |
Chuck Lever | f8a38e2 | 2020-10-20 17:04:03 -0400 | [diff] [blame] | 313 | static bool |
| 314 | svcxdr_decode_specdata3(struct xdr_stream *xdr, struct nfsd3_mknodargs *args) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | { |
Chuck Lever | f8a38e2 | 2020-10-20 17:04:03 -0400 | [diff] [blame] | 316 | __be32 *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | |
Chuck Lever | f8a38e2 | 2020-10-20 17:04:03 -0400 | [diff] [blame] | 318 | p = xdr_inline_decode(xdr, XDR_UNIT * 2); |
| 319 | if (!p) |
| 320 | return false; |
| 321 | args->major = be32_to_cpup(p++); |
| 322 | args->minor = be32_to_cpup(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | |
Chuck Lever | f8a38e2 | 2020-10-20 17:04:03 -0400 | [diff] [blame] | 324 | return true; |
| 325 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | |
Chuck Lever | f8a38e2 | 2020-10-20 17:04:03 -0400 | [diff] [blame] | 327 | static bool |
| 328 | svcxdr_decode_devicedata3(struct svc_rqst *rqstp, struct xdr_stream *xdr, |
| 329 | struct nfsd3_mknodargs *args) |
| 330 | { |
| 331 | return svcxdr_decode_sattr3(rqstp, xdr, &args->attrs) && |
| 332 | svcxdr_decode_specdata3(xdr, args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | } |
| 334 | |
Chuck Lever | 2c42f80 | 2020-10-21 11:58:41 -0400 | [diff] [blame] | 335 | static bool |
| 336 | svcxdr_encode_fattr3(struct svc_rqst *rqstp, struct xdr_stream *xdr, |
| 337 | const struct svc_fh *fhp, const struct kstat *stat) |
| 338 | { |
| 339 | struct user_namespace *userns = nfsd_user_namespace(rqstp); |
| 340 | __be32 *p; |
| 341 | u64 fsid; |
| 342 | |
| 343 | p = xdr_reserve_space(xdr, XDR_UNIT * 21); |
| 344 | if (!p) |
| 345 | return false; |
| 346 | |
| 347 | *p++ = cpu_to_be32(nfs3_ftypes[(stat->mode & S_IFMT) >> 12]); |
| 348 | *p++ = cpu_to_be32((u32)(stat->mode & S_IALLUGO)); |
| 349 | *p++ = cpu_to_be32((u32)stat->nlink); |
| 350 | *p++ = cpu_to_be32((u32)from_kuid_munged(userns, stat->uid)); |
| 351 | *p++ = cpu_to_be32((u32)from_kgid_munged(userns, stat->gid)); |
| 352 | if (S_ISLNK(stat->mode) && stat->size > NFS3_MAXPATHLEN) |
| 353 | p = xdr_encode_hyper(p, (u64)NFS3_MAXPATHLEN); |
| 354 | else |
| 355 | p = xdr_encode_hyper(p, (u64)stat->size); |
| 356 | |
| 357 | /* used */ |
| 358 | p = xdr_encode_hyper(p, ((u64)stat->blocks) << 9); |
| 359 | |
| 360 | /* rdev */ |
| 361 | *p++ = cpu_to_be32((u32)MAJOR(stat->rdev)); |
| 362 | *p++ = cpu_to_be32((u32)MINOR(stat->rdev)); |
| 363 | |
| 364 | switch(fsid_source(fhp)) { |
| 365 | case FSIDSOURCE_FSID: |
| 366 | fsid = (u64)fhp->fh_export->ex_fsid; |
| 367 | break; |
| 368 | case FSIDSOURCE_UUID: |
| 369 | fsid = ((u64 *)fhp->fh_export->ex_uuid)[0]; |
| 370 | fsid ^= ((u64 *)fhp->fh_export->ex_uuid)[1]; |
| 371 | break; |
| 372 | default: |
| 373 | fsid = (u64)huge_encode_dev(fhp->fh_dentry->d_sb->s_dev); |
| 374 | } |
| 375 | p = xdr_encode_hyper(p, fsid); |
| 376 | |
| 377 | /* fileid */ |
| 378 | p = xdr_encode_hyper(p, stat->ino); |
| 379 | |
| 380 | p = encode_nfstime3(p, &stat->atime); |
| 381 | p = encode_nfstime3(p, &stat->mtime); |
| 382 | encode_nfstime3(p, &stat->ctime); |
| 383 | |
| 384 | return true; |
| 385 | } |
| 386 | |
NeilBrown | af6a4e2 | 2007-02-14 00:33:12 -0800 | [diff] [blame] | 387 | static __be32 *encode_fsid(__be32 *p, struct svc_fh *fhp) |
| 388 | { |
| 389 | u64 f; |
| 390 | switch(fsid_source(fhp)) { |
| 391 | default: |
| 392 | case FSIDSOURCE_DEV: |
| 393 | p = xdr_encode_hyper(p, (u64)huge_encode_dev |
Al Viro | fc64005 | 2016-04-10 01:33:30 -0400 | [diff] [blame] | 394 | (fhp->fh_dentry->d_sb->s_dev)); |
NeilBrown | af6a4e2 | 2007-02-14 00:33:12 -0800 | [diff] [blame] | 395 | break; |
| 396 | case FSIDSOURCE_FSID: |
| 397 | p = xdr_encode_hyper(p, (u64) fhp->fh_export->ex_fsid); |
| 398 | break; |
| 399 | case FSIDSOURCE_UUID: |
| 400 | f = ((u64*)fhp->fh_export->ex_uuid)[0]; |
| 401 | f ^= ((u64*)fhp->fh_export->ex_uuid)[1]; |
| 402 | p = xdr_encode_hyper(p, f); |
| 403 | break; |
| 404 | } |
| 405 | return p; |
| 406 | } |
| 407 | |
Adrian Bunk | 3ee6f61 | 2006-12-06 20:40:23 -0800 | [diff] [blame] | 408 | static __be32 * |
Al Viro | 91f0716 | 2006-10-19 23:28:57 -0700 | [diff] [blame] | 409 | encode_fattr3(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp, |
David Shaw | a334de2 | 2006-01-06 00:19:58 -0800 | [diff] [blame] | 410 | struct kstat *stat) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | { |
Trond Myklebust | e45d1a1 | 2019-04-09 12:13:42 -0400 | [diff] [blame] | 412 | struct user_namespace *userns = nfsd_user_namespace(rqstp); |
David Shaw | a334de2 | 2006-01-06 00:19:58 -0800 | [diff] [blame] | 413 | *p++ = htonl(nfs3_ftypes[(stat->mode & S_IFMT) >> 12]); |
Albert Fluegel | 6e14b46 | 2013-11-18 12:18:01 -0500 | [diff] [blame] | 414 | *p++ = htonl((u32) (stat->mode & S_IALLUGO)); |
David Shaw | a334de2 | 2006-01-06 00:19:58 -0800 | [diff] [blame] | 415 | *p++ = htonl((u32) stat->nlink); |
Trond Myklebust | e45d1a1 | 2019-04-09 12:13:42 -0400 | [diff] [blame] | 416 | *p++ = htonl((u32) from_kuid_munged(userns, stat->uid)); |
| 417 | *p++ = htonl((u32) from_kgid_munged(userns, stat->gid)); |
David Shaw | a334de2 | 2006-01-06 00:19:58 -0800 | [diff] [blame] | 418 | if (S_ISLNK(stat->mode) && stat->size > NFS3_MAXPATHLEN) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | p = xdr_encode_hyper(p, (u64) NFS3_MAXPATHLEN); |
| 420 | } else { |
David Shaw | a334de2 | 2006-01-06 00:19:58 -0800 | [diff] [blame] | 421 | p = xdr_encode_hyper(p, (u64) stat->size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | } |
David Shaw | a334de2 | 2006-01-06 00:19:58 -0800 | [diff] [blame] | 423 | p = xdr_encode_hyper(p, ((u64)stat->blocks) << 9); |
| 424 | *p++ = htonl((u32) MAJOR(stat->rdev)); |
| 425 | *p++ = htonl((u32) MINOR(stat->rdev)); |
NeilBrown | af6a4e2 | 2007-02-14 00:33:12 -0800 | [diff] [blame] | 426 | p = encode_fsid(p, fhp); |
Peter Staubach | 40ee5dc | 2007-08-16 12:10:07 -0400 | [diff] [blame] | 427 | p = xdr_encode_hyper(p, stat->ino); |
Arnd Bergmann | 92c5e46 | 2019-10-31 14:55:32 +0100 | [diff] [blame] | 428 | p = encode_time3(p, &stat->atime); |
| 429 | p = encode_time3(p, &stat->mtime); |
| 430 | p = encode_time3(p, &stat->ctime); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | |
| 432 | return p; |
| 433 | } |
| 434 | |
Chuck Lever | 907c382 | 2020-10-22 13:56:58 -0400 | [diff] [blame] | 435 | static bool |
Chuck Lever | 70f8e83 | 2020-10-22 15:12:38 -0400 | [diff] [blame] | 436 | svcxdr_encode_wcc_attr(struct xdr_stream *xdr, const struct svc_fh *fhp) |
| 437 | { |
| 438 | __be32 *p; |
| 439 | |
| 440 | p = xdr_reserve_space(xdr, XDR_UNIT * 6); |
| 441 | if (!p) |
| 442 | return false; |
| 443 | p = xdr_encode_hyper(p, (u64)fhp->fh_pre_size); |
| 444 | p = encode_nfstime3(p, &fhp->fh_pre_mtime); |
| 445 | encode_nfstime3(p, &fhp->fh_pre_ctime); |
| 446 | |
| 447 | return true; |
| 448 | } |
| 449 | |
| 450 | static bool |
| 451 | svcxdr_encode_pre_op_attr(struct xdr_stream *xdr, const struct svc_fh *fhp) |
| 452 | { |
| 453 | if (!fhp->fh_pre_saved) { |
| 454 | if (xdr_stream_encode_item_absent(xdr) < 0) |
| 455 | return false; |
| 456 | return true; |
| 457 | } |
| 458 | |
| 459 | if (xdr_stream_encode_item_present(xdr) < 0) |
| 460 | return false; |
| 461 | return svcxdr_encode_wcc_attr(xdr, fhp); |
| 462 | } |
| 463 | |
| 464 | static bool |
Chuck Lever | 907c382 | 2020-10-22 13:56:58 -0400 | [diff] [blame] | 465 | svcxdr_encode_post_op_attr(struct svc_rqst *rqstp, struct xdr_stream *xdr, |
| 466 | const struct svc_fh *fhp) |
| 467 | { |
| 468 | struct dentry *dentry = fhp->fh_dentry; |
| 469 | struct kstat stat; |
| 470 | |
| 471 | /* |
| 472 | * The inode may be NULL if the call failed because of a |
| 473 | * stale file handle. In this case, no attributes are |
| 474 | * returned. |
| 475 | */ |
| 476 | if (fhp->fh_no_wcc || !dentry || !d_really_is_positive(dentry)) |
| 477 | goto no_post_op_attrs; |
| 478 | if (fh_getattr(fhp, &stat) != nfs_ok) |
| 479 | goto no_post_op_attrs; |
| 480 | |
| 481 | if (xdr_stream_encode_item_present(xdr) < 0) |
| 482 | return false; |
| 483 | lease_get_mtime(d_inode(dentry), &stat.mtime); |
| 484 | if (!svcxdr_encode_fattr3(rqstp, xdr, fhp, &stat)) |
| 485 | return false; |
| 486 | |
| 487 | return true; |
| 488 | |
| 489 | no_post_op_attrs: |
| 490 | return xdr_stream_encode_item_absent(xdr) > 0; |
| 491 | } |
| 492 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | /* |
| 494 | * Encode post-operation attributes. |
| 495 | * The inode may be NULL if the call failed because of a stale file |
| 496 | * handle. In this case, no attributes are returned. |
| 497 | */ |
Al Viro | 91f0716 | 2006-10-19 23:28:57 -0700 | [diff] [blame] | 498 | static __be32 * |
| 499 | encode_post_op_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | { |
| 501 | struct dentry *dentry = fhp->fh_dentry; |
Jeff Layton | daab110 | 2020-11-30 17:03:14 -0500 | [diff] [blame] | 502 | if (!fhp->fh_no_wcc && dentry && d_really_is_positive(dentry)) { |
Al Viro | 3dadecc | 2013-01-24 02:18:08 -0500 | [diff] [blame] | 503 | __be32 err; |
David Shaw | a334de2 | 2006-01-06 00:19:58 -0800 | [diff] [blame] | 504 | struct kstat stat; |
| 505 | |
Al Viro | 3dadecc | 2013-01-24 02:18:08 -0500 | [diff] [blame] | 506 | err = fh_getattr(fhp, &stat); |
David Shaw | a334de2 | 2006-01-06 00:19:58 -0800 | [diff] [blame] | 507 | if (!err) { |
| 508 | *p++ = xdr_one; /* attributes follow */ |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 509 | lease_get_mtime(d_inode(dentry), &stat.mtime); |
David Shaw | a334de2 | 2006-01-06 00:19:58 -0800 | [diff] [blame] | 510 | return encode_fattr3(rqstp, p, fhp, &stat); |
| 511 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | } |
| 513 | *p++ = xdr_zero; |
| 514 | return p; |
| 515 | } |
| 516 | |
Andreas Gruenbacher | a257cdd | 2005-06-22 17:16:26 +0000 | [diff] [blame] | 517 | /* Helper for NFSv3 ACLs */ |
Al Viro | 91f0716 | 2006-10-19 23:28:57 -0700 | [diff] [blame] | 518 | __be32 * |
| 519 | nfs3svc_encode_post_op_attr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp) |
Andreas Gruenbacher | a257cdd | 2005-06-22 17:16:26 +0000 | [diff] [blame] | 520 | { |
| 521 | return encode_post_op_attr(rqstp, p, fhp); |
| 522 | } |
| 523 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | /* |
Chuck Lever | 70f8e83 | 2020-10-22 15:12:38 -0400 | [diff] [blame] | 525 | * Encode weak cache consistency data |
| 526 | */ |
| 527 | static bool |
| 528 | svcxdr_encode_wcc_data(struct svc_rqst *rqstp, struct xdr_stream *xdr, |
| 529 | const struct svc_fh *fhp) |
| 530 | { |
| 531 | struct dentry *dentry = fhp->fh_dentry; |
| 532 | |
| 533 | if (!dentry || !d_really_is_positive(dentry) || !fhp->fh_post_saved) |
| 534 | goto neither; |
| 535 | |
| 536 | /* before */ |
| 537 | if (!svcxdr_encode_pre_op_attr(xdr, fhp)) |
| 538 | return false; |
| 539 | |
| 540 | /* after */ |
| 541 | if (xdr_stream_encode_item_present(xdr) < 0) |
| 542 | return false; |
| 543 | if (!svcxdr_encode_fattr3(rqstp, xdr, fhp, &fhp->fh_post_attr)) |
| 544 | return false; |
| 545 | |
| 546 | return true; |
| 547 | |
| 548 | neither: |
| 549 | if (xdr_stream_encode_item_absent(xdr) < 0) |
| 550 | return false; |
| 551 | if (!svcxdr_encode_post_op_attr(rqstp, xdr, fhp)) |
| 552 | return false; |
| 553 | |
| 554 | return true; |
| 555 | } |
| 556 | |
J. Bruce Fields | 428a23d | 2021-01-29 14:27:01 -0500 | [diff] [blame] | 557 | static bool fs_supports_change_attribute(struct super_block *sb) |
| 558 | { |
| 559 | return sb->s_flags & SB_I_VERSION || sb->s_export_op->fetch_iversion; |
| 560 | } |
| 561 | |
Peter Staubach | 40ee5dc | 2007-08-16 12:10:07 -0400 | [diff] [blame] | 562 | /* |
Amir Goldstein | 39ca1bf | 2018-01-03 17:14:35 +0200 | [diff] [blame] | 563 | * Fill in the pre_op attr for the wcc data |
| 564 | */ |
| 565 | void fill_pre_wcc(struct svc_fh *fhp) |
| 566 | { |
| 567 | struct inode *inode; |
| 568 | struct kstat stat; |
J. Bruce Fields | 942b20dc | 2020-11-30 17:46:17 -0500 | [diff] [blame] | 569 | bool v4 = (fhp->fh_maxsize == NFS4_FHSIZE); |
Amir Goldstein | 39ca1bf | 2018-01-03 17:14:35 +0200 | [diff] [blame] | 570 | |
Jeff Layton | daab110 | 2020-11-30 17:03:14 -0500 | [diff] [blame] | 571 | if (fhp->fh_no_wcc || fhp->fh_pre_saved) |
Amir Goldstein | 39ca1bf | 2018-01-03 17:14:35 +0200 | [diff] [blame] | 572 | return; |
Amir Goldstein | 39ca1bf | 2018-01-03 17:14:35 +0200 | [diff] [blame] | 573 | inode = d_inode(fhp->fh_dentry); |
J. Bruce Fields | 428a23d | 2021-01-29 14:27:01 -0500 | [diff] [blame] | 574 | if (fs_supports_change_attribute(inode->i_sb) || !v4) { |
| 575 | __be32 err = fh_getattr(fhp, &stat); |
| 576 | |
| 577 | if (err) { |
| 578 | /* Grab the times from inode anyway */ |
| 579 | stat.mtime = inode->i_mtime; |
| 580 | stat.ctime = inode->i_ctime; |
| 581 | stat.size = inode->i_size; |
| 582 | } |
| 583 | fhp->fh_pre_mtime = stat.mtime; |
| 584 | fhp->fh_pre_ctime = stat.ctime; |
| 585 | fhp->fh_pre_size = stat.size; |
Amir Goldstein | 39ca1bf | 2018-01-03 17:14:35 +0200 | [diff] [blame] | 586 | } |
J. Bruce Fields | 942b20dc | 2020-11-30 17:46:17 -0500 | [diff] [blame] | 587 | if (v4) |
| 588 | fhp->fh_pre_change = nfsd4_change_attribute(&stat, inode); |
Amir Goldstein | 39ca1bf | 2018-01-03 17:14:35 +0200 | [diff] [blame] | 589 | |
Amir Goldstein | 39ca1bf | 2018-01-03 17:14:35 +0200 | [diff] [blame] | 590 | fhp->fh_pre_saved = true; |
| 591 | } |
| 592 | |
| 593 | /* |
Peter Staubach | 40ee5dc | 2007-08-16 12:10:07 -0400 | [diff] [blame] | 594 | * Fill in the post_op attr for the wcc data |
| 595 | */ |
| 596 | void fill_post_wcc(struct svc_fh *fhp) |
| 597 | { |
J. Bruce Fields | 942b20dc | 2020-11-30 17:46:17 -0500 | [diff] [blame] | 598 | bool v4 = (fhp->fh_maxsize == NFS4_FHSIZE); |
| 599 | struct inode *inode = d_inode(fhp->fh_dentry); |
Peter Staubach | 40ee5dc | 2007-08-16 12:10:07 -0400 | [diff] [blame] | 600 | |
Jeff Layton | daab110 | 2020-11-30 17:03:14 -0500 | [diff] [blame] | 601 | if (fhp->fh_no_wcc) |
| 602 | return; |
| 603 | |
Peter Staubach | 40ee5dc | 2007-08-16 12:10:07 -0400 | [diff] [blame] | 604 | if (fhp->fh_post_saved) |
| 605 | printk("nfsd: inode locked twice during operation.\n"); |
| 606 | |
J. Bruce Fields | 428a23d | 2021-01-29 14:27:01 -0500 | [diff] [blame] | 607 | fhp->fh_post_saved = true; |
| 608 | |
| 609 | if (fs_supports_change_attribute(inode->i_sb) || !v4) { |
| 610 | __be32 err = fh_getattr(fhp, &fhp->fh_post_attr); |
| 611 | |
| 612 | if (err) { |
| 613 | fhp->fh_post_saved = false; |
| 614 | fhp->fh_post_attr.ctime = inode->i_ctime; |
| 615 | } |
| 616 | } |
J. Bruce Fields | 942b20dc | 2020-11-30 17:46:17 -0500 | [diff] [blame] | 617 | if (v4) |
| 618 | fhp->fh_post_change = |
| 619 | nfsd4_change_attribute(&fhp->fh_post_attr, inode); |
Peter Staubach | 40ee5dc | 2007-08-16 12:10:07 -0400 | [diff] [blame] | 620 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | |
| 622 | /* |
| 623 | * XDR decode functions |
| 624 | */ |
Chuck Lever | dcc4699 | 2020-10-01 18:59:12 -0400 | [diff] [blame] | 625 | |
| 626 | int |
Chuck Lever | 9575363 | 2020-10-20 14:30:02 -0400 | [diff] [blame] | 627 | nfs3svc_decode_fhandleargs(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | { |
Chuck Lever | 9575363 | 2020-10-20 14:30:02 -0400 | [diff] [blame] | 629 | struct xdr_stream *xdr = &rqstp->rq_arg_stream; |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 630 | struct nfsd_fhandle *args = rqstp->rq_argp; |
| 631 | |
Chuck Lever | 9575363 | 2020-10-20 14:30:02 -0400 | [diff] [blame] | 632 | return svcxdr_decode_nfs_fh3(xdr, &args->fh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | int |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 636 | nfs3svc_decode_sattrargs(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | { |
Chuck Lever | 9cde936 | 2020-10-20 15:48:22 -0400 | [diff] [blame] | 638 | struct xdr_stream *xdr = &rqstp->rq_arg_stream; |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 639 | struct nfsd3_sattrargs *args = rqstp->rq_argp; |
| 640 | |
Chuck Lever | 9cde936 | 2020-10-20 15:48:22 -0400 | [diff] [blame] | 641 | return svcxdr_decode_nfs_fh3(xdr, &args->fh) && |
| 642 | svcxdr_decode_sattr3(rqstp, xdr, &args->attrs) && |
| 643 | svcxdr_decode_sattrguard3(xdr, args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | int |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 647 | nfs3svc_decode_diropargs(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | { |
Chuck Lever | 54d1d43 | 2020-10-20 15:42:33 -0400 | [diff] [blame] | 649 | struct xdr_stream *xdr = &rqstp->rq_arg_stream; |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 650 | struct nfsd3_diropargs *args = rqstp->rq_argp; |
| 651 | |
Chuck Lever | 54d1d43 | 2020-10-20 15:42:33 -0400 | [diff] [blame] | 652 | return svcxdr_decode_diropargs3(xdr, &args->fh, &args->name, &args->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | int |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 656 | nfs3svc_decode_accessargs(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | { |
Chuck Lever | 3b921a2 | 2020-10-20 14:32:04 -0400 | [diff] [blame] | 658 | struct xdr_stream *xdr = &rqstp->rq_arg_stream; |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 659 | struct nfsd3_accessargs *args = rqstp->rq_argp; |
| 660 | |
Chuck Lever | 3b921a2 | 2020-10-20 14:32:04 -0400 | [diff] [blame] | 661 | if (!svcxdr_decode_nfs_fh3(xdr, &args->fh)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | return 0; |
Chuck Lever | 3b921a2 | 2020-10-20 14:32:04 -0400 | [diff] [blame] | 663 | if (xdr_stream_decode_u32(xdr, &args->access) < 0) |
| 664 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | |
Chuck Lever | 3b921a2 | 2020-10-20 14:32:04 -0400 | [diff] [blame] | 666 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | int |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 670 | nfs3svc_decode_readargs(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | { |
Chuck Lever | be63bd2 | 2020-10-20 14:34:40 -0400 | [diff] [blame] | 672 | struct xdr_stream *xdr = &rqstp->rq_arg_stream; |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 673 | struct nfsd3_readargs *args = rqstp->rq_argp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | |
Chuck Lever | be63bd2 | 2020-10-20 14:34:40 -0400 | [diff] [blame] | 675 | if (!svcxdr_decode_nfs_fh3(xdr, &args->fh)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | return 0; |
Chuck Lever | be63bd2 | 2020-10-20 14:34:40 -0400 | [diff] [blame] | 677 | if (xdr_stream_decode_u64(xdr, &args->offset) < 0) |
| 678 | return 0; |
| 679 | if (xdr_stream_decode_u32(xdr, &args->count) < 0) |
| 680 | return 0; |
J. Bruce Fields | 9512a16 | 2017-05-16 15:57:42 -0400 | [diff] [blame] | 681 | |
Chuck Lever | be63bd2 | 2020-10-20 14:34:40 -0400 | [diff] [blame] | 682 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | int |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 686 | nfs3svc_decode_writeargs(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | { |
Chuck Lever | c43b2f2 | 2020-10-22 11:14:55 -0400 | [diff] [blame] | 688 | struct xdr_stream *xdr = &rqstp->rq_arg_stream; |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 689 | struct nfsd3_writeargs *args = rqstp->rq_argp; |
Greg Banks | 7adae48 | 2006-10-04 02:15:47 -0700 | [diff] [blame] | 690 | u32 max_blocksize = svc_max_payload(rqstp); |
J. Bruce Fields | db44bac | 2017-04-25 16:21:34 -0400 | [diff] [blame] | 691 | struct kvec *head = rqstp->rq_arg.head; |
| 692 | struct kvec *tail = rqstp->rq_arg.tail; |
Chuck Lever | c43b2f2 | 2020-10-22 11:14:55 -0400 | [diff] [blame] | 693 | size_t remaining; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | |
Chuck Lever | c43b2f2 | 2020-10-22 11:14:55 -0400 | [diff] [blame] | 695 | if (!svcxdr_decode_nfs_fh3(xdr, &args->fh)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | return 0; |
Chuck Lever | c43b2f2 | 2020-10-22 11:14:55 -0400 | [diff] [blame] | 697 | if (xdr_stream_decode_u64(xdr, &args->offset) < 0) |
| 698 | return 0; |
| 699 | if (xdr_stream_decode_u32(xdr, &args->count) < 0) |
| 700 | return 0; |
| 701 | if (xdr_stream_decode_u32(xdr, &args->stable) < 0) |
| 702 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | |
Chuck Lever | c43b2f2 | 2020-10-22 11:14:55 -0400 | [diff] [blame] | 704 | /* opaque data */ |
| 705 | if (xdr_stream_decode_u32(xdr, &args->len) < 0) |
J. Bruce Fields | 13bf9fb | 2017-04-21 15:26:30 -0400 | [diff] [blame] | 706 | return 0; |
Chuck Lever | c43b2f2 | 2020-10-22 11:14:55 -0400 | [diff] [blame] | 707 | |
| 708 | /* request sanity */ |
Peter Staubach | f34b9568 | 2007-05-09 02:34:48 -0700 | [diff] [blame] | 709 | if (args->count != args->len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | return 0; |
Chuck Lever | c43b2f2 | 2020-10-22 11:14:55 -0400 | [diff] [blame] | 711 | remaining = head->iov_len + rqstp->rq_arg.page_len + tail->iov_len; |
| 712 | remaining -= xdr_stream_pos(xdr); |
| 713 | if (remaining < xdr_align_size(args->len)) |
Peter Staubach | f34b9568 | 2007-05-09 02:34:48 -0700 | [diff] [blame] | 714 | return 0; |
Peter Staubach | f34b9568 | 2007-05-09 02:34:48 -0700 | [diff] [blame] | 715 | if (args->count > max_blocksize) { |
| 716 | args->count = max_blocksize; |
Chuck Lever | c43b2f2 | 2020-10-22 11:14:55 -0400 | [diff] [blame] | 717 | args->len = max_blocksize; |
Peter Staubach | f34b9568 | 2007-05-09 02:34:48 -0700 | [diff] [blame] | 718 | } |
Chuck Lever | 8154ef2 | 2018-03-27 10:54:07 -0400 | [diff] [blame] | 719 | |
Chuck Lever | c43b2f2 | 2020-10-22 11:14:55 -0400 | [diff] [blame] | 720 | args->first.iov_base = xdr->p; |
| 721 | args->first.iov_len = head->iov_len - xdr_stream_pos(xdr); |
| 722 | |
Peter Staubach | f34b9568 | 2007-05-09 02:34:48 -0700 | [diff] [blame] | 723 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | int |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 727 | nfs3svc_decode_createargs(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | { |
Chuck Lever | 6b3a119 | 2020-10-20 15:56:11 -0400 | [diff] [blame] | 729 | struct xdr_stream *xdr = &rqstp->rq_arg_stream; |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 730 | struct nfsd3_createargs *args = rqstp->rq_argp; |
| 731 | |
Chuck Lever | 6b3a119 | 2020-10-20 15:56:11 -0400 | [diff] [blame] | 732 | if (!svcxdr_decode_diropargs3(xdr, &args->fh, &args->name, &args->len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | return 0; |
Chuck Lever | 6b3a119 | 2020-10-20 15:56:11 -0400 | [diff] [blame] | 734 | if (xdr_stream_decode_u32(xdr, &args->createmode) < 0) |
| 735 | return 0; |
| 736 | switch (args->createmode) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | case NFS3_CREATE_UNCHECKED: |
| 738 | case NFS3_CREATE_GUARDED: |
Chuck Lever | 6b3a119 | 2020-10-20 15:56:11 -0400 | [diff] [blame] | 739 | return svcxdr_decode_sattr3(rqstp, xdr, &args->attrs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | case NFS3_CREATE_EXCLUSIVE: |
Chuck Lever | 6b3a119 | 2020-10-20 15:56:11 -0400 | [diff] [blame] | 741 | args->verf = xdr_inline_decode(xdr, NFS3_CREATEVERFSIZE); |
| 742 | if (!args->verf) |
| 743 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | break; |
| 745 | default: |
| 746 | return 0; |
| 747 | } |
Chuck Lever | 6b3a119 | 2020-10-20 15:56:11 -0400 | [diff] [blame] | 748 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | } |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 750 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | int |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 752 | nfs3svc_decode_mkdirargs(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | { |
Chuck Lever | 83374c2 | 2020-10-20 17:02:16 -0400 | [diff] [blame] | 754 | struct xdr_stream *xdr = &rqstp->rq_arg_stream; |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 755 | struct nfsd3_createargs *args = rqstp->rq_argp; |
| 756 | |
Chuck Lever | 83374c2 | 2020-10-20 17:02:16 -0400 | [diff] [blame] | 757 | return svcxdr_decode_diropargs3(xdr, &args->fh, |
| 758 | &args->name, &args->len) && |
| 759 | svcxdr_decode_sattr3(rqstp, xdr, &args->attrs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | int |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 763 | nfs3svc_decode_symlinkargs(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | { |
Chuck Lever | da39201 | 2020-10-20 16:01:16 -0400 | [diff] [blame] | 765 | struct xdr_stream *xdr = &rqstp->rq_arg_stream; |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 766 | struct nfsd3_symlinkargs *args = rqstp->rq_argp; |
Chuck Lever | da39201 | 2020-10-20 16:01:16 -0400 | [diff] [blame] | 767 | struct kvec *head = rqstp->rq_arg.head; |
| 768 | struct kvec *tail = rqstp->rq_arg.tail; |
| 769 | size_t remaining; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | |
Chuck Lever | da39201 | 2020-10-20 16:01:16 -0400 | [diff] [blame] | 771 | if (!svcxdr_decode_diropargs3(xdr, &args->ffh, &args->fname, &args->flen)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | return 0; |
Chuck Lever | da39201 | 2020-10-20 16:01:16 -0400 | [diff] [blame] | 773 | if (!svcxdr_decode_sattr3(rqstp, xdr, &args->attrs)) |
Chuck Lever | 38a7031 | 2018-03-27 10:54:21 -0400 | [diff] [blame] | 774 | return 0; |
Chuck Lever | da39201 | 2020-10-20 16:01:16 -0400 | [diff] [blame] | 775 | if (xdr_stream_decode_u32(xdr, &args->tlen) < 0) |
| 776 | return 0; |
| 777 | |
| 778 | /* request sanity */ |
| 779 | remaining = head->iov_len + rqstp->rq_arg.page_len + tail->iov_len; |
| 780 | remaining -= xdr_stream_pos(xdr); |
| 781 | if (remaining < xdr_align_size(args->tlen)) |
| 782 | return 0; |
| 783 | |
| 784 | args->first.iov_base = xdr->p; |
| 785 | args->first.iov_len = head->iov_len - xdr_stream_pos(xdr); |
| 786 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | return 1; |
| 788 | } |
| 789 | |
| 790 | int |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 791 | nfs3svc_decode_mknodargs(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | { |
Chuck Lever | f8a38e2 | 2020-10-20 17:04:03 -0400 | [diff] [blame] | 793 | struct xdr_stream *xdr = &rqstp->rq_arg_stream; |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 794 | struct nfsd3_mknodargs *args = rqstp->rq_argp; |
| 795 | |
Chuck Lever | f8a38e2 | 2020-10-20 17:04:03 -0400 | [diff] [blame] | 796 | if (!svcxdr_decode_diropargs3(xdr, &args->fh, &args->name, &args->len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | return 0; |
Chuck Lever | f8a38e2 | 2020-10-20 17:04:03 -0400 | [diff] [blame] | 798 | if (xdr_stream_decode_u32(xdr, &args->ftype) < 0) |
| 799 | return 0; |
| 800 | switch (args->ftype) { |
| 801 | case NF3CHR: |
| 802 | case NF3BLK: |
| 803 | return svcxdr_decode_devicedata3(rqstp, xdr, args); |
| 804 | case NF3SOCK: |
| 805 | case NF3FIFO: |
| 806 | return svcxdr_decode_sattr3(rqstp, xdr, &args->attrs); |
| 807 | case NF3REG: |
| 808 | case NF3DIR: |
| 809 | case NF3LNK: |
| 810 | /* Valid XDR but illegal file types */ |
| 811 | break; |
| 812 | default: |
| 813 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | } |
| 815 | |
Chuck Lever | f8a38e2 | 2020-10-20 17:04:03 -0400 | [diff] [blame] | 816 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | int |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 820 | nfs3svc_decode_renameargs(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | { |
Chuck Lever | d181e0a | 2020-10-20 15:44:12 -0400 | [diff] [blame] | 822 | struct xdr_stream *xdr = &rqstp->rq_arg_stream; |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 823 | struct nfsd3_renameargs *args = rqstp->rq_argp; |
| 824 | |
Chuck Lever | d181e0a | 2020-10-20 15:44:12 -0400 | [diff] [blame] | 825 | return svcxdr_decode_diropargs3(xdr, &args->ffh, |
| 826 | &args->fname, &args->flen) && |
| 827 | svcxdr_decode_diropargs3(xdr, &args->tfh, |
| 828 | &args->tname, &args->tlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | int |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 832 | nfs3svc_decode_linkargs(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | { |
Chuck Lever | efaa1e7 | 2020-10-19 13:26:32 -0400 | [diff] [blame] | 834 | struct xdr_stream *xdr = &rqstp->rq_arg_stream; |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 835 | struct nfsd3_linkargs *args = rqstp->rq_argp; |
| 836 | |
Chuck Lever | efaa1e7 | 2020-10-19 13:26:32 -0400 | [diff] [blame] | 837 | return svcxdr_decode_nfs_fh3(xdr, &args->ffh) && |
| 838 | svcxdr_decode_diropargs3(xdr, &args->tfh, |
| 839 | &args->tname, &args->tlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | int |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 843 | nfs3svc_decode_readdirargs(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | { |
Chuck Lever | 9cedc2e | 2020-10-19 13:23:52 -0400 | [diff] [blame] | 845 | struct xdr_stream *xdr = &rqstp->rq_arg_stream; |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 846 | struct nfsd3_readdirargs *args = rqstp->rq_argp; |
NeilBrown | f875a79 | 2019-03-07 09:49:46 +1100 | [diff] [blame] | 847 | |
Chuck Lever | 9cedc2e | 2020-10-19 13:23:52 -0400 | [diff] [blame] | 848 | if (!svcxdr_decode_nfs_fh3(xdr, &args->fh)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | return 0; |
Chuck Lever | 9cedc2e | 2020-10-19 13:23:52 -0400 | [diff] [blame] | 850 | if (xdr_stream_decode_u64(xdr, &args->cookie) < 0) |
| 851 | return 0; |
| 852 | args->verf = xdr_inline_decode(xdr, NFS3_COOKIEVERFSIZE); |
| 853 | if (!args->verf) |
| 854 | return 0; |
| 855 | if (xdr_stream_decode_u32(xdr, &args->count) < 0) |
| 856 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | |
Chuck Lever | 9cedc2e | 2020-10-19 13:23:52 -0400 | [diff] [blame] | 858 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | int |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 862 | nfs3svc_decode_readdirplusargs(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | { |
Chuck Lever | 9cedc2e | 2020-10-19 13:23:52 -0400 | [diff] [blame] | 864 | struct xdr_stream *xdr = &rqstp->rq_arg_stream; |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 865 | struct nfsd3_readdirargs *args = rqstp->rq_argp; |
Chuck Lever | 9cedc2e | 2020-10-19 13:23:52 -0400 | [diff] [blame] | 866 | u32 dircount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | |
Chuck Lever | 9cedc2e | 2020-10-19 13:23:52 -0400 | [diff] [blame] | 868 | if (!svcxdr_decode_nfs_fh3(xdr, &args->fh)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | return 0; |
Chuck Lever | 9cedc2e | 2020-10-19 13:23:52 -0400 | [diff] [blame] | 870 | if (xdr_stream_decode_u64(xdr, &args->cookie) < 0) |
| 871 | return 0; |
| 872 | args->verf = xdr_inline_decode(xdr, NFS3_COOKIEVERFSIZE); |
| 873 | if (!args->verf) |
| 874 | return 0; |
| 875 | /* dircount is ignored */ |
| 876 | if (xdr_stream_decode_u32(xdr, &dircount) < 0) |
| 877 | return 0; |
| 878 | if (xdr_stream_decode_u32(xdr, &args->count) < 0) |
| 879 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | |
Chuck Lever | 9cedc2e | 2020-10-19 13:23:52 -0400 | [diff] [blame] | 881 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | int |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 885 | nfs3svc_decode_commitargs(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | { |
Chuck Lever | c8d26a0 | 2020-10-20 14:41:56 -0400 | [diff] [blame] | 887 | struct xdr_stream *xdr = &rqstp->rq_arg_stream; |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 888 | struct nfsd3_commitargs *args = rqstp->rq_argp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | |
Chuck Lever | c8d26a0 | 2020-10-20 14:41:56 -0400 | [diff] [blame] | 890 | if (!svcxdr_decode_nfs_fh3(xdr, &args->fh)) |
| 891 | return 0; |
| 892 | if (xdr_stream_decode_u64(xdr, &args->offset) < 0) |
| 893 | return 0; |
| 894 | if (xdr_stream_decode_u32(xdr, &args->count) < 0) |
| 895 | return 0; |
| 896 | |
| 897 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | /* |
| 901 | * XDR encode functions |
| 902 | */ |
Chuck Lever | cc028a1 | 2020-10-02 15:52:44 -0400 | [diff] [blame] | 903 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 904 | /* GETATTR */ |
| 905 | int |
Chuck Lever | 2c42f80 | 2020-10-21 11:58:41 -0400 | [diff] [blame] | 906 | nfs3svc_encode_getattrres(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | { |
Chuck Lever | 2c42f80 | 2020-10-21 11:58:41 -0400 | [diff] [blame] | 908 | struct xdr_stream *xdr = &rqstp->rq_res_stream; |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 909 | struct nfsd3_attrstat *resp = rqstp->rq_resp; |
| 910 | |
Chuck Lever | 2c42f80 | 2020-10-21 11:58:41 -0400 | [diff] [blame] | 911 | if (!svcxdr_encode_nfsstat3(xdr, resp->status)) |
| 912 | return 0; |
| 913 | switch (resp->status) { |
| 914 | case nfs_ok: |
| 915 | lease_get_mtime(d_inode(resp->fh.fh_dentry), &resp->stat.mtime); |
| 916 | if (!svcxdr_encode_fattr3(rqstp, xdr, &resp->fh, &resp->stat)) |
| 917 | return 0; |
| 918 | break; |
Peter Staubach | 40ee5dc | 2007-08-16 12:10:07 -0400 | [diff] [blame] | 919 | } |
Chuck Lever | 2c42f80 | 2020-10-21 11:58:41 -0400 | [diff] [blame] | 920 | |
| 921 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | /* SETATTR, REMOVE, RMDIR */ |
| 925 | int |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 926 | nfs3svc_encode_wccstat(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | { |
Chuck Lever | 70f8e83 | 2020-10-22 15:12:38 -0400 | [diff] [blame] | 928 | struct xdr_stream *xdr = &rqstp->rq_res_stream; |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 929 | struct nfsd3_attrstat *resp = rqstp->rq_resp; |
| 930 | |
Chuck Lever | 70f8e83 | 2020-10-22 15:12:38 -0400 | [diff] [blame] | 931 | return svcxdr_encode_nfsstat3(xdr, resp->status) && |
| 932 | svcxdr_encode_wcc_data(rqstp, xdr, &resp->fh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | /* LOOKUP */ |
Chuck Lever | 5cf3533 | 2020-10-22 14:46:58 -0400 | [diff] [blame] | 936 | int nfs3svc_encode_lookupres(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | { |
Chuck Lever | 5cf3533 | 2020-10-22 14:46:58 -0400 | [diff] [blame] | 938 | struct xdr_stream *xdr = &rqstp->rq_res_stream; |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 939 | struct nfsd3_diropres *resp = rqstp->rq_resp; |
| 940 | |
Chuck Lever | 5cf3533 | 2020-10-22 14:46:58 -0400 | [diff] [blame] | 941 | if (!svcxdr_encode_nfsstat3(xdr, resp->status)) |
| 942 | return 0; |
| 943 | switch (resp->status) { |
| 944 | case nfs_ok: |
| 945 | if (!svcxdr_encode_nfs_fh3(xdr, &resp->fh)) |
| 946 | return 0; |
| 947 | if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh)) |
| 948 | return 0; |
| 949 | if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->dirfh)) |
| 950 | return 0; |
| 951 | break; |
| 952 | default: |
| 953 | if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->dirfh)) |
| 954 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | } |
Chuck Lever | 5cf3533 | 2020-10-22 14:46:58 -0400 | [diff] [blame] | 956 | |
| 957 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | /* ACCESS */ |
| 961 | int |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 962 | nfs3svc_encode_accessres(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | { |
Chuck Lever | 907c382 | 2020-10-22 13:56:58 -0400 | [diff] [blame] | 964 | struct xdr_stream *xdr = &rqstp->rq_res_stream; |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 965 | struct nfsd3_accessres *resp = rqstp->rq_resp; |
| 966 | |
Chuck Lever | 907c382 | 2020-10-22 13:56:58 -0400 | [diff] [blame] | 967 | if (!svcxdr_encode_nfsstat3(xdr, resp->status)) |
| 968 | return 0; |
| 969 | switch (resp->status) { |
| 970 | case nfs_ok: |
| 971 | if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh)) |
| 972 | return 0; |
| 973 | if (xdr_stream_encode_u32(xdr, resp->access) < 0) |
| 974 | return 0; |
| 975 | break; |
| 976 | default: |
| 977 | if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh)) |
| 978 | return 0; |
| 979 | } |
| 980 | |
| 981 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | /* READLINK */ |
| 985 | int |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 986 | nfs3svc_encode_readlinkres(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | { |
Chuck Lever | 9a9c892 | 2020-10-22 15:18:40 -0400 | [diff] [blame] | 988 | struct xdr_stream *xdr = &rqstp->rq_res_stream; |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 989 | struct nfsd3_readlinkres *resp = rqstp->rq_resp; |
Chuck Lever | 76e5492 | 2020-11-05 10:24:19 -0500 | [diff] [blame] | 990 | struct kvec *head = rqstp->rq_res.head; |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 991 | |
Chuck Lever | 9a9c892 | 2020-10-22 15:18:40 -0400 | [diff] [blame] | 992 | if (!svcxdr_encode_nfsstat3(xdr, resp->status)) |
| 993 | return 0; |
| 994 | switch (resp->status) { |
| 995 | case nfs_ok: |
| 996 | if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh)) |
Chuck Lever | 76e5492 | 2020-11-05 10:24:19 -0500 | [diff] [blame] | 997 | return 0; |
Chuck Lever | 9a9c892 | 2020-10-22 15:18:40 -0400 | [diff] [blame] | 998 | if (xdr_stream_encode_u32(xdr, resp->len) < 0) |
| 999 | return 0; |
| 1000 | xdr_write_pages(xdr, resp->pages, 0, resp->len); |
| 1001 | if (svc_encode_result_payload(rqstp, head->iov_len, resp->len) < 0) |
| 1002 | return 0; |
| 1003 | break; |
| 1004 | default: |
| 1005 | if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh)) |
| 1006 | return 0; |
| 1007 | } |
| 1008 | |
| 1009 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | } |
| 1011 | |
| 1012 | /* READ */ |
| 1013 | int |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 1014 | nfs3svc_encode_readres(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | { |
Chuck Lever | cc9bcda | 2020-10-22 15:23:50 -0400 | [diff] [blame] | 1016 | struct xdr_stream *xdr = &rqstp->rq_res_stream; |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 1017 | struct nfsd3_readres *resp = rqstp->rq_resp; |
Chuck Lever | 76e5492 | 2020-11-05 10:24:19 -0500 | [diff] [blame] | 1018 | struct kvec *head = rqstp->rq_res.head; |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 1019 | |
Chuck Lever | cc9bcda | 2020-10-22 15:23:50 -0400 | [diff] [blame] | 1020 | if (!svcxdr_encode_nfsstat3(xdr, resp->status)) |
| 1021 | return 0; |
| 1022 | switch (resp->status) { |
| 1023 | case nfs_ok: |
| 1024 | if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh)) |
Chuck Lever | 76e5492 | 2020-11-05 10:24:19 -0500 | [diff] [blame] | 1025 | return 0; |
Chuck Lever | cc9bcda | 2020-10-22 15:23:50 -0400 | [diff] [blame] | 1026 | if (xdr_stream_encode_u32(xdr, resp->count) < 0) |
| 1027 | return 0; |
| 1028 | if (xdr_stream_encode_bool(xdr, resp->eof) < 0) |
| 1029 | return 0; |
| 1030 | if (xdr_stream_encode_u32(xdr, resp->count) < 0) |
| 1031 | return 0; |
| 1032 | xdr_write_pages(xdr, resp->pages, rqstp->rq_res.page_base, |
| 1033 | resp->count); |
| 1034 | if (svc_encode_result_payload(rqstp, head->iov_len, resp->count) < 0) |
| 1035 | return 0; |
| 1036 | break; |
| 1037 | default: |
| 1038 | if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh)) |
| 1039 | return 0; |
| 1040 | } |
| 1041 | |
| 1042 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | /* WRITE */ |
| 1046 | int |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 1047 | nfs3svc_encode_writeres(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | { |
Chuck Lever | ecb7a08 | 2020-10-22 15:26:31 -0400 | [diff] [blame] | 1049 | struct xdr_stream *xdr = &rqstp->rq_res_stream; |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 1050 | struct nfsd3_writeres *resp = rqstp->rq_resp; |
Stanislav Kinsbursky | b9c0ef8 | 2012-12-06 14:23:19 +0300 | [diff] [blame] | 1051 | |
Chuck Lever | ecb7a08 | 2020-10-22 15:26:31 -0400 | [diff] [blame] | 1052 | if (!svcxdr_encode_nfsstat3(xdr, resp->status)) |
| 1053 | return 0; |
| 1054 | switch (resp->status) { |
| 1055 | case nfs_ok: |
| 1056 | if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->fh)) |
| 1057 | return 0; |
| 1058 | if (xdr_stream_encode_u32(xdr, resp->count) < 0) |
| 1059 | return 0; |
| 1060 | if (xdr_stream_encode_u32(xdr, resp->committed) < 0) |
| 1061 | return 0; |
| 1062 | if (!svcxdr_encode_writeverf3(xdr, resp->verf)) |
| 1063 | return 0; |
| 1064 | break; |
| 1065 | default: |
| 1066 | if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->fh)) |
| 1067 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | } |
Chuck Lever | ecb7a08 | 2020-10-22 15:26:31 -0400 | [diff] [blame] | 1069 | |
| 1070 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | } |
| 1072 | |
| 1073 | /* CREATE, MKDIR, SYMLINK, MKNOD */ |
| 1074 | int |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 1075 | nfs3svc_encode_createres(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | { |
Chuck Lever | 78315b3 | 2020-10-22 15:27:23 -0400 | [diff] [blame] | 1077 | struct xdr_stream *xdr = &rqstp->rq_res_stream; |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 1078 | struct nfsd3_diropres *resp = rqstp->rq_resp; |
| 1079 | |
Chuck Lever | 78315b3 | 2020-10-22 15:27:23 -0400 | [diff] [blame] | 1080 | if (!svcxdr_encode_nfsstat3(xdr, resp->status)) |
| 1081 | return 0; |
| 1082 | switch (resp->status) { |
| 1083 | case nfs_ok: |
| 1084 | if (!svcxdr_encode_post_op_fh3(xdr, &resp->fh)) |
| 1085 | return 0; |
| 1086 | if (!svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh)) |
| 1087 | return 0; |
| 1088 | if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->dirfh)) |
| 1089 | return 0; |
| 1090 | break; |
| 1091 | default: |
| 1092 | if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->dirfh)) |
| 1093 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1094 | } |
Chuck Lever | 78315b3 | 2020-10-22 15:27:23 -0400 | [diff] [blame] | 1095 | |
| 1096 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | /* RENAME */ |
| 1100 | int |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 1101 | nfs3svc_encode_renameres(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | { |
Chuck Lever | 89d79e9 | 2020-10-22 15:33:05 -0400 | [diff] [blame] | 1103 | struct xdr_stream *xdr = &rqstp->rq_res_stream; |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 1104 | struct nfsd3_renameres *resp = rqstp->rq_resp; |
| 1105 | |
Chuck Lever | 89d79e9 | 2020-10-22 15:33:05 -0400 | [diff] [blame] | 1106 | return svcxdr_encode_nfsstat3(xdr, resp->status) && |
| 1107 | svcxdr_encode_wcc_data(rqstp, xdr, &resp->ffh) && |
| 1108 | svcxdr_encode_wcc_data(rqstp, xdr, &resp->tfh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1109 | } |
| 1110 | |
| 1111 | /* LINK */ |
| 1112 | int |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 1113 | nfs3svc_encode_linkres(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | { |
Chuck Lever | 4d74380 | 2020-10-22 15:08:29 -0400 | [diff] [blame] | 1115 | struct xdr_stream *xdr = &rqstp->rq_res_stream; |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 1116 | struct nfsd3_linkres *resp = rqstp->rq_resp; |
| 1117 | |
Chuck Lever | 4d74380 | 2020-10-22 15:08:29 -0400 | [diff] [blame] | 1118 | return svcxdr_encode_nfsstat3(xdr, resp->status) && |
| 1119 | svcxdr_encode_post_op_attr(rqstp, xdr, &resp->fh) && |
| 1120 | svcxdr_encode_wcc_data(rqstp, xdr, &resp->tfh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | } |
| 1122 | |
| 1123 | /* READDIR */ |
| 1124 | int |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 1125 | nfs3svc_encode_readdirres(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 | { |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 1127 | struct nfsd3_readdirres *resp = rqstp->rq_resp; |
| 1128 | |
Chuck Lever | cc028a1 | 2020-10-02 15:52:44 -0400 | [diff] [blame] | 1129 | *p++ = resp->status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | p = encode_post_op_attr(rqstp, p, &resp->fh); |
| 1131 | |
| 1132 | if (resp->status == 0) { |
| 1133 | /* stupid readdir cookie */ |
| 1134 | memcpy(p, resp->verf, 8); p += 2; |
| 1135 | xdr_ressize_check(rqstp, p); |
| 1136 | if (rqstp->rq_res.head[0].iov_len + (2<<2) > PAGE_SIZE) |
| 1137 | return 1; /*No room for trailer */ |
| 1138 | rqstp->rq_res.page_len = (resp->count) << 2; |
| 1139 | |
| 1140 | /* add the 'tail' to the end of the 'head' page - page 0. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | rqstp->rq_res.tail[0].iov_base = p; |
| 1142 | *p++ = 0; /* no more entries */ |
| 1143 | *p++ = htonl(resp->common.err == nfserr_eof); |
| 1144 | rqstp->rq_res.tail[0].iov_len = 2<<2; |
| 1145 | return 1; |
| 1146 | } else |
| 1147 | return xdr_ressize_check(rqstp, p); |
| 1148 | } |
| 1149 | |
Adrian Bunk | 3ee6f61 | 2006-12-06 20:40:23 -0800 | [diff] [blame] | 1150 | static __be32 * |
Al Viro | 91f0716 | 2006-10-19 23:28:57 -0700 | [diff] [blame] | 1151 | encode_entry_baggage(struct nfsd3_readdirres *cd, __be32 *p, const char *name, |
Peter Staubach | 40ee5dc | 2007-08-16 12:10:07 -0400 | [diff] [blame] | 1152 | int namlen, u64 ino) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | { |
| 1154 | *p++ = xdr_one; /* mark entry present */ |
| 1155 | p = xdr_encode_hyper(p, ino); /* file id */ |
| 1156 | p = xdr_encode_array(p, name, namlen);/* name length & name */ |
| 1157 | |
| 1158 | cd->offset = p; /* remember pointer */ |
| 1159 | p = xdr_encode_hyper(p, NFS_OFFSET_MAX);/* offset of next entry */ |
| 1160 | |
| 1161 | return p; |
| 1162 | } |
| 1163 | |
Al Viro | efe3965 | 2012-04-13 00:32:14 -0400 | [diff] [blame] | 1164 | static __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | compose_entry_fh(struct nfsd3_readdirres *cd, struct svc_fh *fhp, |
NeilBrown | 43b0e7e | 2015-05-03 09:16:53 +1000 | [diff] [blame] | 1166 | const char *name, int namlen, u64 ino) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | { |
| 1168 | struct svc_export *exp; |
| 1169 | struct dentry *dparent, *dchild; |
Al Viro | efe3965 | 2012-04-13 00:32:14 -0400 | [diff] [blame] | 1170 | __be32 rv = nfserr_noent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | |
| 1172 | dparent = cd->fh.fh_dentry; |
| 1173 | exp = cd->fh.fh_export; |
| 1174 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1175 | if (isdotent(name, namlen)) { |
| 1176 | if (namlen == 2) { |
| 1177 | dchild = dget_parent(dparent); |
J. Bruce Fields | 51b2ee7 | 2021-01-11 16:01:29 -0500 | [diff] [blame] | 1178 | /* |
| 1179 | * Don't return filehandle for ".." if we're at |
| 1180 | * the filesystem or export root: |
| 1181 | */ |
Al Viro | efe3965 | 2012-04-13 00:32:14 -0400 | [diff] [blame] | 1182 | if (dchild == dparent) |
| 1183 | goto out; |
J. Bruce Fields | 51b2ee7 | 2021-01-11 16:01:29 -0500 | [diff] [blame] | 1184 | if (dparent == exp->ex_path.dentry) |
| 1185 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | } else |
| 1187 | dchild = dget(dparent); |
| 1188 | } else |
Al Viro | 6c2d4798 | 2019-10-31 01:21:58 -0400 | [diff] [blame] | 1189 | dchild = lookup_positive_unlocked(name, dparent, namlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | if (IS_ERR(dchild)) |
Al Viro | efe3965 | 2012-04-13 00:32:14 -0400 | [diff] [blame] | 1191 | return rv; |
J. Bruce Fields | 8177e6d | 2009-09-04 14:13:09 -0400 | [diff] [blame] | 1192 | if (d_mountpoint(dchild)) |
| 1193 | goto out; |
NeilBrown | 43b0e7e | 2015-05-03 09:16:53 +1000 | [diff] [blame] | 1194 | if (dchild->d_inode->i_ino != ino) |
| 1195 | goto out; |
Al Viro | efe3965 | 2012-04-13 00:32:14 -0400 | [diff] [blame] | 1196 | rv = fh_compose(fhp, exp, dchild, &cd->fh); |
J. Bruce Fields | 8177e6d | 2009-09-04 14:13:09 -0400 | [diff] [blame] | 1197 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1198 | dput(dchild); |
| 1199 | return rv; |
| 1200 | } |
| 1201 | |
NeilBrown | 43b0e7e | 2015-05-03 09:16:53 +1000 | [diff] [blame] | 1202 | static __be32 *encode_entryplus_baggage(struct nfsd3_readdirres *cd, __be32 *p, const char *name, int namlen, u64 ino) |
J. Bruce Fields | 8177e6d | 2009-09-04 14:13:09 -0400 | [diff] [blame] | 1203 | { |
J. Bruce Fields | 068c34c | 2014-01-09 16:24:35 -0500 | [diff] [blame] | 1204 | struct svc_fh *fh = &cd->scratch; |
Al Viro | efe3965 | 2012-04-13 00:32:14 -0400 | [diff] [blame] | 1205 | __be32 err; |
J. Bruce Fields | 8177e6d | 2009-09-04 14:13:09 -0400 | [diff] [blame] | 1206 | |
J. Bruce Fields | 068c34c | 2014-01-09 16:24:35 -0500 | [diff] [blame] | 1207 | fh_init(fh, NFS3_FHSIZE); |
NeilBrown | 43b0e7e | 2015-05-03 09:16:53 +1000 | [diff] [blame] | 1208 | err = compose_entry_fh(cd, fh, name, namlen, ino); |
J. Bruce Fields | 8177e6d | 2009-09-04 14:13:09 -0400 | [diff] [blame] | 1209 | if (err) { |
| 1210 | *p++ = 0; |
| 1211 | *p++ = 0; |
J. Bruce Fields | aed100f | 2009-09-04 14:40:36 -0400 | [diff] [blame] | 1212 | goto out; |
J. Bruce Fields | 8177e6d | 2009-09-04 14:13:09 -0400 | [diff] [blame] | 1213 | } |
J. Bruce Fields | 068c34c | 2014-01-09 16:24:35 -0500 | [diff] [blame] | 1214 | p = encode_post_op_attr(cd->rqstp, p, fh); |
J. Bruce Fields | 8177e6d | 2009-09-04 14:13:09 -0400 | [diff] [blame] | 1215 | *p++ = xdr_one; /* yes, a file handle follows */ |
J. Bruce Fields | 068c34c | 2014-01-09 16:24:35 -0500 | [diff] [blame] | 1216 | p = encode_fh(p, fh); |
J. Bruce Fields | aed100f | 2009-09-04 14:40:36 -0400 | [diff] [blame] | 1217 | out: |
J. Bruce Fields | 068c34c | 2014-01-09 16:24:35 -0500 | [diff] [blame] | 1218 | fh_put(fh); |
J. Bruce Fields | 8177e6d | 2009-09-04 14:13:09 -0400 | [diff] [blame] | 1219 | return p; |
| 1220 | } |
| 1221 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | /* |
| 1223 | * Encode a directory entry. This one works for both normal readdir |
| 1224 | * and readdirplus. |
| 1225 | * The normal readdir reply requires 2 (fileid) + 1 (stringlen) |
| 1226 | * + string + 2 (cookie) + 1 (next) words, i.e. 6 + strlen. |
| 1227 | * |
| 1228 | * The readdirplus baggage is 1+21 words for post_op_attr, plus the |
| 1229 | * file handle. |
| 1230 | */ |
| 1231 | |
| 1232 | #define NFS3_ENTRY_BAGGAGE (2 + 1 + 2 + 1) |
| 1233 | #define NFS3_ENTRYPLUS_BAGGAGE (1 + 21 + 1 + (NFS3_FHSIZE >> 2)) |
| 1234 | static int |
NeilBrown | 598b9a5 | 2007-03-26 21:32:08 -0800 | [diff] [blame] | 1235 | encode_entry(struct readdir_cd *ccd, const char *name, int namlen, |
Peter Staubach | 40ee5dc | 2007-08-16 12:10:07 -0400 | [diff] [blame] | 1236 | loff_t offset, u64 ino, unsigned int d_type, int plus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 | { |
| 1238 | struct nfsd3_readdirres *cd = container_of(ccd, struct nfsd3_readdirres, |
| 1239 | common); |
Al Viro | 91f0716 | 2006-10-19 23:28:57 -0700 | [diff] [blame] | 1240 | __be32 *p = cd->buffer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | caddr_t curr_page_addr = NULL; |
J. Bruce Fields | afc5940 | 2012-12-10 18:01:37 -0500 | [diff] [blame] | 1242 | struct page ** page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1243 | int slen; /* string (name) length */ |
| 1244 | int elen; /* estimated entry length in words */ |
| 1245 | int num_entry_words = 0; /* actual number of words */ |
| 1246 | |
| 1247 | if (cd->offset) { |
| 1248 | u64 offset64 = offset; |
| 1249 | |
| 1250 | if (unlikely(cd->offset1)) { |
| 1251 | /* we ended up with offset on a page boundary */ |
| 1252 | *cd->offset = htonl(offset64 >> 32); |
| 1253 | *cd->offset1 = htonl(offset64 & 0xffffffff); |
| 1254 | cd->offset1 = NULL; |
| 1255 | } else { |
NeilBrown | 598b9a5 | 2007-03-26 21:32:08 -0800 | [diff] [blame] | 1256 | xdr_encode_hyper(cd->offset, offset64); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | } |
NeilBrown | b602345 | 2019-03-04 14:08:22 +1100 | [diff] [blame] | 1258 | cd->offset = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1259 | } |
| 1260 | |
| 1261 | /* |
| 1262 | dprintk("encode_entry(%.*s @%ld%s)\n", |
| 1263 | namlen, name, (long) offset, plus? " plus" : ""); |
| 1264 | */ |
| 1265 | |
| 1266 | /* truncate filename if too long */ |
Kinglong Mee | 3c7aa15 | 2014-06-10 18:08:19 +0800 | [diff] [blame] | 1267 | namlen = min(namlen, NFS3_MAXNAMLEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | |
| 1269 | slen = XDR_QUADLEN(namlen); |
| 1270 | elen = slen + NFS3_ENTRY_BAGGAGE |
| 1271 | + (plus? NFS3_ENTRYPLUS_BAGGAGE : 0); |
| 1272 | |
| 1273 | if (cd->buflen < elen) { |
| 1274 | cd->common.err = nfserr_toosmall; |
| 1275 | return -EINVAL; |
| 1276 | } |
| 1277 | |
| 1278 | /* determine which page in rq_respages[] we are currently filling */ |
J. Bruce Fields | afc5940 | 2012-12-10 18:01:37 -0500 | [diff] [blame] | 1279 | for (page = cd->rqstp->rq_respages + 1; |
| 1280 | page < cd->rqstp->rq_next_page; page++) { |
| 1281 | curr_page_addr = page_address(*page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1282 | |
| 1283 | if (((caddr_t)cd->buffer >= curr_page_addr) && |
| 1284 | ((caddr_t)cd->buffer < curr_page_addr + PAGE_SIZE)) |
| 1285 | break; |
| 1286 | } |
| 1287 | |
| 1288 | if ((caddr_t)(cd->buffer + elen) < (curr_page_addr + PAGE_SIZE)) { |
| 1289 | /* encode entry in current page */ |
| 1290 | |
| 1291 | p = encode_entry_baggage(cd, p, name, namlen, ino); |
| 1292 | |
J. Bruce Fields | 8177e6d | 2009-09-04 14:13:09 -0400 | [diff] [blame] | 1293 | if (plus) |
NeilBrown | 43b0e7e | 2015-05-03 09:16:53 +1000 | [diff] [blame] | 1294 | p = encode_entryplus_baggage(cd, p, name, namlen, ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1295 | num_entry_words = p - cd->buffer; |
J. Bruce Fields | afc5940 | 2012-12-10 18:01:37 -0500 | [diff] [blame] | 1296 | } else if (*(page+1) != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1297 | /* temporarily encode entry into next page, then move back to |
| 1298 | * current and next page in rq_respages[] */ |
Al Viro | 91f0716 | 2006-10-19 23:28:57 -0700 | [diff] [blame] | 1299 | __be32 *p1, *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | int len1, len2; |
| 1301 | |
| 1302 | /* grab next page for temporary storage of entry */ |
J. Bruce Fields | afc5940 | 2012-12-10 18:01:37 -0500 | [diff] [blame] | 1303 | p1 = tmp = page_address(*(page+1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | |
| 1305 | p1 = encode_entry_baggage(cd, p1, name, namlen, ino); |
| 1306 | |
J. Bruce Fields | 8177e6d | 2009-09-04 14:13:09 -0400 | [diff] [blame] | 1307 | if (plus) |
NeilBrown | 43b0e7e | 2015-05-03 09:16:53 +1000 | [diff] [blame] | 1308 | p1 = encode_entryplus_baggage(cd, p1, name, namlen, ino); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | |
| 1310 | /* determine entry word length and lengths to go in pages */ |
| 1311 | num_entry_words = p1 - tmp; |
| 1312 | len1 = curr_page_addr + PAGE_SIZE - (caddr_t)cd->buffer; |
| 1313 | if ((num_entry_words << 2) < len1) { |
| 1314 | /* the actual number of words in the entry is less |
| 1315 | * than elen and can still fit in the current page |
| 1316 | */ |
| 1317 | memmove(p, tmp, num_entry_words << 2); |
| 1318 | p += num_entry_words; |
| 1319 | |
| 1320 | /* update offset */ |
| 1321 | cd->offset = cd->buffer + (cd->offset - tmp); |
| 1322 | } else { |
| 1323 | unsigned int offset_r = (cd->offset - tmp) << 2; |
| 1324 | |
| 1325 | /* update pointer to offset location. |
| 1326 | * This is a 64bit quantity, so we need to |
| 1327 | * deal with 3 cases: |
| 1328 | * - entirely in first page |
| 1329 | * - entirely in second page |
| 1330 | * - 4 bytes in each page |
| 1331 | */ |
| 1332 | if (offset_r + 8 <= len1) { |
| 1333 | cd->offset = p + (cd->offset - tmp); |
| 1334 | } else if (offset_r >= len1) { |
| 1335 | cd->offset -= len1 >> 2; |
| 1336 | } else { |
| 1337 | /* sitting on the fence */ |
| 1338 | BUG_ON(offset_r != len1 - 4); |
| 1339 | cd->offset = p + (cd->offset - tmp); |
| 1340 | cd->offset1 = tmp; |
| 1341 | } |
| 1342 | |
| 1343 | len2 = (num_entry_words << 2) - len1; |
| 1344 | |
| 1345 | /* move from temp page to current and next pages */ |
| 1346 | memmove(p, tmp, len1); |
| 1347 | memmove(tmp, (caddr_t)tmp+len1, len2); |
| 1348 | |
| 1349 | p = tmp + (len2 >> 2); |
| 1350 | } |
| 1351 | } |
| 1352 | else { |
| 1353 | cd->common.err = nfserr_toosmall; |
| 1354 | return -EINVAL; |
| 1355 | } |
| 1356 | |
| 1357 | cd->buflen -= num_entry_words; |
| 1358 | cd->buffer = p; |
| 1359 | cd->common.err = nfs_ok; |
| 1360 | return 0; |
| 1361 | |
| 1362 | } |
| 1363 | |
| 1364 | int |
NeilBrown | a0ad13e | 2007-01-26 00:57:10 -0800 | [diff] [blame] | 1365 | nfs3svc_encode_entry(void *cd, const char *name, |
| 1366 | int namlen, loff_t offset, u64 ino, unsigned int d_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1367 | { |
| 1368 | return encode_entry(cd, name, namlen, offset, ino, d_type, 0); |
| 1369 | } |
| 1370 | |
| 1371 | int |
NeilBrown | a0ad13e | 2007-01-26 00:57:10 -0800 | [diff] [blame] | 1372 | nfs3svc_encode_entry_plus(void *cd, const char *name, |
| 1373 | int namlen, loff_t offset, u64 ino, |
| 1374 | unsigned int d_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | { |
| 1376 | return encode_entry(cd, name, namlen, offset, ino, d_type, 1); |
| 1377 | } |
| 1378 | |
Chuck Lever | 8b70449 | 2020-11-06 13:08:45 -0500 | [diff] [blame] | 1379 | static bool |
| 1380 | svcxdr_encode_fsstat3resok(struct xdr_stream *xdr, |
| 1381 | const struct nfsd3_fsstatres *resp) |
| 1382 | { |
| 1383 | const struct kstatfs *s = &resp->stats; |
| 1384 | u64 bs = s->f_bsize; |
| 1385 | __be32 *p; |
| 1386 | |
| 1387 | p = xdr_reserve_space(xdr, XDR_UNIT * 13); |
| 1388 | if (!p) |
| 1389 | return false; |
| 1390 | p = xdr_encode_hyper(p, bs * s->f_blocks); /* total bytes */ |
| 1391 | p = xdr_encode_hyper(p, bs * s->f_bfree); /* free bytes */ |
| 1392 | p = xdr_encode_hyper(p, bs * s->f_bavail); /* user available bytes */ |
| 1393 | p = xdr_encode_hyper(p, s->f_files); /* total inodes */ |
| 1394 | p = xdr_encode_hyper(p, s->f_ffree); /* free inodes */ |
| 1395 | p = xdr_encode_hyper(p, s->f_ffree); /* user available inodes */ |
| 1396 | *p = cpu_to_be32(resp->invarsec); /* mean unchanged time */ |
| 1397 | |
| 1398 | return true; |
| 1399 | } |
| 1400 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | /* FSSTAT */ |
| 1402 | int |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 1403 | nfs3svc_encode_fsstatres(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | { |
Chuck Lever | 8b70449 | 2020-11-06 13:08:45 -0500 | [diff] [blame] | 1405 | struct xdr_stream *xdr = &rqstp->rq_res_stream; |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 1406 | struct nfsd3_fsstatres *resp = rqstp->rq_resp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1407 | |
Chuck Lever | 8b70449 | 2020-11-06 13:08:45 -0500 | [diff] [blame] | 1408 | if (!svcxdr_encode_nfsstat3(xdr, resp->status)) |
| 1409 | return 0; |
| 1410 | switch (resp->status) { |
| 1411 | case nfs_ok: |
| 1412 | if (!svcxdr_encode_post_op_attr(rqstp, xdr, &nfs3svc_null_fh)) |
| 1413 | return 0; |
| 1414 | if (!svcxdr_encode_fsstat3resok(xdr, resp)) |
| 1415 | return 0; |
| 1416 | break; |
| 1417 | default: |
| 1418 | if (!svcxdr_encode_post_op_attr(rqstp, xdr, &nfs3svc_null_fh)) |
| 1419 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1420 | } |
Chuck Lever | 8b70449 | 2020-11-06 13:08:45 -0500 | [diff] [blame] | 1421 | |
| 1422 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1423 | } |
| 1424 | |
Chuck Lever | 0a139d1 | 2020-10-22 13:42:13 -0400 | [diff] [blame] | 1425 | static bool |
| 1426 | svcxdr_encode_fsinfo3resok(struct xdr_stream *xdr, |
| 1427 | const struct nfsd3_fsinfores *resp) |
| 1428 | { |
| 1429 | __be32 *p; |
| 1430 | |
| 1431 | p = xdr_reserve_space(xdr, XDR_UNIT * 12); |
| 1432 | if (!p) |
| 1433 | return false; |
| 1434 | *p++ = cpu_to_be32(resp->f_rtmax); |
| 1435 | *p++ = cpu_to_be32(resp->f_rtpref); |
| 1436 | *p++ = cpu_to_be32(resp->f_rtmult); |
| 1437 | *p++ = cpu_to_be32(resp->f_wtmax); |
| 1438 | *p++ = cpu_to_be32(resp->f_wtpref); |
| 1439 | *p++ = cpu_to_be32(resp->f_wtmult); |
| 1440 | *p++ = cpu_to_be32(resp->f_dtpref); |
| 1441 | p = xdr_encode_hyper(p, resp->f_maxfilesize); |
| 1442 | p = encode_nfstime3(p, &nfs3svc_time_delta); |
| 1443 | *p = cpu_to_be32(resp->f_properties); |
| 1444 | |
| 1445 | return true; |
| 1446 | } |
| 1447 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1448 | /* FSINFO */ |
| 1449 | int |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 1450 | nfs3svc_encode_fsinfores(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | { |
Chuck Lever | 0a139d1 | 2020-10-22 13:42:13 -0400 | [diff] [blame] | 1452 | struct xdr_stream *xdr = &rqstp->rq_res_stream; |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 1453 | struct nfsd3_fsinfores *resp = rqstp->rq_resp; |
| 1454 | |
Chuck Lever | 0a139d1 | 2020-10-22 13:42:13 -0400 | [diff] [blame] | 1455 | if (!svcxdr_encode_nfsstat3(xdr, resp->status)) |
| 1456 | return 0; |
| 1457 | switch (resp->status) { |
| 1458 | case nfs_ok: |
| 1459 | if (!svcxdr_encode_post_op_attr(rqstp, xdr, &nfs3svc_null_fh)) |
| 1460 | return 0; |
| 1461 | if (!svcxdr_encode_fsinfo3resok(xdr, resp)) |
| 1462 | return 0; |
| 1463 | break; |
| 1464 | default: |
| 1465 | if (!svcxdr_encode_post_op_attr(rqstp, xdr, &nfs3svc_null_fh)) |
| 1466 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 | } |
| 1468 | |
Chuck Lever | 0a139d1 | 2020-10-22 13:42:13 -0400 | [diff] [blame] | 1469 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | } |
| 1471 | |
Chuck Lever | ded04a5 | 2020-11-06 13:15:09 -0500 | [diff] [blame] | 1472 | static bool |
| 1473 | svcxdr_encode_pathconf3resok(struct xdr_stream *xdr, |
| 1474 | const struct nfsd3_pathconfres *resp) |
| 1475 | { |
| 1476 | __be32 *p; |
| 1477 | |
| 1478 | p = xdr_reserve_space(xdr, XDR_UNIT * 6); |
| 1479 | if (!p) |
| 1480 | return false; |
| 1481 | *p++ = cpu_to_be32(resp->p_link_max); |
| 1482 | *p++ = cpu_to_be32(resp->p_name_max); |
| 1483 | p = xdr_encode_bool(p, resp->p_no_trunc); |
| 1484 | p = xdr_encode_bool(p, resp->p_chown_restricted); |
| 1485 | p = xdr_encode_bool(p, resp->p_case_insensitive); |
| 1486 | xdr_encode_bool(p, resp->p_case_preserving); |
| 1487 | |
| 1488 | return true; |
| 1489 | } |
| 1490 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1491 | /* PATHCONF */ |
| 1492 | int |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 1493 | nfs3svc_encode_pathconfres(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1494 | { |
Chuck Lever | ded04a5 | 2020-11-06 13:15:09 -0500 | [diff] [blame] | 1495 | struct xdr_stream *xdr = &rqstp->rq_res_stream; |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 1496 | struct nfsd3_pathconfres *resp = rqstp->rq_resp; |
| 1497 | |
Chuck Lever | ded04a5 | 2020-11-06 13:15:09 -0500 | [diff] [blame] | 1498 | if (!svcxdr_encode_nfsstat3(xdr, resp->status)) |
| 1499 | return 0; |
| 1500 | switch (resp->status) { |
| 1501 | case nfs_ok: |
| 1502 | if (!svcxdr_encode_post_op_attr(rqstp, xdr, &nfs3svc_null_fh)) |
| 1503 | return 0; |
| 1504 | if (!svcxdr_encode_pathconf3resok(xdr, resp)) |
| 1505 | return 0; |
| 1506 | break; |
| 1507 | default: |
| 1508 | if (!svcxdr_encode_post_op_attr(rqstp, xdr, &nfs3svc_null_fh)) |
| 1509 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1510 | } |
| 1511 | |
Chuck Lever | ded04a5 | 2020-11-06 13:15:09 -0500 | [diff] [blame] | 1512 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1513 | } |
| 1514 | |
| 1515 | /* COMMIT */ |
| 1516 | int |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 1517 | nfs3svc_encode_commitres(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1518 | { |
Chuck Lever | 5ef2826 | 2020-10-22 15:35:46 -0400 | [diff] [blame^] | 1519 | struct xdr_stream *xdr = &rqstp->rq_res_stream; |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 1520 | struct nfsd3_commitres *resp = rqstp->rq_resp; |
Stanislav Kinsbursky | b9c0ef8 | 2012-12-06 14:23:19 +0300 | [diff] [blame] | 1521 | |
Chuck Lever | 5ef2826 | 2020-10-22 15:35:46 -0400 | [diff] [blame^] | 1522 | if (!svcxdr_encode_nfsstat3(xdr, resp->status)) |
| 1523 | return 0; |
| 1524 | switch (resp->status) { |
| 1525 | case nfs_ok: |
| 1526 | if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->fh)) |
| 1527 | return 0; |
| 1528 | if (!svcxdr_encode_writeverf3(xdr, resp->verf)) |
| 1529 | return 0; |
| 1530 | break; |
| 1531 | default: |
| 1532 | if (!svcxdr_encode_wcc_data(rqstp, xdr, &resp->fh)) |
| 1533 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1534 | } |
Chuck Lever | 5ef2826 | 2020-10-22 15:35:46 -0400 | [diff] [blame^] | 1535 | |
| 1536 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1537 | } |
| 1538 | |
| 1539 | /* |
| 1540 | * XDR release functions |
| 1541 | */ |
Christoph Hellwig | 8537488 | 2017-05-08 18:48:24 +0200 | [diff] [blame] | 1542 | void |
| 1543 | nfs3svc_release_fhandle(struct svc_rqst *rqstp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 | { |
Christoph Hellwig | 8537488 | 2017-05-08 18:48:24 +0200 | [diff] [blame] | 1545 | struct nfsd3_attrstat *resp = rqstp->rq_resp; |
| 1546 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1547 | fh_put(&resp->fh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1548 | } |
| 1549 | |
Christoph Hellwig | 8537488 | 2017-05-08 18:48:24 +0200 | [diff] [blame] | 1550 | void |
| 1551 | nfs3svc_release_fhandle2(struct svc_rqst *rqstp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1552 | { |
Christoph Hellwig | 8537488 | 2017-05-08 18:48:24 +0200 | [diff] [blame] | 1553 | struct nfsd3_fhandle_pair *resp = rqstp->rq_resp; |
| 1554 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1555 | fh_put(&resp->fh1); |
| 1556 | fh_put(&resp->fh2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1557 | } |