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