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$"); 36 37 #include "opt_inet6.h" 38 39 #include <sys/capsicum.h> 40 41 /* 42 * generally, I don't like #includes inside .h files, but it seems to 43 * be the easiest way to handle the port. 44 */ 45 #include <sys/hash.h> 46 #include <fs/nfs/nfsport.h> 47 #include <netinet/if_ether.h> 48 #include <net/if_types.h> 49 50 #include <fs/nfsclient/nfs_kdtrace.h> 51 52 #ifdef KDTRACE_HOOKS 53 dtrace_nfsclient_attrcache_flush_probe_func_t 54 dtrace_nfscl_attrcache_flush_done_probe; 55 uint32_t nfscl_attrcache_flush_done_id; 56 57 dtrace_nfsclient_attrcache_get_hit_probe_func_t 58 dtrace_nfscl_attrcache_get_hit_probe; 59 uint32_t nfscl_attrcache_get_hit_id; 60 61 dtrace_nfsclient_attrcache_get_miss_probe_func_t 62 dtrace_nfscl_attrcache_get_miss_probe; 63 uint32_t nfscl_attrcache_get_miss_id; 64 65 dtrace_nfsclient_attrcache_load_probe_func_t 66 dtrace_nfscl_attrcache_load_done_probe; 67 uint32_t nfscl_attrcache_load_done_id; 68 #endif /* !KDTRACE_HOOKS */ 69 70 extern u_int32_t newnfs_true, newnfs_false, newnfs_xdrneg1; 71 extern struct vop_vector newnfs_vnodeops; 72 extern struct vop_vector newnfs_fifoops; 73 extern uma_zone_t newnfsnode_zone; 74 extern struct buf_ops buf_ops_newnfs; 75 extern int ncl_pbuf_freecnt; 76 extern short nfsv4_cbport; 77 extern int nfscl_enablecallb; 78 extern int nfs_numnfscbd; 79 extern int nfscl_inited; 80 struct mtx nfs_clstate_mutex; 81 struct mtx ncl_iod_mutex; 82 NFSDLOCKMUTEX; 83 84 extern void (*ncl_call_invalcaches)(struct vnode *); 85 86 /* 87 * Comparison function for vfs_hash functions. 88 */ 89 int 90 newnfs_vncmpf(struct vnode *vp, void *arg) 91 { 92 struct nfsfh *nfhp = (struct nfsfh *)arg; 93 struct nfsnode *np = VTONFS(vp); 94 95 if (np->n_fhp->nfh_len != nfhp->nfh_len || 96 NFSBCMP(np->n_fhp->nfh_fh, nfhp->nfh_fh, nfhp->nfh_len)) 97 return (1); 98 return (0); 99 } 100 101 /* 102 * Look up a vnode/nfsnode by file handle. 103 * Callers must check for mount points!! 104 * In all cases, a pointer to a 105 * nfsnode structure is returned. 106 * This variant takes a "struct nfsfh *" as second argument and uses 107 * that structure up, either by hanging off the nfsnode or FREEing it. 108 */ 109 int 110 nfscl_nget(struct mount *mntp, struct vnode *dvp, struct nfsfh *nfhp, 111 struct componentname *cnp, struct thread *td, struct nfsnode **npp, 112 void *stuff, int lkflags) 113 { 114 struct nfsnode *np, *dnp; 115 struct vnode *vp, *nvp; 116 struct nfsv4node *newd, *oldd; 117 int error; 118 u_int hash; 119 struct nfsmount *nmp; 120 121 nmp = VFSTONFS(mntp); 122 dnp = VTONFS(dvp); 123 *npp = NULL; 124 125 hash = fnv_32_buf(nfhp->nfh_fh, nfhp->nfh_len, FNV1_32_INIT); 126 127 error = vfs_hash_get(mntp, hash, lkflags, 128 td, &nvp, newnfs_vncmpf, nfhp); 129 if (error == 0 && nvp != NULL) { 130 /* 131 * I believe there is a slight chance that vgonel() could 132 * get called on this vnode between when NFSVOPLOCK() drops 133 * the VI_LOCK() and vget() acquires it again, so that it 134 * hasn't yet had v_usecount incremented. If this were to 135 * happen, the VI_DOOMED flag would be set, so check for 136 * that here. Since we now have the v_usecount incremented, 137 * we should be ok until we vrele() it, if the VI_DOOMED 138 * flag isn't set now. 139 */ 140 VI_LOCK(nvp); 141 if ((nvp->v_iflag & VI_DOOMED)) { 142 VI_UNLOCK(nvp); 143 vrele(nvp); 144 error = ENOENT; 145 } else { 146 VI_UNLOCK(nvp); 147 } 148 } 149 if (error) { 150 FREE((caddr_t)nfhp, M_NFSFH); 151 return (error); 152 } 153 if (nvp != NULL) { 154 np = VTONFS(nvp); 155 /* 156 * For NFSv4, check to see if it is the same name and 157 * replace the name, if it is different. 158 */ 159 oldd = newd = NULL; 160 if ((nmp->nm_flag & NFSMNT_NFSV4) && np->n_v4 != NULL && 161 nvp->v_type == VREG && 162 (np->n_v4->n4_namelen != cnp->cn_namelen || 163 NFSBCMP(cnp->cn_nameptr, NFS4NODENAME(np->n_v4), 164 cnp->cn_namelen) || 165 dnp->n_fhp->nfh_len != np->n_v4->n4_fhlen || 166 NFSBCMP(dnp->n_fhp->nfh_fh, np->n_v4->n4_data, 167 dnp->n_fhp->nfh_len))) { 168 MALLOC(newd, struct nfsv4node *, 169 sizeof (struct nfsv4node) + dnp->n_fhp->nfh_len + 170 + cnp->cn_namelen - 1, M_NFSV4NODE, M_WAITOK); 171 NFSLOCKNODE(np); 172 if (newd != NULL && np->n_v4 != NULL && nvp->v_type == VREG 173 && (np->n_v4->n4_namelen != cnp->cn_namelen || 174 NFSBCMP(cnp->cn_nameptr, NFS4NODENAME(np->n_v4), 175 cnp->cn_namelen) || 176 dnp->n_fhp->nfh_len != np->n_v4->n4_fhlen || 177 NFSBCMP(dnp->n_fhp->nfh_fh, np->n_v4->n4_data, 178 dnp->n_fhp->nfh_len))) { 179 oldd = np->n_v4; 180 np->n_v4 = newd; 181 newd = NULL; 182 np->n_v4->n4_fhlen = dnp->n_fhp->nfh_len; 183 np->n_v4->n4_namelen = cnp->cn_namelen; 184 NFSBCOPY(dnp->n_fhp->nfh_fh, np->n_v4->n4_data, 185 dnp->n_fhp->nfh_len); 186 NFSBCOPY(cnp->cn_nameptr, NFS4NODENAME(np->n_v4), 187 cnp->cn_namelen); 188 } 189 NFSUNLOCKNODE(np); 190 } 191 if (newd != NULL) 192 FREE((caddr_t)newd, M_NFSV4NODE); 193 if (oldd != NULL) 194 FREE((caddr_t)oldd, M_NFSV4NODE); 195 *npp = np; 196 FREE((caddr_t)nfhp, M_NFSFH); 197 return (0); 198 } 199 np = uma_zalloc(newnfsnode_zone, M_WAITOK | M_ZERO); 200 201 error = getnewvnode("newnfs", mntp, &newnfs_vnodeops, &nvp); 202 if (error) { 203 uma_zfree(newnfsnode_zone, np); 204 FREE((caddr_t)nfhp, M_NFSFH); 205 return (error); 206 } 207 vp = nvp; 208 KASSERT(vp->v_bufobj.bo_bsize != 0, ("nfscl_nget: bo_bsize == 0")); 209 vp->v_bufobj.bo_ops = &buf_ops_newnfs; 210 vp->v_data = np; 211 np->n_vnode = vp; 212 /* 213 * Initialize the mutex even if the vnode is going to be a loser. 214 * This simplifies the logic in reclaim, which can then unconditionally 215 * destroy the mutex (in the case of the loser, or if hash_insert 216 * happened to return an error no special casing is needed). 217 */ 218 mtx_init(&np->n_mtx, "NEWNFSnode lock", NULL, MTX_DEF | MTX_DUPOK); 219 220 /* 221 * Are we getting the root? If so, make sure the vnode flags 222 * are correct 223 */ 224 if ((nfhp->nfh_len == nmp->nm_fhsize) && 225 !bcmp(nfhp->nfh_fh, nmp->nm_fh, nfhp->nfh_len)) { 226 if (vp->v_type == VNON) 227 vp->v_type = VDIR; 228 vp->v_vflag |= VV_ROOT; 229 } 230 231 np->n_fhp = nfhp; 232 /* 233 * For NFSv4, we have to attach the directory file handle and 234 * file name, so that Open Ops can be done later. 235 */ 236 if (nmp->nm_flag & NFSMNT_NFSV4) { 237 MALLOC(np->n_v4, struct nfsv4node *, sizeof (struct nfsv4node) 238 + dnp->n_fhp->nfh_len + cnp->cn_namelen - 1, M_NFSV4NODE, 239 M_WAITOK); 240 np->n_v4->n4_fhlen = dnp->n_fhp->nfh_len; 241 np->n_v4->n4_namelen = cnp->cn_namelen; 242 NFSBCOPY(dnp->n_fhp->nfh_fh, np->n_v4->n4_data, 243 dnp->n_fhp->nfh_len); 244 NFSBCOPY(cnp->cn_nameptr, NFS4NODENAME(np->n_v4), 245 cnp->cn_namelen); 246 } else { 247 np->n_v4 = NULL; 248 } 249 250 /* 251 * NFS supports recursive and shared locking. 252 */ 253 lockmgr(vp->v_vnlock, LK_EXCLUSIVE | LK_NOWITNESS, NULL); 254 VN_LOCK_AREC(vp); 255 VN_LOCK_ASHARE(vp); 256 error = insmntque(vp, mntp); 257 if (error != 0) { 258 *npp = NULL; 259 mtx_destroy(&np->n_mtx); 260 FREE((caddr_t)nfhp, M_NFSFH); 261 if (np->n_v4 != NULL) 262 FREE((caddr_t)np->n_v4, M_NFSV4NODE); 263 uma_zfree(newnfsnode_zone, np); 264 return (error); 265 } 266 error = vfs_hash_insert(vp, hash, lkflags, 267 td, &nvp, newnfs_vncmpf, nfhp); 268 if (error) 269 return (error); 270 if (nvp != NULL) { 271 *npp = VTONFS(nvp); 272 /* vfs_hash_insert() vput()'s the losing vnode */ 273 return (0); 274 } 275 *npp = np; 276 277 return (0); 278 } 279 280 /* 281 * Anothe variant of nfs_nget(). This one is only used by reopen. It 282 * takes almost the same args as nfs_nget(), but only succeeds if an entry 283 * exists in the cache. (Since files should already be "open" with a 284 * vnode ref cnt on the node when reopen calls this, it should always 285 * succeed.) 286 * Also, don't get a vnode lock, since it may already be locked by some 287 * other process that is handling it. This is ok, since all other threads 288 * on the client are blocked by the nfsc_lock being exclusively held by the 289 * caller of this function. 290 */ 291 int 292 nfscl_ngetreopen(struct mount *mntp, u_int8_t *fhp, int fhsize, 293 struct thread *td, struct nfsnode **npp) 294 { 295 struct vnode *nvp; 296 u_int hash; 297 struct nfsfh *nfhp; 298 int error; 299 300 *npp = NULL; 301 /* For forced dismounts, just return error. */ 302 if ((mntp->mnt_kern_flag & MNTK_UNMOUNTF)) 303 return (EINTR); 304 MALLOC(nfhp, struct nfsfh *, sizeof (struct nfsfh) + fhsize, 305 M_NFSFH, M_WAITOK); 306 bcopy(fhp, &nfhp->nfh_fh[0], fhsize); 307 nfhp->nfh_len = fhsize; 308 309 hash = fnv_32_buf(fhp, fhsize, FNV1_32_INIT); 310 311 /* 312 * First, try to get the vnode locked, but don't block for the lock. 313 */ 314 error = vfs_hash_get(mntp, hash, (LK_EXCLUSIVE | LK_NOWAIT), td, &nvp, 315 newnfs_vncmpf, nfhp); 316 if (error == 0 && nvp != NULL) { 317 NFSVOPUNLOCK(nvp, 0); 318 } else if (error == EBUSY) { 319 /* 320 * The LK_EXCLOTHER lock type tells nfs_lock1() to not try 321 * and lock the vnode, but just get a v_usecount on it. 322 * LK_NOWAIT is set so that when vget() returns ENOENT, 323 * vfs_hash_get() fails instead of looping. 324 * If this succeeds, it is safe so long as a vflush() with 325 * FORCECLOSE has not been done. Since the Renew thread is 326 * stopped and the MNTK_UNMOUNTF flag is set before doing 327 * a vflush() with FORCECLOSE, we should be ok here. 328 */ 329 if ((mntp->mnt_kern_flag & MNTK_UNMOUNTF)) 330 error = EINTR; 331 else 332 error = vfs_hash_get(mntp, hash, 333 (LK_EXCLOTHER | LK_NOWAIT), td, &nvp, 334 newnfs_vncmpf, nfhp); 335 } 336 FREE(nfhp, M_NFSFH); 337 if (error) 338 return (error); 339 if (nvp != NULL) { 340 *npp = VTONFS(nvp); 341 return (0); 342 } 343 return (EINVAL); 344 } 345 346 /* 347 * Load the attribute cache (that lives in the nfsnode entry) with 348 * the attributes of the second argument and 349 * Iff vaper not NULL 350 * copy the attributes to *vaper 351 * Similar to nfs_loadattrcache(), except the attributes are passed in 352 * instead of being parsed out of the mbuf list. 353 */ 354 int 355 nfscl_loadattrcache(struct vnode **vpp, struct nfsvattr *nap, void *nvaper, 356 void *stuff, int writeattr, int dontshrink) 357 { 358 struct vnode *vp = *vpp; 359 struct vattr *vap, *nvap = &nap->na_vattr, *vaper = nvaper; 360 struct nfsnode *np; 361 struct nfsmount *nmp; 362 struct timespec mtime_save; 363 u_quad_t nsize; 364 int setnsize; 365 366 /* 367 * If v_type == VNON it is a new node, so fill in the v_type, 368 * n_mtime fields. Check to see if it represents a special 369 * device, and if so, check for a possible alias. Once the 370 * correct vnode has been obtained, fill in the rest of the 371 * information. 372 */ 373 np = VTONFS(vp); 374 NFSLOCKNODE(np); 375 if (vp->v_type != nvap->va_type) { 376 vp->v_type = nvap->va_type; 377 if (vp->v_type == VFIFO) 378 vp->v_op = &newnfs_fifoops; 379 np->n_mtime = nvap->va_mtime; 380 } 381 nmp = VFSTONFS(vp->v_mount); 382 vap = &np->n_vattr.na_vattr; 383 mtime_save = vap->va_mtime; 384 if (writeattr) { 385 np->n_vattr.na_filerev = nap->na_filerev; 386 np->n_vattr.na_size = nap->na_size; 387 np->n_vattr.na_mtime = nap->na_mtime; 388 np->n_vattr.na_ctime = nap->na_ctime; 389 np->n_vattr.na_fsid = nap->na_fsid; 390 np->n_vattr.na_mode = nap->na_mode; 391 } else { 392 NFSBCOPY((caddr_t)nap, (caddr_t)&np->n_vattr, 393 sizeof (struct nfsvattr)); 394 } 395 396 /* 397 * For NFSv4, if the node's fsid is not equal to the mount point's 398 * fsid, return the low order 32bits of the node's fsid. This 399 * allows getcwd(3) to work. There is a chance that the fsid might 400 * be the same as a local fs, but since this is in an NFS mount 401 * point, I don't think that will cause any problems? 402 */ 403 if (NFSHASNFSV4(nmp) && NFSHASHASSETFSID(nmp) && 404 (nmp->nm_fsid[0] != np->n_vattr.na_filesid[0] || 405 nmp->nm_fsid[1] != np->n_vattr.na_filesid[1])) { 406 /* 407 * va_fsid needs to be set to some value derived from 408 * np->n_vattr.na_filesid that is not equal 409 * vp->v_mount->mnt_stat.f_fsid[0], so that it changes 410 * from the value used for the top level server volume 411 * in the mounted subtree. 412 */ 413 if (vp->v_mount->mnt_stat.f_fsid.val[0] != 414 (uint32_t)np->n_vattr.na_filesid[0]) 415 vap->va_fsid = (uint32_t)np->n_vattr.na_filesid[0]; 416 else 417 vap->va_fsid = (uint32_t)hash32_buf( 418 np->n_vattr.na_filesid, 2 * sizeof(uint64_t), 0); 419 } else 420 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0]; 421 np->n_attrstamp = time_second; 422 setnsize = 0; 423 nsize = 0; 424 if (vap->va_size != np->n_size) { 425 if (vap->va_type == VREG) { 426 if (dontshrink && vap->va_size < np->n_size) { 427 /* 428 * We've been told not to shrink the file; 429 * zero np->n_attrstamp to indicate that 430 * the attributes are stale. 431 */ 432 vap->va_size = np->n_size; 433 np->n_attrstamp = 0; 434 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp); 435 vnode_pager_setsize(vp, np->n_size); 436 } else if (np->n_flag & NMODIFIED) { 437 /* 438 * We've modified the file: Use the larger 439 * of our size, and the server's size. 440 */ 441 if (vap->va_size < np->n_size) { 442 vap->va_size = np->n_size; 443 } else { 444 np->n_size = vap->va_size; 445 np->n_flag |= NSIZECHANGED; 446 } 447 vnode_pager_setsize(vp, np->n_size); 448 } else if (vap->va_size < np->n_size) { 449 /* 450 * When shrinking the size, the call to 451 * vnode_pager_setsize() cannot be done 452 * with the mutex held, so delay it until 453 * after the mtx_unlock call. 454 */ 455 nsize = np->n_size = vap->va_size; 456 np->n_flag |= NSIZECHANGED; 457 setnsize = 1; 458 } else { 459 np->n_size = vap->va_size; 460 np->n_flag |= NSIZECHANGED; 461 vnode_pager_setsize(vp, np->n_size); 462 } 463 } else { 464 np->n_size = vap->va_size; 465 } 466 } 467 /* 468 * The following checks are added to prevent a race between (say) 469 * a READDIR+ and a WRITE. 470 * READDIR+, WRITE requests sent out. 471 * READDIR+ resp, WRITE resp received on client. 472 * However, the WRITE resp was handled before the READDIR+ resp 473 * causing the post op attrs from the write to be loaded first 474 * and the attrs from the READDIR+ to be loaded later. If this 475 * happens, we have stale attrs loaded into the attrcache. 476 * We detect this by for the mtime moving back. We invalidate the 477 * attrcache when this happens. 478 */ 479 if (timespeccmp(&mtime_save, &vap->va_mtime, >)) { 480 /* Size changed or mtime went backwards */ 481 np->n_attrstamp = 0; 482 KDTRACE_NFS_ATTRCACHE_FLUSH_DONE(vp); 483 } 484 if (vaper != NULL) { 485 NFSBCOPY((caddr_t)vap, (caddr_t)vaper, sizeof(*vap)); 486 if (np->n_flag & NCHG) { 487 if (np->n_flag & NACC) 488 vaper->va_atime = np->n_atim; 489 if (np->n_flag & NUPD) 490 vaper->va_mtime = np->n_mtim; 491 } 492 } 493 #ifdef KDTRACE_HOOKS 494 if (np->n_attrstamp != 0) 495 KDTRACE_NFS_ATTRCACHE_LOAD_DONE(vp, vap, 0); 496 #endif 497 NFSUNLOCKNODE(np); 498 if (setnsize) 499 vnode_pager_setsize(vp, nsize); 500 return (0); 501 } 502 503 /* 504 * Fill in the client id name. For these bytes: 505 * 1 - they must be unique 506 * 2 - they should be persistent across client reboots 507 * 1 is more critical than 2 508 * Use the mount point's unique id plus either the uuid or, if that 509 * isn't set, random junk. 510 */ 511 void 512 nfscl_fillclid(u_int64_t clval, char *uuid, u_int8_t *cp, u_int16_t idlen) 513 { 514 int uuidlen; 515 516 /* 517 * First, put in the 64bit mount point identifier. 518 */ 519 if (idlen >= sizeof (u_int64_t)) { 520 NFSBCOPY((caddr_t)&clval, cp, sizeof (u_int64_t)); 521 cp += sizeof (u_int64_t); 522 idlen -= sizeof (u_int64_t); 523 } 524 525 /* 526 * If uuid is non-zero length, use it. 527 */ 528 uuidlen = strlen(uuid); 529 if (uuidlen > 0 && idlen >= uuidlen) { 530 NFSBCOPY(uuid, cp, uuidlen); 531 cp += uuidlen; 532 idlen -= uuidlen; 533 } 534 535 /* 536 * This only normally happens if the uuid isn't set. 537 */ 538 while (idlen > 0) { 539 *cp++ = (u_int8_t)(arc4random() % 256); 540 idlen--; 541 } 542 } 543 544 /* 545 * Fill in a lock owner name. For now, pid + the process's creation time. 546 */ 547 void 548 nfscl_filllockowner(void *id, u_int8_t *cp, int flags) 549 { 550 union { 551 u_int32_t lval; 552 u_int8_t cval[4]; 553 } tl; 554 struct proc *p; 555 556 if (id == NULL) { 557 printf("NULL id\n"); 558 bzero(cp, NFSV4CL_LOCKNAMELEN); 559 return; 560 } 561 if ((flags & F_POSIX) != 0) { 562 p = (struct proc *)id; 563 tl.lval = p->p_pid; 564 *cp++ = tl.cval[0]; 565 *cp++ = tl.cval[1]; 566 *cp++ = tl.cval[2]; 567 *cp++ = tl.cval[3]; 568 tl.lval = p->p_stats->p_start.tv_sec; 569 *cp++ = tl.cval[0]; 570 *cp++ = tl.cval[1]; 571 *cp++ = tl.cval[2]; 572 *cp++ = tl.cval[3]; 573 tl.lval = p->p_stats->p_start.tv_usec; 574 *cp++ = tl.cval[0]; 575 *cp++ = tl.cval[1]; 576 *cp++ = tl.cval[2]; 577 *cp = tl.cval[3]; 578 } else if ((flags & F_FLOCK) != 0) { 579 bcopy(&id, cp, sizeof(id)); 580 bzero(&cp[sizeof(id)], NFSV4CL_LOCKNAMELEN - sizeof(id)); 581 } else { 582 printf("nfscl_filllockowner: not F_POSIX or F_FLOCK\n"); 583 bzero(cp, NFSV4CL_LOCKNAMELEN); 584 } 585 } 586 587 /* 588 * Find the parent process for the thread passed in as an argument. 589 * If none exists, return NULL, otherwise return a thread for the parent. 590 * (Can be any of the threads, since it is only used for td->td_proc.) 591 */ 592 NFSPROC_T * 593 nfscl_getparent(struct thread *td) 594 { 595 struct proc *p; 596 struct thread *ptd; 597 598 if (td == NULL) 599 return (NULL); 600 p = td->td_proc; 601 if (p->p_pid == 0) 602 return (NULL); 603 p = p->p_pptr; 604 if (p == NULL) 605 return (NULL); 606 ptd = TAILQ_FIRST(&p->p_threads); 607 return (ptd); 608 } 609 610 /* 611 * Start up the renew kernel thread. 612 */ 613 static void 614 start_nfscl(void *arg) 615 { 616 struct nfsclclient *clp; 617 struct thread *td; 618 619 clp = (struct nfsclclient *)arg; 620 td = TAILQ_FIRST(&clp->nfsc_renewthread->p_threads); 621 nfscl_renewthread(clp, td); 622 kproc_exit(0); 623 } 624 625 void 626 nfscl_start_renewthread(struct nfsclclient *clp) 627 { 628 629 kproc_create(start_nfscl, (void *)clp, &clp->nfsc_renewthread, 0, 0, 630 "nfscl"); 631 } 632 633 /* 634 * Handle wcc_data. 635 * For NFSv4, it assumes that nfsv4_wccattr() was used to set up the getattr 636 * as the first Op after PutFH. 637 * (For NFSv4, the postop attributes are after the Op, so they can't be 638 * parsed here. A separate call to nfscl_postop_attr() is required.) 639 */ 640 int 641 nfscl_wcc_data(struct nfsrv_descript *nd, struct vnode *vp, 642 struct nfsvattr *nap, int *flagp, int *wccflagp, void *stuff) 643 { 644 u_int32_t *tl; 645 struct nfsnode *np = VTONFS(vp); 646 struct nfsvattr nfsva; 647 int error = 0; 648 649 if (wccflagp != NULL) 650 *wccflagp = 0; 651 if (nd->nd_flag & ND_NFSV3) { 652 *flagp = 0; 653 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 654 if (*tl == newnfs_true) { 655 NFSM_DISSECT(tl, u_int32_t *, 6 * NFSX_UNSIGNED); 656 if (wccflagp != NULL) { 657 mtx_lock(&np->n_mtx); 658 *wccflagp = (np->n_mtime.tv_sec == 659 fxdr_unsigned(u_int32_t, *(tl + 2)) && 660 np->n_mtime.tv_nsec == 661 fxdr_unsigned(u_int32_t, *(tl + 3))); 662 mtx_unlock(&np->n_mtx); 663 } 664 } 665 error = nfscl_postop_attr(nd, nap, flagp, stuff); 666 } else if ((nd->nd_flag & (ND_NOMOREDATA | ND_NFSV4 | ND_V4WCCATTR)) 667 == (ND_NFSV4 | ND_V4WCCATTR)) { 668 error = nfsv4_loadattr(nd, NULL, &nfsva, NULL, 669 NULL, 0, NULL, NULL, NULL, NULL, NULL, 0, 670 NULL, NULL, NULL, NULL, NULL); 671 if (error) 672 return (error); 673 /* 674 * Get rid of Op# and status for next op. 675 */ 676 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 677 if (*++tl) 678 nd->nd_flag |= ND_NOMOREDATA; 679 if (wccflagp != NULL && 680 nfsva.na_vattr.va_mtime.tv_sec != 0) { 681 mtx_lock(&np->n_mtx); 682 *wccflagp = (np->n_mtime.tv_sec == 683 nfsva.na_vattr.va_mtime.tv_sec && 684 np->n_mtime.tv_nsec == 685 nfsva.na_vattr.va_mtime.tv_sec); 686 mtx_unlock(&np->n_mtx); 687 } 688 } 689 nfsmout: 690 return (error); 691 } 692 693 /* 694 * Get postop attributes. 695 */ 696 int 697 nfscl_postop_attr(struct nfsrv_descript *nd, struct nfsvattr *nap, int *retp, 698 void *stuff) 699 { 700 u_int32_t *tl; 701 int error = 0; 702 703 *retp = 0; 704 if (nd->nd_flag & ND_NOMOREDATA) 705 return (error); 706 if (nd->nd_flag & ND_NFSV3) { 707 NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED); 708 *retp = fxdr_unsigned(int, *tl); 709 } else if (nd->nd_flag & ND_NFSV4) { 710 /* 711 * For NFSv4, the postop attr are at the end, so no point 712 * in looking if nd_repstat != 0. 713 */ 714 if (!nd->nd_repstat) { 715 NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 716 if (*(tl + 1)) 717 /* should never happen since nd_repstat != 0 */ 718 nd->nd_flag |= ND_NOMOREDATA; 719 else 720 *retp = 1; 721 } 722 } else if (!nd->nd_repstat) { 723 /* For NFSv2, the attributes are here iff nd_repstat == 0 */ 724 *retp = 1; 725 } 726 if (*retp) { 727 error = nfsm_loadattr(nd, nap); 728 if (error) 729 *retp = 0; 730 } 731 nfsmout: 732 return (error); 733 } 734 735 /* 736 * Fill in the setable attributes. The full argument indicates whether 737 * to fill in them all or just mode and time. 738 */ 739 void 740 nfscl_fillsattr(struct nfsrv_descript *nd, struct vattr *vap, 741 struct vnode *vp, int flags, u_int32_t rdev) 742 { 743 u_int32_t *tl; 744 struct nfsv2_sattr *sp; 745 nfsattrbit_t attrbits; 746 747 switch (nd->nd_flag & (ND_NFSV2 | ND_NFSV3 | ND_NFSV4)) { 748 case ND_NFSV2: 749 NFSM_BUILD(sp, struct nfsv2_sattr *, NFSX_V2SATTR); 750 if (vap->va_mode == (mode_t)VNOVAL) 751 sp->sa_mode = newnfs_xdrneg1; 752 else 753 sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode); 754 if (vap->va_uid == (uid_t)VNOVAL) 755 sp->sa_uid = newnfs_xdrneg1; 756 else 757 sp->sa_uid = txdr_unsigned(vap->va_uid); 758 if (vap->va_gid == (gid_t)VNOVAL) 759 sp->sa_gid = newnfs_xdrneg1; 760 else 761 sp->sa_gid = txdr_unsigned(vap->va_gid); 762 if (flags & NFSSATTR_SIZE0) 763 sp->sa_size = 0; 764 else if (flags & NFSSATTR_SIZENEG1) 765 sp->sa_size = newnfs_xdrneg1; 766 else if (flags & NFSSATTR_SIZERDEV) 767 sp->sa_size = txdr_unsigned(rdev); 768 else 769 sp->sa_size = txdr_unsigned(vap->va_size); 770 txdr_nfsv2time(&vap->va_atime, &sp->sa_atime); 771 txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime); 772 break; 773 case ND_NFSV3: 774 if (vap->va_mode != (mode_t)VNOVAL) { 775 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 776 *tl++ = newnfs_true; 777 *tl = txdr_unsigned(vap->va_mode); 778 } else { 779 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 780 *tl = newnfs_false; 781 } 782 if ((flags & NFSSATTR_FULL) && vap->va_uid != (uid_t)VNOVAL) { 783 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 784 *tl++ = newnfs_true; 785 *tl = txdr_unsigned(vap->va_uid); 786 } else { 787 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 788 *tl = newnfs_false; 789 } 790 if ((flags & NFSSATTR_FULL) && vap->va_gid != (gid_t)VNOVAL) { 791 NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED); 792 *tl++ = newnfs_true; 793 *tl = txdr_unsigned(vap->va_gid); 794 } else { 795 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 796 *tl = newnfs_false; 797 } 798 if ((flags & NFSSATTR_FULL) && vap->va_size != VNOVAL) { 799 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 800 *tl++ = newnfs_true; 801 txdr_hyper(vap->va_size, tl); 802 } else { 803 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 804 *tl = newnfs_false; 805 } 806 if (vap->va_atime.tv_sec != VNOVAL) { 807 if ((vap->va_vaflags & VA_UTIMES_NULL) == 0) { 808 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 809 *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT); 810 txdr_nfsv3time(&vap->va_atime, tl); 811 } else { 812 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 813 *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER); 814 } 815 } else { 816 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 817 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE); 818 } 819 if (vap->va_mtime.tv_sec != VNOVAL) { 820 if ((vap->va_vaflags & VA_UTIMES_NULL) == 0) { 821 NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED); 822 *tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT); 823 txdr_nfsv3time(&vap->va_mtime, tl); 824 } else { 825 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 826 *tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER); 827 } 828 } else { 829 NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED); 830 *tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE); 831 } 832 break; 833 case ND_NFSV4: 834 NFSZERO_ATTRBIT(&attrbits); 835 if (vap->va_mode != (mode_t)VNOVAL) 836 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_MODE); 837 if ((flags & NFSSATTR_FULL) && vap->va_uid != (uid_t)VNOVAL) 838 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_OWNER); 839 if ((flags & NFSSATTR_FULL) && vap->va_gid != (gid_t)VNOVAL) 840 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_OWNERGROUP); 841 if ((flags & NFSSATTR_FULL) && vap->va_size != VNOVAL) 842 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE); 843 if (vap->va_atime.tv_sec != VNOVAL) 844 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESSSET); 845 if (vap->va_mtime.tv_sec != VNOVAL) 846 NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFYSET); 847 (void) nfsv4_fillattr(nd, vp->v_mount, vp, NULL, vap, NULL, 0, 848 &attrbits, NULL, NULL, 0, 0, 0, 0, (uint64_t)0); 849 break; 850 }; 851 } 852 853 /* 854 * nfscl_request() - mostly a wrapper for newnfs_request(). 855 */ 856 int 857 nfscl_request(struct nfsrv_descript *nd, struct vnode *vp, NFSPROC_T *p, 858 struct ucred *cred, void *stuff) 859 { 860 int ret, vers; 861 struct nfsmount *nmp; 862 863 nmp = VFSTONFS(vp->v_mount); 864 if (nd->nd_flag & ND_NFSV4) 865 vers = NFS_VER4; 866 else if (nd->nd_flag & ND_NFSV3) 867 vers = NFS_VER3; 868 else 869 vers = NFS_VER2; 870 ret = newnfs_request(nd, nmp, NULL, &nmp->nm_sockreq, vp, p, cred, 871 NFS_PROG, vers, NULL, 1, NULL, NULL); 872 return (ret); 873 } 874 875 /* 876 * fill in this bsden's variant of statfs using nfsstatfs. 877 */ 878 void 879 nfscl_loadsbinfo(struct nfsmount *nmp, struct nfsstatfs *sfp, void *statfs) 880 { 881 struct statfs *sbp = (struct statfs *)statfs; 882 883 if (nmp->nm_flag & (NFSMNT_NFSV3 | NFSMNT_NFSV4)) { 884 sbp->f_bsize = NFS_FABLKSIZE; 885 sbp->f_blocks = sfp->sf_tbytes / NFS_FABLKSIZE; 886 sbp->f_bfree = sfp->sf_fbytes / NFS_FABLKSIZE; 887 /* 888 * Although sf_abytes is uint64_t and f_bavail is int64_t, 889 * the value after dividing by NFS_FABLKSIZE is small 890 * enough that it will fit in 63bits, so it is ok to 891 * assign it to f_bavail without fear that it will become 892 * negative. 893 */ 894 sbp->f_bavail = sfp->sf_abytes / NFS_FABLKSIZE; 895 sbp->f_files = sfp->sf_tfiles; 896 /* Since f_ffree is int64_t, clip it to 63bits. */ 897 if (sfp->sf_ffiles > INT64_MAX) 898 sbp->f_ffree = INT64_MAX; 899 else 900 sbp->f_ffree = sfp->sf_ffiles; 901 } else if ((nmp->nm_flag & NFSMNT_NFSV4) == 0) { 902 /* 903 * The type casts to (int32_t) ensure that this code is 904 * compatible with the old NFS client, in that it will 905 * propagate bit31 to the high order bits. This may or may 906 * not be correct for NFSv2, but since it is a legacy 907 * environment, I'd rather retain backwards compatibility. 908 */ 909 sbp->f_bsize = (int32_t)sfp->sf_bsize; 910 sbp->f_blocks = (int32_t)sfp->sf_blocks; 911 sbp->f_bfree = (int32_t)sfp->sf_bfree; 912 sbp->f_bavail = (int32_t)sfp->sf_bavail; 913 sbp->f_files = 0; 914 sbp->f_ffree = 0; 915 } 916 } 917 918 /* 919 * Use the fsinfo stuff to update the mount point. 920 */ 921 void 922 nfscl_loadfsinfo(struct nfsmount *nmp, struct nfsfsinfo *fsp) 923 { 924 925 if ((nmp->nm_wsize == 0 || fsp->fs_wtpref < nmp->nm_wsize) && 926 fsp->fs_wtpref >= NFS_FABLKSIZE) 927 nmp->nm_wsize = (fsp->fs_wtpref + NFS_FABLKSIZE - 1) & 928 ~(NFS_FABLKSIZE - 1); 929 if (fsp->fs_wtmax < nmp->nm_wsize && fsp->fs_wtmax > 0) { 930 nmp->nm_wsize = fsp->fs_wtmax & ~(NFS_FABLKSIZE - 1); 931 if (nmp->nm_wsize == 0) 932 nmp->nm_wsize = fsp->fs_wtmax; 933 } 934 if (nmp->nm_wsize < NFS_FABLKSIZE) 935 nmp->nm_wsize = NFS_FABLKSIZE; 936 if ((nmp->nm_rsize == 0 || fsp->fs_rtpref < nmp->nm_rsize) && 937 fsp->fs_rtpref >= NFS_FABLKSIZE) 938 nmp->nm_rsize = (fsp->fs_rtpref + NFS_FABLKSIZE - 1) & 939 ~(NFS_FABLKSIZE - 1); 940 if (fsp->fs_rtmax < nmp->nm_rsize && fsp->fs_rtmax > 0) { 941 nmp->nm_rsize = fsp->fs_rtmax & ~(NFS_FABLKSIZE - 1); 942 if (nmp->nm_rsize == 0) 943 nmp->nm_rsize = fsp->fs_rtmax; 944 } 945 if (nmp->nm_rsize < NFS_FABLKSIZE) 946 nmp->nm_rsize = NFS_FABLKSIZE; 947 if ((nmp->nm_readdirsize == 0 || fsp->fs_dtpref < nmp->nm_readdirsize) 948 && fsp->fs_dtpref >= NFS_DIRBLKSIZ) 949 nmp->nm_readdirsize = (fsp->fs_dtpref + NFS_DIRBLKSIZ - 1) & 950 ~(NFS_DIRBLKSIZ - 1); 951 if (fsp->fs_rtmax < nmp->nm_readdirsize && fsp->fs_rtmax > 0) { 952 nmp->nm_readdirsize = fsp->fs_rtmax & ~(NFS_DIRBLKSIZ - 1); 953 if (nmp->nm_readdirsize == 0) 954 nmp->nm_readdirsize = fsp->fs_rtmax; 955 } 956 if (nmp->nm_readdirsize < NFS_DIRBLKSIZ) 957 nmp->nm_readdirsize = NFS_DIRBLKSIZ; 958 if (fsp->fs_maxfilesize > 0 && 959 fsp->fs_maxfilesize < nmp->nm_maxfilesize) 960 nmp->nm_maxfilesize = fsp->fs_maxfilesize; 961 nmp->nm_mountp->mnt_stat.f_iosize = newnfs_iosize(nmp); 962 nmp->nm_state |= NFSSTA_GOTFSINFO; 963 } 964 965 /* 966 * Get a pointer to my IP addrress and return it. 967 * Return NULL if you can't find one. 968 */ 969 u_int8_t * 970 nfscl_getmyip(struct nfsmount *nmp, int *isinet6p) 971 { 972 struct sockaddr_in sad, *sin; 973 struct rtentry *rt; 974 u_int8_t *retp = NULL; 975 static struct in_addr laddr; 976 977 *isinet6p = 0; 978 /* 979 * Loop up a route for the destination address. 980 */ 981 if (nmp->nm_nam->sa_family == AF_INET) { 982 bzero(&sad, sizeof (sad)); 983 sin = (struct sockaddr_in *)nmp->nm_nam; 984 sad.sin_family = AF_INET; 985 sad.sin_len = sizeof (struct sockaddr_in); 986 sad.sin_addr.s_addr = sin->sin_addr.s_addr; 987 CURVNET_SET(CRED_TO_VNET(nmp->nm_sockreq.nr_cred)); 988 rt = rtalloc1_fib((struct sockaddr *)&sad, 0, 0UL, 989 curthread->td_proc->p_fibnum); 990 if (rt != NULL) { 991 if (rt->rt_ifp != NULL && 992 rt->rt_ifa != NULL && 993 ((rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) && 994 rt->rt_ifa->ifa_addr->sa_family == AF_INET) { 995 sin = (struct sockaddr_in *) 996 rt->rt_ifa->ifa_addr; 997 laddr.s_addr = sin->sin_addr.s_addr; 998 retp = (u_int8_t *)&laddr; 999 } 1000 RTFREE_LOCKED(rt); 1001 } 1002 CURVNET_RESTORE(); 1003 #ifdef INET6 1004 } else if (nmp->nm_nam->sa_family == AF_INET6) { 1005 struct sockaddr_in6 sad6, *sin6; 1006 static struct in6_addr laddr6; 1007 1008 bzero(&sad6, sizeof (sad6)); 1009 sin6 = (struct sockaddr_in6 *)nmp->nm_nam; 1010 sad6.sin6_family = AF_INET6; 1011 sad6.sin6_len = sizeof (struct sockaddr_in6); 1012 sad6.sin6_addr = sin6->sin6_addr; 1013 CURVNET_SET(CRED_TO_VNET(nmp->nm_sockreq.nr_cred)); 1014 rt = rtalloc1_fib((struct sockaddr *)&sad6, 0, 0UL, 1015 curthread->td_proc->p_fibnum); 1016 if (rt != NULL) { 1017 if (rt->rt_ifp != NULL && 1018 rt->rt_ifa != NULL && 1019 ((rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) && 1020 rt->rt_ifa->ifa_addr->sa_family == AF_INET6) { 1021 sin6 = (struct sockaddr_in6 *) 1022 rt->rt_ifa->ifa_addr; 1023 laddr6 = sin6->sin6_addr; 1024 retp = (u_int8_t *)&laddr6; 1025 *isinet6p = 1; 1026 } 1027 RTFREE_LOCKED(rt); 1028 } 1029 CURVNET_RESTORE(); 1030 #endif 1031 } 1032 return (retp); 1033 } 1034 1035 /* 1036 * Copy NFS uid, gids from the cred structure. 1037 */ 1038 void 1039 newnfs_copyincred(struct ucred *cr, struct nfscred *nfscr) 1040 { 1041 int i; 1042 1043 KASSERT(cr->cr_ngroups >= 0, 1044 ("newnfs_copyincred: negative cr_ngroups")); 1045 nfscr->nfsc_uid = cr->cr_uid; 1046 nfscr->nfsc_ngroups = MIN(cr->cr_ngroups, NFS_MAXGRPS + 1); 1047 for (i = 0; i < nfscr->nfsc_ngroups; i++) 1048 nfscr->nfsc_groups[i] = cr->cr_groups[i]; 1049 } 1050 1051 1052 /* 1053 * Do any client specific initialization. 1054 */ 1055 void 1056 nfscl_init(void) 1057 { 1058 static int inited = 0; 1059 1060 if (inited) 1061 return; 1062 inited = 1; 1063 nfscl_inited = 1; 1064 ncl_pbuf_freecnt = nswbuf / 2 + 1; 1065 } 1066 1067 /* 1068 * Check each of the attributes to be set, to ensure they aren't already 1069 * the correct value. Disable setting ones already correct. 1070 */ 1071 int 1072 nfscl_checksattr(struct vattr *vap, struct nfsvattr *nvap) 1073 { 1074 1075 if (vap->va_mode != (mode_t)VNOVAL) { 1076 if (vap->va_mode == nvap->na_mode) 1077 vap->va_mode = (mode_t)VNOVAL; 1078 } 1079 if (vap->va_uid != (uid_t)VNOVAL) { 1080 if (vap->va_uid == nvap->na_uid) 1081 vap->va_uid = (uid_t)VNOVAL; 1082 } 1083 if (vap->va_gid != (gid_t)VNOVAL) { 1084 if (vap->va_gid == nvap->na_gid) 1085 vap->va_gid = (gid_t)VNOVAL; 1086 } 1087 if (vap->va_size != VNOVAL) { 1088 if (vap->va_size == nvap->na_size) 1089 vap->va_size = VNOVAL; 1090 } 1091 1092 /* 1093 * We are normally called with only a partially initialized 1094 * VAP. Since the NFSv3 spec says that server may use the 1095 * file attributes to store the verifier, the spec requires 1096 * us to do a SETATTR RPC. FreeBSD servers store the verifier 1097 * in atime, but we can't really assume that all servers will 1098 * so we ensure that our SETATTR sets both atime and mtime. 1099 */ 1100 if (vap->va_mtime.tv_sec == VNOVAL) 1101 vfs_timestamp(&vap->va_mtime); 1102 if (vap->va_atime.tv_sec == VNOVAL) 1103 vap->va_atime = vap->va_mtime; 1104 return (1); 1105 } 1106 1107 /* 1108 * Map nfsv4 errors to errno.h errors. 1109 * The uid and gid arguments are only used for NFSERR_BADOWNER and that 1110 * error should only be returned for the Open, Create and Setattr Ops. 1111 * As such, most calls can just pass in 0 for those arguments. 1112 */ 1113 APPLESTATIC int 1114 nfscl_maperr(struct thread *td, int error, uid_t uid, gid_t gid) 1115 { 1116 struct proc *p; 1117 1118 if (error < 10000) 1119 return (error); 1120 if (td != NULL) 1121 p = td->td_proc; 1122 else 1123 p = NULL; 1124 switch (error) { 1125 case NFSERR_BADOWNER: 1126 tprintf(p, LOG_INFO, 1127 "No name and/or group mapping for uid,gid:(%d,%d)\n", 1128 uid, gid); 1129 return (EPERM); 1130 case NFSERR_BADNAME: 1131 case NFSERR_BADCHAR: 1132 printf("nfsv4 char/name not handled by server\n"); 1133 return (ENOENT); 1134 case NFSERR_STALECLIENTID: 1135 case NFSERR_STALESTATEID: 1136 case NFSERR_EXPIRED: 1137 case NFSERR_BADSTATEID: 1138 case NFSERR_BADSESSION: 1139 printf("nfsv4 recover err returned %d\n", error); 1140 return (EIO); 1141 case NFSERR_BADHANDLE: 1142 case NFSERR_SERVERFAULT: 1143 case NFSERR_BADTYPE: 1144 case NFSERR_FHEXPIRED: 1145 case NFSERR_RESOURCE: 1146 case NFSERR_MOVED: 1147 case NFSERR_NOFILEHANDLE: 1148 case NFSERR_MINORVERMISMATCH: 1149 case NFSERR_OLDSTATEID: 1150 case NFSERR_BADSEQID: 1151 case NFSERR_LEASEMOVED: 1152 case NFSERR_RECLAIMBAD: 1153 case NFSERR_BADXDR: 1154 case NFSERR_OPILLEGAL: 1155 printf("nfsv4 client/server protocol prob err=%d\n", 1156 error); 1157 return (EIO); 1158 default: 1159 tprintf(p, LOG_INFO, "nfsv4 err=%d\n", error); 1160 return (EIO); 1161 }; 1162 } 1163 1164 /* 1165 * Check to see if the process for this owner exists. Return 1 if it doesn't 1166 * and 0 otherwise. 1167 */ 1168 int 1169 nfscl_procdoesntexist(u_int8_t *own) 1170 { 1171 union { 1172 u_int32_t lval; 1173 u_int8_t cval[4]; 1174 } tl; 1175 struct proc *p; 1176 pid_t pid; 1177 int ret = 0; 1178 1179 tl.cval[0] = *own++; 1180 tl.cval[1] = *own++; 1181 tl.cval[2] = *own++; 1182 tl.cval[3] = *own++; 1183 pid = tl.lval; 1184 p = pfind_locked(pid); 1185 if (p == NULL) 1186 return (1); 1187 if (p->p_stats == NULL) { 1188 PROC_UNLOCK(p); 1189 return (0); 1190 } 1191 tl.cval[0] = *own++; 1192 tl.cval[1] = *own++; 1193 tl.cval[2] = *own++; 1194 tl.cval[3] = *own++; 1195 if (tl.lval != p->p_stats->p_start.tv_sec) { 1196 ret = 1; 1197 } else { 1198 tl.cval[0] = *own++; 1199 tl.cval[1] = *own++; 1200 tl.cval[2] = *own++; 1201 tl.cval[3] = *own; 1202 if (tl.lval != p->p_stats->p_start.tv_usec) 1203 ret = 1; 1204 } 1205 PROC_UNLOCK(p); 1206 return (ret); 1207 } 1208 1209 /* 1210 * - nfs pseudo system call for the client 1211 */ 1212 /* 1213 * MPSAFE 1214 */ 1215 static int 1216 nfssvc_nfscl(struct thread *td, struct nfssvc_args *uap) 1217 { 1218 struct file *fp; 1219 struct nfscbd_args nfscbdarg; 1220 struct nfsd_nfscbd_args nfscbdarg2; 1221 struct nameidata nd; 1222 struct nfscl_dumpmntopts dumpmntopts; 1223 cap_rights_t rights; 1224 char *buf; 1225 int error; 1226 1227 if (uap->flag & NFSSVC_CBADDSOCK) { 1228 error = copyin(uap->argp, (caddr_t)&nfscbdarg, sizeof(nfscbdarg)); 1229 if (error) 1230 return (error); 1231 /* 1232 * Since we don't know what rights might be required, 1233 * pretend that we need them all. It is better to be too 1234 * careful than too reckless. 1235 */ 1236 error = fget(td, nfscbdarg.sock, 1237 cap_rights_init(&rights, CAP_SOCK_CLIENT), &fp); 1238 if (error) 1239 return (error); 1240 if (fp->f_type != DTYPE_SOCKET) { 1241 fdrop(fp, td); 1242 return (EPERM); 1243 } 1244 error = nfscbd_addsock(fp); 1245 fdrop(fp, td); 1246 if (!error && nfscl_enablecallb == 0) { 1247 nfsv4_cbport = nfscbdarg.port; 1248 nfscl_enablecallb = 1; 1249 } 1250 } else if (uap->flag & NFSSVC_NFSCBD) { 1251 if (uap->argp == NULL) 1252 return (EINVAL); 1253 error = copyin(uap->argp, (caddr_t)&nfscbdarg2, 1254 sizeof(nfscbdarg2)); 1255 if (error) 1256 return (error); 1257 error = nfscbd_nfsd(td, &nfscbdarg2); 1258 } else if (uap->flag & NFSSVC_DUMPMNTOPTS) { 1259 error = copyin(uap->argp, &dumpmntopts, sizeof(dumpmntopts)); 1260 if (error == 0 && (dumpmntopts.ndmnt_blen < 256 || 1261 dumpmntopts.ndmnt_blen > 1024)) 1262 error = EINVAL; 1263 if (error == 0) 1264 error = nfsrv_lookupfilename(&nd, 1265 dumpmntopts.ndmnt_fname, td); 1266 if (error == 0 && strcmp(nd.ni_vp->v_mount->mnt_vfc->vfc_name, 1267 "nfs") != 0) { 1268 vput(nd.ni_vp); 1269 error = EINVAL; 1270 } 1271 if (error == 0) { 1272 buf = malloc(dumpmntopts.ndmnt_blen, M_TEMP, M_WAITOK); 1273 nfscl_retopts(VFSTONFS(nd.ni_vp->v_mount), buf, 1274 dumpmntopts.ndmnt_blen); 1275 vput(nd.ni_vp); 1276 error = copyout(buf, dumpmntopts.ndmnt_buf, 1277 dumpmntopts.ndmnt_blen); 1278 free(buf, M_TEMP); 1279 } 1280 } else { 1281 error = EINVAL; 1282 } 1283 return (error); 1284 } 1285 1286 extern int (*nfsd_call_nfscl)(struct thread *, struct nfssvc_args *); 1287 1288 /* 1289 * Called once to initialize data structures... 1290 */ 1291 static int 1292 nfscl_modevent(module_t mod, int type, void *data) 1293 { 1294 int error = 0; 1295 static int loaded = 0; 1296 1297 switch (type) { 1298 case MOD_LOAD: 1299 if (loaded) 1300 return (0); 1301 newnfs_portinit(); 1302 mtx_init(&nfs_clstate_mutex, "nfs_clstate_mutex", NULL, 1303 MTX_DEF); 1304 mtx_init(&ncl_iod_mutex, "ncl_iod_mutex", NULL, MTX_DEF); 1305 nfscl_init(); 1306 NFSD_LOCK(); 1307 nfsrvd_cbinit(0); 1308 NFSD_UNLOCK(); 1309 ncl_call_invalcaches = ncl_invalcaches; 1310 nfsd_call_nfscl = nfssvc_nfscl; 1311 loaded = 1; 1312 break; 1313 1314 case MOD_UNLOAD: 1315 if (nfs_numnfscbd != 0) { 1316 error = EBUSY; 1317 break; 1318 } 1319 1320 /* 1321 * XXX: Unloading of nfscl module is unsupported. 1322 */ 1323 #if 0 1324 ncl_call_invalcaches = NULL; 1325 nfsd_call_nfscl = NULL; 1326 /* and get rid of the mutexes */ 1327 mtx_destroy(&nfs_clstate_mutex); 1328 mtx_destroy(&ncl_iod_mutex); 1329 loaded = 0; 1330 break; 1331 #else 1332 /* FALLTHROUGH */ 1333 #endif 1334 default: 1335 error = EOPNOTSUPP; 1336 break; 1337 } 1338 return error; 1339 } 1340 static moduledata_t nfscl_mod = { 1341 "nfscl", 1342 nfscl_modevent, 1343 NULL, 1344 }; 1345 DECLARE_MODULE(nfscl, nfscl_mod, SI_SUB_VFS, SI_ORDER_FIRST); 1346 1347 /* So that loader and kldload(2) can find us, wherever we are.. */ 1348 MODULE_VERSION(nfscl, 1); 1349 MODULE_DEPEND(nfscl, nfscommon, 1, 1, 1); 1350 MODULE_DEPEND(nfscl, krpc, 1, 1, 1); 1351 MODULE_DEPEND(nfscl, nfssvc, 1, 1, 1); 1352 MODULE_DEPEND(nfscl, nfslock, 1, 1, 1); 1353 1354