1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * XDR support for nfsd 4 * 5 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de> 6 */ 7 8 #include <linux/filelock.h> 9 10 #include "vfs.h" 11 #include "nfserr.h" 12 #include "xdr.h" 13 #include "nfs2xdr_gen.h" 14 #include "auth.h" 15 #include "trace.h" 16 17 /* 18 * Linux-internal ftype values for socket and unknown inodes, not 19 * in RFC 1094's wire enum; values match enum nfs_ftype. 20 */ 21 #define NFBAD (7) 22 #define NFSOCK (6) 23 24 /* 25 * Mapping of S_IF* types to NFS file types 26 */ 27 static const u32 nfs_ftypes[] = { 28 NFNON, NFCHR, NFCHR, NFBAD, 29 NFDIR, NFBAD, NFBLK, NFBAD, 30 NFREG, NFBAD, NFLNK, NFBAD, 31 NFSOCK, NFBAD, NFLNK, NFBAD, 32 }; 33 34 35 /* 36 * Basic NFSv2 data types (RFC 1094 Section 2.3) 37 */ 38 39 /** 40 * svcxdr_encode_stat - Encode an NFSv2 status code 41 * @xdr: XDR stream 42 * @status: status value to encode 43 * 44 * Return values: 45 * %false: Send buffer space was exhausted 46 * %true: Success 47 */ 48 bool 49 svcxdr_encode_stat(struct xdr_stream *xdr, __be32 status) 50 { 51 __be32 *p; 52 53 p = xdr_reserve_space(xdr, sizeof(status)); 54 if (!p) 55 return false; 56 *p = status; 57 58 return true; 59 } 60 61 /** 62 * svcxdr_decode_fhandle - Decode an NFSv2 file handle 63 * @xdr: XDR stream positioned at an encoded NFSv2 FH 64 * @fhp: OUT: filled-in server file handle 65 * 66 * Return values: 67 * %false: The encoded file handle was not valid 68 * %true: @fhp has been initialized 69 */ 70 bool 71 svcxdr_decode_fhandle(struct xdr_stream *xdr, struct svc_fh *fhp) 72 { 73 __be32 *p; 74 75 p = xdr_inline_decode(xdr, NFS_FHSIZE); 76 if (!p) 77 return false; 78 fh_init(fhp, NFS_FHSIZE); 79 memcpy(&fhp->fh_handle.fh_raw, p, NFS_FHSIZE); 80 fhp->fh_handle.fh_size = NFS_FHSIZE; 81 82 return true; 83 } 84 85 static __be32 * 86 encode_timeval(__be32 *p, const struct timespec64 *time) 87 { 88 *p++ = cpu_to_be32((u32)time->tv_sec); 89 if (time->tv_nsec) 90 *p++ = cpu_to_be32(time->tv_nsec / NSEC_PER_USEC); 91 else 92 *p++ = xdr_zero; 93 return p; 94 } 95 96 /** 97 * svcxdr_encode_fattr - Encode NFSv2 file attributes 98 * @rqstp: Context of a completed RPC transaction 99 * @xdr: XDR stream 100 * @fhp: File handle to encode 101 * @stat: Attributes to encode 102 * 103 * Return values: 104 * %false: Send buffer space was exhausted 105 * %true: Success 106 */ 107 bool 108 svcxdr_encode_fattr(struct svc_rqst *rqstp, struct xdr_stream *xdr, 109 const struct svc_fh *fhp, const struct kstat *stat) 110 { 111 struct user_namespace *userns = nfsd_user_namespace(rqstp); 112 struct dentry *dentry = fhp->fh_dentry; 113 int type = stat->mode & S_IFMT; 114 struct timespec64 time; 115 __be32 *p; 116 u32 fsid; 117 118 p = xdr_reserve_space(xdr, XDR_UNIT * 17); 119 if (!p) 120 return false; 121 122 *p++ = cpu_to_be32(nfs_ftypes[type >> 12]); 123 *p++ = cpu_to_be32((u32)stat->mode); 124 *p++ = cpu_to_be32((u32)stat->nlink); 125 *p++ = cpu_to_be32((u32)from_kuid_munged(userns, stat->uid)); 126 *p++ = cpu_to_be32((u32)from_kgid_munged(userns, stat->gid)); 127 128 if (S_ISLNK(type) && stat->size > NFS_MAXPATHLEN) 129 *p++ = cpu_to_be32(NFS_MAXPATHLEN); 130 else 131 *p++ = cpu_to_be32((u32) stat->size); 132 *p++ = cpu_to_be32((u32) stat->blksize); 133 if (S_ISCHR(type) || S_ISBLK(type)) 134 *p++ = cpu_to_be32(new_encode_dev(stat->rdev)); 135 else 136 *p++ = cpu_to_be32(0xffffffff); 137 *p++ = cpu_to_be32((u32)stat->blocks); 138 139 switch (fsid_source(fhp)) { 140 case FSIDSOURCE_FSID: 141 fsid = (u32)fhp->fh_export->ex_fsid; 142 break; 143 case FSIDSOURCE_UUID: 144 fsid = ((u32 *)fhp->fh_export->ex_uuid)[0]; 145 fsid ^= ((u32 *)fhp->fh_export->ex_uuid)[1]; 146 fsid ^= ((u32 *)fhp->fh_export->ex_uuid)[2]; 147 fsid ^= ((u32 *)fhp->fh_export->ex_uuid)[3]; 148 break; 149 default: 150 fsid = new_encode_dev(stat->dev); 151 break; 152 } 153 *p++ = cpu_to_be32(fsid); 154 155 *p++ = cpu_to_be32((u32)stat->ino); 156 p = encode_timeval(p, &stat->atime); 157 time = stat->mtime; 158 lease_get_mtime(d_inode(dentry), &time); 159 p = encode_timeval(p, &time); 160 encode_timeval(p, &stat->ctime); 161 162 return true; 163 } 164 165 /* 166 * XDR decode functions 167 */ 168 169 bool 170 nfssvc_decode_fhandleargs(struct svc_rqst *rqstp, struct xdr_stream *xdr) 171 { 172 struct nfsd_fhandle *args = rqstp->rq_argp; 173 174 return svcxdr_decode_fhandle(xdr, &args->fh); 175 } 176 177 /* 178 * XDR encode functions 179 */ 180 181 bool 182 nfssvc_encode_attrstatres(struct svc_rqst *rqstp, struct xdr_stream *xdr) 183 { 184 struct nfsd_attrstat *resp = rqstp->rq_resp; 185 186 if (!svcxdr_encode_stat(xdr, resp->status)) 187 return false; 188 switch (resp->status) { 189 case nfs_ok: 190 if (!svcxdr_encode_fattr(rqstp, xdr, &resp->fh, &resp->stat)) 191 return false; 192 break; 193 } 194 195 return true; 196 } 197 198 /* 199 * READDIR reply entry list (RFC 1094). The entry list is the 200 * value-follows form of "entry *entries": each entry prefixed by TRUE, 201 * the sequence closed by FALSE. xdrgen's aggregate codec owns that 202 * framing; the hooks below stream one entry at a time straight from the 203 * directory into the live reply, mirroring nfsd4_encode_dirlist4. The 204 * directory was opened in nfsd_proc_readdir(), so reading and encoding 205 * happen together here during reply encoding. 206 */ 207 208 /** 209 * nfssvc_encode_nfscookie - Back-patch a directory entry cookie 210 * @xdr: stream holding the reserved cookie slot 211 * @pos: byte position of the cookie slot, or 0 when there is none 212 * @cookie: cookie value to write 213 * 214 * An NFSv2 entry's cookie is the offset at which the following entry is 215 * read. That offset is not known until the following entry is pulled, 216 * so each entry is encoded with a placeholder cookie that a later call 217 * overwrites once the resume offset is known. 218 */ 219 void nfssvc_encode_nfscookie(struct xdr_stream *xdr, unsigned int pos, 220 u32 cookie) 221 { 222 __be32 wire_cookie = cpu_to_be32(cookie); 223 224 if (!pos) 225 return; 226 write_bytes_to_xdr_buf(xdr->buf, pos, &wire_cookie, XDR_UNIT); 227 } 228 229 /** 230 * nfs2_readdirok_encode_begin - size the streaming entry budget 231 * @c: aggregate cursor for the entry list 232 * 233 * Derive the entry byte budget from the space left in the live reply 234 * and the client's count hint, reserving room for the list terminator 235 * and the eof flag that follow the entries. 236 * 237 * Return: true. 238 */ 239 bool nfs2_readdirok_encode_begin(struct xdrgen_aggregate_cursor *c) 240 { 241 struct svc_rqst *rqstp = c->ctx; 242 struct readdirres_wrapper *resp = rqstp->rq_resp; 243 struct xdr_stream *xdr = c->xdr; 244 int bytes_left; 245 246 /* Reserve the terminator FALSE and the eof bool (two words). */ 247 bytes_left = xdr->buf->buflen - xdr->buf->len - XDR_UNIT * 2; 248 if (bytes_left < 0) 249 bytes_left = 0; 250 resp->space_left = min_t(u32, bytes_left, 251 clamp(resp->count, (u32)(XDR_UNIT * 2), 252 (u32)PAGE_SIZE) - XDR_UNIT * 2); 253 resp->cookie_offset = 0; 254 return true; 255 } 256 257 /** 258 * nfs2_readdirok_encode - stream the next READDIR entry 259 * @c: aggregate cursor for the entry list 260 * @out: OUT: entry the framing encodes when one is produced 261 * 262 * Back-patch the previous entry's placeholder cookie with this entry's 263 * resume offset, then, if the reply budget allows, project the next 264 * directory entry into @out for the generated per-entry encoder. 265 * 266 * Return: true when @out holds an entry to encode; false to end the 267 * list -- because the directory is exhausted, a host error struck, or 268 * the reply budget filled. On budget exhaustion the previous entry's 269 * cookie already points at the rejected entry, the client's resume 270 * point. 271 */ 272 bool nfs2_readdirok_encode(struct xdrgen_aggregate_cursor *c, 273 struct entry *out) 274 { 275 struct svc_rqst *rqstp = c->ctx; 276 struct readdirres_wrapper *resp = rqstp->rq_resp; 277 struct xdr_stream *xdr = c->xdr; 278 struct buffered_dirent *de; 279 int namlen; 280 u32 need; 281 282 /* 283 * The previous entry was just encoded; commit the stream and note 284 * its cookie slot -- the entry's last XDR word -- so it can be 285 * back-patched once this entry's resume offset is known. 286 */ 287 if (c->index) { 288 xdr_commit_encode(xdr); 289 resp->cookie_offset = xdr->buf->len - XDR_UNIT; 290 } 291 292 de = nfsd_readdir_next(&resp->iter); 293 if (!de) 294 return false; 295 296 /* The previous entry's cookie is this entry's resume offset. */ 297 nfssvc_encode_nfscookie(xdr, resp->cookie_offset, (u32)de->offset); 298 299 namlen = min_t(int, de->namlen, NFS_MAXNAMLEN); 300 301 /* value-follows + fileid + name (length + data) + cookie */ 302 need = XDR_UNIT * (4 + XDR_QUADLEN(namlen)); 303 if (need > resp->space_left) 304 return false; 305 resp->space_left -= need; 306 307 out->fileid = (u32)de->ino; 308 out->name.len = namlen; 309 out->name.data = (unsigned char *)de->name; 310 memset(out->cookie, 0, sizeof(out->cookie)); /* back-patched later */ 311 312 trace_nfsd_dirent(resp->iter.fhp, de->ino, de->name, namlen); 313 return true; 314 } 315 316 /** 317 * nfs2_readdirok_encode_end - finish the streamed entry list 318 * @c: aggregate cursor for the entry list 319 * @ok: false if the framing hit a wire error while encoding the list 320 * 321 * Back-patch the final entry's cookie with the directory's resume 322 * offset and report eof. A list cut short by the reply budget or a 323 * host error reports eof false, so the client reads the rest with a 324 * follow-up request. 325 * 326 * Return: true. 327 */ 328 bool nfs2_readdirok_encode_end(struct xdrgen_aggregate_cursor *c, bool ok) 329 { 330 struct svc_rqst *rqstp = c->ctx; 331 struct readdirres_wrapper *resp = rqstp->rq_resp; 332 struct xdr_stream *xdr = c->xdr; 333 334 if (ok) 335 nfssvc_encode_nfscookie(xdr, resp->cookie_offset, 336 (u32)resp->iter.offset); 337 resp->xdrgen.u.readdirok.eof = resp->iter.eof; 338 339 /* 340 * The xdr_stream primitives don't manage rq_next_page, and 341 * svcrdma retains only the pages below it for Send completion. 342 * The eof word that follows starts a new page when this one is 343 * full. 344 */ 345 rqstp->rq_next_page = xdr->page_ptr + 1; 346 if (xdr->p == xdr->end) 347 rqstp->rq_next_page++; 348 return true; 349 } 350 351 /* 352 * A server never decodes a READDIR result; these satisfy the linkage 353 * of the generated (unused) decoder. 354 */ 355 bool nfs2_readdirok_decode_begin(struct xdrgen_aggregate_cursor *c) 356 { 357 return false; 358 } 359 360 bool nfs2_readdirok_decode(struct xdrgen_aggregate_cursor *c, 361 const struct entry *in) 362 { 363 return false; 364 } 365 366 bool nfs2_readdirok_decode_end(struct xdrgen_aggregate_cursor *c, bool ok) 367 { 368 return false; 369 } 370 371 /* 372 * XDR release functions 373 */ 374 void nfssvc_release_attrstat(struct svc_rqst *rqstp) 375 { 376 struct nfsd_attrstat *resp = rqstp->rq_resp; 377 378 fh_put(&resp->fh); 379 } 380 381 void nfssvc_release_readdirres(struct svc_rqst *rqstp) 382 { 383 struct readdirres_wrapper *resp = rqstp->rq_resp; 384 385 nfsd_readdir_close(&resp->iter); 386 fh_put(&resp->fh); 387 } 388