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 |
| 4 | * |
| 5 | * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de> |
| 6 | */ |
| 7 | |
Al Viro | 3dadecc | 2013-01-24 02:18:08 -0500 | [diff] [blame] | 8 | #include "vfs.h" |
Boaz Harrosh | 9a74af2 | 2009-12-03 20:30:56 +0200 | [diff] [blame] | 9 | #include "xdr.h" |
J. Bruce Fields | 2e8138a | 2007-11-15 17:05:43 -0500 | [diff] [blame] | 10 | #include "auth.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | |
| 12 | #define NFSDDBG_FACILITY NFSDDBG_XDR |
| 13 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | /* |
| 15 | * Mapping of S_IF* types to NFS file types |
| 16 | */ |
| 17 | static u32 nfs_ftypes[] = { |
| 18 | NFNON, NFCHR, NFCHR, NFBAD, |
| 19 | NFDIR, NFBAD, NFBLK, NFBAD, |
| 20 | NFREG, NFBAD, NFLNK, NFBAD, |
| 21 | NFSOCK, NFBAD, NFLNK, NFBAD, |
| 22 | }; |
| 23 | |
| 24 | |
| 25 | /* |
Chuck Lever | ebcd8e8 | 2020-10-21 12:14:23 -0400 | [diff] [blame] | 26 | * Basic NFSv2 data types (RFC 1094 Section 2.3) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | */ |
Chuck Lever | ebcd8e8 | 2020-10-21 12:14:23 -0400 | [diff] [blame] | 28 | |
Al Viro | 131a21c | 2006-10-19 23:28:56 -0700 | [diff] [blame] | 29 | static __be32 * |
| 30 | decode_fh(__be32 *p, struct svc_fh *fhp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | { |
| 32 | fh_init(fhp, NFS_FHSIZE); |
| 33 | memcpy(&fhp->fh_handle.fh_base, p, NFS_FHSIZE); |
| 34 | fhp->fh_handle.fh_size = NFS_FHSIZE; |
| 35 | |
| 36 | /* FIXME: Look up export pointer here and verify |
| 37 | * Sun Secure RPC if requested */ |
| 38 | return p + (NFS_FHSIZE >> 2); |
| 39 | } |
| 40 | |
Chuck Lever | ebcd8e8 | 2020-10-21 12:14:23 -0400 | [diff] [blame] | 41 | static bool |
| 42 | svcxdr_decode_fhandle(struct xdr_stream *xdr, struct svc_fh *fhp) |
| 43 | { |
| 44 | __be32 *p; |
| 45 | |
| 46 | p = xdr_inline_decode(xdr, NFS_FHSIZE); |
| 47 | if (!p) |
| 48 | return false; |
| 49 | fh_init(fhp, NFS_FHSIZE); |
| 50 | memcpy(&fhp->fh_handle.fh_base, p, NFS_FHSIZE); |
| 51 | fhp->fh_handle.fh_size = NFS_FHSIZE; |
| 52 | |
| 53 | return true; |
| 54 | } |
| 55 | |
Andreas Gruenbacher | a257cdd | 2005-06-22 17:16:26 +0000 | [diff] [blame] | 56 | /* Helper function for NFSv2 ACL code */ |
Al Viro | 131a21c | 2006-10-19 23:28:56 -0700 | [diff] [blame] | 57 | __be32 *nfs2svc_decode_fh(__be32 *p, struct svc_fh *fhp) |
Andreas Gruenbacher | a257cdd | 2005-06-22 17:16:26 +0000 | [diff] [blame] | 58 | { |
| 59 | return decode_fh(p, fhp); |
| 60 | } |
| 61 | |
Adrian Bunk | 3ee6f61 | 2006-12-06 20:40:23 -0800 | [diff] [blame] | 62 | static __be32 * |
Al Viro | 131a21c | 2006-10-19 23:28:56 -0700 | [diff] [blame] | 63 | encode_fh(__be32 *p, struct svc_fh *fhp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | { |
| 65 | memcpy(p, &fhp->fh_handle.fh_base, NFS_FHSIZE); |
| 66 | return p + (NFS_FHSIZE>> 2); |
| 67 | } |
| 68 | |
| 69 | /* |
| 70 | * Decode a file name and make sure that the path contains |
| 71 | * no slashes or null bytes. |
| 72 | */ |
Adrian Bunk | 3ee6f61 | 2006-12-06 20:40:23 -0800 | [diff] [blame] | 73 | static __be32 * |
Chuck Lever | ee1a95b | 2007-11-01 16:56:58 -0400 | [diff] [blame] | 74 | decode_filename(__be32 *p, char **namp, unsigned int *lenp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | { |
| 76 | char *name; |
Chuck Lever | ee1a95b | 2007-11-01 16:56:58 -0400 | [diff] [blame] | 77 | unsigned int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
| 79 | if ((p = xdr_decode_string_inplace(p, namp, lenp, NFS_MAXNAMLEN)) != NULL) { |
| 80 | for (i = 0, name = *namp; i < *lenp; i++, name++) { |
| 81 | if (*name == '\0' || *name == '/') |
| 82 | return NULL; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | return p; |
| 87 | } |
| 88 | |
Chuck Lever | 6d742c1 | 2020-10-19 14:33:24 -0400 | [diff] [blame] | 89 | static bool |
| 90 | svcxdr_decode_filename(struct xdr_stream *xdr, char **name, unsigned int *len) |
| 91 | { |
| 92 | u32 size, i; |
| 93 | __be32 *p; |
| 94 | char *c; |
| 95 | |
| 96 | if (xdr_stream_decode_u32(xdr, &size) < 0) |
| 97 | return false; |
| 98 | if (size == 0 || size > NFS_MAXNAMLEN) |
| 99 | return false; |
| 100 | p = xdr_inline_decode(xdr, size); |
| 101 | if (!p) |
| 102 | return false; |
| 103 | |
| 104 | *len = size; |
| 105 | *name = (char *)p; |
| 106 | for (i = 0, c = *name; i < size; i++, c++) |
| 107 | if (*c == '\0' || *c == '/') |
| 108 | return false; |
| 109 | |
| 110 | return true; |
| 111 | } |
| 112 | |
| 113 | static bool |
| 114 | svcxdr_decode_diropargs(struct xdr_stream *xdr, struct svc_fh *fhp, |
| 115 | char **name, unsigned int *len) |
| 116 | { |
| 117 | return svcxdr_decode_fhandle(xdr, fhp) && |
| 118 | svcxdr_decode_filename(xdr, name, len); |
| 119 | } |
| 120 | |
Adrian Bunk | 3ee6f61 | 2006-12-06 20:40:23 -0800 | [diff] [blame] | 121 | static __be32 * |
Trond Myklebust | e45d1a1 | 2019-04-09 12:13:42 -0400 | [diff] [blame] | 122 | decode_sattr(__be32 *p, struct iattr *iap, struct user_namespace *userns) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | { |
| 124 | u32 tmp, tmp1; |
| 125 | |
| 126 | iap->ia_valid = 0; |
| 127 | |
| 128 | /* Sun client bug compatibility check: some sun clients seem to |
| 129 | * put 0xffff in the mode field when they mean 0xffffffff. |
| 130 | * Quoting the 4.4BSD nfs server code: Nah nah nah nah na nah. |
| 131 | */ |
| 132 | if ((tmp = ntohl(*p++)) != (u32)-1 && tmp != 0xffff) { |
| 133 | iap->ia_valid |= ATTR_MODE; |
| 134 | iap->ia_mode = tmp; |
| 135 | } |
| 136 | if ((tmp = ntohl(*p++)) != (u32)-1) { |
Trond Myklebust | e45d1a1 | 2019-04-09 12:13:42 -0400 | [diff] [blame] | 137 | iap->ia_uid = make_kuid(userns, tmp); |
Eric W. Biederman | 7c19723 | 2013-02-02 04:32:35 -0800 | [diff] [blame] | 138 | if (uid_valid(iap->ia_uid)) |
| 139 | iap->ia_valid |= ATTR_UID; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | } |
| 141 | if ((tmp = ntohl(*p++)) != (u32)-1) { |
Trond Myklebust | e45d1a1 | 2019-04-09 12:13:42 -0400 | [diff] [blame] | 142 | iap->ia_gid = make_kgid(userns, tmp); |
Eric W. Biederman | 7c19723 | 2013-02-02 04:32:35 -0800 | [diff] [blame] | 143 | if (gid_valid(iap->ia_gid)) |
| 144 | iap->ia_valid |= ATTR_GID; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | } |
| 146 | if ((tmp = ntohl(*p++)) != (u32)-1) { |
| 147 | iap->ia_valid |= ATTR_SIZE; |
| 148 | iap->ia_size = tmp; |
| 149 | } |
| 150 | tmp = ntohl(*p++); tmp1 = ntohl(*p++); |
| 151 | if (tmp != (u32)-1 && tmp1 != (u32)-1) { |
| 152 | iap->ia_valid |= ATTR_ATIME | ATTR_ATIME_SET; |
| 153 | iap->ia_atime.tv_sec = tmp; |
| 154 | iap->ia_atime.tv_nsec = tmp1 * 1000; |
| 155 | } |
| 156 | tmp = ntohl(*p++); tmp1 = ntohl(*p++); |
| 157 | if (tmp != (u32)-1 && tmp1 != (u32)-1) { |
| 158 | iap->ia_valid |= ATTR_MTIME | ATTR_MTIME_SET; |
| 159 | iap->ia_mtime.tv_sec = tmp; |
| 160 | iap->ia_mtime.tv_nsec = tmp1 * 1000; |
| 161 | /* |
| 162 | * Passing the invalid value useconds=1000000 for mtime |
| 163 | * is a Sun convention for "set both mtime and atime to |
| 164 | * current server time". It's needed to make permissions |
| 165 | * checks for the "touch" program across v2 mounts to |
| 166 | * Solaris and Irix boxes work correctly. See description of |
| 167 | * sattr in section 6.1 of "NFS Illustrated" by |
| 168 | * Brent Callaghan, Addison-Wesley, ISBN 0-201-32750-5 |
| 169 | */ |
| 170 | if (tmp1 == 1000000) |
| 171 | iap->ia_valid &= ~(ATTR_ATIME_SET|ATTR_MTIME_SET); |
| 172 | } |
| 173 | return p; |
| 174 | } |
| 175 | |
Chuck Lever | 2fdd6bd | 2020-10-21 12:39:06 -0400 | [diff] [blame^] | 176 | static bool |
| 177 | svcxdr_decode_sattr(struct svc_rqst *rqstp, struct xdr_stream *xdr, |
| 178 | struct iattr *iap) |
| 179 | { |
| 180 | u32 tmp1, tmp2; |
| 181 | __be32 *p; |
| 182 | |
| 183 | p = xdr_inline_decode(xdr, XDR_UNIT * 8); |
| 184 | if (!p) |
| 185 | return false; |
| 186 | |
| 187 | iap->ia_valid = 0; |
| 188 | |
| 189 | /* |
| 190 | * Some Sun clients put 0xffff in the mode field when they |
| 191 | * mean 0xffffffff. |
| 192 | */ |
| 193 | tmp1 = be32_to_cpup(p++); |
| 194 | if (tmp1 != (u32)-1 && tmp1 != 0xffff) { |
| 195 | iap->ia_valid |= ATTR_MODE; |
| 196 | iap->ia_mode = tmp1; |
| 197 | } |
| 198 | |
| 199 | tmp1 = be32_to_cpup(p++); |
| 200 | if (tmp1 != (u32)-1) { |
| 201 | iap->ia_uid = make_kuid(nfsd_user_namespace(rqstp), tmp1); |
| 202 | if (uid_valid(iap->ia_uid)) |
| 203 | iap->ia_valid |= ATTR_UID; |
| 204 | } |
| 205 | |
| 206 | tmp1 = be32_to_cpup(p++); |
| 207 | if (tmp1 != (u32)-1) { |
| 208 | iap->ia_gid = make_kgid(nfsd_user_namespace(rqstp), tmp1); |
| 209 | if (gid_valid(iap->ia_gid)) |
| 210 | iap->ia_valid |= ATTR_GID; |
| 211 | } |
| 212 | |
| 213 | tmp1 = be32_to_cpup(p++); |
| 214 | if (tmp1 != (u32)-1) { |
| 215 | iap->ia_valid |= ATTR_SIZE; |
| 216 | iap->ia_size = tmp1; |
| 217 | } |
| 218 | |
| 219 | tmp1 = be32_to_cpup(p++); |
| 220 | tmp2 = be32_to_cpup(p++); |
| 221 | if (tmp1 != (u32)-1 && tmp2 != (u32)-1) { |
| 222 | iap->ia_valid |= ATTR_ATIME | ATTR_ATIME_SET; |
| 223 | iap->ia_atime.tv_sec = tmp1; |
| 224 | iap->ia_atime.tv_nsec = tmp2 * NSEC_PER_USEC; |
| 225 | } |
| 226 | |
| 227 | tmp1 = be32_to_cpup(p++); |
| 228 | tmp2 = be32_to_cpup(p++); |
| 229 | if (tmp1 != (u32)-1 && tmp2 != (u32)-1) { |
| 230 | iap->ia_valid |= ATTR_MTIME | ATTR_MTIME_SET; |
| 231 | iap->ia_mtime.tv_sec = tmp1; |
| 232 | iap->ia_mtime.tv_nsec = tmp2 * NSEC_PER_USEC; |
| 233 | /* |
| 234 | * Passing the invalid value useconds=1000000 for mtime |
| 235 | * is a Sun convention for "set both mtime and atime to |
| 236 | * current server time". It's needed to make permissions |
| 237 | * checks for the "touch" program across v2 mounts to |
| 238 | * Solaris and Irix boxes work correctly. See description of |
| 239 | * sattr in section 6.1 of "NFS Illustrated" by |
| 240 | * Brent Callaghan, Addison-Wesley, ISBN 0-201-32750-5 |
| 241 | */ |
| 242 | if (tmp2 == 1000000) |
| 243 | iap->ia_valid &= ~(ATTR_ATIME_SET|ATTR_MTIME_SET); |
| 244 | } |
| 245 | |
| 246 | return true; |
| 247 | } |
| 248 | |
Al Viro | 131a21c | 2006-10-19 23:28:56 -0700 | [diff] [blame] | 249 | static __be32 * |
| 250 | encode_fattr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp, |
David Shaw | a334de2 | 2006-01-06 00:19:58 -0800 | [diff] [blame] | 251 | struct kstat *stat) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | { |
Trond Myklebust | e45d1a1 | 2019-04-09 12:13:42 -0400 | [diff] [blame] | 253 | struct user_namespace *userns = nfsd_user_namespace(rqstp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | struct dentry *dentry = fhp->fh_dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | int type; |
Deepa Dinamani | 95582b0 | 2018-05-08 19:36:02 -0700 | [diff] [blame] | 256 | struct timespec64 time; |
NeilBrown | af6a4e2 | 2007-02-14 00:33:12 -0800 | [diff] [blame] | 257 | u32 f; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | |
David Shaw | a334de2 | 2006-01-06 00:19:58 -0800 | [diff] [blame] | 259 | type = (stat->mode & S_IFMT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | |
| 261 | *p++ = htonl(nfs_ftypes[type >> 12]); |
J. Bruce Fields | 082f31a | 2014-04-03 15:10:35 -0400 | [diff] [blame] | 262 | *p++ = htonl((u32) stat->mode); |
David Shaw | a334de2 | 2006-01-06 00:19:58 -0800 | [diff] [blame] | 263 | *p++ = htonl((u32) stat->nlink); |
Trond Myklebust | e45d1a1 | 2019-04-09 12:13:42 -0400 | [diff] [blame] | 264 | *p++ = htonl((u32) from_kuid_munged(userns, stat->uid)); |
| 265 | *p++ = htonl((u32) from_kgid_munged(userns, stat->gid)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | |
David Shaw | a334de2 | 2006-01-06 00:19:58 -0800 | [diff] [blame] | 267 | if (S_ISLNK(type) && stat->size > NFS_MAXPATHLEN) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | *p++ = htonl(NFS_MAXPATHLEN); |
| 269 | } else { |
David Shaw | a334de2 | 2006-01-06 00:19:58 -0800 | [diff] [blame] | 270 | *p++ = htonl((u32) stat->size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | } |
David Shaw | a334de2 | 2006-01-06 00:19:58 -0800 | [diff] [blame] | 272 | *p++ = htonl((u32) stat->blksize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | if (S_ISCHR(type) || S_ISBLK(type)) |
David Shaw | a334de2 | 2006-01-06 00:19:58 -0800 | [diff] [blame] | 274 | *p++ = htonl(new_encode_dev(stat->rdev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | else |
| 276 | *p++ = htonl(0xffffffff); |
David Shaw | a334de2 | 2006-01-06 00:19:58 -0800 | [diff] [blame] | 277 | *p++ = htonl((u32) stat->blocks); |
NeilBrown | af6a4e2 | 2007-02-14 00:33:12 -0800 | [diff] [blame] | 278 | switch (fsid_source(fhp)) { |
| 279 | default: |
| 280 | case FSIDSOURCE_DEV: |
David Shaw | a334de2 | 2006-01-06 00:19:58 -0800 | [diff] [blame] | 281 | *p++ = htonl(new_encode_dev(stat->dev)); |
NeilBrown | af6a4e2 | 2007-02-14 00:33:12 -0800 | [diff] [blame] | 282 | break; |
| 283 | case FSIDSOURCE_FSID: |
| 284 | *p++ = htonl((u32) fhp->fh_export->ex_fsid); |
| 285 | break; |
| 286 | case FSIDSOURCE_UUID: |
| 287 | f = ((u32*)fhp->fh_export->ex_uuid)[0]; |
| 288 | f ^= ((u32*)fhp->fh_export->ex_uuid)[1]; |
| 289 | f ^= ((u32*)fhp->fh_export->ex_uuid)[2]; |
| 290 | f ^= ((u32*)fhp->fh_export->ex_uuid)[3]; |
| 291 | *p++ = htonl(f); |
| 292 | break; |
| 293 | } |
David Shaw | a334de2 | 2006-01-06 00:19:58 -0800 | [diff] [blame] | 294 | *p++ = htonl((u32) stat->ino); |
| 295 | *p++ = htonl((u32) stat->atime.tv_sec); |
| 296 | *p++ = htonl(stat->atime.tv_nsec ? stat->atime.tv_nsec / 1000 : 0); |
Amir Goldstein | 76c4794 | 2018-01-03 17:14:34 +0200 | [diff] [blame] | 297 | time = stat->mtime; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 298 | lease_get_mtime(d_inode(dentry), &time); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | *p++ = htonl((u32) time.tv_sec); |
| 300 | *p++ = htonl(time.tv_nsec ? time.tv_nsec / 1000 : 0); |
David Shaw | a334de2 | 2006-01-06 00:19:58 -0800 | [diff] [blame] | 301 | *p++ = htonl((u32) stat->ctime.tv_sec); |
| 302 | *p++ = htonl(stat->ctime.tv_nsec ? stat->ctime.tv_nsec / 1000 : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | |
| 304 | return p; |
| 305 | } |
| 306 | |
Andreas Gruenbacher | a257cdd | 2005-06-22 17:16:26 +0000 | [diff] [blame] | 307 | /* Helper function for NFSv2 ACL code */ |
J. Bruce Fields | 4f4a4fa | 2013-02-01 15:13:04 -0500 | [diff] [blame] | 308 | __be32 *nfs2svc_encode_fattr(struct svc_rqst *rqstp, __be32 *p, struct svc_fh *fhp, struct kstat *stat) |
Andreas Gruenbacher | a257cdd | 2005-06-22 17:16:26 +0000 | [diff] [blame] | 309 | { |
J. Bruce Fields | 4f4a4fa | 2013-02-01 15:13:04 -0500 | [diff] [blame] | 310 | return encode_fattr(rqstp, p, fhp, stat); |
Andreas Gruenbacher | a257cdd | 2005-06-22 17:16:26 +0000 | [diff] [blame] | 311 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | |
| 313 | /* |
| 314 | * XDR decode functions |
| 315 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | |
| 317 | int |
Chuck Lever | ebcd8e8 | 2020-10-21 12:14:23 -0400 | [diff] [blame] | 318 | nfssvc_decode_fhandleargs(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | { |
Chuck Lever | ebcd8e8 | 2020-10-21 12:14:23 -0400 | [diff] [blame] | 320 | struct xdr_stream *xdr = &rqstp->rq_arg_stream; |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 321 | struct nfsd_fhandle *args = rqstp->rq_argp; |
| 322 | |
Chuck Lever | ebcd8e8 | 2020-10-21 12:14:23 -0400 | [diff] [blame] | 323 | return svcxdr_decode_fhandle(xdr, &args->fh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | int |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 327 | nfssvc_decode_sattrargs(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | { |
Chuck Lever | 2fdd6bd | 2020-10-21 12:39:06 -0400 | [diff] [blame^] | 329 | struct xdr_stream *xdr = &rqstp->rq_arg_stream; |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 330 | struct nfsd_sattrargs *args = rqstp->rq_argp; |
| 331 | |
Chuck Lever | 2fdd6bd | 2020-10-21 12:39:06 -0400 | [diff] [blame^] | 332 | return svcxdr_decode_fhandle(xdr, &args->fh) && |
| 333 | svcxdr_decode_sattr(rqstp, xdr, &args->attrs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | int |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 337 | nfssvc_decode_diropargs(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | { |
Chuck Lever | 6d742c1 | 2020-10-19 14:33:24 -0400 | [diff] [blame] | 339 | struct xdr_stream *xdr = &rqstp->rq_arg_stream; |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 340 | struct nfsd_diropargs *args = rqstp->rq_argp; |
| 341 | |
Chuck Lever | 6d742c1 | 2020-10-19 14:33:24 -0400 | [diff] [blame] | 342 | return svcxdr_decode_diropargs(xdr, &args->fh, &args->name, &args->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | int |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 346 | nfssvc_decode_readargs(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | { |
Chuck Lever | 8c293ef | 2020-10-21 12:15:51 -0400 | [diff] [blame] | 348 | struct xdr_stream *xdr = &rqstp->rq_arg_stream; |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 349 | struct nfsd_readargs *args = rqstp->rq_argp; |
Chuck Lever | 8c293ef | 2020-10-21 12:15:51 -0400 | [diff] [blame] | 350 | u32 totalcount; |
| 351 | |
| 352 | if (!svcxdr_decode_fhandle(xdr, &args->fh)) |
| 353 | return 0; |
| 354 | if (xdr_stream_decode_u32(xdr, &args->offset) < 0) |
| 355 | return 0; |
| 356 | if (xdr_stream_decode_u32(xdr, &args->count) < 0) |
| 357 | return 0; |
| 358 | /* totalcount is ignored */ |
| 359 | if (xdr_stream_decode_u32(xdr, &totalcount) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | return 0; |
| 361 | |
Chuck Lever | 8c293ef | 2020-10-21 12:15:51 -0400 | [diff] [blame] | 362 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | int |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 366 | nfssvc_decode_writeargs(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | { |
Chuck Lever | a51b5b7 | 2020-10-21 12:18:36 -0400 | [diff] [blame] | 368 | struct xdr_stream *xdr = &rqstp->rq_arg_stream; |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 369 | struct nfsd_writeargs *args = rqstp->rq_argp; |
J. Bruce Fields | db44bac | 2017-04-25 16:21:34 -0400 | [diff] [blame] | 370 | struct kvec *head = rqstp->rq_arg.head; |
Chuck Lever | a51b5b7 | 2020-10-21 12:18:36 -0400 | [diff] [blame] | 371 | struct kvec *tail = rqstp->rq_arg.tail; |
| 372 | u32 beginoffset, totalcount; |
| 373 | size_t remaining; |
Peter Staubach | f34b9568 | 2007-05-09 02:34:48 -0700 | [diff] [blame] | 374 | |
Chuck Lever | a51b5b7 | 2020-10-21 12:18:36 -0400 | [diff] [blame] | 375 | if (!svcxdr_decode_fhandle(xdr, &args->fh)) |
| 376 | return 0; |
| 377 | /* beginoffset is ignored */ |
| 378 | if (xdr_stream_decode_u32(xdr, &beginoffset) < 0) |
| 379 | return 0; |
| 380 | if (xdr_stream_decode_u32(xdr, &args->offset) < 0) |
| 381 | return 0; |
| 382 | /* totalcount is ignored */ |
| 383 | if (xdr_stream_decode_u32(xdr, &totalcount) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | return 0; |
| 385 | |
Chuck Lever | a51b5b7 | 2020-10-21 12:18:36 -0400 | [diff] [blame] | 386 | /* opaque data */ |
| 387 | if (xdr_stream_decode_u32(xdr, &args->len) < 0) |
Peter Staubach | f34b9568 | 2007-05-09 02:34:48 -0700 | [diff] [blame] | 388 | return 0; |
Chuck Lever | a51b5b7 | 2020-10-21 12:18:36 -0400 | [diff] [blame] | 389 | if (args->len > NFSSVC_MAXBLKSIZE_V2) |
J. Bruce Fields | 13bf9fb | 2017-04-21 15:26:30 -0400 | [diff] [blame] | 390 | return 0; |
Chuck Lever | a51b5b7 | 2020-10-21 12:18:36 -0400 | [diff] [blame] | 391 | remaining = head->iov_len + rqstp->rq_arg.page_len + tail->iov_len; |
| 392 | remaining -= xdr_stream_pos(xdr); |
| 393 | if (remaining < xdr_align_size(args->len)) |
Peter Staubach | f34b9568 | 2007-05-09 02:34:48 -0700 | [diff] [blame] | 394 | return 0; |
Chuck Lever | a51b5b7 | 2020-10-21 12:18:36 -0400 | [diff] [blame] | 395 | args->first.iov_base = xdr->p; |
| 396 | args->first.iov_len = head->iov_len - xdr_stream_pos(xdr); |
Peter Staubach | f34b9568 | 2007-05-09 02:34:48 -0700 | [diff] [blame] | 397 | |
Peter Staubach | f34b9568 | 2007-05-09 02:34:48 -0700 | [diff] [blame] | 398 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | int |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 402 | nfssvc_decode_createargs(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | { |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 404 | struct nfsd_createargs *args = rqstp->rq_argp; |
| 405 | |
NeilBrown | 072f62e | 2007-05-09 02:34:57 -0700 | [diff] [blame] | 406 | if ( !(p = decode_fh(p, &args->fh)) |
| 407 | || !(p = decode_filename(p, &args->name, &args->len))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | return 0; |
Trond Myklebust | e45d1a1 | 2019-04-09 12:13:42 -0400 | [diff] [blame] | 409 | p = decode_sattr(p, &args->attrs, nfsd_user_namespace(rqstp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | |
| 411 | return xdr_argsize_check(rqstp, p); |
| 412 | } |
| 413 | |
| 414 | int |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 415 | nfssvc_decode_renameargs(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | { |
Chuck Lever | 62aa557 | 2020-10-21 12:35:41 -0400 | [diff] [blame] | 417 | struct xdr_stream *xdr = &rqstp->rq_arg_stream; |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 418 | struct nfsd_renameargs *args = rqstp->rq_argp; |
| 419 | |
Chuck Lever | 62aa557 | 2020-10-21 12:35:41 -0400 | [diff] [blame] | 420 | return svcxdr_decode_diropargs(xdr, &args->ffh, |
| 421 | &args->fname, &args->flen) && |
| 422 | svcxdr_decode_diropargs(xdr, &args->tfh, |
| 423 | &args->tname, &args->tlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | int |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 427 | nfssvc_decode_linkargs(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | { |
Chuck Lever | 77edcdf | 2020-10-21 12:34:24 -0400 | [diff] [blame] | 429 | struct xdr_stream *xdr = &rqstp->rq_arg_stream; |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 430 | struct nfsd_linkargs *args = rqstp->rq_argp; |
| 431 | |
Chuck Lever | 77edcdf | 2020-10-21 12:34:24 -0400 | [diff] [blame] | 432 | return svcxdr_decode_fhandle(xdr, &args->ffh) && |
| 433 | svcxdr_decode_diropargs(xdr, &args->tfh, |
| 434 | &args->tname, &args->tlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | int |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 438 | nfssvc_decode_symlinkargs(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | { |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 440 | struct nfsd_symlinkargs *args = rqstp->rq_argp; |
Chuck Lever | 38a7031 | 2018-03-27 10:54:21 -0400 | [diff] [blame] | 441 | char *base = (char *)p; |
| 442 | size_t xdrlen; |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 443 | |
NeilBrown | 072f62e | 2007-05-09 02:34:57 -0700 | [diff] [blame] | 444 | if ( !(p = decode_fh(p, &args->ffh)) |
Chuck Lever | 38a7031 | 2018-03-27 10:54:21 -0400 | [diff] [blame] | 445 | || !(p = decode_filename(p, &args->fname, &args->flen))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | return 0; |
| 447 | |
Chuck Lever | 38a7031 | 2018-03-27 10:54:21 -0400 | [diff] [blame] | 448 | args->tlen = ntohl(*p++); |
| 449 | if (args->tlen == 0) |
| 450 | return 0; |
| 451 | |
| 452 | args->first.iov_base = p; |
| 453 | args->first.iov_len = rqstp->rq_arg.head[0].iov_len; |
| 454 | args->first.iov_len -= (char *)p - base; |
| 455 | |
| 456 | /* This request is never larger than a page. Therefore, |
| 457 | * transport will deliver either: |
| 458 | * 1. pathname in the pagelist -> sattr is in the tail. |
| 459 | * 2. everything in the head buffer -> sattr is in the head. |
| 460 | */ |
| 461 | if (rqstp->rq_arg.page_len) { |
| 462 | if (args->tlen != rqstp->rq_arg.page_len) |
| 463 | return 0; |
| 464 | p = rqstp->rq_arg.tail[0].iov_base; |
| 465 | } else { |
| 466 | xdrlen = XDR_QUADLEN(args->tlen); |
| 467 | if (xdrlen > args->first.iov_len - (8 * sizeof(__be32))) |
| 468 | return 0; |
| 469 | p += xdrlen; |
| 470 | } |
Trond Myklebust | e45d1a1 | 2019-04-09 12:13:42 -0400 | [diff] [blame] | 471 | decode_sattr(p, &args->attrs, nfsd_user_namespace(rqstp)); |
Chuck Lever | 38a7031 | 2018-03-27 10:54:21 -0400 | [diff] [blame] | 472 | |
| 473 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | int |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 477 | nfssvc_decode_readdirargs(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | { |
Chuck Lever | 8688361 | 2020-10-19 14:15:51 -0400 | [diff] [blame] | 479 | struct xdr_stream *xdr = &rqstp->rq_arg_stream; |
Christoph Hellwig | 026fec7 | 2017-05-08 19:01:48 +0200 | [diff] [blame] | 480 | struct nfsd_readdirargs *args = rqstp->rq_argp; |
| 481 | |
Chuck Lever | 8688361 | 2020-10-19 14:15:51 -0400 | [diff] [blame] | 482 | if (!svcxdr_decode_fhandle(xdr, &args->fh)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | return 0; |
Chuck Lever | 8688361 | 2020-10-19 14:15:51 -0400 | [diff] [blame] | 484 | if (xdr_stream_decode_u32(xdr, &args->cookie) < 0) |
| 485 | return 0; |
| 486 | if (xdr_stream_decode_u32(xdr, &args->count) < 0) |
| 487 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | |
Chuck Lever | 8688361 | 2020-10-19 14:15:51 -0400 | [diff] [blame] | 489 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | /* |
| 493 | * XDR encode functions |
| 494 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | |
| 496 | int |
Chuck Lever | cc028a1 | 2020-10-02 15:52:44 -0400 | [diff] [blame] | 497 | nfssvc_encode_stat(struct svc_rqst *rqstp, __be32 *p) |
| 498 | { |
| 499 | struct nfsd_stat *resp = rqstp->rq_resp; |
| 500 | |
| 501 | *p++ = resp->status; |
| 502 | return xdr_ressize_check(rqstp, p); |
| 503 | } |
| 504 | |
| 505 | int |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 506 | nfssvc_encode_attrstat(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | { |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 508 | struct nfsd_attrstat *resp = rqstp->rq_resp; |
| 509 | |
Chuck Lever | cc028a1 | 2020-10-02 15:52:44 -0400 | [diff] [blame] | 510 | *p++ = resp->status; |
Chuck Lever | f0af221 | 2020-10-01 18:59:49 -0400 | [diff] [blame] | 511 | if (resp->status != nfs_ok) |
Chuck Lever | cc028a1 | 2020-10-02 15:52:44 -0400 | [diff] [blame] | 512 | goto out; |
David Shaw | a334de2 | 2006-01-06 00:19:58 -0800 | [diff] [blame] | 513 | p = encode_fattr(rqstp, p, &resp->fh, &resp->stat); |
Chuck Lever | cc028a1 | 2020-10-02 15:52:44 -0400 | [diff] [blame] | 514 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | return xdr_ressize_check(rqstp, p); |
| 516 | } |
| 517 | |
| 518 | int |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 519 | nfssvc_encode_diropres(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | { |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 521 | struct nfsd_diropres *resp = rqstp->rq_resp; |
| 522 | |
Chuck Lever | cc028a1 | 2020-10-02 15:52:44 -0400 | [diff] [blame] | 523 | *p++ = resp->status; |
Chuck Lever | f0af221 | 2020-10-01 18:59:49 -0400 | [diff] [blame] | 524 | if (resp->status != nfs_ok) |
Chuck Lever | cc028a1 | 2020-10-02 15:52:44 -0400 | [diff] [blame] | 525 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | p = encode_fh(p, &resp->fh); |
David Shaw | a334de2 | 2006-01-06 00:19:58 -0800 | [diff] [blame] | 527 | p = encode_fattr(rqstp, p, &resp->fh, &resp->stat); |
Chuck Lever | cc028a1 | 2020-10-02 15:52:44 -0400 | [diff] [blame] | 528 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | return xdr_ressize_check(rqstp, p); |
| 530 | } |
| 531 | |
| 532 | int |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 533 | nfssvc_encode_readlinkres(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | { |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 535 | struct nfsd_readlinkres *resp = rqstp->rq_resp; |
Chuck Lever | 76e5492 | 2020-11-05 10:24:19 -0500 | [diff] [blame] | 536 | struct kvec *head = rqstp->rq_res.head; |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 537 | |
Chuck Lever | cc028a1 | 2020-10-02 15:52:44 -0400 | [diff] [blame] | 538 | *p++ = resp->status; |
Chuck Lever | f0af221 | 2020-10-01 18:59:49 -0400 | [diff] [blame] | 539 | if (resp->status != nfs_ok) |
| 540 | return xdr_ressize_check(rqstp, p); |
| 541 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | *p++ = htonl(resp->len); |
| 543 | xdr_ressize_check(rqstp, p); |
| 544 | rqstp->rq_res.page_len = resp->len; |
| 545 | if (resp->len & 3) { |
| 546 | /* need to pad the tail */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | rqstp->rq_res.tail[0].iov_base = p; |
| 548 | *p = 0; |
| 549 | rqstp->rq_res.tail[0].iov_len = 4 - (resp->len&3); |
| 550 | } |
Chuck Lever | 76e5492 | 2020-11-05 10:24:19 -0500 | [diff] [blame] | 551 | if (svc_encode_result_payload(rqstp, head->iov_len, resp->len)) |
| 552 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | return 1; |
| 554 | } |
| 555 | |
| 556 | int |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 557 | nfssvc_encode_readres(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | { |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 559 | struct nfsd_readres *resp = rqstp->rq_resp; |
Chuck Lever | 76e5492 | 2020-11-05 10:24:19 -0500 | [diff] [blame] | 560 | struct kvec *head = rqstp->rq_res.head; |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 561 | |
Chuck Lever | cc028a1 | 2020-10-02 15:52:44 -0400 | [diff] [blame] | 562 | *p++ = resp->status; |
Chuck Lever | f0af221 | 2020-10-01 18:59:49 -0400 | [diff] [blame] | 563 | if (resp->status != nfs_ok) |
| 564 | return xdr_ressize_check(rqstp, p); |
| 565 | |
David Shaw | a334de2 | 2006-01-06 00:19:58 -0800 | [diff] [blame] | 566 | p = encode_fattr(rqstp, p, &resp->fh, &resp->stat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | *p++ = htonl(resp->count); |
| 568 | xdr_ressize_check(rqstp, p); |
| 569 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 570 | /* now update rqstp->rq_res to reflect data as well */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | rqstp->rq_res.page_len = resp->count; |
| 572 | if (resp->count & 3) { |
| 573 | /* need to pad the tail */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | rqstp->rq_res.tail[0].iov_base = p; |
| 575 | *p = 0; |
| 576 | rqstp->rq_res.tail[0].iov_len = 4 - (resp->count&3); |
| 577 | } |
Chuck Lever | 76e5492 | 2020-11-05 10:24:19 -0500 | [diff] [blame] | 578 | if (svc_encode_result_payload(rqstp, head->iov_len, resp->count)) |
| 579 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | return 1; |
| 581 | } |
| 582 | |
| 583 | int |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 584 | nfssvc_encode_readdirres(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | { |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 586 | struct nfsd_readdirres *resp = rqstp->rq_resp; |
| 587 | |
Chuck Lever | cc028a1 | 2020-10-02 15:52:44 -0400 | [diff] [blame] | 588 | *p++ = resp->status; |
Chuck Lever | f0af221 | 2020-10-01 18:59:49 -0400 | [diff] [blame] | 589 | if (resp->status != nfs_ok) |
| 590 | return xdr_ressize_check(rqstp, p); |
| 591 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | xdr_ressize_check(rqstp, p); |
| 593 | p = resp->buffer; |
| 594 | *p++ = 0; /* no more entries */ |
| 595 | *p++ = htonl((resp->common.err == nfserr_eof)); |
| 596 | rqstp->rq_res.page_len = (((unsigned long)p-1) & ~PAGE_MASK)+1; |
| 597 | |
| 598 | return 1; |
| 599 | } |
| 600 | |
| 601 | int |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 602 | nfssvc_encode_statfsres(struct svc_rqst *rqstp, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | { |
Christoph Hellwig | 63f8de3 | 2017-05-08 19:42:02 +0200 | [diff] [blame] | 604 | struct nfsd_statfsres *resp = rqstp->rq_resp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | struct kstatfs *stat = &resp->stats; |
| 606 | |
Chuck Lever | cc028a1 | 2020-10-02 15:52:44 -0400 | [diff] [blame] | 607 | *p++ = resp->status; |
Chuck Lever | f0af221 | 2020-10-01 18:59:49 -0400 | [diff] [blame] | 608 | if (resp->status != nfs_ok) |
| 609 | return xdr_ressize_check(rqstp, p); |
| 610 | |
Greg Banks | 7adae48 | 2006-10-04 02:15:47 -0700 | [diff] [blame] | 611 | *p++ = htonl(NFSSVC_MAXBLKSIZE_V2); /* max transfer size */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | *p++ = htonl(stat->f_bsize); |
| 613 | *p++ = htonl(stat->f_blocks); |
| 614 | *p++ = htonl(stat->f_bfree); |
| 615 | *p++ = htonl(stat->f_bavail); |
| 616 | return xdr_ressize_check(rqstp, p); |
| 617 | } |
| 618 | |
| 619 | int |
NeilBrown | a0ad13e | 2007-01-26 00:57:10 -0800 | [diff] [blame] | 620 | nfssvc_encode_entry(void *ccdv, const char *name, |
| 621 | int namlen, loff_t offset, u64 ino, unsigned int d_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | { |
NeilBrown | a0ad13e | 2007-01-26 00:57:10 -0800 | [diff] [blame] | 623 | struct readdir_cd *ccd = ccdv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | struct nfsd_readdirres *cd = container_of(ccd, struct nfsd_readdirres, common); |
Al Viro | 131a21c | 2006-10-19 23:28:56 -0700 | [diff] [blame] | 625 | __be32 *p = cd->buffer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | int buflen, slen; |
| 627 | |
| 628 | /* |
| 629 | dprintk("nfsd: entry(%.*s off %ld ino %ld)\n", |
| 630 | namlen, name, offset, ino); |
| 631 | */ |
| 632 | |
| 633 | if (offset > ~((u32) 0)) { |
| 634 | cd->common.err = nfserr_fbig; |
| 635 | return -EINVAL; |
| 636 | } |
| 637 | if (cd->offset) |
| 638 | *cd->offset = htonl(offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | |
Kinglong Mee | 3c7aa15 | 2014-06-10 18:08:19 +0800 | [diff] [blame] | 640 | /* truncate filename */ |
| 641 | namlen = min(namlen, NFS2_MAXNAMLEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | slen = XDR_QUADLEN(namlen); |
Kinglong Mee | 3c7aa15 | 2014-06-10 18:08:19 +0800 | [diff] [blame] | 643 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | if ((buflen = cd->buflen - slen - 4) < 0) { |
| 645 | cd->common.err = nfserr_toosmall; |
| 646 | return -EINVAL; |
| 647 | } |
Peter Staubach | 40ee5dc | 2007-08-16 12:10:07 -0400 | [diff] [blame] | 648 | if (ino > ~((u32) 0)) { |
| 649 | cd->common.err = nfserr_fbig; |
| 650 | return -EINVAL; |
| 651 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | *p++ = xdr_one; /* mark entry present */ |
| 653 | *p++ = htonl((u32) ino); /* file id */ |
| 654 | p = xdr_encode_array(p, name, namlen);/* name length & name */ |
| 655 | cd->offset = p; /* remember pointer */ |
Al Viro | 131a21c | 2006-10-19 23:28:56 -0700 | [diff] [blame] | 656 | *p++ = htonl(~0U); /* offset of next entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | |
| 658 | cd->buflen = buflen; |
| 659 | cd->buffer = p; |
| 660 | cd->common.err = nfs_ok; |
| 661 | return 0; |
| 662 | } |
| 663 | |
| 664 | /* |
| 665 | * XDR release functions |
| 666 | */ |
Chuck Lever | 1841b9b | 2020-10-01 18:59:44 -0400 | [diff] [blame] | 667 | void nfssvc_release_attrstat(struct svc_rqst *rqstp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | { |
Chuck Lever | 1841b9b | 2020-10-01 18:59:44 -0400 | [diff] [blame] | 669 | struct nfsd_attrstat *resp = rqstp->rq_resp; |
| 670 | |
| 671 | fh_put(&resp->fh); |
| 672 | } |
| 673 | |
| 674 | void nfssvc_release_diropres(struct svc_rqst *rqstp) |
| 675 | { |
| 676 | struct nfsd_diropres *resp = rqstp->rq_resp; |
| 677 | |
| 678 | fh_put(&resp->fh); |
| 679 | } |
| 680 | |
| 681 | void nfssvc_release_readres(struct svc_rqst *rqstp) |
| 682 | { |
| 683 | struct nfsd_readres *resp = rqstp->rq_resp; |
Christoph Hellwig | 8537488 | 2017-05-08 18:48:24 +0200 | [diff] [blame] | 684 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | fh_put(&resp->fh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | } |