1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1989, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Rick Macklem at The University of Guelph. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 */ 35 36 #include <sys/cdefs.h> 37 __FBSDID("$FreeBSD$"); 38 39 #include <sys/capsicum.h> 40 41 /* 42 * Functions that perform the vfs operations required by the routines in 43 * nfsd_serv.c. It is hoped that this change will make the server more 44 * portable. 45 */ 46 47 #include <fs/nfs/nfsport.h> 48 #include <sys/hash.h> 49 #include <sys/sysctl.h> 50 #include <nlm/nlm_prot.h> 51 #include <nlm/nlm.h> 52 53 FEATURE(nfsd, "NFSv4 server"); 54 55 extern u_int32_t newnfs_true, newnfs_false, newnfs_xdrneg1; 56 extern int nfsrv_useacl; 57 extern int newnfs_numnfsd; 58 extern struct mount nfsv4root_mnt; 59 extern struct nfsrv_stablefirst nfsrv_stablefirst; 60 extern void (*nfsd_call_servertimer)(void); 61 extern SVCPOOL *nfsrvd_pool; 62 extern struct nfsv4lock nfsd_suspend_lock; 63 extern struct nfsclienthashhead *nfsclienthash; 64 extern struct nfslockhashhead *nfslockhash; 65 extern struct nfssessionhash *nfssessionhash; 66 extern int nfsrv_sessionhashsize; 67 extern struct nfsstatsv1 nfsstatsv1; 68 struct vfsoptlist nfsv4root_opt, nfsv4root_newopt; 69 NFSDLOCKMUTEX; 70 struct nfsrchash_bucket nfsrchash_table[NFSRVCACHE_HASHSIZE]; 71 struct nfsrchash_bucket nfsrcahash_table[NFSRVCACHE_HASHSIZE]; 72 struct mtx nfsrc_udpmtx; 73 struct mtx nfs_v4root_mutex; 74 struct nfsrvfh nfs_rootfh, nfs_pubfh; 75 int nfs_pubfhset = 0, nfs_rootfhset = 0; 76 struct proc *nfsd_master_proc = NULL; 77 int nfsd_debuglevel = 0; 78 static pid_t nfsd_master_pid = (pid_t)-1; 79 static char nfsd_master_comm[MAXCOMLEN + 1]; 80 static struct timeval nfsd_master_start; 81 static uint32_t nfsv4_sysid = 0; 82 83 static int nfssvc_srvcall(struct thread *, struct nfssvc_args *, 84 struct ucred *); 85 86 int nfsrv_enable_crossmntpt = 1; 87 static int nfs_commit_blks; 88 static int nfs_commit_miss; 89 extern int nfsrv_issuedelegs; 90 extern int nfsrv_dolocallocks; 91 extern int nfsd_enable_stringtouid; 92 93 SYSCTL_NODE(_vfs, OID_AUTO, nfsd, CTLFLAG_RW, 0, "NFS server"); 94 SYSCTL_INT(_vfs_nfsd, OID_AUTO, mirrormnt, CTLFLAG_RW, 95 &nfsrv_enable_crossmntpt, 0, "Enable nfsd to cross mount points"); 96 SYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_blks, CTLFLAG_RW, &nfs_commit_blks, 97 0, ""); 98 SYSCTL_INT(_vfs_nfsd, OID_AUTO, commit_miss, CTLFLAG_RW, &nfs_commit_miss, 99 0, ""); 100 SYSCTL_INT(_vfs_nfsd, OID_AUTO, issue_delegations, CTLFLAG_RW, 101 &nfsrv_issuedelegs, 0, "Enable nfsd to issue delegations"); 102 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_locallocks, CTLFLAG_RW, 103 &nfsrv_dolocallocks, 0, "Enable nfsd to acquire local locks on files"); 104 SYSCTL_INT(_vfs_nfsd, OID_AUTO, debuglevel, CTLFLAG_RW, &nfsd_debuglevel, 105 0, "Debug level for NFS server"); 106 SYSCTL_INT(_vfs_nfsd, OID_AUTO, enable_stringtouid, CTLFLAG_RW, 107 &nfsd_enable_stringtouid, 0, "Enable nfsd to accept numeric owner_names"); 108 109 #define MAX_REORDERED_RPC 16 110 #define NUM_HEURISTIC 1031 111 #define NHUSE_INIT 64 112 #define NHUSE_INC 16 113 #define NHUSE_MAX 2048 114 115 static struct nfsheur { 116 struct vnode *nh_vp; /* vp to match (unreferenced pointer) */ 117 off_t nh_nextoff; /* next offset for sequential detection */ 118 int nh_use; /* use count for selection */ 119 int nh_seqcount; /* heuristic */ 120 } nfsheur[NUM_HEURISTIC]; 121 122 123 /* 124 * Heuristic to detect sequential operation. 125 */ 126 static struct nfsheur * 127 nfsrv_sequential_heuristic(struct uio *uio, struct vnode *vp) 128 { 129 struct nfsheur *nh; 130 int hi, try; 131 132 /* Locate best candidate. */ 133 try = 32; 134 hi = ((int)(vm_offset_t)vp / sizeof(struct vnode)) % NUM_HEURISTIC; 135 nh = &nfsheur[hi]; 136 while (try--) { 137 if (nfsheur[hi].nh_vp == vp) { 138 nh = &nfsheur[hi]; 139 break; 140 } 141 if (nfsheur[hi].nh_use > 0) 142 --nfsheur[hi].nh_use; 143 hi = (hi + 1) % NUM_HEURISTIC; 144 if (nfsheur[hi].nh_use < nh->nh_use) 145 nh = &nfsheur[hi]; 146 } 147 148 /* Initialize hint if this is a new file. */ 149 if (nh->nh_vp != vp) { 150 nh->nh_vp = vp; 151 nh->nh_nextoff = uio->uio_offset; 152 nh->nh_use = NHUSE_INIT; 153 if (uio->uio_offset == 0) 154 nh->nh_seqcount = 4; 155 else 156 nh->nh_seqcount = 1; 157 } 158 159 /* Calculate heuristic. */ 160 if ((uio->uio_offset == 0 && nh->nh_seqcount > 0) || 161 uio->uio_offset == nh->nh_nextoff) { 162 /* See comments in vfs_vnops.c:sequential_heuristic(). */ 163 nh->nh_seqcount += howmany(uio->uio_resid, 16384); 164 if (nh->nh_seqcount > IO_SEQMAX) 165 nh->nh_seqcount = IO_SEQMAX; 166 } else if (qabs(uio->uio_offset - nh->nh_nextoff) <= MAX_REORDERED_RPC * 167 imax(vp->v_mount->mnt_stat.f_iosize, uio->uio_resid)) { 168 /* Probably a reordered RPC, leave seqcount alone. */ 169 } else if (nh->nh_seqcount > 1) { 170 nh->nh_seqcount /= 2; 171 } else { 172 nh->nh_seqcount = 0; 173 } 174 nh->nh_use += NHUSE_INC; 175 if (nh->nh_use > NHUSE_MAX) 176 nh->nh_use = NHUSE_MAX; 177 return (nh); 178 } 179 180 /* 181 * Get attributes into nfsvattr structure. 182 */ 183 int 184 nfsvno_getattr(struct vnode *vp, struct nfsvattr *nvap, struct ucred *cred, 185 struct thread *p, int vpislocked) 186 { 187 int error, lockedit = 0; 188 189 if (vpislocked == 0) { 190 /* 191 * When vpislocked == 0, the vnode is either exclusively 192 * locked by this thread or not locked by this thread. 193 * As such, shared lock it, if not exclusively locked. 194 */ 195 if (NFSVOPISLOCKED(vp) != LK_EXCLUSIVE) { 196 lockedit = 1; 197 NFSVOPLOCK(vp, LK_SHARED | LK_RETRY); 198 } 199 } 200 error = VOP_GETATTR(vp, &nvap->na_vattr, cred); 201 if (lockedit != 0) 202 NFSVOPUNLOCK(vp, 0); 203 204 NFSEXITCODE(error); 205 return (error); 206 } 207 208 /* 209 * Get a file handle for a vnode. 210 */ 211 int 212 nfsvno_getfh(struct vnode *vp, fhandle_t *fhp, struct thread *p) 213 { 214 int error; 215 216 NFSBZERO((caddr_t)fhp, sizeof(fhandle_t)); 217 fhp->fh_fsid = vp->v_mount->mnt_stat.f_fsid; 218 error = VOP_VPTOFH(vp, &fhp->fh_fid); 219 220 NFSEXITCODE(error); 221 return (error); 222 } 223 224 /* 225 * Perform access checking for vnodes obtained from file handles that would 226 * refer to files already opened by a Unix client. You cannot just use 227 * vn_writechk() and VOP_ACCESSX() for two reasons. 228 * 1 - You must check for exported rdonly as well as MNT_RDONLY for the write 229 * case. 230 * 2 - The owner is to be given access irrespective of mode bits for some 231 * operations, so that processes that chmod after opening a file don't 232 * break. 233 */ 234 int 235 nfsvno_accchk(struct vnode *vp, accmode_t accmode, struct ucred *cred, 236 struct nfsexstuff *exp, struct thread *p, int override, int vpislocked, 237 u_int32_t *supportedtypep) 238 { 239 struct vattr vattr; 240 int error = 0, getret = 0; 241 242 if (vpislocked == 0) { 243 if (NFSVOPLOCK(vp, LK_SHARED) != 0) { 244 error = EPERM; 245 goto out; 246 } 247 } 248 if (accmode & VWRITE) { 249 /* Just vn_writechk() changed to check rdonly */ 250 /* 251 * Disallow write attempts on read-only file systems; 252 * unless the file is a socket or a block or character 253 * device resident on the file system. 254 */ 255 if (NFSVNO_EXRDONLY(exp) || 256 (vp->v_mount->mnt_flag & MNT_RDONLY)) { 257 switch (vp->v_type) { 258 case VREG: 259 case VDIR: 260 case VLNK: 261 error = EROFS; 262 default: 263 break; 264 } 265 } 266 /* 267 * If there's shared text associated with 268 * the inode, try to free it up once. If 269 * we fail, we can't allow writing. 270 */ 271 if (VOP_IS_TEXT(vp) && error == 0) 272 error = ETXTBSY; 273 } 274 if (error != 0) { 275 if (vpislocked == 0) 276 NFSVOPUNLOCK(vp, 0); 277 goto out; 278 } 279 280 /* 281 * Should the override still be applied when ACLs are enabled? 282 */ 283 error = VOP_ACCESSX(vp, accmode, cred, p); 284 if (error != 0 && (accmode & (VDELETE | VDELETE_CHILD))) { 285 /* 286 * Try again with VEXPLICIT_DENY, to see if the test for 287 * deletion is supported. 288 */ 289 error = VOP_ACCESSX(vp, accmode | VEXPLICIT_DENY, cred, p); 290 if (error == 0) { 291 if (vp->v_type == VDIR) { 292 accmode &= ~(VDELETE | VDELETE_CHILD); 293 accmode |= VWRITE; 294 error = VOP_ACCESSX(vp, accmode, cred, p); 295 } else if (supportedtypep != NULL) { 296 *supportedtypep &= ~NFSACCESS_DELETE; 297 } 298 } 299 } 300 301 /* 302 * Allow certain operations for the owner (reads and writes 303 * on files that are already open). 304 */ 305 if (override != NFSACCCHK_NOOVERRIDE && 306 (error == EPERM || error == EACCES)) { 307 if (cred->cr_uid == 0 && (override & NFSACCCHK_ALLOWROOT)) 308 error = 0; 309 else if (override & NFSACCCHK_ALLOWOWNER) { 310 getret = VOP_GETATTR(vp, &vattr, cred); 311 if (getret == 0 && cred->cr_uid == vattr.va_uid) 312 error = 0; 313 } 314 } 315 if (vpislocked == 0) 316 NFSVOPUNLOCK(vp, 0); 317 318 out: 319 NFSEXITCODE(error); 320 return (error); 321 } 322 323 /* 324 * Set attribute(s) vnop. 325 */ 326 int 327 nfsvno_setattr(struct vnode *vp, struct nfsvattr *nvap, struct ucred *cred, 328 struct thread *p, struct nfsexstuff *exp) 329 { 330 int error; 331 332 error = VOP_SETATTR(vp, &nvap->na_vattr, cred); 333 NFSEXITCODE(error); 334 return (error); 335 } 336 337 /* 338 * Set up nameidata for a lookup() call and do it. 339 */ 340 int 341 nfsvno_namei(struct nfsrv_descript *nd, struct nameidata *ndp, 342 struct vnode *dp, int islocked, struct nfsexstuff *exp, struct thread *p, 343 struct vnode **retdirp) 344 { 345 struct componentname *cnp = &ndp->ni_cnd; 346 int i; 347 struct iovec aiov; 348 struct uio auio; 349 int lockleaf = (cnp->cn_flags & LOCKLEAF) != 0, linklen; 350 int error = 0; 351 char *cp; 352 353 *retdirp = NULL; 354 cnp->cn_nameptr = cnp->cn_pnbuf; 355 ndp->ni_lcf = 0; 356 /* 357 * Extract and set starting directory. 358 */ 359 if (dp->v_type != VDIR) { 360 if (islocked) 361 vput(dp); 362 else 363 vrele(dp); 364 nfsvno_relpathbuf(ndp); 365 error = ENOTDIR; 366 goto out1; 367 } 368 if (islocked) 369 NFSVOPUNLOCK(dp, 0); 370 VREF(dp); 371 *retdirp = dp; 372 if (NFSVNO_EXRDONLY(exp)) 373 cnp->cn_flags |= RDONLY; 374 ndp->ni_segflg = UIO_SYSSPACE; 375 376 if (nd->nd_flag & ND_PUBLOOKUP) { 377 ndp->ni_loopcnt = 0; 378 if (cnp->cn_pnbuf[0] == '/') { 379 vrele(dp); 380 /* 381 * Check for degenerate pathnames here, since lookup() 382 * panics on them. 383 */ 384 for (i = 1; i < ndp->ni_pathlen; i++) 385 if (cnp->cn_pnbuf[i] != '/') 386 break; 387 if (i == ndp->ni_pathlen) { 388 error = NFSERR_ACCES; 389 goto out; 390 } 391 dp = rootvnode; 392 VREF(dp); 393 } 394 } else if ((nfsrv_enable_crossmntpt == 0 && NFSVNO_EXPORTED(exp)) || 395 (nd->nd_flag & ND_NFSV4) == 0) { 396 /* 397 * Only cross mount points for NFSv4 when doing a 398 * mount while traversing the file system above 399 * the mount point, unless nfsrv_enable_crossmntpt is set. 400 */ 401 cnp->cn_flags |= NOCROSSMOUNT; 402 } 403 404 /* 405 * Initialize for scan, set ni_startdir and bump ref on dp again 406 * because lookup() will dereference ni_startdir. 407 */ 408 409 cnp->cn_thread = p; 410 ndp->ni_startdir = dp; 411 ndp->ni_rootdir = rootvnode; 412 ndp->ni_topdir = NULL; 413 414 if (!lockleaf) 415 cnp->cn_flags |= LOCKLEAF; 416 for (;;) { 417 cnp->cn_nameptr = cnp->cn_pnbuf; 418 /* 419 * Call lookup() to do the real work. If an error occurs, 420 * ndp->ni_vp and ni_dvp are left uninitialized or NULL and 421 * we do not have to dereference anything before returning. 422 * In either case ni_startdir will be dereferenced and NULLed 423 * out. 424 */ 425 error = lookup(ndp); 426 if (error) 427 break; 428 429 /* 430 * Check for encountering a symbolic link. Trivial 431 * termination occurs if no symlink encountered. 432 */ 433 if ((cnp->cn_flags & ISSYMLINK) == 0) { 434 if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) 435 nfsvno_relpathbuf(ndp); 436 if (ndp->ni_vp && !lockleaf) 437 NFSVOPUNLOCK(ndp->ni_vp, 0); 438 break; 439 } 440 441 /* 442 * Validate symlink 443 */ 444 if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1) 445 NFSVOPUNLOCK(ndp->ni_dvp, 0); 446 if (!(nd->nd_flag & ND_PUBLOOKUP)) { 447 error = EINVAL; 448 goto badlink2; 449 } 450 451 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) { 452 error = ELOOP; 453 goto badlink2; 454 } 455 if (ndp->ni_pathlen > 1) 456 cp = uma_zalloc(namei_zone, M_WAITOK); 457 else 458 cp = cnp->cn_pnbuf; 459 aiov.iov_base = cp; 460 aiov.iov_len = MAXPATHLEN; 461 auio.uio_iov = &aiov; 462 auio.uio_iovcnt = 1; 463 auio.uio_offset = 0; 464 auio.uio_rw = UIO_READ; 465 auio.uio_segflg = UIO_SYSSPACE; 466 auio.uio_td = NULL; 467 auio.uio_resid = MAXPATHLEN; 468 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred); 469 if (error) { 470 badlink1: 471 if (ndp->ni_pathlen > 1) 472 uma_zfree(namei_zone, cp); 473 badlink2: 474 vrele(ndp->ni_dvp); 475 vput(ndp->ni_vp); 476 break; 477 } 478 linklen = MAXPATHLEN - auio.uio_resid; 479 if (linklen == 0) { 480 error = ENOENT; 481 goto badlink1; 482 } 483 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) { 484 error = ENAMETOOLONG; 485 goto badlink1; 486 } 487 488 /* 489 * Adjust or replace path 490 */ 491 if (ndp->ni_pathlen > 1) { 492 NFSBCOPY(ndp->ni_next, cp + linklen, ndp->ni_pathlen); 493 uma_zfree(namei_zone, cnp->cn_pnbuf); 494 cnp->cn_pnbuf = cp; 495 } else 496 cnp->cn_pnbuf[linklen] = '\0'; 497 ndp->ni_pathlen += linklen; 498 499 /* 500 * Cleanup refs for next loop and check if root directory 501 * should replace current directory. Normally ni_dvp 502 * becomes the new base directory and is cleaned up when 503 * we loop. Explicitly null pointers after invalidation 504 * to clarify operation. 505 */ 506 vput(ndp->ni_vp); 507 ndp->ni_vp = NULL; 508 509 if (cnp->cn_pnbuf[0] == '/') { 510 vrele(ndp->ni_dvp); 511 ndp->ni_dvp = ndp->ni_rootdir; 512 VREF(ndp->ni_dvp); 513 } 514 ndp->ni_startdir = ndp->ni_dvp; 515 ndp->ni_dvp = NULL; 516 } 517 if (!lockleaf) 518 cnp->cn_flags &= ~LOCKLEAF; 519 520 out: 521 if (error) { 522 nfsvno_relpathbuf(ndp); 523 ndp->ni_vp = NULL; 524 ndp->ni_dvp = NULL; 525 ndp->ni_startdir = NULL; 526 } else if ((ndp->ni_cnd.cn_flags & (WANTPARENT|LOCKPARENT)) == 0) { 527 ndp->ni_dvp = NULL; 528 } 529 530 out1: 531 NFSEXITCODE2(error, nd); 532 return (error); 533 } 534 535 /* 536 * Set up a pathname buffer and return a pointer to it and, optionally 537 * set a hash pointer. 538 */ 539 void 540 nfsvno_setpathbuf(struct nameidata *ndp, char **bufpp, u_long **hashpp) 541 { 542 struct componentname *cnp = &ndp->ni_cnd; 543 544 cnp->cn_flags |= (NOMACCHECK | HASBUF); 545 cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK); 546 if (hashpp != NULL) 547 *hashpp = NULL; 548 *bufpp = cnp->cn_pnbuf; 549 } 550 551 /* 552 * Release the above path buffer, if not released by nfsvno_namei(). 553 */ 554 void 555 nfsvno_relpathbuf(struct nameidata *ndp) 556 { 557 558 if ((ndp->ni_cnd.cn_flags & HASBUF) == 0) 559 panic("nfsrelpath"); 560 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf); 561 ndp->ni_cnd.cn_flags &= ~HASBUF; 562 } 563 564 /* 565 * Readlink vnode op into an mbuf list. 566 */ 567 int 568 nfsvno_readlink(struct vnode *vp, struct ucred *cred, struct thread *p, 569 struct mbuf **mpp, struct mbuf **mpendp, int *lenp) 570 { 571 struct iovec iv[(NFS_MAXPATHLEN+MLEN-1)/MLEN]; 572 struct iovec *ivp = iv; 573 struct uio io, *uiop = &io; 574 struct mbuf *mp, *mp2 = NULL, *mp3 = NULL; 575 int i, len, tlen, error = 0; 576 577 len = 0; 578 i = 0; 579 while (len < NFS_MAXPATHLEN) { 580 NFSMGET(mp); 581 MCLGET(mp, M_WAITOK); 582 mp->m_len = M_SIZE(mp); 583 if (len == 0) { 584 mp3 = mp2 = mp; 585 } else { 586 mp2->m_next = mp; 587 mp2 = mp; 588 } 589 if ((len + mp->m_len) > NFS_MAXPATHLEN) { 590 mp->m_len = NFS_MAXPATHLEN - len; 591 len = NFS_MAXPATHLEN; 592 } else { 593 len += mp->m_len; 594 } 595 ivp->iov_base = mtod(mp, caddr_t); 596 ivp->iov_len = mp->m_len; 597 i++; 598 ivp++; 599 } 600 uiop->uio_iov = iv; 601 uiop->uio_iovcnt = i; 602 uiop->uio_offset = 0; 603 uiop->uio_resid = len; 604 uiop->uio_rw = UIO_READ; 605 uiop->uio_segflg = UIO_SYSSPACE; 606 uiop->uio_td = NULL; 607 error = VOP_READLINK(vp, uiop, cred); 608 if (error) { 609 m_freem(mp3); 610 *lenp = 0; 611 goto out; 612 } 613 if (uiop->uio_resid > 0) { 614 len -= uiop->uio_resid; 615 tlen = NFSM_RNDUP(len); 616 nfsrv_adj(mp3, NFS_MAXPATHLEN - tlen, tlen - len); 617 } 618 *lenp = len; 619 *mpp = mp3; 620 *mpendp = mp; 621 622 out: 623 NFSEXITCODE(error); 624 return (error); 625 } 626 627 /* 628 * Read vnode op call into mbuf list. 629 */ 630 int 631 nfsvno_read(struct vnode *vp, off_t off, int cnt, struct ucred *cred, 632 struct thread *p, struct mbuf **mpp, struct mbuf **mpendp) 633 { 634 struct mbuf *m; 635 int i; 636 struct iovec *iv; 637 struct iovec *iv2; 638 int error = 0, len, left, siz, tlen, ioflag = 0; 639 struct mbuf *m2 = NULL, *m3; 640 struct uio io, *uiop = &io; 641 struct nfsheur *nh; 642 643 len = left = NFSM_RNDUP(cnt); 644 m3 = NULL; 645 /* 646 * Generate the mbuf list with the uio_iov ref. to it. 647 */ 648 i = 0; 649 while (left > 0) { 650 NFSMGET(m); 651 MCLGET(m, M_WAITOK); 652 m->m_len = 0; 653 siz = min(M_TRAILINGSPACE(m), left); 654 left -= siz; 655 i++; 656 if (m3) 657 m2->m_next = m; 658 else 659 m3 = m; 660 m2 = m; 661 } 662 iv = malloc(i * sizeof (struct iovec), 663 M_TEMP, M_WAITOK); 664 uiop->uio_iov = iv2 = iv; 665 m = m3; 666 left = len; 667 i = 0; 668 while (left > 0) { 669 if (m == NULL) 670 panic("nfsvno_read iov"); 671 siz = min(M_TRAILINGSPACE(m), left); 672 if (siz > 0) { 673 iv->iov_base = mtod(m, caddr_t) + m->m_len; 674 iv->iov_len = siz; 675 m->m_len += siz; 676 left -= siz; 677 iv++; 678 i++; 679 } 680 m = m->m_next; 681 } 682 uiop->uio_iovcnt = i; 683 uiop->uio_offset = off; 684 uiop->uio_resid = len; 685 uiop->uio_rw = UIO_READ; 686 uiop->uio_segflg = UIO_SYSSPACE; 687 uiop->uio_td = NULL; 688 nh = nfsrv_sequential_heuristic(uiop, vp); 689 ioflag |= nh->nh_seqcount << IO_SEQSHIFT; 690 /* XXX KDM make this more systematic? */ 691 nfsstatsv1.srvbytes[NFSV4OP_READ] += uiop->uio_resid; 692 error = VOP_READ(vp, uiop, IO_NODELOCKED | ioflag, cred); 693 free(iv2, M_TEMP); 694 if (error) { 695 m_freem(m3); 696 *mpp = NULL; 697 goto out; 698 } 699 nh->nh_nextoff = uiop->uio_offset; 700 tlen = len - uiop->uio_resid; 701 cnt = cnt < tlen ? cnt : tlen; 702 tlen = NFSM_RNDUP(cnt); 703 if (tlen == 0) { 704 m_freem(m3); 705 m3 = NULL; 706 } else if (len != tlen || tlen != cnt) 707 nfsrv_adj(m3, len - tlen, tlen - cnt); 708 *mpp = m3; 709 *mpendp = m2; 710 711 out: 712 NFSEXITCODE(error); 713 return (error); 714 } 715 716 /* 717 * Write vnode op from an mbuf list. 718 */ 719 int 720 nfsvno_write(struct vnode *vp, off_t off, int retlen, int cnt, int stable, 721 struct mbuf *mp, char *cp, struct ucred *cred, struct thread *p) 722 { 723 struct iovec *ivp; 724 int i, len; 725 struct iovec *iv; 726 int ioflags, error; 727 struct uio io, *uiop = &io; 728 struct nfsheur *nh; 729 730 ivp = malloc(cnt * sizeof (struct iovec), M_TEMP, 731 M_WAITOK); 732 uiop->uio_iov = iv = ivp; 733 uiop->uio_iovcnt = cnt; 734 i = mtod(mp, caddr_t) + mp->m_len - cp; 735 len = retlen; 736 while (len > 0) { 737 if (mp == NULL) 738 panic("nfsvno_write"); 739 if (i > 0) { 740 i = min(i, len); 741 ivp->iov_base = cp; 742 ivp->iov_len = i; 743 ivp++; 744 len -= i; 745 } 746 mp = mp->m_next; 747 if (mp) { 748 i = mp->m_len; 749 cp = mtod(mp, caddr_t); 750 } 751 } 752 753 if (stable == NFSWRITE_UNSTABLE) 754 ioflags = IO_NODELOCKED; 755 else 756 ioflags = (IO_SYNC | IO_NODELOCKED); 757 uiop->uio_resid = retlen; 758 uiop->uio_rw = UIO_WRITE; 759 uiop->uio_segflg = UIO_SYSSPACE; 760 NFSUIOPROC(uiop, p); 761 uiop->uio_offset = off; 762 nh = nfsrv_sequential_heuristic(uiop, vp); 763 ioflags |= nh->nh_seqcount << IO_SEQSHIFT; 764 /* XXX KDM make this more systematic? */ 765 nfsstatsv1.srvbytes[NFSV4OP_WRITE] += uiop->uio_resid; 766 error = VOP_WRITE(vp, uiop, ioflags, cred); 767 if (error == 0) 768 nh->nh_nextoff = uiop->uio_offset; 769 free(iv, M_TEMP); 770 771 NFSEXITCODE(error); 772 return (error); 773 } 774 775 /* 776 * Common code for creating a regular file (plus special files for V2). 777 */ 778 int 779 nfsvno_createsub(struct nfsrv_descript *nd, struct nameidata *ndp, 780 struct vnode **vpp, struct nfsvattr *nvap, int *exclusive_flagp, 781 int32_t *cverf, NFSDEV_T rdev, struct thread *p, struct nfsexstuff *exp) 782 { 783 u_quad_t tempsize; 784 int error; 785 786 error = nd->nd_repstat; 787 if (!error && ndp->ni_vp == NULL) { 788 if (nvap->na_type == VREG || nvap->na_type == VSOCK) { 789 vrele(ndp->ni_startdir); 790 error = VOP_CREATE(ndp->ni_dvp, 791 &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr); 792 vput(ndp->ni_dvp); 793 nfsvno_relpathbuf(ndp); 794 if (!error) { 795 if (*exclusive_flagp) { 796 *exclusive_flagp = 0; 797 NFSVNO_ATTRINIT(nvap); 798 nvap->na_atime.tv_sec = cverf[0]; 799 nvap->na_atime.tv_nsec = cverf[1]; 800 error = VOP_SETATTR(ndp->ni_vp, 801 &nvap->na_vattr, nd->nd_cred); 802 if (error != 0) { 803 vput(ndp->ni_vp); 804 ndp->ni_vp = NULL; 805 error = NFSERR_NOTSUPP; 806 } 807 } 808 } 809 /* 810 * NFS V2 Only. nfsrvd_mknod() does this for V3. 811 * (This implies, just get out on an error.) 812 */ 813 } else if (nvap->na_type == VCHR || nvap->na_type == VBLK || 814 nvap->na_type == VFIFO) { 815 if (nvap->na_type == VCHR && rdev == 0xffffffff) 816 nvap->na_type = VFIFO; 817 if (nvap->na_type != VFIFO && 818 (error = priv_check_cred(nd->nd_cred, 819 PRIV_VFS_MKNOD_DEV, 0))) { 820 vrele(ndp->ni_startdir); 821 nfsvno_relpathbuf(ndp); 822 vput(ndp->ni_dvp); 823 goto out; 824 } 825 nvap->na_rdev = rdev; 826 error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp, 827 &ndp->ni_cnd, &nvap->na_vattr); 828 vput(ndp->ni_dvp); 829 nfsvno_relpathbuf(ndp); 830 vrele(ndp->ni_startdir); 831 if (error) 832 goto out; 833 } else { 834 vrele(ndp->ni_startdir); 835 nfsvno_relpathbuf(ndp); 836 vput(ndp->ni_dvp); 837 error = ENXIO; 838 goto out; 839 } 840 *vpp = ndp->ni_vp; 841 } else { 842 /* 843 * Handle cases where error is already set and/or 844 * the file exists. 845 * 1 - clean up the lookup 846 * 2 - iff !error and na_size set, truncate it 847 */ 848 vrele(ndp->ni_startdir); 849 nfsvno_relpathbuf(ndp); 850 *vpp = ndp->ni_vp; 851 if (ndp->ni_dvp == *vpp) 852 vrele(ndp->ni_dvp); 853 else 854 vput(ndp->ni_dvp); 855 if (!error && nvap->na_size != VNOVAL) { 856 error = nfsvno_accchk(*vpp, VWRITE, 857 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE, 858 NFSACCCHK_VPISLOCKED, NULL); 859 if (!error) { 860 tempsize = nvap->na_size; 861 NFSVNO_ATTRINIT(nvap); 862 nvap->na_size = tempsize; 863 error = VOP_SETATTR(*vpp, 864 &nvap->na_vattr, nd->nd_cred); 865 } 866 } 867 if (error) 868 vput(*vpp); 869 } 870 871 out: 872 NFSEXITCODE(error); 873 return (error); 874 } 875 876 /* 877 * Do a mknod vnode op. 878 */ 879 int 880 nfsvno_mknod(struct nameidata *ndp, struct nfsvattr *nvap, struct ucred *cred, 881 struct thread *p) 882 { 883 int error = 0; 884 enum vtype vtyp; 885 886 vtyp = nvap->na_type; 887 /* 888 * Iff doesn't exist, create it. 889 */ 890 if (ndp->ni_vp) { 891 vrele(ndp->ni_startdir); 892 nfsvno_relpathbuf(ndp); 893 vput(ndp->ni_dvp); 894 vrele(ndp->ni_vp); 895 error = EEXIST; 896 goto out; 897 } 898 if (vtyp != VCHR && vtyp != VBLK && vtyp != VSOCK && vtyp != VFIFO) { 899 vrele(ndp->ni_startdir); 900 nfsvno_relpathbuf(ndp); 901 vput(ndp->ni_dvp); 902 error = NFSERR_BADTYPE; 903 goto out; 904 } 905 if (vtyp == VSOCK) { 906 vrele(ndp->ni_startdir); 907 error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp, 908 &ndp->ni_cnd, &nvap->na_vattr); 909 vput(ndp->ni_dvp); 910 nfsvno_relpathbuf(ndp); 911 } else { 912 if (nvap->na_type != VFIFO && 913 (error = priv_check_cred(cred, PRIV_VFS_MKNOD_DEV, 0))) { 914 vrele(ndp->ni_startdir); 915 nfsvno_relpathbuf(ndp); 916 vput(ndp->ni_dvp); 917 goto out; 918 } 919 error = VOP_MKNOD(ndp->ni_dvp, &ndp->ni_vp, 920 &ndp->ni_cnd, &nvap->na_vattr); 921 vput(ndp->ni_dvp); 922 nfsvno_relpathbuf(ndp); 923 vrele(ndp->ni_startdir); 924 /* 925 * Since VOP_MKNOD returns the ni_vp, I can't 926 * see any reason to do the lookup. 927 */ 928 } 929 930 out: 931 NFSEXITCODE(error); 932 return (error); 933 } 934 935 /* 936 * Mkdir vnode op. 937 */ 938 int 939 nfsvno_mkdir(struct nameidata *ndp, struct nfsvattr *nvap, uid_t saved_uid, 940 struct ucred *cred, struct thread *p, struct nfsexstuff *exp) 941 { 942 int error = 0; 943 944 if (ndp->ni_vp != NULL) { 945 if (ndp->ni_dvp == ndp->ni_vp) 946 vrele(ndp->ni_dvp); 947 else 948 vput(ndp->ni_dvp); 949 vrele(ndp->ni_vp); 950 nfsvno_relpathbuf(ndp); 951 error = EEXIST; 952 goto out; 953 } 954 error = VOP_MKDIR(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd, 955 &nvap->na_vattr); 956 vput(ndp->ni_dvp); 957 nfsvno_relpathbuf(ndp); 958 959 out: 960 NFSEXITCODE(error); 961 return (error); 962 } 963 964 /* 965 * symlink vnode op. 966 */ 967 int 968 nfsvno_symlink(struct nameidata *ndp, struct nfsvattr *nvap, char *pathcp, 969 int pathlen, int not_v2, uid_t saved_uid, struct ucred *cred, struct thread *p, 970 struct nfsexstuff *exp) 971 { 972 int error = 0; 973 974 if (ndp->ni_vp) { 975 vrele(ndp->ni_startdir); 976 nfsvno_relpathbuf(ndp); 977 if (ndp->ni_dvp == ndp->ni_vp) 978 vrele(ndp->ni_dvp); 979 else 980 vput(ndp->ni_dvp); 981 vrele(ndp->ni_vp); 982 error = EEXIST; 983 goto out; 984 } 985 986 error = VOP_SYMLINK(ndp->ni_dvp, &ndp->ni_vp, &ndp->ni_cnd, 987 &nvap->na_vattr, pathcp); 988 vput(ndp->ni_dvp); 989 vrele(ndp->ni_startdir); 990 nfsvno_relpathbuf(ndp); 991 /* 992 * Although FreeBSD still had the lookup code in 993 * it for 7/current, there doesn't seem to be any 994 * point, since VOP_SYMLINK() returns the ni_vp. 995 * Just vput it for v2. 996 */ 997 if (!not_v2 && !error) 998 vput(ndp->ni_vp); 999 1000 out: 1001 NFSEXITCODE(error); 1002 return (error); 1003 } 1004 1005 /* 1006 * Parse symbolic link arguments. 1007 * This function has an ugly side effect. It will malloc() an area for 1008 * the symlink and set iov_base to point to it, only if it succeeds. 1009 * So, if it returns with uiop->uio_iov->iov_base != NULL, that must 1010 * be FREE'd later. 1011 */ 1012 int 1013 nfsvno_getsymlink(struct nfsrv_descript *nd, struct nfsvattr *nvap, 1014 struct thread *p, char **pathcpp, int *lenp) 1015 { 1016 u_int32_t *tl; 1017 char *pathcp = NULL; 1018 int error = 0, len; 1019 struct nfsv2_sattr *sp; 1020 1021 *pathcpp = NULL; 1022 *lenp = 0; 1023 if ((nd->nd_flag & ND_NFSV3) && 1024 (error = nfsrv_sattr(nd, NULL, nvap, NULL, NULL, p))) 1025 goto nfsmout; 1026 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 1027 len = fxdr_unsigned(int, *tl); 1028 if (len > NFS_MAXPATHLEN || len <= 0) { 1029 error = EBADRPC; 1030 goto nfsmout; 1031 } 1032 pathcp = malloc(len + 1, M_TEMP, M_WAITOK); 1033 error = nfsrv_mtostr(nd, pathcp, len); 1034 if (error) 1035 goto nfsmout; 1036 if (nd->nd_flag & ND_NFSV2) { 1037 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR); 1038 nvap->na_mode = fxdr_unsigned(u_int16_t, sp->sa_mode); 1039 } 1040 *pathcpp = pathcp; 1041 *lenp = len; 1042 NFSEXITCODE2(0, nd); 1043 return (0); 1044 nfsmout: 1045 if (pathcp) 1046 free(pathcp, M_TEMP); 1047 NFSEXITCODE2(error, nd); 1048 return (error); 1049 } 1050 1051 /* 1052 * Remove a non-directory object. 1053 */ 1054 int 1055 nfsvno_removesub(struct nameidata *ndp, int is_v4, struct ucred *cred, 1056 struct thread *p, struct nfsexstuff *exp) 1057 { 1058 struct vnode *vp; 1059 int error = 0; 1060 1061 vp = ndp->ni_vp; 1062 if (vp->v_type == VDIR) 1063 error = NFSERR_ISDIR; 1064 else if (is_v4) 1065 error = nfsrv_checkremove(vp, 1, p); 1066 if (!error) 1067 error = VOP_REMOVE(ndp->ni_dvp, vp, &ndp->ni_cnd); 1068 if (ndp->ni_dvp == vp) 1069 vrele(ndp->ni_dvp); 1070 else 1071 vput(ndp->ni_dvp); 1072 vput(vp); 1073 if ((ndp->ni_cnd.cn_flags & SAVENAME) != 0) 1074 nfsvno_relpathbuf(ndp); 1075 NFSEXITCODE(error); 1076 return (error); 1077 } 1078 1079 /* 1080 * Remove a directory. 1081 */ 1082 int 1083 nfsvno_rmdirsub(struct nameidata *ndp, int is_v4, struct ucred *cred, 1084 struct thread *p, struct nfsexstuff *exp) 1085 { 1086 struct vnode *vp; 1087 int error = 0; 1088 1089 vp = ndp->ni_vp; 1090 if (vp->v_type != VDIR) { 1091 error = ENOTDIR; 1092 goto out; 1093 } 1094 /* 1095 * No rmdir "." please. 1096 */ 1097 if (ndp->ni_dvp == vp) { 1098 error = EINVAL; 1099 goto out; 1100 } 1101 /* 1102 * The root of a mounted filesystem cannot be deleted. 1103 */ 1104 if (vp->v_vflag & VV_ROOT) 1105 error = EBUSY; 1106 out: 1107 if (!error) 1108 error = VOP_RMDIR(ndp->ni_dvp, vp, &ndp->ni_cnd); 1109 if (ndp->ni_dvp == vp) 1110 vrele(ndp->ni_dvp); 1111 else 1112 vput(ndp->ni_dvp); 1113 vput(vp); 1114 if ((ndp->ni_cnd.cn_flags & SAVENAME) != 0) 1115 nfsvno_relpathbuf(ndp); 1116 NFSEXITCODE(error); 1117 return (error); 1118 } 1119 1120 /* 1121 * Rename vnode op. 1122 */ 1123 int 1124 nfsvno_rename(struct nameidata *fromndp, struct nameidata *tondp, 1125 u_int32_t ndstat, u_int32_t ndflag, struct ucred *cred, struct thread *p) 1126 { 1127 struct vnode *fvp, *tvp, *tdvp; 1128 int error = 0; 1129 1130 fvp = fromndp->ni_vp; 1131 if (ndstat) { 1132 vrele(fromndp->ni_dvp); 1133 vrele(fvp); 1134 error = ndstat; 1135 goto out1; 1136 } 1137 tdvp = tondp->ni_dvp; 1138 tvp = tondp->ni_vp; 1139 if (tvp != NULL) { 1140 if (fvp->v_type == VDIR && tvp->v_type != VDIR) { 1141 error = (ndflag & ND_NFSV2) ? EISDIR : EEXIST; 1142 goto out; 1143 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) { 1144 error = (ndflag & ND_NFSV2) ? ENOTDIR : EEXIST; 1145 goto out; 1146 } 1147 if (tvp->v_type == VDIR && tvp->v_mountedhere) { 1148 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV; 1149 goto out; 1150 } 1151 1152 /* 1153 * A rename to '.' or '..' results in a prematurely 1154 * unlocked vnode on FreeBSD5, so I'm just going to fail that 1155 * here. 1156 */ 1157 if ((tondp->ni_cnd.cn_namelen == 1 && 1158 tondp->ni_cnd.cn_nameptr[0] == '.') || 1159 (tondp->ni_cnd.cn_namelen == 2 && 1160 tondp->ni_cnd.cn_nameptr[0] == '.' && 1161 tondp->ni_cnd.cn_nameptr[1] == '.')) { 1162 error = EINVAL; 1163 goto out; 1164 } 1165 } 1166 if (fvp->v_type == VDIR && fvp->v_mountedhere) { 1167 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV; 1168 goto out; 1169 } 1170 if (fvp->v_mount != tdvp->v_mount) { 1171 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EXDEV; 1172 goto out; 1173 } 1174 if (fvp == tdvp) { 1175 error = (ndflag & ND_NFSV2) ? ENOTEMPTY : EINVAL; 1176 goto out; 1177 } 1178 if (fvp == tvp) { 1179 /* 1180 * If source and destination are the same, there is nothing to 1181 * do. Set error to -1 to indicate this. 1182 */ 1183 error = -1; 1184 goto out; 1185 } 1186 if (ndflag & ND_NFSV4) { 1187 if (NFSVOPLOCK(fvp, LK_EXCLUSIVE) == 0) { 1188 error = nfsrv_checkremove(fvp, 0, p); 1189 NFSVOPUNLOCK(fvp, 0); 1190 } else 1191 error = EPERM; 1192 if (tvp && !error) 1193 error = nfsrv_checkremove(tvp, 1, p); 1194 } else { 1195 /* 1196 * For NFSv2 and NFSv3, try to get rid of the delegation, so 1197 * that the NFSv4 client won't be confused by the rename. 1198 * Since nfsd_recalldelegation() can only be called on an 1199 * unlocked vnode at this point and fvp is the file that will 1200 * still exist after the rename, just do fvp. 1201 */ 1202 nfsd_recalldelegation(fvp, p); 1203 } 1204 out: 1205 if (!error) { 1206 error = VOP_RENAME(fromndp->ni_dvp, fromndp->ni_vp, 1207 &fromndp->ni_cnd, tondp->ni_dvp, tondp->ni_vp, 1208 &tondp->ni_cnd); 1209 } else { 1210 if (tdvp == tvp) 1211 vrele(tdvp); 1212 else 1213 vput(tdvp); 1214 if (tvp) 1215 vput(tvp); 1216 vrele(fromndp->ni_dvp); 1217 vrele(fvp); 1218 if (error == -1) 1219 error = 0; 1220 } 1221 vrele(tondp->ni_startdir); 1222 nfsvno_relpathbuf(tondp); 1223 out1: 1224 vrele(fromndp->ni_startdir); 1225 nfsvno_relpathbuf(fromndp); 1226 NFSEXITCODE(error); 1227 return (error); 1228 } 1229 1230 /* 1231 * Link vnode op. 1232 */ 1233 int 1234 nfsvno_link(struct nameidata *ndp, struct vnode *vp, struct ucred *cred, 1235 struct thread *p, struct nfsexstuff *exp) 1236 { 1237 struct vnode *xp; 1238 int error = 0; 1239 1240 xp = ndp->ni_vp; 1241 if (xp != NULL) { 1242 error = EEXIST; 1243 } else { 1244 xp = ndp->ni_dvp; 1245 if (vp->v_mount != xp->v_mount) 1246 error = EXDEV; 1247 } 1248 if (!error) { 1249 NFSVOPLOCK(vp, LK_EXCLUSIVE | LK_RETRY); 1250 if ((vp->v_iflag & VI_DOOMED) == 0) 1251 error = VOP_LINK(ndp->ni_dvp, vp, &ndp->ni_cnd); 1252 else 1253 error = EPERM; 1254 if (ndp->ni_dvp == vp) 1255 vrele(ndp->ni_dvp); 1256 else 1257 vput(ndp->ni_dvp); 1258 NFSVOPUNLOCK(vp, 0); 1259 } else { 1260 if (ndp->ni_dvp == ndp->ni_vp) 1261 vrele(ndp->ni_dvp); 1262 else 1263 vput(ndp->ni_dvp); 1264 if (ndp->ni_vp) 1265 vrele(ndp->ni_vp); 1266 } 1267 nfsvno_relpathbuf(ndp); 1268 NFSEXITCODE(error); 1269 return (error); 1270 } 1271 1272 /* 1273 * Do the fsync() appropriate for the commit. 1274 */ 1275 int 1276 nfsvno_fsync(struct vnode *vp, u_int64_t off, int cnt, struct ucred *cred, 1277 struct thread *td) 1278 { 1279 int error = 0; 1280 1281 /* 1282 * RFC 1813 3.3.21: if count is 0, a flush from offset to the end of 1283 * file is done. At this time VOP_FSYNC does not accept offset and 1284 * byte count parameters so call VOP_FSYNC the whole file for now. 1285 * The same is true for NFSv4: RFC 3530 Sec. 14.2.3. 1286 * File systems that do not use the buffer cache (as indicated 1287 * by MNTK_USES_BCACHE not being set) must use VOP_FSYNC(). 1288 */ 1289 if (cnt == 0 || cnt > MAX_COMMIT_COUNT || 1290 (vp->v_mount->mnt_kern_flag & MNTK_USES_BCACHE) == 0) { 1291 /* 1292 * Give up and do the whole thing 1293 */ 1294 if (vp->v_object && 1295 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) { 1296 VM_OBJECT_WLOCK(vp->v_object); 1297 vm_object_page_clean(vp->v_object, 0, 0, OBJPC_SYNC); 1298 VM_OBJECT_WUNLOCK(vp->v_object); 1299 } 1300 error = VOP_FSYNC(vp, MNT_WAIT, td); 1301 } else { 1302 /* 1303 * Locate and synchronously write any buffers that fall 1304 * into the requested range. Note: we are assuming that 1305 * f_iosize is a power of 2. 1306 */ 1307 int iosize = vp->v_mount->mnt_stat.f_iosize; 1308 int iomask = iosize - 1; 1309 struct bufobj *bo; 1310 daddr_t lblkno; 1311 1312 /* 1313 * Align to iosize boundary, super-align to page boundary. 1314 */ 1315 if (off & iomask) { 1316 cnt += off & iomask; 1317 off &= ~(u_quad_t)iomask; 1318 } 1319 if (off & PAGE_MASK) { 1320 cnt += off & PAGE_MASK; 1321 off &= ~(u_quad_t)PAGE_MASK; 1322 } 1323 lblkno = off / iosize; 1324 1325 if (vp->v_object && 1326 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) { 1327 VM_OBJECT_WLOCK(vp->v_object); 1328 vm_object_page_clean(vp->v_object, off, off + cnt, 1329 OBJPC_SYNC); 1330 VM_OBJECT_WUNLOCK(vp->v_object); 1331 } 1332 1333 bo = &vp->v_bufobj; 1334 BO_LOCK(bo); 1335 while (cnt > 0) { 1336 struct buf *bp; 1337 1338 /* 1339 * If we have a buffer and it is marked B_DELWRI we 1340 * have to lock and write it. Otherwise the prior 1341 * write is assumed to have already been committed. 1342 * 1343 * gbincore() can return invalid buffers now so we 1344 * have to check that bit as well (though B_DELWRI 1345 * should not be set if B_INVAL is set there could be 1346 * a race here since we haven't locked the buffer). 1347 */ 1348 if ((bp = gbincore(&vp->v_bufobj, lblkno)) != NULL) { 1349 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL | 1350 LK_INTERLOCK, BO_LOCKPTR(bo)) == ENOLCK) { 1351 BO_LOCK(bo); 1352 continue; /* retry */ 1353 } 1354 if ((bp->b_flags & (B_DELWRI|B_INVAL)) == 1355 B_DELWRI) { 1356 bremfree(bp); 1357 bp->b_flags &= ~B_ASYNC; 1358 bwrite(bp); 1359 ++nfs_commit_miss; 1360 } else 1361 BUF_UNLOCK(bp); 1362 BO_LOCK(bo); 1363 } 1364 ++nfs_commit_blks; 1365 if (cnt < iosize) 1366 break; 1367 cnt -= iosize; 1368 ++lblkno; 1369 } 1370 BO_UNLOCK(bo); 1371 } 1372 NFSEXITCODE(error); 1373 return (error); 1374 } 1375 1376 /* 1377 * Statfs vnode op. 1378 */ 1379 int 1380 nfsvno_statfs(struct vnode *vp, struct statfs *sf) 1381 { 1382 int error; 1383 1384 error = VFS_STATFS(vp->v_mount, sf); 1385 if (error == 0) { 1386 /* 1387 * Since NFS handles these values as unsigned on the 1388 * wire, there is no way to represent negative values, 1389 * so set them to 0. Without this, they will appear 1390 * to be very large positive values for clients like 1391 * Solaris10. 1392 */ 1393 if (sf->f_bavail < 0) 1394 sf->f_bavail = 0; 1395 if (sf->f_ffree < 0) 1396 sf->f_ffree = 0; 1397 } 1398 NFSEXITCODE(error); 1399 return (error); 1400 } 1401 1402 /* 1403 * Do the vnode op stuff for Open. Similar to nfsvno_createsub(), but 1404 * must handle nfsrv_opencheck() calls after any other access checks. 1405 */ 1406 void 1407 nfsvno_open(struct nfsrv_descript *nd, struct nameidata *ndp, 1408 nfsquad_t clientid, nfsv4stateid_t *stateidp, struct nfsstate *stp, 1409 int *exclusive_flagp, struct nfsvattr *nvap, int32_t *cverf, int create, 1410 NFSACL_T *aclp, nfsattrbit_t *attrbitp, struct ucred *cred, struct thread *p, 1411 struct nfsexstuff *exp, struct vnode **vpp) 1412 { 1413 struct vnode *vp = NULL; 1414 u_quad_t tempsize; 1415 struct nfsexstuff nes; 1416 1417 if (ndp->ni_vp == NULL) 1418 nd->nd_repstat = nfsrv_opencheck(clientid, 1419 stateidp, stp, NULL, nd, p, nd->nd_repstat); 1420 if (!nd->nd_repstat) { 1421 if (ndp->ni_vp == NULL) { 1422 vrele(ndp->ni_startdir); 1423 nd->nd_repstat = VOP_CREATE(ndp->ni_dvp, 1424 &ndp->ni_vp, &ndp->ni_cnd, &nvap->na_vattr); 1425 vput(ndp->ni_dvp); 1426 nfsvno_relpathbuf(ndp); 1427 if (!nd->nd_repstat) { 1428 if (*exclusive_flagp) { 1429 *exclusive_flagp = 0; 1430 NFSVNO_ATTRINIT(nvap); 1431 nvap->na_atime.tv_sec = cverf[0]; 1432 nvap->na_atime.tv_nsec = cverf[1]; 1433 nd->nd_repstat = VOP_SETATTR(ndp->ni_vp, 1434 &nvap->na_vattr, cred); 1435 if (nd->nd_repstat != 0) { 1436 vput(ndp->ni_vp); 1437 ndp->ni_vp = NULL; 1438 nd->nd_repstat = NFSERR_NOTSUPP; 1439 } else 1440 NFSSETBIT_ATTRBIT(attrbitp, 1441 NFSATTRBIT_TIMEACCESS); 1442 } else { 1443 nfsrv_fixattr(nd, ndp->ni_vp, nvap, 1444 aclp, p, attrbitp, exp); 1445 } 1446 } 1447 vp = ndp->ni_vp; 1448 } else { 1449 if (ndp->ni_startdir) 1450 vrele(ndp->ni_startdir); 1451 nfsvno_relpathbuf(ndp); 1452 vp = ndp->ni_vp; 1453 if (create == NFSV4OPEN_CREATE) { 1454 if (ndp->ni_dvp == vp) 1455 vrele(ndp->ni_dvp); 1456 else 1457 vput(ndp->ni_dvp); 1458 } 1459 if (NFSVNO_ISSETSIZE(nvap) && vp->v_type == VREG) { 1460 if (ndp->ni_cnd.cn_flags & RDONLY) 1461 NFSVNO_SETEXRDONLY(&nes); 1462 else 1463 NFSVNO_EXINIT(&nes); 1464 nd->nd_repstat = nfsvno_accchk(vp, 1465 VWRITE, cred, &nes, p, 1466 NFSACCCHK_NOOVERRIDE, 1467 NFSACCCHK_VPISLOCKED, NULL); 1468 nd->nd_repstat = nfsrv_opencheck(clientid, 1469 stateidp, stp, vp, nd, p, nd->nd_repstat); 1470 if (!nd->nd_repstat) { 1471 tempsize = nvap->na_size; 1472 NFSVNO_ATTRINIT(nvap); 1473 nvap->na_size = tempsize; 1474 nd->nd_repstat = VOP_SETATTR(vp, 1475 &nvap->na_vattr, cred); 1476 } 1477 } else if (vp->v_type == VREG) { 1478 nd->nd_repstat = nfsrv_opencheck(clientid, 1479 stateidp, stp, vp, nd, p, nd->nd_repstat); 1480 } 1481 } 1482 } else { 1483 if (ndp->ni_cnd.cn_flags & HASBUF) 1484 nfsvno_relpathbuf(ndp); 1485 if (ndp->ni_startdir && create == NFSV4OPEN_CREATE) { 1486 vrele(ndp->ni_startdir); 1487 if (ndp->ni_dvp == ndp->ni_vp) 1488 vrele(ndp->ni_dvp); 1489 else 1490 vput(ndp->ni_dvp); 1491 if (ndp->ni_vp) 1492 vput(ndp->ni_vp); 1493 } 1494 } 1495 *vpp = vp; 1496 1497 NFSEXITCODE2(0, nd); 1498 } 1499 1500 /* 1501 * Updates the file rev and sets the mtime and ctime 1502 * to the current clock time, returning the va_filerev and va_Xtime 1503 * values. 1504 * Return ESTALE to indicate the vnode is VI_DOOMED. 1505 */ 1506 int 1507 nfsvno_updfilerev(struct vnode *vp, struct nfsvattr *nvap, 1508 struct ucred *cred, struct thread *p) 1509 { 1510 struct vattr va; 1511 1512 VATTR_NULL(&va); 1513 vfs_timestamp(&va.va_mtime); 1514 if (NFSVOPISLOCKED(vp) != LK_EXCLUSIVE) { 1515 NFSVOPLOCK(vp, LK_UPGRADE | LK_RETRY); 1516 if ((vp->v_iflag & VI_DOOMED) != 0) 1517 return (ESTALE); 1518 } 1519 (void) VOP_SETATTR(vp, &va, cred); 1520 (void) nfsvno_getattr(vp, nvap, cred, p, 1); 1521 return (0); 1522 } 1523 1524 /* 1525 * Glue routine to nfsv4_fillattr(). 1526 */ 1527 int 1528 nfsvno_fillattr(struct nfsrv_descript *nd, struct mount *mp, struct vnode *vp, 1529 struct nfsvattr *nvap, fhandle_t *fhp, int rderror, nfsattrbit_t *attrbitp, 1530 struct ucred *cred, struct thread *p, int isdgram, int reterr, 1531 int supports_nfsv4acls, int at_root, uint64_t mounted_on_fileno) 1532 { 1533 int error; 1534 1535 error = nfsv4_fillattr(nd, mp, vp, NULL, &nvap->na_vattr, fhp, rderror, 1536 attrbitp, cred, p, isdgram, reterr, supports_nfsv4acls, at_root, 1537 mounted_on_fileno); 1538 NFSEXITCODE2(0, nd); 1539 return (error); 1540 } 1541 1542 /* Since the Readdir vnode ops vary, put the entire functions in here. */ 1543 /* 1544 * nfs readdir service 1545 * - mallocs what it thinks is enough to read 1546 * count rounded up to a multiple of DIRBLKSIZ <= NFS_MAXREADDIR 1547 * - calls VOP_READDIR() 1548 * - loops around building the reply 1549 * if the output generated exceeds count break out of loop 1550 * The NFSM_CLGET macro is used here so that the reply will be packed 1551 * tightly in mbuf clusters. 1552 * - it trims out records with d_fileno == 0 1553 * this doesn't matter for Unix clients, but they might confuse clients 1554 * for other os'. 1555 * - it trims out records with d_type == DT_WHT 1556 * these cannot be seen through NFS (unless we extend the protocol) 1557 * The alternate call nfsrvd_readdirplus() does lookups as well. 1558 * PS: The NFS protocol spec. does not clarify what the "count" byte 1559 * argument is a count of.. just name strings and file id's or the 1560 * entire reply rpc or ... 1561 * I tried just file name and id sizes and it confused the Sun client, 1562 * so I am using the full rpc size now. The "paranoia.." comment refers 1563 * to including the status longwords that are not a part of the dir. 1564 * "entry" structures, but are in the rpc. 1565 */ 1566 int 1567 nfsrvd_readdir(struct nfsrv_descript *nd, int isdgram, 1568 struct vnode *vp, struct thread *p, struct nfsexstuff *exp) 1569 { 1570 struct dirent *dp; 1571 u_int32_t *tl; 1572 int dirlen; 1573 char *cpos, *cend, *rbuf; 1574 struct nfsvattr at; 1575 int nlen, error = 0, getret = 1; 1576 int siz, cnt, fullsiz, eofflag, ncookies; 1577 u_int64_t off, toff; 1578 #if 0 1579 u_int64_t verf; 1580 #endif 1581 u_long *cookies = NULL, *cookiep; 1582 struct uio io; 1583 struct iovec iv; 1584 int is_ufs; 1585 1586 if (nd->nd_repstat) { 1587 nfsrv_postopattr(nd, getret, &at); 1588 goto out; 1589 } 1590 if (nd->nd_flag & ND_NFSV2) { 1591 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1592 off = fxdr_unsigned(u_quad_t, *tl++); 1593 } else { 1594 NFSM_DISSECT(tl, u_int32_t *, 5 * NFSX_UNSIGNED); 1595 off = fxdr_hyper(tl); 1596 tl += 2; 1597 #if 0 1598 verf = fxdr_hyper(tl); 1599 #endif 1600 tl += 2; 1601 } 1602 toff = off; 1603 cnt = fxdr_unsigned(int, *tl); 1604 if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0) 1605 cnt = NFS_SRVMAXDATA(nd); 1606 siz = ((cnt + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1)); 1607 fullsiz = siz; 1608 if (nd->nd_flag & ND_NFSV3) { 1609 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd->nd_cred, 1610 p, 1); 1611 #if 0 1612 /* 1613 * va_filerev is not sufficient as a cookie verifier, 1614 * since it is not supposed to change when entries are 1615 * removed/added unless that offset cookies returned to 1616 * the client are no longer valid. 1617 */ 1618 if (!nd->nd_repstat && toff && verf != at.na_filerev) 1619 nd->nd_repstat = NFSERR_BAD_COOKIE; 1620 #endif 1621 } 1622 if (!nd->nd_repstat && vp->v_type != VDIR) 1623 nd->nd_repstat = NFSERR_NOTDIR; 1624 if (nd->nd_repstat == 0 && cnt == 0) { 1625 if (nd->nd_flag & ND_NFSV2) 1626 /* NFSv2 does not have NFSERR_TOOSMALL */ 1627 nd->nd_repstat = EPERM; 1628 else 1629 nd->nd_repstat = NFSERR_TOOSMALL; 1630 } 1631 if (!nd->nd_repstat) 1632 nd->nd_repstat = nfsvno_accchk(vp, VEXEC, 1633 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE, 1634 NFSACCCHK_VPISLOCKED, NULL); 1635 if (nd->nd_repstat) { 1636 vput(vp); 1637 if (nd->nd_flag & ND_NFSV3) 1638 nfsrv_postopattr(nd, getret, &at); 1639 goto out; 1640 } 1641 is_ufs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "ufs") == 0; 1642 rbuf = malloc(siz, M_TEMP, M_WAITOK); 1643 again: 1644 eofflag = 0; 1645 if (cookies) { 1646 free(cookies, M_TEMP); 1647 cookies = NULL; 1648 } 1649 1650 iv.iov_base = rbuf; 1651 iv.iov_len = siz; 1652 io.uio_iov = &iv; 1653 io.uio_iovcnt = 1; 1654 io.uio_offset = (off_t)off; 1655 io.uio_resid = siz; 1656 io.uio_segflg = UIO_SYSSPACE; 1657 io.uio_rw = UIO_READ; 1658 io.uio_td = NULL; 1659 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies, 1660 &cookies); 1661 off = (u_int64_t)io.uio_offset; 1662 if (io.uio_resid) 1663 siz -= io.uio_resid; 1664 1665 if (!cookies && !nd->nd_repstat) 1666 nd->nd_repstat = NFSERR_PERM; 1667 if (nd->nd_flag & ND_NFSV3) { 1668 getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1); 1669 if (!nd->nd_repstat) 1670 nd->nd_repstat = getret; 1671 } 1672 1673 /* 1674 * Handles the failed cases. nd->nd_repstat == 0 past here. 1675 */ 1676 if (nd->nd_repstat) { 1677 vput(vp); 1678 free(rbuf, M_TEMP); 1679 if (cookies) 1680 free(cookies, M_TEMP); 1681 if (nd->nd_flag & ND_NFSV3) 1682 nfsrv_postopattr(nd, getret, &at); 1683 goto out; 1684 } 1685 /* 1686 * If nothing read, return eof 1687 * rpc reply 1688 */ 1689 if (siz == 0) { 1690 vput(vp); 1691 if (nd->nd_flag & ND_NFSV2) { 1692 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1693 } else { 1694 nfsrv_postopattr(nd, getret, &at); 1695 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED); 1696 txdr_hyper(at.na_filerev, tl); 1697 tl += 2; 1698 } 1699 *tl++ = newnfs_false; 1700 *tl = newnfs_true; 1701 free(rbuf, M_TEMP); 1702 free(cookies, M_TEMP); 1703 goto out; 1704 } 1705 1706 /* 1707 * Check for degenerate cases of nothing useful read. 1708 * If so go try again 1709 */ 1710 cpos = rbuf; 1711 cend = rbuf + siz; 1712 dp = (struct dirent *)cpos; 1713 cookiep = cookies; 1714 1715 /* 1716 * For some reason FreeBSD's ufs_readdir() chooses to back the 1717 * directory offset up to a block boundary, so it is necessary to 1718 * skip over the records that precede the requested offset. This 1719 * requires the assumption that file offset cookies monotonically 1720 * increase. 1721 */ 1722 while (cpos < cend && ncookies > 0 && 1723 (dp->d_fileno == 0 || dp->d_type == DT_WHT || 1724 (is_ufs == 1 && ((u_quad_t)(*cookiep)) <= toff))) { 1725 cpos += dp->d_reclen; 1726 dp = (struct dirent *)cpos; 1727 cookiep++; 1728 ncookies--; 1729 } 1730 if (cpos >= cend || ncookies == 0) { 1731 siz = fullsiz; 1732 toff = off; 1733 goto again; 1734 } 1735 vput(vp); 1736 1737 /* 1738 * dirlen is the size of the reply, including all XDR and must 1739 * not exceed cnt. For NFSv2, RFC1094 didn't clearly indicate 1740 * if the XDR should be included in "count", but to be safe, we do. 1741 * (Include the two booleans at the end of the reply in dirlen now.) 1742 */ 1743 if (nd->nd_flag & ND_NFSV3) { 1744 nfsrv_postopattr(nd, getret, &at); 1745 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1746 txdr_hyper(at.na_filerev, tl); 1747 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED; 1748 } else { 1749 dirlen = 2 * NFSX_UNSIGNED; 1750 } 1751 1752 /* Loop through the records and build reply */ 1753 while (cpos < cend && ncookies > 0) { 1754 nlen = dp->d_namlen; 1755 if (dp->d_fileno != 0 && dp->d_type != DT_WHT && 1756 nlen <= NFS_MAXNAMLEN) { 1757 if (nd->nd_flag & ND_NFSV3) 1758 dirlen += (6*NFSX_UNSIGNED + NFSM_RNDUP(nlen)); 1759 else 1760 dirlen += (4*NFSX_UNSIGNED + NFSM_RNDUP(nlen)); 1761 if (dirlen > cnt) { 1762 eofflag = 0; 1763 break; 1764 } 1765 1766 /* 1767 * Build the directory record xdr from 1768 * the dirent entry. 1769 */ 1770 if (nd->nd_flag & ND_NFSV3) { 1771 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 1772 *tl++ = newnfs_true; 1773 *tl++ = 0; 1774 } else { 1775 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1776 *tl++ = newnfs_true; 1777 } 1778 *tl = txdr_unsigned(dp->d_fileno); 1779 (void) nfsm_strtom(nd, dp->d_name, nlen); 1780 if (nd->nd_flag & ND_NFSV3) { 1781 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1782 *tl++ = 0; 1783 } else 1784 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 1785 *tl = txdr_unsigned(*cookiep); 1786 } 1787 cpos += dp->d_reclen; 1788 dp = (struct dirent *)cpos; 1789 cookiep++; 1790 ncookies--; 1791 } 1792 if (cpos < cend) 1793 eofflag = 0; 1794 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 1795 *tl++ = newnfs_false; 1796 if (eofflag) 1797 *tl = newnfs_true; 1798 else 1799 *tl = newnfs_false; 1800 free(rbuf, M_TEMP); 1801 free(cookies, M_TEMP); 1802 1803 out: 1804 NFSEXITCODE2(0, nd); 1805 return (0); 1806 nfsmout: 1807 vput(vp); 1808 NFSEXITCODE2(error, nd); 1809 return (error); 1810 } 1811 1812 /* 1813 * Readdirplus for V3 and Readdir for V4. 1814 */ 1815 int 1816 nfsrvd_readdirplus(struct nfsrv_descript *nd, int isdgram, 1817 struct vnode *vp, struct thread *p, struct nfsexstuff *exp) 1818 { 1819 struct dirent *dp; 1820 u_int32_t *tl; 1821 int dirlen; 1822 char *cpos, *cend, *rbuf; 1823 struct vnode *nvp; 1824 fhandle_t nfh; 1825 struct nfsvattr nva, at, *nvap = &nva; 1826 struct mbuf *mb0, *mb1; 1827 struct nfsreferral *refp; 1828 int nlen, r, error = 0, getret = 1, usevget = 1; 1829 int siz, cnt, fullsiz, eofflag, ncookies, entrycnt; 1830 caddr_t bpos0, bpos1; 1831 u_int64_t off, toff, verf; 1832 u_long *cookies = NULL, *cookiep; 1833 nfsattrbit_t attrbits, rderrbits, savbits; 1834 struct uio io; 1835 struct iovec iv; 1836 struct componentname cn; 1837 int at_root, is_ufs, is_zfs, needs_unbusy, supports_nfsv4acls; 1838 struct mount *mp, *new_mp; 1839 uint64_t mounted_on_fileno; 1840 1841 if (nd->nd_repstat) { 1842 nfsrv_postopattr(nd, getret, &at); 1843 goto out; 1844 } 1845 NFSM_DISSECT(tl, u_int32_t *, 6 * NFSX_UNSIGNED); 1846 off = fxdr_hyper(tl); 1847 toff = off; 1848 tl += 2; 1849 verf = fxdr_hyper(tl); 1850 tl += 2; 1851 siz = fxdr_unsigned(int, *tl++); 1852 cnt = fxdr_unsigned(int, *tl); 1853 1854 /* 1855 * Use the server's maximum data transfer size as the upper bound 1856 * on reply datalen. 1857 */ 1858 if (cnt > NFS_SRVMAXDATA(nd) || cnt < 0) 1859 cnt = NFS_SRVMAXDATA(nd); 1860 1861 /* 1862 * siz is a "hint" of how much directory information (name, fileid, 1863 * cookie) should be in the reply. At least one client "hints" 0, 1864 * so I set it to cnt for that case. I also round it up to the 1865 * next multiple of DIRBLKSIZ. 1866 */ 1867 if (siz <= 0) 1868 siz = cnt; 1869 siz = ((siz + DIRBLKSIZ - 1) & ~(DIRBLKSIZ - 1)); 1870 1871 if (nd->nd_flag & ND_NFSV4) { 1872 error = nfsrv_getattrbits(nd, &attrbits, NULL, NULL); 1873 if (error) 1874 goto nfsmout; 1875 NFSSET_ATTRBIT(&savbits, &attrbits); 1876 NFSCLRNOTFILLABLE_ATTRBIT(&attrbits); 1877 NFSZERO_ATTRBIT(&rderrbits); 1878 NFSSETBIT_ATTRBIT(&rderrbits, NFSATTRBIT_RDATTRERROR); 1879 } else { 1880 NFSZERO_ATTRBIT(&attrbits); 1881 } 1882 fullsiz = siz; 1883 nd->nd_repstat = getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1); 1884 if (!nd->nd_repstat) { 1885 if (off && verf != at.na_filerev) { 1886 /* 1887 * va_filerev is not sufficient as a cookie verifier, 1888 * since it is not supposed to change when entries are 1889 * removed/added unless that offset cookies returned to 1890 * the client are no longer valid. 1891 */ 1892 #if 0 1893 if (nd->nd_flag & ND_NFSV4) { 1894 nd->nd_repstat = NFSERR_NOTSAME; 1895 } else { 1896 nd->nd_repstat = NFSERR_BAD_COOKIE; 1897 } 1898 #endif 1899 } else if ((nd->nd_flag & ND_NFSV4) && off == 0 && verf != 0) { 1900 nd->nd_repstat = NFSERR_BAD_COOKIE; 1901 } 1902 } 1903 if (!nd->nd_repstat && vp->v_type != VDIR) 1904 nd->nd_repstat = NFSERR_NOTDIR; 1905 if (!nd->nd_repstat && cnt == 0) 1906 nd->nd_repstat = NFSERR_TOOSMALL; 1907 if (!nd->nd_repstat) 1908 nd->nd_repstat = nfsvno_accchk(vp, VEXEC, 1909 nd->nd_cred, exp, p, NFSACCCHK_NOOVERRIDE, 1910 NFSACCCHK_VPISLOCKED, NULL); 1911 if (nd->nd_repstat) { 1912 vput(vp); 1913 if (nd->nd_flag & ND_NFSV3) 1914 nfsrv_postopattr(nd, getret, &at); 1915 goto out; 1916 } 1917 is_ufs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "ufs") == 0; 1918 is_zfs = strcmp(vp->v_mount->mnt_vfc->vfc_name, "zfs") == 0; 1919 1920 rbuf = malloc(siz, M_TEMP, M_WAITOK); 1921 again: 1922 eofflag = 0; 1923 if (cookies) { 1924 free(cookies, M_TEMP); 1925 cookies = NULL; 1926 } 1927 1928 iv.iov_base = rbuf; 1929 iv.iov_len = siz; 1930 io.uio_iov = &iv; 1931 io.uio_iovcnt = 1; 1932 io.uio_offset = (off_t)off; 1933 io.uio_resid = siz; 1934 io.uio_segflg = UIO_SYSSPACE; 1935 io.uio_rw = UIO_READ; 1936 io.uio_td = NULL; 1937 nd->nd_repstat = VOP_READDIR(vp, &io, nd->nd_cred, &eofflag, &ncookies, 1938 &cookies); 1939 off = (u_int64_t)io.uio_offset; 1940 if (io.uio_resid) 1941 siz -= io.uio_resid; 1942 1943 getret = nfsvno_getattr(vp, &at, nd->nd_cred, p, 1); 1944 1945 if (!cookies && !nd->nd_repstat) 1946 nd->nd_repstat = NFSERR_PERM; 1947 if (!nd->nd_repstat) 1948 nd->nd_repstat = getret; 1949 if (nd->nd_repstat) { 1950 vput(vp); 1951 if (cookies) 1952 free(cookies, M_TEMP); 1953 free(rbuf, M_TEMP); 1954 if (nd->nd_flag & ND_NFSV3) 1955 nfsrv_postopattr(nd, getret, &at); 1956 goto out; 1957 } 1958 /* 1959 * If nothing read, return eof 1960 * rpc reply 1961 */ 1962 if (siz == 0) { 1963 vput(vp); 1964 if (nd->nd_flag & ND_NFSV3) 1965 nfsrv_postopattr(nd, getret, &at); 1966 NFSM_BUILD(tl, u_int32_t *, 4 * NFSX_UNSIGNED); 1967 txdr_hyper(at.na_filerev, tl); 1968 tl += 2; 1969 *tl++ = newnfs_false; 1970 *tl = newnfs_true; 1971 free(cookies, M_TEMP); 1972 free(rbuf, M_TEMP); 1973 goto out; 1974 } 1975 1976 /* 1977 * Check for degenerate cases of nothing useful read. 1978 * If so go try again 1979 */ 1980 cpos = rbuf; 1981 cend = rbuf + siz; 1982 dp = (struct dirent *)cpos; 1983 cookiep = cookies; 1984 1985 /* 1986 * For some reason FreeBSD's ufs_readdir() chooses to back the 1987 * directory offset up to a block boundary, so it is necessary to 1988 * skip over the records that precede the requested offset. This 1989 * requires the assumption that file offset cookies monotonically 1990 * increase. 1991 */ 1992 while (cpos < cend && ncookies > 0 && 1993 (dp->d_fileno == 0 || dp->d_type == DT_WHT || 1994 (is_ufs == 1 && ((u_quad_t)(*cookiep)) <= toff) || 1995 ((nd->nd_flag & ND_NFSV4) && 1996 ((dp->d_namlen == 1 && dp->d_name[0] == '.') || 1997 (dp->d_namlen==2 && dp->d_name[0]=='.' && dp->d_name[1]=='.'))))) { 1998 cpos += dp->d_reclen; 1999 dp = (struct dirent *)cpos; 2000 cookiep++; 2001 ncookies--; 2002 } 2003 if (cpos >= cend || ncookies == 0) { 2004 siz = fullsiz; 2005 toff = off; 2006 goto again; 2007 } 2008 2009 /* 2010 * Busy the file system so that the mount point won't go away 2011 * and, as such, VFS_VGET() can be used safely. 2012 */ 2013 mp = vp->v_mount; 2014 vfs_ref(mp); 2015 NFSVOPUNLOCK(vp, 0); 2016 nd->nd_repstat = vfs_busy(mp, 0); 2017 vfs_rel(mp); 2018 if (nd->nd_repstat != 0) { 2019 vrele(vp); 2020 free(cookies, M_TEMP); 2021 free(rbuf, M_TEMP); 2022 if (nd->nd_flag & ND_NFSV3) 2023 nfsrv_postopattr(nd, getret, &at); 2024 goto out; 2025 } 2026 2027 /* 2028 * Check to see if entries in this directory can be safely acquired 2029 * via VFS_VGET() or if a switch to VOP_LOOKUP() is required. 2030 * ZFS snapshot directories need VOP_LOOKUP(), so that any 2031 * automount of the snapshot directory that is required will 2032 * be done. 2033 * This needs to be done here for NFSv4, since NFSv4 never does 2034 * a VFS_VGET() for "." or "..". 2035 */ 2036 if (is_zfs == 1) { 2037 r = VFS_VGET(mp, at.na_fileid, LK_SHARED, &nvp); 2038 if (r == EOPNOTSUPP) { 2039 usevget = 0; 2040 cn.cn_nameiop = LOOKUP; 2041 cn.cn_lkflags = LK_SHARED | LK_RETRY; 2042 cn.cn_cred = nd->nd_cred; 2043 cn.cn_thread = p; 2044 } else if (r == 0) 2045 vput(nvp); 2046 } 2047 2048 /* 2049 * Save this position, in case there is an error before one entry 2050 * is created. 2051 */ 2052 mb0 = nd->nd_mb; 2053 bpos0 = nd->nd_bpos; 2054 2055 /* 2056 * Fill in the first part of the reply. 2057 * dirlen is the reply length in bytes and cannot exceed cnt. 2058 * (Include the two booleans at the end of the reply in dirlen now, 2059 * so we recognize when we have exceeded cnt.) 2060 */ 2061 if (nd->nd_flag & ND_NFSV3) { 2062 dirlen = NFSX_V3POSTOPATTR + NFSX_VERF + 2 * NFSX_UNSIGNED; 2063 nfsrv_postopattr(nd, getret, &at); 2064 } else { 2065 dirlen = NFSX_VERF + 2 * NFSX_UNSIGNED; 2066 } 2067 NFSM_BUILD(tl, u_int32_t *, NFSX_VERF); 2068 txdr_hyper(at.na_filerev, tl); 2069 2070 /* 2071 * Save this position, in case there is an empty reply needed. 2072 */ 2073 mb1 = nd->nd_mb; 2074 bpos1 = nd->nd_bpos; 2075 2076 /* Loop through the records and build reply */ 2077 entrycnt = 0; 2078 while (cpos < cend && ncookies > 0 && dirlen < cnt) { 2079 nlen = dp->d_namlen; 2080 if (dp->d_fileno != 0 && dp->d_type != DT_WHT && 2081 nlen <= NFS_MAXNAMLEN && 2082 ((nd->nd_flag & ND_NFSV3) || nlen > 2 || 2083 (nlen==2 && (dp->d_name[0]!='.' || dp->d_name[1]!='.')) 2084 || (nlen == 1 && dp->d_name[0] != '.'))) { 2085 /* 2086 * Save the current position in the reply, in case 2087 * this entry exceeds cnt. 2088 */ 2089 mb1 = nd->nd_mb; 2090 bpos1 = nd->nd_bpos; 2091 2092 /* 2093 * For readdir_and_lookup get the vnode using 2094 * the file number. 2095 */ 2096 nvp = NULL; 2097 refp = NULL; 2098 r = 0; 2099 at_root = 0; 2100 needs_unbusy = 0; 2101 new_mp = mp; 2102 mounted_on_fileno = (uint64_t)dp->d_fileno; 2103 if ((nd->nd_flag & ND_NFSV3) || 2104 NFSNONZERO_ATTRBIT(&savbits)) { 2105 if (nd->nd_flag & ND_NFSV4) 2106 refp = nfsv4root_getreferral(NULL, 2107 vp, dp->d_fileno); 2108 if (refp == NULL) { 2109 if (usevget) 2110 r = VFS_VGET(mp, dp->d_fileno, 2111 LK_SHARED, &nvp); 2112 else 2113 r = EOPNOTSUPP; 2114 if (r == EOPNOTSUPP) { 2115 if (usevget) { 2116 usevget = 0; 2117 cn.cn_nameiop = LOOKUP; 2118 cn.cn_lkflags = 2119 LK_SHARED | 2120 LK_RETRY; 2121 cn.cn_cred = 2122 nd->nd_cred; 2123 cn.cn_thread = p; 2124 } 2125 cn.cn_nameptr = dp->d_name; 2126 cn.cn_namelen = nlen; 2127 cn.cn_flags = ISLASTCN | 2128 NOFOLLOW | LOCKLEAF; 2129 if (nlen == 2 && 2130 dp->d_name[0] == '.' && 2131 dp->d_name[1] == '.') 2132 cn.cn_flags |= 2133 ISDOTDOT; 2134 if (NFSVOPLOCK(vp, LK_SHARED) 2135 != 0) { 2136 nd->nd_repstat = EPERM; 2137 break; 2138 } 2139 if ((vp->v_vflag & VV_ROOT) != 0 2140 && (cn.cn_flags & ISDOTDOT) 2141 != 0) { 2142 vref(vp); 2143 nvp = vp; 2144 r = 0; 2145 } else { 2146 r = VOP_LOOKUP(vp, &nvp, 2147 &cn); 2148 if (vp != nvp) 2149 NFSVOPUNLOCK(vp, 2150 0); 2151 } 2152 } 2153 2154 /* 2155 * For NFSv4, check to see if nvp is 2156 * a mount point and get the mount 2157 * point vnode, as required. 2158 */ 2159 if (r == 0 && 2160 nfsrv_enable_crossmntpt != 0 && 2161 (nd->nd_flag & ND_NFSV4) != 0 && 2162 nvp->v_type == VDIR && 2163 nvp->v_mountedhere != NULL) { 2164 new_mp = nvp->v_mountedhere; 2165 r = vfs_busy(new_mp, 0); 2166 vput(nvp); 2167 nvp = NULL; 2168 if (r == 0) { 2169 r = VFS_ROOT(new_mp, 2170 LK_SHARED, &nvp); 2171 needs_unbusy = 1; 2172 if (r == 0) 2173 at_root = 1; 2174 } 2175 } 2176 } 2177 if (!r) { 2178 if (refp == NULL && 2179 ((nd->nd_flag & ND_NFSV3) || 2180 NFSNONZERO_ATTRBIT(&attrbits))) { 2181 r = nfsvno_getfh(nvp, &nfh, p); 2182 if (!r) 2183 r = nfsvno_getattr(nvp, nvap, 2184 nd->nd_cred, p, 1); 2185 if (r == 0 && is_zfs == 1 && 2186 nfsrv_enable_crossmntpt != 0 && 2187 (nd->nd_flag & ND_NFSV4) != 0 && 2188 nvp->v_type == VDIR && 2189 vp->v_mount != nvp->v_mount) { 2190 /* 2191 * For a ZFS snapshot, there is a 2192 * pseudo mount that does not set 2193 * v_mountedhere, so it needs to 2194 * be detected via a different 2195 * mount structure. 2196 */ 2197 at_root = 1; 2198 if (new_mp == mp) 2199 new_mp = nvp->v_mount; 2200 } 2201 } 2202 } else { 2203 nvp = NULL; 2204 } 2205 if (r) { 2206 if (!NFSISSET_ATTRBIT(&attrbits, 2207 NFSATTRBIT_RDATTRERROR)) { 2208 if (nvp != NULL) 2209 vput(nvp); 2210 if (needs_unbusy != 0) 2211 vfs_unbusy(new_mp); 2212 nd->nd_repstat = r; 2213 break; 2214 } 2215 } 2216 } 2217 2218 /* 2219 * Build the directory record xdr 2220 */ 2221 if (nd->nd_flag & ND_NFSV3) { 2222 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 2223 *tl++ = newnfs_true; 2224 *tl++ = 0; 2225 *tl = txdr_unsigned(dp->d_fileno); 2226 dirlen += nfsm_strtom(nd, dp->d_name, nlen); 2227 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2228 *tl++ = 0; 2229 *tl = txdr_unsigned(*cookiep); 2230 nfsrv_postopattr(nd, 0, nvap); 2231 dirlen += nfsm_fhtom(nd,(u_int8_t *)&nfh,0,1); 2232 dirlen += (5*NFSX_UNSIGNED+NFSX_V3POSTOPATTR); 2233 if (nvp != NULL) 2234 vput(nvp); 2235 } else { 2236 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 2237 *tl++ = newnfs_true; 2238 *tl++ = 0; 2239 *tl = txdr_unsigned(*cookiep); 2240 dirlen += nfsm_strtom(nd, dp->d_name, nlen); 2241 if (nvp != NULL) { 2242 supports_nfsv4acls = 2243 nfs_supportsnfsv4acls(nvp); 2244 NFSVOPUNLOCK(nvp, 0); 2245 } else 2246 supports_nfsv4acls = 0; 2247 if (refp != NULL) { 2248 dirlen += nfsrv_putreferralattr(nd, 2249 &savbits, refp, 0, 2250 &nd->nd_repstat); 2251 if (nd->nd_repstat) { 2252 if (nvp != NULL) 2253 vrele(nvp); 2254 if (needs_unbusy != 0) 2255 vfs_unbusy(new_mp); 2256 break; 2257 } 2258 } else if (r) { 2259 dirlen += nfsvno_fillattr(nd, new_mp, 2260 nvp, nvap, &nfh, r, &rderrbits, 2261 nd->nd_cred, p, isdgram, 0, 2262 supports_nfsv4acls, at_root, 2263 mounted_on_fileno); 2264 } else { 2265 dirlen += nfsvno_fillattr(nd, new_mp, 2266 nvp, nvap, &nfh, r, &attrbits, 2267 nd->nd_cred, p, isdgram, 0, 2268 supports_nfsv4acls, at_root, 2269 mounted_on_fileno); 2270 } 2271 if (nvp != NULL) 2272 vrele(nvp); 2273 dirlen += (3 * NFSX_UNSIGNED); 2274 } 2275 if (needs_unbusy != 0) 2276 vfs_unbusy(new_mp); 2277 if (dirlen <= cnt) 2278 entrycnt++; 2279 } 2280 cpos += dp->d_reclen; 2281 dp = (struct dirent *)cpos; 2282 cookiep++; 2283 ncookies--; 2284 } 2285 vrele(vp); 2286 vfs_unbusy(mp); 2287 2288 /* 2289 * If dirlen > cnt, we must strip off the last entry. If that 2290 * results in an empty reply, report NFSERR_TOOSMALL. 2291 */ 2292 if (dirlen > cnt || nd->nd_repstat) { 2293 if (!nd->nd_repstat && entrycnt == 0) 2294 nd->nd_repstat = NFSERR_TOOSMALL; 2295 if (nd->nd_repstat) { 2296 newnfs_trimtrailing(nd, mb0, bpos0); 2297 if (nd->nd_flag & ND_NFSV3) 2298 nfsrv_postopattr(nd, getret, &at); 2299 } else 2300 newnfs_trimtrailing(nd, mb1, bpos1); 2301 eofflag = 0; 2302 } else if (cpos < cend) 2303 eofflag = 0; 2304 if (!nd->nd_repstat) { 2305 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2306 *tl++ = newnfs_false; 2307 if (eofflag) 2308 *tl = newnfs_true; 2309 else 2310 *tl = newnfs_false; 2311 } 2312 free(cookies, M_TEMP); 2313 free(rbuf, M_TEMP); 2314 2315 out: 2316 NFSEXITCODE2(0, nd); 2317 return (0); 2318 nfsmout: 2319 vput(vp); 2320 NFSEXITCODE2(error, nd); 2321 return (error); 2322 } 2323 2324 /* 2325 * Get the settable attributes out of the mbuf list. 2326 * (Return 0 or EBADRPC) 2327 */ 2328 int 2329 nfsrv_sattr(struct nfsrv_descript *nd, vnode_t vp, struct nfsvattr *nvap, 2330 nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p) 2331 { 2332 u_int32_t *tl; 2333 struct nfsv2_sattr *sp; 2334 int error = 0, toclient = 0; 2335 2336 switch (nd->nd_flag & (ND_NFSV2 | ND_NFSV3 | ND_NFSV4)) { 2337 case ND_NFSV2: 2338 NFSM_DISSECT(sp, struct nfsv2_sattr *, NFSX_V2SATTR); 2339 /* 2340 * Some old clients didn't fill in the high order 16bits. 2341 * --> check the low order 2 bytes for 0xffff 2342 */ 2343 if ((fxdr_unsigned(int, sp->sa_mode) & 0xffff) != 0xffff) 2344 nvap->na_mode = nfstov_mode(sp->sa_mode); 2345 if (sp->sa_uid != newnfs_xdrneg1) 2346 nvap->na_uid = fxdr_unsigned(uid_t, sp->sa_uid); 2347 if (sp->sa_gid != newnfs_xdrneg1) 2348 nvap->na_gid = fxdr_unsigned(gid_t, sp->sa_gid); 2349 if (sp->sa_size != newnfs_xdrneg1) 2350 nvap->na_size = fxdr_unsigned(u_quad_t, sp->sa_size); 2351 if (sp->sa_atime.nfsv2_sec != newnfs_xdrneg1) { 2352 #ifdef notyet 2353 fxdr_nfsv2time(&sp->sa_atime, &nvap->na_atime); 2354 #else 2355 nvap->na_atime.tv_sec = 2356 fxdr_unsigned(u_int32_t,sp->sa_atime.nfsv2_sec); 2357 nvap->na_atime.tv_nsec = 0; 2358 #endif 2359 } 2360 if (sp->sa_mtime.nfsv2_sec != newnfs_xdrneg1) 2361 fxdr_nfsv2time(&sp->sa_mtime, &nvap->na_mtime); 2362 break; 2363 case ND_NFSV3: 2364 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2365 if (*tl == newnfs_true) { 2366 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2367 nvap->na_mode = nfstov_mode(*tl); 2368 } 2369 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2370 if (*tl == newnfs_true) { 2371 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2372 nvap->na_uid = fxdr_unsigned(uid_t, *tl); 2373 } 2374 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2375 if (*tl == newnfs_true) { 2376 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2377 nvap->na_gid = fxdr_unsigned(gid_t, *tl); 2378 } 2379 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2380 if (*tl == newnfs_true) { 2381 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2382 nvap->na_size = fxdr_hyper(tl); 2383 } 2384 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2385 switch (fxdr_unsigned(int, *tl)) { 2386 case NFSV3SATTRTIME_TOCLIENT: 2387 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2388 fxdr_nfsv3time(tl, &nvap->na_atime); 2389 toclient = 1; 2390 break; 2391 case NFSV3SATTRTIME_TOSERVER: 2392 vfs_timestamp(&nvap->na_atime); 2393 nvap->na_vaflags |= VA_UTIMES_NULL; 2394 break; 2395 } 2396 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2397 switch (fxdr_unsigned(int, *tl)) { 2398 case NFSV3SATTRTIME_TOCLIENT: 2399 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 2400 fxdr_nfsv3time(tl, &nvap->na_mtime); 2401 nvap->na_vaflags &= ~VA_UTIMES_NULL; 2402 break; 2403 case NFSV3SATTRTIME_TOSERVER: 2404 vfs_timestamp(&nvap->na_mtime); 2405 if (!toclient) 2406 nvap->na_vaflags |= VA_UTIMES_NULL; 2407 break; 2408 } 2409 break; 2410 case ND_NFSV4: 2411 error = nfsv4_sattr(nd, vp, nvap, attrbitp, aclp, p); 2412 } 2413 nfsmout: 2414 NFSEXITCODE2(error, nd); 2415 return (error); 2416 } 2417 2418 /* 2419 * Handle the setable attributes for V4. 2420 * Returns NFSERR_BADXDR if it can't be parsed, 0 otherwise. 2421 */ 2422 int 2423 nfsv4_sattr(struct nfsrv_descript *nd, vnode_t vp, struct nfsvattr *nvap, 2424 nfsattrbit_t *attrbitp, NFSACL_T *aclp, struct thread *p) 2425 { 2426 u_int32_t *tl; 2427 int attrsum = 0; 2428 int i, j; 2429 int error, attrsize, bitpos, aclsize, aceerr, retnotsup = 0; 2430 int toclient = 0; 2431 u_char *cp, namestr[NFSV4_SMALLSTR + 1]; 2432 uid_t uid; 2433 gid_t gid; 2434 2435 error = nfsrv_getattrbits(nd, attrbitp, NULL, &retnotsup); 2436 if (error) 2437 goto nfsmout; 2438 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2439 attrsize = fxdr_unsigned(int, *tl); 2440 2441 /* 2442 * Loop around getting the setable attributes. If an unsupported 2443 * one is found, set nd_repstat == NFSERR_ATTRNOTSUPP and return. 2444 */ 2445 if (retnotsup) { 2446 nd->nd_repstat = NFSERR_ATTRNOTSUPP; 2447 bitpos = NFSATTRBIT_MAX; 2448 } else { 2449 bitpos = 0; 2450 } 2451 for (; bitpos < NFSATTRBIT_MAX; bitpos++) { 2452 if (attrsum > attrsize) { 2453 error = NFSERR_BADXDR; 2454 goto nfsmout; 2455 } 2456 if (NFSISSET_ATTRBIT(attrbitp, bitpos)) 2457 switch (bitpos) { 2458 case NFSATTRBIT_SIZE: 2459 NFSM_DISSECT(tl, u_int32_t *, NFSX_HYPER); 2460 if (vp != NULL && vp->v_type != VREG) { 2461 error = (vp->v_type == VDIR) ? NFSERR_ISDIR : 2462 NFSERR_INVAL; 2463 goto nfsmout; 2464 } 2465 nvap->na_size = fxdr_hyper(tl); 2466 attrsum += NFSX_HYPER; 2467 break; 2468 case NFSATTRBIT_ACL: 2469 error = nfsrv_dissectacl(nd, aclp, &aceerr, &aclsize, 2470 p); 2471 if (error) 2472 goto nfsmout; 2473 if (aceerr && !nd->nd_repstat) 2474 nd->nd_repstat = aceerr; 2475 attrsum += aclsize; 2476 break; 2477 case NFSATTRBIT_ARCHIVE: 2478 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2479 if (!nd->nd_repstat) 2480 nd->nd_repstat = NFSERR_ATTRNOTSUPP; 2481 attrsum += NFSX_UNSIGNED; 2482 break; 2483 case NFSATTRBIT_HIDDEN: 2484 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2485 if (!nd->nd_repstat) 2486 nd->nd_repstat = NFSERR_ATTRNOTSUPP; 2487 attrsum += NFSX_UNSIGNED; 2488 break; 2489 case NFSATTRBIT_MIMETYPE: 2490 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2491 i = fxdr_unsigned(int, *tl); 2492 error = nfsm_advance(nd, NFSM_RNDUP(i), -1); 2493 if (error) 2494 goto nfsmout; 2495 if (!nd->nd_repstat) 2496 nd->nd_repstat = NFSERR_ATTRNOTSUPP; 2497 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(i)); 2498 break; 2499 case NFSATTRBIT_MODE: 2500 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2501 nvap->na_mode = nfstov_mode(*tl); 2502 attrsum += NFSX_UNSIGNED; 2503 break; 2504 case NFSATTRBIT_OWNER: 2505 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2506 j = fxdr_unsigned(int, *tl); 2507 if (j < 0) { 2508 error = NFSERR_BADXDR; 2509 goto nfsmout; 2510 } 2511 if (j > NFSV4_SMALLSTR) 2512 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK); 2513 else 2514 cp = namestr; 2515 error = nfsrv_mtostr(nd, cp, j); 2516 if (error) { 2517 if (j > NFSV4_SMALLSTR) 2518 free(cp, M_NFSSTRING); 2519 goto nfsmout; 2520 } 2521 if (!nd->nd_repstat) { 2522 nd->nd_repstat = nfsv4_strtouid(nd, cp, j, &uid, 2523 p); 2524 if (!nd->nd_repstat) 2525 nvap->na_uid = uid; 2526 } 2527 if (j > NFSV4_SMALLSTR) 2528 free(cp, M_NFSSTRING); 2529 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j)); 2530 break; 2531 case NFSATTRBIT_OWNERGROUP: 2532 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2533 j = fxdr_unsigned(int, *tl); 2534 if (j < 0) { 2535 error = NFSERR_BADXDR; 2536 goto nfsmout; 2537 } 2538 if (j > NFSV4_SMALLSTR) 2539 cp = malloc(j + 1, M_NFSSTRING, M_WAITOK); 2540 else 2541 cp = namestr; 2542 error = nfsrv_mtostr(nd, cp, j); 2543 if (error) { 2544 if (j > NFSV4_SMALLSTR) 2545 free(cp, M_NFSSTRING); 2546 goto nfsmout; 2547 } 2548 if (!nd->nd_repstat) { 2549 nd->nd_repstat = nfsv4_strtogid(nd, cp, j, &gid, 2550 p); 2551 if (!nd->nd_repstat) 2552 nvap->na_gid = gid; 2553 } 2554 if (j > NFSV4_SMALLSTR) 2555 free(cp, M_NFSSTRING); 2556 attrsum += (NFSX_UNSIGNED + NFSM_RNDUP(j)); 2557 break; 2558 case NFSATTRBIT_SYSTEM: 2559 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2560 if (!nd->nd_repstat) 2561 nd->nd_repstat = NFSERR_ATTRNOTSUPP; 2562 attrsum += NFSX_UNSIGNED; 2563 break; 2564 case NFSATTRBIT_TIMEACCESSSET: 2565 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2566 attrsum += NFSX_UNSIGNED; 2567 if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) { 2568 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME); 2569 fxdr_nfsv4time(tl, &nvap->na_atime); 2570 toclient = 1; 2571 attrsum += NFSX_V4TIME; 2572 } else { 2573 vfs_timestamp(&nvap->na_atime); 2574 nvap->na_vaflags |= VA_UTIMES_NULL; 2575 } 2576 break; 2577 case NFSATTRBIT_TIMEBACKUP: 2578 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME); 2579 if (!nd->nd_repstat) 2580 nd->nd_repstat = NFSERR_ATTRNOTSUPP; 2581 attrsum += NFSX_V4TIME; 2582 break; 2583 case NFSATTRBIT_TIMECREATE: 2584 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME); 2585 if (!nd->nd_repstat) 2586 nd->nd_repstat = NFSERR_ATTRNOTSUPP; 2587 attrsum += NFSX_V4TIME; 2588 break; 2589 case NFSATTRBIT_TIMEMODIFYSET: 2590 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 2591 attrsum += NFSX_UNSIGNED; 2592 if (fxdr_unsigned(int, *tl)==NFSV4SATTRTIME_TOCLIENT) { 2593 NFSM_DISSECT(tl, u_int32_t *, NFSX_V4TIME); 2594 fxdr_nfsv4time(tl, &nvap->na_mtime); 2595 nvap->na_vaflags &= ~VA_UTIMES_NULL; 2596 attrsum += NFSX_V4TIME; 2597 } else { 2598 vfs_timestamp(&nvap->na_mtime); 2599 if (!toclient) 2600 nvap->na_vaflags |= VA_UTIMES_NULL; 2601 } 2602 break; 2603 default: 2604 nd->nd_repstat = NFSERR_ATTRNOTSUPP; 2605 /* 2606 * set bitpos so we drop out of the loop. 2607 */ 2608 bitpos = NFSATTRBIT_MAX; 2609 break; 2610 } 2611 } 2612 2613 /* 2614 * some clients pad the attrlist, so we need to skip over the 2615 * padding. 2616 */ 2617 if (attrsum > attrsize) { 2618 error = NFSERR_BADXDR; 2619 } else { 2620 attrsize = NFSM_RNDUP(attrsize); 2621 if (attrsum < attrsize) 2622 error = nfsm_advance(nd, attrsize - attrsum, -1); 2623 } 2624 nfsmout: 2625 NFSEXITCODE2(error, nd); 2626 return (error); 2627 } 2628 2629 /* 2630 * Check/setup export credentials. 2631 */ 2632 int 2633 nfsd_excred(struct nfsrv_descript *nd, struct nfsexstuff *exp, 2634 struct ucred *credanon) 2635 { 2636 int error = 0; 2637 2638 /* 2639 * Check/setup credentials. 2640 */ 2641 if (nd->nd_flag & ND_GSS) 2642 exp->nes_exflag &= ~MNT_EXPORTANON; 2643 2644 /* 2645 * Check to see if the operation is allowed for this security flavor. 2646 * RFC2623 suggests that the NFSv3 Fsinfo RPC be allowed to 2647 * AUTH_NONE or AUTH_SYS for file systems requiring RPCSEC_GSS. 2648 * Also, allow Secinfo, so that it can acquire the correct flavor(s). 2649 */ 2650 if (nfsvno_testexp(nd, exp) && 2651 nd->nd_procnum != NFSV4OP_SECINFO && 2652 nd->nd_procnum != NFSPROC_FSINFO) { 2653 if (nd->nd_flag & ND_NFSV4) 2654 error = NFSERR_WRONGSEC; 2655 else 2656 error = (NFSERR_AUTHERR | AUTH_TOOWEAK); 2657 goto out; 2658 } 2659 2660 /* 2661 * Check to see if the file system is exported V4 only. 2662 */ 2663 if (NFSVNO_EXV4ONLY(exp) && !(nd->nd_flag & ND_NFSV4)) { 2664 error = NFSERR_PROGNOTV4; 2665 goto out; 2666 } 2667 2668 /* 2669 * Now, map the user credentials. 2670 * (Note that ND_AUTHNONE will only be set for an NFSv3 2671 * Fsinfo RPC. If set for anything else, this code might need 2672 * to change.) 2673 */ 2674 if (NFSVNO_EXPORTED(exp)) { 2675 if (((nd->nd_flag & ND_GSS) == 0 && nd->nd_cred->cr_uid == 0) || 2676 NFSVNO_EXPORTANON(exp) || 2677 (nd->nd_flag & ND_AUTHNONE) != 0) { 2678 nd->nd_cred->cr_uid = credanon->cr_uid; 2679 nd->nd_cred->cr_gid = credanon->cr_gid; 2680 crsetgroups(nd->nd_cred, credanon->cr_ngroups, 2681 credanon->cr_groups); 2682 } else if ((nd->nd_flag & ND_GSS) == 0) { 2683 /* 2684 * If using AUTH_SYS, call nfsrv_getgrpscred() to see 2685 * if there is a replacement credential with a group 2686 * list set up by "nfsuserd -manage-gids". 2687 * If there is no replacement, nfsrv_getgrpscred() 2688 * simply returns its argument. 2689 */ 2690 nd->nd_cred = nfsrv_getgrpscred(nd->nd_cred); 2691 } 2692 } 2693 2694 out: 2695 NFSEXITCODE2(error, nd); 2696 return (error); 2697 } 2698 2699 /* 2700 * Check exports. 2701 */ 2702 int 2703 nfsvno_checkexp(struct mount *mp, struct sockaddr *nam, struct nfsexstuff *exp, 2704 struct ucred **credp) 2705 { 2706 int i, error, *secflavors; 2707 2708 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp, 2709 &exp->nes_numsecflavor, &secflavors); 2710 if (error) { 2711 if (nfs_rootfhset) { 2712 exp->nes_exflag = 0; 2713 exp->nes_numsecflavor = 0; 2714 error = 0; 2715 } 2716 } else { 2717 /* Copy the security flavors. */ 2718 for (i = 0; i < exp->nes_numsecflavor; i++) 2719 exp->nes_secflavors[i] = secflavors[i]; 2720 } 2721 NFSEXITCODE(error); 2722 return (error); 2723 } 2724 2725 /* 2726 * Get a vnode for a file handle and export stuff. 2727 */ 2728 int 2729 nfsvno_fhtovp(struct mount *mp, fhandle_t *fhp, struct sockaddr *nam, 2730 int lktype, struct vnode **vpp, struct nfsexstuff *exp, 2731 struct ucred **credp) 2732 { 2733 int i, error, *secflavors; 2734 2735 *credp = NULL; 2736 exp->nes_numsecflavor = 0; 2737 error = VFS_FHTOVP(mp, &fhp->fh_fid, lktype, vpp); 2738 if (error != 0) 2739 /* Make sure the server replies ESTALE to the client. */ 2740 error = ESTALE; 2741 if (nam && !error) { 2742 error = VFS_CHECKEXP(mp, nam, &exp->nes_exflag, credp, 2743 &exp->nes_numsecflavor, &secflavors); 2744 if (error) { 2745 if (nfs_rootfhset) { 2746 exp->nes_exflag = 0; 2747 exp->nes_numsecflavor = 0; 2748 error = 0; 2749 } else { 2750 vput(*vpp); 2751 } 2752 } else { 2753 /* Copy the security flavors. */ 2754 for (i = 0; i < exp->nes_numsecflavor; i++) 2755 exp->nes_secflavors[i] = secflavors[i]; 2756 } 2757 } 2758 NFSEXITCODE(error); 2759 return (error); 2760 } 2761 2762 /* 2763 * nfsd_fhtovp() - convert a fh to a vnode ptr 2764 * - look up fsid in mount list (if not found ret error) 2765 * - get vp and export rights by calling nfsvno_fhtovp() 2766 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon 2767 * for AUTH_SYS 2768 * - if mpp != NULL, return the mount point so that it can 2769 * be used for vn_finished_write() by the caller 2770 */ 2771 void 2772 nfsd_fhtovp(struct nfsrv_descript *nd, struct nfsrvfh *nfp, int lktype, 2773 struct vnode **vpp, struct nfsexstuff *exp, 2774 struct mount **mpp, int startwrite, struct thread *p) 2775 { 2776 struct mount *mp; 2777 struct ucred *credanon; 2778 fhandle_t *fhp; 2779 2780 fhp = (fhandle_t *)nfp->nfsrvfh_data; 2781 /* 2782 * Check for the special case of the nfsv4root_fh. 2783 */ 2784 mp = vfs_busyfs(&fhp->fh_fsid); 2785 if (mpp != NULL) 2786 *mpp = mp; 2787 if (mp == NULL) { 2788 *vpp = NULL; 2789 nd->nd_repstat = ESTALE; 2790 goto out; 2791 } 2792 2793 if (startwrite) { 2794 vn_start_write(NULL, mpp, V_WAIT); 2795 if (lktype == LK_SHARED && !(MNT_SHARED_WRITES(mp))) 2796 lktype = LK_EXCLUSIVE; 2797 } 2798 nd->nd_repstat = nfsvno_fhtovp(mp, fhp, nd->nd_nam, lktype, vpp, exp, 2799 &credanon); 2800 vfs_unbusy(mp); 2801 2802 /* 2803 * For NFSv4 without a pseudo root fs, unexported file handles 2804 * can be returned, so that Lookup works everywhere. 2805 */ 2806 if (!nd->nd_repstat && exp->nes_exflag == 0 && 2807 !(nd->nd_flag & ND_NFSV4)) { 2808 vput(*vpp); 2809 nd->nd_repstat = EACCES; 2810 } 2811 2812 /* 2813 * Personally, I've never seen any point in requiring a 2814 * reserved port#, since only in the rare case where the 2815 * clients are all boxes with secure system privileges, 2816 * does it provide any enhanced security, but... some people 2817 * believe it to be useful and keep putting this code back in. 2818 * (There is also some "security checker" out there that 2819 * complains if the nfs server doesn't enforce this.) 2820 * However, note the following: 2821 * RFC3530 (NFSv4) specifies that a reserved port# not be 2822 * required. 2823 * RFC2623 recommends that, if a reserved port# is checked for, 2824 * that there be a way to turn that off--> ifdef'd. 2825 */ 2826 #ifdef NFS_REQRSVPORT 2827 if (!nd->nd_repstat) { 2828 struct sockaddr_in *saddr; 2829 struct sockaddr_in6 *saddr6; 2830 2831 saddr = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in *); 2832 saddr6 = NFSSOCKADDR(nd->nd_nam, struct sockaddr_in6 *); 2833 if (!(nd->nd_flag & ND_NFSV4) && 2834 ((saddr->sin_family == AF_INET && 2835 ntohs(saddr->sin_port) >= IPPORT_RESERVED) || 2836 (saddr6->sin6_family == AF_INET6 && 2837 ntohs(saddr6->sin6_port) >= IPPORT_RESERVED))) { 2838 vput(*vpp); 2839 nd->nd_repstat = (NFSERR_AUTHERR | AUTH_TOOWEAK); 2840 } 2841 } 2842 #endif /* NFS_REQRSVPORT */ 2843 2844 /* 2845 * Check/setup credentials. 2846 */ 2847 if (!nd->nd_repstat) { 2848 nd->nd_saveduid = nd->nd_cred->cr_uid; 2849 nd->nd_repstat = nfsd_excred(nd, exp, credanon); 2850 if (nd->nd_repstat) 2851 vput(*vpp); 2852 } 2853 if (credanon != NULL) 2854 crfree(credanon); 2855 if (nd->nd_repstat) { 2856 if (startwrite) 2857 vn_finished_write(mp); 2858 *vpp = NULL; 2859 if (mpp != NULL) 2860 *mpp = NULL; 2861 } 2862 2863 out: 2864 NFSEXITCODE2(0, nd); 2865 } 2866 2867 /* 2868 * glue for fp. 2869 */ 2870 static int 2871 fp_getfvp(struct thread *p, int fd, struct file **fpp, struct vnode **vpp) 2872 { 2873 struct filedesc *fdp; 2874 struct file *fp; 2875 int error = 0; 2876 2877 fdp = p->td_proc->p_fd; 2878 if (fd < 0 || fd >= fdp->fd_nfiles || 2879 (fp = fdp->fd_ofiles[fd].fde_file) == NULL) { 2880 error = EBADF; 2881 goto out; 2882 } 2883 *fpp = fp; 2884 2885 out: 2886 NFSEXITCODE(error); 2887 return (error); 2888 } 2889 2890 /* 2891 * Called from nfssvc() to update the exports list. Just call 2892 * vfs_export(). This has to be done, since the v4 root fake fs isn't 2893 * in the mount list. 2894 */ 2895 int 2896 nfsrv_v4rootexport(void *argp, struct ucred *cred, struct thread *p) 2897 { 2898 struct nfsex_args *nfsexargp = (struct nfsex_args *)argp; 2899 int error = 0; 2900 struct nameidata nd; 2901 fhandle_t fh; 2902 2903 error = vfs_export(&nfsv4root_mnt, &nfsexargp->export); 2904 if ((nfsexargp->export.ex_flags & MNT_DELEXPORT) != 0) 2905 nfs_rootfhset = 0; 2906 else if (error == 0) { 2907 if (nfsexargp->fspec == NULL) { 2908 error = EPERM; 2909 goto out; 2910 } 2911 /* 2912 * If fspec != NULL, this is the v4root path. 2913 */ 2914 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, 2915 nfsexargp->fspec, p); 2916 if ((error = namei(&nd)) != 0) 2917 goto out; 2918 error = nfsvno_getfh(nd.ni_vp, &fh, p); 2919 vrele(nd.ni_vp); 2920 if (!error) { 2921 nfs_rootfh.nfsrvfh_len = NFSX_MYFH; 2922 NFSBCOPY((caddr_t)&fh, 2923 nfs_rootfh.nfsrvfh_data, 2924 sizeof (fhandle_t)); 2925 nfs_rootfhset = 1; 2926 } 2927 } 2928 2929 out: 2930 NFSEXITCODE(error); 2931 return (error); 2932 } 2933 2934 /* 2935 * This function needs to test to see if the system is near its limit 2936 * for memory allocation via malloc() or mget() and return True iff 2937 * either of these resources are near their limit. 2938 * XXX (For now, this is just a stub.) 2939 */ 2940 int nfsrv_testmalloclimit = 0; 2941 int 2942 nfsrv_mallocmget_limit(void) 2943 { 2944 static int printmesg = 0; 2945 static int testval = 1; 2946 2947 if (nfsrv_testmalloclimit && (testval++ % 1000) == 0) { 2948 if ((printmesg++ % 100) == 0) 2949 printf("nfsd: malloc/mget near limit\n"); 2950 return (1); 2951 } 2952 return (0); 2953 } 2954 2955 /* 2956 * BSD specific initialization of a mount point. 2957 */ 2958 void 2959 nfsd_mntinit(void) 2960 { 2961 static int inited = 0; 2962 2963 if (inited) 2964 return; 2965 inited = 1; 2966 nfsv4root_mnt.mnt_flag = (MNT_RDONLY | MNT_EXPORTED); 2967 TAILQ_INIT(&nfsv4root_mnt.mnt_nvnodelist); 2968 TAILQ_INIT(&nfsv4root_mnt.mnt_activevnodelist); 2969 nfsv4root_mnt.mnt_export = NULL; 2970 TAILQ_INIT(&nfsv4root_opt); 2971 TAILQ_INIT(&nfsv4root_newopt); 2972 nfsv4root_mnt.mnt_opt = &nfsv4root_opt; 2973 nfsv4root_mnt.mnt_optnew = &nfsv4root_newopt; 2974 nfsv4root_mnt.mnt_nvnodelistsize = 0; 2975 nfsv4root_mnt.mnt_activevnodelistsize = 0; 2976 } 2977 2978 /* 2979 * Get a vnode for a file handle, without checking exports, etc. 2980 */ 2981 struct vnode * 2982 nfsvno_getvp(fhandle_t *fhp) 2983 { 2984 struct mount *mp; 2985 struct vnode *vp; 2986 int error; 2987 2988 mp = vfs_busyfs(&fhp->fh_fsid); 2989 if (mp == NULL) 2990 return (NULL); 2991 error = VFS_FHTOVP(mp, &fhp->fh_fid, LK_EXCLUSIVE, &vp); 2992 vfs_unbusy(mp); 2993 if (error) 2994 return (NULL); 2995 return (vp); 2996 } 2997 2998 /* 2999 * Do a local VOP_ADVLOCK(). 3000 */ 3001 int 3002 nfsvno_advlock(struct vnode *vp, int ftype, u_int64_t first, 3003 u_int64_t end, struct thread *td) 3004 { 3005 int error = 0; 3006 struct flock fl; 3007 u_int64_t tlen; 3008 3009 if (nfsrv_dolocallocks == 0) 3010 goto out; 3011 ASSERT_VOP_UNLOCKED(vp, "nfsvno_advlock: vp locked"); 3012 3013 fl.l_whence = SEEK_SET; 3014 fl.l_type = ftype; 3015 fl.l_start = (off_t)first; 3016 if (end == NFS64BITSSET) { 3017 fl.l_len = 0; 3018 } else { 3019 tlen = end - first; 3020 fl.l_len = (off_t)tlen; 3021 } 3022 /* 3023 * For FreeBSD8, the l_pid and l_sysid must be set to the same 3024 * values for all calls, so that all locks will be held by the 3025 * nfsd server. (The nfsd server handles conflicts between the 3026 * various clients.) 3027 * Since an NFSv4 lockowner is a ClientID plus an array of up to 1024 3028 * bytes, so it can't be put in l_sysid. 3029 */ 3030 if (nfsv4_sysid == 0) 3031 nfsv4_sysid = nlm_acquire_next_sysid(); 3032 fl.l_pid = (pid_t)0; 3033 fl.l_sysid = (int)nfsv4_sysid; 3034 3035 if (ftype == F_UNLCK) 3036 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_UNLCK, &fl, 3037 (F_POSIX | F_REMOTE)); 3038 else 3039 error = VOP_ADVLOCK(vp, (caddr_t)td->td_proc, F_SETLK, &fl, 3040 (F_POSIX | F_REMOTE)); 3041 3042 out: 3043 NFSEXITCODE(error); 3044 return (error); 3045 } 3046 3047 /* 3048 * Check the nfsv4 root exports. 3049 */ 3050 int 3051 nfsvno_v4rootexport(struct nfsrv_descript *nd) 3052 { 3053 struct ucred *credanon; 3054 int exflags, error = 0, numsecflavor, *secflavors, i; 3055 3056 error = vfs_stdcheckexp(&nfsv4root_mnt, nd->nd_nam, &exflags, 3057 &credanon, &numsecflavor, &secflavors); 3058 if (error) { 3059 error = NFSERR_PROGUNAVAIL; 3060 goto out; 3061 } 3062 if (credanon != NULL) 3063 crfree(credanon); 3064 for (i = 0; i < numsecflavor; i++) { 3065 if (secflavors[i] == AUTH_SYS) 3066 nd->nd_flag |= ND_EXAUTHSYS; 3067 else if (secflavors[i] == RPCSEC_GSS_KRB5) 3068 nd->nd_flag |= ND_EXGSS; 3069 else if (secflavors[i] == RPCSEC_GSS_KRB5I) 3070 nd->nd_flag |= ND_EXGSSINTEGRITY; 3071 else if (secflavors[i] == RPCSEC_GSS_KRB5P) 3072 nd->nd_flag |= ND_EXGSSPRIVACY; 3073 } 3074 3075 out: 3076 NFSEXITCODE(error); 3077 return (error); 3078 } 3079 3080 /* 3081 * Nfs server pseudo system call for the nfsd's 3082 */ 3083 /* 3084 * MPSAFE 3085 */ 3086 static int 3087 nfssvc_nfsd(struct thread *td, struct nfssvc_args *uap) 3088 { 3089 struct file *fp; 3090 struct nfsd_addsock_args sockarg; 3091 struct nfsd_nfsd_args nfsdarg; 3092 cap_rights_t rights; 3093 int error; 3094 3095 if (uap->flag & NFSSVC_NFSDADDSOCK) { 3096 error = copyin(uap->argp, (caddr_t)&sockarg, sizeof (sockarg)); 3097 if (error) 3098 goto out; 3099 /* 3100 * Since we don't know what rights might be required, 3101 * pretend that we need them all. It is better to be too 3102 * careful than too reckless. 3103 */ 3104 error = fget(td, sockarg.sock, 3105 cap_rights_init(&rights, CAP_SOCK_SERVER), &fp); 3106 if (error != 0) 3107 goto out; 3108 if (fp->f_type != DTYPE_SOCKET) { 3109 fdrop(fp, td); 3110 error = EPERM; 3111 goto out; 3112 } 3113 error = nfsrvd_addsock(fp); 3114 fdrop(fp, td); 3115 } else if (uap->flag & NFSSVC_NFSDNFSD) { 3116 if (uap->argp == NULL) { 3117 error = EINVAL; 3118 goto out; 3119 } 3120 error = copyin(uap->argp, (caddr_t)&nfsdarg, 3121 sizeof (nfsdarg)); 3122 if (error) 3123 goto out; 3124 error = nfsrvd_nfsd(td, &nfsdarg); 3125 } else { 3126 error = nfssvc_srvcall(td, uap, td->td_ucred); 3127 } 3128 3129 out: 3130 NFSEXITCODE(error); 3131 return (error); 3132 } 3133 3134 static int 3135 nfssvc_srvcall(struct thread *p, struct nfssvc_args *uap, struct ucred *cred) 3136 { 3137 struct nfsex_args export; 3138 struct file *fp = NULL; 3139 int stablefd, len; 3140 struct nfsd_clid adminrevoke; 3141 struct nfsd_dumplist dumplist; 3142 struct nfsd_dumpclients *dumpclients; 3143 struct nfsd_dumplocklist dumplocklist; 3144 struct nfsd_dumplocks *dumplocks; 3145 struct nameidata nd; 3146 vnode_t vp; 3147 int error = EINVAL, igotlock; 3148 struct proc *procp; 3149 static int suspend_nfsd = 0; 3150 3151 if (uap->flag & NFSSVC_PUBLICFH) { 3152 NFSBZERO((caddr_t)&nfs_pubfh.nfsrvfh_data, 3153 sizeof (fhandle_t)); 3154 error = copyin(uap->argp, 3155 &nfs_pubfh.nfsrvfh_data, sizeof (fhandle_t)); 3156 if (!error) 3157 nfs_pubfhset = 1; 3158 } else if (uap->flag & NFSSVC_V4ROOTEXPORT) { 3159 error = copyin(uap->argp,(caddr_t)&export, 3160 sizeof (struct nfsex_args)); 3161 if (!error) 3162 error = nfsrv_v4rootexport(&export, cred, p); 3163 } else if (uap->flag & NFSSVC_NOPUBLICFH) { 3164 nfs_pubfhset = 0; 3165 error = 0; 3166 } else if (uap->flag & NFSSVC_STABLERESTART) { 3167 error = copyin(uap->argp, (caddr_t)&stablefd, 3168 sizeof (int)); 3169 if (!error) 3170 error = fp_getfvp(p, stablefd, &fp, &vp); 3171 if (!error && (NFSFPFLAG(fp) & (FREAD | FWRITE)) != (FREAD | FWRITE)) 3172 error = EBADF; 3173 if (!error && newnfs_numnfsd != 0) 3174 error = EPERM; 3175 if (!error) { 3176 nfsrv_stablefirst.nsf_fp = fp; 3177 nfsrv_setupstable(p); 3178 } 3179 } else if (uap->flag & NFSSVC_ADMINREVOKE) { 3180 error = copyin(uap->argp, (caddr_t)&adminrevoke, 3181 sizeof (struct nfsd_clid)); 3182 if (!error) 3183 error = nfsrv_adminrevoke(&adminrevoke, p); 3184 } else if (uap->flag & NFSSVC_DUMPCLIENTS) { 3185 error = copyin(uap->argp, (caddr_t)&dumplist, 3186 sizeof (struct nfsd_dumplist)); 3187 if (!error && (dumplist.ndl_size < 1 || 3188 dumplist.ndl_size > NFSRV_MAXDUMPLIST)) 3189 error = EPERM; 3190 if (!error) { 3191 len = sizeof (struct nfsd_dumpclients) * dumplist.ndl_size; 3192 dumpclients = (struct nfsd_dumpclients *)malloc(len, 3193 M_TEMP, M_WAITOK); 3194 nfsrv_dumpclients(dumpclients, dumplist.ndl_size); 3195 error = copyout(dumpclients, 3196 CAST_USER_ADDR_T(dumplist.ndl_list), len); 3197 free(dumpclients, M_TEMP); 3198 } 3199 } else if (uap->flag & NFSSVC_DUMPLOCKS) { 3200 error = copyin(uap->argp, (caddr_t)&dumplocklist, 3201 sizeof (struct nfsd_dumplocklist)); 3202 if (!error && (dumplocklist.ndllck_size < 1 || 3203 dumplocklist.ndllck_size > NFSRV_MAXDUMPLIST)) 3204 error = EPERM; 3205 if (!error) 3206 error = nfsrv_lookupfilename(&nd, 3207 dumplocklist.ndllck_fname, p); 3208 if (!error) { 3209 len = sizeof (struct nfsd_dumplocks) * 3210 dumplocklist.ndllck_size; 3211 dumplocks = (struct nfsd_dumplocks *)malloc(len, 3212 M_TEMP, M_WAITOK); 3213 nfsrv_dumplocks(nd.ni_vp, dumplocks, 3214 dumplocklist.ndllck_size, p); 3215 vput(nd.ni_vp); 3216 error = copyout(dumplocks, 3217 CAST_USER_ADDR_T(dumplocklist.ndllck_list), len); 3218 free(dumplocks, M_TEMP); 3219 } 3220 } else if (uap->flag & NFSSVC_BACKUPSTABLE) { 3221 procp = p->td_proc; 3222 PROC_LOCK(procp); 3223 nfsd_master_pid = procp->p_pid; 3224 bcopy(procp->p_comm, nfsd_master_comm, MAXCOMLEN + 1); 3225 nfsd_master_start = procp->p_stats->p_start; 3226 nfsd_master_proc = procp; 3227 PROC_UNLOCK(procp); 3228 } else if ((uap->flag & NFSSVC_SUSPENDNFSD) != 0) { 3229 NFSLOCKV4ROOTMUTEX(); 3230 if (suspend_nfsd == 0) { 3231 /* Lock out all nfsd threads */ 3232 do { 3233 igotlock = nfsv4_lock(&nfsd_suspend_lock, 1, 3234 NULL, NFSV4ROOTLOCKMUTEXPTR, NULL); 3235 } while (igotlock == 0 && suspend_nfsd == 0); 3236 suspend_nfsd = 1; 3237 } 3238 NFSUNLOCKV4ROOTMUTEX(); 3239 error = 0; 3240 } else if ((uap->flag & NFSSVC_RESUMENFSD) != 0) { 3241 NFSLOCKV4ROOTMUTEX(); 3242 if (suspend_nfsd != 0) { 3243 nfsv4_unlock(&nfsd_suspend_lock, 0); 3244 suspend_nfsd = 0; 3245 } 3246 NFSUNLOCKV4ROOTMUTEX(); 3247 error = 0; 3248 } 3249 3250 NFSEXITCODE(error); 3251 return (error); 3252 } 3253 3254 /* 3255 * Check exports. 3256 * Returns 0 if ok, 1 otherwise. 3257 */ 3258 int 3259 nfsvno_testexp(struct nfsrv_descript *nd, struct nfsexstuff *exp) 3260 { 3261 int i; 3262 3263 /* 3264 * This seems odd, but allow the case where the security flavor 3265 * list is empty. This happens when NFSv4 is traversing non-exported 3266 * file systems. Exported file systems should always have a non-empty 3267 * security flavor list. 3268 */ 3269 if (exp->nes_numsecflavor == 0) 3270 return (0); 3271 3272 for (i = 0; i < exp->nes_numsecflavor; i++) { 3273 /* 3274 * The tests for privacy and integrity must be first, 3275 * since ND_GSS is set for everything but AUTH_SYS. 3276 */ 3277 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5P && 3278 (nd->nd_flag & ND_GSSPRIVACY)) 3279 return (0); 3280 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5I && 3281 (nd->nd_flag & ND_GSSINTEGRITY)) 3282 return (0); 3283 if (exp->nes_secflavors[i] == RPCSEC_GSS_KRB5 && 3284 (nd->nd_flag & ND_GSS)) 3285 return (0); 3286 if (exp->nes_secflavors[i] == AUTH_SYS && 3287 (nd->nd_flag & ND_GSS) == 0) 3288 return (0); 3289 } 3290 return (1); 3291 } 3292 3293 /* 3294 * Calculate a hash value for the fid in a file handle. 3295 */ 3296 uint32_t 3297 nfsrv_hashfh(fhandle_t *fhp) 3298 { 3299 uint32_t hashval; 3300 3301 hashval = hash32_buf(&fhp->fh_fid, sizeof(struct fid), 0); 3302 return (hashval); 3303 } 3304 3305 /* 3306 * Calculate a hash value for the sessionid. 3307 */ 3308 uint32_t 3309 nfsrv_hashsessionid(uint8_t *sessionid) 3310 { 3311 uint32_t hashval; 3312 3313 hashval = hash32_buf(sessionid, NFSX_V4SESSIONID, 0); 3314 return (hashval); 3315 } 3316 3317 /* 3318 * Signal the userland master nfsd to backup the stable restart file. 3319 */ 3320 void 3321 nfsrv_backupstable(void) 3322 { 3323 struct proc *procp; 3324 3325 if (nfsd_master_proc != NULL) { 3326 procp = pfind(nfsd_master_pid); 3327 /* Try to make sure it is the correct process. */ 3328 if (procp == nfsd_master_proc && 3329 procp->p_stats->p_start.tv_sec == 3330 nfsd_master_start.tv_sec && 3331 procp->p_stats->p_start.tv_usec == 3332 nfsd_master_start.tv_usec && 3333 strcmp(procp->p_comm, nfsd_master_comm) == 0) 3334 kern_psignal(procp, SIGUSR2); 3335 else 3336 nfsd_master_proc = NULL; 3337 3338 if (procp != NULL) 3339 PROC_UNLOCK(procp); 3340 } 3341 } 3342 3343 extern int (*nfsd_call_nfsd)(struct thread *, struct nfssvc_args *); 3344 3345 /* 3346 * Called once to initialize data structures... 3347 */ 3348 static int 3349 nfsd_modevent(module_t mod, int type, void *data) 3350 { 3351 int error = 0, i; 3352 static int loaded = 0; 3353 3354 switch (type) { 3355 case MOD_LOAD: 3356 if (loaded) 3357 goto out; 3358 newnfs_portinit(); 3359 for (i = 0; i < NFSRVCACHE_HASHSIZE; i++) { 3360 mtx_init(&nfsrchash_table[i].mtx, "nfsrtc", NULL, 3361 MTX_DEF); 3362 mtx_init(&nfsrcahash_table[i].mtx, "nfsrtca", NULL, 3363 MTX_DEF); 3364 } 3365 mtx_init(&nfsrc_udpmtx, "nfsuc", NULL, MTX_DEF); 3366 mtx_init(&nfs_v4root_mutex, "nfs4rt", NULL, MTX_DEF); 3367 mtx_init(&nfsv4root_mnt.mnt_mtx, "nfs4mnt", NULL, MTX_DEF); 3368 lockinit(&nfsv4root_mnt.mnt_explock, PVFS, "explock", 0, 0); 3369 nfsrvd_initcache(); 3370 nfsd_init(); 3371 NFSD_LOCK(); 3372 nfsrvd_init(0); 3373 NFSD_UNLOCK(); 3374 nfsd_mntinit(); 3375 #ifdef VV_DISABLEDELEG 3376 vn_deleg_ops.vndeleg_recall = nfsd_recalldelegation; 3377 vn_deleg_ops.vndeleg_disable = nfsd_disabledelegation; 3378 #endif 3379 nfsd_call_servertimer = nfsrv_servertimer; 3380 nfsd_call_nfsd = nfssvc_nfsd; 3381 loaded = 1; 3382 break; 3383 3384 case MOD_UNLOAD: 3385 if (newnfs_numnfsd != 0) { 3386 error = EBUSY; 3387 break; 3388 } 3389 3390 #ifdef VV_DISABLEDELEG 3391 vn_deleg_ops.vndeleg_recall = NULL; 3392 vn_deleg_ops.vndeleg_disable = NULL; 3393 #endif 3394 nfsd_call_servertimer = NULL; 3395 nfsd_call_nfsd = NULL; 3396 3397 /* Clean out all NFSv4 state. */ 3398 nfsrv_throwawayallstate(curthread); 3399 3400 /* Clean the NFS server reply cache */ 3401 nfsrvd_cleancache(); 3402 3403 /* Free up the krpc server pool. */ 3404 if (nfsrvd_pool != NULL) 3405 svcpool_destroy(nfsrvd_pool); 3406 3407 /* and get rid of the locks */ 3408 for (i = 0; i < NFSRVCACHE_HASHSIZE; i++) { 3409 mtx_destroy(&nfsrchash_table[i].mtx); 3410 mtx_destroy(&nfsrcahash_table[i].mtx); 3411 } 3412 mtx_destroy(&nfsrc_udpmtx); 3413 mtx_destroy(&nfs_v4root_mutex); 3414 mtx_destroy(&nfsv4root_mnt.mnt_mtx); 3415 for (i = 0; i < nfsrv_sessionhashsize; i++) 3416 mtx_destroy(&nfssessionhash[i].mtx); 3417 lockdestroy(&nfsv4root_mnt.mnt_explock); 3418 free(nfsclienthash, M_NFSDCLIENT); 3419 free(nfslockhash, M_NFSDLOCKFILE); 3420 free(nfssessionhash, M_NFSDSESSION); 3421 loaded = 0; 3422 break; 3423 default: 3424 error = EOPNOTSUPP; 3425 break; 3426 } 3427 3428 out: 3429 NFSEXITCODE(error); 3430 return (error); 3431 } 3432 static moduledata_t nfsd_mod = { 3433 "nfsd", 3434 nfsd_modevent, 3435 NULL, 3436 }; 3437 DECLARE_MODULE(nfsd, nfsd_mod, SI_SUB_VFS, SI_ORDER_ANY); 3438 3439 /* So that loader and kldload(2) can find us, wherever we are.. */ 3440 MODULE_VERSION(nfsd, 1); 3441 MODULE_DEPEND(nfsd, nfscommon, 1, 1, 1); 3442 MODULE_DEPEND(nfsd, nfslock, 1, 1, 1); 3443 MODULE_DEPEND(nfsd, nfslockd, 1, 1, 1); 3444 MODULE_DEPEND(nfsd, krpc, 1, 1, 1); 3445 MODULE_DEPEND(nfsd, nfssvc, 1, 1, 1); 3446 3447