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