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