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